3 * (C) Copyright 2018, Linaro Limited
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <avb_verify.h>
14 #define AVB_BOOTARGS "avb_bootargs"
15 static struct AvbOps *avb_ops;
17 static const char * const requested_partitions[] = {"boot",
22 int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
24 unsigned long mmc_dev;
29 mmc_dev = simple_strtoul(argv[1], NULL, 16);
32 avb_ops_free(avb_ops);
34 avb_ops = avb_ops_alloc(mmc_dev);
36 return CMD_RET_SUCCESS;
38 return CMD_RET_FAILURE;
41 int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
45 size_t bytes, bytes_read = 0;
49 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
57 offset = simple_strtoul(argv[2], NULL, 16);
58 bytes = simple_strtoul(argv[3], NULL, 16);
59 buffer = (void *)simple_strtoul(argv[4], NULL, 16);
61 if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
62 buffer, &bytes_read) ==
64 printf("Read %zu bytes\n", bytes_read);
65 return CMD_RET_SUCCESS;
68 return CMD_RET_FAILURE;
71 int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
76 size_t bytes, bytes_read = 0;
80 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
88 offset = simple_strtoul(argv[2], NULL, 16);
89 bytes = simple_strtoul(argv[3], NULL, 16);
91 buffer = malloc(bytes);
93 printf("Failed to tlb_allocate buffer for data\n");
94 return CMD_RET_FAILURE;
96 memset(buffer, 0, bytes);
98 if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
99 &bytes_read) == AVB_IO_RESULT_OK) {
100 printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
102 for (int i = 0; i < bytes_read; i++)
103 printf("%02X", buffer[i]);
108 return CMD_RET_SUCCESS;
112 return CMD_RET_FAILURE;
115 int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
124 return CMD_RET_FAILURE;
128 return CMD_RET_USAGE;
131 offset = simple_strtoul(argv[2], NULL, 16);
132 bytes = simple_strtoul(argv[3], NULL, 16);
133 buffer = (void *)simple_strtoul(argv[4], NULL, 16);
135 if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
137 printf("Wrote %zu bytes\n", bytes);
138 return CMD_RET_SUCCESS;
141 return CMD_RET_FAILURE;
144 int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
150 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
151 return CMD_RET_FAILURE;
155 return CMD_RET_USAGE;
157 index = (size_t)simple_strtoul(argv[1], NULL, 16);
159 if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
161 printf("Rollback index: %llu\n", rb_idx);
162 return CMD_RET_SUCCESS;
164 return CMD_RET_FAILURE;
167 int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
173 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
174 return CMD_RET_FAILURE;
178 return CMD_RET_USAGE;
180 index = (size_t)simple_strtoul(argv[1], NULL, 16);
181 rb_idx = simple_strtoul(argv[2], NULL, 16);
183 if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
185 return CMD_RET_SUCCESS;
187 return CMD_RET_FAILURE;
190 int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
191 int argc, char * const argv[])
194 char buffer[UUID_STR_LEN + 1];
197 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
198 return CMD_RET_FAILURE;
202 return CMD_RET_USAGE;
206 if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
209 printf("'%s' UUID: %s\n", part, buffer);
210 return CMD_RET_SUCCESS;
213 return CMD_RET_FAILURE;
216 int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
217 int argc, char *const argv[])
219 AvbSlotVerifyResult slot_result;
220 AvbSlotVerifyData *out_data;
222 bool unlocked = false;
223 int res = CMD_RET_FAILURE;
226 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
227 return CMD_RET_FAILURE;
231 return CMD_RET_USAGE;
233 printf("## Android Verified Boot 2.0 version %s\n",
234 avb_version_string());
236 if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
238 printf("Can't determine device lock state.\n");
239 return CMD_RET_FAILURE;
243 avb_slot_verify(avb_ops,
244 requested_partitions,
247 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
250 switch (slot_result) {
251 case AVB_SLOT_VERIFY_RESULT_OK:
252 printf("Verification passed successfully\n");
254 /* export additional bootargs to AVB_BOOTARGS env var */
255 env_set(AVB_BOOTARGS, out_data->cmdline);
257 res = CMD_RET_SUCCESS;
259 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
260 printf("Verification failed\n");
262 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
263 printf("I/O error occurred during verification\n");
265 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
266 printf("OOM error occurred during verification\n");
268 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
269 printf("Corrupted dm-verity metadata detected\n");
271 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
272 printf("Unsupported version avbtool was used\n");
274 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
275 printf("Checking rollback index failed\n");
277 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
278 printf("Public key was rejected\n");
281 printf("Unknown error occurred\n");
287 int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
288 int argc, char * const argv[])
293 printf("AVB not initialized, run 'avb init' first\n");
294 return CMD_RET_FAILURE;
298 printf("--%s(-1)\n", __func__);
299 return CMD_RET_USAGE;
302 if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
304 printf("Unlocked = %d\n", unlock);
305 return CMD_RET_SUCCESS;
308 return CMD_RET_FAILURE;
311 static cmd_tbl_t cmd_avb[] = {
312 U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
313 U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
314 U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
315 U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
316 U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
317 U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
318 U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
319 U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
320 U_BOOT_CMD_MKENT(verify, 1, 0, do_avb_verify_part, "", ""),
323 static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
327 cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
332 if (!cp || argc > cp->maxargs)
333 return CMD_RET_USAGE;
335 if (flag == CMD_FLAG_REPEAT)
336 return CMD_RET_FAILURE;
338 return cp->cmd(cmdtp, flag, argc, argv);
343 "Provides commands for testing Android Verified Boot 2.0 functionality",
344 "init <dev> - initialize avb2 for <dev>\n"
345 "avb read_rb <num> - read rollback index at location <num>\n"
346 "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
347 "avb is_unlocked - returns unlock status of the device\n"
348 "avb get_uuid <partname> - read and print uuid of partition <part>\n"
349 "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
350 " partition <partname> to buffer <addr>\n"
351 "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
352 " partition <partname> and print to stdout\n"
353 "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
354 " <partname> by <offset> using data from <addr>\n"
355 "avb verify - run verification process using hash data\n"
356 " from vbmeta structure\n"