Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <dm.h> |
| 7 | #include <dfu.h> |
| 8 | #include <fwu.h> |
| 9 | #include <fwu_mdata.h> |
| 10 | #include <log.h> |
| 11 | #include <malloc.h> |
| 12 | #include <mtd.h> |
| 13 | #include <uuid.h> |
Raymond Mao | 24da831 | 2024-05-16 14:11:52 -0700 | [diff] [blame] | 14 | #include <stdio.h> |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 15 | |
| 16 | #include <dm/ofnode.h> |
| 17 | |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 18 | static struct fwu_mtd_image_info *mtd_img_by_uuid(const char *uuidbuf) |
| 19 | { |
Sughosh Ganu | 3d7ffcf | 2024-03-22 16:27:25 +0530 | [diff] [blame] | 20 | int num_images; |
| 21 | struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(fwu_get_dev()); |
| 22 | struct fwu_mtd_image_info *image_info = mtd_priv->fwu_mtd_images; |
| 23 | |
| 24 | if (!image_info) |
| 25 | return NULL; |
| 26 | |
| 27 | num_images = CONFIG_FWU_NUM_BANKS * |
| 28 | CONFIG_FWU_NUM_IMAGES_PER_BANK; |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 29 | |
| 30 | for (int i = 0; i < num_images; i++) |
Sughosh Ganu | 3d7ffcf | 2024-03-22 16:27:25 +0530 | [diff] [blame] | 31 | if (!strcmp(uuidbuf, image_info[i].uuidbuf)) |
| 32 | return &image_info[i]; |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num, |
| 38 | const char *mtd_dev) |
| 39 | { |
| 40 | struct fwu_mtd_image_info *mtd_img_info; |
| 41 | char uuidbuf[UUID_STR_LEN + 1]; |
| 42 | fdt_addr_t offset, size = 0; |
| 43 | struct dfu_entity *dfu; |
| 44 | int i, nalt, ret; |
| 45 | |
| 46 | mtd_probe_devices(); |
| 47 | |
| 48 | uuid_bin_to_str(image_id->b, uuidbuf, UUID_STR_FORMAT_STD); |
| 49 | |
| 50 | mtd_img_info = mtd_img_by_uuid(uuidbuf); |
| 51 | if (!mtd_img_info) { |
| 52 | log_err("%s: Not found partition for image %s\n", __func__, uuidbuf); |
| 53 | return -ENOENT; |
| 54 | } |
| 55 | |
| 56 | offset = mtd_img_info->start; |
| 57 | size = mtd_img_info->size; |
| 58 | |
| 59 | ret = dfu_init_env_entities(NULL, NULL); |
| 60 | if (ret) |
| 61 | return -ENOENT; |
| 62 | |
| 63 | nalt = 0; |
| 64 | list_for_each_entry(dfu, &dfu_list, list) |
| 65 | nalt++; |
| 66 | |
| 67 | if (!nalt) { |
| 68 | log_warning("No entities in dfu_alt_info\n"); |
| 69 | dfu_free_entities(); |
| 70 | return -ENOENT; |
| 71 | } |
| 72 | |
| 73 | ret = -ENOENT; |
| 74 | for (i = 0; i < nalt; i++) { |
| 75 | dfu = dfu_get_entity(i); |
| 76 | |
| 77 | /* Only MTD RAW access */ |
| 78 | if (!dfu || dfu->dev_type != DFU_DEV_MTD || |
| 79 | dfu->layout != DFU_RAW_ADDR || |
| 80 | dfu->data.mtd.start != offset || |
| 81 | dfu->data.mtd.size != size) |
| 82 | continue; |
| 83 | |
| 84 | *alt_num = dfu->alt; |
| 85 | ret = 0; |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | dfu_free_entities(); |
| 90 | |
| 91 | log_debug("%s: %s -> %d\n", __func__, uuidbuf, *alt_num); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * fwu_plat_get_alt_num() - Get the DFU Alt Num for the image from the platform |
| 97 | * @dev: FWU device |
| 98 | * @image_id: Image GUID for which DFU alt number needs to be retrieved |
| 99 | * @alt_num: Pointer to the alt_num |
| 100 | * |
| 101 | * Get the DFU alt number from the platform for the image specified by the |
| 102 | * image GUID. |
| 103 | * |
| 104 | * Note: This is a weak function and platforms can override this with |
| 105 | * their own implementation for obtaining the alt number value. |
| 106 | * |
| 107 | * Return: 0 if OK, -ve on error |
| 108 | */ |
| 109 | __weak int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_id, |
| 110 | u8 *alt_num) |
| 111 | { |
| 112 | return fwu_mtd_get_alt_num(image_id, alt_num, "nor1"); |
| 113 | } |
| 114 | |
Sughosh Ganu | 5db141c | 2024-03-22 16:27:24 +0530 | [diff] [blame] | 115 | static int gen_image_alt_info(char *buf, size_t len, |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 116 | struct fwu_image_entry *img, struct mtd_info *mtd) |
| 117 | { |
| 118 | char *p = buf, *end = buf + len; |
| 119 | int i; |
| 120 | |
| 121 | p += snprintf(p, end - p, "mtd %s", mtd->name); |
| 122 | if (end < p) { |
| 123 | log_err("%s:%d Run out of buffer\n", __func__, __LINE__); |
| 124 | return -E2BIG; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * List the image banks in the FWU mdata and search the corresponding |
| 129 | * partition based on partition's uuid. |
| 130 | */ |
| 131 | for (i = 0; i < CONFIG_FWU_NUM_BANKS; i++) { |
| 132 | struct fwu_mtd_image_info *mtd_img_info; |
| 133 | struct fwu_image_bank_info *bank; |
| 134 | char uuidbuf[UUID_STR_LEN + 1]; |
| 135 | u32 offset, size; |
| 136 | |
| 137 | /* Query a partition by image UUID */ |
| 138 | bank = &img->img_bank_info[i]; |
Sughosh Ganu | a7188b4 | 2024-03-22 16:27:26 +0530 | [diff] [blame] | 139 | uuid_bin_to_str(bank->image_guid.b, uuidbuf, UUID_STR_FORMAT_STD); |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 140 | |
| 141 | mtd_img_info = mtd_img_by_uuid(uuidbuf); |
| 142 | if (!mtd_img_info) { |
| 143 | log_err("%s: Not found partition for image %s\n", __func__, uuidbuf); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | offset = mtd_img_info->start; |
| 148 | size = mtd_img_info->size; |
| 149 | |
| 150 | p += snprintf(p, end - p, "%sbank%d raw %x %x", |
| 151 | i == 0 ? "=" : ";", i, offset, size); |
| 152 | if (end < p) { |
| 153 | log_err("%s:%d Run out of buffer\n", __func__, __LINE__); |
| 154 | return -E2BIG; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (i == CONFIG_FWU_NUM_BANKS) |
| 159 | return 0; |
| 160 | |
| 161 | return -ENOENT; |
| 162 | } |
| 163 | |
| 164 | int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd) |
| 165 | { |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 166 | int i, l, ret; |
Sughosh Ganu | a7188b4 | 2024-03-22 16:27:26 +0530 | [diff] [blame] | 167 | struct fwu_data *data = fwu_get_data(); |
| 168 | struct fwu_image_entry *img_entry; |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 169 | |
| 170 | for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { |
Sughosh Ganu | a7188b4 | 2024-03-22 16:27:26 +0530 | [diff] [blame] | 171 | img_entry = &data->fwu_images[i]; |
| 172 | ret = gen_image_alt_info(buf, len, img_entry, mtd); |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 173 | if (ret) |
| 174 | break; |
| 175 | |
| 176 | l = strlen(buf); |
| 177 | /* Replace the last ';' with '&' if there is another image. */ |
Michal Simek | 4b5ab71 | 2023-07-13 16:36:27 +0200 | [diff] [blame] | 178 | if (i != CONFIG_FWU_NUM_IMAGES_PER_BANK - 1 && l) { |
| 179 | buf[l] = '&'; |
| 180 | buf++; |
| 181 | } |
Masami Hiramatsu | 1f52b64 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 182 | len -= l; |
| 183 | buf += l; |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |