]>
Commit | Line | Data |
---|---|---|
80cabfad FB |
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 | #include <stdlib.h> | |
25 | #include <stdio.h> | |
26 | #include <stdarg.h> | |
27 | #include <string.h> | |
28 | #include <getopt.h> | |
29 | #include <inttypes.h> | |
30 | #include <unistd.h> | |
31 | #include <sys/mman.h> | |
32 | #include <fcntl.h> | |
33 | #include <signal.h> | |
34 | #include <time.h> | |
35 | #include <sys/time.h> | |
36 | #include <malloc.h> | |
37 | #include <termios.h> | |
38 | #include <sys/poll.h> | |
39 | #include <errno.h> | |
40 | #include <sys/wait.h> | |
41 | #include <netinet/in.h> | |
42 | ||
43 | #include "cpu.h" | |
44 | #include "vl.h" | |
45 | ||
46 | #define BIOS_FILENAME "bios.bin" | |
47 | #define VGABIOS_FILENAME "vgabios.bin" | |
48 | #define LINUX_BOOT_FILENAME "linux_boot.bin" | |
49 | ||
50 | #define KERNEL_LOAD_ADDR 0x00100000 | |
51 | #define INITRD_LOAD_ADDR 0x00400000 | |
52 | #define KERNEL_PARAMS_ADDR 0x00090000 | |
53 | #define KERNEL_CMDLINE_ADDR 0x00099000 | |
54 | ||
55 | int speaker_data_on; | |
56 | int dummy_refresh_clock; | |
57 | ||
58 | static void ioport80_write(CPUState *env, uint32_t addr, uint32_t data) | |
59 | { | |
60 | } | |
61 | ||
62 | #define REG_EQUIPMENT_BYTE 0x14 | |
63 | ||
64 | static void cmos_init(int ram_size, int boot_device) | |
65 | { | |
66 | RTCState *s = &rtc_state; | |
67 | int val; | |
68 | ||
69 | /* various important CMOS locations needed by PC/Bochs bios */ | |
70 | ||
71 | s->cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */ | |
72 | s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x04; /* PS/2 mouse installed */ | |
73 | ||
74 | /* memory size */ | |
75 | val = (ram_size / 1024) - 1024; | |
76 | if (val > 65535) | |
77 | val = 65535; | |
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; | |
82 | ||
83 | val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536); | |
84 | if (val > 65535) | |
85 | val = 65535; | |
86 | s->cmos_data[0x34] = val; | |
87 | s->cmos_data[0x35] = val >> 8; | |
88 | ||
89 | switch(boot_device) { | |
90 | case 'a': | |
91 | case 'b': | |
92 | s->cmos_data[0x3d] = 0x01; /* floppy boot */ | |
93 | break; | |
94 | default: | |
95 | case 'c': | |
96 | s->cmos_data[0x3d] = 0x02; /* hard drive boot */ | |
97 | break; | |
98 | case 'd': | |
99 | s->cmos_data[0x3d] = 0x03; /* CD-ROM boot */ | |
100 | break; | |
101 | } | |
102 | } | |
103 | ||
104 | void cmos_register_fd (uint8_t fd0, uint8_t fd1) | |
105 | { | |
106 | RTCState *s = &rtc_state; | |
107 | int nb = 0; | |
108 | ||
109 | s->cmos_data[0x10] = 0; | |
110 | switch (fd0) { | |
111 | case 0: | |
112 | /* 1.44 Mb 3"5 drive */ | |
113 | s->cmos_data[0x10] |= 0x40; | |
114 | break; | |
115 | case 1: | |
116 | /* 2.88 Mb 3"5 drive */ | |
117 | s->cmos_data[0x10] |= 0x60; | |
118 | break; | |
119 | case 2: | |
120 | /* 1.2 Mb 5"5 drive */ | |
121 | s->cmos_data[0x10] |= 0x20; | |
122 | break; | |
123 | } | |
124 | switch (fd1) { | |
125 | case 0: | |
126 | /* 1.44 Mb 3"5 drive */ | |
127 | s->cmos_data[0x10] |= 0x04; | |
128 | break; | |
129 | case 1: | |
130 | /* 2.88 Mb 3"5 drive */ | |
131 | s->cmos_data[0x10] |= 0x06; | |
132 | break; | |
133 | case 2: | |
134 | /* 1.2 Mb 5"5 drive */ | |
135 | s->cmos_data[0x10] |= 0x02; | |
136 | break; | |
137 | } | |
138 | if (fd0 < 3) | |
139 | nb++; | |
140 | if (fd1 < 3) | |
141 | nb++; | |
142 | switch (nb) { | |
143 | case 0: | |
144 | break; | |
145 | case 1: | |
146 | s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x01; /* 1 drive, ready for boot */ | |
147 | break; | |
148 | case 2: | |
149 | s->cmos_data[REG_EQUIPMENT_BYTE] |= 0x41; /* 2 drives, ready for boot */ | |
150 | break; | |
151 | } | |
152 | } | |
153 | ||
154 | void speaker_ioport_write(CPUState *env, uint32_t addr, uint32_t val) | |
155 | { | |
156 | speaker_data_on = (val >> 1) & 1; | |
157 | pit_set_gate(&pit_channels[2], val & 1); | |
158 | } | |
159 | ||
160 | uint32_t speaker_ioport_read(CPUState *env, uint32_t addr) | |
161 | { | |
162 | int out; | |
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); | |
167 | } | |
168 | ||
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 | |
174 | ||
175 | static void fdctrl_register (unsigned char **disknames, int ro, | |
176 | char boot_device) | |
177 | { | |
178 | int i; | |
179 | ||
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); | |
184 | } | |
185 | } | |
186 | ||
187 | /***********************************************************/ | |
188 | /* Bochs BIOS debug ports */ | |
189 | ||
190 | void bochs_bios_write(CPUX86State *env, uint32_t addr, uint32_t val) | |
191 | { | |
192 | switch(addr) { | |
193 | /* Bochs BIOS messages */ | |
194 | case 0x400: | |
195 | case 0x401: | |
196 | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val); | |
197 | exit(1); | |
198 | case 0x402: | |
199 | case 0x403: | |
200 | #ifdef DEBUG_BIOS | |
201 | fprintf(stderr, "%c", val); | |
202 | #endif | |
203 | break; | |
204 | ||
205 | /* LGPL'ed VGA BIOS messages */ | |
206 | case 0x501: | |
207 | case 0x502: | |
208 | fprintf(stderr, "VGA BIOS panic, line %d\n", val); | |
209 | exit(1); | |
210 | case 0x500: | |
211 | case 0x503: | |
212 | #ifdef DEBUG_BIOS | |
213 | fprintf(stderr, "%c", val); | |
214 | #endif | |
215 | break; | |
216 | } | |
217 | } | |
218 | ||
219 | void bochs_bios_init(void) | |
220 | { | |
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); | |
225 | ||
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); | |
230 | } | |
231 | ||
232 | ||
233 | int load_kernel(const char *filename, uint8_t *addr, | |
234 | uint8_t *real_addr) | |
235 | { | |
236 | int fd, size; | |
237 | int setup_sects; | |
238 | ||
239 | fd = open(filename, O_RDONLY); | |
240 | if (fd < 0) | |
241 | return -1; | |
242 | ||
243 | /* load 16 bit code */ | |
244 | if (read(fd, real_addr, 512) != 512) | |
245 | goto fail; | |
246 | setup_sects = real_addr[0x1F1]; | |
247 | if (!setup_sects) | |
248 | setup_sects = 4; | |
249 | if (read(fd, real_addr + 512, setup_sects * 512) != | |
250 | setup_sects * 512) | |
251 | goto fail; | |
252 | ||
253 | /* load 32 bit code */ | |
254 | size = read(fd, addr, 16 * 1024 * 1024); | |
255 | if (size < 0) | |
256 | goto fail; | |
257 | close(fd); | |
258 | return size; | |
259 | fail: | |
260 | close(fd); | |
261 | return -1; | |
262 | } | |
263 | ||
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) | |
269 | { | |
270 | char buf[1024]; | |
271 | int ret, linux_boot, initrd_size; | |
272 | ||
273 | linux_boot = (kernel_filename != NULL); | |
274 | ||
275 | /* allocate RAM */ | |
276 | cpu_register_physical_memory(0, ram_size, 0); | |
277 | ||
278 | /* BIOS load */ | |
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); | |
283 | exit(1); | |
284 | } | |
285 | ||
286 | /* VGA BIOS load */ | |
287 | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME); | |
288 | ret = load_image(buf, phys_ram_base + 0x000c0000); | |
289 | ||
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); | |
293 | ||
294 | bochs_bios_init(); | |
295 | ||
296 | if (linux_boot) { | |
297 | uint8_t bootsect[512]; | |
298 | ||
299 | if (bs_table[0] == NULL) { | |
300 | fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n"); | |
301 | exit(1); | |
302 | } | |
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", | |
307 | buf); | |
308 | exit(1); | |
309 | } | |
310 | ||
311 | bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect)); | |
312 | ||
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); | |
317 | if (ret < 0) { | |
318 | fprintf(stderr, "qemu: could not load kernel '%s'\n", | |
319 | kernel_filename); | |
320 | exit(1); | |
321 | } | |
322 | ||
323 | /* load initrd */ | |
324 | initrd_size = 0; | |
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", | |
329 | initrd_filename); | |
330 | exit(1); | |
331 | } | |
332 | } | |
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); | |
336 | } | |
337 | pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096, | |
338 | kernel_cmdline); | |
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); | |
342 | /* loader type */ | |
343 | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01); | |
344 | } | |
345 | ||
346 | /* init basic PC hardware */ | |
347 | register_ioport_write(0x80, 1, ioport80_write, 1); | |
348 | ||
349 | vga_initialize(ds, phys_ram_base + ram_size, ram_size, | |
350 | vga_ram_size); | |
351 | ||
352 | rtc_init(0x70, 8); | |
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); | |
356 | ||
357 | pic_init(); | |
358 | pit_init(); | |
359 | serial_init(0x3f8, 4); | |
360 | ne2000_init(0x300, 9); | |
361 | ide_init(); | |
362 | kbd_init(); | |
363 | AUD_init(); | |
364 | DMA_init(); | |
365 | SB16_init(); | |
366 | ||
367 | fdctrl_register((unsigned char **)fd_filename, snapshot, boot_device); | |
368 | } |