Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
4 */
5#include <linux/efi.h>
6#include <asm/efi.h>
7
8#include "efistub.h"
9
10static efi_guid_t cpu_state_guid = LINUX_EFI_ARM_CPU_STATE_TABLE_GUID;
11
12struct efi_arm_entry_state *efi_entry_state;
13
14static void get_cpu_state(u32 *cpsr, u32 *sctlr)
15{
16 asm("mrs %0, cpsr" : "=r"(*cpsr));
17 if ((*cpsr & MODE_MASK) == HYP_MODE)
18 asm("mrc p15, 4, %0, c1, c0, 0" : "=r"(*sctlr));
19 else
20 asm("mrc p15, 0, %0, c1, c0, 0" : "=r"(*sctlr));
21}
22
23efi_status_t check_platform_features(void)
24{
25 efi_status_t status;
26 u32 cpsr, sctlr;
27 int block;
28
29 get_cpu_state(&cpsr, &sctlr);
30
31 efi_info("Entering in %s mode with MMU %sabled\n",
32 ((cpsr & MODE_MASK) == HYP_MODE) ? "HYP" : "SVC",
33 (sctlr & 1) ? "en" : "dis");
34
35 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
36 sizeof(*efi_entry_state),
37 (void **)&efi_entry_state);
38 if (status != EFI_SUCCESS) {
39 efi_err("allocate_pool() failed\n");
40 return status;
41 }
42
43 efi_entry_state->cpsr_before_ebs = cpsr;
44 efi_entry_state->sctlr_before_ebs = sctlr;
45
46 status = efi_bs_call(install_configuration_table, &cpu_state_guid,
47 efi_entry_state);
48 if (status != EFI_SUCCESS) {
49 efi_err("install_configuration_table() failed\n");
50 goto free_state;
51 }
52
53 /* non-LPAE kernels can run anywhere */
54 if (!IS_ENABLED(CONFIG_ARM_LPAE))
55 return EFI_SUCCESS;
56
57 /* LPAE kernels need compatible hardware */
58 block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
59 if (block < 5) {
60 efi_err("This LPAE kernel is not supported by your CPU\n");
61 status = EFI_UNSUPPORTED;
62 goto drop_table;
63 }
64 return EFI_SUCCESS;
65
66drop_table:
67 efi_bs_call(install_configuration_table, &cpu_state_guid, NULL);
68free_state:
69 efi_bs_call(free_pool, efi_entry_state);
70 return status;
71}
72
73void efi_handle_post_ebs_state(void)
74{
75 get_cpu_state(&efi_entry_state->cpsr_after_ebs,
76 &efi_entry_state->sctlr_after_ebs);
77}
78
79static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
80
81struct screen_info *alloc_screen_info(void)
82{
83 struct screen_info *si;
84 efi_status_t status;
85
86 /*
87 * Unlike on arm64, where we can directly fill out the screen_info
88 * structure from the stub, we need to allocate a buffer to hold
89 * its contents while we hand over to the kernel proper from the
90 * decompressor.
91 */
92 status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
93 sizeof(*si), (void **)&si);
94
95 if (status != EFI_SUCCESS)
96 return NULL;
97
98 status = efi_bs_call(install_configuration_table,
99 &screen_info_guid, si);
100 if (status == EFI_SUCCESS)
101 return si;
102
103 efi_bs_call(free_pool, si);
104 return NULL;
105}
106
107void free_screen_info(struct screen_info *si)
108{
109 if (!si)
110 return;
111
112 efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
113 efi_bs_call(free_pool, si);
114}
115
116static efi_status_t reserve_kernel_base(unsigned long dram_base,
117 unsigned long *reserve_addr,
118 unsigned long *reserve_size)
119{
120 efi_physical_addr_t alloc_addr;
121 efi_memory_desc_t *memory_map;
122 unsigned long nr_pages, map_size, desc_size, buff_size;
123 efi_status_t status;
124 unsigned long l;
125
126 struct efi_boot_memmap map = {
127 .map = &memory_map,
128 .map_size = &map_size,
129 .desc_size = &desc_size,
130 .desc_ver = NULL,
131 .key_ptr = NULL,
132 .buff_size = &buff_size,
133 };
134
135 /*
136 * Reserve memory for the uncompressed kernel image. This is
137 * all that prevents any future allocations from conflicting
138 * with the kernel. Since we can't tell from the compressed
139 * image how much DRAM the kernel actually uses (due to BSS
140 * size uncertainty) we allocate the maximum possible size.
141 * Do this very early, as prints can cause memory allocations
142 * that may conflict with this.
143 */
144 alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE;
145 nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE;
146 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
147 EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr);
148 if (status == EFI_SUCCESS) {
149 if (alloc_addr == dram_base) {
150 *reserve_addr = alloc_addr;
151 *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
152 return EFI_SUCCESS;
153 }
154 /*
155 * If we end up here, the allocation succeeded but starts below
156 * dram_base. This can only occur if the real base of DRAM is
157 * not a multiple of 128 MB, in which case dram_base will have
158 * been rounded up. Since this implies that a part of the region
159 * was already occupied, we need to fall through to the code
160 * below to ensure that the existing allocations don't conflict.
161 * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
162 * EFI_LOADER_DATA, which we wouldn't able to distinguish from
163 * allocations that we want to disallow.
164 */
165 }
166
167 /*
168 * If the allocation above failed, we may still be able to proceed:
169 * if the only allocations in the region are of types that will be
170 * released to the OS after ExitBootServices(), the decompressor can
171 * safely overwrite them.
172 */
173 status = efi_get_memory_map(&map);
174 if (status != EFI_SUCCESS) {
175 efi_err("reserve_kernel_base(): Unable to retrieve memory map.\n");
176 return status;
177 }
178
179 for (l = 0; l < map_size; l += desc_size) {
180 efi_memory_desc_t *desc;
181 u64 start, end;
182
183 desc = (void *)memory_map + l;
184 start = desc->phys_addr;
185 end = start + desc->num_pages * EFI_PAGE_SIZE;
186
187 /* Skip if entry does not intersect with region */
188 if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE ||
189 end <= dram_base)
190 continue;
191
192 switch (desc->type) {
193 case EFI_BOOT_SERVICES_CODE:
194 case EFI_BOOT_SERVICES_DATA:
195 /* Ignore types that are released to the OS anyway */
196 continue;
197
198 case EFI_CONVENTIONAL_MEMORY:
199 /* Skip soft reserved conventional memory */
200 if (efi_soft_reserve_enabled() &&
201 (desc->attribute & EFI_MEMORY_SP))
202 continue;
203
204 /*
205 * Reserve the intersection between this entry and the
206 * region.
207 */
208 start = max(start, (u64)dram_base);
209 end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE);
210
211 status = efi_bs_call(allocate_pages,
212 EFI_ALLOCATE_ADDRESS,
213 EFI_LOADER_DATA,
214 (end - start) / EFI_PAGE_SIZE,
215 &start);
216 if (status != EFI_SUCCESS) {
217 efi_err("reserve_kernel_base(): alloc failed.\n");
218 goto out;
219 }
220 break;
221
222 case EFI_LOADER_CODE:
223 case EFI_LOADER_DATA:
224 /*
225 * These regions may be released and reallocated for
226 * another purpose (including EFI_RUNTIME_SERVICE_DATA)
227 * at any time during the execution of the OS loader,
228 * so we cannot consider them as safe.
229 */
230 default:
231 /*
232 * Treat any other allocation in the region as unsafe */
233 status = EFI_OUT_OF_RESOURCES;
234 goto out;
235 }
236 }
237
238 status = EFI_SUCCESS;
239out:
240 efi_bs_call(free_pool, memory_map);
241 return status;
242}
243
244efi_status_t handle_kernel_image(unsigned long *image_addr,
245 unsigned long *image_size,
246 unsigned long *reserve_addr,
247 unsigned long *reserve_size,
248 unsigned long dram_base,
249 efi_loaded_image_t *image)
250{
251 unsigned long kernel_base;
252 efi_status_t status;
253
254 /* use a 16 MiB aligned base for the decompressed kernel */
255 kernel_base = round_up(dram_base, SZ_16M) + TEXT_OFFSET;
256
257 /*
258 * Note that some platforms (notably, the Raspberry Pi 2) put
259 * spin-tables and other pieces of firmware at the base of RAM,
260 * abusing the fact that the window of TEXT_OFFSET bytes at the
261 * base of the kernel image is only partially used at the moment.
262 * (Up to 5 pages are used for the swapper page tables)
263 */
264 status = reserve_kernel_base(kernel_base - 5 * PAGE_SIZE, reserve_addr,
265 reserve_size);
266 if (status != EFI_SUCCESS) {
267 efi_err("Unable to allocate memory for uncompressed kernel.\n");
268 return status;
269 }
270
271 *image_addr = kernel_base;
272 *image_size = 0;
273 return EFI_SUCCESS;
274}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
4 */
5#include <linux/efi.h>
6#include <asm/efi.h>
7
8#include "efistub.h"
9
10efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
11{
12 int block;
13
14 /* non-LPAE kernels can run anywhere */
15 if (!IS_ENABLED(CONFIG_ARM_LPAE))
16 return EFI_SUCCESS;
17
18 /* LPAE kernels need compatible hardware */
19 block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
20 if (block < 5) {
21 pr_efi_err(sys_table_arg, "This LPAE kernel is not supported by your CPU\n");
22 return EFI_UNSUPPORTED;
23 }
24 return EFI_SUCCESS;
25}
26
27static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
28
29struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg)
30{
31 struct screen_info *si;
32 efi_status_t status;
33
34 /*
35 * Unlike on arm64, where we can directly fill out the screen_info
36 * structure from the stub, we need to allocate a buffer to hold
37 * its contents while we hand over to the kernel proper from the
38 * decompressor.
39 */
40 status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
41 sizeof(*si), (void **)&si);
42
43 if (status != EFI_SUCCESS)
44 return NULL;
45
46 status = efi_call_early(install_configuration_table,
47 &screen_info_guid, si);
48 if (status == EFI_SUCCESS)
49 return si;
50
51 efi_call_early(free_pool, si);
52 return NULL;
53}
54
55void free_screen_info(efi_system_table_t *sys_table_arg, struct screen_info *si)
56{
57 if (!si)
58 return;
59
60 efi_call_early(install_configuration_table, &screen_info_guid, NULL);
61 efi_call_early(free_pool, si);
62}
63
64static efi_status_t reserve_kernel_base(efi_system_table_t *sys_table_arg,
65 unsigned long dram_base,
66 unsigned long *reserve_addr,
67 unsigned long *reserve_size)
68{
69 efi_physical_addr_t alloc_addr;
70 efi_memory_desc_t *memory_map;
71 unsigned long nr_pages, map_size, desc_size, buff_size;
72 efi_status_t status;
73 unsigned long l;
74
75 struct efi_boot_memmap map = {
76 .map = &memory_map,
77 .map_size = &map_size,
78 .desc_size = &desc_size,
79 .desc_ver = NULL,
80 .key_ptr = NULL,
81 .buff_size = &buff_size,
82 };
83
84 /*
85 * Reserve memory for the uncompressed kernel image. This is
86 * all that prevents any future allocations from conflicting
87 * with the kernel. Since we can't tell from the compressed
88 * image how much DRAM the kernel actually uses (due to BSS
89 * size uncertainty) we allocate the maximum possible size.
90 * Do this very early, as prints can cause memory allocations
91 * that may conflict with this.
92 */
93 alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE;
94 nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE;
95 status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
96 EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr);
97 if (status == EFI_SUCCESS) {
98 if (alloc_addr == dram_base) {
99 *reserve_addr = alloc_addr;
100 *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
101 return EFI_SUCCESS;
102 }
103 /*
104 * If we end up here, the allocation succeeded but starts below
105 * dram_base. This can only occur if the real base of DRAM is
106 * not a multiple of 128 MB, in which case dram_base will have
107 * been rounded up. Since this implies that a part of the region
108 * was already occupied, we need to fall through to the code
109 * below to ensure that the existing allocations don't conflict.
110 * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
111 * EFI_LOADER_DATA, which we wouldn't able to distinguish from
112 * allocations that we want to disallow.
113 */
114 }
115
116 /*
117 * If the allocation above failed, we may still be able to proceed:
118 * if the only allocations in the region are of types that will be
119 * released to the OS after ExitBootServices(), the decompressor can
120 * safely overwrite them.
121 */
122 status = efi_get_memory_map(sys_table_arg, &map);
123 if (status != EFI_SUCCESS) {
124 pr_efi_err(sys_table_arg,
125 "reserve_kernel_base(): Unable to retrieve memory map.\n");
126 return status;
127 }
128
129 for (l = 0; l < map_size; l += desc_size) {
130 efi_memory_desc_t *desc;
131 u64 start, end;
132
133 desc = (void *)memory_map + l;
134 start = desc->phys_addr;
135 end = start + desc->num_pages * EFI_PAGE_SIZE;
136
137 /* Skip if entry does not intersect with region */
138 if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE ||
139 end <= dram_base)
140 continue;
141
142 switch (desc->type) {
143 case EFI_BOOT_SERVICES_CODE:
144 case EFI_BOOT_SERVICES_DATA:
145 /* Ignore types that are released to the OS anyway */
146 continue;
147
148 case EFI_CONVENTIONAL_MEMORY:
149 /*
150 * Reserve the intersection between this entry and the
151 * region.
152 */
153 start = max(start, (u64)dram_base);
154 end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE);
155
156 status = efi_call_early(allocate_pages,
157 EFI_ALLOCATE_ADDRESS,
158 EFI_LOADER_DATA,
159 (end - start) / EFI_PAGE_SIZE,
160 &start);
161 if (status != EFI_SUCCESS) {
162 pr_efi_err(sys_table_arg,
163 "reserve_kernel_base(): alloc failed.\n");
164 goto out;
165 }
166 break;
167
168 case EFI_LOADER_CODE:
169 case EFI_LOADER_DATA:
170 /*
171 * These regions may be released and reallocated for
172 * another purpose (including EFI_RUNTIME_SERVICE_DATA)
173 * at any time during the execution of the OS loader,
174 * so we cannot consider them as safe.
175 */
176 default:
177 /*
178 * Treat any other allocation in the region as unsafe */
179 status = EFI_OUT_OF_RESOURCES;
180 goto out;
181 }
182 }
183
184 status = EFI_SUCCESS;
185out:
186 efi_call_early(free_pool, memory_map);
187 return status;
188}
189
190efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
191 unsigned long *image_addr,
192 unsigned long *image_size,
193 unsigned long *reserve_addr,
194 unsigned long *reserve_size,
195 unsigned long dram_base,
196 efi_loaded_image_t *image)
197{
198 unsigned long kernel_base;
199 efi_status_t status;
200
201 /*
202 * Verify that the DRAM base address is compatible with the ARM
203 * boot protocol, which determines the base of DRAM by masking
204 * off the low 27 bits of the address at which the zImage is
205 * loaded. These assumptions are made by the decompressor,
206 * before any memory map is available.
207 */
208 kernel_base = round_up(dram_base, SZ_128M);
209
210 /*
211 * Note that some platforms (notably, the Raspberry Pi 2) put
212 * spin-tables and other pieces of firmware at the base of RAM,
213 * abusing the fact that the window of TEXT_OFFSET bytes at the
214 * base of the kernel image is only partially used at the moment.
215 * (Up to 5 pages are used for the swapper page tables)
216 */
217 kernel_base += TEXT_OFFSET - 5 * PAGE_SIZE;
218
219 status = reserve_kernel_base(sys_table, kernel_base, reserve_addr,
220 reserve_size);
221 if (status != EFI_SUCCESS) {
222 pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");
223 return status;
224 }
225
226 /*
227 * Relocate the zImage, so that it appears in the lowest 128 MB
228 * memory window.
229 */
230 *image_size = image->image_size;
231 status = efi_relocate_kernel(sys_table, image_addr, *image_size,
232 *image_size,
233 kernel_base + MAX_UNCOMP_KERNEL_SIZE, 0, 0);
234 if (status != EFI_SUCCESS) {
235 pr_efi_err(sys_table, "Failed to relocate kernel.\n");
236 efi_free(sys_table, *reserve_size, *reserve_addr);
237 *reserve_size = 0;
238 return status;
239 }
240
241 /*
242 * Check to see if we were able to allocate memory low enough
243 * in memory. The kernel determines the base of DRAM from the
244 * address at which the zImage is loaded.
245 */
246 if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {
247 pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");
248 efi_free(sys_table, *reserve_size, *reserve_addr);
249 *reserve_size = 0;
250 efi_free(sys_table, *image_size, *image_addr);
251 *image_size = 0;
252 return EFI_LOAD_ERROR;
253 }
254 return EFI_SUCCESS;
255}