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