2 * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org>
4 * This file implements the EFI boot stub for the arm64 kernel.
5 * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
14 * To prevent the compiler from emitting GOT-indirected (and thus absolute)
15 * references to the section markers, override their visibility as 'hidden'
17 #pragma GCC visibility push(hidden)
18 #include <asm/sections.h>
19 #pragma GCC visibility pop
21 #include <linux/efi.h>
24 efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg,
25 unsigned long *image_addr,
26 unsigned long *image_size,
27 unsigned long *reserve_addr,
28 unsigned long *reserve_size,
29 unsigned long dram_base,
30 efi_loaded_image_t *image)
33 unsigned long kernel_size, kernel_memsize = 0;
34 unsigned long nr_pages;
35 void *old_image_addr = (void *)*image_addr;
36 unsigned long preferred_offset;
39 * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond
40 * a 2 MB aligned base, which itself may be lower than dram_base, as
41 * long as the resulting offset equals or exceeds it.
43 preferred_offset = round_down(dram_base, SZ_2M) + TEXT_OFFSET;
44 if (preferred_offset < dram_base)
45 preferred_offset += SZ_2M;
47 /* Relocate the image, if required. */
48 kernel_size = _edata - _text;
49 if (*image_addr != preferred_offset) {
50 kernel_memsize = kernel_size + (_end - _edata);
53 * First, try a straight allocation at the preferred offset.
54 * This will work around the issue where, if dram_base == 0x0,
55 * efi_low_alloc() refuses to allocate at 0x0 (to prevent the
56 * address of the allocation to be mistaken for a FAIL return
57 * value or a NULL pointer). It will also ensure that, on
58 * platforms where the [dram_base, dram_base + TEXT_OFFSET)
59 * interval is partially occupied by the firmware (like on APM
60 * Mustang), we can still place the kernel at the address
61 * 'dram_base + TEXT_OFFSET'.
63 *image_addr = *reserve_addr = preferred_offset;
64 nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) /
66 status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,
67 EFI_LOADER_DATA, nr_pages,
68 (efi_physical_addr_t *)reserve_addr);
69 if (status != EFI_SUCCESS) {
70 kernel_memsize += TEXT_OFFSET;
71 status = efi_low_alloc(sys_table_arg, kernel_memsize,
74 if (status != EFI_SUCCESS) {
75 pr_efi_err(sys_table_arg, "Failed to relocate kernel\n");
78 *image_addr = *reserve_addr + TEXT_OFFSET;
80 memcpy((void *)*image_addr, old_image_addr, kernel_size);
81 *reserve_size = kernel_memsize;