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