avb.c 11 KB

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