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.
13 #include <linux/efi.h>
18 #define EFI_READ_CHUNK_SIZE (1024 * 1024)
21 efi_file_handle_t *handle;
25 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
29 for (s8 = str; *s8; s8++) {
30 efi_char16_t ch[2] = { 0 };
34 efi_char16_t nl[2] = { '\r', 0 };
35 efi_char16_printk(sys_table_arg, nl);
38 efi_char16_printk(sys_table_arg, ch);
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,
47 unsigned long *key_ptr)
49 efi_memory_desc_t *m = NULL;
54 *map_size = sizeof(*m) * 32;
57 * Add an additional efi_memory_desc_t because we're doing an
58 * allocation which may be in a new descriptor region.
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)
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);
75 if (status != EFI_SUCCESS)
76 efi_call_early(free_pool, m);
78 if (key_ptr && status == EFI_SUCCESS)
80 if (desc_ver && status == EFI_SUCCESS)
81 *desc_ver = desc_version;
89 unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
92 unsigned long map_size;
93 unsigned long membase = EFI_ERROR;
94 struct efi_memory_map map;
95 efi_memory_desc_t *md;
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)
102 map.map_end = map.map + map_size;
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;
109 efi_call_early(free_pool, map.map);
115 * Allocate at the highest possible address that is not above 'max'.
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)
121 unsigned long map_size, desc_size;
122 efi_memory_desc_t *map;
124 unsigned long nr_pages;
128 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
130 if (status != EFI_SUCCESS)
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.
138 if (align < EFI_PAGE_SIZE)
139 align = EFI_PAGE_SIZE;
141 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
143 for (i = 0; i < map_size / desc_size; i++) {
144 efi_memory_desc_t *desc;
145 unsigned long m = (unsigned long)map;
148 desc = (efi_memory_desc_t *)(m + (i * desc_size));
149 if (desc->type != EFI_CONVENTIONAL_MEMORY)
152 if (desc->num_pages < nr_pages)
155 start = desc->phys_addr;
156 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
158 if ((start + size) > end || (start + size) > max)
161 if (end - size > max)
164 if (round_down(end - size, align) < start)
167 start = round_down(end - size, align);
170 * Don't allocate at 0x0. It will confuse code that
171 * checks pointers against NULL.
176 if (start > max_addr)
181 status = EFI_NOT_FOUND;
183 status = efi_call_early(allocate_pages,
184 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
185 nr_pages, &max_addr);
186 if (status != EFI_SUCCESS) {
195 efi_call_early(free_pool, map);
201 * Allocate at the lowest possible address.
203 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
204 unsigned long size, unsigned long align,
207 unsigned long map_size, desc_size;
208 efi_memory_desc_t *map;
210 unsigned long nr_pages;
213 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
215 if (status != EFI_SUCCESS)
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.
223 if (align < EFI_PAGE_SIZE)
224 align = EFI_PAGE_SIZE;
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;
232 desc = (efi_memory_desc_t *)(m + (i * desc_size));
234 if (desc->type != EFI_CONVENTIONAL_MEMORY)
237 if (desc->num_pages < nr_pages)
240 start = desc->phys_addr;
241 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
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.
251 start = round_up(start, align);
252 if ((start + size) > end)
255 status = efi_call_early(allocate_pages,
256 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
258 if (status == EFI_SUCCESS) {
264 if (i == map_size / desc_size)
265 status = EFI_NOT_FOUND;
267 efi_call_early(free_pool, map);
272 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
275 unsigned long nr_pages;
280 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
281 efi_call_early(free_pages, addr, nr_pages);
286 * Check the cmdline for a LILO-style file= arguments.
288 * We only support loading a file from the same filesystem as
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)
298 struct file_info *files;
299 unsigned long file_addr;
301 efi_file_handle_t *fh = NULL;
312 j = 0; /* See close_handles */
314 if (!load_addr || !load_size)
315 return EFI_INVALID_PARAMETER;
323 for (nr_files = 0; *str; nr_files++) {
324 str = strstr(str, option_string);
328 str += strlen(option_string);
330 /* Skip any leading slashes */
331 while (*str == '/' || *str == '\\')
334 while (*str && *str != ' ' && *str != '\n')
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");
349 for (i = 0; i < nr_files; i++) {
350 struct file_info *file;
351 efi_char16_t filename_16[256];
354 str = strstr(str, option_string);
358 str += strlen(option_string);
363 /* Skip any leading slashes */
364 while (*str == '/' || *str == '\\')
367 while (*str && *str != ' ' && *str != '\n') {
368 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
381 /* Only open the volume once. */
383 status = efi_open_volume(sys_table_arg, image,
385 if (status != EFI_SUCCESS)
389 status = efi_file_size(sys_table_arg, fh, filename_16,
390 (void **)&file->handle, &file->size);
391 if (status != EFI_SUCCESS)
394 file_size_total += file->size;
397 if (file_size_total) {
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.
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");
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;
420 for (j = 0; j < nr_files; j++) {
423 size = files[j].size;
425 unsigned long chunksize;
426 if (size > EFI_READ_CHUNK_SIZE)
427 chunksize = EFI_READ_CHUNK_SIZE;
431 status = efi_file_read(files[j].handle,
434 if (status != EFI_SUCCESS) {
435 pr_efi_err(sys_table_arg, "Failed to read file\n");
436 goto free_file_total;
442 efi_file_close(files[j].handle);
447 efi_call_early(free_pool, files);
449 *load_addr = file_addr;
450 *load_size = file_size_total;
455 efi_free(sys_table_arg, file_size_total, file_addr);
458 for (k = j; k < i; k++)
459 efi_file_close(files[k].handle);
461 efi_call_early(free_pool, files);
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
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)
485 unsigned long cur_image_addr;
486 unsigned long new_addr = 0;
488 unsigned long nr_pages;
489 efi_physical_addr_t efi_addr = preferred_addr;
491 if (!image_addr || !image_size || !alloc_size)
492 return EFI_INVALID_PARAMETER;
493 if (alloc_size < image_size)
494 return EFI_INVALID_PARAMETER;
496 cur_image_addr = *image_addr;
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.
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);
512 * If preferred address allocation failed allocate as low as
515 if (status != EFI_SUCCESS) {
516 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
519 if (status != EFI_SUCCESS) {
520 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
525 * We know source/dest won't overlap since both memory ranges
526 * have been allocated by UEFI, so we can safely use memcpy.
528 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
530 /* Return the new address of the relocated image. */
531 *image_addr = new_addr;
537 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
538 * This overestimates for surrogates, but that is okay.
540 static int efi_utf8_bytes(u16 c)
542 return 1 + (c >= 0x80) + (c >= 0x800);
546 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
548 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
554 if (n && c >= 0xd800 && c <= 0xdbff &&
555 *src >= 0xdc00 && *src <= 0xdfff) {
556 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
560 if (c >= 0xd800 && c <= 0xdfff)
561 c = 0xfffd; /* Unmatched surrogate */
567 *dst++ = 0xc0 + (c >> 6);
571 *dst++ = 0xe0 + (c >> 12);
574 *dst++ = 0xf0 + (c >> 18);
575 *dst++ = 0x80 + ((c >> 12) & 0x3f);
577 *dst++ = 0x80 + ((c >> 6) & 0x3f);
579 *dst++ = 0x80 + (c & 0x3f);
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.
590 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
591 efi_loaded_image_t *image,
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 */
606 while (*s2 && *s2 != '\n'
607 && options_chars < load_options_chars) {
608 options_bytes += efi_utf8_bytes(*s2++);
613 if (!options_chars) {
614 /* No command line options, so return empty string*/
618 options_bytes++; /* NUL termination */
620 status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
621 if (status != EFI_SUCCESS)
624 s1 = (u8 *)cmdline_addr;
625 s2 = (const u16 *)options;
627 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
630 *cmd_line_len = options_bytes;
631 return (char *)cmdline_addr;