blob: fd0823b2bfe1800f4b586211c13de4456e58ad3d [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>
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);
15static void getvar_bootloader_version(char *var_parameter, char *response);
16static 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 Roscad73d9fb2019-04-09 21:11:40 +020020static void getvar_platform(char *var_parameter, char *response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000021static void getvar_current_slot(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030022#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000023static void getvar_has_slot(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030024#endif
Alex Kiernanf73a7df2018-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 Protsenko139db352019-07-03 19:00:22 +030031static void getvar_is_userspace(char *var_parameter, char *response);
Alex Kiernanf73a7df2018-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 }, {
41 .variable = "bootloader-version",
42 .dispatch = getvar_bootloader_version
43 }, {
44 .variable = "version-bootloader",
45 .dispatch = getvar_bootloader_version
46 }, {
47 .variable = "downloadsize",
48 .dispatch = getvar_downloadsize
49 }, {
50 .variable = "max-download-size",
51 .dispatch = getvar_downloadsize
52 }, {
53 .variable = "serialno",
54 .dispatch = getvar_serialno
55 }, {
56 .variable = "version-baseband",
57 .dispatch = getvar_version_baseband
58 }, {
59 .variable = "product",
60 .dispatch = getvar_product
61 }, {
Eugeniu Roscad73d9fb2019-04-09 21:11:40 +020062 .variable = "platform",
63 .dispatch = getvar_platform
64 }, {
Alex Kiernanf73a7df2018-05-29 15:30:53 +000065 .variable = "current-slot",
66 .dispatch = getvar_current_slot
Igor Opaniuk220f6552019-06-13 21:11:09 +030067#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000068 }, {
Eugeniu Rosca4c829462019-03-26 17:46:14 +010069 .variable = "has-slot",
Alex Kiernanf73a7df2018-05-29 15:30:53 +000070 .dispatch = getvar_has_slot
Igor Opaniuk220f6552019-06-13 21:11:09 +030071#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000072#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
73 }, {
74 .variable = "partition-type",
75 .dispatch = getvar_partition_type
76#endif
77#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
78 }, {
79 .variable = "partition-size",
80 .dispatch = getvar_partition_size
81#endif
Sam Protsenko139db352019-07-03 19:00:22 +030082 }, {
83 .variable = "is-userspace",
84 .dispatch = getvar_is_userspace
Alex Kiernanf73a7df2018-05-29 15:30:53 +000085 }
86};
87
Sam Protsenkof23a87d2019-06-13 21:11:08 +030088#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
89/**
90 * Get partition number and size for any storage type.
91 *
92 * Can be used to check if partition with specified name exists.
93 *
94 * If error occurs, this function guarantees to fill @p response with fail
95 * string. @p response can be rewritten in caller, if needed.
96 *
97 * @param[in] part_name Info for which partition name to look for
98 * @param[in,out] response Pointer to fastboot response buffer
99 * @param[out] size If not NULL, will contain partition size (in blocks)
100 * @return Partition number or negative value on error
101 */
102static int getvar_get_part_info(const char *part_name, char *response,
103 size_t *size)
104{
105 int r;
106# if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
107 struct blk_desc *dev_desc;
108 disk_partition_t part_info;
109
110 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
111 response);
112 if (r >= 0 && size)
113 *size = part_info.size;
114# elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
115 struct part_info *part_info;
116
117 r = fastboot_nand_get_part_info(part_name, &part_info, response);
118 if (r >= 0 && size)
119 *size = part_info->size;
120# else
121 fastboot_fail("this storage is not supported in bootloader", response);
122 r = -ENODEV;
123# endif
124
125 return r;
126}
127#endif
128
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000129static void getvar_version(char *var_parameter, char *response)
130{
131 fastboot_okay(FASTBOOT_VERSION, response);
132}
133
134static void getvar_bootloader_version(char *var_parameter, char *response)
135{
136 fastboot_okay(U_BOOT_VERSION, response);
137}
138
139static void getvar_downloadsize(char *var_parameter, char *response)
140{
141 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
142}
143
144static void getvar_serialno(char *var_parameter, char *response)
145{
146 const char *tmp = env_get("serial#");
147
148 if (tmp)
149 fastboot_okay(tmp, response);
150 else
151 fastboot_fail("Value not set", response);
152}
153
154static void getvar_version_baseband(char *var_parameter, char *response)
155{
156 fastboot_okay("N/A", response);
157}
158
159static void getvar_product(char *var_parameter, char *response)
160{
161 const char *board = env_get("board");
162
163 if (board)
164 fastboot_okay(board, response);
165 else
166 fastboot_fail("Board not set", response);
167}
168
Eugeniu Roscad73d9fb2019-04-09 21:11:40 +0200169static void getvar_platform(char *var_parameter, char *response)
170{
171 const char *p = env_get("platform");
172
173 if (p)
174 fastboot_okay(p, response);
175 else
176 fastboot_fail("platform not set", response);
177}
178
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000179static void getvar_current_slot(char *var_parameter, char *response)
180{
Sam Protsenko97a0c6f2019-06-13 00:49:45 +0300181 /* A/B not implemented, for now always return "a" */
182 fastboot_okay("a", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000183}
184
Igor Opaniuk220f6552019-06-13 21:11:09 +0300185#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000186static void getvar_has_slot(char *part_name, char *response)
187{
Igor Opaniuk220f6552019-06-13 21:11:09 +0300188 char part_name_wslot[PART_NAME_LEN];
189 size_t len;
190 int r;
191
192 if (!part_name || part_name[0] == '\0')
193 goto fail;
194
195 /* part_name_wslot = part_name + "_a" */
196 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
197 if (len > PART_NAME_LEN - 3)
198 goto fail;
199 strcat(part_name_wslot, "_a");
200
201 r = getvar_get_part_info(part_name_wslot, response, NULL);
202 if (r >= 0) {
203 fastboot_okay("yes", response); /* part exists and slotted */
204 return;
205 }
206
207 r = getvar_get_part_info(part_name, response, NULL);
208 if (r >= 0)
209 fastboot_okay("no", response); /* part exists but not slotted */
210
211 /* At this point response is filled with okay or fail string */
212 return;
213
214fail:
215 fastboot_fail("invalid partition name", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000216}
Igor Opaniuk220f6552019-06-13 21:11:09 +0300217#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000218
219#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
220static void getvar_partition_type(char *part_name, char *response)
221{
222 int r;
223 struct blk_desc *dev_desc;
224 disk_partition_t part_info;
225
226 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
227 response);
228 if (r >= 0) {
229 r = fs_set_blk_dev_with_part(dev_desc, r);
230 if (r < 0)
231 fastboot_fail("failed to set partition", response);
232 else
233 fastboot_okay(fs_get_type_name(), response);
234 }
235}
236#endif
237
238#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
239static void getvar_partition_size(char *part_name, char *response)
240{
241 int r;
242 size_t size;
243
Sam Protsenkof23a87d2019-06-13 21:11:08 +0300244 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000245 if (r >= 0)
246 fastboot_response("OKAY", response, "0x%016zx", size);
247}
248#endif
249
Sam Protsenko139db352019-07-03 19:00:22 +0300250static void getvar_is_userspace(char *var_parameter, char *response)
251{
252 fastboot_okay("no", response);
253}
254
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000255/**
256 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
257 *
258 * @cmd_parameter: Pointer to command parameter
259 * @response: Pointer to fastboot response buffer
260 *
261 * Look up cmd_parameter first as an environment variable of the form
262 * fastboot.<cmd_parameter>, if that exists return use its value to set
263 * response.
264 *
265 * Otherwise lookup the name of variable and execute the appropriate
266 * function to return the requested value.
267 */
268void fastboot_getvar(char *cmd_parameter, char *response)
269{
270 if (!cmd_parameter) {
271 fastboot_fail("missing var", response);
272 } else {
273#define FASTBOOT_ENV_PREFIX "fastboot."
274 int i;
275 char *var_parameter = cmd_parameter;
276 char envstr[FASTBOOT_RESPONSE_LEN];
277 const char *s;
278
279 snprintf(envstr, sizeof(envstr) - 1,
280 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
281 s = env_get(envstr);
282 if (s) {
283 fastboot_response("OKAY", response, "%s", s);
284 return;
285 }
286
287 strsep(&var_parameter, ":");
288 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
289 if (!strcmp(getvar_dispatch[i].variable,
290 cmd_parameter)) {
291 getvar_dispatch[i].dispatch(var_parameter,
292 response);
293 return;
294 }
295 }
296 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
297 fastboot_fail("Variable not implemented", response);
298 }
299}