blob: dd3475e0a8bd1afefe61a91534b0a8d1fee2bc38 [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>
Simon Glass0af6e2d2019-08-01 09:46:52 -06007#include <env.h>
Alex Kiernand5aa57c2018-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 Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000014#include <version.h>
15
16static void getvar_version(char *var_parameter, char *response);
Sam Protsenkoac21e742019-07-03 19:34:07 +030017static void getvar_version_bootloader(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-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 Roscafb614d42019-04-09 21:11:40 +020022static void getvar_platform(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000023static void getvar_current_slot(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000024static void getvar_has_slot(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000025static void getvar_partition_type(char *part_name, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000026static void getvar_partition_size(char *part_name, char *response);
Sam Protsenko0d748252019-07-03 19:00:22 +030027static void getvar_is_userspace(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000028
29static const struct {
30 const char *variable;
31 void (*dispatch)(char *var_parameter, char *response);
32} getvar_dispatch[] = {
33 {
34 .variable = "version",
35 .dispatch = getvar_version
36 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000037 .variable = "version-bootloader",
Sam Protsenkoac21e742019-07-03 19:34:07 +030038 .dispatch = getvar_version_bootloader
Alex Kiernand5aa57c2018-05-29 15:30:53 +000039 }, {
40 .variable = "downloadsize",
41 .dispatch = getvar_downloadsize
42 }, {
43 .variable = "max-download-size",
44 .dispatch = getvar_downloadsize
45 }, {
46 .variable = "serialno",
47 .dispatch = getvar_serialno
48 }, {
49 .variable = "version-baseband",
50 .dispatch = getvar_version_baseband
51 }, {
52 .variable = "product",
53 .dispatch = getvar_product
54 }, {
Eugeniu Roscafb614d42019-04-09 21:11:40 +020055 .variable = "platform",
56 .dispatch = getvar_platform
57 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000058 .variable = "current-slot",
59 .dispatch = getvar_current_slot
Simon Glass3eaba0792023-02-05 15:39:53 -070060#if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000061 }, {
Eugeniu Rosca2ef97cf2019-03-26 17:46:14 +010062 .variable = "has-slot",
Alex Kiernand5aa57c2018-05-29 15:30:53 +000063 .dispatch = getvar_has_slot
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030064#endif
Simon Glass091a0712023-02-05 17:54:12 -070065#if IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000066 }, {
67 .variable = "partition-type",
68 .dispatch = getvar_partition_type
69#endif
Simon Glass3eaba0792023-02-05 15:39:53 -070070#if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000071 }, {
72 .variable = "partition-size",
73 .dispatch = getvar_partition_size
74#endif
Sam Protsenko0d748252019-07-03 19:00:22 +030075 }, {
76 .variable = "is-userspace",
77 .dispatch = getvar_is_userspace
Alex Kiernand5aa57c2018-05-29 15:30:53 +000078 }
79};
80
Sam Protsenko6646bbd2019-06-13 21:11:08 +030081/**
82 * Get partition number and size for any storage type.
83 *
84 * Can be used to check if partition with specified name exists.
85 *
86 * If error occurs, this function guarantees to fill @p response with fail
87 * string. @p response can be rewritten in caller, if needed.
88 *
89 * @param[in] part_name Info for which partition name to look for
90 * @param[in,out] response Pointer to fastboot response buffer
Gary Bissonf8674282020-08-27 10:51:14 +020091 * @param[out] size If not NULL, will contain partition size
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010092 * Return: Partition number or negative value on error
Sam Protsenko6646bbd2019-06-13 21:11:08 +030093 */
94static int getvar_get_part_info(const char *part_name, char *response,
95 size_t *size)
96{
97 int r;
Sam Protsenko6646bbd2019-06-13 21:11:08 +030098 struct blk_desc *dev_desc;
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010099 struct disk_partition disk_part;
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300100 struct part_info *part_info;
101
Simon Glass091a0712023-02-05 17:54:12 -0700102 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100103 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &disk_part,
104 response);
105 if (r >= 0 && size)
106 *size = disk_part.size * disk_part.blksz;
Simon Glass067995932023-02-05 17:54:13 -0700107 } else if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100108 r = fastboot_nand_get_part_info(part_name, &part_info, response);
109 if (r >= 0 && size)
110 *size = part_info->size;
111 } else {
112 fastboot_fail("this storage is not supported in bootloader", response);
113 r = -ENODEV;
114 }
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300115
116 return r;
117}
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300118
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000119static void getvar_version(char *var_parameter, char *response)
120{
121 fastboot_okay(FASTBOOT_VERSION, response);
122}
123
Sam Protsenkoac21e742019-07-03 19:34:07 +0300124static void getvar_version_bootloader(char *var_parameter, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000125{
126 fastboot_okay(U_BOOT_VERSION, response);
127}
128
129static void getvar_downloadsize(char *var_parameter, char *response)
130{
131 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
132}
133
134static void getvar_serialno(char *var_parameter, char *response)
135{
136 const char *tmp = env_get("serial#");
137
138 if (tmp)
139 fastboot_okay(tmp, response);
140 else
141 fastboot_fail("Value not set", response);
142}
143
144static void getvar_version_baseband(char *var_parameter, char *response)
145{
146 fastboot_okay("N/A", response);
147}
148
149static void getvar_product(char *var_parameter, char *response)
150{
151 const char *board = env_get("board");
152
153 if (board)
154 fastboot_okay(board, response);
155 else
156 fastboot_fail("Board not set", response);
157}
158
Eugeniu Roscafb614d42019-04-09 21:11:40 +0200159static void getvar_platform(char *var_parameter, char *response)
160{
161 const char *p = env_get("platform");
162
163 if (p)
164 fastboot_okay(p, response);
165 else
166 fastboot_fail("platform not set", response);
167}
168
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000169static void getvar_current_slot(char *var_parameter, char *response)
170{
Sam Protsenkofd7467e2019-06-13 00:49:45 +0300171 /* A/B not implemented, for now always return "a" */
172 fastboot_okay("a", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000173}
174
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100175static void __maybe_unused getvar_has_slot(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000176{
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300177 char part_name_wslot[PART_NAME_LEN];
178 size_t len;
179 int r;
180
181 if (!part_name || part_name[0] == '\0')
182 goto fail;
183
184 /* part_name_wslot = part_name + "_a" */
185 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
186 if (len > PART_NAME_LEN - 3)
187 goto fail;
188 strcat(part_name_wslot, "_a");
189
190 r = getvar_get_part_info(part_name_wslot, response, NULL);
191 if (r >= 0) {
192 fastboot_okay("yes", response); /* part exists and slotted */
193 return;
194 }
195
196 r = getvar_get_part_info(part_name, response, NULL);
197 if (r >= 0)
198 fastboot_okay("no", response); /* part exists but not slotted */
199
200 /* At this point response is filled with okay or fail string */
201 return;
202
203fail:
204 fastboot_fail("invalid partition name", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000205}
206
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100207static void __maybe_unused getvar_partition_type(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000208{
209 int r;
210 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600211 struct disk_partition part_info;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000212
213 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
214 response);
215 if (r >= 0) {
216 r = fs_set_blk_dev_with_part(dev_desc, r);
217 if (r < 0)
218 fastboot_fail("failed to set partition", response);
219 else
220 fastboot_okay(fs_get_type_name(), response);
221 }
222}
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000223
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100224static void __maybe_unused getvar_partition_size(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000225{
226 int r;
227 size_t size;
228
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300229 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000230 if (r >= 0)
231 fastboot_response("OKAY", response, "0x%016zx", size);
232}
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000233
Sam Protsenko0d748252019-07-03 19:00:22 +0300234static void getvar_is_userspace(char *var_parameter, char *response)
235{
236 fastboot_okay("no", response);
237}
238
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000239/**
240 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
241 *
242 * @cmd_parameter: Pointer to command parameter
243 * @response: Pointer to fastboot response buffer
244 *
245 * Look up cmd_parameter first as an environment variable of the form
246 * fastboot.<cmd_parameter>, if that exists return use its value to set
247 * response.
248 *
249 * Otherwise lookup the name of variable and execute the appropriate
250 * function to return the requested value.
251 */
252void fastboot_getvar(char *cmd_parameter, char *response)
253{
254 if (!cmd_parameter) {
255 fastboot_fail("missing var", response);
256 } else {
257#define FASTBOOT_ENV_PREFIX "fastboot."
258 int i;
259 char *var_parameter = cmd_parameter;
260 char envstr[FASTBOOT_RESPONSE_LEN];
261 const char *s;
262
263 snprintf(envstr, sizeof(envstr) - 1,
264 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
265 s = env_get(envstr);
266 if (s) {
267 fastboot_response("OKAY", response, "%s", s);
268 return;
269 }
270
271 strsep(&var_parameter, ":");
272 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
273 if (!strcmp(getvar_dispatch[i].variable,
274 cmd_parameter)) {
275 getvar_dispatch[i].dispatch(var_parameter,
276 response);
277 return;
278 }
279 }
280 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
281 fastboot_fail("Variable not implemented", response);
282 }
283}