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