1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
8 #include <fastboot-internal.h>
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);
26 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
27 static void getvar_partition_type(char *part_name, char *response);
29 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
30 static void getvar_partition_size(char *part_name, char *response);
35 void (*dispatch)(char *var_parameter, char *response);
36 } getvar_dispatch[] = {
38 .variable = "version",
39 .dispatch = getvar_version
41 .variable = "bootloader-version",
42 .dispatch = getvar_bootloader_version
44 .variable = "version-bootloader",
45 .dispatch = getvar_bootloader_version
47 .variable = "downloadsize",
48 .dispatch = getvar_downloadsize
50 .variable = "max-download-size",
51 .dispatch = getvar_downloadsize
53 .variable = "serialno",
54 .dispatch = getvar_serialno
56 .variable = "version-baseband",
57 .dispatch = getvar_version_baseband
59 .variable = "product",
60 .dispatch = getvar_product
62 .variable = "platform",
63 .dispatch = getvar_platform
65 .variable = "current-slot",
66 .dispatch = getvar_current_slot
68 .variable = "slot-suffixes",
69 .dispatch = getvar_slot_suffixes
70 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
72 .variable = "has-slot",
73 .dispatch = getvar_has_slot
75 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
77 .variable = "partition-type",
78 .dispatch = getvar_partition_type
80 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
82 .variable = "partition-size",
83 .dispatch = getvar_partition_size
88 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
90 * Get partition number and size for any storage type.
92 * Can be used to check if partition with specified name exists.
94 * If error occurs, this function guarantees to fill @p response with fail
95 * string. @p response can be rewritten in caller, if needed.
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
102 static int getvar_get_part_info(const char *part_name, char *response,
106 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
107 struct blk_desc *dev_desc;
108 disk_partition_t part_info;
110 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
113 *size = part_info.size;
114 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
115 struct part_info *part_info;
117 r = fastboot_nand_get_part_info(part_name, &part_info, response);
119 *size = part_info->size;
121 fastboot_fail("this storage is not supported in bootloader", response);
129 static void getvar_version(char *var_parameter, char *response)
131 fastboot_okay(FASTBOOT_VERSION, response);
134 static void getvar_bootloader_version(char *var_parameter, char *response)
136 fastboot_okay(U_BOOT_VERSION, response);
139 static void getvar_downloadsize(char *var_parameter, char *response)
141 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
144 static void getvar_serialno(char *var_parameter, char *response)
146 const char *tmp = env_get("serial#");
149 fastboot_okay(tmp, response);
151 fastboot_fail("Value not set", response);
154 static void getvar_version_baseband(char *var_parameter, char *response)
156 fastboot_okay("N/A", response);
159 static void getvar_product(char *var_parameter, char *response)
161 const char *board = env_get("board");
164 fastboot_okay(board, response);
166 fastboot_fail("Board not set", response);
169 static void getvar_platform(char *var_parameter, char *response)
171 const char *p = env_get("platform");
174 fastboot_okay(p, response);
176 fastboot_fail("platform not set", response);
179 static void getvar_current_slot(char *var_parameter, char *response)
181 /* A/B not implemented, for now always return "a" */
182 fastboot_okay("a", response);
185 static void getvar_slot_suffixes(char *var_parameter, char *response)
187 fastboot_okay("a,b", response);
190 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
191 static void getvar_has_slot(char *part_name, char *response)
193 char part_name_wslot[PART_NAME_LEN];
197 if (!part_name || part_name[0] == '\0')
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)
204 strcat(part_name_wslot, "_a");
206 r = getvar_get_part_info(part_name_wslot, response, NULL);
208 fastboot_okay("yes", response); /* part exists and slotted */
212 r = getvar_get_part_info(part_name, response, NULL);
214 fastboot_okay("no", response); /* part exists but not slotted */
216 /* At this point response is filled with okay or fail string */
220 fastboot_fail("invalid partition name", response);
224 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
225 static void getvar_partition_type(char *part_name, char *response)
228 struct blk_desc *dev_desc;
229 disk_partition_t part_info;
231 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
234 r = fs_set_blk_dev_with_part(dev_desc, r);
236 fastboot_fail("failed to set partition", response);
238 fastboot_okay(fs_get_type_name(), response);
243 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
244 static void getvar_partition_size(char *part_name, char *response)
249 r = getvar_get_part_info(part_name, response, &size);
251 fastboot_response("OKAY", response, "0x%016zx", size);
256 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
258 * @cmd_parameter: Pointer to command parameter
259 * @response: Pointer to fastboot response buffer
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
265 * Otherwise lookup the name of variable and execute the appropriate
266 * function to return the requested value.
268 void fastboot_getvar(char *cmd_parameter, char *response)
270 if (!cmd_parameter) {
271 fastboot_fail("missing var", response);
273 #define FASTBOOT_ENV_PREFIX "fastboot."
275 char *var_parameter = cmd_parameter;
276 char envstr[FASTBOOT_RESPONSE_LEN];
279 snprintf(envstr, sizeof(envstr) - 1,
280 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
283 fastboot_response("OKAY", response, "%s", s);
287 strsep(&var_parameter, ":");
288 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
289 if (!strcmp(getvar_dispatch[i].variable,
291 getvar_dispatch[i].dispatch(var_parameter,
296 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
297 fastboot_fail("Variable not implemented", response);