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 efi_memory_desc_t *m = NULL;
59 *map_size = sizeof(*m) * 32;
62 * Add an additional efi_memory_desc_t because we're doing an
63 * allocation which may be in a new descriptor region.
65 *map_size += sizeof(*m);
66 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
67 EFI_LOADER_DATA, *map_size, (void **)&m);
68 if (status != EFI_SUCCESS)
71 status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
72 map_size, m, &key, desc_size, &desc_version);
73 if (status == EFI_BUFFER_TOO_SMALL) {
74 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
78 if (status != EFI_SUCCESS)
79 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
87 * Allocate at the highest possible address that is not above 'max'.
89 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
90 unsigned long size, unsigned long align,
91 unsigned long *addr, unsigned long max)
93 unsigned long map_size, desc_size;
94 efi_memory_desc_t *map;
96 unsigned long nr_pages;
100 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size);
101 if (status != EFI_SUCCESS)
105 * Enforce minimum alignment that EFI requires when requesting
106 * a specific address. We are doing page-based allocations,
107 * so we must be aligned to a page.
109 if (align < EFI_PAGE_SIZE)
110 align = EFI_PAGE_SIZE;
112 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
114 for (i = 0; i < map_size / desc_size; i++) {
115 efi_memory_desc_t *desc;
116 unsigned long m = (unsigned long)map;
119 desc = (efi_memory_desc_t *)(m + (i * desc_size));
120 if (desc->type != EFI_CONVENTIONAL_MEMORY)
123 if (desc->num_pages < nr_pages)
126 start = desc->phys_addr;
127 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
129 if ((start + size) > end || (start + size) > max)
132 if (end - size > max)
135 if (round_down(end - size, align) < start)
138 start = round_down(end - size, align);
141 * Don't allocate at 0x0. It will confuse code that
142 * checks pointers against NULL.
147 if (start > max_addr)
152 status = EFI_NOT_FOUND;
154 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
155 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
156 nr_pages, &max_addr);
157 if (status != EFI_SUCCESS) {
167 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
174 * Allocate at the lowest possible address.
176 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
177 unsigned long size, unsigned long align,
180 unsigned long map_size, desc_size;
181 efi_memory_desc_t *map;
183 unsigned long nr_pages;
186 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size);
187 if (status != EFI_SUCCESS)
191 * Enforce minimum alignment that EFI requires when requesting
192 * a specific address. We are doing page-based allocations,
193 * so we must be aligned to a page.
195 if (align < EFI_PAGE_SIZE)
196 align = EFI_PAGE_SIZE;
198 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
199 for (i = 0; i < map_size / desc_size; i++) {
200 efi_memory_desc_t *desc;
201 unsigned long m = (unsigned long)map;
204 desc = (efi_memory_desc_t *)(m + (i * desc_size));
206 if (desc->type != EFI_CONVENTIONAL_MEMORY)
209 if (desc->num_pages < nr_pages)
212 start = desc->phys_addr;
213 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
216 * Don't allocate at 0x0. It will confuse code that
217 * checks pointers against NULL. Skip the first 8
218 * bytes so we start at a nice even number.
223 start = round_up(start, align);
224 if ((start + size) > end)
227 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
228 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
230 if (status == EFI_SUCCESS) {
236 if (i == map_size / desc_size)
237 status = EFI_NOT_FOUND;
240 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
245 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
248 unsigned long nr_pages;
250 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
251 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
256 * Check the cmdline for a LILO-style initrd= arguments.
258 * We only support loading an initrd from the same filesystem as the
261 static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
262 efi_loaded_image_t *image,
263 struct setup_header *hdr)
265 struct initrd *initrds;
266 unsigned long initrd_addr;
267 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
269 efi_file_io_interface_t *io;
270 efi_file_handle_t *fh;
279 str = (char *)(unsigned long)hdr->cmd_line_ptr;
281 j = 0; /* See close_handles */
286 for (nr_initrds = 0; *str; nr_initrds++) {
287 str = strstr(str, "initrd=");
293 /* Skip any leading slashes */
294 while (*str == '/' || *str == '\\')
297 while (*str && *str != ' ' && *str != '\n')
304 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
306 nr_initrds * sizeof(*initrds),
308 if (status != EFI_SUCCESS) {
309 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
313 str = (char *)(unsigned long)hdr->cmd_line_ptr;
314 for (i = 0; i < nr_initrds; i++) {
315 struct initrd *initrd;
316 efi_file_handle_t *h;
317 efi_file_info_t *info;
318 efi_char16_t filename_16[256];
319 unsigned long info_sz;
320 efi_guid_t info_guid = EFI_FILE_INFO_ID;
324 str = strstr(str, "initrd=");
330 initrd = &initrds[i];
333 /* Skip any leading slashes */
334 while (*str == '/' || *str == '\\')
337 while (*str && *str != ' ' && *str != '\n') {
338 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
351 /* Only open the volume once. */
353 efi_boot_services_t *boottime;
355 boottime = sys_table_arg->boottime;
357 status = efi_call_phys3(boottime->handle_protocol,
358 image->device_handle, &fs_proto, &io);
359 if (status != EFI_SUCCESS) {
360 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
364 status = efi_call_phys2(io->open_volume, io, &fh);
365 if (status != EFI_SUCCESS) {
366 efi_printk(sys_table_arg, "Failed to open volume\n");
371 status = efi_call_phys5(fh->open, fh, &h, filename_16,
372 EFI_FILE_MODE_READ, (u64)0);
373 if (status != EFI_SUCCESS) {
374 efi_printk(sys_table_arg, "Failed to open initrd file: ");
375 efi_char16_printk(sys_table_arg, filename_16);
376 efi_printk(sys_table_arg, "\n");
383 status = efi_call_phys4(h->get_info, h, &info_guid,
385 if (status != EFI_BUFFER_TOO_SMALL) {
386 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
391 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
392 EFI_LOADER_DATA, info_sz, &info);
393 if (status != EFI_SUCCESS) {
394 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
398 status = efi_call_phys4(h->get_info, h, &info_guid,
400 if (status == EFI_BUFFER_TOO_SMALL) {
401 efi_call_phys1(sys_table_arg->boottime->free_pool,
406 file_sz = info->file_size;
407 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
409 if (status != EFI_SUCCESS) {
410 efi_printk(sys_table_arg, "Failed to get initrd info\n");
414 initrd->size = file_sz;
415 initrd_total += file_sz;
422 * Multiple initrd's need to be at consecutive
423 * addresses in memory, so allocate enough memory for
426 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
427 &initrd_addr, hdr->initrd_addr_max);
428 if (status != EFI_SUCCESS) {
429 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
433 /* We've run out of free low memory. */
434 if (initrd_addr > hdr->initrd_addr_max) {
435 efi_printk(sys_table_arg, "We've run out of free low memory\n");
436 status = EFI_INVALID_PARAMETER;
437 goto free_initrd_total;
441 for (j = 0; j < nr_initrds; j++) {
444 size = initrds[j].size;
447 if (size > EFI_READ_CHUNK_SIZE)
448 chunksize = EFI_READ_CHUNK_SIZE;
451 status = efi_call_phys3(fh->read,
454 if (status != EFI_SUCCESS) {
455 efi_printk(sys_table_arg, "Failed to read initrd\n");
456 goto free_initrd_total;
462 efi_call_phys1(fh->close, initrds[j].handle);
467 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
469 hdr->ramdisk_image = initrd_addr;
470 hdr->ramdisk_size = initrd_total;
475 efi_free(sys_table_arg, initrd_total, initrd_addr);
478 for (k = j; k < i; k++)
479 efi_call_phys1(fh->close, initrds[k].handle);
481 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
483 hdr->ramdisk_image = 0;
484 hdr->ramdisk_size = 0;
489 * Relocate a kernel image, either compressed or uncompressed.
490 * In the ARM64 case, all kernel images are currently
491 * uncompressed, and as such when we relocate it we need to
492 * allocate additional space for the BSS segment. Any low
493 * memory that this function should avoid needs to be
494 * unavailable in the EFI memory map, as if the preferred
495 * address is not available the lowest available address will
498 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
499 unsigned long *image_addr,
500 unsigned long image_size,
501 unsigned long alloc_size,
502 unsigned long preferred_addr,
503 unsigned long alignment)
505 unsigned long cur_image_addr;
506 unsigned long new_addr = 0;
508 unsigned long nr_pages;
509 efi_physical_addr_t efi_addr = preferred_addr;
511 if (!image_addr || !image_size || !alloc_size)
512 return EFI_INVALID_PARAMETER;
513 if (alloc_size < image_size)
514 return EFI_INVALID_PARAMETER;
516 cur_image_addr = *image_addr;
519 * The EFI firmware loader could have placed the kernel image
520 * anywhere in memory, but the kernel has restrictions on the
521 * max physical address it can run at. Some architectures
522 * also have a prefered address, so first try to relocate
523 * to the preferred address. If that fails, allocate as low
524 * as possible while respecting the required alignment.
526 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
527 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
528 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
529 nr_pages, &efi_addr);
532 * If preferred address allocation failed allocate as low as
535 if (status != EFI_SUCCESS) {
536 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
539 if (status != EFI_SUCCESS) {
540 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
545 * We know source/dest won't overlap since both memory ranges
546 * have been allocated by UEFI, so we can safely use memcpy.
548 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
549 /* Zero any extra space we may have allocated for BSS. */
550 memset((void *)(new_addr + image_size), alloc_size - image_size, 0);
552 /* Return the new address of the relocated image. */
553 *image_addr = new_addr;
559 * Convert the unicode UEFI command line to ASCII to pass to kernel.
560 * Size of memory allocated return in *cmd_line_len.
561 * Returns NULL on error.
563 static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
564 efi_loaded_image_t *image,
569 unsigned long cmdline_addr = 0;
570 int load_options_size = image->load_options_size / 2; /* ASCII */
571 void *options = image->load_options;
572 int options_size = 0;
579 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
585 if (options_size == 0) {
586 /* No command line options, so return empty string*/
591 options_size++; /* NUL termination */
594 * For ARM, allocate at a high address to avoid reserved
595 * regions at low addresses that we don't know the specfics of
596 * at the time we are processing the command line.
598 status = efi_high_alloc(sys_table_arg, options_size, 0,
599 &cmdline_addr, 0xfffff000);
601 status = efi_low_alloc(sys_table_arg, options_size, 0,
604 if (status != EFI_SUCCESS)
607 s1 = (u8 *)cmdline_addr;
610 for (i = 0; i < options_size - 1; i++)
615 *cmd_line_len = options_size;
616 return (char *)cmdline_addr;