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/global_data.h>
14 #include <asm/ptrace.h>
15 #include <linux/kernel.h>
16 #include <linux/arm-smccc.h>
18 #include <asm/system.h>
19 #include <asm/types.h>
20 #include <asm/macro.h>
21 #include <asm/armv8/sec_firmware.h>
23 DECLARE_GLOBAL_DATA_PTR;
24 extern void c_runtime_cpu_setup(void);
26 #define SEC_FIRMWARE_LOADED 0x1
27 #define SEC_FIRMWARE_RUNNING 0x2
28 #define SEC_FIRMWARE_ADDR_MASK (~0x3)
30 * Secure firmware load addr
31 * Flags used: 0x1 secure firmware has been loaded to secure memory
32 * 0x2 secure firmware is running
34 phys_addr_t sec_firmware_addr;
36 #ifndef SEC_FIRMWARE_FIT_IMAGE
37 #define SEC_FIRMWARE_FIT_IMAGE "firmware"
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 return fit_get_data_conf_prop(sec_firmware_img, SEC_FIRMWARE_FIT_IMAGE,
51 * SEC Firmware FIT image parser checks if the image is in FIT
52 * format, verifies integrity of the image and calculates raw
53 * image address and size values.
55 * Returns 0 on success and a negative errno on error task fail.
57 static int sec_firmware_parse_image(const void *sec_firmware_img,
58 const void **raw_image_addr,
59 size_t *raw_image_size)
63 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
68 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
69 *raw_image_addr, *raw_image_size);
75 * SEC Firmware FIT image parser to check if any loadable is
76 * present. If present, verify integrity of the loadable and
77 * copy loadable to address provided in (loadable_h, loadable_l).
79 * Returns 0 on success and a negative errno on error task fail.
81 static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
82 u32 *loadable_l, u32 *loadable_h)
84 phys_addr_t sec_firmware_loadable_addr = 0;
85 int conf_node_off, ld_node_off, images;
89 const char *name, *str, *type;
92 conf_node_off = fit_conf_get_node(sec_firmware_img, NULL);
93 if (conf_node_off < 0) {
94 puts("SEC Firmware: no config\n");
98 /* find the node holding the images information */
99 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
101 printf("%s: Cannot find /images node: %d\n", __func__, images);
105 type = FIT_LOADABLE_PROP;
107 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
109 /* Loadables not present */
113 printf("SEC Firmware: '%s' present in config\n", type);
115 for (str = name; str && ((str - name) < len);
116 str = strchr(str, '\0') + 1) {
117 printf("%s: '%s'\n", type, str);
118 ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str);
119 if (ld_node_off < 0) {
120 printf("cannot find image node '%s': %d\n", str,
125 /* Verify secure firmware image */
126 if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
127 printf("SEC Loadable: Bad loadable image (bad CRC)\n");
131 if (fit_image_get_data(sec_firmware_img, ld_node_off,
133 printf("SEC Loadable: Can't get subimage data/size");
137 /* Get load address, treated as load offset to secure memory */
138 if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
139 printf("SEC Loadable: Can't get subimage load");
143 /* Compute load address for loadable in secure memory */
144 sec_firmware_loadable_addr = (sec_firmware_addr -
145 gd->arch.tlb_size) + load;
147 /* Copy loadable to secure memory and flush dcache */
148 debug("%s copied to address 0x%p\n",
149 FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
150 memcpy((void *)sec_firmware_loadable_addr, data, size);
151 flush_dcache_range(sec_firmware_loadable_addr,
152 sec_firmware_loadable_addr + size);
154 /* Populate loadable address only for Trusted OS */
155 if (!strcmp(str, "trustedOS@1")) {
157 * Populate address ptrs for loadable image with
160 out_le32(loadable_l, (sec_firmware_loadable_addr &
162 out_le32(loadable_h, (sec_firmware_loadable_addr >>
170 static int sec_firmware_copy_image(const char *title,
171 u64 image_addr, u32 image_size, u64 sec_firmware)
173 debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
174 memcpy((void *)sec_firmware, (void *)image_addr, image_size);
175 flush_dcache_range(sec_firmware, sec_firmware + image_size);
181 * This function will parse the SEC Firmware image, and then load it
182 * to secure memory. Also load any loadable if present along with SEC
185 static int sec_firmware_load_image(const void *sec_firmware_img,
186 u32 *loadable_l, u32 *loadable_h)
188 const void *raw_image_addr;
189 size_t raw_image_size = 0;
193 * The Excetpion Level must be EL3 to load and initialize
196 if (current_el() != 3) {
201 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
203 * The SEC Firmware must be stored in secure memory.
204 * Append SEC Firmware to secure mmu table.
206 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
211 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
214 #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
217 /* Align SEC Firmware base address to 4K */
218 sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
219 debug("SEC Firmware: Load address: 0x%llx\n",
220 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
222 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
228 * Check if the end addr of SEC Firmware has been extend the secure
232 /* Copy the secure firmware to secure memory */
233 ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
234 raw_image_size, sec_firmware_addr &
235 SEC_FIRMWARE_ADDR_MASK);
240 * Check if any loadable are present along with firmware image, if
243 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
248 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
249 debug("SEC Firmware: Entry point: 0x%llx\n",
250 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
255 printf("SEC Firmware: error (%d)\n", ret);
256 sec_firmware_addr = 0;
261 static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
263 const void *entry = (void *)(sec_firmware_addr &
264 SEC_FIRMWARE_ADDR_MASK);
266 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
269 /* Check the secure firmware FIT image */
270 __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
272 if (fdt_check_header(sec_firmware_img)) {
273 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
277 if (fit_check_format(sec_firmware_img, IMAGE_SIZE_INVAL)) {
278 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
285 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
287 * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
288 * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
289 * number will be returned according to SMC Calling Conventions. But
290 * when getting the NOT_SUPPORTED error number, we cannot ensure if
291 * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
292 * won't be supported by this framework.
293 * And if the secure firmware isn't running, return NOT_SUPPORTED.
295 * The return value on success is PSCI version in format
296 * major[31:16]:minor[15:0].
298 unsigned int sec_firmware_support_psci_version(void)
300 if (current_el() == SEC_FIRMWARE_TARGET_EL)
301 return _sec_firmware_support_psci_version();
303 return PSCI_INVALID_VER;
308 * Check with sec_firmware if it supports random number generation
311 * The return value will be true if it is supported
313 bool sec_firmware_support_hwrng(void)
315 #ifdef CONFIG_TFABOOT
316 /* return true as TFA has one job ring reserved */
319 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
327 * sec_firmware_get_random - Get a random number from SEC Firmware
328 * @rand: random number buffer to be filled
329 * @bytes: Number of bytes of random number to be supported
330 * @eret: -1 in case of error, 0 for success
332 int sec_firmware_get_random(uint8_t *rand, int bytes)
334 struct arm_smccc_res res;
335 unsigned long long num;
338 if (!bytes || bytes > 8) {
339 printf("Max Random bytes genration supported is 8\n");
347 #define SIP_RNG_64 0xC200FF11
348 arm_smccc_smc(SIP_RNG_64, param1, 0, 0, 0, 0, 0, 0, &res);
353 memcpy(rand, &num, bytes);
359 * sec_firmware_init - Initialize the SEC Firmware
360 * @sec_firmware_img: the SEC Firmware image address
361 * @eret_hold_l: the address to hold exception return address low
362 * @eret_hold_h: the address to hold exception return address high
363 * @loadable_l: the address to hold loadable address low
364 * @loadable_h: the address to hold loadable address high
366 int sec_firmware_init(const void *sec_firmware_img,
374 if (!sec_firmware_is_valid(sec_firmware_img))
377 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
380 printf("SEC Firmware: Failed to load image\n");
382 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
383 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
385 printf("SEC Firmware: Failed to initialize\n");
390 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
394 * The PE will be turned into target EL when returned from
397 if (current_el() != SEC_FIRMWARE_TARGET_EL)
400 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
402 /* Set exception table and enable caches if it isn't EL3 */
403 if (current_el() != 3) {
404 c_runtime_cpu_setup();
412 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
414 * @eret: 0 in case of error, 1 for success
416 int fdt_fixup_kaslr(void *fdt)
422 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
423 /* Check if random seed generation is supported */
424 if (sec_firmware_support_hwrng() == false) {
425 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
429 err = sec_firmware_get_random(rand, 8);
431 printf("WARNING: No random number to set kaslr-seed\n");
435 err = fdt_check_header(fdt);
437 printf("fdt_chosen: %s\n", fdt_strerror(err));
441 /* find or create "/chosen" node. */
442 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
446 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
449 printf("WARNING: can't set kaslr-seed %s.\n",