1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2023, Linaro Limited
16 #include <dm/ofnode.h>
18 struct fwu_mtd_image_info
19 fwu_mtd_images[CONFIG_FWU_NUM_BANKS * CONFIG_FWU_NUM_IMAGES_PER_BANK];
21 static struct fwu_mtd_image_info *mtd_img_by_uuid(const char *uuidbuf)
23 int num_images = ARRAY_SIZE(fwu_mtd_images);
25 for (int i = 0; i < num_images; i++)
26 if (!strcmp(uuidbuf, fwu_mtd_images[i].uuidbuf))
27 return &fwu_mtd_images[i];
32 int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num,
35 struct fwu_mtd_image_info *mtd_img_info;
36 char uuidbuf[UUID_STR_LEN + 1];
37 fdt_addr_t offset, size = 0;
38 struct dfu_entity *dfu;
43 uuid_bin_to_str(image_id->b, uuidbuf, UUID_STR_FORMAT_STD);
45 mtd_img_info = mtd_img_by_uuid(uuidbuf);
47 log_err("%s: Not found partition for image %s\n", __func__, uuidbuf);
51 offset = mtd_img_info->start;
52 size = mtd_img_info->size;
54 ret = dfu_init_env_entities(NULL, NULL);
59 list_for_each_entry(dfu, &dfu_list, list)
63 log_warning("No entities in dfu_alt_info\n");
69 for (i = 0; i < nalt; i++) {
70 dfu = dfu_get_entity(i);
72 /* Only MTD RAW access */
73 if (!dfu || dfu->dev_type != DFU_DEV_MTD ||
74 dfu->layout != DFU_RAW_ADDR ||
75 dfu->data.mtd.start != offset ||
76 dfu->data.mtd.size != size)
86 log_debug("%s: %s -> %d\n", __func__, uuidbuf, *alt_num);
91 * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform
93 * @image_id: Image GUID for which DFU alt number needs to be retrieved
94 * @alt_num: Pointer to the alt_num
96 * Get the DFU alt number from the platform for the image specified by the
99 * Note: This is a weak function and platforms can override this with
100 * their own implementation for obtaining the alt number value.
102 * Return: 0 if OK, -ve on error
104 __weak int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_id,
107 return fwu_mtd_get_alt_num(image_id, alt_num, "nor1");
110 static int gen_image_alt_info(char *buf, size_t len, int sidx,
111 struct fwu_image_entry *img, struct mtd_info *mtd)
113 char *p = buf, *end = buf + len;
116 p += snprintf(p, end - p, "mtd %s", mtd->name);
118 log_err("%s:%d Run out of buffer\n", __func__, __LINE__);
123 * List the image banks in the FWU mdata and search the corresponding
124 * partition based on partition's uuid.
126 for (i = 0; i < CONFIG_FWU_NUM_BANKS; i++) {
127 struct fwu_mtd_image_info *mtd_img_info;
128 struct fwu_image_bank_info *bank;
129 char uuidbuf[UUID_STR_LEN + 1];
132 /* Query a partition by image UUID */
133 bank = &img->img_bank_info[i];
134 uuid_bin_to_str(bank->image_uuid.b, uuidbuf, UUID_STR_FORMAT_STD);
136 mtd_img_info = mtd_img_by_uuid(uuidbuf);
138 log_err("%s: Not found partition for image %s\n", __func__, uuidbuf);
142 offset = mtd_img_info->start;
143 size = mtd_img_info->size;
145 p += snprintf(p, end - p, "%sbank%d raw %x %x",
146 i == 0 ? "=" : ";", i, offset, size);
148 log_err("%s:%d Run out of buffer\n", __func__, __LINE__);
153 if (i == CONFIG_FWU_NUM_BANKS)
159 int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd)
161 struct fwu_mdata mdata;
164 ret = fwu_get_mdata(&mdata);
166 log_err("Failed to get the FWU mdata.\n");
170 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
171 ret = gen_image_alt_info(buf, len, i * CONFIG_FWU_NUM_BANKS,
172 &mdata.img_entry[i], mtd);
177 /* Replace the last ';' with '&' if there is another image. */
178 if (i != CONFIG_FWU_NUM_IMAGES_PER_BANK - 1 && l)