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
, 0);
200 if (kernel_size
< 0) {
201 fprintf(stderr
, "Error while loading elf kernel\n");
204 mh_load_addr
= elf_low
;
205 mb_kernel_size
= elf_high
- elf_low
;
206 mh_entry_addr
= elf_entry
;
208 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
209 if (rom_copy(mbs
.mb_buf
, mh_load_addr
, mb_kernel_size
) != mb_kernel_size
) {
210 fprintf(stderr
, "Error while fetching elf kernel from rom\n");
214 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
215 mb_kernel_size
, (size_t)mh_entry_addr
);
217 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
218 uint32_t mh_header_addr
= ldl_p(header
+i
+12);
219 uint32_t mh_load_end_addr
= ldl_p(header
+i
+20);
220 uint32_t mh_bss_end_addr
= ldl_p(header
+i
+24);
221 mh_load_addr
= ldl_p(header
+i
+16);
222 uint32_t mb_kernel_text_offset
= i
- (mh_header_addr
- mh_load_addr
);
223 uint32_t mb_load_size
= 0;
224 mh_entry_addr
= ldl_p(header
+i
+28);
226 if (mh_load_end_addr
) {
227 mb_kernel_size
= mh_bss_end_addr
- mh_load_addr
;
228 mb_load_size
= mh_load_end_addr
- mh_load_addr
;
230 mb_kernel_size
= kernel_file_size
- mb_kernel_text_offset
;
231 mb_load_size
= mb_kernel_size
;
234 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
235 uint32_t mh_mode_type = ldl_p(header+i+32);
236 uint32_t mh_width = ldl_p(header+i+36);
237 uint32_t mh_height = ldl_p(header+i+40);
238 uint32_t mh_depth = ldl_p(header+i+44); */
240 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr
);
241 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr
);
242 mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr
);
243 mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr
);
244 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
245 mb_load_size
, mh_load_addr
);
247 mbs
.mb_buf
= g_malloc(mb_kernel_size
);
248 fseek(f
, mb_kernel_text_offset
, SEEK_SET
);
249 if (fread(mbs
.mb_buf
, 1, mb_load_size
, f
) != mb_load_size
) {
250 fprintf(stderr
, "fread() failed\n");
253 memset(mbs
.mb_buf
+ mb_load_size
, 0, mb_kernel_size
- mb_load_size
);
257 mbs
.mb_buf_phys
= mh_load_addr
;
259 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_kernel_size
);
260 mbs
.offset_mbinfo
= mbs
.mb_buf_size
;
262 /* Calculate space for cmdlines, bootloader name, and mb_mods */
263 cmdline_len
= strlen(kernel_filename
) + 1;
264 cmdline_len
+= strlen(kernel_cmdline
) + 1;
265 if (initrd_filename
) {
266 const char *r
= initrd_filename
;
267 cmdline_len
+= strlen(r
) + 1;
268 mbs
.mb_mods_avail
= 1;
269 while (*(r
= get_opt_value(NULL
, 0, r
))) {
275 mbs
.mb_buf_size
+= cmdline_len
;
276 mbs
.mb_buf_size
+= MB_MOD_SIZE
* mbs
.mb_mods_avail
;
277 mbs
.mb_buf_size
+= strlen(bootloader_name
) + 1;
279 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mbs
.mb_buf_size
);
281 /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
282 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
283 mbs
.offset_cmdlines
= mbs
.offset_mbinfo
+ mbs
.mb_mods_avail
* MB_MOD_SIZE
;
284 mbs
.offset_bootloader
= mbs
.offset_cmdlines
+ cmdline_len
;
286 if (initrd_filename
) {
287 char *next_initrd
, not_last
;
289 mbs
.offset_mods
= mbs
.mb_buf_size
;
294 uint32_t offs
= mbs
.mb_buf_size
;
296 next_initrd
= (char *)get_opt_value(NULL
, 0, initrd_filename
);
297 not_last
= *next_initrd
;
299 /* if a space comes after the module filename, treat everything
300 after that as parameters */
301 hwaddr c
= mb_add_cmdline(&mbs
, initrd_filename
);
302 if ((next_space
= strchr(initrd_filename
, ' ')))
304 mb_debug("multiboot loading module: %s\n", initrd_filename
);
305 mb_mod_length
= get_image_size(initrd_filename
);
306 if (mb_mod_length
< 0) {
307 fprintf(stderr
, "Failed to open file '%s'\n", initrd_filename
);
311 mbs
.mb_buf_size
= TARGET_PAGE_ALIGN(mb_mod_length
+ mbs
.mb_buf_size
);
312 mbs
.mb_buf
= g_realloc(mbs
.mb_buf
, mbs
.mb_buf_size
);
314 load_image(initrd_filename
, (unsigned char *)mbs
.mb_buf
+ offs
);
315 mb_add_mod(&mbs
, mbs
.mb_buf_phys
+ offs
,
316 mbs
.mb_buf_phys
+ offs
+ mb_mod_length
, c
);
318 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx
"\n",
319 (char *)mbs
.mb_buf
+ offs
,
320 (char *)mbs
.mb_buf
+ offs
+ mb_mod_length
, c
);
321 initrd_filename
= next_initrd
+1;
325 /* Commandline support */
326 char kcmdline
[strlen(kernel_filename
) + strlen(kernel_cmdline
) + 2];
327 snprintf(kcmdline
, sizeof(kcmdline
), "%s %s",
328 kernel_filename
, kernel_cmdline
);
329 stl_p(bootinfo
+ MBI_CMDLINE
, mb_add_cmdline(&mbs
, kcmdline
));
331 stl_p(bootinfo
+ MBI_BOOTLOADER
, mb_add_bootloader(&mbs
, bootloader_name
));
333 stl_p(bootinfo
+ MBI_MODS_ADDR
, mbs
.mb_buf_phys
+ mbs
.offset_mbinfo
);
334 stl_p(bootinfo
+ MBI_MODS_COUNT
, mbs
.mb_mods_count
); /* mods_count */
336 /* the kernel is where we want it to be now */
337 stl_p(bootinfo
+ MBI_FLAGS
, MULTIBOOT_FLAGS_MEMORY
338 | MULTIBOOT_FLAGS_BOOT_DEVICE
339 | MULTIBOOT_FLAGS_CMDLINE
340 | MULTIBOOT_FLAGS_MODULES
341 | MULTIBOOT_FLAGS_MMAP
342 | MULTIBOOT_FLAGS_BOOTLOADER
);
343 stl_p(bootinfo
+ MBI_BOOT_DEVICE
, 0x8000ffff); /* XXX: use the -boot switch? */
344 stl_p(bootinfo
+ MBI_MMAP_ADDR
, ADDR_E820_MAP
);
346 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr
);
347 mb_debug(" mb_buf_phys = "TARGET_FMT_plx
"\n", mbs
.mb_buf_phys
);
348 mb_debug(" mod_start = "TARGET_FMT_plx
"\n", mbs
.mb_buf_phys
+ mbs
.offset_mods
);
349 mb_debug(" mb_mods_count = %d\n", mbs
.mb_mods_count
);
351 /* save bootinfo off the stack */
352 mb_bootinfo_data
= g_malloc(sizeof(bootinfo
));
353 memcpy(mb_bootinfo_data
, bootinfo
, sizeof(bootinfo
));
355 /* Pass variables to option rom */
356 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ENTRY
, mh_entry_addr
);
357 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, mh_load_addr
);
358 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, mbs
.mb_buf_size
);
359 fw_cfg_add_bytes(fw_cfg
, FW_CFG_KERNEL_DATA
,
360 mbs
.mb_buf
, mbs
.mb_buf_size
);
362 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, ADDR_MBI
);
363 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, sizeof(bootinfo
));
364 fw_cfg_add_bytes(fw_cfg
, FW_CFG_INITRD_DATA
, mb_bootinfo_data
,
367 option_rom
[nb_option_roms
].name
= "multiboot.bin";
368 option_rom
[nb_option_roms
].bootindex
= 0;
371 return 1; /* yes, we are multiboot */