Igor Opaniuk | 29cb886 | 2024-02-09 20:20:40 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2018, Linaro Limited |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <avb_verify.h> |
| 7 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 9 | #include <image.h> |
| 10 | #include <malloc.h> |
| 11 | #include <mmc.h> |
| 12 | |
| 13 | #define AVB_BOOTARGS "avb_bootargs" |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 14 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 15 | static struct AvbOps *avb_ops; |
| 16 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | int do_avb_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 18 | { |
| 19 | unsigned long mmc_dev; |
| 20 | |
| 21 | if (argc != 2) |
| 22 | return CMD_RET_USAGE; |
| 23 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 24 | mmc_dev = hextoul(argv[1], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 25 | |
| 26 | if (avb_ops) |
| 27 | avb_ops_free(avb_ops); |
| 28 | |
| 29 | avb_ops = avb_ops_alloc(mmc_dev); |
| 30 | if (avb_ops) |
| 31 | return CMD_RET_SUCCESS; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 32 | else |
| 33 | printf("Can't allocate AvbOps"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 34 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 35 | printf("Failed to initialize AVB\n"); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 36 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 37 | return CMD_RET_FAILURE; |
| 38 | } |
| 39 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 40 | int do_avb_read_part(struct cmd_tbl *cmdtp, int flag, int argc, |
| 41 | char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 42 | { |
| 43 | const char *part; |
| 44 | s64 offset; |
| 45 | size_t bytes, bytes_read = 0; |
| 46 | void *buffer; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 47 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 48 | |
| 49 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 50 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
| 51 | return CMD_RET_FAILURE; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | if (argc != 5) |
| 55 | return CMD_RET_USAGE; |
| 56 | |
| 57 | part = argv[1]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 58 | offset = hextoul(argv[2], NULL); |
| 59 | bytes = hextoul(argv[3], NULL); |
| 60 | buffer = (void *)hextoul(argv[4], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 61 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 62 | ret = avb_ops->read_from_partition(avb_ops, part, offset, |
| 63 | bytes, buffer, &bytes_read); |
| 64 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 65 | printf("Read %zu bytes\n", bytes_read); |
| 66 | return CMD_RET_SUCCESS; |
| 67 | } |
| 68 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 69 | printf("Failed to read from partition '%s', err = %d\n", |
| 70 | part, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 71 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 72 | return CMD_RET_FAILURE; |
| 73 | } |
| 74 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 75 | int do_avb_read_part_hex(struct cmd_tbl *cmdtp, int flag, int argc, |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 76 | char *const argv[]) |
| 77 | { |
| 78 | const char *part; |
| 79 | s64 offset; |
| 80 | size_t bytes, bytes_read = 0; |
| 81 | char *buffer; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 82 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 83 | |
| 84 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 85 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
| 86 | return CMD_RET_FAILURE; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (argc != 4) |
| 90 | return CMD_RET_USAGE; |
| 91 | |
| 92 | part = argv[1]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 93 | offset = hextoul(argv[2], NULL); |
| 94 | bytes = hextoul(argv[3], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 95 | |
| 96 | buffer = malloc(bytes); |
| 97 | if (!buffer) { |
| 98 | printf("Failed to tlb_allocate buffer for data\n"); |
| 99 | return CMD_RET_FAILURE; |
| 100 | } |
| 101 | memset(buffer, 0, bytes); |
| 102 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 103 | ret = avb_ops->read_from_partition(avb_ops, part, offset, |
| 104 | bytes, buffer, &bytes_read); |
| 105 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 106 | printf("Requested %zu, read %zu bytes\n", bytes, bytes_read); |
| 107 | printf("Data: "); |
| 108 | for (int i = 0; i < bytes_read; i++) |
| 109 | printf("%02X", buffer[i]); |
| 110 | |
| 111 | printf("\n"); |
| 112 | |
| 113 | free(buffer); |
| 114 | return CMD_RET_SUCCESS; |
| 115 | } |
| 116 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 117 | printf("Failed to read from partition '%s', err = %d\n", |
| 118 | part, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 119 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 120 | free(buffer); |
| 121 | return CMD_RET_FAILURE; |
| 122 | } |
| 123 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 124 | int do_avb_write_part(struct cmd_tbl *cmdtp, int flag, int argc, |
| 125 | char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 126 | { |
| 127 | const char *part; |
| 128 | s64 offset; |
| 129 | size_t bytes; |
| 130 | void *buffer; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 131 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 132 | |
| 133 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 134 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 135 | return CMD_RET_FAILURE; |
| 136 | } |
| 137 | |
| 138 | if (argc != 5) |
| 139 | return CMD_RET_USAGE; |
| 140 | |
| 141 | part = argv[1]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 142 | offset = hextoul(argv[2], NULL); |
| 143 | bytes = hextoul(argv[3], NULL); |
| 144 | buffer = (void *)hextoul(argv[4], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 145 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 146 | ret = avb_ops->write_to_partition(avb_ops, part, offset, |
| 147 | bytes, buffer); |
| 148 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 149 | printf("Wrote %zu bytes\n", bytes); |
| 150 | return CMD_RET_SUCCESS; |
| 151 | } |
| 152 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 153 | printf("Failed to write in partition '%s', err = %d\n", |
| 154 | part, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 155 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 156 | return CMD_RET_FAILURE; |
| 157 | } |
| 158 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 159 | int do_avb_read_rb(struct cmd_tbl *cmdtp, int flag, int argc, |
| 160 | char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 161 | { |
| 162 | size_t index; |
| 163 | u64 rb_idx; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 164 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 165 | |
| 166 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 167 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 168 | return CMD_RET_FAILURE; |
| 169 | } |
| 170 | |
| 171 | if (argc != 2) |
| 172 | return CMD_RET_USAGE; |
| 173 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 174 | index = (size_t)hextoul(argv[1], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 175 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 176 | ret = avb_ops->read_rollback_index(avb_ops, index, &rb_idx); |
| 177 | if (ret == AVB_IO_RESULT_OK) { |
Jens Wiklander | 5f51dcc | 2018-09-25 16:40:06 +0200 | [diff] [blame] | 178 | printf("Rollback index: %llx\n", rb_idx); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 179 | return CMD_RET_SUCCESS; |
| 180 | } |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 181 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 182 | printf("Failed to read rollback index id = %zu, err = %d\n", |
| 183 | index, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 184 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 185 | return CMD_RET_FAILURE; |
| 186 | } |
| 187 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 188 | int do_avb_write_rb(struct cmd_tbl *cmdtp, int flag, int argc, |
| 189 | char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 190 | { |
| 191 | size_t index; |
| 192 | u64 rb_idx; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 193 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 194 | |
| 195 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 196 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 197 | return CMD_RET_FAILURE; |
| 198 | } |
| 199 | |
| 200 | if (argc != 3) |
| 201 | return CMD_RET_USAGE; |
| 202 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 203 | index = (size_t)hextoul(argv[1], NULL); |
| 204 | rb_idx = hextoul(argv[2], NULL); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 205 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 206 | ret = avb_ops->write_rollback_index(avb_ops, index, rb_idx); |
| 207 | if (ret == AVB_IO_RESULT_OK) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 208 | return CMD_RET_SUCCESS; |
| 209 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 210 | printf("Failed to write rollback index id = %zu, err = %d\n", |
| 211 | index, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 212 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 213 | return CMD_RET_FAILURE; |
| 214 | } |
| 215 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 216 | int do_avb_get_uuid(struct cmd_tbl *cmdtp, int flag, |
| 217 | int argc, char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 218 | { |
| 219 | const char *part; |
| 220 | char buffer[UUID_STR_LEN + 1]; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 221 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 222 | |
| 223 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 224 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 225 | return CMD_RET_FAILURE; |
| 226 | } |
| 227 | |
| 228 | if (argc != 2) |
| 229 | return CMD_RET_USAGE; |
| 230 | |
| 231 | part = argv[1]; |
| 232 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 233 | ret = avb_ops->get_unique_guid_for_partition(avb_ops, part, |
| 234 | buffer, |
| 235 | UUID_STR_LEN + 1); |
| 236 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 237 | printf("'%s' UUID: %s\n", part, buffer); |
| 238 | return CMD_RET_SUCCESS; |
| 239 | } |
| 240 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 241 | printf("Failed to read partition '%s' UUID, err = %d\n", |
| 242 | part, ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 243 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 244 | return CMD_RET_FAILURE; |
| 245 | } |
| 246 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 247 | int do_avb_verify_part(struct cmd_tbl *cmdtp, int flag, |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 248 | int argc, char *const argv[]) |
| 249 | { |
Sam Protsenko | ac80a98 | 2019-08-15 20:49:47 +0300 | [diff] [blame] | 250 | const char * const requested_partitions[] = {"boot", NULL}; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 251 | AvbSlotVerifyResult slot_result; |
| 252 | AvbSlotVerifyData *out_data; |
Igor Opaniuk | d2e83e2 | 2024-02-09 20:20:44 +0100 | [diff] [blame] | 253 | enum avb_boot_state boot_state; |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 254 | char *cmdline; |
| 255 | char *extra_args; |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 256 | char *slot_suffix = ""; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 257 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 258 | |
| 259 | bool unlocked = false; |
| 260 | int res = CMD_RET_FAILURE; |
| 261 | |
| 262 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 263 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 264 | return CMD_RET_FAILURE; |
| 265 | } |
| 266 | |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 267 | if (argc < 1 || argc > 2) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 268 | return CMD_RET_USAGE; |
| 269 | |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 270 | if (argc == 2) |
| 271 | slot_suffix = argv[1]; |
| 272 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 273 | printf("## Android Verified Boot 2.0 version %s\n", |
| 274 | avb_version_string()); |
| 275 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 276 | ret = avb_ops->read_is_device_unlocked(avb_ops, &unlocked); |
| 277 | if (ret != AVB_IO_RESULT_OK) { |
| 278 | printf("Can't determine device lock state, err = %d\n", |
| 279 | ret); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 280 | return CMD_RET_FAILURE; |
| 281 | } |
| 282 | |
| 283 | slot_result = |
| 284 | avb_slot_verify(avb_ops, |
| 285 | requested_partitions, |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 286 | slot_suffix, |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 287 | unlocked, |
| 288 | AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, |
| 289 | &out_data); |
| 290 | |
Igor Opaniuk | d2e83e2 | 2024-02-09 20:20:44 +0100 | [diff] [blame] | 291 | /* |
| 292 | * LOCKED devices with custom root of trust setup is not supported (YELLOW) |
| 293 | */ |
| 294 | if (slot_result == AVB_SLOT_VERIFY_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 295 | printf("Verification passed successfully\n"); |
| 296 | |
Igor Opaniuk | d2e83e2 | 2024-02-09 20:20:44 +0100 | [diff] [blame] | 297 | /* |
| 298 | * ORANGE state indicates that device may be freely modified. |
| 299 | * Device integrity is left to the user to verify out-of-band. |
| 300 | */ |
| 301 | if (unlocked) |
| 302 | boot_state = AVB_ORANGE; |
| 303 | else |
| 304 | boot_state = AVB_GREEN; |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 305 | |
Igor Opaniuk | d2e83e2 | 2024-02-09 20:20:44 +0100 | [diff] [blame] | 306 | /* export boot state to AVB_BOOTARGS env var */ |
| 307 | extra_args = avb_set_state(avb_ops, boot_state); |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 308 | if (extra_args) |
| 309 | cmdline = append_cmd_line(out_data->cmdline, |
| 310 | extra_args); |
| 311 | else |
| 312 | cmdline = out_data->cmdline; |
| 313 | |
| 314 | env_set(AVB_BOOTARGS, cmdline); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 315 | |
| 316 | res = CMD_RET_SUCCESS; |
Igor Opaniuk | d2e83e2 | 2024-02-09 20:20:44 +0100 | [diff] [blame] | 317 | } else { |
| 318 | printf("Verification failed, reason: %s\n", str_avb_slot_error(slot_result)); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Gary Bisson | b12df3a | 2020-05-11 12:11:53 +0200 | [diff] [blame] | 321 | if (out_data) |
| 322 | avb_slot_verify_data_free(out_data); |
| 323 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 324 | return res; |
| 325 | } |
| 326 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 327 | int do_avb_is_unlocked(struct cmd_tbl *cmdtp, int flag, |
| 328 | int argc, char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 329 | { |
| 330 | bool unlock; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 331 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 332 | |
| 333 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 334 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 335 | return CMD_RET_FAILURE; |
| 336 | } |
| 337 | |
| 338 | if (argc != 1) { |
| 339 | printf("--%s(-1)\n", __func__); |
| 340 | return CMD_RET_USAGE; |
| 341 | } |
| 342 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 343 | ret = avb_ops->read_is_device_unlocked(avb_ops, &unlock); |
| 344 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 345 | printf("Unlocked = %d\n", unlock); |
| 346 | return CMD_RET_SUCCESS; |
| 347 | } |
| 348 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 349 | printf("Can't determine device lock state, err = %d\n", |
| 350 | ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 351 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 352 | return CMD_RET_FAILURE; |
| 353 | } |
| 354 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 355 | int do_avb_read_pvalue(struct cmd_tbl *cmdtp, int flag, int argc, |
| 356 | char *const argv[]) |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 357 | { |
| 358 | const char *name; |
| 359 | size_t bytes; |
| 360 | size_t bytes_read; |
| 361 | void *buffer; |
| 362 | char *endp; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 363 | int ret; |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 364 | |
| 365 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 366 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 367 | return CMD_RET_FAILURE; |
| 368 | } |
| 369 | |
| 370 | if (argc != 3) |
| 371 | return CMD_RET_USAGE; |
| 372 | |
| 373 | name = argv[1]; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 374 | bytes = dectoul(argv[2], &endp); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 375 | if (*endp && *endp != '\n') |
| 376 | return CMD_RET_USAGE; |
| 377 | |
| 378 | buffer = malloc(bytes); |
| 379 | if (!buffer) |
| 380 | return CMD_RET_FAILURE; |
| 381 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 382 | ret = avb_ops->read_persistent_value(avb_ops, name, bytes, |
| 383 | buffer, &bytes_read); |
| 384 | if (ret == AVB_IO_RESULT_OK) { |
Sam Protsenko | 6954351 | 2019-07-31 19:59:09 +0300 | [diff] [blame] | 385 | printf("Read %zu bytes, value = %s\n", bytes_read, |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 386 | (char *)buffer); |
| 387 | free(buffer); |
| 388 | return CMD_RET_SUCCESS; |
| 389 | } |
| 390 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 391 | printf("Failed to read persistent value, err = %d\n", ret); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 392 | |
| 393 | free(buffer); |
| 394 | |
| 395 | return CMD_RET_FAILURE; |
| 396 | } |
| 397 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 398 | int do_avb_write_pvalue(struct cmd_tbl *cmdtp, int flag, int argc, |
| 399 | char *const argv[]) |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 400 | { |
| 401 | const char *name; |
| 402 | const char *value; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 403 | int ret; |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 404 | |
| 405 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 406 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 407 | return CMD_RET_FAILURE; |
| 408 | } |
| 409 | |
| 410 | if (argc != 3) |
| 411 | return CMD_RET_USAGE; |
| 412 | |
| 413 | name = argv[1]; |
| 414 | value = argv[2]; |
| 415 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 416 | ret = avb_ops->write_persistent_value(avb_ops, name, |
| 417 | strlen(value) + 1, |
| 418 | (const uint8_t *)value); |
| 419 | if (ret == AVB_IO_RESULT_OK) { |
Sam Protsenko | 6954351 | 2019-07-31 19:59:09 +0300 | [diff] [blame] | 420 | printf("Wrote %zu bytes\n", strlen(value) + 1); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 421 | return CMD_RET_SUCCESS; |
| 422 | } |
| 423 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame] | 424 | printf("Failed to write persistent value `%s` = `%s`, err = %d\n", |
| 425 | name, value, ret); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 426 | |
| 427 | return CMD_RET_FAILURE; |
| 428 | } |
| 429 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 430 | static struct cmd_tbl cmd_avb[] = { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 431 | U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""), |
| 432 | U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""), |
| 433 | U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""), |
| 434 | U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""), |
| 435 | U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""), |
| 436 | U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""), |
| 437 | U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""), |
| 438 | U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""), |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 439 | U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""), |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_OPTEE_TA_AVB |
| 441 | U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""), |
| 442 | U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""), |
| 443 | #endif |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 444 | }; |
| 445 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 446 | static int do_avb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 447 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 448 | struct cmd_tbl *cp; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 449 | |
| 450 | cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb)); |
| 451 | |
| 452 | argc--; |
| 453 | argv++; |
| 454 | |
| 455 | if (!cp || argc > cp->maxargs) |
| 456 | return CMD_RET_USAGE; |
| 457 | |
| 458 | if (flag == CMD_FLAG_REPEAT) |
| 459 | return CMD_RET_FAILURE; |
| 460 | |
| 461 | return cp->cmd(cmdtp, flag, argc, argv); |
| 462 | } |
| 463 | |
| 464 | U_BOOT_CMD( |
| 465 | avb, 29, 0, do_avb, |
| 466 | "Provides commands for testing Android Verified Boot 2.0 functionality", |
| 467 | "init <dev> - initialize avb2 for <dev>\n" |
| 468 | "avb read_rb <num> - read rollback index at location <num>\n" |
| 469 | "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n" |
| 470 | "avb is_unlocked - returns unlock status of the device\n" |
| 471 | "avb get_uuid <partname> - read and print uuid of partition <part>\n" |
| 472 | "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n" |
| 473 | " partition <partname> to buffer <addr>\n" |
| 474 | "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n" |
| 475 | " partition <partname> and print to stdout\n" |
| 476 | "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n" |
| 477 | " <partname> by <offset> using data from <addr>\n" |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 478 | #ifdef CONFIG_OPTEE_TA_AVB |
| 479 | "avb read_pvalue <name> <bytes> - read a persistent value <name>\n" |
| 480 | "avb write_pvalue <name> <value> - write a persistent value <name>\n" |
| 481 | #endif |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 482 | "avb verify [slot_suffix] - run verification process using hash data\n" |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 483 | " from vbmeta structure\n" |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 484 | " [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)\n" |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 485 | ); |