avb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. static const char * const requested_partitions[] = {"boot",
  15. "system",
  16. "vendor",
  17. NULL};
  18. int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  19. {
  20. unsigned long mmc_dev;
  21. if (argc != 2)
  22. return CMD_RET_USAGE;
  23. mmc_dev = simple_strtoul(argv[1], NULL, 16);
  24. if (avb_ops)
  25. avb_ops_free(avb_ops);
  26. avb_ops = avb_ops_alloc(mmc_dev);
  27. if (avb_ops)
  28. return CMD_RET_SUCCESS;
  29. printf("Failed to initialize avb2\n");
  30. return CMD_RET_FAILURE;
  31. }
  32. int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  33. {
  34. const char *part;
  35. s64 offset;
  36. size_t bytes, bytes_read = 0;
  37. void *buffer;
  38. if (!avb_ops) {
  39. printf("AVB 2.0 is not initialized, please run 'avb init'\n");
  40. return CMD_RET_USAGE;
  41. }
  42. if (argc != 5)
  43. return CMD_RET_USAGE;
  44. part = argv[1];
  45. offset = simple_strtoul(argv[2], NULL, 16);
  46. bytes = simple_strtoul(argv[3], NULL, 16);
  47. buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  48. if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
  49. buffer, &bytes_read) ==
  50. AVB_IO_RESULT_OK) {
  51. printf("Read %zu bytes\n", bytes_read);
  52. return CMD_RET_SUCCESS;
  53. }
  54. printf("Failed to read from partition\n");
  55. return CMD_RET_FAILURE;
  56. }
  57. int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
  58. char *const argv[])
  59. {
  60. const char *part;
  61. s64 offset;
  62. size_t bytes, bytes_read = 0;
  63. char *buffer;
  64. if (!avb_ops) {
  65. printf("AVB 2.0 is not initialized, please run 'avb init'\n");
  66. return CMD_RET_USAGE;
  67. }
  68. if (argc != 4)
  69. return CMD_RET_USAGE;
  70. part = argv[1];
  71. offset = simple_strtoul(argv[2], NULL, 16);
  72. bytes = simple_strtoul(argv[3], NULL, 16);
  73. buffer = malloc(bytes);
  74. if (!buffer) {
  75. printf("Failed to tlb_allocate buffer for data\n");
  76. return CMD_RET_FAILURE;
  77. }
  78. memset(buffer, 0, bytes);
  79. if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
  80. &bytes_read) == AVB_IO_RESULT_OK) {
  81. printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
  82. printf("Data: ");
  83. for (int i = 0; i < bytes_read; i++)
  84. printf("%02X", buffer[i]);
  85. printf("\n");
  86. free(buffer);
  87. return CMD_RET_SUCCESS;
  88. }
  89. printf("Failed to read from partition\n");
  90. free(buffer);
  91. return CMD_RET_FAILURE;
  92. }
  93. int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  94. {
  95. const char *part;
  96. s64 offset;
  97. size_t bytes;
  98. void *buffer;
  99. if (!avb_ops) {
  100. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  101. return CMD_RET_FAILURE;
  102. }
  103. if (argc != 5)
  104. return CMD_RET_USAGE;
  105. part = argv[1];
  106. offset = simple_strtoul(argv[2], NULL, 16);
  107. bytes = simple_strtoul(argv[3], NULL, 16);
  108. buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  109. if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
  110. AVB_IO_RESULT_OK) {
  111. printf("Wrote %zu bytes\n", bytes);
  112. return CMD_RET_SUCCESS;
  113. }
  114. printf("Failed to write in partition\n");
  115. return CMD_RET_FAILURE;
  116. }
  117. int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  118. {
  119. size_t index;
  120. u64 rb_idx;
  121. if (!avb_ops) {
  122. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  123. return CMD_RET_FAILURE;
  124. }
  125. if (argc != 2)
  126. return CMD_RET_USAGE;
  127. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  128. if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
  129. AVB_IO_RESULT_OK) {
  130. printf("Rollback index: %llx\n", rb_idx);
  131. return CMD_RET_SUCCESS;
  132. }
  133. printf("Failed to read rollback index\n");
  134. return CMD_RET_FAILURE;
  135. }
  136. int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  137. {
  138. size_t index;
  139. u64 rb_idx;
  140. if (!avb_ops) {
  141. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  142. return CMD_RET_FAILURE;
  143. }
  144. if (argc != 3)
  145. return CMD_RET_USAGE;
  146. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  147. rb_idx = simple_strtoul(argv[2], NULL, 16);
  148. if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
  149. AVB_IO_RESULT_OK)
  150. return CMD_RET_SUCCESS;
  151. printf("Failed to write rollback index\n");
  152. return CMD_RET_FAILURE;
  153. }
  154. int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
  155. int argc, char * const argv[])
  156. {
  157. const char *part;
  158. char buffer[UUID_STR_LEN + 1];
  159. if (!avb_ops) {
  160. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  161. return CMD_RET_FAILURE;
  162. }
  163. if (argc != 2)
  164. return CMD_RET_USAGE;
  165. part = argv[1];
  166. if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
  167. UUID_STR_LEN + 1) ==
  168. AVB_IO_RESULT_OK) {
  169. printf("'%s' UUID: %s\n", part, buffer);
  170. return CMD_RET_SUCCESS;
  171. }
  172. printf("Failed to read UUID\n");
  173. return CMD_RET_FAILURE;
  174. }
  175. int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
  176. int argc, char *const argv[])
  177. {
  178. AvbSlotVerifyResult slot_result;
  179. AvbSlotVerifyData *out_data;
  180. char *cmdline;
  181. char *extra_args;
  182. bool unlocked = false;
  183. int res = CMD_RET_FAILURE;
  184. if (!avb_ops) {
  185. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  186. return CMD_RET_FAILURE;
  187. }
  188. if (argc != 1)
  189. return CMD_RET_USAGE;
  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. "",
  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, 1, 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 - run verification process using hash data\n"
  368. " from vbmeta structure\n"
  369. );