2 * Helper functions used by the EFI stub on multiple
3 * architectures. This should be #included by the EFI stub
4 * implementation files.
6 * Copyright 2011 Intel Corporation; author Matt Fleming
8 * This file is part of the Linux kernel, and is made available
9 * under the terms of the GNU General Public License version 2.
13 #include <linux/efi.h>
19 * Some firmware implementations have problems reading files in one go.
20 * A read chunk size of 1MB seems to work for most platforms.
22 * Unfortunately, reading files in chunks triggers *other* bugs on some
23 * platforms, so we provide a way to disable this workaround, which can
24 * be done by passing "efi=nochunk" on the EFI boot stub command line.
26 * If you experience issues with initrd images being corrupt it's worth
27 * trying efi=nochunk, but chunking is enabled by default because there
28 * are far more machines that require the workaround than those that
29 * break with it enabled.
31 #define EFI_READ_CHUNK_SIZE (1024 * 1024)
33 static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
35 static int __section(.data) __nokaslr;
37 int __pure nokaslr(void)
43 * Allow the platform to override the allocation granularity: this allows
44 * systems that have the capability to run with a larger page size to deal
45 * with the allocations for initrd and fdt more efficiently.
47 #ifndef EFI_ALLOC_ALIGN
48 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
51 #define EFI_MMAP_NR_SLACK_SLOTS 8
54 efi_file_handle_t *handle;
58 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
62 for (s8 = str; *s8; s8++) {
63 efi_char16_t ch[2] = { 0 };
67 efi_char16_t nl[2] = { '\r', 0 };
68 efi_char16_printk(sys_table_arg, nl);
71 efi_char16_printk(sys_table_arg, ch);
75 static inline bool mmap_has_headroom(unsigned long buff_size,
76 unsigned long map_size,
77 unsigned long desc_size)
79 unsigned long slack = buff_size - map_size;
81 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
84 efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
85 struct efi_boot_memmap *map)
87 efi_memory_desc_t *m = NULL;
92 *map->desc_size = sizeof(*m);
93 *map->map_size = *map->desc_size * 32;
94 *map->buff_size = *map->map_size;
96 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
97 *map->map_size, (void **)&m);
98 if (status != EFI_SUCCESS)
103 status = efi_call_early(get_memory_map, map->map_size, m,
104 &key, map->desc_size, &desc_version);
105 if (status == EFI_BUFFER_TOO_SMALL ||
106 !mmap_has_headroom(*map->buff_size, *map->map_size,
108 efi_call_early(free_pool, m);
110 * Make sure there is some entries of headroom so that the
111 * buffer can be reused for a new map after allocations are
112 * no longer permitted. Its unlikely that the map will grow to
113 * exceed this headroom once we are ready to trigger
116 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
117 *map->buff_size = *map->map_size;
121 if (status != EFI_SUCCESS)
122 efi_call_early(free_pool, m);
124 if (map->key_ptr && status == EFI_SUCCESS)
126 if (map->desc_ver && status == EFI_SUCCESS)
127 *map->desc_ver = desc_version;
135 unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
138 unsigned long map_size, buff_size;
139 unsigned long membase = EFI_ERROR;
140 struct efi_memory_map map;
141 efi_memory_desc_t *md;
142 struct efi_boot_memmap boot_map;
144 boot_map.map = (efi_memory_desc_t **)&map.map;
145 boot_map.map_size = &map_size;
146 boot_map.desc_size = &map.desc_size;
147 boot_map.desc_ver = NULL;
148 boot_map.key_ptr = NULL;
149 boot_map.buff_size = &buff_size;
151 status = efi_get_memory_map(sys_table_arg, &boot_map);
152 if (status != EFI_SUCCESS)
155 map.map_end = map.map + map_size;
157 for_each_efi_memory_desc_in_map(&map, md) {
158 if (md->attribute & EFI_MEMORY_WB) {
159 if (membase > md->phys_addr)
160 membase = md->phys_addr;
164 efi_call_early(free_pool, map.map);
170 * Allocate at the highest possible address that is not above 'max'.
172 efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
173 unsigned long size, unsigned long align,
174 unsigned long *addr, unsigned long max)
176 unsigned long map_size, desc_size, buff_size;
177 efi_memory_desc_t *map;
179 unsigned long nr_pages;
182 struct efi_boot_memmap boot_map;
185 boot_map.map_size = &map_size;
186 boot_map.desc_size = &desc_size;
187 boot_map.desc_ver = NULL;
188 boot_map.key_ptr = NULL;
189 boot_map.buff_size = &buff_size;
191 status = efi_get_memory_map(sys_table_arg, &boot_map);
192 if (status != EFI_SUCCESS)
196 * Enforce minimum alignment that EFI requires when requesting
197 * a specific address. We are doing page-based allocations,
198 * so we must be aligned to a page.
200 if (align < EFI_ALLOC_ALIGN)
201 align = EFI_ALLOC_ALIGN;
203 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
205 for (i = 0; i < map_size / desc_size; i++) {
206 efi_memory_desc_t *desc;
207 unsigned long m = (unsigned long)map;
210 desc = (efi_memory_desc_t *)(m + (i * desc_size));
211 if (desc->type != EFI_CONVENTIONAL_MEMORY)
214 if (desc->num_pages < nr_pages)
217 start = desc->phys_addr;
218 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
223 if ((start + size) > end)
226 if (round_down(end - size, align) < start)
229 start = round_down(end - size, align);
232 * Don't allocate at 0x0. It will confuse code that
233 * checks pointers against NULL.
238 if (start > max_addr)
243 status = EFI_NOT_FOUND;
245 status = efi_call_early(allocate_pages,
246 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
247 nr_pages, &max_addr);
248 if (status != EFI_SUCCESS) {
257 efi_call_early(free_pool, map);
263 * Allocate at the lowest possible address.
265 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
266 unsigned long size, unsigned long align,
269 unsigned long map_size, desc_size, buff_size;
270 efi_memory_desc_t *map;
272 unsigned long nr_pages;
274 struct efi_boot_memmap boot_map;
277 boot_map.map_size = &map_size;
278 boot_map.desc_size = &desc_size;
279 boot_map.desc_ver = NULL;
280 boot_map.key_ptr = NULL;
281 boot_map.buff_size = &buff_size;
283 status = efi_get_memory_map(sys_table_arg, &boot_map);
284 if (status != EFI_SUCCESS)
288 * Enforce minimum alignment that EFI requires when requesting
289 * a specific address. We are doing page-based allocations,
290 * so we must be aligned to a page.
292 if (align < EFI_ALLOC_ALIGN)
293 align = EFI_ALLOC_ALIGN;
295 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
296 for (i = 0; i < map_size / desc_size; i++) {
297 efi_memory_desc_t *desc;
298 unsigned long m = (unsigned long)map;
301 desc = (efi_memory_desc_t *)(m + (i * desc_size));
303 if (desc->type != EFI_CONVENTIONAL_MEMORY)
306 if (desc->num_pages < nr_pages)
309 start = desc->phys_addr;
310 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
313 * Don't allocate at 0x0. It will confuse code that
314 * checks pointers against NULL. Skip the first 8
315 * bytes so we start at a nice even number.
320 start = round_up(start, align);
321 if ((start + size) > end)
324 status = efi_call_early(allocate_pages,
325 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
327 if (status == EFI_SUCCESS) {
333 if (i == map_size / desc_size)
334 status = EFI_NOT_FOUND;
336 efi_call_early(free_pool, map);
341 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
344 unsigned long nr_pages;
349 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
350 efi_call_early(free_pages, addr, nr_pages);
354 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
355 * option, e.g. efi=nochunk.
357 * It should be noted that efi= is parsed in two very different
358 * environments, first in the early boot environment of the EFI boot
359 * stub, and subsequently during the kernel boot.
361 efi_status_t efi_parse_options(char const *cmdline)
365 str = strstr(cmdline, "nokaslr");
366 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
370 * If no EFI parameters were specified on the cmdline we've got
373 str = strstr(cmdline, "efi=");
377 /* Skip ahead to first argument */
378 str += strlen("efi=");
381 * Remember, because efi= is also used by the kernel we need to
382 * skip over arguments we don't understand.
385 if (!strncmp(str, "nochunk", 7)) {
386 str += strlen("nochunk");
390 /* Group words together, delimited by "," */
391 while (*str && *str != ',')
402 * Check the cmdline for a LILO-style file= arguments.
404 * We only support loading a file from the same filesystem as
407 efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
408 efi_loaded_image_t *image,
409 char *cmd_line, char *option_string,
410 unsigned long max_addr,
411 unsigned long *load_addr,
412 unsigned long *load_size)
414 struct file_info *files;
415 unsigned long file_addr;
417 efi_file_handle_t *fh = NULL;
428 j = 0; /* See close_handles */
430 if (!load_addr || !load_size)
431 return EFI_INVALID_PARAMETER;
439 for (nr_files = 0; *str; nr_files++) {
440 str = strstr(str, option_string);
444 str += strlen(option_string);
446 /* Skip any leading slashes */
447 while (*str == '/' || *str == '\\')
450 while (*str && *str != ' ' && *str != '\n')
457 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
458 nr_files * sizeof(*files), (void **)&files);
459 if (status != EFI_SUCCESS) {
460 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
465 for (i = 0; i < nr_files; i++) {
466 struct file_info *file;
467 efi_char16_t filename_16[256];
470 str = strstr(str, option_string);
474 str += strlen(option_string);
479 /* Skip any leading slashes */
480 while (*str == '/' || *str == '\\')
483 while (*str && *str != ' ' && *str != '\n') {
484 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
497 /* Only open the volume once. */
499 status = efi_open_volume(sys_table_arg, image,
501 if (status != EFI_SUCCESS)
505 status = efi_file_size(sys_table_arg, fh, filename_16,
506 (void **)&file->handle, &file->size);
507 if (status != EFI_SUCCESS)
510 file_size_total += file->size;
513 if (file_size_total) {
517 * Multiple files need to be at consecutive addresses in memory,
518 * so allocate enough memory for all the files. This is used
519 * for loading multiple files.
521 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
522 &file_addr, max_addr);
523 if (status != EFI_SUCCESS) {
524 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
528 /* We've run out of free low memory. */
529 if (file_addr > max_addr) {
530 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
531 status = EFI_INVALID_PARAMETER;
532 goto free_file_total;
536 for (j = 0; j < nr_files; j++) {
539 size = files[j].size;
541 unsigned long chunksize;
543 if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
544 chunksize = __chunk_size;
548 status = efi_file_read(files[j].handle,
551 if (status != EFI_SUCCESS) {
552 pr_efi_err(sys_table_arg, "Failed to read file\n");
553 goto free_file_total;
559 efi_file_close(files[j].handle);
564 efi_call_early(free_pool, files);
566 *load_addr = file_addr;
567 *load_size = file_size_total;
572 efi_free(sys_table_arg, file_size_total, file_addr);
575 for (k = j; k < i; k++)
576 efi_file_close(files[k].handle);
578 efi_call_early(free_pool, files);
586 * Relocate a kernel image, either compressed or uncompressed.
587 * In the ARM64 case, all kernel images are currently
588 * uncompressed, and as such when we relocate it we need to
589 * allocate additional space for the BSS segment. Any low
590 * memory that this function should avoid needs to be
591 * unavailable in the EFI memory map, as if the preferred
592 * address is not available the lowest available address will
595 efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
596 unsigned long *image_addr,
597 unsigned long image_size,
598 unsigned long alloc_size,
599 unsigned long preferred_addr,
600 unsigned long alignment)
602 unsigned long cur_image_addr;
603 unsigned long new_addr = 0;
605 unsigned long nr_pages;
606 efi_physical_addr_t efi_addr = preferred_addr;
608 if (!image_addr || !image_size || !alloc_size)
609 return EFI_INVALID_PARAMETER;
610 if (alloc_size < image_size)
611 return EFI_INVALID_PARAMETER;
613 cur_image_addr = *image_addr;
616 * The EFI firmware loader could have placed the kernel image
617 * anywhere in memory, but the kernel has restrictions on the
618 * max physical address it can run at. Some architectures
619 * also have a prefered address, so first try to relocate
620 * to the preferred address. If that fails, allocate as low
621 * as possible while respecting the required alignment.
623 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
624 status = efi_call_early(allocate_pages,
625 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
626 nr_pages, &efi_addr);
629 * If preferred address allocation failed allocate as low as
632 if (status != EFI_SUCCESS) {
633 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
636 if (status != EFI_SUCCESS) {
637 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
642 * We know source/dest won't overlap since both memory ranges
643 * have been allocated by UEFI, so we can safely use memcpy.
645 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
647 /* Return the new address of the relocated image. */
648 *image_addr = new_addr;
654 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
655 * This overestimates for surrogates, but that is okay.
657 static int efi_utf8_bytes(u16 c)
659 return 1 + (c >= 0x80) + (c >= 0x800);
663 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
665 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
671 if (n && c >= 0xd800 && c <= 0xdbff &&
672 *src >= 0xdc00 && *src <= 0xdfff) {
673 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
677 if (c >= 0xd800 && c <= 0xdfff)
678 c = 0xfffd; /* Unmatched surrogate */
684 *dst++ = 0xc0 + (c >> 6);
688 *dst++ = 0xe0 + (c >> 12);
691 *dst++ = 0xf0 + (c >> 18);
692 *dst++ = 0x80 + ((c >> 12) & 0x3f);
694 *dst++ = 0x80 + ((c >> 6) & 0x3f);
696 *dst++ = 0x80 + (c & 0x3f);
702 #ifndef MAX_CMDLINE_ADDRESS
703 #define MAX_CMDLINE_ADDRESS ULONG_MAX
707 * Convert the unicode UEFI command line to ASCII to pass to kernel.
708 * Size of memory allocated return in *cmd_line_len.
709 * Returns NULL on error.
711 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
712 efi_loaded_image_t *image,
717 unsigned long cmdline_addr = 0;
718 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
719 const u16 *options = image->load_options;
720 int options_bytes = 0; /* UTF-8 bytes */
721 int options_chars = 0; /* UTF-16 chars */
727 while (*s2 && *s2 != '\n'
728 && options_chars < load_options_chars) {
729 options_bytes += efi_utf8_bytes(*s2++);
734 if (!options_chars) {
735 /* No command line options, so return empty string*/
739 options_bytes++; /* NUL termination */
741 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
742 &cmdline_addr, MAX_CMDLINE_ADDRESS);
743 if (status != EFI_SUCCESS)
746 s1 = (u8 *)cmdline_addr;
747 s2 = (const u16 *)options;
749 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
752 *cmd_line_len = options_bytes;
753 return (char *)cmdline_addr;
757 * Handle calling ExitBootServices according to the requirements set out by the
758 * spec. Obtains the current memory map, and returns that info after calling
759 * ExitBootServices. The client must specify a function to perform any
760 * processing of the memory map data prior to ExitBootServices. A client
761 * specific structure may be passed to the function via priv. The client
762 * function may be called multiple times.
764 efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
766 struct efi_boot_memmap *map,
768 efi_exit_boot_map_processing priv_func)
772 status = efi_get_memory_map(sys_table_arg, map);
774 if (status != EFI_SUCCESS)
777 status = priv_func(sys_table_arg, map, priv);
778 if (status != EFI_SUCCESS)
781 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
783 if (status == EFI_INVALID_PARAMETER) {
785 * The memory map changed between efi_get_memory_map() and
786 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
787 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
788 * updated map, and try again. The spec implies one retry
789 * should be sufficent, which is confirmed against the EDK2
790 * implementation. Per the spec, we can only invoke
791 * get_memory_map() and exit_boot_services() - we cannot alloc
792 * so efi_get_memory_map() cannot be used, and we must reuse
793 * the buffer. For all practical purposes, the headroom in the
794 * buffer should account for any changes in the map so the call
795 * to get_memory_map() is expected to succeed here.
797 *map->map_size = *map->buff_size;
798 status = efi_call_early(get_memory_map,
805 /* exit_boot_services() was called, thus cannot free */
806 if (status != EFI_SUCCESS)
809 status = priv_func(sys_table_arg, map, priv);
810 /* exit_boot_services() was called, thus cannot free */
811 if (status != EFI_SUCCESS)
814 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
817 /* exit_boot_services() was called, thus cannot free */
818 if (status != EFI_SUCCESS)
824 efi_call_early(free_pool, *map->map);