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> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <asm/cache.h> |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 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 Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 21 | #ifdef CONFIG_CHAIN_OF_TRUST |
| 22 | #include <fsl_validate.h> |
| 23 | #endif |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 24 | |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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 | |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 33 | int ppa_init(void) |
| 34 | { |
York Sun | e6b871e | 2017-05-15 08:51:59 -0700 | [diff] [blame] | 35 | unsigned int el = current_el(); |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 36 | void *ppa_fit_addr; |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 37 | u32 *boot_loc_ptr_l, *boot_loc_ptr_h; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 38 | u32 *loadable_l, *loadable_h; |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 39 | int ret; |
| 40 | |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 41 | #ifdef CONFIG_CHAIN_OF_TRUST |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 42 | uintptr_t ppa_esbc_hdr = 0; |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 43 | uintptr_t ppa_img_addr = 0; |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 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 |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 48 | #endif |
| 49 | |
York Sun | e6b871e | 2017-05-15 08:51:59 -0700 | [diff] [blame] | 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 | |
Hou Zhiqiang | 7f83a5f | 2016-07-29 19:26:34 +0800 | [diff] [blame] | 56 | #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 57 | ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR; |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 58 | debug("%s: PPA image load from XIP\n", __func__); |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 59 | #ifdef CONFIG_CHAIN_OF_TRUST |
| 60 | ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR; |
| 61 | #endif |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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; |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 71 | u32 blk; |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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 | |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 99 | blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512; |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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); |
Yinbo Zhu | 45c20bd | 2018-09-25 14:47:06 +0800 | [diff] [blame] | 103 | ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, fitp); |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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 Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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 | } |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 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); |
Yinbo Zhu | 45c20bd | 2018-09-25 14:47:06 +0800 | [diff] [blame] | 127 | 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] | 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 Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 135 | ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr; |
| 136 | #endif |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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 | |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 149 | blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512; |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 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); |
Yinbo Zhu | 45c20bd | 2018-09-25 14:47:06 +0800 | [diff] [blame] | 153 | 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] | 154 | if (ret != cnt) { |
| 155 | free(ppa_fit_addr); |
| 156 | printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n", |
| 157 | CONFIG_SYS_LS_PPA_FW_ADDR); |
| 158 | return -EIO; |
| 159 | } |
| 160 | |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 161 | #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND) |
| 162 | struct fdt_header fit; |
| 163 | |
| 164 | debug("%s: PPA image load from NAND\n", __func__); |
| 165 | |
| 166 | nand_init(); |
Grygorii Strashko | 3f24355 | 2017-06-26 19:13:07 -0500 | [diff] [blame] | 167 | ret = nand_read(get_nand_dev_by_index(0), |
| 168 | (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR, |
| 169 | &fdt_header_len, (u_char *)&fit); |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 170 | if (ret == -EUCLEAN) { |
| 171 | printf("NAND read of PPA FIT header at offset 0x%x failed\n", |
| 172 | CONFIG_SYS_LS_PPA_FW_ADDR); |
| 173 | return -EIO; |
| 174 | } |
| 175 | |
| 176 | ret = fdt_check_header(&fit); |
| 177 | if (ret) { |
| 178 | printf("%s: fdt_check_header() failed\n", __func__); |
| 179 | return ret; |
| 180 | } |
| 181 | |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 182 | #ifdef CONFIG_CHAIN_OF_TRUST |
| 183 | ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE); |
| 184 | if (!ppa_hdr_ddr) { |
| 185 | printf("PPA: malloc failed for PPA header\n"); |
| 186 | return -ENOMEM; |
| 187 | } |
| 188 | |
| 189 | fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE; |
| 190 | |
Grygorii Strashko | 3f24355 | 2017-06-26 19:13:07 -0500 | [diff] [blame] | 191 | ret = nand_read(get_nand_dev_by_index(0), |
| 192 | (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR, |
| 193 | &fw_length, (u_char *)ppa_hdr_ddr); |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 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 | |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 202 | ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr; |
| 203 | #endif |
| 204 | |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 205 | fw_length = fdt_totalsize(&fit); |
| 206 | |
| 207 | ppa_fit_addr = malloc(fw_length); |
| 208 | if (!ppa_fit_addr) { |
| 209 | printf("PPA: malloc failed for PPA image(size 0x%zx)\n", |
| 210 | fw_length); |
| 211 | return -ENOMEM; |
| 212 | } |
| 213 | |
Grygorii Strashko | 3f24355 | 2017-06-26 19:13:07 -0500 | [diff] [blame] | 214 | ret = nand_read(get_nand_dev_by_index(0), |
| 215 | (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR, |
| 216 | &fw_length, (u_char *)ppa_fit_addr); |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 217 | if (ret == -EUCLEAN) { |
| 218 | free(ppa_fit_addr); |
| 219 | printf("NAND read of PPA firmware at offset 0x%x failed\n", |
| 220 | CONFIG_SYS_LS_PPA_FW_ADDR); |
| 221 | return -EIO; |
| 222 | } |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 223 | #else |
| 224 | #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined" |
| 225 | #endif |
| 226 | |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 227 | #endif |
| 228 | |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 229 | #ifdef CONFIG_CHAIN_OF_TRUST |
| 230 | ppa_img_addr = (uintptr_t)ppa_fit_addr; |
| 231 | if (fsl_check_boot_mode_secure() != 0) { |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 232 | /* |
| 233 | * In case of failure in validation, fsl_secboot_validate |
| 234 | * would not return back in case of Production environment |
| 235 | * with ITS=1. In Development environment (ITS=0 and |
| 236 | * SB_EN=1), the function may return back in case of |
| 237 | * non-fatal failures. |
| 238 | */ |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 239 | ret = fsl_secboot_validate(ppa_esbc_hdr, |
Vinitha Pillai-B57223 | a4b3ded | 2017-03-23 13:48:14 +0530 | [diff] [blame] | 240 | PPA_KEY_HASH, |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 241 | &ppa_img_addr); |
| 242 | if (ret != 0) |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 243 | printf("SEC firmware(s) validation failed\n"); |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 244 | else |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 245 | printf("SEC firmware(s) validation Successful\n"); |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 246 | } |
Sumit Garg | 22a66f8 | 2017-04-20 05:09:12 +0530 | [diff] [blame] | 247 | #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \ |
| 248 | defined(CONFIG_SYS_LS_PPA_FW_IN_NAND) |
| 249 | free(ppa_hdr_ddr); |
| 250 | #endif |
Sumit Garg | e0f9e9b | 2016-09-01 12:56:44 -0400 | [diff] [blame] | 251 | #endif |
| 252 | |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 253 | #ifdef CONFIG_FSL_LSCH3 |
| 254 | struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR); |
| 255 | boot_loc_ptr_l = &gur->bootlocptrl; |
| 256 | boot_loc_ptr_h = &gur->bootlocptrh; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 257 | |
| 258 | /* Assign addresses to loadable ptrs */ |
| 259 | loadable_l = &gur->scratchrw[4]; |
| 260 | loadable_h = &gur->scratchrw[5]; |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 261 | #elif defined(CONFIG_FSL_LSCH2) |
| 262 | struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR); |
| 263 | boot_loc_ptr_l = &scfg->scratchrw[1]; |
| 264 | boot_loc_ptr_h = &scfg->scratchrw[0]; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 265 | |
| 266 | /* Assign addresses to loadable ptrs */ |
| 267 | loadable_l = &scfg->scratchrw[2]; |
| 268 | loadable_h = &scfg->scratchrw[3]; |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 269 | #endif |
| 270 | |
| 271 | debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n", |
| 272 | boot_loc_ptr_l, boot_loc_ptr_h); |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 273 | ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h, |
| 274 | loadable_l, loadable_h); |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 275 | |
Hou Zhiqiang | c697bd1 | 2017-03-17 16:12:32 +0800 | [diff] [blame] | 276 | #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \ |
| 277 | defined(CONFIG_SYS_LS_PPA_FW_IN_NAND) |
| 278 | free(ppa_fit_addr); |
| 279 | #endif |
| 280 | |
Hou Zhiqiang | 0078786 | 2016-06-28 20:18:14 +0800 | [diff] [blame] | 281 | return ret; |
| 282 | } |