Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 NXP Semiconductor, Inc. |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 9 | #include <fdt_support.h> |
| 10 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 11 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <asm/cache.h> |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <asm/types.h> |
| 17 | #include <asm/macro.h> |
| 18 | #include <asm/armv8/sec_firmware.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | extern void c_runtime_cpu_setup(void); |
| 22 | |
| 23 | #define SEC_FIRMWARE_LOADED 0x1 |
| 24 | #define SEC_FIRMWARE_RUNNING 0x2 |
| 25 | #define SEC_FIRMWARE_ADDR_MASK (~0x3) |
Hou Zhiqiang | 9c2eca1 | 2016-09-06 14:23:07 +0800 | [diff] [blame] | 26 | /* |
| 27 | * Secure firmware load addr |
| 28 | * Flags used: 0x1 secure firmware has been loaded to secure memory |
| 29 | * 0x2 secure firmware is running |
| 30 | */ |
| 31 | phys_addr_t sec_firmware_addr; |
| 32 | |
| 33 | #ifndef SEC_FIRMWARE_FIT_IMAGE |
| 34 | #define SEC_FIRMWARE_FIT_IMAGE "firmware" |
| 35 | #endif |
Thomas Hebb | 1dbd3d1 | 2019-11-10 08:23:15 -0800 | [diff] [blame] | 36 | #ifndef SEC_FIRMWARE_FIT_CNF_NAME |
| 37 | #define SEC_FIRMWARE_FIT_CNF_NAME "config-1" |
Hou Zhiqiang | 9c2eca1 | 2016-09-06 14:23:07 +0800 | [diff] [blame] | 38 | #endif |
| 39 | #ifndef SEC_FIRMWARE_TARGET_EL |
| 40 | #define SEC_FIRMWARE_TARGET_EL 2 |
| 41 | #endif |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 42 | |
| 43 | static int sec_firmware_get_data(const void *sec_firmware_img, |
| 44 | const void **data, size_t *size) |
| 45 | { |
| 46 | int conf_node_off, fw_node_off; |
| 47 | char *conf_node_name = NULL; |
| 48 | char *desc; |
| 49 | int ret; |
| 50 | |
Thomas Hebb | 1dbd3d1 | 2019-11-10 08:23:15 -0800 | [diff] [blame] | 51 | conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME; |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 52 | |
| 53 | conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name); |
| 54 | if (conf_node_off < 0) { |
| 55 | printf("SEC Firmware: %s: no such config\n", conf_node_name); |
| 56 | return -ENOENT; |
| 57 | } |
| 58 | |
| 59 | fw_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off, |
| 60 | SEC_FIRMWARE_FIT_IMAGE); |
| 61 | if (fw_node_off < 0) { |
| 62 | printf("SEC Firmware: No '%s' in config\n", |
| 63 | SEC_FIRMWARE_FIT_IMAGE); |
| 64 | return -ENOLINK; |
| 65 | } |
| 66 | |
| 67 | /* Verify secure firmware image */ |
| 68 | if (!(fit_image_verify(sec_firmware_img, fw_node_off))) { |
| 69 | printf("SEC Firmware: Bad firmware image (bad CRC)\n"); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | if (fit_image_get_data(sec_firmware_img, fw_node_off, data, size)) { |
| 74 | printf("SEC Firmware: Can't get %s subimage data/size", |
| 75 | SEC_FIRMWARE_FIT_IMAGE); |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | |
| 79 | ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc); |
| 80 | if (ret) |
| 81 | printf("SEC Firmware: Can't get description\n"); |
| 82 | else |
| 83 | printf("%s\n", desc); |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * SEC Firmware FIT image parser checks if the image is in FIT |
| 90 | * format, verifies integrity of the image and calculates raw |
| 91 | * image address and size values. |
| 92 | * |
| 93 | * Returns 0 on success and a negative errno on error task fail. |
| 94 | */ |
| 95 | static int sec_firmware_parse_image(const void *sec_firmware_img, |
| 96 | const void **raw_image_addr, |
| 97 | size_t *raw_image_size) |
| 98 | { |
| 99 | int ret; |
| 100 | |
| 101 | ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr, |
| 102 | raw_image_size); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n", |
| 107 | *raw_image_addr, *raw_image_size); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 112 | /* |
| 113 | * SEC Firmware FIT image parser to check if any loadable is |
| 114 | * present. If present, verify integrity of the loadable and |
| 115 | * copy loadable to address provided in (loadable_h, loadable_l). |
| 116 | * |
| 117 | * Returns 0 on success and a negative errno on error task fail. |
| 118 | */ |
| 119 | static int sec_firmware_check_copy_loadable(const void *sec_firmware_img, |
| 120 | u32 *loadable_l, u32 *loadable_h) |
| 121 | { |
| 122 | phys_addr_t sec_firmware_loadable_addr = 0; |
Sumit Garg | d521708 | 2018-04-24 03:23:28 +0530 | [diff] [blame] | 123 | int conf_node_off, ld_node_off, images; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 124 | char *conf_node_name = NULL; |
| 125 | const void *data; |
| 126 | size_t size; |
| 127 | ulong load; |
Sumit Garg | d521708 | 2018-04-24 03:23:28 +0530 | [diff] [blame] | 128 | const char *name, *str, *type; |
| 129 | int len; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 130 | |
Thomas Hebb | 1dbd3d1 | 2019-11-10 08:23:15 -0800 | [diff] [blame] | 131 | conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 132 | |
| 133 | conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name); |
| 134 | if (conf_node_off < 0) { |
| 135 | printf("SEC Firmware: %s: no such config\n", conf_node_name); |
Sumit Garg | d521708 | 2018-04-24 03:23:28 +0530 | [diff] [blame] | 136 | return -ENOENT; |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 137 | } |
| 138 | |
Sumit Garg | d521708 | 2018-04-24 03:23:28 +0530 | [diff] [blame] | 139 | /* find the node holding the images information */ |
| 140 | images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH); |
| 141 | if (images < 0) { |
| 142 | printf("%s: Cannot find /images node: %d\n", __func__, images); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | type = FIT_LOADABLE_PROP; |
| 147 | |
| 148 | name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len); |
| 149 | if (!name) { |
| 150 | /* Loadables not present */ |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | printf("SEC Firmware: '%s' present in config\n", type); |
| 155 | |
| 156 | for (str = name; str && ((str - name) < len); |
| 157 | str = strchr(str, '\0') + 1) { |
| 158 | printf("%s: '%s'\n", type, str); |
| 159 | ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str); |
| 160 | if (ld_node_off < 0) { |
| 161 | printf("cannot find image node '%s': %d\n", str, |
| 162 | ld_node_off); |
| 163 | return -EINVAL; |
| 164 | } |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 165 | |
| 166 | /* Verify secure firmware image */ |
| 167 | if (!(fit_image_verify(sec_firmware_img, ld_node_off))) { |
| 168 | printf("SEC Loadable: Bad loadable image (bad CRC)\n"); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | if (fit_image_get_data(sec_firmware_img, ld_node_off, |
| 173 | &data, &size)) { |
| 174 | printf("SEC Loadable: Can't get subimage data/size"); |
| 175 | return -ENOENT; |
| 176 | } |
| 177 | |
| 178 | /* Get load address, treated as load offset to secure memory */ |
| 179 | if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) { |
| 180 | printf("SEC Loadable: Can't get subimage load"); |
| 181 | return -ENOENT; |
| 182 | } |
| 183 | |
| 184 | /* Compute load address for loadable in secure memory */ |
| 185 | sec_firmware_loadable_addr = (sec_firmware_addr - |
| 186 | gd->arch.tlb_size) + load; |
| 187 | |
| 188 | /* Copy loadable to secure memory and flush dcache */ |
| 189 | debug("%s copied to address 0x%p\n", |
| 190 | FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr); |
| 191 | memcpy((void *)sec_firmware_loadable_addr, data, size); |
| 192 | flush_dcache_range(sec_firmware_loadable_addr, |
| 193 | sec_firmware_loadable_addr + size); |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 194 | |
Sumit Garg | d521708 | 2018-04-24 03:23:28 +0530 | [diff] [blame] | 195 | /* Populate loadable address only for Trusted OS */ |
| 196 | if (!strcmp(str, "trustedOS@1")) { |
| 197 | /* |
| 198 | * Populate address ptrs for loadable image with |
| 199 | * loadbale addr |
| 200 | */ |
| 201 | out_le32(loadable_l, (sec_firmware_loadable_addr & |
| 202 | WORD_MASK)); |
| 203 | out_le32(loadable_h, (sec_firmware_loadable_addr >> |
| 204 | WORD_SHIFT)); |
| 205 | } |
| 206 | } |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 211 | static int sec_firmware_copy_image(const char *title, |
| 212 | u64 image_addr, u32 image_size, u64 sec_firmware) |
| 213 | { |
| 214 | debug("%s copied to address 0x%p\n", title, (void *)sec_firmware); |
| 215 | memcpy((void *)sec_firmware, (void *)image_addr, image_size); |
| 216 | flush_dcache_range(sec_firmware, sec_firmware + image_size); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * This function will parse the SEC Firmware image, and then load it |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 223 | * to secure memory. Also load any loadable if present along with SEC |
| 224 | * Firmware image. |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 225 | */ |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 226 | static int sec_firmware_load_image(const void *sec_firmware_img, |
| 227 | u32 *loadable_l, u32 *loadable_h) |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 228 | { |
| 229 | const void *raw_image_addr; |
| 230 | size_t raw_image_size = 0; |
| 231 | int ret; |
| 232 | |
| 233 | /* |
| 234 | * The Excetpion Level must be EL3 to load and initialize |
| 235 | * the SEC Firmware. |
| 236 | */ |
| 237 | if (current_el() != 3) { |
| 238 | ret = -EACCES; |
| 239 | goto out; |
| 240 | } |
| 241 | |
| 242 | #ifdef CONFIG_SYS_MEM_RESERVE_SECURE |
| 243 | /* |
| 244 | * The SEC Firmware must be stored in secure memory. |
| 245 | * Append SEC Firmware to secure mmu table. |
| 246 | */ |
| 247 | if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) { |
| 248 | ret = -ENXIO; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) + |
| 253 | gd->arch.tlb_size; |
| 254 | #else |
| 255 | #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support" |
| 256 | #endif |
| 257 | |
| 258 | /* Align SEC Firmware base address to 4K */ |
| 259 | sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff; |
| 260 | debug("SEC Firmware: Load address: 0x%llx\n", |
| 261 | sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK); |
| 262 | |
| 263 | ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr, |
| 264 | &raw_image_size); |
| 265 | if (ret) |
| 266 | goto out; |
| 267 | |
| 268 | /* TODO: |
| 269 | * Check if the end addr of SEC Firmware has been extend the secure |
| 270 | * memory. |
| 271 | */ |
| 272 | |
| 273 | /* Copy the secure firmware to secure memory */ |
| 274 | ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr, |
| 275 | raw_image_size, sec_firmware_addr & |
| 276 | SEC_FIRMWARE_ADDR_MASK); |
| 277 | if (ret) |
| 278 | goto out; |
| 279 | |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 280 | /* |
| 281 | * Check if any loadable are present along with firmware image, if |
| 282 | * present load them. |
| 283 | */ |
| 284 | ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l, |
| 285 | loadable_h); |
| 286 | if (ret) |
| 287 | goto out; |
| 288 | |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 289 | sec_firmware_addr |= SEC_FIRMWARE_LOADED; |
| 290 | debug("SEC Firmware: Entry point: 0x%llx\n", |
| 291 | sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK); |
| 292 | |
| 293 | return 0; |
| 294 | |
| 295 | out: |
| 296 | printf("SEC Firmware: error (%d)\n", ret); |
| 297 | sec_firmware_addr = 0; |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h) |
| 303 | { |
| 304 | const void *entry = (void *)(sec_firmware_addr & |
| 305 | SEC_FIRMWARE_ADDR_MASK); |
| 306 | |
| 307 | return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h); |
| 308 | } |
| 309 | |
| 310 | /* Check the secure firmware FIT image */ |
| 311 | __weak bool sec_firmware_is_valid(const void *sec_firmware_img) |
| 312 | { |
| 313 | if (fdt_check_header(sec_firmware_img)) { |
| 314 | printf("SEC Firmware: Bad firmware image (not a FIT image)\n"); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (!fit_check_format(sec_firmware_img)) { |
| 319 | printf("SEC Firmware: Bad firmware image (bad FIT header)\n"); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
Hou Zhiqiang | 6be115d | 2017-01-16 17:31:48 +0800 | [diff] [blame] | 326 | #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 327 | /* |
| 328 | * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI |
| 329 | * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error |
| 330 | * number will be returned according to SMC Calling Conventions. But |
| 331 | * when getting the NOT_SUPPORTED error number, we cannot ensure if |
| 332 | * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1 |
| 333 | * won't be supported by this framework. |
| 334 | * And if the secure firmware isn't running, return NOT_SUPPORTED. |
| 335 | * |
| 336 | * The return value on success is PSCI version in format |
| 337 | * major[31:16]:minor[15:0]. |
| 338 | */ |
| 339 | unsigned int sec_firmware_support_psci_version(void) |
| 340 | { |
York Sun | e6b871e | 2017-05-15 08:51:59 -0700 | [diff] [blame] | 341 | if (current_el() == SEC_FIRMWARE_TARGET_EL) |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 342 | return _sec_firmware_support_psci_version(); |
| 343 | |
Yuantian Tang | aec3b14 | 2017-04-19 13:27:39 +0800 | [diff] [blame] | 344 | return PSCI_INVALID_VER; |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 345 | } |
| 346 | #endif |
| 347 | |
| 348 | /* |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 349 | * Check with sec_firmware if it supports random number generation |
| 350 | * via HW RNG |
| 351 | * |
| 352 | * The return value will be true if it is supported |
| 353 | */ |
| 354 | bool sec_firmware_support_hwrng(void) |
| 355 | { |
Pankit Garg | 4696957 | 2018-11-05 18:02:19 +0000 | [diff] [blame] | 356 | #ifdef CONFIG_TFABOOT |
| 357 | /* return true as TFA has one job ring reserved */ |
| 358 | return true; |
| 359 | #endif |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 360 | if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) { |
Thomas Hebb | fd37f24 | 2019-11-13 18:18:03 -0800 | [diff] [blame] | 361 | return true; |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * sec_firmware_get_random - Get a random number from SEC Firmware |
| 369 | * @rand: random number buffer to be filled |
| 370 | * @bytes: Number of bytes of random number to be supported |
| 371 | * @eret: -1 in case of error, 0 for success |
| 372 | */ |
| 373 | int sec_firmware_get_random(uint8_t *rand, int bytes) |
| 374 | { |
| 375 | unsigned long long num; |
| 376 | struct pt_regs regs; |
| 377 | int param1; |
| 378 | |
| 379 | if (!bytes || bytes > 8) { |
| 380 | printf("Max Random bytes genration supported is 8\n"); |
| 381 | return -1; |
| 382 | } |
| 383 | #define SIP_RNG_64 0xC200FF11 |
| 384 | regs.regs[0] = SIP_RNG_64; |
| 385 | |
| 386 | if (bytes <= 4) |
| 387 | param1 = 0; |
| 388 | else |
| 389 | param1 = 1; |
| 390 | regs.regs[1] = param1; |
| 391 | |
| 392 | smc_call(®s); |
| 393 | |
| 394 | if (regs.regs[0]) |
| 395 | return -1; |
| 396 | |
| 397 | num = regs.regs[1]; |
| 398 | memcpy(rand, &num, bytes); |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 404 | * sec_firmware_init - Initialize the SEC Firmware |
| 405 | * @sec_firmware_img: the SEC Firmware image address |
| 406 | * @eret_hold_l: the address to hold exception return address low |
| 407 | * @eret_hold_h: the address to hold exception return address high |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 408 | * @loadable_l: the address to hold loadable address low |
| 409 | * @loadable_h: the address to hold loadable address high |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 410 | */ |
| 411 | int sec_firmware_init(const void *sec_firmware_img, |
| 412 | u32 *eret_hold_l, |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 413 | u32 *eret_hold_h, |
| 414 | u32 *loadable_l, |
| 415 | u32 *loadable_h) |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 416 | { |
| 417 | int ret; |
| 418 | |
| 419 | if (!sec_firmware_is_valid(sec_firmware_img)) |
| 420 | return -EINVAL; |
| 421 | |
Sumit Garg | b6fa55e | 2017-09-01 13:55:01 +0530 | [diff] [blame] | 422 | ret = sec_firmware_load_image(sec_firmware_img, loadable_l, |
| 423 | loadable_h); |
Hou Zhiqiang | 505d735 | 2016-06-28 20:18:13 +0800 | [diff] [blame] | 424 | if (ret) { |
| 425 | printf("SEC Firmware: Failed to load image\n"); |
| 426 | return ret; |
| 427 | } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) { |
| 428 | ret = sec_firmware_entry(eret_hold_l, eret_hold_h); |
| 429 | if (ret) { |
| 430 | printf("SEC Firmware: Failed to initialize\n"); |
| 431 | return ret; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n", |
| 436 | current_el()); |
| 437 | |
| 438 | /* |
| 439 | * The PE will be turned into target EL when returned from |
| 440 | * SEC Firmware. |
| 441 | */ |
| 442 | if (current_el() != SEC_FIRMWARE_TARGET_EL) |
| 443 | return -EACCES; |
| 444 | |
| 445 | sec_firmware_addr |= SEC_FIRMWARE_RUNNING; |
| 446 | |
| 447 | /* Set exception table and enable caches if it isn't EL3 */ |
| 448 | if (current_el() != 3) { |
| 449 | c_runtime_cpu_setup(); |
| 450 | enable_caches(); |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * fdt_fix_kaslr - Add kalsr-seed node in Device tree |
| 458 | * @fdt: Device tree |
| 459 | * @eret: 0 in case of error, 1 for success |
| 460 | */ |
| 461 | int fdt_fixup_kaslr(void *fdt) |
| 462 | { |
| 463 | int nodeoffset; |
| 464 | int err, ret = 0; |
| 465 | u8 rand[8]; |
| 466 | |
| 467 | #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT) |
| 468 | /* Check if random seed generation is supported */ |
Ruchika Gupta | fc8e340 | 2018-04-12 16:24:35 +0530 | [diff] [blame] | 469 | if (sec_firmware_support_hwrng() == false) { |
| 470 | printf("WARNING: SEC firmware not running, no kaslr-seed\n"); |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 471 | return 0; |
Ruchika Gupta | fc8e340 | 2018-04-12 16:24:35 +0530 | [diff] [blame] | 472 | } |
Ruchika Gupta | db204d7 | 2017-08-16 15:58:10 +0530 | [diff] [blame] | 473 | |
| 474 | ret = sec_firmware_get_random(rand, 8); |
| 475 | if (ret < 0) { |
| 476 | printf("WARNING: No random number to set kaslr-seed\n"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | err = fdt_check_header(fdt); |
| 481 | if (err < 0) { |
| 482 | printf("fdt_chosen: %s\n", fdt_strerror(err)); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* find or create "/chosen" node. */ |
| 487 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); |
| 488 | if (nodeoffset < 0) |
| 489 | return 0; |
| 490 | |
| 491 | err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand, |
| 492 | sizeof(rand)); |
| 493 | if (err < 0) { |
| 494 | printf("WARNING: can't set kaslr-seed %s.\n", |
| 495 | fdt_strerror(err)); |
| 496 | return 0; |
| 497 | } |
| 498 | ret = 1; |
| 499 | #endif |
| 500 | |
| 501 | return ret; |
| 502 | } |