Eugeniu Rosca | 35c34be | 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 | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 9 | #include <bcb.h> |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 10 | #include <command.h> |
| 11 | #include <common.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 12 | #include <display_options.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <part.h> |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 15 | #include <malloc.h> |
Gary Bisson | f3f2c7b | 2022-01-11 18:06:06 +0100 | [diff] [blame] | 16 | #include <memalign.h> |
Mattijs Korpershoek | cb25410 | 2022-10-13 14:25:59 +0200 | [diff] [blame^] | 17 | #include <linux/err.h> |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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; |
Gary Bisson | f3f2c7b | 2022-01-11 18:06:06 +0100 | [diff] [blame] | 30 | static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } }; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 31 | |
| 32 | static int bcb_cmd_get(char *cmd) |
| 33 | { |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 34 | if (!strcmp(cmd, "load")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 35 | return BCB_CMD_LOAD; |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 36 | if (!strcmp(cmd, "set")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 37 | return BCB_CMD_FIELD_SET; |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 38 | if (!strcmp(cmd, "clear")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 39 | return BCB_CMD_FIELD_CLEAR; |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 40 | if (!strcmp(cmd, "test")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 41 | return BCB_CMD_FIELD_TEST; |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 42 | if (!strcmp(cmd, "store")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 43 | return BCB_CMD_STORE; |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 44 | if (!strcmp(cmd, "dump")) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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: |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 93 | static int bcb_field_get(char *name, char **fieldp, int *sizep) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 94 | { |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 95 | if (!strcmp(name, "command")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 96 | *fieldp = bcb.command; |
| 97 | *sizep = sizeof(bcb.command); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 98 | } else if (!strcmp(name, "status")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 99 | *fieldp = bcb.status; |
| 100 | *sizep = sizeof(bcb.status); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 101 | } else if (!strcmp(name, "recovery")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 102 | *fieldp = bcb.recovery; |
| 103 | *sizep = sizeof(bcb.recovery); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 104 | } else if (!strcmp(name, "stage")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 105 | *fieldp = bcb.stage; |
| 106 | *sizep = sizeof(bcb.stage); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 107 | } else if (!strcmp(name, "reserved")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 108 | *fieldp = bcb.reserved; |
| 109 | *sizep = sizeof(bcb.reserved); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 110 | } else { |
| 111 | printf("Error: Unknown bcb field '%s'\n", name); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 118 | static int __bcb_load(int devnum, const char *partp) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 119 | { |
| 120 | struct blk_desc *desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 121 | struct disk_partition info; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 122 | u64 cnt; |
| 123 | char *endp; |
| 124 | int part, ret; |
| 125 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 126 | desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, devnum); |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 127 | if (!desc) { |
| 128 | ret = -ENODEV; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 129 | goto err_read_fail; |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 130 | } |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 131 | |
Mattijs Korpershoek | cb25410 | 2022-10-13 14:25:59 +0200 | [diff] [blame^] | 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 | |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 142 | part = simple_strtoul(partp, &endp, 0); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 143 | if (*endp == '\0') { |
| 144 | ret = part_get_info(desc, part, &info); |
| 145 | if (ret) |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 146 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 147 | } else { |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 148 | part = part_get_info_by_name(desc, partp, &info); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 149 | if (part < 0) { |
| 150 | ret = part; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 151 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), info.blksz); |
| 156 | if (cnt > info.size) |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 157 | goto err_too_small; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 158 | |
| 159 | if (blk_dread(desc, info.start, cnt, &bcb) != cnt) { |
| 160 | ret = -EIO; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 161 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 169 | err_read_fail: |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 170 | printf("Error: mmc %d:%s read failed (%d)\n", devnum, partp, ret); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 171 | goto err; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 172 | err_too_small: |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 173 | printf("Error: mmc %d:%s too small!", devnum, partp); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 174 | goto err; |
| 175 | err: |
| 176 | bcb_dev = -1; |
| 177 | bcb_part = -1; |
| 178 | |
| 179 | return CMD_RET_FAILURE; |
| 180 | } |
| 181 | |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 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 | |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 196 | static int __bcb_set(char *fieldp, const char *valp) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 197 | { |
| 198 | int size, len; |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 199 | char *field, *str, *found, *tmp; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 200 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 201 | if (bcb_field_get(fieldp, &field, &size)) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 202 | return CMD_RET_FAILURE; |
| 203 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 204 | len = strlen(valp); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 205 | if (len >= size) { |
| 206 | printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n", |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 207 | valp, len, size, fieldp); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 208 | return CMD_RET_FAILURE; |
| 209 | } |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 210 | str = strdup(valp); |
| 211 | if (!str) { |
| 212 | printf("Error: Out of memory while strdup\n"); |
| 213 | return CMD_RET_FAILURE; |
| 214 | } |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 215 | |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 216 | tmp = str; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 217 | field[0] = '\0'; |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 218 | while ((found = strsep(&tmp, ":"))) { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 219 | if (field[0] != '\0') |
| 220 | strcat(field, "\n"); |
| 221 | strcat(field, found); |
| 222 | } |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 223 | free(str); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 224 | |
| 225 | return CMD_RET_SUCCESS; |
| 226 | } |
| 227 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 234 | static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc, |
| 235 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 253 | static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc, |
| 254 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 280 | static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 281 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Eugeniu Rosca | f6155bd | 2020-10-23 11:52:21 +0300 | [diff] [blame] | 294 | static int __bcb_store(void) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 295 | { |
| 296 | struct blk_desc *desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 297 | struct disk_partition info; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 298 | u64 cnt; |
| 299 | int ret; |
| 300 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 301 | desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, bcb_dev); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Eugeniu Rosca | f6155bd | 2020-10-23 11:52:21 +0300 | [diff] [blame] | 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 | |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 331 | int bcb_write_reboot_reason(int devnum, char *partp, const char *reasonp) |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 350 | static struct cmd_tbl cmd_bcb_sub[] = { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 359 | static int do_bcb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 360 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 361 | struct cmd_tbl *c; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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)) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 374 | /* |
| 375 | * We try to improve the user experience by reporting the |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 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 | ); |