[PATCH] libpayload: Add multiboot support

Make libpayload applications multiboot compatible.  Add the
multiboot OS table and grok the loader table, especially the
memory map and the command line.  This makes libpayload 
applications loadable by GRUB.

Signed-off-by: Jordan Crouse <jordan.crouse@amd.com>
Acked-by: Peter Stuge <peter@stuge.se>


git-svn-id: svn://svn.coreboot.org/coreboot/trunk@3673 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
This commit is contained in:
Jordan Crouse 2008-10-20 16:51:43 +00:00
parent 369a5f6c7a
commit 20c9cf12a4
10 changed files with 234 additions and 3 deletions

View File

@ -35,6 +35,14 @@ config TARGET_I386
bool bool
default y default y
menu "Architecture Options"
config MULTIBOOT
bool "Multiboot header support"
default y
endmenu
menu "Standard Libraries" menu "Standard Libraries"
config LIBC config LIBC

View File

@ -76,6 +76,11 @@ TARGETS-y :=
BUILD-y := crypto/Makefile.inc libc/Makefile.inc drivers/Makefile.inc BUILD-y := crypto/Makefile.inc libc/Makefile.inc drivers/Makefile.inc
BUILD-$(CONFIG_TINYCURSES) += curses/Makefile.inc BUILD-$(CONFIG_TINYCURSES) += curses/Makefile.inc
# The primary target needs to be here before we include the
# other files
all: lib
include $(PLATFORM-y) $(BUILD-y) include $(PLATFORM-y) $(BUILD-y)
OBJS := $(patsubst %,$(obj)/%,$(TARGETS-y)) OBJS := $(patsubst %,$(obj)/%,$(TARGETS-y))

View File

@ -31,3 +31,5 @@ TARGETS-y += i386/head.S.o i386/main.o i386/sysinfo.o
TARGETS-y += i386/timer.o i386/coreboot.o i386/util.S.o TARGETS-y += i386/timer.o i386/coreboot.o i386/util.S.o
TARGETS-y += i386/exec.S.o i386/virtual.o TARGETS-y += i386/exec.S.o i386/virtual.o
# Multiboot support is configurable
TARGETS-$(CONFIG_MULTIBOOT) += i386/multiboot.o

View File

@ -27,6 +27,7 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
.code32
.global _entry, _leave .global _entry, _leave
.text .text
.align 4 .align 4
@ -42,6 +43,21 @@ _entry:
/* We're back - go back to the bootloader. */ /* We're back - go back to the bootloader. */
ret ret
.align 4
#define MB_MAGIC 0x1BADB002
#define MB_FLAGS 0x00010003
mb_header:
.long MB_MAGIC
.long MB_FLAGS
.long -(MB_MAGIC + MB_FLAGS)
.long mb_header
.long _start
.long _edata
.long _end
.long _init
/* /*
* This function saves off the previous stack and switches us to our * This function saves off the previous stack and switches us to our
* own execution environment. * own execution environment.
@ -53,6 +69,11 @@ _init:
/* Store current stack pointer. */ /* Store current stack pointer. */
movl %esp, %esi movl %esp, %esi
/* Store EAX and EBX */
movl %eax,loader_eax
movl %ebx,loader_ebx
/* Setup new stack. */ /* Setup new stack. */
movl $_stack, %ebx movl $_stack, %ebx

View File

@ -29,13 +29,21 @@
#include <libpayload.h> #include <libpayload.h>
unsigned long loader_eax; /**< The value of EAX passed from the loader */
unsigned long loader_ebx; /**< The value of EBX passed from the loader */
unsigned int main_argc; /**< The argc value to pass to main() */
/** The argv value to pass to main() */
char *main_argv[MAX_ARGC_COUNT];
/** /**
* This is our C entry function - set up the system * This is our C entry function - set up the system
* and jump into the payload entry point. * and jump into the payload entry point.
*/ */
void start_main(void) void start_main(void)
{ {
extern int main(void); extern int main(int argc, char **argv);
/* Set up the consoles. */ /* Set up the consoles. */
console_init(); console_init();
@ -52,7 +60,8 @@ void start_main(void)
* Go to the entry point. * Go to the entry point.
* In the future we may care about the return value. * In the future we may care about the return value.
*/ */
(void) main();
(void) main(main_argc, (main_argc != 0) ? main_argv : NULL);
/* /*
* Returning here will go to the _leave function to return * Returning here will go to the _leave function to return

View File

@ -0,0 +1,96 @@
/*
* This file is part of the libpayload project.
*
* Copyright (C) 2008 Advanced Micro Devices, Inc.
*
* 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <config.h>
#include <libpayload.h>
#include <multiboot_tables.h>
extern unsigned long loader_eax;
extern unsigned long loader_ebx;
static void mb_parse_mmap(struct multiboot_header *table,
struct sysinfo_t *info)
{
u8 *start = (u8 *) phys_to_virt(table->mmap_addr);
u8 *ptr = start;
info->n_memranges = 0;
while(ptr < (start + table->mmap_length)) {
struct multiboot_mmap *mmap = (struct multiboot_mmap *) ptr;
/* 1 == normal RAM. Ignore everything else for now */
if (mmap->type == 1) {
info->memrange[info->n_memranges].base = mmap->addr;
info->memrange[info->n_memranges].size = mmap->length;
if (++info->n_memranges == SYSINFO_MAX_MEM_RANGES)
return;
}
ptr += (mmap->size + sizeof(mmap->size));
}
}
static void mb_parse_cmdline(struct multiboot_header *table)
{
extern int main_argc;
extern char *main_argv[];
char *c = phys_to_virt(table->cmdline);
while(*c != '\0' && main_argc < MAX_ARGC_COUNT) {
main_argv[main_argc++] = c;
for( ; *c != '\0' && !isspace(*c); c++);
if (*c) {
*c = 0;
c++;
}
}
}
int get_multiboot_info(struct sysinfo_t *info)
{
struct multiboot_header *table;
if (loader_eax != MULTIBOOT_MAGIC)
return -1;
table = (struct multiboot_header *) phys_to_virt(loader_ebx);
if (table->flags & MULTIBOOT_FLAGS_MMAP)
mb_parse_mmap(table, info);
if (table->flags & MULTIBOOT_FLAGS_CMDLINE)
mb_parse_cmdline(table);
return 0;
}

View File

@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#include <libpayload.h> #include <libpayload.h>
#include <multiboot_tables.h>
/** /**
* This is a global structure that is used through the library - we set it * This is a global structure that is used through the library - we set it
@ -48,7 +49,15 @@ void lib_get_sysinfo(void)
/* Get the CPU speed (for delays). */ /* Get the CPU speed (for delays). */
lib_sysinfo.cpu_khz = get_cpu_speed(); lib_sysinfo.cpu_khz = get_cpu_speed();
/* Get the memory information. */ #ifdef CONFIG_MULTIBOOT
/* Get the information from the multiboot tables,
* if they exist */
get_multiboot_info(&lib_sysinfo);
#endif
/* Get information from the coreboot tables,
* if they exist */
get_coreboot_info(&lib_sysinfo); get_coreboot_info(&lib_sysinfo);
if (!lib_sysinfo.n_memranges) { if (!lib_sysinfo.n_memranges) {

View File

@ -64,6 +64,8 @@
#define RAND_MAX 0x7fffffff #define RAND_MAX 0x7fffffff
#define MAX_ARGC_COUNT 10
/* /*
* Payload information parameters - these are used to pass information * Payload information parameters - these are used to pass information
* to the entity loading the payload. * to the entity loading the payload.
@ -407,6 +409,7 @@ int lfclose(struct LFILE *file);
* @{ * @{
*/ */
int get_coreboot_info(struct sysinfo_t *info); int get_coreboot_info(struct sysinfo_t *info);
int get_multiboot_info(struct sysinfo_t *info);
void lib_get_sysinfo(void); void lib_get_sysinfo(void);

View File

@ -0,0 +1,76 @@
/*
* This file is part of the libpayload project.
*
* Copyright (C) 2008 Advanced Micro Devices, Inc.
*
* 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 MULTIBOOT_TABLES_H_
#define MULTIBOOT_TABLES_H_
#include <arch/types.h>
#define MULTIBOOT_MAGIC 0x2BADB002UL
#define MULTIBOOT_FLAGS_MMAP (1 << 6)
#define MULTIBOOT_FLAGS_CMDLINE (1 << 2)
struct multiboot_header {
u32 flags;
u32 mem_lower;
u32 mem_higher;
u32 boot_device;
u32 cmdline;
u32 mods_count;
u32 mods_addr;
u32 syms[4];
u32 mmap_length;
u32 mmap_addr;
u32 drives_length;
u32 drives_addr;
u32 config_table;
u32 boot_loader_name;
u32 apm_table;
u32 vbe_control_info;
u32 vbe_mode_info;
u32 vbe_mode;
u32 vbe_interface_seg;
u32 vbe_interface_off;
u32 vbe_interface_len;
};
struct multiboot_mmap {
u32 size;
u64 addr;
u64 length;
u32 type;
} __attribute((packed));
#endif

View File

@ -60,6 +60,8 @@ SECTIONS
*(.data.*) *(.data.*)
} }
_edata = .;
.bss : { .bss : {
*(.bss) *(.bss)
*(.bss.*) *(.bss.*)