avb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. return CMD_RET_FAILURE;
  29. }
  30. int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  31. {
  32. const char *part;
  33. s64 offset;
  34. size_t bytes, bytes_read = 0;
  35. void *buffer;
  36. if (!avb_ops) {
  37. printf("AVB 2.0 is not initialized, please run 'avb init'\n");
  38. return CMD_RET_USAGE;
  39. }
  40. if (argc != 5)
  41. return CMD_RET_USAGE;
  42. part = argv[1];
  43. offset = simple_strtoul(argv[2], NULL, 16);
  44. bytes = simple_strtoul(argv[3], NULL, 16);
  45. buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  46. if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
  47. buffer, &bytes_read) ==
  48. AVB_IO_RESULT_OK) {
  49. printf("Read %zu bytes\n", bytes_read);
  50. return CMD_RET_SUCCESS;
  51. }
  52. return CMD_RET_FAILURE;
  53. }
  54. int do_avb_read_part_hex(cmd_tbl_t *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 = simple_strtoul(argv[2], NULL, 16);
  69. bytes = simple_strtoul(argv[3], NULL, 16);
  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. 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. return CMD_RET_FAILURE;
  111. }
  112. int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  113. {
  114. size_t index;
  115. u64 rb_idx;
  116. if (!avb_ops) {
  117. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  118. return CMD_RET_FAILURE;
  119. }
  120. if (argc != 2)
  121. return CMD_RET_USAGE;
  122. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  123. if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
  124. AVB_IO_RESULT_OK) {
  125. printf("Rollback index: %llu\n", rb_idx);
  126. return CMD_RET_SUCCESS;
  127. }
  128. return CMD_RET_FAILURE;
  129. }
  130. int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  131. {
  132. size_t index;
  133. u64 rb_idx;
  134. if (!avb_ops) {
  135. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  136. return CMD_RET_FAILURE;
  137. }
  138. if (argc != 3)
  139. return CMD_RET_USAGE;
  140. index = (size_t)simple_strtoul(argv[1], NULL, 16);
  141. rb_idx = simple_strtoul(argv[2], NULL, 16);
  142. if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
  143. AVB_IO_RESULT_OK)
  144. return CMD_RET_SUCCESS;
  145. return CMD_RET_FAILURE;
  146. }
  147. int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
  148. int argc, char * const argv[])
  149. {
  150. const char *part;
  151. char buffer[UUID_STR_LEN + 1];
  152. if (!avb_ops) {
  153. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  154. return CMD_RET_FAILURE;
  155. }
  156. if (argc != 2)
  157. return CMD_RET_USAGE;
  158. part = argv[1];
  159. if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
  160. UUID_STR_LEN + 1) ==
  161. AVB_IO_RESULT_OK) {
  162. printf("'%s' UUID: %s\n", part, buffer);
  163. return CMD_RET_SUCCESS;
  164. }
  165. return CMD_RET_FAILURE;
  166. }
  167. int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
  168. int argc, char *const argv[])
  169. {
  170. AvbSlotVerifyResult slot_result;
  171. AvbSlotVerifyData *out_data;
  172. char *cmdline;
  173. char *extra_args;
  174. bool unlocked = false;
  175. int res = CMD_RET_FAILURE;
  176. if (!avb_ops) {
  177. printf("AVB 2.0 is not initialized, run 'avb init' first\n");
  178. return CMD_RET_FAILURE;
  179. }
  180. if (argc != 1)
  181. return CMD_RET_USAGE;
  182. printf("## Android Verified Boot 2.0 version %s\n",
  183. avb_version_string());
  184. if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
  185. AVB_IO_RESULT_OK) {
  186. printf("Can't determine device lock state.\n");
  187. return CMD_RET_FAILURE;
  188. }
  189. slot_result =
  190. avb_slot_verify(avb_ops,
  191. requested_partitions,
  192. "",
  193. unlocked,
  194. AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
  195. &out_data);
  196. switch (slot_result) {
  197. case AVB_SLOT_VERIFY_RESULT_OK:
  198. /* Until we don't have support of changing unlock states, we
  199. * assume that we are by default in locked state.
  200. * So in this case we can boot only when verification is
  201. * successful; we also supply in cmdline GREEN boot state
  202. */
  203. printf("Verification passed successfully\n");
  204. /* export additional bootargs to AVB_BOOTARGS env var */
  205. extra_args = avb_set_state(avb_ops, AVB_GREEN);
  206. if (extra_args)
  207. cmdline = append_cmd_line(out_data->cmdline,
  208. extra_args);
  209. else
  210. cmdline = out_data->cmdline;
  211. env_set(AVB_BOOTARGS, cmdline);
  212. res = CMD_RET_SUCCESS;
  213. break;
  214. case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
  215. printf("Verification failed\n");
  216. break;
  217. case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
  218. printf("I/O error occurred during verification\n");
  219. break;
  220. case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
  221. printf("OOM error occurred during verification\n");
  222. break;
  223. case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
  224. printf("Corrupted dm-verity metadata detected\n");
  225. break;
  226. case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
  227. printf("Unsupported version avbtool was used\n");
  228. break;
  229. case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
  230. printf("Checking rollback index failed\n");
  231. break;
  232. case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
  233. printf("Public key was rejected\n");
  234. break;
  235. default:
  236. printf("Unknown error occurred\n");
  237. }
  238. return res;
  239. }
  240. int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
  241. int argc, char * const argv[])
  242. {
  243. bool unlock;
  244. if (!avb_ops) {
  245. printf("AVB not initialized, run 'avb init' first\n");
  246. return CMD_RET_FAILURE;
  247. }
  248. if (argc != 1) {
  249. printf("--%s(-1)\n", __func__);
  250. return CMD_RET_USAGE;
  251. }
  252. if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
  253. AVB_IO_RESULT_OK) {
  254. printf("Unlocked = %d\n", unlock);
  255. return CMD_RET_SUCCESS;
  256. }
  257. return CMD_RET_FAILURE;
  258. }
  259. static cmd_tbl_t cmd_avb[] = {
  260. U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
  261. U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
  262. U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
  263. U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
  264. U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
  265. U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
  266. U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
  267. U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
  268. U_BOOT_CMD_MKENT(verify, 1, 0, do_avb_verify_part, "", ""),
  269. };
  270. static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  271. {
  272. cmd_tbl_t *cp;
  273. cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
  274. argc--;
  275. argv++;
  276. if (!cp || argc > cp->maxargs)
  277. return CMD_RET_USAGE;
  278. if (flag == CMD_FLAG_REPEAT)
  279. return CMD_RET_FAILURE;
  280. return cp->cmd(cmdtp, flag, argc, argv);
  281. }
  282. U_BOOT_CMD(
  283. avb, 29, 0, do_avb,
  284. "Provides commands for testing Android Verified Boot 2.0 functionality",
  285. "init <dev> - initialize avb2 for <dev>\n"
  286. "avb read_rb <num> - read rollback index at location <num>\n"
  287. "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
  288. "avb is_unlocked - returns unlock status of the device\n"
  289. "avb get_uuid <partname> - read and print uuid of partition <part>\n"
  290. "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
  291. " partition <partname> to buffer <addr>\n"
  292. "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
  293. " partition <partname> and print to stdout\n"
  294. "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
  295. " <partname> by <offset> using data from <addr>\n"
  296. "avb verify - run verification process using hash data\n"
  297. " from vbmeta structure\n"
  298. );