modulization:move out efi dir from hypervisor

move acrn-hypervisor/hypervisor/bsp/uefi/efi
to   arcn-hypervisor/efi-stub
move acrn-hypervisor/hypervisor/bsp/uefi/clearlinux
to   acrn-hypervisor/efi-stub/clearlinux

Changes to be committed:
  modified:   Makefile
  modified:   doc/getting-started/apl-nuc.rst
  renamed:    hypervisor/bsp/uefi/efi/Makefile -> efi-stub/Makefile
  renamed:    hypervisor/bsp/uefi/efi/boot.c -> efi-stub/boot.c
  renamed:    hypervisor/bsp/uefi/efi/boot.h -> efi-stub/boot.h
  renamed:    hypervisor/bsp/uefi/clearlinux/acrn.conf ->
	      efi-stub/clearlinux/acrn.conf
  renamed:    hypervisor/bsp/uefi/efi/efilinux.h -> efi-stub/efilinux.h
  renamed:    hypervisor/bsp/uefi/efi/malloc.c -> efi-stub/malloc.c
  renamed:    hypervisor/bsp/uefi/efi/multiboot.h -> efi-stub/multiboot.h
  renamed:    hypervisor/bsp/uefi/efi/pe.c -> efi-stub/pe.c
  renamed:    hypervisor/bsp/uefi/efi/stdlib.h -> efi-stub/stdlib.h
  modified:   hypervisor/Makefile

Tracked-On: #1842
Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com>
Acked-by: Anthony Xu <anthony.xu@intel.com>
This commit is contained in:
Mingqiang Chi
2018-12-10 19:32:42 +08:00
committed by wenlingz
parent 59e3f562b8
commit 74849cd983
12 changed files with 18 additions and 19 deletions

115
efi-stub/Makefile Normal file
View File

@@ -0,0 +1,115 @@
#
# Copyright (c) 2011, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Intel Corporation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
HV_OBJDIR:=build
HV_FILE:=acrn
C_SRCS = boot.c pe.c malloc.c
ACRN_OBJS := $(patsubst %.c,$(EFI_OBJDIR)/%.o,$(C_SRCS))
INCLUDE_PATH += $(HV_OBJDIR)/include
OBJCOPY=objcopy
HOST = $(shell $(CC) -dumpmachine | sed "s/\(-\).*$$//")
ARCH := $(shell $(CC) -dumpmachine | sed "s/\(-\).*$$//")
ifeq ($(ARCH),x86_64)
FORMAT=efi-app-x86-64
else
ARCH=ia32
FORMAT=efi-app-ia32
endif
# Different Linux distributions have the 'gnu-efi' package install
# its tools and libraries in different folders. The next couple of
# variables will determine and set the right path for both the
# tools $(GNUEFI_DIR) and libraries $(LIBDIR)
GNUEFI_DIR := $(shell find $(SYSROOT)/usr/lib* -name elf_$(ARCH)_efi.lds -type f | xargs dirname)
LIBDIR := $(subst gnuefi,,$(GNUEFI_DIR))
CRT0 := $(GNUEFI_DIR)/crt0-efi-$(ARCH).o
LDSCRIPT := $(GNUEFI_DIR)/elf_$(ARCH)_efi.lds
INCDIR := $(SYSROOT)/usr/include
CFLAGS=-I. -I.. -I../hypervisor/include/arch/x86/guest -I$(INCDIR)/efi -I$(INCDIR)/efi/$(ARCH) \
-I../hypervisor/include/public -I../hypervisor/include/lib \
-DEFI_FUNCTION_WRAPPER -fPIC -fshort-wchar -ffreestanding \
-Wall -I../fs/ -D$(ARCH) -O2 \
-include config.h
CFLAGS += -mno-mmx -mno-sse -mno-sse2 -mno-80387 -mno-fp-ret-in-387
ifeq ($(ARCH),ia32)
ifeq ($(HOST),x86_64)
CFLAGS += -m32
endif
endif
ifeq ($(ARCH),x86_64)
CFLAGS += -mno-red-zone
endif
LDFLAGS=-T $(LDSCRIPT) -Bsymbolic -shared -nostdlib -znocombreloc \
-L$(LIBDIR) $(CRT0)
EFIBIN=$(HV_OBJDIR)/$(HV_FILE).efi
BOOT=$(EFI_OBJDIR)/boot.efi
CONF_FILE=$(CURDIR)/clearlinux/acrn.conf
all: $(EFIBIN)
$(OBJCOPY) --add-section .hv="$(HV_OBJDIR)/$(HV_FILE).bin" --change-section-vma .hv=0x6e000 --set-section-flags .hv=alloc,data,contents,load --section-alignment 0x1000 $(EFI_OBJDIR)/boot.efi $(EFIBIN)
install: $(EFIBIN) install-conf
install -D $(EFIBIN) $(DESTDIR)/usr/lib/acrn/$(HV_FILE).efi
$(EFIBIN): $(BOOT)
$(EFI_OBJDIR)/boot.efi: $(EFI_OBJDIR)/boot.so
$(EFI_OBJDIR)/boot.so: $(ACRN_OBJS) $(FS)
$(LD) $(LDFLAGS) -o $@ $^ -lgnuefi -lefi $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
install-conf: $(CONF_FILE)
install -D --mode=0644 $^ $(DESTDIR)/usr/share/acrn/samples/nuc/acrn.conf
clean:
rm -f $(BOOT) $(HV_OBJDIR)/$(HV_FILE).efi $(EFI_OBJDIR)/boot.so $(ACRN_OBJS) $(FS)
$(EFI_OBJDIR)/%.o:%.S $(HV_OBJDIR)/$(HV_CONFIG_H)
[ ! -e $@ ] && mkdir -p $(dir $@); \
$(CC) $(CFLAGS) -c -o $@ $<
$(EFI_OBJDIR)/%.o: %.c $(HV_OBJDIR)/$(HV_CONFIG_H)
[ ! -e $@ ] && mkdir -p $(dir $@); \
$(CC) $(patsubst %, -I%, $(INCLUDE_PATH)) -I. -c $(CFLAGS) $(ARCH_CFLAGS) $< -o $@
%.efi: %.so
$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel \
-j .rela -j .reloc --target=$(FORMAT) $*.so $@

428
efi-stub/boot.c Normal file
View File

@@ -0,0 +1,428 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <efi.h>
#include <efilib.h>
#include "efilinux.h"
#include "stdlib.h"
#include "boot.h"
#include "acrn_common.h"
#include "vm0_boot.h"
EFI_SYSTEM_TABLE *sys_table;
EFI_BOOT_SERVICES *boot;
char *cmdline = NULL;
extern const uint64_t guest_entry;
static UINT64 hv_hpa;
static inline void hv_jump(EFI_PHYSICAL_ADDRESS hv_start,
struct multiboot_info *mbi, struct efi_context *efi_ctx)
{
hv_func hf;
efi_ctx->vcpu_regs.rip = (uint64_t)&guest_entry;
/* The 64-bit entry of acrn hypervisor is 0x200 from the start
* address of hv image. But due to there is multiboot header,
* so it has to be added with 0x10.
*
* FIXME: The hardcode value 0x210 should be worked out
* from the link address of cpu_primary_start_64 in acrn.out
*/
hf = (hv_func)(hv_start + 0x210);
asm volatile ("cli");
/* jump to acrn hypervisor */
hf(MULTIBOOT_INFO_MAGIC, mbi);
}
EFI_STATUS construct_mbi(EFI_PHYSICAL_ADDRESS hv_hpa)
{
UINTN map_size, map_key;
UINT32 desc_version;
UINTN desc_size;
EFI_MEMORY_DESCRIPTOR *map_buf;
EFI_STATUS err = EFI_SUCCESS;
struct multiboot_info *mbi;
struct multiboot_mmap *mmap;
struct efi_context *efi_ctx;
int32_t i, j;
mbi = MBOOT_INFO_PTR(hv_hpa);
mmap = MBOOT_MMAP_PTR(hv_hpa);
efi_ctx = BOOT_CTX_PTR(hv_hpa);
(void)memset((void *)mbi, 0x0, MBOOT_INFO_SIZE);
(void)memset((void *)mmap, 0x0, MBOOT_MMAP_SIZE);
/* We're just interested in the map's size for now */
map_size = 0;
err = get_memory_map(&map_size, NULL, NULL, NULL, NULL);
if (err != EFI_SUCCESS && err != EFI_BUFFER_TOO_SMALL)
goto out;
again:
err = allocate_pool(EfiLoaderData, map_size, (void **) &map_buf);
if (err != EFI_SUCCESS)
goto out;
/*
* Remember! We've already allocated map_buf with emalloc (and
* 'map_size' contains its size) which means that it should be
* positioned below our allocation for the kernel. Use that
* space for the memory map.
*/
err = get_memory_map(&map_size, map_buf, &map_key,
&desc_size, &desc_version);
if (err != EFI_SUCCESS) {
if (err == EFI_BUFFER_TOO_SMALL) {
/*
* Argh! The buffer that we allocated further
* up wasn't large enough which means we need
* to allocate them again, but this time
* larger. 'map_size' has been updated by the
* call to memory_map().
*/
free_pool(map_buf);
goto again;
}
goto out;
}
/*
* Convert the EFI memory map to E820.
*/
for (i = 0, j = 0; i < map_size / desc_size; i++) {
EFI_MEMORY_DESCRIPTOR *d;
uint32_t e820_type = 0;
d = (EFI_MEMORY_DESCRIPTOR *)((uint64_t)map_buf + (i * desc_size));
switch(d->Type) {
case EfiReservedMemoryType:
case EfiRuntimeServicesCode:
case EfiRuntimeServicesData:
case EfiMemoryMappedIO:
case EfiMemoryMappedIOPortSpace:
case EfiPalCode:
e820_type = E820_RESERVED;
break;
case EfiUnusableMemory:
e820_type = E820_UNUSABLE;
break;
case EfiACPIReclaimMemory:
e820_type = E820_ACPI;
break;
case EfiLoaderCode:
case EfiLoaderData:
case EfiBootServicesCode:
case EfiBootServicesData:
case EfiConventionalMemory:
e820_type = E820_RAM;
break;
case EfiACPIMemoryNVS:
e820_type = E820_NVS;
break;
default:
continue;
}
if ((j != 0) && mmap[j-1].mm_type == e820_type &&
(mmap[j-1].mm_base_addr + mmap[j-1].mm_length)
== d->PhysicalStart) {
mmap[j-1].mm_length += d->NumberOfPages << EFI_PAGE_SHIFT;
} else {
mmap[j].mm_base_addr = d->PhysicalStart;
mmap[j].mm_length = d->NumberOfPages << EFI_PAGE_SHIFT;
mmap[j].mm_type = e820_type;
j++;
}
}
/* switch hv memory region(0x20000000 ~ 0x22000000) to
* available RAM in e820 table
*/
mmap[j].mm_base_addr = hv_hpa;
mmap[j].mm_length = HV_RUNTIME_MEM_SIZE;
mmap[j].mm_type = E820_RAM;
j++;
mbi->mi_flags |= MULTIBOOT_INFO_HAS_MMAP | MULTIBOOT_INFO_HAS_CMDLINE;
mbi->mi_mmap_length = j*sizeof(struct multiboot_mmap);
mbi->mi_cmdline = (UINTN)cmdline;
mbi->mi_mmap_addr = (UINTN)mmap;
mbi->mi_flags |= MULTIBOOT_INFO_HAS_DRIVES;
mbi->mi_drives_addr = (UINT32)(UINTN)efi_ctx;
out:
return err;
}
static EFI_STATUS
switch_to_guest_mode(EFI_HANDLE image, EFI_PHYSICAL_ADDRESS hv_hpa)
{
EFI_PHYSICAL_ADDRESS addr;
EFI_STATUS err;
struct multiboot_info *mbi;
struct efi_context *efi_ctx;
struct acpi_table_rsdp *rsdp = NULL;
int32_t i;
EFI_CONFIGURATION_TABLE *config_table;
mbi = MBOOT_INFO_PTR(hv_hpa);
efi_ctx = BOOT_CTX_PTR(hv_hpa);
(void)memset((void *)efi_ctx, 0x0, BOOT_CTX_SIZE);
/* reserve secondary memory region for CPU trampoline code */
err = emalloc_reserved_mem(&addr, CONFIG_LOW_RAM_SIZE, MEM_ADDR_1MB);
if (err != EFI_SUCCESS)
goto out;
if (addr < 4096)
Print(L"Warning: CPU trampoline code buf occupied zero-page\n");
efi_ctx->ap_trampoline_buf = (void *)addr;
config_table = sys_table->ConfigurationTable;
for (i = 0; i < sys_table->NumberOfTableEntries; i++) {
EFI_GUID acpi_20_table_guid = ACPI_20_TABLE_GUID;
EFI_GUID acpi_table_guid = ACPI_TABLE_GUID;
if (CompareGuid(&acpi_20_table_guid,
&config_table->VendorGuid) == 0) {
rsdp = config_table->VendorTable;
break;
}
if (CompareGuid(&acpi_table_guid,
&config_table->VendorGuid) == 0)
rsdp = config_table->VendorTable;
config_table++;
}
if (rsdp == NULL) {
Print(L"unable to find RSDP\n");
goto out;
}
efi_ctx->rsdp = rsdp;
/* construct multiboot info and deliver it to hypervisor */
err = construct_mbi(hv_hpa);
if (err != EFI_SUCCESS)
goto out;
asm volatile ("pushf\n\t"
"pop %0\n\t"
: "=r"(efi_ctx->vcpu_regs.rflags)
: );
asm volatile ("movq %%rax, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rax));
asm volatile ("movq %%rbx, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rbx));
asm volatile ("movq %%rcx, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rcx));
asm volatile ("movq %%rdx, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rdx));
asm volatile ("movq %%rdi, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rdi));
asm volatile ("movq %%rsi, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rsi));
asm volatile ("movq %%rsp, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rsp));
asm volatile ("movq %%rbp, %0" : "=r"(efi_ctx->vcpu_regs.gprs.rbp));
asm volatile ("movq %%r8, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r8));
asm volatile ("movq %%r9, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r9));
asm volatile ("movq %%r10, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r10));
asm volatile ("movq %%r11, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r11));
asm volatile ("movq %%r12, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r12));
asm volatile ("movq %%r13, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r13));
asm volatile ("movq %%r14, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r14));
asm volatile ("movq %%r15, %0" : "=r"(efi_ctx->vcpu_regs.gprs.r15));
hv_jump(hv_hpa, mbi, efi_ctx);
asm volatile (".global guest_entry\n\t"
"guest_entry:\n\t");
out:
return err;
}
static inline EFI_STATUS isspace(CHAR8 ch)
{
return ((uint8_t)ch <= ' ');
}
/**
* efi_main - The entry point for the OS loader image.
* @image: firmware-allocated handle that identifies the image
* @sys_table: EFI system table
*/
EFI_STATUS
efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
{
WCHAR *error_buf;
EFI_STATUS err;
EFI_LOADED_IMAGE *info;
UINTN sec_addr;
UINTN sec_size;
char *section;
EFI_DEVICE_PATH *path;
INTN i, index;
CHAR16 *bootloader_name = NULL;
CHAR16 bootloader_param[] = L"bootloader=";
EFI_HANDLE bootloader_image;
CHAR16 *options = NULL;
UINT32 options_size = 0;
CHAR16 *cmdline16, *n;
InitializeLib(image, _table);
sys_table = _table;
boot = sys_table->BootServices;
if (CheckCrc(sys_table->Hdr.HeaderSize, &sys_table->Hdr) != TRUE)
return EFI_LOAD_ERROR;
err = handle_protocol(image, &LoadedImageProtocol, (void **)&info);
if (err != EFI_SUCCESS)
goto failed;
/* get the options */
options = info->LoadOptions;
options_size = info->LoadOptionsSize;
/* convert the options to cmdline */
if (options_size > 0)
cmdline = ch16_2_ch8(options);
/* First check if we were given a bootloader name
* E.g.: "bootloader=\EFI\org.clearlinux\bootloaderx64.efi"
*/
cmdline16 = StrDuplicate(options);
bootloader_name = strstr_16(cmdline16, bootloader_param);
if (bootloader_name) {
bootloader_name = bootloader_name + StrLen(bootloader_param);
n = bootloader_name;
i = 0;
while (*n && !isspace((CHAR8)*n) && (*n < 0xff)) {
n++; i++;
}
*n++ = '\0';
} else {
/*
* If we reach this point, it means we did not receive a specific
* bootloader name to be used. Fall back to the default bootloader
* as specified in config.h
*/
bootloader_name = ch8_2_ch16(CONFIG_UEFI_OS_LOADER_NAME);
}
section = ".hv";
err = get_pe_section(info->ImageBase, section, &sec_addr, &sec_size);
if (EFI_ERROR(err)) {
Print(L"Unable to locate section of ACRNHV %r ", err);
goto failed;
}
/* without relocateion enabled, hypervisor binary need to reside in
* fixed memory address starting from CONFIG_HV_RAM_START, make a call
* to emalloc_fixed_addr for that case. With CONFIG_RELOC enabled,
* hypervisor is able to do relocation, the only requirement is that
* it need to reside in memory below 4GB, call emalloc_reserved_mem()
* instead.
*/
#ifdef CONFIG_RELOC
err = emalloc_reserved_mem(&hv_hpa, HV_RUNTIME_MEM_SIZE, MEM_ADDR_4GB);
#else
err = emalloc_fixed_addr(&hv_hpa, HV_RUNTIME_MEM_SIZE, CONFIG_HV_RAM_START);
#endif
if (err != EFI_SUCCESS)
goto failed;
memcpy((char *)hv_hpa, info->ImageBase + sec_addr, sec_size);
/* load hypervisor and begin to run on it */
err = switch_to_guest_mode(image, hv_hpa);
if (err != EFI_SUCCESS)
goto failed;
/* load and start the default bootloader */
path = FileDevicePath(info->DeviceHandle, bootloader_name);
if (!path)
goto free_args;
FreePool(bootloader_name);
err = uefi_call_wrapper(boot->LoadImage, 6, FALSE, image,
path, NULL, 0, &bootloader_image);
if (EFI_ERROR(err)) {
uefi_call_wrapper(boot->Stall, 1, 3 * 1000 * 1000);
goto failed;
}
err = uefi_call_wrapper(boot->StartImage, 3, bootloader_image,
NULL, NULL);
if (EFI_ERROR(err)) {
uefi_call_wrapper(boot->Stall, 1, 3 * 1000 * 1000);
goto failed;
}
uefi_call_wrapper(boot->UnloadImage, 1, bootloader_image);
return EFI_SUCCESS;
free_args:
FreePool(bootloader_name);
failed:
/*
* We need to be careful not to trash 'err' here. If we fail
* to allocate enough memory to hold the error string fallback
* to returning 'err'.
*/
if (allocate_pool(EfiLoaderData, ERROR_STRING_LENGTH,
(void **)&error_buf) != EFI_SUCCESS) {
Print(L"Couldn't allocate pages for error string\n");
return err;
}
StatusToString(error_buf, err);
Print(L": %s\n", error_buf);
/* If we don't wait for user input, (s)he will not see the error message */
uefi_call_wrapper(sys_table->ConOut->OutputString, 2, sys_table->ConOut, \
L"\r\n\r\n\r\nHit any key to exit\r\n");
uefi_call_wrapper(sys_table->BootServices->WaitForEvent, 3, 1, \
&sys_table->ConIn->WaitForKey, &index);
return exit(image, err, ERROR_STRING_LENGTH, error_buf);
}

158
efi-stub/boot.h Normal file
View File

@@ -0,0 +1,158 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ACRNBOOT_H__
#define __ACRNBOOT_H__
#include "multiboot.h"
#define E820_RAM 1
#define E820_RESERVED 2
#define E820_ACPI 3
#define E820_NVS 4
#define E820_UNUSABLE 5
#define ERROR_STRING_LENGTH 32
#define EFI_LOADER_SIGNATURE "EL64"
#define ACPI_XSDT_ENTRY_SIZE (sizeof(UINT64))
#define ACPI_NAME_SIZE 4
#define ACPI_OEM_ID_SIZE 6
#define ACPI_OEM_TABLE_ID_SIZE 8
#define MSR_IA32_PAT 0x00000277 /* PAT */
#define MSR_IA32_EFER 0xC0000080
#define MSR_IA32_FS_BASE 0xC0000100U
#define MSR_IA32_GS_BASE 0xC0000101
#define MSR_IA32_SYSENTER_ESP 0x00000175 /* ESP for sysenter */
#define MSR_IA32_SYSENTER_EIP 0x00000176 /* EIP for sysenter */
/* Read MSR */
#define CPU_MSR_READ(reg, msr_val_ptr) \
{ \
uint32_t msrl, msrh; \
asm volatile ("rdmsr" : "=a"(msrl), \
"=d"(msrh) : "c" (reg)); \
*msr_val_ptr = ((uint64_t)msrh << 32U) | msrl; \
}
EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size);
typedef void(*hv_func)(int32_t, struct multiboot_info*);
/*
* We allocate memory for the following struct together with hyperivosr itself
* memory allocation during boot.
*/
#define MBOOT_MMAP_NUMS 128
#define MBOOT_MMAP_SIZE (sizeof(struct multiboot_mmap) * MBOOT_MMAP_NUMS)
#define MBOOT_INFO_SIZE (sizeof(struct multiboot_info))
#define BOOT_CTX_SIZE (sizeof(struct efi_context))
#define HV_RUNTIME_MEM_SIZE \
(CONFIG_HV_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE + BOOT_CTX_SIZE)
#define MBOOT_MMAP_PTR(addr) \
((struct multiboot_mmap *)((VOID *)addr + CONFIG_HV_RAM_SIZE))
#define MBOOT_INFO_PTR(addr) ((struct multiboot_info *) \
((VOID *)addr + CONFIG_HV_RAM_SIZE + MBOOT_MMAP_SIZE))
#define BOOT_CTX_PTR(addr) ((struct efi_context *) \
((VOID *)addr + CONFIG_HV_RAM_SIZE + MBOOT_MMAP_SIZE + MBOOT_INFO_SIZE))
struct efi_info {
UINT32 efi_loader_signature;
UINT32 efi_systab;
UINT32 efi_memdesc_size;
UINT32 efi_memdesc_version;
UINT32 efi_memmap;
UINT32 efi_memmap_size;
UINT32 efi_systab_hi;
UINT32 efi_memmap_hi;
};
struct e820_entry {
UINT64 addr; /* start of memory segment */
UINT64 size; /* size of memory segment */
UINT32 type; /* type of memory segment */
} __attribute__((packed));
struct acpi_table_rsdp {
/* ACPI signature, contains "RSD PTR " */
char signature[8];
/* ACPI 1.0 checksum */
UINT8 checksum;
/* OEM identification */
char oem_id[ACPI_OEM_ID_SIZE];
/* Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+ */
UINT8 revision;
/* 32-bit physical address of the RSDT */
UINT32 rsdt_physical_address;
/* Table length in bytes, including header (ACPI 2.0+) */
UINT32 length;
/* 64-bit physical address of the XSDT (ACPI 2.0+) */
UINT64 xsdt_physical_address;
/* Checksum of entire table (ACPI 2.0+) */
UINT8 extended_checksum;
/* Reserved, must be zero */
UINT8 reserved[3];
};
struct acpi_table_header {
/* ASCII table signature */
char signature[ACPI_NAME_SIZE];
/* Length of table in bytes, including this header */
UINT32 length;
/* ACPI Specification minor version number */
UINT8 revision;
/* To make sum of entire table == 0 */
UINT8 checksum;
/* ASCII OEM identification */
char oem_id[ACPI_OEM_ID_SIZE];
/* ASCII OEM table identification */
char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
/* OEM revision number */
UINT32 oem_revision;
/* ASCII ASL compiler vendor ID */
char asl_compiler_id[ACPI_NAME_SIZE];
/* ASL compiler version */
UINT32 asl_compiler_revision;
};
static inline uint64_t
msr_read(uint32_t reg_num)
{
uint64_t msr_val;
CPU_MSR_READ(reg_num, &msr_val);
return msr_val;
}
#endif

3
efi-stub/clearlinux/acrn.conf Executable file
View File

@@ -0,0 +1,3 @@
title The ACRN Service OS
linux /EFI/org.clearlinux/kernel-org.clearlinux.iot-lts2018-sos.4.19.5-29
options console=tty0 console=ttyS0 i915.nuclear_pageflip=1 root=PARTUUID=<UUID of rootfs partition> rw rootwait ignore_loglevel no_timer_check consoleblank=0 i915.tsd_init=7 i915.tsd_delay=2000 i915.avail_planes_per_pipe=0x01010F i915.domain_plane_owners=0x011111110000 i915.enable_guc_loading=0 i915.enable_guc_submission=0 i915.enable_preemption=1 i915.context_priority_mode=2 i915.enable_gvt=1 i915.enable_guc=0 hvlog=2M@0x1FE00000

272
efi-stub/efilinux.h Normal file
View File

@@ -0,0 +1,272 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file contains some wrappers around the gnu-efi functions. As
* we're not going through uefi_call_wrapper() directly, this allows
* us to get some type-safety for function call arguments and for the
* compiler to check that the number of function call arguments is
* correct.
*
* It's also a good place to document the EFI interface.
*/
#ifndef __EFILINUX_H__
#define __EFILINUX_H__
#define EFILINUX_VERSION_MAJOR 1
#define EFILINUX_VERSION_MINOR 0
#define MEM_ADDR_1MB (1U << 20U)
#define MEM_ADDR_4GB (0xFFFFFFFFU)
extern EFI_SYSTEM_TABLE *sys_table;
extern EFI_BOOT_SERVICES *boot;
/**
* allocate_pages - Allocate memory pages from the system
* @atype: type of allocation to perform
* @mtype: type of memory to allocate
* @num_pages: number of contiguous 4KB pages to allocate
* @memory: used to return the address of allocated pages
*
* Allocate @num_pages physically contiguous pages from the system
* memory and return a pointer to the base of the allocation in
* @memory if the allocation succeeds. On success, the firmware memory
* map is updated accordingly.
*
* If @atype is AllocateAddress then, on input, @memory specifies the
* address at which to attempt to allocate the memory pages.
*/
static inline EFI_STATUS
allocate_pages(EFI_ALLOCATE_TYPE atype, EFI_MEMORY_TYPE mtype,
UINTN num_pages, EFI_PHYSICAL_ADDRESS *memory)
{
return uefi_call_wrapper(boot->AllocatePages, 4, atype,
mtype, num_pages, memory);
}
/**
* free_pages - Return memory allocated by allocate_pages() to the firmware
* @memory: physical base address of the page range to be freed
* @num_pages: number of contiguous 4KB pages to free
*
* On success, the firmware memory map is updated accordingly.
*/
static inline EFI_STATUS
free_pages(EFI_PHYSICAL_ADDRESS memory, UINTN num_pages)
{
return uefi_call_wrapper(boot->FreePages, 2, memory, num_pages);
}
/**
* allocate_pool - Allocate pool memory
* @type: the type of pool to allocate
* @size: number of bytes to allocate from pool of @type
* @buffer: used to return the address of allocated memory
*
* Allocate memory from pool of @type. If the pool needs more memory
* pages are allocated from EfiConventionalMemory in order to grow the
* pool.
*
* All allocations are eight-byte aligned.
*/
static inline EFI_STATUS
allocate_pool(EFI_MEMORY_TYPE type, UINTN size, void **buffer)
{
return uefi_call_wrapper(boot->AllocatePool, 3, type, size, buffer);
}
/**
* free_pool - Return pool memory to the system
* @buffer: the buffer to free
*
* Return @buffer to the system. The returned memory is marked as
* EfiConventionalMemory.
*/
static inline EFI_STATUS free_pool(void *buffer)
{
return uefi_call_wrapper(boot->FreePool, 1, buffer);
}
/**
* get_memory_map - Return the current memory map
* @size: the size in bytes of @map
* @map: buffer to hold the current memory map
* @key: used to return the key for the current memory map
* @descr_size: used to return the size in bytes of EFI_MEMORY_DESCRIPTOR
* @descr_version: used to return the version of EFI_MEMORY_DESCRIPTOR
*
* Get a copy of the current memory map. The memory map is an array of
* EFI_MEMORY_DESCRIPTORs. An EFI_MEMORY_DESCRIPTOR describes a
* contiguous block of memory.
*
* On success, @key is updated to contain an identifer for the current
* memory map. The firmware's key is changed every time something in
* the memory map changes. @size is updated to indicate the size of
* the memory map pointed to by @map.
*
* @descr_size and @descr_version are used to ensure backwards
* compatibility with future changes made to the EFI_MEMORY_DESCRIPTOR
* structure. @descr_size MUST be used when the size of an
* EFI_MEMORY_DESCRIPTOR is used in a calculation, e.g when iterating
* over an array of EFI_MEMORY_DESCRIPTORs.
*
* On failure, and if the buffer pointed to by @map is too small to
* hold the memory map, EFI_BUFFER_TOO_SMALL is returned and @size is
* updated to reflect the size of a buffer required to hold the memory
* map.
*/
static inline EFI_STATUS
get_memory_map(UINTN *size, EFI_MEMORY_DESCRIPTOR *map, UINTN *key,
UINTN *descr_size, UINT32 *descr_version)
{
return uefi_call_wrapper(boot->GetMemoryMap, 5, size, map,
key, descr_size, descr_version);
}
/**
* exit_boot_serivces - Terminate all boot services
* @image: firmware-allocated handle that identifies the image
* @key: key to the latest memory map
*
* This function is called when efilinux wants to take complete
* control of the system. efilinux should not make calls to boot time
* services after this function is called.
*/
static inline EFI_STATUS
exit_boot_services(EFI_HANDLE image, UINTN key)
{
return uefi_call_wrapper(boot->ExitBootServices, 2, image, key);
}
/**
* handle_protocol - Query @handle to see if it supports @protocol
* @handle: the handle being queried
* @protocol: the GUID of the protocol
* @interface: used to return the protocol interface
*
* Query @handle to see if @protocol is supported. If it is supported,
* @interface contains the protocol interface.
*/
static inline EFI_STATUS
handle_protocol(EFI_HANDLE handle, EFI_GUID *protocol, void **interface)
{
return uefi_call_wrapper(boot->HandleProtocol, 3,
handle, protocol, interface);
}
/*
* emalloc_reserved_mem - it is called to allocate memory hypervisor itself
* and trampoline code, and mark the allocate memory as EfiReserved memory
* type so that SOS won't touch it during boot.
* @addr: a pointer to the allocated address on success
* @size: size in bytes of the requested allocation
* @max_addr: the allocated memory must be no more than this threshold
*/
static inline EFI_STATUS emalloc_reserved_mem(EFI_PHYSICAL_ADDRESS *addr,
UINTN size, EFI_PHYSICAL_ADDRESS max_addr)
{
*addr = max_addr;
return allocate_pages(AllocateMaxAddress, EfiReservedMemoryType,
EFI_SIZE_TO_PAGES(size), addr);
}
/*
* emalloc_fixed_addr - it is called to allocate memory hypervisor itself
* when CONFIG_RELOC config is NOT enable.And mark the allocated memory as
* EfiReserved memory type so that SOS won't touch it during boot.
* @addr: a pointer to the allocated address on success
* @size: size in bytes of the requested allocation
*/
static inline EFI_STATUS emalloc_fixed_addr(EFI_PHYSICAL_ADDRESS *addr,
UINTN size, EFI_PHYSICAL_ADDRESS fixed_addr)
{
*addr = fixed_addr;
return allocate_pages(AllocateAddress, EfiReservedMemoryType,
EFI_SIZE_TO_PAGES(size), addr);
}
/**
* exit - Terminate a loaded EFI image
* @image: firmware-allocated handle that identifies the image
* @status: the image's exit code
* @size: size in bytes of @reason. Ignored if @status is EFI_SUCCESS
* @reason: a NUL-terminated status string, optionally followed by binary data
*
* This function terminates @image and returns control to the boot
* services. This function MUST NOT be called until all loaded child
* images have exited. All memory allocated by the image must be freed
* before calling this function, apart from the buffer @reason, which
* will be freed by the firmware.
*/
static inline EFI_STATUS
exit(EFI_HANDLE image, EFI_STATUS status, UINTN size, CHAR16 *reason)
{
return uefi_call_wrapper(boot->Exit, 4, image, status, size, reason);
}
#define PAGE_SIZE 4096
static const CHAR16 *memory_types[] = {
L"EfiReservedMemoryType",
L"EfiLoaderCode",
L"EfiLoaderData",
L"EfiBootServicesCode",
L"EfiBootServicesData",
L"EfiRuntimeServicesCode",
L"EfiRuntimeServicesData",
L"EfiConventionalMemory",
L"EfiUnusableMemory",
L"EfiACPIReclaimMemory",
L"EfiACPIMemoryNVS",
L"EfiMemoryMappedIO",
L"EfiMemoryMappedIOPortSpace",
L"EfiPalCode",
};
static inline const CHAR16 *memory_type_to_str(UINT32 type)
{
if (type > sizeof(memory_types)/sizeof(CHAR16 *))
return L"Unknown";
return memory_types[type];
}
extern EFI_STATUS memory_map(EFI_MEMORY_DESCRIPTOR **map_buf,
UINTN *map_size, UINTN *map_key,
UINTN *desc_size, UINT32 *desc_version);
#endif /* __EFILINUX_H__ */

132
efi-stub/malloc.c Normal file
View File

@@ -0,0 +1,132 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <efi.h>
#include <efilib.h>
#include "efilinux.h"
#include "stdlib.h"
/**
* memory_map - Allocate and fill out an array of memory descriptors
* @map_buf: buffer containing the memory map
* @map_size: size of the buffer containing the memory map
* @map_key: key for the current memory map
* @desc_size: size of the desc
* @desc_version: memory descriptor version
*
* On success, @map_size contains the size of the memory map pointed
* to by @map_buf and @map_key, @desc_size and @desc_version are
* updated.
*/
EFI_STATUS
memory_map(EFI_MEMORY_DESCRIPTOR **map_buf, UINTN *map_size,
UINTN *map_key, UINTN *desc_size, UINT32 *desc_version)
{
EFI_STATUS err;
*map_size = sizeof(**map_buf) * 31;
get_map:
/*
* Because we're about to allocate memory, we may
* potentially create a new memory descriptor, thereby
* increasing the size of the memory map. So increase
* the buffer size by the size of one memory
* descriptor, just in case.
*/
*map_size += sizeof(**map_buf);
err = allocate_pool(EfiLoaderData, *map_size,
(void **)map_buf);
if (err != EFI_SUCCESS) {
Print(L"Failed to allocate pool for memory map");
goto failed;
}
err = get_memory_map(map_size, *map_buf, map_key,
desc_size, desc_version);
if (err != EFI_SUCCESS) {
if (err == EFI_BUFFER_TOO_SMALL) {
/*
* 'map_size' has been updated to reflect the
* required size of a map buffer.
*/
free_pool((void *)*map_buf);
goto get_map;
}
Print(L"Failed to get memory map");
goto failed;
}
failed:
return err;
}
EFI_STATUS dump_e820(void)
{
UINTN map_size, map_key, desc_size;
EFI_MEMORY_DESCRIPTOR *map_buf;
UINTN d, map_end;
UINTN i;
UINT32 desc_version;
EFI_STATUS err;
err = memory_map(&map_buf, &map_size, &map_key,
&desc_size, &desc_version);
if (err != EFI_SUCCESS)
goto fail;
d = (UINTN)map_buf;
map_end = (UINTN)map_buf + map_size;
for (i = 0; d < map_end; d += desc_size, i++) {
EFI_MEMORY_DESCRIPTOR *desc;
EFI_PHYSICAL_ADDRESS start, end;
desc = (EFI_MEMORY_DESCRIPTOR *)d;
if (desc->Type != EfiConventionalMemory)
continue;
start = desc->PhysicalStart;
end = start + (desc->NumberOfPages << EFI_PAGE_SHIFT);
Print(L"[%d]start:%lx, end:%lx, type:%d\n", i, start, end, desc->Type);
}
free_pool(map_buf);
fail:
return err;
}

179
efi-stub/multiboot.h Normal file
View File

@@ -0,0 +1,179 @@
/* [ORIGIN: src/sys/arch/i386/include/... */
/* $NetBSD: multiboot.h,v 1.8 2009/02/22 18:05:42 ahoka Exp $ */
/*-
* Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Julio M. Merino Vidal.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* multiboot.h
*/
#ifndef _MULTIBOOT_H
#define _MULTIBOOT_H
#include <stdint.h>
struct multiboot_info;
extern struct multiboot_info mbi;
/*
* Multiboot header structure.
*/
#define MULTIBOOT_HEADER_MAGIC 0x1BADB002
#define MULTIBOOT_HEADER_MODS_ALIGNED 0x00000001
#define MULTIBOOT_HEADER_WANT_MEMORY 0x00000002
#define MULTIBOOT_HEADER_HAS_VBE 0x00000004
#define MULTIBOOT_HEADER_HAS_ADDR 0x00010000
#if !defined(_LOCORE)
struct multiboot_header {
uint32_t mh_magic;
uint32_t mh_flags;
uint32_t mh_checksum;
/* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
uint32_t mh_header_addr;
uint32_t mh_load_addr;
uint32_t mh_load_end_addr;
uint32_t mh_bss_end_addr;
uint32_t mh_entry_addr;
/* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE. */
uint32_t mh_mode_type;
uint32_t mh_width;
uint32_t mh_height;
uint32_t mh_depth;
};
#endif /* !defined(_LOCORE) */
/*
* Symbols defined in locore.S.
*/
extern struct multiboot_header *Multiboot_Header;
/*
* Multiboot information structure.
*/
#define MULTIBOOT_INFO_MAGIC 0x2BADB002U
#define MULTIBOOT_INFO_HAS_MEMORY 0x00000001U
#define MULTIBOOT_INFO_HAS_BOOT_DEVICE 0x00000002U
#define MULTIBOOT_INFO_HAS_CMDLINE 0x00000004U
#define MULTIBOOT_INFO_HAS_MODS 0x00000008U
#define MULTIBOOT_INFO_HAS_AOUT_SYMS 0x00000010U
#define MULTIBOOT_INFO_HAS_ELF_SYMS 0x00000020U
#define MULTIBOOT_INFO_HAS_MMAP 0x00000040U
#define MULTIBOOT_INFO_HAS_DRIVES 0x00000080U
#define MULTIBOOT_INFO_HAS_CONFIG_TABLE 0x00000100U
#define MULTIBOOT_INFO_HAS_LOADER_NAME 0x00000200U
#define MULTIBOOT_INFO_HAS_APM_TABLE 0x00000400U
#define MULTIBOOT_INFO_HAS_VBE 0x00000800U
#if !defined(_LOCORE)
struct multiboot_info {
uint32_t mi_flags;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_MEMORY. */
uint32_t mi_mem_lower;
uint32_t mi_mem_upper;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_BOOT_DEVICE. */
uint8_t mi_boot_device_part3;
uint8_t mi_boot_device_part2;
uint8_t mi_boot_device_part1;
uint8_t mi_boot_device_drive;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_CMDLINE. */
uint32_t mi_cmdline;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_MODS. */
uint32_t mi_mods_count;
uint32_t mi_mods_addr;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_{AOUT,ELF}_SYMS. */
uint32_t mi_elfshdr_num;
uint32_t mi_elfshdr_size;
uint32_t mi_elfshdr_addr;
uint32_t mi_elfshdr_shndx;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_MMAP. */
uint32_t mi_mmap_length;
uint32_t mi_mmap_addr;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_DRIVES. */
uint32_t mi_drives_length;
uint32_t mi_drives_addr;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_CONFIG_TABLE. */
uint32_t unused_mi_config_table;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_LOADER_NAME. */
uint32_t mi_loader_name;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_APM. */
uint32_t unused_mi_apm_table;
/* Valid if mi_flags sets MULTIBOOT_INFO_HAS_VBE. */
uint32_t unused_mi_vbe_control_info;
uint32_t unused_mi_vbe_mode_info;
uint32_t unused_mi_vbe_interface_seg;
uint32_t unused_mi_vbe_interface_off;
uint32_t unused_mi_vbe_interface_len;
}__attribute__((aligned(8)));
/*
* Memory mapping. This describes an entry in the memory mappings table
* as pointed to by mi_mmap_addr.
*
* Be aware that mm_size specifies the size of all other fields *except*
* for mm_size. In order to jump between two different entries, you
* have to count mm_size + 4 bytes.
*/
struct __attribute__((packed)) multiboot_mmap {
uint32_t mm_size;
uint64_t mm_base_addr;
uint64_t mm_length;
uint32_t mm_type;
};
/*
* Modules. This describes an entry in the modules table as pointed
* to by mi_mods_addr.
*/
struct multiboot_module {
uint32_t mmo_start;
uint32_t mmo_end;
char * mmo_string;
uint32_t mmo_reserved;
};
#endif /* !defined(_LOCORE) */
#endif /* _MULTIBOOT_H */

172
efi-stub/pe.c Normal file
View File

@@ -0,0 +1,172 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file contains some wrappers around the gnu-efi functions. As
* we're not going through uefi_call_wrapper() directly, this allows
* us to get some type-safety for function call arguments and for the
* compiler to check that the number of function call arguments is
* correct.
*
* It's also a good place to document the EFI interface.
*/
#include <efi.h>
#include <efilib.h>
#include "stdlib.h"
#define DOS_FILE_MAGIC_NUMBER 0x5A4D /* "MZ" */
struct DosFileHeader {
uint16_t mMagic;
uint16_t LastSize;
uint16_t nBlocks;
uint16_t nReloc;
uint16_t HdrSize;
uint16_t MinAlloc;
uint16_t MaxAlloc;
uint16_t ss;
uint16_t sp;
uint16_t Checksum;
uint16_t ip;
uint16_t cs;
uint16_t RelocPos;
uint16_t nOverlay;
uint16_t reserved[4];
uint16_t OEMId;
uint16_t OEMInfo;
uint16_t reserved2[10];
uint32_t ExeHeader;
} __attribute__((packed));
#define IMAGE_FILE_MACHINE_I386 0x14c
#define IMAGE_FILE_MACHINE_AMD64 0x8664
#define PE_FILE_MAGIC_NUMBER 0x00004550 /* "PE\0\0" */
struct PeHeader {
uint32_t mMagic;
uint16_t mMachine;
uint16_t mNumberOfSections;
uint32_t mTimeDateStamp;
uint32_t mPointerToSymbolTable;
uint32_t mNumberOfSymbols;
uint16_t mSizeOfOptionalHeader;
uint16_t mCharacteristics;
} __attribute__((packed));
struct OptionHeader {
uint16_t Format;
uint8_t MajorLinkVer;
uint8_t MinorLinkVer;
uint32_t CodeSize;
uint32_t InitializedDataSize;
uint32_t UninitializedDataSize;
uint32_t EntryPoint;
uint32_t BaseOfCode;
uint32_t BaseOfDate;
} __attribute__((packed));
struct PeSectionHeader {
char mName[8];
uint32_t mVirtualSize;
uint32_t mVirtualAddress;
uint32_t mSizeOfRawData;
uint32_t mPointerToRawData;
uint32_t mPointerToRealocations;
uint32_t mPointerToLinenumbers;
uint16_t mNumberOfRealocations;
uint16_t mNumberOfLinenumbers;
uint32_t mCharacteristics;
} __attribute__((packed));
EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size)
{
struct PeSectionHeader *ph;
struct DosFileHeader *dh;
struct PeHeader *pe;
UINTN i;
UINTN offset;
dh = (struct DosFileHeader *)base;
if (dh->mMagic != DOS_FILE_MAGIC_NUMBER)
return EFI_LOAD_ERROR;
pe = (struct PeHeader *)&base[dh->ExeHeader];
if (pe->mMagic != PE_FILE_MAGIC_NUMBER)
return EFI_LOAD_ERROR;
if ((pe->mMachine != IMAGE_FILE_MACHINE_AMD64)
&& (pe->mMachine != IMAGE_FILE_MACHINE_I386))
return EFI_LOAD_ERROR;
offset = dh->ExeHeader + sizeof(*pe) + pe->mSizeOfOptionalHeader;
for (i = 0; i < pe->mNumberOfSections; i++) {
ph = (struct PeSectionHeader *)&base[offset];
if (CompareMem(ph->mName, section, strlen(section)) == 0) {
*vaddr = (UINTN)ph->mVirtualAddress;
*size = (UINTN)ph->mVirtualSize;
break;
}
offset += sizeof(*ph);
}
return EFI_SUCCESS;
}
EFI_IMAGE_ENTRY_POINT get_pe_entry(CHAR8 *base)
{
struct DosFileHeader* dh;
struct PeHeader* pe;
struct OptionHeader* oh;
UINTN offset;
dh = (struct DosFileHeader *)base;
if (dh->mMagic != DOS_FILE_MAGIC_NUMBER)
return NULL;
pe = (struct PeHeader *)&base[dh->ExeHeader];
if (pe->mMagic != PE_FILE_MAGIC_NUMBER)
return NULL;
if ((pe->mMachine != IMAGE_FILE_MACHINE_AMD64)
&& (pe->mMachine != IMAGE_FILE_MACHINE_I386))
return NULL;
offset = dh->ExeHeader + sizeof(*pe);
oh = (struct OptionHeader*)&base[offset];
return (EFI_IMAGE_ENTRY_POINT)((UINT64)base + oh->EntryPoint);
}

129
efi-stub/stdlib.h Normal file
View File

@@ -0,0 +1,129 @@
/*
* Copyright (c) 2011, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file contains some wrappers around the gnu-efi functions. As
* we're not going through uefi_call_wrapper() directly, this allows
* us to get some type-safety for function call arguments and for the
* compiler to check that the number of function call arguments is
* correct.
*
* It's also a good place to document the EFI interface.
*/
#ifndef __STDLIB_H__
#define __STDLIB_H__
static inline void memset(void *dstv, char ch, UINTN size)
{
char *dst = dstv;
int32_t i;
for (i = 0; i < size; i++)
dst[i] = ch;
}
static inline void memcpy(char *dst, const char *src, UINTN size)
{
int32_t i;
for (i = 0; i < size; i++)
*dst++ = *src++;
}
static inline int32_t strlen(const char *str)
{
int32_t len;
len = 0;
while (*str++)
len++;
return len;
}
static inline CHAR16 *strstr_16(CHAR16 *haystack, CHAR16 *needle)
{
CHAR16 *p;
CHAR16 *word = NULL;
UINTN len = StrLen(needle);
if (!len)
return NULL;
p = haystack;
while (*p) {
if (!StrnCmp(p, needle, len)) {
word = p;
break;
}
p++;
}
return (CHAR16*)word;
}
static inline CHAR16 *ch8_2_ch16(char *str8)
{
UINTN len, i;
CHAR16 *str16;
len = strlen(str8);
str16 = AllocatePool((len + 1) * sizeof(CHAR16));
for (i = 0; i < len; i++)
str16[i] = str8[i];
str16[len] = 0;
return str16;
}
static inline char *ch16_2_ch8(CHAR16 *str16)
{
UINTN len, i;
char *str8;
len = StrLen(str16);
str8 = AllocatePool((len + 1) * sizeof(char));
for (i = 0; i < len; i++)
str8[i] = str16[i];
str8[len] = 0;
return str8;
}
#endif /* __STDLIB_H__ */