blob: 7e6e4064ffe0fca913cfb7f1bda81aa65f1501e4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hou Zhiqiang505d7352016-06-28 20:18:13 +08002/*
3 * Copyright 2016 NXP Semiconductor, Inc.
Hou Zhiqiang505d7352016-06-28 20:18:13 +08004 */
5
6#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Hou Zhiqiang505d7352016-06-28 20:18:13 +08008#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <fdt_support.h>
10#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060014#include <asm/ptrace.h>
Hou Zhiqiang505d7352016-06-28 20:18:13 +080015#include <linux/kernel.h>
Michael Walle166ea482022-04-22 14:53:27 +053016#include <linux/arm-smccc.h>
Hou Zhiqiang505d7352016-06-28 20:18:13 +080017#include <asm/io.h>
18#include <asm/system.h>
19#include <asm/types.h>
20#include <asm/macro.h>
21#include <asm/armv8/sec_firmware.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24extern void c_runtime_cpu_setup(void);
25
26#define SEC_FIRMWARE_LOADED 0x1
27#define SEC_FIRMWARE_RUNNING 0x2
28#define SEC_FIRMWARE_ADDR_MASK (~0x3)
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080029/*
30 * Secure firmware load addr
31 * Flags used: 0x1 secure firmware has been loaded to secure memory
32 * 0x2 secure firmware is running
33 */
34phys_addr_t sec_firmware_addr;
35
36#ifndef SEC_FIRMWARE_FIT_IMAGE
37#define SEC_FIRMWARE_FIT_IMAGE "firmware"
38#endif
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080039#ifndef SEC_FIRMWARE_FIT_CNF_NAME
40#define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080041#endif
42#ifndef SEC_FIRMWARE_TARGET_EL
43#define SEC_FIRMWARE_TARGET_EL 2
44#endif
Hou Zhiqiang505d7352016-06-28 20:18:13 +080045
46static int sec_firmware_get_data(const void *sec_firmware_img,
47 const void **data, size_t *size)
48{
49 int conf_node_off, fw_node_off;
50 char *conf_node_name = NULL;
51 char *desc;
52 int ret;
53
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080054 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Hou Zhiqiang505d7352016-06-28 20:18:13 +080055
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);
59 return -ENOENT;
60 }
61
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);
67 return -ENOLINK;
68 }
69
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");
73 return -EINVAL;
74 }
75
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);
79 return -ENOENT;
80 }
81
82 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
83 if (ret)
84 printf("SEC Firmware: Can't get description\n");
85 else
86 printf("%s\n", desc);
87
88 return ret;
89}
90
91/*
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.
95 *
96 * Returns 0 on success and a negative errno on error task fail.
97 */
98static int sec_firmware_parse_image(const void *sec_firmware_img,
99 const void **raw_image_addr,
100 size_t *raw_image_size)
101{
102 int ret;
103
104 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
105 raw_image_size);
106 if (ret)
107 return ret;
108
109 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
110 *raw_image_addr, *raw_image_size);
111
112 return 0;
113}
114
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530115/*
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).
119 *
120 * Returns 0 on success and a negative errno on error task fail.
121 */
122static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
123 u32 *loadable_l, u32 *loadable_h)
124{
125 phys_addr_t sec_firmware_loadable_addr = 0;
Sumit Gargd5217082018-04-24 03:23:28 +0530126 int conf_node_off, ld_node_off, images;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530127 char *conf_node_name = NULL;
128 const void *data;
129 size_t size;
130 ulong load;
Sumit Gargd5217082018-04-24 03:23:28 +0530131 const char *name, *str, *type;
132 int len;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530133
Thomas Hebb1dbd3d12019-11-10 08:23:15 -0800134 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530135
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);
Sumit Gargd5217082018-04-24 03:23:28 +0530139 return -ENOENT;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530140 }
141
Sumit Gargd5217082018-04-24 03:23:28 +0530142 /* find the node holding the images information */
143 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
144 if (images < 0) {
145 printf("%s: Cannot find /images node: %d\n", __func__, images);
146 return -1;
147 }
148
149 type = FIT_LOADABLE_PROP;
150
151 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
152 if (!name) {
153 /* Loadables not present */
154 return 0;
155 }
156
157 printf("SEC Firmware: '%s' present in config\n", type);
158
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,
165 ld_node_off);
166 return -EINVAL;
167 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530168
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");
172 return -EINVAL;
173 }
174
175 if (fit_image_get_data(sec_firmware_img, ld_node_off,
176 &data, &size)) {
177 printf("SEC Loadable: Can't get subimage data/size");
178 return -ENOENT;
179 }
180
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");
184 return -ENOENT;
185 }
186
187 /* Compute load address for loadable in secure memory */
188 sec_firmware_loadable_addr = (sec_firmware_addr -
189 gd->arch.tlb_size) + load;
190
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);
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530197
Sumit Gargd5217082018-04-24 03:23:28 +0530198 /* Populate loadable address only for Trusted OS */
199 if (!strcmp(str, "trustedOS@1")) {
200 /*
201 * Populate address ptrs for loadable image with
202 * loadbale addr
203 */
204 out_le32(loadable_l, (sec_firmware_loadable_addr &
205 WORD_MASK));
206 out_le32(loadable_h, (sec_firmware_loadable_addr >>
207 WORD_SHIFT));
208 }
209 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530210
211 return 0;
212}
213
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800214static int sec_firmware_copy_image(const char *title,
215 u64 image_addr, u32 image_size, u64 sec_firmware)
216{
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);
220
221 return 0;
222}
223
224/*
225 * This function will parse the SEC Firmware image, and then load it
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530226 * to secure memory. Also load any loadable if present along with SEC
227 * Firmware image.
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800228 */
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530229static int sec_firmware_load_image(const void *sec_firmware_img,
230 u32 *loadable_l, u32 *loadable_h)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800231{
232 const void *raw_image_addr;
233 size_t raw_image_size = 0;
234 int ret;
235
236 /*
237 * The Excetpion Level must be EL3 to load and initialize
238 * the SEC Firmware.
239 */
240 if (current_el() != 3) {
241 ret = -EACCES;
242 goto out;
243 }
244
245#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
246 /*
247 * The SEC Firmware must be stored in secure memory.
248 * Append SEC Firmware to secure mmu table.
249 */
250 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
251 ret = -ENXIO;
252 goto out;
253 }
254
255 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
256 gd->arch.tlb_size;
257#else
258#error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
259#endif
260
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);
265
266 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
267 &raw_image_size);
268 if (ret)
269 goto out;
270
271 /* TODO:
272 * Check if the end addr of SEC Firmware has been extend the secure
273 * memory.
274 */
275
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);
280 if (ret)
281 goto out;
282
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530283 /*
284 * Check if any loadable are present along with firmware image, if
285 * present load them.
286 */
287 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
288 loadable_h);
289 if (ret)
290 goto out;
291
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800292 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
293 debug("SEC Firmware: Entry point: 0x%llx\n",
294 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
295
296 return 0;
297
298out:
299 printf("SEC Firmware: error (%d)\n", ret);
300 sec_firmware_addr = 0;
301
302 return ret;
303}
304
305static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
306{
307 const void *entry = (void *)(sec_firmware_addr &
308 SEC_FIRMWARE_ADDR_MASK);
309
310 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
311}
312
313/* Check the secure firmware FIT image */
314__weak bool sec_firmware_is_valid(const void *sec_firmware_img)
315{
316 if (fdt_check_header(sec_firmware_img)) {
317 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
318 return false;
319 }
320
Simon Glassd563c252021-02-15 17:08:09 -0700321 if (fit_check_format(sec_firmware_img, IMAGE_SIZE_INVAL)) {
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800322 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
323 return false;
324 }
325
326 return true;
327}
328
Hou Zhiqiang6be115d2017-01-16 17:31:48 +0800329#ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800330/*
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.
338 *
339 * The return value on success is PSCI version in format
340 * major[31:16]:minor[15:0].
341 */
342unsigned int sec_firmware_support_psci_version(void)
343{
York Sune6b871e2017-05-15 08:51:59 -0700344 if (current_el() == SEC_FIRMWARE_TARGET_EL)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800345 return _sec_firmware_support_psci_version();
346
Yuantian Tangaec3b142017-04-19 13:27:39 +0800347 return PSCI_INVALID_VER;
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800348}
349#endif
350
351/*
Ruchika Guptadb204d72017-08-16 15:58:10 +0530352 * Check with sec_firmware if it supports random number generation
353 * via HW RNG
354 *
355 * The return value will be true if it is supported
356 */
357bool sec_firmware_support_hwrng(void)
358{
Pankit Garg46969572018-11-05 18:02:19 +0000359#ifdef CONFIG_TFABOOT
360 /* return true as TFA has one job ring reserved */
361 return true;
362#endif
Ruchika Guptadb204d72017-08-16 15:58:10 +0530363 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
Thomas Hebbfd37f242019-11-13 18:18:03 -0800364 return true;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530365 }
366
367 return false;
368}
369
370/*
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
375 */
376int sec_firmware_get_random(uint8_t *rand, int bytes)
377{
Michael Walle166ea482022-04-22 14:53:27 +0530378 struct arm_smccc_res res;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530379 unsigned long long num;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530380 int param1;
381
382 if (!bytes || bytes > 8) {
383 printf("Max Random bytes genration supported is 8\n");
384 return -1;
385 }
Ruchika Guptadb204d72017-08-16 15:58:10 +0530386 if (bytes <= 4)
387 param1 = 0;
388 else
389 param1 = 1;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530390
Michael Walle166ea482022-04-22 14:53:27 +0530391#define SIP_RNG_64 0xC200FF11
392 arm_smccc_smc(SIP_RNG_64, param1, 0, 0, 0, 0, 0, 0, &res);
393 if (res.a0)
Ruchika Guptadb204d72017-08-16 15:58:10 +0530394 return -1;
395
Michael Walle166ea482022-04-22 14:53:27 +0530396 num = res.a1;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530397 memcpy(rand, &num, bytes);
398
399 return 0;
400}
401
402/*
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800403 * 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
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530407 * @loadable_l: the address to hold loadable address low
408 * @loadable_h: the address to hold loadable address high
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800409 */
410int sec_firmware_init(const void *sec_firmware_img,
411 u32 *eret_hold_l,
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530412 u32 *eret_hold_h,
413 u32 *loadable_l,
414 u32 *loadable_h)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800415{
416 int ret;
417
418 if (!sec_firmware_is_valid(sec_firmware_img))
419 return -EINVAL;
420
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530421 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
422 loadable_h);
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800423 if (ret) {
424 printf("SEC Firmware: Failed to load image\n");
425 return ret;
426 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
427 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
428 if (ret) {
429 printf("SEC Firmware: Failed to initialize\n");
430 return ret;
431 }
432 }
433
434 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
435 current_el());
436
437 /*
438 * The PE will be turned into target EL when returned from
439 * SEC Firmware.
440 */
441 if (current_el() != SEC_FIRMWARE_TARGET_EL)
442 return -EACCES;
443
444 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
445
446 /* Set exception table and enable caches if it isn't EL3 */
447 if (current_el() != 3) {
448 c_runtime_cpu_setup();
449 enable_caches();
450 }
451
452 return 0;
453}
Ruchika Guptadb204d72017-08-16 15:58:10 +0530454
455/*
456 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
457 * @fdt: Device tree
458 * @eret: 0 in case of error, 1 for success
459 */
460int fdt_fixup_kaslr(void *fdt)
461{
462 int nodeoffset;
463 int err, ret = 0;
464 u8 rand[8];
465
466#if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
467 /* Check if random seed generation is supported */
Ruchika Guptafc8e3402018-04-12 16:24:35 +0530468 if (sec_firmware_support_hwrng() == false) {
469 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
Ruchika Guptadb204d72017-08-16 15:58:10 +0530470 return 0;
Ruchika Guptafc8e3402018-04-12 16:24:35 +0530471 }
Ruchika Guptadb204d72017-08-16 15:58:10 +0530472
Michael Walle166ea482022-04-22 14:53:27 +0530473 err = sec_firmware_get_random(rand, 8);
474 if (err < 0) {
Ruchika Guptadb204d72017-08-16 15:58:10 +0530475 printf("WARNING: No random number to set kaslr-seed\n");
476 return 0;
477 }
478
479 err = fdt_check_header(fdt);
480 if (err < 0) {
481 printf("fdt_chosen: %s\n", fdt_strerror(err));
482 return 0;
483 }
484
485 /* find or create "/chosen" node. */
486 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
487 if (nodeoffset < 0)
488 return 0;
489
490 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
491 sizeof(rand));
492 if (err < 0) {
493 printf("WARNING: can't set kaslr-seed %s.\n",
494 fdt_strerror(err));
495 return 0;
496 }
497 ret = 1;
498#endif
499
500 return ret;
501}