1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 NXP Semiconductor, Inc.
9 #include <fdt_support.h>
11 #include <asm/cache.h>
12 #include <linux/kernel.h>
14 #include <asm/system.h>
15 #include <asm/types.h>
16 #include <asm/macro.h>
17 #include <asm/armv8/sec_firmware.h>
19 DECLARE_GLOBAL_DATA_PTR;
20 extern void c_runtime_cpu_setup(void);
22 #define SEC_FIRMWARE_LOADED 0x1
23 #define SEC_FIRMWARE_RUNNING 0x2
24 #define SEC_FIRMWARE_ADDR_MASK (~0x3)
26 * Secure firmware load addr
27 * Flags used: 0x1 secure firmware has been loaded to secure memory
28 * 0x2 secure firmware is running
30 phys_addr_t sec_firmware_addr;
32 #ifndef SEC_FIRMWARE_FIT_IMAGE
33 #define SEC_FIRMWARE_FIT_IMAGE "firmware"
35 #ifndef SEC_FIRMWARE_FIT_CNF_NAME
36 #define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
38 #ifndef SEC_FIRMWARE_TARGET_EL
39 #define SEC_FIRMWARE_TARGET_EL 2
42 static int sec_firmware_get_data(const void *sec_firmware_img,
43 const void **data, size_t *size)
45 int conf_node_off, fw_node_off;
46 char *conf_node_name = NULL;
50 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
52 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
53 if (conf_node_off < 0) {
54 printf("SEC Firmware: %s: no such config\n", conf_node_name);
58 fw_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off,
59 SEC_FIRMWARE_FIT_IMAGE);
60 if (fw_node_off < 0) {
61 printf("SEC Firmware: No '%s' in config\n",
62 SEC_FIRMWARE_FIT_IMAGE);
66 /* Verify secure firmware image */
67 if (!(fit_image_verify(sec_firmware_img, fw_node_off))) {
68 printf("SEC Firmware: Bad firmware image (bad CRC)\n");
72 if (fit_image_get_data(sec_firmware_img, fw_node_off, data, size)) {
73 printf("SEC Firmware: Can't get %s subimage data/size",
74 SEC_FIRMWARE_FIT_IMAGE);
78 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
80 printf("SEC Firmware: Can't get description\n");
88 * SEC Firmware FIT image parser checks if the image is in FIT
89 * format, verifies integrity of the image and calculates raw
90 * image address and size values.
92 * Returns 0 on success and a negative errno on error task fail.
94 static int sec_firmware_parse_image(const void *sec_firmware_img,
95 const void **raw_image_addr,
96 size_t *raw_image_size)
100 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
105 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
106 *raw_image_addr, *raw_image_size);
112 * SEC Firmware FIT image parser to check if any loadable is
113 * present. If present, verify integrity of the loadable and
114 * copy loadable to address provided in (loadable_h, loadable_l).
116 * Returns 0 on success and a negative errno on error task fail.
118 static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
119 u32 *loadable_l, u32 *loadable_h)
121 phys_addr_t sec_firmware_loadable_addr = 0;
122 int conf_node_off, ld_node_off, images;
123 char *conf_node_name = NULL;
127 const char *name, *str, *type;
130 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
132 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
133 if (conf_node_off < 0) {
134 printf("SEC Firmware: %s: no such config\n", conf_node_name);
138 /* find the node holding the images information */
139 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
141 printf("%s: Cannot find /images node: %d\n", __func__, images);
145 type = FIT_LOADABLE_PROP;
147 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
149 /* Loadables not present */
153 printf("SEC Firmware: '%s' present in config\n", type);
155 for (str = name; str && ((str - name) < len);
156 str = strchr(str, '\0') + 1) {
157 printf("%s: '%s'\n", type, str);
158 ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str);
159 if (ld_node_off < 0) {
160 printf("cannot find image node '%s': %d\n", str,
165 /* Verify secure firmware image */
166 if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
167 printf("SEC Loadable: Bad loadable image (bad CRC)\n");
171 if (fit_image_get_data(sec_firmware_img, ld_node_off,
173 printf("SEC Loadable: Can't get subimage data/size");
177 /* Get load address, treated as load offset to secure memory */
178 if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
179 printf("SEC Loadable: Can't get subimage load");
183 /* Compute load address for loadable in secure memory */
184 sec_firmware_loadable_addr = (sec_firmware_addr -
185 gd->arch.tlb_size) + load;
187 /* Copy loadable to secure memory and flush dcache */
188 debug("%s copied to address 0x%p\n",
189 FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
190 memcpy((void *)sec_firmware_loadable_addr, data, size);
191 flush_dcache_range(sec_firmware_loadable_addr,
192 sec_firmware_loadable_addr + size);
194 /* Populate loadable address only for Trusted OS */
195 if (!strcmp(str, "trustedOS@1")) {
197 * Populate address ptrs for loadable image with
200 out_le32(loadable_l, (sec_firmware_loadable_addr &
202 out_le32(loadable_h, (sec_firmware_loadable_addr >>
210 static int sec_firmware_copy_image(const char *title,
211 u64 image_addr, u32 image_size, u64 sec_firmware)
213 debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
214 memcpy((void *)sec_firmware, (void *)image_addr, image_size);
215 flush_dcache_range(sec_firmware, sec_firmware + image_size);
221 * This function will parse the SEC Firmware image, and then load it
222 * to secure memory. Also load any loadable if present along with SEC
225 static int sec_firmware_load_image(const void *sec_firmware_img,
226 u32 *loadable_l, u32 *loadable_h)
228 const void *raw_image_addr;
229 size_t raw_image_size = 0;
233 * The Excetpion Level must be EL3 to load and initialize
236 if (current_el() != 3) {
241 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
243 * The SEC Firmware must be stored in secure memory.
244 * Append SEC Firmware to secure mmu table.
246 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
251 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
254 #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
257 /* Align SEC Firmware base address to 4K */
258 sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
259 debug("SEC Firmware: Load address: 0x%llx\n",
260 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
262 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
268 * Check if the end addr of SEC Firmware has been extend the secure
272 /* Copy the secure firmware to secure memory */
273 ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
274 raw_image_size, sec_firmware_addr &
275 SEC_FIRMWARE_ADDR_MASK);
280 * Check if any loadable are present along with firmware image, if
283 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
288 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
289 debug("SEC Firmware: Entry point: 0x%llx\n",
290 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
295 printf("SEC Firmware: error (%d)\n", ret);
296 sec_firmware_addr = 0;
301 static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
303 const void *entry = (void *)(sec_firmware_addr &
304 SEC_FIRMWARE_ADDR_MASK);
306 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
309 /* Check the secure firmware FIT image */
310 __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
312 if (fdt_check_header(sec_firmware_img)) {
313 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
317 if (!fit_check_format(sec_firmware_img)) {
318 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
325 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
327 * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
328 * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
329 * number will be returned according to SMC Calling Conventions. But
330 * when getting the NOT_SUPPORTED error number, we cannot ensure if
331 * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
332 * won't be supported by this framework.
333 * And if the secure firmware isn't running, return NOT_SUPPORTED.
335 * The return value on success is PSCI version in format
336 * major[31:16]:minor[15:0].
338 unsigned int sec_firmware_support_psci_version(void)
340 if (current_el() == SEC_FIRMWARE_TARGET_EL)
341 return _sec_firmware_support_psci_version();
343 return PSCI_INVALID_VER;
348 * Check with sec_firmware if it supports random number generation
351 * The return value will be true if it is supported
353 bool sec_firmware_support_hwrng(void)
355 #ifdef CONFIG_TFABOOT
356 /* return true as TFA has one job ring reserved */
359 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
367 * sec_firmware_get_random - Get a random number from SEC Firmware
368 * @rand: random number buffer to be filled
369 * @bytes: Number of bytes of random number to be supported
370 * @eret: -1 in case of error, 0 for success
372 int sec_firmware_get_random(uint8_t *rand, int bytes)
374 unsigned long long num;
378 if (!bytes || bytes > 8) {
379 printf("Max Random bytes genration supported is 8\n");
382 #define SIP_RNG_64 0xC200FF11
383 regs.regs[0] = SIP_RNG_64;
389 regs.regs[1] = param1;
397 memcpy(rand, &num, bytes);
403 * sec_firmware_init - Initialize the SEC Firmware
404 * @sec_firmware_img: the SEC Firmware image address
405 * @eret_hold_l: the address to hold exception return address low
406 * @eret_hold_h: the address to hold exception return address high
407 * @loadable_l: the address to hold loadable address low
408 * @loadable_h: the address to hold loadable address high
410 int sec_firmware_init(const void *sec_firmware_img,
418 if (!sec_firmware_is_valid(sec_firmware_img))
421 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
424 printf("SEC Firmware: Failed to load image\n");
426 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
427 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
429 printf("SEC Firmware: Failed to initialize\n");
434 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
438 * The PE will be turned into target EL when returned from
441 if (current_el() != SEC_FIRMWARE_TARGET_EL)
444 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
446 /* Set exception table and enable caches if it isn't EL3 */
447 if (current_el() != 3) {
448 c_runtime_cpu_setup();
456 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
458 * @eret: 0 in case of error, 1 for success
460 int fdt_fixup_kaslr(void *fdt)
466 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
467 /* Check if random seed generation is supported */
468 if (sec_firmware_support_hwrng() == false) {
469 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
473 ret = sec_firmware_get_random(rand, 8);
475 printf("WARNING: No random number to set kaslr-seed\n");
479 err = fdt_check_header(fdt);
481 printf("fdt_chosen: %s\n", fdt_strerror(err));
485 /* find or create "/chosen" node. */
486 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
490 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
493 printf("WARNING: can't set kaslr-seed %s.\n",