1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
9 #include <fastboot-internal.h>
16 static void getvar_version(char *var_parameter, char *response);
17 static void getvar_version_bootloader(char *var_parameter, char *response);
18 static void getvar_downloadsize(char *var_parameter, char *response);
19 static void getvar_serialno(char *var_parameter, char *response);
20 static void getvar_version_baseband(char *var_parameter, char *response);
21 static void getvar_product(char *var_parameter, char *response);
22 static void getvar_platform(char *var_parameter, char *response);
23 static void getvar_current_slot(char *var_parameter, char *response);
24 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
25 static void getvar_has_slot(char *var_parameter, char *response);
27 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
28 static void getvar_partition_type(char *part_name, char *response);
30 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
31 static void getvar_partition_size(char *part_name, char *response);
33 static void getvar_is_userspace(char *var_parameter, char *response);
37 void (*dispatch)(char *var_parameter, char *response);
38 } getvar_dispatch[] = {
40 .variable = "version",
41 .dispatch = getvar_version
43 .variable = "version-bootloader",
44 .dispatch = getvar_version_bootloader
46 .variable = "downloadsize",
47 .dispatch = getvar_downloadsize
49 .variable = "max-download-size",
50 .dispatch = getvar_downloadsize
52 .variable = "serialno",
53 .dispatch = getvar_serialno
55 .variable = "version-baseband",
56 .dispatch = getvar_version_baseband
58 .variable = "product",
59 .dispatch = getvar_product
61 .variable = "platform",
62 .dispatch = getvar_platform
64 .variable = "current-slot",
65 .dispatch = getvar_current_slot
66 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
68 .variable = "has-slot",
69 .dispatch = getvar_has_slot
71 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
73 .variable = "partition-type",
74 .dispatch = getvar_partition_type
76 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
78 .variable = "partition-size",
79 .dispatch = getvar_partition_size
82 .variable = "is-userspace",
83 .dispatch = getvar_is_userspace
87 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
89 * Get partition number and size for any storage type.
91 * Can be used to check if partition with specified name exists.
93 * If error occurs, this function guarantees to fill @p response with fail
94 * string. @p response can be rewritten in caller, if needed.
96 * @param[in] part_name Info for which partition name to look for
97 * @param[in,out] response Pointer to fastboot response buffer
98 * @param[out] size If not NULL, will contain partition size (in blocks)
99 * @return Partition number or negative value on error
101 static int getvar_get_part_info(const char *part_name, char *response,
105 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
106 struct blk_desc *dev_desc;
107 struct disk_partition part_info;
109 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
112 *size = part_info.size;
113 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
114 struct part_info *part_info;
116 r = fastboot_nand_get_part_info(part_name, &part_info, response);
118 *size = part_info->size;
120 fastboot_fail("this storage is not supported in bootloader", response);
128 static void getvar_version(char *var_parameter, char *response)
130 fastboot_okay(FASTBOOT_VERSION, response);
133 static void getvar_version_bootloader(char *var_parameter, char *response)
135 fastboot_okay(U_BOOT_VERSION, response);
138 static void getvar_downloadsize(char *var_parameter, char *response)
140 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
143 static void getvar_serialno(char *var_parameter, char *response)
145 const char *tmp = env_get("serial#");
148 fastboot_okay(tmp, response);
150 fastboot_fail("Value not set", response);
153 static void getvar_version_baseband(char *var_parameter, char *response)
155 fastboot_okay("N/A", response);
158 static void getvar_product(char *var_parameter, char *response)
160 const char *board = env_get("board");
163 fastboot_okay(board, response);
165 fastboot_fail("Board not set", response);
168 static void getvar_platform(char *var_parameter, char *response)
170 const char *p = env_get("platform");
173 fastboot_okay(p, response);
175 fastboot_fail("platform not set", response);
178 static void getvar_current_slot(char *var_parameter, char *response)
180 /* A/B not implemented, for now always return "a" */
181 fastboot_okay("a", response);
184 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
185 static void getvar_has_slot(char *part_name, char *response)
187 char part_name_wslot[PART_NAME_LEN];
191 if (!part_name || part_name[0] == '\0')
194 /* part_name_wslot = part_name + "_a" */
195 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
196 if (len > PART_NAME_LEN - 3)
198 strcat(part_name_wslot, "_a");
200 r = getvar_get_part_info(part_name_wslot, response, NULL);
202 fastboot_okay("yes", response); /* part exists and slotted */
206 r = getvar_get_part_info(part_name, response, NULL);
208 fastboot_okay("no", response); /* part exists but not slotted */
210 /* At this point response is filled with okay or fail string */
214 fastboot_fail("invalid partition name", response);
218 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
219 static void getvar_partition_type(char *part_name, char *response)
222 struct blk_desc *dev_desc;
223 struct disk_partition part_info;
225 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
228 r = fs_set_blk_dev_with_part(dev_desc, r);
230 fastboot_fail("failed to set partition", response);
232 fastboot_okay(fs_get_type_name(), response);
237 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
238 static void getvar_partition_size(char *part_name, char *response)
243 r = getvar_get_part_info(part_name, response, &size);
245 fastboot_response("OKAY", response, "0x%016zx", size);
249 static void getvar_is_userspace(char *var_parameter, char *response)
251 fastboot_okay("no", response);
255 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
257 * @cmd_parameter: Pointer to command parameter
258 * @response: Pointer to fastboot response buffer
260 * Look up cmd_parameter first as an environment variable of the form
261 * fastboot.<cmd_parameter>, if that exists return use its value to set
264 * Otherwise lookup the name of variable and execute the appropriate
265 * function to return the requested value.
267 void fastboot_getvar(char *cmd_parameter, char *response)
269 if (!cmd_parameter) {
270 fastboot_fail("missing var", response);
272 #define FASTBOOT_ENV_PREFIX "fastboot."
274 char *var_parameter = cmd_parameter;
275 char envstr[FASTBOOT_RESPONSE_LEN];
278 snprintf(envstr, sizeof(envstr) - 1,
279 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
282 fastboot_response("OKAY", response, "%s", s);
286 strsep(&var_parameter, ":");
287 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
288 if (!strcmp(getvar_dispatch[i].variable,
290 getvar_dispatch[i].dispatch(var_parameter,
295 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
296 fastboot_fail("Variable not implemented", response);