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