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)
15 efi_file_handle_t *handle;
19 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
23 for (s8 = str; *s8; s8++) {
24 efi_char16_t ch[2] = { 0 };
28 efi_char16_t nl[2] = { '\r', 0 };
29 efi_char16_printk(sys_table_arg, nl);
32 efi_char16_printk(sys_table_arg, ch);
37 static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
38 efi_memory_desc_t **map,
39 unsigned long *map_size,
40 unsigned long *desc_size,
42 unsigned long *key_ptr)
44 efi_memory_desc_t *m = NULL;
49 *map_size = sizeof(*m) * 32;
52 * Add an additional efi_memory_desc_t because we're doing an
53 * allocation which may be in a new descriptor region.
55 *map_size += sizeof(*m);
56 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
57 *map_size, (void **)&m);
58 if (status != EFI_SUCCESS)
63 status = efi_call_early(get_memory_map, map_size, m,
64 &key, desc_size, &desc_version);
65 if (status == EFI_BUFFER_TOO_SMALL) {
66 efi_call_early(free_pool, m);
70 if (status != EFI_SUCCESS)
71 efi_call_early(free_pool, m);
73 if (key_ptr && status == EFI_SUCCESS)
75 if (desc_ver && status == EFI_SUCCESS)
76 *desc_ver = desc_version;
84 * Allocate at the highest possible address that is not above 'max'.
86 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
87 unsigned long size, unsigned long align,
88 unsigned long *addr, unsigned long max)
90 unsigned long map_size, desc_size;
91 efi_memory_desc_t *map;
93 unsigned long nr_pages;
97 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
99 if (status != EFI_SUCCESS)
103 * Enforce minimum alignment that EFI requires when requesting
104 * a specific address. We are doing page-based allocations,
105 * so we must be aligned to a page.
107 if (align < EFI_PAGE_SIZE)
108 align = EFI_PAGE_SIZE;
110 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
112 for (i = 0; i < map_size / desc_size; i++) {
113 efi_memory_desc_t *desc;
114 unsigned long m = (unsigned long)map;
117 desc = (efi_memory_desc_t *)(m + (i * desc_size));
118 if (desc->type != EFI_CONVENTIONAL_MEMORY)
121 if (desc->num_pages < nr_pages)
124 start = desc->phys_addr;
125 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
127 if ((start + size) > end || (start + size) > max)
130 if (end - size > max)
133 if (round_down(end - size, align) < start)
136 start = round_down(end - size, align);
139 * Don't allocate at 0x0. It will confuse code that
140 * checks pointers against NULL.
145 if (start > max_addr)
150 status = EFI_NOT_FOUND;
152 status = efi_call_early(allocate_pages,
153 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
154 nr_pages, &max_addr);
155 if (status != EFI_SUCCESS) {
164 efi_call_early(free_pool, map);
170 * Allocate at the lowest possible address.
172 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
173 unsigned long size, unsigned long align,
176 unsigned long map_size, desc_size;
177 efi_memory_desc_t *map;
179 unsigned long nr_pages;
182 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
184 if (status != EFI_SUCCESS)
188 * Enforce minimum alignment that EFI requires when requesting
189 * a specific address. We are doing page-based allocations,
190 * so we must be aligned to a page.
192 if (align < EFI_PAGE_SIZE)
193 align = EFI_PAGE_SIZE;
195 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
196 for (i = 0; i < map_size / desc_size; i++) {
197 efi_memory_desc_t *desc;
198 unsigned long m = (unsigned long)map;
201 desc = (efi_memory_desc_t *)(m + (i * desc_size));
203 if (desc->type != EFI_CONVENTIONAL_MEMORY)
206 if (desc->num_pages < nr_pages)
209 start = desc->phys_addr;
210 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
213 * Don't allocate at 0x0. It will confuse code that
214 * checks pointers against NULL. Skip the first 8
215 * bytes so we start at a nice even number.
220 start = round_up(start, align);
221 if ((start + size) > end)
224 status = efi_call_early(allocate_pages,
225 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
227 if (status == EFI_SUCCESS) {
233 if (i == map_size / desc_size)
234 status = EFI_NOT_FOUND;
236 efi_call_early(free_pool, map);
241 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
244 unsigned long nr_pages;
249 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
250 efi_call_early(free_pages, addr, nr_pages);
255 * Check the cmdline for a LILO-style file= arguments.
257 * We only support loading a file from the same filesystem as
260 static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
261 efi_loaded_image_t *image,
262 char *cmd_line, char *option_string,
263 unsigned long max_addr,
264 unsigned long *load_addr,
265 unsigned long *load_size)
267 struct file_info *files;
268 unsigned long file_addr;
270 efi_file_handle_t *fh;
281 j = 0; /* See close_handles */
283 if (!load_addr || !load_size)
284 return EFI_INVALID_PARAMETER;
292 for (nr_files = 0; *str; nr_files++) {
293 str = strstr(str, option_string);
297 str += strlen(option_string);
299 /* Skip any leading slashes */
300 while (*str == '/' || *str == '\\')
303 while (*str && *str != ' ' && *str != '\n')
310 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
311 nr_files * sizeof(*files), (void **)&files);
312 if (status != EFI_SUCCESS) {
313 efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
318 for (i = 0; i < nr_files; i++) {
319 struct file_info *file;
320 efi_char16_t filename_16[256];
323 str = strstr(str, option_string);
327 str += strlen(option_string);
332 /* Skip any leading slashes */
333 while (*str == '/' || *str == '\\')
336 while (*str && *str != ' ' && *str != '\n') {
337 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
350 /* Only open the volume once. */
352 status = efi_open_volume(sys_table_arg, image,
354 if (status != EFI_SUCCESS)
358 status = efi_file_size(sys_table_arg, fh, filename_16,
359 (void **)&file->handle, &file->size);
360 if (status != EFI_SUCCESS)
363 file_size_total += file->size;
366 if (file_size_total) {
370 * Multiple files need to be at consecutive addresses in memory,
371 * so allocate enough memory for all the files. This is used
372 * for loading multiple files.
374 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
375 &file_addr, max_addr);
376 if (status != EFI_SUCCESS) {
377 efi_printk(sys_table_arg, "Failed to alloc highmem for files\n");
381 /* We've run out of free low memory. */
382 if (file_addr > max_addr) {
383 efi_printk(sys_table_arg, "We've run out of free low memory\n");
384 status = EFI_INVALID_PARAMETER;
385 goto free_file_total;
389 for (j = 0; j < nr_files; j++) {
392 size = files[j].size;
394 unsigned long chunksize;
395 if (size > EFI_READ_CHUNK_SIZE)
396 chunksize = EFI_READ_CHUNK_SIZE;
400 status = efi_file_read(files[j].handle,
403 if (status != EFI_SUCCESS) {
404 efi_printk(sys_table_arg, "Failed to read file\n");
405 goto free_file_total;
411 efi_file_close(files[j].handle);
416 efi_call_early(free_pool, files);
418 *load_addr = file_addr;
419 *load_size = file_size_total;
424 efi_free(sys_table_arg, file_size_total, file_addr);
427 for (k = j; k < i; k++)
428 efi_file_close(files[k].handle);
430 efi_call_early(free_pool, files);
438 * Relocate a kernel image, either compressed or uncompressed.
439 * In the ARM64 case, all kernel images are currently
440 * uncompressed, and as such when we relocate it we need to
441 * allocate additional space for the BSS segment. Any low
442 * memory that this function should avoid needs to be
443 * unavailable in the EFI memory map, as if the preferred
444 * address is not available the lowest available address will
447 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
448 unsigned long *image_addr,
449 unsigned long image_size,
450 unsigned long alloc_size,
451 unsigned long preferred_addr,
452 unsigned long alignment)
454 unsigned long cur_image_addr;
455 unsigned long new_addr = 0;
457 unsigned long nr_pages;
458 efi_physical_addr_t efi_addr = preferred_addr;
460 if (!image_addr || !image_size || !alloc_size)
461 return EFI_INVALID_PARAMETER;
462 if (alloc_size < image_size)
463 return EFI_INVALID_PARAMETER;
465 cur_image_addr = *image_addr;
468 * The EFI firmware loader could have placed the kernel image
469 * anywhere in memory, but the kernel has restrictions on the
470 * max physical address it can run at. Some architectures
471 * also have a prefered address, so first try to relocate
472 * to the preferred address. If that fails, allocate as low
473 * as possible while respecting the required alignment.
475 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
476 status = efi_call_early(allocate_pages,
477 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
478 nr_pages, &efi_addr);
481 * If preferred address allocation failed allocate as low as
484 if (status != EFI_SUCCESS) {
485 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
488 if (status != EFI_SUCCESS) {
489 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
494 * We know source/dest won't overlap since both memory ranges
495 * have been allocated by UEFI, so we can safely use memcpy.
497 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
499 /* Return the new address of the relocated image. */
500 *image_addr = new_addr;
506 * Convert the unicode UEFI command line to ASCII to pass to kernel.
507 * Size of memory allocated return in *cmd_line_len.
508 * Returns NULL on error.
510 static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
511 efi_loaded_image_t *image,
516 unsigned long cmdline_addr = 0;
517 int load_options_size = image->load_options_size / 2; /* ASCII */
518 void *options = image->load_options;
519 int options_size = 0;
526 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
532 if (options_size == 0) {
533 /* No command line options, so return empty string*/
538 options_size++; /* NUL termination */
541 * For ARM, allocate at a high address to avoid reserved
542 * regions at low addresses that we don't know the specfics of
543 * at the time we are processing the command line.
545 status = efi_high_alloc(sys_table_arg, options_size, 0,
546 &cmdline_addr, 0xfffff000);
548 status = efi_low_alloc(sys_table_arg, options_size, 0,
551 if (status != EFI_SUCCESS)
554 s1 = (u8 *)cmdline_addr;
557 for (i = 0; i < options_size - 1; i++)
562 *cmd_line_len = options_size;
563 return (char *)cmdline_addr;