avb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * (C) Copyright 2018, Linaro Limited
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <avb_verify.h>
  7. #include <command.h>
  8. #include <env.h>
  9. #include <image.h>
  10. #include <malloc.h>
  11. #include <mmc.h>
  12. #define AVB_BOOTARGS "avb_bootargs"
  13. static struct AvbOps *avb_ops;
  14. int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  15. {
  16. unsigned long mmc_dev;
  17. if (argc != 2)
  18. return CMD_RET_USAGE;
  19. mmc_dev = simple_strtoul(argv[1], NULL, 16);
  20. if (avb_ops)
  21. avb_ops_free(avb_ops);
  22. avb_ops = avb_ops_alloc(mmc_dev);
  23. if (avb_ops)
  24. return CMD_RET_SUCCESS;
  25. printf("Failed to initialize avb2\n");
  26. return CMD_RET_FAILURE;
  27. }
  28. int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  29. {
  30. const char *part;
  31. s64 offset;
  32. size_t bytes, bytes_read = 0;
  33. void *buffer;
  34. if (!avb_ops) {
  35. printf("AVB 2.0 is not initialized, please run 'avb init'\n");
  36. return CMD_RET_USAGE;
  37. }
  38. if (argc != 5)
  39. return CMD_RET_USAGE;
  40. part = argv[1];
  41. offset = simple_strtoul(argv[2], NULL, 16);
  42. bytes = simple_strtoul(argv[3], NULL, 16);
  43. buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  44. if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
  45. buffer, &bytes_read) ==
  46. AVB_IO_RESULT_OK) {
  47. printf("Read %zu bytes\n", bytes_read);
  48. return CMD_RET_SUCCESS;
  49. }
  50. printf("Failed to read from partition\n");
  51. return CMD_RET_FAILURE;
  52. }
  53. int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
  54. char *const argv[])
  55. {
  56. const char *part;
  57. s64 offset;
  58. size_t bytes, bytes_read = 0;
  59. char *buffer;
  60. if (!avb_ops) {
  61. printf("AVB 2.0 is not initialized, please run 'avb init'\n");
  62. return CMD_RET_USAGE;
  63. }
  64. if (argc != 4)
  65. return CMD_RET_USAGE;
  66. part = argv[1];
  67. offset = simple_strtoul(argv[2], NULL, 16);
  68. bytes = simple_strtoul(argv[3], NULL, 16);
  69. buffer = malloc(bytes);
  70. if (!buffer) {
  71. printf("Failed to tlb_allocate buffer for data\n");
  72. return CMD_RET_FAILURE;
  73. }
  74. memset(buffer, 0, bytes);
  75. if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
  76. &bytes_read) == AVB_IO_RESULT_OK) {
  77. printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
  78. printf("Data: ");
  79. for (int i = 0; i < bytes_read; i++)
  80. printf("%02X", buffer[i]);
  81. printf("\n");
  82. free(buffer);
  83. return CMD_RET_SUCCESS;
  84. }
  85. printf("Failed to read from partition\n");
  86. free(buffer);
  87. return CMD_RET_FAILURE;
  88. }
  89. int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  90. {
  91. const char *part;
  92. s64 offset;
  93. size_t bytes;
  94. void *buffer;
  95. if (!avb_ops) {
  96. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  97. return CMD_RET_FAILURE;
  98. }
  99. if (argc != 5)
  100. return CMD_RET_USAGE;
  101. part = argv[1];
  102. offset = simple_strtoul(argv[2], NULL, 16);
  103. bytes = simple_strtoul(argv[3], NULL, 16);
  104. buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  105. if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
  106. AVB_IO_RESULT_OK) {
  107. printf("Wrote %zu bytes\n", bytes);
  108. return CMD_RET_SUCCESS;
  109. }
  110. printf("Failed to write in partition\n");
  111. return CMD_RET_FAILURE;
  112. }
  113. int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  114. {
  115. size_t index;
  116. u64 rb_idx;
  117. if (!avb_ops) {
  118. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  119. return CMD_RET_FAILURE;
  120. }
  121. if (argc != 2)
  122. return CMD_RET_USAGE;
  123. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  124. if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
  125. AVB_IO_RESULT_OK) {
  126. printf("Rollback index: %llx\n", rb_idx);
  127. return CMD_RET_SUCCESS;
  128. }
  129. printf("Failed to read rollback index\n");
  130. return CMD_RET_FAILURE;
  131. }
  132. int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  133. {
  134. size_t index;
  135. u64 rb_idx;
  136. if (!avb_ops) {
  137. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  138. return CMD_RET_FAILURE;
  139. }
  140. if (argc != 3)
  141. return CMD_RET_USAGE;
  142. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  143. rb_idx = simple_strtoul(argv[2], NULL, 16);
  144. if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
  145. AVB_IO_RESULT_OK)
  146. return CMD_RET_SUCCESS;
  147. printf("Failed to write rollback index\n");
  148. return CMD_RET_FAILURE;
  149. }
  150. int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
  151. int argc, char * const argv[])
  152. {
  153. const char *part;
  154. char buffer[UUID_STR_LEN + 1];
  155. if (!avb_ops) {
  156. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  157. return CMD_RET_FAILURE;
  158. }
  159. if (argc != 2)
  160. return CMD_RET_USAGE;
  161. part = argv[1];
  162. if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
  163. UUID_STR_LEN + 1) ==
  164. AVB_IO_RESULT_OK) {
  165. printf("'%s' UUID: %s\n", part, buffer);
  166. return CMD_RET_SUCCESS;
  167. }
  168. printf("Failed to read UUID\n");
  169. return CMD_RET_FAILURE;
  170. }
  171. int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
  172. int argc, char *const argv[])
  173. {
  174. const char * const requested_partitions[] = {"boot", NULL};
  175. AvbSlotVerifyResult slot_result;
  176. AvbSlotVerifyData *out_data;
  177. char *cmdline;
  178. char *extra_args;
  179. char *slot_suffix = "";
  180. bool unlocked = false;
  181. int res = CMD_RET_FAILURE;
  182. if (!avb_ops) {
  183. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  184. return CMD_RET_FAILURE;
  185. }
  186. if (argc < 1 || argc > 2)
  187. return CMD_RET_USAGE;
  188. if (argc == 2)
  189. slot_suffix = argv[1];
  190. printf("## Android Verified Boot 2.0 version %s\n",
  191. avb_version_string());
  192. if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
  193. AVB_IO_RESULT_OK) {
  194. printf("Can't determine device lock state.\n");
  195. return CMD_RET_FAILURE;
  196. }
  197. slot_result =
  198. avb_slot_verify(avb_ops,
  199. requested_partitions,
  200. slot_suffix,
  201. unlocked,
  202. AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
  203. &out_data);
  204. switch (slot_result) {
  205. case AVB_SLOT_VERIFY_RESULT_OK:
  206. /* Until we don't have support of changing unlock states, we
  207. * assume that we are by default in locked state.
  208. * So in this case we can boot only when verification is
  209. * successful; we also supply in cmdline GREEN boot state
  210. */
  211. printf("Verification passed successfully\n");
  212. /* export additional bootargs to AVB_BOOTARGS env var */
  213. extra_args = avb_set_state(avb_ops, AVB_GREEN);
  214. if (extra_args)
  215. cmdline = append_cmd_line(out_data->cmdline,
  216. extra_args);
  217. else
  218. cmdline = out_data->cmdline;
  219. env_set(AVB_BOOTARGS, cmdline);
  220. res = CMD_RET_SUCCESS;
  221. break;
  222. case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
  223. printf("Verification failed\n");
  224. break;
  225. case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
  226. printf("I/O error occurred during verification\n");
  227. break;
  228. case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
  229. printf("OOM error occurred during verification\n");
  230. break;
  231. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
  232. printf("Corrupted dm-verity metadata detected\n");
  233. break;
  234. case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
  235. printf("Unsupported version avbtool was used\n");
  236. break;
  237. case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
  238. printf("Checking rollback index failed\n");
  239. break;
  240. case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
  241. printf("Public key was rejected\n");
  242. break;
  243. default:
  244. printf("Unknown error occurred\n");
  245. }
  246. return res;
  247. }
  248. int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
  249. int argc, char * const argv[])
  250. {
  251. bool unlock;
  252. if (!avb_ops) {
  253. printf("AVB not initialized, run 'avb init' first\n");
  254. return CMD_RET_FAILURE;
  255. }
  256. if (argc != 1) {
  257. printf("--%s(-1)\n", __func__);
  258. return CMD_RET_USAGE;
  259. }
  260. if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
  261. AVB_IO_RESULT_OK) {
  262. printf("Unlocked = %d\n", unlock);
  263. return CMD_RET_SUCCESS;
  264. }
  265. printf("Can't determine device lock state.\n");
  266. return CMD_RET_FAILURE;
  267. }
  268. int do_avb_read_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
  269. char * const argv[])
  270. {
  271. const char *name;
  272. size_t bytes;
  273. size_t bytes_read;
  274. void *buffer;
  275. char *endp;
  276. if (!avb_ops) {
  277. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  278. return CMD_RET_FAILURE;
  279. }
  280. if (argc != 3)
  281. return CMD_RET_USAGE;
  282. name = argv[1];
  283. bytes = simple_strtoul(argv[2], &endp, 10);
  284. if (*endp && *endp != '\n')
  285. return CMD_RET_USAGE;
  286. buffer = malloc(bytes);
  287. if (!buffer)
  288. return CMD_RET_FAILURE;
  289. if (avb_ops->read_persistent_value(avb_ops, name, bytes, buffer,
  290. &bytes_read) == AVB_IO_RESULT_OK) {
  291. printf("Read %zu bytes, value = %s\n", bytes_read,
  292. (char *)buffer);
  293. free(buffer);
  294. return CMD_RET_SUCCESS;
  295. }
  296. printf("Failed to read persistent value\n");
  297. free(buffer);
  298. return CMD_RET_FAILURE;
  299. }
  300. int do_avb_write_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
  301. char * const argv[])
  302. {
  303. const char *name;
  304. const char *value;
  305. if (!avb_ops) {
  306. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  307. return CMD_RET_FAILURE;
  308. }
  309. if (argc != 3)
  310. return CMD_RET_USAGE;
  311. name = argv[1];
  312. value = argv[2];
  313. if (avb_ops->write_persistent_value(avb_ops, name, strlen(value) + 1,
  314. (const uint8_t *)value) ==
  315. AVB_IO_RESULT_OK) {
  316. printf("Wrote %zu bytes\n", strlen(value) + 1);
  317. return CMD_RET_SUCCESS;
  318. }
  319. printf("Failed to write persistent value\n");
  320. return CMD_RET_FAILURE;
  321. }
  322. static cmd_tbl_t cmd_avb[] = {
  323. U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
  324. U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
  325. U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
  326. U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
  327. U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
  328. U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
  329. U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
  330. U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
  331. U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""),
  332. #ifdef CONFIG_OPTEE_TA_AVB
  333. U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""),
  334. U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""),
  335. #endif
  336. };
  337. static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  338. {
  339. cmd_tbl_t *cp;
  340. cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
  341. argc--;
  342. argv++;
  343. if (!cp || argc > cp->maxargs)
  344. return CMD_RET_USAGE;
  345. if (flag == CMD_FLAG_REPEAT)
  346. return CMD_RET_FAILURE;
  347. return cp->cmd(cmdtp, flag, argc, argv);
  348. }
  349. U_BOOT_CMD(
  350. avb, 29, 0, do_avb,
  351. "Provides commands for testing Android Verified Boot 2.0 functionality",
  352. "init <dev> - initialize avb2 for <dev>\n"
  353. "avb read_rb <num> - read rollback index at location <num>\n"
  354. "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
  355. "avb is_unlocked - returns unlock status of the device\n"
  356. "avb get_uuid <partname> - read and print uuid of partition <part>\n"
  357. "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
  358. " partition <partname> to buffer <addr>\n"
  359. "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
  360. " partition <partname> and print to stdout\n"
  361. "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
  362. " <partname> by <offset> using data from <addr>\n"
  363. #ifdef CONFIG_OPTEE_TA_AVB
  364. "avb read_pvalue <name> <bytes> - read a persistent value <name>\n"
  365. "avb write_pvalue <name> <value> - write a persistent value <name>\n"
  366. #endif
  367. "avb verify [slot_suffix] - run verification process using hash data\n"
  368. " from vbmeta structure\n"
  369. " [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)\n"
  370. );