armv8: layerscape: get rid of smc_call()
[platform/kernel/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 <asm/global_data.h>
14 #include <asm/ptrace.h>
15 #include <linux/kernel.h>
16 #include <linux/arm-smccc.h>
17 #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
23 DECLARE_GLOBAL_DATA_PTR;
24 extern 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)
29 /*
30  * Secure firmware load addr
31  * Flags used: 0x1 secure firmware has been loaded to secure memory
32  *             0x2 secure firmware is running
33  */
34 phys_addr_t sec_firmware_addr;
35
36 #ifndef SEC_FIRMWARE_FIT_IMAGE
37 #define SEC_FIRMWARE_FIT_IMAGE          "firmware"
38 #endif
39 #ifndef SEC_FIRMWARE_FIT_CNF_NAME
40 #define SEC_FIRMWARE_FIT_CNF_NAME       "config-1"
41 #endif
42 #ifndef SEC_FIRMWARE_TARGET_EL
43 #define SEC_FIRMWARE_TARGET_EL          2
44 #endif
45
46 static 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
54         conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
55
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  */
98 static 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
115 /*
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  */
122 static 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;
126         int conf_node_off, ld_node_off, images;
127         char *conf_node_name = NULL;
128         const void *data;
129         size_t size;
130         ulong load;
131         const char *name, *str, *type;
132         int len;
133
134         conf_node_name = SEC_FIRMWARE_FIT_CNF_NAME;
135
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);
139                 return -ENOENT;
140         }
141
142         /* 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                 }
168
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);
197
198                 /* 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         }
210
211         return 0;
212 }
213
214 static 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
226  * to secure memory. Also load any loadable if present along with SEC
227  * Firmware image.
228  */
229 static int sec_firmware_load_image(const void *sec_firmware_img,
230                                    u32 *loadable_l, u32 *loadable_h)
231 {
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
283         /*
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
292         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
298 out:
299         printf("SEC Firmware: error (%d)\n", ret);
300         sec_firmware_addr = 0;
301
302         return ret;
303 }
304
305 static 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
321         if (fit_check_format(sec_firmware_img, IMAGE_SIZE_INVAL)) {
322                 printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
323                 return false;
324         }
325
326         return true;
327 }
328
329 #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
330 /*
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  */
342 unsigned int sec_firmware_support_psci_version(void)
343 {
344         if (current_el() == SEC_FIRMWARE_TARGET_EL)
345                 return _sec_firmware_support_psci_version();
346
347         return PSCI_INVALID_VER;
348 }
349 #endif
350
351 /*
352  * 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  */
357 bool sec_firmware_support_hwrng(void)
358 {
359 #ifdef CONFIG_TFABOOT
360         /* return true as TFA has one job ring reserved */
361         return true;
362 #endif
363         if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
364                 return true;
365         }
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  */
376 int sec_firmware_get_random(uint8_t *rand, int bytes)
377 {
378         struct arm_smccc_res res;
379         unsigned long long num;
380         int param1;
381
382         if (!bytes || bytes > 8) {
383                 printf("Max Random bytes genration supported is 8\n");
384                 return -1;
385         }
386         if (bytes <= 4)
387                 param1 = 0;
388         else
389                 param1 = 1;
390
391 #define SIP_RNG_64 0xC200FF11
392         arm_smccc_smc(SIP_RNG_64, param1, 0, 0, 0, 0, 0, 0, &res);
393         if (res.a0)
394                 return -1;
395
396         num = res.a1;
397         memcpy(rand, &num, bytes);
398
399         return 0;
400 }
401
402 /*
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
409  */
410 int sec_firmware_init(const void *sec_firmware_img,
411                         u32 *eret_hold_l,
412                         u32 *eret_hold_h,
413                         u32 *loadable_l,
414                         u32 *loadable_h)
415 {
416         int ret;
417
418         if (!sec_firmware_is_valid(sec_firmware_img))
419                 return -EINVAL;
420
421         ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
422                                       loadable_h);
423         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 }
454
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  */
460 int 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 */
468         if (sec_firmware_support_hwrng() == false) {
469                 printf("WARNING: SEC firmware not running, no kaslr-seed\n");
470                 return 0;
471         }
472
473         err = sec_firmware_get_random(rand, 8);
474         if (err < 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 }