blob: cddcee964afd0560a3d78be1b8d2467db206d675 [file] [log] [blame]
Hou Zhiqiang00787862016-06-28 20:18:14 +08001/*
2 * Copyright 2016 NXP Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6#include <common.h>
Hou Zhiqiangc697bd12017-03-17 16:12:32 +08007#include <malloc.h>
Hou Zhiqiang00787862016-06-28 20:18:14 +08008#include <config.h>
9#include <errno.h>
10#include <asm/system.h>
11#include <asm/types.h>
12#include <asm/arch/soc.h>
13#ifdef CONFIG_FSL_LSCH3
14#include <asm/arch/immap_lsch3.h>
15#elif defined(CONFIG_FSL_LSCH2)
16#include <asm/arch/immap_lsch2.h>
17#endif
18#ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
19#include <asm/armv8/sec_firmware.h>
20#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -040021#ifdef CONFIG_CHAIN_OF_TRUST
22#include <fsl_validate.h>
23#endif
Hou Zhiqiang00787862016-06-28 20:18:14 +080024
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080025#ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
26#include <nand.h>
27#elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
28#include <mmc.h>
29#endif
30
31DECLARE_GLOBAL_DATA_PTR;
32
Hou Zhiqiang00787862016-06-28 20:18:14 +080033int ppa_init(void)
34{
York Sune6b871e2017-05-15 08:51:59 -070035 unsigned int el = current_el();
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080036 void *ppa_fit_addr;
Hou Zhiqiang00787862016-06-28 20:18:14 +080037 u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
Sumit Gargb6fa55e2017-09-01 13:55:01 +053038 u32 *loadable_l, *loadable_h;
Hou Zhiqiang00787862016-06-28 20:18:14 +080039 int ret;
40
Sumit Garge0f9e9b2016-09-01 12:56:44 -040041#ifdef CONFIG_CHAIN_OF_TRUST
Sumit Garg22a66f82017-04-20 05:09:12 +053042 uintptr_t ppa_esbc_hdr = 0;
Sumit Garge0f9e9b2016-09-01 12:56:44 -040043 uintptr_t ppa_img_addr = 0;
Sumit Garg22a66f82017-04-20 05:09:12 +053044#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
45 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
46 void *ppa_hdr_ddr;
47#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -040048#endif
49
York Sune6b871e2017-05-15 08:51:59 -070050 /* Skip if running at lower exception level */
51 if (el < 3) {
52 debug("Skipping PPA init, running at EL%d\n", el);
53 return 0;
54 }
55
Hou Zhiqiang7f83a5f2016-07-29 19:26:34 +080056#ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
Hou Zhiqiang00787862016-06-28 20:18:14 +080057 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080058 debug("%s: PPA image load from XIP\n", __func__);
Sumit Garg22a66f82017-04-20 05:09:12 +053059#ifdef CONFIG_CHAIN_OF_TRUST
60 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
61#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080062#else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
63 size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
64
65 /* Copy PPA image from MMC/SD/NAND to allocated memory */
66#ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
67 struct mmc *mmc;
68 int dev = CONFIG_SYS_MMC_ENV_DEV;
69 struct fdt_header *fitp;
70 u32 cnt;
Sumit Garg22a66f82017-04-20 05:09:12 +053071 u32 blk;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080072
73 debug("%s: PPA image load from eMMC/SD\n", __func__);
74
75 ret = mmc_initialize(gd->bd);
76 if (ret) {
77 printf("%s: mmc_initialize() failed\n", __func__);
78 return ret;
79 }
80 mmc = find_mmc_device(dev);
81 if (!mmc) {
82 printf("PPA: MMC cannot find device for PPA firmware\n");
83 return -ENODEV;
84 }
85
86 ret = mmc_init(mmc);
87 if (ret) {
88 printf("%s: mmc_init() failed\n", __func__);
89 return ret;
90 }
91
92 fitp = malloc(roundup(fdt_header_len, 512));
93 if (!fitp) {
94 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
95 roundup(fdt_header_len, 512));
96 return -ENOMEM;
97 }
98
Sumit Garg22a66f82017-04-20 05:09:12 +053099 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800100 cnt = DIV_ROUND_UP(fdt_header_len, 512);
101 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
102 __func__, dev, blk, cnt);
103 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, fitp);
104 if (ret != cnt) {
105 free(fitp);
106 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
107 CONFIG_SYS_LS_PPA_FW_ADDR);
108 return -EIO;
109 }
110
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800111 ret = fdt_check_header(fitp);
112 if (ret) {
113 free(fitp);
114 printf("%s: fdt_check_header() failed\n", __func__);
115 return ret;
116 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530117
118#ifdef CONFIG_CHAIN_OF_TRUST
119 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
120 if (!ppa_hdr_ddr) {
121 printf("PPA: malloc failed for PPA header\n");
122 return -ENOMEM;
123 }
124
125 blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
126 cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
127 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, ppa_hdr_ddr);
128 if (ret != cnt) {
129 free(ppa_hdr_ddr);
130 printf("MMC/SD read of PPA header failed\n");
131 return -EIO;
132 }
133 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
134
Sumit Garg22a66f82017-04-20 05:09:12 +0530135 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
136#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800137
138 fw_length = fdt_totalsize(fitp);
139 free(fitp);
140
141 fw_length = roundup(fw_length, 512);
142 ppa_fit_addr = malloc(fw_length);
143 if (!ppa_fit_addr) {
144 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
145 fw_length);
146 return -ENOMEM;
147 }
148
Sumit Garg22a66f82017-04-20 05:09:12 +0530149 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800150 cnt = DIV_ROUND_UP(fw_length, 512);
151 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
152 __func__, dev, blk, cnt);
153 ret = mmc->block_dev.block_read(&mmc->block_dev,
154 blk, cnt, ppa_fit_addr);
155 if (ret != cnt) {
156 free(ppa_fit_addr);
157 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
158 CONFIG_SYS_LS_PPA_FW_ADDR);
159 return -EIO;
160 }
161
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800162#elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
163 struct fdt_header fit;
164
165 debug("%s: PPA image load from NAND\n", __func__);
166
167 nand_init();
Grygorii Strashko3f243552017-06-26 19:13:07 -0500168 ret = nand_read(get_nand_dev_by_index(0),
169 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
170 &fdt_header_len, (u_char *)&fit);
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800171 if (ret == -EUCLEAN) {
172 printf("NAND read of PPA FIT header at offset 0x%x failed\n",
173 CONFIG_SYS_LS_PPA_FW_ADDR);
174 return -EIO;
175 }
176
177 ret = fdt_check_header(&fit);
178 if (ret) {
179 printf("%s: fdt_check_header() failed\n", __func__);
180 return ret;
181 }
182
Sumit Garg22a66f82017-04-20 05:09:12 +0530183#ifdef CONFIG_CHAIN_OF_TRUST
184 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
185 if (!ppa_hdr_ddr) {
186 printf("PPA: malloc failed for PPA header\n");
187 return -ENOMEM;
188 }
189
190 fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
191
Grygorii Strashko3f243552017-06-26 19:13:07 -0500192 ret = nand_read(get_nand_dev_by_index(0),
193 (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
194 &fw_length, (u_char *)ppa_hdr_ddr);
Sumit Garg22a66f82017-04-20 05:09:12 +0530195 if (ret == -EUCLEAN) {
196 free(ppa_hdr_ddr);
197 printf("NAND read of PPA firmware at offset 0x%x failed\n",
198 CONFIG_SYS_LS_PPA_FW_ADDR);
199 return -EIO;
200 }
201 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
202
Sumit Garg22a66f82017-04-20 05:09:12 +0530203 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
204#endif
205
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800206 fw_length = fdt_totalsize(&fit);
207
208 ppa_fit_addr = malloc(fw_length);
209 if (!ppa_fit_addr) {
210 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
211 fw_length);
212 return -ENOMEM;
213 }
214
Grygorii Strashko3f243552017-06-26 19:13:07 -0500215 ret = nand_read(get_nand_dev_by_index(0),
216 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
217 &fw_length, (u_char *)ppa_fit_addr);
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800218 if (ret == -EUCLEAN) {
219 free(ppa_fit_addr);
220 printf("NAND read of PPA firmware at offset 0x%x failed\n",
221 CONFIG_SYS_LS_PPA_FW_ADDR);
222 return -EIO;
223 }
Hou Zhiqiang00787862016-06-28 20:18:14 +0800224#else
225#error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
226#endif
227
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800228#endif
229
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400230#ifdef CONFIG_CHAIN_OF_TRUST
231 ppa_img_addr = (uintptr_t)ppa_fit_addr;
232 if (fsl_check_boot_mode_secure() != 0) {
Sumit Garg22a66f82017-04-20 05:09:12 +0530233 /*
234 * In case of failure in validation, fsl_secboot_validate
235 * would not return back in case of Production environment
236 * with ITS=1. In Development environment (ITS=0 and
237 * SB_EN=1), the function may return back in case of
238 * non-fatal failures.
239 */
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400240 ret = fsl_secboot_validate(ppa_esbc_hdr,
Vinitha Pillai-B57223a4b3ded2017-03-23 13:48:14 +0530241 PPA_KEY_HASH,
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400242 &ppa_img_addr);
243 if (ret != 0)
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530244 printf("SEC firmware(s) validation failed\n");
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400245 else
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530246 printf("SEC firmware(s) validation Successful\n");
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400247 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530248#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
249 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
250 free(ppa_hdr_ddr);
251#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400252#endif
253
Hou Zhiqiang00787862016-06-28 20:18:14 +0800254#ifdef CONFIG_FSL_LSCH3
255 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
256 boot_loc_ptr_l = &gur->bootlocptrl;
257 boot_loc_ptr_h = &gur->bootlocptrh;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530258
259 /* Assign addresses to loadable ptrs */
260 loadable_l = &gur->scratchrw[4];
261 loadable_h = &gur->scratchrw[5];
Hou Zhiqiang00787862016-06-28 20:18:14 +0800262#elif defined(CONFIG_FSL_LSCH2)
263 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
264 boot_loc_ptr_l = &scfg->scratchrw[1];
265 boot_loc_ptr_h = &scfg->scratchrw[0];
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530266
267 /* Assign addresses to loadable ptrs */
268 loadable_l = &scfg->scratchrw[2];
269 loadable_h = &scfg->scratchrw[3];
Hou Zhiqiang00787862016-06-28 20:18:14 +0800270#endif
271
272 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
273 boot_loc_ptr_l, boot_loc_ptr_h);
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530274 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h,
275 loadable_l, loadable_h);
Hou Zhiqiang00787862016-06-28 20:18:14 +0800276
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800277#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
278 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
279 free(ppa_fit_addr);
280#endif
281
Hou Zhiqiang00787862016-06-28 20:18:14 +0800282 return ret;
283}