Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2
3/* -----------------------------------------------------------------------
4 *
5 * Copyright 2011 Intel Corporation; author Matt Fleming
6 *
7 * ----------------------------------------------------------------------- */
8
9#include <linux/efi.h>
10#include <linux/pci.h>
11#include <linux/stddef.h>
12
13#include <asm/efi.h>
14#include <asm/e820/types.h>
15#include <asm/setup.h>
16#include <asm/desc.h>
17#include <asm/boot.h>
18
19#include "efistub.h"
20
21/* Maximum physical address for 64-bit kernel with 4-level paging */
22#define MAXMEM_X86_64_4LEVEL (1ull << 46)
23
24const efi_system_table_t *efi_system_table;
25extern u32 image_offset;
26static efi_loaded_image_t *image = NULL;
27
28static efi_status_t
29preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
30{
31 struct pci_setup_rom *rom = NULL;
32 efi_status_t status;
33 unsigned long size;
34 uint64_t romsize;
35 void *romimage;
36
37 /*
38 * Some firmware images contain EFI function pointers at the place where
39 * the romimage and romsize fields are supposed to be. Typically the EFI
40 * code is mapped at high addresses, translating to an unrealistically
41 * large romsize. The UEFI spec limits the size of option ROMs to 16
42 * MiB so we reject any ROMs over 16 MiB in size to catch this.
43 */
44 romimage = efi_table_attr(pci, romimage);
45 romsize = efi_table_attr(pci, romsize);
46 if (!romimage || !romsize || romsize > SZ_16M)
47 return EFI_INVALID_PARAMETER;
48
49 size = romsize + sizeof(*rom);
50
51 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
52 (void **)&rom);
53 if (status != EFI_SUCCESS) {
54 efi_err("Failed to allocate memory for 'rom'\n");
55 return status;
56 }
57
58 memset(rom, 0, sizeof(*rom));
59
60 rom->data.type = SETUP_PCI;
61 rom->data.len = size - sizeof(struct setup_data);
62 rom->data.next = 0;
63 rom->pcilen = pci->romsize;
64 *__rom = rom;
65
66 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
67 PCI_VENDOR_ID, 1, &rom->vendor);
68
69 if (status != EFI_SUCCESS) {
70 efi_err("Failed to read rom->vendor\n");
71 goto free_struct;
72 }
73
74 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
75 PCI_DEVICE_ID, 1, &rom->devid);
76
77 if (status != EFI_SUCCESS) {
78 efi_err("Failed to read rom->devid\n");
79 goto free_struct;
80 }
81
82 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
83 &rom->device, &rom->function);
84
85 if (status != EFI_SUCCESS)
86 goto free_struct;
87
88 memcpy(rom->romdata, romimage, romsize);
89 return status;
90
91free_struct:
92 efi_bs_call(free_pool, rom);
93 return status;
94}
95
96/*
97 * There's no way to return an informative status from this function,
98 * because any analysis (and printing of error messages) needs to be
99 * done directly at the EFI function call-site.
100 *
101 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
102 * just didn't find any PCI devices, but there's no way to tell outside
103 * the context of the call.
104 */
105static void setup_efi_pci(struct boot_params *params)
106{
107 efi_status_t status;
108 void **pci_handle = NULL;
109 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
110 unsigned long size = 0;
111 struct setup_data *data;
112 efi_handle_t h;
113 int i;
114
115 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
116 &pci_proto, NULL, &size, pci_handle);
117
118 if (status == EFI_BUFFER_TOO_SMALL) {
119 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
120 (void **)&pci_handle);
121
122 if (status != EFI_SUCCESS) {
123 efi_err("Failed to allocate memory for 'pci_handle'\n");
124 return;
125 }
126
127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
129 }
130
131 if (status != EFI_SUCCESS)
132 goto free_handle;
133
134 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
135
136 while (data && data->next)
137 data = (struct setup_data *)(unsigned long)data->next;
138
139 for_each_efi_handle(h, pci_handle, size, i) {
140 efi_pci_io_protocol_t *pci = NULL;
141 struct pci_setup_rom *rom;
142
143 status = efi_bs_call(handle_protocol, h, &pci_proto,
144 (void **)&pci);
145 if (status != EFI_SUCCESS || !pci)
146 continue;
147
148 status = preserve_pci_rom_image(pci, &rom);
149 if (status != EFI_SUCCESS)
150 continue;
151
152 if (data)
153 data->next = (unsigned long)rom;
154 else
155 params->hdr.setup_data = (unsigned long)rom;
156
157 data = (struct setup_data *)rom;
158 }
159
160free_handle:
161 efi_bs_call(free_pool, pci_handle);
162}
163
164static void retrieve_apple_device_properties(struct boot_params *boot_params)
165{
166 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
167 struct setup_data *data, *new;
168 efi_status_t status;
169 u32 size = 0;
170 apple_properties_protocol_t *p;
171
172 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
173 if (status != EFI_SUCCESS)
174 return;
175
176 if (efi_table_attr(p, version) != 0x10000) {
177 efi_err("Unsupported properties proto version\n");
178 return;
179 }
180
181 efi_call_proto(p, get_all, NULL, &size);
182 if (!size)
183 return;
184
185 do {
186 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
187 size + sizeof(struct setup_data),
188 (void **)&new);
189 if (status != EFI_SUCCESS) {
190 efi_err("Failed to allocate memory for 'properties'\n");
191 return;
192 }
193
194 status = efi_call_proto(p, get_all, new->data, &size);
195
196 if (status == EFI_BUFFER_TOO_SMALL)
197 efi_bs_call(free_pool, new);
198 } while (status == EFI_BUFFER_TOO_SMALL);
199
200 new->type = SETUP_APPLE_PROPERTIES;
201 new->len = size;
202 new->next = 0;
203
204 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
205 if (!data) {
206 boot_params->hdr.setup_data = (unsigned long)new;
207 } else {
208 while (data->next)
209 data = (struct setup_data *)(unsigned long)data->next;
210 data->next = (unsigned long)new;
211 }
212}
213
214static const efi_char16_t apple[] = L"Apple";
215
216static void setup_quirks(struct boot_params *boot_params)
217{
218 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
219 efi_table_attr(efi_system_table, fw_vendor);
220
221 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
222 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
223 retrieve_apple_device_properties(boot_params);
224 }
225}
226
227/*
228 * See if we have Universal Graphics Adapter (UGA) protocol
229 */
230static efi_status_t
231setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
232{
233 efi_status_t status;
234 u32 width, height;
235 void **uga_handle = NULL;
236 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
237 efi_handle_t handle;
238 int i;
239
240 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
241 (void **)&uga_handle);
242 if (status != EFI_SUCCESS)
243 return status;
244
245 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
246 uga_proto, NULL, &size, uga_handle);
247 if (status != EFI_SUCCESS)
248 goto free_handle;
249
250 height = 0;
251 width = 0;
252
253 first_uga = NULL;
254 for_each_efi_handle(handle, uga_handle, size, i) {
255 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
256 u32 w, h, depth, refresh;
257 void *pciio;
258
259 status = efi_bs_call(handle_protocol, handle, uga_proto,
260 (void **)&uga);
261 if (status != EFI_SUCCESS)
262 continue;
263
264 pciio = NULL;
265 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
266
267 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
268 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
269 width = w;
270 height = h;
271
272 /*
273 * Once we've found a UGA supporting PCIIO,
274 * don't bother looking any further.
275 */
276 if (pciio)
277 break;
278
279 first_uga = uga;
280 }
281 }
282
283 if (!width && !height)
284 goto free_handle;
285
286 /* EFI framebuffer */
287 si->orig_video_isVGA = VIDEO_TYPE_EFI;
288
289 si->lfb_depth = 32;
290 si->lfb_width = width;
291 si->lfb_height = height;
292
293 si->red_size = 8;
294 si->red_pos = 16;
295 si->green_size = 8;
296 si->green_pos = 8;
297 si->blue_size = 8;
298 si->blue_pos = 0;
299 si->rsvd_size = 8;
300 si->rsvd_pos = 24;
301
302free_handle:
303 efi_bs_call(free_pool, uga_handle);
304
305 return status;
306}
307
308static void setup_graphics(struct boot_params *boot_params)
309{
310 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
311 struct screen_info *si;
312 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
313 efi_status_t status;
314 unsigned long size;
315 void **gop_handle = NULL;
316 void **uga_handle = NULL;
317
318 si = &boot_params->screen_info;
319 memset(si, 0, sizeof(*si));
320
321 size = 0;
322 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
323 &graphics_proto, NULL, &size, gop_handle);
324 if (status == EFI_BUFFER_TOO_SMALL)
325 status = efi_setup_gop(si, &graphics_proto, size);
326
327 if (status != EFI_SUCCESS) {
328 size = 0;
329 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
330 &uga_proto, NULL, &size, uga_handle);
331 if (status == EFI_BUFFER_TOO_SMALL)
332 setup_uga(si, &uga_proto, size);
333 }
334}
335
336
337static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
338{
339 efi_bs_call(exit, handle, status, 0, NULL);
340 for(;;)
341 asm("hlt");
342}
343
344void startup_32(struct boot_params *boot_params);
345
346void __noreturn efi_stub_entry(efi_handle_t handle,
347 efi_system_table_t *sys_table_arg,
348 struct boot_params *boot_params);
349
350/*
351 * Because the x86 boot code expects to be passed a boot_params we
352 * need to create one ourselves (usually the bootloader would create
353 * one for us).
354 */
355efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
356 efi_system_table_t *sys_table_arg)
357{
358 struct boot_params *boot_params;
359 struct setup_header *hdr;
360 void *image_base;
361 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
362 int options_size = 0;
363 efi_status_t status;
364 char *cmdline_ptr;
365
366 efi_system_table = sys_table_arg;
367
368 /* Check if we were booted by the EFI firmware */
369 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
370 efi_exit(handle, EFI_INVALID_PARAMETER);
371
372 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
373 if (status != EFI_SUCCESS) {
374 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
375 efi_exit(handle, status);
376 }
377
378 image_base = efi_table_attr(image, image_base);
379 image_offset = (void *)startup_32 - image_base;
380
381 status = efi_allocate_pages(sizeof(struct boot_params),
382 (unsigned long *)&boot_params, ULONG_MAX);
383 if (status != EFI_SUCCESS) {
384 efi_err("Failed to allocate lowmem for boot params\n");
385 efi_exit(handle, status);
386 }
387
388 memset(boot_params, 0x0, sizeof(struct boot_params));
389
390 hdr = &boot_params->hdr;
391
392 /* Copy the setup header from the second sector to boot_params */
393 memcpy(&hdr->jump, image_base + 512,
394 sizeof(struct setup_header) - offsetof(struct setup_header, jump));
395
396 /*
397 * Fill out some of the header fields ourselves because the
398 * EFI firmware loader doesn't load the first sector.
399 */
400 hdr->root_flags = 1;
401 hdr->vid_mode = 0xffff;
402 hdr->boot_flag = 0xAA55;
403
404 hdr->type_of_loader = 0x21;
405
406 /* Convert unicode cmdline to ascii */
407 cmdline_ptr = efi_convert_cmdline(image, &options_size);
408 if (!cmdline_ptr)
409 goto fail;
410
411 efi_set_u64_split((unsigned long)cmdline_ptr,
412 &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
413
414 hdr->ramdisk_image = 0;
415 hdr->ramdisk_size = 0;
416
417 efi_stub_entry(handle, sys_table_arg, boot_params);
418 /* not reached */
419
420fail:
421 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
422
423 efi_exit(handle, status);
424}
425
426static void add_e820ext(struct boot_params *params,
427 struct setup_data *e820ext, u32 nr_entries)
428{
429 struct setup_data *data;
430
431 e820ext->type = SETUP_E820_EXT;
432 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
433 e820ext->next = 0;
434
435 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
436
437 while (data && data->next)
438 data = (struct setup_data *)(unsigned long)data->next;
439
440 if (data)
441 data->next = (unsigned long)e820ext;
442 else
443 params->hdr.setup_data = (unsigned long)e820ext;
444}
445
446static efi_status_t
447setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
448{
449 struct boot_e820_entry *entry = params->e820_table;
450 struct efi_info *efi = ¶ms->efi_info;
451 struct boot_e820_entry *prev = NULL;
452 u32 nr_entries;
453 u32 nr_desc;
454 int i;
455
456 nr_entries = 0;
457 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
458
459 for (i = 0; i < nr_desc; i++) {
460 efi_memory_desc_t *d;
461 unsigned int e820_type = 0;
462 unsigned long m = efi->efi_memmap;
463
464#ifdef CONFIG_X86_64
465 m |= (u64)efi->efi_memmap_hi << 32;
466#endif
467
468 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
469 switch (d->type) {
470 case EFI_RESERVED_TYPE:
471 case EFI_RUNTIME_SERVICES_CODE:
472 case EFI_RUNTIME_SERVICES_DATA:
473 case EFI_MEMORY_MAPPED_IO:
474 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
475 case EFI_PAL_CODE:
476 e820_type = E820_TYPE_RESERVED;
477 break;
478
479 case EFI_UNUSABLE_MEMORY:
480 e820_type = E820_TYPE_UNUSABLE;
481 break;
482
483 case EFI_ACPI_RECLAIM_MEMORY:
484 e820_type = E820_TYPE_ACPI;
485 break;
486
487 case EFI_LOADER_CODE:
488 case EFI_LOADER_DATA:
489 case EFI_BOOT_SERVICES_CODE:
490 case EFI_BOOT_SERVICES_DATA:
491 case EFI_CONVENTIONAL_MEMORY:
492 if (efi_soft_reserve_enabled() &&
493 (d->attribute & EFI_MEMORY_SP))
494 e820_type = E820_TYPE_SOFT_RESERVED;
495 else
496 e820_type = E820_TYPE_RAM;
497 break;
498
499 case EFI_ACPI_MEMORY_NVS:
500 e820_type = E820_TYPE_NVS;
501 break;
502
503 case EFI_PERSISTENT_MEMORY:
504 e820_type = E820_TYPE_PMEM;
505 break;
506
507 default:
508 continue;
509 }
510
511 /* Merge adjacent mappings */
512 if (prev && prev->type == e820_type &&
513 (prev->addr + prev->size) == d->phys_addr) {
514 prev->size += d->num_pages << 12;
515 continue;
516 }
517
518 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
519 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
520 sizeof(struct setup_data);
521
522 if (!e820ext || e820ext_size < need)
523 return EFI_BUFFER_TOO_SMALL;
524
525 /* boot_params map full, switch to e820 extended */
526 entry = (struct boot_e820_entry *)e820ext->data;
527 }
528
529 entry->addr = d->phys_addr;
530 entry->size = d->num_pages << PAGE_SHIFT;
531 entry->type = e820_type;
532 prev = entry++;
533 nr_entries++;
534 }
535
536 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
537 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
538
539 add_e820ext(params, e820ext, nr_e820ext);
540 nr_entries -= nr_e820ext;
541 }
542
543 params->e820_entries = (u8)nr_entries;
544
545 return EFI_SUCCESS;
546}
547
548static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
549 u32 *e820ext_size)
550{
551 efi_status_t status;
552 unsigned long size;
553
554 size = sizeof(struct setup_data) +
555 sizeof(struct e820_entry) * nr_desc;
556
557 if (*e820ext) {
558 efi_bs_call(free_pool, *e820ext);
559 *e820ext = NULL;
560 *e820ext_size = 0;
561 }
562
563 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
564 (void **)e820ext);
565 if (status == EFI_SUCCESS)
566 *e820ext_size = size;
567
568 return status;
569}
570
571static efi_status_t allocate_e820(struct boot_params *params,
572 struct setup_data **e820ext,
573 u32 *e820ext_size)
574{
575 unsigned long map_size, desc_size, map_key;
576 efi_status_t status;
577 __u32 nr_desc, desc_version;
578
579 /* Only need the size of the mem map and size of each mem descriptor */
580 map_size = 0;
581 status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
582 &desc_size, &desc_version);
583 if (status != EFI_BUFFER_TOO_SMALL)
584 return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
585
586 nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
587
588 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
589 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
590
591 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
592 if (status != EFI_SUCCESS)
593 return status;
594 }
595
596 return EFI_SUCCESS;
597}
598
599struct exit_boot_struct {
600 struct boot_params *boot_params;
601 struct efi_info *efi;
602};
603
604static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
605 void *priv)
606{
607 const char *signature;
608 struct exit_boot_struct *p = priv;
609
610 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
611 : EFI32_LOADER_SIGNATURE;
612 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
613
614 efi_set_u64_split((unsigned long)efi_system_table,
615 &p->efi->efi_systab, &p->efi->efi_systab_hi);
616 p->efi->efi_memdesc_size = *map->desc_size;
617 p->efi->efi_memdesc_version = *map->desc_ver;
618 efi_set_u64_split((unsigned long)*map->map,
619 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
620 p->efi->efi_memmap_size = *map->map_size;
621
622 return EFI_SUCCESS;
623}
624
625static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
626{
627 unsigned long map_sz, key, desc_size, buff_size;
628 efi_memory_desc_t *mem_map;
629 struct setup_data *e820ext = NULL;
630 __u32 e820ext_size = 0;
631 efi_status_t status;
632 __u32 desc_version;
633 struct efi_boot_memmap map;
634 struct exit_boot_struct priv;
635
636 map.map = &mem_map;
637 map.map_size = &map_sz;
638 map.desc_size = &desc_size;
639 map.desc_ver = &desc_version;
640 map.key_ptr = &key;
641 map.buff_size = &buff_size;
642 priv.boot_params = boot_params;
643 priv.efi = &boot_params->efi_info;
644
645 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
646 if (status != EFI_SUCCESS)
647 return status;
648
649 /* Might as well exit boot services now */
650 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
651 if (status != EFI_SUCCESS)
652 return status;
653
654 /* Historic? */
655 boot_params->alt_mem_k = 32 * 1024;
656
657 status = setup_e820(boot_params, e820ext, e820ext_size);
658 if (status != EFI_SUCCESS)
659 return status;
660
661 return EFI_SUCCESS;
662}
663
664/*
665 * On success, we return the address of startup_32, which has potentially been
666 * relocated by efi_relocate_kernel.
667 * On failure, we exit to the firmware via efi_exit instead of returning.
668 */
669unsigned long efi_main(efi_handle_t handle,
670 efi_system_table_t *sys_table_arg,
671 struct boot_params *boot_params)
672{
673 unsigned long bzimage_addr = (unsigned long)startup_32;
674 unsigned long buffer_start, buffer_end;
675 struct setup_header *hdr = &boot_params->hdr;
676 efi_status_t status;
677
678 efi_system_table = sys_table_arg;
679
680 /* Check if we were booted by the EFI firmware */
681 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
682 efi_exit(handle, EFI_INVALID_PARAMETER);
683
684 /*
685 * If the kernel isn't already loaded at a suitable address,
686 * relocate it.
687 *
688 * It must be loaded above LOAD_PHYSICAL_ADDR.
689 *
690 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
691 * is defined as the macro MAXMEM, but unfortunately that is not a
692 * compile-time constant if 5-level paging is configured, so we instead
693 * define our own macro for use here.
694 *
695 * For 32-bit, the maximum address is complicated to figure out, for
696 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
697 * KASLR uses.
698 *
699 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
700 * loaded by LoadImage, but rather by a bootloader that called the
701 * handover entry. The reason we must always relocate in this case is
702 * to handle the case of systemd-boot booting a unified kernel image,
703 * which is a PE executable that contains the bzImage and an initrd as
704 * COFF sections. The initrd section is placed after the bzImage
705 * without ensuring that there are at least init_size bytes available
706 * for the bzImage, and thus the compressed kernel's startup code may
707 * overwrite the initrd unless it is moved out of the way.
708 */
709
710 buffer_start = ALIGN(bzimage_addr - image_offset,
711 hdr->kernel_alignment);
712 buffer_end = buffer_start + hdr->init_size;
713
714 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
715 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
716 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
717 (image_offset == 0)) {
718 status = efi_relocate_kernel(&bzimage_addr,
719 hdr->init_size, hdr->init_size,
720 hdr->pref_address,
721 hdr->kernel_alignment,
722 LOAD_PHYSICAL_ADDR);
723 if (status != EFI_SUCCESS) {
724 efi_err("efi_relocate_kernel() failed!\n");
725 goto fail;
726 }
727 /*
728 * Now that we've copied the kernel elsewhere, we no longer
729 * have a set up block before startup_32(), so reset image_offset
730 * to zero in case it was set earlier.
731 */
732 image_offset = 0;
733 }
734
735#ifdef CONFIG_CMDLINE_BOOL
736 status = efi_parse_options(CONFIG_CMDLINE);
737 if (status != EFI_SUCCESS) {
738 efi_err("Failed to parse options\n");
739 goto fail;
740 }
741#endif
742 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
743 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
744 ((u64)boot_params->ext_cmd_line_ptr << 32));
745 status = efi_parse_options((char *)cmdline_paddr);
746 if (status != EFI_SUCCESS) {
747 efi_err("Failed to parse options\n");
748 goto fail;
749 }
750 }
751
752 /*
753 * At this point, an initrd may already have been loaded by the
754 * bootloader and passed via bootparams. We permit an initrd loaded
755 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
756 *
757 * If the device path is not present, any command-line initrd=
758 * arguments will be processed only if image is not NULL, which will be
759 * the case only if we were loaded via the PE entry point.
760 */
761 if (!efi_noinitrd) {
762 unsigned long addr, size;
763
764 status = efi_load_initrd(image, &addr, &size,
765 hdr->initrd_addr_max, ULONG_MAX);
766
767 if (status != EFI_SUCCESS) {
768 efi_err("Failed to load initrd!\n");
769 goto fail;
770 }
771 if (size > 0) {
772 efi_set_u64_split(addr, &hdr->ramdisk_image,
773 &boot_params->ext_ramdisk_image);
774 efi_set_u64_split(size, &hdr->ramdisk_size,
775 &boot_params->ext_ramdisk_size);
776 }
777 }
778
779 /*
780 * If the boot loader gave us a value for secure_boot then we use that,
781 * otherwise we ask the BIOS.
782 */
783 if (boot_params->secure_boot == efi_secureboot_mode_unset)
784 boot_params->secure_boot = efi_get_secureboot();
785
786 /* Ask the firmware to clear memory on unclean shutdown */
787 efi_enable_reset_attack_mitigation();
788
789 efi_random_get_seed();
790
791 efi_retrieve_tpm2_eventlog();
792
793 setup_graphics(boot_params);
794
795 setup_efi_pci(boot_params);
796
797 setup_quirks(boot_params);
798
799 status = exit_boot(boot_params, handle);
800 if (status != EFI_SUCCESS) {
801 efi_err("exit_boot() failed!\n");
802 goto fail;
803 }
804
805 return bzimage_addr;
806fail:
807 efi_err("efi_main() failed!\n");
808
809 efi_exit(handle, status);
810}
1// SPDX-License-Identifier: GPL-2.0-only
2
3/* -----------------------------------------------------------------------
4 *
5 * Copyright 2011 Intel Corporation; author Matt Fleming
6 *
7 * ----------------------------------------------------------------------- */
8
9#include <linux/efi.h>
10#include <linux/pci.h>
11#include <linux/stddef.h>
12
13#include <asm/efi.h>
14#include <asm/e820/types.h>
15#include <asm/setup.h>
16#include <asm/desc.h>
17#include <asm/boot.h>
18#include <asm/kaslr.h>
19#include <asm/sev.h>
20
21#include "efistub.h"
22#include "x86-stub.h"
23
24const efi_system_table_t *efi_system_table;
25const efi_dxe_services_table_t *efi_dxe_table;
26static efi_loaded_image_t *image = NULL;
27static efi_memory_attribute_protocol_t *memattr;
28
29typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30union sev_memory_acceptance_protocol {
31 struct {
32 efi_status_t (__efiapi * allow_unaccepted_memory)(
33 sev_memory_acceptance_protocol_t *);
34 };
35 struct {
36 u32 allow_unaccepted_memory;
37 } mixed_mode;
38};
39
40static efi_status_t
41preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
42{
43 struct pci_setup_rom *rom = NULL;
44 efi_status_t status;
45 unsigned long size;
46 uint64_t romsize;
47 void *romimage;
48
49 /*
50 * Some firmware images contain EFI function pointers at the place where
51 * the romimage and romsize fields are supposed to be. Typically the EFI
52 * code is mapped at high addresses, translating to an unrealistically
53 * large romsize. The UEFI spec limits the size of option ROMs to 16
54 * MiB so we reject any ROMs over 16 MiB in size to catch this.
55 */
56 romimage = efi_table_attr(pci, romimage);
57 romsize = efi_table_attr(pci, romsize);
58 if (!romimage || !romsize || romsize > SZ_16M)
59 return EFI_INVALID_PARAMETER;
60
61 size = romsize + sizeof(*rom);
62
63 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
64 (void **)&rom);
65 if (status != EFI_SUCCESS) {
66 efi_err("Failed to allocate memory for 'rom'\n");
67 return status;
68 }
69
70 memset(rom, 0, sizeof(*rom));
71
72 rom->data.type = SETUP_PCI;
73 rom->data.len = size - sizeof(struct setup_data);
74 rom->data.next = 0;
75 rom->pcilen = romsize;
76 *__rom = rom;
77
78 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
79 PCI_VENDOR_ID, 1, &rom->vendor);
80
81 if (status != EFI_SUCCESS) {
82 efi_err("Failed to read rom->vendor\n");
83 goto free_struct;
84 }
85
86 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
87 PCI_DEVICE_ID, 1, &rom->devid);
88
89 if (status != EFI_SUCCESS) {
90 efi_err("Failed to read rom->devid\n");
91 goto free_struct;
92 }
93
94 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
95 &rom->device, &rom->function);
96
97 if (status != EFI_SUCCESS)
98 goto free_struct;
99
100 memcpy(rom->romdata, romimage, romsize);
101 return status;
102
103free_struct:
104 efi_bs_call(free_pool, rom);
105 return status;
106}
107
108/*
109 * There's no way to return an informative status from this function,
110 * because any analysis (and printing of error messages) needs to be
111 * done directly at the EFI function call-site.
112 *
113 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
114 * just didn't find any PCI devices, but there's no way to tell outside
115 * the context of the call.
116 */
117static void setup_efi_pci(struct boot_params *params)
118{
119 efi_status_t status;
120 void **pci_handle = NULL;
121 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
122 unsigned long size = 0;
123 struct setup_data *data;
124 efi_handle_t h;
125 int i;
126
127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
129
130 if (status == EFI_BUFFER_TOO_SMALL) {
131 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
132 (void **)&pci_handle);
133
134 if (status != EFI_SUCCESS) {
135 efi_err("Failed to allocate memory for 'pci_handle'\n");
136 return;
137 }
138
139 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
140 &pci_proto, NULL, &size, pci_handle);
141 }
142
143 if (status != EFI_SUCCESS)
144 goto free_handle;
145
146 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
147
148 while (data && data->next)
149 data = (struct setup_data *)(unsigned long)data->next;
150
151 for_each_efi_handle(h, pci_handle, size, i) {
152 efi_pci_io_protocol_t *pci = NULL;
153 struct pci_setup_rom *rom;
154
155 status = efi_bs_call(handle_protocol, h, &pci_proto,
156 (void **)&pci);
157 if (status != EFI_SUCCESS || !pci)
158 continue;
159
160 status = preserve_pci_rom_image(pci, &rom);
161 if (status != EFI_SUCCESS)
162 continue;
163
164 if (data)
165 data->next = (unsigned long)rom;
166 else
167 params->hdr.setup_data = (unsigned long)rom;
168
169 data = (struct setup_data *)rom;
170 }
171
172free_handle:
173 efi_bs_call(free_pool, pci_handle);
174}
175
176static void retrieve_apple_device_properties(struct boot_params *boot_params)
177{
178 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
179 struct setup_data *data, *new;
180 efi_status_t status;
181 u32 size = 0;
182 apple_properties_protocol_t *p;
183
184 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
185 if (status != EFI_SUCCESS)
186 return;
187
188 if (efi_table_attr(p, version) != 0x10000) {
189 efi_err("Unsupported properties proto version\n");
190 return;
191 }
192
193 efi_call_proto(p, get_all, NULL, &size);
194 if (!size)
195 return;
196
197 do {
198 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
199 size + sizeof(struct setup_data),
200 (void **)&new);
201 if (status != EFI_SUCCESS) {
202 efi_err("Failed to allocate memory for 'properties'\n");
203 return;
204 }
205
206 status = efi_call_proto(p, get_all, new->data, &size);
207
208 if (status == EFI_BUFFER_TOO_SMALL)
209 efi_bs_call(free_pool, new);
210 } while (status == EFI_BUFFER_TOO_SMALL);
211
212 new->type = SETUP_APPLE_PROPERTIES;
213 new->len = size;
214 new->next = 0;
215
216 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
217 if (!data) {
218 boot_params->hdr.setup_data = (unsigned long)new;
219 } else {
220 while (data->next)
221 data = (struct setup_data *)(unsigned long)data->next;
222 data->next = (unsigned long)new;
223 }
224}
225
226efi_status_t efi_adjust_memory_range_protection(unsigned long start,
227 unsigned long size)
228{
229 efi_status_t status;
230 efi_gcd_memory_space_desc_t desc;
231 unsigned long end, next;
232 unsigned long rounded_start, rounded_end;
233 unsigned long unprotect_start, unprotect_size;
234
235 rounded_start = rounddown(start, EFI_PAGE_SIZE);
236 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
237
238 if (memattr != NULL) {
239 status = efi_call_proto(memattr, clear_memory_attributes,
240 rounded_start,
241 rounded_end - rounded_start,
242 EFI_MEMORY_XP);
243 if (status != EFI_SUCCESS)
244 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
245 return status;
246 }
247
248 if (efi_dxe_table == NULL)
249 return EFI_SUCCESS;
250
251 /*
252 * Don't modify memory region attributes, they are
253 * already suitable, to lower the possibility to
254 * encounter firmware bugs.
255 */
256
257 for (end = start + size; start < end; start = next) {
258
259 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
260
261 if (status != EFI_SUCCESS)
262 break;
263
264 next = desc.base_address + desc.length;
265
266 /*
267 * Only system memory is suitable for trampoline/kernel image placement,
268 * so only this type of memory needs its attributes to be modified.
269 */
270
271 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
272 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
273 continue;
274
275 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
276 unprotect_size = min(rounded_end, next) - unprotect_start;
277
278 status = efi_dxe_call(set_memory_space_attributes,
279 unprotect_start, unprotect_size,
280 EFI_MEMORY_WB);
281
282 if (status != EFI_SUCCESS) {
283 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
284 unprotect_start,
285 unprotect_start + unprotect_size,
286 status);
287 break;
288 }
289 }
290 return EFI_SUCCESS;
291}
292
293static void setup_unaccepted_memory(void)
294{
295 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
296 sev_memory_acceptance_protocol_t *proto;
297 efi_status_t status;
298
299 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
300 return;
301
302 /*
303 * Enable unaccepted memory before calling exit boot services in order
304 * for the UEFI to not accept all memory on EBS.
305 */
306 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
307 (void **)&proto);
308 if (status != EFI_SUCCESS)
309 return;
310
311 status = efi_call_proto(proto, allow_unaccepted_memory);
312 if (status != EFI_SUCCESS)
313 efi_err("Memory acceptance protocol failed\n");
314}
315
316static efi_char16_t *efistub_fw_vendor(void)
317{
318 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
319
320 return (efi_char16_t *)vendor;
321}
322
323static const efi_char16_t apple[] = L"Apple";
324
325static void setup_quirks(struct boot_params *boot_params)
326{
327 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
328 !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
329 retrieve_apple_device_properties(boot_params);
330}
331
332/*
333 * See if we have Universal Graphics Adapter (UGA) protocol
334 */
335static efi_status_t
336setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
337{
338 efi_status_t status;
339 u32 width, height;
340 void **uga_handle = NULL;
341 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
342 efi_handle_t handle;
343 int i;
344
345 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
346 (void **)&uga_handle);
347 if (status != EFI_SUCCESS)
348 return status;
349
350 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
351 uga_proto, NULL, &size, uga_handle);
352 if (status != EFI_SUCCESS)
353 goto free_handle;
354
355 height = 0;
356 width = 0;
357
358 first_uga = NULL;
359 for_each_efi_handle(handle, uga_handle, size, i) {
360 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
361 u32 w, h, depth, refresh;
362 void *pciio;
363
364 status = efi_bs_call(handle_protocol, handle, uga_proto,
365 (void **)&uga);
366 if (status != EFI_SUCCESS)
367 continue;
368
369 pciio = NULL;
370 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
371
372 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
373 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
374 width = w;
375 height = h;
376
377 /*
378 * Once we've found a UGA supporting PCIIO,
379 * don't bother looking any further.
380 */
381 if (pciio)
382 break;
383
384 first_uga = uga;
385 }
386 }
387
388 if (!width && !height)
389 goto free_handle;
390
391 /* EFI framebuffer */
392 si->orig_video_isVGA = VIDEO_TYPE_EFI;
393
394 si->lfb_depth = 32;
395 si->lfb_width = width;
396 si->lfb_height = height;
397
398 si->red_size = 8;
399 si->red_pos = 16;
400 si->green_size = 8;
401 si->green_pos = 8;
402 si->blue_size = 8;
403 si->blue_pos = 0;
404 si->rsvd_size = 8;
405 si->rsvd_pos = 24;
406
407free_handle:
408 efi_bs_call(free_pool, uga_handle);
409
410 return status;
411}
412
413static void setup_graphics(struct boot_params *boot_params)
414{
415 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
416 struct screen_info *si;
417 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
418 efi_status_t status;
419 unsigned long size;
420 void **gop_handle = NULL;
421 void **uga_handle = NULL;
422
423 si = &boot_params->screen_info;
424 memset(si, 0, sizeof(*si));
425
426 size = 0;
427 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
428 &graphics_proto, NULL, &size, gop_handle);
429 if (status == EFI_BUFFER_TOO_SMALL)
430 status = efi_setup_gop(si, &graphics_proto, size);
431
432 if (status != EFI_SUCCESS) {
433 size = 0;
434 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
435 &uga_proto, NULL, &size, uga_handle);
436 if (status == EFI_BUFFER_TOO_SMALL)
437 setup_uga(si, &uga_proto, size);
438 }
439}
440
441
442static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
443{
444 efi_bs_call(exit, handle, status, 0, NULL);
445 for(;;)
446 asm("hlt");
447}
448
449void __noreturn efi_stub_entry(efi_handle_t handle,
450 efi_system_table_t *sys_table_arg,
451 struct boot_params *boot_params);
452
453/*
454 * Because the x86 boot code expects to be passed a boot_params we
455 * need to create one ourselves (usually the bootloader would create
456 * one for us).
457 */
458efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
459 efi_system_table_t *sys_table_arg)
460{
461 static struct boot_params boot_params __page_aligned_bss;
462 struct setup_header *hdr = &boot_params.hdr;
463 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
464 int options_size = 0;
465 efi_status_t status;
466 char *cmdline_ptr;
467
468 efi_system_table = sys_table_arg;
469
470 /* Check if we were booted by the EFI firmware */
471 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
472 efi_exit(handle, EFI_INVALID_PARAMETER);
473
474 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
475 if (status != EFI_SUCCESS) {
476 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
477 efi_exit(handle, status);
478 }
479
480 /* Assign the setup_header fields that the kernel actually cares about */
481 hdr->root_flags = 1;
482 hdr->vid_mode = 0xffff;
483
484 hdr->type_of_loader = 0x21;
485
486 /* Convert unicode cmdline to ascii */
487 cmdline_ptr = efi_convert_cmdline(image, &options_size);
488 if (!cmdline_ptr)
489 goto fail;
490
491 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
492 &boot_params.ext_cmd_line_ptr);
493
494 efi_stub_entry(handle, sys_table_arg, &boot_params);
495 /* not reached */
496
497fail:
498 efi_exit(handle, status);
499}
500
501static void add_e820ext(struct boot_params *params,
502 struct setup_data *e820ext, u32 nr_entries)
503{
504 struct setup_data *data;
505
506 e820ext->type = SETUP_E820_EXT;
507 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
508 e820ext->next = 0;
509
510 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
511
512 while (data && data->next)
513 data = (struct setup_data *)(unsigned long)data->next;
514
515 if (data)
516 data->next = (unsigned long)e820ext;
517 else
518 params->hdr.setup_data = (unsigned long)e820ext;
519}
520
521static efi_status_t
522setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
523{
524 struct boot_e820_entry *entry = params->e820_table;
525 struct efi_info *efi = ¶ms->efi_info;
526 struct boot_e820_entry *prev = NULL;
527 u32 nr_entries;
528 u32 nr_desc;
529 int i;
530
531 nr_entries = 0;
532 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
533
534 for (i = 0; i < nr_desc; i++) {
535 efi_memory_desc_t *d;
536 unsigned int e820_type = 0;
537 unsigned long m = efi->efi_memmap;
538
539#ifdef CONFIG_X86_64
540 m |= (u64)efi->efi_memmap_hi << 32;
541#endif
542
543 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
544 switch (d->type) {
545 case EFI_RESERVED_TYPE:
546 case EFI_RUNTIME_SERVICES_CODE:
547 case EFI_RUNTIME_SERVICES_DATA:
548 case EFI_MEMORY_MAPPED_IO:
549 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
550 case EFI_PAL_CODE:
551 e820_type = E820_TYPE_RESERVED;
552 break;
553
554 case EFI_UNUSABLE_MEMORY:
555 e820_type = E820_TYPE_UNUSABLE;
556 break;
557
558 case EFI_ACPI_RECLAIM_MEMORY:
559 e820_type = E820_TYPE_ACPI;
560 break;
561
562 case EFI_LOADER_CODE:
563 case EFI_LOADER_DATA:
564 case EFI_BOOT_SERVICES_CODE:
565 case EFI_BOOT_SERVICES_DATA:
566 case EFI_CONVENTIONAL_MEMORY:
567 if (efi_soft_reserve_enabled() &&
568 (d->attribute & EFI_MEMORY_SP))
569 e820_type = E820_TYPE_SOFT_RESERVED;
570 else
571 e820_type = E820_TYPE_RAM;
572 break;
573
574 case EFI_ACPI_MEMORY_NVS:
575 e820_type = E820_TYPE_NVS;
576 break;
577
578 case EFI_PERSISTENT_MEMORY:
579 e820_type = E820_TYPE_PMEM;
580 break;
581
582 case EFI_UNACCEPTED_MEMORY:
583 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
584 continue;
585 e820_type = E820_TYPE_RAM;
586 process_unaccepted_memory(d->phys_addr,
587 d->phys_addr + PAGE_SIZE * d->num_pages);
588 break;
589 default:
590 continue;
591 }
592
593 /* Merge adjacent mappings */
594 if (prev && prev->type == e820_type &&
595 (prev->addr + prev->size) == d->phys_addr) {
596 prev->size += d->num_pages << 12;
597 continue;
598 }
599
600 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
601 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
602 sizeof(struct setup_data);
603
604 if (!e820ext || e820ext_size < need)
605 return EFI_BUFFER_TOO_SMALL;
606
607 /* boot_params map full, switch to e820 extended */
608 entry = (struct boot_e820_entry *)e820ext->data;
609 }
610
611 entry->addr = d->phys_addr;
612 entry->size = d->num_pages << PAGE_SHIFT;
613 entry->type = e820_type;
614 prev = entry++;
615 nr_entries++;
616 }
617
618 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
619 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
620
621 add_e820ext(params, e820ext, nr_e820ext);
622 nr_entries -= nr_e820ext;
623 }
624
625 params->e820_entries = (u8)nr_entries;
626
627 return EFI_SUCCESS;
628}
629
630static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
631 u32 *e820ext_size)
632{
633 efi_status_t status;
634 unsigned long size;
635
636 size = sizeof(struct setup_data) +
637 sizeof(struct e820_entry) * nr_desc;
638
639 if (*e820ext) {
640 efi_bs_call(free_pool, *e820ext);
641 *e820ext = NULL;
642 *e820ext_size = 0;
643 }
644
645 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
646 (void **)e820ext);
647 if (status == EFI_SUCCESS)
648 *e820ext_size = size;
649
650 return status;
651}
652
653static efi_status_t allocate_e820(struct boot_params *params,
654 struct setup_data **e820ext,
655 u32 *e820ext_size)
656{
657 struct efi_boot_memmap *map;
658 efi_status_t status;
659 __u32 nr_desc;
660
661 status = efi_get_memory_map(&map, false);
662 if (status != EFI_SUCCESS)
663 return status;
664
665 nr_desc = map->map_size / map->desc_size;
666 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
667 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
668 EFI_MMAP_NR_SLACK_SLOTS;
669
670 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
671 }
672
673 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
674 status = allocate_unaccepted_bitmap(nr_desc, map);
675
676 efi_bs_call(free_pool, map);
677 return status;
678}
679
680struct exit_boot_struct {
681 struct boot_params *boot_params;
682 struct efi_info *efi;
683};
684
685static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
686 void *priv)
687{
688 const char *signature;
689 struct exit_boot_struct *p = priv;
690
691 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
692 : EFI32_LOADER_SIGNATURE;
693 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
694
695 efi_set_u64_split((unsigned long)efi_system_table,
696 &p->efi->efi_systab, &p->efi->efi_systab_hi);
697 p->efi->efi_memdesc_size = map->desc_size;
698 p->efi->efi_memdesc_version = map->desc_ver;
699 efi_set_u64_split((unsigned long)map->map,
700 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
701 p->efi->efi_memmap_size = map->map_size;
702
703 return EFI_SUCCESS;
704}
705
706static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
707{
708 struct setup_data *e820ext = NULL;
709 __u32 e820ext_size = 0;
710 efi_status_t status;
711 struct exit_boot_struct priv;
712
713 priv.boot_params = boot_params;
714 priv.efi = &boot_params->efi_info;
715
716 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
717 if (status != EFI_SUCCESS)
718 return status;
719
720 /* Might as well exit boot services now */
721 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
722 if (status != EFI_SUCCESS)
723 return status;
724
725 /* Historic? */
726 boot_params->alt_mem_k = 32 * 1024;
727
728 status = setup_e820(boot_params, e820ext, e820ext_size);
729 if (status != EFI_SUCCESS)
730 return status;
731
732 return EFI_SUCCESS;
733}
734
735static bool have_unsupported_snp_features(void)
736{
737 u64 unsupported;
738
739 unsupported = snp_get_unsupported_features(sev_get_status());
740 if (unsupported) {
741 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
742 unsupported);
743 return true;
744 }
745 return false;
746}
747
748static void efi_get_seed(void *seed, int size)
749{
750 efi_get_random_bytes(size, seed);
751
752 /*
753 * This only updates seed[0] when running on 32-bit, but in that case,
754 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
755 */
756 *(unsigned long *)seed ^= kaslr_get_random_long("EFI");
757}
758
759static void error(char *str)
760{
761 efi_warn("Decompression failed: %s\n", str);
762}
763
764static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
765{
766 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
767 unsigned long addr, alloc_size, entry;
768 efi_status_t status;
769 u32 seed[2] = {};
770
771 /* determine the required size of the allocation */
772 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
773 MIN_KERNEL_ALIGN);
774
775 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
776 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
777 static const efi_char16_t ami[] = L"American Megatrends";
778
779 efi_get_seed(seed, sizeof(seed));
780
781 virt_addr += (range * seed[1]) >> 32;
782 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
783
784 /*
785 * Older Dell systems with AMI UEFI firmware v2.0 may hang
786 * while decompressing the kernel if physical address
787 * randomization is enabled.
788 *
789 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
790 */
791 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
792 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
793 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
794 seed[0] = 0;
795 }
796
797 boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
798 }
799
800 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
801 seed[0], EFI_LOADER_CODE,
802 LOAD_PHYSICAL_ADDR,
803 EFI_X86_KERNEL_ALLOC_LIMIT);
804 if (status != EFI_SUCCESS)
805 return status;
806
807 entry = decompress_kernel((void *)addr, virt_addr, error);
808 if (entry == ULONG_MAX) {
809 efi_free(alloc_size, addr);
810 return EFI_LOAD_ERROR;
811 }
812
813 *kernel_entry = addr + entry;
814
815 return efi_adjust_memory_range_protection(addr, kernel_total_size);
816}
817
818static void __noreturn enter_kernel(unsigned long kernel_addr,
819 struct boot_params *boot_params)
820{
821 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
822 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
823
824 unreachable();
825}
826
827/*
828 * On success, this routine will jump to the relocated image directly and never
829 * return. On failure, it will exit to the firmware via efi_exit() instead of
830 * returning.
831 */
832void __noreturn efi_stub_entry(efi_handle_t handle,
833 efi_system_table_t *sys_table_arg,
834 struct boot_params *boot_params)
835{
836 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
837 struct setup_header *hdr = &boot_params->hdr;
838 const struct linux_efi_initrd *initrd = NULL;
839 unsigned long kernel_entry;
840 efi_status_t status;
841
842 boot_params_ptr = boot_params;
843
844 efi_system_table = sys_table_arg;
845 /* Check if we were booted by the EFI firmware */
846 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
847 efi_exit(handle, EFI_INVALID_PARAMETER);
848
849 if (have_unsupported_snp_features())
850 efi_exit(handle, EFI_UNSUPPORTED);
851
852 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
853 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
854 if (efi_dxe_table &&
855 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
856 efi_warn("Ignoring DXE services table: invalid signature\n");
857 efi_dxe_table = NULL;
858 }
859 }
860
861 /* grab the memory attributes protocol if it exists */
862 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
863
864 status = efi_setup_5level_paging();
865 if (status != EFI_SUCCESS) {
866 efi_err("efi_setup_5level_paging() failed!\n");
867 goto fail;
868 }
869
870#ifdef CONFIG_CMDLINE_BOOL
871 status = efi_parse_options(CONFIG_CMDLINE);
872 if (status != EFI_SUCCESS) {
873 efi_err("Failed to parse options\n");
874 goto fail;
875 }
876#endif
877 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
878 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
879 ((u64)boot_params->ext_cmd_line_ptr << 32));
880 status = efi_parse_options((char *)cmdline_paddr);
881 if (status != EFI_SUCCESS) {
882 efi_err("Failed to parse options\n");
883 goto fail;
884 }
885 }
886
887 status = efi_decompress_kernel(&kernel_entry);
888 if (status != EFI_SUCCESS) {
889 efi_err("Failed to decompress kernel\n");
890 goto fail;
891 }
892
893 /*
894 * At this point, an initrd may already have been loaded by the
895 * bootloader and passed via bootparams. We permit an initrd loaded
896 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
897 *
898 * If the device path is not present, any command-line initrd=
899 * arguments will be processed only if image is not NULL, which will be
900 * the case only if we were loaded via the PE entry point.
901 */
902 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
903 &initrd);
904 if (status != EFI_SUCCESS)
905 goto fail;
906 if (initrd && initrd->size > 0) {
907 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
908 &boot_params->ext_ramdisk_image);
909 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
910 &boot_params->ext_ramdisk_size);
911 }
912
913
914 /*
915 * If the boot loader gave us a value for secure_boot then we use that,
916 * otherwise we ask the BIOS.
917 */
918 if (boot_params->secure_boot == efi_secureboot_mode_unset)
919 boot_params->secure_boot = efi_get_secureboot();
920
921 /* Ask the firmware to clear memory on unclean shutdown */
922 efi_enable_reset_attack_mitigation();
923
924 efi_random_get_seed();
925
926 efi_retrieve_tpm2_eventlog();
927
928 setup_graphics(boot_params);
929
930 setup_efi_pci(boot_params);
931
932 setup_quirks(boot_params);
933
934 setup_unaccepted_memory();
935
936 status = exit_boot(boot_params, handle);
937 if (status != EFI_SUCCESS) {
938 efi_err("exit_boot() failed!\n");
939 goto fail;
940 }
941
942 /*
943 * Call the SEV init code while still running with the firmware's
944 * GDT/IDT, so #VC exceptions will be handled by EFI.
945 */
946 sev_enable(boot_params);
947
948 efi_5level_switch();
949
950 enter_kernel(kernel_entry, boot_params);
951fail:
952 efi_err("efi_stub_entry() failed!\n");
953
954 efi_exit(handle, status);
955}
956
957#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
958void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
959 struct boot_params *boot_params)
960{
961 extern char _bss[], _ebss[];
962
963 memset(_bss, 0, _ebss - _bss);
964 efi_stub_entry(handle, sys_table_arg, boot_params);
965}
966
967#ifndef CONFIG_EFI_MIXED
968extern __alias(efi_handover_entry)
969void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
970 struct boot_params *boot_params);
971
972extern __alias(efi_handover_entry)
973void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
974 struct boot_params *boot_params);
975#endif
976#endif