]> git.ipfire.org Git - people/ms/u-boot.git/blame - arch/arm/mach-imx/hab.c
arm: imx: hab: Move CSF_PAD_SIZE to hab.h
[people/ms/u-boot.git] / arch / arm / mach-imx / hab.c
CommitLineData
b83c709e 1/*
29067abf 2 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
b83c709e
SB
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
fba6f9ef
AA
8#include <config.h>
9#include <fuse.h>
b83c709e 10#include <asm/io.h>
36c1ca4d 11#include <asm/system.h>
36c1ca4d 12#include <asm/arch/clock.h>
f2f07e85 13#include <asm/arch/sys_proto.h>
552a848e 14#include <asm/mach-imx/hab.h>
b83c709e
SB
15
16/* -------- start of HAB API updates ------------*/
f2f07e85
SB
17
18#define hab_rvt_report_event_p \
19( \
b5437a80
PF
20 (is_mx6dqp()) ? \
21 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
27cd0da4 22 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
f2f07e85 23 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
27cd0da4 24 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
f2f07e85
SB
25 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
26 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT) \
27)
28
29#define hab_rvt_report_status_p \
30( \
b5437a80
PF
31 (is_mx6dqp()) ? \
32 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
27cd0da4 33 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
f2f07e85 34 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
27cd0da4 35 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
f2f07e85
SB
36 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
37 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS) \
38)
39
40#define hab_rvt_authenticate_image_p \
41( \
b5437a80
PF
42 (is_mx6dqp()) ? \
43 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
27cd0da4 44 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
f2f07e85 45 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
27cd0da4 46 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
f2f07e85
SB
47 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
48 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE) \
49)
50
51#define hab_rvt_entry_p \
52( \
b5437a80
PF
53 (is_mx6dqp()) ? \
54 ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
27cd0da4 55 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
f2f07e85 56 ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
27cd0da4 57 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
f2f07e85
SB
58 ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
59 ((hab_rvt_entry_t *)HAB_RVT_ENTRY) \
60)
61
62#define hab_rvt_exit_p \
63( \
b5437a80
PF
64 (is_mx6dqp()) ? \
65 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
27cd0da4 66 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
f2f07e85 67 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
27cd0da4 68 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
f2f07e85
SB
69 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
70 ((hab_rvt_exit_t *)HAB_RVT_EXIT) \
71)
b83c709e 72
36c1ca4d 73#define ALIGN_SIZE 0x1000
36c1ca4d
NG
74#define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
75#define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
76#define MX6SL_PU_IROM_MMU_EN_VAR 0x00900a18
ee3899aa 77#define IS_HAB_ENABLED_BIT \
27117b20
PF
78 (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \
79 (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2))
36c1ca4d
NG
80
81/*
82 * +------------+ 0x0 (DDR_UIMAGE_START) -
83 * | Header | |
84 * +------------+ 0x40 |
85 * | | |
86 * | | |
87 * | | |
88 * | | |
89 * | Image Data | |
90 * . | |
91 * . | > Stuff to be authenticated ----+
92 * . | | |
93 * | | | |
94 * | | | |
95 * +------------+ | |
96 * | | | |
97 * | Fill Data | | |
98 * | | | |
99 * +------------+ Align to ALIGN_SIZE | |
100 * | IVT | | |
101 * +------------+ + IVT_SIZE - |
102 * | | |
103 * | CSF DATA | <---------------------------------------------------------+
104 * | |
105 * +------------+
106 * | |
107 * | Fill Data |
108 * | |
109 * +------------+ + CSF_PAD_SIZE
110 */
111
15b505b0
SE
112static bool is_hab_enabled(void);
113
114#if !defined(CONFIG_SPL_BUILD)
115
29067abf
UC
116#define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
117
118struct record {
119 uint8_t tag; /* Tag */
120 uint8_t len[2]; /* Length */
121 uint8_t par; /* Version */
122 uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
123 bool any_rec_flag;
124};
125
126char *rsn_str[] = {"RSN = HAB_RSN_ANY (0x00)\n",
127 "RSN = HAB_ENG_FAIL (0x30)\n",
128 "RSN = HAB_INV_ADDRESS (0x22)\n",
129 "RSN = HAB_INV_ASSERTION (0x0C)\n",
130 "RSN = HAB_INV_CALL (0x28)\n",
131 "RSN = HAB_INV_CERTIFICATE (0x21)\n",
132 "RSN = HAB_INV_COMMAND (0x06)\n",
133 "RSN = HAB_INV_CSF (0x11)\n",
134 "RSN = HAB_INV_DCD (0x27)\n",
135 "RSN = HAB_INV_INDEX (0x0F)\n",
136 "RSN = HAB_INV_IVT (0x05)\n",
137 "RSN = HAB_INV_KEY (0x1D)\n",
138 "RSN = HAB_INV_RETURN (0x1E)\n",
139 "RSN = HAB_INV_SIGNATURE (0x18)\n",
140 "RSN = HAB_INV_SIZE (0x17)\n",
141 "RSN = HAB_MEM_FAIL (0x2E)\n",
142 "RSN = HAB_OVR_COUNT (0x2B)\n",
143 "RSN = HAB_OVR_STORAGE (0x2D)\n",
144 "RSN = HAB_UNS_ALGORITHM (0x12)\n",
145 "RSN = HAB_UNS_COMMAND (0x03)\n",
146 "RSN = HAB_UNS_ENGINE (0x0A)\n",
147 "RSN = HAB_UNS_ITEM (0x24)\n",
148 "RSN = HAB_UNS_KEY (0x1B)\n",
149 "RSN = HAB_UNS_PROTOCOL (0x14)\n",
150 "RSN = HAB_UNS_STATE (0x09)\n",
151 "RSN = INVALID\n",
152 NULL};
153
154char *sts_str[] = {"STS = HAB_SUCCESS (0xF0)\n",
155 "STS = HAB_FAILURE (0x33)\n",
156 "STS = HAB_WARNING (0x69)\n",
157 "STS = INVALID\n",
158 NULL};
159
160char *eng_str[] = {"ENG = HAB_ENG_ANY (0x00)\n",
161 "ENG = HAB_ENG_SCC (0x03)\n",
162 "ENG = HAB_ENG_RTIC (0x05)\n",
163 "ENG = HAB_ENG_SAHARA (0x06)\n",
164 "ENG = HAB_ENG_CSU (0x0A)\n",
165 "ENG = HAB_ENG_SRTC (0x0C)\n",
166 "ENG = HAB_ENG_DCP (0x1B)\n",
167 "ENG = HAB_ENG_CAAM (0x1D)\n",
168 "ENG = HAB_ENG_SNVS (0x1E)\n",
169 "ENG = HAB_ENG_OCOTP (0x21)\n",
170 "ENG = HAB_ENG_DTCP (0x22)\n",
171 "ENG = HAB_ENG_ROM (0x36)\n",
172 "ENG = HAB_ENG_HDCP (0x24)\n",
173 "ENG = HAB_ENG_RTL (0x77)\n",
174 "ENG = HAB_ENG_SW (0xFF)\n",
175 "ENG = INVALID\n",
176 NULL};
177
178char *ctx_str[] = {"CTX = HAB_CTX_ANY(0x00)\n",
179 "CTX = HAB_CTX_FAB (0xFF)\n",
180 "CTX = HAB_CTX_ENTRY (0xE1)\n",
181 "CTX = HAB_CTX_TARGET (0x33)\n",
182 "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
183 "CTX = HAB_CTX_DCD (0xDD)\n",
184 "CTX = HAB_CTX_CSF (0xCF)\n",
185 "CTX = HAB_CTX_COMMAND (0xC0)\n",
186 "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
187 "CTX = HAB_CTX_ASSERT (0xA0)\n",
188 "CTX = HAB_CTX_EXIT (0xEE)\n",
189 "CTX = INVALID\n",
190 NULL};
191
192uint8_t hab_statuses[5] = {
193 HAB_STS_ANY,
194 HAB_FAILURE,
195 HAB_WARNING,
196 HAB_SUCCESS,
197 -1
198};
199
200uint8_t hab_reasons[26] = {
201 HAB_RSN_ANY,
202 HAB_ENG_FAIL,
203 HAB_INV_ADDRESS,
204 HAB_INV_ASSERTION,
205 HAB_INV_CALL,
206 HAB_INV_CERTIFICATE,
207 HAB_INV_COMMAND,
208 HAB_INV_CSF,
209 HAB_INV_DCD,
210 HAB_INV_INDEX,
211 HAB_INV_IVT,
212 HAB_INV_KEY,
213 HAB_INV_RETURN,
214 HAB_INV_SIGNATURE,
215 HAB_INV_SIZE,
216 HAB_MEM_FAIL,
217 HAB_OVR_COUNT,
218 HAB_OVR_STORAGE,
219 HAB_UNS_ALGORITHM,
220 HAB_UNS_COMMAND,
221 HAB_UNS_ENGINE,
222 HAB_UNS_ITEM,
223 HAB_UNS_KEY,
224 HAB_UNS_PROTOCOL,
225 HAB_UNS_STATE,
226 -1
227};
228
229uint8_t hab_contexts[12] = {
230 HAB_CTX_ANY,
231 HAB_CTX_FAB,
232 HAB_CTX_ENTRY,
233 HAB_CTX_TARGET,
234 HAB_CTX_AUTHENTICATE,
235 HAB_CTX_DCD,
236 HAB_CTX_CSF,
237 HAB_CTX_COMMAND,
238 HAB_CTX_AUT_DAT,
239 HAB_CTX_ASSERT,
240 HAB_CTX_EXIT,
241 -1
242};
243
244uint8_t hab_engines[16] = {
245 HAB_ENG_ANY,
246 HAB_ENG_SCC,
247 HAB_ENG_RTIC,
248 HAB_ENG_SAHARA,
249 HAB_ENG_CSU,
250 HAB_ENG_SRTC,
251 HAB_ENG_DCP,
252 HAB_ENG_CAAM,
253 HAB_ENG_SNVS,
254 HAB_ENG_OCOTP,
255 HAB_ENG_DTCP,
256 HAB_ENG_ROM,
257 HAB_ENG_HDCP,
258 HAB_ENG_RTL,
259 HAB_ENG_SW,
260 -1
261};
262
29067abf
UC
263static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
264{
265 uint8_t idx = 0;
266 uint8_t element = list[idx];
267 while (element != -1) {
268 if (element == tgt)
269 return idx;
270 element = list[++idx];
271 }
272 return -1;
273}
274
275void process_event_record(uint8_t *event_data, size_t bytes)
276{
277 struct record *rec = (struct record *)event_data;
278
279 printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
280 printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
281 printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
282 printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
283}
284
b83c709e
SB
285void display_event(uint8_t *event_data, size_t bytes)
286{
287 uint32_t i;
288
289 if (!(event_data && bytes > 0))
290 return;
291
292 for (i = 0; i < bytes; i++) {
293 if (i == 0)
294 printf("\t0x%02x", event_data[i]);
295 else if ((i % 8) == 0)
296 printf("\n\t0x%02x", event_data[i]);
297 else
298 printf(" 0x%02x", event_data[i]);
299 }
29067abf
UC
300
301 process_event_record(event_data, bytes);
b83c709e
SB
302}
303
304int get_hab_status(void)
305{
306 uint32_t index = 0; /* Loop index */
307 uint8_t event_data[128]; /* Event data buffer */
308 size_t bytes = sizeof(event_data); /* Event size in bytes */
309 enum hab_config config = 0;
310 enum hab_state state = 0;
f2f07e85
SB
311 hab_rvt_report_event_t *hab_rvt_report_event;
312 hab_rvt_report_status_t *hab_rvt_report_status;
313
314 hab_rvt_report_event = hab_rvt_report_event_p;
315 hab_rvt_report_status = hab_rvt_report_status_p;
b83c709e
SB
316
317 if (is_hab_enabled())
318 puts("\nSecure boot enabled\n");
319 else
320 puts("\nSecure boot disabled\n");
321
322 /* Check HAB status */
323 if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
324 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
325 config, state);
326
327 /* Display HAB Error events */
328 while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
329 &bytes) == HAB_SUCCESS) {
330 puts("\n");
331 printf("--------- HAB Event %d -----------------\n",
332 index + 1);
333 puts("event data:\n");
334 display_event(event_data, bytes);
335 puts("\n");
336 bytes = sizeof(event_data);
337 index++;
338 }
339 }
340 /* Display message if no HAB events are found */
341 else {
342 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
343 config, state);
344 puts("No HAB Events Found!\n\n");
345 }
346 return 0;
347}
348
15b505b0
SE
349int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
350{
351 if ((argc != 1)) {
352 cmd_usage(cmdtp);
353 return 1;
354 }
355
356 get_hab_status();
357
358 return 0;
359}
360
361static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc,
362 char * const argv[])
363{
364 ulong addr, ivt_offset;
365 int rcode = 0;
366
367 if (argc < 3)
368 return CMD_RET_USAGE;
369
370 addr = simple_strtoul(argv[1], NULL, 16);
371 ivt_offset = simple_strtoul(argv[2], NULL, 16);
372
373 rcode = authenticate_image(addr, ivt_offset);
9535b397
BD
374 if (rcode == 0)
375 rcode = CMD_RET_SUCCESS;
376 else
377 rcode = CMD_RET_FAILURE;
15b505b0
SE
378 return rcode;
379}
380
381U_BOOT_CMD(
382 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
383 "display HAB status",
384 ""
385 );
386
387U_BOOT_CMD(
388 hab_auth_img, 3, 0, do_authenticate_image,
389 "authenticate image via HAB",
390 "addr ivt_offset\n"
391 "addr - image hex address\n"
392 "ivt_offset - hex offset of IVT in the image"
393 );
394
395
396#endif /* !defined(CONFIG_SPL_BUILD) */
397
398static bool is_hab_enabled(void)
399{
400 struct imx_sec_config_fuse_t *fuse =
401 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
402 uint32_t reg;
403 int ret;
404
405 ret = fuse_read(fuse->bank, fuse->word, &reg);
406 if (ret) {
407 puts("\nSecure boot fuse read error\n");
408 return ret;
409 }
410
411 return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
412}
413
adbb051f 414int authenticate_image(uint32_t ddr_start, uint32_t image_size)
36c1ca4d
NG
415{
416 uint32_t load_addr = 0;
417 size_t bytes;
418 ptrdiff_t ivt_offset = 0;
9535b397 419 int result = 1;
36c1ca4d
NG
420 ulong start;
421 hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
422 hab_rvt_entry_t *hab_rvt_entry;
423 hab_rvt_exit_t *hab_rvt_exit;
424
425 hab_rvt_authenticate_image = hab_rvt_authenticate_image_p;
426 hab_rvt_entry = hab_rvt_entry_p;
427 hab_rvt_exit = hab_rvt_exit_p;
428
d2c61800
BD
429 if (!is_hab_enabled()) {
430 puts("hab fuse not enabled\n");
431 return result;
432 }
36c1ca4d 433
d2c61800
BD
434 printf("\nAuthenticate image from DDR location 0x%x...\n",
435 ddr_start);
36c1ca4d 436
d2c61800 437 hab_caam_clock_enable(1);
36c1ca4d 438
53c8a510
BD
439 if (hab_rvt_entry() != HAB_SUCCESS) {
440 puts("hab entry function fail\n");
441 goto hab_caam_clock_disable;
442 }
d2c61800 443
53c8a510
BD
444 /* If not already aligned, Align to ALIGN_SIZE */
445 ivt_offset = (image_size + ALIGN_SIZE - 1) &
446 ~(ALIGN_SIZE - 1);
447
448 start = ddr_start;
449 bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE;
36c1ca4d 450#ifdef DEBUG
53c8a510
BD
451 printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n",
452 ivt_offset, ddr_start + ivt_offset);
453 puts("Dumping IVT\n");
454 print_buffer(ddr_start + ivt_offset,
455 (void *)(ddr_start + ivt_offset),
456 4, 0x8, 0);
457
458 puts("Dumping CSF Header\n");
459 print_buffer(ddr_start + ivt_offset + IVT_SIZE,
460 (void *)(ddr_start + ivt_offset + IVT_SIZE),
461 4, 0x10, 0);
36c1ca4d 462
15b505b0 463#if !defined(CONFIG_SPL_BUILD)
53c8a510 464 get_hab_status();
15b505b0 465#endif
36c1ca4d 466
53c8a510
BD
467 puts("\nCalling authenticate_image in ROM\n");
468 printf("\tivt_offset = 0x%x\n", ivt_offset);
469 printf("\tstart = 0x%08lx\n", start);
470 printf("\tbytes = 0x%x\n", bytes);
36c1ca4d 471#endif
53c8a510
BD
472 /*
473 * If the MMU is enabled, we have to notify the ROM
474 * code, or it won't flush the caches when needed.
475 * This is done, by setting the "pu_irom_mmu_enabled"
476 * word to 1. You can find its address by looking in
477 * the ROM map. This is critical for
478 * authenticate_image(). If MMU is enabled, without
479 * setting this bit, authentication will fail and may
480 * crash.
481 */
482 /* Check MMU enabled */
483 if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
484 if (is_mx6dq()) {
485 /*
486 * This won't work on Rev 1.0.0 of
487 * i.MX6Q/D, since their ROM doesn't
488 * do cache flushes. don't think any
489 * exist, so we ignore them.
490 */
491 if (!is_mx6dqp())
492 writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
493 } else if (is_mx6sdl()) {
494 writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
495 } else if (is_mx6sl()) {
496 writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
d2c61800 497 }
53c8a510 498 }
36c1ca4d 499
53c8a510
BD
500 load_addr = (uint32_t)hab_rvt_authenticate_image(
501 HAB_CID_UBOOT,
502 ivt_offset, (void **)&start,
503 (size_t *)&bytes, NULL);
504 if (hab_rvt_exit() != HAB_SUCCESS) {
505 puts("hab exit function fail\n");
506 load_addr = 0;
d2c61800 507 }
36c1ca4d 508
53c8a510 509hab_caam_clock_disable:
d2c61800 510 hab_caam_clock_enable(0);
36c1ca4d 511
15b505b0 512#if !defined(CONFIG_SPL_BUILD)
d2c61800 513 get_hab_status();
15b505b0 514#endif
d2c61800 515 if (load_addr != 0)
9535b397 516 result = 0;
36c1ca4d
NG
517
518 return result;
519}