1 /*
2  * Helper functions used by the EFI stub on multiple
3  * architectures. This should be #included by the EFI stub
4  * implementation files.
5  *
6  * Copyright 2011 Intel Corporation; author Matt Fleming
7  *
8  * This file is part of the Linux kernel, and is made available
9  * under the terms of the GNU General Public License version 2.
10  *
11  */
12 
13 #include <linux/efi.h>
14 #include <asm/efi.h>
15 
16 #include "efistub.h"
17 
18 #define EFI_READ_CHUNK_SIZE	(1024 * 1024)
19 
20 struct file_info {
21 	efi_file_handle_t *handle;
22 	u64 size;
23 };
24 
25 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
26 {
27 	char *s8;
28 
29 	for (s8 = str; *s8; s8++) {
30 		efi_char16_t ch[2] = { 0 };
31 
32 		ch[0] = *s8;
33 		if (*s8 == '\n') {
34 			efi_char16_t nl[2] = { '\r', 0 };
35 			efi_char16_printk(sys_table_arg, nl);
36 		}
37 
38 		efi_char16_printk(sys_table_arg, ch);
39 	}
40 }
41 
42 efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
43 				efi_memory_desc_t **map,
44 				unsigned long *map_size,
45 				unsigned long *desc_size,
46 				u32 *desc_ver,
47 				unsigned long *key_ptr)
48 {
49 	efi_memory_desc_t *m = NULL;
50 	efi_status_t status;
51 	unsigned long key;
52 	u32 desc_version;
53 
54 	*map_size = sizeof(*m) * 32;
55 again:
56 	/*
57 	 * Add an additional efi_memory_desc_t because we're doing an
58 	 * allocation which may be in a new descriptor region.
59 	 */
60 	*map_size += sizeof(*m);
61 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
62 				*map_size, (void **)&m);
63 	if (status != EFI_SUCCESS)
64 		goto fail;
65 
66 	*desc_size = 0;
67 	key = 0;
68 	status = efi_call_early(get_memory_map, map_size, m,
69 				&key, desc_size, &desc_version);
70 	if (status == EFI_BUFFER_TOO_SMALL) {
71 		efi_call_early(free_pool, m);
72 		goto again;
73 	}
74 
75 	if (status != EFI_SUCCESS)
76 		efi_call_early(free_pool, m);
77 
78 	if (key_ptr && status == EFI_SUCCESS)
79 		*key_ptr = key;
80 	if (desc_ver && status == EFI_SUCCESS)
81 		*desc_ver = desc_version;
82 
83 fail:
84 	*map = m;
85 	return status;
86 }
87 
88 
89 unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
90 {
91 	efi_status_t status;
92 	unsigned long map_size;
93 	unsigned long membase  = EFI_ERROR;
94 	struct efi_memory_map map;
95 	efi_memory_desc_t *md;
96 
97 	status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
98 				    &map_size, &map.desc_size, NULL, NULL);
99 	if (status != EFI_SUCCESS)
100 		return membase;
101 
102 	map.map_end = map.map + map_size;
103 
104 	for_each_efi_memory_desc(&map, md)
105 		if (md->attribute & EFI_MEMORY_WB)
106 			if (membase > md->phys_addr)
107 				membase = md->phys_addr;
108 
109 	efi_call_early(free_pool, map.map);
110 
111 	return membase;
112 }
113 
114 /*
115  * Allocate at the highest possible address that is not above 'max'.
116  */
117 efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
118 			    unsigned long size, unsigned long align,
119 			    unsigned long *addr, unsigned long max)
120 {
121 	unsigned long map_size, desc_size;
122 	efi_memory_desc_t *map;
123 	efi_status_t status;
124 	unsigned long nr_pages;
125 	u64 max_addr = 0;
126 	int i;
127 
128 	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
129 				    NULL, NULL);
130 	if (status != EFI_SUCCESS)
131 		goto fail;
132 
133 	/*
134 	 * Enforce minimum alignment that EFI requires when requesting
135 	 * a specific address.  We are doing page-based allocations,
136 	 * so we must be aligned to a page.
137 	 */
138 	if (align < EFI_PAGE_SIZE)
139 		align = EFI_PAGE_SIZE;
140 
141 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
142 again:
143 	for (i = 0; i < map_size / desc_size; i++) {
144 		efi_memory_desc_t *desc;
145 		unsigned long m = (unsigned long)map;
146 		u64 start, end;
147 
148 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
149 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
150 			continue;
151 
152 		if (desc->num_pages < nr_pages)
153 			continue;
154 
155 		start = desc->phys_addr;
156 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
157 
158 		if ((start + size) > end || (start + size) > max)
159 			continue;
160 
161 		if (end - size > max)
162 			end = max;
163 
164 		if (round_down(end - size, align) < start)
165 			continue;
166 
167 		start = round_down(end - size, align);
168 
169 		/*
170 		 * Don't allocate at 0x0. It will confuse code that
171 		 * checks pointers against NULL.
172 		 */
173 		if (start == 0x0)
174 			continue;
175 
176 		if (start > max_addr)
177 			max_addr = start;
178 	}
179 
180 	if (!max_addr)
181 		status = EFI_NOT_FOUND;
182 	else {
183 		status = efi_call_early(allocate_pages,
184 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
185 					nr_pages, &max_addr);
186 		if (status != EFI_SUCCESS) {
187 			max = max_addr;
188 			max_addr = 0;
189 			goto again;
190 		}
191 
192 		*addr = max_addr;
193 	}
194 
195 	efi_call_early(free_pool, map);
196 fail:
197 	return status;
198 }
199 
200 /*
201  * Allocate at the lowest possible address.
202  */
203 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
204 			   unsigned long size, unsigned long align,
205 			   unsigned long *addr)
206 {
207 	unsigned long map_size, desc_size;
208 	efi_memory_desc_t *map;
209 	efi_status_t status;
210 	unsigned long nr_pages;
211 	int i;
212 
213 	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
214 				    NULL, NULL);
215 	if (status != EFI_SUCCESS)
216 		goto fail;
217 
218 	/*
219 	 * Enforce minimum alignment that EFI requires when requesting
220 	 * a specific address.  We are doing page-based allocations,
221 	 * so we must be aligned to a page.
222 	 */
223 	if (align < EFI_PAGE_SIZE)
224 		align = EFI_PAGE_SIZE;
225 
226 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
227 	for (i = 0; i < map_size / desc_size; i++) {
228 		efi_memory_desc_t *desc;
229 		unsigned long m = (unsigned long)map;
230 		u64 start, end;
231 
232 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
233 
234 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
235 			continue;
236 
237 		if (desc->num_pages < nr_pages)
238 			continue;
239 
240 		start = desc->phys_addr;
241 		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
242 
243 		/*
244 		 * Don't allocate at 0x0. It will confuse code that
245 		 * checks pointers against NULL. Skip the first 8
246 		 * bytes so we start at a nice even number.
247 		 */
248 		if (start == 0x0)
249 			start += 8;
250 
251 		start = round_up(start, align);
252 		if ((start + size) > end)
253 			continue;
254 
255 		status = efi_call_early(allocate_pages,
256 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
257 					nr_pages, &start);
258 		if (status == EFI_SUCCESS) {
259 			*addr = start;
260 			break;
261 		}
262 	}
263 
264 	if (i == map_size / desc_size)
265 		status = EFI_NOT_FOUND;
266 
267 	efi_call_early(free_pool, map);
268 fail:
269 	return status;
270 }
271 
272 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
273 	      unsigned long addr)
274 {
275 	unsigned long nr_pages;
276 
277 	if (!size)
278 		return;
279 
280 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
281 	efi_call_early(free_pages, addr, nr_pages);
282 }
283 
284 
285 /*
286  * Check the cmdline for a LILO-style file= arguments.
287  *
288  * We only support loading a file from the same filesystem as
289  * the kernel image.
290  */
291 efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
292 				  efi_loaded_image_t *image,
293 				  char *cmd_line, char *option_string,
294 				  unsigned long max_addr,
295 				  unsigned long *load_addr,
296 				  unsigned long *load_size)
297 {
298 	struct file_info *files;
299 	unsigned long file_addr;
300 	u64 file_size_total;
301 	efi_file_handle_t *fh = NULL;
302 	efi_status_t status;
303 	int nr_files;
304 	char *str;
305 	int i, j, k;
306 
307 	file_addr = 0;
308 	file_size_total = 0;
309 
310 	str = cmd_line;
311 
312 	j = 0;			/* See close_handles */
313 
314 	if (!load_addr || !load_size)
315 		return EFI_INVALID_PARAMETER;
316 
317 	*load_addr = 0;
318 	*load_size = 0;
319 
320 	if (!str || !*str)
321 		return EFI_SUCCESS;
322 
323 	for (nr_files = 0; *str; nr_files++) {
324 		str = strstr(str, option_string);
325 		if (!str)
326 			break;
327 
328 		str += strlen(option_string);
329 
330 		/* Skip any leading slashes */
331 		while (*str == '/' || *str == '\\')
332 			str++;
333 
334 		while (*str && *str != ' ' && *str != '\n')
335 			str++;
336 	}
337 
338 	if (!nr_files)
339 		return EFI_SUCCESS;
340 
341 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
342 				nr_files * sizeof(*files), (void **)&files);
343 	if (status != EFI_SUCCESS) {
344 		pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
345 		goto fail;
346 	}
347 
348 	str = cmd_line;
349 	for (i = 0; i < nr_files; i++) {
350 		struct file_info *file;
351 		efi_char16_t filename_16[256];
352 		efi_char16_t *p;
353 
354 		str = strstr(str, option_string);
355 		if (!str)
356 			break;
357 
358 		str += strlen(option_string);
359 
360 		file = &files[i];
361 		p = filename_16;
362 
363 		/* Skip any leading slashes */
364 		while (*str == '/' || *str == '\\')
365 			str++;
366 
367 		while (*str && *str != ' ' && *str != '\n') {
368 			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
369 				break;
370 
371 			if (*str == '/') {
372 				*p++ = '\\';
373 				str++;
374 			} else {
375 				*p++ = *str++;
376 			}
377 		}
378 
379 		*p = '\0';
380 
381 		/* Only open the volume once. */
382 		if (!i) {
383 			status = efi_open_volume(sys_table_arg, image,
384 						 (void **)&fh);
385 			if (status != EFI_SUCCESS)
386 				goto free_files;
387 		}
388 
389 		status = efi_file_size(sys_table_arg, fh, filename_16,
390 				       (void **)&file->handle, &file->size);
391 		if (status != EFI_SUCCESS)
392 			goto close_handles;
393 
394 		file_size_total += file->size;
395 	}
396 
397 	if (file_size_total) {
398 		unsigned long addr;
399 
400 		/*
401 		 * Multiple files need to be at consecutive addresses in memory,
402 		 * so allocate enough memory for all the files.  This is used
403 		 * for loading multiple files.
404 		 */
405 		status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
406 				    &file_addr, max_addr);
407 		if (status != EFI_SUCCESS) {
408 			pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
409 			goto close_handles;
410 		}
411 
412 		/* We've run out of free low memory. */
413 		if (file_addr > max_addr) {
414 			pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
415 			status = EFI_INVALID_PARAMETER;
416 			goto free_file_total;
417 		}
418 
419 		addr = file_addr;
420 		for (j = 0; j < nr_files; j++) {
421 			unsigned long size;
422 
423 			size = files[j].size;
424 			while (size) {
425 				unsigned long chunksize;
426 				if (size > EFI_READ_CHUNK_SIZE)
427 					chunksize = EFI_READ_CHUNK_SIZE;
428 				else
429 					chunksize = size;
430 
431 				status = efi_file_read(files[j].handle,
432 						       &chunksize,
433 						       (void *)addr);
434 				if (status != EFI_SUCCESS) {
435 					pr_efi_err(sys_table_arg, "Failed to read file\n");
436 					goto free_file_total;
437 				}
438 				addr += chunksize;
439 				size -= chunksize;
440 			}
441 
442 			efi_file_close(files[j].handle);
443 		}
444 
445 	}
446 
447 	efi_call_early(free_pool, files);
448 
449 	*load_addr = file_addr;
450 	*load_size = file_size_total;
451 
452 	return status;
453 
454 free_file_total:
455 	efi_free(sys_table_arg, file_size_total, file_addr);
456 
457 close_handles:
458 	for (k = j; k < i; k++)
459 		efi_file_close(files[k].handle);
460 free_files:
461 	efi_call_early(free_pool, files);
462 fail:
463 	*load_addr = 0;
464 	*load_size = 0;
465 
466 	return status;
467 }
468 /*
469  * Relocate a kernel image, either compressed or uncompressed.
470  * In the ARM64 case, all kernel images are currently
471  * uncompressed, and as such when we relocate it we need to
472  * allocate additional space for the BSS segment. Any low
473  * memory that this function should avoid needs to be
474  * unavailable in the EFI memory map, as if the preferred
475  * address is not available the lowest available address will
476  * be used.
477  */
478 efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
479 				 unsigned long *image_addr,
480 				 unsigned long image_size,
481 				 unsigned long alloc_size,
482 				 unsigned long preferred_addr,
483 				 unsigned long alignment)
484 {
485 	unsigned long cur_image_addr;
486 	unsigned long new_addr = 0;
487 	efi_status_t status;
488 	unsigned long nr_pages;
489 	efi_physical_addr_t efi_addr = preferred_addr;
490 
491 	if (!image_addr || !image_size || !alloc_size)
492 		return EFI_INVALID_PARAMETER;
493 	if (alloc_size < image_size)
494 		return EFI_INVALID_PARAMETER;
495 
496 	cur_image_addr = *image_addr;
497 
498 	/*
499 	 * The EFI firmware loader could have placed the kernel image
500 	 * anywhere in memory, but the kernel has restrictions on the
501 	 * max physical address it can run at.  Some architectures
502 	 * also have a prefered address, so first try to relocate
503 	 * to the preferred address.  If that fails, allocate as low
504 	 * as possible while respecting the required alignment.
505 	 */
506 	nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
507 	status = efi_call_early(allocate_pages,
508 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
509 				nr_pages, &efi_addr);
510 	new_addr = efi_addr;
511 	/*
512 	 * If preferred address allocation failed allocate as low as
513 	 * possible.
514 	 */
515 	if (status != EFI_SUCCESS) {
516 		status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
517 				       &new_addr);
518 	}
519 	if (status != EFI_SUCCESS) {
520 		pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
521 		return status;
522 	}
523 
524 	/*
525 	 * We know source/dest won't overlap since both memory ranges
526 	 * have been allocated by UEFI, so we can safely use memcpy.
527 	 */
528 	memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
529 
530 	/* Return the new address of the relocated image. */
531 	*image_addr = new_addr;
532 
533 	return status;
534 }
535 
536 /*
537  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
538  * This overestimates for surrogates, but that is okay.
539  */
540 static int efi_utf8_bytes(u16 c)
541 {
542 	return 1 + (c >= 0x80) + (c >= 0x800);
543 }
544 
545 /*
546  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
547  */
548 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
549 {
550 	unsigned int c;
551 
552 	while (n--) {
553 		c = *src++;
554 		if (n && c >= 0xd800 && c <= 0xdbff &&
555 		    *src >= 0xdc00 && *src <= 0xdfff) {
556 			c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
557 			src++;
558 			n--;
559 		}
560 		if (c >= 0xd800 && c <= 0xdfff)
561 			c = 0xfffd; /* Unmatched surrogate */
562 		if (c < 0x80) {
563 			*dst++ = c;
564 			continue;
565 		}
566 		if (c < 0x800) {
567 			*dst++ = 0xc0 + (c >> 6);
568 			goto t1;
569 		}
570 		if (c < 0x10000) {
571 			*dst++ = 0xe0 + (c >> 12);
572 			goto t2;
573 		}
574 		*dst++ = 0xf0 + (c >> 18);
575 		*dst++ = 0x80 + ((c >> 12) & 0x3f);
576 	t2:
577 		*dst++ = 0x80 + ((c >> 6) & 0x3f);
578 	t1:
579 		*dst++ = 0x80 + (c & 0x3f);
580 	}
581 
582 	return dst;
583 }
584 
585 /*
586  * Convert the unicode UEFI command line to ASCII to pass to kernel.
587  * Size of memory allocated return in *cmd_line_len.
588  * Returns NULL on error.
589  */
590 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
591 			  efi_loaded_image_t *image,
592 			  int *cmd_line_len)
593 {
594 	const u16 *s2;
595 	u8 *s1 = NULL;
596 	unsigned long cmdline_addr = 0;
597 	int load_options_chars = image->load_options_size / 2; /* UTF-16 */
598 	const u16 *options = image->load_options;
599 	int options_bytes = 0;  /* UTF-8 bytes */
600 	int options_chars = 0;  /* UTF-16 chars */
601 	efi_status_t status;
602 	u16 zero = 0;
603 
604 	if (options) {
605 		s2 = options;
606 		while (*s2 && *s2 != '\n'
607 		       && options_chars < load_options_chars) {
608 			options_bytes += efi_utf8_bytes(*s2++);
609 			options_chars++;
610 		}
611 	}
612 
613 	if (!options_chars) {
614 		/* No command line options, so return empty string*/
615 		options = &zero;
616 	}
617 
618 	options_bytes++;	/* NUL termination */
619 
620 	status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
621 	if (status != EFI_SUCCESS)
622 		return NULL;
623 
624 	s1 = (u8 *)cmdline_addr;
625 	s2 = (const u16 *)options;
626 
627 	s1 = efi_utf16_to_utf8(s1, s2, options_chars);
628 	*s1 = '\0';
629 
630 	*cmd_line_len = options_bytes;
631 	return (char *)cmdline_addr;
632 }
633