1 /* 2 * Copyright 2016 NXP Semiconductor, Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 #include <common.h> 7 #include <malloc.h> 8 #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 21 #ifdef CONFIG_CHAIN_OF_TRUST 22 #include <fsl_validate.h> 23 #endif 24 25 #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 31 DECLARE_GLOBAL_DATA_PTR; 32 33 int ppa_init(void) 34 { 35 unsigned int el = current_el(); 36 void *ppa_fit_addr; 37 u32 *boot_loc_ptr_l, *boot_loc_ptr_h; 38 u32 *loadable_l, *loadable_h; 39 int ret; 40 41 #ifdef CONFIG_CHAIN_OF_TRUST 42 uintptr_t ppa_esbc_hdr = 0; 43 uintptr_t ppa_img_addr = 0; 44 #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 48 #endif 49 50 /* 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 56 #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP 57 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR; 58 debug("%s: PPA image load from XIP\n", __func__); 59 #ifdef CONFIG_CHAIN_OF_TRUST 60 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR; 61 #endif 62 #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; 71 u32 blk; 72 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 99 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512; 100 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 111 ret = fdt_check_header(fitp); 112 if (ret) { 113 free(fitp); 114 printf("%s: fdt_check_header() failed\n", __func__); 115 return ret; 116 } 117 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 135 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr; 136 #endif 137 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 149 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512; 150 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 162 #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(); 168 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); 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 183 #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(get_nand_dev_by_index(0), 193 (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR, 194 &fw_length, (u_char *)ppa_hdr_ddr); 195 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 203 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr; 204 #endif 205 206 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 215 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); 218 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 } 224 #else 225 #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined" 226 #endif 227 228 #endif 229 230 #ifdef CONFIG_CHAIN_OF_TRUST 231 ppa_img_addr = (uintptr_t)ppa_fit_addr; 232 if (fsl_check_boot_mode_secure() != 0) { 233 /* 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 */ 240 ret = fsl_secboot_validate(ppa_esbc_hdr, 241 PPA_KEY_HASH, 242 &ppa_img_addr); 243 if (ret != 0) 244 printf("SEC firmware(s) validation failed\n"); 245 else 246 printf("SEC firmware(s) validation Successful\n"); 247 } 248 #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 252 #endif 253 254 #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; 258 259 /* Assign addresses to loadable ptrs */ 260 loadable_l = &gur->scratchrw[4]; 261 loadable_h = &gur->scratchrw[5]; 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 267 /* Assign addresses to loadable ptrs */ 268 loadable_l = &scfg->scratchrw[2]; 269 loadable_h = &scfg->scratchrw[3]; 270 #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); 274 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h, 275 loadable_l, loadable_h); 276 277 #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 282 return ret; 283 } 284