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;
22 static void efi_char16_printk(efi_system_table_t *sys_table_arg,
25 struct efi_simple_text_output_protocol *out;
27 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
28 efi_call_phys2(out->output_string, out, str);
31 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
35 for (s8 = str; *s8; s8++) {
36 efi_char16_t ch[2] = { 0 };
40 efi_char16_t nl[2] = { '\r', 0 };
41 efi_char16_printk(sys_table_arg, nl);
44 efi_char16_printk(sys_table_arg, ch);
49 static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
50 efi_memory_desc_t **map,
51 unsigned long *map_size,
52 unsigned long *desc_size,
54 unsigned long *key_ptr)
56 efi_memory_desc_t *m = NULL;
61 *map_size = sizeof(*m) * 32;
64 * Add an additional efi_memory_desc_t because we're doing an
65 * allocation which may be in a new descriptor region.
67 *map_size += sizeof(*m);
68 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
69 EFI_LOADER_DATA, *map_size, (void **)&m);
70 if (status != EFI_SUCCESS)
73 status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
74 map_size, m, &key, desc_size, &desc_version);
75 if (status == EFI_BUFFER_TOO_SMALL) {
76 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
80 if (status != EFI_SUCCESS)
81 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
82 if (key_ptr && status == EFI_SUCCESS)
84 if (desc_ver && status == EFI_SUCCESS)
85 *desc_ver = desc_version;
93 * Allocate at the highest possible address that is not above 'max'.
95 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
96 unsigned long size, unsigned long align,
97 unsigned long *addr, unsigned long max)
99 unsigned long map_size, desc_size;
100 efi_memory_desc_t *map;
102 unsigned long nr_pages;
106 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
108 if (status != EFI_SUCCESS)
112 * Enforce minimum alignment that EFI requires when requesting
113 * a specific address. We are doing page-based allocations,
114 * so we must be aligned to a page.
116 if (align < EFI_PAGE_SIZE)
117 align = EFI_PAGE_SIZE;
119 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
121 for (i = 0; i < map_size / desc_size; i++) {
122 efi_memory_desc_t *desc;
123 unsigned long m = (unsigned long)map;
126 desc = (efi_memory_desc_t *)(m + (i * desc_size));
127 if (desc->type != EFI_CONVENTIONAL_MEMORY)
130 if (desc->num_pages < nr_pages)
133 start = desc->phys_addr;
134 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
136 if ((start + size) > end || (start + size) > max)
139 if (end - size > max)
142 if (round_down(end - size, align) < start)
145 start = round_down(end - size, align);
148 * Don't allocate at 0x0. It will confuse code that
149 * checks pointers against NULL.
154 if (start > max_addr)
159 status = EFI_NOT_FOUND;
161 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
162 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
163 nr_pages, &max_addr);
164 if (status != EFI_SUCCESS) {
174 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
181 * Allocate at the lowest possible address.
183 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
184 unsigned long size, unsigned long align,
187 unsigned long map_size, desc_size;
188 efi_memory_desc_t *map;
190 unsigned long nr_pages;
193 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
195 if (status != EFI_SUCCESS)
199 * Enforce minimum alignment that EFI requires when requesting
200 * a specific address. We are doing page-based allocations,
201 * so we must be aligned to a page.
203 if (align < EFI_PAGE_SIZE)
204 align = EFI_PAGE_SIZE;
206 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
207 for (i = 0; i < map_size / desc_size; i++) {
208 efi_memory_desc_t *desc;
209 unsigned long m = (unsigned long)map;
212 desc = (efi_memory_desc_t *)(m + (i * desc_size));
214 if (desc->type != EFI_CONVENTIONAL_MEMORY)
217 if (desc->num_pages < nr_pages)
220 start = desc->phys_addr;
221 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
224 * Don't allocate at 0x0. It will confuse code that
225 * checks pointers against NULL. Skip the first 8
226 * bytes so we start at a nice even number.
231 start = round_up(start, align);
232 if ((start + size) > end)
235 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
236 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
238 if (status == EFI_SUCCESS) {
244 if (i == map_size / desc_size)
245 status = EFI_NOT_FOUND;
248 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
253 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
256 unsigned long nr_pages;
261 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
262 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
267 * Check the cmdline for a LILO-style initrd= arguments.
269 * We only support loading an initrd from the same filesystem as the
272 static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
273 efi_loaded_image_t *image,
274 struct setup_header *hdr)
276 struct initrd *initrds;
277 unsigned long initrd_addr;
278 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
280 efi_file_io_interface_t *io;
281 efi_file_handle_t *fh;
290 str = (char *)(unsigned long)hdr->cmd_line_ptr;
292 j = 0; /* See close_handles */
297 for (nr_initrds = 0; *str; nr_initrds++) {
298 str = strstr(str, "initrd=");
304 /* Skip any leading slashes */
305 while (*str == '/' || *str == '\\')
308 while (*str && *str != ' ' && *str != '\n')
315 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
317 nr_initrds * sizeof(*initrds),
319 if (status != EFI_SUCCESS) {
320 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
324 str = (char *)(unsigned long)hdr->cmd_line_ptr;
325 for (i = 0; i < nr_initrds; i++) {
326 struct initrd *initrd;
327 efi_file_handle_t *h;
328 efi_file_info_t *info;
329 efi_char16_t filename_16[256];
330 unsigned long info_sz;
331 efi_guid_t info_guid = EFI_FILE_INFO_ID;
335 str = strstr(str, "initrd=");
341 initrd = &initrds[i];
344 /* Skip any leading slashes */
345 while (*str == '/' || *str == '\\')
348 while (*str && *str != ' ' && *str != '\n') {
349 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
362 /* Only open the volume once. */
364 efi_boot_services_t *boottime;
366 boottime = sys_table_arg->boottime;
368 status = efi_call_phys3(boottime->handle_protocol,
369 image->device_handle, &fs_proto, &io);
370 if (status != EFI_SUCCESS) {
371 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
375 status = efi_call_phys2(io->open_volume, io, &fh);
376 if (status != EFI_SUCCESS) {
377 efi_printk(sys_table_arg, "Failed to open volume\n");
382 status = efi_call_phys5(fh->open, fh, &h, filename_16,
383 EFI_FILE_MODE_READ, (u64)0);
384 if (status != EFI_SUCCESS) {
385 efi_printk(sys_table_arg, "Failed to open initrd file: ");
386 efi_char16_printk(sys_table_arg, filename_16);
387 efi_printk(sys_table_arg, "\n");
394 status = efi_call_phys4(h->get_info, h, &info_guid,
396 if (status != EFI_BUFFER_TOO_SMALL) {
397 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
402 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
403 EFI_LOADER_DATA, info_sz, &info);
404 if (status != EFI_SUCCESS) {
405 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
409 status = efi_call_phys4(h->get_info, h, &info_guid,
411 if (status == EFI_BUFFER_TOO_SMALL) {
412 efi_call_phys1(sys_table_arg->boottime->free_pool,
417 file_sz = info->file_size;
418 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
420 if (status != EFI_SUCCESS) {
421 efi_printk(sys_table_arg, "Failed to get initrd info\n");
425 initrd->size = file_sz;
426 initrd_total += file_sz;
433 * Multiple initrd's need to be at consecutive
434 * addresses in memory, so allocate enough memory for
437 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
438 &initrd_addr, hdr->initrd_addr_max);
439 if (status != EFI_SUCCESS) {
440 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
444 /* We've run out of free low memory. */
445 if (initrd_addr > hdr->initrd_addr_max) {
446 efi_printk(sys_table_arg, "We've run out of free low memory\n");
447 status = EFI_INVALID_PARAMETER;
448 goto free_initrd_total;
452 for (j = 0; j < nr_initrds; j++) {
455 size = initrds[j].size;
458 if (size > EFI_READ_CHUNK_SIZE)
459 chunksize = EFI_READ_CHUNK_SIZE;
462 status = efi_call_phys3(fh->read,
465 if (status != EFI_SUCCESS) {
466 efi_printk(sys_table_arg, "Failed to read initrd\n");
467 goto free_initrd_total;
473 efi_call_phys1(fh->close, initrds[j].handle);
478 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
480 hdr->ramdisk_image = initrd_addr;
481 hdr->ramdisk_size = initrd_total;
486 efi_free(sys_table_arg, initrd_total, initrd_addr);
489 for (k = j; k < i; k++)
490 efi_call_phys1(fh->close, initrds[k].handle);
492 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
494 hdr->ramdisk_image = 0;
495 hdr->ramdisk_size = 0;
500 * Relocate a kernel image, either compressed or uncompressed.
501 * In the ARM64 case, all kernel images are currently
502 * uncompressed, and as such when we relocate it we need to
503 * allocate additional space for the BSS segment. Any low
504 * memory that this function should avoid needs to be
505 * unavailable in the EFI memory map, as if the preferred
506 * address is not available the lowest available address will
509 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
510 unsigned long *image_addr,
511 unsigned long image_size,
512 unsigned long alloc_size,
513 unsigned long preferred_addr,
514 unsigned long alignment)
516 unsigned long cur_image_addr;
517 unsigned long new_addr = 0;
519 unsigned long nr_pages;
520 efi_physical_addr_t efi_addr = preferred_addr;
522 if (!image_addr || !image_size || !alloc_size)
523 return EFI_INVALID_PARAMETER;
524 if (alloc_size < image_size)
525 return EFI_INVALID_PARAMETER;
527 cur_image_addr = *image_addr;
530 * The EFI firmware loader could have placed the kernel image
531 * anywhere in memory, but the kernel has restrictions on the
532 * max physical address it can run at. Some architectures
533 * also have a prefered address, so first try to relocate
534 * to the preferred address. If that fails, allocate as low
535 * as possible while respecting the required alignment.
537 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
538 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
539 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
540 nr_pages, &efi_addr);
543 * If preferred address allocation failed allocate as low as
546 if (status != EFI_SUCCESS) {
547 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
550 if (status != EFI_SUCCESS) {
551 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
556 * We know source/dest won't overlap since both memory ranges
557 * have been allocated by UEFI, so we can safely use memcpy.
559 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
560 /* Zero any extra space we may have allocated for BSS. */
561 memset((void *)(new_addr + image_size), alloc_size - image_size, 0);
563 /* Return the new address of the relocated image. */
564 *image_addr = new_addr;
570 * Convert the unicode UEFI command line to ASCII to pass to kernel.
571 * Size of memory allocated return in *cmd_line_len.
572 * Returns NULL on error.
574 static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
575 efi_loaded_image_t *image,
580 unsigned long cmdline_addr = 0;
581 int load_options_size = image->load_options_size / 2; /* ASCII */
582 void *options = image->load_options;
583 int options_size = 0;
590 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
596 if (options_size == 0) {
597 /* No command line options, so return empty string*/
602 options_size++; /* NUL termination */
605 * For ARM, allocate at a high address to avoid reserved
606 * regions at low addresses that we don't know the specfics of
607 * at the time we are processing the command line.
609 status = efi_high_alloc(sys_table_arg, options_size, 0,
610 &cmdline_addr, 0xfffff000);
612 status = efi_low_alloc(sys_table_arg, options_size, 0,
615 if (status != EFI_SUCCESS)
618 s1 = (u8 *)cmdline_addr;
621 for (i = 0; i < options_size - 1; i++)
626 *cmd_line_len = options_size;
627 return (char *)cmdline_addr;