efi/libstub: Unify command line param parsing
[platform/kernel/linux-rpi.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 efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
22                              void *__image, void **__fh)
23 {
24         efi_file_io_interface_t *io;
25         efi_loaded_image_t *image = __image;
26         efi_file_handle_t *fh;
27         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
28         efi_status_t status;
29         void *handle = (void *)(unsigned long)image->device_handle;
30
31         status = sys_table_arg->boottime->handle_protocol(handle,
32                                  &fs_proto, (void **)&io);
33         if (status != EFI_SUCCESS) {
34                 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
35                 return status;
36         }
37
38         status = io->open_volume(io, &fh);
39         if (status != EFI_SUCCESS)
40                 efi_printk(sys_table_arg, "Failed to open volume\n");
41
42         *__fh = fh;
43         return status;
44 }
45
46 void efi_char16_printk(efi_system_table_t *sys_table_arg,
47                               efi_char16_t *str)
48 {
49         struct efi_simple_text_output_protocol *out;
50
51         out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
52         out->output_string(out, str);
53 }
54
55 static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
56 {
57         efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
58         efi_status_t status;
59         unsigned long size;
60         void **gop_handle = NULL;
61         struct screen_info *si = NULL;
62
63         size = 0;
64         status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
65                                 &gop_proto, NULL, &size, gop_handle);
66         if (status == EFI_BUFFER_TOO_SMALL) {
67                 si = alloc_screen_info(sys_table_arg);
68                 if (!si)
69                         return NULL;
70                 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
71         }
72         return si;
73 }
74
75 /*
76  * This function handles the architcture specific differences between arm and
77  * arm64 regarding where the kernel image must be loaded and any memory that
78  * must be reserved. On failure it is required to free all
79  * all allocations it has made.
80  */
81 efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
82                                  unsigned long *image_addr,
83                                  unsigned long *image_size,
84                                  unsigned long *reserve_addr,
85                                  unsigned long *reserve_size,
86                                  unsigned long dram_base,
87                                  efi_loaded_image_t *image);
88 /*
89  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
90  * that is described in the PE/COFF header.  Most of the code is the same
91  * for both archictectures, with the arch-specific code provided in the
92  * handle_kernel_image() function.
93  */
94 unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
95                                unsigned long *image_addr)
96 {
97         efi_loaded_image_t *image;
98         efi_status_t status;
99         unsigned long image_size = 0;
100         unsigned long dram_base;
101         /* addr/point and size pairs for memory management*/
102         unsigned long initrd_addr;
103         u64 initrd_size = 0;
104         unsigned long fdt_addr = 0;  /* Original DTB */
105         unsigned long fdt_size = 0;
106         char *cmdline_ptr = NULL;
107         int cmdline_size = 0;
108         unsigned long new_fdt_addr;
109         efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
110         unsigned long reserve_addr = 0;
111         unsigned long reserve_size = 0;
112         enum efi_secureboot_mode secure_boot;
113         struct screen_info *si;
114
115         /* Check if we were booted by the EFI firmware */
116         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
117                 goto fail;
118
119         pr_efi(sys_table, "Booting Linux Kernel...\n");
120
121         status = check_platform_features(sys_table);
122         if (status != EFI_SUCCESS)
123                 goto fail;
124
125         /*
126          * Get a handle to the loaded image protocol.  This is used to get
127          * information about the running image, such as size and the command
128          * line.
129          */
130         status = sys_table->boottime->handle_protocol(handle,
131                                         &loaded_image_proto, (void *)&image);
132         if (status != EFI_SUCCESS) {
133                 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
134                 goto fail;
135         }
136
137         dram_base = get_dram_base(sys_table);
138         if (dram_base == EFI_ERROR) {
139                 pr_efi_err(sys_table, "Failed to find DRAM base\n");
140                 goto fail;
141         }
142
143         /*
144          * Get the command line from EFI, using the LOADED_IMAGE
145          * protocol. We are going to copy the command line into the
146          * device tree, so this can be allocated anywhere.
147          */
148         cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
149         if (!cmdline_ptr) {
150                 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
151                 goto fail;
152         }
153
154         si = setup_graphics(sys_table);
155
156         status = handle_kernel_image(sys_table, image_addr, &image_size,
157                                      &reserve_addr,
158                                      &reserve_size,
159                                      dram_base, image);
160         if (status != EFI_SUCCESS) {
161                 pr_efi_err(sys_table, "Failed to relocate kernel\n");
162                 goto fail_free_cmdline;
163         }
164
165         if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
166             IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
167             cmdline_size == 0)
168                 efi_parse_options(CONFIG_CMDLINE);
169
170         if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
171                 efi_parse_options(cmdline_ptr);
172
173         secure_boot = efi_get_secureboot(sys_table);
174
175         /*
176          * Unauthenticated device tree data is a security hazard, so ignore
177          * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
178          * boot is enabled if we can't determine its state.
179          */
180         if (secure_boot != efi_secureboot_mode_disabled &&
181             strstr(cmdline_ptr, "dtb=")) {
182                 pr_efi(sys_table, "Ignoring DTB from command line.\n");
183         } else {
184                 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
185                                               "dtb=",
186                                               ~0UL, &fdt_addr, &fdt_size);
187
188                 if (status != EFI_SUCCESS) {
189                         pr_efi_err(sys_table, "Failed to load device tree!\n");
190                         goto fail_free_image;
191                 }
192         }
193
194         if (fdt_addr) {
195                 pr_efi(sys_table, "Using DTB from command line\n");
196         } else {
197                 /* Look for a device tree configuration table entry. */
198                 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
199                 if (fdt_addr)
200                         pr_efi(sys_table, "Using DTB from configuration table\n");
201         }
202
203         if (!fdt_addr)
204                 pr_efi(sys_table, "Generating empty DTB\n");
205
206         status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
207                                       efi_get_max_initrd_addr(dram_base,
208                                                               *image_addr),
209                                       (unsigned long *)&initrd_addr,
210                                       (unsigned long *)&initrd_size);
211         if (status != EFI_SUCCESS)
212                 pr_efi_err(sys_table, "Failed initrd from command line!\n");
213
214         efi_random_get_seed(sys_table);
215
216         new_fdt_addr = fdt_addr;
217         status = allocate_new_fdt_and_exit_boot(sys_table, handle,
218                                 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
219                                 initrd_addr, initrd_size, cmdline_ptr,
220                                 fdt_addr, fdt_size);
221
222         /*
223          * If all went well, we need to return the FDT address to the
224          * calling function so it can be passed to kernel as part of
225          * the kernel boot protocol.
226          */
227         if (status == EFI_SUCCESS)
228                 return new_fdt_addr;
229
230         pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
231
232         efi_free(sys_table, initrd_size, initrd_addr);
233         efi_free(sys_table, fdt_size, fdt_addr);
234
235 fail_free_image:
236         efi_free(sys_table, image_size, *image_addr);
237         efi_free(sys_table, reserve_size, reserve_addr);
238 fail_free_cmdline:
239         free_screen_info(sys_table, si);
240         efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
241 fail:
242         return EFI_ERROR;
243 }
244
245 /*
246  * This is the base address at which to start allocating virtual memory ranges
247  * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
248  * any allocation we choose, and eliminate the risk of a conflict after kexec.
249  * The value chosen is the largest non-zero power of 2 suitable for this purpose
250  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
251  * be mapped efficiently.
252  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
253  * map everything below 1 GB.
254  */
255 #define EFI_RT_VIRTUAL_BASE     SZ_512M
256
257 static int cmp_mem_desc(const void *l, const void *r)
258 {
259         const efi_memory_desc_t *left = l, *right = r;
260
261         return (left->phys_addr > right->phys_addr) ? 1 : -1;
262 }
263
264 /*
265  * Returns whether region @left ends exactly where region @right starts,
266  * or false if either argument is NULL.
267  */
268 static bool regions_are_adjacent(efi_memory_desc_t *left,
269                                  efi_memory_desc_t *right)
270 {
271         u64 left_end;
272
273         if (left == NULL || right == NULL)
274                 return false;
275
276         left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
277
278         return left_end == right->phys_addr;
279 }
280
281 /*
282  * Returns whether region @left and region @right have compatible memory type
283  * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
284  */
285 static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
286                                                       efi_memory_desc_t *right)
287 {
288         static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
289                                          EFI_MEMORY_WC | EFI_MEMORY_UC |
290                                          EFI_MEMORY_RUNTIME;
291
292         return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
293 }
294
295 /*
296  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
297  *
298  * This function populates the virt_addr fields of all memory region descriptors
299  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
300  * are also copied to @runtime_map, and their total count is returned in @count.
301  */
302 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
303                      unsigned long desc_size, efi_memory_desc_t *runtime_map,
304                      int *count)
305 {
306         u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
307         efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
308         int l;
309
310         /*
311          * To work around potential issues with the Properties Table feature
312          * introduced in UEFI 2.5, which may split PE/COFF executable images
313          * in memory into several RuntimeServicesCode and RuntimeServicesData
314          * regions, we need to preserve the relative offsets between adjacent
315          * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
316          * The easiest way to find adjacent regions is to sort the memory map
317          * before traversing it.
318          */
319         sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
320
321         for (l = 0; l < map_size; l += desc_size, prev = in) {
322                 u64 paddr, size;
323
324                 in = (void *)memory_map + l;
325                 if (!(in->attribute & EFI_MEMORY_RUNTIME))
326                         continue;
327
328                 paddr = in->phys_addr;
329                 size = in->num_pages * EFI_PAGE_SIZE;
330
331                 /*
332                  * Make the mapping compatible with 64k pages: this allows
333                  * a 4k page size kernel to kexec a 64k page size kernel and
334                  * vice versa.
335                  */
336                 if (!regions_are_adjacent(prev, in) ||
337                     !regions_have_compatible_memory_type_attrs(prev, in)) {
338
339                         paddr = round_down(in->phys_addr, SZ_64K);
340                         size += in->phys_addr - paddr;
341
342                         /*
343                          * Avoid wasting memory on PTEs by choosing a virtual
344                          * base that is compatible with section mappings if this
345                          * region has the appropriate size and physical
346                          * alignment. (Sections are 2 MB on 4k granule kernels)
347                          */
348                         if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
349                                 efi_virt_base = round_up(efi_virt_base, SZ_2M);
350                         else
351                                 efi_virt_base = round_up(efi_virt_base, SZ_64K);
352                 }
353
354                 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
355                 efi_virt_base += size;
356
357                 memcpy(out, in, desc_size);
358                 out = (void *)out + desc_size;
359                 ++*count;
360         }
361 }