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;
224 bool unlocked = false;
225 int res = CMD_RET_FAILURE;
228 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
229 return CMD_RET_FAILURE;
233 return CMD_RET_USAGE;
235 printf("## Android Verified Boot 2.0 version %s\n",
236 avb_version_string());
238 if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
240 printf("Can't determine device lock state.\n");
241 return CMD_RET_FAILURE;
245 avb_slot_verify(avb_ops,
246 requested_partitions,
249 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
252 switch (slot_result) {
253 case AVB_SLOT_VERIFY_RESULT_OK:
254 /* Until we don't have support of changing unlock states, we
255 * assume that we are by default in locked state.
256 * So in this case we can boot only when verification is
257 * successful; we also supply in cmdline GREEN boot state
259 printf("Verification passed successfully\n");
261 /* export additional bootargs to AVB_BOOTARGS env var */
263 extra_args = avb_set_state(avb_ops, AVB_GREEN);
265 cmdline = append_cmd_line(out_data->cmdline,
268 cmdline = out_data->cmdline;
270 env_set(AVB_BOOTARGS, cmdline);
272 res = CMD_RET_SUCCESS;
274 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
275 printf("Verification failed\n");
277 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
278 printf("I/O error occurred during verification\n");
280 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
281 printf("OOM error occurred during verification\n");
283 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
284 printf("Corrupted dm-verity metadata detected\n");
286 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
287 printf("Unsupported version avbtool was used\n");
289 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
290 printf("Checking rollback index failed\n");
292 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
293 printf("Public key was rejected\n");
296 printf("Unknown error occurred\n");
302 int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
303 int argc, char * const argv[])
308 printf("AVB not initialized, run 'avb init' first\n");
309 return CMD_RET_FAILURE;
313 printf("--%s(-1)\n", __func__);
314 return CMD_RET_USAGE;
317 if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
319 printf("Unlocked = %d\n", unlock);
320 return CMD_RET_SUCCESS;
323 return CMD_RET_FAILURE;
326 static cmd_tbl_t cmd_avb[] = {
327 U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
328 U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
329 U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
330 U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
331 U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
332 U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
333 U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
334 U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
335 U_BOOT_CMD_MKENT(verify, 1, 0, do_avb_verify_part, "", ""),
338 static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
342 cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
347 if (!cp || argc > cp->maxargs)
348 return CMD_RET_USAGE;
350 if (flag == CMD_FLAG_REPEAT)
351 return CMD_RET_FAILURE;
353 return cp->cmd(cmdtp, flag, argc, argv);
358 "Provides commands for testing Android Verified Boot 2.0 functionality",
359 "init <dev> - initialize avb2 for <dev>\n"
360 "avb read_rb <num> - read rollback index at location <num>\n"
361 "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
362 "avb is_unlocked - returns unlock status of the device\n"
363 "avb get_uuid <partname> - read and print uuid of partition <part>\n"
364 "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
365 " partition <partname> to buffer <addr>\n"
366 "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
367 " partition <partname> and print to stdout\n"
368 "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
369 " <partname> by <offset> using data from <addr>\n"
370 "avb verify - run verification process using hash data\n"
371 " from vbmeta structure\n"