blob: ebe5c8a1043685df302a9e7f155e96a39a2c5273 [file] [log] [blame]
Alex Kiernand5aa57c2018-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>
7#include <fastboot.h>
8#include <fastboot-internal.h>
9#include <fb_mmc.h>
10#include <fb_nand.h>
11#include <fs.h>
12#include <version.h>
13
14static void getvar_version(char *var_parameter, char *response);
Sam Protsenkoac21e742019-07-03 19:34:07 +030015static void getvar_version_bootloader(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000016static void getvar_downloadsize(char *var_parameter, char *response);
17static void getvar_serialno(char *var_parameter, char *response);
18static void getvar_version_baseband(char *var_parameter, char *response);
19static void getvar_product(char *var_parameter, char *response);
Eugeniu Roscafb614d42019-04-09 21:11:40 +020020static void getvar_platform(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000021static void getvar_current_slot(char *var_parameter, char *response);
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030022#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000023static void getvar_has_slot(char *var_parameter, char *response);
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030024#endif
Alex Kiernand5aa57c2018-05-29 15:30:53 +000025#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
26static void getvar_partition_type(char *part_name, char *response);
27#endif
28#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
29static void getvar_partition_size(char *part_name, char *response);
30#endif
Sam Protsenko0d748252019-07-03 19:00:22 +030031static void getvar_is_userspace(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000032
33static const struct {
34 const char *variable;
35 void (*dispatch)(char *var_parameter, char *response);
36} getvar_dispatch[] = {
37 {
38 .variable = "version",
39 .dispatch = getvar_version
40 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000041 .variable = "version-bootloader",
Sam Protsenkoac21e742019-07-03 19:34:07 +030042 .dispatch = getvar_version_bootloader
Alex Kiernand5aa57c2018-05-29 15:30:53 +000043 }, {
44 .variable = "downloadsize",
45 .dispatch = getvar_downloadsize
46 }, {
47 .variable = "max-download-size",
48 .dispatch = getvar_downloadsize
49 }, {
50 .variable = "serialno",
51 .dispatch = getvar_serialno
52 }, {
53 .variable = "version-baseband",
54 .dispatch = getvar_version_baseband
55 }, {
56 .variable = "product",
57 .dispatch = getvar_product
58 }, {
Eugeniu Roscafb614d42019-04-09 21:11:40 +020059 .variable = "platform",
60 .dispatch = getvar_platform
61 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000062 .variable = "current-slot",
63 .dispatch = getvar_current_slot
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030064#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000065 }, {
Eugeniu Rosca2ef97cf2019-03-26 17:46:14 +010066 .variable = "has-slot",
Alex Kiernand5aa57c2018-05-29 15:30:53 +000067 .dispatch = getvar_has_slot
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030068#endif
Alex Kiernand5aa57c2018-05-29 15:30:53 +000069#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
70 }, {
71 .variable = "partition-type",
72 .dispatch = getvar_partition_type
73#endif
74#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
75 }, {
76 .variable = "partition-size",
77 .dispatch = getvar_partition_size
78#endif
Sam Protsenko0d748252019-07-03 19:00:22 +030079 }, {
80 .variable = "is-userspace",
81 .dispatch = getvar_is_userspace
Alex Kiernand5aa57c2018-05-29 15:30:53 +000082 }
83};
84
Sam Protsenko6646bbd2019-06-13 21:11:08 +030085#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
86/**
87 * Get partition number and size for any storage type.
88 *
89 * Can be used to check if partition with specified name exists.
90 *
91 * If error occurs, this function guarantees to fill @p response with fail
92 * string. @p response can be rewritten in caller, if needed.
93 *
94 * @param[in] part_name Info for which partition name to look for
95 * @param[in,out] response Pointer to fastboot response buffer
96 * @param[out] size If not NULL, will contain partition size (in blocks)
97 * @return Partition number or negative value on error
98 */
99static int getvar_get_part_info(const char *part_name, char *response,
100 size_t *size)
101{
102 int r;
103# if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
104 struct blk_desc *dev_desc;
105 disk_partition_t part_info;
106
107 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
108 response);
109 if (r >= 0 && size)
110 *size = part_info.size;
111# elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
112 struct part_info *part_info;
113
114 r = fastboot_nand_get_part_info(part_name, &part_info, response);
115 if (r >= 0 && size)
116 *size = part_info->size;
117# else
118 fastboot_fail("this storage is not supported in bootloader", response);
119 r = -ENODEV;
120# endif
121
122 return r;
123}
124#endif
125
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000126static void getvar_version(char *var_parameter, char *response)
127{
128 fastboot_okay(FASTBOOT_VERSION, response);
129}
130
Sam Protsenkoac21e742019-07-03 19:34:07 +0300131static void getvar_version_bootloader(char *var_parameter, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000132{
133 fastboot_okay(U_BOOT_VERSION, response);
134}
135
136static void getvar_downloadsize(char *var_parameter, char *response)
137{
138 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
139}
140
141static void getvar_serialno(char *var_parameter, char *response)
142{
143 const char *tmp = env_get("serial#");
144
145 if (tmp)
146 fastboot_okay(tmp, response);
147 else
148 fastboot_fail("Value not set", response);
149}
150
151static void getvar_version_baseband(char *var_parameter, char *response)
152{
153 fastboot_okay("N/A", response);
154}
155
156static void getvar_product(char *var_parameter, char *response)
157{
158 const char *board = env_get("board");
159
160 if (board)
161 fastboot_okay(board, response);
162 else
163 fastboot_fail("Board not set", response);
164}
165
Eugeniu Roscafb614d42019-04-09 21:11:40 +0200166static void getvar_platform(char *var_parameter, char *response)
167{
168 const char *p = env_get("platform");
169
170 if (p)
171 fastboot_okay(p, response);
172 else
173 fastboot_fail("platform not set", response);
174}
175
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000176static void getvar_current_slot(char *var_parameter, char *response)
177{
Sam Protsenkofd7467e2019-06-13 00:49:45 +0300178 /* A/B not implemented, for now always return "a" */
179 fastboot_okay("a", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000180}
181
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300182#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000183static void getvar_has_slot(char *part_name, char *response)
184{
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300185 char part_name_wslot[PART_NAME_LEN];
186 size_t len;
187 int r;
188
189 if (!part_name || part_name[0] == '\0')
190 goto fail;
191
192 /* part_name_wslot = part_name + "_a" */
193 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
194 if (len > PART_NAME_LEN - 3)
195 goto fail;
196 strcat(part_name_wslot, "_a");
197
198 r = getvar_get_part_info(part_name_wslot, response, NULL);
199 if (r >= 0) {
200 fastboot_okay("yes", response); /* part exists and slotted */
201 return;
202 }
203
204 r = getvar_get_part_info(part_name, response, NULL);
205 if (r >= 0)
206 fastboot_okay("no", response); /* part exists but not slotted */
207
208 /* At this point response is filled with okay or fail string */
209 return;
210
211fail:
212 fastboot_fail("invalid partition name", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000213}
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300214#endif
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000215
216#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
217static void getvar_partition_type(char *part_name, char *response)
218{
219 int r;
220 struct blk_desc *dev_desc;
221 disk_partition_t part_info;
222
223 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
224 response);
225 if (r >= 0) {
226 r = fs_set_blk_dev_with_part(dev_desc, r);
227 if (r < 0)
228 fastboot_fail("failed to set partition", response);
229 else
230 fastboot_okay(fs_get_type_name(), response);
231 }
232}
233#endif
234
235#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
236static void getvar_partition_size(char *part_name, char *response)
237{
238 int r;
239 size_t size;
240
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300241 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000242 if (r >= 0)
243 fastboot_response("OKAY", response, "0x%016zx", size);
244}
245#endif
246
Sam Protsenko0d748252019-07-03 19:00:22 +0300247static void getvar_is_userspace(char *var_parameter, char *response)
248{
249 fastboot_okay("no", response);
250}
251
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000252/**
253 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
254 *
255 * @cmd_parameter: Pointer to command parameter
256 * @response: Pointer to fastboot response buffer
257 *
258 * Look up cmd_parameter first as an environment variable of the form
259 * fastboot.<cmd_parameter>, if that exists return use its value to set
260 * response.
261 *
262 * Otherwise lookup the name of variable and execute the appropriate
263 * function to return the requested value.
264 */
265void fastboot_getvar(char *cmd_parameter, char *response)
266{
267 if (!cmd_parameter) {
268 fastboot_fail("missing var", response);
269 } else {
270#define FASTBOOT_ENV_PREFIX "fastboot."
271 int i;
272 char *var_parameter = cmd_parameter;
273 char envstr[FASTBOOT_RESPONSE_LEN];
274 const char *s;
275
276 snprintf(envstr, sizeof(envstr) - 1,
277 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
278 s = env_get(envstr);
279 if (s) {
280 fastboot_response("OKAY", response, "%s", s);
281 return;
282 }
283
284 strsep(&var_parameter, ":");
285 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
286 if (!strcmp(getvar_dispatch[i].variable,
287 cmd_parameter)) {
288 getvar_dispatch[i].dispatch(var_parameter,
289 response);
290 return;
291 }
292 }
293 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
294 fastboot_fail("Variable not implemented", response);
295 }
296}