efi/libstub: arm64: Remap relocated image with strict permissions
[linux-2.6-block.git] / drivers / firmware / efi / libstub / arm64-stub.c
CommitLineData
4febfb8d 1// SPDX-License-Identifier: GPL-2.0
3c7f2550
MS
2/*
3 * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org>
4 *
5 * This file implements the EFI boot stub for the arm64 kernel.
6 * Adapted from ARM version by Mark Salter <msalter@redhat.com>
3c7f2550 7 */
0426a4e6 8
0426a4e6 9
3c7f2550 10#include <linux/efi.h>
a13b0077 11#include <asm/efi.h>
170976bc 12#include <asm/memory.h>
6f05106e 13#include <asm/sections.h>
3c7f2550 14
2b5fe07a
AB
15#include "efistub.h"
16
5b94046e
AB
17/*
18 * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
19 * to provide space, and fail to zero it). Check for this condition by double
20 * checking that the first and the last byte of the image are covered by the
21 * same EFI memory map entry.
22 */
23static bool check_image_region(u64 base, u64 size)
24{
eab31265 25 struct efi_boot_memmap *map;
5b94046e
AB
26 efi_status_t status;
27 bool ret = false;
28 int map_offset;
29
171539f5 30 status = efi_get_memory_map(&map, false);
5b94046e
AB
31 if (status != EFI_SUCCESS)
32 return false;
33
eab31265
AB
34 for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
35 efi_memory_desc_t *md = (void *)map->map + map_offset;
5b94046e
AB
36 u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
37
38 /*
39 * Find the region that covers base, and return whether
40 * it covers base+size bytes.
41 */
42 if (base >= md->phys_addr && base < end) {
43 ret = (base + size) <= end;
44 break;
45 }
46 }
47
eab31265 48 efi_bs_call(free_pool, map);
5b94046e
AB
49
50 return ret;
51}
52
cd33a5c1 53efi_status_t handle_kernel_image(unsigned long *image_addr,
dae31fd2
AB
54 unsigned long *image_size,
55 unsigned long *reserve_addr,
56 unsigned long *reserve_size,
416a9f84
AB
57 efi_loaded_image_t *image,
58 efi_handle_t image_handle)
3c7f2550
MS
59{
60 efi_status_t status;
61786170 61 unsigned long kernel_size, kernel_codesize, kernel_memsize;
5d12da9d 62 u32 phys_seed = 0;
895bc3a1 63 u64 min_kimg_align = efi_get_kimg_min_align();
3a262423 64
2b5fe07a 65 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
07768c55
AB
66 efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
67 void *p;
68
69 if (efi_nokaslr) {
70 efi_info("KASLR disabled on kernel command line\n");
71 } else if (efi_bs_call(handle_protocol, image_handle,
72 &li_fixed_proto, &p) == EFI_SUCCESS) {
73 efi_info("Image placement fixed by loader\n");
74 } else {
cd33a5c1 75 status = efi_get_random_bytes(sizeof(phys_seed),
2b5fe07a
AB
76 (u8 *)&phys_seed);
77 if (status == EFI_NOT_FOUND) {
1c761ee9 78 efi_info("EFI_RNG_PROTOCOL unavailable\n");
d32de913 79 efi_nokaslr = true;
2b5fe07a 80 } else if (status != EFI_SUCCESS) {
1c761ee9 81 efi_err("efi_get_random_bytes() failed (0x%lx)\n",
d32de913
AB
82 status);
83 efi_nokaslr = true;
2b5fe07a 84 }
2b5fe07a
AB
85 }
86 }
73effccb 87
82046702 88 if (image->image_base != _text)
793473c2 89 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
3c7f2550 90
e9b7c3a4
MC
91 if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
92 efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
93 SEGMENT_ALIGN >> 10);
c32ac11d 94
3c7f2550 95 kernel_size = _edata - _text;
61786170 96 kernel_codesize = __inittext_end - _text;
2b5fe07a 97 kernel_memsize = kernel_size + (_end - _edata);
120dc60d 98 *reserve_size = kernel_memsize;
2b5fe07a
AB
99
100 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
101 /*
102 * If KASLR is enabled, and we have some randomness available,
103 * locate the kernel at a randomized offset in physical memory.
104 */
3a262423 105 status = efi_random_alloc(*reserve_size, min_kimg_align,
9cf42bca
AB
106 reserve_addr, phys_seed,
107 EFI_LOADER_CODE);
ff80ef5b
AB
108 if (status != EFI_SUCCESS)
109 efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
2b5fe07a 110 } else {
82046702 111 status = EFI_OUT_OF_RESOURCES;
2b5fe07a 112 }
e38457c3 113
2b5fe07a 114 if (status != EFI_SUCCESS) {
5b94046e
AB
115 if (!check_image_region((u64)_text, kernel_memsize)) {
116 efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
a37dac5c
AB
117 } else if (IS_ALIGNED((u64)_text, min_kimg_align) &&
118 (u64)_end < EFI_ALLOC_LIMIT) {
82046702
AB
119 /*
120 * Just execute from wherever we were loaded by the
a37dac5c 121 * UEFI PE/COFF loader if the placement is suitable.
82046702
AB
122 */
123 *image_addr = (u64)_text;
124 *reserve_size = 0;
61786170 125 return EFI_SUCCESS;
82046702
AB
126 }
127
e71356fe 128 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
9cf42bca
AB
129 ULONG_MAX, min_kimg_align,
130 EFI_LOADER_CODE);
2b5fe07a
AB
131
132 if (status != EFI_SUCCESS) {
793473c2 133 efi_err("Failed to relocate kernel\n");
2b5fe07a
AB
134 *reserve_size = 0;
135 return status;
3c7f2550 136 }
3c7f2550 137 }
c2136dce 138
120dc60d 139 *image_addr = *reserve_addr;
c2136dce 140 memcpy((void *)*image_addr, _text, kernel_size);
61786170 141 caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize);
3c60f67b 142 efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
3c7f2550 143
61786170
AB
144 return EFI_SUCCESS;
145}
146
147asmlinkage void primary_entry(void);
148
149unsigned long primary_entry_offset(void)
150{
aaeb3fc6 151 /*
61786170
AB
152 * When built as part of the kernel, the EFI stub cannot branch to the
153 * kernel proper via the image header, as the PE/COFF header is
154 * strictly not part of the in-memory presentation of the image, only
155 * of the file representation. So instead, we need to jump to the
156 * actual entrypoint in the .text region of the image.
aaeb3fc6 157 */
61786170 158 return (char *)primary_entry - _text;
3c7f2550 159}