diff options
author | Shea Levy <shea@shealevy.com> | 2011-10-01 23:00:48 +0000 |
---|---|---|
committer | Shea Levy <shea@shealevy.com> | 2011-10-01 23:00:48 +0000 |
commit | 72f41379be9d2b37b1078ca4ab87207a054c5662 (patch) | |
tree | 9aea6493792ec61d1ad4876d563ae009dfaa320c /pkgs | |
parent | 4c3642403ce2909bd2aa2508776852d96376a780 (diff) |
Add a patch to enable adding efi stub code to the linux kernel
svn path=/nixpkgs/trunk/; revision=29555
Diffstat (limited to 'pkgs')
-rw-r--r-- | pkgs/os-specific/linux/kernel/efi-stub.patch | 1778 | ||||
-rw-r--r-- | pkgs/os-specific/linux/kernel/patches.nix | 9 |
2 files changed, 1787 insertions, 0 deletions
diff --git a/pkgs/os-specific/linux/kernel/efi-stub.patch b/pkgs/os-specific/linux/kernel/efi-stub.patch new file mode 100644 index 000000000000..581a9fff27f3 --- /dev/null +++ b/pkgs/os-specific/linux/kernel/efi-stub.patch @@ -0,0 +1,1778 @@ +diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig +index 6a47bb2..d40c876 100644 +--- a/arch/x86/Kconfig ++++ b/arch/x86/Kconfig +@@ -1466,6 +1466,13 @@ config EFI + resultant kernel should continue to boot on existing non-EFI + platforms. + ++config EFI_STUB ++ bool "EFI stub support" ++ depends on EFI ++ ---help--- ++ This kernel feature allows a bzImage to be loaded directly ++ by EFI firmware without the use of a bootloader. ++ + config SECCOMP + def_bool y + prompt "Enable seccomp to safely compute untrusted bytecode" +diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile +index 09664ef..b123b9a 100644 +--- a/arch/x86/boot/compressed/Makefile ++++ b/arch/x86/boot/compressed/Makefile +@@ -23,7 +23,15 @@ LDFLAGS_vmlinux := -T + + hostprogs-y := mkpiggy + +-$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o $(obj)/string.o $(obj)/cmdline.o $(obj)/early_serial_console.o $(obj)/piggy.o FORCE ++VMLINUX_OBJS = $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o \ ++ $(obj)/string.o $(obj)/cmdline.o $(obj)/early_serial_console.o \ ++ $(obj)/piggy.o ++ ++ifeq ($(CONFIG_EFI_STUB), y) ++ VMLINUX_OBJS += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o ++endif ++ ++$(obj)/vmlinux: $(VMLINUX_OBJS) FORCE + $(call if_changed,ld) + @: + +diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c +new file mode 100644 +index 0000000..c1b494d +--- /dev/null ++++ b/arch/x86/boot/compressed/eboot.c +@@ -0,0 +1,975 @@ ++/* ----------------------------------------------------------------------- ++ * ++ * Copyright 2011 Intel Corporation; author Matt Fleming ++ * ++ * This file is part of the Linux kernel, and is made available under ++ * the terms of the GNU General Public License version 2. ++ * ++ * ----------------------------------------------------------------------- */ ++ ++#include <linux/efi.h> ++#include <asm/efi.h> ++#include <asm/setup.h> ++#include <asm/desc.h> ++ ++#define SEG_TYPE_DATA (0 << 3) ++#define SEG_TYPE_READ_WRITE (1 << 1) ++#define SEG_TYPE_CODE (1 << 3) ++#define SEG_TYPE_EXEC_READ (1 << 1) ++#define SEG_TYPE_TSS ((1 << 3) | (1 << 0)) ++#define SEG_OP_SIZE_32BIT (1 << 0) ++#define SEG_GRANULARITY_4KB (1 << 0) ++ ++#define DESC_TYPE_CODE_DATA (1 << 0) ++ ++#define EFI_PAGE_SIZE (1UL << EFI_PAGE_SHIFT) ++ ++#define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0 ++#define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1 ++#define PIXEL_BIT_MASK 2 ++#define PIXEL_BLT_ONLY 3 ++#define PIXEL_FORMAT_MAX 4 ++ ++typedef struct { ++ u32 red_mask; ++ u32 green_mask; ++ u32 blue_mask; ++ u32 reserved_mask; ++} efi_pixel_bitmask_t; ++ ++typedef struct { ++ u32 version; ++ u32 horizontal_resolution; ++ u32 vertical_resolution; ++ int pixel_format; ++ efi_pixel_bitmask_t pixel_information; ++ u32 pixels_per_scan_line; ++} __attribute__((packed)) efi_graphics_output_mode_information_t; ++ ++typedef struct { ++ u32 max_mode; ++ u32 mode; ++ unsigned long info; ++ unsigned long size_of_info; ++ u64 frame_buffer_base; ++ unsigned long frame_buffer_size; ++} __attribute__((packed)) efi_graphics_output_protocol_mode_t; ++ ++typedef struct { ++ void *query_mode; ++ unsigned long set_mode; ++ unsigned long blt; ++ efi_graphics_output_protocol_mode_t *mode; ++} efi_graphics_output_protocol_t; ++ ++typedef struct { ++ void *get_mode; ++ void *set_mode; ++ void *blt; ++} efi_uga_draw_protocol_t; ++ ++static efi_system_table_t *sys_table; ++ ++static efi_status_t low_alloc(unsigned long size, unsigned long align, ++ unsigned long *addr) ++{ ++ unsigned long map_size, key, desc_size; ++ efi_memory_desc_t *map; ++ efi_status_t status; ++ unsigned long nr_pages; ++ u32 desc_version; ++ int i; ++ ++ nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; ++ ++ map_size = sizeof(*map) * 32; ++ ++again: ++ /* ++ * Add an additional efi_memory_desc_t because we're doing an ++ * allocation which may be in a new descriptor region. ++ */ ++ map_size += sizeof(*map); ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, map_size, (void **)&map); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ status = efi_call_phys5(sys_table->boottime->get_memory_map, &map_size, ++ map, &key, &desc_size, &desc_version); ++ if (status == EFI_BUFFER_TOO_SMALL) { ++ efi_call_phys1(sys_table->boottime->free_pool, map); ++ goto again; ++ } ++ ++ if (status != EFI_SUCCESS) ++ goto free_pool; ++ ++ for (i = 0; i < map_size / desc_size; i++) { ++ efi_memory_desc_t *desc; ++ u64 start, end; ++ ++ desc = (efi_memory_desc_t *)((unsigned long)map + (i * desc_size)); ++ ++ if (desc->type != EFI_CONVENTIONAL_MEMORY) ++ continue; ++ ++ if (desc->num_pages < nr_pages) ++ continue; ++ ++ start = desc->phys_addr; ++ end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT); ++ ++ /* ++ * Don't allocate at 0x0. It will confuse code that ++ * checks pointers against NULL. Skip the first 8 ++ * bytes so we start at a nice even number. ++ */ ++ if (start == 0x0) { ++ start += 8; ++ ++ /* Check for tiny memory regions */ ++ if (start >= end) ++ continue; ++ } ++ ++ start = round_up(start, align); ++ if ((start + size) > end) ++ continue; ++ ++ status = efi_call_phys4(sys_table->boottime->allocate_pages, ++ EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, ++ nr_pages, &start); ++ if (status == EFI_SUCCESS) { ++ *addr = start; ++ break; ++ } ++ } ++ ++ if (i == map_size / desc_size) ++ status = EFI_NOT_FOUND; ++ ++free_pool: ++ efi_call_phys1(sys_table->boottime->free_pool, map); ++fail: ++ return status; ++} ++ ++static void low_free(unsigned long size, unsigned long addr) ++{ ++ unsigned long nr_pages; ++ ++ nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; ++ efi_call_phys2(sys_table->boottime->free_pages, addr, size); ++} ++ ++static void find_bits(unsigned long mask, u8 *pos, u8 *size) ++{ ++ u8 first, len; ++ ++ first = 0; ++ len = 0; ++ ++ if (mask) { ++ while (!(mask & 0x1)) { ++ mask = mask >> 1; ++ first++; ++ } ++ ++ while (mask & 0x1) { ++ mask = mask >> 1; ++ len++; ++ } ++ } ++ ++ *pos = first; ++ *size = len; ++} ++ ++/* ++ * See if we have Graphics Output Protocol ++ */ ++static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto, ++ unsigned long size) ++{ ++ efi_graphics_output_protocol_t *gop, *first_gop; ++ efi_pixel_bitmask_t pixel_info; ++ unsigned long nr_gops; ++ efi_status_t status; ++ void **gop_handle; ++ u16 width, height; ++ u32 fb_base, fb_size; ++ u32 pixels_per_scan_line; ++ int pixel_format; ++ int i; ++ ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, size, &gop_handle); ++ if (status != EFI_SUCCESS) ++ return status; ++ ++ status = efi_call_phys5(sys_table->boottime->locate_handle, ++ EFI_LOCATE_BY_PROTOCOL, proto, ++ NULL, &size, gop_handle); ++ if (status != EFI_SUCCESS) ++ goto free_handle; ++ ++ first_gop = NULL; ++ ++ nr_gops = size / sizeof(void *); ++ for (i = 0; i < nr_gops; i++) { ++ efi_graphics_output_mode_information_t *info; ++ efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; ++ void *pciio; ++ void *h = gop_handle[i]; ++ ++ status = efi_call_phys3(sys_table->boottime->handle_protocol, ++ h, proto, &gop); ++ if (status != EFI_SUCCESS) ++ continue; ++ ++ efi_call_phys3(sys_table->boottime->handle_protocol, ++ h, &pciio_proto, &pciio); ++ ++ status = efi_call_phys4(gop->query_mode, gop, ++ gop->mode->mode, &size, &info); ++ if (status == EFI_SUCCESS && (!first_gop || pciio)) { ++ /* ++ * Apple provide GOPs that are not backed by ++ * real hardware (they're used to handle ++ * multiple displays). The workaround is to ++ * search for a GOP implementing the PCIIO ++ * protocol, and if one isn't found, to just ++ * fallback to the first GOP. ++ */ ++ width = info->horizontal_resolution; ++ height = info->vertical_resolution; ++ fb_base = gop->mode->frame_buffer_base; ++ fb_size = gop->mode->frame_buffer_size; ++ pixel_format = info->pixel_format; ++ pixel_info = info->pixel_information; ++ pixels_per_scan_line = info->pixels_per_scan_line; ++ ++ /* ++ * Once we've found a GOP supporting PCIIO, ++ * don't bother looking any further. ++ */ ++ if (pciio) ++ break; ++ ++ first_gop = gop; ++ } ++ } ++ ++ /* Did we find any GOPs? */ ++ if (!first_gop) ++ goto free_handle; ++ ++ /* EFI framebuffer */ ++ si->orig_video_isVGA = VIDEO_TYPE_EFI; ++ ++ si->lfb_width = width; ++ si->lfb_height = height; ++ si->lfb_base = fb_base; ++ si->lfb_size = fb_size; ++ si->pages = 1; ++ ++ if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { ++ si->lfb_depth = 32; ++ si->lfb_linelength = pixels_per_scan_line * 4; ++ si->red_size = 8; ++ si->red_pos = 0; ++ si->green_size = 8; ++ si->green_pos = 8; ++ si->blue_size = 8; ++ si->blue_pos = 16; ++ si->rsvd_size = 8; ++ si->rsvd_pos = 24; ++ } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { ++ si->lfb_depth = 32; ++ si->lfb_linelength = pixels_per_scan_line * 4; ++ si->red_size = 8; ++ si->red_pos = 16; ++ si->green_size = 8; ++ si->green_pos = 8; ++ si->blue_size = 8; ++ si->blue_pos = 0; ++ si->rsvd_size = 8; ++ si->rsvd_pos = 24; ++ } else if (pixel_format == PIXEL_BIT_MASK) { ++ find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); ++ find_bits(pixel_info.green_mask, &si->green_pos, ++ &si->green_size); ++ find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); ++ find_bits(pixel_info.reserved_mask, &si->rsvd_pos, ++ &si->rsvd_size); ++ si->lfb_depth = si->red_size + si->green_size + ++ si->blue_size + si->rsvd_size; ++ si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; ++ } else { ++ si->lfb_depth = 4; ++ si->lfb_linelength = si->lfb_width / 2; ++ si->red_size = 0; ++ si->red_pos = 0; ++ si->green_size = 0; ++ si->green_pos = 0; ++ si->blue_size = 0; ++ si->blue_pos = 0; ++ si->rsvd_size = 0; ++ si->rsvd_pos = 0; ++ } ++ ++free_handle: ++ efi_call_phys1(sys_table->boottime->free_pool, gop_handle); ++ return status; ++} ++ ++/* ++ * See if we have Universal Graphics Adapter (UGA) protocol ++ */ ++static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, ++ unsigned long size) ++{ ++ efi_uga_draw_protocol_t *uga, *first_uga; ++ unsigned long nr_ugas; ++ efi_status_t status; ++ u32 width, height; ++ void **uga_handle = NULL; ++ int i; ++ ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, size, &uga_handle); ++ if (status != EFI_SUCCESS) ++ return status; ++ ++ status = efi_call_phys5(sys_table->boottime->locate_handle, ++ EFI_LOCATE_BY_PROTOCOL, uga_proto, ++ NULL, &size, uga_handle); ++ if (status != EFI_SUCCESS) ++ goto free_handle; ++ ++ first_uga = NULL; ++ ++ nr_ugas = size / sizeof(void *); ++ for (i = 0; i < nr_ugas; i++) { ++ efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; ++ void *handle = uga_handle[i]; ++ u32 w, h, depth, refresh; ++ void *pciio; ++ ++ status = efi_call_phys3(sys_table->boottime->handle_protocol, ++ handle, uga_proto, &uga); ++ if (status != EFI_SUCCESS) ++ continue; ++ ++ efi_call_phys3(sys_table->boottime->handle_protocol, ++ handle, &pciio_proto, &pciio); ++ ++ status = efi_call_phys5(uga->get_mode, uga, &w, &h, ++ &depth, &refresh); ++ if (status == EFI_SUCCESS && (!first_uga || pciio)) { ++ width = w; ++ height = h; ++ ++ /* ++ * Once we've found a UGA supporting PCIIO, ++ * don't bother looking any further. ++ */ ++ if (pciio) ++ break; ++ ++ first_uga = uga; ++ } ++ } ++ ++ if (!first_uga) ++ goto free_handle; ++ ++ /* EFI framebuffer */ ++ si->orig_video_isVGA = VIDEO_TYPE_EFI; ++ ++ si->lfb_depth = 32; ++ si->lfb_width = width; ++ si->lfb_height = height; ++ ++ si->red_size = 8; ++ si->red_pos = 16; ++ si->green_size = 8; ++ si->green_pos = 8; ++ si->blue_size = 8; ++ si->blue_pos = 0; ++ si->rsvd_size = 8; ++ si->rsvd_pos = 24; ++ ++ ++free_handle: ++ efi_call_phys1(sys_table->boottime->free_pool, uga_handle); ++ return status; ++} ++ ++void setup_graphics(struct boot_params *boot_params) ++{ ++ efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; ++ struct screen_info *si; ++ efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; ++ efi_status_t status; ++ unsigned long size; ++ void **gop_handle = NULL; ++ void **uga_handle = NULL; ++ ++ si = &boot_params->screen_info; ++ memset(si, 0, sizeof(*si)); ++ ++ size = 0; ++ status = efi_call_phys5(sys_table->boottime->locate_handle, ++ EFI_LOCATE_BY_PROTOCOL, &graphics_proto, ++ NULL, &size, gop_handle); ++ if (status == EFI_BUFFER_TOO_SMALL) ++ status = setup_gop(si, &graphics_proto, size); ++ ++ if (status != EFI_SUCCESS) { ++ size = 0; ++ status = efi_call_phys5(sys_table->boottime->locate_handle, ++ EFI_LOCATE_BY_PROTOCOL, &uga_proto, ++ NULL, &size, uga_handle); ++ if (status == EFI_BUFFER_TOO_SMALL) ++ setup_uga(si, &uga_proto, size); ++ } ++} ++ ++struct initrd { ++ efi_file_handle_t *handle; ++ u64 size; ++}; ++ ++/* ++ * Check the cmdline for a LILO-style initrd= arguments. ++ * ++ * We only support loading an initrd from the same filesystem as the ++ * kernel image. ++ */ ++static efi_status_t handle_ramdisks(efi_loaded_image_t *image, ++ struct setup_header *hdr) ++{ ++ struct initrd *initrds; ++ unsigned long initrd_addr; ++ efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; ++ u64 initrd_total; ++ efi_file_io_interface_t *io; ++ efi_file_handle_t *fh; ++ efi_status_t status; ++ int nr_initrds; ++ char *str; ++ int i, j, k; ++ ++ initrd_addr = 0; ++ initrd_total = 0; ++ ++ str = (char *)(unsigned long)hdr->cmd_line_ptr; ++ ++ j = 0; /* See close_handles */ ++ ++ if (!str || !*str) ++ return EFI_SUCCESS; ++ ++ for (nr_initrds = 0; *str; nr_initrds++) { ++ str = strstr(str, "initrd="); ++ if (!str) ++ break; ++ ++ str += 7; ++ ++ /* Skip any leading slashes */ ++ while (*str == '/' || *str == '\\') ++ str++; ++ ++ while (*str && *str != ' ' && *str != '\n') ++ str++; ++ } ++ ++ if (!nr_initrds) ++ return EFI_SUCCESS; ++ ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, ++ nr_initrds * sizeof(*initrds), ++ &initrds); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ str = (char *)(unsigned long)hdr->cmd_line_ptr; ++ for (i = 0; i < nr_initrds; i++) { ++ struct initrd *initrd; ++ efi_file_handle_t *h; ++ efi_file_info_t *info; ++ efi_char16_t filename[256]; ++ unsigned long info_sz; ++ efi_guid_t info_guid = EFI_FILE_INFO_ID; ++ efi_char16_t *p; ++ u64 file_sz; ++ ++ str = strstr(str, "initrd="); ++ if (!str) ++ break; ++ ++ str += 7; ++ ++ initrd = &initrds[i]; ++ p = filename; ++ ++ /* Skip any leading slashes */ ++ while (*str == '/' || *str == '\\') ++ str++; ++ ++ while (*str && *str != ' ' && *str != '\n') { ++ if (p >= filename + sizeof(filename)) ++ break; ++ ++ *p++ = *str++; ++ } ++ ++ *p = '\0'; ++ ++ /* Only open the volume once. */ ++ if (!i) { ++ efi_boot_services_t *boottime; ++ ++ boottime = sys_table->boottime; ++ ++ status = efi_call_phys3(boottime->handle_protocol, ++ image->device_handle, &fs_proto, &io); ++ if (status != EFI_SUCCESS) ++ goto free_initrds; ++ ++ status = efi_call_phys2(io->open_volume, io, &fh); ++ if (status != EFI_SUCCESS) ++ goto free_initrds; ++ } ++ ++ status = efi_call_phys5(fh->open, fh, &h, filename, ++ EFI_FILE_MODE_READ, (u64)0); ++ if (status != EFI_SUCCESS) ++ goto close_handles; ++ ++ initrd->handle = h; ++ ++ info_sz = 0; ++ status = efi_call_phys4(h->get_info, h, &info_guid, ++ &info_sz, NULL); ++ if (status != EFI_BUFFER_TOO_SMALL) ++ goto close_handles; ++ ++grow: ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, info_sz, &info); ++ if (status != EFI_SUCCESS) ++ goto close_handles; ++ ++ status = efi_call_phys4(h->get_info, h, &info_guid, ++ &info_sz, info); ++ if (status == EFI_BUFFER_TOO_SMALL) { ++ efi_call_phys1(sys_table->boottime->free_pool, info); ++ goto grow; ++ } ++ ++ file_sz = info->file_size; ++ efi_call_phys1(sys_table->boottime->free_pool, info); ++ ++ if (status != EFI_SUCCESS) ++ goto close_handles; ++ ++ initrd->size = file_sz; ++ initrd_total += file_sz; ++ } ++ ++ if (initrd_total) { ++ unsigned long addr; ++ ++ /* ++ * Multiple initrd's need to be at consecutive ++ * addresses in memory, so allocate enough memory for ++ * all the initrd's. ++ */ ++ status = low_alloc(initrd_total, 0x1000, &initrd_addr); ++ if (status != EFI_SUCCESS) ++ goto close_handles; ++ ++ /* We've run out of free low memory. */ ++ if (initrd_addr > hdr->initrd_addr_max) { ++ status = EFI_INVALID_PARAMETER; ++ goto free_initrd_total; ++ } ++ ++ addr = initrd_addr; ++ for (j = 0; j < nr_initrds; j++) { ++ u64 size; ++ ++ size = initrds[j].size; ++ status = efi_call_phys3(fh->read, initrds[j].handle, ++ &size, addr); ++ if (status != EFI_SUCCESS) ++ goto free_initrd_total; ++ ++ efi_call_phys1(fh->close, initrds[j].handle); ++ ++ addr += size; ++ } ++ ++ } ++ ++ efi_call_phys1(sys_table->boottime->free_pool, initrds); ++ ++ hdr->ramdisk_image = initrd_addr; ++ hdr->ramdisk_size = initrd_total; ++ ++ return status; ++ ++free_initrd_total: ++ low_free(initrd_total, initrd_addr); ++ ++close_handles: ++ for (k = j; k < nr_initrds; k++) ++ efi_call_phys1(fh->close, initrds[k].handle); ++free_initrds: ++ efi_call_phys1(sys_table->boottime->free_pool, initrds); ++fail: ++ hdr->ramdisk_image = 0; ++ hdr->ramdisk_size = 0; ++ ++ return status; ++} ++ ++/* ++ * Because the x86 boot code expects to be passed a boot_params we ++ * need to create one ourselves (usually the bootloader would create ++ * one for us). ++ */ ++static efi_status_t make_boot_params(struct boot_params *boot_params, ++ efi_loaded_image_t *image, ++ void *handle) ++{ ++ struct efi_info *efi = &boot_params->efi_info; ++ struct apm_bios_info *bi = &boot_params->apm_bios_info; ++ struct sys_desc_table *sdt = &boot_params->sys_desc_table; ++ struct e820entry *e820_map = &boot_params->e820_map[0]; ++ struct e820entry *prev = NULL; ++ struct setup_header *hdr = &boot_params->hdr; ++ unsigned long size, key, desc_size, _size; ++ efi_memory_desc_t *mem_map; ++ void *options = image->load_options; ++ u32 load_options_size = image->load_options_size; ++ int options_size = 0; ++ efi_status_t status; ++ __u32 desc_version; ++ unsigned long cmdline; ++ u8 nr_entries; ++ u16 *s2; ++ u8 *s1; ++ int i; ++ ++ hdr->type_of_loader = 0x21; ++ ++ /* Convert unicode cmdline to ascii */ ++ cmdline = 0; ++ s2 = (u16 *)options; ++ ++ if (s2) { ++ while (*s2 && *s2 != '\n' && options_size < load_options_size) { ++ s2++; ++ options_size++; ++ } ++ ++ if (options_size) { ++ if (options_size > hdr->cmdline_size) ++ options_size = hdr->cmdline_size; ++ ++ options_size++; /* NUL termination */ ++ ++ status = low_alloc(options_size, 1, &cmdline); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ s1 = (u8 *)(unsigned long)cmdline; ++ s2 = (u16 *)options; ++ ++ for (i = 0; i < options_size - 1; i++) ++ *s1++ = *s2++; ++ ++ *s1 = '\0'; ++ } ++ } ++ ++ hdr->cmd_line_ptr = cmdline; ++ ++ hdr->ramdisk_image = 0; ++ hdr->ramdisk_size = 0; ++ ++ status = handle_ramdisks(image, hdr); ++ if (status != EFI_SUCCESS) ++ goto free_cmdline; ++ ++ setup_graphics(boot_params); ++ ++ /* Clear APM BIOS info */ ++ memset(bi, 0, sizeof(*bi)); ++ ++ memset(sdt, 0, sizeof(*sdt)); ++ ++ memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32)); ++ ++ size = sizeof(*mem_map) * 32; ++ ++again: ++ size += sizeof(*mem_map); ++ _size = size; ++ status = low_alloc(size, 1, (unsigned long *)&mem_map); ++ if (status != EFI_SUCCESS) ++ goto free_cmdline; ++ ++ status = efi_call_phys5(sys_table->boottime->get_memory_map, &size, ++ mem_map, &key, &desc_size, &desc_version); ++ if (status == EFI_BUFFER_TOO_SMALL) { ++ low_free(_size, (unsigned long)mem_map); ++ goto again; ++ } ++ ++ if (status != EFI_SUCCESS) ++ goto free_mem_map; ++ ++ efi->efi_systab = (unsigned long)sys_table; ++ efi->efi_memdesc_size = desc_size; ++ efi->efi_memdesc_version = desc_version; ++ efi->efi_memmap = (unsigned long)mem_map; ++ efi->efi_memmap_size = size; ++ ++#ifdef CONFIG_X86_64 ++ efi->efi_systab_hi = (unsigned long)sys_table >> 32; ++ efi->efi_memmap_hi = (unsigned long)mem_map >> 32; ++#endif ++ ++ /* Might as well exit boot services now */ ++ status = efi_call_phys2(sys_table->boottime->exit_boot_services, ++ handle, key); ++ if (status != EFI_SUCCESS) ++ goto free_mem_map; ++ ++ /* Historic? */ ++ boot_params->alt_mem_k = 32 * 1024; ++ ++ /* ++ * Convert the EFI memory map to E820. ++ */ ++ nr_entries = 0; ++ for (i = 0; i < size / desc_size; i++) { ++ efi_memory_desc_t *d; ++ unsigned int e820_type = 0; ++ ++ d = (efi_memory_desc_t *)((unsigned long)mem_map + (i * desc_size)); ++ switch(d->type) { ++ case EFI_RESERVED_TYPE: ++ case EFI_RUNTIME_SERVICES_CODE: ++ case EFI_RUNTIME_SERVICES_DATA: ++ case EFI_MEMORY_MAPPED_IO: ++ case EFI_MEMORY_MAPPED_IO_PORT_SPACE: ++ case EFI_PAL_CODE: ++ e820_type = E820_RESERVED; ++ break; ++ ++ case EFI_UNUSABLE_MEMORY: ++ e820_type = E820_UNUSABLE; ++ break; ++ ++ case EFI_ACPI_RECLAIM_MEMORY: ++ e820_type = E820_ACPI; ++ break; ++ ++ case EFI_LOADER_CODE: ++ case EFI_LOADER_DATA: ++ case EFI_BOOT_SERVICES_CODE: ++ case EFI_BOOT_SERVICES_DATA: ++ case EFI_CONVENTIONAL_MEMORY: ++ e820_type = E820_RAM; ++ break; ++ ++ case EFI_ACPI_MEMORY_NVS: ++ e820_type = E820_NVS; ++ break; ++ ++ default: ++ continue; ++ } ++ ++ /* Merge adjacent mappings */ ++ if (prev && prev->type == e820_type && ++ (prev->addr + prev->size) == d->phys_addr) ++ prev->size += d->num_pages << 12; ++ else { ++ e820_map->addr = d->phys_addr; ++ e820_map->size = d->num_pages << 12; ++ e820_map->type = e820_type; ++ prev = e820_map++; ++ nr_entries++; ++ } ++ } ++ ++ boot_params->e820_entries = nr_entries; ++ ++ return EFI_SUCCESS; ++ ++free_mem_map: ++ low_free(_size, (unsigned long)mem_map); ++free_cmdline: ++ if (options_size) ++ low_free(options_size, hdr->cmd_line_ptr); ++fail: ++ return status; ++} ++ ++/* ++ * On success we return a pointer to a boot_params structure, and NULL ++ * on failure. ++ */ ++struct boot_params *efi_main(void *handle, efi_system_table_t *_table) ++{ ++ struct boot_params *boot_params; ++ unsigned long start, nr_pages; ++ struct desc_ptr *gdt, *idt; ++ efi_loaded_image_t *image; ++ struct setup_header *hdr; ++ efi_status_t status; ++ efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; ++ struct desc_struct *desc; ++ ++ sys_table = _table; ++ ++ /* Check if we were booted by the EFI firmware */ ++ if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) ++ goto fail; ++ ++ status = efi_call_phys3(sys_table->boottime->handle_protocol, ++ handle, &proto, (void *)&image); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ status = low_alloc(0x4000, 1, (unsigned long *)&boot_params); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ memset(boot_params, 0x0, 0x4000); ++ ++ /* Copy first two sectors to boot_params */ ++ memcpy(boot_params, image->image_base, 1024); ++ ++ hdr = &boot_params->hdr; ++ ++ /* ++ * The EFI firmware loader could have placed the kernel image ++ * anywhere in memory, but the kernel has various restrictions ++ * on the max physical address it can run at. Attempt to move ++ * the kernel to boot_params.pref_address, or as close as ++ * possible to it. ++ */ ++ start = hdr->pref_address; ++ nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; ++ ++ status = efi_call_phys4(sys_table->boottime->allocate_pages, ++ EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, ++ nr_pages, &start); ++ if (status != EFI_SUCCESS) { ++ status = low_alloc(hdr->init_size, hdr->kernel_alignment, ++ &start); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ } ++ ++ hdr->code32_start = (__u32)start; ++ hdr->pref_address = (__u64)(unsigned long)image->image_base; ++ ++ memcpy((void *)start, image->image_base, image->image_size); ++ ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, sizeof(*gdt), ++ (void **)&gdt); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ gdt->size = 0x800; ++ status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ status = efi_call_phys3(sys_table->boottime->allocate_pool, ++ EFI_LOADER_DATA, sizeof(*idt), ++ (void **)&idt); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ idt->size = 0; ++ idt->address = 0; ++ ++ status = make_boot_params(boot_params, image, handle); ++ if (status != EFI_SUCCESS) ++ goto fail; ++ ++ memset((char *)gdt->address, 0x0, gdt->size); ++ desc = (struct desc_struct *)gdt->address; ++ ++ /* The first GDT is a dummy and the second is unused. */ ++ desc += 2; ++ ++ desc->limit0 = 0xffff; ++ desc->base0 = 0x0000; ++ desc->base1 = 0x0000; ++ desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; ++ desc->s = DESC_TYPE_CODE_DATA; ++ desc->dpl = 0; ++ desc->p = 1; ++ desc->limit = 0xf; ++ desc->avl = 0; ++ desc->l = 0; ++ desc->d = SEG_OP_SIZE_32BIT; ++ desc->g = SEG_GRANULARITY_4KB; ++ desc->base2 = 0x00; ++ ++ desc++; ++ desc->limit0 = 0xffff; ++ desc->base0 = 0x0000; ++ desc->base1 = 0x0000; ++ desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; ++ desc->s = DESC_TYPE_CODE_DATA; ++ desc->dpl = 0; ++ desc->p = 1; ++ desc->limit = 0xf; ++ desc->avl = 0; ++ desc->l = 0; ++ desc->d = SEG_OP_SIZE_32BIT; ++ desc->g = SEG_GRANULARITY_4KB; ++ desc->base2 = 0x00; ++ ++#ifdef CONFIG_X86_64 ++ /* Task segment value */ ++ desc++; ++ desc->limit0 = 0x0000; ++ desc->base0 = 0x0000; ++ desc->base1 = 0x0000; ++ desc->type = SEG_TYPE_TSS; ++ desc->s = 0; ++ desc->dpl = 0; ++ desc->p = 1; ++ desc->limit = 0x0; ++ desc->avl = 0; ++ desc->l = 0; ++ desc->d = 0; ++ desc->g = SEG_GRANULARITY_4KB; ++ desc->base2 = 0x00; ++#endif /* CONFIG_X86_64 */ ++ ++ asm volatile ("lidt %0" :: "m" (*idt)); ++ asm volatile ("lgdt %0" :: "m" (*gdt)); ++ ++ asm volatile("cli"); ++ ++ return boot_params; ++fail: ++ return NULL; ++} +diff --git a/arch/x86/boot/compressed/efi_stub_32.S b/arch/x86/boot/compressed/efi_stub_32.S +new file mode 100644 +index 0000000..5047cd9 +--- /dev/null ++++ b/arch/x86/boot/compressed/efi_stub_32.S +@@ -0,0 +1,87 @@ ++/* ++ * EFI call stub for IA32. ++ * ++ * This stub allows us to make EFI calls in physical mode with interrupts ++ * turned off. Note that this implementation is different from the one in ++ * arch/x86/platform/efi/efi_stub_32.S because we're _already_ in physical ++ * mode at this point. ++ */ ++ ++#include <linux/linkage.h> ++#include <asm/page_types.h> ++ ++/* ++ * efi_call_phys(void *, ...) is a function with variable parameters. ++ * All the callers of this function assure that all the parameters are 4-bytes. ++ */ ++ ++/* ++ * In gcc calling convention, EBX, ESP, EBP, ESI and EDI are all callee save. ++ * So we'd better save all of them at the beginning of this function and restore ++ * at the end no matter how many we use, because we can not assure EFI runtime ++ * service functions will comply with gcc calling convention, too. ++ */ ++ ++.text ++ENTRY(efi_call_phys) ++ /* ++ * 0. The function can only be called in Linux kernel. So CS has been ++ * set to 0x0010, DS and SS have been set to 0x0018. In EFI, I found ++ * the values of these registers are the same. And, the corresponding ++ * GDT entries are identical. So I will do nothing about segment reg ++ * and GDT, but change GDT base register in prelog and epilog. ++ */ ++ ++ /* ++ * 1. Because we haven't been relocated by this point we need to ++ * use relative addressing. ++ */ ++ call 1f ++1: popl %edx ++ subl $1b, %edx ++ ++ /* ++ * 2. Now on the top of stack is the return ++ * address in the caller of efi_call_phys(), then parameter 1, ++ * parameter 2, ..., param n. To make things easy, we save the return ++ * address of efi_call_phys in a global variable. ++ */ ++ popl %ecx ++ movl %ecx, saved_return_addr(%edx) ++ /* get the function pointer into ECX*/ ++ popl %ecx ++ movl %ecx, efi_rt_function_ptr(%edx) ++ ++ /* ++ * 3. Call the physical function. ++ */ ++ call *%ecx ++ ++ /* ++ * 4. Balance the stack. And because EAX contain the return value, ++ * we'd better not clobber it. We need to calculate our address ++ * again because %ecx and %edx are not preserved across EFI function ++ * calls. ++ */ ++ call 1f ++1: popl %edx ++ subl $1b, %edx ++ ++ movl efi_rt_function_ptr(%edx), %ecx ++ pushl %ecx ++ ++ /* ++ * 10. Push the saved return address onto the stack and return. ++ */ ++ movl saved_return_addr(%edx), %ecx ++ pushl %ecx ++ ret ++ENDPROC(efi_call_phys) ++.previous ++ ++.data ++saved_return_addr: ++ .long 0 ++efi_rt_function_ptr: ++ .long 0 ++ +diff --git a/arch/x86/boot/compressed/efi_stub_64.S b/arch/x86/boot/compressed/efi_stub_64.S +new file mode 100644 +index 0000000..cedc60d +--- /dev/null ++++ b/arch/x86/boot/compressed/efi_stub_64.S +@@ -0,0 +1 @@ ++#include "../../platform/efi/efi_stub_64.S" +diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S +index 67a655a..a055993 100644 +--- a/arch/x86/boot/compressed/head_32.S ++++ b/arch/x86/boot/compressed/head_32.S +@@ -32,6 +32,28 @@ + + __HEAD + ENTRY(startup_32) ++#ifdef CONFIG_EFI_STUB ++ /* ++ * We don't need the return a |