1 // SPDX-License-Identifier: GPL-2.0
3 * Helper functions used by the EFI stub on multiple
4 * architectures. This should be #included by the EFI stub
5 * implementation files.
7 * Copyright 2011 Intel Corporation; author Matt Fleming
10 #include <linux/efi.h>
15 static bool __efistub_global efi_nochunk
;
16 static bool __efistub_global efi_nokaslr
;
17 static bool __efistub_global efi_noinitrd
;
18 static bool __efistub_global efi_quiet
;
19 static bool __efistub_global efi_novamap
;
20 static bool __efistub_global efi_nosoftreserve
;
21 static bool __efistub_global efi_disable_pci_dma
=
22 IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA
);
24 bool __pure
nochunk(void)
28 bool __pure
nokaslr(void)
32 bool __pure
noinitrd(void)
36 bool __pure
is_quiet(void)
40 bool __pure
novamap(void)
44 bool __pure
__efi_soft_reserve_enabled(void)
46 return !efi_nosoftreserve
;
49 void efi_printk(char *str
)
53 for (s8
= str
; *s8
; s8
++) {
54 efi_char16_t ch
[2] = { 0 };
58 efi_char16_t nl
[2] = { '\r', 0 };
59 efi_char16_printk(nl
);
62 efi_char16_printk(ch
);
67 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
68 * option, e.g. efi=nochunk.
70 * It should be noted that efi= is parsed in two very different
71 * environments, first in the early boot environment of the EFI boot
72 * stub, and subsequently during the kernel boot.
74 efi_status_t
efi_parse_options(char const *cmdline
)
76 size_t len
= strlen(cmdline
) + 1;
80 status
= efi_bs_call(allocate_pool
, EFI_LOADER_DATA
, len
, (void **)&buf
);
81 if (status
!= EFI_SUCCESS
)
84 str
= skip_spaces(memcpy(buf
, cmdline
, len
));
89 str
= next_arg(str
, ¶m
, &val
);
91 if (!strcmp(param
, "nokaslr")) {
93 } else if (!strcmp(param
, "quiet")) {
95 } else if (!strcmp(param
, "noinitrd")) {
97 } else if (!strcmp(param
, "efi") && val
) {
98 efi_nochunk
= parse_option_str(val
, "nochunk");
99 efi_novamap
= parse_option_str(val
, "novamap");
101 efi_nosoftreserve
= IS_ENABLED(CONFIG_EFI_SOFT_RESERVE
) &&
102 parse_option_str(val
, "nosoftreserve");
104 if (parse_option_str(val
, "disable_early_pci_dma"))
105 efi_disable_pci_dma
= true;
106 if (parse_option_str(val
, "no_disable_early_pci_dma"))
107 efi_disable_pci_dma
= false;
110 efi_bs_call(free_pool
, buf
);
115 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
116 * This overestimates for surrogates, but that is okay.
118 static int efi_utf8_bytes(u16 c
)
120 return 1 + (c
>= 0x80) + (c
>= 0x800);
124 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
126 static u8
*efi_utf16_to_utf8(u8
*dst
, const u16
*src
, int n
)
132 if (n
&& c
>= 0xd800 && c
<= 0xdbff &&
133 *src
>= 0xdc00 && *src
<= 0xdfff) {
134 c
= 0x10000 + ((c
& 0x3ff) << 10) + (*src
& 0x3ff);
138 if (c
>= 0xd800 && c
<= 0xdfff)
139 c
= 0xfffd; /* Unmatched surrogate */
145 *dst
++ = 0xc0 + (c
>> 6);
149 *dst
++ = 0xe0 + (c
>> 12);
152 *dst
++ = 0xf0 + (c
>> 18);
153 *dst
++ = 0x80 + ((c
>> 12) & 0x3f);
155 *dst
++ = 0x80 + ((c
>> 6) & 0x3f);
157 *dst
++ = 0x80 + (c
& 0x3f);
164 * Convert the unicode UEFI command line to ASCII to pass to kernel.
165 * Size of memory allocated return in *cmd_line_len.
166 * Returns NULL on error.
168 char *efi_convert_cmdline(efi_loaded_image_t
*image
,
169 int *cmd_line_len
, unsigned long max_addr
)
173 unsigned long cmdline_addr
= 0;
174 int load_options_chars
= efi_table_attr(image
, load_options_size
) / 2;
175 const u16
*options
= efi_table_attr(image
, load_options
);
176 int options_bytes
= 0; /* UTF-8 bytes */
177 int options_chars
= 0; /* UTF-16 chars */
183 while (*s2
&& *s2
!= '\n'
184 && options_chars
< load_options_chars
) {
185 options_bytes
+= efi_utf8_bytes(*s2
++);
190 if (!options_chars
) {
191 /* No command line options, so return empty string*/
195 options_bytes
++; /* NUL termination */
197 status
= efi_allocate_pages(options_bytes
, &cmdline_addr
, max_addr
);
198 if (status
!= EFI_SUCCESS
)
201 s1
= (u8
*)cmdline_addr
;
202 s2
= (const u16
*)options
;
204 s1
= efi_utf16_to_utf8(s1
, s2
, options_chars
);
207 *cmd_line_len
= options_bytes
;
208 return (char *)cmdline_addr
;
212 * Handle calling ExitBootServices according to the requirements set out by the
213 * spec. Obtains the current memory map, and returns that info after calling
214 * ExitBootServices. The client must specify a function to perform any
215 * processing of the memory map data prior to ExitBootServices. A client
216 * specific structure may be passed to the function via priv. The client
217 * function may be called multiple times.
219 efi_status_t
efi_exit_boot_services(void *handle
,
220 struct efi_boot_memmap
*map
,
222 efi_exit_boot_map_processing priv_func
)
226 status
= efi_get_memory_map(map
);
228 if (status
!= EFI_SUCCESS
)
231 status
= priv_func(map
, priv
);
232 if (status
!= EFI_SUCCESS
)
235 if (efi_disable_pci_dma
)
236 efi_pci_disable_bridge_busmaster();
238 status
= efi_bs_call(exit_boot_services
, handle
, *map
->key_ptr
);
240 if (status
== EFI_INVALID_PARAMETER
) {
242 * The memory map changed between efi_get_memory_map() and
243 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
244 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
245 * updated map, and try again. The spec implies one retry
246 * should be sufficent, which is confirmed against the EDK2
247 * implementation. Per the spec, we can only invoke
248 * get_memory_map() and exit_boot_services() - we cannot alloc
249 * so efi_get_memory_map() cannot be used, and we must reuse
250 * the buffer. For all practical purposes, the headroom in the
251 * buffer should account for any changes in the map so the call
252 * to get_memory_map() is expected to succeed here.
254 *map
->map_size
= *map
->buff_size
;
255 status
= efi_bs_call(get_memory_map
,
262 /* exit_boot_services() was called, thus cannot free */
263 if (status
!= EFI_SUCCESS
)
266 status
= priv_func(map
, priv
);
267 /* exit_boot_services() was called, thus cannot free */
268 if (status
!= EFI_SUCCESS
)
271 status
= efi_bs_call(exit_boot_services
, handle
, *map
->key_ptr
);
274 /* exit_boot_services() was called, thus cannot free */
275 if (status
!= EFI_SUCCESS
)
281 efi_bs_call(free_pool
, *map
->map
);
286 void *get_efi_config_table(efi_guid_t guid
)
288 unsigned long tables
= efi_table_attr(efi_system_table(), tables
);
289 int nr_tables
= efi_table_attr(efi_system_table(), nr_tables
);
292 for (i
= 0; i
< nr_tables
; i
++) {
293 efi_config_table_t
*t
= (void *)tables
;
295 if (efi_guidcmp(t
->guid
, guid
) == 0)
296 return efi_table_attr(t
, table
);
298 tables
+= efi_is_native() ? sizeof(efi_config_table_t
)
299 : sizeof(efi_config_table_32_t
);
304 void efi_char16_printk(efi_char16_t
*str
)
306 efi_call_proto(efi_table_attr(efi_system_table(), con_out
),
311 * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
312 * for the firmware or bootloader to expose the initrd data directly to the stub
313 * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
314 * very easy to implement. It is a simple Linux initrd specific conduit between
315 * kernel and firmware, allowing us to put the EFI stub (being part of the
316 * kernel) in charge of where and when to load the initrd, while leaving it up
317 * to the firmware to decide whether it needs to expose its filesystem hierarchy
320 static const struct {
321 struct efi_vendor_dev_path vendor
;
322 struct efi_generic_dev_path end
;
323 } __packed initrd_dev_path
= {
327 EFI_DEV_MEDIA_VENDOR
,
328 sizeof(struct efi_vendor_dev_path
),
330 LINUX_EFI_INITRD_MEDIA_GUID
334 sizeof(struct efi_generic_dev_path
)
339 * efi_load_initrd_dev_path - load the initrd from the Linux initrd device path
340 * @load_addr: pointer to store the address where the initrd was loaded
341 * @load_size: pointer to store the size of the loaded initrd
342 * @max: upper limit for the initrd memory allocation
343 * @return: %EFI_SUCCESS if the initrd was loaded successfully, in which
344 * case @load_addr and @load_size are assigned accordingly
345 * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd
347 * %EFI_INVALID_PARAMETER if load_addr == NULL or load_size == NULL
348 * %EFI_OUT_OF_RESOURCES if memory allocation failed
349 * %EFI_LOAD_ERROR in all other cases
351 efi_status_t
efi_load_initrd_dev_path(unsigned long *load_addr
,
352 unsigned long *load_size
,
355 efi_guid_t lf2_proto_guid
= EFI_LOAD_FILE2_PROTOCOL_GUID
;
356 efi_device_path_protocol_t
*dp
;
357 efi_load_file2_protocol_t
*lf2
;
358 unsigned long initrd_addr
;
359 unsigned long initrd_size
;
363 if (!load_addr
|| !load_size
)
364 return EFI_INVALID_PARAMETER
;
366 dp
= (efi_device_path_protocol_t
*)&initrd_dev_path
;
367 status
= efi_bs_call(locate_device_path
, &lf2_proto_guid
, &dp
, &handle
);
368 if (status
!= EFI_SUCCESS
)
371 status
= efi_bs_call(handle_protocol
, handle
, &lf2_proto_guid
,
373 if (status
!= EFI_SUCCESS
)
376 status
= efi_call_proto(lf2
, load_file
, dp
, false, &initrd_size
, NULL
);
377 if (status
!= EFI_BUFFER_TOO_SMALL
)
378 return EFI_LOAD_ERROR
;
380 status
= efi_allocate_pages(initrd_size
, &initrd_addr
, max
);
381 if (status
!= EFI_SUCCESS
)
384 status
= efi_call_proto(lf2
, load_file
, dp
, false, &initrd_size
,
385 (void *)initrd_addr
);
386 if (status
!= EFI_SUCCESS
) {
387 efi_free(initrd_size
, initrd_addr
);
388 return EFI_LOAD_ERROR
;
391 *load_addr
= initrd_addr
;
392 *load_size
= initrd_size
;