tests/qtest/bios-tables-test: Check for virtio-iommu device before using it
[qemu/kevin.git] / hw / i386 / multiboot.c
blob3332712ab35265f00538b6e3c337d52d452dfd7b
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/option.h"
27 #include "cpu.h"
28 #include "hw/nvram/fw_cfg.h"
29 #include "multiboot.h"
30 #include "hw/loader.h"
31 #include "elf.h"
32 #include "sysemu/sysemu.h"
33 #include "qemu/error-report.h"
35 /* Show multiboot debug output */
36 //#define DEBUG_MULTIBOOT
38 #ifdef DEBUG_MULTIBOOT
39 #define mb_debug(a...) error_report(a)
40 #else
41 #define mb_debug(a...)
42 #endif
44 #define MULTIBOOT_STRUCT_ADDR 0x9000
46 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
47 #error multiboot struct needs to fit in 16 bit real mode
48 #endif
50 enum {
51 /* Multiboot info */
52 MBI_FLAGS = 0,
53 MBI_MEM_LOWER = 4,
54 MBI_MEM_UPPER = 8,
55 MBI_BOOT_DEVICE = 12,
56 MBI_CMDLINE = 16,
57 MBI_MODS_COUNT = 20,
58 MBI_MODS_ADDR = 24,
59 MBI_MMAP_ADDR = 48,
60 MBI_BOOTLOADER = 64,
62 MBI_SIZE = 88,
64 /* Multiboot modules */
65 MB_MOD_START = 0,
66 MB_MOD_END = 4,
67 MB_MOD_CMDLINE = 8,
69 MB_MOD_SIZE = 16,
71 /* Region offsets */
72 ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
73 ADDR_MBI = ADDR_E820_MAP + 0x500,
75 /* Multiboot flags */
76 MULTIBOOT_FLAGS_MEMORY = 1 << 0,
77 MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
78 MULTIBOOT_FLAGS_CMDLINE = 1 << 2,
79 MULTIBOOT_FLAGS_MODULES = 1 << 3,
80 MULTIBOOT_FLAGS_MMAP = 1 << 6,
81 MULTIBOOT_FLAGS_BOOTLOADER = 1 << 9,
84 typedef struct {
85 /* buffer holding kernel, cmdlines and mb_infos */
86 void *mb_buf;
87 /* address in target */
88 hwaddr mb_buf_phys;
89 /* size of mb_buf in bytes */
90 unsigned mb_buf_size;
91 /* offset of mb-info's in bytes */
92 hwaddr offset_mbinfo;
93 /* offset in buffer for cmdlines in bytes */
94 hwaddr offset_cmdlines;
95 /* offset in buffer for bootloader name in bytes */
96 hwaddr offset_bootloader;
97 /* offset of modules in bytes */
98 hwaddr offset_mods;
99 /* available slots for mb modules infos */
100 int mb_mods_avail;
101 /* currently used slots of mb modules */
102 int mb_mods_count;
103 } MultibootState;
105 const char *bootloader_name = "qemu";
107 static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
109 hwaddr p = s->offset_cmdlines;
110 char *b = (char *)s->mb_buf + p;
112 memcpy(b, cmdline, strlen(cmdline) + 1);
113 s->offset_cmdlines += strlen(b) + 1;
114 return s->mb_buf_phys + p;
117 static uint32_t mb_add_bootloader(MultibootState *s, const char *bootloader)
119 hwaddr p = s->offset_bootloader;
120 char *b = (char *)s->mb_buf + p;
122 memcpy(b, bootloader, strlen(bootloader) + 1);
123 s->offset_bootloader += strlen(b) + 1;
124 return s->mb_buf_phys + p;
127 static void mb_add_mod(MultibootState *s,
128 hwaddr start, hwaddr end,
129 hwaddr cmdline_phys)
131 char *p;
132 assert(s->mb_mods_count < s->mb_mods_avail);
134 p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
136 stl_p(p + MB_MOD_START, start);
137 stl_p(p + MB_MOD_END, end);
138 stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
140 mb_debug("mod%02d: "HWADDR_FMT_plx" - "HWADDR_FMT_plx,
141 s->mb_mods_count, start, end);
143 s->mb_mods_count++;
146 int load_multiboot(X86MachineState *x86ms,
147 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)
155 bool multiboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
156 int i, is_multiboot = 0;
157 uint32_t flags = 0;
158 uint32_t mh_entry_addr;
159 uint32_t mh_load_addr;
160 uint32_t mb_kernel_size;
161 MultibootState mbs;
162 uint8_t bootinfo[MBI_SIZE];
163 uint8_t *mb_bootinfo_data;
164 uint32_t cmdline_len;
165 GList *mods = NULL;
166 g_autofree char *kcmdline = NULL;
168 /* Ok, let's see if it is a multiboot image.
169 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
170 for (i = 0; i < (8192 - 48); i += 4) {
171 if (ldl_p(header+i) == 0x1BADB002) {
172 uint32_t checksum = ldl_p(header+i+8);
173 flags = ldl_p(header+i+4);
174 checksum += flags;
175 checksum += (uint32_t)0x1BADB002;
176 if (!checksum) {
177 is_multiboot = 1;
178 break;
183 if (!is_multiboot)
184 return 0; /* no multiboot */
186 mb_debug("I believe we found a multiboot image!");
187 memset(bootinfo, 0, sizeof(bootinfo));
188 memset(&mbs, 0, sizeof(mbs));
190 if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
191 error_report("multiboot knows VBE. we don't");
193 if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
194 uint64_t elf_entry;
195 uint64_t elf_low, elf_high;
196 int kernel_size;
197 fclose(f);
199 if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
200 error_report("Cannot load x86-64 image, give a 32bit one.");
201 exit(1);
204 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, &elf_entry,
205 &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
206 0, 0);
207 if (kernel_size < 0) {
208 error_report("Error while loading elf kernel");
209 exit(1);
211 mh_load_addr = elf_low;
212 mb_kernel_size = elf_high - elf_low;
213 mh_entry_addr = elf_entry;
215 mbs.mb_buf = g_malloc(mb_kernel_size);
216 if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
217 error_report("Error while fetching elf kernel from rom");
218 exit(1);
221 mb_debug("loading multiboot-elf kernel "
222 "(%#x bytes) with entry %#zx",
223 mb_kernel_size, (size_t)mh_entry_addr);
224 } else {
225 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
226 uint32_t mh_header_addr = ldl_p(header+i+12);
227 uint32_t mh_load_end_addr = ldl_p(header+i+20);
228 uint32_t mh_bss_end_addr = ldl_p(header+i+24);
230 mh_load_addr = ldl_p(header+i+16);
231 if (mh_header_addr < mh_load_addr) {
232 error_report("invalid load_addr address");
233 exit(1);
235 if (mh_header_addr - mh_load_addr > i) {
236 error_report("invalid header_addr address");
237 exit(1);
240 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
241 uint32_t mb_load_size = 0;
242 mh_entry_addr = ldl_p(header+i+28);
244 if (mh_load_end_addr) {
245 if (mh_load_end_addr < mh_load_addr) {
246 error_report("invalid load_end_addr address");
247 exit(1);
249 mb_load_size = mh_load_end_addr - mh_load_addr;
250 } else {
251 if (kernel_file_size < mb_kernel_text_offset) {
252 error_report("invalid kernel_file_size");
253 exit(1);
255 mb_load_size = kernel_file_size - mb_kernel_text_offset;
257 if (mb_load_size > UINT32_MAX - mh_load_addr) {
258 error_report("kernel does not fit in address space");
259 exit(1);
261 if (mh_bss_end_addr) {
262 if (mh_bss_end_addr < (mh_load_addr + mb_load_size)) {
263 error_report("invalid bss_end_addr address");
264 exit(1);
266 mb_kernel_size = mh_bss_end_addr - mh_load_addr;
267 } else {
268 mb_kernel_size = mb_load_size;
271 mb_debug("multiboot: header_addr = %#x", mh_header_addr);
272 mb_debug("multiboot: load_addr = %#x", mh_load_addr);
273 mb_debug("multiboot: load_end_addr = %#x", mh_load_end_addr);
274 mb_debug("multiboot: bss_end_addr = %#x", mh_bss_end_addr);
275 mb_debug("loading multiboot kernel (%#x bytes) at %#x",
276 mb_load_size, mh_load_addr);
278 mbs.mb_buf = g_malloc(mb_kernel_size);
279 fseek(f, mb_kernel_text_offset, SEEK_SET);
280 if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
281 error_report("fread() failed");
282 exit(1);
284 memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
285 fclose(f);
288 mbs.mb_buf_phys = mh_load_addr;
290 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
291 mbs.offset_mbinfo = mbs.mb_buf_size;
293 /* Calculate space for cmdlines, bootloader name, and mb_mods */
294 cmdline_len = strlen(kernel_filename) + 1;
295 cmdline_len += strlen(kernel_cmdline) + 1;
296 if (initrd_filename) {
297 const char *r = initrd_filename;
298 cmdline_len += strlen(initrd_filename) + 1;
299 while (*r) {
300 char *value;
301 r = get_opt_value(r, &value);
302 mbs.mb_mods_avail++;
303 mods = g_list_append(mods, value);
304 if (*r) {
305 r++;
310 mbs.mb_buf_size += cmdline_len;
311 mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
312 mbs.mb_buf_size += strlen(bootloader_name) + 1;
314 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
316 /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
317 mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
318 mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
319 mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
321 if (mods) {
322 GList *tmpl = mods;
323 mbs.offset_mods = mbs.mb_buf_size;
325 while (tmpl) {
326 char *next_space;
327 int mb_mod_length;
328 uint32_t offs = mbs.mb_buf_size;
329 char *one_file = tmpl->data;
331 /* if a space comes after the module filename, treat everything
332 after that as parameters */
333 hwaddr c = mb_add_cmdline(&mbs, one_file);
334 next_space = strchr(one_file, ' ');
335 if (next_space) {
336 *next_space = '\0';
338 mb_debug("multiboot loading module: %s", one_file);
339 mb_mod_length = get_image_size(one_file);
340 if (mb_mod_length < 0) {
341 error_report("Failed to open file '%s'", one_file);
342 exit(1);
345 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
346 mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
348 if (load_image_size(one_file, (unsigned char *)mbs.mb_buf + offs,
349 mbs.mb_buf_size - offs) < 0) {
350 error_report("Error loading file '%s'", one_file);
351 exit(1);
353 mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
354 mbs.mb_buf_phys + offs + mb_mod_length, c);
356 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "HWADDR_FMT_plx,
357 (char *)mbs.mb_buf + offs,
358 (char *)mbs.mb_buf + offs + mb_mod_length, c);
359 g_free(one_file);
360 tmpl = tmpl->next;
362 g_list_free(mods);
365 /* Commandline support */
366 kcmdline = g_strdup_printf("%s %s", kernel_filename, kernel_cmdline);
367 stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
369 stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
371 stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
372 stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
374 /* the kernel is where we want it to be now */
375 stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
376 | MULTIBOOT_FLAGS_BOOT_DEVICE
377 | MULTIBOOT_FLAGS_CMDLINE
378 | MULTIBOOT_FLAGS_MODULES
379 | MULTIBOOT_FLAGS_MMAP
380 | MULTIBOOT_FLAGS_BOOTLOADER);
381 stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
382 stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
384 mb_debug("multiboot: entry_addr = %#x", mh_entry_addr);
385 mb_debug(" mb_buf_phys = "HWADDR_FMT_plx, mbs.mb_buf_phys);
386 mb_debug(" mod_start = "HWADDR_FMT_plx,
387 mbs.mb_buf_phys + mbs.offset_mods);
388 mb_debug(" mb_mods_count = %d", mbs.mb_mods_count);
390 /* save bootinfo off the stack */
391 mb_bootinfo_data = g_memdup(bootinfo, sizeof(bootinfo));
393 /* Pass variables to option rom */
394 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
395 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
396 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
397 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
398 mbs.mb_buf, mbs.mb_buf_size);
400 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
401 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
402 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
403 sizeof(bootinfo));
405 if (multiboot_dma_enabled) {
406 option_rom[nb_option_roms].name = "multiboot_dma.bin";
407 } else {
408 option_rom[nb_option_roms].name = "multiboot.bin";
410 option_rom[nb_option_roms].bootindex = 0;
411 nb_option_roms++;
413 return 1; /* yes, we are multiboot */