1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
9 #include <fastboot-internal.h>
15 static void getvar_version(char *var_parameter
, char *response
);
16 static void getvar_version_bootloader(char *var_parameter
, char *response
);
17 static void getvar_downloadsize(char *var_parameter
, char *response
);
18 static void getvar_serialno(char *var_parameter
, char *response
);
19 static void getvar_version_baseband(char *var_parameter
, char *response
);
20 static void getvar_product(char *var_parameter
, char *response
);
21 static void getvar_platform(char *var_parameter
, char *response
);
22 static void getvar_current_slot(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
);
32 static void getvar_is_userspace(char *var_parameter
, char *response
);
36 void (*dispatch
)(char *var_parameter
, char *response
);
37 } getvar_dispatch
[] = {
39 .variable
= "version",
40 .dispatch
= getvar_version
42 .variable
= "version-bootloader",
43 .dispatch
= getvar_version_bootloader
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
65 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
67 .variable
= "has-slot",
68 .dispatch
= getvar_has_slot
70 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
72 .variable
= "partition-type",
73 .dispatch
= getvar_partition_type
75 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
77 .variable
= "partition-size",
78 .dispatch
= getvar_partition_size
81 .variable
= "is-userspace",
82 .dispatch
= getvar_is_userspace
86 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
88 * Get partition number and size for any storage type.
90 * Can be used to check if partition with specified name exists.
92 * If error occurs, this function guarantees to fill @p response with fail
93 * string. @p response can be rewritten in caller, if needed.
95 * @param[in] part_name Info for which partition name to look for
96 * @param[in,out] response Pointer to fastboot response buffer
97 * @param[out] size If not NULL, will contain partition size (in blocks)
98 * @return Partition number or negative value on error
100 static int getvar_get_part_info(const char *part_name
, char *response
,
104 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
105 struct blk_desc
*dev_desc
;
106 disk_partition_t part_info
;
108 r
= fastboot_mmc_get_part_info(part_name
, &dev_desc
, &part_info
,
111 *size
= part_info
.size
;
112 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
113 struct part_info
*part_info
;
115 r
= fastboot_nand_get_part_info(part_name
, &part_info
, response
);
117 *size
= part_info
->size
;
119 fastboot_fail("this storage is not supported in bootloader", response
);
127 static void getvar_version(char *var_parameter
, char *response
)
129 fastboot_okay(FASTBOOT_VERSION
, response
);
132 static void getvar_version_bootloader(char *var_parameter
, char *response
)
134 fastboot_okay(U_BOOT_VERSION
, response
);
137 static void getvar_downloadsize(char *var_parameter
, char *response
)
139 fastboot_response("OKAY", response
, "0x%08x", fastboot_buf_size
);
142 static void getvar_serialno(char *var_parameter
, char *response
)
144 const char *tmp
= env_get("serial#");
147 fastboot_okay(tmp
, response
);
149 fastboot_fail("Value not set", response
);
152 static void getvar_version_baseband(char *var_parameter
, char *response
)
154 fastboot_okay("N/A", response
);
157 static void getvar_product(char *var_parameter
, char *response
)
159 const char *board
= env_get("board");
162 fastboot_okay(board
, response
);
164 fastboot_fail("Board not set", response
);
167 static void getvar_platform(char *var_parameter
, char *response
)
169 const char *p
= env_get("platform");
172 fastboot_okay(p
, response
);
174 fastboot_fail("platform not set", response
);
177 static void getvar_current_slot(char *var_parameter
, char *response
)
179 /* A/B not implemented, for now always return "a" */
180 fastboot_okay("a", response
);
183 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
184 static void getvar_has_slot(char *part_name
, char *response
)
186 char part_name_wslot
[PART_NAME_LEN
];
190 if (!part_name
|| part_name
[0] == '\0')
193 /* part_name_wslot = part_name + "_a" */
194 len
= strlcpy(part_name_wslot
, part_name
, PART_NAME_LEN
- 3);
195 if (len
> PART_NAME_LEN
- 3)
197 strcat(part_name_wslot
, "_a");
199 r
= getvar_get_part_info(part_name_wslot
, response
, NULL
);
201 fastboot_okay("yes", response
); /* part exists and slotted */
205 r
= getvar_get_part_info(part_name
, response
, NULL
);
207 fastboot_okay("no", response
); /* part exists but not slotted */
209 /* At this point response is filled with okay or fail string */
213 fastboot_fail("invalid partition name", response
);
217 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
218 static void getvar_partition_type(char *part_name
, char *response
)
221 struct blk_desc
*dev_desc
;
222 disk_partition_t part_info
;
224 r
= fastboot_mmc_get_part_info(part_name
, &dev_desc
, &part_info
,
227 r
= fs_set_blk_dev_with_part(dev_desc
, r
);
229 fastboot_fail("failed to set partition", response
);
231 fastboot_okay(fs_get_type_name(), response
);
236 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
237 static void getvar_partition_size(char *part_name
, char *response
)
242 r
= getvar_get_part_info(part_name
, response
, &size
);
244 fastboot_response("OKAY", response
, "0x%016zx", size
);
248 static void getvar_is_userspace(char *var_parameter
, char *response
)
250 fastboot_okay("no", response
);
254 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
256 * @cmd_parameter: Pointer to command parameter
257 * @response: Pointer to fastboot response buffer
259 * Look up cmd_parameter first as an environment variable of the form
260 * fastboot.<cmd_parameter>, if that exists return use its value to set
263 * Otherwise lookup the name of variable and execute the appropriate
264 * function to return the requested value.
266 void fastboot_getvar(char *cmd_parameter
, char *response
)
268 if (!cmd_parameter
) {
269 fastboot_fail("missing var", response
);
271 #define FASTBOOT_ENV_PREFIX "fastboot."
273 char *var_parameter
= cmd_parameter
;
274 char envstr
[FASTBOOT_RESPONSE_LEN
];
277 snprintf(envstr
, sizeof(envstr
) - 1,
278 FASTBOOT_ENV_PREFIX
"%s", cmd_parameter
);
281 fastboot_response("OKAY", response
, "%s", s
);
285 strsep(&var_parameter
, ":");
286 for (i
= 0; i
< ARRAY_SIZE(getvar_dispatch
); ++i
) {
287 if (!strcmp(getvar_dispatch
[i
].variable
,
289 getvar_dispatch
[i
].dispatch(var_parameter
,
294 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter
);
295 fastboot_fail("Variable not implemented", response
);