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 | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 253 | char *cmdline; |
| 254 | char *extra_args; |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 255 | char *slot_suffix = ""; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 256 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 257 | |
| 258 | bool unlocked = false; |
| 259 | int res = CMD_RET_FAILURE; |
| 260 | |
| 261 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 262 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 263 | return CMD_RET_FAILURE; |
| 264 | } |
| 265 | |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 266 | if (argc < 1 || argc > 2) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 267 | return CMD_RET_USAGE; |
| 268 | |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 269 | if (argc == 2) |
| 270 | slot_suffix = argv[1]; |
| 271 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 272 | printf("## Android Verified Boot 2.0 version %s\n", |
| 273 | avb_version_string()); |
| 274 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 275 | ret = avb_ops->read_is_device_unlocked(avb_ops, &unlocked); |
| 276 | if (ret != AVB_IO_RESULT_OK) { |
| 277 | printf("Can't determine device lock state, err = %d\n", |
| 278 | ret); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 279 | return CMD_RET_FAILURE; |
| 280 | } |
| 281 | |
| 282 | slot_result = |
| 283 | avb_slot_verify(avb_ops, |
| 284 | requested_partitions, |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 285 | slot_suffix, |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 286 | unlocked, |
| 287 | AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, |
| 288 | &out_data); |
| 289 | |
| 290 | switch (slot_result) { |
| 291 | case AVB_SLOT_VERIFY_RESULT_OK: |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 292 | /* Until we don't have support of changing unlock states, we |
| 293 | * assume that we are by default in locked state. |
| 294 | * So in this case we can boot only when verification is |
| 295 | * successful; we also supply in cmdline GREEN boot state |
| 296 | */ |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 297 | printf("Verification passed successfully\n"); |
| 298 | |
| 299 | /* export additional bootargs to AVB_BOOTARGS env var */ |
Igor Opaniuk | f0f3bfe | 2018-06-03 21:56:40 +0300 | [diff] [blame] | 300 | |
| 301 | extra_args = avb_set_state(avb_ops, AVB_GREEN); |
| 302 | if (extra_args) |
| 303 | cmdline = append_cmd_line(out_data->cmdline, |
| 304 | extra_args); |
| 305 | else |
| 306 | cmdline = out_data->cmdline; |
| 307 | |
| 308 | env_set(AVB_BOOTARGS, cmdline); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 309 | |
| 310 | res = CMD_RET_SUCCESS; |
| 311 | break; |
| 312 | case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: |
| 313 | printf("Verification failed\n"); |
| 314 | break; |
| 315 | case AVB_SLOT_VERIFY_RESULT_ERROR_IO: |
| 316 | printf("I/O error occurred during verification\n"); |
| 317 | break; |
| 318 | case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: |
| 319 | printf("OOM error occurred during verification\n"); |
| 320 | break; |
| 321 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: |
| 322 | printf("Corrupted dm-verity metadata detected\n"); |
| 323 | break; |
| 324 | case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 325 | printf("Unsupported version of avbtool was used\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 326 | break; |
| 327 | case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 328 | printf("Rollback index check failed\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 329 | break; |
| 330 | case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: |
| 331 | printf("Public key was rejected\n"); |
| 332 | break; |
| 333 | default: |
| 334 | printf("Unknown error occurred\n"); |
| 335 | } |
| 336 | |
Gary Bisson | b12df3a | 2020-05-11 12:11:53 +0200 | [diff] [blame] | 337 | if (out_data) |
| 338 | avb_slot_verify_data_free(out_data); |
| 339 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 340 | return res; |
| 341 | } |
| 342 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 343 | int do_avb_is_unlocked(struct cmd_tbl *cmdtp, int flag, |
| 344 | int argc, char *const argv[]) |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 345 | { |
| 346 | bool unlock; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 347 | int ret; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 348 | |
| 349 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 350 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 351 | return CMD_RET_FAILURE; |
| 352 | } |
| 353 | |
| 354 | if (argc != 1) { |
| 355 | printf("--%s(-1)\n", __func__); |
| 356 | return CMD_RET_USAGE; |
| 357 | } |
| 358 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 359 | ret = avb_ops->read_is_device_unlocked(avb_ops, &unlock); |
| 360 | if (ret == AVB_IO_RESULT_OK) { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 361 | printf("Unlocked = %d\n", unlock); |
| 362 | return CMD_RET_SUCCESS; |
| 363 | } |
| 364 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 365 | printf("Can't determine device lock state, err = %d\n", |
| 366 | ret); |
Jens Wiklander | 6815afc | 2018-09-25 16:40:07 +0200 | [diff] [blame] | 367 | |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 368 | return CMD_RET_FAILURE; |
| 369 | } |
| 370 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 371 | int do_avb_read_pvalue(struct cmd_tbl *cmdtp, int flag, int argc, |
| 372 | char *const argv[]) |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 373 | { |
| 374 | const char *name; |
| 375 | size_t bytes; |
| 376 | size_t bytes_read; |
| 377 | void *buffer; |
| 378 | char *endp; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 379 | int ret; |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 380 | |
| 381 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 382 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 383 | return CMD_RET_FAILURE; |
| 384 | } |
| 385 | |
| 386 | if (argc != 3) |
| 387 | return CMD_RET_USAGE; |
| 388 | |
| 389 | name = argv[1]; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 390 | bytes = dectoul(argv[2], &endp); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 391 | if (*endp && *endp != '\n') |
| 392 | return CMD_RET_USAGE; |
| 393 | |
| 394 | buffer = malloc(bytes); |
| 395 | if (!buffer) |
| 396 | return CMD_RET_FAILURE; |
| 397 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 398 | ret = avb_ops->read_persistent_value(avb_ops, name, bytes, |
| 399 | buffer, &bytes_read); |
| 400 | if (ret == AVB_IO_RESULT_OK) { |
Sam Protsenko | 6954351 | 2019-07-31 19:59:09 +0300 | [diff] [blame] | 401 | printf("Read %zu bytes, value = %s\n", bytes_read, |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 402 | (char *)buffer); |
| 403 | free(buffer); |
| 404 | return CMD_RET_SUCCESS; |
| 405 | } |
| 406 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 407 | printf("Failed to read persistent value, err = %d\n", ret); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 408 | |
| 409 | free(buffer); |
| 410 | |
| 411 | return CMD_RET_FAILURE; |
| 412 | } |
| 413 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 414 | int do_avb_write_pvalue(struct cmd_tbl *cmdtp, int flag, int argc, |
| 415 | char *const argv[]) |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 416 | { |
| 417 | const char *name; |
| 418 | const char *value; |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 419 | int ret; |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 420 | |
| 421 | if (!avb_ops) { |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 422 | printf("AVB is not initialized, please run 'avb init <id>'\n"); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 423 | return CMD_RET_FAILURE; |
| 424 | } |
| 425 | |
| 426 | if (argc != 3) |
| 427 | return CMD_RET_USAGE; |
| 428 | |
| 429 | name = argv[1]; |
| 430 | value = argv[2]; |
| 431 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 432 | ret = avb_ops->write_persistent_value(avb_ops, name, |
| 433 | strlen(value) + 1, |
| 434 | (const uint8_t *)value); |
| 435 | if (ret == AVB_IO_RESULT_OK) { |
Sam Protsenko | 6954351 | 2019-07-31 19:59:09 +0300 | [diff] [blame] | 436 | printf("Wrote %zu bytes\n", strlen(value) + 1); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 437 | return CMD_RET_SUCCESS; |
| 438 | } |
| 439 | |
Igor Opaniuk | a4669d7 | 2024-02-09 20:20:42 +0100 | [diff] [blame^] | 440 | printf("Failed to write persistent value `%s` = `%s`, err = %d\n", |
| 441 | name, value, ret); |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 442 | |
| 443 | return CMD_RET_FAILURE; |
| 444 | } |
| 445 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 446 | static struct cmd_tbl cmd_avb[] = { |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 447 | U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""), |
| 448 | U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""), |
| 449 | U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""), |
| 450 | U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""), |
| 451 | U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""), |
| 452 | U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""), |
| 453 | U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""), |
| 454 | U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""), |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 455 | U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""), |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 456 | #ifdef CONFIG_OPTEE_TA_AVB |
| 457 | U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""), |
| 458 | U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""), |
| 459 | #endif |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 460 | }; |
| 461 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 462 | 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] | 463 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 464 | struct cmd_tbl *cp; |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 465 | |
| 466 | cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb)); |
| 467 | |
| 468 | argc--; |
| 469 | argv++; |
| 470 | |
| 471 | if (!cp || argc > cp->maxargs) |
| 472 | return CMD_RET_USAGE; |
| 473 | |
| 474 | if (flag == CMD_FLAG_REPEAT) |
| 475 | return CMD_RET_FAILURE; |
| 476 | |
| 477 | return cp->cmd(cmdtp, flag, argc, argv); |
| 478 | } |
| 479 | |
| 480 | U_BOOT_CMD( |
| 481 | avb, 29, 0, do_avb, |
| 482 | "Provides commands for testing Android Verified Boot 2.0 functionality", |
| 483 | "init <dev> - initialize avb2 for <dev>\n" |
| 484 | "avb read_rb <num> - read rollback index at location <num>\n" |
| 485 | "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n" |
| 486 | "avb is_unlocked - returns unlock status of the device\n" |
| 487 | "avb get_uuid <partname> - read and print uuid of partition <part>\n" |
| 488 | "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n" |
| 489 | " partition <partname> to buffer <addr>\n" |
| 490 | "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n" |
| 491 | " partition <partname> and print to stdout\n" |
| 492 | "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n" |
| 493 | " <partname> by <offset> using data from <addr>\n" |
Igor Opaniuk | 78b0b9d | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 494 | #ifdef CONFIG_OPTEE_TA_AVB |
| 495 | "avb read_pvalue <name> <bytes> - read a persistent value <name>\n" |
| 496 | "avb write_pvalue <name> <value> - write a persistent value <name>\n" |
| 497 | #endif |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 498 | "avb verify [slot_suffix] - run verification process using hash data\n" |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 499 | " from vbmeta structure\n" |
Sam Protsenko | f6b91bf | 2019-10-21 13:55:16 +0300 | [diff] [blame] | 500 | " [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)\n" |
Igor Opaniuk | 6029119 | 2018-06-03 21:56:39 +0300 | [diff] [blame] | 501 | ); |