root/arch/x86/boot/compressed/eboot.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __efi_early
  2. efi_char16_printk
  3. preserve_pci_rom_image
  4. setup_efi_pci
  5. retrieve_apple_device_properties
  6. setup_quirks
  7. setup_uga
  8. setup_graphics
  9. make_boot_params
  10. add_e820ext
  11. setup_e820
  12. alloc_e820ext
  13. allocate_e820
  14. exit_boot_func
  15. exit_boot
  16. efi_main

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

/* [<][>][^][v][top][bottom][index][help] */