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 TASK_SIZE_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
97
98
99
100
101
102
103efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
104 unsigned long *image_addr,
105 unsigned long *image_size,
106 unsigned long *reserve_addr,
107 unsigned long *reserve_size,
108 unsigned long dram_base,
109 efi_loaded_image_t *image);
110
111
112
113
114
115
116unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
117 unsigned long *image_addr)
118{
119 efi_loaded_image_t *image;
120 efi_status_t status;
121 unsigned long image_size = 0;
122 unsigned long dram_base;
123
124 unsigned long initrd_addr;
125 u64 initrd_size = 0;
126 unsigned long fdt_addr = 0;
127 unsigned long fdt_size = 0;
128 char *cmdline_ptr = NULL;
129 int cmdline_size = 0;
130 unsigned long new_fdt_addr;
131 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
132 unsigned long reserve_addr = 0;
133 unsigned long reserve_size = 0;
134 enum efi_secureboot_mode secure_boot;
135 struct screen_info *si;
136
137
138 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
139 goto fail;
140
141 status = check_platform_features(sys_table);
142 if (status != EFI_SUCCESS)
143 goto fail;
144
145
146
147
148
149
150 status = sys_table->boottime->handle_protocol(handle,
151 &loaded_image_proto, (void *)&image);
152 if (status != EFI_SUCCESS) {
153 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
154 goto fail;
155 }
156
157 dram_base = get_dram_base(sys_table);
158 if (dram_base == EFI_ERROR) {
159 pr_efi_err(sys_table, "Failed to find DRAM base\n");
160 goto fail;
161 }
162
163
164
165
166
167
168 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
169 if (!cmdline_ptr) {
170 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
171 goto fail;
172 }
173
174 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
175 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
176 cmdline_size == 0)
177 efi_parse_options(CONFIG_CMDLINE);
178
179 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
180 efi_parse_options(cmdline_ptr);
181
182 pr_efi(sys_table, "Booting Linux Kernel...\n");
183
184 si = setup_graphics(sys_table);
185
186 status = handle_kernel_image(sys_table, image_addr, &image_size,
187 &reserve_addr,
188 &reserve_size,
189 dram_base, image);
190 if (status != EFI_SUCCESS) {
191 pr_efi_err(sys_table, "Failed to relocate kernel\n");
192 goto fail_free_cmdline;
193 }
194
195
196 efi_enable_reset_attack_mitigation(sys_table);
197
198 secure_boot = efi_get_secureboot(sys_table);
199
200
201
202
203
204
205 if (secure_boot != efi_secureboot_mode_disabled &&
206 strstr(cmdline_ptr, "dtb=")) {
207 pr_efi(sys_table, "Ignoring DTB from command line.\n");
208 } else {
209 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
210 "dtb=",
211 ~0UL, &fdt_addr, &fdt_size);
212
213 if (status != EFI_SUCCESS) {
214 pr_efi_err(sys_table, "Failed to load device tree!\n");
215 goto fail_free_image;
216 }
217 }
218
219 if (fdt_addr) {
220 pr_efi(sys_table, "Using DTB from command line\n");
221 } else {
222
223 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
224 if (fdt_addr)
225 pr_efi(sys_table, "Using DTB from configuration table\n");
226 }
227
228 if (!fdt_addr)
229 pr_efi(sys_table, "Generating empty DTB\n");
230
231 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
232 efi_get_max_initrd_addr(dram_base,
233 *image_addr),
234 (unsigned long *)&initrd_addr,
235 (unsigned long *)&initrd_size);
236 if (status != EFI_SUCCESS)
237 pr_efi_err(sys_table, "Failed initrd from command line!\n");
238
239 efi_random_get_seed(sys_table);
240
241
242 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
243
244
245
246
247
248
249 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
250 EFI_RT_VIRTUAL_BASE -
251 EFI_RT_VIRTUAL_SIZE;
252 u32 rnd;
253
254 status = efi_get_random_bytes(sys_table, sizeof(rnd),
255 (u8 *)&rnd);
256 if (status == EFI_SUCCESS) {
257 virtmap_base = EFI_RT_VIRTUAL_BASE +
258 (((headroom >> 21) * rnd) >> (32 - 21));
259 }
260 }
261
262 new_fdt_addr = fdt_addr;
263 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
264 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
265 initrd_addr, initrd_size, cmdline_ptr,
266 fdt_addr, fdt_size);
267
268
269
270
271
272
273 if (status == EFI_SUCCESS)
274 return new_fdt_addr;
275
276 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
277
278 efi_free(sys_table, initrd_size, initrd_addr);
279 efi_free(sys_table, fdt_size, fdt_addr);
280
281fail_free_image:
282 efi_free(sys_table, image_size, *image_addr);
283 efi_free(sys_table, reserve_size, reserve_addr);
284fail_free_cmdline:
285 free_screen_info(sys_table, si);
286 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
287fail:
288 return EFI_ERROR;
289}
290
291static int cmp_mem_desc(const void *l, const void *r)
292{
293 const efi_memory_desc_t *left = l, *right = r;
294
295 return (left->phys_addr > right->phys_addr) ? 1 : -1;
296}
297
298
299
300
301
302static bool regions_are_adjacent(efi_memory_desc_t *left,
303 efi_memory_desc_t *right)
304{
305 u64 left_end;
306
307 if (left == NULL || right == NULL)
308 return false;
309
310 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
311
312 return left_end == right->phys_addr;
313}
314
315
316
317
318
319static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
320 efi_memory_desc_t *right)
321{
322 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
323 EFI_MEMORY_WC | EFI_MEMORY_UC |
324 EFI_MEMORY_RUNTIME;
325
326 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
327}
328
329
330
331
332
333
334
335
336void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
337 unsigned long desc_size, efi_memory_desc_t *runtime_map,
338 int *count)
339{
340 u64 efi_virt_base = virtmap_base;
341 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
342 int l;
343
344
345
346
347
348
349
350
351
352
353 if (IS_ENABLED(CONFIG_ARM64))
354 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
355 NULL);
356
357 for (l = 0; l < map_size; l += desc_size, prev = in) {
358 u64 paddr, size;
359
360 in = (void *)memory_map + l;
361 if (!(in->attribute & EFI_MEMORY_RUNTIME))
362 continue;
363
364 paddr = in->phys_addr;
365 size = in->num_pages * EFI_PAGE_SIZE;
366
367
368
369
370
371
372 if ((IS_ENABLED(CONFIG_ARM64) &&
373 !regions_are_adjacent(prev, in)) ||
374 !regions_have_compatible_memory_type_attrs(prev, in)) {
375
376 paddr = round_down(in->phys_addr, SZ_64K);
377 size += in->phys_addr - paddr;
378
379
380
381
382
383
384
385 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
386 efi_virt_base = round_up(efi_virt_base, SZ_2M);
387 else
388 efi_virt_base = round_up(efi_virt_base, SZ_64K);
389 }
390
391 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
392 efi_virt_base += size;
393
394 memcpy(out, in, desc_size);
395 out = (void *)out + desc_size;
396 ++*count;
397 }
398}
399