Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org> |
| 3 | * |
| 4 | * This file implements the EFI boot stub for the arm64 kernel. |
| 5 | * Adapted from ARM version by Mark Salter <msalter@redhat.com> |
| 6 | * |
| 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. |
| 10 | * |
| 11 | */ |
| 12 | #include <linux/efi.h> |
Ard Biesheuvel | a13b007 | 2014-07-02 14:54:41 +0200 | [diff] [blame] | 13 | #include <asm/efi.h> |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 14 | #include <asm/sections.h> |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 15 | |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 16 | efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg, |
Ard Biesheuvel | ddeeefe | 2015-01-12 20:28:20 +0000 | [diff] [blame] | 17 | unsigned long *image_addr, |
| 18 | unsigned long *image_size, |
| 19 | unsigned long *reserve_addr, |
| 20 | unsigned long *reserve_size, |
| 21 | unsigned long dram_base, |
| 22 | efi_loaded_image_t *image) |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 23 | { |
| 24 | efi_status_t status; |
| 25 | unsigned long kernel_size, kernel_memsize = 0; |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 26 | unsigned long nr_pages; |
| 27 | void *old_image_addr = (void *)*image_addr; |
Ard Biesheuvel | 73effcc | 2015-10-29 15:07:25 +0100 | [diff] [blame] | 28 | unsigned long preferred_offset; |
| 29 | |
| 30 | /* |
| 31 | * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond |
| 32 | * a 2 MB aligned base, which itself may be lower than dram_base, as |
| 33 | * long as the resulting offset equals or exceeds it. |
| 34 | */ |
| 35 | preferred_offset = round_down(dram_base, SZ_2M) + TEXT_OFFSET; |
| 36 | if (preferred_offset < dram_base) |
| 37 | preferred_offset += SZ_2M; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 38 | |
| 39 | /* Relocate the image, if required. */ |
| 40 | kernel_size = _edata - _text; |
Ard Biesheuvel | 73effcc | 2015-10-29 15:07:25 +0100 | [diff] [blame] | 41 | if (*image_addr != preferred_offset) { |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 42 | kernel_memsize = kernel_size + (_end - _edata); |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * First, try a straight allocation at the preferred offset. |
| 46 | * This will work around the issue where, if dram_base == 0x0, |
| 47 | * efi_low_alloc() refuses to allocate at 0x0 (to prevent the |
| 48 | * address of the allocation to be mistaken for a FAIL return |
| 49 | * value or a NULL pointer). It will also ensure that, on |
| 50 | * platforms where the [dram_base, dram_base + TEXT_OFFSET) |
| 51 | * interval is partially occupied by the firmware (like on APM |
| 52 | * Mustang), we can still place the kernel at the address |
| 53 | * 'dram_base + TEXT_OFFSET'. |
| 54 | */ |
Ard Biesheuvel | 73effcc | 2015-10-29 15:07:25 +0100 | [diff] [blame] | 55 | *image_addr = *reserve_addr = preferred_offset; |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 56 | nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) / |
| 57 | EFI_PAGE_SIZE; |
| 58 | status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS, |
| 59 | EFI_LOADER_DATA, nr_pages, |
| 60 | (efi_physical_addr_t *)reserve_addr); |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 61 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 62 | kernel_memsize += TEXT_OFFSET; |
| 63 | status = efi_low_alloc(sys_table_arg, kernel_memsize, |
| 64 | SZ_2M, reserve_addr); |
| 65 | |
| 66 | if (status != EFI_SUCCESS) { |
| 67 | pr_efi_err(sys_table_arg, "Failed to relocate kernel\n"); |
| 68 | return status; |
| 69 | } |
| 70 | *image_addr = *reserve_addr + TEXT_OFFSET; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 71 | } |
Ard Biesheuvel | e38457c | 2015-07-24 12:38:27 +0100 | [diff] [blame] | 72 | memcpy((void *)*image_addr, old_image_addr, kernel_size); |
| 73 | *reserve_size = kernel_memsize; |
Mark Salter | 3c7f255 | 2014-04-15 22:47:52 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | return EFI_SUCCESS; |
| 78 | } |