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) {
173 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
180 * Allocate at the lowest possible address.
182 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
183 unsigned long size, unsigned long align,
186 unsigned long map_size, desc_size;
187 efi_memory_desc_t *map;
189 unsigned long nr_pages;
192 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
194 if (status != EFI_SUCCESS)
198 * Enforce minimum alignment that EFI requires when requesting
199 * a specific address. We are doing page-based allocations,
200 * so we must be aligned to a page.
202 if (align < EFI_PAGE_SIZE)
203 align = EFI_PAGE_SIZE;
205 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
206 for (i = 0; i < map_size / desc_size; i++) {
207 efi_memory_desc_t *desc;
208 unsigned long m = (unsigned long)map;
211 desc = (efi_memory_desc_t *)(m + (i * desc_size));
213 if (desc->type != EFI_CONVENTIONAL_MEMORY)
216 if (desc->num_pages < nr_pages)
219 start = desc->phys_addr;
220 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
223 * Don't allocate at 0x0. It will confuse code that
224 * checks pointers against NULL. Skip the first 8
225 * bytes so we start at a nice even number.
230 start = round_up(start, align);
231 if ((start + size) > end)
234 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
235 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
237 if (status == EFI_SUCCESS) {
243 if (i == map_size / desc_size)
244 status = EFI_NOT_FOUND;
246 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
251 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
254 unsigned long nr_pages;
259 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
260 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
265 * Check the cmdline for a LILO-style file= arguments.
267 * We only support loading a file from the same filesystem as
270 static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
271 efi_loaded_image_t *image,
272 char *cmd_line, char *option_string,
273 unsigned long max_addr,
274 unsigned long *load_addr,
275 unsigned long *load_size)
277 struct file_info *files;
278 unsigned long file_addr;
279 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
281 efi_file_io_interface_t *io;
282 efi_file_handle_t *fh;
293 j = 0; /* See close_handles */
295 if (!load_addr || !load_size)
296 return EFI_INVALID_PARAMETER;
304 for (nr_files = 0; *str; nr_files++) {
305 str = strstr(str, option_string);
309 str += strlen(option_string);
311 /* Skip any leading slashes */
312 while (*str == '/' || *str == '\\')
315 while (*str && *str != ' ' && *str != '\n')
322 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
324 nr_files * sizeof(*files),
326 if (status != EFI_SUCCESS) {
327 efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
332 for (i = 0; i < nr_files; i++) {
333 struct file_info *file;
334 efi_file_handle_t *h;
335 efi_file_info_t *info;
336 efi_char16_t filename_16[256];
337 unsigned long info_sz;
338 efi_guid_t info_guid = EFI_FILE_INFO_ID;
342 str = strstr(str, option_string);
346 str += strlen(option_string);
351 /* Skip any leading slashes */
352 while (*str == '/' || *str == '\\')
355 while (*str && *str != ' ' && *str != '\n') {
356 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
369 /* Only open the volume once. */
371 efi_boot_services_t *boottime;
373 boottime = sys_table_arg->boottime;
375 status = efi_call_phys3(boottime->handle_protocol,
376 image->device_handle, &fs_proto,
378 if (status != EFI_SUCCESS) {
379 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
383 status = efi_call_phys2(io->open_volume, io, &fh);
384 if (status != EFI_SUCCESS) {
385 efi_printk(sys_table_arg, "Failed to open volume\n");
390 status = efi_call_phys5(fh->open, fh, &h, filename_16,
391 EFI_FILE_MODE_READ, (u64)0);
392 if (status != EFI_SUCCESS) {
393 efi_printk(sys_table_arg, "Failed to open file: ");
394 efi_char16_printk(sys_table_arg, filename_16);
395 efi_printk(sys_table_arg, "\n");
402 status = efi_call_phys4(h->get_info, h, &info_guid,
404 if (status != EFI_BUFFER_TOO_SMALL) {
405 efi_printk(sys_table_arg, "Failed to get file info size\n");
410 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
411 EFI_LOADER_DATA, info_sz,
413 if (status != EFI_SUCCESS) {
414 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
418 status = efi_call_phys4(h->get_info, h, &info_guid,
420 if (status == EFI_BUFFER_TOO_SMALL) {
421 efi_call_phys1(sys_table_arg->boottime->free_pool,
426 file_sz = info->file_size;
427 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
429 if (status != EFI_SUCCESS) {
430 efi_printk(sys_table_arg, "Failed to get file info\n");
434 file->size = file_sz;
435 file_size_total += file_sz;
438 if (file_size_total) {
442 * Multiple files need to be at consecutive addresses in memory,
443 * so allocate enough memory for all the files. This is used
444 * for loading multiple files.
446 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
447 &file_addr, max_addr);
448 if (status != EFI_SUCCESS) {
449 efi_printk(sys_table_arg, "Failed to alloc highmem for files\n");
453 /* We've run out of free low memory. */
454 if (file_addr > max_addr) {
455 efi_printk(sys_table_arg, "We've run out of free low memory\n");
456 status = EFI_INVALID_PARAMETER;
457 goto free_file_total;
461 for (j = 0; j < nr_files; j++) {
464 size = files[j].size;
466 unsigned long chunksize;
467 if (size > EFI_READ_CHUNK_SIZE)
468 chunksize = EFI_READ_CHUNK_SIZE;
471 status = efi_call_phys3(fh->read,
475 if (status != EFI_SUCCESS) {
476 efi_printk(sys_table_arg, "Failed to read file\n");
477 goto free_file_total;
483 efi_call_phys1(fh->close, files[j].handle);
488 efi_call_phys1(sys_table_arg->boottime->free_pool, files);
490 *load_addr = file_addr;
491 *load_size = file_size_total;
496 efi_free(sys_table_arg, file_size_total, file_addr);
499 for (k = j; k < i; k++)
500 efi_call_phys1(fh->close, files[k].handle);
502 efi_call_phys1(sys_table_arg->boottime->free_pool, files);
510 * Relocate a kernel image, either compressed or uncompressed.
511 * In the ARM64 case, all kernel images are currently
512 * uncompressed, and as such when we relocate it we need to
513 * allocate additional space for the BSS segment. Any low
514 * memory that this function should avoid needs to be
515 * unavailable in the EFI memory map, as if the preferred
516 * address is not available the lowest available address will
519 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
520 unsigned long *image_addr,
521 unsigned long image_size,
522 unsigned long alloc_size,
523 unsigned long preferred_addr,
524 unsigned long alignment)
526 unsigned long cur_image_addr;
527 unsigned long new_addr = 0;
529 unsigned long nr_pages;
530 efi_physical_addr_t efi_addr = preferred_addr;
532 if (!image_addr || !image_size || !alloc_size)
533 return EFI_INVALID_PARAMETER;
534 if (alloc_size < image_size)
535 return EFI_INVALID_PARAMETER;
537 cur_image_addr = *image_addr;
540 * The EFI firmware loader could have placed the kernel image
541 * anywhere in memory, but the kernel has restrictions on the
542 * max physical address it can run at. Some architectures
543 * also have a prefered address, so first try to relocate
544 * to the preferred address. If that fails, allocate as low
545 * as possible while respecting the required alignment.
547 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
548 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
549 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
550 nr_pages, &efi_addr);
553 * If preferred address allocation failed allocate as low as
556 if (status != EFI_SUCCESS) {
557 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
560 if (status != EFI_SUCCESS) {
561 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
566 * We know source/dest won't overlap since both memory ranges
567 * have been allocated by UEFI, so we can safely use memcpy.
569 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
571 /* Return the new address of the relocated image. */
572 *image_addr = new_addr;
578 * Convert the unicode UEFI command line to ASCII to pass to kernel.
579 * Size of memory allocated return in *cmd_line_len.
580 * Returns NULL on error.
582 static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
583 efi_loaded_image_t *image,
588 unsigned long cmdline_addr = 0;
589 int load_options_size = image->load_options_size / 2; /* ASCII */
590 void *options = image->load_options;
591 int options_size = 0;
598 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
604 if (options_size == 0) {
605 /* No command line options, so return empty string*/
610 options_size++; /* NUL termination */
613 * For ARM, allocate at a high address to avoid reserved
614 * regions at low addresses that we don't know the specfics of
615 * at the time we are processing the command line.
617 status = efi_high_alloc(sys_table_arg, options_size, 0,
618 &cmdline_addr, 0xfffff000);
620 status = efi_low_alloc(sys_table_arg, options_size, 0,
623 if (status != EFI_SUCCESS)
626 s1 = (u8 *)cmdline_addr;
629 for (i = 0; i < options_size - 1; i++)
634 *cmd_line_len = options_size;
635 return (char *)cmdline_addr;