1f4f75ad5SArd Biesheuvel /*
2f4f75ad5SArd Biesheuvel  * Helper functions used by the EFI stub on multiple
3f4f75ad5SArd Biesheuvel  * architectures. This should be #included by the EFI stub
4f4f75ad5SArd Biesheuvel  * implementation files.
5f4f75ad5SArd Biesheuvel  *
6f4f75ad5SArd Biesheuvel  * Copyright 2011 Intel Corporation; author Matt Fleming
7f4f75ad5SArd Biesheuvel  *
8f4f75ad5SArd Biesheuvel  * This file is part of the Linux kernel, and is made available
9f4f75ad5SArd Biesheuvel  * under the terms of the GNU General Public License version 2.
10f4f75ad5SArd Biesheuvel  *
11f4f75ad5SArd Biesheuvel  */
12f4f75ad5SArd Biesheuvel 
13f4f75ad5SArd Biesheuvel #include <linux/efi.h>
14f4f75ad5SArd Biesheuvel #include <asm/efi.h>
15f4f75ad5SArd Biesheuvel 
16f4f75ad5SArd Biesheuvel #include "efistub.h"
17f4f75ad5SArd Biesheuvel 
185a17dae4SMatt Fleming /*
195a17dae4SMatt Fleming  * Some firmware implementations have problems reading files in one go.
205a17dae4SMatt Fleming  * A read chunk size of 1MB seems to work for most platforms.
215a17dae4SMatt Fleming  *
225a17dae4SMatt Fleming  * Unfortunately, reading files in chunks triggers *other* bugs on some
235a17dae4SMatt Fleming  * platforms, so we provide a way to disable this workaround, which can
245a17dae4SMatt Fleming  * be done by passing "efi=nochunk" on the EFI boot stub command line.
255a17dae4SMatt Fleming  *
265a17dae4SMatt Fleming  * If you experience issues with initrd images being corrupt it's worth
275a17dae4SMatt Fleming  * trying efi=nochunk, but chunking is enabled by default because there
285a17dae4SMatt Fleming  * are far more machines that require the workaround than those that
295a17dae4SMatt Fleming  * break with it enabled.
305a17dae4SMatt Fleming  */
31f4f75ad5SArd Biesheuvel #define EFI_READ_CHUNK_SIZE	(1024 * 1024)
32f4f75ad5SArd Biesheuvel 
335a17dae4SMatt Fleming static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
345a17dae4SMatt Fleming 
3560f38de7SArd Biesheuvel static int __section(.data) __nokaslr;
36eeff7d63SArd Biesheuvel static int __section(.data) __quiet;
3760f38de7SArd Biesheuvel 
3860f38de7SArd Biesheuvel int __pure nokaslr(void)
3960f38de7SArd Biesheuvel {
4060f38de7SArd Biesheuvel 	return __nokaslr;
4160f38de7SArd Biesheuvel }
42eeff7d63SArd Biesheuvel int __pure is_quiet(void)
43eeff7d63SArd Biesheuvel {
44eeff7d63SArd Biesheuvel 	return __quiet;
45eeff7d63SArd Biesheuvel }
4660f38de7SArd Biesheuvel 
47dadb57abSJeffrey Hugo #define EFI_MMAP_NR_SLACK_SLOTS	8
48dadb57abSJeffrey Hugo 
49f4f75ad5SArd Biesheuvel struct file_info {
50f4f75ad5SArd Biesheuvel 	efi_file_handle_t *handle;
51f4f75ad5SArd Biesheuvel 	u64 size;
52f4f75ad5SArd Biesheuvel };
53f4f75ad5SArd Biesheuvel 
54f4f75ad5SArd Biesheuvel void efi_printk(efi_system_table_t *sys_table_arg, char *str)
55f4f75ad5SArd Biesheuvel {
56f4f75ad5SArd Biesheuvel 	char *s8;
57f4f75ad5SArd Biesheuvel 
58f4f75ad5SArd Biesheuvel 	for (s8 = str; *s8; s8++) {
59f4f75ad5SArd Biesheuvel 		efi_char16_t ch[2] = { 0 };
60f4f75ad5SArd Biesheuvel 
61f4f75ad5SArd Biesheuvel 		ch[0] = *s8;
62f4f75ad5SArd Biesheuvel 		if (*s8 == '\n') {
63f4f75ad5SArd Biesheuvel 			efi_char16_t nl[2] = { '\r', 0 };
64f4f75ad5SArd Biesheuvel 			efi_char16_printk(sys_table_arg, nl);
65f4f75ad5SArd Biesheuvel 		}
66f4f75ad5SArd Biesheuvel 
67f4f75ad5SArd Biesheuvel 		efi_char16_printk(sys_table_arg, ch);
68f4f75ad5SArd Biesheuvel 	}
69f4f75ad5SArd Biesheuvel }
70f4f75ad5SArd Biesheuvel 
71dadb57abSJeffrey Hugo static inline bool mmap_has_headroom(unsigned long buff_size,
72dadb57abSJeffrey Hugo 				     unsigned long map_size,
73dadb57abSJeffrey Hugo 				     unsigned long desc_size)
74dadb57abSJeffrey Hugo {
75dadb57abSJeffrey Hugo 	unsigned long slack = buff_size - map_size;
76dadb57abSJeffrey Hugo 
77dadb57abSJeffrey Hugo 	return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
78dadb57abSJeffrey Hugo }
79dadb57abSJeffrey Hugo 
80f4f75ad5SArd Biesheuvel efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
81dadb57abSJeffrey Hugo 				struct efi_boot_memmap *map)
82f4f75ad5SArd Biesheuvel {
83f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *m = NULL;
84f4f75ad5SArd Biesheuvel 	efi_status_t status;
85f4f75ad5SArd Biesheuvel 	unsigned long key;
86f4f75ad5SArd Biesheuvel 	u32 desc_version;
87f4f75ad5SArd Biesheuvel 
88dadb57abSJeffrey Hugo 	*map->desc_size =	sizeof(*m);
89dadb57abSJeffrey Hugo 	*map->map_size =	*map->desc_size * 32;
90dadb57abSJeffrey Hugo 	*map->buff_size =	*map->map_size;
9143a9f696SMatt Fleming again:
92f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
93dadb57abSJeffrey Hugo 				*map->map_size, (void **)&m);
94f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
95f4f75ad5SArd Biesheuvel 		goto fail;
96f4f75ad5SArd Biesheuvel 
97dadb57abSJeffrey Hugo 	*map->desc_size = 0;
9843a9f696SMatt Fleming 	key = 0;
99dadb57abSJeffrey Hugo 	status = efi_call_early(get_memory_map, map->map_size, m,
100dadb57abSJeffrey Hugo 				&key, map->desc_size, &desc_version);
101dadb57abSJeffrey Hugo 	if (status == EFI_BUFFER_TOO_SMALL ||
102dadb57abSJeffrey Hugo 	    !mmap_has_headroom(*map->buff_size, *map->map_size,
103dadb57abSJeffrey Hugo 			       *map->desc_size)) {
104f4f75ad5SArd Biesheuvel 		efi_call_early(free_pool, m);
105dadb57abSJeffrey Hugo 		/*
106dadb57abSJeffrey Hugo 		 * Make sure there is some entries of headroom so that the
107dadb57abSJeffrey Hugo 		 * buffer can be reused for a new map after allocations are
108dadb57abSJeffrey Hugo 		 * no longer permitted.  Its unlikely that the map will grow to
109dadb57abSJeffrey Hugo 		 * exceed this headroom once we are ready to trigger
110dadb57abSJeffrey Hugo 		 * ExitBootServices()
111dadb57abSJeffrey Hugo 		 */
112dadb57abSJeffrey Hugo 		*map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
113dadb57abSJeffrey Hugo 		*map->buff_size = *map->map_size;
11443a9f696SMatt Fleming 		goto again;
115f4f75ad5SArd Biesheuvel 	}
116f4f75ad5SArd Biesheuvel 
117f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
118f4f75ad5SArd Biesheuvel 		efi_call_early(free_pool, m);
119f4f75ad5SArd Biesheuvel 
120dadb57abSJeffrey Hugo 	if (map->key_ptr && status == EFI_SUCCESS)
121dadb57abSJeffrey Hugo 		*map->key_ptr = key;
122dadb57abSJeffrey Hugo 	if (map->desc_ver && status == EFI_SUCCESS)
123dadb57abSJeffrey Hugo 		*map->desc_ver = desc_version;
124f4f75ad5SArd Biesheuvel 
125f4f75ad5SArd Biesheuvel fail:
126dadb57abSJeffrey Hugo 	*map->map = m;
127f4f75ad5SArd Biesheuvel 	return status;
128f4f75ad5SArd Biesheuvel }
129f4f75ad5SArd Biesheuvel 
130f4f75ad5SArd Biesheuvel 
131ddeeefe2SArd Biesheuvel unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
132f4f75ad5SArd Biesheuvel {
133f4f75ad5SArd Biesheuvel 	efi_status_t status;
134dadb57abSJeffrey Hugo 	unsigned long map_size, buff_size;
135f4f75ad5SArd Biesheuvel 	unsigned long membase  = EFI_ERROR;
136f4f75ad5SArd Biesheuvel 	struct efi_memory_map map;
137f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *md;
138dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
139f4f75ad5SArd Biesheuvel 
140dadb57abSJeffrey Hugo 	boot_map.map =		(efi_memory_desc_t **)&map.map;
141dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
142dadb57abSJeffrey Hugo 	boot_map.desc_size =	&map.desc_size;
143dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
144dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
145dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
146dadb57abSJeffrey Hugo 
147dadb57abSJeffrey Hugo 	status = efi_get_memory_map(sys_table_arg, &boot_map);
148f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
149f4f75ad5SArd Biesheuvel 		return membase;
150f4f75ad5SArd Biesheuvel 
151f4f75ad5SArd Biesheuvel 	map.map_end = map.map + map_size;
152f4f75ad5SArd Biesheuvel 
15378ce248fSMatt Fleming 	for_each_efi_memory_desc_in_map(&map, md) {
15478ce248fSMatt Fleming 		if (md->attribute & EFI_MEMORY_WB) {
155f4f75ad5SArd Biesheuvel 			if (membase > md->phys_addr)
156f4f75ad5SArd Biesheuvel 				membase = md->phys_addr;
15778ce248fSMatt Fleming 		}
15878ce248fSMatt Fleming 	}
159f4f75ad5SArd Biesheuvel 
160f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map.map);
161f4f75ad5SArd Biesheuvel 
162f4f75ad5SArd Biesheuvel 	return membase;
163f4f75ad5SArd Biesheuvel }
164f4f75ad5SArd Biesheuvel 
165f4f75ad5SArd Biesheuvel /*
166f4f75ad5SArd Biesheuvel  * Allocate at the highest possible address that is not above 'max'.
167f4f75ad5SArd Biesheuvel  */
168f4f75ad5SArd Biesheuvel efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
169f4f75ad5SArd Biesheuvel 			    unsigned long size, unsigned long align,
170f4f75ad5SArd Biesheuvel 			    unsigned long *addr, unsigned long max)
171f4f75ad5SArd Biesheuvel {
172dadb57abSJeffrey Hugo 	unsigned long map_size, desc_size, buff_size;
173f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
174f4f75ad5SArd Biesheuvel 	efi_status_t status;
175f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
176f4f75ad5SArd Biesheuvel 	u64 max_addr = 0;
177f4f75ad5SArd Biesheuvel 	int i;
178dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
179f4f75ad5SArd Biesheuvel 
180dadb57abSJeffrey Hugo 	boot_map.map =		&map;
181dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
182dadb57abSJeffrey Hugo 	boot_map.desc_size =	&desc_size;
183dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
184dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
185dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
186dadb57abSJeffrey Hugo 
187dadb57abSJeffrey Hugo 	status = efi_get_memory_map(sys_table_arg, &boot_map);
188f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
189f4f75ad5SArd Biesheuvel 		goto fail;
190f4f75ad5SArd Biesheuvel 
191f4f75ad5SArd Biesheuvel 	/*
1925b88a31cSRoy Franz 	 * Enforce minimum alignment that EFI or Linux requires when
1935b88a31cSRoy Franz 	 * requesting a specific address.  We are doing page-based (or
1945b88a31cSRoy Franz 	 * larger) allocations, and both the address and size must meet
1955b88a31cSRoy Franz 	 * alignment constraints.
196f4f75ad5SArd Biesheuvel 	 */
197cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
198cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
199f4f75ad5SArd Biesheuvel 
2005b88a31cSRoy Franz 	size = round_up(size, EFI_ALLOC_ALIGN);
2015b88a31cSRoy Franz 	nr_pages = size / EFI_PAGE_SIZE;
202f4f75ad5SArd Biesheuvel again:
203f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
204f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
205f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
206f4f75ad5SArd Biesheuvel 		u64 start, end;
207f4f75ad5SArd Biesheuvel 
20802e43c2dSBaoquan He 		desc = efi_early_memdesc_ptr(m, desc_size, i);
209f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
210f4f75ad5SArd Biesheuvel 			continue;
211f4f75ad5SArd Biesheuvel 
212f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
213f4f75ad5SArd Biesheuvel 			continue;
214f4f75ad5SArd Biesheuvel 
215f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
2165b88a31cSRoy Franz 		end = start + desc->num_pages * EFI_PAGE_SIZE;
217f4f75ad5SArd Biesheuvel 
2187ed620bbSYinghai Lu 		if (end > max)
219f4f75ad5SArd Biesheuvel 			end = max;
220f4f75ad5SArd Biesheuvel 
2217ed620bbSYinghai Lu 		if ((start + size) > end)
2227ed620bbSYinghai Lu 			continue;
2237ed620bbSYinghai Lu 
224f4f75ad5SArd Biesheuvel 		if (round_down(end - size, align) < start)
225f4f75ad5SArd Biesheuvel 			continue;
226f4f75ad5SArd Biesheuvel 
227f4f75ad5SArd Biesheuvel 		start = round_down(end - size, align);
228f4f75ad5SArd Biesheuvel 
229f4f75ad5SArd Biesheuvel 		/*
230f4f75ad5SArd Biesheuvel 		 * Don't allocate at 0x0. It will confuse code that
231f4f75ad5SArd Biesheuvel 		 * checks pointers against NULL.
232f4f75ad5SArd Biesheuvel 		 */
233f4f75ad5SArd Biesheuvel 		if (start == 0x0)
234f4f75ad5SArd Biesheuvel 			continue;
235f4f75ad5SArd Biesheuvel 
236f4f75ad5SArd Biesheuvel 		if (start > max_addr)
237f4f75ad5SArd Biesheuvel 			max_addr = start;
238f4f75ad5SArd Biesheuvel 	}
239f4f75ad5SArd Biesheuvel 
240f4f75ad5SArd Biesheuvel 	if (!max_addr)
241f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
242f4f75ad5SArd Biesheuvel 	else {
243f4f75ad5SArd Biesheuvel 		status = efi_call_early(allocate_pages,
244f4f75ad5SArd Biesheuvel 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
245f4f75ad5SArd Biesheuvel 					nr_pages, &max_addr);
246f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
247f4f75ad5SArd Biesheuvel 			max = max_addr;
248f4f75ad5SArd Biesheuvel 			max_addr = 0;
249f4f75ad5SArd Biesheuvel 			goto again;
250f4f75ad5SArd Biesheuvel 		}
251f4f75ad5SArd Biesheuvel 
252f4f75ad5SArd Biesheuvel 		*addr = max_addr;
253f4f75ad5SArd Biesheuvel 	}
254f4f75ad5SArd Biesheuvel 
255f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map);
256f4f75ad5SArd Biesheuvel fail:
257f4f75ad5SArd Biesheuvel 	return status;
258f4f75ad5SArd Biesheuvel }
259f4f75ad5SArd Biesheuvel 
260f4f75ad5SArd Biesheuvel /*
261f4f75ad5SArd Biesheuvel  * Allocate at the lowest possible address.
262f4f75ad5SArd Biesheuvel  */
263f4f75ad5SArd Biesheuvel efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
264f4f75ad5SArd Biesheuvel 			   unsigned long size, unsigned long align,
265f4f75ad5SArd Biesheuvel 			   unsigned long *addr)
266f4f75ad5SArd Biesheuvel {
267dadb57abSJeffrey Hugo 	unsigned long map_size, desc_size, buff_size;
268f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
269f4f75ad5SArd Biesheuvel 	efi_status_t status;
270f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
271f4f75ad5SArd Biesheuvel 	int i;
272dadb57abSJeffrey Hugo 	struct efi_boot_memmap boot_map;
273f4f75ad5SArd Biesheuvel 
274dadb57abSJeffrey Hugo 	boot_map.map =		&map;
275dadb57abSJeffrey Hugo 	boot_map.map_size =	&map_size;
276dadb57abSJeffrey Hugo 	boot_map.desc_size =	&desc_size;
277dadb57abSJeffrey Hugo 	boot_map.desc_ver =	NULL;
278dadb57abSJeffrey Hugo 	boot_map.key_ptr =	NULL;
279dadb57abSJeffrey Hugo 	boot_map.buff_size =	&buff_size;
280dadb57abSJeffrey Hugo 
281dadb57abSJeffrey Hugo 	status = efi_get_memory_map(sys_table_arg, &boot_map);
282f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
283f4f75ad5SArd Biesheuvel 		goto fail;
284f4f75ad5SArd Biesheuvel 
285f4f75ad5SArd Biesheuvel 	/*
2865b88a31cSRoy Franz 	 * Enforce minimum alignment that EFI or Linux requires when
2875b88a31cSRoy Franz 	 * requesting a specific address.  We are doing page-based (or
2885b88a31cSRoy Franz 	 * larger) allocations, and both the address and size must meet
2895b88a31cSRoy Franz 	 * alignment constraints.
290f4f75ad5SArd Biesheuvel 	 */
291cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
292cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
293f4f75ad5SArd Biesheuvel 
2945b88a31cSRoy Franz 	size = round_up(size, EFI_ALLOC_ALIGN);
2955b88a31cSRoy Franz 	nr_pages = size / EFI_PAGE_SIZE;
296f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
297f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
298f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
299f4f75ad5SArd Biesheuvel 		u64 start, end;
300f4f75ad5SArd Biesheuvel 
30102e43c2dSBaoquan He 		desc = efi_early_memdesc_ptr(m, desc_size, i);
302f4f75ad5SArd Biesheuvel 
303f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
304f4f75ad5SArd Biesheuvel 			continue;
305f4f75ad5SArd Biesheuvel 
306f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
307f4f75ad5SArd Biesheuvel 			continue;
308f4f75ad5SArd Biesheuvel 
309f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
3105b88a31cSRoy Franz 		end = start + desc->num_pages * EFI_PAGE_SIZE;
311f4f75ad5SArd Biesheuvel 
312f4f75ad5SArd Biesheuvel 		/*
313f4f75ad5SArd Biesheuvel 		 * Don't allocate at 0x0. It will confuse code that
314f4f75ad5SArd Biesheuvel 		 * checks pointers against NULL. Skip the first 8
315f4f75ad5SArd Biesheuvel 		 * bytes so we start at a nice even number.
316f4f75ad5SArd Biesheuvel 		 */
317f4f75ad5SArd Biesheuvel 		if (start == 0x0)
318f4f75ad5SArd Biesheuvel 			start += 8;
319f4f75ad5SArd Biesheuvel 
320f4f75ad5SArd Biesheuvel 		start = round_up(start, align);
321f4f75ad5SArd Biesheuvel 		if ((start + size) > end)
322f4f75ad5SArd Biesheuvel 			continue;
323f4f75ad5SArd Biesheuvel 
324f4f75ad5SArd Biesheuvel 		status = efi_call_early(allocate_pages,
325f4f75ad5SArd Biesheuvel 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
326f4f75ad5SArd Biesheuvel 					nr_pages, &start);
327f4f75ad5SArd Biesheuvel 		if (status == EFI_SUCCESS) {
328f4f75ad5SArd Biesheuvel 			*addr = start;
329f4f75ad5SArd Biesheuvel 			break;
330f4f75ad5SArd Biesheuvel 		}
331f4f75ad5SArd Biesheuvel 	}
332f4f75ad5SArd Biesheuvel 
333f4f75ad5SArd Biesheuvel 	if (i == map_size / desc_size)
334f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
335f4f75ad5SArd Biesheuvel 
336f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map);
337f4f75ad5SArd Biesheuvel fail:
338f4f75ad5SArd Biesheuvel 	return status;
339f4f75ad5SArd Biesheuvel }
340f4f75ad5SArd Biesheuvel 
341f4f75ad5SArd Biesheuvel void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
342f4f75ad5SArd Biesheuvel 	      unsigned long addr)
343f4f75ad5SArd Biesheuvel {
344f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
345f4f75ad5SArd Biesheuvel 
346f4f75ad5SArd Biesheuvel 	if (!size)
347f4f75ad5SArd Biesheuvel 		return;
348f4f75ad5SArd Biesheuvel 
349cf2b0f10SArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
350f4f75ad5SArd Biesheuvel 	efi_call_early(free_pages, addr, nr_pages);
351f4f75ad5SArd Biesheuvel }
352f4f75ad5SArd Biesheuvel 
3532bd79f30SLukas Wunner static efi_status_t efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
3542bd79f30SLukas Wunner 				  efi_char16_t *filename_16, void **handle,
3552bd79f30SLukas Wunner 				  u64 *file_sz)
3562bd79f30SLukas Wunner {
3572bd79f30SLukas Wunner 	efi_file_handle_t *h, *fh = __fh;
3582bd79f30SLukas Wunner 	efi_file_info_t *info;
3592bd79f30SLukas Wunner 	efi_status_t status;
3602bd79f30SLukas Wunner 	efi_guid_t info_guid = EFI_FILE_INFO_ID;
3612bd79f30SLukas Wunner 	unsigned long info_sz;
3622bd79f30SLukas Wunner 
3632bd79f30SLukas Wunner 	status = efi_call_proto(efi_file_handle, open, fh, &h, filename_16,
3642bd79f30SLukas Wunner 				EFI_FILE_MODE_READ, (u64)0);
3652bd79f30SLukas Wunner 	if (status != EFI_SUCCESS) {
3662bd79f30SLukas Wunner 		efi_printk(sys_table_arg, "Failed to open file: ");
3672bd79f30SLukas Wunner 		efi_char16_printk(sys_table_arg, filename_16);
3682bd79f30SLukas Wunner 		efi_printk(sys_table_arg, "\n");
3692bd79f30SLukas Wunner 		return status;
3702bd79f30SLukas Wunner 	}
3712bd79f30SLukas Wunner 
3722bd79f30SLukas Wunner 	*handle = h;
3732bd79f30SLukas Wunner 
3742bd79f30SLukas Wunner 	info_sz = 0;
3752bd79f30SLukas Wunner 	status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
3762bd79f30SLukas Wunner 				&info_sz, NULL);
3772bd79f30SLukas Wunner 	if (status != EFI_BUFFER_TOO_SMALL) {
3782bd79f30SLukas Wunner 		efi_printk(sys_table_arg, "Failed to get file info size\n");
3792bd79f30SLukas Wunner 		return status;
3802bd79f30SLukas Wunner 	}
3812bd79f30SLukas Wunner 
3822bd79f30SLukas Wunner grow:
3832bd79f30SLukas Wunner 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
3842bd79f30SLukas Wunner 				info_sz, (void **)&info);
3852bd79f30SLukas Wunner 	if (status != EFI_SUCCESS) {
3862bd79f30SLukas Wunner 		efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
3872bd79f30SLukas Wunner 		return status;
3882bd79f30SLukas Wunner 	}
3892bd79f30SLukas Wunner 
3902bd79f30SLukas Wunner 	status = efi_call_proto(efi_file_handle, get_info, h, &info_guid,
3912bd79f30SLukas Wunner 				&info_sz, info);
3922bd79f30SLukas Wunner 	if (status == EFI_BUFFER_TOO_SMALL) {
3932bd79f30SLukas Wunner 		efi_call_early(free_pool, info);
3942bd79f30SLukas Wunner 		goto grow;
3952bd79f30SLukas Wunner 	}
3962bd79f30SLukas Wunner 
3972bd79f30SLukas Wunner 	*file_sz = info->file_size;
3982bd79f30SLukas Wunner 	efi_call_early(free_pool, info);
3992bd79f30SLukas Wunner 
4002bd79f30SLukas Wunner 	if (status != EFI_SUCCESS)
4012bd79f30SLukas Wunner 		efi_printk(sys_table_arg, "Failed to get initrd info\n");
4022bd79f30SLukas Wunner 
4032bd79f30SLukas Wunner 	return status;
4042bd79f30SLukas Wunner }
4052bd79f30SLukas Wunner 
4062bd79f30SLukas Wunner static efi_status_t efi_file_read(void *handle, unsigned long *size, void *addr)
4072bd79f30SLukas Wunner {
4082bd79f30SLukas Wunner 	return efi_call_proto(efi_file_handle, read, handle, size, addr);
4092bd79f30SLukas Wunner }
4102bd79f30SLukas Wunner 
4112bd79f30SLukas Wunner static efi_status_t efi_file_close(void *handle)
4122bd79f30SLukas Wunner {
4132bd79f30SLukas Wunner 	return efi_call_proto(efi_file_handle, close, handle);
4142bd79f30SLukas Wunner }
4152bd79f30SLukas Wunner 
416c4db9c1eSLukas Wunner static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
417c4db9c1eSLukas Wunner 				    efi_loaded_image_t *image,
418c4db9c1eSLukas Wunner 				    efi_file_handle_t **__fh)
419c4db9c1eSLukas Wunner {
420c4db9c1eSLukas Wunner 	efi_file_io_interface_t *io;
421c4db9c1eSLukas Wunner 	efi_file_handle_t *fh;
422c4db9c1eSLukas Wunner 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
423c4db9c1eSLukas Wunner 	efi_status_t status;
424c4db9c1eSLukas Wunner 	void *handle = (void *)(unsigned long)efi_table_attr(efi_loaded_image,
425c4db9c1eSLukas Wunner 							     device_handle,
426c4db9c1eSLukas Wunner 							     image);
427c4db9c1eSLukas Wunner 
428c4db9c1eSLukas Wunner 	status = efi_call_early(handle_protocol, handle,
429c4db9c1eSLukas Wunner 				&fs_proto, (void **)&io);
430c4db9c1eSLukas Wunner 	if (status != EFI_SUCCESS) {
431c4db9c1eSLukas Wunner 		efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
432c4db9c1eSLukas Wunner 		return status;
433c4db9c1eSLukas Wunner 	}
434c4db9c1eSLukas Wunner 
435c4db9c1eSLukas Wunner 	status = efi_call_proto(efi_file_io_interface, open_volume, io, &fh);
436c4db9c1eSLukas Wunner 	if (status != EFI_SUCCESS)
437c4db9c1eSLukas Wunner 		efi_printk(sys_table_arg, "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) == ' '))
45860f38de7SArd Biesheuvel 		__nokaslr = 1;
459b3879a4dSArd Biesheuvel 
460eeff7d63SArd Biesheuvel 	str = strstr(cmdline, "quiet");
461eeff7d63SArd Biesheuvel 	if (str == cmdline || (str && str > cmdline && *(str - 1) == ' '))
462eeff7d63SArd Biesheuvel 		__quiet = 1;
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");
4825a17dae4SMatt Fleming 			__chunk_size = -1UL;
4835a17dae4SMatt Fleming 		}
4845a17dae4SMatt Fleming 
4855a17dae4SMatt Fleming 		/* Group words together, delimited by "," */
4864c3f14bbSArd Biesheuvel 		while (*str && *str != ' ' && *str != ',')
4875a17dae4SMatt Fleming 			str++;
4885a17dae4SMatt Fleming 
4895a17dae4SMatt Fleming 		if (*str == ',')
4905a17dae4SMatt Fleming 			str++;
4915a17dae4SMatt Fleming 	}
4925a17dae4SMatt Fleming 
4935a17dae4SMatt Fleming 	return EFI_SUCCESS;
4945a17dae4SMatt Fleming }
495f4f75ad5SArd Biesheuvel 
496f4f75ad5SArd Biesheuvel /*
497f4f75ad5SArd Biesheuvel  * Check the cmdline for a LILO-style file= arguments.
498f4f75ad5SArd Biesheuvel  *
499f4f75ad5SArd Biesheuvel  * We only support loading a file from the same filesystem as
500f4f75ad5SArd Biesheuvel  * the kernel image.
501f4f75ad5SArd Biesheuvel  */
502f4f75ad5SArd Biesheuvel efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
503f4f75ad5SArd Biesheuvel 				  efi_loaded_image_t *image,
504f4f75ad5SArd Biesheuvel 				  char *cmd_line, char *option_string,
505f4f75ad5SArd Biesheuvel 				  unsigned long max_addr,
506f4f75ad5SArd Biesheuvel 				  unsigned long *load_addr,
507f4f75ad5SArd Biesheuvel 				  unsigned long *load_size)
508f4f75ad5SArd Biesheuvel {
509f4f75ad5SArd Biesheuvel 	struct file_info *files;
510f4f75ad5SArd Biesheuvel 	unsigned long file_addr;
511f4f75ad5SArd Biesheuvel 	u64 file_size_total;
512f4f75ad5SArd Biesheuvel 	efi_file_handle_t *fh = NULL;
513f4f75ad5SArd Biesheuvel 	efi_status_t status;
514f4f75ad5SArd Biesheuvel 	int nr_files;
515f4f75ad5SArd Biesheuvel 	char *str;
516f4f75ad5SArd Biesheuvel 	int i, j, k;
517f4f75ad5SArd Biesheuvel 
518f4f75ad5SArd Biesheuvel 	file_addr = 0;
519f4f75ad5SArd Biesheuvel 	file_size_total = 0;
520f4f75ad5SArd Biesheuvel 
521f4f75ad5SArd Biesheuvel 	str = cmd_line;
522f4f75ad5SArd Biesheuvel 
523f4f75ad5SArd Biesheuvel 	j = 0;			/* See close_handles */
524f4f75ad5SArd Biesheuvel 
525f4f75ad5SArd Biesheuvel 	if (!load_addr || !load_size)
526f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
527f4f75ad5SArd Biesheuvel 
528f4f75ad5SArd Biesheuvel 	*load_addr = 0;
529f4f75ad5SArd Biesheuvel 	*load_size = 0;
530f4f75ad5SArd Biesheuvel 
531f4f75ad5SArd Biesheuvel 	if (!str || !*str)
532f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
533f4f75ad5SArd Biesheuvel 
534f4f75ad5SArd Biesheuvel 	for (nr_files = 0; *str; nr_files++) {
535f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
536f4f75ad5SArd Biesheuvel 		if (!str)
537f4f75ad5SArd Biesheuvel 			break;
538f4f75ad5SArd Biesheuvel 
539f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
540f4f75ad5SArd Biesheuvel 
541f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
542f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
543f4f75ad5SArd Biesheuvel 			str++;
544f4f75ad5SArd Biesheuvel 
545f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n')
546f4f75ad5SArd Biesheuvel 			str++;
547f4f75ad5SArd Biesheuvel 	}
548f4f75ad5SArd Biesheuvel 
549f4f75ad5SArd Biesheuvel 	if (!nr_files)
550f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
551f4f75ad5SArd Biesheuvel 
552f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
553f4f75ad5SArd Biesheuvel 				nr_files * sizeof(*files), (void **)&files);
554f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
555f4f75ad5SArd Biesheuvel 		pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
556f4f75ad5SArd Biesheuvel 		goto fail;
557f4f75ad5SArd Biesheuvel 	}
558f4f75ad5SArd Biesheuvel 
559f4f75ad5SArd Biesheuvel 	str = cmd_line;
560f4f75ad5SArd Biesheuvel 	for (i = 0; i < nr_files; i++) {
561f4f75ad5SArd Biesheuvel 		struct file_info *file;
562f4f75ad5SArd Biesheuvel 		efi_char16_t filename_16[256];
563f4f75ad5SArd Biesheuvel 		efi_char16_t *p;
564f4f75ad5SArd Biesheuvel 
565f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
566f4f75ad5SArd Biesheuvel 		if (!str)
567f4f75ad5SArd Biesheuvel 			break;
568f4f75ad5SArd Biesheuvel 
569f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
570f4f75ad5SArd Biesheuvel 
571f4f75ad5SArd Biesheuvel 		file = &files[i];
572f4f75ad5SArd Biesheuvel 		p = filename_16;
573f4f75ad5SArd Biesheuvel 
574f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
575f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
576f4f75ad5SArd Biesheuvel 			str++;
577f4f75ad5SArd Biesheuvel 
578f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n') {
579f4f75ad5SArd Biesheuvel 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
580f4f75ad5SArd Biesheuvel 				break;
581f4f75ad5SArd Biesheuvel 
582f4f75ad5SArd Biesheuvel 			if (*str == '/') {
583f4f75ad5SArd Biesheuvel 				*p++ = '\\';
584f4f75ad5SArd Biesheuvel 				str++;
585f4f75ad5SArd Biesheuvel 			} else {
586f4f75ad5SArd Biesheuvel 				*p++ = *str++;
587f4f75ad5SArd Biesheuvel 			}
588f4f75ad5SArd Biesheuvel 		}
589f4f75ad5SArd Biesheuvel 
590f4f75ad5SArd Biesheuvel 		*p = '\0';
591f4f75ad5SArd Biesheuvel 
592f4f75ad5SArd Biesheuvel 		/* Only open the volume once. */
593f4f75ad5SArd Biesheuvel 		if (!i) {
594c4db9c1eSLukas Wunner 			status = efi_open_volume(sys_table_arg, image, &fh);
595f4f75ad5SArd Biesheuvel 			if (status != EFI_SUCCESS)
596f4f75ad5SArd Biesheuvel 				goto free_files;
597f4f75ad5SArd Biesheuvel 		}
598f4f75ad5SArd Biesheuvel 
599f4f75ad5SArd Biesheuvel 		status = efi_file_size(sys_table_arg, fh, filename_16,
600f4f75ad5SArd Biesheuvel 				       (void **)&file->handle, &file->size);
601f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS)
602f4f75ad5SArd Biesheuvel 			goto close_handles;
603f4f75ad5SArd Biesheuvel 
604f4f75ad5SArd Biesheuvel 		file_size_total += file->size;
605f4f75ad5SArd Biesheuvel 	}
606f4f75ad5SArd Biesheuvel 
607f4f75ad5SArd Biesheuvel 	if (file_size_total) {
608f4f75ad5SArd Biesheuvel 		unsigned long addr;
609f4f75ad5SArd Biesheuvel 
610f4f75ad5SArd Biesheuvel 		/*
611f4f75ad5SArd Biesheuvel 		 * Multiple files need to be at consecutive addresses in memory,
612f4f75ad5SArd Biesheuvel 		 * so allocate enough memory for all the files.  This is used
613f4f75ad5SArd Biesheuvel 		 * for loading multiple files.
614f4f75ad5SArd Biesheuvel 		 */
615f4f75ad5SArd Biesheuvel 		status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
616f4f75ad5SArd Biesheuvel 				    &file_addr, max_addr);
617f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
618f4f75ad5SArd Biesheuvel 			pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
619f4f75ad5SArd Biesheuvel 			goto close_handles;
620f4f75ad5SArd Biesheuvel 		}
621f4f75ad5SArd Biesheuvel 
622f4f75ad5SArd Biesheuvel 		/* We've run out of free low memory. */
623f4f75ad5SArd Biesheuvel 		if (file_addr > max_addr) {
624f4f75ad5SArd Biesheuvel 			pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
625f4f75ad5SArd Biesheuvel 			status = EFI_INVALID_PARAMETER;
626f4f75ad5SArd Biesheuvel 			goto free_file_total;
627f4f75ad5SArd Biesheuvel 		}
628f4f75ad5SArd Biesheuvel 
629f4f75ad5SArd Biesheuvel 		addr = file_addr;
630f4f75ad5SArd Biesheuvel 		for (j = 0; j < nr_files; j++) {
631f4f75ad5SArd Biesheuvel 			unsigned long size;
632f4f75ad5SArd Biesheuvel 
633f4f75ad5SArd Biesheuvel 			size = files[j].size;
634f4f75ad5SArd Biesheuvel 			while (size) {
635f4f75ad5SArd Biesheuvel 				unsigned long chunksize;
636b3879a4dSArd Biesheuvel 
637b3879a4dSArd Biesheuvel 				if (IS_ENABLED(CONFIG_X86) && size > __chunk_size)
6385a17dae4SMatt Fleming 					chunksize = __chunk_size;
639f4f75ad5SArd Biesheuvel 				else
640f4f75ad5SArd Biesheuvel 					chunksize = size;
641f4f75ad5SArd Biesheuvel 
642f4f75ad5SArd Biesheuvel 				status = efi_file_read(files[j].handle,
643f4f75ad5SArd Biesheuvel 						       &chunksize,
644f4f75ad5SArd Biesheuvel 						       (void *)addr);
645f4f75ad5SArd Biesheuvel 				if (status != EFI_SUCCESS) {
646f4f75ad5SArd Biesheuvel 					pr_efi_err(sys_table_arg, "Failed to read file\n");
647f4f75ad5SArd Biesheuvel 					goto free_file_total;
648f4f75ad5SArd Biesheuvel 				}
649f4f75ad5SArd Biesheuvel 				addr += chunksize;
650f4f75ad5SArd Biesheuvel 				size -= chunksize;
651f4f75ad5SArd Biesheuvel 			}
652f4f75ad5SArd Biesheuvel 
653f4f75ad5SArd Biesheuvel 			efi_file_close(files[j].handle);
654f4f75ad5SArd Biesheuvel 		}
655f4f75ad5SArd Biesheuvel 
656f4f75ad5SArd Biesheuvel 	}
657f4f75ad5SArd Biesheuvel 
658f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, files);
659f4f75ad5SArd Biesheuvel 
660f4f75ad5SArd Biesheuvel 	*load_addr = file_addr;
661f4f75ad5SArd Biesheuvel 	*load_size = file_size_total;
662f4f75ad5SArd Biesheuvel 
663f4f75ad5SArd Biesheuvel 	return status;
664f4f75ad5SArd Biesheuvel 
665f4f75ad5SArd Biesheuvel free_file_total:
666f4f75ad5SArd Biesheuvel 	efi_free(sys_table_arg, file_size_total, file_addr);
667f4f75ad5SArd Biesheuvel 
668f4f75ad5SArd Biesheuvel close_handles:
669f4f75ad5SArd Biesheuvel 	for (k = j; k < i; k++)
670f4f75ad5SArd Biesheuvel 		efi_file_close(files[k].handle);
671f4f75ad5SArd Biesheuvel free_files:
672f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, files);
673f4f75ad5SArd Biesheuvel fail:
674f4f75ad5SArd Biesheuvel 	*load_addr = 0;
675f4f75ad5SArd Biesheuvel 	*load_size = 0;
676f4f75ad5SArd Biesheuvel 
677f4f75ad5SArd Biesheuvel 	return status;
678f4f75ad5SArd Biesheuvel }
679f4f75ad5SArd Biesheuvel /*
680f4f75ad5SArd Biesheuvel  * Relocate a kernel image, either compressed or uncompressed.
681f4f75ad5SArd Biesheuvel  * In the ARM64 case, all kernel images are currently
682f4f75ad5SArd Biesheuvel  * uncompressed, and as such when we relocate it we need to
683f4f75ad5SArd Biesheuvel  * allocate additional space for the BSS segment. Any low
684f4f75ad5SArd Biesheuvel  * memory that this function should avoid needs to be
685f4f75ad5SArd Biesheuvel  * unavailable in the EFI memory map, as if the preferred
686f4f75ad5SArd Biesheuvel  * address is not available the lowest available address will
687f4f75ad5SArd Biesheuvel  * be used.
688f4f75ad5SArd Biesheuvel  */
689f4f75ad5SArd Biesheuvel efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
690f4f75ad5SArd Biesheuvel 				 unsigned long *image_addr,
691f4f75ad5SArd Biesheuvel 				 unsigned long image_size,
692f4f75ad5SArd Biesheuvel 				 unsigned long alloc_size,
693f4f75ad5SArd Biesheuvel 				 unsigned long preferred_addr,
694f4f75ad5SArd Biesheuvel 				 unsigned long alignment)
695f4f75ad5SArd Biesheuvel {
696f4f75ad5SArd Biesheuvel 	unsigned long cur_image_addr;
697f4f75ad5SArd Biesheuvel 	unsigned long new_addr = 0;
698f4f75ad5SArd Biesheuvel 	efi_status_t status;
699f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
700f4f75ad5SArd Biesheuvel 	efi_physical_addr_t efi_addr = preferred_addr;
701f4f75ad5SArd Biesheuvel 
702f4f75ad5SArd Biesheuvel 	if (!image_addr || !image_size || !alloc_size)
703f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
704f4f75ad5SArd Biesheuvel 	if (alloc_size < image_size)
705f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
706f4f75ad5SArd Biesheuvel 
707f4f75ad5SArd Biesheuvel 	cur_image_addr = *image_addr;
708f4f75ad5SArd Biesheuvel 
709f4f75ad5SArd Biesheuvel 	/*
710f4f75ad5SArd Biesheuvel 	 * The EFI firmware loader could have placed the kernel image
711f4f75ad5SArd Biesheuvel 	 * anywhere in memory, but the kernel has restrictions on the
712f4f75ad5SArd Biesheuvel 	 * max physical address it can run at.  Some architectures
713f4f75ad5SArd Biesheuvel 	 * also have a prefered address, so first try to relocate
714f4f75ad5SArd Biesheuvel 	 * to the preferred address.  If that fails, allocate as low
715f4f75ad5SArd Biesheuvel 	 * as possible while respecting the required alignment.
716f4f75ad5SArd Biesheuvel 	 */
717cf2b0f10SArd Biesheuvel 	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
718f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pages,
719f4f75ad5SArd Biesheuvel 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
720f4f75ad5SArd Biesheuvel 				nr_pages, &efi_addr);
721f4f75ad5SArd Biesheuvel 	new_addr = efi_addr;
722f4f75ad5SArd Biesheuvel 	/*
723f4f75ad5SArd Biesheuvel 	 * If preferred address allocation failed allocate as low as
724f4f75ad5SArd Biesheuvel 	 * possible.
725f4f75ad5SArd Biesheuvel 	 */
726f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
727f4f75ad5SArd Biesheuvel 		status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
728f4f75ad5SArd Biesheuvel 				       &new_addr);
729f4f75ad5SArd Biesheuvel 	}
730f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
731f4f75ad5SArd Biesheuvel 		pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
732f4f75ad5SArd Biesheuvel 		return status;
733f4f75ad5SArd Biesheuvel 	}
734f4f75ad5SArd Biesheuvel 
735f4f75ad5SArd Biesheuvel 	/*
736f4f75ad5SArd Biesheuvel 	 * We know source/dest won't overlap since both memory ranges
737f4f75ad5SArd Biesheuvel 	 * have been allocated by UEFI, so we can safely use memcpy.
738f4f75ad5SArd Biesheuvel 	 */
739f4f75ad5SArd Biesheuvel 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
740f4f75ad5SArd Biesheuvel 
741f4f75ad5SArd Biesheuvel 	/* Return the new address of the relocated image. */
742f4f75ad5SArd Biesheuvel 	*image_addr = new_addr;
743f4f75ad5SArd Biesheuvel 
744f4f75ad5SArd Biesheuvel 	return status;
745f4f75ad5SArd Biesheuvel }
746f4f75ad5SArd Biesheuvel 
747f4f75ad5SArd Biesheuvel /*
748f4f75ad5SArd Biesheuvel  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
749f4f75ad5SArd Biesheuvel  * This overestimates for surrogates, but that is okay.
750f4f75ad5SArd Biesheuvel  */
751f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c)
752f4f75ad5SArd Biesheuvel {
753f4f75ad5SArd Biesheuvel 	return 1 + (c >= 0x80) + (c >= 0x800);
754f4f75ad5SArd Biesheuvel }
755f4f75ad5SArd Biesheuvel 
756f4f75ad5SArd Biesheuvel /*
757f4f75ad5SArd Biesheuvel  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
758f4f75ad5SArd Biesheuvel  */
759f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
760f4f75ad5SArd Biesheuvel {
761f4f75ad5SArd Biesheuvel 	unsigned int c;
762f4f75ad5SArd Biesheuvel 
763f4f75ad5SArd Biesheuvel 	while (n--) {
764f4f75ad5SArd Biesheuvel 		c = *src++;
765f4f75ad5SArd Biesheuvel 		if (n && c >= 0xd800 && c <= 0xdbff &&
766f4f75ad5SArd Biesheuvel 		    *src >= 0xdc00 && *src <= 0xdfff) {
767f4f75ad5SArd Biesheuvel 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
768f4f75ad5SArd Biesheuvel 			src++;
769f4f75ad5SArd Biesheuvel 			n--;
770f4f75ad5SArd Biesheuvel 		}
771f4f75ad5SArd Biesheuvel 		if (c >= 0xd800 && c <= 0xdfff)
772f4f75ad5SArd Biesheuvel 			c = 0xfffd; /* Unmatched surrogate */
773f4f75ad5SArd Biesheuvel 		if (c < 0x80) {
774f4f75ad5SArd Biesheuvel 			*dst++ = c;
775f4f75ad5SArd Biesheuvel 			continue;
776f4f75ad5SArd Biesheuvel 		}
777f4f75ad5SArd Biesheuvel 		if (c < 0x800) {
778f4f75ad5SArd Biesheuvel 			*dst++ = 0xc0 + (c >> 6);
779f4f75ad5SArd Biesheuvel 			goto t1;
780f4f75ad5SArd Biesheuvel 		}
781f4f75ad5SArd Biesheuvel 		if (c < 0x10000) {
782f4f75ad5SArd Biesheuvel 			*dst++ = 0xe0 + (c >> 12);
783f4f75ad5SArd Biesheuvel 			goto t2;
784f4f75ad5SArd Biesheuvel 		}
785f4f75ad5SArd Biesheuvel 		*dst++ = 0xf0 + (c >> 18);
786f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
787f4f75ad5SArd Biesheuvel 	t2:
788f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
789f4f75ad5SArd Biesheuvel 	t1:
790f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + (c & 0x3f);
791f4f75ad5SArd Biesheuvel 	}
792f4f75ad5SArd Biesheuvel 
793f4f75ad5SArd Biesheuvel 	return dst;
794f4f75ad5SArd Biesheuvel }
795f4f75ad5SArd Biesheuvel 
79648fcb2d0SArd Biesheuvel #ifndef MAX_CMDLINE_ADDRESS
79748fcb2d0SArd Biesheuvel #define MAX_CMDLINE_ADDRESS	ULONG_MAX
79848fcb2d0SArd Biesheuvel #endif
79948fcb2d0SArd Biesheuvel 
800f4f75ad5SArd Biesheuvel /*
801f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
802f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
803f4f75ad5SArd Biesheuvel  * Returns NULL on error.
804f4f75ad5SArd Biesheuvel  */
805f4f75ad5SArd Biesheuvel char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
806f4f75ad5SArd Biesheuvel 			  efi_loaded_image_t *image,
807f4f75ad5SArd Biesheuvel 			  int *cmd_line_len)
808f4f75ad5SArd Biesheuvel {
809f4f75ad5SArd Biesheuvel 	const u16 *s2;
810f4f75ad5SArd Biesheuvel 	u8 *s1 = NULL;
811f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
812f4f75ad5SArd Biesheuvel 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
813f4f75ad5SArd Biesheuvel 	const u16 *options = image->load_options;
814f4f75ad5SArd Biesheuvel 	int options_bytes = 0;  /* UTF-8 bytes */
815f4f75ad5SArd Biesheuvel 	int options_chars = 0;  /* UTF-16 chars */
816f4f75ad5SArd Biesheuvel 	efi_status_t status;
817f4f75ad5SArd Biesheuvel 	u16 zero = 0;
818f4f75ad5SArd Biesheuvel 
819f4f75ad5SArd Biesheuvel 	if (options) {
820f4f75ad5SArd Biesheuvel 		s2 = options;
821f4f75ad5SArd Biesheuvel 		while (*s2 && *s2 != '\n'
822f4f75ad5SArd Biesheuvel 		       && options_chars < load_options_chars) {
823f4f75ad5SArd Biesheuvel 			options_bytes += efi_utf8_bytes(*s2++);
824f4f75ad5SArd Biesheuvel 			options_chars++;
825f4f75ad5SArd Biesheuvel 		}
826f4f75ad5SArd Biesheuvel 	}
827f4f75ad5SArd Biesheuvel 
828f4f75ad5SArd Biesheuvel 	if (!options_chars) {
829f4f75ad5SArd Biesheuvel 		/* No command line options, so return empty string*/
830f4f75ad5SArd Biesheuvel 		options = &zero;
831f4f75ad5SArd Biesheuvel 	}
832f4f75ad5SArd Biesheuvel 
833f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
834f4f75ad5SArd Biesheuvel 
83548fcb2d0SArd Biesheuvel 	status = efi_high_alloc(sys_table_arg, options_bytes, 0,
83648fcb2d0SArd Biesheuvel 				&cmdline_addr, MAX_CMDLINE_ADDRESS);
837f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
838f4f75ad5SArd Biesheuvel 		return NULL;
839f4f75ad5SArd Biesheuvel 
840f4f75ad5SArd Biesheuvel 	s1 = (u8 *)cmdline_addr;
841f4f75ad5SArd Biesheuvel 	s2 = (const u16 *)options;
842f4f75ad5SArd Biesheuvel 
843f4f75ad5SArd Biesheuvel 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
844f4f75ad5SArd Biesheuvel 	*s1 = '\0';
845f4f75ad5SArd Biesheuvel 
846f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
847f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
848f4f75ad5SArd Biesheuvel }
849fc07716bSJeffrey Hugo 
850fc07716bSJeffrey Hugo /*
851fc07716bSJeffrey Hugo  * Handle calling ExitBootServices according to the requirements set out by the
852fc07716bSJeffrey Hugo  * spec.  Obtains the current memory map, and returns that info after calling
853fc07716bSJeffrey Hugo  * ExitBootServices.  The client must specify a function to perform any
854fc07716bSJeffrey Hugo  * processing of the memory map data prior to ExitBootServices.  A client
855fc07716bSJeffrey Hugo  * specific structure may be passed to the function via priv.  The client
856fc07716bSJeffrey Hugo  * function may be called multiple times.
857fc07716bSJeffrey Hugo  */
858fc07716bSJeffrey Hugo efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
859fc07716bSJeffrey Hugo 				    void *handle,
860fc07716bSJeffrey Hugo 				    struct efi_boot_memmap *map,
861fc07716bSJeffrey Hugo 				    void *priv,
862fc07716bSJeffrey Hugo 				    efi_exit_boot_map_processing priv_func)
863fc07716bSJeffrey Hugo {
864fc07716bSJeffrey Hugo 	efi_status_t status;
865fc07716bSJeffrey Hugo 
866fc07716bSJeffrey Hugo 	status = efi_get_memory_map(sys_table_arg, map);
867fc07716bSJeffrey Hugo 
868fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
869fc07716bSJeffrey Hugo 		goto fail;
870fc07716bSJeffrey Hugo 
871fc07716bSJeffrey Hugo 	status = priv_func(sys_table_arg, map, priv);
872fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
873fc07716bSJeffrey Hugo 		goto free_map;
874fc07716bSJeffrey Hugo 
875fc07716bSJeffrey Hugo 	status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
876fc07716bSJeffrey Hugo 
877fc07716bSJeffrey Hugo 	if (status == EFI_INVALID_PARAMETER) {
878fc07716bSJeffrey Hugo 		/*
879fc07716bSJeffrey Hugo 		 * The memory map changed between efi_get_memory_map() and
880fc07716bSJeffrey Hugo 		 * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
881fc07716bSJeffrey Hugo 		 * EFI_BOOT_SERVICES.ExitBootServices we need to get the
882fc07716bSJeffrey Hugo 		 * updated map, and try again.  The spec implies one retry
883fc07716bSJeffrey Hugo 		 * should be sufficent, which is confirmed against the EDK2
884fc07716bSJeffrey Hugo 		 * implementation.  Per the spec, we can only invoke
885fc07716bSJeffrey Hugo 		 * get_memory_map() and exit_boot_services() - we cannot alloc
886fc07716bSJeffrey Hugo 		 * so efi_get_memory_map() cannot be used, and we must reuse
887fc07716bSJeffrey Hugo 		 * the buffer.  For all practical purposes, the headroom in the
888fc07716bSJeffrey Hugo 		 * buffer should account for any changes in the map so the call
889fc07716bSJeffrey Hugo 		 * to get_memory_map() is expected to succeed here.
890fc07716bSJeffrey Hugo 		 */
891fc07716bSJeffrey Hugo 		*map->map_size = *map->buff_size;
892fc07716bSJeffrey Hugo 		status = efi_call_early(get_memory_map,
893fc07716bSJeffrey Hugo 					map->map_size,
894fc07716bSJeffrey Hugo 					*map->map,
895fc07716bSJeffrey Hugo 					map->key_ptr,
896fc07716bSJeffrey Hugo 					map->desc_size,
897fc07716bSJeffrey Hugo 					map->desc_ver);
898fc07716bSJeffrey Hugo 
899fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
900fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
901fc07716bSJeffrey Hugo 			goto fail;
902fc07716bSJeffrey Hugo 
903fc07716bSJeffrey Hugo 		status = priv_func(sys_table_arg, map, priv);
904fc07716bSJeffrey Hugo 		/* exit_boot_services() was called, thus cannot free */
905fc07716bSJeffrey Hugo 		if (status != EFI_SUCCESS)
906fc07716bSJeffrey Hugo 			goto fail;
907fc07716bSJeffrey Hugo 
908fc07716bSJeffrey Hugo 		status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
909fc07716bSJeffrey Hugo 	}
910fc07716bSJeffrey Hugo 
911fc07716bSJeffrey Hugo 	/* exit_boot_services() was called, thus cannot free */
912fc07716bSJeffrey Hugo 	if (status != EFI_SUCCESS)
913fc07716bSJeffrey Hugo 		goto fail;
914fc07716bSJeffrey Hugo 
915fc07716bSJeffrey Hugo 	return EFI_SUCCESS;
916fc07716bSJeffrey Hugo 
917fc07716bSJeffrey Hugo free_map:
918fc07716bSJeffrey Hugo 	efi_call_early(free_pool, *map->map);
919fc07716bSJeffrey Hugo fail:
920fc07716bSJeffrey Hugo 	return status;
921fc07716bSJeffrey Hugo }
922