1c2d0b470SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-only 2c2d0b470SArd Biesheuvel 3c2d0b470SArd Biesheuvel /* ----------------------------------------------------------------------- 4c2d0b470SArd Biesheuvel * 5c2d0b470SArd Biesheuvel * Copyright 2011 Intel Corporation; author Matt Fleming 6c2d0b470SArd Biesheuvel * 7c2d0b470SArd Biesheuvel * ----------------------------------------------------------------------- */ 8c2d0b470SArd Biesheuvel 9c2d0b470SArd Biesheuvel #include <linux/efi.h> 10c2d0b470SArd Biesheuvel #include <linux/pci.h> 11c2d0b470SArd Biesheuvel 12c2d0b470SArd Biesheuvel #include <asm/efi.h> 13c2d0b470SArd Biesheuvel #include <asm/e820/types.h> 14c2d0b470SArd Biesheuvel #include <asm/setup.h> 15c2d0b470SArd Biesheuvel #include <asm/desc.h> 16c2d0b470SArd Biesheuvel #include <asm/boot.h> 17c2d0b470SArd Biesheuvel 18c2d0b470SArd Biesheuvel #include "efistub.h" 19c2d0b470SArd Biesheuvel 20c2d0b470SArd Biesheuvel static efi_system_table_t *sys_table; 21c2d0b470SArd Biesheuvel extern const bool efi_is64; 22c2d0b470SArd Biesheuvel 23c2d0b470SArd Biesheuvel __pure efi_system_table_t *efi_system_table(void) 24c2d0b470SArd Biesheuvel { 25c2d0b470SArd Biesheuvel return sys_table; 26c2d0b470SArd Biesheuvel } 27c2d0b470SArd Biesheuvel 28c2d0b470SArd Biesheuvel __attribute_const__ bool efi_is_64bit(void) 29c2d0b470SArd Biesheuvel { 30c2d0b470SArd Biesheuvel if (IS_ENABLED(CONFIG_EFI_MIXED)) 31c2d0b470SArd Biesheuvel return efi_is64; 32c2d0b470SArd Biesheuvel return IS_ENABLED(CONFIG_X86_64); 33c2d0b470SArd Biesheuvel } 34c2d0b470SArd Biesheuvel 35c2d0b470SArd Biesheuvel static efi_status_t 36c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) 37c2d0b470SArd Biesheuvel { 38c2d0b470SArd Biesheuvel struct pci_setup_rom *rom = NULL; 39c2d0b470SArd Biesheuvel efi_status_t status; 40c2d0b470SArd Biesheuvel unsigned long size; 41c2d0b470SArd Biesheuvel uint64_t romsize; 42c2d0b470SArd Biesheuvel void *romimage; 43c2d0b470SArd Biesheuvel 44c2d0b470SArd Biesheuvel /* 45c2d0b470SArd Biesheuvel * Some firmware images contain EFI function pointers at the place where 46c2d0b470SArd Biesheuvel * the romimage and romsize fields are supposed to be. Typically the EFI 47c2d0b470SArd Biesheuvel * code is mapped at high addresses, translating to an unrealistically 48c2d0b470SArd Biesheuvel * large romsize. The UEFI spec limits the size of option ROMs to 16 49c2d0b470SArd Biesheuvel * MiB so we reject any ROMs over 16 MiB in size to catch this. 50c2d0b470SArd Biesheuvel */ 51c2d0b470SArd Biesheuvel romimage = efi_table_attr(pci, romimage); 52c2d0b470SArd Biesheuvel romsize = efi_table_attr(pci, romsize); 53c2d0b470SArd Biesheuvel if (!romimage || !romsize || romsize > SZ_16M) 54c2d0b470SArd Biesheuvel return EFI_INVALID_PARAMETER; 55c2d0b470SArd Biesheuvel 56c2d0b470SArd Biesheuvel size = romsize + sizeof(*rom); 57c2d0b470SArd Biesheuvel 58c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 59c2d0b470SArd Biesheuvel (void **)&rom); 60c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 61c2d0b470SArd Biesheuvel efi_printk("Failed to allocate memory for 'rom'\n"); 62c2d0b470SArd Biesheuvel return status; 63c2d0b470SArd Biesheuvel } 64c2d0b470SArd Biesheuvel 65c2d0b470SArd Biesheuvel memset(rom, 0, sizeof(*rom)); 66c2d0b470SArd Biesheuvel 67c2d0b470SArd Biesheuvel rom->data.type = SETUP_PCI; 68c2d0b470SArd Biesheuvel rom->data.len = size - sizeof(struct setup_data); 69c2d0b470SArd Biesheuvel rom->data.next = 0; 70c2d0b470SArd Biesheuvel rom->pcilen = pci->romsize; 71c2d0b470SArd Biesheuvel *__rom = rom; 72c2d0b470SArd Biesheuvel 73c2d0b470SArd Biesheuvel status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 74c2d0b470SArd Biesheuvel PCI_VENDOR_ID, 1, &rom->vendor); 75c2d0b470SArd Biesheuvel 76c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 77c2d0b470SArd Biesheuvel efi_printk("Failed to read rom->vendor\n"); 78c2d0b470SArd Biesheuvel goto free_struct; 79c2d0b470SArd Biesheuvel } 80c2d0b470SArd Biesheuvel 81c2d0b470SArd Biesheuvel status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 82c2d0b470SArd Biesheuvel PCI_DEVICE_ID, 1, &rom->devid); 83c2d0b470SArd Biesheuvel 84c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 85c2d0b470SArd Biesheuvel efi_printk("Failed to read rom->devid\n"); 86c2d0b470SArd Biesheuvel goto free_struct; 87c2d0b470SArd Biesheuvel } 88c2d0b470SArd Biesheuvel 89c2d0b470SArd Biesheuvel status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, 90c2d0b470SArd Biesheuvel &rom->device, &rom->function); 91c2d0b470SArd Biesheuvel 92c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 93c2d0b470SArd Biesheuvel goto free_struct; 94c2d0b470SArd Biesheuvel 95c2d0b470SArd Biesheuvel memcpy(rom->romdata, romimage, romsize); 96c2d0b470SArd Biesheuvel return status; 97c2d0b470SArd Biesheuvel 98c2d0b470SArd Biesheuvel free_struct: 99c2d0b470SArd Biesheuvel efi_bs_call(free_pool, rom); 100c2d0b470SArd Biesheuvel return status; 101c2d0b470SArd Biesheuvel } 102c2d0b470SArd Biesheuvel 103c2d0b470SArd Biesheuvel /* 104c2d0b470SArd Biesheuvel * There's no way to return an informative status from this function, 105c2d0b470SArd Biesheuvel * because any analysis (and printing of error messages) needs to be 106c2d0b470SArd Biesheuvel * done directly at the EFI function call-site. 107c2d0b470SArd Biesheuvel * 108c2d0b470SArd Biesheuvel * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we 109c2d0b470SArd Biesheuvel * just didn't find any PCI devices, but there's no way to tell outside 110c2d0b470SArd Biesheuvel * the context of the call. 111c2d0b470SArd Biesheuvel */ 112c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params) 113c2d0b470SArd Biesheuvel { 114c2d0b470SArd Biesheuvel efi_status_t status; 115c2d0b470SArd Biesheuvel void **pci_handle = NULL; 116c2d0b470SArd Biesheuvel efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; 117c2d0b470SArd Biesheuvel unsigned long size = 0; 118c2d0b470SArd Biesheuvel struct setup_data *data; 119c2d0b470SArd Biesheuvel efi_handle_t h; 120c2d0b470SArd Biesheuvel int i; 121c2d0b470SArd Biesheuvel 122c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 123c2d0b470SArd Biesheuvel &pci_proto, NULL, &size, pci_handle); 124c2d0b470SArd Biesheuvel 125c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) { 126c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 127c2d0b470SArd Biesheuvel (void **)&pci_handle); 128c2d0b470SArd Biesheuvel 129c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 130c2d0b470SArd Biesheuvel efi_printk("Failed to allocate memory for 'pci_handle'\n"); 131c2d0b470SArd Biesheuvel return; 132c2d0b470SArd Biesheuvel } 133c2d0b470SArd Biesheuvel 134c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 135c2d0b470SArd Biesheuvel &pci_proto, NULL, &size, pci_handle); 136c2d0b470SArd Biesheuvel } 137c2d0b470SArd Biesheuvel 138c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 139c2d0b470SArd Biesheuvel goto free_handle; 140c2d0b470SArd Biesheuvel 141c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 142c2d0b470SArd Biesheuvel 143c2d0b470SArd Biesheuvel while (data && data->next) 144c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 145c2d0b470SArd Biesheuvel 146c2d0b470SArd Biesheuvel for_each_efi_handle(h, pci_handle, size, i) { 147c2d0b470SArd Biesheuvel efi_pci_io_protocol_t *pci = NULL; 148c2d0b470SArd Biesheuvel struct pci_setup_rom *rom; 149c2d0b470SArd Biesheuvel 150c2d0b470SArd Biesheuvel status = efi_bs_call(handle_protocol, h, &pci_proto, 151c2d0b470SArd Biesheuvel (void **)&pci); 152c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS || !pci) 153c2d0b470SArd Biesheuvel continue; 154c2d0b470SArd Biesheuvel 155c2d0b470SArd Biesheuvel status = preserve_pci_rom_image(pci, &rom); 156c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 157c2d0b470SArd Biesheuvel continue; 158c2d0b470SArd Biesheuvel 159c2d0b470SArd Biesheuvel if (data) 160c2d0b470SArd Biesheuvel data->next = (unsigned long)rom; 161c2d0b470SArd Biesheuvel else 162c2d0b470SArd Biesheuvel params->hdr.setup_data = (unsigned long)rom; 163c2d0b470SArd Biesheuvel 164c2d0b470SArd Biesheuvel data = (struct setup_data *)rom; 165c2d0b470SArd Biesheuvel } 166c2d0b470SArd Biesheuvel 167c2d0b470SArd Biesheuvel free_handle: 168c2d0b470SArd Biesheuvel efi_bs_call(free_pool, pci_handle); 169c2d0b470SArd Biesheuvel } 170c2d0b470SArd Biesheuvel 171c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params) 172c2d0b470SArd Biesheuvel { 173c2d0b470SArd Biesheuvel efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; 174c2d0b470SArd Biesheuvel struct setup_data *data, *new; 175c2d0b470SArd Biesheuvel efi_status_t status; 176c2d0b470SArd Biesheuvel u32 size = 0; 177c2d0b470SArd Biesheuvel apple_properties_protocol_t *p; 178c2d0b470SArd Biesheuvel 179c2d0b470SArd Biesheuvel status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); 180c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 181c2d0b470SArd Biesheuvel return; 182c2d0b470SArd Biesheuvel 183c2d0b470SArd Biesheuvel if (efi_table_attr(p, version) != 0x10000) { 184c2d0b470SArd Biesheuvel efi_printk("Unsupported properties proto version\n"); 185c2d0b470SArd Biesheuvel return; 186c2d0b470SArd Biesheuvel } 187c2d0b470SArd Biesheuvel 188c2d0b470SArd Biesheuvel efi_call_proto(p, get_all, NULL, &size); 189c2d0b470SArd Biesheuvel if (!size) 190c2d0b470SArd Biesheuvel return; 191c2d0b470SArd Biesheuvel 192c2d0b470SArd Biesheuvel do { 193c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, 194c2d0b470SArd Biesheuvel size + sizeof(struct setup_data), 195c2d0b470SArd Biesheuvel (void **)&new); 196c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 197c2d0b470SArd Biesheuvel efi_printk("Failed to allocate memory for 'properties'\n"); 198c2d0b470SArd Biesheuvel return; 199c2d0b470SArd Biesheuvel } 200c2d0b470SArd Biesheuvel 201c2d0b470SArd Biesheuvel status = efi_call_proto(p, get_all, new->data, &size); 202c2d0b470SArd Biesheuvel 203c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 204c2d0b470SArd Biesheuvel efi_bs_call(free_pool, new); 205c2d0b470SArd Biesheuvel } while (status == EFI_BUFFER_TOO_SMALL); 206c2d0b470SArd Biesheuvel 207c2d0b470SArd Biesheuvel new->type = SETUP_APPLE_PROPERTIES; 208c2d0b470SArd Biesheuvel new->len = size; 209c2d0b470SArd Biesheuvel new->next = 0; 210c2d0b470SArd Biesheuvel 211c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; 212c2d0b470SArd Biesheuvel if (!data) { 213c2d0b470SArd Biesheuvel boot_params->hdr.setup_data = (unsigned long)new; 214c2d0b470SArd Biesheuvel } else { 215c2d0b470SArd Biesheuvel while (data->next) 216c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 217c2d0b470SArd Biesheuvel data->next = (unsigned long)new; 218c2d0b470SArd Biesheuvel } 219c2d0b470SArd Biesheuvel } 220c2d0b470SArd Biesheuvel 221c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple"; 222c2d0b470SArd Biesheuvel 223c2d0b470SArd Biesheuvel static void setup_quirks(struct boot_params *boot_params) 224c2d0b470SArd Biesheuvel { 225c2d0b470SArd Biesheuvel efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) 226c2d0b470SArd Biesheuvel efi_table_attr(efi_system_table(), fw_vendor); 227c2d0b470SArd Biesheuvel 228c2d0b470SArd Biesheuvel if (!memcmp(fw_vendor, apple, sizeof(apple))) { 229c2d0b470SArd Biesheuvel if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) 230c2d0b470SArd Biesheuvel retrieve_apple_device_properties(boot_params); 231c2d0b470SArd Biesheuvel } 232c2d0b470SArd Biesheuvel } 233c2d0b470SArd Biesheuvel 234c2d0b470SArd Biesheuvel /* 235c2d0b470SArd Biesheuvel * See if we have Universal Graphics Adapter (UGA) protocol 236c2d0b470SArd Biesheuvel */ 237c2d0b470SArd Biesheuvel static efi_status_t 238c2d0b470SArd Biesheuvel setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) 239c2d0b470SArd Biesheuvel { 240c2d0b470SArd Biesheuvel efi_status_t status; 241c2d0b470SArd Biesheuvel u32 width, height; 242c2d0b470SArd Biesheuvel void **uga_handle = NULL; 243c2d0b470SArd Biesheuvel efi_uga_draw_protocol_t *uga = NULL, *first_uga; 244c2d0b470SArd Biesheuvel efi_handle_t handle; 245c2d0b470SArd Biesheuvel int i; 246c2d0b470SArd Biesheuvel 247c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 248c2d0b470SArd Biesheuvel (void **)&uga_handle); 249c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 250c2d0b470SArd Biesheuvel return status; 251c2d0b470SArd Biesheuvel 252c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 253c2d0b470SArd Biesheuvel uga_proto, NULL, &size, uga_handle); 254c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 255c2d0b470SArd Biesheuvel goto free_handle; 256c2d0b470SArd Biesheuvel 257c2d0b470SArd Biesheuvel height = 0; 258c2d0b470SArd Biesheuvel width = 0; 259c2d0b470SArd Biesheuvel 260c2d0b470SArd Biesheuvel first_uga = NULL; 261c2d0b470SArd Biesheuvel for_each_efi_handle(handle, uga_handle, size, i) { 262c2d0b470SArd Biesheuvel efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 263c2d0b470SArd Biesheuvel u32 w, h, depth, refresh; 264c2d0b470SArd Biesheuvel void *pciio; 265c2d0b470SArd Biesheuvel 266c2d0b470SArd Biesheuvel status = efi_bs_call(handle_protocol, handle, uga_proto, 267c2d0b470SArd Biesheuvel (void **)&uga); 268c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 269c2d0b470SArd Biesheuvel continue; 270c2d0b470SArd Biesheuvel 271c2d0b470SArd Biesheuvel pciio = NULL; 272c2d0b470SArd Biesheuvel efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); 273c2d0b470SArd Biesheuvel 274c2d0b470SArd Biesheuvel status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); 275c2d0b470SArd Biesheuvel if (status == EFI_SUCCESS && (!first_uga || pciio)) { 276c2d0b470SArd Biesheuvel width = w; 277c2d0b470SArd Biesheuvel height = h; 278c2d0b470SArd Biesheuvel 279c2d0b470SArd Biesheuvel /* 280c2d0b470SArd Biesheuvel * Once we've found a UGA supporting PCIIO, 281c2d0b470SArd Biesheuvel * don't bother looking any further. 282c2d0b470SArd Biesheuvel */ 283c2d0b470SArd Biesheuvel if (pciio) 284c2d0b470SArd Biesheuvel break; 285c2d0b470SArd Biesheuvel 286c2d0b470SArd Biesheuvel first_uga = uga; 287c2d0b470SArd Biesheuvel } 288c2d0b470SArd Biesheuvel } 289c2d0b470SArd Biesheuvel 290c2d0b470SArd Biesheuvel if (!width && !height) 291c2d0b470SArd Biesheuvel goto free_handle; 292c2d0b470SArd Biesheuvel 293c2d0b470SArd Biesheuvel /* EFI framebuffer */ 294c2d0b470SArd Biesheuvel si->orig_video_isVGA = VIDEO_TYPE_EFI; 295c2d0b470SArd Biesheuvel 296c2d0b470SArd Biesheuvel si->lfb_depth = 32; 297c2d0b470SArd Biesheuvel si->lfb_width = width; 298c2d0b470SArd Biesheuvel si->lfb_height = height; 299c2d0b470SArd Biesheuvel 300c2d0b470SArd Biesheuvel si->red_size = 8; 301c2d0b470SArd Biesheuvel si->red_pos = 16; 302c2d0b470SArd Biesheuvel si->green_size = 8; 303c2d0b470SArd Biesheuvel si->green_pos = 8; 304c2d0b470SArd Biesheuvel si->blue_size = 8; 305c2d0b470SArd Biesheuvel si->blue_pos = 0; 306c2d0b470SArd Biesheuvel si->rsvd_size = 8; 307c2d0b470SArd Biesheuvel si->rsvd_pos = 24; 308c2d0b470SArd Biesheuvel 309c2d0b470SArd Biesheuvel free_handle: 310c2d0b470SArd Biesheuvel efi_bs_call(free_pool, uga_handle); 311c2d0b470SArd Biesheuvel 312c2d0b470SArd Biesheuvel return status; 313c2d0b470SArd Biesheuvel } 314c2d0b470SArd Biesheuvel 315c2d0b470SArd Biesheuvel static void setup_graphics(struct boot_params *boot_params) 316c2d0b470SArd Biesheuvel { 317c2d0b470SArd Biesheuvel efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 318c2d0b470SArd Biesheuvel struct screen_info *si; 319c2d0b470SArd Biesheuvel efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; 320c2d0b470SArd Biesheuvel efi_status_t status; 321c2d0b470SArd Biesheuvel unsigned long size; 322c2d0b470SArd Biesheuvel void **gop_handle = NULL; 323c2d0b470SArd Biesheuvel void **uga_handle = NULL; 324c2d0b470SArd Biesheuvel 325c2d0b470SArd Biesheuvel si = &boot_params->screen_info; 326c2d0b470SArd Biesheuvel memset(si, 0, sizeof(*si)); 327c2d0b470SArd Biesheuvel 328c2d0b470SArd Biesheuvel size = 0; 329c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 330c2d0b470SArd Biesheuvel &graphics_proto, NULL, &size, gop_handle); 331c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 332c2d0b470SArd Biesheuvel status = efi_setup_gop(si, &graphics_proto, size); 333c2d0b470SArd Biesheuvel 334c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 335c2d0b470SArd Biesheuvel size = 0; 336c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 337c2d0b470SArd Biesheuvel &uga_proto, NULL, &size, uga_handle); 338c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 339c2d0b470SArd Biesheuvel setup_uga(si, &uga_proto, size); 340c2d0b470SArd Biesheuvel } 341c2d0b470SArd Biesheuvel } 342c2d0b470SArd Biesheuvel 343c2d0b470SArd Biesheuvel void startup_32(struct boot_params *boot_params); 344c2d0b470SArd Biesheuvel 345c2d0b470SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle, 346c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg, 347c2d0b470SArd Biesheuvel struct boot_params *boot_params); 348c2d0b470SArd Biesheuvel 349c2d0b470SArd Biesheuvel /* 350c2d0b470SArd Biesheuvel * Because the x86 boot code expects to be passed a boot_params we 351c2d0b470SArd Biesheuvel * need to create one ourselves (usually the bootloader would create 352c2d0b470SArd Biesheuvel * one for us). 353c2d0b470SArd Biesheuvel */ 354c2d0b470SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 355c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg) 356c2d0b470SArd Biesheuvel { 357c2d0b470SArd Biesheuvel struct boot_params *boot_params; 358c2d0b470SArd Biesheuvel struct setup_header *hdr; 359c2d0b470SArd Biesheuvel efi_loaded_image_t *image; 360c2d0b470SArd Biesheuvel efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 361c2d0b470SArd Biesheuvel int options_size = 0; 362c2d0b470SArd Biesheuvel efi_status_t status; 363c2d0b470SArd Biesheuvel char *cmdline_ptr; 364c2d0b470SArd Biesheuvel unsigned long ramdisk_addr; 365c2d0b470SArd Biesheuvel unsigned long ramdisk_size; 3666a4db9bfSArd Biesheuvel bool above4g; 367c2d0b470SArd Biesheuvel 368c2d0b470SArd Biesheuvel sys_table = sys_table_arg; 369c2d0b470SArd Biesheuvel 370c2d0b470SArd Biesheuvel /* Check if we were booted by the EFI firmware */ 371c2d0b470SArd Biesheuvel if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 372c2d0b470SArd Biesheuvel return EFI_INVALID_PARAMETER; 373c2d0b470SArd Biesheuvel 374c2d0b470SArd Biesheuvel status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image); 375c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 376c2d0b470SArd Biesheuvel efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 377c2d0b470SArd Biesheuvel return status; 378c2d0b470SArd Biesheuvel } 379c2d0b470SArd Biesheuvel 3806a4db9bfSArd Biesheuvel hdr = &((struct boot_params *)image->image_base)->hdr; 3816a4db9bfSArd Biesheuvel above4g = hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G; 3826a4db9bfSArd Biesheuvel 3836a4db9bfSArd Biesheuvel status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params, 3846a4db9bfSArd Biesheuvel above4g ? ULONG_MAX : UINT_MAX); 385c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 386c2d0b470SArd Biesheuvel efi_printk("Failed to allocate lowmem for boot params\n"); 387c2d0b470SArd Biesheuvel return status; 388c2d0b470SArd Biesheuvel } 389c2d0b470SArd Biesheuvel 390c2d0b470SArd Biesheuvel memset(boot_params, 0x0, 0x4000); 391c2d0b470SArd Biesheuvel 392c2d0b470SArd Biesheuvel hdr = &boot_params->hdr; 393c2d0b470SArd Biesheuvel 394c2d0b470SArd Biesheuvel /* Copy the second sector to boot_params */ 395c2d0b470SArd Biesheuvel memcpy(&hdr->jump, image->image_base + 512, 512); 396c2d0b470SArd Biesheuvel 397c2d0b470SArd Biesheuvel /* 398c2d0b470SArd Biesheuvel * Fill out some of the header fields ourselves because the 399c2d0b470SArd Biesheuvel * EFI firmware loader doesn't load the first sector. 400c2d0b470SArd Biesheuvel */ 401c2d0b470SArd Biesheuvel hdr->root_flags = 1; 402c2d0b470SArd Biesheuvel hdr->vid_mode = 0xffff; 403c2d0b470SArd Biesheuvel hdr->boot_flag = 0xAA55; 404c2d0b470SArd Biesheuvel 405c2d0b470SArd Biesheuvel hdr->type_of_loader = 0x21; 406c2d0b470SArd Biesheuvel 407c2d0b470SArd Biesheuvel /* Convert unicode cmdline to ascii */ 4081e45bf73SArd Biesheuvel cmdline_ptr = efi_convert_cmdline(image, &options_size, 4091e45bf73SArd Biesheuvel above4g ? ULONG_MAX : UINT_MAX); 410c2d0b470SArd Biesheuvel if (!cmdline_ptr) 411c2d0b470SArd Biesheuvel goto fail; 412c2d0b470SArd Biesheuvel 413c2d0b470SArd Biesheuvel hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; 414c2d0b470SArd Biesheuvel /* Fill in upper bits of command line address, NOP on 32 bit */ 415c2d0b470SArd Biesheuvel boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; 416c2d0b470SArd Biesheuvel 417c2d0b470SArd Biesheuvel hdr->ramdisk_image = 0; 418c2d0b470SArd Biesheuvel hdr->ramdisk_size = 0; 419c2d0b470SArd Biesheuvel 420c2d0b470SArd Biesheuvel status = efi_parse_options(cmdline_ptr); 421c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 422c2d0b470SArd Biesheuvel goto fail2; 423c2d0b470SArd Biesheuvel 4249302c1bbSArd Biesheuvel status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size, 42531f5e546SArd Biesheuvel hdr->initrd_addr_max, 42631f5e546SArd Biesheuvel above4g ? ULONG_MAX : hdr->initrd_addr_max); 427c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 428c2d0b470SArd Biesheuvel goto fail2; 429c2d0b470SArd Biesheuvel hdr->ramdisk_image = ramdisk_addr & 0xffffffff; 430c2d0b470SArd Biesheuvel hdr->ramdisk_size = ramdisk_size & 0xffffffff; 431c2d0b470SArd Biesheuvel boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; 432c2d0b470SArd Biesheuvel boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; 433c2d0b470SArd Biesheuvel 434c2d0b470SArd Biesheuvel efi_stub_entry(handle, sys_table, boot_params); 435c2d0b470SArd Biesheuvel /* not reached */ 436c2d0b470SArd Biesheuvel 437c2d0b470SArd Biesheuvel fail2: 4381e45bf73SArd Biesheuvel efi_free(options_size, (unsigned long)cmdline_ptr); 439c2d0b470SArd Biesheuvel fail: 440c2d0b470SArd Biesheuvel efi_free(0x4000, (unsigned long)boot_params); 441c2d0b470SArd Biesheuvel 442c2d0b470SArd Biesheuvel return status; 443c2d0b470SArd Biesheuvel } 444c2d0b470SArd Biesheuvel 445c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params, 446c2d0b470SArd Biesheuvel struct setup_data *e820ext, u32 nr_entries) 447c2d0b470SArd Biesheuvel { 448c2d0b470SArd Biesheuvel struct setup_data *data; 449c2d0b470SArd Biesheuvel 450c2d0b470SArd Biesheuvel e820ext->type = SETUP_E820_EXT; 451c2d0b470SArd Biesheuvel e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 452c2d0b470SArd Biesheuvel e820ext->next = 0; 453c2d0b470SArd Biesheuvel 454c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 455c2d0b470SArd Biesheuvel 456c2d0b470SArd Biesheuvel while (data && data->next) 457c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 458c2d0b470SArd Biesheuvel 459c2d0b470SArd Biesheuvel if (data) 460c2d0b470SArd Biesheuvel data->next = (unsigned long)e820ext; 461c2d0b470SArd Biesheuvel else 462c2d0b470SArd Biesheuvel params->hdr.setup_data = (unsigned long)e820ext; 463c2d0b470SArd Biesheuvel } 464c2d0b470SArd Biesheuvel 465c2d0b470SArd Biesheuvel static efi_status_t 466c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 467c2d0b470SArd Biesheuvel { 468c2d0b470SArd Biesheuvel struct boot_e820_entry *entry = params->e820_table; 469c2d0b470SArd Biesheuvel struct efi_info *efi = ¶ms->efi_info; 470c2d0b470SArd Biesheuvel struct boot_e820_entry *prev = NULL; 471c2d0b470SArd Biesheuvel u32 nr_entries; 472c2d0b470SArd Biesheuvel u32 nr_desc; 473c2d0b470SArd Biesheuvel int i; 474c2d0b470SArd Biesheuvel 475c2d0b470SArd Biesheuvel nr_entries = 0; 476c2d0b470SArd Biesheuvel nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 477c2d0b470SArd Biesheuvel 478c2d0b470SArd Biesheuvel for (i = 0; i < nr_desc; i++) { 479c2d0b470SArd Biesheuvel efi_memory_desc_t *d; 480c2d0b470SArd Biesheuvel unsigned int e820_type = 0; 481c2d0b470SArd Biesheuvel unsigned long m = efi->efi_memmap; 482c2d0b470SArd Biesheuvel 483c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64 484c2d0b470SArd Biesheuvel m |= (u64)efi->efi_memmap_hi << 32; 485c2d0b470SArd Biesheuvel #endif 486c2d0b470SArd Biesheuvel 487c2d0b470SArd Biesheuvel d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); 488c2d0b470SArd Biesheuvel switch (d->type) { 489c2d0b470SArd Biesheuvel case EFI_RESERVED_TYPE: 490c2d0b470SArd Biesheuvel case EFI_RUNTIME_SERVICES_CODE: 491c2d0b470SArd Biesheuvel case EFI_RUNTIME_SERVICES_DATA: 492c2d0b470SArd Biesheuvel case EFI_MEMORY_MAPPED_IO: 493c2d0b470SArd Biesheuvel case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 494c2d0b470SArd Biesheuvel case EFI_PAL_CODE: 495c2d0b470SArd Biesheuvel e820_type = E820_TYPE_RESERVED; 496c2d0b470SArd Biesheuvel break; 497c2d0b470SArd Biesheuvel 498c2d0b470SArd Biesheuvel case EFI_UNUSABLE_MEMORY: 499c2d0b470SArd Biesheuvel e820_type = E820_TYPE_UNUSABLE; 500c2d0b470SArd Biesheuvel break; 501c2d0b470SArd Biesheuvel 502c2d0b470SArd Biesheuvel case EFI_ACPI_RECLAIM_MEMORY: 503c2d0b470SArd Biesheuvel e820_type = E820_TYPE_ACPI; 504c2d0b470SArd Biesheuvel break; 505c2d0b470SArd Biesheuvel 506c2d0b470SArd Biesheuvel case EFI_LOADER_CODE: 507c2d0b470SArd Biesheuvel case EFI_LOADER_DATA: 508c2d0b470SArd Biesheuvel case EFI_BOOT_SERVICES_CODE: 509c2d0b470SArd Biesheuvel case EFI_BOOT_SERVICES_DATA: 510c2d0b470SArd Biesheuvel case EFI_CONVENTIONAL_MEMORY: 511c2d0b470SArd Biesheuvel if (efi_soft_reserve_enabled() && 512c2d0b470SArd Biesheuvel (d->attribute & EFI_MEMORY_SP)) 513c2d0b470SArd Biesheuvel e820_type = E820_TYPE_SOFT_RESERVED; 514c2d0b470SArd Biesheuvel else 515c2d0b470SArd Biesheuvel e820_type = E820_TYPE_RAM; 516c2d0b470SArd Biesheuvel break; 517c2d0b470SArd Biesheuvel 518c2d0b470SArd Biesheuvel case EFI_ACPI_MEMORY_NVS: 519c2d0b470SArd Biesheuvel e820_type = E820_TYPE_NVS; 520c2d0b470SArd Biesheuvel break; 521c2d0b470SArd Biesheuvel 522c2d0b470SArd Biesheuvel case EFI_PERSISTENT_MEMORY: 523c2d0b470SArd Biesheuvel e820_type = E820_TYPE_PMEM; 524c2d0b470SArd Biesheuvel break; 525c2d0b470SArd Biesheuvel 526c2d0b470SArd Biesheuvel default: 527c2d0b470SArd Biesheuvel continue; 528c2d0b470SArd Biesheuvel } 529c2d0b470SArd Biesheuvel 530c2d0b470SArd Biesheuvel /* Merge adjacent mappings */ 531c2d0b470SArd Biesheuvel if (prev && prev->type == e820_type && 532c2d0b470SArd Biesheuvel (prev->addr + prev->size) == d->phys_addr) { 533c2d0b470SArd Biesheuvel prev->size += d->num_pages << 12; 534c2d0b470SArd Biesheuvel continue; 535c2d0b470SArd Biesheuvel } 536c2d0b470SArd Biesheuvel 537c2d0b470SArd Biesheuvel if (nr_entries == ARRAY_SIZE(params->e820_table)) { 538c2d0b470SArd Biesheuvel u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 539c2d0b470SArd Biesheuvel sizeof(struct setup_data); 540c2d0b470SArd Biesheuvel 541c2d0b470SArd Biesheuvel if (!e820ext || e820ext_size < need) 542c2d0b470SArd Biesheuvel return EFI_BUFFER_TOO_SMALL; 543c2d0b470SArd Biesheuvel 544c2d0b470SArd Biesheuvel /* boot_params map full, switch to e820 extended */ 545c2d0b470SArd Biesheuvel entry = (struct boot_e820_entry *)e820ext->data; 546c2d0b470SArd Biesheuvel } 547c2d0b470SArd Biesheuvel 548c2d0b470SArd Biesheuvel entry->addr = d->phys_addr; 549c2d0b470SArd Biesheuvel entry->size = d->num_pages << PAGE_SHIFT; 550c2d0b470SArd Biesheuvel entry->type = e820_type; 551c2d0b470SArd Biesheuvel prev = entry++; 552c2d0b470SArd Biesheuvel nr_entries++; 553c2d0b470SArd Biesheuvel } 554c2d0b470SArd Biesheuvel 555c2d0b470SArd Biesheuvel if (nr_entries > ARRAY_SIZE(params->e820_table)) { 556c2d0b470SArd Biesheuvel u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 557c2d0b470SArd Biesheuvel 558c2d0b470SArd Biesheuvel add_e820ext(params, e820ext, nr_e820ext); 559c2d0b470SArd Biesheuvel nr_entries -= nr_e820ext; 560c2d0b470SArd Biesheuvel } 561c2d0b470SArd Biesheuvel 562c2d0b470SArd Biesheuvel params->e820_entries = (u8)nr_entries; 563c2d0b470SArd Biesheuvel 564c2d0b470SArd Biesheuvel return EFI_SUCCESS; 565c2d0b470SArd Biesheuvel } 566c2d0b470SArd Biesheuvel 567c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 568c2d0b470SArd Biesheuvel u32 *e820ext_size) 569c2d0b470SArd Biesheuvel { 570c2d0b470SArd Biesheuvel efi_status_t status; 571c2d0b470SArd Biesheuvel unsigned long size; 572c2d0b470SArd Biesheuvel 573c2d0b470SArd Biesheuvel size = sizeof(struct setup_data) + 574c2d0b470SArd Biesheuvel sizeof(struct e820_entry) * nr_desc; 575c2d0b470SArd Biesheuvel 576c2d0b470SArd Biesheuvel if (*e820ext) { 577c2d0b470SArd Biesheuvel efi_bs_call(free_pool, *e820ext); 578c2d0b470SArd Biesheuvel *e820ext = NULL; 579c2d0b470SArd Biesheuvel *e820ext_size = 0; 580c2d0b470SArd Biesheuvel } 581c2d0b470SArd Biesheuvel 582c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 583c2d0b470SArd Biesheuvel (void **)e820ext); 584c2d0b470SArd Biesheuvel if (status == EFI_SUCCESS) 585c2d0b470SArd Biesheuvel *e820ext_size = size; 586c2d0b470SArd Biesheuvel 587c2d0b470SArd Biesheuvel return status; 588c2d0b470SArd Biesheuvel } 589c2d0b470SArd Biesheuvel 590c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params, 591c2d0b470SArd Biesheuvel struct setup_data **e820ext, 592c2d0b470SArd Biesheuvel u32 *e820ext_size) 593c2d0b470SArd Biesheuvel { 594c2d0b470SArd Biesheuvel unsigned long map_size, desc_size, buff_size; 595c2d0b470SArd Biesheuvel struct efi_boot_memmap boot_map; 596c2d0b470SArd Biesheuvel efi_memory_desc_t *map; 597c2d0b470SArd Biesheuvel efi_status_t status; 598c2d0b470SArd Biesheuvel __u32 nr_desc; 599c2d0b470SArd Biesheuvel 600c2d0b470SArd Biesheuvel boot_map.map = ↦ 601c2d0b470SArd Biesheuvel boot_map.map_size = &map_size; 602c2d0b470SArd Biesheuvel boot_map.desc_size = &desc_size; 603c2d0b470SArd Biesheuvel boot_map.desc_ver = NULL; 604c2d0b470SArd Biesheuvel boot_map.key_ptr = NULL; 605c2d0b470SArd Biesheuvel boot_map.buff_size = &buff_size; 606c2d0b470SArd Biesheuvel 607c2d0b470SArd Biesheuvel status = efi_get_memory_map(&boot_map); 608c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 609c2d0b470SArd Biesheuvel return status; 610c2d0b470SArd Biesheuvel 611c2d0b470SArd Biesheuvel nr_desc = buff_size / desc_size; 612c2d0b470SArd Biesheuvel 613c2d0b470SArd Biesheuvel if (nr_desc > ARRAY_SIZE(params->e820_table)) { 614c2d0b470SArd Biesheuvel u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); 615c2d0b470SArd Biesheuvel 616c2d0b470SArd Biesheuvel status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 617c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 618c2d0b470SArd Biesheuvel return status; 619c2d0b470SArd Biesheuvel } 620c2d0b470SArd Biesheuvel 621c2d0b470SArd Biesheuvel return EFI_SUCCESS; 622c2d0b470SArd Biesheuvel } 623c2d0b470SArd Biesheuvel 624c2d0b470SArd Biesheuvel struct exit_boot_struct { 625c2d0b470SArd Biesheuvel struct boot_params *boot_params; 626c2d0b470SArd Biesheuvel struct efi_info *efi; 627c2d0b470SArd Biesheuvel }; 628c2d0b470SArd Biesheuvel 629c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 630c2d0b470SArd Biesheuvel void *priv) 631c2d0b470SArd Biesheuvel { 632c2d0b470SArd Biesheuvel const char *signature; 633c2d0b470SArd Biesheuvel struct exit_boot_struct *p = priv; 634c2d0b470SArd Biesheuvel 635c2d0b470SArd Biesheuvel signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 636c2d0b470SArd Biesheuvel : EFI32_LOADER_SIGNATURE; 637c2d0b470SArd Biesheuvel memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 638c2d0b470SArd Biesheuvel 639c2d0b470SArd Biesheuvel p->efi->efi_systab = (unsigned long)efi_system_table(); 640c2d0b470SArd Biesheuvel p->efi->efi_memdesc_size = *map->desc_size; 641c2d0b470SArd Biesheuvel p->efi->efi_memdesc_version = *map->desc_ver; 642c2d0b470SArd Biesheuvel p->efi->efi_memmap = (unsigned long)*map->map; 643c2d0b470SArd Biesheuvel p->efi->efi_memmap_size = *map->map_size; 644c2d0b470SArd Biesheuvel 645c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64 646c2d0b470SArd Biesheuvel p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32; 647c2d0b470SArd Biesheuvel p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; 648c2d0b470SArd Biesheuvel #endif 649c2d0b470SArd Biesheuvel 650c2d0b470SArd Biesheuvel return EFI_SUCCESS; 651c2d0b470SArd Biesheuvel } 652c2d0b470SArd Biesheuvel 653c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 654c2d0b470SArd Biesheuvel { 655c2d0b470SArd Biesheuvel unsigned long map_sz, key, desc_size, buff_size; 656c2d0b470SArd Biesheuvel efi_memory_desc_t *mem_map; 657c2d0b470SArd Biesheuvel struct setup_data *e820ext = NULL; 658c2d0b470SArd Biesheuvel __u32 e820ext_size = 0; 659c2d0b470SArd Biesheuvel efi_status_t status; 660c2d0b470SArd Biesheuvel __u32 desc_version; 661c2d0b470SArd Biesheuvel struct efi_boot_memmap map; 662c2d0b470SArd Biesheuvel struct exit_boot_struct priv; 663c2d0b470SArd Biesheuvel 664c2d0b470SArd Biesheuvel map.map = &mem_map; 665c2d0b470SArd Biesheuvel map.map_size = &map_sz; 666c2d0b470SArd Biesheuvel map.desc_size = &desc_size; 667c2d0b470SArd Biesheuvel map.desc_ver = &desc_version; 668c2d0b470SArd Biesheuvel map.key_ptr = &key; 669c2d0b470SArd Biesheuvel map.buff_size = &buff_size; 670c2d0b470SArd Biesheuvel priv.boot_params = boot_params; 671c2d0b470SArd Biesheuvel priv.efi = &boot_params->efi_info; 672c2d0b470SArd Biesheuvel 673c2d0b470SArd Biesheuvel status = allocate_e820(boot_params, &e820ext, &e820ext_size); 674c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 675c2d0b470SArd Biesheuvel return status; 676c2d0b470SArd Biesheuvel 677c2d0b470SArd Biesheuvel /* Might as well exit boot services now */ 678c2d0b470SArd Biesheuvel status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); 679c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 680c2d0b470SArd Biesheuvel return status; 681c2d0b470SArd Biesheuvel 682c2d0b470SArd Biesheuvel /* Historic? */ 683c2d0b470SArd Biesheuvel boot_params->alt_mem_k = 32 * 1024; 684c2d0b470SArd Biesheuvel 685c2d0b470SArd Biesheuvel status = setup_e820(boot_params, e820ext, e820ext_size); 686c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 687c2d0b470SArd Biesheuvel return status; 688c2d0b470SArd Biesheuvel 689c2d0b470SArd Biesheuvel return EFI_SUCCESS; 690c2d0b470SArd Biesheuvel } 691c2d0b470SArd Biesheuvel 692c2d0b470SArd Biesheuvel /* 693c2d0b470SArd Biesheuvel * On success we return a pointer to a boot_params structure, and NULL 694c2d0b470SArd Biesheuvel * on failure. 695c2d0b470SArd Biesheuvel */ 696c2d0b470SArd Biesheuvel struct boot_params *efi_main(efi_handle_t handle, 697c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg, 698c2d0b470SArd Biesheuvel struct boot_params *boot_params) 699c2d0b470SArd Biesheuvel { 700c2d0b470SArd Biesheuvel unsigned long bzimage_addr = (unsigned long)startup_32; 701c2d0b470SArd Biesheuvel struct setup_header *hdr = &boot_params->hdr; 702c2d0b470SArd Biesheuvel efi_status_t status; 703c2d0b470SArd Biesheuvel unsigned long cmdline_paddr; 704c2d0b470SArd Biesheuvel 705c2d0b470SArd Biesheuvel sys_table = sys_table_arg; 706c2d0b470SArd Biesheuvel 707c2d0b470SArd Biesheuvel /* Check if we were booted by the EFI firmware */ 708c2d0b470SArd Biesheuvel if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 709c2d0b470SArd Biesheuvel goto fail; 710c2d0b470SArd Biesheuvel 711c2d0b470SArd Biesheuvel /* 712c2d0b470SArd Biesheuvel * If the kernel isn't already loaded at the preferred load 713c2d0b470SArd Biesheuvel * address, relocate it. 714c2d0b470SArd Biesheuvel */ 715c2d0b470SArd Biesheuvel if (bzimage_addr != hdr->pref_address) { 716c2d0b470SArd Biesheuvel status = efi_relocate_kernel(&bzimage_addr, 717c2d0b470SArd Biesheuvel hdr->init_size, hdr->init_size, 718c2d0b470SArd Biesheuvel hdr->pref_address, 719c2d0b470SArd Biesheuvel hdr->kernel_alignment, 720c2d0b470SArd Biesheuvel LOAD_PHYSICAL_ADDR); 721c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 722c2d0b470SArd Biesheuvel efi_printk("efi_relocate_kernel() failed!\n"); 723c2d0b470SArd Biesheuvel goto fail; 724c2d0b470SArd Biesheuvel } 725c2d0b470SArd Biesheuvel } 726c2d0b470SArd Biesheuvel hdr->code32_start = (u32)bzimage_addr; 727c2d0b470SArd Biesheuvel 728c2d0b470SArd Biesheuvel /* 729c2d0b470SArd Biesheuvel * make_boot_params() may have been called before efi_main(), in which 730c2d0b470SArd Biesheuvel * case this is the second time we parse the cmdline. This is ok, 731c2d0b470SArd Biesheuvel * parsing the cmdline multiple times does not have side-effects. 732c2d0b470SArd Biesheuvel */ 733c2d0b470SArd Biesheuvel cmdline_paddr = ((u64)hdr->cmd_line_ptr | 734c2d0b470SArd Biesheuvel ((u64)boot_params->ext_cmd_line_ptr << 32)); 735c2d0b470SArd Biesheuvel efi_parse_options((char *)cmdline_paddr); 736c2d0b470SArd Biesheuvel 737c2d0b470SArd Biesheuvel /* 738c2d0b470SArd Biesheuvel * If the boot loader gave us a value for secure_boot then we use that, 739c2d0b470SArd Biesheuvel * otherwise we ask the BIOS. 740c2d0b470SArd Biesheuvel */ 741c2d0b470SArd Biesheuvel if (boot_params->secure_boot == efi_secureboot_mode_unset) 742c2d0b470SArd Biesheuvel boot_params->secure_boot = efi_get_secureboot(); 743c2d0b470SArd Biesheuvel 744c2d0b470SArd Biesheuvel /* Ask the firmware to clear memory on unclean shutdown */ 745c2d0b470SArd Biesheuvel efi_enable_reset_attack_mitigation(); 746c2d0b470SArd Biesheuvel 747c2d0b470SArd Biesheuvel efi_random_get_seed(); 748c2d0b470SArd Biesheuvel 749c2d0b470SArd Biesheuvel efi_retrieve_tpm2_eventlog(); 750c2d0b470SArd Biesheuvel 751c2d0b470SArd Biesheuvel setup_graphics(boot_params); 752c2d0b470SArd Biesheuvel 753c2d0b470SArd Biesheuvel setup_efi_pci(boot_params); 754c2d0b470SArd Biesheuvel 755c2d0b470SArd Biesheuvel setup_quirks(boot_params); 756c2d0b470SArd Biesheuvel 757c2d0b470SArd Biesheuvel status = exit_boot(boot_params, handle); 758c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 759c2d0b470SArd Biesheuvel efi_printk("exit_boot() failed!\n"); 760c2d0b470SArd Biesheuvel goto fail; 761c2d0b470SArd Biesheuvel } 762c2d0b470SArd Biesheuvel 763c2d0b470SArd Biesheuvel return boot_params; 764c2d0b470SArd Biesheuvel fail: 765c2d0b470SArd Biesheuvel efi_printk("efi_main() failed!\n"); 766c2d0b470SArd Biesheuvel 767c2d0b470SArd Biesheuvel for (;;) 768c2d0b470SArd Biesheuvel asm("hlt"); 769c2d0b470SArd Biesheuvel } 770