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"
26 #include "qemu/option.h"
29 #include "hw/nvram/fw_cfg.h"
30 #include "multiboot.h"
31 #include "hw/loader.h"
33 #include "sysemu/sysemu.h"
34 #include "qemu/error-report.h"
36 /* Show multiboot debug output */
37 //#define DEBUG_MULTIBOOT
39 #ifdef DEBUG_MULTIBOOT
40 #define mb_debug(a...) error_report(a)
42 #define mb_debug(a...)
45 #define MULTIBOOT_STRUCT_ADDR 0x9000
47 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
48 #error multiboot struct needs to fit in 16 bit real mode
65 /* Multiboot modules */
73 ADDR_E820_MAP
= MULTIBOOT_STRUCT_ADDR
+ 0,
74 ADDR_MBI
= ADDR_E820_MAP
+ 0x500,
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,
86 /* buffer holding kernel, cmdlines and mb_infos */
88 /* address in target */
90 /* size of mb_buf in bytes */
92 /* offset of mb-info's in bytes */
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 */
100 /* available slots for mb modules infos */
102 /* currently used slots of mb modules */
106 const char *bootloader_name
= "qemu";
108 static uint32_t mb_add_cmdline(MultibootState
*s
, const char *cmdline
)
110 hwaddr p
= s
->offset_cmdlines
;
111 char *b
= (char *)s
->mb_buf
+ p
;
113 memcpy(b
, cmdline
, strlen(cmdline
) + 1);
114 s
->offset_cmdlines
+= strlen(b
) + 1;
115 return s
->mb_buf_phys
+ p
;
118 static uint32_t mb_add_bootloader(MultibootState
*s
, const char *bootloader
)
120 hwaddr p
= s
->offset_bootloader
;
121 char *b
= (char *)s
->mb_buf
+ p
;
123 memcpy(b
, bootloader
, strlen(bootloader
) + 1);
124 s
->offset_bootloader
+= strlen(b
) + 1;
125 return s
->mb_buf_phys
+ p
;
128 static void mb_add_mod(MultibootState
*s
,
129 hwaddr start
, hwaddr end
,
133 assert(s
->mb_mods_count
< s
->mb_mods_avail
);
135 p
= (char *)s
->mb_buf
+ s
->offset_mbinfo
+ MB_MOD_SIZE
* s
->mb_mods_count
;
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
);
141 mb_debug("mod%02d: "TARGET_FMT_plx
" - "TARGET_FMT_plx
,
142 s
->mb_mods_count
, start
, end
);
147 int load_multiboot(FWCfgState
*fw_cfg
,
149 const char *kernel_filename
,
150 const char *initrd_filename
,
151 const char *kernel_cmdline
,
152 int kernel_file_size
,
155 int i
, is_multiboot
= 0;
157 uint32_t mh_entry_addr
;
158 uint32_t mh_load_addr
;
159 uint32_t mb_kernel_size
;
161 uint8_t bootinfo
[MBI_SIZE
];
162 uint8_t *mb_bootinfo_data
;
163 uint32_t cmdline_len
;
166 /* Ok, let's see if it is a multiboot image.
167 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
168 for (i
= 0; i
< (8192 - 48); i
+= 4) {
169 if (ldl_p(header
+i
) == 0x1BADB002) {
170 uint32_t checksum
= ldl_p(header
+i
+8);
171 flags
= ldl_p(header
+i
+4);
173 checksum
+= (uint32_t)0x1BADB002;
182 return 0; /* no multiboot */
184 mb_debug("qemu: I believe we found a multiboot image!");
185 memset(bootinfo
, 0, sizeof(bootinfo
));
186 memset(&mbs
, 0, sizeof(mbs
));
188 if (flags
& 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
189 error_report("qemu: multiboot knows VBE. we don't.");
191 if (!(flags
& 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
193 uint64_t elf_low
, elf_high
;
197 if (((struct elf64_hdr
*)header
)->e_machine
== EM_X86_64
) {
198 error_report("Cannot load x86-64 image, give a 32bit one.");
202 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, &elf_entry
,
203 &elf_low
, &elf_high
, 0, I386_ELF_MACHINE
,
205 if (kernel_size
< 0) {
206 error_report("Error while loading elf kernel");
209 mh_load_addr
= elf_low
;
210 mb_kernel_size
= elf_high
- elf_low
;
211 mh_entry_addr
= elf_entry
;
213 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
214 if (rom_copy(mbs
.mb_buf
, mh_load_addr
, mb_kernel_size
) != mb_kernel_size
) {
215 error_report("Error while fetching elf kernel from rom");
219 mb_debug("qemu: loading multiboot-elf kernel "
220 "(%#x bytes) with entry %#zx",
221 mb_kernel_size
, (size_t)mh_entry_addr
);
223 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
224 uint32_t mh_header_addr
= ldl_p(header
+i
+12);
225 uint32_t mh_load_end_addr
= ldl_p(header
+i
+20);
226 uint32_t mh_bss_end_addr
= ldl_p(header
+i
+24);
228 mh_load_addr
= ldl_p(header
+i
+16);
229 if (mh_header_addr
< mh_load_addr
) {
230 error_report("invalid load_addr address");
233 if (mh_header_addr
- mh_load_addr
> i
) {
234 error_report("invalid header_addr address");
238 uint32_t mb_kernel_text_offset
= i
- (mh_header_addr
- mh_load_addr
);
239 uint32_t mb_load_size
= 0;
240 mh_entry_addr
= ldl_p(header
+i
+28);
242 if (mh_load_end_addr
) {
243 if (mh_load_end_addr
< mh_load_addr
) {
244 error_report("invalid load_end_addr address");
247 mb_load_size
= mh_load_end_addr
- mh_load_addr
;
249 if (kernel_file_size
< mb_kernel_text_offset
) {
250 error_report("invalid kernel_file_size");
253 mb_load_size
= kernel_file_size
- mb_kernel_text_offset
;
255 if (mb_load_size
> UINT32_MAX
- mh_load_addr
) {
256 error_report("kernel does not fit in address space");
259 if (mh_bss_end_addr
) {
260 if (mh_bss_end_addr
< (mh_load_addr
+ mb_load_size
)) {
261 error_report("invalid bss_end_addr address");
264 mb_kernel_size
= mh_bss_end_addr
- mh_load_addr
;
266 mb_kernel_size
= mb_load_size
;
269 mb_debug("multiboot: header_addr = %#x", mh_header_addr
);
270 mb_debug("multiboot: load_addr = %#x", mh_load_addr
);
271 mb_debug("multiboot: load_end_addr = %#x", mh_load_end_addr
);
272 mb_debug("multiboot: bss_end_addr = %#x", mh_bss_end_addr
);
273 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x",
274 mb_load_size
, mh_load_addr
);
276 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
277 fseek(f
, mb_kernel_text_offset
, SEEK_SET
);
278 if (fread(mbs
.mb_buf
, 1, mb_load_size
, f
) != mb_load_size
) {
279 error_report("fread() failed");
282 memset(mbs
.mb_buf
+ mb_load_size
, 0, mb_kernel_size
- mb_load_size
);
286 mbs
.mb_buf_phys
= mh_load_addr
;
288 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_kernel_size
);
289 mbs
.offset_mbinfo
= mbs
.mb_buf_size
;
291 /* Calculate space for cmdlines, bootloader name, and mb_mods */
292 cmdline_len
= strlen(kernel_filename
) + 1;
293 cmdline_len
+= strlen(kernel_cmdline
) + 1;
294 if (initrd_filename
) {
295 const char *r
= initrd_filename
;
296 cmdline_len
+= strlen(initrd_filename
) + 1;
299 r
= get_opt_value(r
, &value
);
301 mods
= g_list_append(mods
, value
);
308 mbs
.mb_buf_size
+= cmdline_len
;
309 mbs
.mb_buf_size
+= MB_MOD_SIZE
* mbs
.mb_mods_avail
;
310 mbs
.mb_buf_size
+= strlen(bootloader_name
) + 1;
312 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mbs
.mb_buf_size
);
314 /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
315 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
316 mbs
.offset_cmdlines
= mbs
.offset_mbinfo
+ mbs
.mb_mods_avail
* MB_MOD_SIZE
;
317 mbs
.offset_bootloader
= mbs
.offset_cmdlines
+ cmdline_len
;
321 mbs
.offset_mods
= mbs
.mb_buf_size
;
326 uint32_t offs
= mbs
.mb_buf_size
;
327 char *one_file
= tmpl
->data
;
329 /* if a space comes after the module filename, treat everything
330 after that as parameters */
331 hwaddr c
= mb_add_cmdline(&mbs
, one_file
);
332 next_space
= strchr(one_file
, ' ');
336 mb_debug("multiboot loading module: %s", one_file
);
337 mb_mod_length
= get_image_size(one_file
);
338 if (mb_mod_length
< 0) {
339 error_report("Failed to open file '%s'", one_file
);
343 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_mod_length
+ mbs
.mb_buf_size
);
344 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
346 load_image(one_file
, (unsigned char *)mbs
.mb_buf
+ offs
);
347 mb_add_mod(&mbs
, mbs
.mb_buf_phys
+ offs
,
348 mbs
.mb_buf_phys
+ offs
+ mb_mod_length
, c
);
350 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx
,
351 (char *)mbs
.mb_buf
+ offs
,
352 (char *)mbs
.mb_buf
+ offs
+ mb_mod_length
, c
);
359 /* Commandline support */
360 char kcmdline
[strlen(kernel_filename
) + strlen(kernel_cmdline
) + 2];
361 snprintf(kcmdline
, sizeof(kcmdline
), "%s %s",
362 kernel_filename
, kernel_cmdline
);
363 stl_p(bootinfo
+ MBI_CMDLINE
, mb_add_cmdline(&mbs
, kcmdline
));
365 stl_p(bootinfo
+ MBI_BOOTLOADER
, mb_add_bootloader(&mbs
, bootloader_name
));
367 stl_p(bootinfo
+ MBI_MODS_ADDR
, mbs
.mb_buf_phys
+ mbs
.offset_mbinfo
);
368 stl_p(bootinfo
+ MBI_MODS_COUNT
, mbs
.mb_mods_count
); /* mods_count */
370 /* the kernel is where we want it to be now */
371 stl_p(bootinfo
+ MBI_FLAGS
, MULTIBOOT_FLAGS_MEMORY
372 | MULTIBOOT_FLAGS_BOOT_DEVICE
373 | MULTIBOOT_FLAGS_CMDLINE
374 | MULTIBOOT_FLAGS_MODULES
375 | MULTIBOOT_FLAGS_MMAP
376 | MULTIBOOT_FLAGS_BOOTLOADER
);
377 stl_p(bootinfo
+ MBI_BOOT_DEVICE
, 0x8000ffff); /* XXX: use the -boot switch? */
378 stl_p(bootinfo
+ MBI_MMAP_ADDR
, ADDR_E820_MAP
);
380 mb_debug("multiboot: entry_addr = %#x", mh_entry_addr
);
381 mb_debug(" mb_buf_phys = "TARGET_FMT_plx
, mbs
.mb_buf_phys
);
382 mb_debug(" mod_start = "TARGET_FMT_plx
,
383 mbs
.mb_buf_phys
+ mbs
.offset_mods
);
384 mb_debug(" mb_mods_count = %d", mbs
.mb_mods_count
);
386 /* save bootinfo off the stack */
387 mb_bootinfo_data
= g_memdup(bootinfo
, sizeof(bootinfo
));
389 /* Pass variables to option rom */
390 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, mh_entry_addr
);
391 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
392 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, mbs
.mb_buf_size
);
393 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
,
394 mbs
.mb_buf
, mbs
.mb_buf_size
);
396 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, ADDR_MBI
);
397 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, sizeof(bootinfo
));
398 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, mb_bootinfo_data
,
401 option_rom
[nb_option_roms
].name
= "multiboot.bin";
402 option_rom
[nb_option_roms
].bootindex
= 0;
405 return 1; /* yes, we are multiboot */