2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "multiboot.h"
29 #include "hw/loader.h"
31 #include "sysemu/sysemu.h"
33 /* Show multiboot debug output */
34 //#define DEBUG_MULTIBOOT
36 #ifdef DEBUG_MULTIBOOT
37 #define mb_debug(a...) fprintf(stderr, ## a)
39 #define mb_debug(a...)
42 #define MULTIBOOT_STRUCT_ADDR 0x9000
44 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
45 #error multiboot struct needs to fit in 16 bit real mode
62 /* Multiboot modules */
70 ADDR_E820_MAP
= MULTIBOOT_STRUCT_ADDR
+ 0,
71 ADDR_MBI
= ADDR_E820_MAP
+ 0x500,
74 MULTIBOOT_FLAGS_MEMORY
= 1 << 0,
75 MULTIBOOT_FLAGS_BOOT_DEVICE
= 1 << 1,
76 MULTIBOOT_FLAGS_CMDLINE
= 1 << 2,
77 MULTIBOOT_FLAGS_MODULES
= 1 << 3,
78 MULTIBOOT_FLAGS_MMAP
= 1 << 6,
79 MULTIBOOT_FLAGS_BOOTLOADER
= 1 << 9,
83 /* buffer holding kernel, cmdlines and mb_infos */
85 /* address in target */
87 /* size of mb_buf in bytes */
89 /* offset of mb-info's in bytes */
91 /* offset in buffer for cmdlines in bytes */
92 hwaddr offset_cmdlines
;
93 /* offset in buffer for bootloader name in bytes */
94 hwaddr offset_bootloader
;
95 /* offset of modules in bytes */
97 /* available slots for mb modules infos */
99 /* currently used slots of mb modules */
103 const char *bootloader_name
= "qemu";
105 static uint32_t mb_add_cmdline(MultibootState
*s
, const char *cmdline
)
107 hwaddr p
= s
->offset_cmdlines
;
108 char *b
= (char *)s
->mb_buf
+ p
;
110 get_opt_value(b
, strlen(cmdline
) + 1, cmdline
);
111 s
->offset_cmdlines
+= strlen(b
) + 1;
112 return s
->mb_buf_phys
+ p
;
115 static uint32_t mb_add_bootloader(MultibootState
*s
, const char *bootloader
)
117 hwaddr p
= s
->offset_bootloader
;
118 char *b
= (char *)s
->mb_buf
+ p
;
120 memcpy(b
, bootloader
, strlen(bootloader
) + 1);
121 s
->offset_bootloader
+= strlen(b
) + 1;
122 return s
->mb_buf_phys
+ p
;
125 static void mb_add_mod(MultibootState
*s
,
126 hwaddr start
, hwaddr end
,
130 assert(s
->mb_mods_count
< s
->mb_mods_avail
);
132 p
= (char *)s
->mb_buf
+ s
->offset_mbinfo
+ MB_MOD_SIZE
* s
->mb_mods_count
;
134 stl_p(p
+ MB_MOD_START
, start
);
135 stl_p(p
+ MB_MOD_END
, end
);
136 stl_p(p
+ MB_MOD_CMDLINE
, cmdline_phys
);
138 mb_debug("mod%02d: "TARGET_FMT_plx
" - "TARGET_FMT_plx
"\n",
139 s
->mb_mods_count
, start
, end
);
144 int load_multiboot(FWCfgState
*fw_cfg
,
146 const char *kernel_filename
,
147 const char *initrd_filename
,
148 const char *kernel_cmdline
,
149 int kernel_file_size
,
152 int i
, is_multiboot
= 0;
154 uint32_t mh_entry_addr
;
155 uint32_t mh_load_addr
;
156 uint32_t mb_kernel_size
;
158 uint8_t bootinfo
[MBI_SIZE
];
159 uint8_t *mb_bootinfo_data
;
160 uint32_t cmdline_len
;
162 /* Ok, let's see if it is a multiboot image.
163 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
164 for (i
= 0; i
< (8192 - 48); i
+= 4) {
165 if (ldl_p(header
+i
) == 0x1BADB002) {
166 uint32_t checksum
= ldl_p(header
+i
+8);
167 flags
= ldl_p(header
+i
+4);
169 checksum
+= (uint32_t)0x1BADB002;
178 return 0; /* no multiboot */
180 mb_debug("qemu: I believe we found a multiboot image!\n");
181 memset(bootinfo
, 0, sizeof(bootinfo
));
182 memset(&mbs
, 0, sizeof(mbs
));
184 if (flags
& 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
185 fprintf(stderr
, "qemu: multiboot knows VBE. we don't.\n");
187 if (!(flags
& 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
189 uint64_t elf_low
, elf_high
;
193 if (((struct elf64_hdr
*)header
)->e_machine
== EM_X86_64
) {
194 fprintf(stderr
, "Cannot load x86-64 image, give a 32bit one.\n");
198 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, &elf_entry
,
199 &elf_low
, &elf_high
, 0, I386_ELF_MACHINE
,
201 if (kernel_size
< 0) {
202 fprintf(stderr
, "Error while loading elf kernel\n");
205 mh_load_addr
= elf_low
;
206 mb_kernel_size
= elf_high
- elf_low
;
207 mh_entry_addr
= elf_entry
;
209 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
210 if (rom_copy(mbs
.mb_buf
, mh_load_addr
, mb_kernel_size
) != mb_kernel_size
) {
211 fprintf(stderr
, "Error while fetching elf kernel from rom\n");
215 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
216 mb_kernel_size
, (size_t)mh_entry_addr
);
218 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
219 uint32_t mh_header_addr
= ldl_p(header
+i
+12);
220 uint32_t mh_load_end_addr
= ldl_p(header
+i
+20);
221 uint32_t mh_bss_end_addr
= ldl_p(header
+i
+24);
222 mh_load_addr
= ldl_p(header
+i
+16);
223 uint32_t mb_kernel_text_offset
= i
- (mh_header_addr
- mh_load_addr
);
224 uint32_t mb_load_size
= 0;
225 mh_entry_addr
= ldl_p(header
+i
+28);
227 if (mh_load_end_addr
) {
228 mb_kernel_size
= mh_bss_end_addr
- mh_load_addr
;
229 mb_load_size
= mh_load_end_addr
- mh_load_addr
;
231 mb_kernel_size
= kernel_file_size
- mb_kernel_text_offset
;
232 mb_load_size
= mb_kernel_size
;
235 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
236 uint32_t mh_mode_type = ldl_p(header+i+32);
237 uint32_t mh_width = ldl_p(header+i+36);
238 uint32_t mh_height = ldl_p(header+i+40);
239 uint32_t mh_depth = ldl_p(header+i+44); */
241 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr
);
242 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr
);
243 mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr
);
244 mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr
);
245 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
246 mb_load_size
, mh_load_addr
);
248 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
249 fseek(f
, mb_kernel_text_offset
, SEEK_SET
);
250 if (fread(mbs
.mb_buf
, 1, mb_load_size
, f
) != mb_load_size
) {
251 fprintf(stderr
, "fread() failed\n");
254 memset(mbs
.mb_buf
+ mb_load_size
, 0, mb_kernel_size
- mb_load_size
);
258 mbs
.mb_buf_phys
= mh_load_addr
;
260 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_kernel_size
);
261 mbs
.offset_mbinfo
= mbs
.mb_buf_size
;
263 /* Calculate space for cmdlines, bootloader name, and mb_mods */
264 cmdline_len
= strlen(kernel_filename
) + 1;
265 cmdline_len
+= strlen(kernel_cmdline
) + 1;
266 if (initrd_filename
) {
267 const char *r
= initrd_filename
;
268 cmdline_len
+= strlen(r
) + 1;
269 mbs
.mb_mods_avail
= 1;
270 while (*(r
= get_opt_value(NULL
, 0, r
))) {
276 mbs
.mb_buf_size
+= cmdline_len
;
277 mbs
.mb_buf_size
+= MB_MOD_SIZE
* mbs
.mb_mods_avail
;
278 mbs
.mb_buf_size
+= strlen(bootloader_name
) + 1;
280 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mbs
.mb_buf_size
);
282 /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
283 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
284 mbs
.offset_cmdlines
= mbs
.offset_mbinfo
+ mbs
.mb_mods_avail
* MB_MOD_SIZE
;
285 mbs
.offset_bootloader
= mbs
.offset_cmdlines
+ cmdline_len
;
287 if (initrd_filename
) {
288 char *next_initrd
, not_last
;
290 mbs
.offset_mods
= mbs
.mb_buf_size
;
295 uint32_t offs
= mbs
.mb_buf_size
;
297 next_initrd
= (char *)get_opt_value(NULL
, 0, initrd_filename
);
298 not_last
= *next_initrd
;
300 /* if a space comes after the module filename, treat everything
301 after that as parameters */
302 hwaddr c
= mb_add_cmdline(&mbs
, initrd_filename
);
303 if ((next_space
= strchr(initrd_filename
, ' ')))
305 mb_debug("multiboot loading module: %s\n", initrd_filename
);
306 mb_mod_length
= get_image_size(initrd_filename
);
307 if (mb_mod_length
< 0) {
308 fprintf(stderr
, "Failed to open file '%s'\n", initrd_filename
);
312 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_mod_length
+ mbs
.mb_buf_size
);
313 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
315 load_image(initrd_filename
, (unsigned char *)mbs
.mb_buf
+ offs
);
316 mb_add_mod(&mbs
, mbs
.mb_buf_phys
+ offs
,
317 mbs
.mb_buf_phys
+ offs
+ mb_mod_length
, c
);
319 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx
"\n",
320 (char *)mbs
.mb_buf
+ offs
,
321 (char *)mbs
.mb_buf
+ offs
+ mb_mod_length
, c
);
322 initrd_filename
= next_initrd
+1;
326 /* Commandline support */
327 char kcmdline
[strlen(kernel_filename
) + strlen(kernel_cmdline
) + 2];
328 snprintf(kcmdline
, sizeof(kcmdline
), "%s %s",
329 kernel_filename
, kernel_cmdline
);
330 stl_p(bootinfo
+ MBI_CMDLINE
, mb_add_cmdline(&mbs
, kcmdline
));
332 stl_p(bootinfo
+ MBI_BOOTLOADER
, mb_add_bootloader(&mbs
, bootloader_name
));
334 stl_p(bootinfo
+ MBI_MODS_ADDR
, mbs
.mb_buf_phys
+ mbs
.offset_mbinfo
);
335 stl_p(bootinfo
+ MBI_MODS_COUNT
, mbs
.mb_mods_count
); /* mods_count */
337 /* the kernel is where we want it to be now */
338 stl_p(bootinfo
+ MBI_FLAGS
, MULTIBOOT_FLAGS_MEMORY
339 | MULTIBOOT_FLAGS_BOOT_DEVICE
340 | MULTIBOOT_FLAGS_CMDLINE
341 | MULTIBOOT_FLAGS_MODULES
342 | MULTIBOOT_FLAGS_MMAP
343 | MULTIBOOT_FLAGS_BOOTLOADER
);
344 stl_p(bootinfo
+ MBI_BOOT_DEVICE
, 0x8000ffff); /* XXX: use the -boot switch? */
345 stl_p(bootinfo
+ MBI_MMAP_ADDR
, ADDR_E820_MAP
);
347 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr
);
348 mb_debug(" mb_buf_phys = "TARGET_FMT_plx
"\n", mbs
.mb_buf_phys
);
349 mb_debug(" mod_start = "TARGET_FMT_plx
"\n", mbs
.mb_buf_phys
+ mbs
.offset_mods
);
350 mb_debug(" mb_mods_count = %d\n", mbs
.mb_mods_count
);
352 /* save bootinfo off the stack */
353 mb_bootinfo_data
= g_malloc(sizeof(bootinfo
));
354 memcpy(mb_bootinfo_data
, bootinfo
, sizeof(bootinfo
));
356 /* Pass variables to option rom */
357 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, mh_entry_addr
);
358 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
359 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, mbs
.mb_buf_size
);
360 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
,
361 mbs
.mb_buf
, mbs
.mb_buf_size
);
363 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, ADDR_MBI
);
364 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, sizeof(bootinfo
));
365 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, mb_bootinfo_data
,
368 option_rom
[nb_option_roms
].name
= "multiboot.bin";
369 option_rom
[nb_option_roms
].bootindex
= 0;
372 return 1; /* yes, we are multiboot */