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_version_bootloader(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 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
23 static void getvar_has_slot(char *var_parameter, char *response);
25 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
26 static void getvar_partition_type(char *part_name, char *response);
28 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
29 static void getvar_partition_size(char *part_name, char *response);
31 static void getvar_is_userspace(char *var_parameter, char *response);
35 void (*dispatch)(char *var_parameter, char *response);
36 } getvar_dispatch[] = {
38 .variable = "version",
39 .dispatch = getvar_version
41 .variable = "version-bootloader",
42 .dispatch = getvar_version_bootloader
44 .variable = "downloadsize",
45 .dispatch = getvar_downloadsize
47 .variable = "max-download-size",
48 .dispatch = getvar_downloadsize
50 .variable = "serialno",
51 .dispatch = getvar_serialno
53 .variable = "version-baseband",
54 .dispatch = getvar_version_baseband
56 .variable = "product",
57 .dispatch = getvar_product
59 .variable = "platform",
60 .dispatch = getvar_platform
62 .variable = "current-slot",
63 .dispatch = getvar_current_slot
64 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
66 .variable = "has-slot",
67 .dispatch = getvar_has_slot
69 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
71 .variable = "partition-type",
72 .dispatch = getvar_partition_type
74 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
76 .variable = "partition-size",
77 .dispatch = getvar_partition_size
80 .variable = "is-userspace",
81 .dispatch = getvar_is_userspace
85 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
87 * Get partition number and size for any storage type.
89 * Can be used to check if partition with specified name exists.
91 * If error occurs, this function guarantees to fill @p response with fail
92 * string. @p response can be rewritten in caller, if needed.
94 * @param[in] part_name Info for which partition name to look for
95 * @param[in,out] response Pointer to fastboot response buffer
96 * @param[out] size If not NULL, will contain partition size (in blocks)
97 * @return Partition number or negative value on error
99 static int getvar_get_part_info(const char *part_name, char *response,
103 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
104 struct blk_desc *dev_desc;
105 disk_partition_t part_info;
107 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
110 *size = part_info.size;
111 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
112 struct part_info *part_info;
114 r = fastboot_nand_get_part_info(part_name, &part_info, response);
116 *size = part_info->size;
118 fastboot_fail("this storage is not supported in bootloader", response);
126 static void getvar_version(char *var_parameter, char *response)
128 fastboot_okay(FASTBOOT_VERSION, response);
131 static void getvar_version_bootloader(char *var_parameter, char *response)
133 fastboot_okay(U_BOOT_VERSION, response);
136 static void getvar_downloadsize(char *var_parameter, char *response)
138 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
141 static void getvar_serialno(char *var_parameter, char *response)
143 const char *tmp = env_get("serial#");
146 fastboot_okay(tmp, response);
148 fastboot_fail("Value not set", response);
151 static void getvar_version_baseband(char *var_parameter, char *response)
153 fastboot_okay("N/A", response);
156 static void getvar_product(char *var_parameter, char *response)
158 const char *board = env_get("board");
161 fastboot_okay(board, response);
163 fastboot_fail("Board not set", response);
166 static void getvar_platform(char *var_parameter, char *response)
168 const char *p = env_get("platform");
171 fastboot_okay(p, response);
173 fastboot_fail("platform not set", response);
176 static void getvar_current_slot(char *var_parameter, char *response)
178 /* A/B not implemented, for now always return "a" */
179 fastboot_okay("a", response);
182 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
183 static void getvar_has_slot(char *part_name, char *response)
185 char part_name_wslot[PART_NAME_LEN];
189 if (!part_name || part_name[0] == '\0')
192 /* part_name_wslot = part_name + "_a" */
193 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
194 if (len > PART_NAME_LEN - 3)
196 strcat(part_name_wslot, "_a");
198 r = getvar_get_part_info(part_name_wslot, response, NULL);
200 fastboot_okay("yes", response); /* part exists and slotted */
204 r = getvar_get_part_info(part_name, response, NULL);
206 fastboot_okay("no", response); /* part exists but not slotted */
208 /* At this point response is filled with okay or fail string */
212 fastboot_fail("invalid partition name", response);
216 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
217 static void getvar_partition_type(char *part_name, char *response)
220 struct blk_desc *dev_desc;
221 disk_partition_t part_info;
223 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
226 r = fs_set_blk_dev_with_part(dev_desc, r);
228 fastboot_fail("failed to set partition", response);
230 fastboot_okay(fs_get_type_name(), response);
235 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
236 static void getvar_partition_size(char *part_name, char *response)
241 r = getvar_get_part_info(part_name, response, &size);
243 fastboot_response("OKAY", response, "0x%016zx", size);
247 static void getvar_is_userspace(char *var_parameter, char *response)
249 fastboot_okay("no", response);
253 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
255 * @cmd_parameter: Pointer to command parameter
256 * @response: Pointer to fastboot response buffer
258 * Look up cmd_parameter first as an environment variable of the form
259 * fastboot.<cmd_parameter>, if that exists return use its value to set
262 * Otherwise lookup the name of variable and execute the appropriate
263 * function to return the requested value.
265 void fastboot_getvar(char *cmd_parameter, char *response)
267 if (!cmd_parameter) {
268 fastboot_fail("missing var", response);
270 #define FASTBOOT_ENV_PREFIX "fastboot."
272 char *var_parameter = cmd_parameter;
273 char envstr[FASTBOOT_RESPONSE_LEN];
276 snprintf(envstr, sizeof(envstr) - 1,
277 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
280 fastboot_response("OKAY", response, "%s", s);
284 strsep(&var_parameter, ":");
285 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
286 if (!strcmp(getvar_dispatch[i].variable,
288 getvar_dispatch[i].dispatch(var_parameter,
293 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
294 fastboot_fail("Variable not implemented", response);