Skip to content

Commit

Permalink
gummiboot: cosmetic fixes in preparation for RFC release
Browse files Browse the repository at this point in the history
Signed-off-by: Todor Minchev <[email protected]>
  • Loading branch information
Todor Minchev committed Oct 30, 2014
1 parent d146093 commit a5734f2
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 218 deletions.
11 changes: 5 additions & 6 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ bin_PROGRAMS = gummiboot
gummiboot_SOURCES = \
src/setup/setup.c \
src/setup/efivars.c \
src/setup/efivars.h
src/setup/efivars.h

gummiboot_CPPFLAGS = \
$(AM_CPPFLAGS) \
-DMACHINE_TYPE_NAME=\"$(MACHINE_TYPE_NAME)\" \
-DGUMMIBOOTLIBDIR=\"$(gummibootlibdir)\"

-DGUMMIBOOTLIBDIR=\"$(gummibootlibdir)\"

gummiboot_CFLAGS = \
$(AM_CFLAGS) \
Expand Down Expand Up @@ -83,7 +82,7 @@ efi_sources = \
src/efi/gummiboot.c \
src/multiboot2/multiboot2.c \
src/elf/elf.c \
src/elf/memcpy.c
src/elf/memcpy.c

efi_headers = \
src/efi/util.h \
Expand All @@ -92,7 +91,7 @@ efi_headers = \
src/multiboot2/multiboot2.h \
src/multiboot2/multiboot2_util.h \
src/multiboot2/acpi.h \
src/elf/elf.h
src/elf/elf.h

efi_cppflags = \
$(EFI_CPPFLAGS) \
Expand All @@ -101,7 +100,7 @@ efi_cppflags = \
-I$(EFI_INC_DIR)/efi/$(ARCH) \
-DMACHINE_TYPE_NAME=\"$(MACHINE_TYPE_NAME)\" \
-I$(top_builddir)/src/multiboot2 \
-I$(top_builddir)/src/elf
-I$(top_builddir)/src/elf

efi_cflags = \
$(EFI_CFLAGS) \
Expand Down
41 changes: 20 additions & 21 deletions src/efi/gummiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "console.h"
#include "graphics.h"


#ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
#define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
#endif
Expand Down Expand Up @@ -1093,12 +1092,12 @@ static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR1
}

if (strcmpa((CHAR8 *)"multiboot2", key) == 0) {
FreePool(entry->multiboot2);
FreePool(entry->multiboot2);
entry->multiboot2 = stra_to_path(value);
continue;
}
if (strcmpa((CHAR8 *)"acm", key) == 0) {
FreePool(entry->acm);
FreePool(entry->acm);
entry->acm = stra_to_path(value);
continue;
}
Expand Down Expand Up @@ -1637,33 +1636,33 @@ static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, con

path = FileDevicePath(entry->device, entry->loader);
if (!path) {
Print(L"Error getting device path.");
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_INVALID_PARAMETER;
Print(L"Error getting device path.");
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_INVALID_PARAMETER;
}

if(entry->multiboot2){
err = copy_file_buf(parent_image, entry->multiboot2, &os_buf, 0) ;
if (EFI_ERROR(err))
goto out;
err = copy_file_buf(parent_image, entry->multiboot2, &os_buf, 0) ;
if (EFI_ERROR(err))
goto out;

/*parse multiboot2 header*/
err = parse_header(os_buf,MULTIBOOT_SEARCH) ;
/*parse multiboot2 header*/
err = parse_header(os_buf,MULTIBOOT_SEARCH) ;

/*load as elf binary*/
if(err == EFI_LOAD_ELF){
err = load_elf(os_buf, &elf_entry) ;
if (EFI_ERROR(err))
goto out;
}else if (EFI_ERROR(err))
goto out;
/*load as elf binary*/
if(err == EFI_LOAD_ELF){
err = load_elf(os_buf, &elf_entry) ;
if (EFI_ERROR(err))
goto out;
}else if (EFI_ERROR(err))
goto out;

err = populate_mbi2(parent_image, entry, &mbi2_buf) ;
if (EFI_ERROR(err))
goto out;
if (EFI_ERROR(err))
goto out;

start_elf(elf_entry, mbi2_buf);
//we should never reach this point
/* we should never reach this point */
}

err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
Expand Down
196 changes: 94 additions & 102 deletions src/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,69 @@ static inline void *memset(void *b, int c, uint32_t len)

EFI_STATUS load_elf(CHAR8 *buf, void **entry)
{
elf_header_t * elf = NULL ;

int i ;
elf_header_t * elf = NULL ;

if ( buf == NULL ) {
Print(L"elf.c : %d: Buffer is zero.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 1 * 1000 * 1000);
return EFI_LOAD_ERROR;
}else
elf = (elf_header_t *) buf ;

/* validate the ELF header */
if (elf->e_ident[EI_MAG0] != ELFMAG0
|| elf->e_ident[EI_MAG1] != ELFMAG1
|| elf->e_ident[EI_MAG2] != ELFMAG2
|| elf->e_ident[EI_MAG3] != ELFMAG3
|| elf->e_ident[EI_DATA] != ELFDATA2LSB){
Print(L"elf.c : %d: Invalid ELF magic.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

if (elf->e_ident[EI_CLASS] != ELFCLASS32 || elf->e_machine != EM_386
|| elf->e_version != EV_CURRENT){
Print(L"elf.c : %d: Invalid ELF class.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

if (elf->e_type != ET_EXEC && elf->e_type != ET_DYN){
Print(L"elf.c : %d: Invalid ELF type.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

/* load loadable segments into memory */
for ( i = 0; i < elf->e_phnum; i++ ) {
elf_program_header_t *ph = (elf_program_header_t *)
((void *)elf + elf->e_phoff + i*elf->e_phentsize);

if ( ph->p_type == PT_LOAD ) {

memcpy((void *)(uint64_t)ph->p_paddr, (void *)elf + ph->p_offset,
ph->p_filesz);

memset((void *)(uint64_t)(ph->p_paddr + ph->p_filesz), 0,
ph->p_memsz - ph->p_filesz);
}
}

*entry = (void*)(uint64_t)elf->e_entry;

return EFI_SUCCESS;
if ( buf == NULL ) {
Print(L"elf.c : %d: Buffer is zero.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 1 * 1000 * 1000);
return EFI_LOAD_ERROR;
}else
elf = (elf_header_t *) buf ;

/* validate the ELF header */
if (elf->e_ident[EI_MAG0] != ELFMAG0
|| elf->e_ident[EI_MAG1] != ELFMAG1
|| elf->e_ident[EI_MAG2] != ELFMAG2
|| elf->e_ident[EI_MAG3] != ELFMAG3
|| elf->e_ident[EI_DATA] != ELFDATA2LSB){
Print(L"elf.c : %d: Invalid ELF magic.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

if (elf->e_ident[EI_CLASS] != ELFCLASS32 || elf->e_machine != EM_386
|| elf->e_version != EV_CURRENT){
Print(L"elf.c : %d: Invalid ELF class.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

if (elf->e_type != ET_EXEC && elf->e_type != ET_DYN){
Print(L"elf.c : %d: Invalid ELF type.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
return EFI_LOAD_ERROR;
}

/* load loadable segments into memory */
for ( i = 0; i < elf->e_phnum; i++ ) {
elf_program_header_t *ph = (elf_program_header_t *)
((void *)elf + elf->e_phoff + i*elf->e_phentsize);

if ( ph->p_type == PT_LOAD ) {
memcpy((void *)(uint64_t)ph->p_paddr, (void *)elf + ph->p_offset,
ph->p_filesz);

memset((void *)(uint64_t)(ph->p_paddr + ph->p_filesz), 0,
ph->p_memsz - ph->p_filesz);
}
}

*entry = (void*)(uint64_t)elf->e_entry;
return EFI_SUCCESS;
}

void start_elf(void *buf, void* mbi2_buf){

struct seg_desc global_desc_table[] = {

/* NULL descriptor 0x0*/
{ 0, },

/* Code32 segment descriptor 0x08*/
populate_4k_seg_descriptor(0, 0xffffffff, 0x1, gdt_cs_flags_limit),

/* Data32 segment descriptor 0x10*/
populate_4k_seg_descriptor(0, 0xffffffff, 0x1, gdt_ds_flags_limit),
/* Task state segment descriptor 0x18*/
{ 0, },

/* Code16 segment descriptor 0x20*/
populate_4k_seg_descriptor(0, 0xffffffff, 0x0, gdt_cs_flags_limit),

Expand All @@ -101,57 +94,56 @@ void start_elf(void *buf, void* mbi2_buf){
gdt_desc.sz = sizeof(global_desc_table) - 1;
gdt_desc.addr = (uint64_t) global_desc_table;

if(!buf){
Print(L"elf.c : %d : Missing ELF entry point. Resetting.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS,0,0);
}

if(!mbi2_buf){
Print(L"elf.c : %d : Missing MBI2 buffer. Resetting.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS,0,0);

}

__asm__ __volatile__(
"lgdt %0 ;" /* load GDT into GDTR*/
"pushq %1 ;" /* push code segment selector on the stack*/
"leaq 1f(%%rip), %%rax ;" /* address to jump into*/
"pushq %%rax ;" /* push jump address on the stack */
"retfq ;"

"1:mov %2, %%ax ;" /* we are in compatibility mode*/
"mov %%ax, %%ds ;" /* reset the data segments */
"mov %%ax, %%es ;"
"mov %%ax, %%fs ;"
"mov %%ax, %%gs ;"
"mov %%ax, %%ss ;"

"mov %%cr0, %%rax ;"
"btcl $31, %%eax ;" /* disable paging*/
"mov %%rax, %%cr0 ;"
"movl $0x0c0000080, %%ecx ;" /* EFER MSR number */
"rdmsr ;" /* Read EFER. */
"btcl $8, %%eax ;" /* Set LME=0. */
"wrmsr ;" /* Write EFER. */
:: "m" (gdt_desc),
"i" (cs_sel),
"i" (ds_sel)); /* Goodbye long mode, we are back to protected mode */

/* hand over to tboot */
if(!buf){
Print(L"elf.c : %d : Missing ELF entry point. Resetting.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS,0,0);
}

if(!mbi2_buf){
Print(L"elf.c : %d : Missing MBI2 buffer. Resetting.\n", __LINE__);
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS,0,0);
}

__asm__ __volatile__(
"lgdt %0 ;" /* load GDT into GDTR*/
"pushq %1 ;" /* push code segment selector on the stack*/
"leaq 1f(%%rip), %%rax ;" /* address to jump into*/
"pushq %%rax ;" /* push jump address on the stack */
"retfq ;"

"1:mov %2, %%ax ;" /* we are in compatibility mode*/
"mov %%ax, %%ds ;" /* reset the data segments */
"mov %%ax, %%es ;"
"mov %%ax, %%fs ;"
"mov %%ax, %%gs ;"
"mov %%ax, %%ss ;"

"mov %%cr0, %%rax ;"
"btcl $31, %%eax ;" /* disable paging*/
"mov %%rax, %%cr0 ;"
"movl $0x0c0000080, %%ecx ;" /* EFER MSR number */
"rdmsr ;" /* Read EFER. */
"btcl $8, %%eax ;" /* Set LME=0. */
"wrmsr ;" /* Write EFER. */
:: "m" (gdt_desc),
"i" (cs_sel),
"i" (ds_sel)); /* Goodbye long mode, we are back to protected mode */

/* hand over to tboot */
/* EAX - multiboot2 magic
EBX - MBI2
ECX - tboot entry point */
ECX - tboot entry point */

__asm__ __volatile__ (
"push %2 ;"
"mov $0x36d76289, %%eax ;" /* TODO - Shouldn't be really needed, but EAX gets corrupted ? */
"ret ;"
::
"a" (MULTIBOOT2_BOOTLOADER_MAGIC),
"b" (mbi2_buf),
"c" (buf));
"push %2 ;"
"mov $0x36d76289, %%eax ;" /* Shouldn't be really needed, but EAX gets corrupted ? */
"ret ;"
::
"a" (MULTIBOOT2_BOOTLOADER_MAGIC),
"b" (mbi2_buf),
"c" (buf));

/* WE SHOULD NEVER REACH HERE */
}
12 changes: 6 additions & 6 deletions src/multiboot2/acpi.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#define EFI_ACPI_TABLE_GUID \
{ 0xeb9d2d30, 0x2d88, 0x11d3, \
{ 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \
#define EFI_ACPI_TABLE_GUID \
{ 0xeb9d2d30, 0x2d88, 0x11d3, \
{ 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \
}

#define EFI_ACPI_20_TABLE_GUID \
{ 0x8868e871, 0xe4f1, 0x11d3, \
{ 0xbc, 0x22, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
#define EFI_ACPI_20_TABLE_GUID \
{ 0x8868e871, 0xe4f1, 0x11d3, \
{ 0xbc, 0x22, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
}

typedef struct {
Expand Down
Loading

0 comments on commit a5734f2

Please sign in to comment.