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> 1159476f80SArvind Sankar #include <linux/stddef.h> 12c2d0b470SArd Biesheuvel 13c2d0b470SArd Biesheuvel #include <asm/efi.h> 14c2d0b470SArd Biesheuvel #include <asm/e820/types.h> 15c2d0b470SArd Biesheuvel #include <asm/setup.h> 16c2d0b470SArd Biesheuvel #include <asm/desc.h> 17c2d0b470SArd Biesheuvel #include <asm/boot.h> 18c2d0b470SArd Biesheuvel 19c2d0b470SArd Biesheuvel #include "efistub.h" 20c2d0b470SArd Biesheuvel 21d5cdf4cfSArvind Sankar /* Maximum physical address for 64-bit kernel with 4-level paging */ 22d5cdf4cfSArvind Sankar #define MAXMEM_X86_64_4LEVEL (1ull << 46) 23d5cdf4cfSArvind Sankar 24ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table; 251887c9b6SArvind Sankar extern u32 image_offset; 26987053a3SArvind Sankar static efi_loaded_image_t *image = NULL; 27c2d0b470SArd Biesheuvel 28c2d0b470SArd Biesheuvel static efi_status_t 29c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) 30c2d0b470SArd Biesheuvel { 31c2d0b470SArd Biesheuvel struct pci_setup_rom *rom = NULL; 32c2d0b470SArd Biesheuvel efi_status_t status; 33c2d0b470SArd Biesheuvel unsigned long size; 34c2d0b470SArd Biesheuvel uint64_t romsize; 35c2d0b470SArd Biesheuvel void *romimage; 36c2d0b470SArd Biesheuvel 37c2d0b470SArd Biesheuvel /* 38c2d0b470SArd Biesheuvel * Some firmware images contain EFI function pointers at the place where 39c2d0b470SArd Biesheuvel * the romimage and romsize fields are supposed to be. Typically the EFI 40c2d0b470SArd Biesheuvel * code is mapped at high addresses, translating to an unrealistically 41c2d0b470SArd Biesheuvel * large romsize. The UEFI spec limits the size of option ROMs to 16 42c2d0b470SArd Biesheuvel * MiB so we reject any ROMs over 16 MiB in size to catch this. 43c2d0b470SArd Biesheuvel */ 44c2d0b470SArd Biesheuvel romimage = efi_table_attr(pci, romimage); 45c2d0b470SArd Biesheuvel romsize = efi_table_attr(pci, romsize); 46c2d0b470SArd Biesheuvel if (!romimage || !romsize || romsize > SZ_16M) 47c2d0b470SArd Biesheuvel return EFI_INVALID_PARAMETER; 48c2d0b470SArd Biesheuvel 49c2d0b470SArd Biesheuvel size = romsize + sizeof(*rom); 50c2d0b470SArd Biesheuvel 51c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 52c2d0b470SArd Biesheuvel (void **)&rom); 53c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 5436bdd0a7SArvind Sankar efi_err("Failed to allocate memory for 'rom'\n"); 55c2d0b470SArd Biesheuvel return status; 56c2d0b470SArd Biesheuvel } 57c2d0b470SArd Biesheuvel 58c2d0b470SArd Biesheuvel memset(rom, 0, sizeof(*rom)); 59c2d0b470SArd Biesheuvel 60c2d0b470SArd Biesheuvel rom->data.type = SETUP_PCI; 61c2d0b470SArd Biesheuvel rom->data.len = size - sizeof(struct setup_data); 62c2d0b470SArd Biesheuvel rom->data.next = 0; 63c2d0b470SArd Biesheuvel rom->pcilen = pci->romsize; 64c2d0b470SArd Biesheuvel *__rom = rom; 65c2d0b470SArd Biesheuvel 66c2d0b470SArd Biesheuvel status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 67c2d0b470SArd Biesheuvel PCI_VENDOR_ID, 1, &rom->vendor); 68c2d0b470SArd Biesheuvel 69c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 7036bdd0a7SArvind Sankar efi_err("Failed to read rom->vendor\n"); 71c2d0b470SArd Biesheuvel goto free_struct; 72c2d0b470SArd Biesheuvel } 73c2d0b470SArd Biesheuvel 74c2d0b470SArd Biesheuvel status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 75c2d0b470SArd Biesheuvel PCI_DEVICE_ID, 1, &rom->devid); 76c2d0b470SArd Biesheuvel 77c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 7836bdd0a7SArvind Sankar efi_err("Failed to read rom->devid\n"); 79c2d0b470SArd Biesheuvel goto free_struct; 80c2d0b470SArd Biesheuvel } 81c2d0b470SArd Biesheuvel 82c2d0b470SArd Biesheuvel status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, 83c2d0b470SArd Biesheuvel &rom->device, &rom->function); 84c2d0b470SArd Biesheuvel 85c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 86c2d0b470SArd Biesheuvel goto free_struct; 87c2d0b470SArd Biesheuvel 88c2d0b470SArd Biesheuvel memcpy(rom->romdata, romimage, romsize); 89c2d0b470SArd Biesheuvel return status; 90c2d0b470SArd Biesheuvel 91c2d0b470SArd Biesheuvel free_struct: 92c2d0b470SArd Biesheuvel efi_bs_call(free_pool, rom); 93c2d0b470SArd Biesheuvel return status; 94c2d0b470SArd Biesheuvel } 95c2d0b470SArd Biesheuvel 96c2d0b470SArd Biesheuvel /* 97c2d0b470SArd Biesheuvel * There's no way to return an informative status from this function, 98c2d0b470SArd Biesheuvel * because any analysis (and printing of error messages) needs to be 99c2d0b470SArd Biesheuvel * done directly at the EFI function call-site. 100c2d0b470SArd Biesheuvel * 101c2d0b470SArd Biesheuvel * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we 102c2d0b470SArd Biesheuvel * just didn't find any PCI devices, but there's no way to tell outside 103c2d0b470SArd Biesheuvel * the context of the call. 104c2d0b470SArd Biesheuvel */ 105c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params) 106c2d0b470SArd Biesheuvel { 107c2d0b470SArd Biesheuvel efi_status_t status; 108c2d0b470SArd Biesheuvel void **pci_handle = NULL; 109c2d0b470SArd Biesheuvel efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; 110c2d0b470SArd Biesheuvel unsigned long size = 0; 111c2d0b470SArd Biesheuvel struct setup_data *data; 112c2d0b470SArd Biesheuvel efi_handle_t h; 113c2d0b470SArd Biesheuvel int i; 114c2d0b470SArd Biesheuvel 115c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 116c2d0b470SArd Biesheuvel &pci_proto, NULL, &size, pci_handle); 117c2d0b470SArd Biesheuvel 118c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) { 119c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 120c2d0b470SArd Biesheuvel (void **)&pci_handle); 121c2d0b470SArd Biesheuvel 122c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 12336bdd0a7SArvind Sankar efi_err("Failed to allocate memory for 'pci_handle'\n"); 124c2d0b470SArd Biesheuvel return; 125c2d0b470SArd Biesheuvel } 126c2d0b470SArd Biesheuvel 127c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 128c2d0b470SArd Biesheuvel &pci_proto, NULL, &size, pci_handle); 129c2d0b470SArd Biesheuvel } 130c2d0b470SArd Biesheuvel 131c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 132c2d0b470SArd Biesheuvel goto free_handle; 133c2d0b470SArd Biesheuvel 134c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 135c2d0b470SArd Biesheuvel 136c2d0b470SArd Biesheuvel while (data && data->next) 137c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 138c2d0b470SArd Biesheuvel 139c2d0b470SArd Biesheuvel for_each_efi_handle(h, pci_handle, size, i) { 140c2d0b470SArd Biesheuvel efi_pci_io_protocol_t *pci = NULL; 141c2d0b470SArd Biesheuvel struct pci_setup_rom *rom; 142c2d0b470SArd Biesheuvel 143c2d0b470SArd Biesheuvel status = efi_bs_call(handle_protocol, h, &pci_proto, 144c2d0b470SArd Biesheuvel (void **)&pci); 145c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS || !pci) 146c2d0b470SArd Biesheuvel continue; 147c2d0b470SArd Biesheuvel 148c2d0b470SArd Biesheuvel status = preserve_pci_rom_image(pci, &rom); 149c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 150c2d0b470SArd Biesheuvel continue; 151c2d0b470SArd Biesheuvel 152c2d0b470SArd Biesheuvel if (data) 153c2d0b470SArd Biesheuvel data->next = (unsigned long)rom; 154c2d0b470SArd Biesheuvel else 155c2d0b470SArd Biesheuvel params->hdr.setup_data = (unsigned long)rom; 156c2d0b470SArd Biesheuvel 157c2d0b470SArd Biesheuvel data = (struct setup_data *)rom; 158c2d0b470SArd Biesheuvel } 159c2d0b470SArd Biesheuvel 160c2d0b470SArd Biesheuvel free_handle: 161c2d0b470SArd Biesheuvel efi_bs_call(free_pool, pci_handle); 162c2d0b470SArd Biesheuvel } 163c2d0b470SArd Biesheuvel 164c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params) 165c2d0b470SArd Biesheuvel { 166c2d0b470SArd Biesheuvel efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; 167c2d0b470SArd Biesheuvel struct setup_data *data, *new; 168c2d0b470SArd Biesheuvel efi_status_t status; 169c2d0b470SArd Biesheuvel u32 size = 0; 170c2d0b470SArd Biesheuvel apple_properties_protocol_t *p; 171c2d0b470SArd Biesheuvel 172c2d0b470SArd Biesheuvel status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); 173c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 174c2d0b470SArd Biesheuvel return; 175c2d0b470SArd Biesheuvel 176c2d0b470SArd Biesheuvel if (efi_table_attr(p, version) != 0x10000) { 17736bdd0a7SArvind Sankar efi_err("Unsupported properties proto version\n"); 178c2d0b470SArd Biesheuvel return; 179c2d0b470SArd Biesheuvel } 180c2d0b470SArd Biesheuvel 181c2d0b470SArd Biesheuvel efi_call_proto(p, get_all, NULL, &size); 182c2d0b470SArd Biesheuvel if (!size) 183c2d0b470SArd Biesheuvel return; 184c2d0b470SArd Biesheuvel 185c2d0b470SArd Biesheuvel do { 186c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, 187c2d0b470SArd Biesheuvel size + sizeof(struct setup_data), 188c2d0b470SArd Biesheuvel (void **)&new); 189c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 19036bdd0a7SArvind Sankar efi_err("Failed to allocate memory for 'properties'\n"); 191c2d0b470SArd Biesheuvel return; 192c2d0b470SArd Biesheuvel } 193c2d0b470SArd Biesheuvel 194c2d0b470SArd Biesheuvel status = efi_call_proto(p, get_all, new->data, &size); 195c2d0b470SArd Biesheuvel 196c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 197c2d0b470SArd Biesheuvel efi_bs_call(free_pool, new); 198c2d0b470SArd Biesheuvel } while (status == EFI_BUFFER_TOO_SMALL); 199c2d0b470SArd Biesheuvel 200c2d0b470SArd Biesheuvel new->type = SETUP_APPLE_PROPERTIES; 201c2d0b470SArd Biesheuvel new->len = size; 202c2d0b470SArd Biesheuvel new->next = 0; 203c2d0b470SArd Biesheuvel 204c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; 205c2d0b470SArd Biesheuvel if (!data) { 206c2d0b470SArd Biesheuvel boot_params->hdr.setup_data = (unsigned long)new; 207c2d0b470SArd Biesheuvel } else { 208c2d0b470SArd Biesheuvel while (data->next) 209c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 210c2d0b470SArd Biesheuvel data->next = (unsigned long)new; 211c2d0b470SArd Biesheuvel } 212c2d0b470SArd Biesheuvel } 213c2d0b470SArd Biesheuvel 214c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple"; 215c2d0b470SArd Biesheuvel 216c2d0b470SArd Biesheuvel static void setup_quirks(struct boot_params *boot_params) 217c2d0b470SArd Biesheuvel { 218c2d0b470SArd Biesheuvel efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) 219ccc27ae7SArd Biesheuvel efi_table_attr(efi_system_table, fw_vendor); 220c2d0b470SArd Biesheuvel 221c2d0b470SArd Biesheuvel if (!memcmp(fw_vendor, apple, sizeof(apple))) { 222c2d0b470SArd Biesheuvel if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) 223c2d0b470SArd Biesheuvel retrieve_apple_device_properties(boot_params); 224c2d0b470SArd Biesheuvel } 225c2d0b470SArd Biesheuvel } 226c2d0b470SArd Biesheuvel 227c2d0b470SArd Biesheuvel /* 228c2d0b470SArd Biesheuvel * See if we have Universal Graphics Adapter (UGA) protocol 229c2d0b470SArd Biesheuvel */ 230c2d0b470SArd Biesheuvel static efi_status_t 231c2d0b470SArd Biesheuvel setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) 232c2d0b470SArd Biesheuvel { 233c2d0b470SArd Biesheuvel efi_status_t status; 234c2d0b470SArd Biesheuvel u32 width, height; 235c2d0b470SArd Biesheuvel void **uga_handle = NULL; 236c2d0b470SArd Biesheuvel efi_uga_draw_protocol_t *uga = NULL, *first_uga; 237c2d0b470SArd Biesheuvel efi_handle_t handle; 238c2d0b470SArd Biesheuvel int i; 239c2d0b470SArd Biesheuvel 240c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 241c2d0b470SArd Biesheuvel (void **)&uga_handle); 242c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 243c2d0b470SArd Biesheuvel return status; 244c2d0b470SArd Biesheuvel 245c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 246c2d0b470SArd Biesheuvel uga_proto, NULL, &size, uga_handle); 247c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 248c2d0b470SArd Biesheuvel goto free_handle; 249c2d0b470SArd Biesheuvel 250c2d0b470SArd Biesheuvel height = 0; 251c2d0b470SArd Biesheuvel width = 0; 252c2d0b470SArd Biesheuvel 253c2d0b470SArd Biesheuvel first_uga = NULL; 254c2d0b470SArd Biesheuvel for_each_efi_handle(handle, uga_handle, size, i) { 255c2d0b470SArd Biesheuvel efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 256c2d0b470SArd Biesheuvel u32 w, h, depth, refresh; 257c2d0b470SArd Biesheuvel void *pciio; 258c2d0b470SArd Biesheuvel 259c2d0b470SArd Biesheuvel status = efi_bs_call(handle_protocol, handle, uga_proto, 260c2d0b470SArd Biesheuvel (void **)&uga); 261c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 262c2d0b470SArd Biesheuvel continue; 263c2d0b470SArd Biesheuvel 264c2d0b470SArd Biesheuvel pciio = NULL; 265c2d0b470SArd Biesheuvel efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); 266c2d0b470SArd Biesheuvel 267c2d0b470SArd Biesheuvel status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); 268c2d0b470SArd Biesheuvel if (status == EFI_SUCCESS && (!first_uga || pciio)) { 269c2d0b470SArd Biesheuvel width = w; 270c2d0b470SArd Biesheuvel height = h; 271c2d0b470SArd Biesheuvel 272c2d0b470SArd Biesheuvel /* 273c2d0b470SArd Biesheuvel * Once we've found a UGA supporting PCIIO, 274c2d0b470SArd Biesheuvel * don't bother looking any further. 275c2d0b470SArd Biesheuvel */ 276c2d0b470SArd Biesheuvel if (pciio) 277c2d0b470SArd Biesheuvel break; 278c2d0b470SArd Biesheuvel 279c2d0b470SArd Biesheuvel first_uga = uga; 280c2d0b470SArd Biesheuvel } 281c2d0b470SArd Biesheuvel } 282c2d0b470SArd Biesheuvel 283c2d0b470SArd Biesheuvel if (!width && !height) 284c2d0b470SArd Biesheuvel goto free_handle; 285c2d0b470SArd Biesheuvel 286c2d0b470SArd Biesheuvel /* EFI framebuffer */ 287c2d0b470SArd Biesheuvel si->orig_video_isVGA = VIDEO_TYPE_EFI; 288c2d0b470SArd Biesheuvel 289c2d0b470SArd Biesheuvel si->lfb_depth = 32; 290c2d0b470SArd Biesheuvel si->lfb_width = width; 291c2d0b470SArd Biesheuvel si->lfb_height = height; 292c2d0b470SArd Biesheuvel 293c2d0b470SArd Biesheuvel si->red_size = 8; 294c2d0b470SArd Biesheuvel si->red_pos = 16; 295c2d0b470SArd Biesheuvel si->green_size = 8; 296c2d0b470SArd Biesheuvel si->green_pos = 8; 297c2d0b470SArd Biesheuvel si->blue_size = 8; 298c2d0b470SArd Biesheuvel si->blue_pos = 0; 299c2d0b470SArd Biesheuvel si->rsvd_size = 8; 300c2d0b470SArd Biesheuvel si->rsvd_pos = 24; 301c2d0b470SArd Biesheuvel 302c2d0b470SArd Biesheuvel free_handle: 303c2d0b470SArd Biesheuvel efi_bs_call(free_pool, uga_handle); 304c2d0b470SArd Biesheuvel 305c2d0b470SArd Biesheuvel return status; 306c2d0b470SArd Biesheuvel } 307c2d0b470SArd Biesheuvel 308c2d0b470SArd Biesheuvel static void setup_graphics(struct boot_params *boot_params) 309c2d0b470SArd Biesheuvel { 310c2d0b470SArd Biesheuvel efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 311c2d0b470SArd Biesheuvel struct screen_info *si; 312c2d0b470SArd Biesheuvel efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; 313c2d0b470SArd Biesheuvel efi_status_t status; 314c2d0b470SArd Biesheuvel unsigned long size; 315c2d0b470SArd Biesheuvel void **gop_handle = NULL; 316c2d0b470SArd Biesheuvel void **uga_handle = NULL; 317c2d0b470SArd Biesheuvel 318c2d0b470SArd Biesheuvel si = &boot_params->screen_info; 319c2d0b470SArd Biesheuvel memset(si, 0, sizeof(*si)); 320c2d0b470SArd Biesheuvel 321c2d0b470SArd Biesheuvel size = 0; 322c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 323c2d0b470SArd Biesheuvel &graphics_proto, NULL, &size, gop_handle); 324c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 325c2d0b470SArd Biesheuvel status = efi_setup_gop(si, &graphics_proto, size); 326c2d0b470SArd Biesheuvel 327c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 328c2d0b470SArd Biesheuvel size = 0; 329c2d0b470SArd Biesheuvel status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 330c2d0b470SArd Biesheuvel &uga_proto, NULL, &size, uga_handle); 331c2d0b470SArd Biesheuvel if (status == EFI_BUFFER_TOO_SMALL) 332c2d0b470SArd Biesheuvel setup_uga(si, &uga_proto, size); 333c2d0b470SArd Biesheuvel } 334c2d0b470SArd Biesheuvel } 335c2d0b470SArd Biesheuvel 3363b8f44fcSArd Biesheuvel 3373b8f44fcSArd Biesheuvel static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status) 3383b8f44fcSArd Biesheuvel { 3393b8f44fcSArd Biesheuvel efi_bs_call(exit, handle, status, 0, NULL); 340f3fa0efcSArd Biesheuvel for(;;) 341f3fa0efcSArd Biesheuvel asm("hlt"); 3423b8f44fcSArd Biesheuvel } 3433b8f44fcSArd Biesheuvel 344c2d0b470SArd Biesheuvel void startup_32(struct boot_params *boot_params); 345c2d0b470SArd Biesheuvel 346c2d0b470SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle, 347c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg, 348c2d0b470SArd Biesheuvel struct boot_params *boot_params); 349c2d0b470SArd Biesheuvel 350c2d0b470SArd Biesheuvel /* 351c2d0b470SArd Biesheuvel * Because the x86 boot code expects to be passed a boot_params we 352c2d0b470SArd Biesheuvel * need to create one ourselves (usually the bootloader would create 353c2d0b470SArd Biesheuvel * one for us). 354c2d0b470SArd Biesheuvel */ 355c2d0b470SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 356c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg) 357c2d0b470SArd Biesheuvel { 358c2d0b470SArd Biesheuvel struct boot_params *boot_params; 359c2d0b470SArd Biesheuvel struct setup_header *hdr; 3601887c9b6SArvind Sankar void *image_base; 361c2d0b470SArd Biesheuvel efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 362c2d0b470SArd Biesheuvel int options_size = 0; 363c2d0b470SArd Biesheuvel efi_status_t status; 364c2d0b470SArd Biesheuvel char *cmdline_ptr; 365c2d0b470SArd Biesheuvel 366ccc27ae7SArd Biesheuvel efi_system_table = sys_table_arg; 367c2d0b470SArd Biesheuvel 368c2d0b470SArd Biesheuvel /* Check if we were booted by the EFI firmware */ 369ccc27ae7SArd Biesheuvel if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 3703b8f44fcSArd Biesheuvel efi_exit(handle, EFI_INVALID_PARAMETER); 371c2d0b470SArd Biesheuvel 3720347d8c2SArvind Sankar status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image); 373c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 37436bdd0a7SArvind Sankar efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 3753b8f44fcSArd Biesheuvel efi_exit(handle, status); 376c2d0b470SArd Biesheuvel } 377c2d0b470SArd Biesheuvel 3781887c9b6SArvind Sankar image_base = efi_table_attr(image, image_base); 3791887c9b6SArvind Sankar image_offset = (void *)startup_32 - image_base; 3801887c9b6SArvind Sankar 381019512f1SArvind Sankar status = efi_allocate_pages(sizeof(struct boot_params), 382019512f1SArvind Sankar (unsigned long *)&boot_params, ULONG_MAX); 383c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 38436bdd0a7SArvind Sankar efi_err("Failed to allocate lowmem for boot params\n"); 3853b8f44fcSArd Biesheuvel efi_exit(handle, status); 386c2d0b470SArd Biesheuvel } 387c2d0b470SArd Biesheuvel 388019512f1SArvind Sankar memset(boot_params, 0x0, sizeof(struct boot_params)); 389c2d0b470SArd Biesheuvel 390c2d0b470SArd Biesheuvel hdr = &boot_params->hdr; 391c2d0b470SArd Biesheuvel 39259476f80SArvind Sankar /* Copy the setup header from the second sector to boot_params */ 39359476f80SArvind Sankar memcpy(&hdr->jump, image_base + 512, 39459476f80SArvind Sankar sizeof(struct setup_header) - offsetof(struct setup_header, jump)); 395c2d0b470SArd Biesheuvel 396c2d0b470SArd Biesheuvel /* 397c2d0b470SArd Biesheuvel * Fill out some of the header fields ourselves because the 398c2d0b470SArd Biesheuvel * EFI firmware loader doesn't load the first sector. 399c2d0b470SArd Biesheuvel */ 400c2d0b470SArd Biesheuvel hdr->root_flags = 1; 401c2d0b470SArd Biesheuvel hdr->vid_mode = 0xffff; 402c2d0b470SArd Biesheuvel hdr->boot_flag = 0xAA55; 403c2d0b470SArd Biesheuvel 404c2d0b470SArd Biesheuvel hdr->type_of_loader = 0x21; 405c2d0b470SArd Biesheuvel 406c2d0b470SArd Biesheuvel /* Convert unicode cmdline to ascii */ 40727cd5511SArd Biesheuvel cmdline_ptr = efi_convert_cmdline(image, &options_size); 408c2d0b470SArd Biesheuvel if (!cmdline_ptr) 409c2d0b470SArd Biesheuvel goto fail; 410c2d0b470SArd Biesheuvel 411eed4e019SArvind Sankar efi_set_u64_split((unsigned long)cmdline_ptr, 412eed4e019SArvind Sankar &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr); 413c2d0b470SArd Biesheuvel 414c2d0b470SArd Biesheuvel hdr->ramdisk_image = 0; 415c2d0b470SArd Biesheuvel hdr->ramdisk_size = 0; 416c2d0b470SArd Biesheuvel 417ccc27ae7SArd Biesheuvel efi_stub_entry(handle, sys_table_arg, boot_params); 418c2d0b470SArd Biesheuvel /* not reached */ 419c2d0b470SArd Biesheuvel 420c2d0b470SArd Biesheuvel fail: 421019512f1SArvind Sankar efi_free(sizeof(struct boot_params), (unsigned long)boot_params); 422c2d0b470SArd Biesheuvel 4233b8f44fcSArd Biesheuvel efi_exit(handle, status); 424c2d0b470SArd Biesheuvel } 425c2d0b470SArd Biesheuvel 426c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params, 427c2d0b470SArd Biesheuvel struct setup_data *e820ext, u32 nr_entries) 428c2d0b470SArd Biesheuvel { 429c2d0b470SArd Biesheuvel struct setup_data *data; 430c2d0b470SArd Biesheuvel 431c2d0b470SArd Biesheuvel e820ext->type = SETUP_E820_EXT; 432c2d0b470SArd Biesheuvel e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 433c2d0b470SArd Biesheuvel e820ext->next = 0; 434c2d0b470SArd Biesheuvel 435c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 436c2d0b470SArd Biesheuvel 437c2d0b470SArd Biesheuvel while (data && data->next) 438c2d0b470SArd Biesheuvel data = (struct setup_data *)(unsigned long)data->next; 439c2d0b470SArd Biesheuvel 440c2d0b470SArd Biesheuvel if (data) 441c2d0b470SArd Biesheuvel data->next = (unsigned long)e820ext; 442c2d0b470SArd Biesheuvel else 443c2d0b470SArd Biesheuvel params->hdr.setup_data = (unsigned long)e820ext; 444c2d0b470SArd Biesheuvel } 445c2d0b470SArd Biesheuvel 446c2d0b470SArd Biesheuvel static efi_status_t 447c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 448c2d0b470SArd Biesheuvel { 449c2d0b470SArd Biesheuvel struct boot_e820_entry *entry = params->e820_table; 450c2d0b470SArd Biesheuvel struct efi_info *efi = ¶ms->efi_info; 451c2d0b470SArd Biesheuvel struct boot_e820_entry *prev = NULL; 452c2d0b470SArd Biesheuvel u32 nr_entries; 453c2d0b470SArd Biesheuvel u32 nr_desc; 454c2d0b470SArd Biesheuvel int i; 455c2d0b470SArd Biesheuvel 456c2d0b470SArd Biesheuvel nr_entries = 0; 457c2d0b470SArd Biesheuvel nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 458c2d0b470SArd Biesheuvel 459c2d0b470SArd Biesheuvel for (i = 0; i < nr_desc; i++) { 460c2d0b470SArd Biesheuvel efi_memory_desc_t *d; 461c2d0b470SArd Biesheuvel unsigned int e820_type = 0; 462c2d0b470SArd Biesheuvel unsigned long m = efi->efi_memmap; 463c2d0b470SArd Biesheuvel 464c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64 465c2d0b470SArd Biesheuvel m |= (u64)efi->efi_memmap_hi << 32; 466c2d0b470SArd Biesheuvel #endif 467c2d0b470SArd Biesheuvel 468c2d0b470SArd Biesheuvel d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); 469c2d0b470SArd Biesheuvel switch (d->type) { 470c2d0b470SArd Biesheuvel case EFI_RESERVED_TYPE: 471c2d0b470SArd Biesheuvel case EFI_RUNTIME_SERVICES_CODE: 472c2d0b470SArd Biesheuvel case EFI_RUNTIME_SERVICES_DATA: 473c2d0b470SArd Biesheuvel case EFI_MEMORY_MAPPED_IO: 474c2d0b470SArd Biesheuvel case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 475c2d0b470SArd Biesheuvel case EFI_PAL_CODE: 476c2d0b470SArd Biesheuvel e820_type = E820_TYPE_RESERVED; 477c2d0b470SArd Biesheuvel break; 478c2d0b470SArd Biesheuvel 479c2d0b470SArd Biesheuvel case EFI_UNUSABLE_MEMORY: 480c2d0b470SArd Biesheuvel e820_type = E820_TYPE_UNUSABLE; 481c2d0b470SArd Biesheuvel break; 482c2d0b470SArd Biesheuvel 483c2d0b470SArd Biesheuvel case EFI_ACPI_RECLAIM_MEMORY: 484c2d0b470SArd Biesheuvel e820_type = E820_TYPE_ACPI; 485c2d0b470SArd Biesheuvel break; 486c2d0b470SArd Biesheuvel 487c2d0b470SArd Biesheuvel case EFI_LOADER_CODE: 488c2d0b470SArd Biesheuvel case EFI_LOADER_DATA: 489c2d0b470SArd Biesheuvel case EFI_BOOT_SERVICES_CODE: 490c2d0b470SArd Biesheuvel case EFI_BOOT_SERVICES_DATA: 491c2d0b470SArd Biesheuvel case EFI_CONVENTIONAL_MEMORY: 492c2d0b470SArd Biesheuvel if (efi_soft_reserve_enabled() && 493c2d0b470SArd Biesheuvel (d->attribute & EFI_MEMORY_SP)) 494c2d0b470SArd Biesheuvel e820_type = E820_TYPE_SOFT_RESERVED; 495c2d0b470SArd Biesheuvel else 496c2d0b470SArd Biesheuvel e820_type = E820_TYPE_RAM; 497c2d0b470SArd Biesheuvel break; 498c2d0b470SArd Biesheuvel 499c2d0b470SArd Biesheuvel case EFI_ACPI_MEMORY_NVS: 500c2d0b470SArd Biesheuvel e820_type = E820_TYPE_NVS; 501c2d0b470SArd Biesheuvel break; 502c2d0b470SArd Biesheuvel 503c2d0b470SArd Biesheuvel case EFI_PERSISTENT_MEMORY: 504c2d0b470SArd Biesheuvel e820_type = E820_TYPE_PMEM; 505c2d0b470SArd Biesheuvel break; 506c2d0b470SArd Biesheuvel 507c2d0b470SArd Biesheuvel default: 508c2d0b470SArd Biesheuvel continue; 509c2d0b470SArd Biesheuvel } 510c2d0b470SArd Biesheuvel 511c2d0b470SArd Biesheuvel /* Merge adjacent mappings */ 512c2d0b470SArd Biesheuvel if (prev && prev->type == e820_type && 513c2d0b470SArd Biesheuvel (prev->addr + prev->size) == d->phys_addr) { 514c2d0b470SArd Biesheuvel prev->size += d->num_pages << 12; 515c2d0b470SArd Biesheuvel continue; 516c2d0b470SArd Biesheuvel } 517c2d0b470SArd Biesheuvel 518c2d0b470SArd Biesheuvel if (nr_entries == ARRAY_SIZE(params->e820_table)) { 519c2d0b470SArd Biesheuvel u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 520c2d0b470SArd Biesheuvel sizeof(struct setup_data); 521c2d0b470SArd Biesheuvel 522c2d0b470SArd Biesheuvel if (!e820ext || e820ext_size < need) 523c2d0b470SArd Biesheuvel return EFI_BUFFER_TOO_SMALL; 524c2d0b470SArd Biesheuvel 525c2d0b470SArd Biesheuvel /* boot_params map full, switch to e820 extended */ 526c2d0b470SArd Biesheuvel entry = (struct boot_e820_entry *)e820ext->data; 527c2d0b470SArd Biesheuvel } 528c2d0b470SArd Biesheuvel 529c2d0b470SArd Biesheuvel entry->addr = d->phys_addr; 530c2d0b470SArd Biesheuvel entry->size = d->num_pages << PAGE_SHIFT; 531c2d0b470SArd Biesheuvel entry->type = e820_type; 532c2d0b470SArd Biesheuvel prev = entry++; 533c2d0b470SArd Biesheuvel nr_entries++; 534c2d0b470SArd Biesheuvel } 535c2d0b470SArd Biesheuvel 536c2d0b470SArd Biesheuvel if (nr_entries > ARRAY_SIZE(params->e820_table)) { 537c2d0b470SArd Biesheuvel u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 538c2d0b470SArd Biesheuvel 539c2d0b470SArd Biesheuvel add_e820ext(params, e820ext, nr_e820ext); 540c2d0b470SArd Biesheuvel nr_entries -= nr_e820ext; 541c2d0b470SArd Biesheuvel } 542c2d0b470SArd Biesheuvel 543c2d0b470SArd Biesheuvel params->e820_entries = (u8)nr_entries; 544c2d0b470SArd Biesheuvel 545c2d0b470SArd Biesheuvel return EFI_SUCCESS; 546c2d0b470SArd Biesheuvel } 547c2d0b470SArd Biesheuvel 548c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 549c2d0b470SArd Biesheuvel u32 *e820ext_size) 550c2d0b470SArd Biesheuvel { 551c2d0b470SArd Biesheuvel efi_status_t status; 552c2d0b470SArd Biesheuvel unsigned long size; 553c2d0b470SArd Biesheuvel 554c2d0b470SArd Biesheuvel size = sizeof(struct setup_data) + 555c2d0b470SArd Biesheuvel sizeof(struct e820_entry) * nr_desc; 556c2d0b470SArd Biesheuvel 557c2d0b470SArd Biesheuvel if (*e820ext) { 558c2d0b470SArd Biesheuvel efi_bs_call(free_pool, *e820ext); 559c2d0b470SArd Biesheuvel *e820ext = NULL; 560c2d0b470SArd Biesheuvel *e820ext_size = 0; 561c2d0b470SArd Biesheuvel } 562c2d0b470SArd Biesheuvel 563c2d0b470SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 564c2d0b470SArd Biesheuvel (void **)e820ext); 565c2d0b470SArd Biesheuvel if (status == EFI_SUCCESS) 566c2d0b470SArd Biesheuvel *e820ext_size = size; 567c2d0b470SArd Biesheuvel 568c2d0b470SArd Biesheuvel return status; 569c2d0b470SArd Biesheuvel } 570c2d0b470SArd Biesheuvel 571c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params, 572c2d0b470SArd Biesheuvel struct setup_data **e820ext, 573c2d0b470SArd Biesheuvel u32 *e820ext_size) 574c2d0b470SArd Biesheuvel { 575fd626195SLenny Szubowicz unsigned long map_size, desc_size, map_key; 576c2d0b470SArd Biesheuvel efi_status_t status; 577fd626195SLenny Szubowicz __u32 nr_desc, desc_version; 578c2d0b470SArd Biesheuvel 579fd626195SLenny Szubowicz /* Only need the size of the mem map and size of each mem descriptor */ 580fd626195SLenny Szubowicz map_size = 0; 581fd626195SLenny Szubowicz status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key, 582fd626195SLenny Szubowicz &desc_size, &desc_version); 583fd626195SLenny Szubowicz if (status != EFI_BUFFER_TOO_SMALL) 584fd626195SLenny Szubowicz return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED; 585c2d0b470SArd Biesheuvel 586fd626195SLenny Szubowicz nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS; 587c2d0b470SArd Biesheuvel 588c2d0b470SArd Biesheuvel if (nr_desc > ARRAY_SIZE(params->e820_table)) { 589c2d0b470SArd Biesheuvel u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); 590c2d0b470SArd Biesheuvel 591c2d0b470SArd Biesheuvel status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 592c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 593c2d0b470SArd Biesheuvel return status; 594c2d0b470SArd Biesheuvel } 595c2d0b470SArd Biesheuvel 596c2d0b470SArd Biesheuvel return EFI_SUCCESS; 597c2d0b470SArd Biesheuvel } 598c2d0b470SArd Biesheuvel 599c2d0b470SArd Biesheuvel struct exit_boot_struct { 600c2d0b470SArd Biesheuvel struct boot_params *boot_params; 601c2d0b470SArd Biesheuvel struct efi_info *efi; 602c2d0b470SArd Biesheuvel }; 603c2d0b470SArd Biesheuvel 604c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 605c2d0b470SArd Biesheuvel void *priv) 606c2d0b470SArd Biesheuvel { 607c2d0b470SArd Biesheuvel const char *signature; 608c2d0b470SArd Biesheuvel struct exit_boot_struct *p = priv; 609c2d0b470SArd Biesheuvel 610c2d0b470SArd Biesheuvel signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 611c2d0b470SArd Biesheuvel : EFI32_LOADER_SIGNATURE; 612c2d0b470SArd Biesheuvel memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 613c2d0b470SArd Biesheuvel 614eed4e019SArvind Sankar efi_set_u64_split((unsigned long)efi_system_table, 615eed4e019SArvind Sankar &p->efi->efi_systab, &p->efi->efi_systab_hi); 616c2d0b470SArd Biesheuvel p->efi->efi_memdesc_size = *map->desc_size; 617c2d0b470SArd Biesheuvel p->efi->efi_memdesc_version = *map->desc_ver; 618eed4e019SArvind Sankar efi_set_u64_split((unsigned long)*map->map, 619eed4e019SArvind Sankar &p->efi->efi_memmap, &p->efi->efi_memmap_hi); 620c2d0b470SArd Biesheuvel p->efi->efi_memmap_size = *map->map_size; 621c2d0b470SArd Biesheuvel 622c2d0b470SArd Biesheuvel return EFI_SUCCESS; 623c2d0b470SArd Biesheuvel } 624c2d0b470SArd Biesheuvel 625c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 626c2d0b470SArd Biesheuvel { 627c2d0b470SArd Biesheuvel unsigned long map_sz, key, desc_size, buff_size; 628c2d0b470SArd Biesheuvel efi_memory_desc_t *mem_map; 629c2d0b470SArd Biesheuvel struct setup_data *e820ext = NULL; 630c2d0b470SArd Biesheuvel __u32 e820ext_size = 0; 631c2d0b470SArd Biesheuvel efi_status_t status; 632c2d0b470SArd Biesheuvel __u32 desc_version; 633c2d0b470SArd Biesheuvel struct efi_boot_memmap map; 634c2d0b470SArd Biesheuvel struct exit_boot_struct priv; 635c2d0b470SArd Biesheuvel 636c2d0b470SArd Biesheuvel map.map = &mem_map; 637c2d0b470SArd Biesheuvel map.map_size = &map_sz; 638c2d0b470SArd Biesheuvel map.desc_size = &desc_size; 639c2d0b470SArd Biesheuvel map.desc_ver = &desc_version; 640c2d0b470SArd Biesheuvel map.key_ptr = &key; 641c2d0b470SArd Biesheuvel map.buff_size = &buff_size; 642c2d0b470SArd Biesheuvel priv.boot_params = boot_params; 643c2d0b470SArd Biesheuvel priv.efi = &boot_params->efi_info; 644c2d0b470SArd Biesheuvel 645c2d0b470SArd Biesheuvel status = allocate_e820(boot_params, &e820ext, &e820ext_size); 646c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 647c2d0b470SArd Biesheuvel return status; 648c2d0b470SArd Biesheuvel 649c2d0b470SArd Biesheuvel /* Might as well exit boot services now */ 650c2d0b470SArd Biesheuvel status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); 651c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 652c2d0b470SArd Biesheuvel return status; 653c2d0b470SArd Biesheuvel 654c2d0b470SArd Biesheuvel /* Historic? */ 655c2d0b470SArd Biesheuvel boot_params->alt_mem_k = 32 * 1024; 656c2d0b470SArd Biesheuvel 657c2d0b470SArd Biesheuvel status = setup_e820(boot_params, e820ext, e820ext_size); 658c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) 659c2d0b470SArd Biesheuvel return status; 660c2d0b470SArd Biesheuvel 661c2d0b470SArd Biesheuvel return EFI_SUCCESS; 662c2d0b470SArd Biesheuvel } 663c2d0b470SArd Biesheuvel 664c2d0b470SArd Biesheuvel /* 6658acf63efSArvind Sankar * On success, we return the address of startup_32, which has potentially been 6668acf63efSArvind Sankar * relocated by efi_relocate_kernel. 6678acf63efSArvind Sankar * On failure, we exit to the firmware via efi_exit instead of returning. 668c2d0b470SArd Biesheuvel */ 6698acf63efSArvind Sankar unsigned long efi_main(efi_handle_t handle, 670c2d0b470SArd Biesheuvel efi_system_table_t *sys_table_arg, 671c2d0b470SArd Biesheuvel struct boot_params *boot_params) 672c2d0b470SArd Biesheuvel { 673c2d0b470SArd Biesheuvel unsigned long bzimage_addr = (unsigned long)startup_32; 674d5cdf4cfSArvind Sankar unsigned long buffer_start, buffer_end; 675c2d0b470SArd Biesheuvel struct setup_header *hdr = &boot_params->hdr; 676c2d0b470SArd Biesheuvel efi_status_t status; 677c2d0b470SArd Biesheuvel 678ccc27ae7SArd Biesheuvel efi_system_table = sys_table_arg; 679c2d0b470SArd Biesheuvel 680c2d0b470SArd Biesheuvel /* Check if we were booted by the EFI firmware */ 681ccc27ae7SArd Biesheuvel if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 6823b8f44fcSArd Biesheuvel efi_exit(handle, EFI_INVALID_PARAMETER); 683c2d0b470SArd Biesheuvel 684c2d0b470SArd Biesheuvel /* 685d5cdf4cfSArvind Sankar * If the kernel isn't already loaded at a suitable address, 686d5cdf4cfSArvind Sankar * relocate it. 687d5cdf4cfSArvind Sankar * 688d5cdf4cfSArvind Sankar * It must be loaded above LOAD_PHYSICAL_ADDR. 689d5cdf4cfSArvind Sankar * 690d5cdf4cfSArvind Sankar * The maximum address for 64-bit is 1 << 46 for 4-level paging. This 691d5cdf4cfSArvind Sankar * is defined as the macro MAXMEM, but unfortunately that is not a 692d5cdf4cfSArvind Sankar * compile-time constant if 5-level paging is configured, so we instead 693d5cdf4cfSArvind Sankar * define our own macro for use here. 694d5cdf4cfSArvind Sankar * 695d5cdf4cfSArvind Sankar * For 32-bit, the maximum address is complicated to figure out, for 696d5cdf4cfSArvind Sankar * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what 697d5cdf4cfSArvind Sankar * KASLR uses. 698d5cdf4cfSArvind Sankar * 69921cb9b41SArvind Sankar * Also relocate it if image_offset is zero, i.e. the kernel wasn't 70021cb9b41SArvind Sankar * loaded by LoadImage, but rather by a bootloader that called the 70121cb9b41SArvind Sankar * handover entry. The reason we must always relocate in this case is 70221cb9b41SArvind Sankar * to handle the case of systemd-boot booting a unified kernel image, 70321cb9b41SArvind Sankar * which is a PE executable that contains the bzImage and an initrd as 70421cb9b41SArvind Sankar * COFF sections. The initrd section is placed after the bzImage 70521cb9b41SArvind Sankar * without ensuring that there are at least init_size bytes available 70621cb9b41SArvind Sankar * for the bzImage, and thus the compressed kernel's startup code may 70721cb9b41SArvind Sankar * overwrite the initrd unless it is moved out of the way. 708c2d0b470SArd Biesheuvel */ 709d5cdf4cfSArvind Sankar 710d5cdf4cfSArvind Sankar buffer_start = ALIGN(bzimage_addr - image_offset, 711d5cdf4cfSArvind Sankar hdr->kernel_alignment); 712d5cdf4cfSArvind Sankar buffer_end = buffer_start + hdr->init_size; 713d5cdf4cfSArvind Sankar 714d5cdf4cfSArvind Sankar if ((buffer_start < LOAD_PHYSICAL_ADDR) || 715d5cdf4cfSArvind Sankar (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) || 716d5cdf4cfSArvind Sankar (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) || 71721cb9b41SArvind Sankar (image_offset == 0)) { 718c2d0b470SArd Biesheuvel status = efi_relocate_kernel(&bzimage_addr, 719c2d0b470SArd Biesheuvel hdr->init_size, hdr->init_size, 720c2d0b470SArd Biesheuvel hdr->pref_address, 721c2d0b470SArd Biesheuvel hdr->kernel_alignment, 722c2d0b470SArd Biesheuvel LOAD_PHYSICAL_ADDR); 723c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 72436bdd0a7SArvind Sankar efi_err("efi_relocate_kernel() failed!\n"); 725c2d0b470SArd Biesheuvel goto fail; 726c2d0b470SArd Biesheuvel } 7271887c9b6SArvind Sankar /* 7281887c9b6SArvind Sankar * Now that we've copied the kernel elsewhere, we no longer 7291887c9b6SArvind Sankar * have a set up block before startup_32(), so reset image_offset 7301887c9b6SArvind Sankar * to zero in case it was set earlier. 7311887c9b6SArvind Sankar */ 7321887c9b6SArvind Sankar image_offset = 0; 733c2d0b470SArd Biesheuvel } 734c2d0b470SArd Biesheuvel 7357dde67f2SArvind Sankar #ifdef CONFIG_CMDLINE_BOOL 736055042beSArvind Sankar status = efi_parse_options(CONFIG_CMDLINE); 737055042beSArvind Sankar if (status != EFI_SUCCESS) { 738055042beSArvind Sankar efi_err("Failed to parse options\n"); 739055042beSArvind Sankar goto fail; 740055042beSArvind Sankar } 7417dde67f2SArvind Sankar #endif 7427dde67f2SArvind Sankar if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) { 7437dde67f2SArvind Sankar unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr | 744c2d0b470SArd Biesheuvel ((u64)boot_params->ext_cmd_line_ptr << 32)); 745055042beSArvind Sankar status = efi_parse_options((char *)cmdline_paddr); 746055042beSArvind Sankar if (status != EFI_SUCCESS) { 747055042beSArvind Sankar efi_err("Failed to parse options\n"); 748055042beSArvind Sankar goto fail; 749055042beSArvind Sankar } 7507dde67f2SArvind Sankar } 751c2d0b470SArd Biesheuvel 752c2d0b470SArd Biesheuvel /* 753987053a3SArvind Sankar * At this point, an initrd may already have been loaded by the 754987053a3SArvind Sankar * bootloader and passed via bootparams. We permit an initrd loaded 755987053a3SArvind Sankar * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it. 756987053a3SArvind Sankar * 757987053a3SArvind Sankar * If the device path is not present, any command-line initrd= 758987053a3SArvind Sankar * arguments will be processed only if image is not NULL, which will be 759987053a3SArvind Sankar * the case only if we were loaded via the PE entry point. 760ec93fc37SArd Biesheuvel */ 761980771f6SArd Biesheuvel if (!efi_noinitrd) { 76279d3219dSArd Biesheuvel unsigned long addr, size; 76379d3219dSArd Biesheuvel 764987053a3SArvind Sankar status = efi_load_initrd(image, &addr, &size, 765f61900fdSArvind Sankar hdr->initrd_addr_max, ULONG_MAX); 766987053a3SArvind Sankar 767987053a3SArvind Sankar if (status != EFI_SUCCESS) { 768987053a3SArvind Sankar efi_err("Failed to load initrd!\n"); 769ec93fc37SArd Biesheuvel goto fail; 770ec93fc37SArd Biesheuvel } 771e9524fb9SArvind Sankar if (size > 0) { 772eed4e019SArvind Sankar efi_set_u64_split(addr, &hdr->ramdisk_image, 773eed4e019SArvind Sankar &boot_params->ext_ramdisk_image); 774eed4e019SArvind Sankar efi_set_u64_split(size, &hdr->ramdisk_size, 775eed4e019SArvind Sankar &boot_params->ext_ramdisk_size); 77679d3219dSArd Biesheuvel } 777e9524fb9SArvind Sankar } 778ec93fc37SArd Biesheuvel 779ec93fc37SArd Biesheuvel /* 780c2d0b470SArd Biesheuvel * If the boot loader gave us a value for secure_boot then we use that, 781c2d0b470SArd Biesheuvel * otherwise we ask the BIOS. 782c2d0b470SArd Biesheuvel */ 783c2d0b470SArd Biesheuvel if (boot_params->secure_boot == efi_secureboot_mode_unset) 784c2d0b470SArd Biesheuvel boot_params->secure_boot = efi_get_secureboot(); 785c2d0b470SArd Biesheuvel 786c2d0b470SArd Biesheuvel /* Ask the firmware to clear memory on unclean shutdown */ 787c2d0b470SArd Biesheuvel efi_enable_reset_attack_mitigation(); 788c2d0b470SArd Biesheuvel 789c2d0b470SArd Biesheuvel efi_random_get_seed(); 790c2d0b470SArd Biesheuvel 791c2d0b470SArd Biesheuvel efi_retrieve_tpm2_eventlog(); 792c2d0b470SArd Biesheuvel 793c2d0b470SArd Biesheuvel setup_graphics(boot_params); 794c2d0b470SArd Biesheuvel 795c2d0b470SArd Biesheuvel setup_efi_pci(boot_params); 796c2d0b470SArd Biesheuvel 797c2d0b470SArd Biesheuvel setup_quirks(boot_params); 798c2d0b470SArd Biesheuvel 799c2d0b470SArd Biesheuvel status = exit_boot(boot_params, handle); 800c2d0b470SArd Biesheuvel if (status != EFI_SUCCESS) { 80136bdd0a7SArvind Sankar efi_err("exit_boot() failed!\n"); 802c2d0b470SArd Biesheuvel goto fail; 803c2d0b470SArd Biesheuvel } 804c2d0b470SArd Biesheuvel 8058acf63efSArvind Sankar return bzimage_addr; 806c2d0b470SArd Biesheuvel fail: 80736bdd0a7SArvind Sankar efi_err("efi_main() failed!\n"); 808c2d0b470SArd Biesheuvel 8093b8f44fcSArd Biesheuvel efi_exit(handle, status); 810c2d0b470SArd Biesheuvel } 811