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