blob: 98b2a71087a27b1721f4bed4160095d65ec75402 [file] [log] [blame]
Eugeniu Rosca35c34be2019-05-23 17:32:22 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Eugeniu Rosca <rosca.eugeniu@gmail.com>
4 *
5 * Command to read/modify/write Android BCB fields
6 */
7
8#include <android_bootloader_message.h>
Eugeniu Rosca54a55482020-10-23 11:52:22 +03009#include <bcb.h>
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020010#include <command.h>
Simon Glass1ab16922022-07-31 12:28:48 -060011#include <display_options.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Eugeniu Rosca6005e932020-10-23 11:52:23 +030014#include <malloc.h>
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010015#include <memalign.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060016#include <vsprintf.h>
Mattijs Korpershoekcb254102022-10-13 14:25:59 +020017#include <linux/err.h>
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020018
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000019static const char * const fields[] = {
20 "command",
21 "status",
22 "recovery",
23 "stage"
24};
25
Gary Bissonf3f2c7b2022-01-11 18:06:06 +010026static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000027static struct disk_partition partition_data;
28
29static struct blk_desc *block;
30static struct disk_partition *partition = &partition_data;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020031
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +030032static int bcb_not_loaded(void)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020033{
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +030034 printf("Error: Please, load BCB first!\n");
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020035 return -1;
36}
37
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000038static int bcb_field_get(const char *name, char **fieldp, int *sizep)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020039{
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020040 if (!strcmp(name, "command")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020041 *fieldp = bcb.command;
42 *sizep = sizeof(bcb.command);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020043 } else if (!strcmp(name, "status")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020044 *fieldp = bcb.status;
45 *sizep = sizeof(bcb.status);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020046 } else if (!strcmp(name, "recovery")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020047 *fieldp = bcb.recovery;
48 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020049 } else if (!strcmp(name, "stage")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020050 *fieldp = bcb.stage;
51 *sizep = sizeof(bcb.stage);
Eugeniu Rosca9bb62832019-07-19 23:26:13 +020052 } else if (!strcmp(name, "reserved")) {
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020053 *fieldp = bcb.reserved;
54 *sizep = sizeof(bcb.reserved);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020055 } else {
56 printf("Error: Unknown bcb field '%s'\n", name);
57 return -1;
58 }
59
60 return 0;
61}
62
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000063static void __bcb_reset(void)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020064{
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000065 block = NULL;
66 partition = &partition_data;
67 memset(&partition_data, 0, sizeof(struct disk_partition));
68 memset(&bcb, 0, sizeof(struct bootloader_message));
69}
70
71static int __bcb_initialize(const char *iface, int devnum, const char *partp)
72{
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020073 char *endp;
74 int part, ret;
75
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000076 block = blk_get_dev(iface, devnum);
77 if (!block) {
Eugeniu Roscaf308af52020-10-23 11:52:19 +030078 ret = -ENODEV;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020079 goto err_read_fail;
Eugeniu Roscaf308af52020-10-23 11:52:19 +030080 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020081
Mattijs Korpershoekcb254102022-10-13 14:25:59 +020082 /*
Dmitrii Merkurev927e2102023-11-10 05:59:54 +000083 * always select the first hwpart in case another
Mattijs Korpershoekcb254102022-10-13 14:25:59 +020084 * blk operation selected a different hwpart
85 */
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000086 ret = blk_dselect_hwpart(block, 0);
Mattijs Korpershoekcb254102022-10-13 14:25:59 +020087 if (IS_ERR_VALUE(ret)) {
88 ret = -ENODEV;
89 goto err_read_fail;
90 }
91
Eugeniu Roscaf308af52020-10-23 11:52:19 +030092 part = simple_strtoul(partp, &endp, 0);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020093 if (*endp == '\0') {
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000094 ret = part_get_info(block, part, partition);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020095 if (ret)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +020096 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020097 } else {
Dmitrii Merkurevacb22812023-11-10 05:59:55 +000098 part = part_get_info_by_name(block, partp, partition);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +020099 if (part < 0) {
100 ret = part;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200101 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200102 }
103 }
104
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000105 return CMD_RET_SUCCESS;
106
107err_read_fail:
Mattijs Korpershoek35ec2972024-07-24 11:51:06 +0200108 printf("Error: %s %d:%s read failed (%d)\n", iface, devnum,
109 partition->name, ret);
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000110 __bcb_reset();
111 return CMD_RET_FAILURE;
112}
113
114static int __bcb_load(void)
115{
116 u64 cnt;
117 int ret;
118
119 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz);
120 if (cnt > partition->size)
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200121 goto err_too_small;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200122
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000123 if (blk_dread(block, partition->start, cnt, &bcb) != cnt) {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200124 ret = -EIO;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200125 goto err_read_fail;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200126 }
127
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000128 debug("%s: Loaded from %d %d:%s\n", __func__, block->uclass_id,
129 block->devnum, partition->name);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200130
131 return CMD_RET_SUCCESS;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200132err_read_fail:
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000133 printf("Error: %d %d:%s read failed (%d)\n", block->uclass_id,
134 block->devnum, partition->name, ret);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200135 goto err;
Eugeniu Roscacb10fc92019-07-19 23:26:14 +0200136err_too_small:
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000137 printf("Error: %d %d:%s too small!", block->uclass_id,
138 block->devnum, partition->name);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200139err:
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000140 __bcb_reset();
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200141 return CMD_RET_FAILURE;
142}
143
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300144static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
145 char * const argv[])
146{
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000147 int ret;
Dmitrii Merkurev927e2102023-11-10 05:59:54 +0000148 int devnum;
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300149 char *endp;
Dmitrii Merkurev927e2102023-11-10 05:59:54 +0000150 char *iface = "mmc";
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300151
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300152 if (argc < 3)
153 return CMD_RET_USAGE;
154
Dmitrii Merkurev927e2102023-11-10 05:59:54 +0000155 if (argc == 4) {
156 iface = argv[1];
157 argc--;
158 argv++;
159 }
160
161 devnum = simple_strtoul(argv[1], &endp, 0);
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300162 if (*endp != '\0') {
163 printf("Error: Device id '%s' not a number\n", argv[1]);
164 return CMD_RET_FAILURE;
165 }
166
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000167 ret = __bcb_initialize(iface, devnum, argv[2]);
168 if (ret != CMD_RET_SUCCESS)
169 return ret;
170
171 return __bcb_load();
Eugeniu Roscaf308af52020-10-23 11:52:19 +0300172}
173
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000174static int __bcb_set(const char *fieldp, const char *valp)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200175{
176 int size, len;
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300177 char *field, *str, *found, *tmp;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200178
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300179 if (bcb_field_get(fieldp, &field, &size))
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200180 return CMD_RET_FAILURE;
181
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300182 len = strlen(valp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200183 if (len >= size) {
184 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300185 valp, len, size, fieldp);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200186 return CMD_RET_FAILURE;
187 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300188 str = strdup(valp);
189 if (!str) {
190 printf("Error: Out of memory while strdup\n");
191 return CMD_RET_FAILURE;
192 }
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200193
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300194 tmp = str;
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200195 field[0] = '\0';
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300196 while ((found = strsep(&tmp, ":"))) {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200197 if (field[0] != '\0')
198 strcat(field, "\n");
199 strcat(field, found);
200 }
Eugeniu Rosca6005e932020-10-23 11:52:23 +0300201 free(str);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200202
203 return CMD_RET_SUCCESS;
204}
205
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300206static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
207 char * const argv[])
208{
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300209 if (argc < 3)
210 return CMD_RET_USAGE;
211
212 if (!block)
213 return bcb_not_loaded();
214
Eugeniu Rosca92be5442020-10-23 11:52:20 +0300215 return __bcb_set(argv[1], argv[2]);
216}
217
Simon Glassed38aef2020-05-10 11:40:03 -0600218static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
219 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200220{
221 int size;
222 char *field;
223
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300224 if (!block)
225 return bcb_not_loaded();
226
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200227 if (argc == 1) {
228 memset(&bcb, 0, sizeof(bcb));
229 return CMD_RET_SUCCESS;
230 }
231
232 if (bcb_field_get(argv[1], &field, &size))
233 return CMD_RET_FAILURE;
234
235 memset(field, 0, size);
236
237 return CMD_RET_SUCCESS;
238}
239
Simon Glassed38aef2020-05-10 11:40:03 -0600240static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
241 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200242{
243 int size;
244 char *field;
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300245 char *op;
246
247 if (argc < 4)
248 return CMD_RET_USAGE;
249
250 if (!block)
251 return bcb_not_loaded();
252
253 op = argv[2];
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200254
255 if (bcb_field_get(argv[1], &field, &size))
256 return CMD_RET_FAILURE;
257
258 if (*op == '=' && *(op + 1) == '\0') {
259 if (!strncmp(argv[3], field, size))
260 return CMD_RET_SUCCESS;
261 else
262 return CMD_RET_FAILURE;
263 } else if (*op == '~' && *(op + 1) == '\0') {
264 if (!strstr(field, argv[3]))
265 return CMD_RET_FAILURE;
266 else
267 return CMD_RET_SUCCESS;
268 } else {
269 printf("Error: Unknown operator '%s'\n", op);
270 }
271
272 return CMD_RET_FAILURE;
273}
274
Simon Glassed38aef2020-05-10 11:40:03 -0600275static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
276 char *const argv[])
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200277{
278 int size;
279 char *field;
280
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300281 if (argc < 2)
282 return CMD_RET_USAGE;
283
284 if (!block)
285 return bcb_not_loaded();
286
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200287 if (bcb_field_get(argv[1], &field, &size))
288 return CMD_RET_FAILURE;
289
290 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
291
292 return CMD_RET_SUCCESS;
293}
294
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300295static int __bcb_store(void)
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200296{
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200297 u64 cnt;
298 int ret;
299
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000300 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200301
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000302 if (blk_dwrite(block, partition->start, cnt, &bcb) != cnt) {
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200303 ret = -EIO;
304 goto err;
305 }
306
307 return CMD_RET_SUCCESS;
308err:
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000309 printf("Error: %d %d:%s write failed (%d)\n", block->uclass_id,
310 block->devnum, partition->name, ret);
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200311
312 return CMD_RET_FAILURE;
313}
314
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300315static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
316 char * const argv[])
317{
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300318 if (!block)
319 return bcb_not_loaded();
320
Eugeniu Roscaf6155bd2020-10-23 11:52:21 +0300321 return __bcb_store();
322}
323
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000324int bcb_find_partition_and_load(const char *iface, int devnum, char *partp)
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300325{
326 int ret;
327
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000328 __bcb_reset();
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300329
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000330 ret = __bcb_initialize(iface, devnum, partp);
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300331 if (ret != CMD_RET_SUCCESS)
332 return ret;
333
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000334 return __bcb_load();
335}
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300336
Dmitrii Merkurevacb22812023-11-10 05:59:55 +0000337int bcb_load(struct blk_desc *block_description, struct disk_partition *disk_partition)
338{
339 __bcb_reset();
340
341 block = block_description;
342 partition = disk_partition;
343
344 return __bcb_load();
345}
346
347int bcb_set(enum bcb_field field, const char *value)
348{
349 if (field > BCB_FIELD_STAGE)
350 return CMD_RET_FAILURE;
351 return __bcb_set(fields[field], value);
352}
353
354int bcb_get(enum bcb_field field, char *value_out, size_t value_size)
355{
356 int size;
357 char *field_value;
358
359 if (field > BCB_FIELD_STAGE)
360 return CMD_RET_FAILURE;
361 if (bcb_field_get(fields[field], &field_value, &size))
362 return CMD_RET_FAILURE;
363
364 strlcpy(value_out, field_value, value_size);
365
366 return CMD_RET_SUCCESS;
367}
368
369int bcb_store(void)
370{
371 return __bcb_store();
372}
373
374void bcb_reset(void)
375{
376 __bcb_reset();
Eugeniu Rosca54a55482020-10-23 11:52:22 +0300377}
378
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300379U_BOOT_LONGHELP(bcb,
Dmitrii Merkurev927e2102023-11-10 05:59:54 +0000380 "load <interface> <dev> <part> - load BCB from <interface> <dev>:<part>\n"
381 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
382 "bcb set <field> <val> - set BCB <field> to <val>\n"
383 "bcb clear [<field>] - clear BCB <field> or all fields\n"
384 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
385 "bcb dump <field> - dump BCB <field>\n"
386 "bcb store - store BCB back to <interface>\n"
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200387 "\n"
388 "Legend:\n"
Dmitrii Merkurev927e2102023-11-10 05:59:54 +0000389 "<interface> - storage device interface (virtio, mmc, etc)\n"
390 "<dev> - storage device index containing the BCB partition\n"
391 "<part> - partition index or name containing the BCB\n"
392 "<field> - one of {command,status,recovery,stage,reserved}\n"
393 "<op> - the binary operator used in 'bcb test':\n"
394 " '=' returns true if <val> matches the string stored in <field>\n"
395 " '~' returns true if <val> matches a subset of <field>'s string\n"
396 "<val> - string/text provided as input to bcb {set,test}\n"
397 " NOTE: any ':' character in <val> will be replaced by line feed\n"
398 " during 'bcb set' and used as separator by upper layers\n"
Eugeniu Rosca35c34be2019-05-23 17:32:22 +0200399);
Dmitry Rokosov4adb34e2024-10-17 17:12:07 +0300400
401U_BOOT_CMD_WITH_SUBCMDS(bcb,
402 "Load/set/clear/test/dump/store Android BCB fields", bcb_help_text,
403 U_BOOT_SUBCMD_MKENT(load, 4, 1, do_bcb_load),
404 U_BOOT_SUBCMD_MKENT(set, 3, 1, do_bcb_set),
405 U_BOOT_SUBCMD_MKENT(clear, 2, 1, do_bcb_clear),
406 U_BOOT_SUBCMD_MKENT(test, 4, 1, do_bcb_test),
407 U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_bcb_dump),
408 U_BOOT_SUBCMD_MKENT(store, 1, 1, do_bcb_store),
409);