blob: bf957e83265162b59cb6b5a68035d9a87a37c9de [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);
22static void getvar_slot_suffixes(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030023#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000024static void getvar_has_slot(char *var_parameter, char *response);
Igor Opaniuk220f6552019-06-13 21:11:09 +030025#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000026#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
27static void getvar_partition_type(char *part_name, char *response);
28#endif
29#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
30static void getvar_partition_size(char *part_name, char *response);
31#endif
32
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
67 }, {
68 .variable = "slot-suffixes",
69 .dispatch = getvar_slot_suffixes
Igor Opaniuk220f6552019-06-13 21:11:09 +030070#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000071 }, {
Eugeniu Rosca4c829462019-03-26 17:46:14 +010072 .variable = "has-slot",
Alex Kiernanf73a7df2018-05-29 15:30:53 +000073 .dispatch = getvar_has_slot
Igor Opaniuk220f6552019-06-13 21:11:09 +030074#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +000075#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
76 }, {
77 .variable = "partition-type",
78 .dispatch = getvar_partition_type
79#endif
80#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
81 }, {
82 .variable = "partition-size",
83 .dispatch = getvar_partition_size
84#endif
85 }
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
185static void getvar_slot_suffixes(char *var_parameter, char *response)
186{
Sam Protsenko97a0c6f2019-06-13 00:49:45 +0300187 fastboot_okay("a,b", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000188}
189
Igor Opaniuk220f6552019-06-13 21:11:09 +0300190#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000191static void getvar_has_slot(char *part_name, char *response)
192{
Igor Opaniuk220f6552019-06-13 21:11:09 +0300193 char part_name_wslot[PART_NAME_LEN];
194 size_t len;
195 int r;
196
197 if (!part_name || part_name[0] == '\0')
198 goto fail;
199
200 /* part_name_wslot = part_name + "_a" */
201 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
202 if (len > PART_NAME_LEN - 3)
203 goto fail;
204 strcat(part_name_wslot, "_a");
205
206 r = getvar_get_part_info(part_name_wslot, response, NULL);
207 if (r >= 0) {
208 fastboot_okay("yes", response); /* part exists and slotted */
209 return;
210 }
211
212 r = getvar_get_part_info(part_name, response, NULL);
213 if (r >= 0)
214 fastboot_okay("no", response); /* part exists but not slotted */
215
216 /* At this point response is filled with okay or fail string */
217 return;
218
219fail:
220 fastboot_fail("invalid partition name", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000221}
Igor Opaniuk220f6552019-06-13 21:11:09 +0300222#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000223
224#if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
225static void getvar_partition_type(char *part_name, char *response)
226{
227 int r;
228 struct blk_desc *dev_desc;
229 disk_partition_t part_info;
230
231 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
232 response);
233 if (r >= 0) {
234 r = fs_set_blk_dev_with_part(dev_desc, r);
235 if (r < 0)
236 fastboot_fail("failed to set partition", response);
237 else
238 fastboot_okay(fs_get_type_name(), response);
239 }
240}
241#endif
242
243#if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
244static void getvar_partition_size(char *part_name, char *response)
245{
246 int r;
247 size_t size;
248
Sam Protsenkof23a87d2019-06-13 21:11:08 +0300249 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000250 if (r >= 0)
251 fastboot_response("OKAY", response, "0x%016zx", size);
252}
253#endif
254
255/**
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}