avb.c 11 KB

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