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 static void getvar_has_slot(char *var_parameter, char *response);
24 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
25 static void getvar_partition_type(char *part_name, char *response);
27 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
28 static void getvar_partition_size(char *part_name, char *response);
33 void (*dispatch)(char *var_parameter, char *response);
34 } getvar_dispatch[] = {
36 .variable = "version",
37 .dispatch = getvar_version
39 .variable = "bootloader-version",
40 .dispatch = getvar_bootloader_version
42 .variable = "version-bootloader",
43 .dispatch = getvar_bootloader_version
45 .variable = "downloadsize",
46 .dispatch = getvar_downloadsize
48 .variable = "max-download-size",
49 .dispatch = getvar_downloadsize
51 .variable = "serialno",
52 .dispatch = getvar_serialno
54 .variable = "version-baseband",
55 .dispatch = getvar_version_baseband
57 .variable = "product",
58 .dispatch = getvar_product
60 .variable = "platform",
61 .dispatch = getvar_platform
63 .variable = "current-slot",
64 .dispatch = getvar_current_slot
66 .variable = "slot-suffixes",
67 .dispatch = getvar_slot_suffixes
69 .variable = "has-slot",
70 .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
84 static void getvar_version(char *var_parameter, char *response)
86 fastboot_okay(FASTBOOT_VERSION, response);
89 static void getvar_bootloader_version(char *var_parameter, char *response)
91 fastboot_okay(U_BOOT_VERSION, response);
94 static void getvar_downloadsize(char *var_parameter, char *response)
96 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
99 static void getvar_serialno(char *var_parameter, char *response)
101 const char *tmp = env_get("serial#");
104 fastboot_okay(tmp, response);
106 fastboot_fail("Value not set", response);
109 static void getvar_version_baseband(char *var_parameter, char *response)
111 fastboot_okay("N/A", response);
114 static void getvar_product(char *var_parameter, char *response)
116 const char *board = env_get("board");
119 fastboot_okay(board, response);
121 fastboot_fail("Board not set", response);
124 static void getvar_platform(char *var_parameter, char *response)
126 const char *p = env_get("platform");
129 fastboot_okay(p, response);
131 fastboot_fail("platform not set", response);
134 static void getvar_current_slot(char *var_parameter, char *response)
136 /* A/B not implemented, for now always return _a */
137 fastboot_okay("_a", response);
140 static void getvar_slot_suffixes(char *var_parameter, char *response)
142 fastboot_okay("_a,_b", response);
145 static void getvar_has_slot(char *part_name, char *response)
147 if (part_name && (!strcmp(part_name, "boot") ||
148 !strcmp(part_name, "system")))
149 fastboot_okay("yes", response);
151 fastboot_okay("no", response);
154 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
155 static void getvar_partition_type(char *part_name, char *response)
158 struct blk_desc *dev_desc;
159 disk_partition_t part_info;
161 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
164 r = fs_set_blk_dev_with_part(dev_desc, r);
166 fastboot_fail("failed to set partition", response);
168 fastboot_okay(fs_get_type_name(), response);
173 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
174 static void getvar_partition_size(char *part_name, char *response)
179 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
180 struct blk_desc *dev_desc;
181 disk_partition_t part_info;
183 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
186 size = part_info.size;
188 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
189 struct part_info *part_info;
191 r = fastboot_nand_get_part_info(part_name, &part_info, response);
193 size = part_info->size;
196 fastboot_response("OKAY", response, "0x%016zx", size);
201 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
203 * @cmd_parameter: Pointer to command parameter
204 * @response: Pointer to fastboot response buffer
206 * Look up cmd_parameter first as an environment variable of the form
207 * fastboot.<cmd_parameter>, if that exists return use its value to set
210 * Otherwise lookup the name of variable and execute the appropriate
211 * function to return the requested value.
213 void fastboot_getvar(char *cmd_parameter, char *response)
215 if (!cmd_parameter) {
216 fastboot_fail("missing var", response);
218 #define FASTBOOT_ENV_PREFIX "fastboot."
220 char *var_parameter = cmd_parameter;
221 char envstr[FASTBOOT_RESPONSE_LEN];
224 snprintf(envstr, sizeof(envstr) - 1,
225 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
228 fastboot_response("OKAY", response, "%s", s);
232 strsep(&var_parameter, ":");
233 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
234 if (!strcmp(getvar_dispatch[i].variable,
236 getvar_dispatch[i].dispatch(var_parameter,
241 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
242 fastboot_fail("Variable not implemented", response);