1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "qemu-common.h"
27#include "cpu.h"
28#include "hw/hw.h"
29#include "hw/nvram/fw_cfg.h"
30#include "multiboot.h"
31#include "hw/loader.h"
32#include "elf.h"
33#include "sysemu/sysemu.h"
34
35
36
37
38#ifdef DEBUG_MULTIBOOT
39#define mb_debug(a...) fprintf(stderr, ## a)
40#else
41#define mb_debug(a...)
42#endif
43
44#define MULTIBOOT_STRUCT_ADDR 0x9000
45
46#if MULTIBOOT_STRUCT_ADDR > 0xf0000
47#error multiboot struct needs to fit in 16 bit real mode
48#endif
49
50enum {
51
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,
61
62 MBI_SIZE = 88,
63
64
65 MB_MOD_START = 0,
66 MB_MOD_END = 4,
67 MB_MOD_CMDLINE = 8,
68
69 MB_MOD_SIZE = 16,
70
71
72 ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
73 ADDR_MBI = ADDR_E820_MAP + 0x500,
74
75
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,
82};
83
84typedef struct {
85
86 void *mb_buf;
87
88 hwaddr mb_buf_phys;
89
90 unsigned mb_buf_size;
91
92 hwaddr offset_mbinfo;
93
94 hwaddr offset_cmdlines;
95
96 hwaddr offset_bootloader;
97
98 hwaddr offset_mods;
99
100 int mb_mods_avail;
101
102 int mb_mods_count;
103} MultibootState;
104
105const char *bootloader_name = "qemu";
106
107static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
108{
109 hwaddr p = s->offset_cmdlines;
110 char *b = (char *)s->mb_buf + p;
111
112 memcpy(b, cmdline, strlen(cmdline) + 1);
113 s->offset_cmdlines += strlen(b) + 1;
114 return s->mb_buf_phys + p;
115}
116
117static uint32_t mb_add_bootloader(MultibootState *s, const char *bootloader)
118{
119 hwaddr p = s->offset_bootloader;
120 char *b = (char *)s->mb_buf + p;
121
122 memcpy(b, bootloader, strlen(bootloader) + 1);
123 s->offset_bootloader += strlen(b) + 1;
124 return s->mb_buf_phys + p;
125}
126
127static void mb_add_mod(MultibootState *s,
128 hwaddr start, hwaddr end,
129 hwaddr cmdline_phys)
130{
131 char *p;
132 assert(s->mb_mods_count < s->mb_mods_avail);
133
134 p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
135
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);
139
140 mb_debug("mod%02d: "TARGET_FMT_plx" - "TARGET_FMT_plx"\n",
141 s->mb_mods_count, start, end);
142
143 s->mb_mods_count++;
144}
145
146int load_multiboot(FWCfgState *fw_cfg,
147 FILE *f,
148 const char *kernel_filename,
149 const char *initrd_filename,
150 const char *kernel_cmdline,
151 int kernel_file_size,
152 uint8_t *header)
153{
154 int i, is_multiboot = 0;
155 uint32_t flags = 0;
156 uint32_t mh_entry_addr;
157 uint32_t mh_load_addr;
158 uint32_t mb_kernel_size;
159 MultibootState mbs;
160 uint8_t bootinfo[MBI_SIZE];
161 uint8_t *mb_bootinfo_data;
162 uint32_t cmdline_len;
163
164
165
166 for (i = 0; i < (8192 - 48); i += 4) {
167 if (ldl_p(header+i) == 0x1BADB002) {
168 uint32_t checksum = ldl_p(header+i+8);
169 flags = ldl_p(header+i+4);
170 checksum += flags;
171 checksum += (uint32_t)0x1BADB002;
172 if (!checksum) {
173 is_multiboot = 1;
174 break;
175 }
176 }
177 }
178
179 if (!is_multiboot)
180 return 0;
181
182 mb_debug("qemu: I believe we found a multiboot image!\n");
183 memset(bootinfo, 0, sizeof(bootinfo));
184 memset(&mbs, 0, sizeof(mbs));
185
186 if (flags & 0x00000004) {
187 fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
188 }
189 if (!(flags & 0x00010000)) {
190 uint64_t elf_entry;
191 uint64_t elf_low, elf_high;
192 int kernel_size;
193 fclose(f);
194
195 if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
196 fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
197 exit(1);
198 }
199
200 kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
201 &elf_low, &elf_high, 0, I386_ELF_MACHINE,
202 0, 0);
203 if (kernel_size < 0) {
204 fprintf(stderr, "Error while loading elf kernel\n");
205 exit(1);
206 }
207 mh_load_addr = elf_low;
208 mb_kernel_size = elf_high - elf_low;
209 mh_entry_addr = elf_entry;
210
211 mbs.mb_buf = g_malloc(mb_kernel_size);
212 if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
213 fprintf(stderr, "Error while fetching elf kernel from rom\n");
214 exit(1);
215 }
216
217 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
218 mb_kernel_size, (size_t)mh_entry_addr);
219 } else {
220
221 uint32_t mh_header_addr = ldl_p(header+i+12);
222 uint32_t mh_load_end_addr = ldl_p(header+i+20);
223 uint32_t mh_bss_end_addr = ldl_p(header+i+24);
224
225 mh_load_addr = ldl_p(header+i+16);
226 if (mh_header_addr < mh_load_addr) {
227 fprintf(stderr, "invalid mh_load_addr address\n");
228 exit(1);
229 }
230
231 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
232 uint32_t mb_load_size = 0;
233 mh_entry_addr = ldl_p(header+i+28);
234
235 if (mh_load_end_addr) {
236 if (mh_bss_end_addr < mh_load_addr) {
237 fprintf(stderr, "invalid mh_bss_end_addr address\n");
238 exit(1);
239 }
240 mb_kernel_size = mh_bss_end_addr - mh_load_addr;
241
242 if (mh_load_end_addr < mh_load_addr) {
243 fprintf(stderr, "invalid mh_load_end_addr address\n");
244 exit(1);
245 }
246 mb_load_size = mh_load_end_addr - mh_load_addr;
247 } else {
248 if (kernel_file_size < mb_kernel_text_offset) {
249 fprintf(stderr, "invalid kernel_file_size\n");
250 exit(1);
251 }
252 mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
253 mb_load_size = mb_kernel_size;
254 }
255
256
257
258
259
260
261
262 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
263 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
264 mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
265 mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
266 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
267 mb_load_size, mh_load_addr);
268
269 mbs.mb_buf = g_malloc(mb_kernel_size);
270 fseek(f, mb_kernel_text_offset, SEEK_SET);
271 if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
272 fprintf(stderr, "fread() failed\n");
273 exit(1);
274 }
275 memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
276 fclose(f);
277 }
278
279 mbs.mb_buf_phys = mh_load_addr;
280
281 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
282 mbs.offset_mbinfo = mbs.mb_buf_size;
283
284
285 cmdline_len = strlen(kernel_filename) + 1;
286 cmdline_len += strlen(kernel_cmdline) + 1;
287 if (initrd_filename) {
288 const char *r = initrd_filename;
289 cmdline_len += strlen(r) + 1;
290 mbs.mb_mods_avail = 1;
291 while (*(r = get_opt_value(NULL, 0, r))) {
292 mbs.mb_mods_avail++;
293 r++;
294 }
295 }
296
297 mbs.mb_buf_size += cmdline_len;
298 mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
299 mbs.mb_buf_size += strlen(bootloader_name) + 1;
300
301 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
302
303
304 mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
305 mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
306 mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
307
308 if (initrd_filename) {
309 const char *next_initrd;
310 char not_last, tmpbuf[strlen(initrd_filename) + 1];
311
312 mbs.offset_mods = mbs.mb_buf_size;
313
314 do {
315 char *next_space;
316 int mb_mod_length;
317 uint32_t offs = mbs.mb_buf_size;
318
319 next_initrd = get_opt_value(tmpbuf, sizeof(tmpbuf), initrd_filename);
320 not_last = *next_initrd;
321
322
323 hwaddr c = mb_add_cmdline(&mbs, tmpbuf);
324 if ((next_space = strchr(tmpbuf, ' ')))
325 *next_space = '\0';
326 mb_debug("multiboot loading module: %s\n", tmpbuf);
327 mb_mod_length = get_image_size(tmpbuf);
328 if (mb_mod_length < 0) {
329 fprintf(stderr, "Failed to open file '%s'\n", tmpbuf);
330 exit(1);
331 }
332
333 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
334 mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
335
336 load_image(tmpbuf, (unsigned char *)mbs.mb_buf + offs);
337 mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
338 mbs.mb_buf_phys + offs + mb_mod_length, c);
339
340 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx"\n",
341 (char *)mbs.mb_buf + offs,
342 (char *)mbs.mb_buf + offs + mb_mod_length, c);
343 initrd_filename = next_initrd+1;
344 } while (not_last);
345 }
346
347
348 char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
349 snprintf(kcmdline, sizeof(kcmdline), "%s %s",
350 kernel_filename, kernel_cmdline);
351 stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
352
353 stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
354
355 stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
356 stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count);
357
358
359 stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
360 | MULTIBOOT_FLAGS_BOOT_DEVICE
361 | MULTIBOOT_FLAGS_CMDLINE
362 | MULTIBOOT_FLAGS_MODULES
363 | MULTIBOOT_FLAGS_MMAP
364 | MULTIBOOT_FLAGS_BOOTLOADER);
365 stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff);
366 stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
367
368 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
369 mb_debug(" mb_buf_phys = "TARGET_FMT_plx"\n", mbs.mb_buf_phys);
370 mb_debug(" mod_start = "TARGET_FMT_plx"\n", mbs.mb_buf_phys + mbs.offset_mods);
371 mb_debug(" mb_mods_count = %d\n", mbs.mb_mods_count);
372
373
374 mb_bootinfo_data = g_memdup(bootinfo, sizeof(bootinfo));
375
376
377 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
378 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
379 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
380 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
381 mbs.mb_buf, mbs.mb_buf_size);
382
383 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
384 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
385 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
386 sizeof(bootinfo));
387
388 option_rom[nb_option_roms].name = "multiboot.bin";
389 option_rom[nb_option_roms].bootindex = 0;
390 nb_option_roms++;
391
392 return 1;
393}
394