blob: 9626f2c69e345c984c83a730fdae1ba3bd11024d [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>
11
12enum bcb_cmd {
13 BCB_CMD_LOAD,
14 BCB_CMD_FIELD_SET,
15 BCB_CMD_FIELD_CLEAR,
16 BCB_CMD_FIELD_TEST,
17 BCB_CMD_FIELD_DUMP,
18 BCB_CMD_STORE,
19};
20
21static int bcb_dev = -1;
22static int bcb_part = -1;
23static struct bootloader_message bcb = { { 0 } };
24
25static int bcb_cmd_get(char *cmd)
26{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020027 if (!strcmp(cmd, "load"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020028 return BCB_CMD_LOAD;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020029 if (!strcmp(cmd, "set"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020030 return BCB_CMD_FIELD_SET;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020031 if (!strcmp(cmd, "clear"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020032 return BCB_CMD_FIELD_CLEAR;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020033 if (!strcmp(cmd, "test"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020034 return BCB_CMD_FIELD_TEST;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020035 if (!strcmp(cmd, "store"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020036 return BCB_CMD_STORE;
Eugeniu Rosca739168c2019-07-19 23:26:13 +020037 if (!strcmp(cmd, "dump"))
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020038 return BCB_CMD_FIELD_DUMP;
39 else
40 return -1;
41}
42
43static int bcb_is_misused(int argc, char *const argv[])
44{
45 int cmd = bcb_cmd_get(argv[0]);
46
47 switch (cmd) {
48 case BCB_CMD_LOAD:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020049 case BCB_CMD_FIELD_SET:
50 if (argc != 3)
51 goto err;
52 break;
53 case BCB_CMD_FIELD_TEST:
54 if (argc != 4)
55 goto err;
56 break;
57 case BCB_CMD_FIELD_CLEAR:
58 if (argc != 1 && argc != 2)
59 goto err;
60 break;
61 case BCB_CMD_STORE:
62 if (argc != 1)
63 goto err;
64 break;
65 case BCB_CMD_FIELD_DUMP:
66 if (argc != 2)
67 goto err;
68 break;
69 default:
70 printf("Error: 'bcb %s' not supported\n", argv[0]);
71 return -1;
72 }
73
74 if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
75 printf("Error: Please, load BCB first!\n");
76 return -1;
77 }
78
79 return 0;
80err:
81 printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
82
83 return -1;
84}
85
Eugeniu Rosca37f18112019-07-19 23:26:14 +020086static int bcb_field_get(char *name, char **fieldp, int *sizep)
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +020087{
Eugeniu Rosca739168c2019-07-19 23:26:13 +020088 if (!strcmp(name, "command")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020089 *fieldp = bcb.command;
90 *sizep = sizeof(bcb.command);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020091 } else if (!strcmp(name, "status")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020092 *fieldp = bcb.status;
93 *sizep = sizeof(bcb.status);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020094 } else if (!strcmp(name, "recovery")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020095 *fieldp = bcb.recovery;
96 *sizep = sizeof(bcb.recovery);
Eugeniu Rosca739168c2019-07-19 23:26:13 +020097 } else if (!strcmp(name, "stage")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +020098 *fieldp = bcb.stage;
99 *sizep = sizeof(bcb.stage);
Eugeniu Rosca739168c2019-07-19 23:26:13 +0200100 } else if (!strcmp(name, "reserved")) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200101 *fieldp = bcb.reserved;
102 *sizep = sizeof(bcb.reserved);
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200103 } else {
104 printf("Error: Unknown bcb field '%s'\n", name);
105 return -1;
106 }
107
108 return 0;
109}
110
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200111static int do_bcb_load(cmd_tbl_t *cmdtp, int flag, int argc,
112 char * const argv[])
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200113{
114 struct blk_desc *desc;
115 disk_partition_t info;
116 u64 cnt;
117 char *endp;
118 int part, ret;
119
120 ret = blk_get_device_by_str("mmc", argv[1], &desc);
121 if (ret < 0)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200122 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200123
124 part = simple_strtoul(argv[2], &endp, 0);
125 if (*endp == '\0') {
126 ret = part_get_info(desc, part, &info);
127 if (ret)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200128 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200129 } else {
130 part = part_get_info_by_name(desc, argv[2], &info);
131 if (part < 0) {
132 ret = part;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200133 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200134 }
135 }
136
137 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
138 if (cnt > info.size)
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200139 goto err_too_small;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200140
141 if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
142 ret = -EIO;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200143 goto err_read_fail;
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200144 }
145
146 bcb_dev = desc->devnum;
147 bcb_part = part;
148 debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
149
150 return CMD_RET_SUCCESS;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200151err_read_fail:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200152 printf("Error: mmc %s:%s read failed (%d)\n", argv[1], argv[2], ret);
153 goto err;
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200154err_too_small:
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200155 printf("Error: mmc %s:%s too small!", argv[1], argv[2]);
156 goto err;
157err:
158 bcb_dev = -1;
159 bcb_part = -1;
160
161 return CMD_RET_FAILURE;
162}
163
164static int do_bcb_set(cmd_tbl_t *cmdtp, int flag, int argc,
165 char * const argv[])
166{
167 int size, len;
168 char *field, *str, *found;
169
170 if (bcb_field_get(argv[1], &field, &size))
171 return CMD_RET_FAILURE;
172
173 len = strlen(argv[2]);
174 if (len >= size) {
175 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
176 argv[2], len, size, argv[1]);
177 return CMD_RET_FAILURE;
178 }
179 str = argv[2];
180
181 field[0] = '\0';
182 while ((found = strsep(&str, ":"))) {
183 if (field[0] != '\0')
184 strcat(field, "\n");
185 strcat(field, found);
186 }
187
188 return CMD_RET_SUCCESS;
189}
190
191static int do_bcb_clear(cmd_tbl_t *cmdtp, int flag, int argc,
192 char * const argv[])
193{
194 int size;
195 char *field;
196
197 if (argc == 1) {
198 memset(&bcb, 0, sizeof(bcb));
199 return CMD_RET_SUCCESS;
200 }
201
202 if (bcb_field_get(argv[1], &field, &size))
203 return CMD_RET_FAILURE;
204
205 memset(field, 0, size);
206
207 return CMD_RET_SUCCESS;
208}
209
210static int do_bcb_test(cmd_tbl_t *cmdtp, int flag, int argc,
211 char * const argv[])
212{
213 int size;
214 char *field;
215 char *op = argv[2];
216
217 if (bcb_field_get(argv[1], &field, &size))
218 return CMD_RET_FAILURE;
219
220 if (*op == '=' && *(op + 1) == '\0') {
221 if (!strncmp(argv[3], field, size))
222 return CMD_RET_SUCCESS;
223 else
224 return CMD_RET_FAILURE;
225 } else if (*op == '~' && *(op + 1) == '\0') {
226 if (!strstr(field, argv[3]))
227 return CMD_RET_FAILURE;
228 else
229 return CMD_RET_SUCCESS;
230 } else {
231 printf("Error: Unknown operator '%s'\n", op);
232 }
233
234 return CMD_RET_FAILURE;
235}
236
237static int do_bcb_dump(cmd_tbl_t *cmdtp, int flag, int argc,
238 char * const argv[])
239{
240 int size;
241 char *field;
242
243 if (bcb_field_get(argv[1], &field, &size))
244 return CMD_RET_FAILURE;
245
246 print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
247
248 return CMD_RET_SUCCESS;
249}
250
251static int do_bcb_store(cmd_tbl_t *cmdtp, int flag, int argc,
252 char * const argv[])
253{
254 struct blk_desc *desc;
255 disk_partition_t info;
256 u64 cnt;
257 int ret;
258
259 desc = blk_get_devnum_by_type(IF_TYPE_MMC, bcb_dev);
260 if (!desc) {
261 ret = -ENODEV;
262 goto err;
263 }
264
265 ret = part_get_info(desc, bcb_part, &info);
266 if (ret)
267 goto err;
268
269 cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
270
271 if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
272 ret = -EIO;
273 goto err;
274 }
275
276 return CMD_RET_SUCCESS;
277err:
278 printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
279
280 return CMD_RET_FAILURE;
281}
282
283static cmd_tbl_t cmd_bcb_sub[] = {
284 U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
285 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
286 U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
287 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
288 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
289 U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
290};
291
292static int do_bcb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
293{
294 cmd_tbl_t *c;
295
296 if (argc < 2)
297 return CMD_RET_USAGE;
298
299 argc--;
300 argv++;
301
302 c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
303 if (!c)
304 return CMD_RET_USAGE;
305
306 if (bcb_is_misused(argc, argv)) {
Eugeniu Rosca37f18112019-07-19 23:26:14 +0200307 /*
308 * We try to improve the user experience by reporting the
Eugeniu Roscadb7b7a02019-05-23 17:32:22 +0200309 * root-cause of misusage, so don't return CMD_RET_USAGE,
310 * since the latter prints out the full-blown help text
311 */
312 return CMD_RET_FAILURE;
313 }
314
315 return c->cmd(cmdtp, flag, argc, argv);
316}
317
318U_BOOT_CMD(
319 bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
320 "Load/set/clear/test/dump/store Android BCB fields",
321 "load <dev> <part> - load BCB from mmc <dev>:<part>\n"
322 "bcb set <field> <val> - set BCB <field> to <val>\n"
323 "bcb clear [<field>] - clear BCB <field> or all fields\n"
324 "bcb test <field> <op> <val> - test BCB <field> against <val>\n"
325 "bcb dump <field> - dump BCB <field>\n"
326 "bcb store - store BCB back to mmc\n"
327 "\n"
328 "Legend:\n"
329 "<dev> - MMC device index containing the BCB partition\n"
330 "<part> - MMC partition index or name containing the BCB\n"
331 "<field> - one of {command,status,recovery,stage,reserved}\n"
332 "<op> - the binary operator used in 'bcb test':\n"
333 " '=' returns true if <val> matches the string stored in <field>\n"
334 " '~' returns true if <val> matches a subset of <field>'s string\n"
335 "<val> - string/text provided as input to bcb {set,test}\n"
336 " NOTE: any ':' character in <val> will be replaced by line feed\n"
337 " during 'bcb set' and used as separator by upper layers\n"
338);