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