blob: e03218066bf22794342a1daa827b5fd80623beb3 [file] [log] [blame]
Eugeniu Roscadb7b7a02019-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>
9#include <command.h>
10#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include <part.h>
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020013
14enum bcb_cmd {
15 BCB_CMD_LOAD,
16 BCB_CMD_FIELD_SET,
17 BCB_CMD_FIELD_CLEAR,
18 BCB_CMD_FIELD_TEST,
19 BCB_CMD_FIELD_DUMP,
20 BCB_CMD_STORE,
21};
22
23static int bcb_dev = -1;
24static int bcb_part = -1;
25static struct bootloader_message bcb = { { 0 } };
26
27static int bcb_cmd_get(char *cmd)
28{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020029 if (!strcmp(cmd, "load"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020030 return BCB_CMD_LOAD;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020031 if (!strcmp(cmd, "set"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020032 return BCB_CMD_FIELD_SET;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020033 if (!strcmp(cmd, "clear"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020034 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020035 if (!strcmp(cmd, "test"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020036 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020037 if (!strcmp(cmd, "store"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020038 return BCB_CMD_STORE;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020039 if (!strcmp(cmd, "dump"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020040 return BCB_CMD_FIELD_DUMP;
41 else
42 return -1;
43}
44
45static int bcb_is_misused(int argc, char *const argv[])
46{
47 int cmd = bcb_cmd_get(argv[0]);
48
49 switch (cmd) {
50 case BCB_CMD_LOAD:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020051 case BCB_CMD_FIELD_SET:
52 if (argc != 3)
53 goto err;
54 break;
55 case BCB_CMD_FIELD_TEST:
56 if (argc != 4)
57 goto err;
58 break;
59 case BCB_CMD_FIELD_CLEAR:
60 if (argc != 1 && argc != 2)
61 goto err;
62 break;
63 case BCB_CMD_STORE:
64 if (argc != 1)
65 goto err;
66 break;
67 case BCB_CMD_FIELD_DUMP:
68 if (argc != 2)
69 goto err;
70 break;
71 default:
72 printf("Error: 'bcb %s' not supported\n", argv[0]);
73 return -1;
74 }
75
76 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
77 printf("Error: Please, load BCB first!\n");
78 return -1;
79 }
80
81 return 0;
82err:
83 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
84
85 return -1;
86}
87
Eugeniu Rosca37f18112019-07-19 23:26:14 +020088static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020089{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020090 if (!strcmp(name, "command")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020091 *fieldp = bcb.command;
92 *sizep = sizeof(bcb.command);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020093 } else if (!strcmp(name, "status")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020094 *fieldp = bcb.status;
95 *sizep = sizeof(bcb.status);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020096 } else if (!strcmp(name, "recovery")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020097 *fieldp = bcb.recovery;
98 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020099 } else if (!strcmp(name, "stage")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200100 *fieldp = bcb.stage;
101 *sizep = sizeof(bcb.stage);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200102 } else if (!strcmp(name, "reserved")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200103 *fieldp = bcb.reserved;
104 *sizep = sizeof(bcb.reserved);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200105 } else {
106 printf("Error: Unknown bcb field '%s'\n", name);
107 return -1;
108 }
109
110 return 0;
111}
112
Simon Glass09140112020-05-10 11:40:03 -0600113static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200115{
116 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600117 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200118 u64 cnt;
119 char *endp;
120 int part, ret;
121
122 ret = blk_get_device_by_str("mmc", argv[1], &desc);
123 if (ret < 0)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200124 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200125
126 part = simple_strtoul(argv[2], &endp, 0);
127 if (*endp == '\0') {
128 ret = part_get_info(desc, part, &info);
129 if (ret)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200130 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200131 } else {
132 part = part_get_info_by_name(desc, argv[2], &info);
133 if (part < 0) {
134 ret = part;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200135 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200136 }
137 }
138
139 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
140 if (cnt > info.size)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200141 goto err_too_small;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200142
143 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
144 ret = -EIO;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200145 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200146 }
147
148 bcb_dev = desc->devnum;
149 bcb_part = part;
150 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
151
152 return CMD_RET_SUCCESS;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200153err_read_fail:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200154 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
155 goto err;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200156err_too_small:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200157 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
158 goto err;
159err:
160 bcb_dev = -1;
161 bcb_part = -1;
162
163 return CMD_RET_FAILURE;
164}
165
Simon Glass09140112020-05-10 11:40:03 -0600166static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
167 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200168{
169 int size, len;
170 char *field, *str, *found;
171
172 if (bcb_field_get(argv[1], &field, &size))
173 return CMD_RET_FAILURE;
174
175 len = strlen(argv[2]);
176 if (len >= size) {
177 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
178 argv[2], len, size, argv[1]);
179 return CMD_RET_FAILURE;
180 }
181 str = argv[2];
182
183 field[0] = '\0';
184 while ((found = strsep(&str, ":"))) {
185 if (field[0] != '\0')
186 strcat(field, "\n");
187 strcat(field, found);
188 }
189
190 return CMD_RET_SUCCESS;
191}
192
Simon Glass09140112020-05-10 11:40:03 -0600193static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
194 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200195{
196 int size;
197 char *field;
198
199 if (argc == 1) {
200 memset(&bcb, 0, sizeof(bcb));
201 return CMD_RET_SUCCESS;
202 }
203
204 if (bcb_field_get(argv[1], &field, &size))
205 return CMD_RET_FAILURE;
206
207 memset(field, 0, size);
208
209 return CMD_RET_SUCCESS;
210}
211
Simon Glass09140112020-05-10 11:40:03 -0600212static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
213 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200214{
215 int size;
216 char *field;
217 char *op = argv[2];
218
219 if (bcb_field_get(argv[1], &field, &size))
220 return CMD_RET_FAILURE;
221
222 if (*op == '=' && *(op + 1) == '\0') {
223 if (!strncmp(argv[3], field, size))
224 return CMD_RET_SUCCESS;
225 else
226 return CMD_RET_FAILURE;
227 } else if (*op == '~' && *(op + 1) == '\0') {
228 if (!strstr(field, argv[3]))
229 return CMD_RET_FAILURE;
230 else
231 return CMD_RET_SUCCESS;
232 } else {
233 printf("Error: Unknown operator '%s'\n", op);
234 }
235
236 return CMD_RET_FAILURE;
237}
238
Simon Glass09140112020-05-10 11:40:03 -0600239static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
240 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200241{
242 int size;
243 char *field;
244
245 if (bcb_field_get(argv[1], &field, &size))
246 return CMD_RET_FAILURE;
247
248 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
249
250 return CMD_RET_SUCCESS;
251}
252
Simon Glass09140112020-05-10 11:40:03 -0600253static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
254 char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200255{
256 struct blk_desc *desc;
Simon Glass05289792020-05-10 11:39:57 -0600257 struct disk_partition info;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200258 u64 cnt;
259 int ret;
260
261 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
262 if (!desc) {
263 ret = -ENODEV;
264 goto err;
265 }
266
267 ret = part_get_info(desc, bcb_part, &info);
268 if (ret)
269 goto err;
270
271 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
272
273 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
274 ret = -EIO;
275 goto err;
276 }
277
278 return CMD_RET_SUCCESS;
279err:
280 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
281
282 return CMD_RET_FAILURE;
283}
284
Simon Glass09140112020-05-10 11:40:03 -0600285static struct cmd_tbl cmd_bcb_sub[] = {
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200286 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
287 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
288 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
289 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
290 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
291 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
292};
293
Simon Glass09140112020-05-10 11:40:03 -0600294static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200295{
Simon Glass09140112020-05-10 11:40:03 -0600296 struct cmd_tbl *c;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200297
298 if (argc < 2)
299 return CMD_RET_USAGE;
300
301 argc--;
302 argv++;
303
304 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
305 if (!c)
306 return CMD_RET_USAGE;
307
308 if (bcb_is_misused(argc, argv)) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200309 /*
310 * We try to improve the user experience by reporting the
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200311 * root-cause of misusage, so don't return CMD_RET_USAGE,
312 * since the latter prints out the full-blown help text
313 */
314 return CMD_RET_FAILURE;
315 }
316
317 return c->cmd(cmdtp, flag, argc, argv);
318}
319
320U_BOOT_CMD(
321 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
322 "Load/set/clear/test/dump/store Android BCB fields",
323 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
324 "bcb set <field> <val> - set BCB <field> to <val>\n"
325 "bcb clear [<field>] - clear BCB <field> or all fields\n"
326 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
327 "bcb dump <field> - dump BCB <field>\n"
328 "bcb store - store BCB back to mmc\n"
329 "\n"
330 "Legend:\n"
331 "<dev> - MMC device index containing the BCB partition\n"
332 "<part> - MMC partition index or name containing the BCB\n"
333 "<field> - one of {command,status,recovery,stage,reserved}\n"
334 "<op> - the binary operator used in 'bcb test':\n"
335 " '=' returns true if <val> matches the string stored in <field>\n"
336 " '~' returns true if <val> matches a subset of <field>'s string\n"
337 "<val> - string/text provided as input to bcb {set,test}\n"
338 " NOTE: any ':' character in <val> will be replaced by line feed\n"
339 " during 'bcb set' and used as separator by upper layers\n"
340);