blob: c7284916c4e05c9b05d29921fb0143866721c3e6 [file] [log] [blame]
Sughosh Ganuee062b52022-10-21 18:15:56 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2022, Linaro Limited
4 */
5
6#define LOG_CATEGORY UCLASS_FWU_MDATA
7
8#include <blk.h>
9#include <dm.h>
10#include <efi_loader.h>
11#include <fwu.h>
12#include <fwu_mdata.h>
13#include <log.h>
14#include <memalign.h>
15#include <part.h>
16#include <part_efi.h>
17
18#include <dm/device-internal.h>
19#include <linux/errno.h>
20#include <linux/types.h>
21
22enum {
23 MDATA_READ = 1,
24 MDATA_WRITE,
25};
26
Jassi Braradfaad22023-03-06 17:18:21 -060027static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */
28
29static int gpt_get_mdata_partitions(struct blk_desc *desc)
Sughosh Ganuee062b52022-10-21 18:15:56 +053030{
Jassi Brara1df12f2023-03-06 17:18:41 -060031 int i;
Sughosh Ganuee062b52022-10-21 18:15:56 +053032 u32 nparts;
33 efi_guid_t part_type_guid;
34 struct disk_partition info;
35 const efi_guid_t fwu_mdata_guid = FWU_MDATA_GUID;
36
Jassi Braradfaad22023-03-06 17:18:21 -060037 /* if primary and secondary partitions already found */
38 if (g_mdata_part[0] && g_mdata_part[1])
39 return 0;
40
Sughosh Ganuee062b52022-10-21 18:15:56 +053041 nparts = 0;
Jassi Braradfaad22023-03-06 17:18:21 -060042 for (i = 1; i < MAX_SEARCH_PARTITIONS && nparts < 2; i++) {
Sughosh Ganuee062b52022-10-21 18:15:56 +053043 if (part_get_info(desc, i, &info))
44 continue;
45 uuid_str_to_bin(info.type_guid, part_type_guid.b,
46 UUID_STR_FORMAT_GUID);
47
Jassi Braradfaad22023-03-06 17:18:21 -060048 if (!guidcmp(&fwu_mdata_guid, &part_type_guid))
49 g_mdata_part[nparts++] = i;
Sughosh Ganuee062b52022-10-21 18:15:56 +053050 }
51
52 if (nparts != 2) {
53 log_debug("Expect two copies of the FWU metadata instead of %d\n",
54 nparts);
Jassi Brara1df12f2023-03-06 17:18:41 -060055 g_mdata_part[0] = 0;
56 g_mdata_part[1] = 0;
57 return -EINVAL;
Sughosh Ganuee062b52022-10-21 18:15:56 +053058 }
59
Jassi Brara1df12f2023-03-06 17:18:41 -060060 return 0;
Sughosh Ganuee062b52022-10-21 18:15:56 +053061}
62
63static int gpt_get_mdata_disk_part(struct blk_desc *desc,
64 struct disk_partition *info,
65 u32 part_num)
66{
67 int ret;
68 char *mdata_guid_str = "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23";
69
70 ret = part_get_info(desc, part_num, info);
71 if (ret < 0) {
72 log_debug("Unable to get the partition info for the FWU metadata part %d\n",
73 part_num);
74 return -ENOENT;
75 }
76
77 /* Check that it is indeed the FWU metadata partition */
78 if (!strncmp(info->type_guid, mdata_guid_str, UUID_STR_LEN))
79 return 0;
80
81 return -ENOENT;
82}
83
84static int gpt_read_write_mdata(struct blk_desc *desc,
85 struct fwu_mdata *mdata,
86 u8 access, u32 part_num)
87{
88 int ret;
89 u32 len, blk_start, blkcnt;
90 struct disk_partition info;
91
92 ALLOC_CACHE_ALIGN_BUFFER_PAD(struct fwu_mdata, mdata_aligned, 1,
93 desc->blksz);
94
95 if (!mdata)
96 return -ENOMEM;
97
98 ret = gpt_get_mdata_disk_part(desc, &info, part_num);
99 if (ret < 0) {
100 printf("Unable to get the FWU metadata partition\n");
101 return -ENOENT;
102 }
103
104 len = sizeof(*mdata);
105 blkcnt = BLOCK_CNT(len, desc);
106 if (blkcnt > info.size) {
107 log_debug("Block count exceeds FWU metadata partition size\n");
108 return -ERANGE;
109 }
110
111 blk_start = info.start;
112 if (access == MDATA_READ) {
113 if (blk_dread(desc, blk_start, blkcnt, mdata_aligned) != blkcnt) {
114 log_debug("Error reading FWU metadata from the device\n");
115 return -EIO;
116 }
117 memcpy(mdata, mdata_aligned, sizeof(struct fwu_mdata));
118 } else {
119 if (blk_dwrite(desc, blk_start, blkcnt, mdata) != blkcnt) {
120 log_debug("Error writing FWU metadata to the device\n");
121 return -EIO;
122 }
123 }
124
Sughosh Ganuee062b52022-10-21 18:15:56 +0530125 return 0;
126}
127
Sughosh Ganuee062b52022-10-21 18:15:56 +0530128static int fwu_get_mdata_device(struct udevice *dev, struct udevice **mdata_dev)
129{
130 u32 phandle;
131 int ret, size;
132 struct udevice *parent;
133 const fdt32_t *phandle_p = NULL;
134
135 phandle_p = dev_read_prop(dev, "fwu-mdata-store", &size);
136 if (!phandle_p) {
137 log_debug("fwu-mdata-store property not found\n");
138 return -ENOENT;
139 }
140
141 phandle = fdt32_to_cpu(*phandle_p);
142
143 ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
144 &parent);
145 if (ret)
146 return ret;
147
148 return blk_get_from_parent(parent, mdata_dev);
149}
150
151static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
152{
153 int ret;
154 struct udevice *mdata_dev = NULL;
155 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
156
157 ret = fwu_get_mdata_device(dev, &mdata_dev);
158 if (ret)
159 return ret;
160
161 priv->blk_dev = mdata_dev;
162
163 return 0;
164}
165
Jassi Brar1acc3c82023-03-06 17:18:35 -0600166static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
167 bool primary)
168{
169 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
170 struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
171 int ret;
172
173 ret = gpt_get_mdata_partitions(desc);
174 if (ret < 0) {
175 log_debug("Error getting the FWU metadata partitions\n");
176 return -ENOENT;
177 }
178
179 return gpt_read_write_mdata(desc, mdata, MDATA_READ,
180 primary ? g_mdata_part[0] : g_mdata_part[1]);
181}
182
183static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
184 bool primary)
185{
186 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
187 struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
188 int ret;
189
190 ret = gpt_get_mdata_partitions(desc);
191 if (ret < 0) {
192 log_debug("Error getting the FWU metadata partitions\n");
193 return -ENOENT;
194 }
195
196 return gpt_read_write_mdata(desc, mdata, MDATA_WRITE,
197 primary ? g_mdata_part[0] : g_mdata_part[1]);
198}
199
Sughosh Ganuee062b52022-10-21 18:15:56 +0530200static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
Jassi Brar1acc3c82023-03-06 17:18:35 -0600201 .read_mdata = fwu_gpt_read_mdata,
202 .write_mdata = fwu_gpt_write_mdata,
Sughosh Ganuee062b52022-10-21 18:15:56 +0530203};
204
205static const struct udevice_id fwu_mdata_ids[] = {
206 { .compatible = "u-boot,fwu-mdata-gpt" },
207 { }
208};
209
210U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
211 .name = "fwu-mdata-gpt-blk",
212 .id = UCLASS_FWU_MDATA,
213 .of_match = fwu_mdata_ids,
214 .ops = &fwu_gpt_blk_ops,
215 .probe = fwu_mdata_gpt_blk_probe,
216 .priv_auto = sizeof(struct fwu_mdata_gpt_blk_priv),
217};