1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022, Linaro Limited
6 #define LOG_CATEGORY UCLASS_FWU_MDATA
10 #include <efi_loader.h>
12 #include <fwu_mdata.h>
18 #include <dm/device-internal.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
27 static int gpt_get_mdata_partitions(struct blk_desc *desc,
32 efi_guid_t part_type_guid;
33 struct disk_partition info;
34 const efi_guid_t fwu_mdata_guid = FWU_MDATA_GUID;
37 for (i = 1; i < MAX_SEARCH_PARTITIONS; i++) {
38 if (part_get_info(desc, i, &info))
40 uuid_str_to_bin(info.type_guid, part_type_guid.b,
41 UUID_STR_FORMAT_GUID);
43 if (!guidcmp(&fwu_mdata_guid, &part_type_guid)) {
45 mdata_parts[nparts] = i;
51 log_debug("Expect two copies of the FWU metadata instead of %d\n",
61 static int gpt_get_mdata_disk_part(struct blk_desc *desc,
62 struct disk_partition *info,
66 char *mdata_guid_str = "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23";
68 ret = part_get_info(desc, part_num, info);
70 log_debug("Unable to get the partition info for the FWU metadata part %d\n",
75 /* Check that it is indeed the FWU metadata partition */
76 if (!strncmp(info->type_guid, mdata_guid_str, UUID_STR_LEN))
82 static int gpt_read_write_mdata(struct blk_desc *desc,
83 struct fwu_mdata *mdata,
84 u8 access, u32 part_num)
87 u32 len, blk_start, blkcnt;
88 struct disk_partition info;
90 ALLOC_CACHE_ALIGN_BUFFER_PAD(struct fwu_mdata, mdata_aligned, 1,
96 ret = gpt_get_mdata_disk_part(desc, &info, part_num);
98 printf("Unable to get the FWU metadata partition\n");
102 len = sizeof(*mdata);
103 blkcnt = BLOCK_CNT(len, desc);
104 if (blkcnt > info.size) {
105 log_debug("Block count exceeds FWU metadata partition size\n");
109 blk_start = info.start;
110 if (access == MDATA_READ) {
111 if (blk_dread(desc, blk_start, blkcnt, mdata_aligned) != blkcnt) {
112 log_debug("Error reading FWU metadata from the device\n");
115 memcpy(mdata, mdata_aligned, sizeof(struct fwu_mdata));
117 if (blk_dwrite(desc, blk_start, blkcnt, mdata) != blkcnt) {
118 log_debug("Error writing FWU metadata to the device\n");
126 static int fwu_gpt_update_mdata(struct udevice *dev, struct fwu_mdata *mdata)
129 struct blk_desc *desc;
131 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
133 desc = dev_get_uclass_plat(priv->blk_dev);
135 ret = gpt_get_mdata_partitions(desc, mdata_parts);
137 log_debug("Error getting the FWU metadata partitions\n");
141 /* First write the primary partition */
142 ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, mdata_parts[0]);
144 log_debug("Updating primary FWU metadata partition failed\n");
148 /* And now the replica */
149 ret = gpt_read_write_mdata(desc, mdata, MDATA_WRITE, mdata_parts[1]);
151 log_debug("Updating secondary FWU metadata partition failed\n");
158 static int gpt_get_mdata(struct blk_desc *desc, struct fwu_mdata *mdata)
163 ret = gpt_get_mdata_partitions(desc, mdata_parts);
166 log_debug("Error getting the FWU metadata partitions\n");
170 ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, mdata_parts[0]);
172 log_debug("Failed to read the FWU metadata from the device\n");
176 ret = fwu_verify_mdata(mdata, 1);
181 * Verification of the primary FWU metadata copy failed.
182 * Try to read the replica.
184 memset(mdata, '\0', sizeof(struct fwu_mdata));
185 ret = gpt_read_write_mdata(desc, mdata, MDATA_READ, mdata_parts[1]);
187 log_debug("Failed to read the FWU metadata from the device\n");
191 ret = fwu_verify_mdata(mdata, 0);
195 /* Both the FWU metadata copies are corrupted. */
199 static int fwu_gpt_get_mdata(struct udevice *dev, struct fwu_mdata *mdata)
201 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
203 return gpt_get_mdata(dev_get_uclass_plat(priv->blk_dev), mdata);
206 static int fwu_gpt_get_mdata_partitions(struct udevice *dev, uint *mdata_parts)
208 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
210 return gpt_get_mdata_partitions(dev_get_uclass_plat(priv->blk_dev),
214 static int fwu_gpt_read_mdata_partition(struct udevice *dev,
215 struct fwu_mdata *mdata, uint part_num)
217 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
219 return gpt_read_write_mdata(dev_get_uclass_plat(priv->blk_dev),
220 mdata, MDATA_READ, part_num);
223 static int fwu_gpt_write_mdata_partition(struct udevice *dev,
224 struct fwu_mdata *mdata, uint part_num)
226 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
228 return gpt_read_write_mdata(dev_get_uclass_plat(priv->blk_dev),
229 mdata, MDATA_WRITE, part_num);
232 static int fwu_get_mdata_device(struct udevice *dev, struct udevice **mdata_dev)
236 struct udevice *parent;
237 const fdt32_t *phandle_p = NULL;
239 phandle_p = dev_read_prop(dev, "fwu-mdata-store", &size);
241 log_debug("fwu-mdata-store property not found\n");
245 phandle = fdt32_to_cpu(*phandle_p);
247 ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
252 return blk_get_from_parent(parent, mdata_dev);
255 static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
258 struct udevice *mdata_dev = NULL;
259 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
261 ret = fwu_get_mdata_device(dev, &mdata_dev);
265 priv->blk_dev = mdata_dev;
270 static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
271 .get_mdata = fwu_gpt_get_mdata,
272 .update_mdata = fwu_gpt_update_mdata,
273 .get_mdata_part_num = fwu_gpt_get_mdata_partitions,
274 .read_mdata_partition = fwu_gpt_read_mdata_partition,
275 .write_mdata_partition = fwu_gpt_write_mdata_partition,
278 static const struct udevice_id fwu_mdata_ids[] = {
279 { .compatible = "u-boot,fwu-mdata-gpt" },
283 U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
284 .name = "fwu-mdata-gpt-blk",
285 .id = UCLASS_FWU_MDATA,
286 .of_match = fwu_mdata_ids,
287 .ops = &fwu_gpt_blk_ops,
288 .probe = fwu_mdata_gpt_blk_probe,
289 .priv_auto = sizeof(struct fwu_mdata_gpt_blk_priv),