xref: /openbmc/qemu/hw/i386/multiboot.c (revision b917da4c)
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,
200                                0, 0);
201         if (kernel_size < 0) {
202             fprintf(stderr, "Error while loading elf kernel\n");
203             exit(1);
204         }
205         mh_load_addr = elf_low;
206         mb_kernel_size = elf_high - elf_low;
207         mh_entry_addr = elf_entry;
208 
209         mbs.mb_buf = g_malloc(mb_kernel_size);
210         if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
211             fprintf(stderr, "Error while fetching elf kernel from rom\n");
212             exit(1);
213         }
214 
215         mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
216                   mb_kernel_size, (size_t)mh_entry_addr);
217     } else {
218         /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
219         uint32_t mh_header_addr = ldl_p(header+i+12);
220         uint32_t mh_load_end_addr = ldl_p(header+i+20);
221         uint32_t mh_bss_end_addr = ldl_p(header+i+24);
222         mh_load_addr = ldl_p(header+i+16);
223         uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
224         uint32_t mb_load_size = 0;
225         mh_entry_addr = ldl_p(header+i+28);
226 
227         if (mh_load_end_addr) {
228             mb_kernel_size = mh_bss_end_addr - mh_load_addr;
229             mb_load_size = mh_load_end_addr - mh_load_addr;
230         } else {
231             mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
232             mb_load_size = mb_kernel_size;
233         }
234 
235         /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
236         uint32_t mh_mode_type = ldl_p(header+i+32);
237         uint32_t mh_width = ldl_p(header+i+36);
238         uint32_t mh_height = ldl_p(header+i+40);
239         uint32_t mh_depth = ldl_p(header+i+44); */
240 
241         mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
242         mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
243         mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
244         mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
245         mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
246                  mb_load_size, mh_load_addr);
247 
248         mbs.mb_buf = g_malloc(mb_kernel_size);
249         fseek(f, mb_kernel_text_offset, SEEK_SET);
250         if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
251             fprintf(stderr, "fread() failed\n");
252             exit(1);
253         }
254         memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
255         fclose(f);
256     }
257 
258     mbs.mb_buf_phys = mh_load_addr;
259 
260     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
261     mbs.offset_mbinfo = mbs.mb_buf_size;
262 
263     /* Calculate space for cmdlines, bootloader name, and mb_mods */
264     cmdline_len = strlen(kernel_filename) + 1;
265     cmdline_len += strlen(kernel_cmdline) + 1;
266     if (initrd_filename) {
267         const char *r = initrd_filename;
268         cmdline_len += strlen(r) + 1;
269         mbs.mb_mods_avail = 1;
270         while (*(r = get_opt_value(NULL, 0, r))) {
271            mbs.mb_mods_avail++;
272            r++;
273         }
274     }
275 
276     mbs.mb_buf_size += cmdline_len;
277     mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
278     mbs.mb_buf_size += strlen(bootloader_name) + 1;
279 
280     mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
281 
282     /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
283     mbs.mb_buf            = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
284     mbs.offset_cmdlines   = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
285     mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
286 
287     if (initrd_filename) {
288         char *next_initrd, not_last;
289 
290         mbs.offset_mods = mbs.mb_buf_size;
291 
292         do {
293             char *next_space;
294             int mb_mod_length;
295             uint32_t offs = mbs.mb_buf_size;
296 
297             next_initrd = (char *)get_opt_value(NULL, 0, initrd_filename);
298             not_last = *next_initrd;
299             *next_initrd = '\0';
300             /* if a space comes after the module filename, treat everything
301                after that as parameters */
302             hwaddr c = mb_add_cmdline(&mbs, initrd_filename);
303             if ((next_space = strchr(initrd_filename, ' ')))
304                 *next_space = '\0';
305             mb_debug("multiboot loading module: %s\n", initrd_filename);
306             mb_mod_length = get_image_size(initrd_filename);
307             if (mb_mod_length < 0) {
308                 fprintf(stderr, "Failed to open file '%s'\n", initrd_filename);
309                 exit(1);
310             }
311 
312             mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
313             mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
314 
315             load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs);
316             mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
317                        mbs.mb_buf_phys + offs + mb_mod_length, c);
318 
319             mb_debug("mod_start: %p\nmod_end:   %p\n  cmdline: "TARGET_FMT_plx"\n",
320                      (char *)mbs.mb_buf + offs,
321                      (char *)mbs.mb_buf + offs + mb_mod_length, c);
322             initrd_filename = next_initrd+1;
323         } while (not_last);
324     }
325 
326     /* Commandline support */
327     char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
328     snprintf(kcmdline, sizeof(kcmdline), "%s %s",
329              kernel_filename, kernel_cmdline);
330     stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
331 
332     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
333 
334     stl_p(bootinfo + MBI_MODS_ADDR,  mbs.mb_buf_phys + mbs.offset_mbinfo);
335     stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
336 
337     /* the kernel is where we want it to be now */
338     stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
339                                 | MULTIBOOT_FLAGS_BOOT_DEVICE
340                                 | MULTIBOOT_FLAGS_CMDLINE
341                                 | MULTIBOOT_FLAGS_MODULES
342                                 | MULTIBOOT_FLAGS_MMAP
343                                 | MULTIBOOT_FLAGS_BOOTLOADER);
344     stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
345     stl_p(bootinfo + MBI_MMAP_ADDR,   ADDR_E820_MAP);
346 
347     mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
348     mb_debug("           mb_buf_phys   = "TARGET_FMT_plx"\n", mbs.mb_buf_phys);
349     mb_debug("           mod_start     = "TARGET_FMT_plx"\n", mbs.mb_buf_phys + mbs.offset_mods);
350     mb_debug("           mb_mods_count = %d\n", mbs.mb_mods_count);
351 
352     /* save bootinfo off the stack */
353     mb_bootinfo_data = g_malloc(sizeof(bootinfo));
354     memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
355 
356     /* Pass variables to option rom */
357     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
358     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
359     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
360     fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
361                      mbs.mb_buf, mbs.mb_buf_size);
362 
363     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
364     fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
365     fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
366                      sizeof(bootinfo));
367 
368     option_rom[nb_option_roms].name = "multiboot.bin";
369     option_rom[nb_option_roms].bootindex = 0;
370     nb_option_roms++;
371 
372     return 1; /* yes, we are multiboot */
373 }
374