blob: c7445e647ee26e796890f2da2b3048b0686f606d [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 Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Hou Zhiqiang505d7352016-06-28 20:18:13 +080012#include <linux/kernel.h>
13#include <asm/io.h>
14#include <asm/system.h>
15#include <asm/types.h>
16#include <asm/macro.h>
17#include <asm/armv8/sec_firmware.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20extern void c_runtime_cpu_setup(void);
21
22#define SEC_FIRMWARE_LOADED 0x1
23#define SEC_FIRMWARE_RUNNING 0x2
24#define SEC_FIRMWARE_ADDR_MASK (~0x3)
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080025/*
26 * Secure firmware load addr
27 * Flags used: 0x1 secure firmware has been loaded to secure memory
28 * 0x2 secure firmware is running
29 */
30phys_addr_t sec_firmware_addr;
31
32#ifndef SEC_FIRMWARE_FIT_IMAGE
33#define SEC_FIRMWARE_FIT_IMAGE "firmware"
34#endif
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080035#ifndef SEC_FIRMWARE_FIT_CNF_NAME
36#define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080037#endif
38#ifndef SEC_FIRMWARE_TARGET_EL
39#define SEC_FIRMWARE_TARGET_EL 2
40#endif
Hou Zhiqiang505d7352016-06-28 20:18:13 +080041
42static int sec_firmware_get_data(const void *sec_firmware_img,
43 const void **data, size_t *size)
44{
45 int conf_node_off, fw_node_off;
46 char *conf_node_name = NULL;
47 char *desc;
48 int ret;
49
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080050 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Hou Zhiqiang505d7352016-06-28 20:18:13 +080051
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);
55 return -ENOENT;
56 }
57
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);
63 return -ENOLINK;
64 }
65
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");
69 return -EINVAL;
70 }
71
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);
75 return -ENOENT;
76 }
77
78 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
79 if (ret)
80 printf("SEC Firmware: Can't get description\n");
81 else
82 printf("%s\n", desc);
83
84 return ret;
85}
86
87/*
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.
91 *
92 * Returns 0 on success and a negative errno on error task fail.
93 */
94static int sec_firmware_parse_image(const void *sec_firmware_img,
95 const void **raw_image_addr,
96 size_t *raw_image_size)
97{
98 int ret;
99
100 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
101 raw_image_size);
102 if (ret)
103 return ret;
104
105 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
106 *raw_image_addr, *raw_image_size);
107
108 return 0;
109}
110
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530111/*
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).
115 *
116 * Returns 0 on success and a negative errno on error task fail.
117 */
118static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
119 u32 *loadable_l, u32 *loadable_h)
120{
121 phys_addr_t sec_firmware_loadable_addr = 0;
Sumit Gargd5217082018-04-24 03:23:28 +0530122 int conf_node_off, ld_node_off, images;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530123 char *conf_node_name = NULL;
124 const void *data;
125 size_t size;
126 ulong load;
Sumit Gargd5217082018-04-24 03:23:28 +0530127 const char *name, *str, *type;
128 int len;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530129
Thomas Hebb1dbd3d12019-11-10 08:23:15 -0800130 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530131
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);
Sumit Gargd5217082018-04-24 03:23:28 +0530135 return -ENOENT;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530136 }
137
Sumit Gargd5217082018-04-24 03:23:28 +0530138 /* find the node holding the images information */
139 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
140 if (images < 0) {
141 printf("%s: Cannot find /images node: %d\n", __func__, images);
142 return -1;
143 }
144
145 type = FIT_LOADABLE_PROP;
146
147 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
148 if (!name) {
149 /* Loadables not present */
150 return 0;
151 }
152
153 printf("SEC Firmware: '%s' present in config\n", type);
154
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,
161 ld_node_off);
162 return -EINVAL;
163 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530164
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");
168 return -EINVAL;
169 }
170
171 if (fit_image_get_data(sec_firmware_img, ld_node_off,
172 &data, &size)) {
173 printf("SEC Loadable: Can't get subimage data/size");
174 return -ENOENT;
175 }
176
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");
180 return -ENOENT;
181 }
182
183 /* Compute load address for loadable in secure memory */
184 sec_firmware_loadable_addr = (sec_firmware_addr -
185 gd->arch.tlb_size) + load;
186
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);
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530193
Sumit Gargd5217082018-04-24 03:23:28 +0530194 /* Populate loadable address only for Trusted OS */
195 if (!strcmp(str, "trustedOS@1")) {
196 /*
197 * Populate address ptrs for loadable image with
198 * loadbale addr
199 */
200 out_le32(loadable_l, (sec_firmware_loadable_addr &
201 WORD_MASK));
202 out_le32(loadable_h, (sec_firmware_loadable_addr >>
203 WORD_SHIFT));
204 }
205 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530206
207 return 0;
208}
209
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800210static int sec_firmware_copy_image(const char *title,
211 u64 image_addr, u32 image_size, u64 sec_firmware)
212{
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);
216
217 return 0;
218}
219
220/*
221 * This function will parse the SEC Firmware image, and then load it
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530222 * to secure memory. Also load any loadable if present along with SEC
223 * Firmware image.
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800224 */
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530225static int sec_firmware_load_image(const void *sec_firmware_img,
226 u32 *loadable_l, u32 *loadable_h)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800227{
228 const void *raw_image_addr;
229 size_t raw_image_size = 0;
230 int ret;
231
232 /*
233 * The Excetpion Level must be EL3 to load and initialize
234 * the SEC Firmware.
235 */
236 if (current_el() != 3) {
237 ret = -EACCES;
238 goto out;
239 }
240
241#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
242 /*
243 * The SEC Firmware must be stored in secure memory.
244 * Append SEC Firmware to secure mmu table.
245 */
246 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
247 ret = -ENXIO;
248 goto out;
249 }
250
251 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
252 gd->arch.tlb_size;
253#else
254#error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
255#endif
256
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);
261
262 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
263 &raw_image_size);
264 if (ret)
265 goto out;
266
267 /* TODO:
268 * Check if the end addr of SEC Firmware has been extend the secure
269 * memory.
270 */
271
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);
276 if (ret)
277 goto out;
278
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530279 /*
280 * Check if any loadable are present along with firmware image, if
281 * present load them.
282 */
283 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
284 loadable_h);
285 if (ret)
286 goto out;
287
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800288 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
289 debug("SEC Firmware: Entry point: 0x%llx\n",
290 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
291
292 return 0;
293
294out:
295 printf("SEC Firmware: error (%d)\n", ret);
296 sec_firmware_addr = 0;
297
298 return ret;
299}
300
301static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
302{
303 const void *entry = (void *)(sec_firmware_addr &
304 SEC_FIRMWARE_ADDR_MASK);
305
306 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
307}
308
309/* Check the secure firmware FIT image */
310__weak bool sec_firmware_is_valid(const void *sec_firmware_img)
311{
312 if (fdt_check_header(sec_firmware_img)) {
313 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
314 return false;
315 }
316
317 if (!fit_check_format(sec_firmware_img)) {
318 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
319 return false;
320 }
321
322 return true;
323}
324
Hou Zhiqiang6be115d2017-01-16 17:31:48 +0800325#ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800326/*
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.
334 *
335 * The return value on success is PSCI version in format
336 * major[31:16]:minor[15:0].
337 */
338unsigned int sec_firmware_support_psci_version(void)
339{
York Sune6b871e2017-05-15 08:51:59 -0700340 if (current_el() == SEC_FIRMWARE_TARGET_EL)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800341 return _sec_firmware_support_psci_version();
342
Yuantian Tangaec3b142017-04-19 13:27:39 +0800343 return PSCI_INVALID_VER;
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800344}
345#endif
346
347/*
Ruchika Guptadb204d72017-08-16 15:58:10 +0530348 * Check with sec_firmware if it supports random number generation
349 * via HW RNG
350 *
351 * The return value will be true if it is supported
352 */
353bool sec_firmware_support_hwrng(void)
354{
Pankit Garg46969572018-11-05 18:02:19 +0000355#ifdef CONFIG_TFABOOT
356 /* return true as TFA has one job ring reserved */
357 return true;
358#endif
Ruchika Guptadb204d72017-08-16 15:58:10 +0530359 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
Thomas Hebbfd37f242019-11-13 18:18:03 -0800360 return true;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530361 }
362
363 return false;
364}
365
366/*
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
371 */
372int sec_firmware_get_random(uint8_t *rand, int bytes)
373{
374 unsigned long long num;
375 struct pt_regs regs;
376 int param1;
377
378 if (!bytes || bytes > 8) {
379 printf("Max Random bytes genration supported is 8\n");
380 return -1;
381 }
382#define SIP_RNG_64 0xC200FF11
383 regs.regs[0] = SIP_RNG_64;
384
385 if (bytes <= 4)
386 param1 = 0;
387 else
388 param1 = 1;
389 regs.regs[1] = param1;
390
391 smc_call(&regs);
392
393 if (regs.regs[0])
394 return -1;
395
396 num = regs.regs[1];
397 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
473 ret = sec_firmware_get_random(rand, 8);
474 if (ret < 0) {
475 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}