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