sparc64: fix udiv and sdiv insns
[qemu/aliguori-queue.git] / hw / multiboot.c
bloba1b665cd41285b8e23859d1c1eeaa09e92640182
1 /*
2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "hw.h"
26 #include "fw_cfg.h"
27 #include "multiboot.h"
28 #include "loader.h"
29 #include "elf.h"
30 #include "sysemu.h"
32 /* Show multiboot debug output */
33 //#define DEBUG_MULTIBOOT
35 #ifdef DEBUG_MULTIBOOT
36 #define mb_debug(a...) fprintf(stderr, ## a)
37 #else
38 #define mb_debug(a...)
39 #endif
41 #define MULTIBOOT_STRUCT_ADDR 0x9000
43 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
44 #error multiboot struct needs to fit in 16 bit real mode
45 #endif
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,
58 MBI_SIZE = 88,
60 /* Multiboot modules */
61 MB_MOD_START = 0,
62 MB_MOD_END = 4,
63 MB_MOD_CMDLINE = 8,
65 MB_MOD_SIZE = 16,
67 /* Region offsets */
68 ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
69 ADDR_MBI = ADDR_E820_MAP + 0x500,
71 /* Multiboot flags */
72 MULTIBOOT_FLAGS_MEMORY = 1 << 0,
73 MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
74 MULTIBOOT_FLAGS_CMDLINE = 1 << 2,
75 MULTIBOOT_FLAGS_MODULES = 1 << 3,
76 MULTIBOOT_FLAGS_MMAP = 1 << 6,
79 typedef struct {
80 /* buffer holding kernel, cmdlines and mb_infos */
81 void *mb_buf;
82 /* address in target */
83 target_phys_addr_t mb_buf_phys;
84 /* size of mb_buf in bytes */
85 unsigned mb_buf_size;
86 /* offset of mb-info's in bytes */
87 target_phys_addr_t offset_mbinfo;
88 /* offset in buffer for cmdlines in bytes */
89 target_phys_addr_t offset_cmdlines;
90 /* offset of modules in bytes */
91 target_phys_addr_t offset_mods;
92 /* available slots for mb modules infos */
93 int mb_mods_avail;
94 /* currently used slots of mb modules */
95 int mb_mods_count;
96 } MultibootState;
98 static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
100 int len = strlen(cmdline) + 1;
101 target_phys_addr_t p = s->offset_cmdlines;
103 pstrcpy((char *)s->mb_buf + p, len, cmdline);
104 s->offset_cmdlines += len;
105 return s->mb_buf_phys + p;
108 static void mb_add_mod(MultibootState *s,
109 target_phys_addr_t start, target_phys_addr_t end,
110 target_phys_addr_t cmdline_phys)
112 char *p;
113 assert(s->mb_mods_count < s->mb_mods_avail);
115 p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
117 stl_p(p + MB_MOD_START, start);
118 stl_p(p + MB_MOD_END, end);
119 stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
121 mb_debug("mod%02d: %08x - %08x\n", s->mb_mods_count, start, end);
123 s->mb_mods_count++;
126 int load_multiboot(void *fw_cfg,
127 FILE *f,
128 const char *kernel_filename,
129 const char *initrd_filename,
130 const char *kernel_cmdline,
131 int kernel_file_size,
132 uint8_t *header)
134 int i, is_multiboot = 0;
135 uint32_t flags = 0;
136 uint32_t mh_entry_addr;
137 uint32_t mh_load_addr;
138 uint32_t mb_kernel_size;
139 MultibootState mbs;
140 uint8_t bootinfo[MBI_SIZE];
141 uint8_t *mb_bootinfo_data;
143 /* Ok, let's see if it is a multiboot image.
144 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
145 for (i = 0; i < (8192 - 48); i += 4) {
146 if (ldl_p(header+i) == 0x1BADB002) {
147 uint32_t checksum = ldl_p(header+i+8);
148 flags = ldl_p(header+i+4);
149 checksum += flags;
150 checksum += (uint32_t)0x1BADB002;
151 if (!checksum) {
152 is_multiboot = 1;
153 break;
158 if (!is_multiboot)
159 return 0; /* no multiboot */
161 mb_debug("qemu: I believe we found a multiboot image!\n");
162 memset(bootinfo, 0, sizeof(bootinfo));
163 memset(&mbs, 0, sizeof(mbs));
165 if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
166 fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
168 if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
169 uint64_t elf_entry;
170 uint64_t elf_low, elf_high;
171 int kernel_size;
172 fclose(f);
173 kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
174 &elf_low, &elf_high, 0, ELF_MACHINE, 0);
175 if (kernel_size < 0) {
176 fprintf(stderr, "Error while loading elf kernel\n");
177 exit(1);
179 mh_load_addr = elf_low;
180 mb_kernel_size = elf_high - elf_low;
181 mh_entry_addr = elf_entry;
183 mbs.mb_buf = qemu_malloc(mb_kernel_size);
184 if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
185 fprintf(stderr, "Error while fetching elf kernel from rom\n");
186 exit(1);
189 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
190 mb_kernel_size, (size_t)mh_entry_addr);
191 } else {
192 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
193 uint32_t mh_header_addr = ldl_p(header+i+12);
194 mh_load_addr = ldl_p(header+i+16);
195 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
197 mh_entry_addr = ldl_p(header+i+28);
198 mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
200 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
201 uint32_t mh_mode_type = ldl_p(header+i+32);
202 uint32_t mh_width = ldl_p(header+i+36);
203 uint32_t mh_height = ldl_p(header+i+40);
204 uint32_t mh_depth = ldl_p(header+i+44); */
206 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
207 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
208 mb_debug("multiboot: mh_load_end_addr = %#x\n", ldl_p(header+i+20));
209 mb_debug("multiboot: mh_bss_end_addr = %#x\n", ldl_p(header+i+24));
210 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
211 mb_kernel_size, mh_load_addr);
213 mbs.mb_buf = qemu_malloc(mb_kernel_size);
214 fseek(f, mb_kernel_text_offset, SEEK_SET);
215 if (fread(mbs.mb_buf, 1, mb_kernel_size, f) != mb_kernel_size) {
216 fprintf(stderr, "fread() failed\n");
217 exit(1);
219 fclose(f);
222 mbs.mb_buf_phys = mh_load_addr;
224 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
225 mbs.offset_mbinfo = mbs.mb_buf_size;
227 /* Calculate space for cmdlines and mb_mods */
228 mbs.mb_buf_size += strlen(kernel_filename) + 1;
229 mbs.mb_buf_size += strlen(kernel_cmdline) + 1;
230 if (initrd_filename) {
231 const char *r = initrd_filename;
232 mbs.mb_buf_size += strlen(r) + 1;
233 mbs.mb_mods_avail = 1;
234 while ((r = strchr(r, ','))) {
235 mbs.mb_mods_avail++;
236 r++;
238 mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
241 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
243 /* enlarge mb_buf to hold cmdlines and mb-info structs */
244 mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size);
245 mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
247 if (initrd_filename) {
248 char *next_initrd;
250 mbs.offset_mods = mbs.mb_buf_size;
252 do {
253 char *next_space;
254 uint32_t mb_mod_length;
255 uint32_t offs = mbs.mb_buf_size;
257 next_initrd = strchr(initrd_filename, ',');
258 if (next_initrd)
259 *next_initrd = '\0';
260 /* if a space comes after the module filename, treat everything
261 after that as parameters */
262 target_phys_addr_t c = mb_add_cmdline(&mbs, initrd_filename);
263 if ((next_space = strchr(initrd_filename, ' ')))
264 *next_space = '\0';
265 mb_debug("multiboot loading module: %s\n", initrd_filename);
266 mb_mod_length = get_image_size(initrd_filename);
267 if (mb_mod_length < 0) {
268 fprintf(stderr, "failed to get %s image size\n", initrd_filename);
269 exit(1);
272 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
273 mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size);
275 load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs);
276 mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
277 mbs.mb_buf_phys + offs + mb_mod_length, c);
279 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: %#x\n",
280 (char *)mbs.mb_buf + offs,
281 (char *)mbs.mb_buf + offs + mb_mod_length, c);
282 initrd_filename = next_initrd+1;
283 } while (next_initrd);
286 /* Commandline support */
287 char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
288 snprintf(kcmdline, sizeof(kcmdline), "%s %s",
289 kernel_filename, kernel_cmdline);
290 stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
292 stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
293 stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
295 /* the kernel is where we want it to be now */
296 stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
297 | MULTIBOOT_FLAGS_BOOT_DEVICE
298 | MULTIBOOT_FLAGS_CMDLINE
299 | MULTIBOOT_FLAGS_MODULES
300 | MULTIBOOT_FLAGS_MMAP);
301 stl_p(bootinfo + MBI_MEM_LOWER, 640);
302 stl_p(bootinfo + MBI_MEM_UPPER, ram_size / 1024);
303 stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8001ffff); /* XXX: use the -boot switch? */
304 stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
306 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
307 mb_debug(" mb_buf_phys = %x\n", mbs.mb_buf_phys);
308 mb_debug(" mod_start = %x\n", mbs.mb_buf_phys + mbs.offset_mods);
309 mb_debug(" mb_mods_count = %d\n", mbs.mb_mods_count);
311 /* save bootinfo off the stack */
312 mb_bootinfo_data = qemu_malloc(sizeof(bootinfo));
313 memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
315 /* Pass variables to option rom */
316 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
317 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
318 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
319 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
320 mbs.mb_buf, mbs.mb_buf_size);
322 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
323 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
324 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
325 sizeof(bootinfo));
327 option_rom[nb_option_roms] = "multiboot.bin";
328 nb_option_roms++;
330 return 1; /* yes, we are multiboot */