From patchwork Mon Nov 30 22:26:51 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Lackorzynski X-Patchwork-Id: 39864 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A76B51007D2 for ; Tue, 1 Dec 2009 09:33:50 +1100 (EST) Received: from localhost ([127.0.0.1]:60669 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NFEoV-00072r-Ji for incoming@patchwork.ozlabs.org; Mon, 30 Nov 2009 17:33:47 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NFEhw-0003i2-BR for qemu-devel@nongnu.org; Mon, 30 Nov 2009 17:27:00 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NFEhr-0003cg-PQ for qemu-devel@nongnu.org; Mon, 30 Nov 2009 17:26:59 -0500 Received: from [199.232.76.173] (port=59636 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NFEhr-0003ca-BY for qemu-devel@nongnu.org; Mon, 30 Nov 2009 17:26:55 -0500 Received: from os.inf.tu-dresden.de ([141.76.48.99]:58594) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NFEhq-0002LF-Kp for qemu-devel@nongnu.org; Mon, 30 Nov 2009 17:26:55 -0500 Received: from erwin.inf.tu-dresden.de ([141.76.48.80] helo=os.inf.tu-dresden.de) by os.inf.tu-dresden.de with esmtps (TLSv1:AES128-SHA:128) (Exim 4.71) id 1NFEho-0005eA-Qj for qemu-devel@nongnu.org; Mon, 30 Nov 2009 23:26:53 +0100 Date: Mon, 30 Nov 2009 23:26:51 +0100 From: Adam Lackorzynski To: qemu-devel@nongnu.org Message-ID: <20091130222650.GB21068@os.inf.tu-dresden.de> References: <20091130222544.GA21068@os.inf.tu-dresden.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20091130222544.GA21068@os.inf.tu-dresden.de> User-Agent: Mutt/1.5.20 (2009-06-14) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [Qemu-devel] [PATCH 2/2] multiboot: Support arbitrary number of modules X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org multiboot: Support arbitrary number of modules Signed-off-by: Adam Lackorzynski --- hw/pc.c | 216 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 143 insertions(+), 73 deletions(-) diff --git a/hw/pc.c b/hw/pc.c index 6bcfe1b..163bec1 100644 --- a/hw/pc.c +++ b/hw/pc.c @@ -507,6 +507,79 @@ static long get_file_size(FILE *f) #error multiboot struct needs to fit in 16 bit real mode #endif +enum { + /* Multiboot info */ + MBI_FLAGS = 0, + MBI_MEM_LOWER = 4, + MBI_MEM_UPPER = 8, + MBI_BOOT_DEVICE = 12, + MBI_CMDLINE = 16, + MBI_MODS_COUNT = 20, + MBI_MODS_ADDR = 24, + MBI_MMAP_ADDR = 48, + + MBI_SIZE = 88, + + /* Multiboot modules */ + MB_MOD_START = 0, + MB_MOD_END = 4, + MB_MOD_CMDLINE = 8, + + MB_MOD_SIZE = 16, + + /* Region offsets */ + ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0, + ADDR_MBI = ADDR_E820_MAP + 0x500, + + /* Multiboot flags */ + MULTIBOOT_FLAGS_MEMORY = 1 << 0, + MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1, + MULTIBOOT_FLAGS_CMDLINE = 1 << 2, + MULTIBOOT_FLAGS_MODULES = 1 << 3, + MULTIBOOT_FLAGS_MMAP = 1 << 6, +}; + +struct mb_state { + void *mb_buf; /* buffer holding kernel, cmdlines and mb_infos */ + uint32_t mb_buf_phys; /* address in target */ + int mb_buf_size; /* size of mb_buf in bytes */ + int mb_mods_avail; /* available slots for mb modules infos */ + int mb_mods_count; /* currently used slots of mb modules */ + int offset_mbinfo; /* offset of mb-info's in bytes */ + int offset_cmdlines; /* offset in buffer for cmdlines */ + int offset_mods; /* offset of modules in bytes */ +}; + +static uint32_t mb_add_cmdline(struct mb_state *s, const char *cmdline) +{ + int len = strlen(cmdline) + 1; + uint32_t p = s->offset_cmdlines; + + pstrcpy((char *)s->mb_buf + p, len, cmdline); + s->offset_cmdlines += len; + return s->mb_buf_phys + p; +} + +static void mb_add_mod(struct mb_state *s, + uint32_t start, uint32_t end, + uint32_t cmdline_phys) +{ + char *p; + assert(s->mb_mods_count < s->mb_mods_avail); + + p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count; + + stl_p(p + MB_MOD_START, start); + stl_p(p + MB_MOD_END, end); + stl_p(p + MB_MOD_CMDLINE, cmdline_phys); + +#ifdef DEBUG_MULTIBOOT + fprintf(stderr, "mod%02d: %08x - %08x\n", s->mb_mods_count, start, end); +#endif + + s->mb_mods_count++; +} + static int load_multiboot(void *fw_cfg, FILE *f, const char *kernel_filename, @@ -519,12 +592,8 @@ static int load_multiboot(void *fw_cfg, uint32_t mh_entry_addr; uint32_t mh_load_addr; uint32_t mb_kernel_size; - uint32_t mmap_addr = MULTIBOOT_STRUCT_ADDR; - uint32_t mb_bootinfo = MULTIBOOT_STRUCT_ADDR + 0x500; - uint32_t mb_mod_end; - uint8_t bootinfo[0x500]; - uint32_t cmdline = 0x200; - uint8_t *mb_kernel_data; + struct mb_state mbs; + uint8_t bootinfo[MBI_SIZE]; uint8_t *mb_bootinfo_data; /* Ok, let's see if it is a multiboot image. @@ -549,6 +618,7 @@ static int load_multiboot(void *fw_cfg, fprintf(stderr, "qemu: I believe we found a multiboot image!\n"); #endif memset(bootinfo, 0, sizeof(bootinfo)); + memset(&mbs, 0, sizeof(mbs)); if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */ fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n"); @@ -566,8 +636,8 @@ static int load_multiboot(void *fw_cfg, mh_load_addr = mh_entry_addr = elf_entry; mb_kernel_size = kernel_size; - mb_kernel_data = qemu_malloc(mb_kernel_size); - if (rom_copy(mb_kernel_data, elf_entry, kernel_size) != kernel_size) { + mbs.mb_buf = qemu_malloc(mb_kernel_size); + if (rom_copy(mbs.mb_buf, elf_entry, kernel_size) != kernel_size) { fprintf(stderr, "Error while fetching elf kernel from rom\n"); exit(1); } @@ -604,104 +674,104 @@ static int load_multiboot(void *fw_cfg, mb_kernel_size, mh_load_addr); #endif - mb_kernel_data = qemu_malloc(mb_kernel_size); + mbs.mb_buf = qemu_malloc(mb_kernel_size); fseek(f, mb_kernel_text_offset, SEEK_SET); - fread(mb_kernel_data, 1, mb_kernel_size, f); + fread(mbs.mb_buf, 1, mb_kernel_size, f); fclose(f); } - /* blob size is only the kernel for now */ - mb_mod_end = mh_load_addr + mb_kernel_size; + mbs.mb_buf_phys = mh_load_addr; + + mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size); + mbs.offset_mbinfo = mbs.mb_buf_size; + + /* Calculate space for cmdlines and mb_mods */ + mbs.mb_buf_size += strlen(kernel_filename) + 1; + mbs.mb_buf_size += strlen(kernel_cmdline) + 1; + if (initrd_filename) { + const char *r = initrd_filename; + mbs.mb_buf_size += strlen(r) + 1; + mbs.mb_mods_avail = 1; + while ((r = strchr(r, ','))) { + mbs.mb_mods_avail++; + r++; + } + mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail; + } + + mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size); + + // enlarge mb_buf to hold cmdlines and mb-info structs + mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size); + mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE; - /* load modules */ - stl_p(bootinfo + 20, 0x0); /* mods_count */ if (initrd_filename) { - uint32_t mb_mod_info = 0x100; - uint32_t mb_mod_cmdline = 0x300; - uint32_t mb_mod_start = mh_load_addr; - uint32_t mb_mod_length = mb_kernel_size; char *next_initrd; - char *next_space; - int mb_mod_count = 0; + + mbs.offset_mods = mbs.mb_buf_size; do { - if (mb_mod_info + 16 > mb_mod_cmdline) { - printf("WARNING: Too many modules loaded, aborting.\n"); - break; - } + char *next_space; + uint32_t mb_mod_length; + uint32_t offs = mbs.mb_buf_size; next_initrd = strchr(initrd_filename, ','); if (next_initrd) *next_initrd = '\0'; /* if a space comes after the module filename, treat everything after that as parameters */ - pstrcpy((char*)bootinfo + mb_mod_cmdline, - sizeof(bootinfo) - mb_mod_cmdline, - initrd_filename); - stl_p(bootinfo + mb_mod_info + 8, mb_bootinfo + mb_mod_cmdline); /* string */ - mb_mod_cmdline += strlen(initrd_filename) + 1; - if (mb_mod_cmdline > sizeof(bootinfo)) { - mb_mod_cmdline = sizeof(bootinfo); - printf("WARNING: Too many module cmdlines loaded, aborting.\n"); - break; - } + uint32_t c = mb_add_cmdline(&mbs, initrd_filename); if ((next_space = strchr(initrd_filename, ' '))) *next_space = '\0'; #ifdef DEBUG_MULTIBOOT printf("multiboot loading module: %s\n", initrd_filename); #endif - mb_mod_start = (mb_mod_start + mb_mod_length + (TARGET_PAGE_SIZE - 1)) - & (TARGET_PAGE_MASK); mb_mod_length = get_image_size(initrd_filename); if (mb_mod_length < 0) { fprintf(stderr, "failed to get %s image size\n", initrd_filename); exit(1); } - mb_mod_end = mb_mod_start + mb_mod_length; - mb_mod_count++; - - /* append module data at the end of last module */ - mb_kernel_data = qemu_realloc(mb_kernel_data, - mb_mod_end - mh_load_addr); - load_image(initrd_filename, - mb_kernel_data + mb_mod_start - mh_load_addr); - - stl_p(bootinfo + mb_mod_info + 0, mb_mod_start); - stl_p(bootinfo + mb_mod_info + 4, mb_mod_start + mb_mod_length); - stl_p(bootinfo + mb_mod_info + 12, 0x0); /* reserved */ + + mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size); + mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size); + + load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs); + mb_add_mod(&mbs, mbs.mb_buf_phys + offs, + mbs.mb_buf_phys + offs + mb_mod_length, c); + #ifdef DEBUG_MULTIBOOT - printf("mod_start: %#x\nmod_end: %#x\n", mb_mod_start, - mb_mod_start + mb_mod_length); + printf("mod_start: %p\nmod_end: %p\n cmdline: %#x\n", + (char *)mbs.mb_buf + offs, + (char *)mbs.mb_buf + offs + mb_mod_length, c); #endif initrd_filename = next_initrd+1; - mb_mod_info += 16; } while (next_initrd); - stl_p(bootinfo + 20, mb_mod_count); /* mods_count */ - stl_p(bootinfo + 24, mb_bootinfo + 0x100); /* mods_addr */ } /* Commandline support */ - stl_p(bootinfo + 16, mb_bootinfo + cmdline); - snprintf((char*)bootinfo + cmdline, 0x100, "%s %s", + char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2]; + snprintf(kcmdline, sizeof(kcmdline), "%s %s", kernel_filename, kernel_cmdline); + stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline)); + + stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo); + stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */ /* the kernel is where we want it to be now */ -#define MULTIBOOT_FLAGS_MEMORY (1 << 0) -#define MULTIBOOT_FLAGS_BOOT_DEVICE (1 << 1) -#define MULTIBOOT_FLAGS_CMDLINE (1 << 2) -#define MULTIBOOT_FLAGS_MODULES (1 << 3) -#define MULTIBOOT_FLAGS_MMAP (1 << 6) - stl_p(bootinfo, MULTIBOOT_FLAGS_MEMORY - | MULTIBOOT_FLAGS_BOOT_DEVICE - | MULTIBOOT_FLAGS_CMDLINE - | MULTIBOOT_FLAGS_MODULES - | MULTIBOOT_FLAGS_MMAP); - stl_p(bootinfo + 4, 640); /* mem_lower */ - stl_p(bootinfo + 8, ram_size / 1024); /* mem_upper */ - stl_p(bootinfo + 12, 0x8001ffff); /* XXX: use the -boot switch? */ - stl_p(bootinfo + 48, mmap_addr); /* mmap_addr */ + stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY + | MULTIBOOT_FLAGS_BOOT_DEVICE + | MULTIBOOT_FLAGS_CMDLINE + | MULTIBOOT_FLAGS_MODULES + | MULTIBOOT_FLAGS_MMAP); + stl_p(bootinfo + MBI_MEM_LOWER, 640); + stl_p(bootinfo + MBI_MEM_UPPER, ram_size / 1024); + stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8001ffff); /* XXX: use the -boot switch? */ + stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP); #ifdef DEBUG_MULTIBOOT fprintf(stderr, "multiboot: mh_entry_addr = %#x\n", mh_entry_addr); + fprintf(stderr, " mb_buf_phys = %x\n", mbs.mb_buf_phys); + fprintf(stderr, " mod_start = %x\n", mbs.mb_buf_phys + mbs.offset_mods); + fprintf(stderr, " mb_mods_count = %d\n", mbs.mb_mods_count); #endif /* save bootinfo off the stack */ @@ -711,11 +781,11 @@ static int load_multiboot(void *fw_cfg, /* Pass variables to option rom */ fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr); fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr); - fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mb_mod_end - mh_load_addr); - fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, mb_kernel_data, - mb_mod_end - mh_load_addr); + fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size); + fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, + mbs.mb_buf, mbs.mb_buf_size); - fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, mb_bootinfo); + fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo)); fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data, sizeof(bootinfo));