blob: 600092389a327ab9c78929addadeb85240e7a1cf [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
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -05007#include <config.h>
Simon Glass1ab16922022-07-31 12:28:48 -06008#include <display_options.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -05009#include <fuse.h>
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +010010#include <mapmem.h>
11#include <image.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020012#include <asm/io.h>
Peng Fan4f3cf8f2021-03-25 17:30:07 +080013#include <asm/global_data.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050014#include <asm/system.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050015#include <asm/arch/clock.h>
Stefano Babic14404422014-06-10 10:26:22 +020016#include <asm/arch/sys_proto.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020017#include <asm/mach-imx/hab.h>
Peng Fan4f3cf8f2021-03-25 17:30:07 +080018#include <linux/arm-smccc.h>
19
20DECLARE_GLOBAL_DATA_PTR;
Stefano Babic83fd8582013-06-28 00:20:21 +020021
Nitin Gargb1ce7012014-09-16 13:33:25 -050022#define ALIGN_SIZE 0x1000
Nitin Gargb1ce7012014-09-16 13:33:25 -050023#define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
24#define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
Breno Matheus Lima25d93482019-05-07 20:19:17 +000025#define MX6SL_PU_IROM_MMU_EN_VAR 0x00901c60
Adrian Alonso00a0b562015-10-12 13:48:15 -050026#define IS_HAB_ENABLED_BIT \
Peng Fana26ba6d2017-02-22 16:21:53 +080027 (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
Peng Fan4f3cf8f2021-03-25 17:30:07 +080028 ((is_soc_type(MXC_SOC_MX7) || is_soc_type(MXC_SOC_IMX8M)) ? 0x2000000 : 0x2))
Paul Geurtsfea40042024-11-01 09:49:21 +010029#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)
Nitin Gargb1ce7012014-09-16 13:33:25 -050037
Breno Lima3adb6f02021-03-25 17:30:13 +080038#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
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +000044static 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
52static 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
Breno Limad03a4d32021-03-25 17:30:12 +080062 if ((ivt_hdr->version & HAB_MAJ_MASK) != HAB_MAJ_VER)
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +000063 result = ivt_header_error("bad version", ivt_hdr);
64
65 return result;
66}
67
Peng Fan4f3cf8f2021-03-25 17:30:07 +080068#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
77static volatile gd_t *gd_save;
78#endif
79
80static inline void save_gd(void)
81{
82#ifdef CONFIG_ARM64
83 gd_save = gd;
84#endif
85}
86
87static 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
98enum 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
123enum 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
146enum 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
168enum 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
190void 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
208enum 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
232void *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
Simon Glass85ed77d2024-09-29 19:49:46 -0600256#if !defined(CONFIG_XPL_BUILD)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100257
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500258#define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
259
260struct 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
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000268static 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};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500297
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000298static char *sts_str[] = {
Ye Li236ef9d2023-06-15 18:09:15 +0800299 "STS = HAB_STS_ANY (0x00)\n",
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000300 "STS = HAB_FAILURE (0x33)\n",
301 "STS = HAB_WARNING (0x69)\n",
Ye Li236ef9d2023-06-15 18:09:15 +0800302 "STS = HAB_SUCCESS (0xF0)\n",
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000303 "STS = INVALID\n",
304 NULL
305};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500306
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000307static 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};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500326
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000327static 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};
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500342
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000343static uint8_t hab_statuses[5] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500344 HAB_STS_ANY,
345 HAB_FAILURE,
346 HAB_WARNING,
Ye Li236ef9d2023-06-15 18:09:15 +0800347 HAB_SUCCESS
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500348};
349
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000350static uint8_t hab_reasons[26] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500351 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,
Ye Li236ef9d2023-06-15 18:09:15 +0800375 HAB_UNS_STATE
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500376};
377
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000378static uint8_t hab_contexts[12] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500379 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,
Ye Li236ef9d2023-06-15 18:09:15 +0800389 HAB_CTX_EXIT
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500390};
391
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000392static uint8_t hab_engines[16] = {
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500393 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,
Ye Li236ef9d2023-06-15 18:09:15 +0800407 HAB_ENG_SW
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500408};
409
Ye Li236ef9d2023-06-15 18:09:15 +0800410static inline u32 get_idx(u8 *list, u8 tgt, u32 size)
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500411{
Ye Li236ef9d2023-06-15 18:09:15 +0800412 u32 idx = 0;
413 u8 element;
414
415 while (idx < size) {
416 element = list[idx];
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500417 if (element == tgt)
418 return idx;
Ye Li236ef9d2023-06-15 18:09:15 +0800419 ++idx;
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500420 }
Ye Li236ef9d2023-06-15 18:09:15 +0800421 return idx;
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500422}
423
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000424static void process_event_record(uint8_t *event_data, size_t bytes)
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500425{
426 struct record *rec = (struct record *)event_data;
427
Ye Li236ef9d2023-06-15 18:09:15 +0800428 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))]);
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500436}
437
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000438static void display_event(uint8_t *event_data, size_t bytes)
Stefano Babic83fd8582013-06-28 00:20:21 +0200439{
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 }
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500453
454 process_event_record(event_data, bytes);
Stefano Babic83fd8582013-06-28 00:20:21 +0200455}
456
Bryan O'Donoghuee92a5f92018-01-12 12:40:12 +0000457static int get_hab_status(void)
Stefano Babic83fd8582013-06-28 00:20:21 +0200458{
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;
Stefano Babic83fd8582013-06-28 00:20:21 +0200464
Bryan O'Donoghue899cab02018-01-12 12:40:14 +0000465 if (imx_hab_is_enabled())
Stefano Babic83fd8582013-06-28 00:20:21 +0200466 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
Utkarsh Gupta1115bed2021-03-25 17:30:11 +0800475 /* Display HAB events */
476 while (hab_rvt_report_event(HAB_STS_ANY, index, event_data,
Stefano Babic83fd8582013-06-28 00:20:21 +0200477 &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
Breno Lima3adb6f02021-03-25 17:30:13 +0800497#ifdef CONFIG_MX7ULP
498
499static int get_record_len(struct record *rec)
500{
501 return (size_t)((rec->len[0] << 8) + (rec->len[1]));
502}
503
504static 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
Simon Glassed38aef2020-05-10 11:40:03 -0600569static int do_hab_status(struct cmd_tbl *cmdtp, int flag, int argc,
570 char *const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100571{
Breno Lima3adb6f02021-03-25 17:30:13 +0800572#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
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100583 if ((argc != 1)) {
584 cmd_usage(cmdtp);
585 return 1;
586 }
587
588 get_hab_status();
Breno Lima3adb6f02021-03-25 17:30:13 +0800589#endif
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100590
591 return 0;
592}
593
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100594static 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)) {
Tom Rinic220bd92019-05-23 07:14:07 -0400600#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100601 case IMAGE_FORMAT_LEGACY:
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600602 return (image_get_image_size((struct legacy_img_hdr *)img_addr)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100603 + 0x1000 - 1) & ~(0x1000 - 1);
604#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600605#if CONFIG_IS_ENABLED(FIT)
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100606 case IMAGE_FORMAT_FIT:
607 return (fit_get_size(buf) + 0x1000 - 1) & ~(0x1000 - 1);
608#endif
609 default:
610 return 0;
611 }
612}
613
Simon Glassed38aef2020-05-10 11:40:03 -0600614static int do_authenticate_image(struct cmd_tbl *cmdtp, int flag, int argc,
615 char *const argv[])
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100616{
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000617 ulong addr, length, ivt_offset;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100618 int rcode = 0;
619
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100620 if (argc < 3)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100621 return CMD_RET_USAGE;
622
Simon Glass3ff49ec2021-07-24 09:03:29 -0600623 addr = hextoul(argv[1], NULL);
624 length = hextoul(argv[2], NULL);
Parthiban Nallathambi2c88aa62018-11-21 14:50:40 +0100625 if (argc == 3)
626 ivt_offset = get_image_ivt_offset(addr);
627 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600628 ivt_offset = hextoul(argv[3], NULL);
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100629
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000630 rcode = imx_hab_authenticate_image(addr, length, ivt_offset);
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000631 if (rcode == 0)
632 rcode = CMD_RET_SUCCESS;
633 else
634 rcode = CMD_RET_FAILURE;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000635
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100636 return rcode;
637}
638
Simon Glassed38aef2020-05-10 11:40:03 -0600639static int do_hab_failsafe(struct cmd_tbl *cmdtp, int flag, int argc,
640 char *const argv[])
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000641{
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000642 if (argc != 1) {
643 cmd_usage(cmdtp);
644 return 1;
645 }
646
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000647 hab_rvt_failsafe();
648
649 return 0;
650}
651
Simon Glassed38aef2020-05-10 11:40:03 -0600652static int do_hab_version(struct cmd_tbl *cmdtp, int flag, int argc,
653 char *const argv[])
Sjoerd Simons27f3fae2019-06-18 23:08:59 +0200654{
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
Simon Glassed38aef2020-05-10 11:40:03 -0600667static int do_authenticate_image_or_failover(struct cmd_tbl *cmdtp, int flag,
668 int argc, char *const argv[])
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100669{
670 int ret = CMD_RET_FAILURE;
671
Eduard Strehlau3552d662023-09-06 22:47:25 -0300672 if (argc < 3) {
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100673 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;
688error:
689 return ret;
690}
691
Breno Lima3adb6f02021-03-25 17:30:13 +0800692#ifdef CONFIG_MX7ULP
693U_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
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100700U_BOOT_CMD(
701 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
702 "display HAB status",
703 ""
704 );
Breno Lima3adb6f02021-03-25 17:30:13 +0800705#endif
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100706
707U_BOOT_CMD(
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000708 hab_auth_img, 4, 0, do_authenticate_image,
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100709 "authenticate image via HAB",
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000710 "addr length ivt_offset\n"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100711 "addr - image hex address\n"
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000712 "length - image hex length\n"
Fabio Estevam76cda332023-09-06 22:47:27 -0300713 "ivt_offset - hex offset of IVT in the image (optional)"
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100714 );
715
Bryan O'Donoghuefaa15022018-01-12 12:40:19 +0000716U_BOOT_CMD(
717 hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe,
718 "run BootROM failsafe routine",
719 ""
720 );
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100721
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100722U_BOOT_CMD(
723 hab_auth_img_or_fail, 4, 0,
724 do_authenticate_image_or_failover,
Fabio Estevam7014d9d2023-09-06 22:47:26 -0300725 "authenticate image via HAB. Switch to USB BootROM mode on failure",
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100726 "addr length ivt_offset\n"
727 "addr - image hex address\n"
728 "length - image hex length\n"
Fabio Estevam76cda332023-09-06 22:47:27 -0300729 "ivt_offset - hex offset of IVT in the image (optional)"
Bryan O'Donoghue933092f2018-03-26 15:36:46 +0100730 );
731
Sjoerd Simons27f3fae2019-06-18 23:08:59 +0200732U_BOOT_CMD(
733 hab_version, 1, 0, do_hab_version,
734 "print HAB major/minor version",
735 ""
736 );
737
Simon Glass85ed77d2024-09-29 19:49:46 -0600738#endif /* !defined(CONFIG_XPL_BUILD) */
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100739
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000740/* Get CSF Header length */
741static 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 */
749static 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 */
758static 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 */
767static 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
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800788 csf_hdr = (u8 *)(ulong)ivt->csf;
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000789
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 {
Utkarsh Gupta09af35c2018-02-20 01:19:25 +0000805 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
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000825 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
Utkarsh Guptaaac4ab52021-03-25 17:30:08 +0800837/*
838 * Validate IVT structure of the image being authenticated
839 */
840static 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
Bryan O'Donoghue47b3f342018-01-12 12:40:16 +0000879bool imx_hab_is_enabled(void)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100880{
Paul Geurtsfea40042024-11-01 09:49:21 +0100881 struct imx_fuse *sec_config =
Paul Geurtsdf0f95a2024-11-01 09:49:20 +0100882 (struct imx_fuse *)&imx_sec_config_fuse;
Paul Geurtsfea40042024-11-01 09:49:21 +0100883 struct imx_fuse *field_return =
884 (struct imx_fuse *)&imx_field_return_fuse;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100885 uint32_t reg;
Paul Geurtsfea40042024-11-01 09:49:21 +0100886 bool is_enabled;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100887 int ret;
888
Paul Geurtsfea40042024-11-01 09:49:21 +0100889 ret = fuse_read(sec_config->bank, sec_config->word, &reg);
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100890 if (ret) {
Paul Geurtsfea40042024-11-01 09:49:21 +0100891 puts("Secure boot fuse read error\n");
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100892 return ret;
893 }
Paul Geurtsfea40042024-11-01 09:49:21 +0100894 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 }
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100903
Paul Geurtsfea40042024-11-01 09:49:21 +0100904 return is_enabled;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100905}
906
Bryan O'Donoghue1efba6a2018-01-12 12:40:13 +0000907int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size,
908 uint32_t ivt_offset)
Nitin Gargb1ce7012014-09-16 13:33:25 -0500909{
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800910 ulong load_addr = 0;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500911 size_t bytes;
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800912 ulong ivt_addr = 0;
Bryan O'Donoghue79987a12018-01-12 12:39:56 +0000913 int result = 1;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500914 ulong start;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000915 struct ivt *ivt;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000916 enum hab_status status;
Nitin Gargb1ce7012014-09-16 13:33:25 -0500917
Breno Lima5deb9302021-03-25 17:30:10 +0800918 if (!imx_hab_is_enabled())
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000919 puts("hab fuse not enabled\n");
Nitin Gargb1ce7012014-09-16 13:33:25 -0500920
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000921 printf("\nAuthenticate image from DDR location 0x%x...\n",
922 ddr_start);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500923
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000924 hab_caam_clock_enable(1);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500925
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000926 /* Calculate IVT address header */
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800927 ivt_addr = (ulong) (ddr_start + ivt_offset);
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000928 ivt = (struct ivt *)ivt_addr;
Bryan O'Donoghue5ee19b32018-01-12 12:40:03 +0000929
930 /* Verify IVT header bugging out on error */
Utkarsh Guptaaac4ab52021-03-25 17:30:08 +0800931 if (!validate_ivt(ivt))
Breno Lima52d389c2018-02-20 01:19:22 +0000932 goto hab_authentication_exit;
Utkarsh Guptaf79f75e2018-02-20 01:19:23 +0000933
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000934 start = ddr_start;
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000935 bytes = image_size;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000936
Utkarsh Gupta2b1f4142018-02-20 01:19:24 +0000937 /* Verify CSF */
938 if (!csf_is_valid(ivt, start, bytes))
939 goto hab_authentication_exit;
940
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000941 if (hab_rvt_entry() != HAB_SUCCESS) {
942 puts("hab entry function fail\n");
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000943 goto hab_exit_failure_print_status;
Bryan O'Donoghued525a282018-01-12 12:40:05 +0000944 }
945
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800946 status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)(ulong)ddr_start, bytes);
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000947 if (status != HAB_SUCCESS) {
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800948 printf("HAB check target 0x%08x-0x%08lx fail\n",
Breno Lima9807f552021-03-25 17:30:14 +0800949 ddr_start, ddr_start + (ulong)bytes);
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +0000950 goto hab_exit_failure_print_status;
Bryan O'Donoghue13513252018-01-12 12:40:10 +0000951 }
Nitin Gargb1ce7012014-09-16 13:33:25 -0500952#ifdef DEBUG
Breno Lima9807f552021-03-25 17:30:14 +0800953 printf("\nivt_offset = 0x%x, ivt addr = 0x%lx\n", ivt_offset, ivt_addr);
Bryan O'Donoghueeb031502018-01-12 12:40:07 +0000954 printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry,
955 ivt->dcd, ivt->csf);
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000956 puts("Dumping IVT\n");
Marek Vasut18eceae2023-05-29 01:29:20 +0200957 print_buffer(ivt_addr, (void *)(uintptr_t)(ivt_addr), 4, 0x8, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500958
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000959 puts("Dumping CSF Header\n");
Marek Vasut18eceae2023-05-29 01:29:20 +0200960 print_buffer(ivt->csf, (void *)(uintptr_t)(ivt->csf), 4, 0x10, 0);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500961
Simon Glass85ed77d2024-09-29 19:49:46 -0600962#if !defined(CONFIG_XPL_BUILD)
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000963 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100964#endif
Nitin Gargb1ce7012014-09-16 13:33:25 -0500965
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000966 puts("\nCalling authenticate_image in ROM\n");
967 printf("\tivt_offset = 0x%x\n", ivt_offset);
968 printf("\tstart = 0x%08lx\n", start);
Marek Vasut18eceae2023-05-29 01:29:20 +0200969 printf("\tbytes = 0x%lx\n", (ulong)bytes);
Nitin Gargb1ce7012014-09-16 13:33:25 -0500970#endif
Peng Fan4f3cf8f2021-03-25 17:30:07 +0800971
972#ifndef CONFIG_ARM64
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000973 /*
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);
Bryan O'Donoghue19369c72018-01-12 12:39:57 +0000998 }
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +0000999 }
Peng Fan4f3cf8f2021-03-25 17:30:07 +08001000#endif
Nitin Gargb1ce7012014-09-16 13:33:25 -05001001
Peng Fan4f3cf8f2021-03-25 17:30:07 +08001002 load_addr = (ulong)hab_rvt_authenticate_image(
Bryan O'Donoghueb8dd7a62018-01-12 12:39:58 +00001003 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;
Bryan O'Donoghue19369c72018-01-12 12:39:57 +00001009 }
Nitin Gargb1ce7012014-09-16 13:33:25 -05001010
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +00001011hab_exit_failure_print_status:
Simon Glass85ed77d2024-09-29 19:49:46 -06001012#if !defined(CONFIG_XPL_BUILD)
Bryan O'Donoghue19369c72018-01-12 12:39:57 +00001013 get_hab_status();
Sven Ebenfeldeba5e332016-11-06 16:37:55 +01001014#endif
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +00001015
Breno Lima52d389c2018-02-20 01:19:22 +00001016hab_authentication_exit:
Bryan O'Donoghue3bf65de2018-01-12 12:40:11 +00001017
Breno Lima5deb9302021-03-25 17:30:10 +08001018 if (load_addr != 0 || !imx_hab_is_enabled())
Bryan O'Donoghue79987a12018-01-12 12:39:56 +00001019 result = 0;
Nitin Gargb1ce7012014-09-16 13:33:25 -05001020
1021 return result;
1022}
Ye Lif66fd7b2021-03-25 17:30:06 +08001023
1024int 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}