GNU Linux-libre 4.9.294-gnu1
[releases.git] / drivers / firmware / efi / libstub / arm-stub.c
1 /*
2  * EFI stub implementation that is shared by arm and arm64 architectures.
3  * This should be #included by the EFI stub implementation files.
4  *
5  * Copyright (C) 2013,2014 Linaro Limited
6  *     Roy Franz <roy.franz@linaro.org
7  * Copyright (C) 2013 Red Hat, Inc.
8  *     Mark Salter <msalter@redhat.com>
9  *
10  * This file is part of the Linux kernel, and is made available under the
11  * terms of the GNU General Public License version 2.
12  *
13  */
14
15 #include <linux/efi.h>
16 #include <linux/sort.h>
17 #include <asm/efi.h>
18
19 #include "efistub.h"
20
21
22 static int efi_get_secureboot(efi_system_table_t *sys_table_arg)
23 {
24         static efi_char16_t const sb_var_name[] = {
25                 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
26         static efi_char16_t const sm_var_name[] = {
27                 'S', 'e', 't', 'u', 'p', 'M', 'o', 'd', 'e', 0 };
28
29         efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
30         efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
31         u8 val;
32         unsigned long size = sizeof(val);
33         efi_status_t status;
34
35         status = f_getvar((efi_char16_t *)sb_var_name, (efi_guid_t *)&var_guid,
36                           NULL, &size, &val);
37
38         if (status != EFI_SUCCESS)
39                 goto out_efi_err;
40
41         if (val == 0)
42                 return 0;
43
44         status = f_getvar((efi_char16_t *)sm_var_name, (efi_guid_t *)&var_guid,
45                           NULL, &size, &val);
46
47         if (status != EFI_SUCCESS)
48                 goto out_efi_err;
49
50         if (val == 1)
51                 return 0;
52
53         return 1;
54
55 out_efi_err:
56         switch (status) {
57         case EFI_NOT_FOUND:
58                 return 0;
59         case EFI_DEVICE_ERROR:
60                 return -EIO;
61         case EFI_SECURITY_VIOLATION:
62                 return -EACCES;
63         default:
64                 return -EINVAL;
65         }
66 }
67
68 efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
69                              void *__image, void **__fh)
70 {
71         efi_file_io_interface_t *io;
72         efi_loaded_image_t *image = __image;
73         efi_file_handle_t *fh;
74         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
75         efi_status_t status;
76         void *handle = (void *)(unsigned long)image->device_handle;
77
78         status = sys_table_arg->boottime->handle_protocol(handle,
79                                  &fs_proto, (void **)&io);
80         if (status != EFI_SUCCESS) {
81                 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
82                 return status;
83         }
84
85         status = io->open_volume(io, &fh);
86         if (status != EFI_SUCCESS)
87                 efi_printk(sys_table_arg, "Failed to open volume\n");
88
89         *__fh = fh;
90         return status;
91 }
92
93 efi_status_t efi_file_close(void *handle)
94 {
95         efi_file_handle_t *fh = handle;
96
97         return fh->close(handle);
98 }
99
100 efi_status_t
101 efi_file_read(void *handle, unsigned long *size, void *addr)
102 {
103         efi_file_handle_t *fh = handle;
104
105         return fh->read(handle, size, addr);
106 }
107
108
109 efi_status_t
110 efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
111               efi_char16_t *filename_16, void **handle, u64 *file_sz)
112 {
113         efi_file_handle_t *h, *fh = __fh;
114         efi_file_info_t *info;
115         efi_status_t status;
116         efi_guid_t info_guid = EFI_FILE_INFO_ID;
117         unsigned long info_sz;
118
119         status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
120         if (status != EFI_SUCCESS) {
121                 efi_printk(sys_table_arg, "Failed to open file: ");
122                 efi_char16_printk(sys_table_arg, filename_16);
123                 efi_printk(sys_table_arg, "\n");
124                 return status;
125         }
126
127         *handle = h;
128
129         info_sz = 0;
130         status = h->get_info(h, &info_guid, &info_sz, NULL);
131         if (status != EFI_BUFFER_TOO_SMALL) {
132                 efi_printk(sys_table_arg, "Failed to get file info size\n");
133                 return status;
134         }
135
136 grow:
137         status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
138                                  info_sz, (void **)&info);
139         if (status != EFI_SUCCESS) {
140                 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
141                 return status;
142         }
143
144         status = h->get_info(h, &info_guid, &info_sz,
145                                                    info);
146         if (status == EFI_BUFFER_TOO_SMALL) {
147                 sys_table_arg->boottime->free_pool(info);
148                 goto grow;
149         }
150
151         *file_sz = info->file_size;
152         sys_table_arg->boottime->free_pool(info);
153
154         if (status != EFI_SUCCESS)
155                 efi_printk(sys_table_arg, "Failed to get initrd info\n");
156
157         return status;
158 }
159
160
161
162 void efi_char16_printk(efi_system_table_t *sys_table_arg,
163                               efi_char16_t *str)
164 {
165         struct efi_simple_text_output_protocol *out;
166
167         out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
168         out->output_string(out, str);
169 }
170
171 static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
172 {
173         efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
174         efi_status_t status;
175         unsigned long size;
176         void **gop_handle = NULL;
177         struct screen_info *si = NULL;
178
179         size = 0;
180         status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
181                                 &gop_proto, NULL, &size, gop_handle);
182         if (status == EFI_BUFFER_TOO_SMALL) {
183                 si = alloc_screen_info(sys_table_arg);
184                 if (!si)
185                         return NULL;
186                 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
187         }
188         return si;
189 }
190
191 /*
192  * This function handles the architcture specific differences between arm and
193  * arm64 regarding where the kernel image must be loaded and any memory that
194  * must be reserved. On failure it is required to free all
195  * all allocations it has made.
196  */
197 efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
198                                  unsigned long *image_addr,
199                                  unsigned long *image_size,
200                                  unsigned long *reserve_addr,
201                                  unsigned long *reserve_size,
202                                  unsigned long dram_base,
203                                  efi_loaded_image_t *image);
204 /*
205  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
206  * that is described in the PE/COFF header.  Most of the code is the same
207  * for both archictectures, with the arch-specific code provided in the
208  * handle_kernel_image() function.
209  */
210 unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
211                                unsigned long *image_addr)
212 {
213         efi_loaded_image_t *image;
214         efi_status_t status;
215         unsigned long image_size = 0;
216         unsigned long dram_base;
217         /* addr/point and size pairs for memory management*/
218         unsigned long initrd_addr;
219         u64 initrd_size = 0;
220         unsigned long fdt_addr = 0;  /* Original DTB */
221         unsigned long fdt_size = 0;
222         char *cmdline_ptr = NULL;
223         int cmdline_size = 0;
224         unsigned long new_fdt_addr;
225         efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
226         unsigned long reserve_addr = 0;
227         unsigned long reserve_size = 0;
228         int secure_boot = 0;
229         struct screen_info *si;
230
231         /* Check if we were booted by the EFI firmware */
232         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
233                 goto fail;
234
235         pr_efi(sys_table, "Booting Linux Kernel...\n");
236
237         status = check_platform_features(sys_table);
238         if (status != EFI_SUCCESS)
239                 goto fail;
240
241         /*
242          * Get a handle to the loaded image protocol.  This is used to get
243          * information about the running image, such as size and the command
244          * line.
245          */
246         status = sys_table->boottime->handle_protocol(handle,
247                                         &loaded_image_proto, (void *)&image);
248         if (status != EFI_SUCCESS) {
249                 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
250                 goto fail;
251         }
252
253         dram_base = get_dram_base(sys_table);
254         if (dram_base == EFI_ERROR) {
255                 pr_efi_err(sys_table, "Failed to find DRAM base\n");
256                 goto fail;
257         }
258
259         /*
260          * Get the command line from EFI, using the LOADED_IMAGE
261          * protocol. We are going to copy the command line into the
262          * device tree, so this can be allocated anywhere.
263          */
264         cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
265         if (!cmdline_ptr) {
266                 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
267                 goto fail;
268         }
269
270         si = setup_graphics(sys_table);
271
272         status = handle_kernel_image(sys_table, image_addr, &image_size,
273                                      &reserve_addr,
274                                      &reserve_size,
275                                      dram_base, image);
276         if (status != EFI_SUCCESS) {
277                 pr_efi_err(sys_table, "Failed to relocate kernel\n");
278                 goto fail_free_cmdline;
279         }
280
281         if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
282             IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
283             cmdline_size == 0)
284                 efi_parse_options(CONFIG_CMDLINE);
285
286         if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
287                 efi_parse_options(cmdline_ptr);
288
289         secure_boot = efi_get_secureboot(sys_table);
290         if (secure_boot > 0)
291                 pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
292
293         if (secure_boot < 0) {
294                 pr_efi_err(sys_table,
295                         "could not determine UEFI Secure Boot status.\n");
296         }
297
298         /*
299          * Unauthenticated device tree data is a security hazard, so
300          * ignore 'dtb=' unless UEFI Secure Boot is disabled.
301          */
302         if (secure_boot != 0 && strstr(cmdline_ptr, "dtb=")) {
303                 pr_efi(sys_table, "Ignoring DTB from command line.\n");
304         } else {
305                 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
306                                               "dtb=",
307                                               ~0UL, &fdt_addr, &fdt_size);
308
309                 if (status != EFI_SUCCESS) {
310                         pr_efi_err(sys_table, "Failed to load device tree!\n");
311                         goto fail_free_image;
312                 }
313         }
314
315         if (fdt_addr) {
316                 pr_efi(sys_table, "Using DTB from command line\n");
317         } else {
318                 /* Look for a device tree configuration table entry. */
319                 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
320                 if (fdt_addr)
321                         pr_efi(sys_table, "Using DTB from configuration table\n");
322         }
323
324         if (!fdt_addr)
325                 pr_efi(sys_table, "Generating empty DTB\n");
326
327         status = handle_cmdline_files(sys_table, image, cmdline_ptr,
328                                       "initrd=", dram_base + SZ_512M,
329                                       (unsigned long *)&initrd_addr,
330                                       (unsigned long *)&initrd_size);
331         if (status != EFI_SUCCESS)
332                 pr_efi_err(sys_table, "Failed initrd from command line!\n");
333
334         new_fdt_addr = fdt_addr;
335         status = allocate_new_fdt_and_exit_boot(sys_table, handle,
336                                 &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
337                                 initrd_addr, initrd_size, cmdline_ptr,
338                                 fdt_addr, fdt_size);
339
340         /*
341          * If all went well, we need to return the FDT address to the
342          * calling function so it can be passed to kernel as part of
343          * the kernel boot protocol.
344          */
345         if (status == EFI_SUCCESS)
346                 return new_fdt_addr;
347
348         pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
349
350         efi_free(sys_table, initrd_size, initrd_addr);
351         efi_free(sys_table, fdt_size, fdt_addr);
352
353 fail_free_image:
354         efi_free(sys_table, image_size, *image_addr);
355         efi_free(sys_table, reserve_size, reserve_addr);
356 fail_free_cmdline:
357         free_screen_info(sys_table, si);
358         efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
359 fail:
360         return EFI_ERROR;
361 }
362
363 /*
364  * This is the base address at which to start allocating virtual memory ranges
365  * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
366  * any allocation we choose, and eliminate the risk of a conflict after kexec.
367  * The value chosen is the largest non-zero power of 2 suitable for this purpose
368  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
369  * be mapped efficiently.
370  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
371  * map everything below 1 GB.
372  */
373 #define EFI_RT_VIRTUAL_BASE     SZ_512M
374
375 static int cmp_mem_desc(const void *l, const void *r)
376 {
377         const efi_memory_desc_t *left = l, *right = r;
378
379         return (left->phys_addr > right->phys_addr) ? 1 : -1;
380 }
381
382 /*
383  * Returns whether region @left ends exactly where region @right starts,
384  * or false if either argument is NULL.
385  */
386 static bool regions_are_adjacent(efi_memory_desc_t *left,
387                                  efi_memory_desc_t *right)
388 {
389         u64 left_end;
390
391         if (left == NULL || right == NULL)
392                 return false;
393
394         left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
395
396         return left_end == right->phys_addr;
397 }
398
399 /*
400  * Returns whether region @left and region @right have compatible memory type
401  * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
402  */
403 static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
404                                                       efi_memory_desc_t *right)
405 {
406         static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
407                                          EFI_MEMORY_WC | EFI_MEMORY_UC |
408                                          EFI_MEMORY_RUNTIME;
409
410         return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
411 }
412
413 /*
414  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
415  *
416  * This function populates the virt_addr fields of all memory region descriptors
417  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
418  * are also copied to @runtime_map, and their total count is returned in @count.
419  */
420 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
421                      unsigned long desc_size, efi_memory_desc_t *runtime_map,
422                      int *count)
423 {
424         u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
425         efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
426         int l;
427
428         /*
429          * To work around potential issues with the Properties Table feature
430          * introduced in UEFI 2.5, which may split PE/COFF executable images
431          * in memory into several RuntimeServicesCode and RuntimeServicesData
432          * regions, we need to preserve the relative offsets between adjacent
433          * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
434          * The easiest way to find adjacent regions is to sort the memory map
435          * before traversing it.
436          */
437         sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
438
439         for (l = 0; l < map_size; l += desc_size, prev = in) {
440                 u64 paddr, size;
441
442                 in = (void *)memory_map + l;
443                 if (!(in->attribute & EFI_MEMORY_RUNTIME))
444                         continue;
445
446                 paddr = in->phys_addr;
447                 size = in->num_pages * EFI_PAGE_SIZE;
448
449                 /*
450                  * Make the mapping compatible with 64k pages: this allows
451                  * a 4k page size kernel to kexec a 64k page size kernel and
452                  * vice versa.
453                  */
454                 if (!regions_are_adjacent(prev, in) ||
455                     !regions_have_compatible_memory_type_attrs(prev, in)) {
456
457                         paddr = round_down(in->phys_addr, SZ_64K);
458                         size += in->phys_addr - paddr;
459
460                         /*
461                          * Avoid wasting memory on PTEs by choosing a virtual
462                          * base that is compatible with section mappings if this
463                          * region has the appropriate size and physical
464                          * alignment. (Sections are 2 MB on 4k granule kernels)
465                          */
466                         if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
467                                 efi_virt_base = round_up(efi_virt_base, SZ_2M);
468                         else
469                                 efi_virt_base = round_up(efi_virt_base, SZ_64K);
470                 }
471
472                 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
473                 efi_virt_base += size;
474
475                 memcpy(out, in, desc_size);
476                 out = (void *)out + desc_size;
477                 ++*count;
478         }
479 }