Merge tag 'u-boot-imx-next-20230404' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / cmd / bcb.c
1 // 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 <bcb.h>
10 #include <command.h>
11 #include <common.h>
12 #include <display_options.h>
13 #include <log.h>
14 #include <part.h>
15 #include <malloc.h>
16 #include <memalign.h>
17 #include <linux/err.h>
18
19 enum bcb_cmd {
20         BCB_CMD_LOAD,
21         BCB_CMD_FIELD_SET,
22         BCB_CMD_FIELD_CLEAR,
23         BCB_CMD_FIELD_TEST,
24         BCB_CMD_FIELD_DUMP,
25         BCB_CMD_STORE,
26 };
27
28 static int bcb_dev = -1;
29 static int bcb_part = -1;
30 static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } };
31
32 static int bcb_cmd_get(char *cmd)
33 {
34         if (!strcmp(cmd, "load"))
35                 return BCB_CMD_LOAD;
36         if (!strcmp(cmd, "set"))
37                 return BCB_CMD_FIELD_SET;
38         if (!strcmp(cmd, "clear"))
39                 return BCB_CMD_FIELD_CLEAR;
40         if (!strcmp(cmd, "test"))
41                 return BCB_CMD_FIELD_TEST;
42         if (!strcmp(cmd, "store"))
43                 return BCB_CMD_STORE;
44         if (!strcmp(cmd, "dump"))
45                 return BCB_CMD_FIELD_DUMP;
46         else
47                 return -1;
48 }
49
50 static int bcb_is_misused(int argc, char *const argv[])
51 {
52         int cmd = bcb_cmd_get(argv[0]);
53
54         switch (cmd) {
55         case BCB_CMD_LOAD:
56         case BCB_CMD_FIELD_SET:
57                 if (argc != 3)
58                         goto err;
59                 break;
60         case BCB_CMD_FIELD_TEST:
61                 if (argc != 4)
62                         goto err;
63                 break;
64         case BCB_CMD_FIELD_CLEAR:
65                 if (argc != 1 && argc != 2)
66                         goto err;
67                 break;
68         case BCB_CMD_STORE:
69                 if (argc != 1)
70                         goto err;
71                 break;
72         case BCB_CMD_FIELD_DUMP:
73                 if (argc != 2)
74                         goto err;
75                 break;
76         default:
77                 printf("Error: 'bcb %s' not supported\n", argv[0]);
78                 return -1;
79         }
80
81         if (cmd != BCB_CMD_LOAD && (bcb_dev < 0 || bcb_part < 0)) {
82                 printf("Error: Please, load BCB first!\n");
83                 return -1;
84         }
85
86         return 0;
87 err:
88         printf("Error: Bad usage of 'bcb %s'\n", argv[0]);
89
90         return -1;
91 }
92
93 static int bcb_field_get(char *name, char **fieldp, int *sizep)
94 {
95         if (!strcmp(name, "command")) {
96                 *fieldp = bcb.command;
97                 *sizep = sizeof(bcb.command);
98         } else if (!strcmp(name, "status")) {
99                 *fieldp = bcb.status;
100                 *sizep = sizeof(bcb.status);
101         } else if (!strcmp(name, "recovery")) {
102                 *fieldp = bcb.recovery;
103                 *sizep = sizeof(bcb.recovery);
104         } else if (!strcmp(name, "stage")) {
105                 *fieldp = bcb.stage;
106                 *sizep = sizeof(bcb.stage);
107         } else if (!strcmp(name, "reserved")) {
108                 *fieldp = bcb.reserved;
109                 *sizep = sizeof(bcb.reserved);
110         } else {
111                 printf("Error: Unknown bcb field '%s'\n", name);
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 static int __bcb_load(int devnum, const char *partp)
119 {
120         struct blk_desc *desc;
121         struct disk_partition info;
122         u64 cnt;
123         char *endp;
124         int part, ret;
125
126         desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, devnum);
127         if (!desc) {
128                 ret = -ENODEV;
129                 goto err_read_fail;
130         }
131
132         /*
133          * always select the USER mmc hwpart in case another
134          * blk operation selected a different hwpart
135          */
136         ret = blk_dselect_hwpart(desc, 0);
137         if (IS_ERR_VALUE(ret)) {
138                 ret = -ENODEV;
139                 goto err_read_fail;
140         }
141
142         part = simple_strtoul(partp, &endp, 0);
143         if (*endp == '\0') {
144                 ret = part_get_info(desc, part, &info);
145                 if (ret)
146                         goto err_read_fail;
147         } else {
148                 part = part_get_info_by_name(desc, partp, &info);
149                 if (part < 0) {
150                         ret = part;
151                         goto err_read_fail;
152                 }
153         }
154
155         cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
156         if (cnt > info.size)
157                 goto err_too_small;
158
159         if (blk_dread(desc, info.start, cnt, &bcb) != cnt) {
160                 ret = -EIO;
161                 goto err_read_fail;
162         }
163
164         bcb_dev = desc->devnum;
165         bcb_part = part;
166         debug("%s: Loaded from mmc %d:%d\n", __func__, bcb_dev, bcb_part);
167
168         return CMD_RET_SUCCESS;
169 err_read_fail:
170         printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret);
171         goto err;
172 err_too_small:
173         printf("Error: mmc %d:%s too small!", devnum, partp);
174         goto err;
175 err:
176         bcb_dev = -1;
177         bcb_part = -1;
178
179         return CMD_RET_FAILURE;
180 }
181
182 static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,
183                        char * const argv[])
184 {
185         char *endp;
186         int devnum = simple_strtoul(argv[1], &endp, 0);
187
188         if (*endp != '\0') {
189                 printf("Error: Device id '%s' not a number\n", argv[1]);
190                 return CMD_RET_FAILURE;
191         }
192
193         return __bcb_load(devnum, argv[2]);
194 }
195
196 static int __bcb_set(char *fieldp, const char *valp)
197 {
198         int size, len;
199         char *field, *str, *found, *tmp;
200
201         if (bcb_field_get(fieldp, &field, &size))
202                 return CMD_RET_FAILURE;
203
204         len = strlen(valp);
205         if (len >= size) {
206                 printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n",
207                        valp, len, size, fieldp);
208                 return CMD_RET_FAILURE;
209         }
210         str = strdup(valp);
211         if (!str) {
212                 printf("Error: Out of memory while strdup\n");
213                 return CMD_RET_FAILURE;
214         }
215
216         tmp = str;
217         field[0] = '\0';
218         while ((found = strsep(&tmp, ":"))) {
219                 if (field[0] != '\0')
220                         strcat(field, "\n");
221                 strcat(field, found);
222         }
223         free(str);
224
225         return CMD_RET_SUCCESS;
226 }
227
228 static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,
229                       char * const argv[])
230 {
231         return __bcb_set(argv[1], argv[2]);
232 }
233
234 static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc,
235                         char *const argv[])
236 {
237         int size;
238         char *field;
239
240         if (argc == 1) {
241                 memset(&bcb, 0, sizeof(bcb));
242                 return CMD_RET_SUCCESS;
243         }
244
245         if (bcb_field_get(argv[1], &field, &size))
246                 return CMD_RET_FAILURE;
247
248         memset(field, 0, size);
249
250         return CMD_RET_SUCCESS;
251 }
252
253 static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc,
254                        char *const argv[])
255 {
256         int size;
257         char *field;
258         char *op = argv[2];
259
260         if (bcb_field_get(argv[1], &field, &size))
261                 return CMD_RET_FAILURE;
262
263         if (*op == '=' && *(op + 1) == '\0') {
264                 if (!strncmp(argv[3], field, size))
265                         return CMD_RET_SUCCESS;
266                 else
267                         return CMD_RET_FAILURE;
268         } else if (*op == '~' && *(op + 1) == '\0') {
269                 if (!strstr(field, argv[3]))
270                         return CMD_RET_FAILURE;
271                 else
272                         return CMD_RET_SUCCESS;
273         } else {
274                 printf("Error: Unknown operator '%s'\n", op);
275         }
276
277         return CMD_RET_FAILURE;
278 }
279
280 static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,
281                        char *const argv[])
282 {
283         int size;
284         char *field;
285
286         if (bcb_field_get(argv[1], &field, &size))
287                 return CMD_RET_FAILURE;
288
289         print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16);
290
291         return CMD_RET_SUCCESS;
292 }
293
294 static int __bcb_store(void)
295 {
296         struct blk_desc *desc;
297         struct disk_partition info;
298         u64 cnt;
299         int ret;
300
301         desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, bcb_dev);
302         if (!desc) {
303                 ret = -ENODEV;
304                 goto err;
305         }
306
307         ret = part_get_info(desc, bcb_part, &info);
308         if (ret)
309                 goto err;
310
311         cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz);
312
313         if (blk_dwrite(desc, info.start, cnt, &bcb) != cnt) {
314                 ret = -EIO;
315                 goto err;
316         }
317
318         return CMD_RET_SUCCESS;
319 err:
320         printf("Error: mmc %d:%d write failed (%d)\n", bcb_dev, bcb_part, ret);
321
322         return CMD_RET_FAILURE;
323 }
324
325 static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,
326                         char * const argv[])
327 {
328         return __bcb_store();
329 }
330
331 int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp)
332 {
333         int ret;
334
335         ret = __bcb_load(devnum, partp);
336         if (ret != CMD_RET_SUCCESS)
337                 return ret;
338
339         ret = __bcb_set("command", reasonp);
340         if (ret != CMD_RET_SUCCESS)
341                 return ret;
342
343         ret = __bcb_store();
344         if (ret != CMD_RET_SUCCESS)
345                 return ret;
346
347         return 0;
348 }
349
350 static struct cmd_tbl cmd_bcb_sub[] = {
351         U_BOOT_CMD_MKENT(load, CONFIG_SYS_MAXARGS, 1, do_bcb_load, "", ""),
352         U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 1, do_bcb_set, "", ""),
353         U_BOOT_CMD_MKENT(clear, CONFIG_SYS_MAXARGS, 1, do_bcb_clear, "", ""),
354         U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_bcb_test, "", ""),
355         U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_bcb_dump, "", ""),
356         U_BOOT_CMD_MKENT(store, CONFIG_SYS_MAXARGS, 1, do_bcb_store, "", ""),
357 };
358
359 static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
360 {
361         struct cmd_tbl *c;
362
363         if (argc < 2)
364                 return CMD_RET_USAGE;
365
366         argc--;
367         argv++;
368
369         c = find_cmd_tbl(argv[0], cmd_bcb_sub, ARRAY_SIZE(cmd_bcb_sub));
370         if (!c)
371                 return CMD_RET_USAGE;
372
373         if (bcb_is_misused(argc, argv)) {
374                 /*
375                  * We try to improve the user experience by reporting the
376                  * root-cause of misusage, so don't return CMD_RET_USAGE,
377                  * since the latter prints out the full-blown help text
378                  */
379                 return CMD_RET_FAILURE;
380         }
381
382         return c->cmd(cmdtp, flag, argc, argv);
383 }
384
385 U_BOOT_CMD(
386         bcb, CONFIG_SYS_MAXARGS, 1, do_bcb,
387         "Load/set/clear/test/dump/store Android BCB fields",
388         "load  <dev> <part>       - load  BCB from mmc <dev>:<part>\n"
389         "bcb set   <field> <val>      - set   BCB <field> to <val>\n"
390         "bcb clear [<field>]          - clear BCB <field> or all fields\n"
391         "bcb test  <field> <op> <val> - test  BCB <field> against <val>\n"
392         "bcb dump  <field>            - dump  BCB <field>\n"
393         "bcb store                    - store BCB back to mmc\n"
394         "\n"
395         "Legend:\n"
396         "<dev>   - MMC device index containing the BCB partition\n"
397         "<part>  - MMC partition index or name containing the BCB\n"
398         "<field> - one of {command,status,recovery,stage,reserved}\n"
399         "<op>    - the binary operator used in 'bcb test':\n"
400         "          '=' returns true if <val> matches the string stored in <field>\n"
401         "          '~' returns true if <val> matches a subset of <field>'s string\n"
402         "<val>   - string/text provided as input to bcb {set,test}\n"
403         "          NOTE: any ':' character in <val> will be replaced by line feed\n"
404         "          during 'bcb set' and used as separator by upper layers\n"
405 );