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_quiet;
18 static bool __efistub_global efi_novamap;
19 static bool __efistub_global efi_nosoftreserve;
20 static bool __efistub_global efi_disable_pci_dma =
21 IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
23 bool __pure nochunk(void)
27 bool __pure nokaslr(void)
31 bool __pure is_quiet(void)
35 bool __pure novamap(void)
39 bool __pure __efi_soft_reserve_enabled(void)
41 return !efi_nosoftreserve;
44 void efi_printk(char *str)
48 for (s8 = str; *s8; s8++) {
49 efi_char16_t ch[2] = { 0 };
53 efi_char16_t nl[2] = { '\r', 0 };
54 efi_char16_printk(nl);
57 efi_char16_printk(ch);
62 * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
63 * option, e.g. efi=nochunk.
65 * It should be noted that efi= is parsed in two very different
66 * environments, first in the early boot environment of the EFI boot
67 * stub, and subsequently during the kernel boot.
69 efi_status_t efi_parse_options(char const *cmdline)
71 size_t len = strlen(cmdline) + 1;
75 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
76 if (status != EFI_SUCCESS)
79 str = skip_spaces(memcpy(buf, cmdline, len));
84 str = next_arg(str, ¶m, &val);
86 if (!strcmp(param, "nokaslr")) {
88 } else if (!strcmp(param, "quiet")) {
90 } else if (!strcmp(param, "efi") && val) {
91 efi_nochunk = parse_option_str(val, "nochunk");
92 efi_novamap = parse_option_str(val, "novamap");
94 efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
95 parse_option_str(val, "nosoftreserve");
97 if (parse_option_str(val, "disable_early_pci_dma"))
98 efi_disable_pci_dma = true;
99 if (parse_option_str(val, "no_disable_early_pci_dma"))
100 efi_disable_pci_dma = false;
103 efi_bs_call(free_pool, buf);
108 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
109 * This overestimates for surrogates, but that is okay.
111 static int efi_utf8_bytes(u16 c)
113 return 1 + (c >= 0x80) + (c >= 0x800);
117 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
119 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
125 if (n && c >= 0xd800 && c <= 0xdbff &&
126 *src >= 0xdc00 && *src <= 0xdfff) {
127 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
131 if (c >= 0xd800 && c <= 0xdfff)
132 c = 0xfffd; /* Unmatched surrogate */
138 *dst++ = 0xc0 + (c >> 6);
142 *dst++ = 0xe0 + (c >> 12);
145 *dst++ = 0xf0 + (c >> 18);
146 *dst++ = 0x80 + ((c >> 12) & 0x3f);
148 *dst++ = 0x80 + ((c >> 6) & 0x3f);
150 *dst++ = 0x80 + (c & 0x3f);
157 * Convert the unicode UEFI command line to ASCII to pass to kernel.
158 * Size of memory allocated return in *cmd_line_len.
159 * Returns NULL on error.
161 char *efi_convert_cmdline(efi_loaded_image_t *image,
162 int *cmd_line_len, unsigned long max_addr)
166 unsigned long cmdline_addr = 0;
167 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
168 const u16 *options = image->load_options;
169 int options_bytes = 0; /* UTF-8 bytes */
170 int options_chars = 0; /* UTF-16 chars */
176 while (*s2 && *s2 != '\n'
177 && options_chars < load_options_chars) {
178 options_bytes += efi_utf8_bytes(*s2++);
183 if (!options_chars) {
184 /* No command line options, so return empty string*/
188 options_bytes++; /* NUL termination */
190 status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
191 if (status != EFI_SUCCESS)
194 s1 = (u8 *)cmdline_addr;
195 s2 = (const u16 *)options;
197 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
200 *cmd_line_len = options_bytes;
201 return (char *)cmdline_addr;
205 * Handle calling ExitBootServices according to the requirements set out by the
206 * spec. Obtains the current memory map, and returns that info after calling
207 * ExitBootServices. The client must specify a function to perform any
208 * processing of the memory map data prior to ExitBootServices. A client
209 * specific structure may be passed to the function via priv. The client
210 * function may be called multiple times.
212 efi_status_t efi_exit_boot_services(void *handle,
213 struct efi_boot_memmap *map,
215 efi_exit_boot_map_processing priv_func)
219 status = efi_get_memory_map(map);
221 if (status != EFI_SUCCESS)
224 status = priv_func(map, priv);
225 if (status != EFI_SUCCESS)
228 if (efi_disable_pci_dma)
229 efi_pci_disable_bridge_busmaster();
231 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
233 if (status == EFI_INVALID_PARAMETER) {
235 * The memory map changed between efi_get_memory_map() and
236 * exit_boot_services(). Per the UEFI Spec v2.6, Section 6.4:
237 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
238 * updated map, and try again. The spec implies one retry
239 * should be sufficent, which is confirmed against the EDK2
240 * implementation. Per the spec, we can only invoke
241 * get_memory_map() and exit_boot_services() - we cannot alloc
242 * so efi_get_memory_map() cannot be used, and we must reuse
243 * the buffer. For all practical purposes, the headroom in the
244 * buffer should account for any changes in the map so the call
245 * to get_memory_map() is expected to succeed here.
247 *map->map_size = *map->buff_size;
248 status = efi_bs_call(get_memory_map,
255 /* exit_boot_services() was called, thus cannot free */
256 if (status != EFI_SUCCESS)
259 status = priv_func(map, priv);
260 /* exit_boot_services() was called, thus cannot free */
261 if (status != EFI_SUCCESS)
264 status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
267 /* exit_boot_services() was called, thus cannot free */
268 if (status != EFI_SUCCESS)
274 efi_bs_call(free_pool, *map->map);
279 void *get_efi_config_table(efi_guid_t guid)
281 unsigned long tables = efi_table_attr(efi_system_table(), tables);
282 int nr_tables = efi_table_attr(efi_system_table(), nr_tables);
285 for (i = 0; i < nr_tables; i++) {
286 efi_config_table_t *t = (void *)tables;
288 if (efi_guidcmp(t->guid, guid) == 0)
289 return efi_table_attr(t, table);
291 tables += efi_is_native() ? sizeof(efi_config_table_t)
292 : sizeof(efi_config_table_32_t);
297 void efi_char16_printk(efi_char16_t *str)
299 efi_call_proto(efi_table_attr(efi_system_table(), con_out),