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;
36 static int __section(.data) __quiet;
37 static int __section(.data) __novamap;
39 int __pure nokaslr(void)
43 int __pure is_quiet(void)
47 int __pure novamap(void)
52 #define EFI_MMAP_NR_SLACK_SLOTS 8
55 efi_file_handle_t *handle;
59 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
63 for (s8 = str; *s8; s8++) {
64 efi_char16_t ch[2] = { 0 };
68 efi_char16_t nl[2] = { '\r', 0 };
69 efi_char16_printk(sys_table_arg, nl);
72 efi_char16_printk(sys_table_arg, ch);
76 static inline bool mmap_has_headroom(unsigned long buff_size,
77 unsigned long map_size,
78 unsigned long desc_size)
80 unsigned long slack = buff_size - map_size;
82 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
85 efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
86 struct efi_boot_memmap *map)
88 efi_memory_desc_t *m = NULL;
93 *map->desc_size = sizeof(*m);
94 *map->map_size = *map->desc_size * 32;
95 *map->buff_size = *map->map_size;
97 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
98 *map->map_size, (void **)&m);
99 if (status != EFI_SUCCESS)
104 status = efi_call_early(get_memory_map, map->map_size, m,
105 &key, map->desc_size, &desc_version);
106 if (status == EFI_BUFFER_TOO_SMALL ||
107 !mmap_has_headroom(*map->buff_size, *map->map_size,
109 efi_call_early(free_pool, m);
111 * Make sure there is some entries of headroom so that the
112 * buffer can be reused for a new map after allocations are
113 * no longer permitted. Its unlikely that the map will grow to
114 * exceed this headroom once we are ready to trigger
117 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
118 *map->buff_size = *map->map_size;
122 if (status != EFI_SUCCESS)
123 efi_call_early(free_pool, m);
125 if (map->key_ptr && status == EFI_SUCCESS)
127 if (map->desc_ver && status == EFI_SUCCESS)
128 *map->desc_ver = desc_version;
136 unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
139 unsigned long map_size, buff_size;
140 unsigned long membase = EFI_ERROR;
141 struct efi_memory_map map;
142 efi_memory_desc_t *md;
143 struct efi_boot_memmap boot_map;
145 boot_map.map = (efi_memory_desc_t **)&map.map;
146 boot_map.map_size = &map_size;
147 boot_map.desc_size = &map.desc_size;
148 boot_map.desc_ver = NULL;
149 boot_map.key_ptr = NULL;
150 boot_map.buff_size = &buff_size;
152 status = efi_get_memory_map(sys_table_arg, &boot_map);
153 if (status != EFI_SUCCESS)
156 map.map_end = map.map + map_size;
158 for_each_efi_memory_desc_in_map(&map, md) {
159 if (md->attribute & EFI_MEMORY_WB) {
160 if (membase > md->phys_addr)
161 membase = md->phys_addr;
165 efi_call_early(free_pool, map.map);
171 * Allocate at the highest possible address that is not above 'max'.
173 efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
174 unsigned long size, unsigned long align,
175 unsigned long *addr, unsigned long max)
177 unsigned long map_size, desc_size, buff_size;
178 efi_memory_desc_t *map;
180 unsigned long nr_pages;
183 struct efi_boot_memmap boot_map;
186 boot_map.map_size = &map_size;
187 boot_map.desc_size = &desc_size;
188 boot_map.desc_ver = NULL;
189 boot_map.key_ptr = NULL;
190 boot_map.buff_size = &buff_size;
192 status = efi_get_memory_map(sys_table_arg, &boot_map);
193 if (status != EFI_SUCCESS)
197 * Enforce minimum alignment that EFI or Linux requires when
198 * requesting a specific address. We are doing page-based (or
199 * larger) allocations, and both the address and size must meet
200 * alignment constraints.
202 if (align < EFI_ALLOC_ALIGN)
203 align = EFI_ALLOC_ALIGN;
205 size = round_up(size, EFI_ALLOC_ALIGN);
206 nr_pages = size / EFI_PAGE_SIZE;
208 for (i = 0; i < map_size / desc_size; i++) {
209 efi_memory_desc_t *desc;
210 unsigned long m = (unsigned long)map;
213 desc = efi_early_memdesc_ptr(m, desc_size, i);
214 if (desc->type != EFI_CONVENTIONAL_MEMORY)
217 if (desc->num_pages < nr_pages)
220 start = desc->phys_addr;
221 end = start + desc->num_pages * EFI_PAGE_SIZE;
226 if ((start + size) > end)
229 if (round_down(end - size, align) < start)
232 start = round_down(end - size, align);
235 * Don't allocate at 0x0. It will confuse code that
236 * checks pointers against NULL.
241 if (start > max_addr)
246 status = EFI_NOT_FOUND;
248 status = efi_call_early(allocate_pages,
249 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
250 nr_pages, &max_addr);
251 if (status != EFI_SUCCESS) {
260 efi_call_early(free_pool, map);
266 * Allocate at the lowest possible address.
268 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
269 unsigned long size, unsigned long align,
272 unsigned long map_size, desc_size, buff_size;
273 efi_memory_desc_t *map;
275 unsigned long nr_pages;
277 struct efi_boot_memmap boot_map;
280 boot_map.map_size = &map_size;
281 boot_map.desc_size = &desc_size;
282 boot_map.desc_ver = NULL;
283 boot_map.key_ptr = NULL;
284 boot_map.buff_size = &buff_size;
286 status = efi_get_memory_map(sys_table_arg, &boot_map);
287 if (status != EFI_SUCCESS)
291 * Enforce minimum alignment that EFI or Linux requires when
292 * requesting a specific address. We are doing page-based (or
293 * larger) allocations, and both the address and size must meet
294 * alignment constraints.
296 if (align < EFI_ALLOC_ALIGN)
297 align = EFI_ALLOC_ALIGN;
299 size = round_up(size, EFI_ALLOC_ALIGN);
300 nr_pages = size / EFI_PAGE_SIZE;
301 for (i = 0; i < map_size / desc_size; i++) {
302 efi_memory_desc_t *desc;
303 unsigned long m = (unsigned long)map;
306 desc = efi_early_memdesc_ptr(m, desc_size, i);
308 if (desc->type != EFI_CONVENTIONAL_MEMORY)
311 if (desc->num_pages < nr_pages)
314 start = desc->phys_addr;
315 end = start + desc->num_pages * EFI_PAGE_SIZE;
318 * Don't allocate at 0x0. It will confuse code that
319 * checks pointers against NULL. Skip the first 8
320 * bytes so we start at a nice even number.
325 start = round_up(start, align);
326 if ((start + size) > end)
329 status = efi_call_early(allocate_pages,
330 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
332 if (status == EFI_SUCCESS) {
338 if (i == map_size / desc_size)
339 status = EFI_NOT_FOUND;
341 efi_call_early(free_pool, map);
346 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
349 unsigned long nr_pages;
354 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
355 efi_call_early(free_pages, addr, nr_pages);
358 static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
359 efi_char16_t *filename_16, void **handle,
362 efi_file_handle_t *h, *fh = __fh;
363 efi_file_info_t *info;
365 efi_guid_t info_guid = EFI_FILE_INFO_ID;
366 unsigned long info_sz;
368 status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
369 EFI_FILE_MODE_READ, (u64)0);
370 if (status != EFI_SUCCESS) {
371 efi_printk(sys_table_arg, "Failed to open file: ");
372 efi_char16_printk(sys_table_arg, filename_16);
373 efi_printk(sys_table_arg, "\n");
380 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
382 if (status != EFI_BUFFER_TOO_SMALL) {
383 efi_printk(sys_table_arg, "Failed to get file info size\n");
388 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
389 info_sz, (void **)&info);
390 if (status != EFI_SUCCESS) {
391 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
395 status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
397 if (status == EFI_BUFFER_TOO_SMALL) {
398 efi_call_early(free_pool, info);
402 *file_sz = info->file_size;
403 efi_call_early(free_pool, info);
405 if (status != EFI_SUCCESS)
406 efi_printk(sys_table_arg, "Failed to get initrd info\n");
411 static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
413 return efi_call_proto(efi_file_handle, read, handle, size, addr);
416 static efi_status_t efi_file_close(void *handle)
418 return efi_call_proto(efi_file_handle, close, handle);
421 static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
422 efi_loaded_image_t *image,
423 efi_file_handle_t **__fh)
425 efi_file_io_interface_t *io;
426 efi_file_handle_t *fh;
427 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
429 void *handle = (void *)(unsigned long)efi_table_attr(efi_loaded_image,
433 status = efi_call_early(handle_protocol, handle,
434 &fs_proto, (void **)&io);
435 if (status != EFI_SUCCESS) {
436 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
440 status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
441 if (status != EFI_SUCCESS)
442 efi_printk(sys_table_arg, "Failed to open volume\n");
450 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
451 * option, e.g. efi=nochunk.
453 * It should be noted that efi= is parsed in two very different
454 * environments, first in the early boot environment of the EFI boot
455 * stub, and subsequently during the kernel boot.
457 efi_status_t efi_parse_options(char const *cmdline)
461 str = strstr(cmdline, "nokaslr");
462 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
465 str = strstr(cmdline, "quiet");
466 if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
470 * If no EFI parameters were specified on the cmdline we've got
473 str = strstr(cmdline, "efi=");
477 /* Skip ahead to first argument */
478 str += strlen("efi=");
481 * Remember, because efi= is also used by the kernel we need to
482 * skip over arguments we don't understand.
484 while (*str && *str != ' ') {
485 if (!strncmp(str, "nochunk", 7)) {
486 str += strlen("nochunk");
490 if (!strncmp(str, "novamap", 7)) {
491 str += strlen("novamap");
495 /* Group words together, delimited by "," */
496 while (*str && *str != ' ' && *str != ',')
507 * Check the cmdline for a LILO-style file= arguments.
509 * We only support loading a file from the same filesystem as
512 efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
513 efi_loaded_image_t *image,
514 char *cmd_line, char *option_string,
515 unsigned long max_addr,
516 unsigned long *load_addr,
517 unsigned long *load_size)
519 struct file_info *files;
520 unsigned long file_addr;
522 efi_file_handle_t *fh = NULL;
533 j = 0; /* See close_handles */
535 if (!load_addr || !load_size)
536 return EFI_INVALID_PARAMETER;
544 for (nr_files = 0; *str; nr_files++) {
545 str = strstr(str, option_string);
549 str += strlen(option_string);
551 /* Skip any leading slashes */
552 while (*str == '/' || *str == '\\')
555 while (*str && *str != ' ' && *str != '\n')
562 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
563 nr_files * sizeof(*files), (void **)&files);
564 if (status != EFI_SUCCESS) {
565 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
570 for (i = 0; i < nr_files; i++) {
571 struct file_info *file;
572 efi_char16_t filename_16[256];
575 str = strstr(str, option_string);
579 str += strlen(option_string);
584 /* Skip any leading slashes */
585 while (*str == '/' || *str == '\\')
588 while (*str && *str != ' ' && *str != '\n') {
589 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
602 /* Only open the volume once. */
604 status = efi_open_volume(sys_table_arg, image, &fh);
605 if (status != EFI_SUCCESS)
609 status = efi_file_size(sys_table_arg, fh, filename_16,
610 (void **)&file->handle, &file->size);
611 if (status != EFI_SUCCESS)
614 file_size_total += file->size;
617 if (file_size_total) {
621 * Multiple files need to be at consecutive addresses in memory,
622 * so allocate enough memory for all the files. This is used
623 * for loading multiple files.
625 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
626 &file_addr, max_addr);
627 if (status != EFI_SUCCESS) {
628 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
632 /* We've run out of free low memory. */
633 if (file_addr > max_addr) {
634 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
635 status = EFI_INVALID_PARAMETER;
636 goto free_file_total;
640 for (j = 0; j < nr_files; j++) {
643 size = files[j].size;
645 unsigned long chunksize;
647 if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
648 chunksize = __chunk_size;
652 status = efi_file_read(files[j].handle,
655 if (status != EFI_SUCCESS) {
656 pr_efi_err(sys_table_arg, "Failed to read file\n");
657 goto free_file_total;
663 efi_file_close(files[j].handle);
668 efi_call_early(free_pool, files);
670 *load_addr = file_addr;
671 *load_size = file_size_total;
676 efi_free(sys_table_arg, file_size_total, file_addr);
679 for (k = j; k < i; k++)
680 efi_file_close(files[k].handle);
682 efi_call_early(free_pool, files);
690 * Relocate a kernel image, either compressed or uncompressed.
691 * In the ARM64 case, all kernel images are currently
692 * uncompressed, and as such when we relocate it we need to
693 * allocate additional space for the BSS segment. Any low
694 * memory that this function should avoid needs to be
695 * unavailable in the EFI memory map, as if the preferred
696 * address is not available the lowest available address will
699 efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
700 unsigned long *image_addr,
701 unsigned long image_size,
702 unsigned long alloc_size,
703 unsigned long preferred_addr,
704 unsigned long alignment)
706 unsigned long cur_image_addr;
707 unsigned long new_addr = 0;
709 unsigned long nr_pages;
710 efi_physical_addr_t efi_addr = preferred_addr;
712 if (!image_addr || !image_size || !alloc_size)
713 return EFI_INVALID_PARAMETER;
714 if (alloc_size < image_size)
715 return EFI_INVALID_PARAMETER;
717 cur_image_addr = *image_addr;
720 * The EFI firmware loader could have placed the kernel image
721 * anywhere in memory, but the kernel has restrictions on the
722 * max physical address it can run at. Some architectures
723 * also have a prefered address, so first try to relocate
724 * to the preferred address. If that fails, allocate as low
725 * as possible while respecting the required alignment.
727 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
728 status = efi_call_early(allocate_pages,
729 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
730 nr_pages, &efi_addr);
733 * If preferred address allocation failed allocate as low as
736 if (status != EFI_SUCCESS) {
737 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
740 if (status != EFI_SUCCESS) {
741 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
746 * We know source/dest won't overlap since both memory ranges
747 * have been allocated by UEFI, so we can safely use memcpy.
749 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
751 /* Return the new address of the relocated image. */
752 *image_addr = new_addr;
758 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
759 * This overestimates for surrogates, but that is okay.
761 static int efi_utf8_bytes(u16 c)
763 return 1 + (c >= 0x80) + (c >= 0x800);
767 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
769 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
775 if (n && c >= 0xd800 && c <= 0xdbff &&
776 *src >= 0xdc00 && *src <= 0xdfff) {
777 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
781 if (c >= 0xd800 && c <= 0xdfff)
782 c = 0xfffd; /* Unmatched surrogate */
788 *dst++ = 0xc0 + (c >> 6);
792 *dst++ = 0xe0 + (c >> 12);
795 *dst++ = 0xf0 + (c >> 18);
796 *dst++ = 0x80 + ((c >> 12) & 0x3f);
798 *dst++ = 0x80 + ((c >> 6) & 0x3f);
800 *dst++ = 0x80 + (c & 0x3f);
806 #ifndef MAX_CMDLINE_ADDRESS
807 #define MAX_CMDLINE_ADDRESS ULONG_MAX
811 * Convert the unicode UEFI command line to ASCII to pass to kernel.
812 * Size of memory allocated return in *cmd_line_len.
813 * Returns NULL on error.
815 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
816 efi_loaded_image_t *image,
821 unsigned long cmdline_addr = 0;
822 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
823 const u16 *options = image->load_options;
824 int options_bytes = 0; /* UTF-8 bytes */
825 int options_chars = 0; /* UTF-16 chars */
831 while (*s2 && *s2 != '\n'
832 && options_chars < load_options_chars) {
833 options_bytes += efi_utf8_bytes(*s2++);
838 if (!options_chars) {
839 /* No command line options, so return empty string*/
843 options_bytes++; /* NUL termination */
845 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
846 &cmdline_addr, MAX_CMDLINE_ADDRESS);
847 if (status != EFI_SUCCESS)
850 s1 = (u8 *)cmdline_addr;
851 s2 = (const u16 *)options;
853 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
856 *cmd_line_len = options_bytes;
857 return (char *)cmdline_addr;
861 * Handle calling ExitBootServices according to the requirements set out by the
862 * spec. Obtains the current memory map, and returns that info after calling
863 * ExitBootServices. The client must specify a function to perform any
864 * processing of the memory map data prior to ExitBootServices. A client
865 * specific structure may be passed to the function via priv. The client
866 * function may be called multiple times.
868 efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
870 struct efi_boot_memmap *map,
872 efi_exit_boot_map_processing priv_func)
876 status = efi_get_memory_map(sys_table_arg, map);
878 if (status != EFI_SUCCESS)
881 status = priv_func(sys_table_arg, map, priv);
882 if (status != EFI_SUCCESS)
885 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
887 if (status == EFI_INVALID_PARAMETER) {
889 * The memory map changed between efi_get_memory_map() and
890 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
891 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
892 * updated map, and try again. The spec implies one retry
893 * should be sufficent, which is confirmed against the EDK2
894 * implementation. Per the spec, we can only invoke
895 * get_memory_map() and exit_boot_services() - we cannot alloc
896 * so efi_get_memory_map() cannot be used, and we must reuse
897 * the buffer. For all practical purposes, the headroom in the
898 * buffer should account for any changes in the map so the call
899 * to get_memory_map() is expected to succeed here.
901 *map->map_size = *map->buff_size;
902 status = efi_call_early(get_memory_map,
909 /* exit_boot_services() was called, thus cannot free */
910 if (status != EFI_SUCCESS)
913 status = priv_func(sys_table_arg, map, priv);
914 /* exit_boot_services() was called, thus cannot free */
915 if (status != EFI_SUCCESS)
918 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
921 /* exit_boot_services() was called, thus cannot free */
922 if (status != EFI_SUCCESS)
928 efi_call_early(free_pool, *map->map);