blob: 52da34b1e37e6fe5f79eaec3d8c79189ef46d1cb [file] [log] [blame]
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2016 The Android Open Source Project
4 */
5
6#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -06007#include <env.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +00008#include <fastboot.h>
9#include <fastboot-internal.h>
10#include <fb_mmc.h>
11#include <fb_nand.h>
12#include <fs.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <part.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000014#include <version.h>
15
16static void getvar_version(char *var_parameter, char *response);
Sam Protsenko29a81142019-07-03 19:34:07 +030017static void getvar_version_bootloader(char *var_parameter, char *response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000018static void getvar_downloadsize(char *var_parameter, char *response);
19static void getvar_serialno(char *var_parameter, char *response);
20static void getvar_version_baseband(char *var_parameter, char *response);
21static void getvar_product(char *var_parameter, char *response);
Eugeniu Roscad73d9fb2019-04-09 21:11:40 +020022static void getvar_platform(char *var_parameter, char *response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000023static void getvar_current_slot(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030024#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000025static void getvar_has_slot(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030026#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000027#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
28static void getvar_partition_type(char *part_name, char *response);
29#endif
30#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
31static void getvar_partition_size(char *part_name, char *response);
32#endif
Sam Protsenko139db352019-07-03 19:00:22 +030033static void getvar_is_userspace(char *var_parameter, char *response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000034
35static const struct {
36 const char *variable;
37 void (*dispatch)(char *var_parameter, char *response);
38} getvar_dispatch[] = {
39 {
40 .variable = "version",
41 .dispatch = getvar_version
42 }, {
Alex Kiernanf73a7df2018-05-29 15:30:53 +000043 .variable = "version-bootloader",
Sam Protsenko29a81142019-07-03 19:34:07 +030044 .dispatch = getvar_version_bootloader
Alex Kiernanf73a7df2018-05-29 15:30:53 +000045 }, {
46 .variable = "downloadsize",
47 .dispatch = getvar_downloadsize
48 }, {
49 .variable = "max-download-size",
50 .dispatch = getvar_downloadsize
51 }, {
52 .variable = "serialno",
53 .dispatch = getvar_serialno
54 }, {
55 .variable = "version-baseband",
56 .dispatch = getvar_version_baseband
57 }, {
58 .variable = "product",
59 .dispatch = getvar_product
60 }, {
Eugeniu Roscad73d9fb2019-04-09 21:11:40 +020061 .variable = "platform",
62 .dispatch = getvar_platform
63 }, {
Alex Kiernanf73a7df2018-05-29 15:30:53 +000064 .variable = "current-slot",
65 .dispatch = getvar_current_slot
Igor Opaniuk220f6552019-06-13 21:11:09 +030066#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000067 }, {
Eugeniu Rosca4c829462019-03-26 17:46:14 +010068 .variable = "has-slot",
Alex Kiernanf73a7df2018-05-29 15:30:53 +000069 .dispatch = getvar_has_slot
Igor Opaniuk220f6552019-06-13 21:11:09 +030070#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000071#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
72 }, {
73 .variable = "partition-type",
74 .dispatch = getvar_partition_type
75#endif
76#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
77 }, {
78 .variable = "partition-size",
79 .dispatch = getvar_partition_size
80#endif
Sam Protsenko139db352019-07-03 19:00:22 +030081 }, {
82 .variable = "is-userspace",
83 .dispatch = getvar_is_userspace
Alex Kiernanf73a7df2018-05-29 15:30:53 +000084 }
85};
86
Sam Protsenkof23a87d2019-06-13 21:11:08 +030087#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
88/**
89 * Get partition number and size for any storage type.
90 *
91 * Can be used to check if partition with specified name exists.
92 *
93 * If error occurs, this function guarantees to fill @p response with fail
94 * string. @p response can be rewritten in caller, if needed.
95 *
96 * @param[in] part_name Info for which partition name to look for
97 * @param[in,out] response Pointer to fastboot response buffer
98 * @param[out] size If not NULL, will contain partition size (in blocks)
99 * @return Partition number or negative value on error
100 */
101static int getvar_get_part_info(const char *part_name, char *response,
102 size_t *size)
103{
104 int r;
105# if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
106 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -0600107 struct disk_partition part_info;
Sam Protsenkof23a87d2019-06-13 21:11:08 +0300108
109 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
110 response);
111 if (r >= 0 && size)
112 *size = part_info.size;
113# elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
114 struct part_info *part_info;
115
116 r = fastboot_nand_get_part_info(part_name, &part_info, response);
117 if (r >= 0 && size)
118 *size = part_info->size;
119# else
120 fastboot_fail("this storage is not supported in bootloader", response);
121 r = -ENODEV;
122# endif
123
124 return r;
125}
126#endif
127
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000128static void getvar_version(char *var_parameter, char *response)
129{
130 fastboot_okay(FASTBOOT_VERSION, response);
131}
132
Sam Protsenko29a81142019-07-03 19:34:07 +0300133static void getvar_version_bootloader(char *var_parameter, char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000134{
135 fastboot_okay(U_BOOT_VERSION, response);
136}
137
138static void getvar_downloadsize(char *var_parameter, char *response)
139{
140 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
141}
142
143static void getvar_serialno(char *var_parameter, char *response)
144{
145 const char *tmp = env_get("serial#");
146
147 if (tmp)
148 fastboot_okay(tmp, response);
149 else
150 fastboot_fail("Value not set", response);
151}
152
153static void getvar_version_baseband(char *var_parameter, char *response)
154{
155 fastboot_okay("N/A", response);
156}
157
158static void getvar_product(char *var_parameter, char *response)
159{
160 const char *board = env_get("board");
161
162 if (board)
163 fastboot_okay(board, response);
164 else
165 fastboot_fail("Board not set", response);
166}
167
Eugeniu Roscad73d9fb2019-04-09 21:11:40 +0200168static void getvar_platform(char *var_parameter, char *response)
169{
170 const char *p = env_get("platform");
171
172 if (p)
173 fastboot_okay(p, response);
174 else
175 fastboot_fail("platform not set", response);
176}
177
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000178static void getvar_current_slot(char *var_parameter, char *response)
179{
Sam Protsenko97a0c6f2019-06-13 00:49:45 +0300180 /* A/B not implemented, for now always return "a" */
181 fastboot_okay("a", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000182}
183
Igor Opaniuk220f6552019-06-13 21:11:09 +0300184#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000185static void getvar_has_slot(char *part_name, char *response)
186{
Igor Opaniuk220f6552019-06-13 21:11:09 +0300187 char part_name_wslot[PART_NAME_LEN];
188 size_t len;
189 int r;
190
191 if (!part_name || part_name[0] == '\0')
192 goto fail;
193
194 /* part_name_wslot = part_name + "_a" */
195 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
196 if (len > PART_NAME_LEN - 3)
197 goto fail;
198 strcat(part_name_wslot, "_a");
199
200 r = getvar_get_part_info(part_name_wslot, response, NULL);
201 if (r >= 0) {
202 fastboot_okay("yes", response); /* part exists and slotted */
203 return;
204 }
205
206 r = getvar_get_part_info(part_name, response, NULL);
207 if (r >= 0)
208 fastboot_okay("no", response); /* part exists but not slotted */
209
210 /* At this point response is filled with okay or fail string */
211 return;
212
213fail:
214 fastboot_fail("invalid partition name", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000215}
Igor Opaniuk220f6552019-06-13 21:11:09 +0300216#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000217
218#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
219static void getvar_partition_type(char *part_name, char *response)
220{
221 int r;
222 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -0600223 struct disk_partition part_info;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000224
225 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
226 response);
227 if (r >= 0) {
228 r = fs_set_blk_dev_with_part(dev_desc, r);
229 if (r < 0)
230 fastboot_fail("failed to set partition", response);
231 else
232 fastboot_okay(fs_get_type_name(), response);
233 }
234}
235#endif
236
237#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
238static void getvar_partition_size(char *part_name, char *response)
239{
240 int r;
241 size_t size;
242
Sam Protsenkof23a87d2019-06-13 21:11:08 +0300243 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000244 if (r >= 0)
245 fastboot_response("OKAY", response, "0x%016zx", size);
246}
247#endif
248
Sam Protsenko139db352019-07-03 19:00:22 +0300249static void getvar_is_userspace(char *var_parameter, char *response)
250{
251 fastboot_okay("no", response);
252}
253
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000254/**
255 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
256 *
257 * @cmd_parameter: Pointer to command parameter
258 * @response: Pointer to fastboot response buffer
259 *
260 * Look up cmd_parameter first as an environment variable of the form
261 * fastboot.<cmd_parameter>, if that exists return use its value to set
262 * response.
263 *
264 * Otherwise lookup the name of variable and execute the appropriate
265 * function to return the requested value.
266 */
267void fastboot_getvar(char *cmd_parameter, char *response)
268{
269 if (!cmd_parameter) {
270 fastboot_fail("missing var", response);
271 } else {
272#define FASTBOOT_ENV_PREFIX "fastboot."
273 int i;
274 char *var_parameter = cmd_parameter;
275 char envstr[FASTBOOT_RESPONSE_LEN];
276 const char *s;
277
278 snprintf(envstr, sizeof(envstr) - 1,
279 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
280 s = env_get(envstr);
281 if (s) {
282 fastboot_response("OKAY", response, "%s", s);
283 return;
284 }
285
286 strsep(&var_parameter, ":");
287 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
288 if (!strcmp(getvar_dispatch[i].variable,
289 cmd_parameter)) {
290 getvar_dispatch[i].dispatch(var_parameter,
291 response);
292 return;
293 }
294 }
295 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
296 fastboot_fail("Variable not implemented", response);
297 }
298}