1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2016 NXP Semiconductor, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include <common.h>
7*4882a593Smuzhiyun #include <malloc.h>
8*4882a593Smuzhiyun #include <config.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <asm/system.h>
11*4882a593Smuzhiyun #include <asm/types.h>
12*4882a593Smuzhiyun #include <asm/arch/soc.h>
13*4882a593Smuzhiyun #ifdef CONFIG_FSL_LSCH3
14*4882a593Smuzhiyun #include <asm/arch/immap_lsch3.h>
15*4882a593Smuzhiyun #elif defined(CONFIG_FSL_LSCH2)
16*4882a593Smuzhiyun #include <asm/arch/immap_lsch2.h>
17*4882a593Smuzhiyun #endif
18*4882a593Smuzhiyun #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
19*4882a593Smuzhiyun #include <asm/armv8/sec_firmware.h>
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
22*4882a593Smuzhiyun #include <fsl_validate.h>
23*4882a593Smuzhiyun #endif
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
26*4882a593Smuzhiyun #include <nand.h>
27*4882a593Smuzhiyun #elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
28*4882a593Smuzhiyun #include <mmc.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
32*4882a593Smuzhiyun
ppa_init(void)33*4882a593Smuzhiyun int ppa_init(void)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun unsigned int el = current_el();
36*4882a593Smuzhiyun void *ppa_fit_addr;
37*4882a593Smuzhiyun u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
38*4882a593Smuzhiyun int ret;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
41*4882a593Smuzhiyun uintptr_t ppa_esbc_hdr = 0;
42*4882a593Smuzhiyun uintptr_t ppa_img_addr = 0;
43*4882a593Smuzhiyun #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
44*4882a593Smuzhiyun defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
45*4882a593Smuzhiyun void *ppa_hdr_ddr;
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /* Skip if running at lower exception level */
50*4882a593Smuzhiyun if (el < 3) {
51*4882a593Smuzhiyun debug("Skipping PPA init, running at EL%d\n", el);
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
56*4882a593Smuzhiyun ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
57*4882a593Smuzhiyun debug("%s: PPA image load from XIP\n", __func__);
58*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
59*4882a593Smuzhiyun ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun #else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
62*4882a593Smuzhiyun size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* Copy PPA image from MMC/SD/NAND to allocated memory */
65*4882a593Smuzhiyun #ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
66*4882a593Smuzhiyun struct mmc *mmc;
67*4882a593Smuzhiyun int dev = CONFIG_SYS_MMC_ENV_DEV;
68*4882a593Smuzhiyun struct fdt_header *fitp;
69*4882a593Smuzhiyun u32 cnt;
70*4882a593Smuzhiyun u32 blk;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun debug("%s: PPA image load from eMMC/SD\n", __func__);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun ret = mmc_initialize(gd->bd);
75*4882a593Smuzhiyun if (ret) {
76*4882a593Smuzhiyun printf("%s: mmc_initialize() failed\n", __func__);
77*4882a593Smuzhiyun return ret;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun mmc = find_mmc_device(dev);
80*4882a593Smuzhiyun if (!mmc) {
81*4882a593Smuzhiyun printf("PPA: MMC cannot find device for PPA firmware\n");
82*4882a593Smuzhiyun return -ENODEV;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun ret = mmc_init(mmc);
86*4882a593Smuzhiyun if (ret) {
87*4882a593Smuzhiyun printf("%s: mmc_init() failed\n", __func__);
88*4882a593Smuzhiyun return ret;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun fitp = malloc(roundup(fdt_header_len, 512));
92*4882a593Smuzhiyun if (!fitp) {
93*4882a593Smuzhiyun printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
94*4882a593Smuzhiyun roundup(fdt_header_len, 512));
95*4882a593Smuzhiyun return -ENOMEM;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
99*4882a593Smuzhiyun cnt = DIV_ROUND_UP(fdt_header_len, 512);
100*4882a593Smuzhiyun debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
101*4882a593Smuzhiyun __func__, dev, blk, cnt);
102*4882a593Smuzhiyun ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, fitp);
103*4882a593Smuzhiyun if (ret != cnt) {
104*4882a593Smuzhiyun free(fitp);
105*4882a593Smuzhiyun printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
106*4882a593Smuzhiyun CONFIG_SYS_LS_PPA_FW_ADDR);
107*4882a593Smuzhiyun return -EIO;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun ret = fdt_check_header(fitp);
111*4882a593Smuzhiyun if (ret) {
112*4882a593Smuzhiyun free(fitp);
113*4882a593Smuzhiyun printf("%s: fdt_check_header() failed\n", __func__);
114*4882a593Smuzhiyun return ret;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
118*4882a593Smuzhiyun ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
119*4882a593Smuzhiyun if (!ppa_hdr_ddr) {
120*4882a593Smuzhiyun printf("PPA: malloc failed for PPA header\n");
121*4882a593Smuzhiyun return -ENOMEM;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
125*4882a593Smuzhiyun cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
126*4882a593Smuzhiyun ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, ppa_hdr_ddr);
127*4882a593Smuzhiyun if (ret != cnt) {
128*4882a593Smuzhiyun free(ppa_hdr_ddr);
129*4882a593Smuzhiyun printf("MMC/SD read of PPA header failed\n");
130*4882a593Smuzhiyun return -EIO;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
135*4882a593Smuzhiyun #endif
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun fw_length = fdt_totalsize(fitp);
138*4882a593Smuzhiyun free(fitp);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun fw_length = roundup(fw_length, 512);
141*4882a593Smuzhiyun ppa_fit_addr = malloc(fw_length);
142*4882a593Smuzhiyun if (!ppa_fit_addr) {
143*4882a593Smuzhiyun printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
144*4882a593Smuzhiyun fw_length);
145*4882a593Smuzhiyun return -ENOMEM;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
149*4882a593Smuzhiyun cnt = DIV_ROUND_UP(fw_length, 512);
150*4882a593Smuzhiyun debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
151*4882a593Smuzhiyun __func__, dev, blk, cnt);
152*4882a593Smuzhiyun ret = mmc->block_dev.block_read(&mmc->block_dev,
153*4882a593Smuzhiyun blk, cnt, ppa_fit_addr);
154*4882a593Smuzhiyun if (ret != cnt) {
155*4882a593Smuzhiyun free(ppa_fit_addr);
156*4882a593Smuzhiyun printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
157*4882a593Smuzhiyun CONFIG_SYS_LS_PPA_FW_ADDR);
158*4882a593Smuzhiyun return -EIO;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
162*4882a593Smuzhiyun struct fdt_header fit;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun debug("%s: PPA image load from NAND\n", __func__);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun nand_init();
167*4882a593Smuzhiyun ret = nand_read(get_nand_dev_by_index(0),
168*4882a593Smuzhiyun (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
169*4882a593Smuzhiyun &fdt_header_len, (u_char *)&fit);
170*4882a593Smuzhiyun if (ret == -EUCLEAN) {
171*4882a593Smuzhiyun printf("NAND read of PPA FIT header at offset 0x%x failed\n",
172*4882a593Smuzhiyun CONFIG_SYS_LS_PPA_FW_ADDR);
173*4882a593Smuzhiyun return -EIO;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun ret = fdt_check_header(&fit);
177*4882a593Smuzhiyun if (ret) {
178*4882a593Smuzhiyun printf("%s: fdt_check_header() failed\n", __func__);
179*4882a593Smuzhiyun return ret;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
183*4882a593Smuzhiyun ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
184*4882a593Smuzhiyun if (!ppa_hdr_ddr) {
185*4882a593Smuzhiyun printf("PPA: malloc failed for PPA header\n");
186*4882a593Smuzhiyun return -ENOMEM;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun ret = nand_read(get_nand_dev_by_index(0),
192*4882a593Smuzhiyun (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
193*4882a593Smuzhiyun &fw_length, (u_char *)ppa_hdr_ddr);
194*4882a593Smuzhiyun if (ret == -EUCLEAN) {
195*4882a593Smuzhiyun free(ppa_hdr_ddr);
196*4882a593Smuzhiyun printf("NAND read of PPA firmware at offset 0x%x failed\n",
197*4882a593Smuzhiyun CONFIG_SYS_LS_PPA_FW_ADDR);
198*4882a593Smuzhiyun return -EIO;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun fw_length = fdt_totalsize(&fit);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun ppa_fit_addr = malloc(fw_length);
208*4882a593Smuzhiyun if (!ppa_fit_addr) {
209*4882a593Smuzhiyun printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
210*4882a593Smuzhiyun fw_length);
211*4882a593Smuzhiyun return -ENOMEM;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun ret = nand_read(get_nand_dev_by_index(0),
215*4882a593Smuzhiyun (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
216*4882a593Smuzhiyun &fw_length, (u_char *)ppa_fit_addr);
217*4882a593Smuzhiyun if (ret == -EUCLEAN) {
218*4882a593Smuzhiyun free(ppa_fit_addr);
219*4882a593Smuzhiyun printf("NAND read of PPA firmware at offset 0x%x failed\n",
220*4882a593Smuzhiyun CONFIG_SYS_LS_PPA_FW_ADDR);
221*4882a593Smuzhiyun return -EIO;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun #else
224*4882a593Smuzhiyun #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
225*4882a593Smuzhiyun #endif
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun #endif
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun #ifdef CONFIG_CHAIN_OF_TRUST
230*4882a593Smuzhiyun ppa_img_addr = (uintptr_t)ppa_fit_addr;
231*4882a593Smuzhiyun if (fsl_check_boot_mode_secure() != 0) {
232*4882a593Smuzhiyun /*
233*4882a593Smuzhiyun * In case of failure in validation, fsl_secboot_validate
234*4882a593Smuzhiyun * would not return back in case of Production environment
235*4882a593Smuzhiyun * with ITS=1. In Development environment (ITS=0 and
236*4882a593Smuzhiyun * SB_EN=1), the function may return back in case of
237*4882a593Smuzhiyun * non-fatal failures.
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun ret = fsl_secboot_validate(ppa_esbc_hdr,
240*4882a593Smuzhiyun PPA_KEY_HASH,
241*4882a593Smuzhiyun &ppa_img_addr);
242*4882a593Smuzhiyun if (ret != 0)
243*4882a593Smuzhiyun printf("PPA validation failed\n");
244*4882a593Smuzhiyun else
245*4882a593Smuzhiyun printf("PPA validation Successful\n");
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
248*4882a593Smuzhiyun defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
249*4882a593Smuzhiyun free(ppa_hdr_ddr);
250*4882a593Smuzhiyun #endif
251*4882a593Smuzhiyun #endif
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun #ifdef CONFIG_FSL_LSCH3
254*4882a593Smuzhiyun struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
255*4882a593Smuzhiyun boot_loc_ptr_l = &gur->bootlocptrl;
256*4882a593Smuzhiyun boot_loc_ptr_h = &gur->bootlocptrh;
257*4882a593Smuzhiyun #elif defined(CONFIG_FSL_LSCH2)
258*4882a593Smuzhiyun struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
259*4882a593Smuzhiyun boot_loc_ptr_l = &scfg->scratchrw[1];
260*4882a593Smuzhiyun boot_loc_ptr_h = &scfg->scratchrw[0];
261*4882a593Smuzhiyun #endif
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
264*4882a593Smuzhiyun boot_loc_ptr_l, boot_loc_ptr_h);
265*4882a593Smuzhiyun ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
268*4882a593Smuzhiyun defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
269*4882a593Smuzhiyun free(ppa_fit_addr);
270*4882a593Smuzhiyun #endif
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun return ret;
273*4882a593Smuzhiyun }
274