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_current_slot(char *var_parameter, char *response);
21 static void getvar_slot_suffixes(char *var_parameter, char *response);
22 static void getvar_has_slot(char *var_parameter, char *response);
23 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
24 static void getvar_partition_type(char *part_name, char *response);
26 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
27 static void getvar_partition_size(char *part_name, char *response);
32 void (*dispatch)(char *var_parameter, char *response);
33 } getvar_dispatch[] = {
35 .variable = "version",
36 .dispatch = getvar_version
38 .variable = "bootloader-version",
39 .dispatch = getvar_bootloader_version
41 .variable = "version-bootloader",
42 .dispatch = getvar_bootloader_version
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 = "current-slot",
60 .dispatch = getvar_current_slot
62 .variable = "slot-suffixes",
63 .dispatch = getvar_slot_suffixes
65 .variable = "has_slot",
66 .dispatch = getvar_has_slot
67 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
69 .variable = "partition-type",
70 .dispatch = getvar_partition_type
72 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
74 .variable = "partition-size",
75 .dispatch = getvar_partition_size
80 static void getvar_version(char *var_parameter, char *response)
82 fastboot_okay(FASTBOOT_VERSION, response);
85 static void getvar_bootloader_version(char *var_parameter, char *response)
87 fastboot_okay(U_BOOT_VERSION, response);
90 static void getvar_downloadsize(char *var_parameter, char *response)
92 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
95 static void getvar_serialno(char *var_parameter, char *response)
97 const char *tmp = env_get("serial#");
100 fastboot_okay(tmp, response);
102 fastboot_fail("Value not set", response);
105 static void getvar_version_baseband(char *var_parameter, char *response)
107 fastboot_okay("N/A", response);
110 static void getvar_product(char *var_parameter, char *response)
112 const char *board = env_get("board");
115 fastboot_okay(board, response);
117 fastboot_fail("Board not set", response);
120 static void getvar_current_slot(char *var_parameter, char *response)
122 /* A/B not implemented, for now always return _a */
123 fastboot_okay("_a", response);
126 static void getvar_slot_suffixes(char *var_parameter, char *response)
128 fastboot_okay("_a,_b", response);
131 static void getvar_has_slot(char *part_name, char *response)
133 if (part_name && (!strcmp(part_name, "boot") ||
134 !strcmp(part_name, "system")))
135 fastboot_okay("yes", response);
137 fastboot_okay("no", response);
140 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
141 static void getvar_partition_type(char *part_name, char *response)
144 struct blk_desc *dev_desc;
145 disk_partition_t part_info;
147 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
150 r = fs_set_blk_dev_with_part(dev_desc, r);
152 fastboot_fail("failed to set partition", response);
154 fastboot_okay(fs_get_type_name(), response);
159 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
160 static void getvar_partition_size(char *part_name, char *response)
165 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
166 struct blk_desc *dev_desc;
167 disk_partition_t part_info;
169 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
172 size = part_info.size;
174 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
175 struct part_info *part_info;
177 r = fastboot_nand_get_part_info(part_name, &part_info, response);
179 size = part_info->size;
182 fastboot_response("OKAY", response, "0x%016zx", size);
187 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
189 * @cmd_parameter: Pointer to command parameter
190 * @response: Pointer to fastboot response buffer
192 * Look up cmd_parameter first as an environment variable of the form
193 * fastboot.<cmd_parameter>, if that exists return use its value to set
196 * Otherwise lookup the name of variable and execute the appropriate
197 * function to return the requested value.
199 void fastboot_getvar(char *cmd_parameter, char *response)
201 if (!cmd_parameter) {
202 fastboot_fail("missing var", response);
204 #define FASTBOOT_ENV_PREFIX "fastboot."
206 char *var_parameter = cmd_parameter;
207 char envstr[FASTBOOT_RESPONSE_LEN];
210 snprintf(envstr, sizeof(envstr) - 1,
211 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
214 fastboot_response("OKAY", response, "%s", s);
218 strsep(&var_parameter, ":");
219 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
220 if (!strcmp(getvar_dispatch[i].variable,
222 getvar_dispatch[i].dispatch(var_parameter,
227 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
228 fastboot_fail("Variable not implemented", response);