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_FIT_CNF_NAME
40 #define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
42 #ifndef SEC_FIRMWARE_TARGET_EL
43 #define SEC_FIRMWARE_TARGET_EL 2
46 static int sec_firmware_get_data(const void *sec_firmware_img,
47 const void **data, size_t *size)
49 int conf_node_off, fw_node_off;
50 char *conf_node_name = NULL;
54 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
56 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
57 if (conf_node_off < 0) {
58 printf("SEC Firmware: %s: no such config\n", conf_node_name);
62 fw_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off,
63 SEC_FIRMWARE_FIT_IMAGE);
64 if (fw_node_off < 0) {
65 printf("SEC Firmware: No '%s' in config\n",
66 SEC_FIRMWARE_FIT_IMAGE);
70 /* Verify secure firmware image */
71 if (!(fit_image_verify(sec_firmware_img, fw_node_off))) {
72 printf("SEC Firmware: Bad firmware image (bad CRC)\n");
76 if (fit_image_get_data(sec_firmware_img, fw_node_off, data, size)) {
77 printf("SEC Firmware: Can't get %s subimage data/size",
78 SEC_FIRMWARE_FIT_IMAGE);
82 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
84 printf("SEC Firmware: Can't get description\n");
92 * SEC Firmware FIT image parser checks if the image is in FIT
93 * format, verifies integrity of the image and calculates raw
94 * image address and size values.
96 * Returns 0 on success and a negative errno on error task fail.
98 static int sec_firmware_parse_image(const void *sec_firmware_img,
99 const void **raw_image_addr,
100 size_t *raw_image_size)
104 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
109 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
110 *raw_image_addr, *raw_image_size);
116 * SEC Firmware FIT image parser to check if any loadable is
117 * present. If present, verify integrity of the loadable and
118 * copy loadable to address provided in (loadable_h, loadable_l).
120 * Returns 0 on success and a negative errno on error task fail.
122 static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
123 u32 *loadable_l, u32 *loadable_h)
125 phys_addr_t sec_firmware_loadable_addr = 0;
126 int conf_node_off, ld_node_off, images;
127 char *conf_node_name = NULL;
131 const char *name, *str, *type;
134 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
136 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
137 if (conf_node_off < 0) {
138 printf("SEC Firmware: %s: no such config\n", conf_node_name);
142 /* find the node holding the images information */
143 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
145 printf("%s: Cannot find /images node: %d\n", __func__, images);
149 type = FIT_LOADABLE_PROP;
151 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
153 /* Loadables not present */
157 printf("SEC Firmware: '%s' present in config\n", type);
159 for (str = name; str && ((str - name) < len);
160 str = strchr(str, '\0') + 1) {
161 printf("%s: '%s'\n", type, str);
162 ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str);
163 if (ld_node_off < 0) {
164 printf("cannot find image node '%s': %d\n", str,
169 /* Verify secure firmware image */
170 if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
171 printf("SEC Loadable: Bad loadable image (bad CRC)\n");
175 if (fit_image_get_data(sec_firmware_img, ld_node_off,
177 printf("SEC Loadable: Can't get subimage data/size");
181 /* Get load address, treated as load offset to secure memory */
182 if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
183 printf("SEC Loadable: Can't get subimage load");
187 /* Compute load address for loadable in secure memory */
188 sec_firmware_loadable_addr = (sec_firmware_addr -
189 gd->arch.tlb_size) + load;
191 /* Copy loadable to secure memory and flush dcache */
192 debug("%s copied to address 0x%p\n",
193 FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
194 memcpy((void *)sec_firmware_loadable_addr, data, size);
195 flush_dcache_range(sec_firmware_loadable_addr,
196 sec_firmware_loadable_addr + size);
198 /* Populate loadable address only for Trusted OS */
199 if (!strcmp(str, "trustedOS@1")) {
201 * Populate address ptrs for loadable image with
204 out_le32(loadable_l, (sec_firmware_loadable_addr &
206 out_le32(loadable_h, (sec_firmware_loadable_addr >>
214 static int sec_firmware_copy_image(const char *title,
215 u64 image_addr, u32 image_size, u64 sec_firmware)
217 debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
218 memcpy((void *)sec_firmware, (void *)image_addr, image_size);
219 flush_dcache_range(sec_firmware, sec_firmware + image_size);
225 * This function will parse the SEC Firmware image, and then load it
226 * to secure memory. Also load any loadable if present along with SEC
229 static int sec_firmware_load_image(const void *sec_firmware_img,
230 u32 *loadable_l, u32 *loadable_h)
232 const void *raw_image_addr;
233 size_t raw_image_size = 0;
237 * The Excetpion Level must be EL3 to load and initialize
240 if (current_el() != 3) {
245 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
247 * The SEC Firmware must be stored in secure memory.
248 * Append SEC Firmware to secure mmu table.
250 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
255 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
258 #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
261 /* Align SEC Firmware base address to 4K */
262 sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
263 debug("SEC Firmware: Load address: 0x%llx\n",
264 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
266 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
272 * Check if the end addr of SEC Firmware has been extend the secure
276 /* Copy the secure firmware to secure memory */
277 ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
278 raw_image_size, sec_firmware_addr &
279 SEC_FIRMWARE_ADDR_MASK);
284 * Check if any loadable are present along with firmware image, if
287 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
292 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
293 debug("SEC Firmware: Entry point: 0x%llx\n",
294 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
299 printf("SEC Firmware: error (%d)\n", ret);
300 sec_firmware_addr = 0;
305 static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
307 const void *entry = (void *)(sec_firmware_addr &
308 SEC_FIRMWARE_ADDR_MASK);
310 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
313 /* Check the secure firmware FIT image */
314 __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
316 if (fdt_check_header(sec_firmware_img)) {
317 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
321 if (fit_check_format(sec_firmware_img, IMAGE_SIZE_INVAL)) {
322 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
329 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
331 * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
332 * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
333 * number will be returned according to SMC Calling Conventions. But
334 * when getting the NOT_SUPPORTED error number, we cannot ensure if
335 * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
336 * won't be supported by this framework.
337 * And if the secure firmware isn't running, return NOT_SUPPORTED.
339 * The return value on success is PSCI version in format
340 * major[31:16]:minor[15:0].
342 unsigned int sec_firmware_support_psci_version(void)
344 if (current_el() == SEC_FIRMWARE_TARGET_EL)
345 return _sec_firmware_support_psci_version();
347 return PSCI_INVALID_VER;
352 * Check with sec_firmware if it supports random number generation
355 * The return value will be true if it is supported
357 bool sec_firmware_support_hwrng(void)
359 #ifdef CONFIG_TFABOOT
360 /* return true as TFA has one job ring reserved */
363 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
371 * sec_firmware_get_random - Get a random number from SEC Firmware
372 * @rand: random number buffer to be filled
373 * @bytes: Number of bytes of random number to be supported
374 * @eret: -1 in case of error, 0 for success
376 int sec_firmware_get_random(uint8_t *rand, int bytes)
378 struct arm_smccc_res res;
379 unsigned long long num;
382 if (!bytes || bytes > 8) {
383 printf("Max Random bytes genration supported is 8\n");
391 #define SIP_RNG_64 0xC200FF11
392 arm_smccc_smc(SIP_RNG_64, param1, 0, 0, 0, 0, 0, 0, &res);
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 err = 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",