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