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
41 #include <netinet/in.h>
46 #define BIOS_FILENAME "bios.bin"
47 #define VGABIOS_FILENAME "vgabios.bin"
48 #define LINUX_BOOT_FILENAME "linux_boot.bin"
50 #define KERNEL_LOAD_ADDR 0x00100000
51 #define INITRD_LOAD_ADDR 0x00400000
52 #define KERNEL_PARAMS_ADDR 0x00090000
53 #define KERNEL_CMDLINE_ADDR 0x00099000
56 int dummy_refresh_clock;
58 static void ioport80_write(CPUState *env, uint32_t addr, uint32_t data)
62 #define REG_EQUIPMENT_BYTE 0x14
64 static void cmos_init(int ram_size, int boot_device)
66 RTCState *s = &rtc_state;
69 /* various important CMOS locations needed by PC/Bochs bios */
71 s->cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */
72 s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x04; /* PS/2 mouse installed */
75 val = (ram_size / 1024) - 1024;
78 s->cmos_data[0x17] = val;
79 s->cmos_data[0x18] = val >> 8;
80 s->cmos_data[0x30] = val;
81 s->cmos_data[0x31] = val >> 8;
83 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
86 s->cmos_data[0x34] = val;
87 s->cmos_data[0x35] = val >> 8;
92 s->cmos_data[0x3d] = 0x01; /* floppy boot */
96 s->cmos_data[0x3d] = 0x02; /* hard drive boot */
99 s->cmos_data[0x3d] = 0x03; /* CD-ROM boot */
104 void cmos_register_fd (uint8_t fd0, uint8_t fd1)
106 RTCState *s = &rtc_state;
109 s->cmos_data[0x10] = 0;
112 /* 1.44 Mb 3"5 drive */
113 s->cmos_data[0x10] |= 0x40;
116 /* 2.88 Mb 3"5 drive */
117 s->cmos_data[0x10] |= 0x60;
120 /* 1.2 Mb 5"5 drive */
121 s->cmos_data[0x10] |= 0x20;
126 /* 1.44 Mb 3"5 drive */
127 s->cmos_data[0x10] |= 0x04;
130 /* 2.88 Mb 3"5 drive */
131 s->cmos_data[0x10] |= 0x06;
134 /* 1.2 Mb 5"5 drive */
135 s->cmos_data[0x10] |= 0x02;
146 s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x01; /* 1 drive, ready for boot */
149 s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x41; /* 2 drives, ready for boot */
154 void speaker_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
156 speaker_data_on = (val >> 1) & 1;
157 pit_set_gate(&pit_channels[2], val & 1);
160 uint32_t speaker_ioport_read(CPUState *env, uint32_t addr)
163 out = pit_get_out(&pit_channels[2]);
164 dummy_refresh_clock ^= 1;
165 return (speaker_data_on << 1) | pit_channels[2].gate | (out << 5) |
166 (dummy_refresh_clock << 4);
169 /***********************************************************/
170 /* PC floppy disk controler emulation glue */
171 #define PC_FDC_DMA 0x2
172 #define PC_FDC_IRQ 0x6
173 #define PC_FDC_BASE 0x3F0
175 static void fdctrl_register (unsigned char **disknames, int ro,
180 fdctrl_init(PC_FDC_IRQ, PC_FDC_DMA, 0, PC_FDC_BASE, boot_device);
181 for (i = 0; i < MAX_FD; i++) {
182 if (disknames[i] != NULL)
183 fdctrl_disk_change(i, disknames[i], ro);
187 /***********************************************************/
188 /* Bochs BIOS debug ports */
190 void bochs_bios_write(CPUX86State *env, uint32_t addr, uint32_t val)
193 /* Bochs BIOS messages */
196 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
201 fprintf(stderr, "%c", val);
205 /* LGPL'ed VGA BIOS messages */
208 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
213 fprintf(stderr, "%c", val);
219 void bochs_bios_init(void)
221 register_ioport_write(0x400, 1, bochs_bios_write, 2);
222 register_ioport_write(0x401, 1, bochs_bios_write, 2);
223 register_ioport_write(0x402, 1, bochs_bios_write, 1);
224 register_ioport_write(0x403, 1, bochs_bios_write, 1);
226 register_ioport_write(0x501, 1, bochs_bios_write, 2);
227 register_ioport_write(0x502, 1, bochs_bios_write, 2);
228 register_ioport_write(0x500, 1, bochs_bios_write, 1);
229 register_ioport_write(0x503, 1, bochs_bios_write, 1);
233 int load_kernel(const char *filename, uint8_t *addr,
239 fd = open(filename, O_RDONLY);
243 /* load 16 bit code */
244 if (read(fd, real_addr, 512) != 512)
246 setup_sects = real_addr[0x1F1];
249 if (read(fd, real_addr + 512, setup_sects * 512) !=
253 /* load 32 bit code */
254 size = read(fd, addr, 16 * 1024 * 1024);
264 /* PC hardware initialisation */
265 void pc_init(int ram_size, int vga_ram_size, int boot_device,
266 DisplayState *ds, const char **fd_filename, int snapshot,
267 const char *kernel_filename, const char *kernel_cmdline,
268 const char *initrd_filename)
271 int ret, linux_boot, initrd_size;
273 linux_boot = (kernel_filename != NULL);
276 cpu_register_physical_memory(0, ram_size, 0);
279 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
280 ret = load_image(buf, phys_ram_base + 0x000f0000);
281 if (ret != 0x10000) {
282 fprintf(stderr, "qemu: could not load PC bios '%s'\n", buf);
287 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
288 ret = load_image(buf, phys_ram_base + 0x000c0000);
290 /* setup basic memory access */
291 cpu_register_physical_memory(0xc0000, 0x10000, 0xc0000 | IO_MEM_ROM);
292 cpu_register_physical_memory(0xf0000, 0x10000, 0xf0000 | IO_MEM_ROM);
297 uint8_t bootsect[512];
299 if (bs_table[0] == NULL) {
300 fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
303 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME);
304 ret = load_image(buf, bootsect);
305 if (ret != sizeof(bootsect)) {
306 fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
311 bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
313 /* now we can load the kernel */
314 ret = load_kernel(kernel_filename,
315 phys_ram_base + KERNEL_LOAD_ADDR,
316 phys_ram_base + KERNEL_PARAMS_ADDR);
318 fprintf(stderr, "qemu: could not load kernel '%s'\n",
325 if (initrd_filename) {
326 initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR);
327 if (initrd_size < 0) {
328 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
333 if (initrd_size > 0) {
334 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, INITRD_LOAD_ADDR);
335 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
337 pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
339 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F);
340 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
341 KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR);
343 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01);
346 /* init basic PC hardware */
347 register_ioport_write(0x80, 1, ioport80_write, 1);
349 vga_initialize(ds, phys_ram_base + ram_size, ram_size,
353 cmos_init(ram_size, boot_device);
354 register_ioport_read(0x61, 1, speaker_ioport_read, 1);
355 register_ioport_write(0x61, 1, speaker_ioport_write, 1);
359 serial_init(0x3f8, 4);
360 ne2000_init(0x300, 9);
367 fdctrl_register((unsigned char **)fd_filename, snapshot, boot_device);