blob: 9ca7badcaf1a13e547418963dc41e64911cf3023 [file] [log] [blame]
Stefano Babic83fd8582013-06-28 00:20:21 +02001/*
Ulises Cardenas8df2acd2015-07-02 21:26:30 -05002 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
Stefano Babic83fd8582013-06-28 00:20:21 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -05008#include <config.h>
9#include <fuse.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020010#include <asm/io.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050011#include <asm/system.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050012#include <asm/arch/clock.h>
Stefano Babic14404422014-06-10 10:26:22 +020013#include <asm/arch/sys_proto.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020014#include <asm/mach-imx/hab.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020015
Nitin Gargb1ce7012014-09-16 13:33:25 -050016#define ALIGN_SIZE 0x1000
Nitin Gargb1ce7012014-09-16 13:33:25 -050017#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 0x00900a18
Adrian Alonso00a0b562015-10-12 13:48:15 -050020#define IS_HAB_ENABLED_BIT \
Peng Fana26ba6d2017-02-22 16:21:53 +080021 (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
22 (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2))
Nitin Gargb1ce7012014-09-16 13:33:25 -050023
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +000024static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr)
25{
26 printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str,
27 ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version);
28
29 return 1;
30}
31
32static int verify_ivt_header(struct ivt_header *ivt_hdr)
33{
34 int result = 0;
35
36 if (ivt_hdr->magic != IVT_HEADER_MAGIC)
37 result = ivt_header_error("bad magic", ivt_hdr);
38
39 if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH)
40 result = ivt_header_error("bad length", ivt_hdr);
41
42 if (ivt_hdr->version != IVT_HEADER_V1 &&
43 ivt_hdr->version != IVT_HEADER_V2)
44 result = ivt_header_error("bad version", ivt_hdr);
45
46 return result;
47}
48
Sven Ebenfeldeba5e332016-11-06 16:37:55 +010049#if !defined(CONFIG_SPL_BUILD)
50
Ulises Cardenas8df2acd2015-07-02 21:26:30 -050051#define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
52
53struct record {
54 uint8_t tag; /* Tag */
55 uint8_t len[2]; /* Length */
56 uint8_t par; /* Version */
57 uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
58 bool any_rec_flag;
59};
60
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +000061static char *rsn_str[] = {
62 "RSN = HAB_RSN_ANY (0x00)\n",
63 "RSN = HAB_ENG_FAIL (0x30)\n",
64 "RSN = HAB_INV_ADDRESS (0x22)\n",
65 "RSN = HAB_INV_ASSERTION (0x0C)\n",
66 "RSN = HAB_INV_CALL (0x28)\n",
67 "RSN = HAB_INV_CERTIFICATE (0x21)\n",
68 "RSN = HAB_INV_COMMAND (0x06)\n",
69 "RSN = HAB_INV_CSF (0x11)\n",
70 "RSN = HAB_INV_DCD (0x27)\n",
71 "RSN = HAB_INV_INDEX (0x0F)\n",
72 "RSN = HAB_INV_IVT (0x05)\n",
73 "RSN = HAB_INV_KEY (0x1D)\n",
74 "RSN = HAB_INV_RETURN (0x1E)\n",
75 "RSN = HAB_INV_SIGNATURE (0x18)\n",
76 "RSN = HAB_INV_SIZE (0x17)\n",
77 "RSN = HAB_MEM_FAIL (0x2E)\n",
78 "RSN = HAB_OVR_COUNT (0x2B)\n",
79 "RSN = HAB_OVR_STORAGE (0x2D)\n",
80 "RSN = HAB_UNS_ALGORITHM (0x12)\n",
81 "RSN = HAB_UNS_COMMAND (0x03)\n",
82 "RSN = HAB_UNS_ENGINE (0x0A)\n",
83 "RSN = HAB_UNS_ITEM (0x24)\n",
84 "RSN = HAB_UNS_KEY (0x1B)\n",
85 "RSN = HAB_UNS_PROTOCOL (0x14)\n",
86 "RSN = HAB_UNS_STATE (0x09)\n",
87 "RSN = INVALID\n",
88 NULL
89};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -050090
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +000091static char *sts_str[] = {
92 "STS = HAB_SUCCESS (0xF0)\n",
93 "STS = HAB_FAILURE (0x33)\n",
94 "STS = HAB_WARNING (0x69)\n",
95 "STS = INVALID\n",
96 NULL
97};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -050098
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +000099static char *eng_str[] = {
100 "ENG = HAB_ENG_ANY (0x00)\n",
101 "ENG = HAB_ENG_SCC (0x03)\n",
102 "ENG = HAB_ENG_RTIC (0x05)\n",
103 "ENG = HAB_ENG_SAHARA (0x06)\n",
104 "ENG = HAB_ENG_CSU (0x0A)\n",
105 "ENG = HAB_ENG_SRTC (0x0C)\n",
106 "ENG = HAB_ENG_DCP (0x1B)\n",
107 "ENG = HAB_ENG_CAAM (0x1D)\n",
108 "ENG = HAB_ENG_SNVS (0x1E)\n",
109 "ENG = HAB_ENG_OCOTP (0x21)\n",
110 "ENG = HAB_ENG_DTCP (0x22)\n",
111 "ENG = HAB_ENG_ROM (0x36)\n",
112 "ENG = HAB_ENG_HDCP (0x24)\n",
113 "ENG = HAB_ENG_RTL (0x77)\n",
114 "ENG = HAB_ENG_SW (0xFF)\n",
115 "ENG = INVALID\n",
116 NULL
117};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500118
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000119static char *ctx_str[] = {
120 "CTX = HAB_CTX_ANY(0x00)\n",
121 "CTX = HAB_CTX_FAB (0xFF)\n",
122 "CTX = HAB_CTX_ENTRY (0xE1)\n",
123 "CTX = HAB_CTX_TARGET (0x33)\n",
124 "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
125 "CTX = HAB_CTX_DCD (0xDD)\n",
126 "CTX = HAB_CTX_CSF (0xCF)\n",
127 "CTX = HAB_CTX_COMMAND (0xC0)\n",
128 "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
129 "CTX = HAB_CTX_ASSERT (0xA0)\n",
130 "CTX = HAB_CTX_EXIT (0xEE)\n",
131 "CTX = INVALID\n",
132 NULL
133};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500134
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000135static uint8_t hab_statuses[5] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500136 HAB_STS_ANY,
137 HAB_FAILURE,
138 HAB_WARNING,
139 HAB_SUCCESS,
140 -1
141};
142
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000143static uint8_t hab_reasons[26] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500144 HAB_RSN_ANY,
145 HAB_ENG_FAIL,
146 HAB_INV_ADDRESS,
147 HAB_INV_ASSERTION,
148 HAB_INV_CALL,
149 HAB_INV_CERTIFICATE,
150 HAB_INV_COMMAND,
151 HAB_INV_CSF,
152 HAB_INV_DCD,
153 HAB_INV_INDEX,
154 HAB_INV_IVT,
155 HAB_INV_KEY,
156 HAB_INV_RETURN,
157 HAB_INV_SIGNATURE,
158 HAB_INV_SIZE,
159 HAB_MEM_FAIL,
160 HAB_OVR_COUNT,
161 HAB_OVR_STORAGE,
162 HAB_UNS_ALGORITHM,
163 HAB_UNS_COMMAND,
164 HAB_UNS_ENGINE,
165 HAB_UNS_ITEM,
166 HAB_UNS_KEY,
167 HAB_UNS_PROTOCOL,
168 HAB_UNS_STATE,
169 -1
170};
171
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000172static uint8_t hab_contexts[12] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500173 HAB_CTX_ANY,
174 HAB_CTX_FAB,
175 HAB_CTX_ENTRY,
176 HAB_CTX_TARGET,
177 HAB_CTX_AUTHENTICATE,
178 HAB_CTX_DCD,
179 HAB_CTX_CSF,
180 HAB_CTX_COMMAND,
181 HAB_CTX_AUT_DAT,
182 HAB_CTX_ASSERT,
183 HAB_CTX_EXIT,
184 -1
185};
186
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000187static uint8_t hab_engines[16] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500188 HAB_ENG_ANY,
189 HAB_ENG_SCC,
190 HAB_ENG_RTIC,
191 HAB_ENG_SAHARA,
192 HAB_ENG_CSU,
193 HAB_ENG_SRTC,
194 HAB_ENG_DCP,
195 HAB_ENG_CAAM,
196 HAB_ENG_SNVS,
197 HAB_ENG_OCOTP,
198 HAB_ENG_DTCP,
199 HAB_ENG_ROM,
200 HAB_ENG_HDCP,
201 HAB_ENG_RTL,
202 HAB_ENG_SW,
203 -1
204};
205
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500206static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
207{
208 uint8_t idx = 0;
209 uint8_t element = list[idx];
210 while (element != -1) {
211 if (element == tgt)
212 return idx;
213 element = list[++idx];
214 }
215 return -1;
216}
217
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000218static void process_event_record(uint8_t *event_data, size_t bytes)
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500219{
220 struct record *rec = (struct record *)event_data;
221
222 printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
223 printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
224 printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
225 printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
226}
227
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000228static void display_event(uint8_t *event_data, size_t bytes)
Stefano Babic83fd8582013-06-28 00:20:21 +0200229{
230 uint32_t i;
231
232 if (!(event_data && bytes > 0))
233 return;
234
235 for (i = 0; i < bytes; i++) {
236 if (i == 0)
237 printf("\t0x%02x", event_data[i]);
238 else if ((i % 8) == 0)
239 printf("\n\t0x%02x", event_data[i]);
240 else
241 printf(" 0x%02x", event_data[i]);
242 }
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500243
244 process_event_record(event_data, bytes);
Stefano Babic83fd8582013-06-28 00:20:21 +0200245}
246
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000247static int get_hab_status(void)
Stefano Babic83fd8582013-06-28 00:20:21 +0200248{
249 uint32_t index = 0; /* Loop index */
250 uint8_t event_data[128]; /* Event data buffer */
251 size_t bytes = sizeof(event_data); /* Event size in bytes */
252 enum hab_config config = 0;
253 enum hab_state state = 0;
Stefano Babic14404422014-06-10 10:26:22 +0200254 hab_rvt_report_event_t *hab_rvt_report_event;
255 hab_rvt_report_status_t *hab_rvt_report_status;
256
Breno Limaf4a72242018-02-20 01:19:26 +0000257 hab_rvt_report_event = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT;
258 hab_rvt_report_status =
259 (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS;
Stefano Babic83fd8582013-06-28 00:20:21 +0200260
Bryan O'Donoghue899cab02018-01-12 12:40:14 +0000261 if (imx_hab_is_enabled())
Stefano Babic83fd8582013-06-28 00:20:21 +0200262 puts("\nSecure boot enabled\n");
263 else
264 puts("\nSecure boot disabled\n");
265
266 /* Check HAB status */
267 if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
268 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
269 config, state);
270
271 /* Display HAB Error events */
272 while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
273 &bytes) == HAB_SUCCESS) {
274 puts("\n");
275 printf("--------- HAB Event %d -----------------\n",
276 index + 1);
277 puts("event data:\n");
278 display_event(event_data, bytes);
279 puts("\n");
280 bytes = sizeof(event_data);
281 index++;
282 }
283 }
284 /* Display message if no HAB events are found */
285 else {
286 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
287 config, state);
288 puts("No HAB Events Found!\n\n");
289 }
290 return 0;
291}
292
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000293static int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc,
294 char * const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100295{
296 if ((argc != 1)) {
297 cmd_usage(cmdtp);
298 return 1;
299 }
300
301 get_hab_status();
302
303 return 0;
304}
305
306static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc,
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000307 char * const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100308{
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000309 ulong addr, length, ivt_offset;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100310 int rcode = 0;
311
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000312 if (argc < 4)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100313 return CMD_RET_USAGE;
314
315 addr = simple_strtoul(argv[1], NULL, 16);
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000316 length = simple_strtoul(argv[2], NULL, 16);
317 ivt_offset = simple_strtoul(argv[3], NULL, 16);
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100318
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000319 rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000320 if (rcode == 0)
321 rcode = CMD_RET_SUCCESS;
322 else
323 rcode = CMD_RET_FAILURE;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000324
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100325 return rcode;
326}
327
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000328static int do_hab_failsafe(cmd_tbl_t *cmdtp, int flag, int argc,
329 char * const argv[])
330{
331 hab_rvt_failsafe_t *hab_rvt_failsafe;
332
333 if (argc != 1) {
334 cmd_usage(cmdtp);
335 return 1;
336 }
337
Breno Limaf4a72242018-02-20 01:19:26 +0000338 hab_rvt_failsafe = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE;
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000339 hab_rvt_failsafe();
340
341 return 0;
342}
343
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100344static int do_authenticate_image_or_failover(cmd_tbl_t *cmdtp, int flag,
345 int argc, char * const argv[])
346{
347 int ret = CMD_RET_FAILURE;
348
349 if (argc != 4) {
350 ret = CMD_RET_USAGE;
351 goto error;
352 }
353
354 if (!imx_hab_is_enabled()) {
355 printf("error: secure boot disabled\n");
356 goto error;
357 }
358
359 if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) {
360 fprintf(stderr, "authentication fail -> %s %s %s %s\n",
361 argv[0], argv[1], argv[2], argv[3]);
362 do_hab_failsafe(0, 0, 1, NULL);
363 };
364 ret = CMD_RET_SUCCESS;
365error:
366 return ret;
367}
368
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100369U_BOOT_CMD(
370 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
371 "display HAB status",
372 ""
373 );
374
375U_BOOT_CMD(
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000376 hab_auth_img, 4, 0, do_authenticate_image,
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100377 "authenticate image via HAB",
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000378 "addr length ivt_offset\n"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100379 "addr - image hex address\n"
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000380 "length - image hex length\n"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100381 "ivt_offset - hex offset of IVT in the image"
382 );
383
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000384U_BOOT_CMD(
385 hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
386 "run BootROM failsafe routine",
387 ""
388 );
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100389
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100390U_BOOT_CMD(
391 hab_auth_img_or_fail, 4, 0,
392 do_authenticate_image_or_failover,
393 "authenticate image via HAB on failure drop to USB BootROM mode",
394 "addr length ivt_offset\n"
395 "addr - image hex address\n"
396 "length - image hex length\n"
397 "ivt_offset - hex offset of IVT in the image"
398 );
399
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100400#endif /* !defined(CONFIG_SPL_BUILD) */
401
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000402/* Get CSF Header length */
403static int get_hab_hdr_len(struct hab_hdr *hdr)
404{
405 return (size_t)((hdr->len[0] << 8) + (hdr->len[1]));
406}
407
408/* Check whether addr lies between start and
409 * end and is within the length of the image
410 */
411static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end)
412{
413 size_t csf_size = (size_t)((end + 1) - addr);
414
415 return (addr && (addr >= start) && (addr <= end) &&
416 (csf_size >= bytes));
417}
418
419/* Get Length of each command in CSF */
420static int get_csf_cmd_hdr_len(u8 *csf_hdr)
421{
422 if (*csf_hdr == HAB_CMD_HDR)
423 return sizeof(struct hab_hdr);
424
425 return get_hab_hdr_len((struct hab_hdr *)csf_hdr);
426}
427
428/* Check if CSF is valid */
429static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes)
430{
431 u8 *start = (u8 *)start_addr;
432 u8 *csf_hdr;
433 u8 *end;
434
435 size_t csf_hdr_len;
436 size_t cmd_hdr_len;
437 size_t offset = 0;
438
439 if (bytes != 0)
440 end = start + bytes - 1;
441 else
442 end = start;
443
444 /* Verify if CSF pointer content is zero */
445 if (!ivt->csf) {
446 puts("Error: CSF pointer is NULL\n");
447 return false;
448 }
449
450 csf_hdr = (u8 *)ivt->csf;
451
452 /* Verify if CSF Header exist */
453 if (*csf_hdr != HAB_CMD_HDR) {
454 puts("Error: CSF header command not found\n");
455 return false;
456 }
457
458 csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr);
459
460 /* Check if the CSF lies within the image bounds */
461 if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) {
462 puts("Error: CSF lies outside the image bounds\n");
463 return false;
464 }
465
466 do {
Utkarsh Gupta09af35c2018-02-20 01:19:25 +0000467 struct hab_hdr *cmd;
468
469 cmd = (struct hab_hdr *)&csf_hdr[offset];
470
471 switch (cmd->tag) {
472 case (HAB_CMD_WRT_DAT):
473 puts("Error: Deprecated write command found\n");
474 return false;
475 case (HAB_CMD_CHK_DAT):
476 puts("Error: Deprecated check command found\n");
477 return false;
478 case (HAB_CMD_SET):
479 if (cmd->par == HAB_PAR_MID) {
480 puts("Error: Deprecated Set MID command found\n");
481 return false;
482 }
483 default:
484 break;
485 }
486
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000487 cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]);
488 if (!cmd_hdr_len) {
489 puts("Error: Invalid command length\n");
490 return false;
491 }
492 offset += cmd_hdr_len;
493
494 } while (offset < csf_hdr_len);
495
496 return true;
497}
498
Bryan O'Donoghue47b3f342018-01-12 12:40:16 +0000499bool imx_hab_is_enabled(void)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100500{
501 struct imx_sec_config_fuse_t *fuse =
502 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
503 uint32_t reg;
504 int ret;
505
506 ret = fuse_read(fuse->bank, fuse->word, &reg);
507 if (ret) {
508 puts("\nSecure boot fuse read error\n");
509 return ret;
510 }
511
512 return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
513}
514
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000515int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
516 uint32_t ivt_offset)
Nitin Gargb1ce7012014-09-16 13:33:25 -0500517{
518 uint32_t load_addr = 0;
519 size_t bytes;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000520 uint32_t ivt_addr = 0;
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000521 int result = 1;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500522 ulong start;
523 hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
524 hab_rvt_entry_t *hab_rvt_entry;
525 hab_rvt_exit_t *hab_rvt_exit;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000526 hab_rvt_check_target_t *hab_rvt_check_target;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000527 struct ivt *ivt;
528 struct ivt_header *ivt_hdr;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000529 enum hab_status status;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500530
Breno Limaf4a72242018-02-20 01:19:26 +0000531 hab_rvt_authenticate_image =
532 (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE;
533 hab_rvt_entry = (hab_rvt_entry_t *)HAB_RVT_ENTRY;
534 hab_rvt_exit = (hab_rvt_exit_t *)HAB_RVT_EXIT;
535 hab_rvt_check_target = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500536
Bryan O'Donoghue899cab02018-01-12 12:40:14 +0000537 if (!imx_hab_is_enabled()) {
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000538 puts("hab fuse not enabled\n");
Bryan O'Donoghuedbad3ad2018-01-12 12:40:15 +0000539 return 0;
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000540 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500541
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000542 printf("\nAuthenticate image from DDR location 0x%x...\n",
543 ddr_start);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500544
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000545 hab_caam_clock_enable(1);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500546
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000547 /* Calculate IVT address header */
548 ivt_addr = ddr_start + ivt_offset;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000549 ivt = (struct ivt *)ivt_addr;
550 ivt_hdr = &ivt->hdr;
551
552 /* Verify IVT header bugging out on error */
553 if (verify_ivt_header(ivt_hdr))
Breno Lima52d389c2018-02-20 01:19:22 +0000554 goto hab_authentication_exit;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000555
Bryan O'Donoghue85fe7b12018-01-12 12:40:04 +0000556 /* Verify IVT body */
557 if (ivt->self != ivt_addr) {
558 printf("ivt->self 0x%08x pointer is 0x%08x\n",
559 ivt->self, ivt_addr);
Breno Lima52d389c2018-02-20 01:19:22 +0000560 goto hab_authentication_exit;
Bryan O'Donoghue85fe7b12018-01-12 12:40:04 +0000561 }
562
Utkarsh Guptaf79f75e2018-02-20 01:19:23 +0000563 /* Verify if IVT DCD pointer is NULL */
Bryan O'Donoghue22e52772018-03-09 13:07:21 +0000564 if (ivt->dcd)
565 puts("Warning: DCD pointer should be NULL\n");
Utkarsh Guptaf79f75e2018-02-20 01:19:23 +0000566
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000567 start = ddr_start;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000568 bytes = image_size;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000569
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000570 /* Verify CSF */
571 if (!csf_is_valid(ivt, start, bytes))
572 goto hab_authentication_exit;
573
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000574 if (hab_rvt_entry() != HAB_SUCCESS) {
575 puts("hab entry function fail\n");
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000576 goto hab_exit_failure_print_status;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000577 }
578
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000579 status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)ddr_start, bytes);
580 if (status != HAB_SUCCESS) {
581 printf("HAB check target 0x%08x-0x%08x fail\n",
582 ddr_start, ddr_start + bytes);
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000583 goto hab_exit_failure_print_status;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000584 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500585#ifdef DEBUG
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000586 printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n", ivt_offset, ivt_addr);
Bryan O'Donoghueeb031502018-01-12 12:40:07 +0000587 printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
588 ivt->dcd, ivt->csf);
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000589 puts("Dumping IVT\n");
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000590 print_buffer(ivt_addr, (void *)(ivt_addr), 4, 0x8, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500591
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000592 puts("Dumping CSF Header\n");
Bryan O'Donoghue6c906ff2018-01-12 12:40:06 +0000593 print_buffer(ivt->csf, (void *)(ivt->csf), 4, 0x10, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500594
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100595#if !defined(CONFIG_SPL_BUILD)
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000596 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100597#endif
Nitin Gargb1ce7012014-09-16 13:33:25 -0500598
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000599 puts("\nCalling authenticate_image in ROM\n");
600 printf("\tivt_offset = 0x%x\n", ivt_offset);
601 printf("\tstart = 0x%08lx\n", start);
602 printf("\tbytes = 0x%x\n", bytes);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500603#endif
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000604 /*
605 * If the MMU is enabled, we have to notify the ROM
606 * code, or it won't flush the caches when needed.
607 * This is done, by setting the "pu_irom_mmu_enabled"
608 * word to 1. You can find its address by looking in
609 * the ROM map. This is critical for
610 * authenticate_image(). If MMU is enabled, without
611 * setting this bit, authentication will fail and may
612 * crash.
613 */
614 /* Check MMU enabled */
615 if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
616 if (is_mx6dq()) {
617 /*
618 * This won't work on Rev 1.0.0 of
619 * i.MX6Q/D, since their ROM doesn't
620 * do cache flushes. don't think any
621 * exist, so we ignore them.
622 */
623 if (!is_mx6dqp())
624 writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
625 } else if (is_mx6sdl()) {
626 writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
627 } else if (is_mx6sl()) {
628 writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000629 }
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000630 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500631
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000632 load_addr = (uint32_t)hab_rvt_authenticate_image(
633 HAB_CID_UBOOT,
634 ivt_offset, (void **)&start,
635 (size_t *)&bytes, NULL);
636 if (hab_rvt_exit() != HAB_SUCCESS) {
637 puts("hab exit function fail\n");
638 load_addr = 0;
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000639 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500640
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000641hab_exit_failure_print_status:
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100642#if !defined(CONFIG_SPL_BUILD)
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000643 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100644#endif
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000645
Breno Lima52d389c2018-02-20 01:19:22 +0000646hab_authentication_exit:
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000647
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000648 if (load_addr != 0)
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000649 result = 0;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500650
651 return result;
652}