SPL: Add HAB image authentication to FIT
[oweals/u-boot.git] / arch / arm / mach-imx / spl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Gateworks Corporation
4  * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
5  *
6  * Author: Tim Harvey <tharvey@gateworks.com>
7  */
8
9 #include <common.h>
10 #include <asm/io.h>
11 #include <asm/arch/imx-regs.h>
12 #include <asm/arch/sys_proto.h>
13 #include <asm/spl.h>
14 #include <spl.h>
15 #include <asm/mach-imx/hab.h>
16 #include <asm/mach-imx/boot_mode.h>
17 #include <g_dnl.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #if defined(CONFIG_MX6)
22 /* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 register */
23 u32 spl_boot_device(void)
24 {
25         unsigned int bmode = readl(&src_base->sbmr2);
26         u32 reg = imx6_src_get_boot_mode();
27
28         /*
29          * Check for BMODE if serial downloader is enabled
30          * BOOT_MODE - see IMX6DQRM Table 8-1
31          */
32         if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
33                 return BOOT_DEVICE_BOARD;
34
35         /*
36          * The above method does not detect that the boot ROM used
37          * serial downloader in case the boot ROM decided to use the
38          * serial downloader as a fall back (primary boot source failed).
39          *
40          * Infer that the boot ROM used the USB serial downloader by
41          * checking whether the USB PHY is currently active... This
42          * assumes that SPL did not (yet) initialize the USB PHY...
43          */
44         if (is_usbotg_phy_active())
45                 return BOOT_DEVICE_BOARD;
46
47         /* BOOT_CFG1[7:4] - see IMX6DQRM Table 8-8 */
48         switch ((reg & IMX6_BMODE_MASK) >> IMX6_BMODE_SHIFT) {
49          /* EIM: See 8.5.1, Table 8-9 */
50         case IMX6_BMODE_EMI:
51                 /* BOOT_CFG1[3]: NOR/OneNAND Selection */
52                 switch ((reg & IMX6_BMODE_EMI_MASK) >> IMX6_BMODE_EMI_SHIFT) {
53                 case IMX6_BMODE_ONENAND:
54                         return BOOT_DEVICE_ONENAND;
55                 case IMX6_BMODE_NOR:
56                         return BOOT_DEVICE_NOR;
57                 break;
58                 }
59         /* Reserved: Used to force Serial Downloader */
60         case IMX6_BMODE_RESERVED:
61                 return BOOT_DEVICE_BOARD;
62         /* SATA: See 8.5.4, Table 8-20 */
63 #if !defined(CONFIG_MX6UL) && !defined(CONFIG_MX6ULL)
64         case IMX6_BMODE_SATA:
65                 return BOOT_DEVICE_SATA;
66 #endif
67         /* Serial ROM: See 8.5.5.1, Table 8-22 */
68         case IMX6_BMODE_SERIAL_ROM:
69                 /* BOOT_CFG4[2:0] */
70                 switch ((reg & IMX6_BMODE_SERIAL_ROM_MASK) >>
71                         IMX6_BMODE_SERIAL_ROM_SHIFT) {
72                 case IMX6_BMODE_ECSPI1:
73                 case IMX6_BMODE_ECSPI2:
74                 case IMX6_BMODE_ECSPI3:
75                 case IMX6_BMODE_ECSPI4:
76                 case IMX6_BMODE_ECSPI5:
77                         return BOOT_DEVICE_SPI;
78                 case IMX6_BMODE_I2C1:
79                 case IMX6_BMODE_I2C2:
80                 case IMX6_BMODE_I2C3:
81                         return BOOT_DEVICE_I2C;
82                 }
83                 break;
84         /* SD/eSD: 8.5.3, Table 8-15  */
85         case IMX6_BMODE_SD:
86         case IMX6_BMODE_ESD:
87                 return BOOT_DEVICE_MMC1;
88         /* MMC/eMMC: 8.5.3 */
89         case IMX6_BMODE_MMC:
90         case IMX6_BMODE_EMMC:
91                 return BOOT_DEVICE_MMC1;
92         /* NAND Flash: 8.5.2, Table 8-10 */
93         case IMX6_BMODE_NAND_MIN ... IMX6_BMODE_NAND_MAX:
94                 return BOOT_DEVICE_NAND;
95         }
96         return BOOT_DEVICE_NONE;
97 }
98
99 #elif defined(CONFIG_MX7) || defined(CONFIG_MX8M)
100 /* Translate iMX7/MX8M boot device to the SPL boot device enumeration */
101 u32 spl_boot_device(void)
102 {
103 #if defined(CONFIG_MX7)
104         unsigned int bmode = readl(&src_base->sbmr2);
105
106         /*
107          * Check for BMODE if serial downloader is enabled
108          * BOOT_MODE - see IMX7DRM Table 6-24
109          */
110         if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
111                 return BOOT_DEVICE_BOARD;
112
113         /*
114          * The above method does not detect that the boot ROM used
115          * serial downloader in case the boot ROM decided to use the
116          * serial downloader as a fall back (primary boot source failed).
117          *
118          * Infer that the boot ROM used the USB serial downloader by
119          * checking whether the USB PHY is currently active... This
120          * assumes that SPL did not (yet) initialize the USB PHY...
121          */
122         if (is_boot_from_usb())
123                 return BOOT_DEVICE_BOARD;
124 #endif
125
126         enum boot_device boot_device_spl = get_boot_device();
127
128         switch (boot_device_spl) {
129         case SD1_BOOT:
130         case MMC1_BOOT:
131         case SD2_BOOT:
132         case MMC2_BOOT:
133         case SD3_BOOT:
134         case MMC3_BOOT:
135                 return BOOT_DEVICE_MMC1;
136         case NAND_BOOT:
137                 return BOOT_DEVICE_NAND;
138         case SPI_NOR_BOOT:
139                 return BOOT_DEVICE_SPI;
140         case USB_BOOT:
141                 return BOOT_DEVICE_USB;
142         default:
143                 return BOOT_DEVICE_NONE;
144         }
145 }
146 #endif /* CONFIG_MX6 || CONFIG_MX7 || CONFIG_MX8M */
147
148 #ifdef CONFIG_SPL_USB_GADGET_SUPPORT
149 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
150 {
151         put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM + 0xfff, &dev->idProduct);
152
153         return 0;
154 }
155 #endif
156
157 #if defined(CONFIG_SPL_MMC_SUPPORT)
158 /* called from spl_mmc to see type of boot mode for storage (RAW or FAT) */
159 u32 spl_boot_mode(const u32 boot_device)
160 {
161         switch (spl_boot_device()) {
162         /* for MMC return either RAW or FAT mode */
163         case BOOT_DEVICE_MMC1:
164         case BOOT_DEVICE_MMC2:
165 #if defined(CONFIG_SPL_FAT_SUPPORT)
166                 return MMCSD_MODE_FS;
167 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
168                 return MMCSD_MODE_EMMCBOOT;
169 #else
170                 return MMCSD_MODE_RAW;
171 #endif
172                 break;
173         default:
174                 puts("spl: ERROR:  unsupported device\n");
175                 hang();
176         }
177 }
178 #endif
179
180 #if defined(CONFIG_SECURE_BOOT)
181
182 /*
183  * +------------+  0x0 (DDR_UIMAGE_START) -
184  * |   Header   |                          |
185  * +------------+  0x40                    |
186  * |            |                          |
187  * |            |                          |
188  * |            |                          |
189  * |            |                          |
190  * | Image Data |                          |
191  * .            |                          |
192  * .            |                           > Stuff to be authenticated ----+
193  * .            |                          |                                |
194  * |            |                          |                                |
195  * |            |                          |                                |
196  * +------------+                          |                                |
197  * |            |                          |                                |
198  * | Fill Data  |                          |                                |
199  * |            |                          |                                |
200  * +------------+ Align to ALIGN_SIZE      |                                |
201  * |    IVT     |                          |                                |
202  * +------------+ + IVT_SIZE              -                                 |
203  * |            |                                                           |
204  * |  CSF DATA  | <---------------------------------------------------------+
205  * |            |
206  * +------------+
207  * |            |
208  * | Fill Data  |
209  * |            |
210  * +------------+ + CSF_PAD_SIZE
211  */
212
213 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
214 {
215         typedef void __noreturn (*image_entry_noargs_t)(void);
216         uint32_t offset;
217
218         image_entry_noargs_t image_entry =
219                 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
220
221         debug("image entry point: 0x%lX\n", spl_image->entry_point);
222
223         if (spl_image->flags & SPL_FIT_FOUND) {
224                 image_entry();
225         } else {
226                 /*
227                  * HAB looks for the CSF at the end of the authenticated
228                  * data therefore, we need to subtract the size of the
229                  * CSF from the actual filesize
230                  */
231                 offset = spl_image->size - CONFIG_CSF_SIZE;
232                 if (!imx_hab_authenticate_image(spl_image->load_addr,
233                                                 offset + IVT_SIZE +
234                                                 CSF_PAD_SIZE, offset)) {
235                         image_entry();
236                 } else {
237                         puts("spl: ERROR:  image authentication fail\n");
238                         hang();
239                 }
240         }
241 }
242
243 ulong board_spl_fit_size_align(ulong size)
244 {
245         /*
246          * HAB authenticate_image requests the IVT offset is
247          * aligned to 0x1000
248          */
249
250         size = ALIGN(size, 0x1000);
251         size += CONFIG_CSF_SIZE;
252
253         return size;
254 }
255
256 void board_spl_fit_post_load(ulong load_addr, size_t length)
257 {
258         u32 offset = length - CONFIG_CSF_SIZE;
259
260         if (imx_hab_authenticate_image(load_addr,
261                                        offset + IVT_SIZE + CSF_PAD_SIZE,
262                                        offset)) {
263                 puts("spl: ERROR:  image authentication unsuccessful\n");
264                 hang();
265         }
266 }
267
268 #endif
269
270 #if defined(CONFIG_MX6) && defined(CONFIG_SPL_OS_BOOT)
271 int dram_init_banksize(void)
272 {
273         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
274         gd->bd->bi_dram[0].size = imx_ddr_size();
275
276         return 0;
277 }
278 #endif