efi/libstub: Remove 'sys_table_arg' from all function prototypes
[linux-2.6-block.git] / arch / x86 / boot / compressed / eboot.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>
5520b7e7 11
291f3632 12#include <asm/efi.h>
5520b7e7 13#include <asm/e820/types.h>
291f3632
MF
14#include <asm/setup.h>
15#include <asm/desc.h>
220dd769 16#include <asm/boot.h>
291f3632 17
393f203f 18#include "../string.h"
291f3632
MF
19#include "eboot.h"
20
21static efi_system_table_t *sys_table;
c3710de5 22static bool efi_is64 = IS_ENABLED(CONFIG_X86_64);
204b0a1a 23
2fcdad2a
AB
24__pure efi_system_table_t *efi_system_table(void)
25{
26 return sys_table;
27}
28
c3710de5
AB
29__pure bool efi_is_64bit(void)
30{
31 return efi_is64;
54b52d87 32}
291f3632 33
c116e8d6 34static efi_status_t
75c5a713 35preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
dd5fc854 36{
c116e8d6 37 struct pci_setup_rom *rom = NULL;
dd5fc854 38 efi_status_t status;
c116e8d6 39 unsigned long size;
e2967018 40 uint64_t romsize;
2c3625cb 41 void *romimage;
dd5fc854 42
1de3a1be 43 /*
e2967018
AB
44 * Some firmware images contain EFI function pointers at the place where
45 * the romimage and romsize fields are supposed to be. Typically the EFI
1de3a1be
HG
46 * code is mapped at high addresses, translating to an unrealistically
47 * large romsize. The UEFI spec limits the size of option ROMs to 16
48 * MiB so we reject any ROMs over 16 MiB in size to catch this.
49 */
f958efe9 50 romimage = efi_table_attr(efi_pci_io_protocol, romimage, pci);
2c3625cb 51 romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
1de3a1be 52 if (!romimage || !romsize || romsize > SZ_16M)
c116e8d6 53 return EFI_INVALID_PARAMETER;
dd5fc854 54
2c3625cb 55 size = romsize + sizeof(*rom);
dd5fc854 56
960a8d01
AB
57 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size,
58 (void **)&rom);
77e21e87 59 if (status != EFI_SUCCESS) {
8173ec79 60 efi_printk("Failed to allocate memory for 'rom'\n");
c116e8d6 61 return status;
77e21e87 62 }
dd5fc854 63
c116e8d6
MF
64 memset(rom, 0, sizeof(*rom));
65
90a2186b
IM
66 rom->data.type = SETUP_PCI;
67 rom->data.len = size - sizeof(struct setup_data);
68 rom->data.next = 0;
69 rom->pcilen = pci->romsize;
c116e8d6
MF
70 *__rom = rom;
71
2c3625cb
AB
72 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
73 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
74 &rom->vendor);
dd5fc854 75
77e21e87 76 if (status != EFI_SUCCESS) {
8173ec79 77 efi_printk("Failed to read rom->vendor\n");
c116e8d6 78 goto free_struct;
77e21e87 79 }
c116e8d6 80
2c3625cb
AB
81 status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
82 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
83 &rom->devid);
c116e8d6 84
77e21e87 85 if (status != EFI_SUCCESS) {
8173ec79 86 efi_printk("Failed to read rom->devid\n");
c116e8d6 87 goto free_struct;
77e21e87 88 }
c116e8d6 89
2c3625cb
AB
90 status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
91 &rom->segment, &rom->bus, &rom->device,
92 &rom->function);
c116e8d6
MF
93
94 if (status != EFI_SUCCESS)
95 goto free_struct;
96
2c3625cb 97 memcpy(rom->romdata, romimage, romsize);
c116e8d6
MF
98 return status;
99
100free_struct:
204b0a1a 101 efi_call_early(free_pool, rom);
c116e8d6
MF
102 return status;
103}
104
56394ab8
MF
105/*
106 * There's no way to return an informative status from this function,
107 * because any analysis (and printing of error messages) needs to be
108 * done directly at the EFI function call-site.
109 *
110 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
111 * just didn't find any PCI devices, but there's no way to tell outside
112 * the context of the call.
113 */
114static void setup_efi_pci(struct boot_params *params)
291f3632 115{
291f3632 116 efi_status_t status;
c116e8d6
MF
117 void **pci_handle = NULL;
118 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
119 unsigned long size = 0;
75c5a713
AB
120 unsigned long nr_pci;
121 struct setup_data *data;
2732ea0d 122 efi_handle_t h;
75c5a713 123 int i;
291f3632 124
204b0a1a
MF
125 status = efi_call_early(locate_handle,
126 EFI_LOCATE_BY_PROTOCOL,
127 &pci_proto, NULL, &size, pci_handle);
291f3632 128
c116e8d6 129 if (status == EFI_BUFFER_TOO_SMALL) {
204b0a1a
MF
130 status = efi_call_early(allocate_pool,
131 EFI_LOADER_DATA,
132 size, (void **)&pci_handle);
291f3632 133
77e21e87 134 if (status != EFI_SUCCESS) {
8173ec79 135 efi_printk("Failed to allocate memory for 'pci_handle'\n");
56394ab8 136 return;
77e21e87 137 }
291f3632 138
204b0a1a
MF
139 status = efi_call_early(locate_handle,
140 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
141 NULL, &size, pci_handle);
291f3632
MF
142 }
143
c116e8d6 144 if (status != EFI_SUCCESS)
291f3632
MF
145 goto free_handle;
146
75c5a713
AB
147 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
148
149 while (data && data->next)
150 data = (struct setup_data *)(unsigned long)data->next;
151
2732ea0d 152 for_each_efi_handle(h, pci_handle, size, i) {
75c5a713
AB
153 efi_pci_io_protocol_t *pci = NULL;
154 struct pci_setup_rom *rom;
155
2732ea0d 156 status = efi_call_early(handle_protocol, h,
75c5a713
AB
157 &pci_proto, (void **)&pci);
158 if (status != EFI_SUCCESS || !pci)
159 continue;
160
161 status = preserve_pci_rom_image(pci, &rom);
162 if (status != EFI_SUCCESS)
163 continue;
164
165 if (data)
166 data->next = (unsigned long)rom;
167 else
168 params->hdr.setup_data = (unsigned long)rom;
169
170 data = (struct setup_data *)rom;
171 }
291f3632 172
c116e8d6 173free_handle:
204b0a1a 174 efi_call_early(free_pool, pci_handle);
c116e8d6 175}
291f3632 176
58c5475a
LW
177static void retrieve_apple_device_properties(struct boot_params *boot_params)
178{
179 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
180 struct setup_data *data, *new;
181 efi_status_t status;
182 u32 size = 0;
960a8d01 183 apple_properties_protocol_t *p;
58c5475a 184
960a8d01 185 status = efi_call_early(locate_protocol, &guid, NULL, (void **)&p);
58c5475a
LW
186 if (status != EFI_SUCCESS)
187 return;
188
189 if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
8173ec79 190 efi_printk("Unsupported properties proto version\n");
58c5475a
LW
191 return;
192 }
193
194 efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
195 if (!size)
196 return;
197
198 do {
199 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
960a8d01
AB
200 size + sizeof(struct setup_data),
201 (void **)&new);
58c5475a 202 if (status != EFI_SUCCESS) {
8173ec79 203 efi_printk("Failed to allocate memory for 'properties'\n");
58c5475a
LW
204 return;
205 }
206
207 status = efi_call_proto(apple_properties_protocol, get_all, p,
208 new->data, &size);
209
210 if (status == EFI_BUFFER_TOO_SMALL)
211 efi_call_early(free_pool, new);
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
36b64976
AB
228static const efi_char16_t apple[] = L"Apple";
229
58c5475a
LW
230static void setup_quirks(struct boot_params *boot_params)
231{
58c5475a
LW
232 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
233 efi_table_attr(efi_system_table, fw_vendor, sys_table);
234
235 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
236 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
237 retrieve_apple_device_properties(boot_params);
238 }
239}
240
290084c2
AB
241/*
242 * See if we have Universal Graphics Adapter (UGA) protocol
243 */
c116e8d6 244static efi_status_t
290084c2 245setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
c116e8d6 246{
290084c2
AB
247 efi_status_t status;
248 u32 width, height;
249 void **uga_handle = NULL;
0b767b16 250 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
c116e8d6 251 unsigned long nr_ugas;
2732ea0d 252 efi_handle_t handle;
c116e8d6
MF
253 int i;
254
290084c2
AB
255 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
256 size, (void **)&uga_handle);
257 if (status != EFI_SUCCESS)
258 return status;
c116e8d6 259
290084c2
AB
260 status = efi_call_early(locate_handle,
261 EFI_LOCATE_BY_PROTOCOL,
262 uga_proto, NULL, &size, uga_handle);
263 if (status != EFI_SUCCESS)
264 goto free_handle;
291f3632 265
290084c2
AB
266 height = 0;
267 width = 0;
c116e8d6
MF
268
269 first_uga = NULL;
2732ea0d 270 for_each_efi_handle(handle, uga_handle, size, i) {
291f3632 271 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
291f3632
MF
272 u32 w, h, depth, refresh;
273 void *pciio;
274
204b0a1a 275 status = efi_call_early(handle_protocol, handle,
290084c2 276 uga_proto, (void **)&uga);
291f3632
MF
277 if (status != EFI_SUCCESS)
278 continue;
279
093174f5 280 pciio = NULL;
204b0a1a 281 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
291f3632 282
290084c2
AB
283 status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
284 &w, &h, &depth, &refresh);
291f3632 285 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
290084c2
AB
286 width = w;
287 height = h;
291f3632
MF
288
289 /*
290 * Once we've found a UGA supporting PCIIO,
291 * don't bother looking any further.
292 */
293 if (pciio)
294 break;
295
296 first_uga = uga;
297 }
298 }
299
c116e8d6 300 if (!width && !height)
291f3632
MF
301 goto free_handle;
302
303 /* EFI framebuffer */
90a2186b 304 si->orig_video_isVGA = VIDEO_TYPE_EFI;
291f3632 305
90a2186b
IM
306 si->lfb_depth = 32;
307 si->lfb_width = width;
308 si->lfb_height = height;
291f3632 309
90a2186b
IM
310 si->red_size = 8;
311 si->red_pos = 16;
312 si->green_size = 8;
313 si->green_pos = 8;
314 si->blue_size = 8;
315 si->blue_pos = 0;
316 si->rsvd_size = 8;
317 si->rsvd_pos = 24;
291f3632 318
291f3632 319free_handle:
204b0a1a 320 efi_call_early(free_pool, uga_handle);
90a2186b 321
291f3632
MF
322 return status;
323}
324
325void setup_graphics(struct boot_params *boot_params)
326{
327 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
328 struct screen_info *si;
329 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
330 efi_status_t status;
331 unsigned long size;
332 void **gop_handle = NULL;
333 void **uga_handle = NULL;
334
335 si = &boot_params->screen_info;
336 memset(si, 0, sizeof(*si));
337
338 size = 0;
204b0a1a
MF
339 status = efi_call_early(locate_handle,
340 EFI_LOCATE_BY_PROTOCOL,
341 &graphics_proto, NULL, &size, gop_handle);
291f3632 342 if (status == EFI_BUFFER_TOO_SMALL)
cd33a5c1 343 status = efi_setup_gop(si, &graphics_proto, size);
291f3632
MF
344
345 if (status != EFI_SUCCESS) {
346 size = 0;
204b0a1a
MF
347 status = efi_call_early(locate_handle,
348 EFI_LOCATE_BY_PROTOCOL,
349 &uga_proto, NULL, &size, uga_handle);
291f3632
MF
350 if (status == EFI_BUFFER_TOO_SMALL)
351 setup_uga(si, &uga_proto, size);
352 }
353}
354
c3710de5
AB
355void startup_32(struct boot_params *boot_params);
356
357void __noreturn efi_stub_entry(efi_handle_t handle,
358 efi_system_table_t *sys_table_arg,
359 struct boot_params *boot_params);
360
291f3632
MF
361/*
362 * Because the x86 boot code expects to be passed a boot_params we
363 * need to create one ourselves (usually the bootloader would create
364 * one for us).
365 */
c3710de5
AB
366efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
367 efi_system_table_t *sys_table_arg)
291f3632 368{
9ca8f72a 369 struct boot_params *boot_params;
9ca8f72a
MF
370 struct apm_bios_info *bi;
371 struct setup_header *hdr;
9ca8f72a 372 efi_loaded_image_t *image;
9ca8f72a 373 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
291f3632
MF
374 int options_size = 0;
375 efi_status_t status;
5fef3870 376 char *cmdline_ptr;
46f4582e
RF
377 unsigned long ramdisk_addr;
378 unsigned long ramdisk_size;
291f3632 379
c3710de5 380 sys_table = sys_table_arg;
9ca8f72a
MF
381
382 /* Check if we were booted by the EFI firmware */
383 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
c3710de5 384 return EFI_INVALID_PARAMETER;
54b52d87 385
204b0a1a
MF
386 status = efi_call_early(handle_protocol, handle,
387 &proto, (void *)&image);
9ca8f72a 388 if (status != EFI_SUCCESS) {
8173ec79 389 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
c3710de5 390 return status;
9ca8f72a
MF
391 }
392
cd33a5c1 393 status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params);
9ca8f72a 394 if (status != EFI_SUCCESS) {
8173ec79 395 efi_printk("Failed to allocate lowmem for boot params\n");
c3710de5 396 return status;
9ca8f72a
MF
397 }
398
399 memset(boot_params, 0x0, 0x4000);
400
401 hdr = &boot_params->hdr;
9ca8f72a 402 bi = &boot_params->apm_bios_info;
9ca8f72a
MF
403
404 /* Copy the second sector to boot_params */
405 memcpy(&hdr->jump, image->image_base + 512, 512);
406
407 /*
408 * Fill out some of the header fields ourselves because the
409 * EFI firmware loader doesn't load the first sector.
410 */
90a2186b
IM
411 hdr->root_flags = 1;
412 hdr->vid_mode = 0xffff;
413 hdr->boot_flag = 0xAA55;
9ca8f72a 414
291f3632
MF
415 hdr->type_of_loader = 0x21;
416
417 /* Convert unicode cmdline to ascii */
cd33a5c1 418 cmdline_ptr = efi_convert_cmdline(image, &options_size);
5fef3870
RF
419 if (!cmdline_ptr)
420 goto fail;
90a2186b 421
5fef3870 422 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
98b228f5
RF
423 /* Fill in upper bits of command line address, NOP on 32 bit */
424 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
291f3632
MF
425
426 hdr->ramdisk_image = 0;
427 hdr->ramdisk_size = 0;
428
291f3632
MF
429 /* Clear APM BIOS info */
430 memset(bi, 0, sizeof(*bi));
431
5a17dae4
MF
432 status = efi_parse_options(cmdline_ptr);
433 if (status != EFI_SUCCESS)
434 goto fail2;
435
cd33a5c1 436 status = handle_cmdline_files(image,
46f4582e 437 (char *)(unsigned long)hdr->cmd_line_ptr,
47226ad4 438 "initrd=", hdr->initrd_addr_max,
46f4582e 439 &ramdisk_addr, &ramdisk_size);
47226ad4
YL
440
441 if (status != EFI_SUCCESS &&
442 hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
8173ec79 443 efi_printk("Trying to load files to higher address\n");
cd33a5c1 444 status = handle_cmdline_files(image,
47226ad4
YL
445 (char *)(unsigned long)hdr->cmd_line_ptr,
446 "initrd=", -1UL,
447 &ramdisk_addr, &ramdisk_size);
448 }
449
9ca8f72a
MF
450 if (status != EFI_SUCCESS)
451 goto fail2;
4bf7111f
YL
452 hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
453 hdr->ramdisk_size = ramdisk_size & 0xffffffff;
454 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
455 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
9ca8f72a 456
c3710de5
AB
457 hdr->code32_start = (u32)(unsigned long)startup_32;
458
459 efi_stub_entry(handle, sys_table, boot_params);
460 /* not reached */
90a2186b 461
9ca8f72a 462fail2:
cd33a5c1 463 efi_free(options_size, hdr->cmd_line_ptr);
9ca8f72a 464fail:
cd33a5c1 465 efi_free(0x4000, (unsigned long)boot_params);
90a2186b 466
c3710de5 467 return status;
9ca8f72a
MF
468}
469
d2078d5a
LC
470static void add_e820ext(struct boot_params *params,
471 struct setup_data *e820ext, u32 nr_entries)
9ca8f72a 472{
d2078d5a 473 struct setup_data *data;
291f3632 474
d2078d5a 475 e820ext->type = SETUP_E820_EXT;
90a2186b 476 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
d2078d5a 477 e820ext->next = 0;
291f3632 478
d2078d5a 479 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
291f3632 480
d2078d5a
LC
481 while (data && data->next)
482 data = (struct setup_data *)(unsigned long)data->next;
d3768d88 483
d2078d5a
LC
484 if (data)
485 data->next = (unsigned long)e820ext;
486 else
487 params->hdr.setup_data = (unsigned long)e820ext;
488}
291f3632 489
90a2186b
IM
490static efi_status_t
491setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
d2078d5a 492{
7410aa1c 493 struct boot_e820_entry *entry = params->e820_table;
d2078d5a 494 struct efi_info *efi = &params->efi_info;
7410aa1c 495 struct boot_e820_entry *prev = NULL;
d2078d5a
LC
496 u32 nr_entries;
497 u32 nr_desc;
498 int i;
291f3632 499
291f3632 500 nr_entries = 0;
d2078d5a
LC
501 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
502
503 for (i = 0; i < nr_desc; i++) {
291f3632
MF
504 efi_memory_desc_t *d;
505 unsigned int e820_type = 0;
d2078d5a 506 unsigned long m = efi->efi_memmap;
291f3632 507
7cc03e48
DS
508#ifdef CONFIG_X86_64
509 m |= (u64)efi->efi_memmap_hi << 32;
510#endif
511
02e43c2d 512 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
291f3632
MF
513 switch (d->type) {
514 case EFI_RESERVED_TYPE:
515 case EFI_RUNTIME_SERVICES_CODE:
516 case EFI_RUNTIME_SERVICES_DATA:
517 case EFI_MEMORY_MAPPED_IO:
518 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
519 case EFI_PAL_CODE:
09821ff1 520 e820_type = E820_TYPE_RESERVED;
291f3632
MF
521 break;
522
523 case EFI_UNUSABLE_MEMORY:
09821ff1 524 e820_type = E820_TYPE_UNUSABLE;
291f3632
MF
525 break;
526
527 case EFI_ACPI_RECLAIM_MEMORY:
09821ff1 528 e820_type = E820_TYPE_ACPI;
291f3632
MF
529 break;
530
531 case EFI_LOADER_CODE:
532 case EFI_LOADER_DATA:
533 case EFI_BOOT_SERVICES_CODE:
534 case EFI_BOOT_SERVICES_DATA:
535 case EFI_CONVENTIONAL_MEMORY:
262b45ae
DW
536 if (efi_soft_reserve_enabled() &&
537 (d->attribute & EFI_MEMORY_SP))
538 e820_type = E820_TYPE_SOFT_RESERVED;
539 else
540 e820_type = E820_TYPE_RAM;
291f3632
MF
541 break;
542
543 case EFI_ACPI_MEMORY_NVS:
09821ff1 544 e820_type = E820_TYPE_NVS;
291f3632
MF
545 break;
546
ad5fb870 547 case EFI_PERSISTENT_MEMORY:
09821ff1 548 e820_type = E820_TYPE_PMEM;
ad5fb870
DW
549 break;
550
291f3632
MF
551 default:
552 continue;
553 }
554
555 /* Merge adjacent mappings */
556 if (prev && prev->type == e820_type &&
d2078d5a 557 (prev->addr + prev->size) == d->phys_addr) {
291f3632 558 prev->size += d->num_pages << 12;
d2078d5a 559 continue;
291f3632 560 }
d2078d5a 561
61a50101 562 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
8ec67d97 563 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
d2078d5a
LC
564 sizeof(struct setup_data);
565
566 if (!e820ext || e820ext_size < need)
567 return EFI_BUFFER_TOO_SMALL;
568
569 /* boot_params map full, switch to e820 extended */
7410aa1c 570 entry = (struct boot_e820_entry *)e820ext->data;
d2078d5a
LC
571 }
572
7410aa1c
IM
573 entry->addr = d->phys_addr;
574 entry->size = d->num_pages << PAGE_SHIFT;
575 entry->type = e820_type;
576 prev = entry++;
d2078d5a 577 nr_entries++;
291f3632
MF
578 }
579
61a50101
IM
580 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
581 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
d2078d5a
LC
582
583 add_e820ext(params, e820ext, nr_e820ext);
584 nr_entries -= nr_e820ext;
585 }
586
587 params->e820_entries = (u8)nr_entries;
588
589 return EFI_SUCCESS;
590}
591
592static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
593 u32 *e820ext_size)
594{
595 efi_status_t status;
596 unsigned long size;
597
598 size = sizeof(struct setup_data) +
8ec67d97 599 sizeof(struct e820_entry) * nr_desc;
d2078d5a
LC
600
601 if (*e820ext) {
204b0a1a 602 efi_call_early(free_pool, *e820ext);
d2078d5a
LC
603 *e820ext = NULL;
604 *e820ext_size = 0;
605 }
606
204b0a1a
MF
607 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
608 size, (void **)e820ext);
d2078d5a
LC
609 if (status == EFI_SUCCESS)
610 *e820ext_size = size;
611
612 return status;
613}
614
b84a64fa
ES
615static efi_status_t allocate_e820(struct boot_params *params,
616 struct setup_data **e820ext,
617 u32 *e820ext_size)
618{
619 unsigned long map_size, desc_size, buff_size;
620 struct efi_boot_memmap boot_map;
621 efi_memory_desc_t *map;
622 efi_status_t status;
623 __u32 nr_desc;
624
625 boot_map.map = &map;
626 boot_map.map_size = &map_size;
627 boot_map.desc_size = &desc_size;
628 boot_map.desc_ver = NULL;
629 boot_map.key_ptr = NULL;
630 boot_map.buff_size = &buff_size;
631
cd33a5c1 632 status = efi_get_memory_map(&boot_map);
b84a64fa
ES
633 if (status != EFI_SUCCESS)
634 return status;
635
636 nr_desc = buff_size / desc_size;
637
638 if (nr_desc > ARRAY_SIZE(params->e820_table)) {
639 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
640
641 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
642 if (status != EFI_SUCCESS)
643 return status;
644 }
645
646 return EFI_SUCCESS;
647}
648
d6493401 649struct exit_boot_struct {
90a2186b
IM
650 struct boot_params *boot_params;
651 struct efi_info *efi;
d6493401
JH
652};
653
cd33a5c1 654static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
d6493401
JH
655 void *priv)
656{
d6493401 657 const char *signature;
d6493401
JH
658 struct exit_boot_struct *p = priv;
659
aab9593c
AB
660 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
661 : EFI32_LOADER_SIGNATURE;
d6493401
JH
662 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
663
cd33a5c1 664 p->efi->efi_systab = (unsigned long)efi_system_table();
90a2186b
IM
665 p->efi->efi_memdesc_size = *map->desc_size;
666 p->efi->efi_memdesc_version = *map->desc_ver;
667 p->efi->efi_memmap = (unsigned long)*map->map;
668 p->efi->efi_memmap_size = *map->map_size;
d6493401
JH
669
670#ifdef CONFIG_X86_64
cd33a5c1 671 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32;
90a2186b 672 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
d6493401
JH
673#endif
674
675 return EFI_SUCCESS;
676}
677
aab9593c 678static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
d2078d5a 679{
dadb57ab 680 unsigned long map_sz, key, desc_size, buff_size;
d2078d5a 681 efi_memory_desc_t *mem_map;
b84a64fa
ES
682 struct setup_data *e820ext = NULL;
683 __u32 e820ext_size = 0;
d2078d5a
LC
684 efi_status_t status;
685 __u32 desc_version;
dadb57ab 686 struct efi_boot_memmap map;
d6493401
JH
687 struct exit_boot_struct priv;
688
90a2186b
IM
689 map.map = &mem_map;
690 map.map_size = &map_sz;
691 map.desc_size = &desc_size;
692 map.desc_ver = &desc_version;
693 map.key_ptr = &key;
694 map.buff_size = &buff_size;
695 priv.boot_params = boot_params;
696 priv.efi = &boot_params->efi_info;
b84a64fa
ES
697
698 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
699 if (status != EFI_SUCCESS)
700 return status;
dadb57ab 701
d6493401 702 /* Might as well exit boot services now */
cd33a5c1 703 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
d2078d5a
LC
704 if (status != EFI_SUCCESS)
705 return status;
706
d2078d5a 707 /* Historic? */
90a2186b 708 boot_params->alt_mem_k = 32 * 1024;
d2078d5a
LC
709
710 status = setup_e820(boot_params, e820ext, e820ext_size);
711 if (status != EFI_SUCCESS)
712 return status;
291f3632
MF
713
714 return EFI_SUCCESS;
291f3632
MF
715}
716
9ca8f72a
MF
717/*
718 * On success we return a pointer to a boot_params structure, and NULL
719 * on failure.
720 */
c3710de5
AB
721struct boot_params *efi_main(efi_handle_t handle,
722 efi_system_table_t *sys_table_arg,
723 struct boot_params *boot_params,
724 bool is64)
9ca8f72a 725{
54b52d87 726 struct desc_ptr *gdt = NULL;
9ca8f72a
MF
727 struct setup_header *hdr = &boot_params->hdr;
728 efi_status_t status;
729 struct desc_struct *desc;
c33ce984 730 unsigned long cmdline_paddr;
54b52d87 731
c3710de5 732 sys_table = sys_table_arg;
9ca8f72a 733
c3710de5
AB
734 if (IS_ENABLED(CONFIG_EFI_MIXED))
735 efi_is64 = is64;
9ca8f72a
MF
736
737 /* Check if we were booted by the EFI firmware */
738 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
739 goto fail;
740
c33ce984
HG
741 /*
742 * make_boot_params() may have been called before efi_main(), in which
743 * case this is the second time we parse the cmdline. This is ok,
744 * parsing the cmdline multiple times does not have side-effects.
745 */
746 cmdline_paddr = ((u64)hdr->cmd_line_ptr |
747 ((u64)boot_params->ext_cmd_line_ptr << 32));
748 efi_parse_options((char *)cmdline_paddr);
749
de8cb458
DH
750 /*
751 * If the boot loader gave us a value for secure_boot then we use that,
752 * otherwise we ask the BIOS.
753 */
754 if (boot_params->secure_boot == efi_secureboot_mode_unset)
cd33a5c1 755 boot_params->secure_boot = efi_get_secureboot();
ccc829ba
MG
756
757 /* Ask the firmware to clear memory on unclean shutdown */
cd33a5c1 758 efi_enable_reset_attack_mitigation();
0d959814 759
cd33a5c1 760 efi_random_get_seed();
0d959814 761
cd33a5c1 762 efi_retrieve_tpm2_eventlog();
de8cb458 763
9ca8f72a 764 setup_graphics(boot_params);
291f3632 765
56394ab8 766 setup_efi_pci(boot_params);
dd5fc854 767
58c5475a
LW
768 setup_quirks(boot_params);
769
204b0a1a
MF
770 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
771 sizeof(*gdt), (void **)&gdt);
9fa7deda 772 if (status != EFI_SUCCESS) {
8173ec79 773 efi_printk("Failed to allocate memory for 'gdt' structure\n");
291f3632 774 goto fail;
9fa7deda 775 }
291f3632
MF
776
777 gdt->size = 0x800;
cd33a5c1 778 status = efi_low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
9fa7deda 779 if (status != EFI_SUCCESS) {
8173ec79 780 efi_printk("Failed to allocate memory for 'gdt'\n");
291f3632 781 goto fail;
9fa7deda 782 }
291f3632 783
9ca8f72a
MF
784 /*
785 * If the kernel isn't already loaded at the preferred load
786 * address, relocate it.
787 */
788 if (hdr->pref_address != hdr->code32_start) {
4a9f3a7c 789 unsigned long bzimage_addr = hdr->code32_start;
cd33a5c1 790 status = efi_relocate_kernel(&bzimage_addr,
4a9f3a7c
RF
791 hdr->init_size, hdr->init_size,
792 hdr->pref_address,
220dd769
KS
793 hdr->kernel_alignment,
794 LOAD_PHYSICAL_ADDR);
fb86b244 795 if (status != EFI_SUCCESS) {
8173ec79 796 efi_printk("efi_relocate_kernel() failed!\n");
9ca8f72a 797 goto fail;
fb86b244 798 }
4a9f3a7c
RF
799
800 hdr->pref_address = hdr->code32_start;
801 hdr->code32_start = bzimage_addr;
9ca8f72a
MF
802 }
803
aab9593c 804 status = exit_boot(boot_params, handle);
fb86b244 805 if (status != EFI_SUCCESS) {
8173ec79 806 efi_printk("exit_boot() failed!\n");
291f3632 807 goto fail;
fb86b244 808 }
291f3632
MF
809
810 memset((char *)gdt->address, 0x0, gdt->size);
811 desc = (struct desc_struct *)gdt->address;
812
919a02d1
KS
813 /* The first GDT is a dummy. */
814 desc++;
815
816 if (IS_ENABLED(CONFIG_X86_64)) {
817 /* __KERNEL32_CS */
90a2186b
IM
818 desc->limit0 = 0xffff;
819 desc->base0 = 0x0000;
820 desc->base1 = 0x0000;
821 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
822 desc->s = DESC_TYPE_CODE_DATA;
823 desc->dpl = 0;
824 desc->p = 1;
825 desc->limit1 = 0xf;
826 desc->avl = 0;
827 desc->l = 0;
828 desc->d = SEG_OP_SIZE_32BIT;
829 desc->g = SEG_GRANULARITY_4KB;
830 desc->base2 = 0x00;
831
919a02d1
KS
832 desc++;
833 } else {
834 /* Second entry is unused on 32-bit */
835 desc++;
836 }
291f3632 837
f8fceacb 838 /* __KERNEL_CS */
90a2186b
IM
839 desc->limit0 = 0xffff;
840 desc->base0 = 0x0000;
841 desc->base1 = 0x0000;
842 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
843 desc->s = DESC_TYPE_CODE_DATA;
844 desc->dpl = 0;
845 desc->p = 1;
846 desc->limit1 = 0xf;
847 desc->avl = 0;
848
4c94117c
KS
849 if (IS_ENABLED(CONFIG_X86_64)) {
850 desc->l = 1;
851 desc->d = 0;
852 } else {
853 desc->l = 0;
854 desc->d = SEG_OP_SIZE_32BIT;
855 }
90a2186b
IM
856 desc->g = SEG_GRANULARITY_4KB;
857 desc->base2 = 0x00;
291f3632 858 desc++;
f8fceacb
KS
859
860 /* __KERNEL_DS */
90a2186b
IM
861 desc->limit0 = 0xffff;
862 desc->base0 = 0x0000;
863 desc->base1 = 0x0000;
864 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
865 desc->s = DESC_TYPE_CODE_DATA;
866 desc->dpl = 0;
867 desc->p = 1;
868 desc->limit1 = 0xf;
869 desc->avl = 0;
870 desc->l = 0;
871 desc->d = SEG_OP_SIZE_32BIT;
872 desc->g = SEG_GRANULARITY_4KB;
873 desc->base2 = 0x00;
291f3632 874 desc++;
f8fceacb
KS
875
876 if (IS_ENABLED(CONFIG_X86_64)) {
877 /* Task segment value */
90a2186b
IM
878 desc->limit0 = 0x0000;
879 desc->base0 = 0x0000;
880 desc->base1 = 0x0000;
881 desc->type = SEG_TYPE_TSS;
882 desc->s = 0;
883 desc->dpl = 0;
884 desc->p = 1;
885 desc->limit1 = 0x0;
886 desc->avl = 0;
887 desc->l = 0;
888 desc->d = 0;
889 desc->g = SEG_GRANULARITY_4KB;
890 desc->base2 = 0x00;
f8fceacb
KS
891 desc++;
892 }
291f3632 893
291f3632 894 asm volatile("cli");
0ce6cda2 895 asm volatile ("lgdt %0" : : "m" (*gdt));
291f3632
MF
896
897 return boot_params;
898fail:
8173ec79 899 efi_printk("efi_main() failed!\n");
90a2186b 900
c3710de5
AB
901 for (;;)
902 asm("hlt");
291f3632 903}