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 
35cf2b0f10SArd Biesheuvel /*
36cf2b0f10SArd Biesheuvel  * Allow the platform to override the allocation granularity: this allows
37cf2b0f10SArd Biesheuvel  * systems that have the capability to run with a larger page size to deal
38cf2b0f10SArd Biesheuvel  * with the allocations for initrd and fdt more efficiently.
39cf2b0f10SArd Biesheuvel  */
40cf2b0f10SArd Biesheuvel #ifndef EFI_ALLOC_ALIGN
41cf2b0f10SArd Biesheuvel #define EFI_ALLOC_ALIGN		EFI_PAGE_SIZE
42cf2b0f10SArd Biesheuvel #endif
43cf2b0f10SArd Biesheuvel 
44f4f75ad5SArd Biesheuvel struct file_info {
45f4f75ad5SArd Biesheuvel 	efi_file_handle_t *handle;
46f4f75ad5SArd Biesheuvel 	u64 size;
47f4f75ad5SArd Biesheuvel };
48f4f75ad5SArd Biesheuvel 
49f4f75ad5SArd Biesheuvel void efi_printk(efi_system_table_t *sys_table_arg, char *str)
50f4f75ad5SArd Biesheuvel {
51f4f75ad5SArd Biesheuvel 	char *s8;
52f4f75ad5SArd Biesheuvel 
53f4f75ad5SArd Biesheuvel 	for (s8 = str; *s8; s8++) {
54f4f75ad5SArd Biesheuvel 		efi_char16_t ch[2] = { 0 };
55f4f75ad5SArd Biesheuvel 
56f4f75ad5SArd Biesheuvel 		ch[0] = *s8;
57f4f75ad5SArd Biesheuvel 		if (*s8 == '\n') {
58f4f75ad5SArd Biesheuvel 			efi_char16_t nl[2] = { '\r', 0 };
59f4f75ad5SArd Biesheuvel 			efi_char16_printk(sys_table_arg, nl);
60f4f75ad5SArd Biesheuvel 		}
61f4f75ad5SArd Biesheuvel 
62f4f75ad5SArd Biesheuvel 		efi_char16_printk(sys_table_arg, ch);
63f4f75ad5SArd Biesheuvel 	}
64f4f75ad5SArd Biesheuvel }
65f4f75ad5SArd Biesheuvel 
66f4f75ad5SArd Biesheuvel efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
67f4f75ad5SArd Biesheuvel 				efi_memory_desc_t **map,
68f4f75ad5SArd Biesheuvel 				unsigned long *map_size,
69f4f75ad5SArd Biesheuvel 				unsigned long *desc_size,
70f4f75ad5SArd Biesheuvel 				u32 *desc_ver,
71f4f75ad5SArd Biesheuvel 				unsigned long *key_ptr)
72f4f75ad5SArd Biesheuvel {
73f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *m = NULL;
74f4f75ad5SArd Biesheuvel 	efi_status_t status;
75f4f75ad5SArd Biesheuvel 	unsigned long key;
76f4f75ad5SArd Biesheuvel 	u32 desc_version;
77f4f75ad5SArd Biesheuvel 
78f4f75ad5SArd Biesheuvel 	*map_size = sizeof(*m) * 32;
79f4f75ad5SArd Biesheuvel again:
80f4f75ad5SArd Biesheuvel 	/*
81f4f75ad5SArd Biesheuvel 	 * Add an additional efi_memory_desc_t because we're doing an
82f4f75ad5SArd Biesheuvel 	 * allocation which may be in a new descriptor region.
83f4f75ad5SArd Biesheuvel 	 */
84f4f75ad5SArd Biesheuvel 	*map_size += sizeof(*m);
85f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
86f4f75ad5SArd Biesheuvel 				*map_size, (void **)&m);
87f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
88f4f75ad5SArd Biesheuvel 		goto fail;
89f4f75ad5SArd Biesheuvel 
90f4f75ad5SArd Biesheuvel 	*desc_size = 0;
91f4f75ad5SArd Biesheuvel 	key = 0;
92f4f75ad5SArd Biesheuvel 	status = efi_call_early(get_memory_map, map_size, m,
93f4f75ad5SArd Biesheuvel 				&key, desc_size, &desc_version);
94f4f75ad5SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL) {
95f4f75ad5SArd Biesheuvel 		efi_call_early(free_pool, m);
96f4f75ad5SArd Biesheuvel 		goto again;
97f4f75ad5SArd Biesheuvel 	}
98f4f75ad5SArd Biesheuvel 
99f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
100f4f75ad5SArd Biesheuvel 		efi_call_early(free_pool, m);
101f4f75ad5SArd Biesheuvel 
102f4f75ad5SArd Biesheuvel 	if (key_ptr && status == EFI_SUCCESS)
103f4f75ad5SArd Biesheuvel 		*key_ptr = key;
104f4f75ad5SArd Biesheuvel 	if (desc_ver && status == EFI_SUCCESS)
105f4f75ad5SArd Biesheuvel 		*desc_ver = desc_version;
106f4f75ad5SArd Biesheuvel 
107f4f75ad5SArd Biesheuvel fail:
108f4f75ad5SArd Biesheuvel 	*map = m;
109f4f75ad5SArd Biesheuvel 	return status;
110f4f75ad5SArd Biesheuvel }
111f4f75ad5SArd Biesheuvel 
112f4f75ad5SArd Biesheuvel 
113f4f75ad5SArd Biesheuvel unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
114f4f75ad5SArd Biesheuvel {
115f4f75ad5SArd Biesheuvel 	efi_status_t status;
116f4f75ad5SArd Biesheuvel 	unsigned long map_size;
117f4f75ad5SArd Biesheuvel 	unsigned long membase  = EFI_ERROR;
118f4f75ad5SArd Biesheuvel 	struct efi_memory_map map;
119f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *md;
120f4f75ad5SArd Biesheuvel 
121f4f75ad5SArd Biesheuvel 	status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
122f4f75ad5SArd Biesheuvel 				    &map_size, &map.desc_size, NULL, NULL);
123f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
124f4f75ad5SArd Biesheuvel 		return membase;
125f4f75ad5SArd Biesheuvel 
126f4f75ad5SArd Biesheuvel 	map.map_end = map.map + map_size;
127f4f75ad5SArd Biesheuvel 
128f4f75ad5SArd Biesheuvel 	for_each_efi_memory_desc(&map, md)
129f4f75ad5SArd Biesheuvel 		if (md->attribute & EFI_MEMORY_WB)
130f4f75ad5SArd Biesheuvel 			if (membase > md->phys_addr)
131f4f75ad5SArd Biesheuvel 				membase = md->phys_addr;
132f4f75ad5SArd Biesheuvel 
133f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map.map);
134f4f75ad5SArd Biesheuvel 
135f4f75ad5SArd Biesheuvel 	return membase;
136f4f75ad5SArd Biesheuvel }
137f4f75ad5SArd Biesheuvel 
138f4f75ad5SArd Biesheuvel /*
139f4f75ad5SArd Biesheuvel  * Allocate at the highest possible address that is not above 'max'.
140f4f75ad5SArd Biesheuvel  */
141f4f75ad5SArd Biesheuvel efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
142f4f75ad5SArd Biesheuvel 			    unsigned long size, unsigned long align,
143f4f75ad5SArd Biesheuvel 			    unsigned long *addr, unsigned long max)
144f4f75ad5SArd Biesheuvel {
145f4f75ad5SArd Biesheuvel 	unsigned long map_size, desc_size;
146f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
147f4f75ad5SArd Biesheuvel 	efi_status_t status;
148f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
149f4f75ad5SArd Biesheuvel 	u64 max_addr = 0;
150f4f75ad5SArd Biesheuvel 	int i;
151f4f75ad5SArd Biesheuvel 
152f4f75ad5SArd Biesheuvel 	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
153f4f75ad5SArd Biesheuvel 				    NULL, NULL);
154f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
155f4f75ad5SArd Biesheuvel 		goto fail;
156f4f75ad5SArd Biesheuvel 
157f4f75ad5SArd Biesheuvel 	/*
158f4f75ad5SArd Biesheuvel 	 * Enforce minimum alignment that EFI requires when requesting
159f4f75ad5SArd Biesheuvel 	 * a specific address.  We are doing page-based allocations,
160f4f75ad5SArd Biesheuvel 	 * so we must be aligned to a page.
161f4f75ad5SArd Biesheuvel 	 */
162cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
163cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
164f4f75ad5SArd Biesheuvel 
165cf2b0f10SArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
166f4f75ad5SArd Biesheuvel again:
167f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
168f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
169f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
170f4f75ad5SArd Biesheuvel 		u64 start, end;
171f4f75ad5SArd Biesheuvel 
172f4f75ad5SArd Biesheuvel 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
173f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
174f4f75ad5SArd Biesheuvel 			continue;
175f4f75ad5SArd Biesheuvel 
176f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
177f4f75ad5SArd Biesheuvel 			continue;
178f4f75ad5SArd Biesheuvel 
179f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
180f4f75ad5SArd Biesheuvel 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
181f4f75ad5SArd Biesheuvel 
182f4f75ad5SArd Biesheuvel 		if ((start + size) > end || (start + size) > max)
183f4f75ad5SArd Biesheuvel 			continue;
184f4f75ad5SArd Biesheuvel 
185f4f75ad5SArd Biesheuvel 		if (end - size > max)
186f4f75ad5SArd Biesheuvel 			end = max;
187f4f75ad5SArd Biesheuvel 
188f4f75ad5SArd Biesheuvel 		if (round_down(end - size, align) < start)
189f4f75ad5SArd Biesheuvel 			continue;
190f4f75ad5SArd Biesheuvel 
191f4f75ad5SArd Biesheuvel 		start = round_down(end - size, align);
192f4f75ad5SArd Biesheuvel 
193f4f75ad5SArd Biesheuvel 		/*
194f4f75ad5SArd Biesheuvel 		 * Don't allocate at 0x0. It will confuse code that
195f4f75ad5SArd Biesheuvel 		 * checks pointers against NULL.
196f4f75ad5SArd Biesheuvel 		 */
197f4f75ad5SArd Biesheuvel 		if (start == 0x0)
198f4f75ad5SArd Biesheuvel 			continue;
199f4f75ad5SArd Biesheuvel 
200f4f75ad5SArd Biesheuvel 		if (start > max_addr)
201f4f75ad5SArd Biesheuvel 			max_addr = start;
202f4f75ad5SArd Biesheuvel 	}
203f4f75ad5SArd Biesheuvel 
204f4f75ad5SArd Biesheuvel 	if (!max_addr)
205f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
206f4f75ad5SArd Biesheuvel 	else {
207f4f75ad5SArd Biesheuvel 		status = efi_call_early(allocate_pages,
208f4f75ad5SArd Biesheuvel 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
209f4f75ad5SArd Biesheuvel 					nr_pages, &max_addr);
210f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
211f4f75ad5SArd Biesheuvel 			max = max_addr;
212f4f75ad5SArd Biesheuvel 			max_addr = 0;
213f4f75ad5SArd Biesheuvel 			goto again;
214f4f75ad5SArd Biesheuvel 		}
215f4f75ad5SArd Biesheuvel 
216f4f75ad5SArd Biesheuvel 		*addr = max_addr;
217f4f75ad5SArd Biesheuvel 	}
218f4f75ad5SArd Biesheuvel 
219f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map);
220f4f75ad5SArd Biesheuvel fail:
221f4f75ad5SArd Biesheuvel 	return status;
222f4f75ad5SArd Biesheuvel }
223f4f75ad5SArd Biesheuvel 
224f4f75ad5SArd Biesheuvel /*
225f4f75ad5SArd Biesheuvel  * Allocate at the lowest possible address.
226f4f75ad5SArd Biesheuvel  */
227f4f75ad5SArd Biesheuvel efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
228f4f75ad5SArd Biesheuvel 			   unsigned long size, unsigned long align,
229f4f75ad5SArd Biesheuvel 			   unsigned long *addr)
230f4f75ad5SArd Biesheuvel {
231f4f75ad5SArd Biesheuvel 	unsigned long map_size, desc_size;
232f4f75ad5SArd Biesheuvel 	efi_memory_desc_t *map;
233f4f75ad5SArd Biesheuvel 	efi_status_t status;
234f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
235f4f75ad5SArd Biesheuvel 	int i;
236f4f75ad5SArd Biesheuvel 
237f4f75ad5SArd Biesheuvel 	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
238f4f75ad5SArd Biesheuvel 				    NULL, NULL);
239f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
240f4f75ad5SArd Biesheuvel 		goto fail;
241f4f75ad5SArd Biesheuvel 
242f4f75ad5SArd Biesheuvel 	/*
243f4f75ad5SArd Biesheuvel 	 * Enforce minimum alignment that EFI requires when requesting
244f4f75ad5SArd Biesheuvel 	 * a specific address.  We are doing page-based allocations,
245f4f75ad5SArd Biesheuvel 	 * so we must be aligned to a page.
246f4f75ad5SArd Biesheuvel 	 */
247cf2b0f10SArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
248cf2b0f10SArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
249f4f75ad5SArd Biesheuvel 
250cf2b0f10SArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
251f4f75ad5SArd Biesheuvel 	for (i = 0; i < map_size / desc_size; i++) {
252f4f75ad5SArd Biesheuvel 		efi_memory_desc_t *desc;
253f4f75ad5SArd Biesheuvel 		unsigned long m = (unsigned long)map;
254f4f75ad5SArd Biesheuvel 		u64 start, end;
255f4f75ad5SArd Biesheuvel 
256f4f75ad5SArd Biesheuvel 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
257f4f75ad5SArd Biesheuvel 
258f4f75ad5SArd Biesheuvel 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
259f4f75ad5SArd Biesheuvel 			continue;
260f4f75ad5SArd Biesheuvel 
261f4f75ad5SArd Biesheuvel 		if (desc->num_pages < nr_pages)
262f4f75ad5SArd Biesheuvel 			continue;
263f4f75ad5SArd Biesheuvel 
264f4f75ad5SArd Biesheuvel 		start = desc->phys_addr;
265f4f75ad5SArd Biesheuvel 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
266f4f75ad5SArd Biesheuvel 
267f4f75ad5SArd Biesheuvel 		/*
268f4f75ad5SArd Biesheuvel 		 * Don't allocate at 0x0. It will confuse code that
269f4f75ad5SArd Biesheuvel 		 * checks pointers against NULL. Skip the first 8
270f4f75ad5SArd Biesheuvel 		 * bytes so we start at a nice even number.
271f4f75ad5SArd Biesheuvel 		 */
272f4f75ad5SArd Biesheuvel 		if (start == 0x0)
273f4f75ad5SArd Biesheuvel 			start += 8;
274f4f75ad5SArd Biesheuvel 
275f4f75ad5SArd Biesheuvel 		start = round_up(start, align);
276f4f75ad5SArd Biesheuvel 		if ((start + size) > end)
277f4f75ad5SArd Biesheuvel 			continue;
278f4f75ad5SArd Biesheuvel 
279f4f75ad5SArd Biesheuvel 		status = efi_call_early(allocate_pages,
280f4f75ad5SArd Biesheuvel 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
281f4f75ad5SArd Biesheuvel 					nr_pages, &start);
282f4f75ad5SArd Biesheuvel 		if (status == EFI_SUCCESS) {
283f4f75ad5SArd Biesheuvel 			*addr = start;
284f4f75ad5SArd Biesheuvel 			break;
285f4f75ad5SArd Biesheuvel 		}
286f4f75ad5SArd Biesheuvel 	}
287f4f75ad5SArd Biesheuvel 
288f4f75ad5SArd Biesheuvel 	if (i == map_size / desc_size)
289f4f75ad5SArd Biesheuvel 		status = EFI_NOT_FOUND;
290f4f75ad5SArd Biesheuvel 
291f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, map);
292f4f75ad5SArd Biesheuvel fail:
293f4f75ad5SArd Biesheuvel 	return status;
294f4f75ad5SArd Biesheuvel }
295f4f75ad5SArd Biesheuvel 
296f4f75ad5SArd Biesheuvel void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
297f4f75ad5SArd Biesheuvel 	      unsigned long addr)
298f4f75ad5SArd Biesheuvel {
299f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
300f4f75ad5SArd Biesheuvel 
301f4f75ad5SArd Biesheuvel 	if (!size)
302f4f75ad5SArd Biesheuvel 		return;
303f4f75ad5SArd Biesheuvel 
304cf2b0f10SArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
305f4f75ad5SArd Biesheuvel 	efi_call_early(free_pages, addr, nr_pages);
306f4f75ad5SArd Biesheuvel }
307f4f75ad5SArd Biesheuvel 
3085a17dae4SMatt Fleming /*
3095a17dae4SMatt Fleming  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
3105a17dae4SMatt Fleming  * option, e.g. efi=nochunk.
3115a17dae4SMatt Fleming  *
3125a17dae4SMatt Fleming  * It should be noted that efi= is parsed in two very different
3135a17dae4SMatt Fleming  * environments, first in the early boot environment of the EFI boot
3145a17dae4SMatt Fleming  * stub, and subsequently during the kernel boot.
3155a17dae4SMatt Fleming  */
3165a17dae4SMatt Fleming efi_status_t efi_parse_options(char *cmdline)
3175a17dae4SMatt Fleming {
3185a17dae4SMatt Fleming 	char *str;
3195a17dae4SMatt Fleming 
3205a17dae4SMatt Fleming 	/*
3215a17dae4SMatt Fleming 	 * If no EFI parameters were specified on the cmdline we've got
3225a17dae4SMatt Fleming 	 * nothing to do.
3235a17dae4SMatt Fleming 	 */
3245a17dae4SMatt Fleming 	str = strstr(cmdline, "efi=");
3255a17dae4SMatt Fleming 	if (!str)
3265a17dae4SMatt Fleming 		return EFI_SUCCESS;
3275a17dae4SMatt Fleming 
3285a17dae4SMatt Fleming 	/* Skip ahead to first argument */
3295a17dae4SMatt Fleming 	str += strlen("efi=");
3305a17dae4SMatt Fleming 
3315a17dae4SMatt Fleming 	/*
3325a17dae4SMatt Fleming 	 * Remember, because efi= is also used by the kernel we need to
3335a17dae4SMatt Fleming 	 * skip over arguments we don't understand.
3345a17dae4SMatt Fleming 	 */
3355a17dae4SMatt Fleming 	while (*str) {
3365a17dae4SMatt Fleming 		if (!strncmp(str, "nochunk", 7)) {
3375a17dae4SMatt Fleming 			str += strlen("nochunk");
3385a17dae4SMatt Fleming 			__chunk_size = -1UL;
3395a17dae4SMatt Fleming 		}
3405a17dae4SMatt Fleming 
3415a17dae4SMatt Fleming 		/* Group words together, delimited by "," */
3425a17dae4SMatt Fleming 		while (*str && *str != ',')
3435a17dae4SMatt Fleming 			str++;
3445a17dae4SMatt Fleming 
3455a17dae4SMatt Fleming 		if (*str == ',')
3465a17dae4SMatt Fleming 			str++;
3475a17dae4SMatt Fleming 	}
3485a17dae4SMatt Fleming 
3495a17dae4SMatt Fleming 	return EFI_SUCCESS;
3505a17dae4SMatt Fleming }
351f4f75ad5SArd Biesheuvel 
352f4f75ad5SArd Biesheuvel /*
353f4f75ad5SArd Biesheuvel  * Check the cmdline for a LILO-style file= arguments.
354f4f75ad5SArd Biesheuvel  *
355f4f75ad5SArd Biesheuvel  * We only support loading a file from the same filesystem as
356f4f75ad5SArd Biesheuvel  * the kernel image.
357f4f75ad5SArd Biesheuvel  */
358f4f75ad5SArd Biesheuvel efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
359f4f75ad5SArd Biesheuvel 				  efi_loaded_image_t *image,
360f4f75ad5SArd Biesheuvel 				  char *cmd_line, char *option_string,
361f4f75ad5SArd Biesheuvel 				  unsigned long max_addr,
362f4f75ad5SArd Biesheuvel 				  unsigned long *load_addr,
363f4f75ad5SArd Biesheuvel 				  unsigned long *load_size)
364f4f75ad5SArd Biesheuvel {
365f4f75ad5SArd Biesheuvel 	struct file_info *files;
366f4f75ad5SArd Biesheuvel 	unsigned long file_addr;
367f4f75ad5SArd Biesheuvel 	u64 file_size_total;
368f4f75ad5SArd Biesheuvel 	efi_file_handle_t *fh = NULL;
369f4f75ad5SArd Biesheuvel 	efi_status_t status;
370f4f75ad5SArd Biesheuvel 	int nr_files;
371f4f75ad5SArd Biesheuvel 	char *str;
372f4f75ad5SArd Biesheuvel 	int i, j, k;
373f4f75ad5SArd Biesheuvel 
374f4f75ad5SArd Biesheuvel 	file_addr = 0;
375f4f75ad5SArd Biesheuvel 	file_size_total = 0;
376f4f75ad5SArd Biesheuvel 
377f4f75ad5SArd Biesheuvel 	str = cmd_line;
378f4f75ad5SArd Biesheuvel 
379f4f75ad5SArd Biesheuvel 	j = 0;			/* See close_handles */
380f4f75ad5SArd Biesheuvel 
381f4f75ad5SArd Biesheuvel 	if (!load_addr || !load_size)
382f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
383f4f75ad5SArd Biesheuvel 
384f4f75ad5SArd Biesheuvel 	*load_addr = 0;
385f4f75ad5SArd Biesheuvel 	*load_size = 0;
386f4f75ad5SArd Biesheuvel 
387f4f75ad5SArd Biesheuvel 	if (!str || !*str)
388f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
389f4f75ad5SArd Biesheuvel 
390f4f75ad5SArd Biesheuvel 	for (nr_files = 0; *str; nr_files++) {
391f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
392f4f75ad5SArd Biesheuvel 		if (!str)
393f4f75ad5SArd Biesheuvel 			break;
394f4f75ad5SArd Biesheuvel 
395f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
396f4f75ad5SArd Biesheuvel 
397f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
398f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
399f4f75ad5SArd Biesheuvel 			str++;
400f4f75ad5SArd Biesheuvel 
401f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n')
402f4f75ad5SArd Biesheuvel 			str++;
403f4f75ad5SArd Biesheuvel 	}
404f4f75ad5SArd Biesheuvel 
405f4f75ad5SArd Biesheuvel 	if (!nr_files)
406f4f75ad5SArd Biesheuvel 		return EFI_SUCCESS;
407f4f75ad5SArd Biesheuvel 
408f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
409f4f75ad5SArd Biesheuvel 				nr_files * sizeof(*files), (void **)&files);
410f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
411f4f75ad5SArd Biesheuvel 		pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
412f4f75ad5SArd Biesheuvel 		goto fail;
413f4f75ad5SArd Biesheuvel 	}
414f4f75ad5SArd Biesheuvel 
415f4f75ad5SArd Biesheuvel 	str = cmd_line;
416f4f75ad5SArd Biesheuvel 	for (i = 0; i < nr_files; i++) {
417f4f75ad5SArd Biesheuvel 		struct file_info *file;
418f4f75ad5SArd Biesheuvel 		efi_char16_t filename_16[256];
419f4f75ad5SArd Biesheuvel 		efi_char16_t *p;
420f4f75ad5SArd Biesheuvel 
421f4f75ad5SArd Biesheuvel 		str = strstr(str, option_string);
422f4f75ad5SArd Biesheuvel 		if (!str)
423f4f75ad5SArd Biesheuvel 			break;
424f4f75ad5SArd Biesheuvel 
425f4f75ad5SArd Biesheuvel 		str += strlen(option_string);
426f4f75ad5SArd Biesheuvel 
427f4f75ad5SArd Biesheuvel 		file = &files[i];
428f4f75ad5SArd Biesheuvel 		p = filename_16;
429f4f75ad5SArd Biesheuvel 
430f4f75ad5SArd Biesheuvel 		/* Skip any leading slashes */
431f4f75ad5SArd Biesheuvel 		while (*str == '/' || *str == '\\')
432f4f75ad5SArd Biesheuvel 			str++;
433f4f75ad5SArd Biesheuvel 
434f4f75ad5SArd Biesheuvel 		while (*str && *str != ' ' && *str != '\n') {
435f4f75ad5SArd Biesheuvel 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
436f4f75ad5SArd Biesheuvel 				break;
437f4f75ad5SArd Biesheuvel 
438f4f75ad5SArd Biesheuvel 			if (*str == '/') {
439f4f75ad5SArd Biesheuvel 				*p++ = '\\';
440f4f75ad5SArd Biesheuvel 				str++;
441f4f75ad5SArd Biesheuvel 			} else {
442f4f75ad5SArd Biesheuvel 				*p++ = *str++;
443f4f75ad5SArd Biesheuvel 			}
444f4f75ad5SArd Biesheuvel 		}
445f4f75ad5SArd Biesheuvel 
446f4f75ad5SArd Biesheuvel 		*p = '\0';
447f4f75ad5SArd Biesheuvel 
448f4f75ad5SArd Biesheuvel 		/* Only open the volume once. */
449f4f75ad5SArd Biesheuvel 		if (!i) {
450f4f75ad5SArd Biesheuvel 			status = efi_open_volume(sys_table_arg, image,
451f4f75ad5SArd Biesheuvel 						 (void **)&fh);
452f4f75ad5SArd Biesheuvel 			if (status != EFI_SUCCESS)
453f4f75ad5SArd Biesheuvel 				goto free_files;
454f4f75ad5SArd Biesheuvel 		}
455f4f75ad5SArd Biesheuvel 
456f4f75ad5SArd Biesheuvel 		status = efi_file_size(sys_table_arg, fh, filename_16,
457f4f75ad5SArd Biesheuvel 				       (void **)&file->handle, &file->size);
458f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS)
459f4f75ad5SArd Biesheuvel 			goto close_handles;
460f4f75ad5SArd Biesheuvel 
461f4f75ad5SArd Biesheuvel 		file_size_total += file->size;
462f4f75ad5SArd Biesheuvel 	}
463f4f75ad5SArd Biesheuvel 
464f4f75ad5SArd Biesheuvel 	if (file_size_total) {
465f4f75ad5SArd Biesheuvel 		unsigned long addr;
466f4f75ad5SArd Biesheuvel 
467f4f75ad5SArd Biesheuvel 		/*
468f4f75ad5SArd Biesheuvel 		 * Multiple files need to be at consecutive addresses in memory,
469f4f75ad5SArd Biesheuvel 		 * so allocate enough memory for all the files.  This is used
470f4f75ad5SArd Biesheuvel 		 * for loading multiple files.
471f4f75ad5SArd Biesheuvel 		 */
472f4f75ad5SArd Biesheuvel 		status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
473f4f75ad5SArd Biesheuvel 				    &file_addr, max_addr);
474f4f75ad5SArd Biesheuvel 		if (status != EFI_SUCCESS) {
475f4f75ad5SArd Biesheuvel 			pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
476f4f75ad5SArd Biesheuvel 			goto close_handles;
477f4f75ad5SArd Biesheuvel 		}
478f4f75ad5SArd Biesheuvel 
479f4f75ad5SArd Biesheuvel 		/* We've run out of free low memory. */
480f4f75ad5SArd Biesheuvel 		if (file_addr > max_addr) {
481f4f75ad5SArd Biesheuvel 			pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
482f4f75ad5SArd Biesheuvel 			status = EFI_INVALID_PARAMETER;
483f4f75ad5SArd Biesheuvel 			goto free_file_total;
484f4f75ad5SArd Biesheuvel 		}
485f4f75ad5SArd Biesheuvel 
486f4f75ad5SArd Biesheuvel 		addr = file_addr;
487f4f75ad5SArd Biesheuvel 		for (j = 0; j < nr_files; j++) {
488f4f75ad5SArd Biesheuvel 			unsigned long size;
489f4f75ad5SArd Biesheuvel 
490f4f75ad5SArd Biesheuvel 			size = files[j].size;
491f4f75ad5SArd Biesheuvel 			while (size) {
492f4f75ad5SArd Biesheuvel 				unsigned long chunksize;
4935a17dae4SMatt Fleming 				if (size > __chunk_size)
4945a17dae4SMatt Fleming 					chunksize = __chunk_size;
495f4f75ad5SArd Biesheuvel 				else
496f4f75ad5SArd Biesheuvel 					chunksize = size;
497f4f75ad5SArd Biesheuvel 
498f4f75ad5SArd Biesheuvel 				status = efi_file_read(files[j].handle,
499f4f75ad5SArd Biesheuvel 						       &chunksize,
500f4f75ad5SArd Biesheuvel 						       (void *)addr);
501f4f75ad5SArd Biesheuvel 				if (status != EFI_SUCCESS) {
502f4f75ad5SArd Biesheuvel 					pr_efi_err(sys_table_arg, "Failed to read file\n");
503f4f75ad5SArd Biesheuvel 					goto free_file_total;
504f4f75ad5SArd Biesheuvel 				}
505f4f75ad5SArd Biesheuvel 				addr += chunksize;
506f4f75ad5SArd Biesheuvel 				size -= chunksize;
507f4f75ad5SArd Biesheuvel 			}
508f4f75ad5SArd Biesheuvel 
509f4f75ad5SArd Biesheuvel 			efi_file_close(files[j].handle);
510f4f75ad5SArd Biesheuvel 		}
511f4f75ad5SArd Biesheuvel 
512f4f75ad5SArd Biesheuvel 	}
513f4f75ad5SArd Biesheuvel 
514f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, files);
515f4f75ad5SArd Biesheuvel 
516f4f75ad5SArd Biesheuvel 	*load_addr = file_addr;
517f4f75ad5SArd Biesheuvel 	*load_size = file_size_total;
518f4f75ad5SArd Biesheuvel 
519f4f75ad5SArd Biesheuvel 	return status;
520f4f75ad5SArd Biesheuvel 
521f4f75ad5SArd Biesheuvel free_file_total:
522f4f75ad5SArd Biesheuvel 	efi_free(sys_table_arg, file_size_total, file_addr);
523f4f75ad5SArd Biesheuvel 
524f4f75ad5SArd Biesheuvel close_handles:
525f4f75ad5SArd Biesheuvel 	for (k = j; k < i; k++)
526f4f75ad5SArd Biesheuvel 		efi_file_close(files[k].handle);
527f4f75ad5SArd Biesheuvel free_files:
528f4f75ad5SArd Biesheuvel 	efi_call_early(free_pool, files);
529f4f75ad5SArd Biesheuvel fail:
530f4f75ad5SArd Biesheuvel 	*load_addr = 0;
531f4f75ad5SArd Biesheuvel 	*load_size = 0;
532f4f75ad5SArd Biesheuvel 
533f4f75ad5SArd Biesheuvel 	return status;
534f4f75ad5SArd Biesheuvel }
535f4f75ad5SArd Biesheuvel /*
536f4f75ad5SArd Biesheuvel  * Relocate a kernel image, either compressed or uncompressed.
537f4f75ad5SArd Biesheuvel  * In the ARM64 case, all kernel images are currently
538f4f75ad5SArd Biesheuvel  * uncompressed, and as such when we relocate it we need to
539f4f75ad5SArd Biesheuvel  * allocate additional space for the BSS segment. Any low
540f4f75ad5SArd Biesheuvel  * memory that this function should avoid needs to be
541f4f75ad5SArd Biesheuvel  * unavailable in the EFI memory map, as if the preferred
542f4f75ad5SArd Biesheuvel  * address is not available the lowest available address will
543f4f75ad5SArd Biesheuvel  * be used.
544f4f75ad5SArd Biesheuvel  */
545f4f75ad5SArd Biesheuvel efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
546f4f75ad5SArd Biesheuvel 				 unsigned long *image_addr,
547f4f75ad5SArd Biesheuvel 				 unsigned long image_size,
548f4f75ad5SArd Biesheuvel 				 unsigned long alloc_size,
549f4f75ad5SArd Biesheuvel 				 unsigned long preferred_addr,
550f4f75ad5SArd Biesheuvel 				 unsigned long alignment)
551f4f75ad5SArd Biesheuvel {
552f4f75ad5SArd Biesheuvel 	unsigned long cur_image_addr;
553f4f75ad5SArd Biesheuvel 	unsigned long new_addr = 0;
554f4f75ad5SArd Biesheuvel 	efi_status_t status;
555f4f75ad5SArd Biesheuvel 	unsigned long nr_pages;
556f4f75ad5SArd Biesheuvel 	efi_physical_addr_t efi_addr = preferred_addr;
557f4f75ad5SArd Biesheuvel 
558f4f75ad5SArd Biesheuvel 	if (!image_addr || !image_size || !alloc_size)
559f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
560f4f75ad5SArd Biesheuvel 	if (alloc_size < image_size)
561f4f75ad5SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
562f4f75ad5SArd Biesheuvel 
563f4f75ad5SArd Biesheuvel 	cur_image_addr = *image_addr;
564f4f75ad5SArd Biesheuvel 
565f4f75ad5SArd Biesheuvel 	/*
566f4f75ad5SArd Biesheuvel 	 * The EFI firmware loader could have placed the kernel image
567f4f75ad5SArd Biesheuvel 	 * anywhere in memory, but the kernel has restrictions on the
568f4f75ad5SArd Biesheuvel 	 * max physical address it can run at.  Some architectures
569f4f75ad5SArd Biesheuvel 	 * also have a prefered address, so first try to relocate
570f4f75ad5SArd Biesheuvel 	 * to the preferred address.  If that fails, allocate as low
571f4f75ad5SArd Biesheuvel 	 * as possible while respecting the required alignment.
572f4f75ad5SArd Biesheuvel 	 */
573cf2b0f10SArd Biesheuvel 	nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
574f4f75ad5SArd Biesheuvel 	status = efi_call_early(allocate_pages,
575f4f75ad5SArd Biesheuvel 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
576f4f75ad5SArd Biesheuvel 				nr_pages, &efi_addr);
577f4f75ad5SArd Biesheuvel 	new_addr = efi_addr;
578f4f75ad5SArd Biesheuvel 	/*
579f4f75ad5SArd Biesheuvel 	 * If preferred address allocation failed allocate as low as
580f4f75ad5SArd Biesheuvel 	 * possible.
581f4f75ad5SArd Biesheuvel 	 */
582f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
583f4f75ad5SArd Biesheuvel 		status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
584f4f75ad5SArd Biesheuvel 				       &new_addr);
585f4f75ad5SArd Biesheuvel 	}
586f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS) {
587f4f75ad5SArd Biesheuvel 		pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
588f4f75ad5SArd Biesheuvel 		return status;
589f4f75ad5SArd Biesheuvel 	}
590f4f75ad5SArd Biesheuvel 
591f4f75ad5SArd Biesheuvel 	/*
592f4f75ad5SArd Biesheuvel 	 * We know source/dest won't overlap since both memory ranges
593f4f75ad5SArd Biesheuvel 	 * have been allocated by UEFI, so we can safely use memcpy.
594f4f75ad5SArd Biesheuvel 	 */
595f4f75ad5SArd Biesheuvel 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
596f4f75ad5SArd Biesheuvel 
597f4f75ad5SArd Biesheuvel 	/* Return the new address of the relocated image. */
598f4f75ad5SArd Biesheuvel 	*image_addr = new_addr;
599f4f75ad5SArd Biesheuvel 
600f4f75ad5SArd Biesheuvel 	return status;
601f4f75ad5SArd Biesheuvel }
602f4f75ad5SArd Biesheuvel 
603f4f75ad5SArd Biesheuvel /*
604f4f75ad5SArd Biesheuvel  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
605f4f75ad5SArd Biesheuvel  * This overestimates for surrogates, but that is okay.
606f4f75ad5SArd Biesheuvel  */
607f4f75ad5SArd Biesheuvel static int efi_utf8_bytes(u16 c)
608f4f75ad5SArd Biesheuvel {
609f4f75ad5SArd Biesheuvel 	return 1 + (c >= 0x80) + (c >= 0x800);
610f4f75ad5SArd Biesheuvel }
611f4f75ad5SArd Biesheuvel 
612f4f75ad5SArd Biesheuvel /*
613f4f75ad5SArd Biesheuvel  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
614f4f75ad5SArd Biesheuvel  */
615f4f75ad5SArd Biesheuvel static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
616f4f75ad5SArd Biesheuvel {
617f4f75ad5SArd Biesheuvel 	unsigned int c;
618f4f75ad5SArd Biesheuvel 
619f4f75ad5SArd Biesheuvel 	while (n--) {
620f4f75ad5SArd Biesheuvel 		c = *src++;
621f4f75ad5SArd Biesheuvel 		if (n && c >= 0xd800 && c <= 0xdbff &&
622f4f75ad5SArd Biesheuvel 		    *src >= 0xdc00 && *src <= 0xdfff) {
623f4f75ad5SArd Biesheuvel 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
624f4f75ad5SArd Biesheuvel 			src++;
625f4f75ad5SArd Biesheuvel 			n--;
626f4f75ad5SArd Biesheuvel 		}
627f4f75ad5SArd Biesheuvel 		if (c >= 0xd800 && c <= 0xdfff)
628f4f75ad5SArd Biesheuvel 			c = 0xfffd; /* Unmatched surrogate */
629f4f75ad5SArd Biesheuvel 		if (c < 0x80) {
630f4f75ad5SArd Biesheuvel 			*dst++ = c;
631f4f75ad5SArd Biesheuvel 			continue;
632f4f75ad5SArd Biesheuvel 		}
633f4f75ad5SArd Biesheuvel 		if (c < 0x800) {
634f4f75ad5SArd Biesheuvel 			*dst++ = 0xc0 + (c >> 6);
635f4f75ad5SArd Biesheuvel 			goto t1;
636f4f75ad5SArd Biesheuvel 		}
637f4f75ad5SArd Biesheuvel 		if (c < 0x10000) {
638f4f75ad5SArd Biesheuvel 			*dst++ = 0xe0 + (c >> 12);
639f4f75ad5SArd Biesheuvel 			goto t2;
640f4f75ad5SArd Biesheuvel 		}
641f4f75ad5SArd Biesheuvel 		*dst++ = 0xf0 + (c >> 18);
642f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
643f4f75ad5SArd Biesheuvel 	t2:
644f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
645f4f75ad5SArd Biesheuvel 	t1:
646f4f75ad5SArd Biesheuvel 		*dst++ = 0x80 + (c & 0x3f);
647f4f75ad5SArd Biesheuvel 	}
648f4f75ad5SArd Biesheuvel 
649f4f75ad5SArd Biesheuvel 	return dst;
650f4f75ad5SArd Biesheuvel }
651f4f75ad5SArd Biesheuvel 
652f4f75ad5SArd Biesheuvel /*
653f4f75ad5SArd Biesheuvel  * Convert the unicode UEFI command line to ASCII to pass to kernel.
654f4f75ad5SArd Biesheuvel  * Size of memory allocated return in *cmd_line_len.
655f4f75ad5SArd Biesheuvel  * Returns NULL on error.
656f4f75ad5SArd Biesheuvel  */
657f4f75ad5SArd Biesheuvel char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
658f4f75ad5SArd Biesheuvel 			  efi_loaded_image_t *image,
659f4f75ad5SArd Biesheuvel 			  int *cmd_line_len)
660f4f75ad5SArd Biesheuvel {
661f4f75ad5SArd Biesheuvel 	const u16 *s2;
662f4f75ad5SArd Biesheuvel 	u8 *s1 = NULL;
663f4f75ad5SArd Biesheuvel 	unsigned long cmdline_addr = 0;
664f4f75ad5SArd Biesheuvel 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
665f4f75ad5SArd Biesheuvel 	const u16 *options = image->load_options;
666f4f75ad5SArd Biesheuvel 	int options_bytes = 0;  /* UTF-8 bytes */
667f4f75ad5SArd Biesheuvel 	int options_chars = 0;  /* UTF-16 chars */
668f4f75ad5SArd Biesheuvel 	efi_status_t status;
669f4f75ad5SArd Biesheuvel 	u16 zero = 0;
670f4f75ad5SArd Biesheuvel 
671f4f75ad5SArd Biesheuvel 	if (options) {
672f4f75ad5SArd Biesheuvel 		s2 = options;
673f4f75ad5SArd Biesheuvel 		while (*s2 && *s2 != '\n'
674f4f75ad5SArd Biesheuvel 		       && options_chars < load_options_chars) {
675f4f75ad5SArd Biesheuvel 			options_bytes += efi_utf8_bytes(*s2++);
676f4f75ad5SArd Biesheuvel 			options_chars++;
677f4f75ad5SArd Biesheuvel 		}
678f4f75ad5SArd Biesheuvel 	}
679f4f75ad5SArd Biesheuvel 
680f4f75ad5SArd Biesheuvel 	if (!options_chars) {
681f4f75ad5SArd Biesheuvel 		/* No command line options, so return empty string*/
682f4f75ad5SArd Biesheuvel 		options = &zero;
683f4f75ad5SArd Biesheuvel 	}
684f4f75ad5SArd Biesheuvel 
685f4f75ad5SArd Biesheuvel 	options_bytes++;	/* NUL termination */
686f4f75ad5SArd Biesheuvel 
687f4f75ad5SArd Biesheuvel 	status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
688f4f75ad5SArd Biesheuvel 	if (status != EFI_SUCCESS)
689f4f75ad5SArd Biesheuvel 		return NULL;
690f4f75ad5SArd Biesheuvel 
691f4f75ad5SArd Biesheuvel 	s1 = (u8 *)cmdline_addr;
692f4f75ad5SArd Biesheuvel 	s2 = (const u16 *)options;
693f4f75ad5SArd Biesheuvel 
694f4f75ad5SArd Biesheuvel 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
695f4f75ad5SArd Biesheuvel 	*s1 = '\0';
696f4f75ad5SArd Biesheuvel 
697f4f75ad5SArd Biesheuvel 	*cmd_line_len = options_bytes;
698f4f75ad5SArd Biesheuvel 	return (char *)cmdline_addr;
699f4f75ad5SArd Biesheuvel }
700