blob: 27e053ef701c2f0660695cc4f046e235b617df05 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefano Babic83fd8582013-06-28 00:20:21 +02002/*
Ulises Cardenas8df2acd2015-07-02 21:26:30 -05003 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
Stefano Babic83fd8582013-06-28 00:20:21 +02004 */
5
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -05008#include <config.h>
Simon Glass1ab16922022-07-31 12:28:48 -06009#include <display_options.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -050010#include <fuse.h>
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +010011#include <mapmem.h>
12#include <image.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020013#include <asm/io.h>
Peng Fan4f3cf8f2021-03-25 17:30:07 +080014#include <asm/global_data.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050015#include <asm/system.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050016#include <asm/arch/clock.h>
Stefano Babic14404422014-06-10 10:26:22 +020017#include <asm/arch/sys_proto.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020018#include <asm/mach-imx/hab.h>
Peng Fan4f3cf8f2021-03-25 17:30:07 +080019#include <linux/arm-smccc.h>
20
21DECLARE_GLOBAL_DATA_PTR;
Stefano Babic83fd8582013-06-28 00:20:21 +020022
Nitin Gargb1ce7012014-09-16 13:33:25 -050023#define ALIGN_SIZE 0x1000
Nitin Gargb1ce7012014-09-16 13:33:25 -050024#define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
25#define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
Breno Matheus Lima25d93482019-05-07 20:19:17 +000026#define MX6SL_PU_IROM_MMU_EN_VAR 0x00901c60
Adrian Alonso00a0b562015-10-12 13:48:15 -050027#define IS_HAB_ENABLED_BIT \
Peng Fana26ba6d2017-02-22 16:21:53 +080028 (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
Peng Fan4f3cf8f2021-03-25 17:30:07 +080029 ((is_soc_type(MXC_SOC_MX7) || is_soc_type(MXC_SOC_IMX8M)) ? 0x2000000 : 0x2))
Nitin Gargb1ce7012014-09-16 13:33:25 -050030
Breno Lima3adb6f02021-03-25 17:30:13 +080031#ifdef CONFIG_MX7ULP
32#define HAB_M4_PERSISTENT_START ((soc_rev() >= CHIP_REV_2_0) ? 0x20008040 : \
33 0x20008180)
34#define HAB_M4_PERSISTENT_BYTES 0xB80
35#endif
36
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +000037static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr)
38{
39 printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str,
40 ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version);
41
42 return 1;
43}
44
45static int verify_ivt_header(struct ivt_header *ivt_hdr)
46{
47 int result = 0;
48
49 if (ivt_hdr->magic != IVT_HEADER_MAGIC)
50 result = ivt_header_error("bad magic", ivt_hdr);
51
52 if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH)
53 result = ivt_header_error("bad length", ivt_hdr);
54
Breno Limad03a4d32021-03-25 17:30:12 +080055 if ((ivt_hdr->version & HAB_MAJ_MASK) != HAB_MAJ_VER)
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +000056 result = ivt_header_error("bad version", ivt_hdr);
57
58 return result;
59}
60
Peng Fan4f3cf8f2021-03-25 17:30:07 +080061#ifdef CONFIG_ARM64
62#define FSL_SIP_HAB 0xC2000007
63#define FSL_SIP_HAB_AUTHENTICATE 0x00
64#define FSL_SIP_HAB_ENTRY 0x01
65#define FSL_SIP_HAB_EXIT 0x02
66#define FSL_SIP_HAB_REPORT_EVENT 0x03
67#define FSL_SIP_HAB_REPORT_STATUS 0x04
68#define FSL_SIP_HAB_FAILSAFE 0x05
69#define FSL_SIP_HAB_CHECK_TARGET 0x06
70static volatile gd_t *gd_save;
71#endif
72
73static inline void save_gd(void)
74{
75#ifdef CONFIG_ARM64
76 gd_save = gd;
77#endif
78}
79
80static inline void restore_gd(void)
81{
82#ifdef CONFIG_ARM64
83 /*
84 * Make will already error that reserving x18 is not supported at the
85 * time of writing, clang: error: unknown argument: '-ffixed-x18'
86 */
87 __asm__ volatile("mov x18, %0\n" : : "r" (gd_save));
88#endif
89}
90
91enum hab_status hab_rvt_report_event(enum hab_status status, u32 index,
92 u8 *event, size_t *bytes)
93{
94 enum hab_status ret;
95 hab_rvt_report_event_t *hab_rvt_report_event_func;
96 struct arm_smccc_res res __maybe_unused;
97
98 hab_rvt_report_event_func = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT;
99#if defined(CONFIG_ARM64)
100 if (current_el() != 3) {
101 /* call sip */
102 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_EVENT, (unsigned long)index,
103 (unsigned long)event, (unsigned long)bytes, 0, 0, 0, &res);
104 return (enum hab_status)res.a0;
105 }
106#endif
107
108 save_gd();
109 ret = hab_rvt_report_event_func(status, index, event, bytes);
110 restore_gd();
111
112 return ret;
113
114}
115
116enum hab_status hab_rvt_report_status(enum hab_config *config, enum hab_state *state)
117{
118 enum hab_status ret;
119 hab_rvt_report_status_t *hab_rvt_report_status_func;
120 struct arm_smccc_res res __maybe_unused;
121
122 hab_rvt_report_status_func = (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS;
123#if defined(CONFIG_ARM64)
124 if (current_el() != 3) {
125 /* call sip */
126 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_STATUS, (unsigned long)config,
127 (unsigned long)state, 0, 0, 0, 0, &res);
128 return (enum hab_status)res.a0;
129 }
130#endif
131
132 save_gd();
133 ret = hab_rvt_report_status_func(config, state);
134 restore_gd();
135
136 return ret;
137}
138
139enum hab_status hab_rvt_entry(void)
140{
141 enum hab_status ret;
142 hab_rvt_entry_t *hab_rvt_entry_func;
143 struct arm_smccc_res res __maybe_unused;
144
145 hab_rvt_entry_func = (hab_rvt_entry_t *)HAB_RVT_ENTRY;
146#if defined(CONFIG_ARM64)
147 if (current_el() != 3) {
148 /* call sip */
149 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_ENTRY, 0, 0, 0, 0, 0, 0, &res);
150 return (enum hab_status)res.a0;
151 }
152#endif
153
154 save_gd();
155 ret = hab_rvt_entry_func();
156 restore_gd();
157
158 return ret;
159}
160
161enum hab_status hab_rvt_exit(void)
162{
163 enum hab_status ret;
164 hab_rvt_exit_t *hab_rvt_exit_func;
165 struct arm_smccc_res res __maybe_unused;
166
167 hab_rvt_exit_func = (hab_rvt_exit_t *)HAB_RVT_EXIT;
168#if defined(CONFIG_ARM64)
169 if (current_el() != 3) {
170 /* call sip */
171 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_EXIT, 0, 0, 0, 0, 0, 0, &res);
172 return (enum hab_status)res.a0;
173 }
174#endif
175
176 save_gd();
177 ret = hab_rvt_exit_func();
178 restore_gd();
179
180 return ret;
181}
182
183void hab_rvt_failsafe(void)
184{
185 hab_rvt_failsafe_t *hab_rvt_failsafe_func;
186
187 hab_rvt_failsafe_func = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE;
188#if defined(CONFIG_ARM64)
189 if (current_el() != 3) {
190 /* call sip */
191 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_FAILSAFE, 0, 0, 0, 0, 0, 0, NULL);
192 return;
193 }
194#endif
195
196 save_gd();
197 hab_rvt_failsafe_func();
198 restore_gd();
199}
200
201enum hab_status hab_rvt_check_target(enum hab_target type, const void *start,
202 size_t bytes)
203{
204 enum hab_status ret;
205 hab_rvt_check_target_t *hab_rvt_check_target_func;
206 struct arm_smccc_res res __maybe_unused;
207
208 hab_rvt_check_target_func = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET;
209#if defined(CONFIG_ARM64)
210 if (current_el() != 3) {
211 /* call sip */
212 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_CHECK_TARGET, (unsigned long)type,
213 (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res);
214 return (enum hab_status)res.a0;
215 }
216#endif
217
218 save_gd();
219 ret = hab_rvt_check_target_func(type, start, bytes);
220 restore_gd();
221
222 return ret;
223}
224
225void *hab_rvt_authenticate_image(uint8_t cid, ptrdiff_t ivt_offset,
226 void **start, size_t *bytes, hab_loader_callback_f_t loader)
227{
228 void *ret;
229 hab_rvt_authenticate_image_t *hab_rvt_authenticate_image_func;
230 struct arm_smccc_res res __maybe_unused;
231
232 hab_rvt_authenticate_image_func = (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE;
233#if defined(CONFIG_ARM64)
234 if (current_el() != 3) {
235 /* call sip */
236 arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_AUTHENTICATE, (unsigned long)ivt_offset,
237 (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res);
238 return (void *)res.a0;
239 }
240#endif
241
242 save_gd();
243 ret = hab_rvt_authenticate_image_func(cid, ivt_offset, start, bytes, loader);
244 restore_gd();
245
246 return ret;
247}
248
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100249#if !defined(CONFIG_SPL_BUILD)
250
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500251#define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
252
253struct record {
254 uint8_t tag; /* Tag */
255 uint8_t len[2]; /* Length */
256 uint8_t par; /* Version */
257 uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
258 bool any_rec_flag;
259};
260
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000261static char *rsn_str[] = {
262 "RSN = HAB_RSN_ANY (0x00)\n",
263 "RSN = HAB_ENG_FAIL (0x30)\n",
264 "RSN = HAB_INV_ADDRESS (0x22)\n",
265 "RSN = HAB_INV_ASSERTION (0x0C)\n",
266 "RSN = HAB_INV_CALL (0x28)\n",
267 "RSN = HAB_INV_CERTIFICATE (0x21)\n",
268 "RSN = HAB_INV_COMMAND (0x06)\n",
269 "RSN = HAB_INV_CSF (0x11)\n",
270 "RSN = HAB_INV_DCD (0x27)\n",
271 "RSN = HAB_INV_INDEX (0x0F)\n",
272 "RSN = HAB_INV_IVT (0x05)\n",
273 "RSN = HAB_INV_KEY (0x1D)\n",
274 "RSN = HAB_INV_RETURN (0x1E)\n",
275 "RSN = HAB_INV_SIGNATURE (0x18)\n",
276 "RSN = HAB_INV_SIZE (0x17)\n",
277 "RSN = HAB_MEM_FAIL (0x2E)\n",
278 "RSN = HAB_OVR_COUNT (0x2B)\n",
279 "RSN = HAB_OVR_STORAGE (0x2D)\n",
280 "RSN = HAB_UNS_ALGORITHM (0x12)\n",
281 "RSN = HAB_UNS_COMMAND (0x03)\n",
282 "RSN = HAB_UNS_ENGINE (0x0A)\n",
283 "RSN = HAB_UNS_ITEM (0x24)\n",
284 "RSN = HAB_UNS_KEY (0x1B)\n",
285 "RSN = HAB_UNS_PROTOCOL (0x14)\n",
286 "RSN = HAB_UNS_STATE (0x09)\n",
287 "RSN = INVALID\n",
288 NULL
289};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500290
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000291static char *sts_str[] = {
Ye Li236ef9d2023-06-15 18:09:15 +0800292 "STS = HAB_STS_ANY (0x00)\n",
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000293 "STS = HAB_FAILURE (0x33)\n",
294 "STS = HAB_WARNING (0x69)\n",
Ye Li236ef9d2023-06-15 18:09:15 +0800295 "STS = HAB_SUCCESS (0xF0)\n",
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000296 "STS = INVALID\n",
297 NULL
298};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500299
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000300static char *eng_str[] = {
301 "ENG = HAB_ENG_ANY (0x00)\n",
302 "ENG = HAB_ENG_SCC (0x03)\n",
303 "ENG = HAB_ENG_RTIC (0x05)\n",
304 "ENG = HAB_ENG_SAHARA (0x06)\n",
305 "ENG = HAB_ENG_CSU (0x0A)\n",
306 "ENG = HAB_ENG_SRTC (0x0C)\n",
307 "ENG = HAB_ENG_DCP (0x1B)\n",
308 "ENG = HAB_ENG_CAAM (0x1D)\n",
309 "ENG = HAB_ENG_SNVS (0x1E)\n",
310 "ENG = HAB_ENG_OCOTP (0x21)\n",
311 "ENG = HAB_ENG_DTCP (0x22)\n",
312 "ENG = HAB_ENG_ROM (0x36)\n",
313 "ENG = HAB_ENG_HDCP (0x24)\n",
314 "ENG = HAB_ENG_RTL (0x77)\n",
315 "ENG = HAB_ENG_SW (0xFF)\n",
316 "ENG = INVALID\n",
317 NULL
318};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500319
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000320static char *ctx_str[] = {
321 "CTX = HAB_CTX_ANY(0x00)\n",
322 "CTX = HAB_CTX_FAB (0xFF)\n",
323 "CTX = HAB_CTX_ENTRY (0xE1)\n",
324 "CTX = HAB_CTX_TARGET (0x33)\n",
325 "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
326 "CTX = HAB_CTX_DCD (0xDD)\n",
327 "CTX = HAB_CTX_CSF (0xCF)\n",
328 "CTX = HAB_CTX_COMMAND (0xC0)\n",
329 "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
330 "CTX = HAB_CTX_ASSERT (0xA0)\n",
331 "CTX = HAB_CTX_EXIT (0xEE)\n",
332 "CTX = INVALID\n",
333 NULL
334};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500335
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000336static uint8_t hab_statuses[5] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500337 HAB_STS_ANY,
338 HAB_FAILURE,
339 HAB_WARNING,
Ye Li236ef9d2023-06-15 18:09:15 +0800340 HAB_SUCCESS
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500341};
342
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000343static uint8_t hab_reasons[26] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500344 HAB_RSN_ANY,
345 HAB_ENG_FAIL,
346 HAB_INV_ADDRESS,
347 HAB_INV_ASSERTION,
348 HAB_INV_CALL,
349 HAB_INV_CERTIFICATE,
350 HAB_INV_COMMAND,
351 HAB_INV_CSF,
352 HAB_INV_DCD,
353 HAB_INV_INDEX,
354 HAB_INV_IVT,
355 HAB_INV_KEY,
356 HAB_INV_RETURN,
357 HAB_INV_SIGNATURE,
358 HAB_INV_SIZE,
359 HAB_MEM_FAIL,
360 HAB_OVR_COUNT,
361 HAB_OVR_STORAGE,
362 HAB_UNS_ALGORITHM,
363 HAB_UNS_COMMAND,
364 HAB_UNS_ENGINE,
365 HAB_UNS_ITEM,
366 HAB_UNS_KEY,
367 HAB_UNS_PROTOCOL,
Ye Li236ef9d2023-06-15 18:09:15 +0800368 HAB_UNS_STATE
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500369};
370
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000371static uint8_t hab_contexts[12] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500372 HAB_CTX_ANY,
373 HAB_CTX_FAB,
374 HAB_CTX_ENTRY,
375 HAB_CTX_TARGET,
376 HAB_CTX_AUTHENTICATE,
377 HAB_CTX_DCD,
378 HAB_CTX_CSF,
379 HAB_CTX_COMMAND,
380 HAB_CTX_AUT_DAT,
381 HAB_CTX_ASSERT,
Ye Li236ef9d2023-06-15 18:09:15 +0800382 HAB_CTX_EXIT
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500383};
384
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000385static uint8_t hab_engines[16] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500386 HAB_ENG_ANY,
387 HAB_ENG_SCC,
388 HAB_ENG_RTIC,
389 HAB_ENG_SAHARA,
390 HAB_ENG_CSU,
391 HAB_ENG_SRTC,
392 HAB_ENG_DCP,
393 HAB_ENG_CAAM,
394 HAB_ENG_SNVS,
395 HAB_ENG_OCOTP,
396 HAB_ENG_DTCP,
397 HAB_ENG_ROM,
398 HAB_ENG_HDCP,
399 HAB_ENG_RTL,
Ye Li236ef9d2023-06-15 18:09:15 +0800400 HAB_ENG_SW
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500401};
402
Ye Li236ef9d2023-06-15 18:09:15 +0800403static inline u32 get_idx(u8 *list, u8 tgt, u32 size)
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500404{
Ye Li236ef9d2023-06-15 18:09:15 +0800405 u32 idx = 0;
406 u8 element;
407
408 while (idx < size) {
409 element = list[idx];
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500410 if (element == tgt)
411 return idx;
Ye Li236ef9d2023-06-15 18:09:15 +0800412 ++idx;
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500413 }
Ye Li236ef9d2023-06-15 18:09:15 +0800414 return idx;
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500415}
416
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000417static void process_event_record(uint8_t *event_data, size_t bytes)
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500418{
419 struct record *rec = (struct record *)event_data;
420
Ye Li236ef9d2023-06-15 18:09:15 +0800421 printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0],
422 ARRAY_SIZE(hab_statuses))]);
423 printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1],
424 ARRAY_SIZE(hab_reasons))]);
425 printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2],
426 ARRAY_SIZE(hab_contexts))]);
427 printf("%s", eng_str[get_idx(hab_engines, rec->contents[3],
428 ARRAY_SIZE(hab_engines))]);
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500429}
430
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000431static void display_event(uint8_t *event_data, size_t bytes)
Stefano Babic83fd8582013-06-28 00:20:21 +0200432{
433 uint32_t i;
434
435 if (!(event_data && bytes > 0))
436 return;
437
438 for (i = 0; i < bytes; i++) {
439 if (i == 0)
440 printf("\t0x%02x", event_data[i]);
441 else if ((i % 8) == 0)
442 printf("\n\t0x%02x", event_data[i]);
443 else
444 printf(" 0x%02x", event_data[i]);
445 }
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500446
447 process_event_record(event_data, bytes);
Stefano Babic83fd8582013-06-28 00:20:21 +0200448}
449
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000450static int get_hab_status(void)
Stefano Babic83fd8582013-06-28 00:20:21 +0200451{
452 uint32_t index = 0; /* Loop index */
453 uint8_t event_data[128]; /* Event data buffer */
454 size_t bytes = sizeof(event_data); /* Event size in bytes */
455 enum hab_config config = 0;
456 enum hab_state state = 0;
Stefano Babic83fd8582013-06-28 00:20:21 +0200457
Bryan O'Donoghue899cab02018-01-12 12:40:14 +0000458 if (imx_hab_is_enabled())
Stefano Babic83fd8582013-06-28 00:20:21 +0200459 puts("\nSecure boot enabled\n");
460 else
461 puts("\nSecure boot disabled\n");
462
463 /* Check HAB status */
464 if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
465 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
466 config, state);
467
Utkarsh Gupta1115bed2021-03-25 17:30:11 +0800468 /* Display HAB events */
469 while (hab_rvt_report_event(HAB_STS_ANY, index, event_data,
Stefano Babic83fd8582013-06-28 00:20:21 +0200470 &bytes) == HAB_SUCCESS) {
471 puts("\n");
472 printf("--------- HAB Event %d -----------------\n",
473 index + 1);
474 puts("event data:\n");
475 display_event(event_data, bytes);
476 puts("\n");
477 bytes = sizeof(event_data);
478 index++;
479 }
480 }
481 /* Display message if no HAB events are found */
482 else {
483 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
484 config, state);
485 puts("No HAB Events Found!\n\n");
486 }
487 return 0;
488}
489
Breno Lima3adb6f02021-03-25 17:30:13 +0800490#ifdef CONFIG_MX7ULP
491
492static int get_record_len(struct record *rec)
493{
494 return (size_t)((rec->len[0] << 8) + (rec->len[1]));
495}
496
497static int get_hab_status_m4(void)
498{
499 unsigned int index = 0;
500 uint8_t event_data[128];
501 size_t record_len, offset = 0;
502 enum hab_config config = 0;
503 enum hab_state state = 0;
504
505 if (imx_hab_is_enabled())
506 puts("\nSecure boot enabled\n");
507 else
508 puts("\nSecure boot disabled\n");
509
510 /*
511 * HAB in both A7 and M4 gather the security state
512 * and configuration of the chip from
513 * shared SNVS module
514 */
515 hab_rvt_report_status(&config, &state);
516 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
517 config, state);
518
519 struct record *rec = (struct record *)(HAB_M4_PERSISTENT_START);
520
521 record_len = get_record_len(rec);
522
523 /* Check if HAB persistent memory is valid */
524 if (rec->tag != HAB_TAG_EVT_DEF ||
525 record_len != sizeof(struct evt_def) ||
526 (rec->par & HAB_MAJ_MASK) != HAB_MAJ_VER) {
527 puts("\nERROR: Invalid HAB persistent memory\n");
528 return 1;
529 }
530
531 /* Parse events in HAB M4 persistent memory region */
532 while (offset < HAB_M4_PERSISTENT_BYTES) {
533 rec = (struct record *)(HAB_M4_PERSISTENT_START + offset);
534
535 record_len = get_record_len(rec);
536
537 if (rec->tag == HAB_TAG_EVT) {
538 memcpy(&event_data, rec, record_len);
539 puts("\n");
540 printf("--------- HAB Event %d -----------------\n",
541 index + 1);
542 puts("event data:\n");
543 display_event(event_data, record_len);
544 puts("\n");
545 index++;
546 }
547
548 offset += record_len;
549
550 /* Ensure all records start on a word boundary */
551 if ((offset % 4) != 0)
552 offset = offset + (4 - (offset % 4));
553 }
554
555 if (!index)
556 puts("No HAB Events Found!\n\n");
557
558 return 0;
559}
560#endif
561
Simon Glassed38aef2020-05-10 11:40:03 -0600562static int do_hab_status(struct cmd_tbl *cmdtp, int flag, int argc,
563 char *const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100564{
Breno Lima3adb6f02021-03-25 17:30:13 +0800565#ifdef CONFIG_MX7ULP
566 if ((argc > 2)) {
567 cmd_usage(cmdtp);
568 return 1;
569 }
570
571 if (strcmp("m4", argv[1]) == 0)
572 get_hab_status_m4();
573 else
574 get_hab_status();
575#else
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100576 if ((argc != 1)) {
577 cmd_usage(cmdtp);
578 return 1;
579 }
580
581 get_hab_status();
Breno Lima3adb6f02021-03-25 17:30:13 +0800582#endif
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100583
584 return 0;
585}
586
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100587static ulong get_image_ivt_offset(ulong img_addr)
588{
589 const void *buf;
590
591 buf = map_sysmem(img_addr, 0);
592 switch (genimg_get_format(buf)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400593#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100594 case IMAGE_FORMAT_LEGACY:
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600595 return (image_get_image_size((struct legacy_img_hdr *)img_addr)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100596 + 0x1000 - 1) & ~(0x1000 - 1);
597#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600598#if CONFIG_IS_ENABLED(FIT)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100599 case IMAGE_FORMAT_FIT:
600 return (fit_get_size(buf) + 0x1000 - 1) & ~(0x1000 - 1);
601#endif
602 default:
603 return 0;
604 }
605}
606
Simon Glassed38aef2020-05-10 11:40:03 -0600607static int do_authenticate_image(struct cmd_tbl *cmdtp, int flag, int argc,
608 char *const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100609{
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000610 ulong addr, length, ivt_offset;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100611 int rcode = 0;
612
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100613 if (argc < 3)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100614 return CMD_RET_USAGE;
615
Simon Glass3ff49ec2021-07-24 09:03:29 -0600616 addr = hextoul(argv[1], NULL);
617 length = hextoul(argv[2], NULL);
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100618 if (argc == 3)
619 ivt_offset = get_image_ivt_offset(addr);
620 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600621 ivt_offset = hextoul(argv[3], NULL);
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100622
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000623 rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000624 if (rcode == 0)
625 rcode = CMD_RET_SUCCESS;
626 else
627 rcode = CMD_RET_FAILURE;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000628
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100629 return rcode;
630}
631
Simon Glassed38aef2020-05-10 11:40:03 -0600632static int do_hab_failsafe(struct cmd_tbl *cmdtp, int flag, int argc,
633 char *const argv[])
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000634{
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000635 if (argc != 1) {
636 cmd_usage(cmdtp);
637 return 1;
638 }
639
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000640 hab_rvt_failsafe();
641
642 return 0;
643}
644
Simon Glassed38aef2020-05-10 11:40:03 -0600645static int do_hab_version(struct cmd_tbl *cmdtp, int flag, int argc,
646 char *const argv[])
Sjoerd Simons27f3fae2019-06-18 23:08:59 +0200647{
648 struct hab_hdr *hdr = (struct hab_hdr *)HAB_RVT_BASE;
649
650 if (hdr->tag != HAB_TAG_RVT) {
651 printf("Unexpected header tag: %x\n", hdr->tag);
652 return CMD_RET_FAILURE;
653 }
654
655 printf("HAB version: %d.%d\n", hdr->par >> 4, hdr->par & 0xf);
656
657 return 0;
658}
659
Simon Glassed38aef2020-05-10 11:40:03 -0600660static int do_authenticate_image_or_failover(struct cmd_tbl *cmdtp, int flag,
661 int argc, char *const argv[])
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100662{
663 int ret = CMD_RET_FAILURE;
664
Eduard Strehlau3552d662023-09-06 22:47:25 -0300665 if (argc < 3) {
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100666 ret = CMD_RET_USAGE;
667 goto error;
668 }
669
670 if (!imx_hab_is_enabled()) {
671 printf("error: secure boot disabled\n");
672 goto error;
673 }
674
675 if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) {
676 fprintf(stderr, "authentication fail -> %s %s %s %s\n",
677 argv[0], argv[1], argv[2], argv[3]);
678 do_hab_failsafe(0, 0, 1, NULL);
679 };
680 ret = CMD_RET_SUCCESS;
681error:
682 return ret;
683}
684
Breno Lima3adb6f02021-03-25 17:30:13 +0800685#ifdef CONFIG_MX7ULP
686U_BOOT_CMD(
687 hab_status, CONFIG_SYS_MAXARGS, 2, do_hab_status,
688 "display HAB status and events",
689 "hab_status - A7 HAB event and status\n"
690 "hab_status m4 - M4 HAB event and status"
691 );
692#else
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100693U_BOOT_CMD(
694 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
695 "display HAB status",
696 ""
697 );
Breno Lima3adb6f02021-03-25 17:30:13 +0800698#endif
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100699
700U_BOOT_CMD(
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000701 hab_auth_img, 4, 0, do_authenticate_image,
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100702 "authenticate image via HAB",
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000703 "addr length ivt_offset\n"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100704 "addr - image hex address\n"
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000705 "length - image hex length\n"
Fabio Estevam76cda332023-09-06 22:47:27 -0300706 "ivt_offset - hex offset of IVT in the image (optional)"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100707 );
708
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000709U_BOOT_CMD(
710 hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
711 "run BootROM failsafe routine",
712 ""
713 );
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100714
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100715U_BOOT_CMD(
716 hab_auth_img_or_fail, 4, 0,
717 do_authenticate_image_or_failover,
Fabio Estevam7014d9d2023-09-06 22:47:26 -0300718 "authenticate image via HAB. Switch to USB BootROM mode on failure",
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100719 "addr length ivt_offset\n"
720 "addr - image hex address\n"
721 "length - image hex length\n"
Fabio Estevam76cda332023-09-06 22:47:27 -0300722 "ivt_offset - hex offset of IVT in the image (optional)"
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100723 );
724
Sjoerd Simons27f3fae2019-06-18 23:08:59 +0200725U_BOOT_CMD(
726 hab_version, 1, 0, do_hab_version,
727 "print HAB major/minor version",
728 ""
729 );
730
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100731#endif /* !defined(CONFIG_SPL_BUILD) */
732
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000733/* Get CSF Header length */
734static int get_hab_hdr_len(struct hab_hdr *hdr)
735{
736 return (size_t)((hdr->len[0] << 8) + (hdr->len[1]));
737}
738
739/* Check whether addr lies between start and
740 * end and is within the length of the image
741 */
742static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end)
743{
744 size_t csf_size = (size_t)((end + 1) - addr);
745
746 return (addr && (addr >= start) && (addr <= end) &&
747 (csf_size >= bytes));
748}
749
750/* Get Length of each command in CSF */
751static int get_csf_cmd_hdr_len(u8 *csf_hdr)
752{
753 if (*csf_hdr == HAB_CMD_HDR)
754 return sizeof(struct hab_hdr);
755
756 return get_hab_hdr_len((struct hab_hdr *)csf_hdr);
757}
758
759/* Check if CSF is valid */
760static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes)
761{
762 u8 *start = (u8 *)start_addr;
763 u8 *csf_hdr;
764 u8 *end;
765
766 size_t csf_hdr_len;
767 size_t cmd_hdr_len;
768 size_t offset = 0;
769
770 if (bytes != 0)
771 end = start + bytes - 1;
772 else
773 end = start;
774
775 /* Verify if CSF pointer content is zero */
776 if (!ivt->csf) {
777 puts("Error: CSF pointer is NULL\n");
778 return false;
779 }
780
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800781 csf_hdr = (u8 *)(ulong)ivt->csf;
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000782
783 /* Verify if CSF Header exist */
784 if (*csf_hdr != HAB_CMD_HDR) {
785 puts("Error: CSF header command not found\n");
786 return false;
787 }
788
789 csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr);
790
791 /* Check if the CSF lies within the image bounds */
792 if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) {
793 puts("Error: CSF lies outside the image bounds\n");
794 return false;
795 }
796
797 do {
Utkarsh Gupta09af35c2018-02-20 01:19:25 +0000798 struct hab_hdr *cmd;
799
800 cmd = (struct hab_hdr *)&csf_hdr[offset];
801
802 switch (cmd->tag) {
803 case (HAB_CMD_WRT_DAT):
804 puts("Error: Deprecated write command found\n");
805 return false;
806 case (HAB_CMD_CHK_DAT):
807 puts("Error: Deprecated check command found\n");
808 return false;
809 case (HAB_CMD_SET):
810 if (cmd->par == HAB_PAR_MID) {
811 puts("Error: Deprecated Set MID command found\n");
812 return false;
813 }
814 default:
815 break;
816 }
817
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000818 cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]);
819 if (!cmd_hdr_len) {
820 puts("Error: Invalid command length\n");
821 return false;
822 }
823 offset += cmd_hdr_len;
824
825 } while (offset < csf_hdr_len);
826
827 return true;
828}
829
Utkarsh Guptaaac4ab52021-03-25 17:30:08 +0800830/*
831 * Validate IVT structure of the image being authenticated
832 */
833static int validate_ivt(struct ivt *ivt_initial)
834{
835 struct ivt_header *ivt_hdr = &ivt_initial->hdr;
836
837 if ((ulong)ivt_initial & 0x3) {
838 puts("Error: Image's start address is not 4 byte aligned\n");
839 return 0;
840 }
841
842 /* Check IVT fields before allowing authentication */
843 if ((!verify_ivt_header(ivt_hdr)) && \
844 (ivt_initial->entry != 0x0) && \
845 (ivt_initial->reserved1 == 0x0) && \
846 (ivt_initial->self == \
847 (uint32_t)((ulong)ivt_initial & 0xffffffff)) && \
848 (ivt_initial->csf != 0x0) && \
849 (ivt_initial->reserved2 == 0x0)) {
850 /* Report boot failure if DCD pointer is found in IVT */
851 if (ivt_initial->dcd != 0x0)
852 puts("Error: DCD pointer must be 0\n");
853 else
854 return 1;
855 }
856
857 puts("Error: Invalid IVT structure\n");
858 debug("\nAllowed IVT structure:\n");
859 debug("IVT HDR = 0x4X2000D1\n");
860 debug("IVT ENTRY = 0xXXXXXXXX\n");
861 debug("IVT RSV1 = 0x0\n");
862 debug("IVT DCD = 0x0\n"); /* Recommended */
863 debug("IVT BOOT_DATA = 0xXXXXXXXX\n"); /* Commonly 0x0 */
864 debug("IVT SELF = 0xXXXXXXXX\n"); /* = ddr_start + ivt_offset */
865 debug("IVT CSF = 0xXXXXXXXX\n");
866 debug("IVT RSV2 = 0x0\n");
867
868 /* Invalid IVT structure */
869 return 0;
870}
871
Bryan O'Donoghue47b3f342018-01-12 12:40:16 +0000872bool imx_hab_is_enabled(void)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100873{
874 struct imx_sec_config_fuse_t *fuse =
875 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
876 uint32_t reg;
877 int ret;
878
879 ret = fuse_read(fuse->bank, fuse->word, &reg);
880 if (ret) {
881 puts("\nSecure boot fuse read error\n");
882 return ret;
883 }
884
885 return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
886}
887
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000888int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
889 uint32_t ivt_offset)
Nitin Gargb1ce7012014-09-16 13:33:25 -0500890{
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800891 ulong load_addr = 0;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500892 size_t bytes;
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800893 ulong ivt_addr = 0;
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000894 int result = 1;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500895 ulong start;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000896 struct ivt *ivt;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000897 enum hab_status status;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500898
Breno Lima5deb9302021-03-25 17:30:10 +0800899 if (!imx_hab_is_enabled())
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000900 puts("hab fuse not enabled\n");
Nitin Gargb1ce7012014-09-16 13:33:25 -0500901
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000902 printf("\nAuthenticate image from DDR location 0x%x...\n",
903 ddr_start);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500904
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000905 hab_caam_clock_enable(1);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500906
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000907 /* Calculate IVT address header */
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800908 ivt_addr = (ulong) (ddr_start + ivt_offset);
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000909 ivt = (struct ivt *)ivt_addr;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000910
911 /* Verify IVT header bugging out on error */
Utkarsh Guptaaac4ab52021-03-25 17:30:08 +0800912 if (!validate_ivt(ivt))
Breno Lima52d389c2018-02-20 01:19:22 +0000913 goto hab_authentication_exit;
Utkarsh Guptaf79f75e2018-02-20 01:19:23 +0000914
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000915 start = ddr_start;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000916 bytes = image_size;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000917
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000918 /* Verify CSF */
919 if (!csf_is_valid(ivt, start, bytes))
920 goto hab_authentication_exit;
921
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000922 if (hab_rvt_entry() != HAB_SUCCESS) {
923 puts("hab entry function fail\n");
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000924 goto hab_exit_failure_print_status;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000925 }
926
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800927 status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)(ulong)ddr_start, bytes);
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000928 if (status != HAB_SUCCESS) {
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800929 printf("HAB check target 0x%08x-0x%08lx fail\n",
Breno Lima9807f552021-03-25 17:30:14 +0800930 ddr_start, ddr_start + (ulong)bytes);
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000931 goto hab_exit_failure_print_status;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000932 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500933#ifdef DEBUG
Breno Lima9807f552021-03-25 17:30:14 +0800934 printf("\nivt_offset = 0x%x, ivt addr = 0x%lx\n", ivt_offset, ivt_addr);
Bryan O'Donoghueeb031502018-01-12 12:40:07 +0000935 printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
936 ivt->dcd, ivt->csf);
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000937 puts("Dumping IVT\n");
Marek Vasut18eceae2023-05-29 01:29:20 +0200938 print_buffer(ivt_addr, (void *)(uintptr_t)(ivt_addr), 4, 0x8, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500939
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000940 puts("Dumping CSF Header\n");
Marek Vasut18eceae2023-05-29 01:29:20 +0200941 print_buffer(ivt->csf, (void *)(uintptr_t)(ivt->csf), 4, 0x10, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500942
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100943#if !defined(CONFIG_SPL_BUILD)
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000944 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100945#endif
Nitin Gargb1ce7012014-09-16 13:33:25 -0500946
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000947 puts("\nCalling authenticate_image in ROM\n");
948 printf("\tivt_offset = 0x%x\n", ivt_offset);
949 printf("\tstart = 0x%08lx\n", start);
Marek Vasut18eceae2023-05-29 01:29:20 +0200950 printf("\tbytes = 0x%lx\n", (ulong)bytes);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500951#endif
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800952
953#ifndef CONFIG_ARM64
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000954 /*
955 * If the MMU is enabled, we have to notify the ROM
956 * code, or it won't flush the caches when needed.
957 * This is done, by setting the "pu_irom_mmu_enabled"
958 * word to 1. You can find its address by looking in
959 * the ROM map. This is critical for
960 * authenticate_image(). If MMU is enabled, without
961 * setting this bit, authentication will fail and may
962 * crash.
963 */
964 /* Check MMU enabled */
965 if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
966 if (is_mx6dq()) {
967 /*
968 * This won't work on Rev 1.0.0 of
969 * i.MX6Q/D, since their ROM doesn't
970 * do cache flushes. don't think any
971 * exist, so we ignore them.
972 */
973 if (!is_mx6dqp())
974 writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
975 } else if (is_mx6sdl()) {
976 writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
977 } else if (is_mx6sl()) {
978 writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000979 }
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000980 }
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800981#endif
Nitin Gargb1ce7012014-09-16 13:33:25 -0500982
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800983 load_addr = (ulong)hab_rvt_authenticate_image(
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000984 HAB_CID_UBOOT,
985 ivt_offset, (void **)&start,
986 (size_t *)&bytes, NULL);
987 if (hab_rvt_exit() != HAB_SUCCESS) {
988 puts("hab exit function fail\n");
989 load_addr = 0;
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000990 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500991
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000992hab_exit_failure_print_status:
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100993#if !defined(CONFIG_SPL_BUILD)
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000994 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100995#endif
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000996
Breno Lima52d389c2018-02-20 01:19:22 +0000997hab_authentication_exit:
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000998
Breno Lima5deb9302021-03-25 17:30:10 +0800999 if (load_addr != 0 || !imx_hab_is_enabled())
Bryan O'Donoghue79987a12018-01-12 12:39:56 +00001000 result = 0;
Nitin Gargb1ce7012014-09-16 13:33:25 -05001001
1002 return result;
1003}
Ye Lif66fd7b2021-03-25 17:30:06 +08001004
1005int authenticate_image(u32 ddr_start, u32 raw_image_size)
1006{
1007 u32 ivt_offset;
1008 size_t bytes;
1009
1010 ivt_offset = (raw_image_size + ALIGN_SIZE - 1) &
1011 ~(ALIGN_SIZE - 1);
1012 bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE;
1013
1014 return imx_hab_authenticate_image(ddr_start, bytes, ivt_offset);
1015}