1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
4  */
5 
6 #include <command.h>
7 #include <config.h>
8 #include <display_options.h>
9 #include <fuse.h>
10 #include <mapmem.h>
11 #include <image.h>
12 #include <asm/io.h>
13 #include <asm/global_data.h>
14 #include <asm/system.h>
15 #include <asm/arch/clock.h>
16 #include <asm/arch/sys_proto.h>
17 #include <asm/mach-imx/hab.h>
18 #include <linux/arm-smccc.h>
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 #define ALIGN_SIZE		0x1000
23 #define MX6DQ_PU_IROM_MMU_EN_VAR	0x009024a8
24 #define MX6DLS_PU_IROM_MMU_EN_VAR	0x00901dd0
25 #define MX6SL_PU_IROM_MMU_EN_VAR	0x00901c60
26 #define IS_HAB_ENABLED_BIT \
27 	(is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 :	\
28 	 ((is_soc_type(MXC_SOC_MX7) || is_soc_type(MXC_SOC_IMX8M)) ? 0x2000000 : 0x2))
29 #define FIELD_RETURN_FUSE_MASK \
30 	(is_imx8mp() ? 0xFFFFFFFF : 0x00000001)
31 /*
32  * The fuse pattern for i.MX8M Plus is 0x28001401, but bit 2 is already set from factory.
33  * This means when field return is set, the fuse word value reads 0x28001405
34  */
35 #define FIELD_RETURN_PATTERN \
36 	(is_imx8mp() ? 0x28001405 : 0x00000001)
37 
38 #ifdef CONFIG_MX7ULP
39 #define HAB_M4_PERSISTENT_START	((soc_rev() >= CHIP_REV_2_0) ? 0x20008040 : \
40 				  0x20008180)
41 #define HAB_M4_PERSISTENT_BYTES		0xB80
42 #endif
43 
ivt_header_error(const char * err_str,struct ivt_header * ivt_hdr)44 static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr)
45 {
46 	printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str,
47 	       ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version);
48 
49 	return 1;
50 }
51 
verify_ivt_header(struct ivt_header * ivt_hdr)52 static int verify_ivt_header(struct ivt_header *ivt_hdr)
53 {
54 	int result = 0;
55 
56 	if (ivt_hdr->magic != IVT_HEADER_MAGIC)
57 		result = ivt_header_error("bad magic", ivt_hdr);
58 
59 	if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH)
60 		result = ivt_header_error("bad length", ivt_hdr);
61 
62 	if ((ivt_hdr->version & HAB_MAJ_MASK) != HAB_MAJ_VER)
63 		result = ivt_header_error("bad version", ivt_hdr);
64 
65 	return result;
66 }
67 
68 #ifdef CONFIG_ARM64
69 #define FSL_SIP_HAB		0xC2000007
70 #define FSL_SIP_HAB_AUTHENTICATE	0x00
71 #define FSL_SIP_HAB_ENTRY		0x01
72 #define FSL_SIP_HAB_EXIT		0x02
73 #define FSL_SIP_HAB_REPORT_EVENT	0x03
74 #define FSL_SIP_HAB_REPORT_STATUS	0x04
75 #define FSL_SIP_HAB_FAILSAFE		0x05
76 #define FSL_SIP_HAB_CHECK_TARGET	0x06
77 static gd_t *gd_save;
78 #endif
79 
save_gd(void)80 static inline void save_gd(void)
81 {
82 #ifdef CONFIG_ARM64
83 	gd_save = gd;
84 #endif
85 }
86 
restore_gd(void)87 static inline void restore_gd(void)
88 {
89 #ifdef CONFIG_ARM64
90 	/*
91 	 * Make will already error that reserving x18 is not supported at the
92 	 * time of writing, clang: error: unknown argument: '-ffixed-x18'
93 	 */
94 	__asm__ volatile("mov x18, %0\n" : : "r" (gd_save));
95 #endif
96 }
97 
hab_rvt_report_event(enum hab_status status,u32 index,u8 * event,size_t * bytes)98 enum hab_status hab_rvt_report_event(enum hab_status status, u32 index,
99 				     u8 *event, size_t *bytes)
100 {
101 	enum hab_status ret;
102 	hab_rvt_report_event_t *hab_rvt_report_event_func;
103 	struct arm_smccc_res res __maybe_unused;
104 
105 	hab_rvt_report_event_func =  (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT;
106 #if defined(CONFIG_ARM64)
107 	if (current_el() != 3) {
108 		/* call sip */
109 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_EVENT, (unsigned long)index,
110 			      (unsigned long)event, (unsigned long)bytes, 0, 0, 0, &res);
111 		return (enum hab_status)res.a0;
112 	}
113 #endif
114 
115 	save_gd();
116 	ret = hab_rvt_report_event_func(status, index, event, bytes);
117 	restore_gd();
118 
119 	return ret;
120 
121 }
122 
hab_rvt_report_status(enum hab_config * config,enum hab_state * state)123 enum hab_status hab_rvt_report_status(enum hab_config *config, enum hab_state *state)
124 {
125 	enum hab_status ret;
126 	hab_rvt_report_status_t *hab_rvt_report_status_func;
127 	struct arm_smccc_res res __maybe_unused;
128 
129 	hab_rvt_report_status_func = (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS;
130 #if defined(CONFIG_ARM64)
131 	if (current_el() != 3) {
132 		/* call sip */
133 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_STATUS, (unsigned long)config,
134 			      (unsigned long)state, 0, 0, 0, 0, &res);
135 		return (enum hab_status)res.a0;
136 	}
137 #endif
138 
139 	save_gd();
140 	ret = hab_rvt_report_status_func(config, state);
141 	restore_gd();
142 
143 	return ret;
144 }
145 
hab_rvt_entry(void)146 enum hab_status hab_rvt_entry(void)
147 {
148 	enum hab_status ret;
149 	hab_rvt_entry_t *hab_rvt_entry_func;
150 	struct arm_smccc_res res __maybe_unused;
151 
152 	hab_rvt_entry_func = (hab_rvt_entry_t *)HAB_RVT_ENTRY;
153 #if defined(CONFIG_ARM64)
154 	if (current_el() != 3) {
155 		/* call sip */
156 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_ENTRY, 0, 0, 0, 0, 0, 0, &res);
157 		return (enum hab_status)res.a0;
158 	}
159 #endif
160 
161 	save_gd();
162 	ret = hab_rvt_entry_func();
163 	restore_gd();
164 
165 	return ret;
166 }
167 
hab_rvt_exit(void)168 enum hab_status hab_rvt_exit(void)
169 {
170 	enum hab_status ret;
171 	hab_rvt_exit_t *hab_rvt_exit_func;
172 	struct arm_smccc_res res __maybe_unused;
173 
174 	hab_rvt_exit_func =  (hab_rvt_exit_t *)HAB_RVT_EXIT;
175 #if defined(CONFIG_ARM64)
176 	if (current_el() != 3) {
177 		/* call sip */
178 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_EXIT, 0, 0, 0, 0, 0, 0, &res);
179 		return (enum hab_status)res.a0;
180 	}
181 #endif
182 
183 	save_gd();
184 	ret = hab_rvt_exit_func();
185 	restore_gd();
186 
187 	return ret;
188 }
189 
hab_rvt_failsafe(void)190 void hab_rvt_failsafe(void)
191 {
192 	hab_rvt_failsafe_t *hab_rvt_failsafe_func;
193 
194 	hab_rvt_failsafe_func = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE;
195 #if defined(CONFIG_ARM64)
196 	if (current_el() != 3) {
197 		/* call sip */
198 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_FAILSAFE, 0, 0, 0, 0, 0, 0, NULL);
199 		return;
200 	}
201 #endif
202 
203 	save_gd();
204 	hab_rvt_failsafe_func();
205 	restore_gd();
206 }
207 
hab_rvt_check_target(enum hab_target type,const void * start,size_t bytes)208 enum hab_status hab_rvt_check_target(enum hab_target type, const void *start,
209 					       size_t bytes)
210 {
211 	enum hab_status ret;
212 	hab_rvt_check_target_t *hab_rvt_check_target_func;
213 	struct arm_smccc_res res __maybe_unused;
214 
215 	hab_rvt_check_target_func =  (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET;
216 #if defined(CONFIG_ARM64)
217 	if (current_el() != 3) {
218 		/* call sip */
219 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_CHECK_TARGET, (unsigned long)type,
220 			      (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res);
221 		return (enum hab_status)res.a0;
222 	}
223 #endif
224 
225 	save_gd();
226 	ret = hab_rvt_check_target_func(type, start, bytes);
227 	restore_gd();
228 
229 	return ret;
230 }
231 
hab_rvt_authenticate_image(uint8_t cid,ptrdiff_t ivt_offset,void ** start,size_t * bytes,hab_loader_callback_f_t loader)232 void *hab_rvt_authenticate_image(uint8_t cid, ptrdiff_t ivt_offset,
233 				 void **start, size_t *bytes, hab_loader_callback_f_t loader)
234 {
235 	void *ret;
236 	hab_rvt_authenticate_image_t *hab_rvt_authenticate_image_func;
237 	struct arm_smccc_res res __maybe_unused;
238 
239 	hab_rvt_authenticate_image_func = (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE;
240 #if defined(CONFIG_ARM64)
241 	if (current_el() != 3) {
242 		/* call sip */
243 		arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_AUTHENTICATE, (unsigned long)ivt_offset,
244 			      (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res);
245 		return (void *)res.a0;
246 	}
247 #endif
248 
249 	save_gd();
250 	ret = hab_rvt_authenticate_image_func(cid, ivt_offset, start, bytes, loader);
251 	restore_gd();
252 
253 	return ret;
254 }
255 
256 #if !defined(CONFIG_XPL_BUILD)
257 
258 #define MAX_RECORD_BYTES     (8*1024) /* 4 kbytes */
259 
260 struct record {
261 	uint8_t  tag;						/* Tag */
262 	uint8_t  len[2];					/* Length */
263 	uint8_t  par;						/* Version */
264 	uint8_t  contents[MAX_RECORD_BYTES];/* Record Data */
265 	bool	 any_rec_flag;
266 };
267 
268 static char *rsn_str[] = {
269 			  "RSN = HAB_RSN_ANY (0x00)\n",
270 			  "RSN = HAB_ENG_FAIL (0x30)\n",
271 			  "RSN = HAB_INV_ADDRESS (0x22)\n",
272 			  "RSN = HAB_INV_ASSERTION (0x0C)\n",
273 			  "RSN = HAB_INV_CALL (0x28)\n",
274 			  "RSN = HAB_INV_CERTIFICATE (0x21)\n",
275 			  "RSN = HAB_INV_COMMAND (0x06)\n",
276 			  "RSN = HAB_INV_CSF (0x11)\n",
277 			  "RSN = HAB_INV_DCD (0x27)\n",
278 			  "RSN = HAB_INV_INDEX (0x0F)\n",
279 			  "RSN = HAB_INV_IVT (0x05)\n",
280 			  "RSN = HAB_INV_KEY (0x1D)\n",
281 			  "RSN = HAB_INV_RETURN (0x1E)\n",
282 			  "RSN = HAB_INV_SIGNATURE (0x18)\n",
283 			  "RSN = HAB_INV_SIZE (0x17)\n",
284 			  "RSN = HAB_MEM_FAIL (0x2E)\n",
285 			  "RSN = HAB_OVR_COUNT (0x2B)\n",
286 			  "RSN = HAB_OVR_STORAGE (0x2D)\n",
287 			  "RSN = HAB_UNS_ALGORITHM (0x12)\n",
288 			  "RSN = HAB_UNS_COMMAND (0x03)\n",
289 			  "RSN = HAB_UNS_ENGINE (0x0A)\n",
290 			  "RSN = HAB_UNS_ITEM (0x24)\n",
291 			  "RSN = HAB_UNS_KEY (0x1B)\n",
292 			  "RSN = HAB_UNS_PROTOCOL (0x14)\n",
293 			  "RSN = HAB_UNS_STATE (0x09)\n",
294 			  "RSN = INVALID\n",
295 			  NULL
296 };
297 
298 static char *sts_str[] = {
299 			  "STS = HAB_STS_ANY (0x00)\n",
300 			  "STS = HAB_FAILURE (0x33)\n",
301 			  "STS = HAB_WARNING (0x69)\n",
302 			  "STS = HAB_SUCCESS (0xF0)\n",
303 			  "STS = INVALID\n",
304 			  NULL
305 };
306 
307 static char *eng_str[] = {
308 			  "ENG = HAB_ENG_ANY (0x00)\n",
309 			  "ENG = HAB_ENG_SCC (0x03)\n",
310 			  "ENG = HAB_ENG_RTIC (0x05)\n",
311 			  "ENG = HAB_ENG_SAHARA (0x06)\n",
312 			  "ENG = HAB_ENG_CSU (0x0A)\n",
313 			  "ENG = HAB_ENG_SRTC (0x0C)\n",
314 			  "ENG = HAB_ENG_DCP (0x1B)\n",
315 			  "ENG = HAB_ENG_CAAM (0x1D)\n",
316 			  "ENG = HAB_ENG_SNVS (0x1E)\n",
317 			  "ENG = HAB_ENG_OCOTP (0x21)\n",
318 			  "ENG = HAB_ENG_DTCP (0x22)\n",
319 			  "ENG = HAB_ENG_ROM (0x36)\n",
320 			  "ENG = HAB_ENG_HDCP (0x24)\n",
321 			  "ENG = HAB_ENG_RTL (0x77)\n",
322 			  "ENG = HAB_ENG_SW (0xFF)\n",
323 			  "ENG = INVALID\n",
324 			  NULL
325 };
326 
327 static char *ctx_str[] = {
328 			  "CTX = HAB_CTX_ANY(0x00)\n",
329 			  "CTX = HAB_CTX_FAB (0xFF)\n",
330 			  "CTX = HAB_CTX_ENTRY (0xE1)\n",
331 			  "CTX = HAB_CTX_TARGET (0x33)\n",
332 			  "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
333 			  "CTX = HAB_CTX_DCD (0xDD)\n",
334 			  "CTX = HAB_CTX_CSF (0xCF)\n",
335 			  "CTX = HAB_CTX_COMMAND (0xC0)\n",
336 			  "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
337 			  "CTX = HAB_CTX_ASSERT (0xA0)\n",
338 			  "CTX = HAB_CTX_EXIT (0xEE)\n",
339 			  "CTX = INVALID\n",
340 			  NULL
341 };
342 
343 static uint8_t hab_statuses[5] = {
344 	HAB_STS_ANY,
345 	HAB_FAILURE,
346 	HAB_WARNING,
347 	HAB_SUCCESS
348 };
349 
350 static uint8_t hab_reasons[26] = {
351 	HAB_RSN_ANY,
352 	HAB_ENG_FAIL,
353 	HAB_INV_ADDRESS,
354 	HAB_INV_ASSERTION,
355 	HAB_INV_CALL,
356 	HAB_INV_CERTIFICATE,
357 	HAB_INV_COMMAND,
358 	HAB_INV_CSF,
359 	HAB_INV_DCD,
360 	HAB_INV_INDEX,
361 	HAB_INV_IVT,
362 	HAB_INV_KEY,
363 	HAB_INV_RETURN,
364 	HAB_INV_SIGNATURE,
365 	HAB_INV_SIZE,
366 	HAB_MEM_FAIL,
367 	HAB_OVR_COUNT,
368 	HAB_OVR_STORAGE,
369 	HAB_UNS_ALGORITHM,
370 	HAB_UNS_COMMAND,
371 	HAB_UNS_ENGINE,
372 	HAB_UNS_ITEM,
373 	HAB_UNS_KEY,
374 	HAB_UNS_PROTOCOL,
375 	HAB_UNS_STATE
376 };
377 
378 static uint8_t hab_contexts[12] = {
379 	HAB_CTX_ANY,
380 	HAB_CTX_FAB,
381 	HAB_CTX_ENTRY,
382 	HAB_CTX_TARGET,
383 	HAB_CTX_AUTHENTICATE,
384 	HAB_CTX_DCD,
385 	HAB_CTX_CSF,
386 	HAB_CTX_COMMAND,
387 	HAB_CTX_AUT_DAT,
388 	HAB_CTX_ASSERT,
389 	HAB_CTX_EXIT
390 };
391 
392 static uint8_t hab_engines[16] = {
393 	HAB_ENG_ANY,
394 	HAB_ENG_SCC,
395 	HAB_ENG_RTIC,
396 	HAB_ENG_SAHARA,
397 	HAB_ENG_CSU,
398 	HAB_ENG_SRTC,
399 	HAB_ENG_DCP,
400 	HAB_ENG_CAAM,
401 	HAB_ENG_SNVS,
402 	HAB_ENG_OCOTP,
403 	HAB_ENG_DTCP,
404 	HAB_ENG_ROM,
405 	HAB_ENG_HDCP,
406 	HAB_ENG_RTL,
407 	HAB_ENG_SW
408 };
409 
get_idx(u8 * list,u8 tgt,u32 size)410 static inline u32 get_idx(u8 *list, u8 tgt, u32 size)
411 {
412 	u32 idx = 0;
413 	u8 element;
414 
415 	while (idx < size) {
416 		element = list[idx];
417 		if (element == tgt)
418 			return idx;
419 		++idx;
420 	}
421 	return idx;
422 }
423 
process_event_record(uint8_t * event_data,size_t bytes)424 static void process_event_record(uint8_t *event_data, size_t bytes)
425 {
426 	struct record *rec = (struct record *)event_data;
427 
428 	printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0],
429 	       ARRAY_SIZE(hab_statuses))]);
430 	printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1],
431 	       ARRAY_SIZE(hab_reasons))]);
432 	printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2],
433 	       ARRAY_SIZE(hab_contexts))]);
434 	printf("%s", eng_str[get_idx(hab_engines, rec->contents[3],
435 	       ARRAY_SIZE(hab_engines))]);
436 }
437 
display_event(uint8_t * event_data,size_t bytes)438 static void display_event(uint8_t *event_data, size_t bytes)
439 {
440 	uint32_t i;
441 
442 	if (!(event_data && bytes > 0))
443 		return;
444 
445 	for (i = 0; i < bytes; i++) {
446 		if (i == 0)
447 			printf("\t0x%02x", event_data[i]);
448 		else if ((i % 8) == 0)
449 			printf("\n\t0x%02x", event_data[i]);
450 		else
451 			printf(" 0x%02x", event_data[i]);
452 	}
453 
454 	process_event_record(event_data, bytes);
455 }
456 
get_hab_status(void)457 static int get_hab_status(void)
458 {
459 	uint32_t index = 0; /* Loop index */
460 	uint8_t event_data[128]; /* Event data buffer */
461 	size_t bytes = sizeof(event_data); /* Event size in bytes */
462 	enum hab_config config = 0;
463 	enum hab_state state = 0;
464 
465 	if (imx_hab_is_enabled())
466 		puts("\nSecure boot enabled\n");
467 	else
468 		puts("\nSecure boot disabled\n");
469 
470 	/* Check HAB status */
471 	if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
472 		printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
473 		       config, state);
474 
475 		/* Display HAB events */
476 		while (hab_rvt_report_event(HAB_STS_ANY, index, event_data,
477 					&bytes) == HAB_SUCCESS) {
478 			puts("\n");
479 			printf("--------- HAB Event %d -----------------\n",
480 			       index + 1);
481 			puts("event data:\n");
482 			display_event(event_data, bytes);
483 			puts("\n");
484 			bytes = sizeof(event_data);
485 			index++;
486 		}
487 	}
488 	/* Display message if no HAB events are found */
489 	else {
490 		printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
491 		       config, state);
492 		puts("No HAB Events Found!\n\n");
493 	}
494 	return 0;
495 }
496 
497 #ifdef CONFIG_MX7ULP
498 
get_record_len(struct record * rec)499 static int get_record_len(struct record *rec)
500 {
501 	return (size_t)((rec->len[0] << 8) + (rec->len[1]));
502 }
503 
get_hab_status_m4(void)504 static int get_hab_status_m4(void)
505 {
506 	unsigned int index = 0;
507 	uint8_t event_data[128];
508 	size_t record_len, offset = 0;
509 	enum hab_config config = 0;
510 	enum hab_state state = 0;
511 
512 	if (imx_hab_is_enabled())
513 		puts("\nSecure boot enabled\n");
514 	else
515 		puts("\nSecure boot disabled\n");
516 
517 	/*
518 	 * HAB in both A7 and M4 gather the security state
519 	 * and configuration of the chip from
520 	 * shared SNVS module
521 	 */
522 	hab_rvt_report_status(&config, &state);
523 	printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
524 	       config, state);
525 
526 	struct record *rec = (struct record *)(HAB_M4_PERSISTENT_START);
527 
528 	record_len = get_record_len(rec);
529 
530 	/* Check if HAB persistent memory is valid */
531 	if (rec->tag != HAB_TAG_EVT_DEF ||
532 	    record_len != sizeof(struct evt_def) ||
533 	    (rec->par & HAB_MAJ_MASK) != HAB_MAJ_VER) {
534 		puts("\nERROR: Invalid HAB persistent memory\n");
535 		return 1;
536 	}
537 
538 	/* Parse events in HAB M4 persistent memory region */
539 	while (offset < HAB_M4_PERSISTENT_BYTES) {
540 		rec = (struct record *)(HAB_M4_PERSISTENT_START + offset);
541 
542 		record_len = get_record_len(rec);
543 
544 		if (rec->tag == HAB_TAG_EVT) {
545 			memcpy(&event_data, rec, record_len);
546 			puts("\n");
547 			printf("--------- HAB Event %d -----------------\n",
548 			       index + 1);
549 			puts("event data:\n");
550 			display_event(event_data, record_len);
551 			puts("\n");
552 			index++;
553 		}
554 
555 		offset += record_len;
556 
557 		/* Ensure all records start on a word boundary */
558 		if ((offset % 4) != 0)
559 			offset =  offset + (4 - (offset % 4));
560 	}
561 
562 	if (!index)
563 		puts("No HAB Events Found!\n\n");
564 
565 	return 0;
566 }
567 #endif
568 
do_hab_status(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])569 static int do_hab_status(struct cmd_tbl *cmdtp, int flag, int argc,
570 			 char *const argv[])
571 {
572 #ifdef CONFIG_MX7ULP
573 	if ((argc > 2)) {
574 		cmd_usage(cmdtp);
575 		return 1;
576 	}
577 
578 	if (strcmp("m4", argv[1]) == 0)
579 		get_hab_status_m4();
580 	else
581 		get_hab_status();
582 #else
583 	if ((argc != 1)) {
584 		cmd_usage(cmdtp);
585 		return 1;
586 	}
587 
588 	get_hab_status();
589 #endif
590 
591 	return 0;
592 }
593 
get_image_ivt_offset(ulong img_addr)594 static ulong get_image_ivt_offset(ulong img_addr)
595 {
596 	const void *buf;
597 
598 	buf = map_sysmem(img_addr, 0);
599 	switch (genimg_get_format(buf)) {
600 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
601 	case IMAGE_FORMAT_LEGACY:
602 		return (image_get_image_size((struct legacy_img_hdr *)img_addr)
603 			+ 0x1000 - 1)  & ~(0x1000 - 1);
604 #endif
605 #if CONFIG_IS_ENABLED(FIT)
606 	case IMAGE_FORMAT_FIT:
607 		return (fit_get_size(buf) + 0x1000 - 1)  & ~(0x1000 - 1);
608 #endif
609 	default:
610 		return 0;
611 	}
612 }
613 
do_authenticate_image(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])614 static int do_authenticate_image(struct cmd_tbl *cmdtp, int flag, int argc,
615 				 char *const argv[])
616 {
617 	ulong	addr, length, ivt_offset;
618 	int	rcode = 0;
619 
620 	if (argc < 3)
621 		return CMD_RET_USAGE;
622 
623 	addr = hextoul(argv[1], NULL);
624 	length = hextoul(argv[2], NULL);
625 	if (argc == 3)
626 		ivt_offset = get_image_ivt_offset(addr);
627 	else
628 		ivt_offset = hextoul(argv[3], NULL);
629 
630 	rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
631 	if (rcode == 0)
632 		rcode = CMD_RET_SUCCESS;
633 	else
634 		rcode = CMD_RET_FAILURE;
635 
636 	return rcode;
637 }
638 
do_hab_failsafe(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])639 static int do_hab_failsafe(struct cmd_tbl *cmdtp, int flag, int argc,
640 			   char *const argv[])
641 {
642 	if (argc != 1) {
643 		cmd_usage(cmdtp);
644 		return 1;
645 	}
646 
647 	hab_rvt_failsafe();
648 
649 	return 0;
650 }
651 
do_hab_version(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])652 static int do_hab_version(struct cmd_tbl *cmdtp, int flag, int argc,
653 			  char *const argv[])
654 {
655 	struct hab_hdr *hdr = (struct hab_hdr *)HAB_RVT_BASE;
656 
657 	if (hdr->tag != HAB_TAG_RVT) {
658 		printf("Unexpected header tag: %x\n", hdr->tag);
659 		return CMD_RET_FAILURE;
660 	}
661 
662 	printf("HAB version: %d.%d\n", hdr->par >> 4, hdr->par & 0xf);
663 
664 	return 0;
665 }
666 
do_authenticate_image_or_failover(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])667 static int do_authenticate_image_or_failover(struct cmd_tbl *cmdtp, int flag,
668 					     int argc, char *const argv[])
669 {
670 	int ret = CMD_RET_FAILURE;
671 
672 	if (argc < 3) {
673 		ret = CMD_RET_USAGE;
674 		goto error;
675 	}
676 
677 	if (!imx_hab_is_enabled()) {
678 		printf("error: secure boot disabled\n");
679 		goto error;
680 	}
681 
682 	if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) {
683 		fprintf(stderr, "authentication fail -> %s %s %s %s\n",
684 			argv[0], argv[1], argv[2], argv[3]);
685 		do_hab_failsafe(0, 0, 1, NULL);
686 	};
687 	ret = CMD_RET_SUCCESS;
688 error:
689 	return ret;
690 }
691 
692 #ifdef CONFIG_MX7ULP
693 U_BOOT_CMD(
694 		hab_status, CONFIG_SYS_MAXARGS, 2, do_hab_status,
695 		"display HAB status and events",
696 		"hab_status - A7 HAB event and status\n"
697 		"hab_status m4 - M4 HAB event and status"
698 	  );
699 #else
700 U_BOOT_CMD(
701 		hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
702 		"display HAB status",
703 		""
704 	  );
705 #endif
706 
707 U_BOOT_CMD(
708 		hab_auth_img, 4, 0, do_authenticate_image,
709 		"authenticate image via HAB",
710 		"addr length ivt_offset\n"
711 		"addr - image hex address\n"
712 		"length - image hex length\n"
713 		"ivt_offset - hex offset of IVT in the image (optional)"
714 	  );
715 
716 U_BOOT_CMD(
717 		hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
718 		"run BootROM failsafe routine",
719 		""
720 	  );
721 
722 U_BOOT_CMD(
723 		hab_auth_img_or_fail, 4, 0,
724 		do_authenticate_image_or_failover,
725 		"authenticate image via HAB. Switch to USB BootROM mode on failure",
726 		"addr length ivt_offset\n"
727 		"addr - image hex address\n"
728 		"length - image hex length\n"
729 		"ivt_offset - hex offset of IVT in the image (optional)"
730 	  );
731 
732 U_BOOT_CMD(
733 		hab_version, 1, 0, do_hab_version,
734 		"print HAB major/minor version",
735 		""
736 	  );
737 
738 #endif /* !defined(CONFIG_XPL_BUILD) */
739 
740 /* Get CSF Header length */
get_hab_hdr_len(struct hab_hdr * hdr)741 static int get_hab_hdr_len(struct hab_hdr *hdr)
742 {
743 	return (size_t)((hdr->len[0] << 8) + (hdr->len[1]));
744 }
745 
746 /* Check whether addr lies between start and
747  * end and is within the length of the image
748  */
chk_bounds(u8 * addr,size_t bytes,u8 * start,u8 * end)749 static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end)
750 {
751 	size_t csf_size = (size_t)((end + 1) - addr);
752 
753 	return (addr && (addr >= start) && (addr <= end) &&
754 		(csf_size >= bytes));
755 }
756 
757 /* Get Length of each command in CSF */
get_csf_cmd_hdr_len(u8 * csf_hdr)758 static int get_csf_cmd_hdr_len(u8 *csf_hdr)
759 {
760 	if (*csf_hdr == HAB_CMD_HDR)
761 		return sizeof(struct hab_hdr);
762 
763 	return get_hab_hdr_len((struct hab_hdr *)csf_hdr);
764 }
765 
766 /* Check if CSF is valid */
csf_is_valid(struct ivt * ivt,ulong start_addr,size_t bytes)767 static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes)
768 {
769 	u8 *start = (u8 *)start_addr;
770 	u8 *csf_hdr;
771 	u8 *end;
772 
773 	size_t csf_hdr_len;
774 	size_t cmd_hdr_len;
775 	size_t offset = 0;
776 
777 	if (bytes != 0)
778 		end = start + bytes - 1;
779 	else
780 		end = start;
781 
782 	/* Verify if CSF pointer content is zero */
783 	if (!ivt->csf) {
784 		puts("Error: CSF pointer is NULL\n");
785 		return false;
786 	}
787 
788 	csf_hdr = (u8 *)(ulong)ivt->csf;
789 
790 	/* Verify if CSF Header exist */
791 	if (*csf_hdr != HAB_CMD_HDR) {
792 		puts("Error: CSF header command not found\n");
793 		return false;
794 	}
795 
796 	csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr);
797 
798 	/* Check if the CSF lies within the image bounds */
799 	if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) {
800 		puts("Error: CSF lies outside the image bounds\n");
801 		return false;
802 	}
803 
804 	do {
805 		struct hab_hdr *cmd;
806 
807 		cmd = (struct hab_hdr *)&csf_hdr[offset];
808 
809 		switch (cmd->tag) {
810 		case (HAB_CMD_WRT_DAT):
811 			puts("Error: Deprecated write command found\n");
812 			return false;
813 		case (HAB_CMD_CHK_DAT):
814 			puts("Error: Deprecated check command found\n");
815 			return false;
816 		case (HAB_CMD_SET):
817 			if (cmd->par == HAB_PAR_MID) {
818 				puts("Error: Deprecated Set MID command found\n");
819 				return false;
820 			}
821 		default:
822 			break;
823 		}
824 
825 		cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]);
826 		if (!cmd_hdr_len) {
827 			puts("Error: Invalid command length\n");
828 			return false;
829 		}
830 		offset += cmd_hdr_len;
831 
832 	} while (offset < csf_hdr_len);
833 
834 	return true;
835 }
836 
837 /*
838  * Validate IVT structure of the image being authenticated
839  */
validate_ivt(struct ivt * ivt_initial)840 static int validate_ivt(struct ivt *ivt_initial)
841 {
842 	struct ivt_header *ivt_hdr = &ivt_initial->hdr;
843 
844 	if ((ulong)ivt_initial & 0x3) {
845 		puts("Error: Image's start address is not 4 byte aligned\n");
846 		return 0;
847 	}
848 
849 	/* Check IVT fields before allowing authentication */
850 	if ((!verify_ivt_header(ivt_hdr)) && \
851 	    (ivt_initial->entry != 0x0) && \
852 	    (ivt_initial->reserved1 == 0x0) && \
853 	    (ivt_initial->self == \
854 		   (uint32_t)((ulong)ivt_initial & 0xffffffff)) && \
855 	    (ivt_initial->csf != 0x0) && \
856 	    (ivt_initial->reserved2 == 0x0)) {
857 		/* Report boot failure if DCD pointer is found in IVT */
858 		if (ivt_initial->dcd != 0x0)
859 			puts("Error: DCD pointer must be 0\n");
860 		else
861 			return 1;
862 	}
863 
864 	puts("Error: Invalid IVT structure\n");
865 	debug("\nAllowed IVT structure:\n");
866 	debug("IVT HDR       = 0x4X2000D1\n");
867 	debug("IVT ENTRY     = 0xXXXXXXXX\n");
868 	debug("IVT RSV1      = 0x0\n");
869 	debug("IVT DCD       = 0x0\n");		/* Recommended */
870 	debug("IVT BOOT_DATA = 0xXXXXXXXX\n");	/* Commonly 0x0 */
871 	debug("IVT SELF      = 0xXXXXXXXX\n");	/* = ddr_start + ivt_offset */
872 	debug("IVT CSF       = 0xXXXXXXXX\n");
873 	debug("IVT RSV2      = 0x0\n");
874 
875 	/* Invalid IVT structure */
876 	return 0;
877 }
878 
imx_hab_is_enabled(void)879 bool imx_hab_is_enabled(void)
880 {
881 	struct imx_fuse *sec_config =
882 		(struct imx_fuse *)&imx_sec_config_fuse;
883 	struct imx_fuse *field_return =
884 		(struct imx_fuse *)&imx_field_return_fuse;
885 	uint32_t reg;
886 	bool is_enabled;
887 	int ret;
888 
889 	ret = fuse_read(sec_config->bank, sec_config->word, &reg);
890 	if (ret) {
891 		puts("Secure boot fuse read error\n");
892 		return ret;
893 	}
894 	is_enabled = reg & IS_HAB_ENABLED_BIT;
895 	if (is_enabled) {
896 		ret = fuse_read(field_return->bank, field_return->word, &reg);
897 		if (ret) {
898 			puts("Field return fuse read error\n");
899 			return ret;
900 		}
901 		is_enabled = (reg & FIELD_RETURN_FUSE_MASK) != FIELD_RETURN_PATTERN;
902 	}
903 
904 	return is_enabled;
905 }
906 
imx_hab_authenticate_image(uint32_t ddr_start,uint32_t image_size,uint32_t ivt_offset)907 int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
908 			       uint32_t ivt_offset)
909 {
910 	ulong load_addr = 0;
911 	size_t bytes;
912 	ulong ivt_addr = 0;
913 	int result = 1;
914 	ulong start;
915 	struct ivt *ivt;
916 	enum hab_status status;
917 
918 	if (!imx_hab_is_enabled())
919 		puts("hab fuse not enabled\n");
920 
921 	printf("\nAuthenticate image from DDR location 0x%x...\n",
922 	       ddr_start);
923 
924 	hab_caam_clock_enable(1);
925 
926 	/* Calculate IVT address header */
927 	ivt_addr = (ulong) (ddr_start + ivt_offset);
928 	ivt = (struct ivt *)ivt_addr;
929 
930 	/* Verify IVT header bugging out on error */
931 	if (!validate_ivt(ivt))
932 		goto hab_authentication_exit;
933 
934 	start = ddr_start;
935 	bytes = image_size;
936 
937 	/* Verify CSF */
938 	if (!csf_is_valid(ivt, start, bytes))
939 		goto hab_authentication_exit;
940 
941 	if (hab_rvt_entry() != HAB_SUCCESS) {
942 		puts("hab entry function fail\n");
943 		goto hab_exit_failure_print_status;
944 	}
945 
946 	status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)(ulong)ddr_start, bytes);
947 	if (status != HAB_SUCCESS) {
948 		printf("HAB check target 0x%08x-0x%08lx fail\n",
949 		       ddr_start, ddr_start + (ulong)bytes);
950 		goto hab_exit_failure_print_status;
951 	}
952 #ifdef DEBUG
953 	printf("\nivt_offset = 0x%x, ivt addr = 0x%lx\n", ivt_offset, ivt_addr);
954 	printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
955 	       ivt->dcd, ivt->csf);
956 	puts("Dumping IVT\n");
957 	print_buffer(ivt_addr, (void *)(uintptr_t)(ivt_addr), 4, 0x8, 0);
958 
959 	puts("Dumping CSF Header\n");
960 	print_buffer(ivt->csf, (void *)(uintptr_t)(ivt->csf), 4, 0x10, 0);
961 
962 #if  !defined(CONFIG_XPL_BUILD)
963 	get_hab_status();
964 #endif
965 
966 	puts("\nCalling authenticate_image in ROM\n");
967 	printf("\tivt_offset = 0x%x\n", ivt_offset);
968 	printf("\tstart = 0x%08lx\n", start);
969 	printf("\tbytes = 0x%lx\n", (ulong)bytes);
970 #endif
971 
972 #ifndef CONFIG_ARM64
973 	/*
974 	 * If the MMU is enabled, we have to notify the ROM
975 	 * code, or it won't flush the caches when needed.
976 	 * This is done, by setting the "pu_irom_mmu_enabled"
977 	 * word to 1. You can find its address by looking in
978 	 * the ROM map. This is critical for
979 	 * authenticate_image(). If MMU is enabled, without
980 	 * setting this bit, authentication will fail and may
981 	 * crash.
982 	 */
983 	/* Check MMU enabled */
984 	if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
985 		if (is_mx6dq()) {
986 			/*
987 			 * This won't work on Rev 1.0.0 of
988 			 * i.MX6Q/D, since their ROM doesn't
989 			 * do cache flushes. don't think any
990 			 * exist, so we ignore them.
991 			 */
992 			if (!is_mx6dqp())
993 				writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
994 		} else if (is_mx6sdl()) {
995 			writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
996 		} else if (is_mx6sl()) {
997 			writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
998 		}
999 	}
1000 #endif
1001 
1002 	load_addr = (ulong)hab_rvt_authenticate_image(
1003 			HAB_CID_UBOOT,
1004 			ivt_offset, (void **)&start,
1005 			(size_t *)&bytes, NULL);
1006 	if (hab_rvt_exit() != HAB_SUCCESS) {
1007 		puts("hab exit function fail\n");
1008 		load_addr = 0;
1009 	}
1010 
1011 hab_exit_failure_print_status:
1012 #if !defined(CONFIG_XPL_BUILD)
1013 	get_hab_status();
1014 #endif
1015 
1016 hab_authentication_exit:
1017 
1018 	if (load_addr != 0 || !imx_hab_is_enabled())
1019 		result = 0;
1020 
1021 	return result;
1022 }
1023 
authenticate_image(u32 ddr_start,u32 raw_image_size)1024 int authenticate_image(u32 ddr_start, u32 raw_image_size)
1025 {
1026 	u32 ivt_offset;
1027 	size_t bytes;
1028 
1029 	ivt_offset = (raw_image_size + ALIGN_SIZE - 1) &
1030 					~(ALIGN_SIZE - 1);
1031 	bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE;
1032 
1033 	return imx_hab_authenticate_image(ddr_start, bytes, ivt_offset);
1034 }
1035