]> git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/fastboot/fb_getvar.c
fastboot: Check if partition really exist in getvar_has_slot()
[thirdparty/u-boot.git] / drivers / fastboot / fb_getvar.c
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2016 The Android Open Source Project
4 */
5
6 #include <common.h>
7 #include <fastboot.h>
8 #include <fastboot-internal.h>
9 #include <fb_mmc.h>
10 #include <fb_nand.h>
11 #include <fs.h>
12 #include <version.h>
13
14 static void getvar_version(char *var_parameter, char *response);
15 static void getvar_bootloader_version(char *var_parameter, char *response);
16 static void getvar_downloadsize(char *var_parameter, char *response);
17 static void getvar_serialno(char *var_parameter, char *response);
18 static void getvar_version_baseband(char *var_parameter, char *response);
19 static void getvar_product(char *var_parameter, char *response);
20 static void getvar_platform(char *var_parameter, char *response);
21 static void getvar_current_slot(char *var_parameter, char *response);
22 static void getvar_slot_suffixes(char *var_parameter, char *response);
23 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
24 static void getvar_has_slot(char *var_parameter, char *response);
25 #endif
26 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
27 static void getvar_partition_type(char *part_name, char *response);
28 #endif
29 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
30 static void getvar_partition_size(char *part_name, char *response);
31 #endif
32
33 static const struct {
34 const char *variable;
35 void (*dispatch)(char *var_parameter, char *response);
36 } getvar_dispatch[] = {
37 {
38 .variable = "version",
39 .dispatch = getvar_version
40 }, {
41 .variable = "bootloader-version",
42 .dispatch = getvar_bootloader_version
43 }, {
44 .variable = "version-bootloader",
45 .dispatch = getvar_bootloader_version
46 }, {
47 .variable = "downloadsize",
48 .dispatch = getvar_downloadsize
49 }, {
50 .variable = "max-download-size",
51 .dispatch = getvar_downloadsize
52 }, {
53 .variable = "serialno",
54 .dispatch = getvar_serialno
55 }, {
56 .variable = "version-baseband",
57 .dispatch = getvar_version_baseband
58 }, {
59 .variable = "product",
60 .dispatch = getvar_product
61 }, {
62 .variable = "platform",
63 .dispatch = getvar_platform
64 }, {
65 .variable = "current-slot",
66 .dispatch = getvar_current_slot
67 }, {
68 .variable = "slot-suffixes",
69 .dispatch = getvar_slot_suffixes
70 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
71 }, {
72 .variable = "has-slot",
73 .dispatch = getvar_has_slot
74 #endif
75 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
76 }, {
77 .variable = "partition-type",
78 .dispatch = getvar_partition_type
79 #endif
80 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
81 }, {
82 .variable = "partition-size",
83 .dispatch = getvar_partition_size
84 #endif
85 }
86 };
87
88 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
89 /**
90 * Get partition number and size for any storage type.
91 *
92 * Can be used to check if partition with specified name exists.
93 *
94 * If error occurs, this function guarantees to fill @p response with fail
95 * string. @p response can be rewritten in caller, if needed.
96 *
97 * @param[in] part_name Info for which partition name to look for
98 * @param[in,out] response Pointer to fastboot response buffer
99 * @param[out] size If not NULL, will contain partition size (in blocks)
100 * @return Partition number or negative value on error
101 */
102 static int getvar_get_part_info(const char *part_name, char *response,
103 size_t *size)
104 {
105 int r;
106 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
107 struct blk_desc *dev_desc;
108 disk_partition_t part_info;
109
110 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
111 response);
112 if (r >= 0 && size)
113 *size = part_info.size;
114 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
115 struct part_info *part_info;
116
117 r = fastboot_nand_get_part_info(part_name, &part_info, response);
118 if (r >= 0 && size)
119 *size = part_info->size;
120 # else
121 fastboot_fail("this storage is not supported in bootloader", response);
122 r = -ENODEV;
123 # endif
124
125 return r;
126 }
127 #endif
128
129 static void getvar_version(char *var_parameter, char *response)
130 {
131 fastboot_okay(FASTBOOT_VERSION, response);
132 }
133
134 static void getvar_bootloader_version(char *var_parameter, char *response)
135 {
136 fastboot_okay(U_BOOT_VERSION, response);
137 }
138
139 static void getvar_downloadsize(char *var_parameter, char *response)
140 {
141 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
142 }
143
144 static void getvar_serialno(char *var_parameter, char *response)
145 {
146 const char *tmp = env_get("serial#");
147
148 if (tmp)
149 fastboot_okay(tmp, response);
150 else
151 fastboot_fail("Value not set", response);
152 }
153
154 static void getvar_version_baseband(char *var_parameter, char *response)
155 {
156 fastboot_okay("N/A", response);
157 }
158
159 static void getvar_product(char *var_parameter, char *response)
160 {
161 const char *board = env_get("board");
162
163 if (board)
164 fastboot_okay(board, response);
165 else
166 fastboot_fail("Board not set", response);
167 }
168
169 static void getvar_platform(char *var_parameter, char *response)
170 {
171 const char *p = env_get("platform");
172
173 if (p)
174 fastboot_okay(p, response);
175 else
176 fastboot_fail("platform not set", response);
177 }
178
179 static void getvar_current_slot(char *var_parameter, char *response)
180 {
181 /* A/B not implemented, for now always return "a" */
182 fastboot_okay("a", response);
183 }
184
185 static void getvar_slot_suffixes(char *var_parameter, char *response)
186 {
187 fastboot_okay("a,b", response);
188 }
189
190 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
191 static void getvar_has_slot(char *part_name, char *response)
192 {
193 char part_name_wslot[PART_NAME_LEN];
194 size_t len;
195 int r;
196
197 if (!part_name || part_name[0] == '\0')
198 goto fail;
199
200 /* part_name_wslot = part_name + "_a" */
201 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
202 if (len > PART_NAME_LEN - 3)
203 goto fail;
204 strcat(part_name_wslot, "_a");
205
206 r = getvar_get_part_info(part_name_wslot, response, NULL);
207 if (r >= 0) {
208 fastboot_okay("yes", response); /* part exists and slotted */
209 return;
210 }
211
212 r = getvar_get_part_info(part_name, response, NULL);
213 if (r >= 0)
214 fastboot_okay("no", response); /* part exists but not slotted */
215
216 /* At this point response is filled with okay or fail string */
217 return;
218
219 fail:
220 fastboot_fail("invalid partition name", response);
221 }
222 #endif
223
224 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
225 static void getvar_partition_type(char *part_name, char *response)
226 {
227 int r;
228 struct blk_desc *dev_desc;
229 disk_partition_t part_info;
230
231 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
232 response);
233 if (r >= 0) {
234 r = fs_set_blk_dev_with_part(dev_desc, r);
235 if (r < 0)
236 fastboot_fail("failed to set partition", response);
237 else
238 fastboot_okay(fs_get_type_name(), response);
239 }
240 }
241 #endif
242
243 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
244 static void getvar_partition_size(char *part_name, char *response)
245 {
246 int r;
247 size_t size;
248
249 r = getvar_get_part_info(part_name, response, &size);
250 if (r >= 0)
251 fastboot_response("OKAY", response, "0x%016zx", size);
252 }
253 #endif
254
255 /**
256 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
257 *
258 * @cmd_parameter: Pointer to command parameter
259 * @response: Pointer to fastboot response buffer
260 *
261 * Look up cmd_parameter first as an environment variable of the form
262 * fastboot.<cmd_parameter>, if that exists return use its value to set
263 * response.
264 *
265 * Otherwise lookup the name of variable and execute the appropriate
266 * function to return the requested value.
267 */
268 void fastboot_getvar(char *cmd_parameter, char *response)
269 {
270 if (!cmd_parameter) {
271 fastboot_fail("missing var", response);
272 } else {
273 #define FASTBOOT_ENV_PREFIX "fastboot."
274 int i;
275 char *var_parameter = cmd_parameter;
276 char envstr[FASTBOOT_RESPONSE_LEN];
277 const char *s;
278
279 snprintf(envstr, sizeof(envstr) - 1,
280 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
281 s = env_get(envstr);
282 if (s) {
283 fastboot_response("OKAY", response, "%s", s);
284 return;
285 }
286
287 strsep(&var_parameter, ":");
288 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
289 if (!strcmp(getvar_dispatch[i].variable,
290 cmd_parameter)) {
291 getvar_dispatch[i].dispatch(var_parameter,
292 response);
293 return;
294 }
295 }
296 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
297 fastboot_fail("Variable not implemented", response);
298 }
299 }