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 /*
16  * Some firmware implementations have problems reading files in one go.
17  * A read chunk size of 1MB seems to work for most platforms.
18  *
19  * Unfortunately, reading files in chunks triggers *other* bugs on some
20  * platforms, so we provide a way to disable this workaround, which can
21  * be done by passing "efi=nochunk" on the EFI boot stub command line.
22  *
23  * If you experience issues with initrd images being corrupt it's worth
24  * trying efi=nochunk, but chunking is enabled by default because there
25  * are far more machines that require the workaround than those that
26  * break with it enabled.
27  */
28 #define EFI_READ_CHUNK_SIZE	(1024 * 1024)
29 
30 static unsigned long efi_chunk_size = EFI_READ_CHUNK_SIZE;
31 
32 static bool __efistub_global efi_nokaslr;
33 static bool __efistub_global efi_quiet;
34 static bool __efistub_global efi_novamap;
35 static bool __efistub_global efi_nosoftreserve;
36 static bool __efistub_global efi_disable_pci_dma =
37 					IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
38 
39 bool __pure nokaslr(void)
40 {
41 	return efi_nokaslr;
42 }
43 bool __pure is_quiet(void)
44 {
45 	return efi_quiet;
46 }
47 bool __pure novamap(void)
48 {
49 	return efi_novamap;
50 }
51 bool __pure __efi_soft_reserve_enabled(void)
52 {
53 	return !efi_nosoftreserve;
54 }
55 
56 #define EFI_MMAP_NR_SLACK_SLOTS	8
57 
58 struct file_info {
59 	efi_file_handle_t *handle;
60 	u64 size;
61 };
62 
63 void efi_printk(char *str)
64 {
65 	char *s8;
66 
67 	for (s8 = str; *s8; s8++) {
68 		efi_char16_t ch[2] = { 0 };
69 
70 		ch[0] = *s8;
71 		if (*s8 == '\n') {
72 			efi_char16_t nl[2] = { '\r', 0 };
73 			efi_char16_printk(nl);
74 		}
75 
76 		efi_char16_printk(ch);
77 	}
78 }
79 
80 static inline bool mmap_has_headroom(unsigned long buff_size,
81 				     unsigned long map_size,
82 				     unsigned long desc_size)
83 {
84 	unsigned long slack = buff_size - map_size;
85 
86 	return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
87 }
88 
89 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
90 {
91 	efi_memory_desc_t *m = NULL;
92 	efi_status_t status;
93 	unsigned long key;
94 	u32 desc_version;
95 
96 	*map->desc_size =	sizeof(*m);
97 	*map->map_size =	*map->desc_size * 32;
98 	*map->buff_size =	*map->map_size;
99 again:
100 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
101 			     *map->map_size, (void **)&m);
102 	if (status != EFI_SUCCESS)
103 		goto fail;
104 
105 	*map->desc_size = 0;
106 	key = 0;
107 	status = efi_bs_call(get_memory_map, map->map_size, m,
108 			     &key, map->desc_size, &desc_version);
109 	if (status == EFI_BUFFER_TOO_SMALL ||
110 	    !mmap_has_headroom(*map->buff_size, *map->map_size,
111 			       *map->desc_size)) {
112 		efi_bs_call(free_pool, m);
113 		/*
114 		 * Make sure there is some entries of headroom so that the
115 		 * buffer can be reused for a new map after allocations are
116 		 * no longer permitted.  Its unlikely that the map will grow to
117 		 * exceed this headroom once we are ready to trigger
118 		 * ExitBootServices()
119 		 */
120 		*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
121 		*map->buff_size = *map->map_size;
122 		goto again;
123 	}
124 
125 	if (status != EFI_SUCCESS)
126 		efi_bs_call(free_pool, m);
127 
128 	if (map->key_ptr && status == EFI_SUCCESS)
129 		*map->key_ptr = key;
130 	if (map->desc_ver && status == EFI_SUCCESS)
131 		*map->desc_ver = desc_version;
132 
133 fail:
134 	*map->map = m;
135 	return status;
136 }
137 
138 
139 unsigned long get_dram_base(void)
140 {
141 	efi_status_t status;
142 	unsigned long map_size, buff_size;
143 	unsigned long membase  = EFI_ERROR;
144 	struct efi_memory_map map;
145 	efi_memory_desc_t *md;
146 	struct efi_boot_memmap boot_map;
147 
148 	boot_map.map =		(efi_memory_desc_t **)&map.map;
149 	boot_map.map_size =	&map_size;
150 	boot_map.desc_size =	&map.desc_size;
151 	boot_map.desc_ver =	NULL;
152 	boot_map.key_ptr =	NULL;
153 	boot_map.buff_size =	&buff_size;
154 
155 	status = efi_get_memory_map(&boot_map);
156 	if (status != EFI_SUCCESS)
157 		return membase;
158 
159 	map.map_end = map.map + map_size;
160 
161 	for_each_efi_memory_desc_in_map(&map, md) {
162 		if (md->attribute & EFI_MEMORY_WB) {
163 			if (membase > md->phys_addr)
164 				membase = md->phys_addr;
165 		}
166 	}
167 
168 	efi_bs_call(free_pool, map.map);
169 
170 	return membase;
171 }
172 
173 /*
174  * Allocate at the highest possible address that is not above 'max'.
175  */
176 efi_status_t efi_high_alloc(unsigned long size, unsigned long align,
177 			    unsigned long *addr, unsigned long max)
178 {
179 	unsigned long map_size, desc_size, buff_size;
180 	efi_memory_desc_t *map;
181 	efi_status_t status;
182 	unsigned long nr_pages;
183 	u64 max_addr = 0;
184 	int i;
185 	struct efi_boot_memmap boot_map;
186 
187 	boot_map.map =		&map;
188 	boot_map.map_size =	&map_size;
189 	boot_map.desc_size =	&desc_size;
190 	boot_map.desc_ver =	NULL;
191 	boot_map.key_ptr =	NULL;
192 	boot_map.buff_size =	&buff_size;
193 
194 	status = efi_get_memory_map(&boot_map);
195 	if (status != EFI_SUCCESS)
196 		goto fail;
197 
198 	/*
199 	 * Enforce minimum alignment that EFI or Linux requires when
200 	 * requesting a specific address.  We are doing page-based (or
201 	 * larger) allocations, and both the address and size must meet
202 	 * alignment constraints.
203 	 */
204 	if (align < EFI_ALLOC_ALIGN)
205 		align = EFI_ALLOC_ALIGN;
206 
207 	size = round_up(size, EFI_ALLOC_ALIGN);
208 	nr_pages = size / EFI_PAGE_SIZE;
209 again:
210 	for (i = 0; i < map_size / desc_size; i++) {
211 		efi_memory_desc_t *desc;
212 		unsigned long m = (unsigned long)map;
213 		u64 start, end;
214 
215 		desc = efi_early_memdesc_ptr(m, desc_size, i);
216 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
217 			continue;
218 
219 		if (efi_soft_reserve_enabled() &&
220 		    (desc->attribute & EFI_MEMORY_SP))
221 			continue;
222 
223 		if (desc->num_pages < nr_pages)
224 			continue;
225 
226 		start = desc->phys_addr;
227 		end = start + desc->num_pages * EFI_PAGE_SIZE;
228 
229 		if (end > max)
230 			end = max;
231 
232 		if ((start + size) > end)
233 			continue;
234 
235 		if (round_down(end - size, align) < start)
236 			continue;
237 
238 		start = round_down(end - size, align);
239 
240 		/*
241 		 * Don't allocate at 0x0. It will confuse code that
242 		 * checks pointers against NULL.
243 		 */
244 		if (start == 0x0)
245 			continue;
246 
247 		if (start > max_addr)
248 			max_addr = start;
249 	}
250 
251 	if (!max_addr)
252 		status = EFI_NOT_FOUND;
253 	else {
254 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
255 				     EFI_LOADER_DATA, nr_pages, &max_addr);
256 		if (status != EFI_SUCCESS) {
257 			max = max_addr;
258 			max_addr = 0;
259 			goto again;
260 		}
261 
262 		*addr = max_addr;
263 	}
264 
265 	efi_bs_call(free_pool, map);
266 fail:
267 	return status;
268 }
269 
270 /*
271  * Allocate at the lowest possible address that is not below 'min'.
272  */
273 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
274 				 unsigned long *addr, unsigned long min)
275 {
276 	unsigned long map_size, desc_size, buff_size;
277 	efi_memory_desc_t *map;
278 	efi_status_t status;
279 	unsigned long nr_pages;
280 	int i;
281 	struct efi_boot_memmap boot_map;
282 
283 	boot_map.map =		&map;
284 	boot_map.map_size =	&map_size;
285 	boot_map.desc_size =	&desc_size;
286 	boot_map.desc_ver =	NULL;
287 	boot_map.key_ptr =	NULL;
288 	boot_map.buff_size =	&buff_size;
289 
290 	status = efi_get_memory_map(&boot_map);
291 	if (status != EFI_SUCCESS)
292 		goto fail;
293 
294 	/*
295 	 * Enforce minimum alignment that EFI or Linux requires when
296 	 * requesting a specific address.  We are doing page-based (or
297 	 * larger) allocations, and both the address and size must meet
298 	 * alignment constraints.
299 	 */
300 	if (align < EFI_ALLOC_ALIGN)
301 		align = EFI_ALLOC_ALIGN;
302 
303 	size = round_up(size, EFI_ALLOC_ALIGN);
304 	nr_pages = size / EFI_PAGE_SIZE;
305 	for (i = 0; i < map_size / desc_size; i++) {
306 		efi_memory_desc_t *desc;
307 		unsigned long m = (unsigned long)map;
308 		u64 start, end;
309 
310 		desc = efi_early_memdesc_ptr(m, desc_size, i);
311 
312 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
313 			continue;
314 
315 		if (efi_soft_reserve_enabled() &&
316 		    (desc->attribute & EFI_MEMORY_SP))
317 			continue;
318 
319 		if (desc->num_pages < nr_pages)
320 			continue;
321 
322 		start = desc->phys_addr;
323 		end = start + desc->num_pages * EFI_PAGE_SIZE;
324 
325 		if (start < min)
326 			start = min;
327 
328 		start = round_up(start, align);
329 		if ((start + size) > end)
330 			continue;
331 
332 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
333 				     EFI_LOADER_DATA, nr_pages, &start);
334 		if (status == EFI_SUCCESS) {
335 			*addr = start;
336 			break;
337 		}
338 	}
339 
340 	if (i == map_size / desc_size)
341 		status = EFI_NOT_FOUND;
342 
343 	efi_bs_call(free_pool, map);
344 fail:
345 	return status;
346 }
347 
348 void efi_free(unsigned long size, unsigned long addr)
349 {
350 	unsigned long nr_pages;
351 
352 	if (!size)
353 		return;
354 
355 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
356 	efi_bs_call(free_pages, addr, nr_pages);
357 }
358 
359 static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16,
360 				  void **handle, u64 *file_sz)
361 {
362 	efi_file_handle_t *h, *fh = __fh;
363 	efi_file_info_t *info;
364 	efi_status_t status;
365 	efi_guid_t info_guid = EFI_FILE_INFO_ID;
366 	unsigned long info_sz;
367 
368 	status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, 0);
369 	if (status != EFI_SUCCESS) {
370 		efi_printk("Failed to open file: ");
371 		efi_char16_printk(filename_16);
372 		efi_printk("\n");
373 		return status;
374 	}
375 
376 	*handle = h;
377 
378 	info_sz = 0;
379 	status = h->get_info(h, &info_guid, &info_sz, NULL);
380 	if (status != EFI_BUFFER_TOO_SMALL) {
381 		efi_printk("Failed to get file info size\n");
382 		return status;
383 	}
384 
385 grow:
386 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, info_sz,
387 			     (void **)&info);
388 	if (status != EFI_SUCCESS) {
389 		efi_printk("Failed to alloc mem for file info\n");
390 		return status;
391 	}
392 
393 	status = h->get_info(h, &info_guid, &info_sz, info);
394 	if (status == EFI_BUFFER_TOO_SMALL) {
395 		efi_bs_call(free_pool, info);
396 		goto grow;
397 	}
398 
399 	*file_sz = info->file_size;
400 	efi_bs_call(free_pool, info);
401 
402 	if (status != EFI_SUCCESS)
403 		efi_printk("Failed to get initrd info\n");
404 
405 	return status;
406 }
407 
408 static efi_status_t efi_file_read(efi_file_handle_t *handle,
409 				  unsigned long *size, void *addr)
410 {
411 	return handle->read(handle, size, addr);
412 }
413 
414 static efi_status_t efi_file_close(efi_file_handle_t *handle)
415 {
416 	return handle->close(handle);
417 }
418 
419 static efi_status_t efi_open_volume(efi_loaded_image_t *image,
420 				    efi_file_handle_t **__fh)
421 {
422 	efi_file_io_interface_t *io;
423 	efi_file_handle_t *fh;
424 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
425 	efi_status_t status;
426 	efi_handle_t handle = image->device_handle;
427 
428 	status = efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io);
429 	if (status != EFI_SUCCESS) {
430 		efi_printk("Failed to handle fs_proto\n");
431 		return status;
432 	}
433 
434 	status = io->open_volume(io, &fh);
435 	if (status != EFI_SUCCESS)
436 		efi_printk("Failed to open volume\n");
437 	else
438 		*__fh = fh;
439 
440 	return status;
441 }
442 
443 /*
444  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
445  * option, e.g. efi=nochunk.
446  *
447  * It should be noted that efi= is parsed in two very different
448  * environments, first in the early boot environment of the EFI boot
449  * stub, and subsequently during the kernel boot.
450  */
451 efi_status_t efi_parse_options(char const *cmdline)
452 {
453 	char *str;
454 
455 	str = strstr(cmdline, "nokaslr");
456 	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
457 		efi_nokaslr = true;
458 
459 	str = strstr(cmdline, "quiet");
460 	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
461 		efi_quiet = true;
462 
463 	/*
464 	 * If no EFI parameters were specified on the cmdline we've got
465 	 * nothing to do.
466 	 */
467 	str = strstr(cmdline, "efi=");
468 	if (!str)
469 		return EFI_SUCCESS;
470 
471 	/* Skip ahead to first argument */
472 	str += strlen("efi=");
473 
474 	/*
475 	 * Remember, because efi= is also used by the kernel we need to
476 	 * skip over arguments we don't understand.
477 	 */
478 	while (*str && *str != ' ') {
479 		if (!strncmp(str, "nochunk", 7)) {
480 			str += strlen("nochunk");
481 			efi_chunk_size = -1UL;
482 		}
483 
484 		if (!strncmp(str, "novamap", 7)) {
485 			str += strlen("novamap");
486 			efi_novamap = true;
487 		}
488 
489 		if (IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
490 		    !strncmp(str, "nosoftreserve", 7)) {
491 			str += strlen("nosoftreserve");
492 			efi_nosoftreserve = true;
493 		}
494 
495 		if (!strncmp(str, "disable_early_pci_dma", 21)) {
496 			str += strlen("disable_early_pci_dma");
497 			efi_disable_pci_dma = true;
498 		}
499 
500 		if (!strncmp(str, "no_disable_early_pci_dma", 24)) {
501 			str += strlen("no_disable_early_pci_dma");
502 			efi_disable_pci_dma = false;
503 		}
504 
505 		/* Group words together, delimited by "," */
506 		while (*str && *str != ' ' && *str != ',')
507 			str++;
508 
509 		if (*str == ',')
510 			str++;
511 	}
512 
513 	return EFI_SUCCESS;
514 }
515 
516 /*
517  * Check the cmdline for a LILO-style file= arguments.
518  *
519  * We only support loading a file from the same filesystem as
520  * the kernel image.
521  */
522 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
523 				  char *cmd_line, char *option_string,
524 				  unsigned long max_addr,
525 				  unsigned long *load_addr,
526 				  unsigned long *load_size)
527 {
528 	struct file_info *files;
529 	unsigned long file_addr;
530 	u64 file_size_total;
531 	efi_file_handle_t *fh = NULL;
532 	efi_status_t status;
533 	int nr_files;
534 	char *str;
535 	int i, j, k;
536 
537 	file_addr = 0;
538 	file_size_total = 0;
539 
540 	str = cmd_line;
541 
542 	j = 0;			/* See close_handles */
543 
544 	if (!load_addr || !load_size)
545 		return EFI_INVALID_PARAMETER;
546 
547 	*load_addr = 0;
548 	*load_size = 0;
549 
550 	if (!str || !*str)
551 		return EFI_SUCCESS;
552 
553 	for (nr_files = 0; *str; nr_files++) {
554 		str = strstr(str, option_string);
555 		if (!str)
556 			break;
557 
558 		str += strlen(option_string);
559 
560 		/* Skip any leading slashes */
561 		while (*str == '/' || *str == '\\')
562 			str++;
563 
564 		while (*str && *str != ' ' && *str != '\n')
565 			str++;
566 	}
567 
568 	if (!nr_files)
569 		return EFI_SUCCESS;
570 
571 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
572 			     nr_files * sizeof(*files), (void **)&files);
573 	if (status != EFI_SUCCESS) {
574 		pr_efi_err("Failed to alloc mem for file handle list\n");
575 		goto fail;
576 	}
577 
578 	str = cmd_line;
579 	for (i = 0; i < nr_files; i++) {
580 		struct file_info *file;
581 		efi_char16_t filename_16[256];
582 		efi_char16_t *p;
583 
584 		str = strstr(str, option_string);
585 		if (!str)
586 			break;
587 
588 		str += strlen(option_string);
589 
590 		file = &files[i];
591 		p = filename_16;
592 
593 		/* Skip any leading slashes */
594 		while (*str == '/' || *str == '\\')
595 			str++;
596 
597 		while (*str && *str != ' ' && *str != '\n') {
598 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
599 				break;
600 
601 			if (*str == '/') {
602 				*p++ = '\\';
603 				str++;
604 			} else {
605 				*p++ = *str++;
606 			}
607 		}
608 
609 		*p = '\0';
610 
611 		/* Only open the volume once. */
612 		if (!i) {
613 			status = efi_open_volume(image, &fh);
614 			if (status != EFI_SUCCESS)
615 				goto free_files;
616 		}
617 
618 		status = efi_file_size(fh, filename_16, (void **)&file->handle,
619 				       &file->size);
620 		if (status != EFI_SUCCESS)
621 			goto close_handles;
622 
623 		file_size_total += file->size;
624 	}
625 
626 	if (file_size_total) {
627 		unsigned long addr;
628 
629 		/*
630 		 * Multiple files need to be at consecutive addresses in memory,
631 		 * so allocate enough memory for all the files.  This is used
632 		 * for loading multiple files.
633 		 */
634 		status = efi_high_alloc(file_size_total, 0x1000, &file_addr,
635 					max_addr);
636 		if (status != EFI_SUCCESS) {
637 			pr_efi_err("Failed to alloc highmem for files\n");
638 			goto close_handles;
639 		}
640 
641 		/* We've run out of free low memory. */
642 		if (file_addr > max_addr) {
643 			pr_efi_err("We've run out of free low memory\n");
644 			status = EFI_INVALID_PARAMETER;
645 			goto free_file_total;
646 		}
647 
648 		addr = file_addr;
649 		for (j = 0; j < nr_files; j++) {
650 			unsigned long size;
651 
652 			size = files[j].size;
653 			while (size) {
654 				unsigned long chunksize;
655 
656 				if (IS_ENABLED(CONFIG_X86) && size > efi_chunk_size)
657 					chunksize = efi_chunk_size;
658 				else
659 					chunksize = size;
660 
661 				status = efi_file_read(files[j].handle,
662 						       &chunksize,
663 						       (void *)addr);
664 				if (status != EFI_SUCCESS) {
665 					pr_efi_err("Failed to read file\n");
666 					goto free_file_total;
667 				}
668 				addr += chunksize;
669 				size -= chunksize;
670 			}
671 
672 			efi_file_close(files[j].handle);
673 		}
674 
675 	}
676 
677 	efi_bs_call(free_pool, files);
678 
679 	*load_addr = file_addr;
680 	*load_size = file_size_total;
681 
682 	return status;
683 
684 free_file_total:
685 	efi_free(file_size_total, file_addr);
686 
687 close_handles:
688 	for (k = j; k < i; k++)
689 		efi_file_close(files[k].handle);
690 free_files:
691 	efi_bs_call(free_pool, files);
692 fail:
693 	*load_addr = 0;
694 	*load_size = 0;
695 
696 	return status;
697 }
698 /*
699  * Relocate a kernel image, either compressed or uncompressed.
700  * In the ARM64 case, all kernel images are currently
701  * uncompressed, and as such when we relocate it we need to
702  * allocate additional space for the BSS segment. Any low
703  * memory that this function should avoid needs to be
704  * unavailable in the EFI memory map, as if the preferred
705  * address is not available the lowest available address will
706  * be used.
707  */
708 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
709 				 unsigned long image_size,
710 				 unsigned long alloc_size,
711 				 unsigned long preferred_addr,
712 				 unsigned long alignment,
713 				 unsigned long min_addr)
714 {
715 	unsigned long cur_image_addr;
716 	unsigned long new_addr = 0;
717 	efi_status_t status;
718 	unsigned long nr_pages;
719 	efi_physical_addr_t efi_addr = preferred_addr;
720 
721 	if (!image_addr || !image_size || !alloc_size)
722 		return EFI_INVALID_PARAMETER;
723 	if (alloc_size < image_size)
724 		return EFI_INVALID_PARAMETER;
725 
726 	cur_image_addr = *image_addr;
727 
728 	/*
729 	 * The EFI firmware loader could have placed the kernel image
730 	 * anywhere in memory, but the kernel has restrictions on the
731 	 * max physical address it can run at.  Some architectures
732 	 * also have a prefered address, so first try to relocate
733 	 * to the preferred address.  If that fails, allocate as low
734 	 * as possible while respecting the required alignment.
735 	 */
736 	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
737 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
738 			     EFI_LOADER_DATA, nr_pages, &efi_addr);
739 	new_addr = efi_addr;
740 	/*
741 	 * If preferred address allocation failed allocate as low as
742 	 * possible.
743 	 */
744 	if (status != EFI_SUCCESS) {
745 		status = efi_low_alloc_above(alloc_size, alignment, &new_addr,
746 					     min_addr);
747 	}
748 	if (status != EFI_SUCCESS) {
749 		pr_efi_err("Failed to allocate usable memory for kernel.\n");
750 		return status;
751 	}
752 
753 	/*
754 	 * We know source/dest won't overlap since both memory ranges
755 	 * have been allocated by UEFI, so we can safely use memcpy.
756 	 */
757 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
758 
759 	/* Return the new address of the relocated image. */
760 	*image_addr = new_addr;
761 
762 	return status;
763 }
764 
765 /*
766  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
767  * This overestimates for surrogates, but that is okay.
768  */
769 static int efi_utf8_bytes(u16 c)
770 {
771 	return 1 + (c >= 0x80) + (c >= 0x800);
772 }
773 
774 /*
775  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
776  */
777 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
778 {
779 	unsigned int c;
780 
781 	while (n--) {
782 		c = *src++;
783 		if (n && c >= 0xd800 && c <= 0xdbff &&
784 		    *src >= 0xdc00 && *src <= 0xdfff) {
785 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
786 			src++;
787 			n--;
788 		}
789 		if (c >= 0xd800 && c <= 0xdfff)
790 			c = 0xfffd; /* Unmatched surrogate */
791 		if (c < 0x80) {
792 			*dst++ = c;
793 			continue;
794 		}
795 		if (c < 0x800) {
796 			*dst++ = 0xc0 + (c >> 6);
797 			goto t1;
798 		}
799 		if (c < 0x10000) {
800 			*dst++ = 0xe0 + (c >> 12);
801 			goto t2;
802 		}
803 		*dst++ = 0xf0 + (c >> 18);
804 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
805 	t2:
806 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
807 	t1:
808 		*dst++ = 0x80 + (c & 0x3f);
809 	}
810 
811 	return dst;
812 }
813 
814 #ifndef MAX_CMDLINE_ADDRESS
815 #define MAX_CMDLINE_ADDRESS	ULONG_MAX
816 #endif
817 
818 /*
819  * Convert the unicode UEFI command line to ASCII to pass to kernel.
820  * Size of memory allocated return in *cmd_line_len.
821  * Returns NULL on error.
822  */
823 char *efi_convert_cmdline(efi_loaded_image_t *image,
824 			  int *cmd_line_len)
825 {
826 	const u16 *s2;
827 	u8 *s1 = NULL;
828 	unsigned long cmdline_addr = 0;
829 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
830 	const u16 *options = image->load_options;
831 	int options_bytes = 0;  /* UTF-8 bytes */
832 	int options_chars = 0;  /* UTF-16 chars */
833 	efi_status_t status;
834 	u16 zero = 0;
835 
836 	if (options) {
837 		s2 = options;
838 		while (*s2 && *s2 != '\n'
839 		       && options_chars < load_options_chars) {
840 			options_bytes += efi_utf8_bytes(*s2++);
841 			options_chars++;
842 		}
843 	}
844 
845 	if (!options_chars) {
846 		/* No command line options, so return empty string*/
847 		options = &zero;
848 	}
849 
850 	options_bytes++;	/* NUL termination */
851 
852 	status = efi_high_alloc(options_bytes, 0, &cmdline_addr,
853 				MAX_CMDLINE_ADDRESS);
854 	if (status != EFI_SUCCESS)
855 		return NULL;
856 
857 	s1 = (u8 *)cmdline_addr;
858 	s2 = (const u16 *)options;
859 
860 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
861 	*s1 = '\0';
862 
863 	*cmd_line_len = options_bytes;
864 	return (char *)cmdline_addr;
865 }
866 
867 /*
868  * Handle calling ExitBootServices according to the requirements set out by the
869  * spec.  Obtains the current memory map, and returns that info after calling
870  * ExitBootServices.  The client must specify a function to perform any
871  * processing of the memory map data prior to ExitBootServices.  A client
872  * specific structure may be passed to the function via priv.  The client
873  * function may be called multiple times.
874  */
875 efi_status_t efi_exit_boot_services(void *handle,
876 				    struct efi_boot_memmap *map,
877 				    void *priv,
878 				    efi_exit_boot_map_processing priv_func)
879 {
880 	efi_status_t status;
881 
882 	status = efi_get_memory_map(map);
883 
884 	if (status != EFI_SUCCESS)
885 		goto fail;
886 
887 	status = priv_func(map, priv);
888 	if (status != EFI_SUCCESS)
889 		goto free_map;
890 
891 	if (efi_disable_pci_dma)
892 		efi_pci_disable_bridge_busmaster();
893 
894 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
895 
896 	if (status == EFI_INVALID_PARAMETER) {
897 		/*
898 		 * The memory map changed between efi_get_memory_map() and
899 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
900 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
901 		 * updated map, and try again.  The spec implies one retry
902 		 * should be sufficent, which is confirmed against the EDK2
903 		 * implementation.  Per the spec, we can only invoke
904 		 * get_memory_map() and exit_boot_services() - we cannot alloc
905 		 * so efi_get_memory_map() cannot be used, and we must reuse
906 		 * the buffer.  For all practical purposes, the headroom in the
907 		 * buffer should account for any changes in the map so the call
908 		 * to get_memory_map() is expected to succeed here.
909 		 */
910 		*map->map_size = *map->buff_size;
911 		status = efi_bs_call(get_memory_map,
912 				     map->map_size,
913 				     *map->map,
914 				     map->key_ptr,
915 				     map->desc_size,
916 				     map->desc_ver);
917 
918 		/* exit_boot_services() was called, thus cannot free */
919 		if (status != EFI_SUCCESS)
920 			goto fail;
921 
922 		status = priv_func(map, priv);
923 		/* exit_boot_services() was called, thus cannot free */
924 		if (status != EFI_SUCCESS)
925 			goto fail;
926 
927 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
928 	}
929 
930 	/* exit_boot_services() was called, thus cannot free */
931 	if (status != EFI_SUCCESS)
932 		goto fail;
933 
934 	return EFI_SUCCESS;
935 
936 free_map:
937 	efi_bs_call(free_pool, *map->map);
938 fail:
939 	return status;
940 }
941 
942 void *get_efi_config_table(efi_guid_t guid)
943 {
944 	unsigned long tables = efi_table_attr(efi_system_table(), tables);
945 	int nr_tables = efi_table_attr(efi_system_table(), nr_tables);
946 	int i;
947 
948 	for (i = 0; i < nr_tables; i++) {
949 		efi_config_table_t *t = (void *)tables;
950 
951 		if (efi_guidcmp(t->guid, guid) == 0)
952 			return efi_table_attr(t, table);
953 
954 		tables += efi_is_native() ? sizeof(efi_config_table_t)
955 					  : sizeof(efi_config_table_32_t);
956 	}
957 	return NULL;
958 }
959 
960 void efi_char16_printk(efi_char16_t *str)
961 {
962 	efi_call_proto(efi_table_attr(efi_system_table(), con_out),
963 		       output_string, str);
964 }
965