1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
10 #include <asm/system.h>
11 #include <asm/arch/clock.h>
12 #include <asm/arch/sys_proto.h>
13 #include <asm/mach-imx/hab.h>
15 #define ALIGN_SIZE 0x1000
16 #define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
17 #define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
18 #define MX6SL_PU_IROM_MMU_EN_VAR 0x00900a18
19 #define IS_HAB_ENABLED_BIT \
20 (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
21 (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2))
23 static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr)
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);
31 static int verify_ivt_header(struct ivt_header *ivt_hdr)
35 if (ivt_hdr->magic != IVT_HEADER_MAGIC)
36 result = ivt_header_error("bad magic", ivt_hdr);
38 if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH)
39 result = ivt_header_error("bad length", ivt_hdr);
41 if (ivt_hdr->version != IVT_HEADER_V1 &&
42 ivt_hdr->version != IVT_HEADER_V2)
43 result = ivt_header_error("bad version", ivt_hdr);
48 #if !defined(CONFIG_SPL_BUILD)
50 #define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
53 uint8_t tag; /* Tag */
54 uint8_t len[2]; /* Length */
55 uint8_t par; /* Version */
56 uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
60 static char *rsn_str[] = {
61 "RSN = HAB_RSN_ANY (0x00)\n",
62 "RSN = HAB_ENG_FAIL (0x30)\n",
63 "RSN = HAB_INV_ADDRESS (0x22)\n",
64 "RSN = HAB_INV_ASSERTION (0x0C)\n",
65 "RSN = HAB_INV_CALL (0x28)\n",
66 "RSN = HAB_INV_CERTIFICATE (0x21)\n",
67 "RSN = HAB_INV_COMMAND (0x06)\n",
68 "RSN = HAB_INV_CSF (0x11)\n",
69 "RSN = HAB_INV_DCD (0x27)\n",
70 "RSN = HAB_INV_INDEX (0x0F)\n",
71 "RSN = HAB_INV_IVT (0x05)\n",
72 "RSN = HAB_INV_KEY (0x1D)\n",
73 "RSN = HAB_INV_RETURN (0x1E)\n",
74 "RSN = HAB_INV_SIGNATURE (0x18)\n",
75 "RSN = HAB_INV_SIZE (0x17)\n",
76 "RSN = HAB_MEM_FAIL (0x2E)\n",
77 "RSN = HAB_OVR_COUNT (0x2B)\n",
78 "RSN = HAB_OVR_STORAGE (0x2D)\n",
79 "RSN = HAB_UNS_ALGORITHM (0x12)\n",
80 "RSN = HAB_UNS_COMMAND (0x03)\n",
81 "RSN = HAB_UNS_ENGINE (0x0A)\n",
82 "RSN = HAB_UNS_ITEM (0x24)\n",
83 "RSN = HAB_UNS_KEY (0x1B)\n",
84 "RSN = HAB_UNS_PROTOCOL (0x14)\n",
85 "RSN = HAB_UNS_STATE (0x09)\n",
90 static char *sts_str[] = {
91 "STS = HAB_SUCCESS (0xF0)\n",
92 "STS = HAB_FAILURE (0x33)\n",
93 "STS = HAB_WARNING (0x69)\n",
98 static char *eng_str[] = {
99 "ENG = HAB_ENG_ANY (0x00)\n",
100 "ENG = HAB_ENG_SCC (0x03)\n",
101 "ENG = HAB_ENG_RTIC (0x05)\n",
102 "ENG = HAB_ENG_SAHARA (0x06)\n",
103 "ENG = HAB_ENG_CSU (0x0A)\n",
104 "ENG = HAB_ENG_SRTC (0x0C)\n",
105 "ENG = HAB_ENG_DCP (0x1B)\n",
106 "ENG = HAB_ENG_CAAM (0x1D)\n",
107 "ENG = HAB_ENG_SNVS (0x1E)\n",
108 "ENG = HAB_ENG_OCOTP (0x21)\n",
109 "ENG = HAB_ENG_DTCP (0x22)\n",
110 "ENG = HAB_ENG_ROM (0x36)\n",
111 "ENG = HAB_ENG_HDCP (0x24)\n",
112 "ENG = HAB_ENG_RTL (0x77)\n",
113 "ENG = HAB_ENG_SW (0xFF)\n",
118 static char *ctx_str[] = {
119 "CTX = HAB_CTX_ANY(0x00)\n",
120 "CTX = HAB_CTX_FAB (0xFF)\n",
121 "CTX = HAB_CTX_ENTRY (0xE1)\n",
122 "CTX = HAB_CTX_TARGET (0x33)\n",
123 "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
124 "CTX = HAB_CTX_DCD (0xDD)\n",
125 "CTX = HAB_CTX_CSF (0xCF)\n",
126 "CTX = HAB_CTX_COMMAND (0xC0)\n",
127 "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
128 "CTX = HAB_CTX_ASSERT (0xA0)\n",
129 "CTX = HAB_CTX_EXIT (0xEE)\n",
134 static uint8_t hab_statuses[5] = {
142 static uint8_t hab_reasons[26] = {
171 static uint8_t hab_contexts[12] = {
176 HAB_CTX_AUTHENTICATE,
186 static uint8_t hab_engines[16] = {
205 static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
208 uint8_t element = list[idx];
209 while (element != -1) {
212 element = list[++idx];
217 static void process_event_record(uint8_t *event_data, size_t bytes)
219 struct record *rec = (struct record *)event_data;
221 printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
222 printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
223 printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
224 printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
227 static void display_event(uint8_t *event_data, size_t bytes)
231 if (!(event_data && bytes > 0))
234 for (i = 0; i < bytes; i++) {
236 printf("\t0x%02x", event_data[i]);
237 else if ((i % 8) == 0)
238 printf("\n\t0x%02x", event_data[i]);
240 printf(" 0x%02x", event_data[i]);
243 process_event_record(event_data, bytes);
246 static int get_hab_status(void)
248 uint32_t index = 0; /* Loop index */
249 uint8_t event_data[128]; /* Event data buffer */
250 size_t bytes = sizeof(event_data); /* Event size in bytes */
251 enum hab_config config = 0;
252 enum hab_state state = 0;
253 hab_rvt_report_event_t *hab_rvt_report_event;
254 hab_rvt_report_status_t *hab_rvt_report_status;
256 hab_rvt_report_event = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT;
257 hab_rvt_report_status =
258 (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS;
260 if (imx_hab_is_enabled())
261 puts("\nSecure boot enabled\n");
263 puts("\nSecure boot disabled\n");
265 /* Check HAB status */
266 if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
267 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
270 /* Display HAB Error events */
271 while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
272 &bytes) == HAB_SUCCESS) {
274 printf("--------- HAB Event %d -----------------\n",
276 puts("event data:\n");
277 display_event(event_data, bytes);
279 bytes = sizeof(event_data);
283 /* Display message if no HAB events are found */
285 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
287 puts("No HAB Events Found!\n\n");
292 static int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc,
305 static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc,
308 ulong addr, length, ivt_offset;
312 return CMD_RET_USAGE;
314 addr = simple_strtoul(argv[1], NULL, 16);
315 length = simple_strtoul(argv[2], NULL, 16);
316 ivt_offset = simple_strtoul(argv[3], NULL, 16);
318 rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
320 rcode = CMD_RET_SUCCESS;
322 rcode = CMD_RET_FAILURE;
327 static int do_hab_failsafe(cmd_tbl_t *cmdtp, int flag, int argc,
330 hab_rvt_failsafe_t *hab_rvt_failsafe;
337 hab_rvt_failsafe = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE;
343 static int do_authenticate_image_or_failover(cmd_tbl_t *cmdtp, int flag,
344 int argc, char * const argv[])
346 int ret = CMD_RET_FAILURE;
353 if (!imx_hab_is_enabled()) {
354 printf("error: secure boot disabled\n");
358 if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) {
359 fprintf(stderr, "authentication fail -> %s %s %s %s\n",
360 argv[0], argv[1], argv[2], argv[3]);
361 do_hab_failsafe(0, 0, 1, NULL);
363 ret = CMD_RET_SUCCESS;
369 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
370 "display HAB status",
375 hab_auth_img, 4, 0, do_authenticate_image,
376 "authenticate image via HAB",
377 "addr length ivt_offset\n"
378 "addr - image hex address\n"
379 "length - image hex length\n"
380 "ivt_offset - hex offset of IVT in the image"
384 hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
385 "run BootROM failsafe routine",
390 hab_auth_img_or_fail, 4, 0,
391 do_authenticate_image_or_failover,
392 "authenticate image via HAB on failure drop to USB BootROM mode",
393 "addr length ivt_offset\n"
394 "addr - image hex address\n"
395 "length - image hex length\n"
396 "ivt_offset - hex offset of IVT in the image"
399 #endif /* !defined(CONFIG_SPL_BUILD) */
401 /* Get CSF Header length */
402 static int get_hab_hdr_len(struct hab_hdr *hdr)
404 return (size_t)((hdr->len[0] << 8) + (hdr->len[1]));
407 /* Check whether addr lies between start and
408 * end and is within the length of the image
410 static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end)
412 size_t csf_size = (size_t)((end + 1) - addr);
414 return (addr && (addr >= start) && (addr <= end) &&
415 (csf_size >= bytes));
418 /* Get Length of each command in CSF */
419 static int get_csf_cmd_hdr_len(u8 *csf_hdr)
421 if (*csf_hdr == HAB_CMD_HDR)
422 return sizeof(struct hab_hdr);
424 return get_hab_hdr_len((struct hab_hdr *)csf_hdr);
427 /* Check if CSF is valid */
428 static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes)
430 u8 *start = (u8 *)start_addr;
439 end = start + bytes - 1;
443 /* Verify if CSF pointer content is zero */
445 puts("Error: CSF pointer is NULL\n");
449 csf_hdr = (u8 *)ivt->csf;
451 /* Verify if CSF Header exist */
452 if (*csf_hdr != HAB_CMD_HDR) {
453 puts("Error: CSF header command not found\n");
457 csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr);
459 /* Check if the CSF lies within the image bounds */
460 if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) {
461 puts("Error: CSF lies outside the image bounds\n");
468 cmd = (struct hab_hdr *)&csf_hdr[offset];
471 case (HAB_CMD_WRT_DAT):
472 puts("Error: Deprecated write command found\n");
474 case (HAB_CMD_CHK_DAT):
475 puts("Error: Deprecated check command found\n");
478 if (cmd->par == HAB_PAR_MID) {
479 puts("Error: Deprecated Set MID command found\n");
486 cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]);
488 puts("Error: Invalid command length\n");
491 offset += cmd_hdr_len;
493 } while (offset < csf_hdr_len);
498 bool imx_hab_is_enabled(void)
500 struct imx_sec_config_fuse_t *fuse =
501 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
505 ret = fuse_read(fuse->bank, fuse->word, ®);
507 puts("\nSecure boot fuse read error\n");
511 return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
514 int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
517 uint32_t load_addr = 0;
519 uint32_t ivt_addr = 0;
522 hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
523 hab_rvt_entry_t *hab_rvt_entry;
524 hab_rvt_exit_t *hab_rvt_exit;
525 hab_rvt_check_target_t *hab_rvt_check_target;
527 struct ivt_header *ivt_hdr;
528 enum hab_status status;
530 hab_rvt_authenticate_image =
531 (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE;
532 hab_rvt_entry = (hab_rvt_entry_t *)HAB_RVT_ENTRY;
533 hab_rvt_exit = (hab_rvt_exit_t *)HAB_RVT_EXIT;
534 hab_rvt_check_target = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET;
536 if (!imx_hab_is_enabled()) {
537 puts("hab fuse not enabled\n");
541 printf("\nAuthenticate image from DDR location 0x%x...\n",
544 hab_caam_clock_enable(1);
546 /* Calculate IVT address header */
547 ivt_addr = ddr_start + ivt_offset;
548 ivt = (struct ivt *)ivt_addr;
551 /* Verify IVT header bugging out on error */
552 if (verify_ivt_header(ivt_hdr))
553 goto hab_authentication_exit;
555 /* Verify IVT body */
556 if (ivt->self != ivt_addr) {
557 printf("ivt->self 0x%08x pointer is 0x%08x\n",
558 ivt->self, ivt_addr);
559 goto hab_authentication_exit;
562 /* Verify if IVT DCD pointer is NULL */
564 puts("Warning: DCD pointer should be NULL\n");
570 if (!csf_is_valid(ivt, start, bytes))
571 goto hab_authentication_exit;
573 if (hab_rvt_entry() != HAB_SUCCESS) {
574 puts("hab entry function fail\n");
575 goto hab_exit_failure_print_status;
578 status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)ddr_start, bytes);
579 if (status != HAB_SUCCESS) {
580 printf("HAB check target 0x%08x-0x%08x fail\n",
581 ddr_start, ddr_start + bytes);
582 goto hab_exit_failure_print_status;
585 printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n", ivt_offset, ivt_addr);
586 printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
588 puts("Dumping IVT\n");
589 print_buffer(ivt_addr, (void *)(ivt_addr), 4, 0x8, 0);
591 puts("Dumping CSF Header\n");
592 print_buffer(ivt->csf, (void *)(ivt->csf), 4, 0x10, 0);
594 #if !defined(CONFIG_SPL_BUILD)
598 puts("\nCalling authenticate_image in ROM\n");
599 printf("\tivt_offset = 0x%x\n", ivt_offset);
600 printf("\tstart = 0x%08lx\n", start);
601 printf("\tbytes = 0x%x\n", bytes);
604 * If the MMU is enabled, we have to notify the ROM
605 * code, or it won't flush the caches when needed.
606 * This is done, by setting the "pu_irom_mmu_enabled"
607 * word to 1. You can find its address by looking in
608 * the ROM map. This is critical for
609 * authenticate_image(). If MMU is enabled, without
610 * setting this bit, authentication will fail and may
613 /* Check MMU enabled */
614 if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
617 * This won't work on Rev 1.0.0 of
618 * i.MX6Q/D, since their ROM doesn't
619 * do cache flushes. don't think any
620 * exist, so we ignore them.
623 writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
624 } else if (is_mx6sdl()) {
625 writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
626 } else if (is_mx6sl()) {
627 writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
631 load_addr = (uint32_t)hab_rvt_authenticate_image(
633 ivt_offset, (void **)&start,
634 (size_t *)&bytes, NULL);
635 if (hab_rvt_exit() != HAB_SUCCESS) {
636 puts("hab exit function fail\n");
640 hab_exit_failure_print_status:
641 #if !defined(CONFIG_SPL_BUILD)
645 hab_authentication_exit: