1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 NXP Semiconductor, Inc.
9 #include <fdt_support.h>
12 #include <asm/cache.h>
13 #include <linux/kernel.h>
15 #include <asm/system.h>
16 #include <asm/types.h>
17 #include <asm/macro.h>
18 #include <asm/armv8/sec_firmware.h>
20 DECLARE_GLOBAL_DATA_PTR;
21 extern void c_runtime_cpu_setup(void);
23 #define SEC_FIRMWARE_LOADED 0x1
24 #define SEC_FIRMWARE_RUNNING 0x2
25 #define SEC_FIRMWARE_ADDR_MASK (~0x3)
27 * Secure firmware load addr
28 * Flags used: 0x1 secure firmware has been loaded to secure memory
29 * 0x2 secure firmware is running
31 phys_addr_t sec_firmware_addr;
33 #ifndef SEC_FIRMWARE_FIT_IMAGE
34 #define SEC_FIRMWARE_FIT_IMAGE "firmware"
36 #ifndef SEC_FIRMWARE_FIT_CNF_NAME
37 #define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
39 #ifndef SEC_FIRMWARE_TARGET_EL
40 #define SEC_FIRMWARE_TARGET_EL 2
43 static int sec_firmware_get_data(const void *sec_firmware_img,
44 const void **data, size_t *size)
46 int conf_node_off, fw_node_off;
47 char *conf_node_name = NULL;
51 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
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);
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);
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");
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);
79 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
81 printf("SEC Firmware: Can't get description\n");
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.
93 * Returns 0 on success and a negative errno on error task fail.
95 static int sec_firmware_parse_image(const void *sec_firmware_img,
96 const void **raw_image_addr,
97 size_t *raw_image_size)
101 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
106 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
107 *raw_image_addr, *raw_image_size);
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).
117 * Returns 0 on success and a negative errno on error task fail.
119 static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
120 u32 *loadable_l, u32 *loadable_h)
122 phys_addr_t sec_firmware_loadable_addr = 0;
123 int conf_node_off, ld_node_off, images;
124 char *conf_node_name = NULL;
128 const char *name, *str, *type;
131 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
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);
139 /* find the node holding the images information */
140 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
142 printf("%s: Cannot find /images node: %d\n", __func__, images);
146 type = FIT_LOADABLE_PROP;
148 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
150 /* Loadables not present */
154 printf("SEC Firmware: '%s' present in config\n", type);
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,
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");
172 if (fit_image_get_data(sec_firmware_img, ld_node_off,
174 printf("SEC Loadable: Can't get subimage data/size");
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");
184 /* Compute load address for loadable in secure memory */
185 sec_firmware_loadable_addr = (sec_firmware_addr -
186 gd->arch.tlb_size) + load;
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);
195 /* Populate loadable address only for Trusted OS */
196 if (!strcmp(str, "trustedOS@1")) {
198 * Populate address ptrs for loadable image with
201 out_le32(loadable_l, (sec_firmware_loadable_addr &
203 out_le32(loadable_h, (sec_firmware_loadable_addr >>
211 static int sec_firmware_copy_image(const char *title,
212 u64 image_addr, u32 image_size, u64 sec_firmware)
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);
222 * This function will parse the SEC Firmware image, and then load it
223 * to secure memory. Also load any loadable if present along with SEC
226 static int sec_firmware_load_image(const void *sec_firmware_img,
227 u32 *loadable_l, u32 *loadable_h)
229 const void *raw_image_addr;
230 size_t raw_image_size = 0;
234 * The Excetpion Level must be EL3 to load and initialize
237 if (current_el() != 3) {
242 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
244 * The SEC Firmware must be stored in secure memory.
245 * Append SEC Firmware to secure mmu table.
247 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
252 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
255 #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
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);
263 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
269 * Check if the end addr of SEC Firmware has been extend the secure
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);
281 * Check if any loadable are present along with firmware image, if
284 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
289 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
290 debug("SEC Firmware: Entry point: 0x%llx\n",
291 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
296 printf("SEC Firmware: error (%d)\n", ret);
297 sec_firmware_addr = 0;
302 static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
304 const void *entry = (void *)(sec_firmware_addr &
305 SEC_FIRMWARE_ADDR_MASK);
307 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
310 /* Check the secure firmware FIT image */
311 __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
313 if (fdt_check_header(sec_firmware_img)) {
314 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
318 if (!fit_check_format(sec_firmware_img)) {
319 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
326 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
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.
336 * The return value on success is PSCI version in format
337 * major[31:16]:minor[15:0].
339 unsigned int sec_firmware_support_psci_version(void)
341 if (current_el() == SEC_FIRMWARE_TARGET_EL)
342 return _sec_firmware_support_psci_version();
344 return PSCI_INVALID_VER;
349 * Check with sec_firmware if it supports random number generation
352 * The return value will be true if it is supported
354 bool sec_firmware_support_hwrng(void)
356 #ifdef CONFIG_TFABOOT
357 /* return true as TFA has one job ring reserved */
360 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
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
373 int sec_firmware_get_random(uint8_t *rand, int bytes)
375 unsigned long long num;
379 if (!bytes || bytes > 8) {
380 printf("Max Random bytes genration supported is 8\n");
383 #define SIP_RNG_64 0xC200FF11
384 regs.regs[0] = SIP_RNG_64;
390 regs.regs[1] = param1;
398 memcpy(rand, &num, bytes);
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
408 * @loadable_l: the address to hold loadable address low
409 * @loadable_h: the address to hold loadable address high
411 int sec_firmware_init(const void *sec_firmware_img,
419 if (!sec_firmware_is_valid(sec_firmware_img))
422 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
425 printf("SEC Firmware: Failed to load image\n");
427 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
428 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
430 printf("SEC Firmware: Failed to initialize\n");
435 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
439 * The PE will be turned into target EL when returned from
442 if (current_el() != SEC_FIRMWARE_TARGET_EL)
445 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
447 /* Set exception table and enable caches if it isn't EL3 */
448 if (current_el() != 3) {
449 c_runtime_cpu_setup();
457 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
459 * @eret: 0 in case of error, 1 for success
461 int fdt_fixup_kaslr(void *fdt)
467 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
468 /* Check if random seed generation is supported */
469 if (sec_firmware_support_hwrng() == false) {
470 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
474 ret = sec_firmware_get_random(rand, 8);
476 printf("WARNING: No random number to set kaslr-seed\n");
480 err = fdt_check_header(fdt);
482 printf("fdt_chosen: %s\n", fdt_strerror(err));
486 /* find or create "/chosen" node. */
487 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
491 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
494 printf("WARNING: can't set kaslr-seed %s.\n",