hab.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <config.h>
  8. #include <fuse.h>
  9. #include <mapmem.h>
  10. #include <image.h>
  11. #include <asm/io.h>
  12. #include <asm/system.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <asm/mach-imx/hab.h>
  16. #define ALIGN_SIZE 0x1000
  17. #define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
  18. #define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
  19. #define MX6SL_PU_IROM_MMU_EN_VAR 0x00901c60
  20. #define IS_HAB_ENABLED_BIT \
  21. (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
  22. (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2))
  23. static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr)
  24. {
  25. printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str,
  26. ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version);
  27. return 1;
  28. }
  29. static int verify_ivt_header(struct ivt_header *ivt_hdr)
  30. {
  31. int result = 0;
  32. if (ivt_hdr->magic != IVT_HEADER_MAGIC)
  33. result = ivt_header_error("bad magic", ivt_hdr);
  34. if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH)
  35. result = ivt_header_error("bad length", ivt_hdr);
  36. if (ivt_hdr->version != IVT_HEADER_V1 &&
  37. ivt_hdr->version != IVT_HEADER_V2)
  38. result = ivt_header_error("bad version", ivt_hdr);
  39. return result;
  40. }
  41. #if !defined(CONFIG_SPL_BUILD)
  42. #define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
  43. struct record {
  44. uint8_t tag; /* Tag */
  45. uint8_t len[2]; /* Length */
  46. uint8_t par; /* Version */
  47. uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
  48. bool any_rec_flag;
  49. };
  50. static char *rsn_str[] = {
  51. "RSN = HAB_RSN_ANY (0x00)\n",
  52. "RSN = HAB_ENG_FAIL (0x30)\n",
  53. "RSN = HAB_INV_ADDRESS (0x22)\n",
  54. "RSN = HAB_INV_ASSERTION (0x0C)\n",
  55. "RSN = HAB_INV_CALL (0x28)\n",
  56. "RSN = HAB_INV_CERTIFICATE (0x21)\n",
  57. "RSN = HAB_INV_COMMAND (0x06)\n",
  58. "RSN = HAB_INV_CSF (0x11)\n",
  59. "RSN = HAB_INV_DCD (0x27)\n",
  60. "RSN = HAB_INV_INDEX (0x0F)\n",
  61. "RSN = HAB_INV_IVT (0x05)\n",
  62. "RSN = HAB_INV_KEY (0x1D)\n",
  63. "RSN = HAB_INV_RETURN (0x1E)\n",
  64. "RSN = HAB_INV_SIGNATURE (0x18)\n",
  65. "RSN = HAB_INV_SIZE (0x17)\n",
  66. "RSN = HAB_MEM_FAIL (0x2E)\n",
  67. "RSN = HAB_OVR_COUNT (0x2B)\n",
  68. "RSN = HAB_OVR_STORAGE (0x2D)\n",
  69. "RSN = HAB_UNS_ALGORITHM (0x12)\n",
  70. "RSN = HAB_UNS_COMMAND (0x03)\n",
  71. "RSN = HAB_UNS_ENGINE (0x0A)\n",
  72. "RSN = HAB_UNS_ITEM (0x24)\n",
  73. "RSN = HAB_UNS_KEY (0x1B)\n",
  74. "RSN = HAB_UNS_PROTOCOL (0x14)\n",
  75. "RSN = HAB_UNS_STATE (0x09)\n",
  76. "RSN = INVALID\n",
  77. NULL
  78. };
  79. static char *sts_str[] = {
  80. "STS = HAB_SUCCESS (0xF0)\n",
  81. "STS = HAB_FAILURE (0x33)\n",
  82. "STS = HAB_WARNING (0x69)\n",
  83. "STS = INVALID\n",
  84. NULL
  85. };
  86. static char *eng_str[] = {
  87. "ENG = HAB_ENG_ANY (0x00)\n",
  88. "ENG = HAB_ENG_SCC (0x03)\n",
  89. "ENG = HAB_ENG_RTIC (0x05)\n",
  90. "ENG = HAB_ENG_SAHARA (0x06)\n",
  91. "ENG = HAB_ENG_CSU (0x0A)\n",
  92. "ENG = HAB_ENG_SRTC (0x0C)\n",
  93. "ENG = HAB_ENG_DCP (0x1B)\n",
  94. "ENG = HAB_ENG_CAAM (0x1D)\n",
  95. "ENG = HAB_ENG_SNVS (0x1E)\n",
  96. "ENG = HAB_ENG_OCOTP (0x21)\n",
  97. "ENG = HAB_ENG_DTCP (0x22)\n",
  98. "ENG = HAB_ENG_ROM (0x36)\n",
  99. "ENG = HAB_ENG_HDCP (0x24)\n",
  100. "ENG = HAB_ENG_RTL (0x77)\n",
  101. "ENG = HAB_ENG_SW (0xFF)\n",
  102. "ENG = INVALID\n",
  103. NULL
  104. };
  105. static char *ctx_str[] = {
  106. "CTX = HAB_CTX_ANY(0x00)\n",
  107. "CTX = HAB_CTX_FAB (0xFF)\n",
  108. "CTX = HAB_CTX_ENTRY (0xE1)\n",
  109. "CTX = HAB_CTX_TARGET (0x33)\n",
  110. "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
  111. "CTX = HAB_CTX_DCD (0xDD)\n",
  112. "CTX = HAB_CTX_CSF (0xCF)\n",
  113. "CTX = HAB_CTX_COMMAND (0xC0)\n",
  114. "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
  115. "CTX = HAB_CTX_ASSERT (0xA0)\n",
  116. "CTX = HAB_CTX_EXIT (0xEE)\n",
  117. "CTX = INVALID\n",
  118. NULL
  119. };
  120. static uint8_t hab_statuses[5] = {
  121. HAB_STS_ANY,
  122. HAB_FAILURE,
  123. HAB_WARNING,
  124. HAB_SUCCESS,
  125. -1
  126. };
  127. static uint8_t hab_reasons[26] = {
  128. HAB_RSN_ANY,
  129. HAB_ENG_FAIL,
  130. HAB_INV_ADDRESS,
  131. HAB_INV_ASSERTION,
  132. HAB_INV_CALL,
  133. HAB_INV_CERTIFICATE,
  134. HAB_INV_COMMAND,
  135. HAB_INV_CSF,
  136. HAB_INV_DCD,
  137. HAB_INV_INDEX,
  138. HAB_INV_IVT,
  139. HAB_INV_KEY,
  140. HAB_INV_RETURN,
  141. HAB_INV_SIGNATURE,
  142. HAB_INV_SIZE,
  143. HAB_MEM_FAIL,
  144. HAB_OVR_COUNT,
  145. HAB_OVR_STORAGE,
  146. HAB_UNS_ALGORITHM,
  147. HAB_UNS_COMMAND,
  148. HAB_UNS_ENGINE,
  149. HAB_UNS_ITEM,
  150. HAB_UNS_KEY,
  151. HAB_UNS_PROTOCOL,
  152. HAB_UNS_STATE,
  153. -1
  154. };
  155. static uint8_t hab_contexts[12] = {
  156. HAB_CTX_ANY,
  157. HAB_CTX_FAB,
  158. HAB_CTX_ENTRY,
  159. HAB_CTX_TARGET,
  160. HAB_CTX_AUTHENTICATE,
  161. HAB_CTX_DCD,
  162. HAB_CTX_CSF,
  163. HAB_CTX_COMMAND,
  164. HAB_CTX_AUT_DAT,
  165. HAB_CTX_ASSERT,
  166. HAB_CTX_EXIT,
  167. -1
  168. };
  169. static uint8_t hab_engines[16] = {
  170. HAB_ENG_ANY,
  171. HAB_ENG_SCC,
  172. HAB_ENG_RTIC,
  173. HAB_ENG_SAHARA,
  174. HAB_ENG_CSU,
  175. HAB_ENG_SRTC,
  176. HAB_ENG_DCP,
  177. HAB_ENG_CAAM,
  178. HAB_ENG_SNVS,
  179. HAB_ENG_OCOTP,
  180. HAB_ENG_DTCP,
  181. HAB_ENG_ROM,
  182. HAB_ENG_HDCP,
  183. HAB_ENG_RTL,
  184. HAB_ENG_SW,
  185. -1
  186. };
  187. static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
  188. {
  189. uint8_t idx = 0;
  190. uint8_t element = list[idx];
  191. while (element != -1) {
  192. if (element == tgt)
  193. return idx;
  194. element = list[++idx];
  195. }
  196. return -1;
  197. }
  198. static void process_event_record(uint8_t *event_data, size_t bytes)
  199. {
  200. struct record *rec = (struct record *)event_data;
  201. printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
  202. printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
  203. printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
  204. printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
  205. }
  206. static void display_event(uint8_t *event_data, size_t bytes)
  207. {
  208. uint32_t i;
  209. if (!(event_data && bytes > 0))
  210. return;
  211. for (i = 0; i < bytes; i++) {
  212. if (i == 0)
  213. printf("\t0x%02x", event_data[i]);
  214. else if ((i % 8) == 0)
  215. printf("\n\t0x%02x", event_data[i]);
  216. else
  217. printf(" 0x%02x", event_data[i]);
  218. }
  219. process_event_record(event_data, bytes);
  220. }
  221. static int get_hab_status(void)
  222. {
  223. uint32_t index = 0; /* Loop index */
  224. uint8_t event_data[128]; /* Event data buffer */
  225. size_t bytes = sizeof(event_data); /* Event size in bytes */
  226. enum hab_config config = 0;
  227. enum hab_state state = 0;
  228. hab_rvt_report_event_t *hab_rvt_report_event;
  229. hab_rvt_report_status_t *hab_rvt_report_status;
  230. hab_rvt_report_event = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT;
  231. hab_rvt_report_status =
  232. (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS;
  233. if (imx_hab_is_enabled())
  234. puts("\nSecure boot enabled\n");
  235. else
  236. puts("\nSecure boot disabled\n");
  237. /* Check HAB status */
  238. if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
  239. printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
  240. config, state);
  241. /* Display HAB Error events */
  242. while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
  243. &bytes) == HAB_SUCCESS) {
  244. puts("\n");
  245. printf("--------- HAB Event %d -----------------\n",
  246. index + 1);
  247. puts("event data:\n");
  248. display_event(event_data, bytes);
  249. puts("\n");
  250. bytes = sizeof(event_data);
  251. index++;
  252. }
  253. }
  254. /* Display message if no HAB events are found */
  255. else {
  256. printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
  257. config, state);
  258. puts("No HAB Events Found!\n\n");
  259. }
  260. return 0;
  261. }
  262. static int do_hab_status(struct cmd_tbl *cmdtp, int flag, int argc,
  263. char *const argv[])
  264. {
  265. if ((argc != 1)) {
  266. cmd_usage(cmdtp);
  267. return 1;
  268. }
  269. get_hab_status();
  270. return 0;
  271. }
  272. static ulong get_image_ivt_offset(ulong img_addr)
  273. {
  274. const void *buf;
  275. buf = map_sysmem(img_addr, 0);
  276. switch (genimg_get_format(buf)) {
  277. #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
  278. case IMAGE_FORMAT_LEGACY:
  279. return (image_get_image_size((image_header_t *)img_addr)
  280. + 0x1000 - 1) & ~(0x1000 - 1);
  281. #endif
  282. #if IMAGE_ENABLE_FIT
  283. case IMAGE_FORMAT_FIT:
  284. return (fit_get_size(buf) + 0x1000 - 1) & ~(0x1000 - 1);
  285. #endif
  286. default:
  287. return 0;
  288. }
  289. }
  290. static int do_authenticate_image(struct cmd_tbl *cmdtp, int flag, int argc,
  291. char *const argv[])
  292. {
  293. ulong addr, length, ivt_offset;
  294. int rcode = 0;
  295. if (argc < 3)
  296. return CMD_RET_USAGE;
  297. addr = simple_strtoul(argv[1], NULL, 16);
  298. length = simple_strtoul(argv[2], NULL, 16);
  299. if (argc == 3)
  300. ivt_offset = get_image_ivt_offset(addr);
  301. else
  302. ivt_offset = simple_strtoul(argv[3], NULL, 16);
  303. rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
  304. if (rcode == 0)
  305. rcode = CMD_RET_SUCCESS;
  306. else
  307. rcode = CMD_RET_FAILURE;
  308. return rcode;
  309. }
  310. static int do_hab_failsafe(struct cmd_tbl *cmdtp, int flag, int argc,
  311. char *const argv[])
  312. {
  313. hab_rvt_failsafe_t *hab_rvt_failsafe;
  314. if (argc != 1) {
  315. cmd_usage(cmdtp);
  316. return 1;
  317. }
  318. hab_rvt_failsafe = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE;
  319. hab_rvt_failsafe();
  320. return 0;
  321. }
  322. static int do_hab_version(struct cmd_tbl *cmdtp, int flag, int argc,
  323. char *const argv[])
  324. {
  325. struct hab_hdr *hdr = (struct hab_hdr *)HAB_RVT_BASE;
  326. if (hdr->tag != HAB_TAG_RVT) {
  327. printf("Unexpected header tag: %x\n", hdr->tag);
  328. return CMD_RET_FAILURE;
  329. }
  330. printf("HAB version: %d.%d\n", hdr->par >> 4, hdr->par & 0xf);
  331. return 0;
  332. }
  333. static int do_authenticate_image_or_failover(struct cmd_tbl *cmdtp, int flag,
  334. int argc, char *const argv[])
  335. {
  336. int ret = CMD_RET_FAILURE;
  337. if (argc != 4) {
  338. ret = CMD_RET_USAGE;
  339. goto error;
  340. }
  341. if (!imx_hab_is_enabled()) {
  342. printf("error: secure boot disabled\n");
  343. goto error;
  344. }
  345. if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) {
  346. fprintf(stderr, "authentication fail -> %s %s %s %s\n",
  347. argv[0], argv[1], argv[2], argv[3]);
  348. do_hab_failsafe(0, 0, 1, NULL);
  349. };
  350. ret = CMD_RET_SUCCESS;
  351. error:
  352. return ret;
  353. }
  354. U_BOOT_CMD(
  355. hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
  356. "display HAB status",
  357. ""
  358. );
  359. U_BOOT_CMD(
  360. hab_auth_img, 4, 0, do_authenticate_image,
  361. "authenticate image via HAB",
  362. "addr length ivt_offset\n"
  363. "addr - image hex address\n"
  364. "length - image hex length\n"
  365. "ivt_offset - hex offset of IVT in the image"
  366. );
  367. U_BOOT_CMD(
  368. hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
  369. "run BootROM failsafe routine",
  370. ""
  371. );
  372. U_BOOT_CMD(
  373. hab_auth_img_or_fail, 4, 0,
  374. do_authenticate_image_or_failover,
  375. "authenticate image via HAB on failure drop to USB BootROM mode",
  376. "addr length ivt_offset\n"
  377. "addr - image hex address\n"
  378. "length - image hex length\n"
  379. "ivt_offset - hex offset of IVT in the image"
  380. );
  381. U_BOOT_CMD(
  382. hab_version, 1, 0, do_hab_version,
  383. "print HAB major/minor version",
  384. ""
  385. );
  386. #endif /* !defined(CONFIG_SPL_BUILD) */
  387. /* Get CSF Header length */
  388. static int get_hab_hdr_len(struct hab_hdr *hdr)
  389. {
  390. return (size_t)((hdr->len[0] << 8) + (hdr->len[1]));
  391. }
  392. /* Check whether addr lies between start and
  393. * end and is within the length of the image
  394. */
  395. static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end)
  396. {
  397. size_t csf_size = (size_t)((end + 1) - addr);
  398. return (addr && (addr >= start) && (addr <= end) &&
  399. (csf_size >= bytes));
  400. }
  401. /* Get Length of each command in CSF */
  402. static int get_csf_cmd_hdr_len(u8 *csf_hdr)
  403. {
  404. if (*csf_hdr == HAB_CMD_HDR)
  405. return sizeof(struct hab_hdr);
  406. return get_hab_hdr_len((struct hab_hdr *)csf_hdr);
  407. }
  408. /* Check if CSF is valid */
  409. static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes)
  410. {
  411. u8 *start = (u8 *)start_addr;
  412. u8 *csf_hdr;
  413. u8 *end;
  414. size_t csf_hdr_len;
  415. size_t cmd_hdr_len;
  416. size_t offset = 0;
  417. if (bytes != 0)
  418. end = start + bytes - 1;
  419. else
  420. end = start;
  421. /* Verify if CSF pointer content is zero */
  422. if (!ivt->csf) {
  423. puts("Error: CSF pointer is NULL\n");
  424. return false;
  425. }
  426. csf_hdr = (u8 *)ivt->csf;
  427. /* Verify if CSF Header exist */
  428. if (*csf_hdr != HAB_CMD_HDR) {
  429. puts("Error: CSF header command not found\n");
  430. return false;
  431. }
  432. csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr);
  433. /* Check if the CSF lies within the image bounds */
  434. if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) {
  435. puts("Error: CSF lies outside the image bounds\n");
  436. return false;
  437. }
  438. do {
  439. struct hab_hdr *cmd;
  440. cmd = (struct hab_hdr *)&csf_hdr[offset];
  441. switch (cmd->tag) {
  442. case (HAB_CMD_WRT_DAT):
  443. puts("Error: Deprecated write command found\n");
  444. return false;
  445. case (HAB_CMD_CHK_DAT):
  446. puts("Error: Deprecated check command found\n");
  447. return false;
  448. case (HAB_CMD_SET):
  449. if (cmd->par == HAB_PAR_MID) {
  450. puts("Error: Deprecated Set MID command found\n");
  451. return false;
  452. }
  453. default:
  454. break;
  455. }
  456. cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]);
  457. if (!cmd_hdr_len) {
  458. puts("Error: Invalid command length\n");
  459. return false;
  460. }
  461. offset += cmd_hdr_len;
  462. } while (offset < csf_hdr_len);
  463. return true;
  464. }
  465. bool imx_hab_is_enabled(void)
  466. {
  467. struct imx_sec_config_fuse_t *fuse =
  468. (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
  469. uint32_t reg;
  470. int ret;
  471. ret = fuse_read(fuse->bank, fuse->word, &reg);
  472. if (ret) {
  473. puts("\nSecure boot fuse read error\n");
  474. return ret;
  475. }
  476. return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
  477. }
  478. int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
  479. uint32_t ivt_offset)
  480. {
  481. uint32_t load_addr = 0;
  482. size_t bytes;
  483. uint32_t ivt_addr = 0;
  484. int result = 1;
  485. ulong start;
  486. hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
  487. hab_rvt_entry_t *hab_rvt_entry;
  488. hab_rvt_exit_t *hab_rvt_exit;
  489. hab_rvt_check_target_t *hab_rvt_check_target;
  490. struct ivt *ivt;
  491. struct ivt_header *ivt_hdr;
  492. enum hab_status status;
  493. hab_rvt_authenticate_image =
  494. (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE;
  495. hab_rvt_entry = (hab_rvt_entry_t *)HAB_RVT_ENTRY;
  496. hab_rvt_exit = (hab_rvt_exit_t *)HAB_RVT_EXIT;
  497. hab_rvt_check_target = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET;
  498. if (!imx_hab_is_enabled()) {
  499. puts("hab fuse not enabled\n");
  500. return 0;
  501. }
  502. printf("\nAuthenticate image from DDR location 0x%x...\n",
  503. ddr_start);
  504. hab_caam_clock_enable(1);
  505. /* Calculate IVT address header */
  506. ivt_addr = ddr_start + ivt_offset;
  507. ivt = (struct ivt *)ivt_addr;
  508. ivt_hdr = &ivt->hdr;
  509. /* Verify IVT header bugging out on error */
  510. if (verify_ivt_header(ivt_hdr))
  511. goto hab_authentication_exit;
  512. /* Verify IVT body */
  513. if (ivt->self != ivt_addr) {
  514. printf("ivt->self 0x%08x pointer is 0x%08x\n",
  515. ivt->self, ivt_addr);
  516. goto hab_authentication_exit;
  517. }
  518. /* Verify if IVT DCD pointer is NULL */
  519. if (ivt->dcd) {
  520. puts("Error: DCD pointer must be NULL\n");
  521. goto hab_authentication_exit;
  522. }
  523. start = ddr_start;
  524. bytes = image_size;
  525. /* Verify CSF */
  526. if (!csf_is_valid(ivt, start, bytes))
  527. goto hab_authentication_exit;
  528. if (hab_rvt_entry() != HAB_SUCCESS) {
  529. puts("hab entry function fail\n");
  530. goto hab_exit_failure_print_status;
  531. }
  532. status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)ddr_start, bytes);
  533. if (status != HAB_SUCCESS) {
  534. printf("HAB check target 0x%08x-0x%08x fail\n",
  535. ddr_start, ddr_start + bytes);
  536. goto hab_exit_failure_print_status;
  537. }
  538. #ifdef DEBUG
  539. printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n", ivt_offset, ivt_addr);
  540. printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
  541. ivt->dcd, ivt->csf);
  542. puts("Dumping IVT\n");
  543. print_buffer(ivt_addr, (void *)(ivt_addr), 4, 0x8, 0);
  544. puts("Dumping CSF Header\n");
  545. print_buffer(ivt->csf, (void *)(ivt->csf), 4, 0x10, 0);
  546. #if !defined(CONFIG_SPL_BUILD)
  547. get_hab_status();
  548. #endif
  549. puts("\nCalling authenticate_image in ROM\n");
  550. printf("\tivt_offset = 0x%x\n", ivt_offset);
  551. printf("\tstart = 0x%08lx\n", start);
  552. printf("\tbytes = 0x%x\n", bytes);
  553. #endif
  554. /*
  555. * If the MMU is enabled, we have to notify the ROM
  556. * code, or it won't flush the caches when needed.
  557. * This is done, by setting the "pu_irom_mmu_enabled"
  558. * word to 1. You can find its address by looking in
  559. * the ROM map. This is critical for
  560. * authenticate_image(). If MMU is enabled, without
  561. * setting this bit, authentication will fail and may
  562. * crash.
  563. */
  564. /* Check MMU enabled */
  565. if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
  566. if (is_mx6dq()) {
  567. /*
  568. * This won't work on Rev 1.0.0 of
  569. * i.MX6Q/D, since their ROM doesn't
  570. * do cache flushes. don't think any
  571. * exist, so we ignore them.
  572. */
  573. if (!is_mx6dqp())
  574. writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
  575. } else if (is_mx6sdl()) {
  576. writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
  577. } else if (is_mx6sl()) {
  578. writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
  579. }
  580. }
  581. load_addr = (uint32_t)hab_rvt_authenticate_image(
  582. HAB_CID_UBOOT,
  583. ivt_offset, (void **)&start,
  584. (size_t *)&bytes, NULL);
  585. if (hab_rvt_exit() != HAB_SUCCESS) {
  586. puts("hab exit function fail\n");
  587. load_addr = 0;
  588. }
  589. hab_exit_failure_print_status:
  590. #if !defined(CONFIG_SPL_BUILD)
  591. get_hab_status();
  592. #endif
  593. hab_authentication_exit:
  594. if (load_addr != 0)
  595. result = 0;
  596. return result;
  597. }