3 * (C) Copyright 2018, Linaro Limited
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <avb_verify.h>
15 #define AVB_BOOTARGS "avb_bootargs"
16 static struct AvbOps *avb_ops;
18 int do_avb_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
20 unsigned long mmc_dev;
25 mmc_dev = simple_strtoul(argv[1], NULL, 16);
28 avb_ops_free(avb_ops);
30 avb_ops = avb_ops_alloc(mmc_dev);
32 return CMD_RET_SUCCESS;
34 printf("Failed to initialize avb2\n");
36 return CMD_RET_FAILURE;
39 int do_avb_read_part(struct cmd_tbl *cmdtp, int flag, int argc,
44 size_t bytes, bytes_read = 0;
48 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
56 offset = simple_strtoul(argv[2], NULL, 16);
57 bytes = simple_strtoul(argv[3], NULL, 16);
58 buffer = (void *)simple_strtoul(argv[4], NULL, 16);
60 if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
61 buffer, &bytes_read) ==
63 printf("Read %zu bytes\n", bytes_read);
64 return CMD_RET_SUCCESS;
67 printf("Failed to read from partition\n");
69 return CMD_RET_FAILURE;
72 int do_avb_read_part_hex(struct cmd_tbl *cmdtp, int flag, int argc,
77 size_t bytes, bytes_read = 0;
81 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
89 offset = simple_strtoul(argv[2], NULL, 16);
90 bytes = simple_strtoul(argv[3], NULL, 16);
92 buffer = malloc(bytes);
94 printf("Failed to tlb_allocate buffer for data\n");
95 return CMD_RET_FAILURE;
97 memset(buffer, 0, bytes);
99 if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
100 &bytes_read) == AVB_IO_RESULT_OK) {
101 printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
103 for (int i = 0; i < bytes_read; i++)
104 printf("%02X", buffer[i]);
109 return CMD_RET_SUCCESS;
112 printf("Failed to read from partition\n");
115 return CMD_RET_FAILURE;
118 int do_avb_write_part(struct cmd_tbl *cmdtp, int flag, int argc,
127 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
128 return CMD_RET_FAILURE;
132 return CMD_RET_USAGE;
135 offset = simple_strtoul(argv[2], NULL, 16);
136 bytes = simple_strtoul(argv[3], NULL, 16);
137 buffer = (void *)simple_strtoul(argv[4], NULL, 16);
139 if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
141 printf("Wrote %zu bytes\n", bytes);
142 return CMD_RET_SUCCESS;
145 printf("Failed to write in partition\n");
147 return CMD_RET_FAILURE;
150 int do_avb_read_rb(struct cmd_tbl *cmdtp, int flag, int argc,
157 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
158 return CMD_RET_FAILURE;
162 return CMD_RET_USAGE;
164 index = (size_t)simple_strtoul(argv[1], NULL, 16);
166 if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
168 printf("Rollback index: %llx\n", rb_idx);
169 return CMD_RET_SUCCESS;
172 printf("Failed to read rollback index\n");
174 return CMD_RET_FAILURE;
177 int do_avb_write_rb(struct cmd_tbl *cmdtp, int flag, int argc,
184 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
185 return CMD_RET_FAILURE;
189 return CMD_RET_USAGE;
191 index = (size_t)simple_strtoul(argv[1], NULL, 16);
192 rb_idx = simple_strtoul(argv[2], NULL, 16);
194 if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
196 return CMD_RET_SUCCESS;
198 printf("Failed to write rollback index\n");
200 return CMD_RET_FAILURE;
203 int do_avb_get_uuid(struct cmd_tbl *cmdtp, int flag,
204 int argc, char *const argv[])
207 char buffer[UUID_STR_LEN + 1];
210 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
211 return CMD_RET_FAILURE;
215 return CMD_RET_USAGE;
219 if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
222 printf("'%s' UUID: %s\n", part, buffer);
223 return CMD_RET_SUCCESS;
226 printf("Failed to read UUID\n");
228 return CMD_RET_FAILURE;
231 int do_avb_verify_part(struct cmd_tbl *cmdtp, int flag,
232 int argc, char *const argv[])
234 const char * const requested_partitions[] = {"boot", NULL};
235 AvbSlotVerifyResult slot_result;
236 AvbSlotVerifyData *out_data;
239 char *slot_suffix = "";
241 bool unlocked = false;
242 int res = CMD_RET_FAILURE;
245 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
246 return CMD_RET_FAILURE;
249 if (argc < 1 || argc > 2)
250 return CMD_RET_USAGE;
253 slot_suffix = argv[1];
255 printf("## Android Verified Boot 2.0 version %s\n",
256 avb_version_string());
258 if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
260 printf("Can't determine device lock state.\n");
261 return CMD_RET_FAILURE;
265 avb_slot_verify(avb_ops,
266 requested_partitions,
269 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
272 switch (slot_result) {
273 case AVB_SLOT_VERIFY_RESULT_OK:
274 /* Until we don't have support of changing unlock states, we
275 * assume that we are by default in locked state.
276 * So in this case we can boot only when verification is
277 * successful; we also supply in cmdline GREEN boot state
279 printf("Verification passed successfully\n");
281 /* export additional bootargs to AVB_BOOTARGS env var */
283 extra_args = avb_set_state(avb_ops, AVB_GREEN);
285 cmdline = append_cmd_line(out_data->cmdline,
288 cmdline = out_data->cmdline;
290 env_set(AVB_BOOTARGS, cmdline);
292 res = CMD_RET_SUCCESS;
294 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
295 printf("Verification failed\n");
297 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
298 printf("I/O error occurred during verification\n");
300 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
301 printf("OOM error occurred during verification\n");
303 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
304 printf("Corrupted dm-verity metadata detected\n");
306 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
307 printf("Unsupported version avbtool was used\n");
309 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
310 printf("Checking rollback index failed\n");
312 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
313 printf("Public key was rejected\n");
316 printf("Unknown error occurred\n");
320 avb_slot_verify_data_free(out_data);
325 int do_avb_is_unlocked(struct cmd_tbl *cmdtp, int flag,
326 int argc, char *const argv[])
331 printf("AVB not initialized, run 'avb init' first\n");
332 return CMD_RET_FAILURE;
336 printf("--%s(-1)\n", __func__);
337 return CMD_RET_USAGE;
340 if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
342 printf("Unlocked = %d\n", unlock);
343 return CMD_RET_SUCCESS;
346 printf("Can't determine device lock state.\n");
348 return CMD_RET_FAILURE;
351 int do_avb_read_pvalue(struct cmd_tbl *cmdtp, int flag, int argc,
361 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
362 return CMD_RET_FAILURE;
366 return CMD_RET_USAGE;
369 bytes = simple_strtoul(argv[2], &endp, 10);
370 if (*endp && *endp != '\n')
371 return CMD_RET_USAGE;
373 buffer = malloc(bytes);
375 return CMD_RET_FAILURE;
377 if (avb_ops->read_persistent_value(avb_ops, name, bytes, buffer,
378 &bytes_read) == AVB_IO_RESULT_OK) {
379 printf("Read %zu bytes, value = %s\n", bytes_read,
382 return CMD_RET_SUCCESS;
385 printf("Failed to read persistent value\n");
389 return CMD_RET_FAILURE;
392 int do_avb_write_pvalue(struct cmd_tbl *cmdtp, int flag, int argc,
399 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
400 return CMD_RET_FAILURE;
404 return CMD_RET_USAGE;
409 if (avb_ops->write_persistent_value(avb_ops, name, strlen(value) + 1,
410 (const uint8_t *)value) ==
412 printf("Wrote %zu bytes\n", strlen(value) + 1);
413 return CMD_RET_SUCCESS;
416 printf("Failed to write persistent value\n");
418 return CMD_RET_FAILURE;
421 static struct cmd_tbl cmd_avb[] = {
422 U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
423 U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
424 U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
425 U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
426 U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
427 U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
428 U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
429 U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
430 U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""),
431 #ifdef CONFIG_OPTEE_TA_AVB
432 U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""),
433 U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""),
437 static int do_avb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
441 cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
446 if (!cp || argc > cp->maxargs)
447 return CMD_RET_USAGE;
449 if (flag == CMD_FLAG_REPEAT)
450 return CMD_RET_FAILURE;
452 return cp->cmd(cmdtp, flag, argc, argv);
457 "Provides commands for testing Android Verified Boot 2.0 functionality",
458 "init <dev> - initialize avb2 for <dev>\n"
459 "avb read_rb <num> - read rollback index at location <num>\n"
460 "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
461 "avb is_unlocked - returns unlock status of the device\n"
462 "avb get_uuid <partname> - read and print uuid of partition <part>\n"
463 "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
464 " partition <partname> to buffer <addr>\n"
465 "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
466 " partition <partname> and print to stdout\n"
467 "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
468 " <partname> by <offset> using data from <addr>\n"
469 #ifdef CONFIG_OPTEE_TA_AVB
470 "avb read_pvalue <name> <bytes> - read a persistent value <name>\n"
471 "avb write_pvalue <name> <value> - write a persistent value <name>\n"
473 "avb verify [slot_suffix] - run verification process using hash data\n"
474 " from vbmeta structure\n"
475 " [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)\n"