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