blob: 67318250607dd23604d2d799c4a4dff23764289b [file] [log] [blame]
Stefano Babic83fd8582013-06-28 00:20:21 +02001/*
Ulises Cardenas8df2acd2015-07-02 21:26:30 -05002 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
Stefano Babic83fd8582013-06-28 00:20:21 +02003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Adrian Alonso1f3029f2015-10-12 13:48:14 -05008#include <config.h>
9#include <fuse.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020010#include <asm/io.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050011#include <asm/system.h>
Nitin Gargb1ce7012014-09-16 13:33:25 -050012#include <asm/arch/clock.h>
Stefano Babic14404422014-06-10 10:26:22 +020013#include <asm/arch/sys_proto.h>
Adrian Alonsoc97e4762015-10-12 13:48:08 -050014#include <asm/imx-common/hab.h>
Stefano Babic83fd8582013-06-28 00:20:21 +020015
16/* -------- start of HAB API updates ------------*/
Stefano Babic14404422014-06-10 10:26:22 +020017
18#define hab_rvt_report_event_p \
19( \
Peng Fan8c0c0862016-05-23 18:35:59 +080020 (is_mx6dqp()) ? \
21 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080022 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020023 ((hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080024 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020025 ((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( \
Peng Fan8c0c0862016-05-23 18:35:59 +080031 (is_mx6dqp()) ? \
32 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
Peng Fandc47b2b2016-05-23 18:35:56 +080033 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020034 ((hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS_NEW) :\
Peng Fandc47b2b2016-05-23 18:35:56 +080035 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020036 ((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( \
Peng Fan8c0c0862016-05-23 18:35:59 +080042 (is_mx6dqp()) ? \
43 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080044 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020045 ((hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080046 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020047 ((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( \
Peng Fan8c0c0862016-05-23 18:35:59 +080053 (is_mx6dqp()) ? \
54 ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080055 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020056 ((hab_rvt_entry_t *)HAB_RVT_ENTRY_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080057 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020058 ((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( \
Peng Fan8c0c0862016-05-23 18:35:59 +080064 (is_mx6dqp()) ? \
65 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080066 (is_mx6dq() && (soc_rev() >= CHIP_REV_1_5)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020067 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
Peng Fandc47b2b2016-05-23 18:35:56 +080068 (is_mx6sdl() && (soc_rev() >= CHIP_REV_1_2)) ? \
Stefano Babic14404422014-06-10 10:26:22 +020069 ((hab_rvt_exit_t *)HAB_RVT_EXIT_NEW) : \
70 ((hab_rvt_exit_t *)HAB_RVT_EXIT) \
71)
Stefano Babic83fd8582013-06-28 00:20:21 +020072
Nitin Gargb1ce7012014-09-16 13:33:25 -050073#define IVT_SIZE 0x20
74#define ALIGN_SIZE 0x1000
75#define CSF_PAD_SIZE 0x2000
76#define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8
77#define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0
78#define MX6SL_PU_IROM_MMU_EN_VAR 0x00900a18
Adrian Alonso00a0b562015-10-12 13:48:15 -050079#define IS_HAB_ENABLED_BIT \
80 (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2)
Nitin Gargb1ce7012014-09-16 13:33:25 -050081
82/*
83 * +------------+ 0x0 (DDR_UIMAGE_START) -
84 * | Header | |
85 * +------------+ 0x40 |
86 * | | |
87 * | | |
88 * | | |
89 * | | |
90 * | Image Data | |
91 * . | |
92 * . | > Stuff to be authenticated ----+
93 * . | | |
94 * | | | |
95 * | | | |
96 * +------------+ | |
97 * | | | |
98 * | Fill Data | | |
99 * | | | |
100 * +------------+ Align to ALIGN_SIZE | |
101 * | IVT | | |
102 * +------------+ + IVT_SIZE - |
103 * | | |
104 * | CSF DATA | <---------------------------------------------------------+
105 * | |
106 * +------------+
107 * | |
108 * | Fill Data |
109 * | |
110 * +------------+ + CSF_PAD_SIZE
111 */
112
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500113#define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */
114
115struct record {
116 uint8_t tag; /* Tag */
117 uint8_t len[2]; /* Length */
118 uint8_t par; /* Version */
119 uint8_t contents[MAX_RECORD_BYTES];/* Record Data */
120 bool any_rec_flag;
121};
122
123char *rsn_str[] = {"RSN = HAB_RSN_ANY (0x00)\n",
124 "RSN = HAB_ENG_FAIL (0x30)\n",
125 "RSN = HAB_INV_ADDRESS (0x22)\n",
126 "RSN = HAB_INV_ASSERTION (0x0C)\n",
127 "RSN = HAB_INV_CALL (0x28)\n",
128 "RSN = HAB_INV_CERTIFICATE (0x21)\n",
129 "RSN = HAB_INV_COMMAND (0x06)\n",
130 "RSN = HAB_INV_CSF (0x11)\n",
131 "RSN = HAB_INV_DCD (0x27)\n",
132 "RSN = HAB_INV_INDEX (0x0F)\n",
133 "RSN = HAB_INV_IVT (0x05)\n",
134 "RSN = HAB_INV_KEY (0x1D)\n",
135 "RSN = HAB_INV_RETURN (0x1E)\n",
136 "RSN = HAB_INV_SIGNATURE (0x18)\n",
137 "RSN = HAB_INV_SIZE (0x17)\n",
138 "RSN = HAB_MEM_FAIL (0x2E)\n",
139 "RSN = HAB_OVR_COUNT (0x2B)\n",
140 "RSN = HAB_OVR_STORAGE (0x2D)\n",
141 "RSN = HAB_UNS_ALGORITHM (0x12)\n",
142 "RSN = HAB_UNS_COMMAND (0x03)\n",
143 "RSN = HAB_UNS_ENGINE (0x0A)\n",
144 "RSN = HAB_UNS_ITEM (0x24)\n",
145 "RSN = HAB_UNS_KEY (0x1B)\n",
146 "RSN = HAB_UNS_PROTOCOL (0x14)\n",
147 "RSN = HAB_UNS_STATE (0x09)\n",
148 "RSN = INVALID\n",
149 NULL};
150
151char *sts_str[] = {"STS = HAB_SUCCESS (0xF0)\n",
152 "STS = HAB_FAILURE (0x33)\n",
153 "STS = HAB_WARNING (0x69)\n",
154 "STS = INVALID\n",
155 NULL};
156
157char *eng_str[] = {"ENG = HAB_ENG_ANY (0x00)\n",
158 "ENG = HAB_ENG_SCC (0x03)\n",
159 "ENG = HAB_ENG_RTIC (0x05)\n",
160 "ENG = HAB_ENG_SAHARA (0x06)\n",
161 "ENG = HAB_ENG_CSU (0x0A)\n",
162 "ENG = HAB_ENG_SRTC (0x0C)\n",
163 "ENG = HAB_ENG_DCP (0x1B)\n",
164 "ENG = HAB_ENG_CAAM (0x1D)\n",
165 "ENG = HAB_ENG_SNVS (0x1E)\n",
166 "ENG = HAB_ENG_OCOTP (0x21)\n",
167 "ENG = HAB_ENG_DTCP (0x22)\n",
168 "ENG = HAB_ENG_ROM (0x36)\n",
169 "ENG = HAB_ENG_HDCP (0x24)\n",
170 "ENG = HAB_ENG_RTL (0x77)\n",
171 "ENG = HAB_ENG_SW (0xFF)\n",
172 "ENG = INVALID\n",
173 NULL};
174
175char *ctx_str[] = {"CTX = HAB_CTX_ANY(0x00)\n",
176 "CTX = HAB_CTX_FAB (0xFF)\n",
177 "CTX = HAB_CTX_ENTRY (0xE1)\n",
178 "CTX = HAB_CTX_TARGET (0x33)\n",
179 "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n",
180 "CTX = HAB_CTX_DCD (0xDD)\n",
181 "CTX = HAB_CTX_CSF (0xCF)\n",
182 "CTX = HAB_CTX_COMMAND (0xC0)\n",
183 "CTX = HAB_CTX_AUT_DAT (0xDB)\n",
184 "CTX = HAB_CTX_ASSERT (0xA0)\n",
185 "CTX = HAB_CTX_EXIT (0xEE)\n",
186 "CTX = INVALID\n",
187 NULL};
188
189uint8_t hab_statuses[5] = {
190 HAB_STS_ANY,
191 HAB_FAILURE,
192 HAB_WARNING,
193 HAB_SUCCESS,
194 -1
195};
196
197uint8_t hab_reasons[26] = {
198 HAB_RSN_ANY,
199 HAB_ENG_FAIL,
200 HAB_INV_ADDRESS,
201 HAB_INV_ASSERTION,
202 HAB_INV_CALL,
203 HAB_INV_CERTIFICATE,
204 HAB_INV_COMMAND,
205 HAB_INV_CSF,
206 HAB_INV_DCD,
207 HAB_INV_INDEX,
208 HAB_INV_IVT,
209 HAB_INV_KEY,
210 HAB_INV_RETURN,
211 HAB_INV_SIGNATURE,
212 HAB_INV_SIZE,
213 HAB_MEM_FAIL,
214 HAB_OVR_COUNT,
215 HAB_OVR_STORAGE,
216 HAB_UNS_ALGORITHM,
217 HAB_UNS_COMMAND,
218 HAB_UNS_ENGINE,
219 HAB_UNS_ITEM,
220 HAB_UNS_KEY,
221 HAB_UNS_PROTOCOL,
222 HAB_UNS_STATE,
223 -1
224};
225
226uint8_t hab_contexts[12] = {
227 HAB_CTX_ANY,
228 HAB_CTX_FAB,
229 HAB_CTX_ENTRY,
230 HAB_CTX_TARGET,
231 HAB_CTX_AUTHENTICATE,
232 HAB_CTX_DCD,
233 HAB_CTX_CSF,
234 HAB_CTX_COMMAND,
235 HAB_CTX_AUT_DAT,
236 HAB_CTX_ASSERT,
237 HAB_CTX_EXIT,
238 -1
239};
240
241uint8_t hab_engines[16] = {
242 HAB_ENG_ANY,
243 HAB_ENG_SCC,
244 HAB_ENG_RTIC,
245 HAB_ENG_SAHARA,
246 HAB_ENG_CSU,
247 HAB_ENG_SRTC,
248 HAB_ENG_DCP,
249 HAB_ENG_CAAM,
250 HAB_ENG_SNVS,
251 HAB_ENG_OCOTP,
252 HAB_ENG_DTCP,
253 HAB_ENG_ROM,
254 HAB_ENG_HDCP,
255 HAB_ENG_RTL,
256 HAB_ENG_SW,
257 -1
258};
259
Stefano Babic83fd8582013-06-28 00:20:21 +0200260bool is_hab_enabled(void)
261{
Adrian Alonso1f3029f2015-10-12 13:48:14 -0500262 struct imx_sec_config_fuse_t *fuse =
263 (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse;
264 uint32_t reg;
265 int ret;
266
267 ret = fuse_read(fuse->bank, fuse->word, &reg);
268 if (ret) {
269 puts("\nSecure boot fuse read error\n");
270 return ret;
271 }
Stefano Babic83fd8582013-06-28 00:20:21 +0200272
Adrian Alonso00a0b562015-10-12 13:48:15 -0500273 return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT;
Stefano Babic83fd8582013-06-28 00:20:21 +0200274}
275
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500276static inline uint8_t get_idx(uint8_t *list, uint8_t tgt)
277{
278 uint8_t idx = 0;
279 uint8_t element = list[idx];
280 while (element != -1) {
281 if (element == tgt)
282 return idx;
283 element = list[++idx];
284 }
285 return -1;
286}
287
288void process_event_record(uint8_t *event_data, size_t bytes)
289{
290 struct record *rec = (struct record *)event_data;
291
292 printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]);
293 printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]);
294 printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]);
295 printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]);
296}
297
Stefano Babic83fd8582013-06-28 00:20:21 +0200298void display_event(uint8_t *event_data, size_t bytes)
299{
300 uint32_t i;
301
302 if (!(event_data && bytes > 0))
303 return;
304
305 for (i = 0; i < bytes; i++) {
306 if (i == 0)
307 printf("\t0x%02x", event_data[i]);
308 else if ((i % 8) == 0)
309 printf("\n\t0x%02x", event_data[i]);
310 else
311 printf(" 0x%02x", event_data[i]);
312 }
Ulises Cardenas8df2acd2015-07-02 21:26:30 -0500313
314 process_event_record(event_data, bytes);
Stefano Babic83fd8582013-06-28 00:20:21 +0200315}
316
317int get_hab_status(void)
318{
319 uint32_t index = 0; /* Loop index */
320 uint8_t event_data[128]; /* Event data buffer */
321 size_t bytes = sizeof(event_data); /* Event size in bytes */
322 enum hab_config config = 0;
323 enum hab_state state = 0;
Stefano Babic14404422014-06-10 10:26:22 +0200324 hab_rvt_report_event_t *hab_rvt_report_event;
325 hab_rvt_report_status_t *hab_rvt_report_status;
326
327 hab_rvt_report_event = hab_rvt_report_event_p;
328 hab_rvt_report_status = hab_rvt_report_status_p;
Stefano Babic83fd8582013-06-28 00:20:21 +0200329
330 if (is_hab_enabled())
331 puts("\nSecure boot enabled\n");
332 else
333 puts("\nSecure boot disabled\n");
334
335 /* Check HAB status */
336 if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) {
337 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
338 config, state);
339
340 /* Display HAB Error events */
341 while (hab_rvt_report_event(HAB_FAILURE, index, event_data,
342 &bytes) == HAB_SUCCESS) {
343 puts("\n");
344 printf("--------- HAB Event %d -----------------\n",
345 index + 1);
346 puts("event data:\n");
347 display_event(event_data, bytes);
348 puts("\n");
349 bytes = sizeof(event_data);
350 index++;
351 }
352 }
353 /* Display message if no HAB events are found */
354 else {
355 printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n",
356 config, state);
357 puts("No HAB Events Found!\n\n");
358 }
359 return 0;
360}
361
Nitin Gargb1ce7012014-09-16 13:33:25 -0500362uint32_t authenticate_image(uint32_t ddr_start, uint32_t image_size)
363{
364 uint32_t load_addr = 0;
365 size_t bytes;
366 ptrdiff_t ivt_offset = 0;
367 int result = 0;
368 ulong start;
369 hab_rvt_authenticate_image_t *hab_rvt_authenticate_image;
370 hab_rvt_entry_t *hab_rvt_entry;
371 hab_rvt_exit_t *hab_rvt_exit;
372
373 hab_rvt_authenticate_image = hab_rvt_authenticate_image_p;
374 hab_rvt_entry = hab_rvt_entry_p;
375 hab_rvt_exit = hab_rvt_exit_p;
376
377 if (is_hab_enabled()) {
378 printf("\nAuthenticate image from DDR location 0x%x...\n",
379 ddr_start);
380
381 hab_caam_clock_enable(1);
382
383 if (hab_rvt_entry() == HAB_SUCCESS) {
384 /* If not already aligned, Align to ALIGN_SIZE */
385 ivt_offset = (image_size + ALIGN_SIZE - 1) &
386 ~(ALIGN_SIZE - 1);
387
388 start = ddr_start;
389 bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE;
390#ifdef DEBUG
391 printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n",
392 ivt_offset, ddr_start + ivt_offset);
393 puts("Dumping IVT\n");
394 print_buffer(ddr_start + ivt_offset,
395 (void *)(ddr_start + ivt_offset),
396 4, 0x8, 0);
397
398 puts("Dumping CSF Header\n");
399 print_buffer(ddr_start + ivt_offset+IVT_SIZE,
400 (void *)(ddr_start + ivt_offset+IVT_SIZE),
401 4, 0x10, 0);
402
403 get_hab_status();
404
405 puts("\nCalling authenticate_image in ROM\n");
406 printf("\tivt_offset = 0x%x\n", ivt_offset);
407 printf("\tstart = 0x%08lx\n", start);
408 printf("\tbytes = 0x%x\n", bytes);
409#endif
410 /*
411 * If the MMU is enabled, we have to notify the ROM
412 * code, or it won't flush the caches when needed.
413 * This is done, by setting the "pu_irom_mmu_enabled"
414 * word to 1. You can find its address by looking in
415 * the ROM map. This is critical for
416 * authenticate_image(). If MMU is enabled, without
417 * setting this bit, authentication will fail and may
418 * crash.
419 */
420 /* Check MMU enabled */
Adrian Alonso00a0b562015-10-12 13:48:15 -0500421 if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) {
Peng Fandc47b2b2016-05-23 18:35:56 +0800422 if (is_mx6dq()) {
Nitin Gargb1ce7012014-09-16 13:33:25 -0500423 /*
424 * This won't work on Rev 1.0.0 of
425 * i.MX6Q/D, since their ROM doesn't
426 * do cache flushes. don't think any
427 * exist, so we ignore them.
428 */
Ye.Lib5f46f72015-07-11 11:38:44 +0800429 if (!is_mx6dqp())
430 writel(1, MX6DQ_PU_IROM_MMU_EN_VAR);
Peng Fandc47b2b2016-05-23 18:35:56 +0800431 } else if (is_mx6sdl()) {
Nitin Gargb1ce7012014-09-16 13:33:25 -0500432 writel(1, MX6DLS_PU_IROM_MMU_EN_VAR);
Peng Fandc47b2b2016-05-23 18:35:56 +0800433 } else if (is_mx6sl()) {
Nitin Gargb1ce7012014-09-16 13:33:25 -0500434 writel(1, MX6SL_PU_IROM_MMU_EN_VAR);
435 }
436 }
437
438 load_addr = (uint32_t)hab_rvt_authenticate_image(
439 HAB_CID_UBOOT,
440 ivt_offset, (void **)&start,
441 (size_t *)&bytes, NULL);
442 if (hab_rvt_exit() != HAB_SUCCESS) {
443 puts("hab exit function fail\n");
444 load_addr = 0;
445 }
446 } else {
447 puts("hab entry function fail\n");
448 }
449
450 hab_caam_clock_enable(0);
451
452 get_hab_status();
453 } else {
454 puts("hab fuse not enabled\n");
455 }
456
457 if ((!is_hab_enabled()) || (load_addr != 0))
458 result = 1;
459
460 return result;
461}
462
Stefano Babic83fd8582013-06-28 00:20:21 +0200463int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
464{
465 if ((argc != 1)) {
466 cmd_usage(cmdtp);
467 return 1;
468 }
469
470 get_hab_status();
471
472 return 0;
473}
474
Nitin Gargb1ce7012014-09-16 13:33:25 -0500475static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc,
476 char * const argv[])
477{
478 ulong addr, ivt_offset;
479 int rcode = 0;
480
481 if (argc < 3)
482 return CMD_RET_USAGE;
483
484 addr = simple_strtoul(argv[1], NULL, 16);
485 ivt_offset = simple_strtoul(argv[2], NULL, 16);
486
487 rcode = authenticate_image(addr, ivt_offset);
488
489 return rcode;
490}
491
Stefano Babic83fd8582013-06-28 00:20:21 +0200492U_BOOT_CMD(
493 hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status,
494 "display HAB status",
495 ""
496 );
Nitin Gargb1ce7012014-09-16 13:33:25 -0500497
498U_BOOT_CMD(
499 hab_auth_img, 3, 0, do_authenticate_image,
500 "authenticate image via HAB",
501 "addr ivt_offset\n"
502 "addr - image hex address\n"
503 "ivt_offset - hex offset of IVT in the image"
504 );