2 * Helper functions used by the EFI stub on multiple
3 * architectures. This should be #included by the EFI stub
4 * implementation files.
6 * Copyright 2011 Intel Corporation; author Matt Fleming
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.
12 #define EFI_READ_CHUNK_SIZE (1024 * 1024)
14 /* error code which can't be mistaken for valid address */
15 #define EFI_ERROR (~0UL)
19 efi_file_handle_t *handle;
23 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
27 for (s8 = str; *s8; s8++) {
28 efi_char16_t ch[2] = { 0 };
32 efi_char16_t nl[2] = { '\r', 0 };
33 efi_char16_printk(sys_table_arg, nl);
36 efi_char16_printk(sys_table_arg, ch);
40 #define pr_efi(sys_table, msg) efi_printk(sys_table, "EFI stub: "msg)
41 #define pr_efi_err(sys_table, msg) efi_printk(sys_table, "EFI stub: ERROR: "msg)
44 static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
45 efi_memory_desc_t **map,
46 unsigned long *map_size,
47 unsigned long *desc_size,
49 unsigned long *key_ptr)
51 efi_memory_desc_t *m = NULL;
56 *map_size = sizeof(*m) * 32;
59 * Add an additional efi_memory_desc_t because we're doing an
60 * allocation which may be in a new descriptor region.
62 *map_size += sizeof(*m);
63 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
64 *map_size, (void **)&m);
65 if (status != EFI_SUCCESS)
70 status = efi_call_early(get_memory_map, map_size, m,
71 &key, desc_size, &desc_version);
72 if (status == EFI_BUFFER_TOO_SMALL) {
73 efi_call_early(free_pool, m);
77 if (status != EFI_SUCCESS)
78 efi_call_early(free_pool, m);
80 if (key_ptr && status == EFI_SUCCESS)
82 if (desc_ver && status == EFI_SUCCESS)
83 *desc_ver = desc_version;
91 static unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
94 unsigned long map_size;
95 unsigned long membase = EFI_ERROR;
96 struct efi_memory_map map;
97 efi_memory_desc_t *md;
99 status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
100 &map_size, &map.desc_size, NULL, NULL);
101 if (status != EFI_SUCCESS)
104 map.map_end = map.map + map_size;
106 for_each_efi_memory_desc(&map, md)
107 if (md->attribute & EFI_MEMORY_WB)
108 if (membase > md->phys_addr)
109 membase = md->phys_addr;
111 efi_call_early(free_pool, map.map);
117 * Allocate at the highest possible address that is not above 'max'.
119 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
120 unsigned long size, unsigned long align,
121 unsigned long *addr, unsigned long max)
123 unsigned long map_size, desc_size;
124 efi_memory_desc_t *map;
126 unsigned long nr_pages;
130 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
132 if (status != EFI_SUCCESS)
136 * Enforce minimum alignment that EFI requires when requesting
137 * a specific address. We are doing page-based allocations,
138 * so we must be aligned to a page.
140 if (align < EFI_PAGE_SIZE)
141 align = EFI_PAGE_SIZE;
143 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
145 for (i = 0; i < map_size / desc_size; i++) {
146 efi_memory_desc_t *desc;
147 unsigned long m = (unsigned long)map;
150 desc = (efi_memory_desc_t *)(m + (i * desc_size));
151 if (desc->type != EFI_CONVENTIONAL_MEMORY)
154 if (desc->num_pages < nr_pages)
157 start = desc->phys_addr;
158 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
160 if ((start + size) > end || (start + size) > max)
163 if (end - size > max)
166 if (round_down(end - size, align) < start)
169 start = round_down(end - size, align);
172 * Don't allocate at 0x0. It will confuse code that
173 * checks pointers against NULL.
178 if (start > max_addr)
183 status = EFI_NOT_FOUND;
185 status = efi_call_early(allocate_pages,
186 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
187 nr_pages, &max_addr);
188 if (status != EFI_SUCCESS) {
197 efi_call_early(free_pool, map);
203 * Allocate at the lowest possible address.
205 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
206 unsigned long size, unsigned long align,
209 unsigned long map_size, desc_size;
210 efi_memory_desc_t *map;
212 unsigned long nr_pages;
215 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
217 if (status != EFI_SUCCESS)
221 * Enforce minimum alignment that EFI requires when requesting
222 * a specific address. We are doing page-based allocations,
223 * so we must be aligned to a page.
225 if (align < EFI_PAGE_SIZE)
226 align = EFI_PAGE_SIZE;
228 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
229 for (i = 0; i < map_size / desc_size; i++) {
230 efi_memory_desc_t *desc;
231 unsigned long m = (unsigned long)map;
234 desc = (efi_memory_desc_t *)(m + (i * desc_size));
236 if (desc->type != EFI_CONVENTIONAL_MEMORY)
239 if (desc->num_pages < nr_pages)
242 start = desc->phys_addr;
243 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
246 * Don't allocate at 0x0. It will confuse code that
247 * checks pointers against NULL. Skip the first 8
248 * bytes so we start at a nice even number.
253 start = round_up(start, align);
254 if ((start + size) > end)
257 status = efi_call_early(allocate_pages,
258 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
260 if (status == EFI_SUCCESS) {
266 if (i == map_size / desc_size)
267 status = EFI_NOT_FOUND;
269 efi_call_early(free_pool, map);
274 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
277 unsigned long nr_pages;
282 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
283 efi_call_early(free_pages, addr, nr_pages);
288 * Check the cmdline for a LILO-style file= arguments.
290 * We only support loading a file from the same filesystem as
293 static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
294 efi_loaded_image_t *image,
295 char *cmd_line, char *option_string,
296 unsigned long max_addr,
297 unsigned long *load_addr,
298 unsigned long *load_size)
300 struct file_info *files;
301 unsigned long file_addr;
303 efi_file_handle_t *fh = NULL;
314 j = 0; /* See close_handles */
316 if (!load_addr || !load_size)
317 return EFI_INVALID_PARAMETER;
325 for (nr_files = 0; *str; nr_files++) {
326 str = strstr(str, option_string);
330 str += strlen(option_string);
332 /* Skip any leading slashes */
333 while (*str == '/' || *str == '\\')
336 while (*str && *str != ' ' && *str != '\n')
343 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
344 nr_files * sizeof(*files), (void **)&files);
345 if (status != EFI_SUCCESS) {
346 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
351 for (i = 0; i < nr_files; i++) {
352 struct file_info *file;
353 efi_char16_t filename_16[256];
356 str = strstr(str, option_string);
360 str += strlen(option_string);
365 /* Skip any leading slashes */
366 while (*str == '/' || *str == '\\')
369 while (*str && *str != ' ' && *str != '\n') {
370 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
383 /* Only open the volume once. */
385 status = efi_open_volume(sys_table_arg, image,
387 if (status != EFI_SUCCESS)
391 status = efi_file_size(sys_table_arg, fh, filename_16,
392 (void **)&file->handle, &file->size);
393 if (status != EFI_SUCCESS)
396 file_size_total += file->size;
399 if (file_size_total) {
403 * Multiple files need to be at consecutive addresses in memory,
404 * so allocate enough memory for all the files. This is used
405 * for loading multiple files.
407 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
408 &file_addr, max_addr);
409 if (status != EFI_SUCCESS) {
410 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
414 /* We've run out of free low memory. */
415 if (file_addr > max_addr) {
416 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
417 status = EFI_INVALID_PARAMETER;
418 goto free_file_total;
422 for (j = 0; j < nr_files; j++) {
425 size = files[j].size;
427 unsigned long chunksize;
428 if (size > EFI_READ_CHUNK_SIZE)
429 chunksize = EFI_READ_CHUNK_SIZE;
433 status = efi_file_read(files[j].handle,
436 if (status != EFI_SUCCESS) {
437 pr_efi_err(sys_table_arg, "Failed to read file\n");
438 goto free_file_total;
444 efi_file_close(files[j].handle);
449 efi_call_early(free_pool, files);
451 *load_addr = file_addr;
452 *load_size = file_size_total;
457 efi_free(sys_table_arg, file_size_total, file_addr);
460 for (k = j; k < i; k++)
461 efi_file_close(files[k].handle);
463 efi_call_early(free_pool, files);
471 * Relocate a kernel image, either compressed or uncompressed.
472 * In the ARM64 case, all kernel images are currently
473 * uncompressed, and as such when we relocate it we need to
474 * allocate additional space for the BSS segment. Any low
475 * memory that this function should avoid needs to be
476 * unavailable in the EFI memory map, as if the preferred
477 * address is not available the lowest available address will
480 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
481 unsigned long *image_addr,
482 unsigned long image_size,
483 unsigned long alloc_size,
484 unsigned long preferred_addr,
485 unsigned long alignment)
487 unsigned long cur_image_addr;
488 unsigned long new_addr = 0;
490 unsigned long nr_pages;
491 efi_physical_addr_t efi_addr = preferred_addr;
493 if (!image_addr || !image_size || !alloc_size)
494 return EFI_INVALID_PARAMETER;
495 if (alloc_size < image_size)
496 return EFI_INVALID_PARAMETER;
498 cur_image_addr = *image_addr;
501 * The EFI firmware loader could have placed the kernel image
502 * anywhere in memory, but the kernel has restrictions on the
503 * max physical address it can run at. Some architectures
504 * also have a prefered address, so first try to relocate
505 * to the preferred address. If that fails, allocate as low
506 * as possible while respecting the required alignment.
508 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
509 status = efi_call_early(allocate_pages,
510 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
511 nr_pages, &efi_addr);
514 * If preferred address allocation failed allocate as low as
517 if (status != EFI_SUCCESS) {
518 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
521 if (status != EFI_SUCCESS) {
522 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
527 * We know source/dest won't overlap since both memory ranges
528 * have been allocated by UEFI, so we can safely use memcpy.
530 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
532 /* Return the new address of the relocated image. */
533 *image_addr = new_addr;
539 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
540 * This overestimates for surrogates, but that is okay.
542 static int efi_utf8_bytes(u16 c)
544 return 1 + (c >= 0x80) + (c >= 0x800);
548 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
550 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
556 if (n && c >= 0xd800 && c <= 0xdbff &&
557 *src >= 0xdc00 && *src <= 0xdfff) {
558 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
562 if (c >= 0xd800 && c <= 0xdfff)
563 c = 0xfffd; /* Unmatched surrogate */
569 *dst++ = 0xc0 + (c >> 6);
573 *dst++ = 0xe0 + (c >> 12);
576 *dst++ = 0xf0 + (c >> 18);
577 *dst++ = 0x80 + ((c >> 12) & 0x3f);
579 *dst++ = 0x80 + ((c >> 6) & 0x3f);
581 *dst++ = 0x80 + (c & 0x3f);
588 * Convert the unicode UEFI command line to ASCII to pass to kernel.
589 * Size of memory allocated return in *cmd_line_len.
590 * Returns NULL on error.
592 static char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
593 efi_loaded_image_t *image,
598 unsigned long cmdline_addr = 0;
599 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
600 const u16 *options = image->load_options;
601 int options_bytes = 0; /* UTF-8 bytes */
602 int options_chars = 0; /* UTF-16 chars */
608 while (*s2 && *s2 != '\n'
609 && options_chars < load_options_chars) {
610 options_bytes += efi_utf8_bytes(*s2++);
615 if (!options_chars) {
616 /* No command line options, so return empty string*/
620 options_bytes++; /* NUL termination */
622 status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
623 if (status != EFI_SUCCESS)
626 s1 = (u8 *)cmdline_addr;
627 s2 = (const u16 *)options;
629 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
632 *cmd_line_len = options_bytes;
633 return (char *)cmdline_addr;