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 <asm/ptrace.h>
14 #include <linux/kernel.h>
16 #include <asm/system.h>
17 #include <asm/types.h>
18 #include <asm/macro.h>
19 #include <asm/armv8/sec_firmware.h>
21 DECLARE_GLOBAL_DATA_PTR;
22 extern void c_runtime_cpu_setup(void);
24 #define SEC_FIRMWARE_LOADED 0x1
25 #define SEC_FIRMWARE_RUNNING 0x2
26 #define SEC_FIRMWARE_ADDR_MASK (~0x3)
28 * Secure firmware load addr
29 * Flags used: 0x1 secure firmware has been loaded to secure memory
30 * 0x2 secure firmware is running
32 phys_addr_t sec_firmware_addr;
34 #ifndef SEC_FIRMWARE_FIT_IMAGE
35 #define SEC_FIRMWARE_FIT_IMAGE "firmware"
37 #ifndef SEC_FIRMWARE_FIT_CNF_NAME
38 #define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
40 #ifndef SEC_FIRMWARE_TARGET_EL
41 #define SEC_FIRMWARE_TARGET_EL 2
44 static int sec_firmware_get_data(const void *sec_firmware_img,
45 const void **data, size_t *size)
47 int conf_node_off, fw_node_off;
48 char *conf_node_name = NULL;
52 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
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);
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);
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");
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);
80 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
82 printf("SEC Firmware: Can't get description\n");
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.
94 * Returns 0 on success and a negative errno on error task fail.
96 static int sec_firmware_parse_image(const void *sec_firmware_img,
97 const void **raw_image_addr,
98 size_t *raw_image_size)
102 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
107 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
108 *raw_image_addr, *raw_image_size);
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).
118 * Returns 0 on success and a negative errno on error task fail.
120 static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
121 u32 *loadable_l, u32 *loadable_h)
123 phys_addr_t sec_firmware_loadable_addr = 0;
124 int conf_node_off, ld_node_off, images;
125 char *conf_node_name = NULL;
129 const char *name, *str, *type;
132 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
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);
140 /* find the node holding the images information */
141 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
143 printf("%s: Cannot find /images node: %d\n", __func__, images);
147 type = FIT_LOADABLE_PROP;
149 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
151 /* Loadables not present */
155 printf("SEC Firmware: '%s' present in config\n", type);
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,
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");
173 if (fit_image_get_data(sec_firmware_img, ld_node_off,
175 printf("SEC Loadable: Can't get subimage data/size");
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");
185 /* Compute load address for loadable in secure memory */
186 sec_firmware_loadable_addr = (sec_firmware_addr -
187 gd->arch.tlb_size) + load;
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);
196 /* Populate loadable address only for Trusted OS */
197 if (!strcmp(str, "trustedOS@1")) {
199 * Populate address ptrs for loadable image with
202 out_le32(loadable_l, (sec_firmware_loadable_addr &
204 out_le32(loadable_h, (sec_firmware_loadable_addr >>
212 static int sec_firmware_copy_image(const char *title,
213 u64 image_addr, u32 image_size, u64 sec_firmware)
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);
223 * This function will parse the SEC Firmware image, and then load it
224 * to secure memory. Also load any loadable if present along with SEC
227 static int sec_firmware_load_image(const void *sec_firmware_img,
228 u32 *loadable_l, u32 *loadable_h)
230 const void *raw_image_addr;
231 size_t raw_image_size = 0;
235 * The Excetpion Level must be EL3 to load and initialize
238 if (current_el() != 3) {
243 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
245 * The SEC Firmware must be stored in secure memory.
246 * Append SEC Firmware to secure mmu table.
248 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
253 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
256 #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
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);
264 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
270 * Check if the end addr of SEC Firmware has been extend the secure
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);
282 * Check if any loadable are present along with firmware image, if
285 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
290 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
291 debug("SEC Firmware: Entry point: 0x%llx\n",
292 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
297 printf("SEC Firmware: error (%d)\n", ret);
298 sec_firmware_addr = 0;
303 static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
305 const void *entry = (void *)(sec_firmware_addr &
306 SEC_FIRMWARE_ADDR_MASK);
308 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
311 /* Check the secure firmware FIT image */
312 __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
314 if (fdt_check_header(sec_firmware_img)) {
315 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
319 if (!fit_check_format(sec_firmware_img)) {
320 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
327 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
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.
337 * The return value on success is PSCI version in format
338 * major[31:16]:minor[15:0].
340 unsigned int sec_firmware_support_psci_version(void)
342 if (current_el() == SEC_FIRMWARE_TARGET_EL)
343 return _sec_firmware_support_psci_version();
345 return PSCI_INVALID_VER;
350 * Check with sec_firmware if it supports random number generation
353 * The return value will be true if it is supported
355 bool sec_firmware_support_hwrng(void)
357 #ifdef CONFIG_TFABOOT
358 /* return true as TFA has one job ring reserved */
361 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
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
374 int sec_firmware_get_random(uint8_t *rand, int bytes)
376 unsigned long long num;
380 if (!bytes || bytes > 8) {
381 printf("Max Random bytes genration supported is 8\n");
384 #define SIP_RNG_64 0xC200FF11
385 regs.regs[0] = SIP_RNG_64;
391 regs.regs[1] = param1;
399 memcpy(rand, &num, bytes);
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
409 * @loadable_l: the address to hold loadable address low
410 * @loadable_h: the address to hold loadable address high
412 int sec_firmware_init(const void *sec_firmware_img,
420 if (!sec_firmware_is_valid(sec_firmware_img))
423 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
426 printf("SEC Firmware: Failed to load image\n");
428 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
429 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
431 printf("SEC Firmware: Failed to initialize\n");
436 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
440 * The PE will be turned into target EL when returned from
443 if (current_el() != SEC_FIRMWARE_TARGET_EL)
446 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
448 /* Set exception table and enable caches if it isn't EL3 */
449 if (current_el() != 3) {
450 c_runtime_cpu_setup();
458 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
460 * @eret: 0 in case of error, 1 for success
462 int fdt_fixup_kaslr(void *fdt)
468 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
469 /* Check if random seed generation is supported */
470 if (sec_firmware_support_hwrng() == false) {
471 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
475 ret = sec_firmware_get_random(rand, 8);
477 printf("WARNING: No random number to set kaslr-seed\n");
481 err = fdt_check_header(fdt);
483 printf("fdt_chosen: %s\n", fdt_strerror(err));
487 /* find or create "/chosen" node. */
488 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
492 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
495 printf("WARNING: can't set kaslr-seed %s.\n",