dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / firmware / efi / libstub / efi-stub-helper.c
blob9f34c72429397de01e65a4f4fd081775b05b883b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
8 */
10 #include <linux/efi.h>
11 #include <asm/efi.h>
13 #include "efistub.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)
26 return efi_nochunk;
28 bool __pure nokaslr(void)
30 return efi_nokaslr;
32 bool __pure noinitrd(void)
34 return efi_noinitrd;
36 bool __pure is_quiet(void)
38 return efi_quiet;
40 bool __pure novamap(void)
42 return efi_novamap;
44 bool __pure __efi_soft_reserve_enabled(void)
46 return !efi_nosoftreserve;
49 void efi_printk(char *str)
51 char *s8;
53 for (s8 = str; *s8; s8++) {
54 efi_char16_t ch[2] = { 0 };
56 ch[0] = *s8;
57 if (*s8 == '\n') {
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;
77 efi_status_t status;
78 char *str, *buf;
80 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
81 if (status != EFI_SUCCESS)
82 return status;
84 str = skip_spaces(memcpy(buf, cmdline, len));
86 while (*str) {
87 char *param, *val;
89 str = next_arg(str, &param, &val);
91 if (!strcmp(param, "nokaslr")) {
92 efi_nokaslr = true;
93 } else if (!strcmp(param, "quiet")) {
94 efi_quiet = true;
95 } else if (!strcmp(param, "noinitrd")) {
96 efi_noinitrd = true;
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);
111 return EFI_SUCCESS;
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)
128 unsigned int c;
130 while (n--) {
131 c = *src++;
132 if (n && c >= 0xd800 && c <= 0xdbff &&
133 *src >= 0xdc00 && *src <= 0xdfff) {
134 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
135 src++;
136 n--;
138 if (c >= 0xd800 && c <= 0xdfff)
139 c = 0xfffd; /* Unmatched surrogate */
140 if (c < 0x80) {
141 *dst++ = c;
142 continue;
144 if (c < 0x800) {
145 *dst++ = 0xc0 + (c >> 6);
146 goto t1;
148 if (c < 0x10000) {
149 *dst++ = 0xe0 + (c >> 12);
150 goto t2;
152 *dst++ = 0xf0 + (c >> 18);
153 *dst++ = 0x80 + ((c >> 12) & 0x3f);
155 *dst++ = 0x80 + ((c >> 6) & 0x3f);
157 *dst++ = 0x80 + (c & 0x3f);
160 return dst;
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)
171 const u16 *s2;
172 u8 *s1 = NULL;
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 */
178 efi_status_t status;
179 u16 zero = 0;
181 if (options) {
182 s2 = options;
183 while (*s2 && *s2 != '\n'
184 && options_chars < load_options_chars) {
185 options_bytes += efi_utf8_bytes(*s2++);
186 options_chars++;
190 if (!options_chars) {
191 /* No command line options, so return empty string*/
192 options = &zero;
195 options_bytes++; /* NUL termination */
197 status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
198 if (status != EFI_SUCCESS)
199 return NULL;
201 s1 = (u8 *)cmdline_addr;
202 s2 = (const u16 *)options;
204 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
205 *s1 = '\0';
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,
221 void *priv,
222 efi_exit_boot_map_processing priv_func)
224 efi_status_t status;
226 status = efi_get_memory_map(map);
228 if (status != EFI_SUCCESS)
229 goto fail;
231 status = priv_func(map, priv);
232 if (status != EFI_SUCCESS)
233 goto free_map;
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,
256 map->map_size,
257 *map->map,
258 map->key_ptr,
259 map->desc_size,
260 map->desc_ver);
262 /* exit_boot_services() was called, thus cannot free */
263 if (status != EFI_SUCCESS)
264 goto fail;
266 status = priv_func(map, priv);
267 /* exit_boot_services() was called, thus cannot free */
268 if (status != EFI_SUCCESS)
269 goto fail;
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)
276 goto fail;
278 return EFI_SUCCESS;
280 free_map:
281 efi_bs_call(free_pool, *map->map);
282 fail:
283 return status;
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);
290 int i;
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);
301 return NULL;
304 void efi_char16_printk(efi_char16_t *str)
306 efi_call_proto(efi_table_attr(efi_system_table(), con_out),
307 output_string, str);
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
318 * via EFI protocols.
320 static const struct {
321 struct efi_vendor_dev_path vendor;
322 struct efi_generic_dev_path end;
323 } __packed initrd_dev_path = {
326 EFI_DEV_MEDIA,
327 EFI_DEV_MEDIA_VENDOR,
328 sizeof(struct efi_vendor_dev_path),
330 LINUX_EFI_INITRD_MEDIA_GUID
331 }, {
332 EFI_DEV_END_PATH,
333 EFI_DEV_END_ENTIRE,
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
346 * device path
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,
353 unsigned long max)
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;
360 efi_handle_t handle;
361 efi_status_t status;
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)
369 return status;
371 status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
372 (void **)&lf2);
373 if (status != EFI_SUCCESS)
374 return status;
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)
382 return status;
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;
393 return EFI_SUCCESS;