Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 /* -----------------------------------------------------------------------
0004  *
0005  *   Copyright 2011 Intel Corporation; author Matt Fleming
0006  *
0007  * ----------------------------------------------------------------------- */
0008 
0009 #include <linux/efi.h>
0010 #include <linux/pci.h>
0011 #include <linux/stddef.h>
0012 
0013 #include <asm/efi.h>
0014 #include <asm/e820/types.h>
0015 #include <asm/setup.h>
0016 #include <asm/desc.h>
0017 #include <asm/boot.h>
0018 
0019 #include "efistub.h"
0020 
0021 /* Maximum physical address for 64-bit kernel with 4-level paging */
0022 #define MAXMEM_X86_64_4LEVEL (1ull << 46)
0023 
0024 const efi_system_table_t *efi_system_table;
0025 const efi_dxe_services_table_t *efi_dxe_table;
0026 extern u32 image_offset;
0027 static efi_loaded_image_t *image = NULL;
0028 
0029 static efi_status_t
0030 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
0031 {
0032     struct pci_setup_rom *rom = NULL;
0033     efi_status_t status;
0034     unsigned long size;
0035     uint64_t romsize;
0036     void *romimage;
0037 
0038     /*
0039      * Some firmware images contain EFI function pointers at the place where
0040      * the romimage and romsize fields are supposed to be. Typically the EFI
0041      * code is mapped at high addresses, translating to an unrealistically
0042      * large romsize. The UEFI spec limits the size of option ROMs to 16
0043      * MiB so we reject any ROMs over 16 MiB in size to catch this.
0044      */
0045     romimage = efi_table_attr(pci, romimage);
0046     romsize = efi_table_attr(pci, romsize);
0047     if (!romimage || !romsize || romsize > SZ_16M)
0048         return EFI_INVALID_PARAMETER;
0049 
0050     size = romsize + sizeof(*rom);
0051 
0052     status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
0053                  (void **)&rom);
0054     if (status != EFI_SUCCESS) {
0055         efi_err("Failed to allocate memory for 'rom'\n");
0056         return status;
0057     }
0058 
0059     memset(rom, 0, sizeof(*rom));
0060 
0061     rom->data.type  = SETUP_PCI;
0062     rom->data.len   = size - sizeof(struct setup_data);
0063     rom->data.next  = 0;
0064     rom->pcilen = pci->romsize;
0065     *__rom = rom;
0066 
0067     status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
0068                 PCI_VENDOR_ID, 1, &rom->vendor);
0069 
0070     if (status != EFI_SUCCESS) {
0071         efi_err("Failed to read rom->vendor\n");
0072         goto free_struct;
0073     }
0074 
0075     status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
0076                 PCI_DEVICE_ID, 1, &rom->devid);
0077 
0078     if (status != EFI_SUCCESS) {
0079         efi_err("Failed to read rom->devid\n");
0080         goto free_struct;
0081     }
0082 
0083     status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
0084                 &rom->device, &rom->function);
0085 
0086     if (status != EFI_SUCCESS)
0087         goto free_struct;
0088 
0089     memcpy(rom->romdata, romimage, romsize);
0090     return status;
0091 
0092 free_struct:
0093     efi_bs_call(free_pool, rom);
0094     return status;
0095 }
0096 
0097 /*
0098  * There's no way to return an informative status from this function,
0099  * because any analysis (and printing of error messages) needs to be
0100  * done directly at the EFI function call-site.
0101  *
0102  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
0103  * just didn't find any PCI devices, but there's no way to tell outside
0104  * the context of the call.
0105  */
0106 static void setup_efi_pci(struct boot_params *params)
0107 {
0108     efi_status_t status;
0109     void **pci_handle = NULL;
0110     efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
0111     unsigned long size = 0;
0112     struct setup_data *data;
0113     efi_handle_t h;
0114     int i;
0115 
0116     status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
0117                  &pci_proto, NULL, &size, pci_handle);
0118 
0119     if (status == EFI_BUFFER_TOO_SMALL) {
0120         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
0121                      (void **)&pci_handle);
0122 
0123         if (status != EFI_SUCCESS) {
0124             efi_err("Failed to allocate memory for 'pci_handle'\n");
0125             return;
0126         }
0127 
0128         status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
0129                      &pci_proto, NULL, &size, pci_handle);
0130     }
0131 
0132     if (status != EFI_SUCCESS)
0133         goto free_handle;
0134 
0135     data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
0136 
0137     while (data && data->next)
0138         data = (struct setup_data *)(unsigned long)data->next;
0139 
0140     for_each_efi_handle(h, pci_handle, size, i) {
0141         efi_pci_io_protocol_t *pci = NULL;
0142         struct pci_setup_rom *rom;
0143 
0144         status = efi_bs_call(handle_protocol, h, &pci_proto,
0145                      (void **)&pci);
0146         if (status != EFI_SUCCESS || !pci)
0147             continue;
0148 
0149         status = preserve_pci_rom_image(pci, &rom);
0150         if (status != EFI_SUCCESS)
0151             continue;
0152 
0153         if (data)
0154             data->next = (unsigned long)rom;
0155         else
0156             params->hdr.setup_data = (unsigned long)rom;
0157 
0158         data = (struct setup_data *)rom;
0159     }
0160 
0161 free_handle:
0162     efi_bs_call(free_pool, pci_handle);
0163 }
0164 
0165 static void retrieve_apple_device_properties(struct boot_params *boot_params)
0166 {
0167     efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
0168     struct setup_data *data, *new;
0169     efi_status_t status;
0170     u32 size = 0;
0171     apple_properties_protocol_t *p;
0172 
0173     status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
0174     if (status != EFI_SUCCESS)
0175         return;
0176 
0177     if (efi_table_attr(p, version) != 0x10000) {
0178         efi_err("Unsupported properties proto version\n");
0179         return;
0180     }
0181 
0182     efi_call_proto(p, get_all, NULL, &size);
0183     if (!size)
0184         return;
0185 
0186     do {
0187         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
0188                      size + sizeof(struct setup_data),
0189                      (void **)&new);
0190         if (status != EFI_SUCCESS) {
0191             efi_err("Failed to allocate memory for 'properties'\n");
0192             return;
0193         }
0194 
0195         status = efi_call_proto(p, get_all, new->data, &size);
0196 
0197         if (status == EFI_BUFFER_TOO_SMALL)
0198             efi_bs_call(free_pool, new);
0199     } while (status == EFI_BUFFER_TOO_SMALL);
0200 
0201     new->type = SETUP_APPLE_PROPERTIES;
0202     new->len  = size;
0203     new->next = 0;
0204 
0205     data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
0206     if (!data) {
0207         boot_params->hdr.setup_data = (unsigned long)new;
0208     } else {
0209         while (data->next)
0210             data = (struct setup_data *)(unsigned long)data->next;
0211         data->next = (unsigned long)new;
0212     }
0213 }
0214 
0215 static void
0216 adjust_memory_range_protection(unsigned long start, unsigned long size)
0217 {
0218     efi_status_t status;
0219     efi_gcd_memory_space_desc_t desc;
0220     unsigned long end, next;
0221     unsigned long rounded_start, rounded_end;
0222     unsigned long unprotect_start, unprotect_size;
0223 
0224     if (efi_dxe_table == NULL)
0225         return;
0226 
0227     rounded_start = rounddown(start, EFI_PAGE_SIZE);
0228     rounded_end = roundup(start + size, EFI_PAGE_SIZE);
0229 
0230     /*
0231      * Don't modify memory region attributes, they are
0232      * already suitable, to lower the possibility to
0233      * encounter firmware bugs.
0234      */
0235 
0236     for (end = start + size; start < end; start = next) {
0237 
0238         status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
0239 
0240         if (status != EFI_SUCCESS)
0241             return;
0242 
0243         next = desc.base_address + desc.length;
0244 
0245         /*
0246          * Only system memory is suitable for trampoline/kernel image placement,
0247          * so only this type of memory needs its attributes to be modified.
0248          */
0249 
0250         if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
0251             (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
0252             continue;
0253 
0254         unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
0255         unprotect_size = min(rounded_end, next) - unprotect_start;
0256 
0257         status = efi_dxe_call(set_memory_space_attributes,
0258                       unprotect_start, unprotect_size,
0259                       EFI_MEMORY_WB);
0260 
0261         if (status != EFI_SUCCESS) {
0262             efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
0263                  unprotect_start,
0264                  unprotect_start + unprotect_size,
0265                  status);
0266         }
0267     }
0268 }
0269 
0270 /*
0271  * Trampoline takes 2 pages and can be loaded in first megabyte of memory
0272  * with its end placed between 128k and 640k where BIOS might start.
0273  * (see arch/x86/boot/compressed/pgtable_64.c)
0274  *
0275  * We cannot find exact trampoline placement since memory map
0276  * can be modified by UEFI, and it can alter the computed address.
0277  */
0278 
0279 #define TRAMPOLINE_PLACEMENT_BASE ((128 - 8)*1024)
0280 #define TRAMPOLINE_PLACEMENT_SIZE (640*1024 - (128 - 8)*1024)
0281 
0282 void startup_32(struct boot_params *boot_params);
0283 
0284 static void
0285 setup_memory_protection(unsigned long image_base, unsigned long image_size)
0286 {
0287     /*
0288      * Allow execution of possible trampoline used
0289      * for switching between 4- and 5-level page tables
0290      * and relocated kernel image.
0291      */
0292 
0293     adjust_memory_range_protection(TRAMPOLINE_PLACEMENT_BASE,
0294                        TRAMPOLINE_PLACEMENT_SIZE);
0295 
0296 #ifdef CONFIG_64BIT
0297     if (image_base != (unsigned long)startup_32)
0298         adjust_memory_range_protection(image_base, image_size);
0299 #else
0300     /*
0301      * Clear protection flags on a whole range of possible
0302      * addresses used for KASLR. We don't need to do that
0303      * on x86_64, since KASLR/extraction is performed after
0304      * dedicated identity page tables are built and we only
0305      * need to remove possible protection on relocated image
0306      * itself disregarding further relocations.
0307      */
0308     adjust_memory_range_protection(LOAD_PHYSICAL_ADDR,
0309                        KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR);
0310 #endif
0311 }
0312 
0313 static const efi_char16_t apple[] = L"Apple";
0314 
0315 static void setup_quirks(struct boot_params *boot_params,
0316              unsigned long image_base,
0317              unsigned long image_size)
0318 {
0319     efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
0320         efi_table_attr(efi_system_table, fw_vendor);
0321 
0322     if (!memcmp(fw_vendor, apple, sizeof(apple))) {
0323         if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
0324             retrieve_apple_device_properties(boot_params);
0325     }
0326 
0327     if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES))
0328         setup_memory_protection(image_base, image_size);
0329 }
0330 
0331 /*
0332  * See if we have Universal Graphics Adapter (UGA) protocol
0333  */
0334 static efi_status_t
0335 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
0336 {
0337     efi_status_t status;
0338     u32 width, height;
0339     void **uga_handle = NULL;
0340     efi_uga_draw_protocol_t *uga = NULL, *first_uga;
0341     efi_handle_t handle;
0342     int i;
0343 
0344     status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
0345                  (void **)&uga_handle);
0346     if (status != EFI_SUCCESS)
0347         return status;
0348 
0349     status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
0350                  uga_proto, NULL, &size, uga_handle);
0351     if (status != EFI_SUCCESS)
0352         goto free_handle;
0353 
0354     height = 0;
0355     width = 0;
0356 
0357     first_uga = NULL;
0358     for_each_efi_handle(handle, uga_handle, size, i) {
0359         efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
0360         u32 w, h, depth, refresh;
0361         void *pciio;
0362 
0363         status = efi_bs_call(handle_protocol, handle, uga_proto,
0364                      (void **)&uga);
0365         if (status != EFI_SUCCESS)
0366             continue;
0367 
0368         pciio = NULL;
0369         efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
0370 
0371         status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
0372         if (status == EFI_SUCCESS && (!first_uga || pciio)) {
0373             width = w;
0374             height = h;
0375 
0376             /*
0377              * Once we've found a UGA supporting PCIIO,
0378              * don't bother looking any further.
0379              */
0380             if (pciio)
0381                 break;
0382 
0383             first_uga = uga;
0384         }
0385     }
0386 
0387     if (!width && !height)
0388         goto free_handle;
0389 
0390     /* EFI framebuffer */
0391     si->orig_video_isVGA    = VIDEO_TYPE_EFI;
0392 
0393     si->lfb_depth       = 32;
0394     si->lfb_width       = width;
0395     si->lfb_height      = height;
0396 
0397     si->red_size        = 8;
0398     si->red_pos     = 16;
0399     si->green_size      = 8;
0400     si->green_pos       = 8;
0401     si->blue_size       = 8;
0402     si->blue_pos        = 0;
0403     si->rsvd_size       = 8;
0404     si->rsvd_pos        = 24;
0405 
0406 free_handle:
0407     efi_bs_call(free_pool, uga_handle);
0408 
0409     return status;
0410 }
0411 
0412 static void setup_graphics(struct boot_params *boot_params)
0413 {
0414     efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
0415     struct screen_info *si;
0416     efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
0417     efi_status_t status;
0418     unsigned long size;
0419     void **gop_handle = NULL;
0420     void **uga_handle = NULL;
0421 
0422     si = &boot_params->screen_info;
0423     memset(si, 0, sizeof(*si));
0424 
0425     size = 0;
0426     status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
0427                  &graphics_proto, NULL, &size, gop_handle);
0428     if (status == EFI_BUFFER_TOO_SMALL)
0429         status = efi_setup_gop(si, &graphics_proto, size);
0430 
0431     if (status != EFI_SUCCESS) {
0432         size = 0;
0433         status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
0434                      &uga_proto, NULL, &size, uga_handle);
0435         if (status == EFI_BUFFER_TOO_SMALL)
0436             setup_uga(si, &uga_proto, size);
0437     }
0438 }
0439 
0440 
0441 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
0442 {
0443     efi_bs_call(exit, handle, status, 0, NULL);
0444     for(;;)
0445         asm("hlt");
0446 }
0447 
0448 void __noreturn efi_stub_entry(efi_handle_t handle,
0449                    efi_system_table_t *sys_table_arg,
0450                    struct boot_params *boot_params);
0451 
0452 /*
0453  * Because the x86 boot code expects to be passed a boot_params we
0454  * need to create one ourselves (usually the bootloader would create
0455  * one for us).
0456  */
0457 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
0458                    efi_system_table_t *sys_table_arg)
0459 {
0460     struct boot_params *boot_params;
0461     struct setup_header *hdr;
0462     void *image_base;
0463     efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
0464     int options_size = 0;
0465     efi_status_t status;
0466     char *cmdline_ptr;
0467 
0468     efi_system_table = sys_table_arg;
0469 
0470     /* Check if we were booted by the EFI firmware */
0471     if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
0472         efi_exit(handle, EFI_INVALID_PARAMETER);
0473 
0474     status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
0475     if (status != EFI_SUCCESS) {
0476         efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
0477         efi_exit(handle, status);
0478     }
0479 
0480     image_base = efi_table_attr(image, image_base);
0481     image_offset = (void *)startup_32 - image_base;
0482 
0483     status = efi_allocate_pages(sizeof(struct boot_params),
0484                     (unsigned long *)&boot_params, ULONG_MAX);
0485     if (status != EFI_SUCCESS) {
0486         efi_err("Failed to allocate lowmem for boot params\n");
0487         efi_exit(handle, status);
0488     }
0489 
0490     memset(boot_params, 0x0, sizeof(struct boot_params));
0491 
0492     hdr = &boot_params->hdr;
0493 
0494     /* Copy the setup header from the second sector to boot_params */
0495     memcpy(&hdr->jump, image_base + 512,
0496            sizeof(struct setup_header) - offsetof(struct setup_header, jump));
0497 
0498     /*
0499      * Fill out some of the header fields ourselves because the
0500      * EFI firmware loader doesn't load the first sector.
0501      */
0502     hdr->root_flags = 1;
0503     hdr->vid_mode   = 0xffff;
0504     hdr->boot_flag  = 0xAA55;
0505 
0506     hdr->type_of_loader = 0x21;
0507 
0508     /* Convert unicode cmdline to ascii */
0509     cmdline_ptr = efi_convert_cmdline(image, &options_size);
0510     if (!cmdline_ptr)
0511         goto fail;
0512 
0513     efi_set_u64_split((unsigned long)cmdline_ptr,
0514               &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
0515 
0516     hdr->ramdisk_image = 0;
0517     hdr->ramdisk_size = 0;
0518 
0519     /*
0520      * Disregard any setup data that was provided by the bootloader:
0521      * setup_data could be pointing anywhere, and we have no way of
0522      * authenticating or validating the payload.
0523      */
0524     hdr->setup_data = 0;
0525 
0526     efi_stub_entry(handle, sys_table_arg, boot_params);
0527     /* not reached */
0528 
0529 fail:
0530     efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
0531 
0532     efi_exit(handle, status);
0533 }
0534 
0535 static void add_e820ext(struct boot_params *params,
0536             struct setup_data *e820ext, u32 nr_entries)
0537 {
0538     struct setup_data *data;
0539 
0540     e820ext->type = SETUP_E820_EXT;
0541     e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
0542     e820ext->next = 0;
0543 
0544     data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
0545 
0546     while (data && data->next)
0547         data = (struct setup_data *)(unsigned long)data->next;
0548 
0549     if (data)
0550         data->next = (unsigned long)e820ext;
0551     else
0552         params->hdr.setup_data = (unsigned long)e820ext;
0553 }
0554 
0555 static efi_status_t
0556 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
0557 {
0558     struct boot_e820_entry *entry = params->e820_table;
0559     struct efi_info *efi = &params->efi_info;
0560     struct boot_e820_entry *prev = NULL;
0561     u32 nr_entries;
0562     u32 nr_desc;
0563     int i;
0564 
0565     nr_entries = 0;
0566     nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
0567 
0568     for (i = 0; i < nr_desc; i++) {
0569         efi_memory_desc_t *d;
0570         unsigned int e820_type = 0;
0571         unsigned long m = efi->efi_memmap;
0572 
0573 #ifdef CONFIG_X86_64
0574         m |= (u64)efi->efi_memmap_hi << 32;
0575 #endif
0576 
0577         d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
0578         switch (d->type) {
0579         case EFI_RESERVED_TYPE:
0580         case EFI_RUNTIME_SERVICES_CODE:
0581         case EFI_RUNTIME_SERVICES_DATA:
0582         case EFI_MEMORY_MAPPED_IO:
0583         case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
0584         case EFI_PAL_CODE:
0585             e820_type = E820_TYPE_RESERVED;
0586             break;
0587 
0588         case EFI_UNUSABLE_MEMORY:
0589             e820_type = E820_TYPE_UNUSABLE;
0590             break;
0591 
0592         case EFI_ACPI_RECLAIM_MEMORY:
0593             e820_type = E820_TYPE_ACPI;
0594             break;
0595 
0596         case EFI_LOADER_CODE:
0597         case EFI_LOADER_DATA:
0598         case EFI_BOOT_SERVICES_CODE:
0599         case EFI_BOOT_SERVICES_DATA:
0600         case EFI_CONVENTIONAL_MEMORY:
0601             if (efi_soft_reserve_enabled() &&
0602                 (d->attribute & EFI_MEMORY_SP))
0603                 e820_type = E820_TYPE_SOFT_RESERVED;
0604             else
0605                 e820_type = E820_TYPE_RAM;
0606             break;
0607 
0608         case EFI_ACPI_MEMORY_NVS:
0609             e820_type = E820_TYPE_NVS;
0610             break;
0611 
0612         case EFI_PERSISTENT_MEMORY:
0613             e820_type = E820_TYPE_PMEM;
0614             break;
0615 
0616         default:
0617             continue;
0618         }
0619 
0620         /* Merge adjacent mappings */
0621         if (prev && prev->type == e820_type &&
0622             (prev->addr + prev->size) == d->phys_addr) {
0623             prev->size += d->num_pages << 12;
0624             continue;
0625         }
0626 
0627         if (nr_entries == ARRAY_SIZE(params->e820_table)) {
0628             u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
0629                    sizeof(struct setup_data);
0630 
0631             if (!e820ext || e820ext_size < need)
0632                 return EFI_BUFFER_TOO_SMALL;
0633 
0634             /* boot_params map full, switch to e820 extended */
0635             entry = (struct boot_e820_entry *)e820ext->data;
0636         }
0637 
0638         entry->addr = d->phys_addr;
0639         entry->size = d->num_pages << PAGE_SHIFT;
0640         entry->type = e820_type;
0641         prev = entry++;
0642         nr_entries++;
0643     }
0644 
0645     if (nr_entries > ARRAY_SIZE(params->e820_table)) {
0646         u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
0647 
0648         add_e820ext(params, e820ext, nr_e820ext);
0649         nr_entries -= nr_e820ext;
0650     }
0651 
0652     params->e820_entries = (u8)nr_entries;
0653 
0654     return EFI_SUCCESS;
0655 }
0656 
0657 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
0658                   u32 *e820ext_size)
0659 {
0660     efi_status_t status;
0661     unsigned long size;
0662 
0663     size = sizeof(struct setup_data) +
0664         sizeof(struct e820_entry) * nr_desc;
0665 
0666     if (*e820ext) {
0667         efi_bs_call(free_pool, *e820ext);
0668         *e820ext = NULL;
0669         *e820ext_size = 0;
0670     }
0671 
0672     status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
0673                  (void **)e820ext);
0674     if (status == EFI_SUCCESS)
0675         *e820ext_size = size;
0676 
0677     return status;
0678 }
0679 
0680 static efi_status_t allocate_e820(struct boot_params *params,
0681                   struct setup_data **e820ext,
0682                   u32 *e820ext_size)
0683 {
0684     unsigned long map_size, desc_size, map_key;
0685     efi_status_t status;
0686     __u32 nr_desc, desc_version;
0687 
0688     /* Only need the size of the mem map and size of each mem descriptor */
0689     map_size = 0;
0690     status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
0691                  &desc_size, &desc_version);
0692     if (status != EFI_BUFFER_TOO_SMALL)
0693         return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
0694 
0695     nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
0696 
0697     if (nr_desc > ARRAY_SIZE(params->e820_table)) {
0698         u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
0699 
0700         status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
0701         if (status != EFI_SUCCESS)
0702             return status;
0703     }
0704 
0705     return EFI_SUCCESS;
0706 }
0707 
0708 struct exit_boot_struct {
0709     struct boot_params  *boot_params;
0710     struct efi_info     *efi;
0711 };
0712 
0713 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
0714                    void *priv)
0715 {
0716     const char *signature;
0717     struct exit_boot_struct *p = priv;
0718 
0719     signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
0720                    : EFI32_LOADER_SIGNATURE;
0721     memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
0722 
0723     efi_set_u64_split((unsigned long)efi_system_table,
0724               &p->efi->efi_systab, &p->efi->efi_systab_hi);
0725     p->efi->efi_memdesc_size    = *map->desc_size;
0726     p->efi->efi_memdesc_version = *map->desc_ver;
0727     efi_set_u64_split((unsigned long)*map->map,
0728               &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
0729     p->efi->efi_memmap_size     = *map->map_size;
0730 
0731     return EFI_SUCCESS;
0732 }
0733 
0734 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
0735 {
0736     unsigned long map_sz, key, desc_size, buff_size;
0737     efi_memory_desc_t *mem_map;
0738     struct setup_data *e820ext = NULL;
0739     __u32 e820ext_size = 0;
0740     efi_status_t status;
0741     __u32 desc_version;
0742     struct efi_boot_memmap map;
0743     struct exit_boot_struct priv;
0744 
0745     map.map         = &mem_map;
0746     map.map_size        = &map_sz;
0747     map.desc_size       = &desc_size;
0748     map.desc_ver        = &desc_version;
0749     map.key_ptr     = &key;
0750     map.buff_size       = &buff_size;
0751     priv.boot_params    = boot_params;
0752     priv.efi        = &boot_params->efi_info;
0753 
0754     status = allocate_e820(boot_params, &e820ext, &e820ext_size);
0755     if (status != EFI_SUCCESS)
0756         return status;
0757 
0758     /* Might as well exit boot services now */
0759     status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
0760     if (status != EFI_SUCCESS)
0761         return status;
0762 
0763     /* Historic? */
0764     boot_params->alt_mem_k  = 32 * 1024;
0765 
0766     status = setup_e820(boot_params, e820ext, e820ext_size);
0767     if (status != EFI_SUCCESS)
0768         return status;
0769 
0770     return EFI_SUCCESS;
0771 }
0772 
0773 /*
0774  * On success, we return the address of startup_32, which has potentially been
0775  * relocated by efi_relocate_kernel.
0776  * On failure, we exit to the firmware via efi_exit instead of returning.
0777  */
0778 unsigned long efi_main(efi_handle_t handle,
0779                  efi_system_table_t *sys_table_arg,
0780                  struct boot_params *boot_params)
0781 {
0782     unsigned long bzimage_addr = (unsigned long)startup_32;
0783     unsigned long buffer_start, buffer_end;
0784     struct setup_header *hdr = &boot_params->hdr;
0785     unsigned long addr, size;
0786     efi_status_t status;
0787 
0788     efi_system_table = sys_table_arg;
0789     /* Check if we were booted by the EFI firmware */
0790     if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
0791         efi_exit(handle, EFI_INVALID_PARAMETER);
0792 
0793     efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
0794     if (efi_dxe_table &&
0795         efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
0796         efi_warn("Ignoring DXE services table: invalid signature\n");
0797         efi_dxe_table = NULL;
0798     }
0799 
0800     /*
0801      * If the kernel isn't already loaded at a suitable address,
0802      * relocate it.
0803      *
0804      * It must be loaded above LOAD_PHYSICAL_ADDR.
0805      *
0806      * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
0807      * is defined as the macro MAXMEM, but unfortunately that is not a
0808      * compile-time constant if 5-level paging is configured, so we instead
0809      * define our own macro for use here.
0810      *
0811      * For 32-bit, the maximum address is complicated to figure out, for
0812      * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
0813      * KASLR uses.
0814      *
0815      * Also relocate it if image_offset is zero, i.e. the kernel wasn't
0816      * loaded by LoadImage, but rather by a bootloader that called the
0817      * handover entry. The reason we must always relocate in this case is
0818      * to handle the case of systemd-boot booting a unified kernel image,
0819      * which is a PE executable that contains the bzImage and an initrd as
0820      * COFF sections. The initrd section is placed after the bzImage
0821      * without ensuring that there are at least init_size bytes available
0822      * for the bzImage, and thus the compressed kernel's startup code may
0823      * overwrite the initrd unless it is moved out of the way.
0824      */
0825 
0826     buffer_start = ALIGN(bzimage_addr - image_offset,
0827                  hdr->kernel_alignment);
0828     buffer_end = buffer_start + hdr->init_size;
0829 
0830     if ((buffer_start < LOAD_PHYSICAL_ADDR)                  ||
0831         (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE)    ||
0832         (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
0833         (image_offset == 0)) {
0834         extern char _bss[];
0835 
0836         status = efi_relocate_kernel(&bzimage_addr,
0837                          (unsigned long)_bss - bzimage_addr,
0838                          hdr->init_size,
0839                          hdr->pref_address,
0840                          hdr->kernel_alignment,
0841                          LOAD_PHYSICAL_ADDR);
0842         if (status != EFI_SUCCESS) {
0843             efi_err("efi_relocate_kernel() failed!\n");
0844             goto fail;
0845         }
0846         /*
0847          * Now that we've copied the kernel elsewhere, we no longer
0848          * have a set up block before startup_32(), so reset image_offset
0849          * to zero in case it was set earlier.
0850          */
0851         image_offset = 0;
0852     }
0853 
0854 #ifdef CONFIG_CMDLINE_BOOL
0855     status = efi_parse_options(CONFIG_CMDLINE);
0856     if (status != EFI_SUCCESS) {
0857         efi_err("Failed to parse options\n");
0858         goto fail;
0859     }
0860 #endif
0861     if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
0862         unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
0863                            ((u64)boot_params->ext_cmd_line_ptr << 32));
0864         status = efi_parse_options((char *)cmdline_paddr);
0865         if (status != EFI_SUCCESS) {
0866             efi_err("Failed to parse options\n");
0867             goto fail;
0868         }
0869     }
0870 
0871     /*
0872      * At this point, an initrd may already have been loaded by the
0873      * bootloader and passed via bootparams. We permit an initrd loaded
0874      * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
0875      *
0876      * If the device path is not present, any command-line initrd=
0877      * arguments will be processed only if image is not NULL, which will be
0878      * the case only if we were loaded via the PE entry point.
0879      */
0880     status = efi_load_initrd(image, &addr, &size, hdr->initrd_addr_max,
0881                  ULONG_MAX);
0882     if (status != EFI_SUCCESS)
0883         goto fail;
0884     if (size > 0) {
0885         efi_set_u64_split(addr, &hdr->ramdisk_image,
0886                   &boot_params->ext_ramdisk_image);
0887         efi_set_u64_split(size, &hdr->ramdisk_size,
0888                   &boot_params->ext_ramdisk_size);
0889     }
0890 
0891     /*
0892      * If the boot loader gave us a value for secure_boot then we use that,
0893      * otherwise we ask the BIOS.
0894      */
0895     if (boot_params->secure_boot == efi_secureboot_mode_unset)
0896         boot_params->secure_boot = efi_get_secureboot();
0897 
0898     /* Ask the firmware to clear memory on unclean shutdown */
0899     efi_enable_reset_attack_mitigation();
0900 
0901     efi_random_get_seed();
0902 
0903     efi_retrieve_tpm2_eventlog();
0904 
0905     setup_graphics(boot_params);
0906 
0907     setup_efi_pci(boot_params);
0908 
0909     setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
0910 
0911     status = exit_boot(boot_params, handle);
0912     if (status != EFI_SUCCESS) {
0913         efi_err("exit_boot() failed!\n");
0914         goto fail;
0915     }
0916 
0917     return bzimage_addr;
0918 fail:
0919     efi_err("efi_main() failed!\n");
0920 
0921     efi_exit(handle, status);
0922 }