blob: 26c47a183c668c4f8754d7727230704b10646b37 [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{
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080035 void *ppa_fit_addr;
Hou Zhiqiang00787862016-06-28 20:18:14 +080036 u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
37 int ret;
38
Sumit Garge0f9e9b2016-09-01 12:56:44 -040039#ifdef CONFIG_CHAIN_OF_TRUST
Sumit Garg22a66f82017-04-20 05:09:12 +053040 uintptr_t ppa_esbc_hdr = 0;
Sumit Garge0f9e9b2016-09-01 12:56:44 -040041 uintptr_t ppa_img_addr = 0;
Sumit Garg22a66f82017-04-20 05:09:12 +053042#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
43 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
44 void *ppa_hdr_ddr;
45#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -040046#endif
47
Hou Zhiqiang7f83a5f2016-07-29 19:26:34 +080048#ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
Hou Zhiqiang00787862016-06-28 20:18:14 +080049 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080050 debug("%s: PPA image load from XIP\n", __func__);
Sumit Garg22a66f82017-04-20 05:09:12 +053051#ifdef CONFIG_CHAIN_OF_TRUST
52 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
53#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080054#else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
55 size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
56
57 /* Copy PPA image from MMC/SD/NAND to allocated memory */
58#ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
59 struct mmc *mmc;
60 int dev = CONFIG_SYS_MMC_ENV_DEV;
61 struct fdt_header *fitp;
62 u32 cnt;
Sumit Garg22a66f82017-04-20 05:09:12 +053063 u32 blk;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080064
65 debug("%s: PPA image load from eMMC/SD\n", __func__);
66
67 ret = mmc_initialize(gd->bd);
68 if (ret) {
69 printf("%s: mmc_initialize() failed\n", __func__);
70 return ret;
71 }
72 mmc = find_mmc_device(dev);
73 if (!mmc) {
74 printf("PPA: MMC cannot find device for PPA firmware\n");
75 return -ENODEV;
76 }
77
78 ret = mmc_init(mmc);
79 if (ret) {
80 printf("%s: mmc_init() failed\n", __func__);
81 return ret;
82 }
83
84 fitp = malloc(roundup(fdt_header_len, 512));
85 if (!fitp) {
86 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
87 roundup(fdt_header_len, 512));
88 return -ENOMEM;
89 }
90
Sumit Garg22a66f82017-04-20 05:09:12 +053091 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080092 cnt = DIV_ROUND_UP(fdt_header_len, 512);
93 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
94 __func__, dev, blk, cnt);
95 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, fitp);
96 if (ret != cnt) {
97 free(fitp);
98 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
99 CONFIG_SYS_LS_PPA_FW_ADDR);
100 return -EIO;
101 }
102
103 /* flush cache after read */
104 flush_cache((ulong)fitp, cnt * 512);
105
106 ret = fdt_check_header(fitp);
107 if (ret) {
108 free(fitp);
109 printf("%s: fdt_check_header() failed\n", __func__);
110 return ret;
111 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530112
113#ifdef CONFIG_CHAIN_OF_TRUST
114 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
115 if (!ppa_hdr_ddr) {
116 printf("PPA: malloc failed for PPA header\n");
117 return -ENOMEM;
118 }
119
120 blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
121 cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
122 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, ppa_hdr_ddr);
123 if (ret != cnt) {
124 free(ppa_hdr_ddr);
125 printf("MMC/SD read of PPA header failed\n");
126 return -EIO;
127 }
128 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
129
130 /* flush cache after read */
131 flush_cache((ulong)ppa_hdr_ddr, cnt * 512);
132
133 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
134#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800135
136 fw_length = fdt_totalsize(fitp);
137 free(fitp);
138
139 fw_length = roundup(fw_length, 512);
140 ppa_fit_addr = malloc(fw_length);
141 if (!ppa_fit_addr) {
142 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
143 fw_length);
144 return -ENOMEM;
145 }
146
Sumit Garg22a66f82017-04-20 05:09:12 +0530147 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800148 cnt = DIV_ROUND_UP(fw_length, 512);
149 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
150 __func__, dev, blk, cnt);
151 ret = mmc->block_dev.block_read(&mmc->block_dev,
152 blk, cnt, ppa_fit_addr);
153 if (ret != cnt) {
154 free(ppa_fit_addr);
155 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
156 CONFIG_SYS_LS_PPA_FW_ADDR);
157 return -EIO;
158 }
159
160 /* flush cache after read */
161 flush_cache((ulong)ppa_fit_addr, cnt * 512);
162
163#elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
164 struct fdt_header fit;
165
166 debug("%s: PPA image load from NAND\n", __func__);
167
168 nand_init();
169 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
170 &fdt_header_len, (u_char *)&fit);
171 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
192 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
193 &fw_length, (u_char *)ppa_hdr_ddr);
194 if (ret == -EUCLEAN) {
195 free(ppa_hdr_ddr);
196 printf("NAND read of PPA firmware at offset 0x%x failed\n",
197 CONFIG_SYS_LS_PPA_FW_ADDR);
198 return -EIO;
199 }
200 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
201
202 /* flush cache after read */
203 flush_cache((ulong)ppa_hdr_ddr, fw_length);
204
205 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
206#endif
207
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800208 fw_length = fdt_totalsize(&fit);
209
210 ppa_fit_addr = malloc(fw_length);
211 if (!ppa_fit_addr) {
212 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
213 fw_length);
214 return -ENOMEM;
215 }
216
217 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
218 &fw_length, (u_char *)ppa_fit_addr);
219 if (ret == -EUCLEAN) {
220 free(ppa_fit_addr);
221 printf("NAND read of PPA firmware at offset 0x%x failed\n",
222 CONFIG_SYS_LS_PPA_FW_ADDR);
223 return -EIO;
224 }
225
226 /* flush cache after read */
227 flush_cache((ulong)ppa_fit_addr, fw_length);
Hou Zhiqiang00787862016-06-28 20:18:14 +0800228#else
229#error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
230#endif
231
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800232#endif
233
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400234#ifdef CONFIG_CHAIN_OF_TRUST
235 ppa_img_addr = (uintptr_t)ppa_fit_addr;
236 if (fsl_check_boot_mode_secure() != 0) {
Sumit Garg22a66f82017-04-20 05:09:12 +0530237 /*
238 * In case of failure in validation, fsl_secboot_validate
239 * would not return back in case of Production environment
240 * with ITS=1. In Development environment (ITS=0 and
241 * SB_EN=1), the function may return back in case of
242 * non-fatal failures.
243 */
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400244 ret = fsl_secboot_validate(ppa_esbc_hdr,
Vinitha Pillai-B57223a4b3ded2017-03-23 13:48:14 +0530245 PPA_KEY_HASH,
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400246 &ppa_img_addr);
247 if (ret != 0)
248 printf("PPA validation failed\n");
249 else
250 printf("PPA validation Successful\n");
251 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530252#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
253 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
254 free(ppa_hdr_ddr);
255#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400256#endif
257
Hou Zhiqiang00787862016-06-28 20:18:14 +0800258#ifdef CONFIG_FSL_LSCH3
259 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
260 boot_loc_ptr_l = &gur->bootlocptrl;
261 boot_loc_ptr_h = &gur->bootlocptrh;
262#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];
266#endif
267
268 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
269 boot_loc_ptr_l, boot_loc_ptr_h);
270 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h);
271
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800272#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
273 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
274 free(ppa_fit_addr);
275#endif
276
Hou Zhiqiang00787862016-06-28 20:18:14 +0800277 return ret;
278}