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