blob: ccaba3f3115ae97aa77dcb309396dcdfbdd50c8c [file] [log] [blame]
Masami Hiramatsu1f52b642023-05-31 00:29:14 -05001// 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 Mao24da8312024-05-16 14:11:52 -070014#include <stdio.h>
Masami Hiramatsu1f52b642023-05-31 00:29:14 -050015
16#include <dm/ofnode.h>
17
Masami Hiramatsu1f52b642023-05-31 00:29:14 -050018static struct fwu_mtd_image_info *mtd_img_by_uuid(const char *uuidbuf)
19{
Sughosh Ganu3d7ffcf2024-03-22 16:27:25 +053020 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 Hiramatsu1f52b642023-05-31 00:29:14 -050029
30 for (int i = 0; i < num_images; i++)
Sughosh Ganu3d7ffcf2024-03-22 16:27:25 +053031 if (!strcmp(uuidbuf, image_info[i].uuidbuf))
32 return &image_info[i];
Masami Hiramatsu1f52b642023-05-31 00:29:14 -050033
34 return NULL;
35}
36
37int 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 Ganu5db141c2024-03-22 16:27:24 +0530115static int gen_image_alt_info(char *buf, size_t len,
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500116 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 Ganua7188b42024-03-22 16:27:26 +0530139 uuid_bin_to_str(bank->image_guid.b, uuidbuf, UUID_STR_FORMAT_STD);
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500140
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
164int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd)
165{
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500166 int i, l, ret;
Sughosh Ganua7188b42024-03-22 16:27:26 +0530167 struct fwu_data *data = fwu_get_data();
168 struct fwu_image_entry *img_entry;
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500169
170 for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) {
Sughosh Ganua7188b42024-03-22 16:27:26 +0530171 img_entry = &data->fwu_images[i];
172 ret = gen_image_alt_info(buf, len, img_entry, mtd);
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500173 if (ret)
174 break;
175
176 l = strlen(buf);
177 /* Replace the last ';' with '&' if there is another image. */
Michal Simek4b5ab712023-07-13 16:36:27 +0200178 if (i != CONFIG_FWU_NUM_IMAGES_PER_BANK - 1 && l) {
179 buf[l] = '&';
180 buf++;
181 }
Masami Hiramatsu1f52b642023-05-31 00:29:14 -0500182 len -= l;
183 buf += l;
184 }
185
186 return ret;
187}