Debian Codegraph @

harness/bios.c

raw
1 #include "harness.h"
2 #include "vm86.h"
3 #include <string.h>
4
5 typedef unsigned char BYTE;
6 typedef unsigned short WORD;
7 typedef unsigned long DWORD;
8 typedef unsigned long long QWORD;
9
10 typedef struct
11 {
12 BYTE size;
13 BYTE reserved;
14 WORD count;
15 WORD addr;
16 WORD seg;
17 QWORD block;
18 } disk_address_packet;
19
20 #define EFLAGS(v86) ((v86)->regs.eflags)
21
22 #define AX(v86) ((unsigned short)((v86)->regs.eax))
23 #define AL(v86) ((unsigned char)((v86)->regs.eax))
24 #define AH(v86) ((unsigned char)((v86)->regs.eax >> 8))
25 #define SET_AL(v86, new_al) \
26 do { \
27 (v86)->regs.eax &= ~0xff; \
28 (v86)->regs.eax |= (new_al); \
29 } while(0)
30 #define SET_AH(v86, new_ah) \
31 do { \
32 (v86)->regs.eax &= ~0xff00; \
33 (v86)->regs.eax |= (new_ah) << 8; \
34 } while(0)
35 #define SET_AX(v86, new_ax) \
36 do { \
37 (v86)->regs.eax &= ~0xffff; \
38 (v86)->regs.eax |= (new_ax); \
39 } while(0)
40
41 #define BX(v86) ((unsigned short)((v86)->regs.ebx))
42 #define SET_BX(v86, new_bx) \
43 do { \
44 (v86)->regs.ebx &= ~0xffff; \
45 (v86)->regs.ebx |= (new_bx); \
46 } while(0)
47
48 #define ECX(v86) ((v86)->regs.ecx)
49 #define CX(v86) ((unsigned short)((v86)->regs.ecx))
50 #define CL(v86) ((unsigned char)((v86)->regs.ecx))
51 #define CH(v86) ((unsigned char)((v86)->regs.ecx >> 8))
52 #define SET_CX(v86, new_cx) \
53 do { \
54 (v86)->regs.ecx &= ~0xffff; \
55 (v86)->regs.ecx |= (new_cx); \
56 } while(0)
57
58 #define EDX(v86) ((v86)->regs.edx)
59 #define DX(v86) ((unsigned short)((v86)->regs.edx))
60 #define DL(v86) ((unsigned char)((v86)->regs.edx))
61 #define DH(v86) ((unsigned char)((v86)->regs.edx >> 8))
62 #define SET_DH(v86, new_dh) \
63 do { \
64 (v86)->regs.edx &= ~0xff00; \
65 (v86)->regs.edx |= (new_dh) << 8; \
66 } while(0)
67 #define SET_DX(v86, new_dx) \
68 do { \
69 (v86)->regs.edx &= ~0xffff; \
70 (v86)->regs.edx |= (new_dx); \
71 } while(0)
72
73 #define SI(v86) ((unsigned short)((v86)->regs.esi))
74
75 #define CS(v86) ((v86)->regs.cs)
76 #define DS(v86) ((v86)->regs.ds)
77 #define ES(v86) ((v86)->regs.es)
78
79 #define EBP(v86) ((v86)->regs.ebp)
80 #define BP(v86) ((unsigned short)((v86)->regs.ebp))
81
82 #define EIP(v86) ((v86)->regs.eip)
83
84 #define FAR(seg, off) ((void*)(16*(unsigned long)(seg)+(off)))
85
86 static unsigned int interfaces_enabled = 0;
87 static unsigned int current_year = 2001, current_month = 1, current_day = 1;
88
89 void bios_init()
90 {
91 set_shift_state(0);
92 }
93
94 void set_interfaces(unsigned int enable, unsigned int mask)
95 {
96 interfaces_enabled = (interfaces_enabled & mask) | enable;
97 }
98
99 void set_date(unsigned int y, unsigned int m, unsigned int d)
100 {
101 current_year = y;
102 current_month = m;
103 current_day = d;
104 }
105
106 int handle_int_10(struct vm86plus_struct *v86)
107 {
108 switch (AH(v86))
109 {
110 case 0x0e: /* Character output */
111 outputc(AL(v86));
112 return 1;
113 }
114 return 0;
115 }
116
117 static void read_sector(unsigned char *data, DWORD location_lo,
118 DWORD location_hi)
119 {
120 /* 0xe4 is inb - should abort if executed. */
121 memset(data, 0xe4, 512);
122 *(WORD*)(data+ 0) = 0xe4cd; /* int 0xe4 */
123 *(DWORD*)(data+ 2) = 0xdecea5ed;
124 *(DWORD*)(data+ 6) = location_lo ^ 0x5569aa96;
125 *(DWORD*)(data+10) = location_hi ^ 0x3ca55ac3;
126 *(WORD*)(data+510) = 0xaa55; /* Signature */
127 }
128
129 /* Handle an INT_13 event. Return true if it is handled. */
130 int handle_int_13(struct vm86plus_struct *v86)
131 {
132 unsigned int drive = DL(v86);
133 unsigned int head = DH(v86);
134 unsigned int cylinder = CH(v86) | (((int)CL(v86) & 0xc0) << 2);
135 unsigned int sector = CL(v86) & 0x3f;
136
137 /* Clear the error flag for most cases. */
138 EFLAGS(v86) &= ~CF;
139
140 /* Check the sub-function. */
141 switch (AH(v86))
142 {
143 case 0x2: /* Disk read. */
144 if (AL(v86) != 1) /* Only allow 1 sector. */
145 break;
146 outputf("Read sector d%d h%d c%d s%d to %04x:%04x\n", drive, head,
147 cylinder, sector, ES(v86), BX(v86));
148 read_sector(FAR(ES(v86), BX(v86)),
149 (sector<<0)|(cylinder<<16),
150 (head<<0)|(drive<<16));
151 return 1;
152
153 case 0x3: /* Disk write. */
154 if (AL(v86) != 1) /* Only allow 1 sector. */
155 break;
156 outputf("Write sector d%d h%d c%d s%d to %04x:%04x\n", drive, head,
157 cylinder, sector, ES(v86), BX(v86));
158 compare_mbr(FAR(ES(v86), BX(v86)));
159 return 1;
160
161 case 0x41: /* Big disk installation check. */
162 /* Make sure the parameters are initialised. */
163 if (BX(v86)!=0x55aa)
164 break;
165
166 /* Only support hard disks. */
167 if (0x80&~DL(v86))
168 break;
169
170 /* Is the interface enabled? */
171 if (interfaces_enabled & INTERFACE_BIG_DISK)
172 {
173 /* Yes. We support this. */
174 SET_BX(v86, 0xaa55);
175 SET_AH(v86, 0x01); /* Major version. */
176 SET_AL(v86, 0x95); /* Random number. */
177 SET_CX(v86, 1); /* Only support functions 0x42-0x44,0x47,0x48. */
178 SET_DH(v86, 0); /* Extension version. */
179 /* FIXME: Could have bad support bitmap. Could change version
180 numbers. */
181 }
182 else
183 {
184 /* No. We don't support this. */
185 EFLAGS(v86) |= CF;
186 SET_AH(v86, 0x1);
187 }
188 return 1;
189
190 case 0x42: /* Extended read. */
191 {
192 disk_address_packet *pkt;
193
194 /* Abort if this interface isn't enabled. */
195 if (INTERFACE_BIG_DISK & ~interfaces_enabled)
196 break;
197
198 /* Only support hard disks. */
199 if (0x80&~DL(v86))
200 break;
201
202 /* Get a pointer to the parameters. */
203 pkt = FAR(DS(v86), SI(v86));
204
205 /* The size must be correct. */
206 if (pkt->size != sizeof(*pkt) || pkt->reserved != 0 ||
207 pkt->count != 1)
208 break;
209
210 outputf("Read sector #0x%08x%08x to %04x:%04x\n",
211 (int)(pkt->block>>32), (int)(pkt->block),
212 pkt->seg, pkt->addr);
213
214 /* Fill in the data. */
215 read_sector(FAR(pkt->seg, pkt->addr), pkt->block, pkt->block>>32);
216
217 /* FIXME: Could return error. */
218 EFLAGS(v86) &= ~CF;
219 SET_AH(v86, 0);
220 return 1;
221 }
222
223 case 0x43: /* Extended write. */
224 break; /* Not supported. */
225 case 0x44: /* Verify sectors. */
226 break; /* Not supported. */
227 case 0x47: /* Extended seek. */
228 break; /* Not supported. */
229 case 0x48: /* Get drive parameters. */
230 break; /* Not supported. */
231 }
232 return 0;
233 }
234
235 int handle_int_16(struct vm86plus_struct *v86)
236 {
237 unsigned long key;
238
239 switch (AH(v86))
240 {
241 case 0: /* Get key stroke */
242 SET_AX(v86, get_key_event());
243 return 1;
244
245 case 1: /* Check for key stroke */
246 key = peek_key_event();
247 if (key == NO_KEY)
248 {
249 EFLAGS(v86) |= ZF;
250 }
251 else
252 {
253 EFLAGS(v86) &= ~(unsigned long)ZF;
254 SET_AX(v86, key);
255 }
256 return 1;
257 }
258 return 0;
259 }
260
261 int handle_int_1a(struct vm86plus_struct *v86)
262 {
263 unsigned int time;
264
265 switch (AH(v86))
266 {
267 case 0: /* Get time */
268 time = get_time();
269 SET_CX(v86, (time>>16) & 0xffff);
270 SET_DX(v86, (time>>0) & 0xffff);
271 return 1;
272 }
273 return 0;
274 }
275
276 static void dump_partition(DWORD seg, DWORD offset)
277 {
278 BYTE *data = FAR(seg, offset);
279 /* Format:
280 * 0 BYTE Active flag.
281 * 1 BYTE Start head (DH).
282 * 2 BYTE Start sector + track high bits (CL).
283 * 3 BYTE Start track (CH).
284 * 4 BYTE Partition Type
285 * 5 BYTE End head (DH).
286 * 6 BYTE End sector + track high bits (CL).
287 * 7 BYTE End track (CH).
288 * 8 DWORD Sectors preceeding partition.
289 * 12 DWORD Partition length.
290 */
291 outputf("Partition: %sactive type: 0x%02x\n",
292 data[0] == 0 ? "in" :
293 data[0] == 0x80 ? "" :
294 "maybe ",
295 data[4]);
296 outputf("Start: C=%d H=%d S=%d End: C=%d H=%d S=%d\n",
297 data[3] | (data[2]&0xc0)*4,
298 data[1],
299 data[2] & 0x3f,
300 data[7] | (data[6]&0xc0)*4,
301 data[5],
302 data[6] & 0x3f);
303 outputf("Sector 0x%08lx Length 0x%08lx\n",
304 ((DWORD*)data)[2],
305 ((DWORD*)data)[3]);
306 }
307
308 void handle_int_e4(struct vm86plus_struct *v86)
309 {
310 DWORD *data = FAR(CS(v86), EIP(v86));
311 DWORD location_lo = data[1]^0x5569aa96;
312 DWORD location_hi = data[2]^0x3ca55ac3;
313
314 if (data[0] != 0xdecea5ed)
315 return;
316
317 outputf("Exit: %04lx %04lx %04lx %04lx\n",
318 location_hi>>16, location_hi&0xffff,
319 location_lo>>16, location_lo&0xffff);
320
321 if ( DL(v86) & 0x80 )
322 {
323 /* I forgot segment register gets used here and I can't find a
324 * helpful spec. I think it's ES, but I could be wrong. Both
325 * should be the same. */
326 dump_partition(ES(v86), SI(v86));
327 if ( DS(v86) != ES(v86) )
328 {
329 outputf("Mismatched segment registers.\n");
330 }
331 }
332 else
333 {
334 outputf("Floppy boot.\n");
335 }
336 }
337
338 void set_shift_state (unsigned long state)
339 {
340 unsigned char *flag = (void*)0x417;
341 *flag = state;
342 }
343
344 /* */
345 /* Local Variables: */
346 /* mode:c */
347 /* c-indentation-style: "whitesmith" */
348 /* c-basic-offset: 2 */
349 /* End: */