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