xref: /openbmc/qemu/hw/i386/multiboot.c (revision ad1a706f)
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 "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"
34 
35 /* Show multiboot debug output */
36 //#define DEBUG_MULTIBOOT
37 
38 #ifdef DEBUG_MULTIBOOT
39 #define mb_debug(a...) error_report(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 
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,
61 
62     MBI_SIZE        = 88,
63 
64     /* Multiboot modules */
65     MB_MOD_START    = 0,
66     MB_MOD_END      = 4,
67     MB_MOD_CMDLINE  = 8,
68 
69     MB_MOD_SIZE     = 16,
70 
71     /* Region offsets */
72     ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
73     ADDR_MBI      = ADDR_E820_MAP + 0x500,
74 
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,
82 };
83 
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;
104 
105 const char *bootloader_name = "qemu";
106 
107 static 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 
117 static 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 
127 static 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,
141              s->mb_mods_count, start, end);
142 
143     s->mb_mods_count++;
144 }
145 
146 int 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     GList *mods = NULL;
164 
165     /* Ok, let's see if it is a multiboot image.
166        The header is 12x32bit long, so the latest entry may be 8192 - 48. */
167     for (i = 0; i < (8192 - 48); i += 4) {
168         if (ldl_p(header+i) == 0x1BADB002) {
169             uint32_t checksum = ldl_p(header+i+8);
170             flags = ldl_p(header+i+4);
171             checksum += flags;
172             checksum += (uint32_t)0x1BADB002;
173             if (!checksum) {
174                 is_multiboot = 1;
175                 break;
176             }
177         }
178     }
179 
180     if (!is_multiboot)
181         return 0; /* no multiboot */
182 
183     mb_debug("I believe we found a multiboot image!");
184     memset(bootinfo, 0, sizeof(bootinfo));
185     memset(&mbs, 0, sizeof(mbs));
186 
187     if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
188         error_report("multiboot knows VBE. we don't");
189     }
190     if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
191         uint64_t elf_entry;
192         uint64_t elf_low, elf_high;
193         int kernel_size;
194         fclose(f);
195 
196         if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
197             error_report("Cannot load x86-64 image, give a 32bit one.");
198             exit(1);
199         }
200 
201         kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, &elf_entry,
202                                &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
203                                0, 0);
204         if (kernel_size < 0) {
205             error_report("Error while loading elf kernel");
206             exit(1);
207         }
208         mh_load_addr = elf_low;
209         mb_kernel_size = elf_high - elf_low;
210         mh_entry_addr = elf_entry;
211 
212         mbs.mb_buf = g_malloc(mb_kernel_size);
213         if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
214             error_report("Error while fetching elf kernel from rom");
215             exit(1);
216         }
217 
218         mb_debug("loading multiboot-elf kernel "
219                  "(%#x bytes) with entry %#zx",
220                  mb_kernel_size, (size_t)mh_entry_addr);
221     } else {
222         /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
223         uint32_t mh_header_addr = ldl_p(header+i+12);
224         uint32_t mh_load_end_addr = ldl_p(header+i+20);
225         uint32_t mh_bss_end_addr = ldl_p(header+i+24);
226 
227         mh_load_addr = ldl_p(header+i+16);
228         if (mh_header_addr < mh_load_addr) {
229             error_report("invalid load_addr address");
230             exit(1);
231         }
232         if (mh_header_addr - mh_load_addr > i) {
233             error_report("invalid header_addr address");
234             exit(1);
235         }
236 
237         uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
238         uint32_t mb_load_size = 0;
239         mh_entry_addr = ldl_p(header+i+28);
240 
241         if (mh_load_end_addr) {
242             if (mh_load_end_addr < mh_load_addr) {
243                 error_report("invalid load_end_addr address");
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                 error_report("invalid kernel_file_size");
250                 exit(1);
251             }
252             mb_load_size = kernel_file_size - mb_kernel_text_offset;
253         }
254         if (mb_load_size > UINT32_MAX - mh_load_addr) {
255             error_report("kernel does not fit in address space");
256             exit(1);
257         }
258         if (mh_bss_end_addr) {
259             if (mh_bss_end_addr < (mh_load_addr + mb_load_size)) {
260                 error_report("invalid bss_end_addr address");
261                 exit(1);
262             }
263             mb_kernel_size = mh_bss_end_addr - mh_load_addr;
264         } else {
265             mb_kernel_size = mb_load_size;
266         }
267 
268         mb_debug("multiboot: header_addr = %#x", mh_header_addr);
269         mb_debug("multiboot: load_addr = %#x", mh_load_addr);
270         mb_debug("multiboot: load_end_addr = %#x", mh_load_end_addr);
271         mb_debug("multiboot: bss_end_addr = %#x", mh_bss_end_addr);
272         mb_debug("loading multiboot kernel (%#x bytes) at %#x",
273                  mb_load_size, mh_load_addr);
274 
275         mbs.mb_buf = g_malloc(mb_kernel_size);
276         fseek(f, mb_kernel_text_offset, SEEK_SET);
277         if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
278             error_report("fread() failed");
279             exit(1);
280         }
281         memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
282         fclose(f);
283     }
284 
285     mbs.mb_buf_phys = mh_load_addr;
286 
287     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
288     mbs.offset_mbinfo = mbs.mb_buf_size;
289 
290     /* Calculate space for cmdlines, bootloader name, and mb_mods */
291     cmdline_len = strlen(kernel_filename) + 1;
292     cmdline_len += strlen(kernel_cmdline) + 1;
293     if (initrd_filename) {
294         const char *r = initrd_filename;
295         cmdline_len += strlen(initrd_filename) + 1;
296         while (*r) {
297             char *value;
298             r = get_opt_value(r, &value);
299             mbs.mb_mods_avail++;
300             mods = g_list_append(mods, value);
301             if (*r) {
302                 r++;
303             }
304         }
305     }
306 
307     mbs.mb_buf_size += cmdline_len;
308     mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
309     mbs.mb_buf_size += strlen(bootloader_name) + 1;
310 
311     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
312 
313     /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
314     mbs.mb_buf            = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
315     mbs.offset_cmdlines   = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
316     mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
317 
318     if (mods) {
319         GList *tmpl = mods;
320         mbs.offset_mods = mbs.mb_buf_size;
321 
322         while (tmpl) {
323             char *next_space;
324             int mb_mod_length;
325             uint32_t offs = mbs.mb_buf_size;
326             char *one_file = tmpl->data;
327 
328             /* if a space comes after the module filename, treat everything
329                after that as parameters */
330             hwaddr c = mb_add_cmdline(&mbs, one_file);
331             next_space = strchr(one_file, ' ');
332             if (next_space) {
333                 *next_space = '\0';
334             }
335             mb_debug("multiboot loading module: %s", one_file);
336             mb_mod_length = get_image_size(one_file);
337             if (mb_mod_length < 0) {
338                 error_report("Failed to open file '%s'", one_file);
339                 exit(1);
340             }
341 
342             mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
343             mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
344 
345             if (load_image_size(one_file, (unsigned char *)mbs.mb_buf + offs,
346                                 mbs.mb_buf_size - offs) < 0) {
347                 error_report("Error loading file '%s'", one_file);
348                 exit(1);
349             }
350             mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
351                        mbs.mb_buf_phys + offs + mb_mod_length, c);
352 
353             mb_debug("mod_start: %p\nmod_end:   %p\n  cmdline: "TARGET_FMT_plx,
354                      (char *)mbs.mb_buf + offs,
355                      (char *)mbs.mb_buf + offs + mb_mod_length, c);
356             g_free(one_file);
357             tmpl = tmpl->next;
358         }
359         g_list_free(mods);
360     }
361 
362     /* Commandline support */
363     char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
364     snprintf(kcmdline, sizeof(kcmdline), "%s %s",
365              kernel_filename, kernel_cmdline);
366     stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
367 
368     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
369 
370     stl_p(bootinfo + MBI_MODS_ADDR,  mbs.mb_buf_phys + mbs.offset_mbinfo);
371     stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
372 
373     /* the kernel is where we want it to be now */
374     stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
375                                 | MULTIBOOT_FLAGS_BOOT_DEVICE
376                                 | MULTIBOOT_FLAGS_CMDLINE
377                                 | MULTIBOOT_FLAGS_MODULES
378                                 | MULTIBOOT_FLAGS_MMAP
379                                 | MULTIBOOT_FLAGS_BOOTLOADER);
380     stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
381     stl_p(bootinfo + MBI_MMAP_ADDR,   ADDR_E820_MAP);
382 
383     mb_debug("multiboot: entry_addr = %#x", mh_entry_addr);
384     mb_debug("           mb_buf_phys   = "TARGET_FMT_plx, mbs.mb_buf_phys);
385     mb_debug("           mod_start     = "TARGET_FMT_plx,
386              mbs.mb_buf_phys + mbs.offset_mods);
387     mb_debug("           mb_mods_count = %d", mbs.mb_mods_count);
388 
389     /* save bootinfo off the stack */
390     mb_bootinfo_data = g_memdup(bootinfo, sizeof(bootinfo));
391 
392     /* Pass variables to option rom */
393     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
394     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
395     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
396     fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
397                      mbs.mb_buf, mbs.mb_buf_size);
398 
399     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
400     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
401     fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
402                      sizeof(bootinfo));
403 
404     option_rom[nb_option_roms].name = "multiboot.bin";
405     option_rom[nb_option_roms].bootindex = 0;
406     nb_option_roms++;
407 
408     return 1; /* yes, we are multiboot */
409 }
410