qemu/hw/i386/multiboot.c
<<
>>
Prefs
   1/*
   2 * QEMU PC System Emulator
   3 *
   4 * Copyright (c) 2003-2004 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "hw/hw.h"
  26#include "hw/nvram/fw_cfg.h"
  27#include "multiboot.h"
  28#include "hw/loader.h"
  29#include "elf.h"
  30#include "sysemu/sysemu.h"
  31
  32/* Show multiboot debug output */
  33//#define DEBUG_MULTIBOOT
  34
  35#ifdef DEBUG_MULTIBOOT
  36#define mb_debug(a...) fprintf(stderr, ## a)
  37#else
  38#define mb_debug(a...)
  39#endif
  40
  41#define MULTIBOOT_STRUCT_ADDR 0x9000
  42
  43#if MULTIBOOT_STRUCT_ADDR > 0xf0000
  44#error multiboot struct needs to fit in 16 bit real mode
  45#endif
  46
  47enum {
  48    /* Multiboot info */
  49    MBI_FLAGS       = 0,
  50    MBI_MEM_LOWER   = 4,
  51    MBI_MEM_UPPER   = 8,
  52    MBI_BOOT_DEVICE = 12,
  53    MBI_CMDLINE     = 16,
  54    MBI_MODS_COUNT  = 20,
  55    MBI_MODS_ADDR   = 24,
  56    MBI_MMAP_ADDR   = 48,
  57    MBI_BOOTLOADER  = 64,
  58
  59    MBI_SIZE        = 88,
  60
  61    /* Multiboot modules */
  62    MB_MOD_START    = 0,
  63    MB_MOD_END      = 4,
  64    MB_MOD_CMDLINE  = 8,
  65
  66    MB_MOD_SIZE     = 16,
  67
  68    /* Region offsets */
  69    ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
  70    ADDR_MBI      = ADDR_E820_MAP + 0x500,
  71
  72    /* Multiboot flags */
  73    MULTIBOOT_FLAGS_MEMORY      = 1 << 0,
  74    MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
  75    MULTIBOOT_FLAGS_CMDLINE     = 1 << 2,
  76    MULTIBOOT_FLAGS_MODULES     = 1 << 3,
  77    MULTIBOOT_FLAGS_MMAP        = 1 << 6,
  78    MULTIBOOT_FLAGS_BOOTLOADER  = 1 << 9,
  79};
  80
  81typedef struct {
  82    /* buffer holding kernel, cmdlines and mb_infos */
  83    void *mb_buf;
  84    /* address in target */
  85    hwaddr mb_buf_phys;
  86    /* size of mb_buf in bytes */
  87    unsigned mb_buf_size;
  88    /* offset of mb-info's in bytes */
  89    hwaddr offset_mbinfo;
  90    /* offset in buffer for cmdlines in bytes */
  91    hwaddr offset_cmdlines;
  92    /* offset in buffer for bootloader name in bytes */
  93    hwaddr offset_bootloader;
  94    /* offset of modules in bytes */
  95    hwaddr offset_mods;
  96    /* available slots for mb modules infos */
  97    int mb_mods_avail;
  98    /* currently used slots of mb modules */
  99    int mb_mods_count;
 100} MultibootState;
 101
 102const char *bootloader_name = "qemu";
 103
 104static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
 105{
 106    hwaddr p = s->offset_cmdlines;
 107    char *b = (char *)s->mb_buf + p;
 108
 109    get_opt_value(b, strlen(cmdline) + 1, cmdline);
 110    s->offset_cmdlines += strlen(b) + 1;
 111    return s->mb_buf_phys + p;
 112}
 113
 114static uint32_t mb_add_bootloader(MultibootState *s, const char *bootloader)
 115{
 116    hwaddr p = s->offset_bootloader;
 117    char *b = (char *)s->mb_buf + p;
 118
 119    memcpy(b, bootloader, strlen(bootloader) + 1);
 120    s->offset_bootloader += strlen(b) + 1;
 121    return s->mb_buf_phys + p;
 122}
 123
 124static void mb_add_mod(MultibootState *s,
 125                       hwaddr start, hwaddr end,
 126                       hwaddr cmdline_phys)
 127{
 128    char *p;
 129    assert(s->mb_mods_count < s->mb_mods_avail);
 130
 131    p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
 132
 133    stl_p(p + MB_MOD_START,   start);
 134    stl_p(p + MB_MOD_END,     end);
 135    stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
 136
 137    mb_debug("mod%02d: "TARGET_FMT_plx" - "TARGET_FMT_plx"\n",
 138             s->mb_mods_count, start, end);
 139
 140    s->mb_mods_count++;
 141}
 142
 143int load_multiboot(FWCfgState *fw_cfg,
 144                   FILE *f,
 145                   const char *kernel_filename,
 146                   const char *initrd_filename,
 147                   const char *kernel_cmdline,
 148                   int kernel_file_size,
 149                   uint8_t *header)
 150{
 151    int i, is_multiboot = 0;
 152    uint32_t flags = 0;
 153    uint32_t mh_entry_addr;
 154    uint32_t mh_load_addr;
 155    uint32_t mb_kernel_size;
 156    MultibootState mbs;
 157    uint8_t bootinfo[MBI_SIZE];
 158    uint8_t *mb_bootinfo_data;
 159    uint32_t cmdline_len;
 160
 161    /* Ok, let's see if it is a multiboot image.
 162       The header is 12x32bit long, so the latest entry may be 8192 - 48. */
 163    for (i = 0; i < (8192 - 48); i += 4) {
 164        if (ldl_p(header+i) == 0x1BADB002) {
 165            uint32_t checksum = ldl_p(header+i+8);
 166            flags = ldl_p(header+i+4);
 167            checksum += flags;
 168            checksum += (uint32_t)0x1BADB002;
 169            if (!checksum) {
 170                is_multiboot = 1;
 171                break;
 172            }
 173        }
 174    }
 175
 176    if (!is_multiboot)
 177        return 0; /* no multiboot */
 178
 179    mb_debug("qemu: I believe we found a multiboot image!\n");
 180    memset(bootinfo, 0, sizeof(bootinfo));
 181    memset(&mbs, 0, sizeof(mbs));
 182
 183    if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
 184        fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
 185    }
 186    if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
 187        uint64_t elf_entry;
 188        uint64_t elf_low, elf_high;
 189        int kernel_size;
 190        fclose(f);
 191
 192        if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
 193            fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
 194            exit(1);
 195        }
 196
 197        kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
 198                               &elf_low, &elf_high, 0, I386_ELF_MACHINE, 0);
 199        if (kernel_size < 0) {
 200            fprintf(stderr, "Error while loading elf kernel\n");
 201            exit(1);
 202        }
 203        mh_load_addr = elf_low;
 204        mb_kernel_size = elf_high - elf_low;
 205        mh_entry_addr = elf_entry;
 206
 207        mbs.mb_buf = g_malloc(mb_kernel_size);
 208        if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
 209            fprintf(stderr, "Error while fetching elf kernel from rom\n");
 210            exit(1);
 211        }
 212
 213        mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
 214                  mb_kernel_size, (size_t)mh_entry_addr);
 215    } else {
 216        /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
 217        uint32_t mh_header_addr = ldl_p(header+i+12);
 218        uint32_t mh_load_end_addr = ldl_p(header+i+20);
 219        uint32_t mh_bss_end_addr = ldl_p(header+i+24);
 220        mh_load_addr = ldl_p(header+i+16);
 221        uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
 222        uint32_t mb_load_size = 0;
 223        mh_entry_addr = ldl_p(header+i+28);
 224
 225        if (mh_load_end_addr) {
 226            mb_kernel_size = mh_bss_end_addr - mh_load_addr;
 227            mb_load_size = mh_load_end_addr - mh_load_addr;
 228        } else {
 229            mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
 230            mb_load_size = mb_kernel_size;
 231        }
 232
 233        /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
 234        uint32_t mh_mode_type = ldl_p(header+i+32);
 235        uint32_t mh_width = ldl_p(header+i+36);
 236        uint32_t mh_height = ldl_p(header+i+40);
 237        uint32_t mh_depth = ldl_p(header+i+44); */
 238
 239        mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
 240        mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
 241        mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
 242        mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
 243        mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
 244                 mb_load_size, mh_load_addr);
 245
 246        mbs.mb_buf = g_malloc(mb_kernel_size);
 247        fseek(f, mb_kernel_text_offset, SEEK_SET);
 248        if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
 249            fprintf(stderr, "fread() failed\n");
 250            exit(1);
 251        }
 252        memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
 253        fclose(f);
 254    }
 255
 256    mbs.mb_buf_phys = mh_load_addr;
 257
 258    mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
 259    mbs.offset_mbinfo = mbs.mb_buf_size;
 260
 261    /* Calculate space for cmdlines, bootloader name, and mb_mods */
 262    cmdline_len = strlen(kernel_filename) + 1;
 263    cmdline_len += strlen(kernel_cmdline) + 1;
 264    if (initrd_filename) {
 265        const char *r = initrd_filename;
 266        cmdline_len += strlen(r) + 1;
 267        mbs.mb_mods_avail = 1;
 268        while (*(r = get_opt_value(NULL, 0, r))) {
 269           mbs.mb_mods_avail++;
 270           r++;
 271        }
 272    }
 273
 274    mbs.mb_buf_size += cmdline_len;
 275    mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
 276    mbs.mb_buf_size += strlen(bootloader_name) + 1;
 277
 278    mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
 279
 280    /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
 281    mbs.mb_buf            = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
 282    mbs.offset_cmdlines   = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
 283    mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
 284
 285    if (initrd_filename) {
 286        char *next_initrd, not_last;
 287
 288        mbs.offset_mods = mbs.mb_buf_size;
 289
 290        do {
 291            char *next_space;
 292            int mb_mod_length;
 293            uint32_t offs = mbs.mb_buf_size;
 294
 295            next_initrd = (char *)get_opt_value(NULL, 0, initrd_filename);
 296            not_last = *next_initrd;
 297            *next_initrd = '\0';
 298            /* if a space comes after the module filename, treat everything
 299               after that as parameters */
 300            hwaddr c = mb_add_cmdline(&mbs, initrd_filename);
 301            if ((next_space = strchr(initrd_filename, ' ')))
 302                *next_space = '\0';
 303            mb_debug("multiboot loading module: %s\n", initrd_filename);
 304            mb_mod_length = get_image_size(initrd_filename);
 305            if (mb_mod_length < 0) {
 306                fprintf(stderr, "Failed to open file '%s'\n", initrd_filename);
 307                exit(1);
 308            }
 309
 310            mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
 311            mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
 312
 313            load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs);
 314            mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
 315                       mbs.mb_buf_phys + offs + mb_mod_length, c);
 316
 317            mb_debug("mod_start: %p\nmod_end:   %p\n  cmdline: "TARGET_FMT_plx"\n",
 318                     (char *)mbs.mb_buf + offs,
 319                     (char *)mbs.mb_buf + offs + mb_mod_length, c);
 320            initrd_filename = next_initrd+1;
 321        } while (not_last);
 322    }
 323
 324    /* Commandline support */
 325    char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
 326    snprintf(kcmdline, sizeof(kcmdline), "%s %s",
 327             kernel_filename, kernel_cmdline);
 328    stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
 329
 330    stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
 331
 332    stl_p(bootinfo + MBI_MODS_ADDR,  mbs.mb_buf_phys + mbs.offset_mbinfo);
 333    stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
 334
 335    /* the kernel is where we want it to be now */
 336    stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
 337                                | MULTIBOOT_FLAGS_BOOT_DEVICE
 338                                | MULTIBOOT_FLAGS_CMDLINE
 339                                | MULTIBOOT_FLAGS_MODULES
 340                                | MULTIBOOT_FLAGS_MMAP
 341                                | MULTIBOOT_FLAGS_BOOTLOADER);
 342    stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
 343    stl_p(bootinfo + MBI_MMAP_ADDR,   ADDR_E820_MAP);
 344
 345    mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
 346    mb_debug("           mb_buf_phys   = "TARGET_FMT_plx"\n", mbs.mb_buf_phys);
 347    mb_debug("           mod_start     = "TARGET_FMT_plx"\n", mbs.mb_buf_phys + mbs.offset_mods);
 348    mb_debug("           mb_mods_count = %d\n", mbs.mb_mods_count);
 349
 350    /* save bootinfo off the stack */
 351    mb_bootinfo_data = g_malloc(sizeof(bootinfo));
 352    memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
 353
 354    /* Pass variables to option rom */
 355    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
 356    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
 357    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
 358    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
 359                     mbs.mb_buf, mbs.mb_buf_size);
 360
 361    fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
 362    fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
 363    fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
 364                     sizeof(bootinfo));
 365
 366    option_rom[nb_option_roms].name = "multiboot.bin";
 367    option_rom[nb_option_roms].bootindex = 0;
 368    nb_option_roms++;
 369
 370    return 1; /* yes, we are multiboot */
 371}
 372