This source file includes following definitions.
- check_platform_features
- alloc_screen_info
- free_screen_info
- reserve_kernel_base
- handle_kernel_image
1
2
3
4
5 #include <linux/efi.h>
6 #include <asm/efi.h>
7
8 #include "efistub.h"
9
10 efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
11 {
12 int block;
13
14
15 if (!IS_ENABLED(CONFIG_ARM_LPAE))
16 return EFI_SUCCESS;
17
18
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
27 static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
28
29 struct 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
36
37
38
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
55 void 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
64 static 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
86
87
88
89
90
91
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
105
106
107
108
109
110
111
112
113
114 }
115
116
117
118
119
120
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
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
146 continue;
147
148 case EFI_CONVENTIONAL_MEMORY:
149
150
151
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
172
173
174
175
176 default:
177
178
179 status = EFI_OUT_OF_RESOURCES;
180 goto out;
181 }
182 }
183
184 status = EFI_SUCCESS;
185 out:
186 efi_call_early(free_pool, memory_map);
187 return status;
188 }
189
190 efi_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
203
204
205
206
207
208 kernel_base = round_up(dram_base, SZ_128M);
209
210
211
212
213
214
215
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
228
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
243
244
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 }