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> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 11 | #include <display_options.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <part.h> |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 14 | #include <malloc.h> |
Gary Bisson | f3f2c7b | 2022-01-11 18:06:06 +0100 | [diff] [blame] | 15 | #include <memalign.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 16 | #include <vsprintf.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 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 19 | static const char * const fields[] = { |
| 20 | "command", |
| 21 | "status", |
| 22 | "recovery", |
| 23 | "stage" |
| 24 | }; |
| 25 | |
Gary Bisson | f3f2c7b | 2022-01-11 18:06:06 +0100 | [diff] [blame] | 26 | static struct bootloader_message bcb __aligned(ARCH_DMA_MINALIGN) = { { 0 } }; |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 27 | static struct disk_partition partition_data; |
| 28 | |
| 29 | static struct blk_desc *block; |
| 30 | static struct disk_partition *partition = &partition_data; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 31 | |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 32 | static int bcb_not_loaded(void) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 33 | { |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 34 | printf("Error: Please, load BCB first!\n"); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 35 | return -1; |
| 36 | } |
| 37 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 38 | static int bcb_field_get(const char *name, char **fieldp, int *sizep) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 39 | { |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 40 | if (!strcmp(name, "command")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 41 | *fieldp = bcb.command; |
| 42 | *sizep = sizeof(bcb.command); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 43 | } else if (!strcmp(name, "status")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 44 | *fieldp = bcb.status; |
| 45 | *sizep = sizeof(bcb.status); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 46 | } else if (!strcmp(name, "recovery")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 47 | *fieldp = bcb.recovery; |
| 48 | *sizep = sizeof(bcb.recovery); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 49 | } else if (!strcmp(name, "stage")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 50 | *fieldp = bcb.stage; |
| 51 | *sizep = sizeof(bcb.stage); |
Eugeniu Rosca | 9bb6283 | 2019-07-19 23:26:13 +0200 | [diff] [blame] | 52 | } else if (!strcmp(name, "reserved")) { |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 53 | *fieldp = bcb.reserved; |
| 54 | *sizep = sizeof(bcb.reserved); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 55 | } else { |
| 56 | printf("Error: Unknown bcb field '%s'\n", name); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 63 | static void __bcb_reset(void) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 64 | { |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 65 | block = NULL; |
| 66 | partition = &partition_data; |
| 67 | memset(&partition_data, 0, sizeof(struct disk_partition)); |
| 68 | memset(&bcb, 0, sizeof(struct bootloader_message)); |
| 69 | } |
| 70 | |
| 71 | static int __bcb_initialize(const char *iface, int devnum, const char *partp) |
| 72 | { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 73 | char *endp; |
| 74 | int part, ret; |
| 75 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 76 | block = blk_get_dev(iface, devnum); |
| 77 | if (!block) { |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 78 | ret = -ENODEV; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 79 | goto err_read_fail; |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 80 | } |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 81 | |
Mattijs Korpershoek | cb25410 | 2022-10-13 14:25:59 +0200 | [diff] [blame] | 82 | /* |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 83 | * always select the first hwpart in case another |
Mattijs Korpershoek | cb25410 | 2022-10-13 14:25:59 +0200 | [diff] [blame] | 84 | * blk operation selected a different hwpart |
| 85 | */ |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 86 | ret = blk_dselect_hwpart(block, 0); |
Mattijs Korpershoek | cb25410 | 2022-10-13 14:25:59 +0200 | [diff] [blame] | 87 | if (IS_ERR_VALUE(ret)) { |
| 88 | ret = -ENODEV; |
| 89 | goto err_read_fail; |
| 90 | } |
| 91 | |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 92 | part = simple_strtoul(partp, &endp, 0); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 93 | if (*endp == '\0') { |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 94 | ret = part_get_info(block, part, partition); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 95 | if (ret) |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 96 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 97 | } else { |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 98 | part = part_get_info_by_name(block, partp, partition); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 99 | if (part < 0) { |
| 100 | ret = part; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 101 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 105 | return CMD_RET_SUCCESS; |
| 106 | |
| 107 | err_read_fail: |
Mattijs Korpershoek | 35ec297 | 2024-07-24 11:51:06 +0200 | [diff] [blame] | 108 | printf("Error: %s %d:%s read failed (%d)\n", iface, devnum, |
| 109 | partition->name, ret); |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 110 | __bcb_reset(); |
| 111 | return CMD_RET_FAILURE; |
| 112 | } |
| 113 | |
| 114 | static int __bcb_load(void) |
| 115 | { |
| 116 | u64 cnt; |
| 117 | int ret; |
| 118 | |
| 119 | cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz); |
| 120 | if (cnt > partition->size) |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 121 | goto err_too_small; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 122 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 123 | if (blk_dread(block, partition->start, cnt, &bcb) != cnt) { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 124 | ret = -EIO; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 125 | goto err_read_fail; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 128 | debug("%s: Loaded from %d %d:%s\n", __func__, block->uclass_id, |
| 129 | block->devnum, partition->name); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 130 | |
| 131 | return CMD_RET_SUCCESS; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 132 | err_read_fail: |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 133 | printf("Error: %d %d:%s read failed (%d)\n", block->uclass_id, |
| 134 | block->devnum, partition->name, ret); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 135 | goto err; |
Eugeniu Rosca | cb10fc9 | 2019-07-19 23:26:14 +0200 | [diff] [blame] | 136 | err_too_small: |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 137 | printf("Error: %d %d:%s too small!", block->uclass_id, |
| 138 | block->devnum, partition->name); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 139 | err: |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 140 | __bcb_reset(); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 141 | return CMD_RET_FAILURE; |
| 142 | } |
| 143 | |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 144 | static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 145 | char * const argv[]) |
| 146 | { |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 147 | int ret; |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 148 | int devnum; |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 149 | char *endp; |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 150 | char *iface = "mmc"; |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 151 | |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 152 | if (argc < 3) |
| 153 | return CMD_RET_USAGE; |
| 154 | |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 155 | if (argc == 4) { |
| 156 | iface = argv[1]; |
| 157 | argc--; |
| 158 | argv++; |
| 159 | } |
| 160 | |
| 161 | devnum = simple_strtoul(argv[1], &endp, 0); |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 162 | if (*endp != '\0') { |
| 163 | printf("Error: Device id '%s' not a number\n", argv[1]); |
| 164 | return CMD_RET_FAILURE; |
| 165 | } |
| 166 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 167 | ret = __bcb_initialize(iface, devnum, argv[2]); |
| 168 | if (ret != CMD_RET_SUCCESS) |
| 169 | return ret; |
| 170 | |
| 171 | return __bcb_load(); |
Eugeniu Rosca | f308af5 | 2020-10-23 11:52:19 +0300 | [diff] [blame] | 172 | } |
| 173 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 174 | static int __bcb_set(const char *fieldp, const char *valp) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 175 | { |
| 176 | int size, len; |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 177 | char *field, *str, *found, *tmp; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 178 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 179 | if (bcb_field_get(fieldp, &field, &size)) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 180 | return CMD_RET_FAILURE; |
| 181 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 182 | len = strlen(valp); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 183 | if (len >= size) { |
| 184 | printf("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n", |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 185 | valp, len, size, fieldp); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 186 | return CMD_RET_FAILURE; |
| 187 | } |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 188 | str = strdup(valp); |
| 189 | if (!str) { |
| 190 | printf("Error: Out of memory while strdup\n"); |
| 191 | return CMD_RET_FAILURE; |
| 192 | } |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 193 | |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 194 | tmp = str; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 195 | field[0] = '\0'; |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 196 | while ((found = strsep(&tmp, ":"))) { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 197 | if (field[0] != '\0') |
| 198 | strcat(field, "\n"); |
| 199 | strcat(field, found); |
| 200 | } |
Eugeniu Rosca | 6005e93 | 2020-10-23 11:52:23 +0300 | [diff] [blame] | 201 | free(str); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 202 | |
| 203 | return CMD_RET_SUCCESS; |
| 204 | } |
| 205 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 206 | static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc, |
| 207 | char * const argv[]) |
| 208 | { |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 209 | if (argc < 3) |
| 210 | return CMD_RET_USAGE; |
| 211 | |
| 212 | if (!block) |
| 213 | return bcb_not_loaded(); |
| 214 | |
Eugeniu Rosca | 92be544 | 2020-10-23 11:52:20 +0300 | [diff] [blame] | 215 | return __bcb_set(argv[1], argv[2]); |
| 216 | } |
| 217 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 218 | static int do_bcb_clear(struct cmd_tbl *cmdtp, int flag, int argc, |
| 219 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 220 | { |
| 221 | int size; |
| 222 | char *field; |
| 223 | |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 224 | if (!block) |
| 225 | return bcb_not_loaded(); |
| 226 | |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 227 | if (argc == 1) { |
| 228 | memset(&bcb, 0, sizeof(bcb)); |
| 229 | return CMD_RET_SUCCESS; |
| 230 | } |
| 231 | |
| 232 | if (bcb_field_get(argv[1], &field, &size)) |
| 233 | return CMD_RET_FAILURE; |
| 234 | |
| 235 | memset(field, 0, size); |
| 236 | |
| 237 | return CMD_RET_SUCCESS; |
| 238 | } |
| 239 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 240 | static int do_bcb_test(struct cmd_tbl *cmdtp, int flag, int argc, |
| 241 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 242 | { |
| 243 | int size; |
| 244 | char *field; |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 245 | char *op; |
| 246 | |
| 247 | if (argc < 4) |
| 248 | return CMD_RET_USAGE; |
| 249 | |
| 250 | if (!block) |
| 251 | return bcb_not_loaded(); |
| 252 | |
| 253 | op = argv[2]; |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 254 | |
| 255 | if (bcb_field_get(argv[1], &field, &size)) |
| 256 | return CMD_RET_FAILURE; |
| 257 | |
| 258 | if (*op == '=' && *(op + 1) == '\0') { |
| 259 | if (!strncmp(argv[3], field, size)) |
| 260 | return CMD_RET_SUCCESS; |
| 261 | else |
| 262 | return CMD_RET_FAILURE; |
| 263 | } else if (*op == '~' && *(op + 1) == '\0') { |
| 264 | if (!strstr(field, argv[3])) |
| 265 | return CMD_RET_FAILURE; |
| 266 | else |
| 267 | return CMD_RET_SUCCESS; |
| 268 | } else { |
| 269 | printf("Error: Unknown operator '%s'\n", op); |
| 270 | } |
| 271 | |
| 272 | return CMD_RET_FAILURE; |
| 273 | } |
| 274 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 275 | static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 276 | char *const argv[]) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 277 | { |
| 278 | int size; |
| 279 | char *field; |
| 280 | |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 281 | if (argc < 2) |
| 282 | return CMD_RET_USAGE; |
| 283 | |
| 284 | if (!block) |
| 285 | return bcb_not_loaded(); |
| 286 | |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 287 | if (bcb_field_get(argv[1], &field, &size)) |
| 288 | return CMD_RET_FAILURE; |
| 289 | |
| 290 | print_buffer((ulong)field - (ulong)&bcb, (void *)field, 1, size, 16); |
| 291 | |
| 292 | return CMD_RET_SUCCESS; |
| 293 | } |
| 294 | |
Eugeniu Rosca | f6155bd | 2020-10-23 11:52:21 +0300 | [diff] [blame] | 295 | static int __bcb_store(void) |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 296 | { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 297 | u64 cnt; |
| 298 | int ret; |
| 299 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 300 | cnt = DIV_ROUND_UP(sizeof(struct bootloader_message), partition->blksz); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 301 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 302 | if (blk_dwrite(block, partition->start, cnt, &bcb) != cnt) { |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 303 | ret = -EIO; |
| 304 | goto err; |
| 305 | } |
| 306 | |
| 307 | return CMD_RET_SUCCESS; |
| 308 | err: |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 309 | printf("Error: %d %d:%s write failed (%d)\n", block->uclass_id, |
| 310 | block->devnum, partition->name, ret); |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 311 | |
| 312 | return CMD_RET_FAILURE; |
| 313 | } |
| 314 | |
Eugeniu Rosca | f6155bd | 2020-10-23 11:52:21 +0300 | [diff] [blame] | 315 | static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc, |
| 316 | char * const argv[]) |
| 317 | { |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 318 | if (!block) |
| 319 | return bcb_not_loaded(); |
| 320 | |
Eugeniu Rosca | f6155bd | 2020-10-23 11:52:21 +0300 | [diff] [blame] | 321 | return __bcb_store(); |
| 322 | } |
| 323 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 324 | int bcb_find_partition_and_load(const char *iface, int devnum, char *partp) |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 325 | { |
| 326 | int ret; |
| 327 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 328 | __bcb_reset(); |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 329 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 330 | ret = __bcb_initialize(iface, devnum, partp); |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 331 | if (ret != CMD_RET_SUCCESS) |
| 332 | return ret; |
| 333 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 334 | return __bcb_load(); |
| 335 | } |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 336 | |
Dmitrii Merkurev | acb2281 | 2023-11-10 05:59:55 +0000 | [diff] [blame] | 337 | int bcb_load(struct blk_desc *block_description, struct disk_partition *disk_partition) |
| 338 | { |
| 339 | __bcb_reset(); |
| 340 | |
| 341 | block = block_description; |
| 342 | partition = disk_partition; |
| 343 | |
| 344 | return __bcb_load(); |
| 345 | } |
| 346 | |
| 347 | int bcb_set(enum bcb_field field, const char *value) |
| 348 | { |
| 349 | if (field > BCB_FIELD_STAGE) |
| 350 | return CMD_RET_FAILURE; |
| 351 | return __bcb_set(fields[field], value); |
| 352 | } |
| 353 | |
| 354 | int bcb_get(enum bcb_field field, char *value_out, size_t value_size) |
| 355 | { |
| 356 | int size; |
| 357 | char *field_value; |
| 358 | |
| 359 | if (field > BCB_FIELD_STAGE) |
| 360 | return CMD_RET_FAILURE; |
| 361 | if (bcb_field_get(fields[field], &field_value, &size)) |
| 362 | return CMD_RET_FAILURE; |
| 363 | |
| 364 | strlcpy(value_out, field_value, value_size); |
| 365 | |
| 366 | return CMD_RET_SUCCESS; |
| 367 | } |
| 368 | |
| 369 | int bcb_store(void) |
| 370 | { |
| 371 | return __bcb_store(); |
| 372 | } |
| 373 | |
| 374 | void bcb_reset(void) |
| 375 | { |
| 376 | __bcb_reset(); |
Eugeniu Rosca | 54a5548 | 2020-10-23 11:52:22 +0300 | [diff] [blame] | 377 | } |
| 378 | |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 379 | U_BOOT_LONGHELP(bcb, |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 380 | "load <interface> <dev> <part> - load BCB from <interface> <dev>:<part>\n" |
| 381 | "load <dev> <part> - load BCB from mmc <dev>:<part>\n" |
| 382 | "bcb set <field> <val> - set BCB <field> to <val>\n" |
| 383 | "bcb clear [<field>] - clear BCB <field> or all fields\n" |
| 384 | "bcb test <field> <op> <val> - test BCB <field> against <val>\n" |
| 385 | "bcb dump <field> - dump BCB <field>\n" |
| 386 | "bcb store - store BCB back to <interface>\n" |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 387 | "\n" |
| 388 | "Legend:\n" |
Dmitrii Merkurev | 927e210 | 2023-11-10 05:59:54 +0000 | [diff] [blame] | 389 | "<interface> - storage device interface (virtio, mmc, etc)\n" |
| 390 | "<dev> - storage device index containing the BCB partition\n" |
| 391 | "<part> - partition index or name containing the BCB\n" |
| 392 | "<field> - one of {command,status,recovery,stage,reserved}\n" |
| 393 | "<op> - the binary operator used in 'bcb test':\n" |
| 394 | " '=' returns true if <val> matches the string stored in <field>\n" |
| 395 | " '~' returns true if <val> matches a subset of <field>'s string\n" |
| 396 | "<val> - string/text provided as input to bcb {set,test}\n" |
| 397 | " NOTE: any ':' character in <val> will be replaced by line feed\n" |
| 398 | " during 'bcb set' and used as separator by upper layers\n" |
Eugeniu Rosca | 35c34be | 2019-05-23 17:32:22 +0200 | [diff] [blame] | 399 | ); |
Dmitry Rokosov | 4adb34e | 2024-10-17 17:12:07 +0300 | [diff] [blame^] | 400 | |
| 401 | U_BOOT_CMD_WITH_SUBCMDS(bcb, |
| 402 | "Load/set/clear/test/dump/store Android BCB fields", bcb_help_text, |
| 403 | U_BOOT_SUBCMD_MKENT(load, 4, 1, do_bcb_load), |
| 404 | U_BOOT_SUBCMD_MKENT(set, 3, 1, do_bcb_set), |
| 405 | U_BOOT_SUBCMD_MKENT(clear, 2, 1, do_bcb_clear), |
| 406 | U_BOOT_SUBCMD_MKENT(test, 4, 1, do_bcb_test), |
| 407 | U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_bcb_dump), |
| 408 | U_BOOT_SUBCMD_MKENT(store, 1, 1, do_bcb_store), |
| 409 | ); |