blob: 95ea57d571b1494c1819004ad3ce124ec4034388 [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>
9#include <linux/kernel.h>
10#include <asm/io.h>
11#include <asm/system.h>
12#include <asm/types.h>
13#include <asm/macro.h>
14#include <asm/armv8/sec_firmware.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17extern void c_runtime_cpu_setup(void);
18
19#define SEC_FIRMWARE_LOADED 0x1
20#define SEC_FIRMWARE_RUNNING 0x2
21#define SEC_FIRMWARE_ADDR_MASK (~0x3)
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080022/*
23 * Secure firmware load addr
24 * Flags used: 0x1 secure firmware has been loaded to secure memory
25 * 0x2 secure firmware is running
26 */
27phys_addr_t sec_firmware_addr;
28
29#ifndef SEC_FIRMWARE_FIT_IMAGE
30#define SEC_FIRMWARE_FIT_IMAGE "firmware"
31#endif
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080032#ifndef SEC_FIRMWARE_FIT_CNF_NAME
33#define SEC_FIRMWARE_FIT_CNF_NAME "config-1"
Hou Zhiqiang9c2eca12016-09-06 14:23:07 +080034#endif
35#ifndef SEC_FIRMWARE_TARGET_EL
36#define SEC_FIRMWARE_TARGET_EL 2
37#endif
Hou Zhiqiang505d7352016-06-28 20:18:13 +080038
39static int sec_firmware_get_data(const void *sec_firmware_img,
40 const void **data, size_t *size)
41{
42 int conf_node_off, fw_node_off;
43 char *conf_node_name = NULL;
44 char *desc;
45 int ret;
46
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080047 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Hou Zhiqiang505d7352016-06-28 20:18:13 +080048
49 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
50 if (conf_node_off < 0) {
51 printf("SEC Firmware: %s: no such config\n", conf_node_name);
52 return -ENOENT;
53 }
54
55 fw_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off,
56 SEC_FIRMWARE_FIT_IMAGE);
57 if (fw_node_off < 0) {
58 printf("SEC Firmware: No '%s' in config\n",
59 SEC_FIRMWARE_FIT_IMAGE);
60 return -ENOLINK;
61 }
62
63 /* Verify secure firmware image */
64 if (!(fit_image_verify(sec_firmware_img, fw_node_off))) {
65 printf("SEC Firmware: Bad firmware image (bad CRC)\n");
66 return -EINVAL;
67 }
68
69 if (fit_image_get_data(sec_firmware_img, fw_node_off, data, size)) {
70 printf("SEC Firmware: Can't get %s subimage data/size",
71 SEC_FIRMWARE_FIT_IMAGE);
72 return -ENOENT;
73 }
74
75 ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
76 if (ret)
77 printf("SEC Firmware: Can't get description\n");
78 else
79 printf("%s\n", desc);
80
81 return ret;
82}
83
84/*
85 * SEC Firmware FIT image parser checks if the image is in FIT
86 * format, verifies integrity of the image and calculates raw
87 * image address and size values.
88 *
89 * Returns 0 on success and a negative errno on error task fail.
90 */
91static int sec_firmware_parse_image(const void *sec_firmware_img,
92 const void **raw_image_addr,
93 size_t *raw_image_size)
94{
95 int ret;
96
97 ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
98 raw_image_size);
99 if (ret)
100 return ret;
101
102 debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
103 *raw_image_addr, *raw_image_size);
104
105 return 0;
106}
107
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530108/*
109 * SEC Firmware FIT image parser to check if any loadable is
110 * present. If present, verify integrity of the loadable and
111 * copy loadable to address provided in (loadable_h, loadable_l).
112 *
113 * Returns 0 on success and a negative errno on error task fail.
114 */
115static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
116 u32 *loadable_l, u32 *loadable_h)
117{
118 phys_addr_t sec_firmware_loadable_addr = 0;
Sumit Gargd5217082018-04-24 03:23:28 +0530119 int conf_node_off, ld_node_off, images;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530120 char *conf_node_name = NULL;
121 const void *data;
122 size_t size;
123 ulong load;
Sumit Gargd5217082018-04-24 03:23:28 +0530124 const char *name, *str, *type;
125 int len;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530126
Thomas Hebb1dbd3d12019-11-10 08:23:15 -0800127 conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530128
129 conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
130 if (conf_node_off < 0) {
131 printf("SEC Firmware: %s: no such config\n", conf_node_name);
Sumit Gargd5217082018-04-24 03:23:28 +0530132 return -ENOENT;
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530133 }
134
Sumit Gargd5217082018-04-24 03:23:28 +0530135 /* find the node holding the images information */
136 images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
137 if (images < 0) {
138 printf("%s: Cannot find /images node: %d\n", __func__, images);
139 return -1;
140 }
141
142 type = FIT_LOADABLE_PROP;
143
144 name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
145 if (!name) {
146 /* Loadables not present */
147 return 0;
148 }
149
150 printf("SEC Firmware: '%s' present in config\n", type);
151
152 for (str = name; str && ((str - name) < len);
153 str = strchr(str, '\0') + 1) {
154 printf("%s: '%s'\n", type, str);
155 ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str);
156 if (ld_node_off < 0) {
157 printf("cannot find image node '%s': %d\n", str,
158 ld_node_off);
159 return -EINVAL;
160 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530161
162 /* Verify secure firmware image */
163 if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
164 printf("SEC Loadable: Bad loadable image (bad CRC)\n");
165 return -EINVAL;
166 }
167
168 if (fit_image_get_data(sec_firmware_img, ld_node_off,
169 &data, &size)) {
170 printf("SEC Loadable: Can't get subimage data/size");
171 return -ENOENT;
172 }
173
174 /* Get load address, treated as load offset to secure memory */
175 if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
176 printf("SEC Loadable: Can't get subimage load");
177 return -ENOENT;
178 }
179
180 /* Compute load address for loadable in secure memory */
181 sec_firmware_loadable_addr = (sec_firmware_addr -
182 gd->arch.tlb_size) + load;
183
184 /* Copy loadable to secure memory and flush dcache */
185 debug("%s copied to address 0x%p\n",
186 FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
187 memcpy((void *)sec_firmware_loadable_addr, data, size);
188 flush_dcache_range(sec_firmware_loadable_addr,
189 sec_firmware_loadable_addr + size);
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530190
Sumit Gargd5217082018-04-24 03:23:28 +0530191 /* Populate loadable address only for Trusted OS */
192 if (!strcmp(str, "trustedOS@1")) {
193 /*
194 * Populate address ptrs for loadable image with
195 * loadbale addr
196 */
197 out_le32(loadable_l, (sec_firmware_loadable_addr &
198 WORD_MASK));
199 out_le32(loadable_h, (sec_firmware_loadable_addr >>
200 WORD_SHIFT));
201 }
202 }
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530203
204 return 0;
205}
206
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800207static int sec_firmware_copy_image(const char *title,
208 u64 image_addr, u32 image_size, u64 sec_firmware)
209{
210 debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
211 memcpy((void *)sec_firmware, (void *)image_addr, image_size);
212 flush_dcache_range(sec_firmware, sec_firmware + image_size);
213
214 return 0;
215}
216
217/*
218 * This function will parse the SEC Firmware image, and then load it
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530219 * to secure memory. Also load any loadable if present along with SEC
220 * Firmware image.
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800221 */
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530222static int sec_firmware_load_image(const void *sec_firmware_img,
223 u32 *loadable_l, u32 *loadable_h)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800224{
225 const void *raw_image_addr;
226 size_t raw_image_size = 0;
227 int ret;
228
229 /*
230 * The Excetpion Level must be EL3 to load and initialize
231 * the SEC Firmware.
232 */
233 if (current_el() != 3) {
234 ret = -EACCES;
235 goto out;
236 }
237
238#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
239 /*
240 * The SEC Firmware must be stored in secure memory.
241 * Append SEC Firmware to secure mmu table.
242 */
243 if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
244 ret = -ENXIO;
245 goto out;
246 }
247
248 sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
249 gd->arch.tlb_size;
250#else
251#error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
252#endif
253
254 /* Align SEC Firmware base address to 4K */
255 sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
256 debug("SEC Firmware: Load address: 0x%llx\n",
257 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
258
259 ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
260 &raw_image_size);
261 if (ret)
262 goto out;
263
264 /* TODO:
265 * Check if the end addr of SEC Firmware has been extend the secure
266 * memory.
267 */
268
269 /* Copy the secure firmware to secure memory */
270 ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
271 raw_image_size, sec_firmware_addr &
272 SEC_FIRMWARE_ADDR_MASK);
273 if (ret)
274 goto out;
275
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530276 /*
277 * Check if any loadable are present along with firmware image, if
278 * present load them.
279 */
280 ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
281 loadable_h);
282 if (ret)
283 goto out;
284
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800285 sec_firmware_addr |= SEC_FIRMWARE_LOADED;
286 debug("SEC Firmware: Entry point: 0x%llx\n",
287 sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
288
289 return 0;
290
291out:
292 printf("SEC Firmware: error (%d)\n", ret);
293 sec_firmware_addr = 0;
294
295 return ret;
296}
297
298static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
299{
300 const void *entry = (void *)(sec_firmware_addr &
301 SEC_FIRMWARE_ADDR_MASK);
302
303 return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
304}
305
306/* Check the secure firmware FIT image */
307__weak bool sec_firmware_is_valid(const void *sec_firmware_img)
308{
309 if (fdt_check_header(sec_firmware_img)) {
310 printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
311 return false;
312 }
313
314 if (!fit_check_format(sec_firmware_img)) {
315 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
316 return false;
317 }
318
319 return true;
320}
321
Hou Zhiqiang6be115d2017-01-16 17:31:48 +0800322#ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800323/*
324 * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
325 * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
326 * number will be returned according to SMC Calling Conventions. But
327 * when getting the NOT_SUPPORTED error number, we cannot ensure if
328 * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
329 * won't be supported by this framework.
330 * And if the secure firmware isn't running, return NOT_SUPPORTED.
331 *
332 * The return value on success is PSCI version in format
333 * major[31:16]:minor[15:0].
334 */
335unsigned int sec_firmware_support_psci_version(void)
336{
York Sune6b871e2017-05-15 08:51:59 -0700337 if (current_el() == SEC_FIRMWARE_TARGET_EL)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800338 return _sec_firmware_support_psci_version();
339
Yuantian Tangaec3b142017-04-19 13:27:39 +0800340 return PSCI_INVALID_VER;
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800341}
342#endif
343
344/*
Ruchika Guptadb204d72017-08-16 15:58:10 +0530345 * Check with sec_firmware if it supports random number generation
346 * via HW RNG
347 *
348 * The return value will be true if it is supported
349 */
350bool sec_firmware_support_hwrng(void)
351{
Pankit Garg46969572018-11-05 18:02:19 +0000352#ifdef CONFIG_TFABOOT
353 /* return true as TFA has one job ring reserved */
354 return true;
355#endif
Ruchika Guptadb204d72017-08-16 15:58:10 +0530356 if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
Thomas Hebbfd37f242019-11-13 18:18:03 -0800357 return true;
Ruchika Guptadb204d72017-08-16 15:58:10 +0530358 }
359
360 return false;
361}
362
363/*
364 * sec_firmware_get_random - Get a random number from SEC Firmware
365 * @rand: random number buffer to be filled
366 * @bytes: Number of bytes of random number to be supported
367 * @eret: -1 in case of error, 0 for success
368 */
369int sec_firmware_get_random(uint8_t *rand, int bytes)
370{
371 unsigned long long num;
372 struct pt_regs regs;
373 int param1;
374
375 if (!bytes || bytes > 8) {
376 printf("Max Random bytes genration supported is 8\n");
377 return -1;
378 }
379#define SIP_RNG_64 0xC200FF11
380 regs.regs[0] = SIP_RNG_64;
381
382 if (bytes <= 4)
383 param1 = 0;
384 else
385 param1 = 1;
386 regs.regs[1] = param1;
387
388 smc_call(&regs);
389
390 if (regs.regs[0])
391 return -1;
392
393 num = regs.regs[1];
394 memcpy(rand, &num, bytes);
395
396 return 0;
397}
398
399/*
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800400 * sec_firmware_init - Initialize the SEC Firmware
401 * @sec_firmware_img: the SEC Firmware image address
402 * @eret_hold_l: the address to hold exception return address low
403 * @eret_hold_h: the address to hold exception return address high
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530404 * @loadable_l: the address to hold loadable address low
405 * @loadable_h: the address to hold loadable address high
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800406 */
407int sec_firmware_init(const void *sec_firmware_img,
408 u32 *eret_hold_l,
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530409 u32 *eret_hold_h,
410 u32 *loadable_l,
411 u32 *loadable_h)
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800412{
413 int ret;
414
415 if (!sec_firmware_is_valid(sec_firmware_img))
416 return -EINVAL;
417
Sumit Gargb6fa55e2017-09-01 13:55:01 +0530418 ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
419 loadable_h);
Hou Zhiqiang505d7352016-06-28 20:18:13 +0800420 if (ret) {
421 printf("SEC Firmware: Failed to load image\n");
422 return ret;
423 } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
424 ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
425 if (ret) {
426 printf("SEC Firmware: Failed to initialize\n");
427 return ret;
428 }
429 }
430
431 debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
432 current_el());
433
434 /*
435 * The PE will be turned into target EL when returned from
436 * SEC Firmware.
437 */
438 if (current_el() != SEC_FIRMWARE_TARGET_EL)
439 return -EACCES;
440
441 sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
442
443 /* Set exception table and enable caches if it isn't EL3 */
444 if (current_el() != 3) {
445 c_runtime_cpu_setup();
446 enable_caches();
447 }
448
449 return 0;
450}
Ruchika Guptadb204d72017-08-16 15:58:10 +0530451
452/*
453 * fdt_fix_kaslr - Add kalsr-seed node in Device tree
454 * @fdt: Device tree
455 * @eret: 0 in case of error, 1 for success
456 */
457int fdt_fixup_kaslr(void *fdt)
458{
459 int nodeoffset;
460 int err, ret = 0;
461 u8 rand[8];
462
463#if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
464 /* Check if random seed generation is supported */
Ruchika Guptafc8e3402018-04-12 16:24:35 +0530465 if (sec_firmware_support_hwrng() == false) {
466 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
Ruchika Guptadb204d72017-08-16 15:58:10 +0530467 return 0;
Ruchika Guptafc8e3402018-04-12 16:24:35 +0530468 }
Ruchika Guptadb204d72017-08-16 15:58:10 +0530469
470 ret = sec_firmware_get_random(rand, 8);
471 if (ret < 0) {
472 printf("WARNING: No random number to set kaslr-seed\n");
473 return 0;
474 }
475
476 err = fdt_check_header(fdt);
477 if (err < 0) {
478 printf("fdt_chosen: %s\n", fdt_strerror(err));
479 return 0;
480 }
481
482 /* find or create "/chosen" node. */
483 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
484 if (nodeoffset < 0)
485 return 0;
486
487 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
488 sizeof(rand));
489 if (err < 0) {
490 printf("WARNING: can't set kaslr-seed %s.\n",
491 fdt_strerror(err));
492 return 0;
493 }
494 ret = 1;
495#endif
496
497 return ret;
498}