blob: 8cb8ffa2c6c84648eeaf5fecb2184f066dc508a0 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000016
17static void getvar_version(char *var_parameter, char *response);
Sam Protsenkoac21e742019-07-03 19:34:07 +030018static void getvar_version_bootloader(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000019static void getvar_downloadsize(char *var_parameter, char *response);
20static void getvar_serialno(char *var_parameter, char *response);
21static void getvar_version_baseband(char *var_parameter, char *response);
22static void getvar_product(char *var_parameter, char *response);
Eugeniu Roscafb614d42019-04-09 21:11:40 +020023static void getvar_platform(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000024static void getvar_current_slot(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000025static void getvar_has_slot(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000026static void getvar_partition_type(char *part_name, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000027static void getvar_partition_size(char *part_name, char *response);
Sam Protsenko0d748252019-07-03 19:00:22 +030028static void getvar_is_userspace(char *var_parameter, char *response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000029
30static const struct {
31 const char *variable;
32 void (*dispatch)(char *var_parameter, char *response);
33} getvar_dispatch[] = {
34 {
35 .variable = "version",
36 .dispatch = getvar_version
37 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000038 .variable = "version-bootloader",
Sam Protsenkoac21e742019-07-03 19:34:07 +030039 .dispatch = getvar_version_bootloader
Alex Kiernand5aa57c2018-05-29 15:30:53 +000040 }, {
41 .variable = "downloadsize",
42 .dispatch = getvar_downloadsize
43 }, {
44 .variable = "max-download-size",
45 .dispatch = getvar_downloadsize
46 }, {
47 .variable = "serialno",
48 .dispatch = getvar_serialno
49 }, {
50 .variable = "version-baseband",
51 .dispatch = getvar_version_baseband
52 }, {
53 .variable = "product",
54 .dispatch = getvar_product
55 }, {
Eugeniu Roscafb614d42019-04-09 21:11:40 +020056 .variable = "platform",
57 .dispatch = getvar_platform
58 }, {
Alex Kiernand5aa57c2018-05-29 15:30:53 +000059 .variable = "current-slot",
60 .dispatch = getvar_current_slot
Simon Glass3eaba0792023-02-05 15:39:53 -070061#if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000062 }, {
Eugeniu Rosca2ef97cf2019-03-26 17:46:14 +010063 .variable = "has-slot",
Alex Kiernand5aa57c2018-05-29 15:30:53 +000064 .dispatch = getvar_has_slot
Igor Opaniuk73dac9b2019-06-13 21:11:09 +030065#endif
Simon Glass091a0712023-02-05 17:54:12 -070066#if IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000067 }, {
68 .variable = "partition-type",
69 .dispatch = getvar_partition_type
70#endif
Simon Glass3eaba0792023-02-05 15:39:53 -070071#if IS_ENABLED(CONFIG_FASTBOOT_FLASH)
Alex Kiernand5aa57c2018-05-29 15:30:53 +000072 }, {
73 .variable = "partition-size",
74 .dispatch = getvar_partition_size
75#endif
Sam Protsenko0d748252019-07-03 19:00:22 +030076 }, {
77 .variable = "is-userspace",
78 .dispatch = getvar_is_userspace
Alex Kiernand5aa57c2018-05-29 15:30:53 +000079 }
80};
81
Sam Protsenko6646bbd2019-06-13 21:11:08 +030082/**
83 * Get partition number and size for any storage type.
84 *
85 * Can be used to check if partition with specified name exists.
86 *
87 * If error occurs, this function guarantees to fill @p response with fail
88 * string. @p response can be rewritten in caller, if needed.
89 *
90 * @param[in] part_name Info for which partition name to look for
91 * @param[in,out] response Pointer to fastboot response buffer
Gary Bissonf8674282020-08-27 10:51:14 +020092 * @param[out] size If not NULL, will contain partition size
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010093 * Return: Partition number or negative value on error
Sam Protsenko6646bbd2019-06-13 21:11:08 +030094 */
95static int getvar_get_part_info(const char *part_name, char *response,
96 size_t *size)
97{
98 int r;
Sam Protsenko6646bbd2019-06-13 21:11:08 +030099 struct blk_desc *dev_desc;
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100100 struct disk_partition disk_part;
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300101 struct part_info *part_info;
102
Simon Glass091a0712023-02-05 17:54:12 -0700103 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100104 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &disk_part,
105 response);
106 if (r >= 0 && size)
107 *size = disk_part.size * disk_part.blksz;
Simon Glass067995932023-02-05 17:54:13 -0700108 } else if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_NAND)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100109 r = fastboot_nand_get_part_info(part_name, &part_info, response);
110 if (r >= 0 && size)
111 *size = part_info->size;
112 } else {
113 fastboot_fail("this storage is not supported in bootloader", response);
114 r = -ENODEV;
115 }
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300116
117 return r;
118}
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300119
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000120static void getvar_version(char *var_parameter, char *response)
121{
122 fastboot_okay(FASTBOOT_VERSION, response);
123}
124
Sam Protsenkoac21e742019-07-03 19:34:07 +0300125static void getvar_version_bootloader(char *var_parameter, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000126{
127 fastboot_okay(U_BOOT_VERSION, response);
128}
129
130static void getvar_downloadsize(char *var_parameter, char *response)
131{
132 fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
133}
134
135static void getvar_serialno(char *var_parameter, char *response)
136{
137 const char *tmp = env_get("serial#");
138
139 if (tmp)
140 fastboot_okay(tmp, response);
141 else
142 fastboot_fail("Value not set", response);
143}
144
145static void getvar_version_baseband(char *var_parameter, char *response)
146{
147 fastboot_okay("N/A", response);
148}
149
150static void getvar_product(char *var_parameter, char *response)
151{
152 const char *board = env_get("board");
153
154 if (board)
155 fastboot_okay(board, response);
156 else
157 fastboot_fail("Board not set", response);
158}
159
Eugeniu Roscafb614d42019-04-09 21:11:40 +0200160static void getvar_platform(char *var_parameter, char *response)
161{
162 const char *p = env_get("platform");
163
164 if (p)
165 fastboot_okay(p, response);
166 else
167 fastboot_fail("platform not set", response);
168}
169
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000170static void getvar_current_slot(char *var_parameter, char *response)
171{
Sam Protsenkofd7467e2019-06-13 00:49:45 +0300172 /* A/B not implemented, for now always return "a" */
173 fastboot_okay("a", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000174}
175
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100176static void __maybe_unused getvar_has_slot(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000177{
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300178 char part_name_wslot[PART_NAME_LEN];
179 size_t len;
180 int r;
181
182 if (!part_name || part_name[0] == '\0')
183 goto fail;
184
185 /* part_name_wslot = part_name + "_a" */
186 len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
Matthias Schiffer02482ee2023-07-14 13:24:50 +0200187 if (len >= PART_NAME_LEN - 3)
Igor Opaniuk73dac9b2019-06-13 21:11:09 +0300188 goto fail;
189 strcat(part_name_wslot, "_a");
190
191 r = getvar_get_part_info(part_name_wslot, response, NULL);
192 if (r >= 0) {
193 fastboot_okay("yes", response); /* part exists and slotted */
194 return;
195 }
196
197 r = getvar_get_part_info(part_name, response, NULL);
198 if (r >= 0)
199 fastboot_okay("no", response); /* part exists but not slotted */
200
201 /* At this point response is filled with okay or fail string */
202 return;
203
204fail:
205 fastboot_fail("invalid partition name", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000206}
207
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100208static void __maybe_unused getvar_partition_type(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000209{
210 int r;
211 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600212 struct disk_partition part_info;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000213
214 r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
215 response);
216 if (r >= 0) {
217 r = fs_set_blk_dev_with_part(dev_desc, r);
218 if (r < 0)
219 fastboot_fail("failed to set partition", response);
220 else
221 fastboot_okay(fs_get_type_name(), response);
222 }
223}
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000224
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100225static void __maybe_unused getvar_partition_size(char *part_name, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000226{
227 int r;
228 size_t size;
229
Sam Protsenko6646bbd2019-06-13 21:11:08 +0300230 r = getvar_get_part_info(part_name, response, &size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000231 if (r >= 0)
232 fastboot_response("OKAY", response, "0x%016zx", size);
233}
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000234
Sam Protsenko0d748252019-07-03 19:00:22 +0300235static void getvar_is_userspace(char *var_parameter, char *response)
236{
237 fastboot_okay("no", response);
238}
239
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000240/**
241 * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
242 *
243 * @cmd_parameter: Pointer to command parameter
244 * @response: Pointer to fastboot response buffer
245 *
246 * Look up cmd_parameter first as an environment variable of the form
247 * fastboot.<cmd_parameter>, if that exists return use its value to set
248 * response.
249 *
250 * Otherwise lookup the name of variable and execute the appropriate
251 * function to return the requested value.
252 */
253void fastboot_getvar(char *cmd_parameter, char *response)
254{
255 if (!cmd_parameter) {
256 fastboot_fail("missing var", response);
257 } else {
258#define FASTBOOT_ENV_PREFIX "fastboot."
259 int i;
260 char *var_parameter = cmd_parameter;
261 char envstr[FASTBOOT_RESPONSE_LEN];
262 const char *s;
263
264 snprintf(envstr, sizeof(envstr) - 1,
265 FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
266 s = env_get(envstr);
267 if (s) {
268 fastboot_response("OKAY", response, "%s", s);
269 return;
270 }
271
272 strsep(&var_parameter, ":");
273 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
274 if (!strcmp(getvar_dispatch[i].variable,
275 cmd_parameter)) {
276 getvar_dispatch[i].dispatch(var_parameter,
277 response);
278 return;
279 }
280 }
281 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
282 fastboot_fail("Variable not implemented", response);
283 }
284}