2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-06-28 12:18:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 NXP Semiconductor, Inc.
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2017-03-17 08:12:32 +00:00
|
|
|
#include <malloc.h>
|
2016-06-28 12:18:14 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/types.h>
|
|
|
|
#include <asm/arch/soc.h>
|
|
|
|
#ifdef CONFIG_FSL_LSCH3
|
|
|
|
#include <asm/arch/immap_lsch3.h>
|
|
|
|
#elif defined(CONFIG_FSL_LSCH2)
|
|
|
|
#include <asm/arch/immap_lsch2.h>
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
|
|
|
|
#include <asm/armv8/sec_firmware.h>
|
|
|
|
#endif
|
2016-09-01 16:56:44 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
|
|
|
#include <fsl_validate.h>
|
|
|
|
#endif
|
2016-06-28 12:18:14 +00:00
|
|
|
|
2017-03-17 08:12:32 +00:00
|
|
|
#ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
|
|
|
|
#include <nand.h>
|
|
|
|
#elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
|
|
|
|
#include <mmc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2016-06-28 12:18:14 +00:00
|
|
|
int ppa_init(void)
|
|
|
|
{
|
2017-05-15 15:51:59 +00:00
|
|
|
unsigned int el = current_el();
|
2017-03-17 08:12:32 +00:00
|
|
|
void *ppa_fit_addr;
|
2016-06-28 12:18:14 +00:00
|
|
|
u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
|
2017-09-01 08:25:01 +00:00
|
|
|
u32 *loadable_l, *loadable_h;
|
2016-06-28 12:18:14 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-09-01 16:56:44 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
2017-04-19 23:39:12 +00:00
|
|
|
uintptr_t ppa_esbc_hdr = 0;
|
2016-09-01 16:56:44 +00:00
|
|
|
uintptr_t ppa_img_addr = 0;
|
2017-04-19 23:39:12 +00:00
|
|
|
#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
|
|
|
|
defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
|
|
|
|
void *ppa_hdr_ddr;
|
|
|
|
#endif
|
2016-09-01 16:56:44 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-15 15:51:59 +00:00
|
|
|
/* Skip if running at lower exception level */
|
|
|
|
if (el < 3) {
|
|
|
|
debug("Skipping PPA init, running at EL%d\n", el);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-29 11:26:34 +00:00
|
|
|
#ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
|
2016-06-28 12:18:14 +00:00
|
|
|
ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
|
2017-03-17 08:12:32 +00:00
|
|
|
debug("%s: PPA image load from XIP\n", __func__);
|
2017-04-19 23:39:12 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
|
|
|
ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
|
|
|
|
#endif
|
2017-03-17 08:12:32 +00:00
|
|
|
#else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
|
|
|
|
size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
|
|
|
|
|
|
|
|
/* Copy PPA image from MMC/SD/NAND to allocated memory */
|
|
|
|
#ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
|
|
|
|
struct mmc *mmc;
|
|
|
|
int dev = CONFIG_SYS_MMC_ENV_DEV;
|
|
|
|
struct fdt_header *fitp;
|
|
|
|
u32 cnt;
|
2017-04-19 23:39:12 +00:00
|
|
|
u32 blk;
|
2017-03-17 08:12:32 +00:00
|
|
|
|
|
|
|
debug("%s: PPA image load from eMMC/SD\n", __func__);
|
|
|
|
|
|
|
|
ret = mmc_initialize(gd->bd);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s: mmc_initialize() failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
mmc = find_mmc_device(dev);
|
|
|
|
if (!mmc) {
|
|
|
|
printf("PPA: MMC cannot find device for PPA firmware\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mmc_init(mmc);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s: mmc_init() failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fitp = malloc(roundup(fdt_header_len, 512));
|
|
|
|
if (!fitp) {
|
|
|
|
printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
|
|
|
|
roundup(fdt_header_len, 512));
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:39:12 +00:00
|
|
|
blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
|
2017-03-17 08:12:32 +00:00
|
|
|
cnt = DIV_ROUND_UP(fdt_header_len, 512);
|
|
|
|
debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
|
|
|
|
__func__, dev, blk, cnt);
|
2018-09-25 06:47:06 +00:00
|
|
|
ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, fitp);
|
2017-03-17 08:12:32 +00:00
|
|
|
if (ret != cnt) {
|
|
|
|
free(fitp);
|
|
|
|
printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
|
|
|
|
CONFIG_SYS_LS_PPA_FW_ADDR);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fdt_check_header(fitp);
|
|
|
|
if (ret) {
|
|
|
|
free(fitp);
|
|
|
|
printf("%s: fdt_check_header() failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:39:12 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
|
|
|
ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
|
|
|
|
if (!ppa_hdr_ddr) {
|
|
|
|
printf("PPA: malloc failed for PPA header\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
|
|
|
|
cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
|
2018-09-25 06:47:06 +00:00
|
|
|
ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ppa_hdr_ddr);
|
2017-04-19 23:39:12 +00:00
|
|
|
if (ret != cnt) {
|
|
|
|
free(ppa_hdr_ddr);
|
|
|
|
printf("MMC/SD read of PPA header failed\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
|
|
|
|
|
|
|
|
ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
|
|
|
|
#endif
|
|
|
|
|
2017-03-17 08:12:32 +00:00
|
|
|
fw_length = fdt_totalsize(fitp);
|
|
|
|
free(fitp);
|
|
|
|
|
|
|
|
fw_length = roundup(fw_length, 512);
|
|
|
|
ppa_fit_addr = malloc(fw_length);
|
|
|
|
if (!ppa_fit_addr) {
|
|
|
|
printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
|
|
|
|
fw_length);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:39:12 +00:00
|
|
|
blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
|
2017-03-17 08:12:32 +00:00
|
|
|
cnt = DIV_ROUND_UP(fw_length, 512);
|
|
|
|
debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
|
|
|
|
__func__, dev, blk, cnt);
|
2018-09-25 06:47:06 +00:00
|
|
|
ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ppa_fit_addr);
|
2017-03-17 08:12:32 +00:00
|
|
|
if (ret != cnt) {
|
|
|
|
free(ppa_fit_addr);
|
|
|
|
printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
|
|
|
|
CONFIG_SYS_LS_PPA_FW_ADDR);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
|
|
|
|
struct fdt_header fit;
|
|
|
|
|
|
|
|
debug("%s: PPA image load from NAND\n", __func__);
|
|
|
|
|
|
|
|
nand_init();
|
2017-06-27 00:13:07 +00:00
|
|
|
ret = nand_read(get_nand_dev_by_index(0),
|
|
|
|
(loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
|
|
|
|
&fdt_header_len, (u_char *)&fit);
|
2017-03-17 08:12:32 +00:00
|
|
|
if (ret == -EUCLEAN) {
|
|
|
|
printf("NAND read of PPA FIT header at offset 0x%x failed\n",
|
|
|
|
CONFIG_SYS_LS_PPA_FW_ADDR);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fdt_check_header(&fit);
|
|
|
|
if (ret) {
|
|
|
|
printf("%s: fdt_check_header() failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:39:12 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
|
|
|
ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
|
|
|
|
if (!ppa_hdr_ddr) {
|
|
|
|
printf("PPA: malloc failed for PPA header\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
|
|
|
|
|
2017-06-27 00:13:07 +00:00
|
|
|
ret = nand_read(get_nand_dev_by_index(0),
|
|
|
|
(loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
|
|
|
|
&fw_length, (u_char *)ppa_hdr_ddr);
|
2017-04-19 23:39:12 +00:00
|
|
|
if (ret == -EUCLEAN) {
|
|
|
|
free(ppa_hdr_ddr);
|
|
|
|
printf("NAND read of PPA firmware at offset 0x%x failed\n",
|
|
|
|
CONFIG_SYS_LS_PPA_FW_ADDR);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
|
|
|
|
|
|
|
|
ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
|
|
|
|
#endif
|
|
|
|
|
2017-03-17 08:12:32 +00:00
|
|
|
fw_length = fdt_totalsize(&fit);
|
|
|
|
|
|
|
|
ppa_fit_addr = malloc(fw_length);
|
|
|
|
if (!ppa_fit_addr) {
|
|
|
|
printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
|
|
|
|
fw_length);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-06-27 00:13:07 +00:00
|
|
|
ret = nand_read(get_nand_dev_by_index(0),
|
|
|
|
(loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
|
|
|
|
&fw_length, (u_char *)ppa_fit_addr);
|
2017-03-17 08:12:32 +00:00
|
|
|
if (ret == -EUCLEAN) {
|
|
|
|
free(ppa_fit_addr);
|
|
|
|
printf("NAND read of PPA firmware at offset 0x%x failed\n",
|
|
|
|
CONFIG_SYS_LS_PPA_FW_ADDR);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2016-06-28 12:18:14 +00:00
|
|
|
#else
|
|
|
|
#error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
|
|
|
|
#endif
|
|
|
|
|
2017-03-17 08:12:32 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 16:56:44 +00:00
|
|
|
#ifdef CONFIG_CHAIN_OF_TRUST
|
|
|
|
ppa_img_addr = (uintptr_t)ppa_fit_addr;
|
|
|
|
if (fsl_check_boot_mode_secure() != 0) {
|
2017-04-19 23:39:12 +00:00
|
|
|
/*
|
|
|
|
* In case of failure in validation, fsl_secboot_validate
|
|
|
|
* would not return back in case of Production environment
|
|
|
|
* with ITS=1. In Development environment (ITS=0 and
|
|
|
|
* SB_EN=1), the function may return back in case of
|
|
|
|
* non-fatal failures.
|
|
|
|
*/
|
2016-09-01 16:56:44 +00:00
|
|
|
ret = fsl_secboot_validate(ppa_esbc_hdr,
|
2017-03-23 08:18:14 +00:00
|
|
|
PPA_KEY_HASH,
|
2016-09-01 16:56:44 +00:00
|
|
|
&ppa_img_addr);
|
|
|
|
if (ret != 0)
|
2017-09-01 08:25:01 +00:00
|
|
|
printf("SEC firmware(s) validation failed\n");
|
2016-09-01 16:56:44 +00:00
|
|
|
else
|
2017-09-01 08:25:01 +00:00
|
|
|
printf("SEC firmware(s) validation Successful\n");
|
2016-09-01 16:56:44 +00:00
|
|
|
}
|
2017-04-19 23:39:12 +00:00
|
|
|
#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
|
|
|
|
defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
|
|
|
|
free(ppa_hdr_ddr);
|
|
|
|
#endif
|
2016-09-01 16:56:44 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-28 12:18:14 +00:00
|
|
|
#ifdef CONFIG_FSL_LSCH3
|
|
|
|
struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
|
|
|
|
boot_loc_ptr_l = &gur->bootlocptrl;
|
|
|
|
boot_loc_ptr_h = &gur->bootlocptrh;
|
2017-09-01 08:25:01 +00:00
|
|
|
|
|
|
|
/* Assign addresses to loadable ptrs */
|
|
|
|
loadable_l = &gur->scratchrw[4];
|
|
|
|
loadable_h = &gur->scratchrw[5];
|
2016-06-28 12:18:14 +00:00
|
|
|
#elif defined(CONFIG_FSL_LSCH2)
|
|
|
|
struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
|
|
|
|
boot_loc_ptr_l = &scfg->scratchrw[1];
|
|
|
|
boot_loc_ptr_h = &scfg->scratchrw[0];
|
2017-09-01 08:25:01 +00:00
|
|
|
|
|
|
|
/* Assign addresses to loadable ptrs */
|
|
|
|
loadable_l = &scfg->scratchrw[2];
|
|
|
|
loadable_h = &scfg->scratchrw[3];
|
2016-06-28 12:18:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
|
|
|
|
boot_loc_ptr_l, boot_loc_ptr_h);
|
2017-09-01 08:25:01 +00:00
|
|
|
ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h,
|
|
|
|
loadable_l, loadable_h);
|
2016-06-28 12:18:14 +00:00
|
|
|
|
2017-03-17 08:12:32 +00:00
|
|
|
#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
|
|
|
|
defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
|
|
|
|
free(ppa_fit_addr);
|
|
|
|
#endif
|
|
|
|
|
2016-06-28 12:18:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|