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.
6  *
7  * Copyright 2011 Intel Corporation; author Matt Fleming
8  */
9 
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 
13 #include "efistub.h"
14 
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);
22 
23 bool __pure nochunk(void)
24 {
25 	return efi_nochunk;
26 }
27 bool __pure nokaslr(void)
28 {
29 	return efi_nokaslr;
30 }
31 bool __pure is_quiet(void)
32 {
33 	return efi_quiet;
34 }
35 bool __pure novamap(void)
36 {
37 	return efi_novamap;
38 }
39 bool __pure __efi_soft_reserve_enabled(void)
40 {
41 	return !efi_nosoftreserve;
42 }
43 
44 void efi_printk(char *str)
45 {
46 	char *s8;
47 
48 	for (s8 = str; *s8; s8++) {
49 		efi_char16_t ch[2] = { 0 };
50 
51 		ch[0] = *s8;
52 		if (*s8 == '\n') {
53 			efi_char16_t nl[2] = { '\r', 0 };
54 			efi_char16_printk(nl);
55 		}
56 
57 		efi_char16_printk(ch);
58 	}
59 }
60 
61 /*
62  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
63  * option, e.g. efi=nochunk.
64  *
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.
68  */
69 efi_status_t efi_parse_options(char const *cmdline)
70 {
71 	size_t len = strlen(cmdline) + 1;
72 	efi_status_t status;
73 	char *str, *buf;
74 
75 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
76 	if (status != EFI_SUCCESS)
77 		return status;
78 
79 	str = skip_spaces(memcpy(buf, cmdline, len));
80 
81 	while (*str) {
82 		char *param, *val;
83 
84 		str = next_arg(str, &param, &val);
85 
86 		if (!strcmp(param, "nokaslr")) {
87 			efi_nokaslr = true;
88 		} else if (!strcmp(param, "quiet")) {
89 			efi_quiet = true;
90 		} else if (!strcmp(param, "efi") && val) {
91 			efi_nochunk = parse_option_str(val, "nochunk");
92 			efi_novamap = parse_option_str(val, "novamap");
93 
94 			efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
95 					    parse_option_str(val, "nosoftreserve");
96 
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;
101 		}
102 	}
103 	efi_bs_call(free_pool, buf);
104 	return EFI_SUCCESS;
105 }
106 
107 /*
108  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
109  * This overestimates for surrogates, but that is okay.
110  */
111 static int efi_utf8_bytes(u16 c)
112 {
113 	return 1 + (c >= 0x80) + (c >= 0x800);
114 }
115 
116 /*
117  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
118  */
119 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
120 {
121 	unsigned int c;
122 
123 	while (n--) {
124 		c = *src++;
125 		if (n && c >= 0xd800 && c <= 0xdbff &&
126 		    *src >= 0xdc00 && *src <= 0xdfff) {
127 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
128 			src++;
129 			n--;
130 		}
131 		if (c >= 0xd800 && c <= 0xdfff)
132 			c = 0xfffd; /* Unmatched surrogate */
133 		if (c < 0x80) {
134 			*dst++ = c;
135 			continue;
136 		}
137 		if (c < 0x800) {
138 			*dst++ = 0xc0 + (c >> 6);
139 			goto t1;
140 		}
141 		if (c < 0x10000) {
142 			*dst++ = 0xe0 + (c >> 12);
143 			goto t2;
144 		}
145 		*dst++ = 0xf0 + (c >> 18);
146 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
147 	t2:
148 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
149 	t1:
150 		*dst++ = 0x80 + (c & 0x3f);
151 	}
152 
153 	return dst;
154 }
155 
156 /*
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.
160  */
161 char *efi_convert_cmdline(efi_loaded_image_t *image,
162 			  int *cmd_line_len, unsigned long max_addr)
163 {
164 	const u16 *s2;
165 	u8 *s1 = NULL;
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 */
171 	efi_status_t status;
172 	u16 zero = 0;
173 
174 	if (options) {
175 		s2 = options;
176 		while (*s2 && *s2 != '\n'
177 		       && options_chars < load_options_chars) {
178 			options_bytes += efi_utf8_bytes(*s2++);
179 			options_chars++;
180 		}
181 	}
182 
183 	if (!options_chars) {
184 		/* No command line options, so return empty string*/
185 		options = &zero;
186 	}
187 
188 	options_bytes++;	/* NUL termination */
189 
190 	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
191 	if (status != EFI_SUCCESS)
192 		return NULL;
193 
194 	s1 = (u8 *)cmdline_addr;
195 	s2 = (const u16 *)options;
196 
197 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
198 	*s1 = '\0';
199 
200 	*cmd_line_len = options_bytes;
201 	return (char *)cmdline_addr;
202 }
203 
204 /*
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.
211  */
212 efi_status_t efi_exit_boot_services(void *handle,
213 				    struct efi_boot_memmap *map,
214 				    void *priv,
215 				    efi_exit_boot_map_processing priv_func)
216 {
217 	efi_status_t status;
218 
219 	status = efi_get_memory_map(map);
220 
221 	if (status != EFI_SUCCESS)
222 		goto fail;
223 
224 	status = priv_func(map, priv);
225 	if (status != EFI_SUCCESS)
226 		goto free_map;
227 
228 	if (efi_disable_pci_dma)
229 		efi_pci_disable_bridge_busmaster();
230 
231 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
232 
233 	if (status == EFI_INVALID_PARAMETER) {
234 		/*
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.
246 		 */
247 		*map->map_size = *map->buff_size;
248 		status = efi_bs_call(get_memory_map,
249 				     map->map_size,
250 				     *map->map,
251 				     map->key_ptr,
252 				     map->desc_size,
253 				     map->desc_ver);
254 
255 		/* exit_boot_services() was called, thus cannot free */
256 		if (status != EFI_SUCCESS)
257 			goto fail;
258 
259 		status = priv_func(map, priv);
260 		/* exit_boot_services() was called, thus cannot free */
261 		if (status != EFI_SUCCESS)
262 			goto fail;
263 
264 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
265 	}
266 
267 	/* exit_boot_services() was called, thus cannot free */
268 	if (status != EFI_SUCCESS)
269 		goto fail;
270 
271 	return EFI_SUCCESS;
272 
273 free_map:
274 	efi_bs_call(free_pool, *map->map);
275 fail:
276 	return status;
277 }
278 
279 void *get_efi_config_table(efi_guid_t guid)
280 {
281 	unsigned long tables = efi_table_attr(efi_system_table(), tables);
282 	int nr_tables = efi_table_attr(efi_system_table(), nr_tables);
283 	int i;
284 
285 	for (i = 0; i < nr_tables; i++) {
286 		efi_config_table_t *t = (void *)tables;
287 
288 		if (efi_guidcmp(t->guid, guid) == 0)
289 			return efi_table_attr(t, table);
290 
291 		tables += efi_is_native() ? sizeof(efi_config_table_t)
292 					  : sizeof(efi_config_table_32_t);
293 	}
294 	return NULL;
295 }
296 
297 void efi_char16_printk(efi_char16_t *str)
298 {
299 	efi_call_proto(efi_table_attr(efi_system_table(), con_out),
300 		       output_string, str);
301 }
302