hab.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <config.h>
  8. #include <fuse.h>
  9. #include <asm/io.h>
  10. #include <asm/system.h>
  11. #include <asm/arch/clock.h>
  12. #include <asm/arch/sys_proto.h>
  13. #include <asm/imx-common/hab.h>
  14. /* -------- start of HAB API updates ------------*/
  15. #define hab_rvt_report_event_p \
  16. ( \
  17. (is_mx6dqp()) ? \
  18. ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
  19. (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
  20. ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
  21. (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
  22. ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
  23. ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT) \
  24. )
  25. #define hab_rvt_report_status_p \
  26. ( \
  27. (is_mx6dqp()) ? \
  28. ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
  29. (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
  30. ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
  31. (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
  32. ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
  33. ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS) \
  34. )
  35. #define hab_rvt_authenticate_image_p \
  36. ( \
  37. (is_mx6dqp()) ? \
  38. ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
  39. (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
  40. ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
  41. (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
  42. ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
  43. ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE) \
  44. )
  45. #define hab_rvt_entry_p \
  46. ( \
  47. (is_mx6dqp()) ? \
  48. ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
  49. (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
  50. ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
  51. (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
  52. ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
  53. ((hab_rvt_entry_t *)HAB_RVT_ENTRY) \
  54. )
  55. #define hab_rvt_exit_p \
  56. ( \
  57. (is_mx6dqp()) ? \
  58. ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
  59. (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
  60. ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
  61. (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
  62. ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
  63. ((hab_rvt_exit_t *)HAB_RVT_EXIT) \
  64. )
  65. #define IVT_SIZE 0x20
  66. #define ALIGN_SIZE 0x1000
  67. #define CSF_PAD_SIZE 0x2000
  68. #define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
  69. #define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
  70. #define MX6SL_PU_IROM_MMU_EN_VAR 0x00900a18
  71. #define IS_HAB_ENABLED_BIT \
  72. (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2)
  73. /*
  74. * +------------+ 0x0 (DDR_UIMAGE_START) -
  75. * | Header | |
  76. * +------------+ 0x40 |
  77. * | | |
  78. * | | |
  79. * | | |
  80. * | | |
  81. * | Image Data | |
  82. * . | |
  83. * . | > Stuff to be authenticated ----+
  84. * . | | |
  85. * | | | |
  86. * | | | |
  87. * +------------+ | |
  88. * | | | |
  89. * | Fill Data | | |
  90. * | | | |
  91. * +------------+ Align to ALIGN_SIZE | |
  92. * | IVT | | |
  93. * +------------+ + IVT_SIZE - |
  94. * | | |
  95. * | CSF DATA | <---------------------------------------------------------+
  96. * | |
  97. * +------------+
  98. * | |
  99. * | Fill Data |
  100. * | |
  101. * +------------+ + CSF_PAD_SIZE
  102. */
  103. static bool is_hab_enabled(void);
  104. #if !defined(CONFIG_SPL_BUILD)
  105. #define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
  106. struct record {
  107. uint8_t tag; /* Tag */
  108. uint8_t len[2]; /* Length */
  109. uint8_t par; /* Version */
  110. uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
  111. bool any_rec_flag;
  112. };
  113. char *rsn_str[] = {"RSN = HAB_RSN_ANY (0x00)\n",
  114. "RSN = HAB_ENG_FAIL (0x30)\n",
  115. "RSN = HAB_INV_ADDRESS (0x22)\n",
  116. "RSN = HAB_INV_ASSERTION (0x0C)\n",
  117. "RSN = HAB_INV_CALL (0x28)\n",
  118. "RSN = HAB_INV_CERTIFICATE (0x21)\n",
  119. "RSN = HAB_INV_COMMAND (0x06)\n",
  120. "RSN = HAB_INV_CSF (0x11)\n",
  121. "RSN = HAB_INV_DCD (0x27)\n",
  122. "RSN = HAB_INV_INDEX (0x0F)\n",
  123. "RSN = HAB_INV_IVT (0x05)\n",
  124. "RSN = HAB_INV_KEY (0x1D)\n",
  125. "RSN = HAB_INV_RETURN (0x1E)\n",
  126. "RSN = HAB_INV_SIGNATURE (0x18)\n",
  127. "RSN = HAB_INV_SIZE (0x17)\n",
  128. "RSN = HAB_MEM_FAIL (0x2E)\n",
  129. "RSN = HAB_OVR_COUNT (0x2B)\n",
  130. "RSN = HAB_OVR_STORAGE (0x2D)\n",
  131. "RSN = HAB_UNS_ALGORITHM (0x12)\n",
  132. "RSN = HAB_UNS_COMMAND (0x03)\n",
  133. "RSN = HAB_UNS_ENGINE (0x0A)\n",
  134. "RSN = HAB_UNS_ITEM (0x24)\n",
  135. "RSN = HAB_UNS_KEY (0x1B)\n",
  136. "RSN = HAB_UNS_PROTOCOL (0x14)\n",
  137. "RSN = HAB_UNS_STATE (0x09)\n",
  138. "RSN = INVALID\n",
  139. NULL};
  140. char *sts_str[] = {"STS = HAB_SUCCESS (0xF0)\n",
  141. "STS = HAB_FAILURE (0x33)\n",
  142. "STS = HAB_WARNING (0x69)\n",
  143. "STS = INVALID\n",
  144. NULL};
  145. char *eng_str[] = {"ENG = HAB_ENG_ANY (0x00)\n",
  146. "ENG = HAB_ENG_SCC (0x03)\n",
  147. "ENG = HAB_ENG_RTIC (0x05)\n",
  148. "ENG = HAB_ENG_SAHARA (0x06)\n",
  149. "ENG = HAB_ENG_CSU (0x0A)\n",
  150. "ENG = HAB_ENG_SRTC (0x0C)\n",
  151. "ENG = HAB_ENG_DCP (0x1B)\n",
  152. "ENG = HAB_ENG_CAAM (0x1D)\n",
  153. "ENG = HAB_ENG_SNVS (0x1E)\n",
  154. "ENG = HAB_ENG_OCOTP (0x21)\n",
  155. "ENG = HAB_ENG_DTCP (0x22)\n",
  156. "ENG = HAB_ENG_ROM (0x36)\n",
  157. "ENG = HAB_ENG_HDCP (0x24)\n",
  158. "ENG = HAB_ENG_RTL (0x77)\n",
  159. "ENG = HAB_ENG_SW (0xFF)\n",
  160. "ENG = INVALID\n",
  161. NULL};
  162. char *ctx_str[] = {"CTX = HAB_CTX_ANY(0x00)\n",
  163. "CTX = HAB_CTX_FAB (0xFF)\n",
  164. "CTX = HAB_CTX_ENTRY (0xE1)\n",
  165. "CTX = HAB_CTX_TARGET (0x33)\n",
  166. "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
  167. "CTX = HAB_CTX_DCD (0xDD)\n",
  168. "CTX = HAB_CTX_CSF (0xCF)\n",
  169. "CTX = HAB_CTX_COMMAND (0xC0)\n",
  170. "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
  171. "CTX = HAB_CTX_ASSERT (0xA0)\n",
  172. "CTX = HAB_CTX_EXIT (0xEE)\n",
  173. "CTX = INVALID\n",
  174. NULL};
  175. uint8_t hab_statuses[5] = {
  176. HAB_STS_ANY,
  177. HAB_FAILURE,
  178. HAB_WARNING,
  179. HAB_SUCCESS,
  180. -1
  181. };
  182. uint8_t hab_reasons[26] = {
  183. HAB_RSN_ANY,
  184. HAB_ENG_FAIL,
  185. HAB_INV_ADDRESS,
  186. HAB_INV_ASSERTION,
  187. HAB_INV_CALL,
  188. HAB_INV_CERTIFICATE,
  189. HAB_INV_COMMAND,
  190. HAB_INV_CSF,
  191. HAB_INV_DCD,
  192. HAB_INV_INDEX,
  193. HAB_INV_IVT,
  194. HAB_INV_KEY,
  195. HAB_INV_RETURN,
  196. HAB_INV_SIGNATURE,
  197. HAB_INV_SIZE,
  198. HAB_MEM_FAIL,
  199. HAB_OVR_COUNT,
  200. HAB_OVR_STORAGE,
  201. HAB_UNS_ALGORITHM,
  202. HAB_UNS_COMMAND,
  203. HAB_UNS_ENGINE,
  204. HAB_UNS_ITEM,
  205. HAB_UNS_KEY,
  206. HAB_UNS_PROTOCOL,
  207. HAB_UNS_STATE,
  208. -1
  209. };
  210. uint8_t hab_contexts[12] = {
  211. HAB_CTX_ANY,
  212. HAB_CTX_FAB,
  213. HAB_CTX_ENTRY,
  214. HAB_CTX_TARGET,
  215. HAB_CTX_AUTHENTICATE,
  216. HAB_CTX_DCD,
  217. HAB_CTX_CSF,
  218. HAB_CTX_COMMAND,
  219. HAB_CTX_AUT_DAT,
  220. HAB_CTX_ASSERT,
  221. HAB_CTX_EXIT,
  222. -1
  223. };
  224. uint8_t hab_engines[16] = {
  225. HAB_ENG_ANY,
  226. HAB_ENG_SCC,
  227. HAB_ENG_RTIC,
  228. HAB_ENG_SAHARA,
  229. HAB_ENG_CSU,
  230. HAB_ENG_SRTC,
  231. HAB_ENG_DCP,
  232. HAB_ENG_CAAM,
  233. HAB_ENG_SNVS,
  234. HAB_ENG_OCOTP,
  235. HAB_ENG_DTCP,
  236. HAB_ENG_ROM,
  237. HAB_ENG_HDCP,
  238. HAB_ENG_RTL,
  239. HAB_ENG_SW,
  240. -1
  241. };
  242. static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
  243. {
  244. uint8_t idx = 0;
  245. uint8_t element = list[idx];
  246. while (element != -1) {
  247. if (element == tgt)
  248. return idx;
  249. element = list[++idx];
  250. }
  251. return -1;
  252. }
  253. void process_event_record(uint8_t *event_data, size_t bytes)
  254. {
  255. struct record *rec = (struct record *)event_data;
  256. printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
  257. printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
  258. printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
  259. printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
  260. }
  261. void display_event(uint8_t *event_data, size_t bytes)
  262. {
  263. uint32_t i;
  264. if (!(event_data && bytes > 0))
  265. return;
  266. for (i = 0; i < bytes; i++) {
  267. if (i == 0)
  268. printf("\t0x%02x", event_data[i]);
  269. else if ((i % 8) == 0)
  270. printf("\n\t0x%02x", event_data[i]);
  271. else
  272. printf(" 0x%02x", event_data[i]);
  273. }
  274. process_event_record(event_data, bytes);
  275. }
  276. int get_hab_status(void)
  277. {
  278. uint32_t index = 0; /* Loop index */
  279. uint8_t event_data[128]; /* Event data buffer */
  280. size_t bytes = sizeof(event_data); /* Event size in bytes */
  281. enum hab_config config = 0;
  282. enum hab_state state = 0;
  283. hab_rvt_report_event_t *hab_rvt_report_event;
  284. hab_rvt_report_status_t *hab_rvt_report_status;
  285. hab_rvt_report_event = hab_rvt_report_event_p;
  286. hab_rvt_report_status = hab_rvt_report_status_p;
  287. if (is_hab_enabled())
  288. puts("\nSecure boot enabled\n");
  289. else
  290. puts("\nSecure boot disabled\n");
  291. /* Check HAB status */
  292. if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
  293. printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
  294. config, state);
  295. /* Display HAB Error events */
  296. while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
  297. &bytes) == HAB_SUCCESS) {
  298. puts("\n");
  299. printf("--------- HAB Event %d -----------------\n",
  300. index + 1);
  301. puts("event data:\n");
  302. display_event(event_data, bytes);
  303. puts("\n");
  304. bytes = sizeof(event_data);
  305. index++;
  306. }
  307. }
  308. /* Display message if no HAB events are found */
  309. else {
  310. printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
  311. config, state);
  312. puts("No HAB Events Found!\n\n");
  313. }
  314. return 0;
  315. }
  316. int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  317. {
  318. if ((argc != 1)) {
  319. cmd_usage(cmdtp);
  320. return 1;
  321. }
  322. get_hab_status();
  323. return 0;
  324. }
  325. static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc,
  326. char * const argv[])
  327. {
  328. ulong addr, ivt_offset;
  329. int rcode = 0;
  330. if (argc < 3)
  331. return CMD_RET_USAGE;
  332. addr = simple_strtoul(argv[1], NULL, 16);
  333. ivt_offset = simple_strtoul(argv[2], NULL, 16);
  334. rcode = authenticate_image(addr, ivt_offset);
  335. return rcode;
  336. }
  337. U_BOOT_CMD(
  338. hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
  339. "display HAB status",
  340. ""
  341. );
  342. U_BOOT_CMD(
  343. hab_auth_img, 3, 0, do_authenticate_image,
  344. "authenticate image via HAB",
  345. "addr ivt_offset\n"
  346. "addr - image hex address\n"
  347. "ivt_offset - hex offset of IVT in the image"
  348. );
  349. #endif /* !defined(CONFIG_SPL_BUILD) */
  350. static bool is_hab_enabled(void)
  351. {
  352. struct imx_sec_config_fuse_t *fuse =
  353. (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
  354. uint32_t reg;
  355. int ret;
  356. ret = fuse_read(fuse->bank, fuse->word, &reg);
  357. if (ret) {
  358. puts("\nSecure boot fuse read error\n");
  359. return ret;
  360. }
  361. return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
  362. }
  363. uint32_t authenticate_image(uint32_t ddr_start, uint32_t image_size)
  364. {
  365. uint32_t load_addr = 0;
  366. size_t bytes;
  367. ptrdiff_t ivt_offset = 0;
  368. int result = 0;
  369. ulong start;
  370. hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
  371. hab_rvt_entry_t *hab_rvt_entry;
  372. hab_rvt_exit_t *hab_rvt_exit;
  373. hab_rvt_authenticate_image = hab_rvt_authenticate_image_p;
  374. hab_rvt_entry = hab_rvt_entry_p;
  375. hab_rvt_exit = hab_rvt_exit_p;
  376. if (is_hab_enabled()) {
  377. printf("\nAuthenticate image from DDR location 0x%x...\n",
  378. ddr_start);
  379. hab_caam_clock_enable(1);
  380. if (hab_rvt_entry() == HAB_SUCCESS) {
  381. /* If not already aligned, Align to ALIGN_SIZE */
  382. ivt_offset = (image_size + ALIGN_SIZE - 1) &
  383. ~(ALIGN_SIZE - 1);
  384. start = ddr_start;
  385. bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE;
  386. #ifdef DEBUG
  387. printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n",
  388. ivt_offset, ddr_start + ivt_offset);
  389. puts("Dumping IVT\n");
  390. print_buffer(ddr_start + ivt_offset,
  391. (void *)(ddr_start + ivt_offset),
  392. 4, 0x8, 0);
  393. puts("Dumping CSF Header\n");
  394. print_buffer(ddr_start + ivt_offset+IVT_SIZE,
  395. (void *)(ddr_start + ivt_offset+IVT_SIZE),
  396. 4, 0x10, 0);
  397. #if !defined(CONFIG_SPL_BUILD)
  398. get_hab_status();
  399. #endif
  400. puts("\nCalling authenticate_image in ROM\n");
  401. printf("\tivt_offset = 0x%x\n", ivt_offset);
  402. printf("\tstart = 0x%08lx\n", start);
  403. printf("\tbytes = 0x%x\n", bytes);
  404. #endif
  405. /*
  406. * If the MMU is enabled, we have to notify the ROM
  407. * code, or it won't flush the caches when needed.
  408. * This is done, by setting the "pu_irom_mmu_enabled"
  409. * word to 1. You can find its address by looking in
  410. * the ROM map. This is critical for
  411. * authenticate_image(). If MMU is enabled, without
  412. * setting this bit, authentication will fail and may
  413. * crash.
  414. */
  415. /* Check MMU enabled */
  416. if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
  417. if (is_mx6dq()) {
  418. /*
  419. * This won't work on Rev 1.0.0 of
  420. * i.MX6Q/D, since their ROM doesn't
  421. * do cache flushes. don't think any
  422. * exist, so we ignore them.
  423. */
  424. if (!is_mx6dqp())
  425. writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
  426. } else if (is_mx6sdl()) {
  427. writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
  428. } else if (is_mx6sl()) {
  429. writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
  430. }
  431. }
  432. load_addr = (uint32_t)hab_rvt_authenticate_image(
  433. HAB_CID_UBOOT,
  434. ivt_offset, (void **)&start,
  435. (size_t *)&bytes, NULL);
  436. if (hab_rvt_exit() != HAB_SUCCESS) {
  437. puts("hab exit function fail\n");
  438. load_addr = 0;
  439. }
  440. } else {
  441. puts("hab entry function fail\n");
  442. }
  443. hab_caam_clock_enable(0);
  444. #if !defined(CONFIG_SPL_BUILD)
  445. get_hab_status();
  446. #endif
  447. } else {
  448. puts("hab fuse not enabled\n");
  449. }
  450. if ((!is_hab_enabled()) || (load_addr != 0))
  451. result = 1;
  452. return result;
  453. }