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 static void getvar_has_slot(char *var_parameter, char *response);
25 static void getvar_partition_type(char *part_name, char *response);
26 static void getvar_partition_size(char *part_name, char *response);
27 static void getvar_is_userspace(char *var_parameter, char *response);
31 void (*dispatch)(char *var_parameter, char *response);
32 } getvar_dispatch[] = {
34 .variable = "version",
35 .dispatch = getvar_version
37 .variable = "version-bootloader",
38 .dispatch = getvar_version_bootloader
40 .variable = "downloadsize",
41 .dispatch = getvar_downloadsize
43 .variable = "max-download-size",
44 .dispatch = getvar_downloadsize
46 .variable = "serialno",
47 .dispatch = getvar_serialno
49 .variable = "version-baseband",
50 .dispatch = getvar_version_baseband
52 .variable = "product",
53 .dispatch = getvar_product
55 .variable = "platform",
56 .dispatch = getvar_platform
58 .variable = "current-slot",
59 .dispatch = getvar_current_slot
60 #if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
62 .variable = "has-slot",
63 .dispatch = getvar_has_slot
65 #if IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)
67 .variable = "partition-type",
68 .dispatch = getvar_partition_type
70 #if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
72 .variable = "partition-size",
73 .dispatch = getvar_partition_size
76 .variable = "is-userspace",
77 .dispatch = getvar_is_userspace
82 * Get partition number and size for any storage type.
84 * Can be used to check if partition with specified name exists.
86 * If error occurs, this function guarantees to fill @p response with fail
87 * string. @p response can be rewritten in caller, if needed.
89 * @param[in] part_name Info for which partition name to look for
90 * @param[in,out] response Pointer to fastboot response buffer
91 * @param[out] size If not NULL, will contain partition size
92 * Return: Partition number or negative value on error
94 static int getvar_get_part_info(const char *part_name, char *response,
98 struct blk_desc *dev_desc;
99 struct disk_partition disk_part;
100 struct part_info *part_info;
102 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) {
103 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &disk_part,
106 *size = disk_part.size * disk_part.blksz;
107 } else if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) {
108 r = fastboot_nand_get_part_info(part_name, &part_info, response);
110 *size = part_info->size;
112 fastboot_fail("this storage is not supported in bootloader", response);
119 static void getvar_version(char *var_parameter, char *response)
121 fastboot_okay(FASTBOOT_VERSION, response);
124 static void getvar_version_bootloader(char *var_parameter, char *response)
126 fastboot_okay(U_BOOT_VERSION, response);
129 static void getvar_downloadsize(char *var_parameter, char *response)
131 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
134 static void getvar_serialno(char *var_parameter, char *response)
136 const char *tmp = env_get("serial#");
139 fastboot_okay(tmp, response);
141 fastboot_fail("Value not set", response);
144 static void getvar_version_baseband(char *var_parameter, char *response)
146 fastboot_okay("N/A", response);
149 static void getvar_product(char *var_parameter, char *response)
151 const char *board = env_get("board");
154 fastboot_okay(board, response);
156 fastboot_fail("Board not set", response);
159 static void getvar_platform(char *var_parameter, char *response)
161 const char *p = env_get("platform");
164 fastboot_okay(p, response);
166 fastboot_fail("platform not set", response);
169 static void getvar_current_slot(char *var_parameter, char *response)
171 /* A/B not implemented, for now always return "a" */
172 fastboot_okay("a", response);
175 static void __maybe_unused getvar_has_slot(char *part_name, char *response)
177 char part_name_wslot[PART_NAME_LEN];
181 if (!part_name || part_name[0] == '\0')
184 /* part_name_wslot = part_name + "_a" */
185 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
186 if (len > PART_NAME_LEN - 3)
188 strcat(part_name_wslot, "_a");
190 r = getvar_get_part_info(part_name_wslot, response, NULL);
192 fastboot_okay("yes", response); /* part exists and slotted */
196 r = getvar_get_part_info(part_name, response, NULL);
198 fastboot_okay("no", response); /* part exists but not slotted */
200 /* At this point response is filled with okay or fail string */
204 fastboot_fail("invalid partition name", response);
207 static void __maybe_unused getvar_partition_type(char *part_name, char *response)
210 struct blk_desc *dev_desc;
211 struct disk_partition part_info;
213 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
216 r = fs_set_blk_dev_with_part(dev_desc, r);
218 fastboot_fail("failed to set partition", response);
220 fastboot_okay(fs_get_type_name(), response);
224 static void __maybe_unused getvar_partition_size(char *part_name, char *response)
229 r = getvar_get_part_info(part_name, response, &size);
231 fastboot_response("OKAY", response, "0x%016zx", size);
234 static void getvar_is_userspace(char *var_parameter, char *response)
236 fastboot_okay("no", response);
240 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
242 * @cmd_parameter: Pointer to command parameter
243 * @response: Pointer to fastboot response buffer
245 * Look up cmd_parameter first as an environment variable of the form
246 * fastboot.<cmd_parameter>, if that exists return use its value to set
249 * Otherwise lookup the name of variable and execute the appropriate
250 * function to return the requested value.
252 void fastboot_getvar(char *cmd_parameter, char *response)
254 if (!cmd_parameter) {
255 fastboot_fail("missing var", response);
257 #define FASTBOOT_ENV_PREFIX "fastboot."
259 char *var_parameter = cmd_parameter;
260 char envstr[FASTBOOT_RESPONSE_LEN];
263 snprintf(envstr, sizeof(envstr) - 1,
264 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
267 fastboot_response("OKAY", response, "%s", s);
271 strsep(&var_parameter, ":");
272 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
273 if (!strcmp(getvar_dispatch[i].variable,
275 getvar_dispatch[i].dispatch(var_parameter,
280 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
281 fastboot_fail("Variable not implemented", response);