xref: /qemu/hw/i386/multiboot.c (revision f2ad72b3)
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 "qemu/osdep.h"
26 #include "hw/hw.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "multiboot.h"
29 #include "hw/loader.h"
30 #include "elf.h"
31 #include "sysemu/sysemu.h"
32 
33 /* Show multiboot debug output */
34 //#define DEBUG_MULTIBOOT
35 
36 #ifdef DEBUG_MULTIBOOT
37 #define mb_debug(a...) fprintf(stderr, ## a)
38 #else
39 #define mb_debug(a...)
40 #endif
41 
42 #define MULTIBOOT_STRUCT_ADDR 0x9000
43 
44 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
45 #error multiboot struct needs to fit in 16 bit real mode
46 #endif
47 
48 enum {
49     /* Multiboot info */
50     MBI_FLAGS       = 0,
51     MBI_MEM_LOWER   = 4,
52     MBI_MEM_UPPER   = 8,
53     MBI_BOOT_DEVICE = 12,
54     MBI_CMDLINE     = 16,
55     MBI_MODS_COUNT  = 20,
56     MBI_MODS_ADDR   = 24,
57     MBI_MMAP_ADDR   = 48,
58     MBI_BOOTLOADER  = 64,
59 
60     MBI_SIZE        = 88,
61 
62     /* Multiboot modules */
63     MB_MOD_START    = 0,
64     MB_MOD_END      = 4,
65     MB_MOD_CMDLINE  = 8,
66 
67     MB_MOD_SIZE     = 16,
68 
69     /* Region offsets */
70     ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
71     ADDR_MBI      = ADDR_E820_MAP + 0x500,
72 
73     /* Multiboot flags */
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,
80 };
81 
82 typedef struct {
83     /* buffer holding kernel, cmdlines and mb_infos */
84     void *mb_buf;
85     /* address in target */
86     hwaddr mb_buf_phys;
87     /* size of mb_buf in bytes */
88     unsigned mb_buf_size;
89     /* offset of mb-info's in bytes */
90     hwaddr offset_mbinfo;
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 */
96     hwaddr offset_mods;
97     /* available slots for mb modules infos */
98     int mb_mods_avail;
99     /* currently used slots of mb modules */
100     int mb_mods_count;
101 } MultibootState;
102 
103 const char *bootloader_name = "qemu";
104 
105 static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
106 {
107     hwaddr p = s->offset_cmdlines;
108     char *b = (char *)s->mb_buf + p;
109 
110     get_opt_value(b, strlen(cmdline) + 1, cmdline);
111     s->offset_cmdlines += strlen(b) + 1;
112     return s->mb_buf_phys + p;
113 }
114 
115 static uint32_t mb_add_bootloader(MultibootState *s, const char *bootloader)
116 {
117     hwaddr p = s->offset_bootloader;
118     char *b = (char *)s->mb_buf + p;
119 
120     memcpy(b, bootloader, strlen(bootloader) + 1);
121     s->offset_bootloader += strlen(b) + 1;
122     return s->mb_buf_phys + p;
123 }
124 
125 static void mb_add_mod(MultibootState *s,
126                        hwaddr start, hwaddr end,
127                        hwaddr cmdline_phys)
128 {
129     char *p;
130     assert(s->mb_mods_count < s->mb_mods_avail);
131 
132     p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
133 
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);
137 
138     mb_debug("mod%02d: "TARGET_FMT_plx" - "TARGET_FMT_plx"\n",
139              s->mb_mods_count, start, end);
140 
141     s->mb_mods_count++;
142 }
143 
144 int load_multiboot(FWCfgState *fw_cfg,
145                    FILE *f,
146                    const char *kernel_filename,
147                    const char *initrd_filename,
148                    const char *kernel_cmdline,
149                    int kernel_file_size,
150                    uint8_t *header)
151 {
152     int i, is_multiboot = 0;
153     uint32_t flags = 0;
154     uint32_t mh_entry_addr;
155     uint32_t mh_load_addr;
156     uint32_t mb_kernel_size;
157     MultibootState mbs;
158     uint8_t bootinfo[MBI_SIZE];
159     uint8_t *mb_bootinfo_data;
160     uint32_t cmdline_len;
161 
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);
168             checksum += flags;
169             checksum += (uint32_t)0x1BADB002;
170             if (!checksum) {
171                 is_multiboot = 1;
172                 break;
173             }
174         }
175     }
176 
177     if (!is_multiboot)
178         return 0; /* no multiboot */
179 
180     mb_debug("qemu: I believe we found a multiboot image!\n");
181     memset(bootinfo, 0, sizeof(bootinfo));
182     memset(&mbs, 0, sizeof(mbs));
183 
184     if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
185         fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
186     }
187     if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
188         uint64_t elf_entry;
189         uint64_t elf_low, elf_high;
190         int kernel_size;
191         fclose(f);
192 
193         if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
194             fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
195             exit(1);
196         }
197 
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");
202             exit(1);
203         }
204         mh_load_addr = elf_low;
205         mb_kernel_size = elf_high - elf_low;
206         mh_entry_addr = elf_entry;
207 
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");
211             exit(1);
212         }
213 
214         mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
215                   mb_kernel_size, (size_t)mh_entry_addr);
216     } else {
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);
225 
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;
229         } else {
230             mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
231             mb_load_size = mb_kernel_size;
232         }
233 
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); */
239 
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);
246 
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");
251             exit(1);
252         }
253         memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
254         fclose(f);
255     }
256 
257     mbs.mb_buf_phys = mh_load_addr;
258 
259     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
260     mbs.offset_mbinfo = mbs.mb_buf_size;
261 
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))) {
270            mbs.mb_mods_avail++;
271            r++;
272         }
273     }
274 
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;
278 
279     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
280 
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;
285 
286     if (initrd_filename) {
287         char *next_initrd, not_last;
288 
289         mbs.offset_mods = mbs.mb_buf_size;
290 
291         do {
292             char *next_space;
293             int mb_mod_length;
294             uint32_t offs = mbs.mb_buf_size;
295 
296             next_initrd = (char *)get_opt_value(NULL, 0, initrd_filename);
297             not_last = *next_initrd;
298             *next_initrd = '\0';
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, ' ')))
303                 *next_space = '\0';
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);
308                 exit(1);
309             }
310 
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);
313 
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);
317 
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;
322         } while (not_last);
323     }
324 
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));
330 
331     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
332 
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 */
335 
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);
345 
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);
350 
351     /* save bootinfo off the stack */
352     mb_bootinfo_data = g_malloc(sizeof(bootinfo));
353     memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
354 
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);
361 
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,
365                      sizeof(bootinfo));
366 
367     option_rom[nb_option_roms].name = "multiboot.bin";
368     option_rom[nb_option_roms].bootindex = 0;
369     nb_option_roms++;
370 
371     return 1; /* yes, we are multiboot */
372 }
373