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