1
2
3
4
5
6
7
8
9
10#include <linux/efi.h>
11#include <asm/efi.h>
12#include <asm/memory.h>
13#include <asm/sections.h>
14#include <asm/sysreg.h>
15
16#include "efistub.h"
17
18efi_status_t check_platform_features(void)
19{
20 u64 tg;
21
22
23 if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
24 return EFI_SUCCESS;
25
26 tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
27 if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
28 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29 efi_err("This 64 KB granular kernel is not supported by your CPU\n");
30 else
31 efi_err("This 16 KB granular kernel is not supported by your CPU\n");
32 return EFI_UNSUPPORTED;
33 }
34 return EFI_SUCCESS;
35}
36
37
38
39
40
41
42
43static bool check_image_region(u64 base, u64 size)
44{
45 unsigned long map_size, desc_size, buff_size;
46 efi_memory_desc_t *memory_map;
47 struct efi_boot_memmap map;
48 efi_status_t status;
49 bool ret = false;
50 int map_offset;
51
52 map.map = &memory_map;
53 map.map_size = &map_size;
54 map.desc_size = &desc_size;
55 map.desc_ver = NULL;
56 map.key_ptr = NULL;
57 map.buff_size = &buff_size;
58
59 status = efi_get_memory_map(&map);
60 if (status != EFI_SUCCESS)
61 return false;
62
63 for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
64 efi_memory_desc_t *md = (void *)memory_map + map_offset;
65 u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
66
67
68
69
70
71 if (base >= md->phys_addr && base < end) {
72 ret = (base + size) <= end;
73 break;
74 }
75 }
76
77 efi_bs_call(free_pool, memory_map);
78
79 return ret;
80}
81
82efi_status_t handle_kernel_image(unsigned long *image_addr,
83 unsigned long *image_size,
84 unsigned long *reserve_addr,
85 unsigned long *reserve_size,
86 efi_loaded_image_t *image,
87 efi_handle_t image_handle)
88{
89 efi_status_t status;
90 unsigned long kernel_size, kernel_memsize = 0;
91 u32 phys_seed = 0;
92
93
94
95
96
97
98
99
100
101 u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
102
103 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
104 efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
105 void *p;
106
107 if (efi_nokaslr) {
108 efi_info("KASLR disabled on kernel command line\n");
109 } else if (efi_bs_call(handle_protocol, image_handle,
110 &li_fixed_proto, &p) == EFI_SUCCESS) {
111 efi_info("Image placement fixed by loader\n");
112 } else {
113 status = efi_get_random_bytes(sizeof(phys_seed),
114 (u8 *)&phys_seed);
115 if (status == EFI_NOT_FOUND) {
116 efi_info("EFI_RNG_PROTOCOL unavailable\n");
117 efi_nokaslr = true;
118 } else if (status != EFI_SUCCESS) {
119 efi_err("efi_get_random_bytes() failed (0x%lx)\n",
120 status);
121 efi_nokaslr = true;
122 }
123 }
124 }
125
126 if (image->image_base != _text)
127 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
128
129 if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
130 efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
131 SEGMENT_ALIGN >> 10);
132
133 kernel_size = _edata - _text;
134 kernel_memsize = kernel_size + (_end - _edata);
135 *reserve_size = kernel_memsize;
136
137 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
138
139
140
141
142 status = efi_random_alloc(*reserve_size, min_kimg_align,
143 reserve_addr, phys_seed);
144 if (status != EFI_SUCCESS)
145 efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
146 } else {
147 status = EFI_OUT_OF_RESOURCES;
148 }
149
150 if (status != EFI_SUCCESS) {
151 if (!check_image_region((u64)_text, kernel_memsize)) {
152 efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
153 } else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
154
155
156
157
158 *image_addr = (u64)_text;
159 *reserve_size = 0;
160 return EFI_SUCCESS;
161 }
162
163 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
164 ULONG_MAX, min_kimg_align);
165
166 if (status != EFI_SUCCESS) {
167 efi_err("Failed to relocate kernel\n");
168 *reserve_size = 0;
169 return status;
170 }
171 }
172
173 *image_addr = *reserve_addr;
174 memcpy((void *)*image_addr, _text, kernel_size);
175
176 return EFI_SUCCESS;
177}
178