1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/efi.h>
16#include <linux/sort.h>
17#include <asm/efi.h>
18
19#include "efistub.h"
20
21
22
23
24
25
26
27
28
29
30
31
32#define EFI_RT_VIRTUAL_BASE SZ_512M
33#define EFI_RT_VIRTUAL_SIZE SZ_512M
34
35#ifdef CONFIG_ARM64
36# define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64
37#else
38# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
39#endif
40
41static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
42
43efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
44 void *__image, void **__fh)
45{
46 efi_file_io_interface_t *io;
47 efi_loaded_image_t *image = __image;
48 efi_file_handle_t *fh;
49 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
50 efi_status_t status;
51 void *handle = (void *)(unsigned long)image->device_handle;
52
53 status = sys_table_arg->boottime->handle_protocol(handle,
54 &fs_proto, (void **)&io);
55 if (status != EFI_SUCCESS) {
56 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
57 return status;
58 }
59
60 status = io->open_volume(io, &fh);
61 if (status != EFI_SUCCESS)
62 efi_printk(sys_table_arg, "Failed to open volume\n");
63
64 *__fh = fh;
65 return status;
66}
67
68void efi_char16_printk(efi_system_table_t *sys_table_arg,
69 efi_char16_t *str)
70{
71 struct efi_simple_text_output_protocol *out;
72
73 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
74 out->output_string(out, str);
75}
76
77static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
78{
79 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
80 efi_status_t status;
81 unsigned long size;
82 void **gop_handle = NULL;
83 struct screen_info *si = NULL;
84
85 size = 0;
86 status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
87 &gop_proto, NULL, &size, gop_handle);
88 if (status == EFI_BUFFER_TOO_SMALL) {
89 si = alloc_screen_info(sys_table_arg);
90 if (!si)
91 return NULL;
92 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
93 }
94 return si;
95}
96
97void install_memreserve_table(efi_system_table_t *sys_table_arg)
98{
99 struct linux_efi_memreserve *rsv;
100 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
101 efi_status_t status;
102
103 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
104 (void **)&rsv);
105 if (status != EFI_SUCCESS) {
106 pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n");
107 return;
108 }
109
110 rsv->next = 0;
111 rsv->size = 0;
112 atomic_set(&rsv->count, 0);
113
114 status = efi_call_early(install_configuration_table,
115 &memreserve_table_guid,
116 rsv);
117 if (status != EFI_SUCCESS)
118 pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n");
119}
120
121
122
123
124
125
126
127
128efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
129 unsigned long *image_addr,
130 unsigned long *image_size,
131 unsigned long *reserve_addr,
132 unsigned long *reserve_size,
133 unsigned long dram_base,
134 efi_loaded_image_t *image);
135
136
137
138
139
140
141unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
142 unsigned long *image_addr)
143{
144 efi_loaded_image_t *image;
145 efi_status_t status;
146 unsigned long image_size = 0;
147 unsigned long dram_base;
148
149 unsigned long initrd_addr;
150 u64 initrd_size = 0;
151 unsigned long fdt_addr = 0;
152 unsigned long fdt_size = 0;
153 char *cmdline_ptr = NULL;
154 int cmdline_size = 0;
155 unsigned long new_fdt_addr;
156 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
157 unsigned long reserve_addr = 0;
158 unsigned long reserve_size = 0;
159 enum efi_secureboot_mode secure_boot;
160 struct screen_info *si;
161
162
163 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
164 goto fail;
165
166 status = check_platform_features(sys_table);
167 if (status != EFI_SUCCESS)
168 goto fail;
169
170
171
172
173
174
175 status = sys_table->boottime->handle_protocol(handle,
176 &loaded_image_proto, (void *)&image);
177 if (status != EFI_SUCCESS) {
178 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
179 goto fail;
180 }
181
182 dram_base = get_dram_base(sys_table);
183 if (dram_base == EFI_ERROR) {
184 pr_efi_err(sys_table, "Failed to find DRAM base\n");
185 goto fail;
186 }
187
188
189
190
191
192
193 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
194 if (!cmdline_ptr) {
195 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
196 goto fail;
197 }
198
199 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
200 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
201 cmdline_size == 0)
202 efi_parse_options(CONFIG_CMDLINE);
203
204 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
205 efi_parse_options(cmdline_ptr);
206
207 pr_efi(sys_table, "Booting Linux Kernel...\n");
208
209 si = setup_graphics(sys_table);
210
211 status = handle_kernel_image(sys_table, image_addr, &image_size,
212 &reserve_addr,
213 &reserve_size,
214 dram_base, image);
215 if (status != EFI_SUCCESS) {
216 pr_efi_err(sys_table, "Failed to relocate kernel\n");
217 goto fail_free_cmdline;
218 }
219
220
221 efi_enable_reset_attack_mitigation(sys_table);
222
223 secure_boot = efi_get_secureboot(sys_table);
224
225
226
227
228
229
230 if (secure_boot != efi_secureboot_mode_disabled &&
231 strstr(cmdline_ptr, "dtb=")) {
232 pr_efi(sys_table, "Ignoring DTB from command line.\n");
233 } else {
234 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
235 "dtb=",
236 ~0UL, &fdt_addr, &fdt_size);
237
238 if (status != EFI_SUCCESS) {
239 pr_efi_err(sys_table, "Failed to load device tree!\n");
240 goto fail_free_image;
241 }
242 }
243
244 if (fdt_addr) {
245 pr_efi(sys_table, "Using DTB from command line\n");
246 } else {
247
248 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
249 if (fdt_addr)
250 pr_efi(sys_table, "Using DTB from configuration table\n");
251 }
252
253 if (!fdt_addr)
254 pr_efi(sys_table, "Generating empty DTB\n");
255
256 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
257 efi_get_max_initrd_addr(dram_base,
258 *image_addr),
259 (unsigned long *)&initrd_addr,
260 (unsigned long *)&initrd_size);
261 if (status != EFI_SUCCESS)
262 pr_efi_err(sys_table, "Failed initrd from command line!\n");
263
264 efi_random_get_seed(sys_table);
265
266
267 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
268
269
270
271
272
273
274 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
275 EFI_RT_VIRTUAL_BASE -
276 EFI_RT_VIRTUAL_SIZE;
277 u32 rnd;
278
279 status = efi_get_random_bytes(sys_table, sizeof(rnd),
280 (u8 *)&rnd);
281 if (status == EFI_SUCCESS) {
282 virtmap_base = EFI_RT_VIRTUAL_BASE +
283 (((headroom >> 21) * rnd) >> (32 - 21));
284 }
285 }
286
287 install_memreserve_table(sys_table);
288
289 new_fdt_addr = fdt_addr;
290 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
291 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
292 initrd_addr, initrd_size, cmdline_ptr,
293 fdt_addr, fdt_size);
294
295
296
297
298
299
300 if (status == EFI_SUCCESS)
301 return new_fdt_addr;
302
303 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
304
305 efi_free(sys_table, initrd_size, initrd_addr);
306 efi_free(sys_table, fdt_size, fdt_addr);
307
308fail_free_image:
309 efi_free(sys_table, image_size, *image_addr);
310 efi_free(sys_table, reserve_size, reserve_addr);
311fail_free_cmdline:
312 free_screen_info(sys_table, si);
313 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
314fail:
315 return EFI_ERROR;
316}
317
318static int cmp_mem_desc(const void *l, const void *r)
319{
320 const efi_memory_desc_t *left = l, *right = r;
321
322 return (left->phys_addr > right->phys_addr) ? 1 : -1;
323}
324
325
326
327
328
329static bool regions_are_adjacent(efi_memory_desc_t *left,
330 efi_memory_desc_t *right)
331{
332 u64 left_end;
333
334 if (left == NULL || right == NULL)
335 return false;
336
337 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
338
339 return left_end == right->phys_addr;
340}
341
342
343
344
345
346static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
347 efi_memory_desc_t *right)
348{
349 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
350 EFI_MEMORY_WC | EFI_MEMORY_UC |
351 EFI_MEMORY_RUNTIME;
352
353 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
354}
355
356
357
358
359
360
361
362
363void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
364 unsigned long desc_size, efi_memory_desc_t *runtime_map,
365 int *count)
366{
367 u64 efi_virt_base = virtmap_base;
368 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
369 int l;
370
371
372
373
374
375
376
377
378
379
380 if (IS_ENABLED(CONFIG_ARM64))
381 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
382 NULL);
383
384 for (l = 0; l < map_size; l += desc_size, prev = in) {
385 u64 paddr, size;
386
387 in = (void *)memory_map + l;
388 if (!(in->attribute & EFI_MEMORY_RUNTIME))
389 continue;
390
391 paddr = in->phys_addr;
392 size = in->num_pages * EFI_PAGE_SIZE;
393
394
395
396
397
398
399 if ((IS_ENABLED(CONFIG_ARM64) &&
400 !regions_are_adjacent(prev, in)) ||
401 !regions_have_compatible_memory_type_attrs(prev, in)) {
402
403 paddr = round_down(in->phys_addr, SZ_64K);
404 size += in->phys_addr - paddr;
405
406
407
408
409
410
411
412 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
413 efi_virt_base = round_up(efi_virt_base, SZ_2M);
414 else
415 efi_virt_base = round_up(efi_virt_base, SZ_64K);
416 }
417
418 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
419 efi_virt_base += size;
420
421 memcpy(out, in, desc_size);
422 out = (void *)out + desc_size;
423 ++*count;
424 }
425}
426