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
;
36 * Allow the platform to override the allocation granularity: this allows
37 * systems that have the capability to run with a larger page size to deal
38 * with the allocations for initrd and fdt more efficiently.
40 #ifndef EFI_ALLOC_ALIGN
41 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
44 #define EFI_MMAP_NR_SLACK_SLOTS 8
47 efi_file_handle_t
*handle
;
51 void efi_printk(efi_system_table_t
*sys_table_arg
, char *str
)
55 for (s8
= str
; *s8
; s8
++) {
56 efi_char16_t ch
[2] = { 0 };
60 efi_char16_t nl
[2] = { '\r', 0 };
61 efi_char16_printk(sys_table_arg
, nl
);
64 efi_char16_printk(sys_table_arg
, ch
);
68 static inline bool mmap_has_headroom(unsigned long buff_size
,
69 unsigned long map_size
,
70 unsigned long desc_size
)
72 unsigned long slack
= buff_size
- map_size
;
74 return slack
/ desc_size
>= EFI_MMAP_NR_SLACK_SLOTS
;
77 efi_status_t
efi_get_memory_map(efi_system_table_t
*sys_table_arg
,
78 struct efi_boot_memmap
*map
)
80 efi_memory_desc_t
*m
= NULL
;
85 *map
->desc_size
= sizeof(*m
);
86 *map
->map_size
= *map
->desc_size
* 32;
87 *map
->buff_size
= *map
->map_size
;
89 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
90 *map
->map_size
, (void **)&m
);
91 if (status
!= EFI_SUCCESS
)
96 status
= efi_call_early(get_memory_map
, map
->map_size
, m
,
97 &key
, map
->desc_size
, &desc_version
);
98 if (status
== EFI_BUFFER_TOO_SMALL
||
99 !mmap_has_headroom(*map
->buff_size
, *map
->map_size
,
101 efi_call_early(free_pool
, m
);
103 * Make sure there is some entries of headroom so that the
104 * buffer can be reused for a new map after allocations are
105 * no longer permitted. Its unlikely that the map will grow to
106 * exceed this headroom once we are ready to trigger
109 *map
->map_size
+= *map
->desc_size
* EFI_MMAP_NR_SLACK_SLOTS
;
110 *map
->buff_size
= *map
->map_size
;
114 if (status
!= EFI_SUCCESS
)
115 efi_call_early(free_pool
, m
);
117 if (map
->key_ptr
&& status
== EFI_SUCCESS
)
119 if (map
->desc_ver
&& status
== EFI_SUCCESS
)
120 *map
->desc_ver
= desc_version
;
128 unsigned long get_dram_base(efi_system_table_t
*sys_table_arg
)
131 unsigned long map_size
, buff_size
;
132 unsigned long membase
= EFI_ERROR
;
133 struct efi_memory_map map
;
134 efi_memory_desc_t
*md
;
135 struct efi_boot_memmap boot_map
;
137 boot_map
.map
= (efi_memory_desc_t
**)&map
.map
;
138 boot_map
.map_size
= &map_size
;
139 boot_map
.desc_size
= &map
.desc_size
;
140 boot_map
.desc_ver
= NULL
;
141 boot_map
.key_ptr
= NULL
;
142 boot_map
.buff_size
= &buff_size
;
144 status
= efi_get_memory_map(sys_table_arg
, &boot_map
);
145 if (status
!= EFI_SUCCESS
)
148 map
.map_end
= map
.map
+ map_size
;
150 for_each_efi_memory_desc_in_map(&map
, md
) {
151 if (md
->attribute
& EFI_MEMORY_WB
) {
152 if (membase
> md
->phys_addr
)
153 membase
= md
->phys_addr
;
157 efi_call_early(free_pool
, map
.map
);
163 * Allocate at the highest possible address that is not above 'max'.
165 efi_status_t
efi_high_alloc(efi_system_table_t
*sys_table_arg
,
166 unsigned long size
, unsigned long align
,
167 unsigned long *addr
, unsigned long max
)
169 unsigned long map_size
, desc_size
, buff_size
;
170 efi_memory_desc_t
*map
;
172 unsigned long nr_pages
;
175 struct efi_boot_memmap boot_map
;
178 boot_map
.map_size
= &map_size
;
179 boot_map
.desc_size
= &desc_size
;
180 boot_map
.desc_ver
= NULL
;
181 boot_map
.key_ptr
= NULL
;
182 boot_map
.buff_size
= &buff_size
;
184 status
= efi_get_memory_map(sys_table_arg
, &boot_map
);
185 if (status
!= EFI_SUCCESS
)
189 * Enforce minimum alignment that EFI requires when requesting
190 * a specific address. We are doing page-based allocations,
191 * so we must be aligned to a page.
193 if (align
< EFI_ALLOC_ALIGN
)
194 align
= EFI_ALLOC_ALIGN
;
196 nr_pages
= round_up(size
, EFI_ALLOC_ALIGN
) / EFI_PAGE_SIZE
;
198 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
199 efi_memory_desc_t
*desc
;
200 unsigned long m
= (unsigned long)map
;
203 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
204 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
207 if (desc
->num_pages
< nr_pages
)
210 start
= desc
->phys_addr
;
211 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
216 if ((start
+ size
) > end
)
219 if (round_down(end
- size
, align
) < start
)
222 start
= round_down(end
- size
, align
);
225 * Don't allocate at 0x0. It will confuse code that
226 * checks pointers against NULL.
231 if (start
> max_addr
)
236 status
= EFI_NOT_FOUND
;
238 status
= efi_call_early(allocate_pages
,
239 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
240 nr_pages
, &max_addr
);
241 if (status
!= EFI_SUCCESS
) {
250 efi_call_early(free_pool
, map
);
256 * Allocate at the lowest possible address.
258 efi_status_t
efi_low_alloc(efi_system_table_t
*sys_table_arg
,
259 unsigned long size
, unsigned long align
,
262 unsigned long map_size
, desc_size
, buff_size
;
263 efi_memory_desc_t
*map
;
265 unsigned long nr_pages
;
267 struct efi_boot_memmap boot_map
;
270 boot_map
.map_size
= &map_size
;
271 boot_map
.desc_size
= &desc_size
;
272 boot_map
.desc_ver
= NULL
;
273 boot_map
.key_ptr
= NULL
;
274 boot_map
.buff_size
= &buff_size
;
276 status
= efi_get_memory_map(sys_table_arg
, &boot_map
);
277 if (status
!= EFI_SUCCESS
)
281 * Enforce minimum alignment that EFI requires when requesting
282 * a specific address. We are doing page-based allocations,
283 * so we must be aligned to a page.
285 if (align
< EFI_ALLOC_ALIGN
)
286 align
= EFI_ALLOC_ALIGN
;
288 nr_pages
= round_up(size
, EFI_ALLOC_ALIGN
) / EFI_PAGE_SIZE
;
289 for (i
= 0; i
< map_size
/ desc_size
; i
++) {
290 efi_memory_desc_t
*desc
;
291 unsigned long m
= (unsigned long)map
;
294 desc
= (efi_memory_desc_t
*)(m
+ (i
* desc_size
));
296 if (desc
->type
!= EFI_CONVENTIONAL_MEMORY
)
299 if (desc
->num_pages
< nr_pages
)
302 start
= desc
->phys_addr
;
303 end
= start
+ desc
->num_pages
* (1UL << EFI_PAGE_SHIFT
);
306 * Don't allocate at 0x0. It will confuse code that
307 * checks pointers against NULL. Skip the first 8
308 * bytes so we start at a nice even number.
313 start
= round_up(start
, align
);
314 if ((start
+ size
) > end
)
317 status
= efi_call_early(allocate_pages
,
318 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
320 if (status
== EFI_SUCCESS
) {
326 if (i
== map_size
/ desc_size
)
327 status
= EFI_NOT_FOUND
;
329 efi_call_early(free_pool
, map
);
334 void efi_free(efi_system_table_t
*sys_table_arg
, unsigned long size
,
337 unsigned long nr_pages
;
342 nr_pages
= round_up(size
, EFI_ALLOC_ALIGN
) / EFI_PAGE_SIZE
;
343 efi_call_early(free_pages
, addr
, nr_pages
);
347 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
348 * option, e.g. efi=nochunk.
350 * It should be noted that efi= is parsed in two very different
351 * environments, first in the early boot environment of the EFI boot
352 * stub, and subsequently during the kernel boot.
354 efi_status_t
efi_parse_options(char *cmdline
)
359 * If no EFI parameters were specified on the cmdline we've got
362 str
= strstr(cmdline
, "efi=");
366 /* Skip ahead to first argument */
367 str
+= strlen("efi=");
370 * Remember, because efi= is also used by the kernel we need to
371 * skip over arguments we don't understand.
374 if (!strncmp(str
, "nochunk", 7)) {
375 str
+= strlen("nochunk");
379 /* Group words together, delimited by "," */
380 while (*str
&& *str
!= ',')
391 * Check the cmdline for a LILO-style file= arguments.
393 * We only support loading a file from the same filesystem as
396 efi_status_t
handle_cmdline_files(efi_system_table_t
*sys_table_arg
,
397 efi_loaded_image_t
*image
,
398 char *cmd_line
, char *option_string
,
399 unsigned long max_addr
,
400 unsigned long *load_addr
,
401 unsigned long *load_size
)
403 struct file_info
*files
;
404 unsigned long file_addr
;
406 efi_file_handle_t
*fh
= NULL
;
417 j
= 0; /* See close_handles */
419 if (!load_addr
|| !load_size
)
420 return EFI_INVALID_PARAMETER
;
428 for (nr_files
= 0; *str
; nr_files
++) {
429 str
= strstr(str
, option_string
);
433 str
+= strlen(option_string
);
435 /* Skip any leading slashes */
436 while (*str
== '/' || *str
== '\\')
439 while (*str
&& *str
!= ' ' && *str
!= '\n')
446 status
= efi_call_early(allocate_pool
, EFI_LOADER_DATA
,
447 nr_files
* sizeof(*files
), (void **)&files
);
448 if (status
!= EFI_SUCCESS
) {
449 pr_efi_err(sys_table_arg
, "Failed to alloc mem for file handle list\n");
454 for (i
= 0; i
< nr_files
; i
++) {
455 struct file_info
*file
;
456 efi_char16_t filename_16
[256];
459 str
= strstr(str
, option_string
);
463 str
+= strlen(option_string
);
468 /* Skip any leading slashes */
469 while (*str
== '/' || *str
== '\\')
472 while (*str
&& *str
!= ' ' && *str
!= '\n') {
473 if ((u8
*)p
>= (u8
*)filename_16
+ sizeof(filename_16
))
486 /* Only open the volume once. */
488 status
= efi_open_volume(sys_table_arg
, image
,
490 if (status
!= EFI_SUCCESS
)
494 status
= efi_file_size(sys_table_arg
, fh
, filename_16
,
495 (void **)&file
->handle
, &file
->size
);
496 if (status
!= EFI_SUCCESS
)
499 file_size_total
+= file
->size
;
502 if (file_size_total
) {
506 * Multiple files need to be at consecutive addresses in memory,
507 * so allocate enough memory for all the files. This is used
508 * for loading multiple files.
510 status
= efi_high_alloc(sys_table_arg
, file_size_total
, 0x1000,
511 &file_addr
, max_addr
);
512 if (status
!= EFI_SUCCESS
) {
513 pr_efi_err(sys_table_arg
, "Failed to alloc highmem for files\n");
517 /* We've run out of free low memory. */
518 if (file_addr
> max_addr
) {
519 pr_efi_err(sys_table_arg
, "We've run out of free low memory\n");
520 status
= EFI_INVALID_PARAMETER
;
521 goto free_file_total
;
525 for (j
= 0; j
< nr_files
; j
++) {
528 size
= files
[j
].size
;
530 unsigned long chunksize
;
531 if (size
> __chunk_size
)
532 chunksize
= __chunk_size
;
536 status
= efi_file_read(files
[j
].handle
,
539 if (status
!= EFI_SUCCESS
) {
540 pr_efi_err(sys_table_arg
, "Failed to read file\n");
541 goto free_file_total
;
547 efi_file_close(files
[j
].handle
);
552 efi_call_early(free_pool
, files
);
554 *load_addr
= file_addr
;
555 *load_size
= file_size_total
;
560 efi_free(sys_table_arg
, file_size_total
, file_addr
);
563 for (k
= j
; k
< i
; k
++)
564 efi_file_close(files
[k
].handle
);
566 efi_call_early(free_pool
, files
);
574 * Relocate a kernel image, either compressed or uncompressed.
575 * In the ARM64 case, all kernel images are currently
576 * uncompressed, and as such when we relocate it we need to
577 * allocate additional space for the BSS segment. Any low
578 * memory that this function should avoid needs to be
579 * unavailable in the EFI memory map, as if the preferred
580 * address is not available the lowest available address will
583 efi_status_t
efi_relocate_kernel(efi_system_table_t
*sys_table_arg
,
584 unsigned long *image_addr
,
585 unsigned long image_size
,
586 unsigned long alloc_size
,
587 unsigned long preferred_addr
,
588 unsigned long alignment
)
590 unsigned long cur_image_addr
;
591 unsigned long new_addr
= 0;
593 unsigned long nr_pages
;
594 efi_physical_addr_t efi_addr
= preferred_addr
;
596 if (!image_addr
|| !image_size
|| !alloc_size
)
597 return EFI_INVALID_PARAMETER
;
598 if (alloc_size
< image_size
)
599 return EFI_INVALID_PARAMETER
;
601 cur_image_addr
= *image_addr
;
604 * The EFI firmware loader could have placed the kernel image
605 * anywhere in memory, but the kernel has restrictions on the
606 * max physical address it can run at. Some architectures
607 * also have a prefered address, so first try to relocate
608 * to the preferred address. If that fails, allocate as low
609 * as possible while respecting the required alignment.
611 nr_pages
= round_up(alloc_size
, EFI_ALLOC_ALIGN
) / EFI_PAGE_SIZE
;
612 status
= efi_call_early(allocate_pages
,
613 EFI_ALLOCATE_ADDRESS
, EFI_LOADER_DATA
,
614 nr_pages
, &efi_addr
);
617 * If preferred address allocation failed allocate as low as
620 if (status
!= EFI_SUCCESS
) {
621 status
= efi_low_alloc(sys_table_arg
, alloc_size
, alignment
,
624 if (status
!= EFI_SUCCESS
) {
625 pr_efi_err(sys_table_arg
, "Failed to allocate usable memory for kernel.\n");
630 * We know source/dest won't overlap since both memory ranges
631 * have been allocated by UEFI, so we can safely use memcpy.
633 memcpy((void *)new_addr
, (void *)cur_image_addr
, image_size
);
635 /* Return the new address of the relocated image. */
636 *image_addr
= new_addr
;
642 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
643 * This overestimates for surrogates, but that is okay.
645 static int efi_utf8_bytes(u16 c
)
647 return 1 + (c
>= 0x80) + (c
>= 0x800);
651 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
653 static u8
*efi_utf16_to_utf8(u8
*dst
, const u16
*src
, int n
)
659 if (n
&& c
>= 0xd800 && c
<= 0xdbff &&
660 *src
>= 0xdc00 && *src
<= 0xdfff) {
661 c
= 0x10000 + ((c
& 0x3ff) << 10) + (*src
& 0x3ff);
665 if (c
>= 0xd800 && c
<= 0xdfff)
666 c
= 0xfffd; /* Unmatched surrogate */
672 *dst
++ = 0xc0 + (c
>> 6);
676 *dst
++ = 0xe0 + (c
>> 12);
679 *dst
++ = 0xf0 + (c
>> 18);
680 *dst
++ = 0x80 + ((c
>> 12) & 0x3f);
682 *dst
++ = 0x80 + ((c
>> 6) & 0x3f);
684 *dst
++ = 0x80 + (c
& 0x3f);
690 #ifndef MAX_CMDLINE_ADDRESS
691 #define MAX_CMDLINE_ADDRESS ULONG_MAX
695 * Convert the unicode UEFI command line to ASCII to pass to kernel.
696 * Size of memory allocated return in *cmd_line_len.
697 * Returns NULL on error.
699 char *efi_convert_cmdline(efi_system_table_t
*sys_table_arg
,
700 efi_loaded_image_t
*image
,
705 unsigned long cmdline_addr
= 0;
706 int load_options_chars
= image
->load_options_size
/ 2; /* UTF-16 */
707 const u16
*options
= image
->load_options
;
708 int options_bytes
= 0; /* UTF-8 bytes */
709 int options_chars
= 0; /* UTF-16 chars */
715 while (*s2
&& *s2
!= '\n'
716 && options_chars
< load_options_chars
) {
717 options_bytes
+= efi_utf8_bytes(*s2
++);
722 if (!options_chars
) {
723 /* No command line options, so return empty string*/
727 options_bytes
++; /* NUL termination */
729 status
= efi_high_alloc(sys_table_arg
, options_bytes
, 0,
730 &cmdline_addr
, MAX_CMDLINE_ADDRESS
);
731 if (status
!= EFI_SUCCESS
)
734 s1
= (u8
*)cmdline_addr
;
735 s2
= (const u16
*)options
;
737 s1
= efi_utf16_to_utf8(s1
, s2
, options_chars
);
740 *cmd_line_len
= options_bytes
;
741 return (char *)cmdline_addr
;
745 * Handle calling ExitBootServices according to the requirements set out by the
746 * spec. Obtains the current memory map, and returns that info after calling
747 * ExitBootServices. The client must specify a function to perform any
748 * processing of the memory map data prior to ExitBootServices. A client
749 * specific structure may be passed to the function via priv. The client
750 * function may be called multiple times.
752 efi_status_t
efi_exit_boot_services(efi_system_table_t
*sys_table_arg
,
754 struct efi_boot_memmap
*map
,
756 efi_exit_boot_map_processing priv_func
)
760 status
= efi_get_memory_map(sys_table_arg
, map
);
762 if (status
!= EFI_SUCCESS
)
765 status
= priv_func(sys_table_arg
, map
, priv
);
766 if (status
!= EFI_SUCCESS
)
769 status
= efi_call_early(exit_boot_services
, handle
, *map
->key_ptr
);
771 if (status
== EFI_INVALID_PARAMETER
) {
773 * The memory map changed between efi_get_memory_map() and
774 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
775 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
776 * updated map, and try again. The spec implies one retry
777 * should be sufficent, which is confirmed against the EDK2
778 * implementation. Per the spec, we can only invoke
779 * get_memory_map() and exit_boot_services() - we cannot alloc
780 * so efi_get_memory_map() cannot be used, and we must reuse
781 * the buffer. For all practical purposes, the headroom in the
782 * buffer should account for any changes in the map so the call
783 * to get_memory_map() is expected to succeed here.
785 *map
->map_size
= *map
->buff_size
;
786 status
= efi_call_early(get_memory_map
,
793 /* exit_boot_services() was called, thus cannot free */
794 if (status
!= EFI_SUCCESS
)
797 status
= priv_func(sys_table_arg
, map
, priv
);
798 /* exit_boot_services() was called, thus cannot free */
799 if (status
!= EFI_SUCCESS
)
802 status
= efi_call_early(exit_boot_services
, handle
, *map
->key_ptr
);
805 /* exit_boot_services() was called, thus cannot free */
806 if (status
!= EFI_SUCCESS
)
812 efi_call_early(free_pool
, *map
->map
);