14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2f4f75ad5SArd Biesheuvel /*
3f4f75ad5SArd Biesheuvel  * Helper functions used by the EFI stub on multiple
4f4f75ad5SArd Biesheuvel  * architectures. This should be #included by the EFI stub
5f4f75ad5SArd Biesheuvel  * implementation files.
6f4f75ad5SArd Biesheuvel  *
7f4f75ad5SArd Biesheuvel  * Copyright 2011 Intel Corporation; author Matt Fleming
8f4f75ad5SArd Biesheuvel  */
9f4f75ad5SArd Biesheuvel 
10f4f75ad5SArd Biesheuvel #include <linux/efi.h>
11f4f75ad5SArd Biesheuvel #include <asm/efi.h>
12f4f75ad5SArd Biesheuvel 
13f4f75ad5SArd Biesheuvel #include "efistub.h"
14f4f75ad5SArd Biesheuvel 
155a17dae4SMatt Fleming /*
165a17dae4SMatt Fleming  * Some firmware implementations have problems reading files in one go.
175a17dae4SMatt Fleming  * A read chunk size of 1MB seems to work for most platforms.
185a17dae4SMatt Fleming  *
195a17dae4SMatt Fleming  * Unfortunately, reading files in chunks triggers *other* bugs on some
205a17dae4SMatt Fleming  * platforms, so we provide a way to disable this workaround, which can
215a17dae4SMatt Fleming  * be done by passing "efi=nochunk" on the EFI boot stub command line.
225a17dae4SMatt Fleming  *
235a17dae4SMatt Fleming  * If you experience issues with initrd images being corrupt it's worth
245a17dae4SMatt Fleming  * trying efi=nochunk, but chunking is enabled by default because there
255a17dae4SMatt Fleming  * are far more machines that require the workaround than those that
265a17dae4SMatt Fleming  * break with it enabled.
275a17dae4SMatt Fleming  */
28f4f75ad5SArd Biesheuvel #define EFI_READ_CHUNK_SIZE	(1024 * 1024)
29f4f75ad5SArd Biesheuvel 
307d4e323dSArd Biesheuvel static unsigned long efi_chunk_size = EFI_READ_CHUNK_SIZE;
315a17dae4SMatt Fleming 
327d4e323dSArd Biesheuvel static bool __efistub_global efi_nokaslr;
337d4e323dSArd Biesheuvel static bool __efistub_global efi_quiet;
347d4e323dSArd Biesheuvel static bool __efistub_global efi_novamap;
357d4e323dSArd Biesheuvel static bool __efistub_global efi_nosoftreserve;
3660f38de7SArd Biesheuvel 
377d4e323dSArd Biesheuvel bool __pure nokaslr(void)
3860f38de7SArd Biesheuvel {
397d4e323dSArd Biesheuvel 	return efi_nokaslr;
4060f38de7SArd Biesheuvel }
417d4e323dSArd Biesheuvel bool __pure is_quiet(void)
42eeff7d63SArd Biesheuvel {
437d4e323dSArd Biesheuvel 	return efi_quiet;
44eeff7d63SArd Biesheuvel }
457d4e323dSArd Biesheuvel bool __pure novamap(void)
464e46c2a9SArd Biesheuvel {
477d4e323dSArd Biesheuvel 	return efi_novamap;
484e46c2a9SArd Biesheuvel }
49b617c526SDan Williams bool __pure __efi_soft_reserve_enabled(void)
50b617c526SDan Williams {
51b617c526SDan Williams 	return !efi_nosoftreserve;
52b617c526SDan Williams }
5360f38de7SArd Biesheuvel 
54dadb57abSJeffrey Hugo #define EFI_MMAP_NR_SLACK_SLOTS	8
55dadb57abSJeffrey Hugo 
56f4f75ad5SArd Biesheuvel struct file_info {
57f4f75ad5SArd Biesheuvel 	efi_file_handle_t *handle;
58f4f75ad5SArd Biesheuvel 	u64 size;
59f4f75ad5SArd Biesheuvel };
60f4f75ad5SArd Biesheuvel 
618173ec79SArd Biesheuvel void efi_printk(char *str)
62f4f75ad5SArd Biesheuvel {
63f4f75ad5SArd Biesheuvel 	char *s8;
64f4f75ad5SArd Biesheuvel 
65f4f75ad5SArd Biesheuvel 	for (s8 = str; *s8; s8++) {
66f4f75ad5SArd Biesheuvel 		efi_char16_t ch[2] = { 0 };
67f4f75ad5SArd Biesheuvel 
68f4f75ad5SArd Biesheuvel 		ch[0] = *s8;
69f4f75ad5SArd Biesheuvel 		if (*s8 == '\n') {
70f4f75ad5SArd Biesheuvel 			efi_char16_t nl[2] = { '\r', 0 };
718173ec79SArd Biesheuvel 			efi_char16_printk(nl);
72f4f75ad5SArd Biesheuvel 		}
73f4f75ad5SArd Biesheuvel 
748173ec79SArd Biesheuvel 		efi_char16_printk(ch);
75f4f75ad5SArd Biesheuvel 	}
76f4f75ad5SArd Biesheuvel }
77f4f75ad5SArd Biesheuvel 
78dadb57abSJeffrey Hugo static inline bool mmap_has_headroom(unsigned long buff_size,
79dadb57abSJeffrey Hugo 				     unsigned long map_size,
80dadb57abSJeffrey Hugo 				     unsigned long desc_size)
81dadb57abSJeffrey Hugo {
82dadb57abSJeffrey Hugo 	unsigned long slack = buff_size - map_size;
83dadb57abSJeffrey Hugo 
84dadb57abSJeffrey Hugo 	return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
85dadb57abSJeffrey Hugo }
86dadb57abSJeffrey Hugo 
87cd33a5c1SArd Biesheuvel efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
88f4f75ad5SArd Biesheuvel {
89f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *m = NULL;
90f4f75ad5SArd Biesheuvel 	efi_status_t status;
91f4f75ad5SArd Biesheuvel 	unsigned long key;
92f4f75ad5SArd Biesheuvel 	u32 desc_version;
93f4f75ad5SArd Biesheuvel 
94dadb57abSJeffrey Hugo 	*map->desc_size =	sizeof(*m);
95dadb57abSJeffrey Hugo 	*map->map_size =	*map->desc_size * 32;
96dadb57abSJeffrey Hugo 	*map->buff_size =	*map->map_size;
9743a9f696SMatt Fleming again:
98966291f6SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
99dadb57abSJeffrey Hugo 			     *map->map_size, (void **)&m);
100f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
101f4f75ad5SArd Biesheuvel 		goto fail;
102f4f75ad5SArd Biesheuvel 
103dadb57abSJeffrey Hugo 	*map->desc_size = 0;
10443a9f696SMatt Fleming 	key = 0;
105966291f6SArd Biesheuvel 	status = efi_bs_call(get_memory_map, map->map_size, m,
106dadb57abSJeffrey Hugo 			     &key, map->desc_size, &desc_version);
107dadb57abSJeffrey Hugo 	if (status == EFI_BUFFER_TOO_SMALL ||
108dadb57abSJeffrey Hugo 	    !mmap_has_headroom(*map->buff_size, *map->map_size,
109dadb57abSJeffrey Hugo 			       *map->desc_size)) {
110966291f6SArd Biesheuvel 		efi_bs_call(free_pool, m);
111dadb57abSJeffrey Hugo 		/*
112dadb57abSJeffrey Hugo 		 * Make sure there is some entries of headroom so that the
113dadb57abSJeffrey Hugo 		 * buffer can be reused for a new map after allocations are
114dadb57abSJeffrey Hugo 		 * no longer permitted.  Its unlikely that the map will grow to
115dadb57abSJeffrey Hugo 		 * exceed this headroom once we are ready to trigger
116dadb57abSJeffrey Hugo 		 * ExitBootServices()
117dadb57abSJeffrey Hugo 		 */
118dadb57abSJeffrey Hugo 		*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
119dadb57abSJeffrey Hugo 		*map->buff_size = *map->map_size;
12043a9f696SMatt Fleming 		goto again;
121f4f75ad5SArd Biesheuvel 	}
122f4f75ad5SArd Biesheuvel 
123f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
124966291f6SArd Biesheuvel 		efi_bs_call(free_pool, m);
125f4f75ad5SArd Biesheuvel 
126dadb57abSJeffrey Hugo 	if (map->key_ptr && status == EFI_SUCCESS)
127dadb57abSJeffrey Hugo 		*map->key_ptr = key;
128dadb57abSJeffrey Hugo 	if (map->desc_ver && status == EFI_SUCCESS)
129dadb57abSJeffrey Hugo 		*map->desc_ver = desc_version;
130f4f75ad5SArd Biesheuvel 
131f4f75ad5SArd Biesheuvel fail:
132dadb57abSJeffrey Hugo 	*map->map = m;
133f4f75ad5SArd Biesheuvel 	return status;
134f4f75ad5SArd Biesheuvel }
135f4f75ad5SArd Biesheuvel 
136f4f75ad5SArd Biesheuvel 
137cd33a5c1SArd Biesheuvel unsigned long get_dram_base(void)
138f4f75ad5SArd Biesheuvel {
139f4f75ad5SArd Biesheuvel 	efi_status_t status;
140dadb57abSJeffrey Hugo 	unsigned long map_size, buff_size;
141f4f75ad5SArd Biesheuvel 	unsigned long membase  = EFI_ERROR;
142f4f75ad5SArd Biesheuvel 	struct efi_memory_map map;
143f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *md;
144dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
145f4f75ad5SArd Biesheuvel 
146dadb57abSJeffrey Hugo 	boot_map.map =		(efi_memory_desc_t **)&map.map;
147dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
148dadb57abSJeffrey Hugo 	boot_map.desc_size =	&map.desc_size;
149dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
150dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
151dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
152dadb57abSJeffrey Hugo 
153cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(&boot_map);
154f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
155f4f75ad5SArd Biesheuvel 		return membase;
156f4f75ad5SArd Biesheuvel 
157f4f75ad5SArd Biesheuvel 	map.map_end = map.map + map_size;
158f4f75ad5SArd Biesheuvel 
15978ce248fSMatt Fleming 	for_each_efi_memory_desc_in_map(&map, md) {
16078ce248fSMatt Fleming 		if (md->attribute & EFI_MEMORY_WB) {
161f4f75ad5SArd Biesheuvel 			if (membase > md->phys_addr)
162f4f75ad5SArd Biesheuvel 				membase = md->phys_addr;
16378ce248fSMatt Fleming 		}
16478ce248fSMatt Fleming 	}
165f4f75ad5SArd Biesheuvel 
166966291f6SArd Biesheuvel 	efi_bs_call(free_pool, map.map);
167f4f75ad5SArd Biesheuvel 
168f4f75ad5SArd Biesheuvel 	return membase;
169f4f75ad5SArd Biesheuvel }
170f4f75ad5SArd Biesheuvel 
171f4f75ad5SArd Biesheuvel /*
172f4f75ad5SArd Biesheuvel  * Allocate at the highest possible address that is not above 'max'.
173f4f75ad5SArd Biesheuvel  */
174cd33a5c1SArd Biesheuvel efi_status_t efi_high_alloc(unsigned long size, unsigned long align,
175f4f75ad5SArd Biesheuvel 			    unsigned long *addr, unsigned long max)
176f4f75ad5SArd Biesheuvel {
177dadb57abSJeffrey Hugo 	unsigned long map_size, desc_size, buff_size;
178f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
179f4f75ad5SArd Biesheuvel 	efi_status_t status;
180f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
181f4f75ad5SArd Biesheuvel 	u64 max_addr = 0;
182f4f75ad5SArd Biesheuvel 	int i;
183dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
184f4f75ad5SArd Biesheuvel 
185dadb57abSJeffrey Hugo 	boot_map.map =		&map;
186dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
187dadb57abSJeffrey Hugo 	boot_map.desc_size =	&desc_size;
188dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
189dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
190dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
191dadb57abSJeffrey Hugo 
192cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(&boot_map);
193f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
194f4f75ad5SArd Biesheuvel 		goto fail;
195f4f75ad5SArd Biesheuvel 
196f4f75ad5SArd Biesheuvel 	/*
1975b88a31cSRoy Franz 	 * Enforce minimum alignment that EFI or Linux requires when
1985b88a31cSRoy Franz 	 * requesting a specific address.  We are doing page-based (or
1995b88a31cSRoy Franz 	 * larger) allocations, and both the address and size must meet
2005b88a31cSRoy Franz 	 * alignment constraints.
201f4f75ad5SArd Biesheuvel 	 */
202cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
203cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
204f4f75ad5SArd Biesheuvel 
2055b88a31cSRoy Franz 	size = round_up(size, EFI_ALLOC_ALIGN);
2065b88a31cSRoy Franz 	nr_pages = size / EFI_PAGE_SIZE;
207f4f75ad5SArd Biesheuvel again:
208f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
209f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
210f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
211f4f75ad5SArd Biesheuvel 		u64 start, end;
212f4f75ad5SArd Biesheuvel 
21302e43c2dSBaoquan He 		desc = efi_early_memdesc_ptr(m, desc_size, i);
214f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
215f4f75ad5SArd Biesheuvel 			continue;
216f4f75ad5SArd Biesheuvel 
217b617c526SDan Williams 		if (efi_soft_reserve_enabled() &&
218b617c526SDan Williams 		    (desc->attribute & EFI_MEMORY_SP))
219b617c526SDan Williams 			continue;
220b617c526SDan Williams 
221f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
222f4f75ad5SArd Biesheuvel 			continue;
223f4f75ad5SArd Biesheuvel 
224f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
2255b88a31cSRoy Franz 		end = start + desc->num_pages * EFI_PAGE_SIZE;
226f4f75ad5SArd Biesheuvel 
2277ed620bbSYinghai Lu 		if (end > max)
228f4f75ad5SArd Biesheuvel 			end = max;
229f4f75ad5SArd Biesheuvel 
2307ed620bbSYinghai Lu 		if ((start + size) > end)
2317ed620bbSYinghai Lu 			continue;
2327ed620bbSYinghai Lu 
233f4f75ad5SArd Biesheuvel 		if (round_down(end - size, align) < start)
234f4f75ad5SArd Biesheuvel 			continue;
235f4f75ad5SArd Biesheuvel 
236f4f75ad5SArd Biesheuvel 		start = round_down(end - size, align);
237f4f75ad5SArd Biesheuvel 
238f4f75ad5SArd Biesheuvel 		/*
239f4f75ad5SArd Biesheuvel 		 * Don't allocate at 0x0. It will confuse code that
240f4f75ad5SArd Biesheuvel 		 * checks pointers against NULL.
241f4f75ad5SArd Biesheuvel 		 */
242f4f75ad5SArd Biesheuvel 		if (start == 0x0)
243f4f75ad5SArd Biesheuvel 			continue;
244f4f75ad5SArd Biesheuvel 
245f4f75ad5SArd Biesheuvel 		if (start > max_addr)
246f4f75ad5SArd Biesheuvel 			max_addr = start;
247f4f75ad5SArd Biesheuvel 	}
248f4f75ad5SArd Biesheuvel 
249f4f75ad5SArd Biesheuvel 	if (!max_addr)
250f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
251f4f75ad5SArd Biesheuvel 	else {
252966291f6SArd Biesheuvel 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
253966291f6SArd Biesheuvel 				     EFI_LOADER_DATA, nr_pages, &max_addr);
254f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
255f4f75ad5SArd Biesheuvel 			max = max_addr;
256f4f75ad5SArd Biesheuvel 			max_addr = 0;
257f4f75ad5SArd Biesheuvel 			goto again;
258f4f75ad5SArd Biesheuvel 		}
259f4f75ad5SArd Biesheuvel 
260f4f75ad5SArd Biesheuvel 		*addr = max_addr;
261f4f75ad5SArd Biesheuvel 	}
262f4f75ad5SArd Biesheuvel 
263966291f6SArd Biesheuvel 	efi_bs_call(free_pool, map);
264f4f75ad5SArd Biesheuvel fail:
265f4f75ad5SArd Biesheuvel 	return status;
266f4f75ad5SArd Biesheuvel }
267f4f75ad5SArd Biesheuvel 
268f4f75ad5SArd Biesheuvel /*
269220dd769SKairui Song  * Allocate at the lowest possible address that is not below 'min'.
270f4f75ad5SArd Biesheuvel  */
271cd33a5c1SArd Biesheuvel efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
272220dd769SKairui Song 				 unsigned long *addr, unsigned long min)
273f4f75ad5SArd Biesheuvel {
274dadb57abSJeffrey Hugo 	unsigned long map_size, desc_size, buff_size;
275f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
276f4f75ad5SArd Biesheuvel 	efi_status_t status;
277f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
278f4f75ad5SArd Biesheuvel 	int i;
279dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
280f4f75ad5SArd Biesheuvel 
281dadb57abSJeffrey Hugo 	boot_map.map =		&map;
282dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
283dadb57abSJeffrey Hugo 	boot_map.desc_size =	&desc_size;
284dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
285dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
286dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
287dadb57abSJeffrey Hugo 
288cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(&boot_map);
289f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
290f4f75ad5SArd Biesheuvel 		goto fail;
291f4f75ad5SArd Biesheuvel 
292f4f75ad5SArd Biesheuvel 	/*
2935b88a31cSRoy Franz 	 * Enforce minimum alignment that EFI or Linux requires when
2945b88a31cSRoy Franz 	 * requesting a specific address.  We are doing page-based (or
2955b88a31cSRoy Franz 	 * larger) allocations, and both the address and size must meet
2965b88a31cSRoy Franz 	 * alignment constraints.
297f4f75ad5SArd Biesheuvel 	 */
298cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
299cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
300f4f75ad5SArd Biesheuvel 
3015b88a31cSRoy Franz 	size = round_up(size, EFI_ALLOC_ALIGN);
3025b88a31cSRoy Franz 	nr_pages = size / EFI_PAGE_SIZE;
303f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
304f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
305f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
306f4f75ad5SArd Biesheuvel 		u64 start, end;
307f4f75ad5SArd Biesheuvel 
30802e43c2dSBaoquan He 		desc = efi_early_memdesc_ptr(m, desc_size, i);
309f4f75ad5SArd Biesheuvel 
310f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
311f4f75ad5SArd Biesheuvel 			continue;
312f4f75ad5SArd Biesheuvel 
313b617c526SDan Williams 		if (efi_soft_reserve_enabled() &&
314b617c526SDan Williams 		    (desc->attribute & EFI_MEMORY_SP))
315b617c526SDan Williams 			continue;
316b617c526SDan Williams 
317f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
318f4f75ad5SArd Biesheuvel 			continue;
319f4f75ad5SArd Biesheuvel 
320f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
3215b88a31cSRoy Franz 		end = start + desc->num_pages * EFI_PAGE_SIZE;
322f4f75ad5SArd Biesheuvel 
323220dd769SKairui Song 		if (start < min)
324220dd769SKairui Song 			start = min;
325f4f75ad5SArd Biesheuvel 
326f4f75ad5SArd Biesheuvel 		start = round_up(start, align);
327f4f75ad5SArd Biesheuvel 		if ((start + size) > end)
328f4f75ad5SArd Biesheuvel 			continue;
329f4f75ad5SArd Biesheuvel 
330966291f6SArd Biesheuvel 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
331966291f6SArd Biesheuvel 				     EFI_LOADER_DATA, nr_pages, &start);
332f4f75ad5SArd Biesheuvel 		if (status == EFI_SUCCESS) {
333f4f75ad5SArd Biesheuvel 			*addr = start;
334f4f75ad5SArd Biesheuvel 			break;
335f4f75ad5SArd Biesheuvel 		}
336f4f75ad5SArd Biesheuvel 	}
337f4f75ad5SArd Biesheuvel 
338f4f75ad5SArd Biesheuvel 	if (i == map_size / desc_size)
339f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
340f4f75ad5SArd Biesheuvel 
341966291f6SArd Biesheuvel 	efi_bs_call(free_pool, map);
342f4f75ad5SArd Biesheuvel fail:
343f4f75ad5SArd Biesheuvel 	return status;
344f4f75ad5SArd Biesheuvel }
345f4f75ad5SArd Biesheuvel 
346cd33a5c1SArd Biesheuvel void efi_free(unsigned long size, unsigned long addr)
34723e60394SArd Biesheuvel 	__weak __alias(efi_free_native);
34823e60394SArd Biesheuvel 
34923e60394SArd Biesheuvel void efi_free_native(unsigned long size, unsigned long addr)
350f4f75ad5SArd Biesheuvel {
351f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
352f4f75ad5SArd Biesheuvel 
353f4f75ad5SArd Biesheuvel 	if (!size)
354f4f75ad5SArd Biesheuvel 		return;
355f4f75ad5SArd Biesheuvel 
356cf2b0f10SArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
35723e60394SArd Biesheuvel 	efi_system_table()->boottime->free_pages(addr, nr_pages);
358f4f75ad5SArd Biesheuvel }
359f4f75ad5SArd Biesheuvel 
360cd33a5c1SArd Biesheuvel static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16,
361cd33a5c1SArd Biesheuvel 				  void **handle, u64 *file_sz)
3622bd79f30SLukas Wunner {
3632bd79f30SLukas Wunner 	efi_file_handle_t *h, *fh = __fh;
3642bd79f30SLukas Wunner 	efi_file_info_t *info;
3652bd79f30SLukas Wunner 	efi_status_t status;
3662bd79f30SLukas Wunner 	efi_guid_t info_guid = EFI_FILE_INFO_ID;
3672bd79f30SLukas Wunner 	unsigned long info_sz;
3682bd79f30SLukas Wunner 
36914e900c7SArd Biesheuvel 	status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, 0);
3702bd79f30SLukas Wunner 	if (status != EFI_SUCCESS) {
3718173ec79SArd Biesheuvel 		efi_printk("Failed to open file: ");
3728173ec79SArd Biesheuvel 		efi_char16_printk(filename_16);
3738173ec79SArd Biesheuvel 		efi_printk("\n");
3742bd79f30SLukas Wunner 		return status;
3752bd79f30SLukas Wunner 	}
3762bd79f30SLukas Wunner 
3772bd79f30SLukas Wunner 	*handle = h;
3782bd79f30SLukas Wunner 
3792bd79f30SLukas Wunner 	info_sz = 0;
38014e900c7SArd Biesheuvel 	status = h->get_info(h, &info_guid, &info_sz, NULL);
3812bd79f30SLukas Wunner 	if (status != EFI_BUFFER_TOO_SMALL) {
3828173ec79SArd Biesheuvel 		efi_printk("Failed to get file info size\n");
3832bd79f30SLukas Wunner 		return status;
3842bd79f30SLukas Wunner 	}
3852bd79f30SLukas Wunner 
3862bd79f30SLukas Wunner grow:
387966291f6SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, info_sz,
388966291f6SArd Biesheuvel 			     (void **)&info);
3892bd79f30SLukas Wunner 	if (status != EFI_SUCCESS) {
3908173ec79SArd Biesheuvel 		efi_printk("Failed to alloc mem for file info\n");
3912bd79f30SLukas Wunner 		return status;
3922bd79f30SLukas Wunner 	}
3932bd79f30SLukas Wunner 
39414e900c7SArd Biesheuvel 	status = h->get_info(h, &info_guid, &info_sz, info);
3952bd79f30SLukas Wunner 	if (status == EFI_BUFFER_TOO_SMALL) {
396966291f6SArd Biesheuvel 		efi_bs_call(free_pool, info);
3972bd79f30SLukas Wunner 		goto grow;
3982bd79f30SLukas Wunner 	}
3992bd79f30SLukas Wunner 
4002bd79f30SLukas Wunner 	*file_sz = info->file_size;
401966291f6SArd Biesheuvel 	efi_bs_call(free_pool, info);
4022bd79f30SLukas Wunner 
4032bd79f30SLukas Wunner 	if (status != EFI_SUCCESS)
4048173ec79SArd Biesheuvel 		efi_printk("Failed to get initrd info\n");
4052bd79f30SLukas Wunner 
4062bd79f30SLukas Wunner 	return status;
4072bd79f30SLukas Wunner }
4082bd79f30SLukas Wunner 
409960a8d01SArd Biesheuvel static efi_status_t efi_file_read(efi_file_handle_t *handle,
410960a8d01SArd Biesheuvel 				  unsigned long *size, void *addr)
4112bd79f30SLukas Wunner {
41214e900c7SArd Biesheuvel 	return handle->read(handle, size, addr);
4132bd79f30SLukas Wunner }
4142bd79f30SLukas Wunner 
415960a8d01SArd Biesheuvel static efi_status_t efi_file_close(efi_file_handle_t *handle)
4162bd79f30SLukas Wunner {
41714e900c7SArd Biesheuvel 	return handle->close(handle);
4182bd79f30SLukas Wunner }
4192bd79f30SLukas Wunner 
420cd33a5c1SArd Biesheuvel static efi_status_t efi_open_volume(efi_loaded_image_t *image,
421c4db9c1eSLukas Wunner 				    efi_file_handle_t **__fh)
422c4db9c1eSLukas Wunner {
423c4db9c1eSLukas Wunner 	efi_file_io_interface_t *io;
424c4db9c1eSLukas Wunner 	efi_file_handle_t *fh;
425c4db9c1eSLukas Wunner 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
426c4db9c1eSLukas Wunner 	efi_status_t status;
42714e900c7SArd Biesheuvel 	efi_handle_t handle = image->device_handle;
428c4db9c1eSLukas Wunner 
429966291f6SArd Biesheuvel 	status = efi_bs_call(handle_protocol, handle, &fs_proto, (void **)&io);
430c4db9c1eSLukas Wunner 	if (status != EFI_SUCCESS) {
4318173ec79SArd Biesheuvel 		efi_printk("Failed to handle fs_proto\n");
432c4db9c1eSLukas Wunner 		return status;
433c4db9c1eSLukas Wunner 	}
434c4db9c1eSLukas Wunner 
43514e900c7SArd Biesheuvel 	status = io->open_volume(io, &fh);
436c4db9c1eSLukas Wunner 	if (status != EFI_SUCCESS)
4378173ec79SArd Biesheuvel 		efi_printk("Failed to open volume\n");
438c4db9c1eSLukas Wunner 	else
439c4db9c1eSLukas Wunner 		*__fh = fh;
440c4db9c1eSLukas Wunner 
441c4db9c1eSLukas Wunner 	return status;
442c4db9c1eSLukas Wunner }
443c4db9c1eSLukas Wunner 
4445a17dae4SMatt Fleming /*
4455a17dae4SMatt Fleming  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
4465a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
4475a17dae4SMatt Fleming  *
4485a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
4495a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
4505a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
4515a17dae4SMatt Fleming  */
45260f38de7SArd Biesheuvel efi_status_t efi_parse_options(char const *cmdline)
4535a17dae4SMatt Fleming {
4545a17dae4SMatt Fleming 	char *str;
4555a17dae4SMatt Fleming 
45660f38de7SArd Biesheuvel 	str = strstr(cmdline, "nokaslr");
45760f38de7SArd Biesheuvel 	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
4587d4e323dSArd Biesheuvel 		efi_nokaslr = true;
459b3879a4dSArd Biesheuvel 
460eeff7d63SArd Biesheuvel 	str = strstr(cmdline, "quiet");
461eeff7d63SArd Biesheuvel 	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
4627d4e323dSArd Biesheuvel 		efi_quiet = true;
463eeff7d63SArd Biesheuvel 
464b3879a4dSArd Biesheuvel 	/*
4655a17dae4SMatt Fleming 	 * If no EFI parameters were specified on the cmdline we've got
4665a17dae4SMatt Fleming 	 * nothing to do.
4675a17dae4SMatt Fleming 	 */
4685a17dae4SMatt Fleming 	str = strstr(cmdline, "efi=");
4695a17dae4SMatt Fleming 	if (!str)
4705a17dae4SMatt Fleming 		return EFI_SUCCESS;
4715a17dae4SMatt Fleming 
4725a17dae4SMatt Fleming 	/* Skip ahead to first argument */
4735a17dae4SMatt Fleming 	str += strlen("efi=");
4745a17dae4SMatt Fleming 
4755a17dae4SMatt Fleming 	/*
4765a17dae4SMatt Fleming 	 * Remember, because efi= is also used by the kernel we need to
4775a17dae4SMatt Fleming 	 * skip over arguments we don't understand.
4785a17dae4SMatt Fleming 	 */
4794c3f14bbSArd Biesheuvel 	while (*str && *str != ' ') {
4805a17dae4SMatt Fleming 		if (!strncmp(str, "nochunk", 7)) {
4815a17dae4SMatt Fleming 			str += strlen("nochunk");
4827d4e323dSArd Biesheuvel 			efi_chunk_size = -1UL;
4835a17dae4SMatt Fleming 		}
4845a17dae4SMatt Fleming 
4854e46c2a9SArd Biesheuvel 		if (!strncmp(str, "novamap", 7)) {
4864e46c2a9SArd Biesheuvel 			str += strlen("novamap");
4877d4e323dSArd Biesheuvel 			efi_novamap = true;
4884e46c2a9SArd Biesheuvel 		}
4894e46c2a9SArd Biesheuvel 
490b617c526SDan Williams 		if (IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
491b617c526SDan Williams 		    !strncmp(str, "nosoftreserve", 7)) {
492b617c526SDan Williams 			str += strlen("nosoftreserve");
4937d4e323dSArd Biesheuvel 			efi_nosoftreserve = true;
494b617c526SDan Williams 		}
495b617c526SDan Williams 
4965a17dae4SMatt Fleming 		/* Group words together, delimited by "," */
4974c3f14bbSArd Biesheuvel 		while (*str && *str != ' ' && *str != ',')
4985a17dae4SMatt Fleming 			str++;
4995a17dae4SMatt Fleming 
5005a17dae4SMatt Fleming 		if (*str == ',')
5015a17dae4SMatt Fleming 			str++;
5025a17dae4SMatt Fleming 	}
5035a17dae4SMatt Fleming 
5045a17dae4SMatt Fleming 	return EFI_SUCCESS;
5055a17dae4SMatt Fleming }
506f4f75ad5SArd Biesheuvel 
507f4f75ad5SArd Biesheuvel /*
508f4f75ad5SArd Biesheuvel  * Check the cmdline for a LILO-style file= arguments.
509f4f75ad5SArd Biesheuvel  *
510f4f75ad5SArd Biesheuvel  * We only support loading a file from the same filesystem as
511f4f75ad5SArd Biesheuvel  * the kernel image.
512f4f75ad5SArd Biesheuvel  */
513cd33a5c1SArd Biesheuvel efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
514f4f75ad5SArd Biesheuvel 				  char *cmd_line, char *option_string,
515f4f75ad5SArd Biesheuvel 				  unsigned long max_addr,
516f4f75ad5SArd Biesheuvel 				  unsigned long *load_addr,
517f4f75ad5SArd Biesheuvel 				  unsigned long *load_size)
518f4f75ad5SArd Biesheuvel {
519f4f75ad5SArd Biesheuvel 	struct file_info *files;
520f4f75ad5SArd Biesheuvel 	unsigned long file_addr;
521f4f75ad5SArd Biesheuvel 	u64 file_size_total;
522f4f75ad5SArd Biesheuvel 	efi_file_handle_t *fh = NULL;
523f4f75ad5SArd Biesheuvel 	efi_status_t status;
524f4f75ad5SArd Biesheuvel 	int nr_files;
525f4f75ad5SArd Biesheuvel 	char *str;
526f4f75ad5SArd Biesheuvel 	int i, j, k;
527f4f75ad5SArd Biesheuvel 
528f4f75ad5SArd Biesheuvel 	file_addr = 0;
529f4f75ad5SArd Biesheuvel 	file_size_total = 0;
530f4f75ad5SArd Biesheuvel 
531f4f75ad5SArd Biesheuvel 	str = cmd_line;
532f4f75ad5SArd Biesheuvel 
533f4f75ad5SArd Biesheuvel 	j = 0;			/* See close_handles */
534f4f75ad5SArd Biesheuvel 
535f4f75ad5SArd Biesheuvel 	if (!load_addr || !load_size)
536f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
537f4f75ad5SArd Biesheuvel 
538f4f75ad5SArd Biesheuvel 	*load_addr = 0;
539f4f75ad5SArd Biesheuvel 	*load_size = 0;
540f4f75ad5SArd Biesheuvel 
541f4f75ad5SArd Biesheuvel 	if (!str || !*str)
542f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
543f4f75ad5SArd Biesheuvel 
544f4f75ad5SArd Biesheuvel 	for (nr_files = 0; *str; nr_files++) {
545f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
546f4f75ad5SArd Biesheuvel 		if (!str)
547f4f75ad5SArd Biesheuvel 			break;
548f4f75ad5SArd Biesheuvel 
549f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
550f4f75ad5SArd Biesheuvel 
551f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
552f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
553f4f75ad5SArd Biesheuvel 			str++;
554f4f75ad5SArd Biesheuvel 
555f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n')
556f4f75ad5SArd Biesheuvel 			str++;
557f4f75ad5SArd Biesheuvel 	}
558f4f75ad5SArd Biesheuvel 
559f4f75ad5SArd Biesheuvel 	if (!nr_files)
560f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
561f4f75ad5SArd Biesheuvel 
562966291f6SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
563f4f75ad5SArd Biesheuvel 			     nr_files * sizeof(*files), (void **)&files);
564f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
5658173ec79SArd Biesheuvel 		pr_efi_err("Failed to alloc mem for file handle list\n");
566f4f75ad5SArd Biesheuvel 		goto fail;
567f4f75ad5SArd Biesheuvel 	}
568f4f75ad5SArd Biesheuvel 
569f4f75ad5SArd Biesheuvel 	str = cmd_line;
570f4f75ad5SArd Biesheuvel 	for (i = 0; i < nr_files; i++) {
571f4f75ad5SArd Biesheuvel 		struct file_info *file;
572f4f75ad5SArd Biesheuvel 		efi_char16_t filename_16[256];
573f4f75ad5SArd Biesheuvel 		efi_char16_t *p;
574f4f75ad5SArd Biesheuvel 
575f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
576f4f75ad5SArd Biesheuvel 		if (!str)
577f4f75ad5SArd Biesheuvel 			break;
578f4f75ad5SArd Biesheuvel 
579f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
580f4f75ad5SArd Biesheuvel 
581f4f75ad5SArd Biesheuvel 		file = &files[i];
582f4f75ad5SArd Biesheuvel 		p = filename_16;
583f4f75ad5SArd Biesheuvel 
584f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
585f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
586f4f75ad5SArd Biesheuvel 			str++;
587f4f75ad5SArd Biesheuvel 
588f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n') {
589f4f75ad5SArd Biesheuvel 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
590f4f75ad5SArd Biesheuvel 				break;
591f4f75ad5SArd Biesheuvel 
592f4f75ad5SArd Biesheuvel 			if (*str == '/') {
593f4f75ad5SArd Biesheuvel 				*p++ = '\\';
594f4f75ad5SArd Biesheuvel 				str++;
595f4f75ad5SArd Biesheuvel 			} else {
596f4f75ad5SArd Biesheuvel 				*p++ = *str++;
597f4f75ad5SArd Biesheuvel 			}
598f4f75ad5SArd Biesheuvel 		}
599f4f75ad5SArd Biesheuvel 
600f4f75ad5SArd Biesheuvel 		*p = '\0';
601f4f75ad5SArd Biesheuvel 
602f4f75ad5SArd Biesheuvel 		/* Only open the volume once. */
603f4f75ad5SArd Biesheuvel 		if (!i) {
604cd33a5c1SArd Biesheuvel 			status = efi_open_volume(image, &fh);
605f4f75ad5SArd Biesheuvel 			if (status != EFI_SUCCESS)
606f4f75ad5SArd Biesheuvel 				goto free_files;
607f4f75ad5SArd Biesheuvel 		}
608f4f75ad5SArd Biesheuvel 
609cd33a5c1SArd Biesheuvel 		status = efi_file_size(fh, filename_16, (void **)&file->handle,
610cd33a5c1SArd Biesheuvel 				       &file->size);
611f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS)
612f4f75ad5SArd Biesheuvel 			goto close_handles;
613f4f75ad5SArd Biesheuvel 
614f4f75ad5SArd Biesheuvel 		file_size_total += file->size;
615f4f75ad5SArd Biesheuvel 	}
616f4f75ad5SArd Biesheuvel 
617f4f75ad5SArd Biesheuvel 	if (file_size_total) {
618f4f75ad5SArd Biesheuvel 		unsigned long addr;
619f4f75ad5SArd Biesheuvel 
620f4f75ad5SArd Biesheuvel 		/*
621f4f75ad5SArd Biesheuvel 		 * Multiple files need to be at consecutive addresses in memory,
622f4f75ad5SArd Biesheuvel 		 * so allocate enough memory for all the files.  This is used
623f4f75ad5SArd Biesheuvel 		 * for loading multiple files.
624f4f75ad5SArd Biesheuvel 		 */
625cd33a5c1SArd Biesheuvel 		status = efi_high_alloc(file_size_total, 0x1000, &file_addr,
626cd33a5c1SArd Biesheuvel 					max_addr);
627f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
6288173ec79SArd Biesheuvel 			pr_efi_err("Failed to alloc highmem for files\n");
629f4f75ad5SArd Biesheuvel 			goto close_handles;
630f4f75ad5SArd Biesheuvel 		}
631f4f75ad5SArd Biesheuvel 
632f4f75ad5SArd Biesheuvel 		/* We've run out of free low memory. */
633f4f75ad5SArd Biesheuvel 		if (file_addr > max_addr) {
6348173ec79SArd Biesheuvel 			pr_efi_err("We've run out of free low memory\n");
635f4f75ad5SArd Biesheuvel 			status = EFI_INVALID_PARAMETER;
636f4f75ad5SArd Biesheuvel 			goto free_file_total;
637f4f75ad5SArd Biesheuvel 		}
638f4f75ad5SArd Biesheuvel 
639f4f75ad5SArd Biesheuvel 		addr = file_addr;
640f4f75ad5SArd Biesheuvel 		for (j = 0; j < nr_files; j++) {
641f4f75ad5SArd Biesheuvel 			unsigned long size;
642f4f75ad5SArd Biesheuvel 
643f4f75ad5SArd Biesheuvel 			size = files[j].size;
644f4f75ad5SArd Biesheuvel 			while (size) {
645f4f75ad5SArd Biesheuvel 				unsigned long chunksize;
646b3879a4dSArd Biesheuvel 
6477d4e323dSArd Biesheuvel 				if (IS_ENABLED(CONFIG_X86) && size > efi_chunk_size)
6487d4e323dSArd Biesheuvel 					chunksize = efi_chunk_size;
649f4f75ad5SArd Biesheuvel 				else
650f4f75ad5SArd Biesheuvel 					chunksize = size;
651f4f75ad5SArd Biesheuvel 
652f4f75ad5SArd Biesheuvel 				status = efi_file_read(files[j].handle,
653f4f75ad5SArd Biesheuvel 						       &chunksize,
654f4f75ad5SArd Biesheuvel 						       (void *)addr);
655f4f75ad5SArd Biesheuvel 				if (status != EFI_SUCCESS) {
6568173ec79SArd Biesheuvel 					pr_efi_err("Failed to read file\n");
657f4f75ad5SArd Biesheuvel 					goto free_file_total;
658f4f75ad5SArd Biesheuvel 				}
659f4f75ad5SArd Biesheuvel 				addr += chunksize;
660f4f75ad5SArd Biesheuvel 				size -= chunksize;
661f4f75ad5SArd Biesheuvel 			}
662f4f75ad5SArd Biesheuvel 
663f4f75ad5SArd Biesheuvel 			efi_file_close(files[j].handle);
664f4f75ad5SArd Biesheuvel 		}
665f4f75ad5SArd Biesheuvel 
666f4f75ad5SArd Biesheuvel 	}
667f4f75ad5SArd Biesheuvel 
668966291f6SArd Biesheuvel 	efi_bs_call(free_pool, files);
669f4f75ad5SArd Biesheuvel 
670f4f75ad5SArd Biesheuvel 	*load_addr = file_addr;
671f4f75ad5SArd Biesheuvel 	*load_size = file_size_total;
672f4f75ad5SArd Biesheuvel 
673f4f75ad5SArd Biesheuvel 	return status;
674f4f75ad5SArd Biesheuvel 
675f4f75ad5SArd Biesheuvel free_file_total:
676cd33a5c1SArd Biesheuvel 	efi_free(file_size_total, file_addr);
677f4f75ad5SArd Biesheuvel 
678f4f75ad5SArd Biesheuvel close_handles:
679f4f75ad5SArd Biesheuvel 	for (k = j; k < i; k++)
680f4f75ad5SArd Biesheuvel 		efi_file_close(files[k].handle);
681f4f75ad5SArd Biesheuvel free_files:
682966291f6SArd Biesheuvel 	efi_bs_call(free_pool, files);
683f4f75ad5SArd Biesheuvel fail:
684f4f75ad5SArd Biesheuvel 	*load_addr = 0;
685f4f75ad5SArd Biesheuvel 	*load_size = 0;
686f4f75ad5SArd Biesheuvel 
687f4f75ad5SArd Biesheuvel 	return status;
688f4f75ad5SArd Biesheuvel }
689f4f75ad5SArd Biesheuvel /*
690f4f75ad5SArd Biesheuvel  * Relocate a kernel image, either compressed or uncompressed.
691f4f75ad5SArd Biesheuvel  * In the ARM64 case, all kernel images are currently
692f4f75ad5SArd Biesheuvel  * uncompressed, and as such when we relocate it we need to
693f4f75ad5SArd Biesheuvel  * allocate additional space for the BSS segment. Any low
694f4f75ad5SArd Biesheuvel  * memory that this function should avoid needs to be
695f4f75ad5SArd Biesheuvel  * unavailable in the EFI memory map, as if the preferred
696f4f75ad5SArd Biesheuvel  * address is not available the lowest available address will
697f4f75ad5SArd Biesheuvel  * be used.
698f4f75ad5SArd Biesheuvel  */
699cd33a5c1SArd Biesheuvel efi_status_t efi_relocate_kernel(unsigned long *image_addr,
700f4f75ad5SArd Biesheuvel 				 unsigned long image_size,
701f4f75ad5SArd Biesheuvel 				 unsigned long alloc_size,
702f4f75ad5SArd Biesheuvel 				 unsigned long preferred_addr,
703220dd769SKairui Song 				 unsigned long alignment,
704220dd769SKairui Song 				 unsigned long min_addr)
705f4f75ad5SArd Biesheuvel {
706f4f75ad5SArd Biesheuvel 	unsigned long cur_image_addr;
707f4f75ad5SArd Biesheuvel 	unsigned long new_addr = 0;
708f4f75ad5SArd Biesheuvel 	efi_status_t status;
709f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
710f4f75ad5SArd Biesheuvel 	efi_physical_addr_t efi_addr = preferred_addr;
711f4f75ad5SArd Biesheuvel 
712f4f75ad5SArd Biesheuvel 	if (!image_addr || !image_size || !alloc_size)
713f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
714f4f75ad5SArd Biesheuvel 	if (alloc_size < image_size)
715f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
716f4f75ad5SArd Biesheuvel 
717f4f75ad5SArd Biesheuvel 	cur_image_addr = *image_addr;
718f4f75ad5SArd Biesheuvel 
719f4f75ad5SArd Biesheuvel 	/*
720f4f75ad5SArd Biesheuvel 	 * The EFI firmware loader could have placed the kernel image
721f4f75ad5SArd Biesheuvel 	 * anywhere in memory, but the kernel has restrictions on the
722f4f75ad5SArd Biesheuvel 	 * max physical address it can run at.  Some architectures
723f4f75ad5SArd Biesheuvel 	 * also have a prefered address, so first try to relocate
724f4f75ad5SArd Biesheuvel 	 * to the preferred address.  If that fails, allocate as low
725f4f75ad5SArd Biesheuvel 	 * as possible while respecting the required alignment.
726f4f75ad5SArd Biesheuvel 	 */
727cf2b0f10SArd Biesheuvel 	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
728966291f6SArd Biesheuvel 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
729966291f6SArd Biesheuvel 			     EFI_LOADER_DATA, nr_pages, &efi_addr);
730f4f75ad5SArd Biesheuvel 	new_addr = efi_addr;
731f4f75ad5SArd Biesheuvel 	/*
732f4f75ad5SArd Biesheuvel 	 * If preferred address allocation failed allocate as low as
733f4f75ad5SArd Biesheuvel 	 * possible.
734f4f75ad5SArd Biesheuvel 	 */
735f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
736cd33a5c1SArd Biesheuvel 		status = efi_low_alloc_above(alloc_size, alignment, &new_addr,
737cd33a5c1SArd Biesheuvel 					     min_addr);
738f4f75ad5SArd Biesheuvel 	}
739f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
7408173ec79SArd Biesheuvel 		pr_efi_err("Failed to allocate usable memory for kernel.\n");
741f4f75ad5SArd Biesheuvel 		return status;
742f4f75ad5SArd Biesheuvel 	}
743f4f75ad5SArd Biesheuvel 
744f4f75ad5SArd Biesheuvel 	/*
745f4f75ad5SArd Biesheuvel 	 * We know source/dest won't overlap since both memory ranges
746f4f75ad5SArd Biesheuvel 	 * have been allocated by UEFI, so we can safely use memcpy.
747f4f75ad5SArd Biesheuvel 	 */
748f4f75ad5SArd Biesheuvel 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
749f4f75ad5SArd Biesheuvel 
750f4f75ad5SArd Biesheuvel 	/* Return the new address of the relocated image. */
751f4f75ad5SArd Biesheuvel 	*image_addr = new_addr;
752f4f75ad5SArd Biesheuvel 
753f4f75ad5SArd Biesheuvel 	return status;
754f4f75ad5SArd Biesheuvel }
755f4f75ad5SArd Biesheuvel 
756f4f75ad5SArd Biesheuvel /*
757f4f75ad5SArd Biesheuvel  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
758f4f75ad5SArd Biesheuvel  * This overestimates for surrogates, but that is okay.
759f4f75ad5SArd Biesheuvel  */
760f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c)
761f4f75ad5SArd Biesheuvel {
762f4f75ad5SArd Biesheuvel 	return 1 + (c >= 0x80) + (c >= 0x800);
763f4f75ad5SArd Biesheuvel }
764f4f75ad5SArd Biesheuvel 
765f4f75ad5SArd Biesheuvel /*
766f4f75ad5SArd Biesheuvel  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
767f4f75ad5SArd Biesheuvel  */
768f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
769f4f75ad5SArd Biesheuvel {
770f4f75ad5SArd Biesheuvel 	unsigned int c;
771f4f75ad5SArd Biesheuvel 
772f4f75ad5SArd Biesheuvel 	while (n--) {
773f4f75ad5SArd Biesheuvel 		c = *src++;
774f4f75ad5SArd Biesheuvel 		if (n && c >= 0xd800 && c <= 0xdbff &&
775f4f75ad5SArd Biesheuvel 		    *src >= 0xdc00 && *src <= 0xdfff) {
776f4f75ad5SArd Biesheuvel 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
777f4f75ad5SArd Biesheuvel 			src++;
778f4f75ad5SArd Biesheuvel 			n--;
779f4f75ad5SArd Biesheuvel 		}
780f4f75ad5SArd Biesheuvel 		if (c >= 0xd800 && c <= 0xdfff)
781f4f75ad5SArd Biesheuvel 			c = 0xfffd; /* Unmatched surrogate */
782f4f75ad5SArd Biesheuvel 		if (c < 0x80) {
783f4f75ad5SArd Biesheuvel 			*dst++ = c;
784f4f75ad5SArd Biesheuvel 			continue;
785f4f75ad5SArd Biesheuvel 		}
786f4f75ad5SArd Biesheuvel 		if (c < 0x800) {
787f4f75ad5SArd Biesheuvel 			*dst++ = 0xc0 + (c >> 6);
788f4f75ad5SArd Biesheuvel 			goto t1;
789f4f75ad5SArd Biesheuvel 		}
790f4f75ad5SArd Biesheuvel 		if (c < 0x10000) {
791f4f75ad5SArd Biesheuvel 			*dst++ = 0xe0 + (c >> 12);
792f4f75ad5SArd Biesheuvel 			goto t2;
793f4f75ad5SArd Biesheuvel 		}
794f4f75ad5SArd Biesheuvel 		*dst++ = 0xf0 + (c >> 18);
795f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
796f4f75ad5SArd Biesheuvel 	t2:
797f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
798f4f75ad5SArd Biesheuvel 	t1:
799f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + (c & 0x3f);
800f4f75ad5SArd Biesheuvel 	}
801f4f75ad5SArd Biesheuvel 
802f4f75ad5SArd Biesheuvel 	return dst;
803f4f75ad5SArd Biesheuvel }
804f4f75ad5SArd Biesheuvel 
80548fcb2d0SArd Biesheuvel #ifndef MAX_CMDLINE_ADDRESS
80648fcb2d0SArd Biesheuvel #define MAX_CMDLINE_ADDRESS	ULONG_MAX
80748fcb2d0SArd Biesheuvel #endif
80848fcb2d0SArd Biesheuvel 
809f4f75ad5SArd Biesheuvel /*
810f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
811f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
812f4f75ad5SArd Biesheuvel  * Returns NULL on error.
813f4f75ad5SArd Biesheuvel  */
814cd33a5c1SArd Biesheuvel char *efi_convert_cmdline(efi_loaded_image_t *image,
815f4f75ad5SArd Biesheuvel 			  int *cmd_line_len)
816f4f75ad5SArd Biesheuvel {
817f4f75ad5SArd Biesheuvel 	const u16 *s2;
818f4f75ad5SArd Biesheuvel 	u8 *s1 = NULL;
819f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
820f4f75ad5SArd Biesheuvel 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
821f4f75ad5SArd Biesheuvel 	const u16 *options = image->load_options;
822f4f75ad5SArd Biesheuvel 	int options_bytes = 0;  /* UTF-8 bytes */
823f4f75ad5SArd Biesheuvel 	int options_chars = 0;  /* UTF-16 chars */
824f4f75ad5SArd Biesheuvel 	efi_status_t status;
825f4f75ad5SArd Biesheuvel 	u16 zero = 0;
826f4f75ad5SArd Biesheuvel 
827f4f75ad5SArd Biesheuvel 	if (options) {
828f4f75ad5SArd Biesheuvel 		s2 = options;
829f4f75ad5SArd Biesheuvel 		while (*s2 && *s2 != '\n'
830f4f75ad5SArd Biesheuvel 		       && options_chars < load_options_chars) {
831f4f75ad5SArd Biesheuvel 			options_bytes += efi_utf8_bytes(*s2++);
832f4f75ad5SArd Biesheuvel 			options_chars++;
833f4f75ad5SArd Biesheuvel 		}
834f4f75ad5SArd Biesheuvel 	}
835f4f75ad5SArd Biesheuvel 
836f4f75ad5SArd Biesheuvel 	if (!options_chars) {
837f4f75ad5SArd Biesheuvel 		/* No command line options, so return empty string*/
838f4f75ad5SArd Biesheuvel 		options = &zero;
839f4f75ad5SArd Biesheuvel 	}
840f4f75ad5SArd Biesheuvel 
841f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
842f4f75ad5SArd Biesheuvel 
843cd33a5c1SArd Biesheuvel 	status = efi_high_alloc(options_bytes, 0, &cmdline_addr,
844cd33a5c1SArd Biesheuvel 				MAX_CMDLINE_ADDRESS);
845f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
846f4f75ad5SArd Biesheuvel 		return NULL;
847f4f75ad5SArd Biesheuvel 
848f4f75ad5SArd Biesheuvel 	s1 = (u8 *)cmdline_addr;
849f4f75ad5SArd Biesheuvel 	s2 = (const u16 *)options;
850f4f75ad5SArd Biesheuvel 
851f4f75ad5SArd Biesheuvel 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
852f4f75ad5SArd Biesheuvel 	*s1 = '\0';
853f4f75ad5SArd Biesheuvel 
854f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
855f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
856f4f75ad5SArd Biesheuvel }
857fc07716bSJeffrey Hugo 
858fc07716bSJeffrey Hugo /*
859fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
860fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
861fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
862fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
863fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
864fc07716bSJeffrey Hugo  * function may be called multiple times.
865fc07716bSJeffrey Hugo  */
866cd33a5c1SArd Biesheuvel efi_status_t efi_exit_boot_services(void *handle,
867fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
868fc07716bSJeffrey Hugo 				    void *priv,
869fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
870fc07716bSJeffrey Hugo {
871fc07716bSJeffrey Hugo 	efi_status_t status;
872fc07716bSJeffrey Hugo 
873cd33a5c1SArd Biesheuvel 	status = efi_get_memory_map(map);
874fc07716bSJeffrey Hugo 
875fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
876fc07716bSJeffrey Hugo 		goto fail;
877fc07716bSJeffrey Hugo 
878cd33a5c1SArd Biesheuvel 	status = priv_func(map, priv);
879fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
880fc07716bSJeffrey Hugo 		goto free_map;
881fc07716bSJeffrey Hugo 
882966291f6SArd Biesheuvel 	status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
883fc07716bSJeffrey Hugo 
884fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
885fc07716bSJeffrey Hugo 		/*
886fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
887fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
888fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
889fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
890fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
891fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
892fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
893fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
894fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
895fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
896fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
897fc07716bSJeffrey Hugo 		 */
898fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
899966291f6SArd Biesheuvel 		status = efi_bs_call(get_memory_map,
900fc07716bSJeffrey Hugo 				     map->map_size,
901fc07716bSJeffrey Hugo 				     *map->map,
902fc07716bSJeffrey Hugo 				     map->key_ptr,
903fc07716bSJeffrey Hugo 				     map->desc_size,
904fc07716bSJeffrey Hugo 				     map->desc_ver);
905fc07716bSJeffrey Hugo 
906fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
907fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
908fc07716bSJeffrey Hugo 			goto fail;
909fc07716bSJeffrey Hugo 
910cd33a5c1SArd Biesheuvel 		status = priv_func(map, priv);
911fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
912fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
913fc07716bSJeffrey Hugo 			goto fail;
914fc07716bSJeffrey Hugo 
915966291f6SArd Biesheuvel 		status = efi_bs_call(exit_boot_services, handle, *map->key_ptr);
916fc07716bSJeffrey Hugo 	}
917fc07716bSJeffrey Hugo 
918fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
919fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
920fc07716bSJeffrey Hugo 		goto fail;
921fc07716bSJeffrey Hugo 
922fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
923fc07716bSJeffrey Hugo 
924fc07716bSJeffrey Hugo free_map:
925966291f6SArd Biesheuvel 	efi_bs_call(free_pool, *map->map);
926fc07716bSJeffrey Hugo fail:
927fc07716bSJeffrey Hugo 	return status;
928fc07716bSJeffrey Hugo }
92982d736acSMatthew Garrett 
930cd33a5c1SArd Biesheuvel void *get_efi_config_table(efi_guid_t guid)
93182d736acSMatthew Garrett {
93299ea8b1dSArd Biesheuvel 	unsigned long tables = efi_table_attr(efi_system_table(), tables);
93399ea8b1dSArd Biesheuvel 	int nr_tables = efi_table_attr(efi_system_table(), nr_tables);
934f958efe9SArd Biesheuvel 	int i;
935f958efe9SArd Biesheuvel 
936f958efe9SArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
937f958efe9SArd Biesheuvel 		efi_config_table_t *t = (void *)tables;
938f958efe9SArd Biesheuvel 
939f958efe9SArd Biesheuvel 		if (efi_guidcmp(t->guid, guid) == 0)
94099ea8b1dSArd Biesheuvel 			return efi_table_attr(t, table);
941f958efe9SArd Biesheuvel 
942f958efe9SArd Biesheuvel 		tables += efi_is_native() ? sizeof(efi_config_table_t)
943f958efe9SArd Biesheuvel 					  : sizeof(efi_config_table_32_t);
944f958efe9SArd Biesheuvel 	}
945f958efe9SArd Biesheuvel 	return NULL;
94682d736acSMatthew Garrett }
947dc29da14SArd Biesheuvel 
9488173ec79SArd Biesheuvel void efi_char16_printk(efi_char16_t *str)
949dc29da14SArd Biesheuvel {
95099ea8b1dSArd Biesheuvel 	efi_call_proto(efi_table_attr(efi_system_table(), con_out),
95147c0fd39SArd Biesheuvel 		       output_string, str);
952dc29da14SArd Biesheuvel }
953