root / hw / pc.c @ 89344d5a
History | View | Annotate | Download (17.5 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU PC System Emulator
|
3 | 80cabfad | bellard | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 80cabfad | bellard | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 80cabfad | bellard | #include "vl.h" |
25 | 80cabfad | bellard | |
26 | b41a2cd1 | bellard | /* output Bochs bios info messages */
|
27 | b41a2cd1 | bellard | //#define DEBUG_BIOS
|
28 | b41a2cd1 | bellard | |
29 | 80cabfad | bellard | #define BIOS_FILENAME "bios.bin" |
30 | 80cabfad | bellard | #define VGABIOS_FILENAME "vgabios.bin" |
31 | de9258a8 | bellard | #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin" |
32 | 80cabfad | bellard | #define LINUX_BOOT_FILENAME "linux_boot.bin" |
33 | 80cabfad | bellard | |
34 | 80cabfad | bellard | #define KERNEL_LOAD_ADDR 0x00100000 |
35 | 80cabfad | bellard | #define INITRD_LOAD_ADDR 0x00400000 |
36 | 80cabfad | bellard | #define KERNEL_PARAMS_ADDR 0x00090000 |
37 | 80cabfad | bellard | #define KERNEL_CMDLINE_ADDR 0x00099000 |
38 | 80cabfad | bellard | |
39 | 80cabfad | bellard | int speaker_data_on;
|
40 | 80cabfad | bellard | int dummy_refresh_clock;
|
41 | baca51fa | bellard | static fdctrl_t *floppy_controller;
|
42 | b0a21b53 | bellard | static RTCState *rtc_state;
|
43 | ec844b96 | bellard | static PITState *pit;
|
44 | 80cabfad | bellard | |
45 | b41a2cd1 | bellard | static void ioport80_write(void *opaque, uint32_t addr, uint32_t data) |
46 | 80cabfad | bellard | { |
47 | 80cabfad | bellard | } |
48 | 80cabfad | bellard | |
49 | f929aad6 | bellard | /* MSDOS compatibility mode FPU exception support */
|
50 | f929aad6 | bellard | /* XXX: add IGNNE support */
|
51 | f929aad6 | bellard | void cpu_set_ferr(CPUX86State *s)
|
52 | f929aad6 | bellard | { |
53 | f929aad6 | bellard | pic_set_irq(13, 1); |
54 | f929aad6 | bellard | } |
55 | f929aad6 | bellard | |
56 | f929aad6 | bellard | static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data) |
57 | f929aad6 | bellard | { |
58 | f929aad6 | bellard | pic_set_irq(13, 0); |
59 | f929aad6 | bellard | } |
60 | f929aad6 | bellard | |
61 | 28ab0e2e | bellard | /* TSC handling */
|
62 | 28ab0e2e | bellard | |
63 | 28ab0e2e | bellard | uint64_t cpu_get_tsc(CPUX86State *env) |
64 | 28ab0e2e | bellard | { |
65 | 28ab0e2e | bellard | return qemu_get_clock(vm_clock);
|
66 | 28ab0e2e | bellard | } |
67 | 28ab0e2e | bellard | |
68 | b0a21b53 | bellard | /* PC cmos mappings */
|
69 | b0a21b53 | bellard | |
70 | 80cabfad | bellard | #define REG_EQUIPMENT_BYTE 0x14 |
71 | b0a21b53 | bellard | #define REG_IBM_CENTURY_BYTE 0x32 |
72 | b0a21b53 | bellard | #define REG_IBM_PS2_CENTURY_BYTE 0x37 |
73 | b0a21b53 | bellard | |
74 | b0a21b53 | bellard | |
75 | b0a21b53 | bellard | static inline int to_bcd(RTCState *s, int a) |
76 | b0a21b53 | bellard | { |
77 | b0a21b53 | bellard | return ((a / 10) << 4) | (a % 10); |
78 | b0a21b53 | bellard | } |
79 | 80cabfad | bellard | |
80 | 777428f2 | bellard | static int cmos_get_fd_drive_type(int fd0) |
81 | 777428f2 | bellard | { |
82 | 777428f2 | bellard | int val;
|
83 | 777428f2 | bellard | |
84 | 777428f2 | bellard | switch (fd0) {
|
85 | 777428f2 | bellard | case 0: |
86 | 777428f2 | bellard | /* 1.44 Mb 3"5 drive */
|
87 | 777428f2 | bellard | val = 4;
|
88 | 777428f2 | bellard | break;
|
89 | 777428f2 | bellard | case 1: |
90 | 777428f2 | bellard | /* 2.88 Mb 3"5 drive */
|
91 | 777428f2 | bellard | val = 5;
|
92 | 777428f2 | bellard | break;
|
93 | 777428f2 | bellard | case 2: |
94 | 777428f2 | bellard | /* 1.2 Mb 5"5 drive */
|
95 | 777428f2 | bellard | val = 2;
|
96 | 777428f2 | bellard | break;
|
97 | 777428f2 | bellard | default:
|
98 | 777428f2 | bellard | val = 0;
|
99 | 777428f2 | bellard | break;
|
100 | 777428f2 | bellard | } |
101 | 777428f2 | bellard | return val;
|
102 | 777428f2 | bellard | } |
103 | 777428f2 | bellard | |
104 | ba6c2377 | bellard | static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) |
105 | ba6c2377 | bellard | { |
106 | ba6c2377 | bellard | RTCState *s = rtc_state; |
107 | ba6c2377 | bellard | int cylinders, heads, sectors;
|
108 | ba6c2377 | bellard | bdrv_get_geometry_hint(hd, &cylinders, &heads, §ors); |
109 | ba6c2377 | bellard | rtc_set_memory(s, type_ofs, 47);
|
110 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs, cylinders); |
111 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 1, cylinders >> 8); |
112 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 2, heads);
|
113 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 3, 0xff); |
114 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 4, 0xff); |
115 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3)); |
116 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 6, cylinders);
|
117 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 7, cylinders >> 8); |
118 | ba6c2377 | bellard | rtc_set_memory(s, info_ofs + 8, sectors);
|
119 | ba6c2377 | bellard | } |
120 | ba6c2377 | bellard | |
121 | ba6c2377 | bellard | /* hd_table must contain 4 block drivers */
|
122 | ba6c2377 | bellard | static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table) |
123 | 80cabfad | bellard | { |
124 | b0a21b53 | bellard | RTCState *s = rtc_state; |
125 | 80cabfad | bellard | int val;
|
126 | b41a2cd1 | bellard | int fd0, fd1, nb;
|
127 | b0a21b53 | bellard | time_t ti; |
128 | b0a21b53 | bellard | struct tm *tm;
|
129 | ba6c2377 | bellard | int i;
|
130 | b0a21b53 | bellard | |
131 | b0a21b53 | bellard | /* set the CMOS date */
|
132 | b0a21b53 | bellard | time(&ti); |
133 | ee22c2f7 | bellard | if (rtc_utc)
|
134 | ee22c2f7 | bellard | tm = gmtime(&ti); |
135 | ee22c2f7 | bellard | else
|
136 | ee22c2f7 | bellard | tm = localtime(&ti); |
137 | b0a21b53 | bellard | rtc_set_date(s, tm); |
138 | b0a21b53 | bellard | |
139 | b0a21b53 | bellard | val = to_bcd(s, (tm->tm_year / 100) + 19); |
140 | b0a21b53 | bellard | rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val); |
141 | b0a21b53 | bellard | rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val); |
142 | 80cabfad | bellard | |
143 | b0a21b53 | bellard | /* various important CMOS locations needed by PC/Bochs bios */
|
144 | 80cabfad | bellard | |
145 | 80cabfad | bellard | /* memory size */
|
146 | 333190eb | bellard | val = 640; /* base memory in K */ |
147 | 333190eb | bellard | rtc_set_memory(s, 0x15, val);
|
148 | 333190eb | bellard | rtc_set_memory(s, 0x16, val >> 8); |
149 | 333190eb | bellard | |
150 | 80cabfad | bellard | val = (ram_size / 1024) - 1024; |
151 | 80cabfad | bellard | if (val > 65535) |
152 | 80cabfad | bellard | val = 65535;
|
153 | b0a21b53 | bellard | rtc_set_memory(s, 0x17, val);
|
154 | b0a21b53 | bellard | rtc_set_memory(s, 0x18, val >> 8); |
155 | b0a21b53 | bellard | rtc_set_memory(s, 0x30, val);
|
156 | b0a21b53 | bellard | rtc_set_memory(s, 0x31, val >> 8); |
157 | 80cabfad | bellard | |
158 | 9da98861 | bellard | if (ram_size > (16 * 1024 * 1024)) |
159 | 9da98861 | bellard | val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536); |
160 | 9da98861 | bellard | else
|
161 | 9da98861 | bellard | val = 0;
|
162 | 80cabfad | bellard | if (val > 65535) |
163 | 80cabfad | bellard | val = 65535;
|
164 | b0a21b53 | bellard | rtc_set_memory(s, 0x34, val);
|
165 | b0a21b53 | bellard | rtc_set_memory(s, 0x35, val >> 8); |
166 | 80cabfad | bellard | |
167 | 80cabfad | bellard | switch(boot_device) {
|
168 | 80cabfad | bellard | case 'a': |
169 | 80cabfad | bellard | case 'b': |
170 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */ |
171 | 80cabfad | bellard | break;
|
172 | 80cabfad | bellard | default:
|
173 | 80cabfad | bellard | case 'c': |
174 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */ |
175 | 80cabfad | bellard | break;
|
176 | 80cabfad | bellard | case 'd': |
177 | b0a21b53 | bellard | rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */ |
178 | 80cabfad | bellard | break;
|
179 | 80cabfad | bellard | } |
180 | 80cabfad | bellard | |
181 | b41a2cd1 | bellard | /* floppy type */
|
182 | b41a2cd1 | bellard | |
183 | baca51fa | bellard | fd0 = fdctrl_get_drive_type(floppy_controller, 0);
|
184 | baca51fa | bellard | fd1 = fdctrl_get_drive_type(floppy_controller, 1);
|
185 | 80cabfad | bellard | |
186 | 777428f2 | bellard | val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
|
187 | b0a21b53 | bellard | rtc_set_memory(s, 0x10, val);
|
188 | b0a21b53 | bellard | |
189 | b0a21b53 | bellard | val = 0;
|
190 | b41a2cd1 | bellard | nb = 0;
|
191 | 80cabfad | bellard | if (fd0 < 3) |
192 | 80cabfad | bellard | nb++; |
193 | 80cabfad | bellard | if (fd1 < 3) |
194 | 80cabfad | bellard | nb++; |
195 | 80cabfad | bellard | switch (nb) {
|
196 | 80cabfad | bellard | case 0: |
197 | 80cabfad | bellard | break;
|
198 | 80cabfad | bellard | case 1: |
199 | b0a21b53 | bellard | val |= 0x01; /* 1 drive, ready for boot */ |
200 | 80cabfad | bellard | break;
|
201 | 80cabfad | bellard | case 2: |
202 | b0a21b53 | bellard | val |= 0x41; /* 2 drives, ready for boot */ |
203 | 80cabfad | bellard | break;
|
204 | 80cabfad | bellard | } |
205 | b0a21b53 | bellard | val |= 0x02; /* FPU is there */ |
206 | b0a21b53 | bellard | val |= 0x04; /* PS/2 mouse installed */ |
207 | b0a21b53 | bellard | rtc_set_memory(s, REG_EQUIPMENT_BYTE, val); |
208 | b0a21b53 | bellard | |
209 | ba6c2377 | bellard | /* hard drives */
|
210 | ba6c2377 | bellard | |
211 | ba6c2377 | bellard | rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0)); |
212 | ba6c2377 | bellard | if (hd_table[0]) |
213 | ba6c2377 | bellard | cmos_init_hd(0x19, 0x1b, hd_table[0]); |
214 | ba6c2377 | bellard | if (hd_table[1]) |
215 | ba6c2377 | bellard | cmos_init_hd(0x1a, 0x24, hd_table[1]); |
216 | ba6c2377 | bellard | |
217 | ba6c2377 | bellard | val = 0;
|
218 | 40b6ecc6 | bellard | for (i = 0; i < 4; i++) { |
219 | ba6c2377 | bellard | if (hd_table[i]) {
|
220 | 46d4767d | bellard | int cylinders, heads, sectors, translation;
|
221 | 46d4767d | bellard | /* NOTE: bdrv_get_geometry_hint() returns the physical
|
222 | 46d4767d | bellard | geometry. It is always such that: 1 <= sects <= 63, 1
|
223 | 46d4767d | bellard | <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
|
224 | 46d4767d | bellard | geometry can be different if a translation is done. */
|
225 | 46d4767d | bellard | translation = bdrv_get_translation_hint(hd_table[i]); |
226 | 46d4767d | bellard | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
227 | 46d4767d | bellard | bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, §ors); |
228 | 46d4767d | bellard | if (cylinders <= 1024 && heads <= 16 && sectors <= 63) { |
229 | 46d4767d | bellard | /* No translation. */
|
230 | 46d4767d | bellard | translation = 0;
|
231 | 46d4767d | bellard | } else {
|
232 | 46d4767d | bellard | /* LBA translation. */
|
233 | 46d4767d | bellard | translation = 1;
|
234 | 46d4767d | bellard | } |
235 | 40b6ecc6 | bellard | } else {
|
236 | 46d4767d | bellard | translation--; |
237 | ba6c2377 | bellard | } |
238 | ba6c2377 | bellard | val |= translation << (i * 2);
|
239 | ba6c2377 | bellard | } |
240 | 40b6ecc6 | bellard | } |
241 | ba6c2377 | bellard | rtc_set_memory(s, 0x39, val);
|
242 | ba6c2377 | bellard | |
243 | ba6c2377 | bellard | /* Disable check of 0x55AA signature on the last two bytes of
|
244 | ba6c2377 | bellard | first sector of disk. XXX: make it the default ? */
|
245 | ba6c2377 | bellard | // rtc_set_memory(s, 0x38, 1);
|
246 | 80cabfad | bellard | } |
247 | 80cabfad | bellard | |
248 | b41a2cd1 | bellard | static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
249 | 80cabfad | bellard | { |
250 | 80cabfad | bellard | speaker_data_on = (val >> 1) & 1; |
251 | ec844b96 | bellard | pit_set_gate(pit, 2, val & 1); |
252 | 80cabfad | bellard | } |
253 | 80cabfad | bellard | |
254 | b41a2cd1 | bellard | static uint32_t speaker_ioport_read(void *opaque, uint32_t addr) |
255 | 80cabfad | bellard | { |
256 | 80cabfad | bellard | int out;
|
257 | ec844b96 | bellard | out = pit_get_out(pit, 2, qemu_get_clock(vm_clock));
|
258 | 80cabfad | bellard | dummy_refresh_clock ^= 1;
|
259 | ec844b96 | bellard | return (speaker_data_on << 1) | pit_get_gate(pit, 2) | (out << 5) | |
260 | 80cabfad | bellard | (dummy_refresh_clock << 4);
|
261 | 80cabfad | bellard | } |
262 | 80cabfad | bellard | |
263 | e1a23744 | bellard | static void ioport92_write(void *opaque, uint32_t addr, uint32_t val) |
264 | e1a23744 | bellard | { |
265 | e1a23744 | bellard | cpu_x86_set_a20(cpu_single_env, (val >> 1) & 1); |
266 | e1a23744 | bellard | /* XXX: bit 0 is fast reset */
|
267 | e1a23744 | bellard | } |
268 | e1a23744 | bellard | |
269 | e1a23744 | bellard | static uint32_t ioport92_read(void *opaque, uint32_t addr) |
270 | e1a23744 | bellard | { |
271 | e1a23744 | bellard | return ((cpu_single_env->a20_mask >> 20) & 1) << 1; |
272 | e1a23744 | bellard | } |
273 | e1a23744 | bellard | |
274 | 80cabfad | bellard | /***********************************************************/
|
275 | 80cabfad | bellard | /* Bochs BIOS debug ports */
|
276 | 80cabfad | bellard | |
277 | b41a2cd1 | bellard | void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val) |
278 | 80cabfad | bellard | { |
279 | a2f659ee | bellard | static const char shutdown_str[8] = "Shutdown"; |
280 | a2f659ee | bellard | static int shutdown_index = 0; |
281 | a2f659ee | bellard | |
282 | 80cabfad | bellard | switch(addr) {
|
283 | 80cabfad | bellard | /* Bochs BIOS messages */
|
284 | 80cabfad | bellard | case 0x400: |
285 | 80cabfad | bellard | case 0x401: |
286 | 80cabfad | bellard | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
|
287 | 80cabfad | bellard | exit(1);
|
288 | 80cabfad | bellard | case 0x402: |
289 | 80cabfad | bellard | case 0x403: |
290 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
291 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
292 | 80cabfad | bellard | #endif
|
293 | 80cabfad | bellard | break;
|
294 | a2f659ee | bellard | case 0x8900: |
295 | a2f659ee | bellard | /* same as Bochs power off */
|
296 | a2f659ee | bellard | if (val == shutdown_str[shutdown_index]) {
|
297 | a2f659ee | bellard | shutdown_index++; |
298 | a2f659ee | bellard | if (shutdown_index == 8) { |
299 | a2f659ee | bellard | shutdown_index = 0;
|
300 | a2f659ee | bellard | qemu_system_shutdown_request(); |
301 | a2f659ee | bellard | } |
302 | a2f659ee | bellard | } else {
|
303 | a2f659ee | bellard | shutdown_index = 0;
|
304 | a2f659ee | bellard | } |
305 | a2f659ee | bellard | break;
|
306 | 80cabfad | bellard | |
307 | 80cabfad | bellard | /* LGPL'ed VGA BIOS messages */
|
308 | 80cabfad | bellard | case 0x501: |
309 | 80cabfad | bellard | case 0x502: |
310 | 80cabfad | bellard | fprintf(stderr, "VGA BIOS panic, line %d\n", val);
|
311 | 80cabfad | bellard | exit(1);
|
312 | 80cabfad | bellard | case 0x500: |
313 | 80cabfad | bellard | case 0x503: |
314 | 80cabfad | bellard | #ifdef DEBUG_BIOS
|
315 | 80cabfad | bellard | fprintf(stderr, "%c", val);
|
316 | 80cabfad | bellard | #endif
|
317 | 80cabfad | bellard | break;
|
318 | 80cabfad | bellard | } |
319 | 80cabfad | bellard | } |
320 | 80cabfad | bellard | |
321 | 80cabfad | bellard | void bochs_bios_init(void) |
322 | 80cabfad | bellard | { |
323 | b41a2cd1 | bellard | register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL); |
324 | b41a2cd1 | bellard | register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL); |
325 | b41a2cd1 | bellard | register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL); |
326 | b41a2cd1 | bellard | register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL); |
327 | a2f659ee | bellard | register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL); |
328 | b41a2cd1 | bellard | |
329 | b41a2cd1 | bellard | register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL); |
330 | b41a2cd1 | bellard | register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL); |
331 | b41a2cd1 | bellard | register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL); |
332 | b41a2cd1 | bellard | register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL); |
333 | 80cabfad | bellard | } |
334 | 80cabfad | bellard | |
335 | 80cabfad | bellard | |
336 | 80cabfad | bellard | int load_kernel(const char *filename, uint8_t *addr, |
337 | 80cabfad | bellard | uint8_t *real_addr) |
338 | 80cabfad | bellard | { |
339 | 80cabfad | bellard | int fd, size;
|
340 | 80cabfad | bellard | int setup_sects;
|
341 | 80cabfad | bellard | |
342 | 096b7ea4 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
343 | 80cabfad | bellard | if (fd < 0) |
344 | 80cabfad | bellard | return -1; |
345 | 80cabfad | bellard | |
346 | 80cabfad | bellard | /* load 16 bit code */
|
347 | 80cabfad | bellard | if (read(fd, real_addr, 512) != 512) |
348 | 80cabfad | bellard | goto fail;
|
349 | 80cabfad | bellard | setup_sects = real_addr[0x1F1];
|
350 | 80cabfad | bellard | if (!setup_sects)
|
351 | 80cabfad | bellard | setup_sects = 4;
|
352 | 80cabfad | bellard | if (read(fd, real_addr + 512, setup_sects * 512) != |
353 | 80cabfad | bellard | setup_sects * 512)
|
354 | 80cabfad | bellard | goto fail;
|
355 | 80cabfad | bellard | |
356 | 80cabfad | bellard | /* load 32 bit code */
|
357 | 80cabfad | bellard | size = read(fd, addr, 16 * 1024 * 1024); |
358 | 80cabfad | bellard | if (size < 0) |
359 | 80cabfad | bellard | goto fail;
|
360 | 80cabfad | bellard | close(fd); |
361 | 80cabfad | bellard | return size;
|
362 | 80cabfad | bellard | fail:
|
363 | 80cabfad | bellard | close(fd); |
364 | 80cabfad | bellard | return -1; |
365 | 80cabfad | bellard | } |
366 | 80cabfad | bellard | |
367 | b41a2cd1 | bellard | static const int ide_iobase[2] = { 0x1f0, 0x170 }; |
368 | b41a2cd1 | bellard | static const int ide_iobase2[2] = { 0x3f6, 0x376 }; |
369 | b41a2cd1 | bellard | static const int ide_irq[2] = { 14, 15 }; |
370 | b41a2cd1 | bellard | |
371 | b41a2cd1 | bellard | #define NE2000_NB_MAX 6 |
372 | b41a2cd1 | bellard | |
373 | 8d11df9e | bellard | static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 }; |
374 | b41a2cd1 | bellard | static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 }; |
375 | b41a2cd1 | bellard | |
376 | 8d11df9e | bellard | static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
377 | 8d11df9e | bellard | static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 }; |
378 | 8d11df9e | bellard | |
379 | 6508fe59 | bellard | static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc }; |
380 | 6508fe59 | bellard | static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 }; |
381 | 6508fe59 | bellard | |
382 | 80cabfad | bellard | /* PC hardware initialisation */
|
383 | 80cabfad | bellard | void pc_init(int ram_size, int vga_ram_size, int boot_device, |
384 | 80cabfad | bellard | DisplayState *ds, const char **fd_filename, int snapshot, |
385 | 80cabfad | bellard | const char *kernel_filename, const char *kernel_cmdline, |
386 | 80cabfad | bellard | const char *initrd_filename) |
387 | 80cabfad | bellard | { |
388 | 80cabfad | bellard | char buf[1024]; |
389 | 82c643ff | bellard | int ret, linux_boot, initrd_size, i, nb_nics1;
|
390 | 7587cf44 | bellard | unsigned long bios_offset, vga_bios_offset; |
391 | 7587cf44 | bellard | int bios_size, isa_bios_size;
|
392 | 46e50e9d | bellard | PCIBus *pci_bus; |
393 | 46e50e9d | bellard | |
394 | 80cabfad | bellard | linux_boot = (kernel_filename != NULL);
|
395 | 80cabfad | bellard | |
396 | 80cabfad | bellard | /* allocate RAM */
|
397 | 80cabfad | bellard | cpu_register_physical_memory(0, ram_size, 0); |
398 | 80cabfad | bellard | |
399 | 80cabfad | bellard | /* BIOS load */
|
400 | 7587cf44 | bellard | bios_offset = ram_size + vga_ram_size; |
401 | 7587cf44 | bellard | vga_bios_offset = bios_offset + 256 * 1024; |
402 | 7587cf44 | bellard | |
403 | 80cabfad | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME); |
404 | 7587cf44 | bellard | bios_size = get_image_size(buf); |
405 | 7587cf44 | bellard | if (bios_size <= 0 || |
406 | 7587cf44 | bellard | (bios_size % 65536) != 0 || |
407 | 7587cf44 | bellard | bios_size > (256 * 1024)) { |
408 | 7587cf44 | bellard | goto bios_error;
|
409 | 7587cf44 | bellard | } |
410 | 7587cf44 | bellard | ret = load_image(buf, phys_ram_base + bios_offset); |
411 | 7587cf44 | bellard | if (ret != bios_size) {
|
412 | 7587cf44 | bellard | bios_error:
|
413 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load PC bios '%s'\n", buf);
|
414 | 80cabfad | bellard | exit(1);
|
415 | 80cabfad | bellard | } |
416 | 7587cf44 | bellard | |
417 | 80cabfad | bellard | /* VGA BIOS load */
|
418 | de9258a8 | bellard | if (cirrus_vga_enabled) {
|
419 | de9258a8 | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME); |
420 | de9258a8 | bellard | } else {
|
421 | de9258a8 | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME); |
422 | de9258a8 | bellard | } |
423 | 7587cf44 | bellard | ret = load_image(buf, phys_ram_base + vga_bios_offset); |
424 | 80cabfad | bellard | |
425 | 80cabfad | bellard | /* setup basic memory access */
|
426 | 7587cf44 | bellard | cpu_register_physical_memory(0xc0000, 0x10000, |
427 | 7587cf44 | bellard | vga_bios_offset | IO_MEM_ROM); |
428 | 7587cf44 | bellard | |
429 | 7587cf44 | bellard | /* map the last 128KB of the BIOS in ISA space */
|
430 | 7587cf44 | bellard | isa_bios_size = bios_size; |
431 | 7587cf44 | bellard | if (isa_bios_size > (128 * 1024)) |
432 | 7587cf44 | bellard | isa_bios_size = 128 * 1024; |
433 | 7587cf44 | bellard | cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size, |
434 | 7587cf44 | bellard | IO_MEM_UNASSIGNED); |
435 | 7587cf44 | bellard | cpu_register_physical_memory(0x100000 - isa_bios_size,
|
436 | 7587cf44 | bellard | isa_bios_size, |
437 | 7587cf44 | bellard | (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM); |
438 | 7587cf44 | bellard | /* map all the bios at the top of memory */
|
439 | 7587cf44 | bellard | cpu_register_physical_memory((uint32_t)(-bios_size), |
440 | 7587cf44 | bellard | bios_size, bios_offset | IO_MEM_ROM); |
441 | 80cabfad | bellard | |
442 | 80cabfad | bellard | bochs_bios_init(); |
443 | 80cabfad | bellard | |
444 | 80cabfad | bellard | if (linux_boot) {
|
445 | 80cabfad | bellard | uint8_t bootsect[512];
|
446 | 41b9be47 | bellard | uint8_t old_bootsect[512];
|
447 | 80cabfad | bellard | |
448 | 80cabfad | bellard | if (bs_table[0] == NULL) { |
449 | 80cabfad | bellard | fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
|
450 | 80cabfad | bellard | exit(1);
|
451 | 80cabfad | bellard | } |
452 | 80cabfad | bellard | snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME); |
453 | 80cabfad | bellard | ret = load_image(buf, bootsect); |
454 | 80cabfad | bellard | if (ret != sizeof(bootsect)) { |
455 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
|
456 | 80cabfad | bellard | buf); |
457 | 80cabfad | bellard | exit(1);
|
458 | 80cabfad | bellard | } |
459 | 80cabfad | bellard | |
460 | 41b9be47 | bellard | if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) { |
461 | 41b9be47 | bellard | /* copy the MSDOS partition table */
|
462 | 41b9be47 | bellard | memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40); |
463 | 41b9be47 | bellard | } |
464 | 41b9be47 | bellard | |
465 | 80cabfad | bellard | bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect)); |
466 | 80cabfad | bellard | |
467 | 80cabfad | bellard | /* now we can load the kernel */
|
468 | 80cabfad | bellard | ret = load_kernel(kernel_filename, |
469 | 80cabfad | bellard | phys_ram_base + KERNEL_LOAD_ADDR, |
470 | 80cabfad | bellard | phys_ram_base + KERNEL_PARAMS_ADDR); |
471 | 80cabfad | bellard | if (ret < 0) { |
472 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load kernel '%s'\n",
|
473 | 80cabfad | bellard | kernel_filename); |
474 | 80cabfad | bellard | exit(1);
|
475 | 80cabfad | bellard | } |
476 | 80cabfad | bellard | |
477 | 80cabfad | bellard | /* load initrd */
|
478 | 80cabfad | bellard | initrd_size = 0;
|
479 | 80cabfad | bellard | if (initrd_filename) {
|
480 | 80cabfad | bellard | initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR); |
481 | 80cabfad | bellard | if (initrd_size < 0) { |
482 | 80cabfad | bellard | fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
|
483 | 80cabfad | bellard | initrd_filename); |
484 | 80cabfad | bellard | exit(1);
|
485 | 80cabfad | bellard | } |
486 | 80cabfad | bellard | } |
487 | 80cabfad | bellard | if (initrd_size > 0) { |
488 | 80cabfad | bellard | stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, INITRD_LOAD_ADDR);
|
489 | 80cabfad | bellard | stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
|
490 | 80cabfad | bellard | } |
491 | 80cabfad | bellard | pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
|
492 | 80cabfad | bellard | kernel_cmdline); |
493 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F); |
494 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
|
495 | 80cabfad | bellard | KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR); |
496 | 80cabfad | bellard | /* loader type */
|
497 | 80cabfad | bellard | stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01); |
498 | 80cabfad | bellard | } |
499 | 80cabfad | bellard | |
500 | 69b91039 | bellard | if (pci_enabled) {
|
501 | 46e50e9d | bellard | pci_bus = i440fx_init(); |
502 | 46e50e9d | bellard | piix3_init(pci_bus); |
503 | 46e50e9d | bellard | } else {
|
504 | 46e50e9d | bellard | pci_bus = NULL;
|
505 | 69b91039 | bellard | } |
506 | 69b91039 | bellard | |
507 | 80cabfad | bellard | /* init basic PC hardware */
|
508 | b41a2cd1 | bellard | register_ioport_write(0x80, 1, 1, ioport80_write, NULL); |
509 | 80cabfad | bellard | |
510 | f929aad6 | bellard | register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL); |
511 | f929aad6 | bellard | |
512 | 1f04275e | bellard | if (cirrus_vga_enabled) {
|
513 | 1f04275e | bellard | if (pci_enabled) {
|
514 | 46e50e9d | bellard | pci_cirrus_vga_init(pci_bus, |
515 | 46e50e9d | bellard | ds, phys_ram_base + ram_size, ram_size, |
516 | 1f04275e | bellard | vga_ram_size); |
517 | 1f04275e | bellard | } else {
|
518 | 1f04275e | bellard | isa_cirrus_vga_init(ds, phys_ram_base + ram_size, ram_size, |
519 | 1f04275e | bellard | vga_ram_size); |
520 | 1f04275e | bellard | } |
521 | 1f04275e | bellard | } else {
|
522 | 46e50e9d | bellard | vga_initialize(pci_bus, ds, phys_ram_base + ram_size, ram_size, |
523 | 46e50e9d | bellard | vga_ram_size); |
524 | 1f04275e | bellard | } |
525 | 80cabfad | bellard | |
526 | b0a21b53 | bellard | rtc_state = rtc_init(0x70, 8); |
527 | b41a2cd1 | bellard | register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL); |
528 | b41a2cd1 | bellard | register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL); |
529 | 80cabfad | bellard | |
530 | e1a23744 | bellard | register_ioport_read(0x92, 1, 1, ioport92_read, NULL); |
531 | e1a23744 | bellard | register_ioport_write(0x92, 1, 1, ioport92_write, NULL); |
532 | e1a23744 | bellard | |
533 | 75598f61 | bellard | if (pci_enabled)
|
534 | 75598f61 | bellard | apic_init(cpu_single_env); |
535 | 80cabfad | bellard | pic_init(); |
536 | ec844b96 | bellard | pit = pit_init(0x40, 0); |
537 | b41a2cd1 | bellard | |
538 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
539 | 8d11df9e | bellard | if (serial_hds[i]) {
|
540 | 8d11df9e | bellard | serial_init(serial_io[i], serial_irq[i], serial_hds[i]); |
541 | 8d11df9e | bellard | } |
542 | 8d11df9e | bellard | } |
543 | b41a2cd1 | bellard | |
544 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
545 | 6508fe59 | bellard | if (parallel_hds[i]) {
|
546 | 6508fe59 | bellard | parallel_init(parallel_io[i], parallel_irq[i], parallel_hds[i]); |
547 | 6508fe59 | bellard | } |
548 | 6508fe59 | bellard | } |
549 | 6508fe59 | bellard | |
550 | 69b91039 | bellard | if (pci_enabled) {
|
551 | 69b91039 | bellard | for(i = 0; i < nb_nics; i++) { |
552 | 46e50e9d | bellard | pci_ne2000_init(pci_bus, &nd_table[i]); |
553 | 69b91039 | bellard | } |
554 | 46e50e9d | bellard | pci_piix3_ide_init(pci_bus, bs_table); |
555 | 69b91039 | bellard | } else {
|
556 | 69b91039 | bellard | nb_nics1 = nb_nics; |
557 | 69b91039 | bellard | if (nb_nics1 > NE2000_NB_MAX)
|
558 | 69b91039 | bellard | nb_nics1 = NE2000_NB_MAX; |
559 | 69b91039 | bellard | for(i = 0; i < nb_nics1; i++) { |
560 | 69b91039 | bellard | isa_ne2000_init(ne2000_io[i], ne2000_irq[i], &nd_table[i]); |
561 | 69b91039 | bellard | } |
562 | b41a2cd1 | bellard | |
563 | 69b91039 | bellard | for(i = 0; i < 2; i++) { |
564 | 69b91039 | bellard | isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i], |
565 | 69b91039 | bellard | bs_table[2 * i], bs_table[2 * i + 1]); |
566 | 69b91039 | bellard | } |
567 | b41a2cd1 | bellard | } |
568 | 69b91039 | bellard | |
569 | 80cabfad | bellard | kbd_init(); |
570 | 7c29d0c0 | bellard | DMA_init(0);
|
571 | 67b915a5 | bellard | |
572 | aaaa7df6 | bellard | if (audio_enabled) {
|
573 | aaaa7df6 | bellard | AUD_init(); |
574 | fb065187 | bellard | #ifdef USE_SB16
|
575 | fb065187 | bellard | if (sb16_enabled)
|
576 | fb065187 | bellard | SB16_init (); |
577 | fb065187 | bellard | #endif
|
578 | fb065187 | bellard | #ifdef CONFIG_ADLIB
|
579 | fb065187 | bellard | if (adlib_enabled)
|
580 | fb065187 | bellard | Adlib_init (); |
581 | fb065187 | bellard | #endif
|
582 | fb065187 | bellard | #ifdef USE_GUS
|
583 | fb065187 | bellard | if (gus_enabled)
|
584 | fb065187 | bellard | GUS_init (); |
585 | fb065187 | bellard | #endif
|
586 | aaaa7df6 | bellard | } |
587 | 80cabfad | bellard | |
588 | baca51fa | bellard | floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table); |
589 | b41a2cd1 | bellard | |
590 | ba6c2377 | bellard | cmos_init(ram_size, boot_device, bs_table); |
591 | 69b91039 | bellard | |
592 | 69b91039 | bellard | /* must be done after all PCI devices are instanciated */
|
593 | 69b91039 | bellard | /* XXX: should be done in the Bochs BIOS */
|
594 | 69b91039 | bellard | if (pci_enabled) {
|
595 | 69b91039 | bellard | pci_bios_init(); |
596 | 69b91039 | bellard | } |
597 | 80cabfad | bellard | } |