1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * Copyright 1993 NeXT Computer, Inc.␊ |
26 | * All rights reserved.␊ |
27 | */␊ |
28 | ␊ |
29 | /* Copyright 2007 David Elliott␊ |
30 | 2007-12-30 dfe␊ |
31 | - Enhanced code to normalize segment/offset to huge pointers so that any␊ |
32 | linear address within the first MB of memory can be passed to BIOS␊ |
33 | functions. This allows some of the __DATA sections to span into the␊ |
34 | next segment and also allows stack variables to be used whereas the␊ |
35 | old code could only operate on static data in the first 64k.␊ |
36 | NOTE: Requires bios.s change to respect DS.␊ |
37 | */␊ |
38 | /* Copyright 2007 VMware Inc.␊ |
39 | 2007-12-29 dfe␊ |
40 | - Added ebiosEjectMedia␊ |
41 | */␊ |
42 | #include "bootstruct.h"␊ |
43 | #include "libsaio.h"␊ |
44 | ␊ |
45 | #define MAX_DRIVES 8␊ |
46 | ␊ |
47 | static biosBuf_t bb;␊ |
48 | ␊ |
49 | int bgetc(void)␊ |
50 | {␊ |
51 | /* Poll for the next character. Most real BIOS do not need this as the␊ |
52 | INT 16h,AH=0h function will block until one is received.␊ |
53 | Unfortunately, Apple's EFI CSM will never wake up. This idea is lifted␊ |
54 | from the grub-a20.patch to GRUB's stage2/asm.S file.␊ |
55 | */␊ |
56 | while(!readKeyboardStatus())␊ |
57 | ;␊ |
58 | bb.intno = 0x16;␊ |
59 | bb.eax.r.h = 0x00;␊ |
60 | bios(&bb);␊ |
61 | return bb.eax.rr;␊ |
62 | }␊ |
63 | ␊ |
64 | int readKeyboardStatus(void)␊ |
65 | {␊ |
66 | bb.intno = 0x16;␊ |
67 | bb.eax.r.h = 0x01;␊ |
68 | bios(&bb);␊ |
69 | if (bb.flags.zf) {␊ |
70 | return 0;␊ |
71 | } else {␊ |
72 | return bb.eax.rr;␊ |
73 | }␊ |
74 | }␊ |
75 | ␊ |
76 | int readKeyboardShiftFlags(void)␊ |
77 | {␊ |
78 | bb.intno = 0x16;␊ |
79 | bb.eax.r.h = 0x02;␊ |
80 | bios(&bb);␊ |
81 | return bb.eax.r.l;␊ |
82 | }␊ |
83 | ␊ |
84 | unsigned int time18(void)␊ |
85 | {␊ |
86 | union {␊ |
87 | struct {␊ |
88 | unsigned int low:16;␊ |
89 | unsigned int high:16;␊ |
90 | } s;␊ |
91 | unsigned int i;␊ |
92 | } time;␊ |
93 | ␊ |
94 | bb.intno = 0x1a;␊ |
95 | bb.eax.r.h = 0x00;␊ |
96 | bios(&bb);␊ |
97 | time.s.low = bb.edx.rr;␊ |
98 | time.s.high = bb.ecx.rr;␊ |
99 | return time.i;␊ |
100 | }␊ |
101 | ␊ |
102 | #if 0␊ |
103 | static unsigned long rerangeMemoryMap(unsigned long count)␊ |
104 | {␊ |
105 | ␉int i, still_changing, newcount = count;␊ |
106 | ␊ |
107 | ␉MemoryRange * range = (MemoryRange *)BIOS_ADDR;␊ |
108 | ␉struct MemoryRange change_tmp;␊ |
109 | ␊ |
110 | ␉/* sort map list by memory addresses (low -> high) */␊ |
111 | ␉still_changing = 1;␊ |
112 | ␉while (still_changing) {␊ |
113 | ␉␉still_changing = 0;␊ |
114 | ␉␉for (i=1; i<count; i++) {␊ |
115 | ␉␉␉/* if <current_addr> > <last_addr>, swap */␊ |
116 | ␉␉␉if (range[i].base < range[i-1].base) {␊ |
117 | ␉␉␉␉change_tmp.base = range[i].base;␊ |
118 | ␉␉␉␉change_tmp.length = range[i].length;␊ |
119 | ␉␉␉␉change_tmp.type = range[i].type;␊ |
120 | ␊ |
121 | ␉␉␉␉range[i].base = range[i-1].base;␊ |
122 | ␉␉␉␉range[i].length = range[i-1].length;␊ |
123 | ␉␉␉␉range[i].type = range[i-1].type;␊ |
124 | ␊ |
125 | ␉␉␉␉range[i-1].base = change_tmp.base;␊ |
126 | ␉␉␉␉range[i-1].length = change_tmp.length;␊ |
127 | ␉␉␉␉range[i-1].type = change_tmp.type;␊ |
128 | ␊ |
129 | ␉␉␉␉still_changing=1;␊ |
130 | ␉␉␉}␊ |
131 | ␉␉}␊ |
132 | ␉}␊ |
133 | ␊ |
134 | ␉/* clear overlaps */␊ |
135 | ␉/* linux's arch/i386/kern/setup.c may have better algorithm */␊ |
136 | ␉for (i=1; i<count; i++) {␊ |
137 | ␉␉if ( range[i-1].base + range[i-1].length > range[i].base ) {␊ |
138 | ␉␉␉range[newcount].base = range[i].base + range[i].length;␊ |
139 | ␉␉␉range[newcount].length = range[i-1].base + range[i-1].length - range[newcount].base;␊ |
140 | ␉␉␉range[newcount].type = range[i-1].type;␊ |
141 | ␉␉␉newcount++;␊ |
142 | ␊ |
143 | ␉␉␉range[i-1].length = range[i].base - range[i-1].base;␊ |
144 | ␉␉}␊ |
145 | ␉}␊ |
146 | ␊ |
147 | ␉/*␊ |
148 | ␉ * 0xb0000000 : 0x10000000 NG␊ |
149 | ␉ * 0xc0000400 NG␊ |
150 | ␉ * 0xf2000000 NG␊ |
151 | ␉ */␊ |
152 | ␉range[newcount].base = 0xb0000000;␊ |
153 | ␉range[newcount].length = 0x0f000000;␊ |
154 | ␉range[newcount].type = kMemoryRangeUsable;␊ |
155 | ␉newcount++;␊ |
156 | ␊ |
157 | ␉return newcount;␊ |
158 | }␊ |
159 | #endif␊ |
160 | ␊ |
161 | unsigned long getMemoryMap( MemoryRange * rangeArray,␊ |
162 | unsigned long maxRangeCount,␊ |
163 | unsigned long * conMemSizePtr,␊ |
164 | unsigned long * extMemSizePtr )␊ |
165 | {␊ |
166 | #define kMemoryMapSignature 'SMAP'␊ |
167 | #define kDescriptorSizeMin 20␊ |
168 | ␊ |
169 | MemoryRange *␉range = (MemoryRange *)BIOS_ADDR;␊ |
170 | unsigned long␉count = 0;␊ |
171 | // unsigned long␉rerangedCount;␊ |
172 | unsigned long long␉conMemSize = 0;␊ |
173 | unsigned long long␉extMemSize = 0;␊ |
174 | ␊ |
175 | // Prepare for the INT15 E820h call. Each call returns a single␊ |
176 | // memory range. A continuation value is returned that must be␊ |
177 | // provided on a subsequent call to fetch the next range.␊ |
178 | //␊ |
179 | // Certain BIOSes (Award 6.00PG) expect the upper word in EAX␊ |
180 | // to be cleared on entry, otherwise only a single range will␊ |
181 | // be reported.␊ |
182 | //␊ |
183 | // Some BIOSes will simply ignore the value of ECX on entry.␊ |
184 | // Probably best to keep its value at 20 to avoid surprises.␊ |
185 | ␊ |
186 | //printf("Get memory map 0x%x, %d\n", rangeArray);getc();␊ |
187 | if (maxRangeCount > (BIOS_LEN / sizeof(MemoryRange))) {␊ |
188 | maxRangeCount = (BIOS_LEN / sizeof(MemoryRange));␊ |
189 | }␊ |
190 | bb.ebx.rx = 0; // Initial continuation value must be zero.␊ |
191 | ␊ |
192 | while ( count < maxRangeCount )␊ |
193 | {␊ |
194 | bb.intno = 0x15;␊ |
195 | bb.eax.rx = 0xe820;␊ |
196 | bb.ecx.rx = kDescriptorSizeMin;␊ |
197 | bb.edx.rx = kMemoryMapSignature;␊ |
198 | bb.edi.rr = NORMALIZED_OFFSET( (unsigned long) range );␊ |
199 | bb.es = NORMALIZED_SEGMENT( (unsigned long) range );␊ |
200 | bios(&bb);␊ |
201 | ␊ |
202 | // Check for errors.␊ |
203 | ␊ |
204 | if ( bb.flags.cf␊ |
205 | || bb.eax.rx != kMemoryMapSignature␊ |
206 | || bb.ecx.rx != kDescriptorSizeMin ) {␊ |
207 | //printf("Got an error %x %x %x\n", bb.flags.cf,␊ |
208 | // bb.eax.rx, bb.ecx.rx);␊ |
209 | break;␊ |
210 | }␊ |
211 | ␊ |
212 | // Tally up the conventional/extended memory sizes.␊ |
213 | ␊ |
214 | if ( range->type == kMemoryRangeUsable ||␊ |
215 | range->type == kMemoryRangeACPI ||␊ |
216 | range->type == kMemoryRangeNVS )␊ |
217 | {␊ |
218 | // Tally the conventional memory ranges.␊ |
219 | if ( range->base + range->length <= 0xa0000 ) {␊ |
220 | conMemSize += range->length;␊ |
221 | } ␊ |
222 | ␊ |
223 | // Record the top of extended memory.␊ |
224 | if ( range->base >= EXTENDED_ADDR ) {␊ |
225 | extMemSize += range->length;␊ |
226 | }␊ |
227 | }␊ |
228 | ␊ |
229 | range++;␊ |
230 | count++;␊ |
231 | ␊ |
232 | // Is this the last address range?␊ |
233 | ␊ |
234 | if ( bb.ebx.rx == 0 ) {␊ |
235 | //printf("last range\n");␊ |
236 | break;␊ |
237 | }␊ |
238 | }␊ |
239 | *conMemSizePtr = conMemSize / 1024; // size in KB␊ |
240 | *extMemSizePtr = extMemSize / 1024; // size in KB␊ |
241 | ␊ |
242 | #if 0␊ |
243 | rerangedCount = rerangeMemoryMap(count);␊ |
244 | range += rerangedCount - count;␊ |
245 | #endif␊ |
246 | ␊ |
247 | // Copy out data␊ |
248 | bcopy((char *)BIOS_ADDR, rangeArray, ((char *)range - (char *)BIOS_ADDR));␊ |
249 | ␊ |
250 | #if DEBUG␊ |
251 | {␊ |
252 | int i;␊ |
253 | printf("%d total ranges\n", count);getc();␊ |
254 | for (i=0, range = rangeArray; i<count; i++, range++) {␊ |
255 | printf("range: type %d, base 0x%x, length 0x%x\n",␊ |
256 | range->type, (unsigned int)range->base, (unsigned int)range->length); getc();␊ |
257 | }␊ |
258 | }␊ |
259 | #endif␊ |
260 | ␊ |
261 | return count;␊ |
262 | }␊ |
263 | ␊ |
264 | unsigned long getExtendedMemorySize()␊ |
265 | {␊ |
266 | // Get extended memory size for large configurations. Not used unless␊ |
267 | // the INT15, E820H call (Get System Address Map) failed.␊ |
268 | //␊ |
269 | // Input:␊ |
270 | //␊ |
271 | // AX Function Code E801h␊ |
272 | //␊ |
273 | // Outputs:␊ |
274 | //␊ |
275 | // CF Carry Flag Carry cleared indicates no error.␊ |
276 | // AX Extended 1 Number of contiguous KB between 1 and 16 MB,␊ |
277 | // maximum 0x3C00 = 15 MB.␊ |
278 | // BX Extended 2 Number of contiguous 64 KB blocks between␊ |
279 | // 16 MB and 4 GB.␊ |
280 | // CX Configured 1 Number of contiguous KB between 1 and 16 MB,␊ |
281 | // maximum 0x3C00 = 15 MB.␊ |
282 | // DX Configured 2 Number of contiguous 64 KB blocks between␊ |
283 | // 16 MB and 4 GB.␊ |
284 | ␊ |
285 | bb.intno = 0x15;␊ |
286 | bb.eax.rx = 0xe801;␊ |
287 | bios(&bb);␊ |
288 | ␊ |
289 | // Return the size of memory above 1MB (extended memory) in kilobytes.␊ |
290 | ␊ |
291 | if ( bb.flags.cf == 0 ) return (bb.ebx.rr * 64 + bb.eax.rr);␊ |
292 | ␊ |
293 | // Get Extended memory size. Called on last resort since the return␊ |
294 | // value is limited to 16-bits (a little less than 64MB max). May␊ |
295 | // not be supported by modern BIOSes.␊ |
296 | //␊ |
297 | // Input:␊ |
298 | //␊ |
299 | // AX Function Code E801h␊ |
300 | //␊ |
301 | // Outputs:␊ |
302 | //␊ |
303 | // CF Carry Flag Carry cleared indicates no error.␊ |
304 | // AX Memory Count Number of contiguous KB above 1MB.␊ |
305 | ␊ |
306 | bb.intno = 0x15;␊ |
307 | bb.eax.rx = 0x88;␊ |
308 | bios(&bb);␊ |
309 | ␊ |
310 | // Return the size of memory above 1MB (extended memory) in kilobytes.␊ |
311 | ␊ |
312 | return bb.flags.cf ? 0 : bb.eax.rr;␊ |
313 | }␊ |
314 | ␊ |
315 | unsigned long getConventionalMemorySize()␊ |
316 | {␊ |
317 | bb.intno = 0x12;␊ |
318 | bios(&bb);␊ |
319 | return bb.eax.rr; // kilobytes␊ |
320 | }␊ |
321 | ␊ |
322 | void video_mode(int mode)␊ |
323 | {␊ |
324 | bb.intno = 0x10;␊ |
325 | bb.eax.r.h = 0x00;␊ |
326 | bb.eax.r.l = mode;␊ |
327 | bios(&bb);␊ |
328 | }␊ |
329 | ␊ |
330 | int biosread(int dev, int cyl, int head, int sec, int num)␊ |
331 | {␊ |
332 | int i;␊ |
333 | ␊ |
334 | bb.intno = 0x13;␊ |
335 | sec += 1; /* sector numbers start at 1 */␊ |
336 | ␊ |
337 | for (i=0;;) {␊ |
338 | bb.ecx.r.h = cyl;␊ |
339 | bb.ecx.r.l = ((cyl & 0x300) >> 2) | (sec & 0x3F);␊ |
340 | bb.edx.r.h = head;␊ |
341 | bb.edx.r.l = dev;␊ |
342 | bb.eax.r.l = num;␊ |
343 | bb.ebx.rr = OFFSET(ptov(BIOS_ADDR));␊ |
344 | bb.es = SEGMENT(ptov(BIOS_ADDR));␊ |
345 | ␊ |
346 | bb.eax.r.h = 0x02;␊ |
347 | bios(&bb);␊ |
348 | ␊ |
349 | /* In case of a successful call, make sure we set AH (return code) to zero. */␊ |
350 | if (bb.flags.cf == 0)␊ |
351 | bb.eax.r.h = 0;␊ |
352 | ␊ |
353 | /* Now we can really check for the return code (AH) value. */␊ |
354 | if ((bb.eax.r.h == 0x00) || (i++ >= 5))␊ |
355 | break;␊ |
356 | ␊ |
357 | /* reset disk subsystem and try again */␊ |
358 | bb.eax.r.h = 0x00;␊ |
359 | bios(&bb);␊ |
360 | }␊ |
361 | return bb.eax.r.h;␊ |
362 | }␊ |
363 | ␊ |
364 | int ebiosread(int dev, unsigned long long sec, int count)␊ |
365 | {␊ |
366 | int i;␊ |
367 | static struct {␊ |
368 | unsigned char size;␊ |
369 | unsigned char reserved;␊ |
370 | unsigned char numblocks;␊ |
371 | unsigned char reserved2;␊ |
372 | unsigned short bufferOffset;␊ |
373 | unsigned short bufferSegment;␊ |
374 | unsigned long long startblock;␊ |
375 | } addrpacket __attribute__((aligned(16))) = {0};␊ |
376 | addrpacket.size = sizeof(addrpacket);␊ |
377 | ␊ |
378 | for (i=0;;) {␊ |
379 | bb.intno = 0x13;␊ |
380 | bb.eax.r.h = 0x42;␊ |
381 | bb.edx.r.l = dev;␊ |
382 | bb.esi.rr = NORMALIZED_OFFSET((unsigned)&addrpacket);␊ |
383 | bb.ds = NORMALIZED_SEGMENT((unsigned)&addrpacket);␊ |
384 | addrpacket.reserved = addrpacket.reserved2 = 0;␊ |
385 | addrpacket.numblocks = count;␊ |
386 | addrpacket.bufferOffset = OFFSET(ptov(BIOS_ADDR));␊ |
387 | addrpacket.bufferSegment = SEGMENT(ptov(BIOS_ADDR));␊ |
388 | addrpacket.startblock = sec;␊ |
389 | bios(&bb);␊ |
390 | ␊ |
391 | /* In case of a successful call, make sure we set AH (return code) to zero. */␊ |
392 | if (bb.flags.cf == 0)␊ |
393 | bb.eax.r.h = 0;␊ |
394 | ␊ |
395 | /* Now we can really check for the return code (AH) value. */␊ |
396 | if ((bb.eax.r.h == 0x00) || (i++ >= 5))␊ |
397 | break;␊ |
398 | ␊ |
399 | /* reset disk subsystem and try again */␊ |
400 | bb.eax.r.h = 0x00;␊ |
401 | bios(&bb);␊ |
402 | }␊ |
403 | return bb.eax.r.h;␊ |
404 | }␊ |
405 | ␊ |
406 | int ebioswrite(int dev, long sec, int count)␊ |
407 | {␊ |
408 | int i;␊ |
409 | static struct {␊ |
410 | unsigned char size;␊ |
411 | unsigned char reserved;␊ |
412 | unsigned char numblocks;␊ |
413 | unsigned char reserved2;␊ |
414 | unsigned short bufferOffset;␊ |
415 | unsigned short bufferSegment;␊ |
416 | unsigned long long startblock;␊ |
417 | } addrpacket __attribute__((aligned(16))) = {0};␊ |
418 | addrpacket.size = sizeof(addrpacket);␊ |
419 | ␊ |
420 | for (i=0;;) {␊ |
421 | bb.intno = 0x13;␊ |
422 | bb.eax.r.l = 0; /* Don't verify */␊ |
423 | bb.eax.r.h = 0x43;␊ |
424 | bb.edx.r.l = dev;␊ |
425 | bb.esi.rr = NORMALIZED_OFFSET((unsigned)&addrpacket);␊ |
426 | bb.ds = NORMALIZED_SEGMENT((unsigned)&addrpacket);␊ |
427 | addrpacket.reserved = addrpacket.reserved2 = 0;␊ |
428 | addrpacket.numblocks = count;␊ |
429 | addrpacket.bufferOffset = OFFSET(ptov(BIOS_ADDR));␊ |
430 | addrpacket.bufferSegment = SEGMENT(ptov(BIOS_ADDR));␊ |
431 | addrpacket.startblock = sec;␊ |
432 | bios(&bb);␊ |
433 | ␊ |
434 | /* In case of a successful call, make sure we set AH (return code) to zero. */␊ |
435 | if (bb.flags.cf == 0)␊ |
436 | bb.eax.r.h = 0;␊ |
437 | ␊ |
438 | /* Now we can really check for the return code (AH) value. */␊ |
439 | if ((bb.eax.r.h == 0x00) || (i++ >= 5))␊ |
440 | break;␊ |
441 | ␊ |
442 | /* reset disk subsystem and try again */␊ |
443 | bb.eax.r.h = 0x00;␊ |
444 | bios(&bb);␊ |
445 | }␊ |
446 | return bb.eax.r.h;␊ |
447 | }␊ |
448 | ␊ |
449 | void bios_putchar(int ch)␊ |
450 | {␊ |
451 | bb.intno = 0x10;␊ |
452 | bb.ebx.r.h = 0x00; /* background black */␊ |
453 | bb.ebx.r.l = 0x0F; /* foreground white */␊ |
454 | bb.eax.r.h = 0x0e;␊ |
455 | bb.eax.r.l = ch;␊ |
456 | bios(&bb);␊ |
457 | }␊ |
458 | ␊ |
459 | void putca(int ch, int attr, int repeat)␊ |
460 | {␊ |
461 | bb.intno = 0x10;␊ |
462 | bb.ebx.r.h = 0x00; /* page number */␊ |
463 | bb.ebx.r.l = attr; /* attribute */␊ |
464 | bb.eax.r.h = 0x9;␊ |
465 | bb.eax.r.l = ch;␊ |
466 | bb.ecx.rx = repeat; /* repeat count */ ␊ |
467 | bios(&bb);␊ |
468 | }␊ |
469 | ␊ |
470 | /* Check to see if the passed-in drive is in El Torito no-emulation mode. */␊ |
471 | int is_no_emulation(int drive)␊ |
472 | {␊ |
473 | ␉struct packet {␊ |
474 | ␉␉unsigned char packet_size;␊ |
475 | ␉␉unsigned char media_type;␊ |
476 | ␉␉unsigned char drive_num;␊ |
477 | ␉␉unsigned char ctrlr_index;␊ |
478 | ␉␉unsigned long lba;␊ |
479 | ␉␉unsigned short device_spec;␊ |
480 | ␉␉unsigned short buffer_segment;␊ |
481 | ␉␉unsigned short load_segment;␊ |
482 | ␉␉unsigned short sector_count;␊ |
483 | ␉␉unsigned char cyl_count;␊ |
484 | ␉␉unsigned char sec_count;␊ |
485 | ␉␉unsigned char head_count;␊ |
486 | ␉␉unsigned char reseved;␊ |
487 | ␉} __attribute__((packed));␊ |
488 | ␉static struct packet pkt;␊ |
489 | ␉␊ |
490 | ␉bzero(&pkt, sizeof(pkt));␊ |
491 | ␉pkt.packet_size = 0x13;␊ |
492 | ␉␊ |
493 | ␉bb.intno␉␉= 0x13;␊ |
494 | ␉bb.eax.r.h␉␉= 0x4b;␊ |
495 | ␉bb.eax.r.l␉␉= 0x01; // subfunc: get info␊ |
496 | ␉bb.edx.r.l␉␉= drive;␊ |
497 | ␉bb.esi.rr␉␉= NORMALIZED_OFFSET((unsigned)&pkt);␊ |
498 | ␉bb.ds␉␉␉= NORMALIZED_SEGMENT((unsigned)&pkt);␊ |
499 | ␉␊ |
500 | ␉bios(&bb);␊ |
501 | #if DEBUG␊ |
502 | ␉printf("el_torito info drive %x\n", drive);␊ |
503 | ␉␊ |
504 | ␉printf("--> cf %x, eax %x\n", bb.flags.cf, bb.eax.rr);␊ |
505 | ␉␊ |
506 | ␉printf("pkt_size: %x\n", pkt.packet_size);␊ |
507 | ␉printf("media_type: %x\n", pkt.media_type);␊ |
508 | ␉printf("drive_num: %x\n", pkt.drive_num);␊ |
509 | ␉printf("device_spec: %x\n", pkt.device_spec);␊ |
510 | ␉printf("press a key->\n");getc();␊ |
511 | #endif␊ |
512 | ␉␊ |
513 | ␉/* Some BIOSes erroneously return cf = 1 */␊ |
514 | ␉/* Just check to see if the drive number is the same. */␊ |
515 | ␉if (pkt.drive_num == drive) {␊ |
516 | ␉␉if ((pkt.media_type & 0x0F) == 0) {␊ |
517 | ␉␉␉/* We are in no-emulation mode. */␊ |
518 | ␉␉␉return 1;␊ |
519 | ␉␉}␊ |
520 | ␉}␊ |
521 | ␉return 0;␊ |
522 | }␊ |
523 | ␊ |
524 | #if DEBUG␊ |
525 | /*␊ |
526 | * BIOS drive information.␊ |
527 | */␊ |
528 | void print_drive_info(boot_drive_info_t *dp)␊ |
529 | {␊ |
530 | //␉printf("buf_size = %x\n", dp->params.buf_size);␊ |
531 | ␉printf("info_flags = %x\n", dp->params.info_flags);␊ |
532 | ␉printf("phys_cyls = %lx\n", dp->params. phys_cyls);␊ |
533 | ␉printf("phys_heads = %lx\n", dp->params. phys_heads);␊ |
534 | ␉printf("phys_spt = %lx\n", dp->params. phys_spt);␊ |
535 | ␉printf("phys_sectors = %lx%lx\n", ((unsigned long *)(&dp->params.phys_sectors))[1],␊ |
536 | ␉␉ ((unsigned long *)(&dp->params.phys_sectors))[0]);␊ |
537 | ␉printf("phys_nbps = %x\n", dp->params.phys_nbps);␊ |
538 | //␉printf("dpte_offset = %x\n", dp->params.dpte_offset);␊ |
539 | //␉printf("dpte_segment = %x\n", dp->params.dpte_segment);␊ |
540 | //␉printf("key = %x\n", dp->params.key);␊ |
541 | //␉printf("path_len = %x\n", dp->params. path_len);␊ |
542 | //␉printf("reserved1 = %x\n", dp->params. reserved1);␊ |
543 | //␉printf("reserved2 = %x\n", dp->params.reserved2);␊ |
544 | //␉printf("bus_type[4] = %x\n", dp->params. bus_type[4]);␊ |
545 | //␉printf("interface_type[8] = %x\n", dp->params. interface_type[8]);␊ |
546 | //␉printf("interface_path[8] = %x\n", dp->params. interface_path[8]);␊ |
547 | //␉printf("dev_path[8] = %x\n", dp->params. dev_path[8]);␊ |
548 | //␉printf("reserved3 = %x\n", dp->params. reserved3);␊ |
549 | //␉printf("checksum = %x\n", dp->params. checksum);␊ |
550 | ␉printf("io_port_base = %x\n", dp->dpte.io_port_base);␊ |
551 | ␉printf("control_port_base = %x\n", dp->dpte.control_port_base);␊ |
552 | ␉printf("head_flags = %x\n", dp->dpte. head_flags);␊ |
553 | ␉printf("vendor_info = %x\n", dp->dpte. vendor_info);␊ |
554 | ␉printf("irq = %x\n", dp->dpte. irq);␊ |
555 | //␉printf("irq_unused = %x\n", dp->dpte. irq_unused);␊ |
556 | ␉printf("block_count = %x\n", dp->dpte. block_count);␊ |
557 | ␉printf("dma_channe = %x\n", dp->dpte. dma_channel);␊ |
558 | ␉printf("dma_type = %x\n", dp->dpte. dma_type);␊ |
559 | ␉printf("pio_type = %x\n", dp->dpte. pio_type);␊ |
560 | ␉printf("pio_unused = %x\n", dp->dpte. pio_unused);␊ |
561 | ␉printf("option_flags = %x\n", dp->dpte.option_flags);␊ |
562 | //␉printf("reserved = %x\n", dp->dpte.reserved);␊ |
563 | ␉printf("revision = %x\n", dp->dpte. revision);␊ |
564 | //␉printf("checksum = %x\n", dp->dpte. checksum);␊ |
565 | }␊ |
566 | ␊ |
567 | #endif␊ |
568 | ␊ |
569 | int get_drive_info(int drive, struct driveInfo *dp)␊ |
570 | {␊ |
571 | ␉boot_drive_info_t *di = &dp->di;␊ |
572 | ␉int ret = 0;␊ |
573 | ␉␊ |
574 | #if UNUSED␊ |
575 | ␉if (maxhd == 0) {␊ |
576 | ␉␉bb.intno = 0x13;␊ |
577 | ␉␉bb.eax.r.h = 0x08;␊ |
578 | ␉␉bb.edx.r.l = 0x80;␊ |
579 | ␉␉bios(&bb);␊ |
580 | ␉␉if (bb.flags.cf == 0)␊ |
581 | ␉␉␉maxhd = 0x7f + bb.edx.r.l;␊ |
582 | ␉};␊ |
583 | ␉␊ |
584 | ␉if (drive > maxhd)␊ |
585 | ␉␉return 0;␊ |
586 | #endif␊ |
587 | ␉␊ |
588 | ␉bzero(dp, sizeof(struct driveInfo));␊ |
589 | ␉dp->biosdev = drive;␊ |
590 | ␉␊ |
591 | ␉/* Check for El Torito no-emulation mode. */␊ |
592 | ␉dp->no_emulation = is_no_emulation(drive);␊ |
593 | ␊ |
594 | ␉/* Check drive for EBIOS support. */␊ |
595 | ␉bb.intno = 0x13;␊ |
596 | ␉bb.eax.r.h = 0x41;␊ |
597 | ␉bb.edx.r.l = drive;␊ |
598 | ␉bb.ebx.rr = 0x55aa;␊ |
599 | ␉bios(&bb);␊ |
600 | ␉␊ |
601 | ␉if ((bb.ebx.rr == 0xaa55) && (bb.flags.cf == 0)) {␊ |
602 | ␉␉/* Get flags for supported operations. */␊ |
603 | ␉␉dp->uses_ebios = bb.ecx.r.l;␊ |
604 | ␉}␊ |
605 | ␉␊ |
606 | ␉if (dp->uses_ebios & (EBIOS_ENHANCED_DRIVE_INFO | EBIOS_LOCKING_ACCESS | EBIOS_FIXED_DISK_ACCESS)) {␊ |
607 | ␉␉/* Get EBIOS drive info. */␊ |
608 | ␉␉static struct drive_params params;␊ |
609 | ␉␉␊ |
610 | ␉␉params.buf_size = sizeof(params);␊ |
611 | ␉␉bb.intno = 0x13;␊ |
612 | ␉␉bb.eax.r.h = 0x48;␊ |
613 | ␉␉bb.edx.r.l = drive;␊ |
614 | ␉␉bb.esi.rr = NORMALIZED_OFFSET((unsigned)¶ms);␊ |
615 | ␉␉bb.ds␉ = NORMALIZED_SEGMENT((unsigned)¶ms);␊ |
616 | ␉␉bios(&bb);␊ |
617 | ␉␉␊ |
618 | ␉␉if (bb.flags.cf != 0 /* || params.phys_sectors < 2097152 */) {␊ |
619 | ␉␉␉dp->uses_ebios = 0;␊ |
620 | ␉␉␉di->params.buf_size = 1;␊ |
621 | ␉␉}␊ |
622 | ␉␉else␊ |
623 | ␉␉{␊ |
624 | ␉␉␉bcopy(¶ms, &di->params, sizeof(params));␊ |
625 | ␉␉␉␊ |
626 | ␉␉␉if (drive >= BASE_HD_DRIVE &&␊ |
627 | ␉␉␉␉(dp->uses_ebios & EBIOS_ENHANCED_DRIVE_INFO) &&␊ |
628 | ␉␉␉␉di->params.buf_size >= 30 &&␊ |
629 | ␉␉␉␉!(di->params.dpte_offset == 0xFFFF && di->params.dpte_segment == 0xFFFF)) {␊ |
630 | ␉␉␉␉␉void *ptr = (void *)(di->params.dpte_offset + ((unsigned int)di->params.dpte_segment << 4));␊ |
631 | ␉␉␉␉␉bcopy(ptr, &di->dpte, sizeof(di->dpte));␊ |
632 | ␉␉␉}␊ |
633 | ␉␉}␊ |
634 | ␉}␊ |
635 | ␊ |
636 | /*␊ |
637 | * zef: This code will fail on recent JMicron and Intel option ROMs␊ |
638 | */ ␊ |
639 | //␉if (di->params.phys_heads == 0 || di->params.phys_spt == 0) {␊ |
640 | //␉␉/* Either it's not EBIOS, or EBIOS didn't tell us. */␊ |
641 | //␉␉bb.intno = 0x13;␊ |
642 | //␉␉bb.eax.r.h = 0x08;␊ |
643 | //␉␉bb.edx.r.l = drive;␊ |
644 | //␉␉bios(&bb);␊ |
645 | //␉␉if (bb.flags.cf == 0 && bb.eax.r.h == 0) {␊ |
646 | //␉␉␉unsigned long cyl;␊ |
647 | //␉␉␉unsigned long sec;␊ |
648 | //␉␉␉unsigned long hds;␊ |
649 | //␉␉␊ |
650 | //␉␉␉hds = bb.edx.r.h;␊ |
651 | //␉␉␉sec = bb.ecx.r.l & 0x3F;␊ |
652 | //␉␉␉if ((dp->uses_ebios & EBIOS_ENHANCED_DRIVE_INFO) && (sec != 0)) {␊ |
653 | //␉␉␉␉cyl = (di->params.phys_sectors / ((hds + 1) * sec)) - 1;␊ |
654 | //␉␉␉} else {␊ |
655 | //␉␉␉␉cyl = bb.ecx.r.h | ((bb.ecx.r.l & 0xC0) << 2);␊ |
656 | //␉␉␉}␊ |
657 | //␉␉␉di->params.phys_heads = hds; ␊ |
658 | //␉␉␉di->params.phys_spt = sec;␊ |
659 | //␉␉␉di->params.phys_cyls = cyl;␊ |
660 | //␉␉} else {␊ |
661 | //␉␉␉ret = -1;␊ |
662 | //␉␉}␊ |
663 | //␉}␊ |
664 | ␊ |
665 | ␉if (dp->no_emulation) {␊ |
666 | ␉␉/* Some BIOSes give us erroneous EBIOS support information.␊ |
667 | ␉ ␉ * Assume that if you're on a CD, then you can use␊ |
668 | ␉ ␉ * EBIOS disk calls.␊ |
669 | ␉ ␉ */␊ |
670 | ␉␉dp->uses_ebios |= EBIOS_FIXED_DISK_ACCESS;␊ |
671 | ␉}␊ |
672 | #if DEBUG␊ |
673 | ␉print_drive_info(di);␊ |
674 | ␉printf("uses_ebios = 0x%x\n", dp->uses_ebios);␊ |
675 | ␉printf("result %d\n", ret);␊ |
676 | ␉printf("press a key->\n");getc();␊ |
677 | #endif␊ |
678 | ␊ |
679 | ␉if (ret == 0) {␊ |
680 | ␉␉dp->valid = 1;␊ |
681 | ␉}␊ |
682 | ␉return ret;␊ |
683 | }␊ |
684 | ␊ |
685 | int ebiosEjectMedia(int biosdev)␊ |
686 | {␊ |
687 | bb.intno = 0x13;␊ |
688 | bb.eax.r.h = 0x46;␊ |
689 | bb.eax.r.l = 0;␊ |
690 | bb.edx.rx = biosdev;␊ |
691 | bios(&bb);␊ |
692 | return bb.eax.r.h;␊ |
693 | }␊ |
694 | ␊ |
695 | void setCursorPosition(int x, int y, int page)␊ |
696 | {␊ |
697 | bb.intno = 0x10;␊ |
698 | bb.eax.r.h = 0x02;␊ |
699 | bb.ebx.r.h = page; /* page 0 for graphics */␊ |
700 | bb.edx.r.l = x;␊ |
701 | bb.edx.r.h = y;␊ |
702 | bios(&bb);␊ |
703 | }␊ |
704 | ␊ |
705 | void setCursorType(int type)␊ |
706 | {␊ |
707 | bb.intno = 0x10;␊ |
708 | bb.eax.r.h = 0x01;␊ |
709 | bb.ecx.rr = type;␊ |
710 | bios(&bb);␊ |
711 | }␊ |
712 | ␊ |
713 | void getCursorPositionAndType(int * x, int * y, int * type)␊ |
714 | {␊ |
715 | bb.intno = 0x10;␊ |
716 | bb.eax.r.h = 0x03;␊ |
717 | bios(&bb);␊ |
718 | *x = bb.edx.r.l;␊ |
719 | *y = bb.edx.r.h;␊ |
720 | *type = bb.ecx.rr;␊ |
721 | }␊ |
722 | ␊ |
723 | void scollPage(int x1, int y1, int x2, int y2, int attr, int rows, int dir)␊ |
724 | {␊ |
725 | bb.intno = 0x10;␊ |
726 | bb.eax.r.h = (dir > 0) ? 0x06 : 0x07;␊ |
727 | bb.eax.r.l = rows;␊ |
728 | bb.ebx.r.h = attr;␊ |
729 | bb.ecx.r.h = y1;␊ |
730 | bb.ecx.r.l = x1;␊ |
731 | bb.edx.r.h = y2;␊ |
732 | bb.edx.r.l = x2;␊ |
733 | bios(&bb);␊ |
734 | }␊ |
735 | ␊ |
736 | void clearScreenRows( int y1, int y2 )␊ |
737 | {␊ |
738 | scollPage( 0, y1, 80 - 1, y2, 0x07, y2 - y1 + 1, 1 );␊ |
739 | }␊ |
740 | ␊ |
741 | void setActiveDisplayPage( int page )␊ |
742 | {␊ |
743 | bb.intno = 0x10;␊ |
744 | bb.eax.r.h = 5;␊ |
745 | bb.eax.r.l = page;␊ |
746 | bios(&bb);␊ |
747 | }␊ |
748 | ␊ |
749 | #if DEBUG␊ |
750 | ␊ |
751 | int terminateDiskEmulation()␊ |
752 | {␊ |
753 | static char cd_spec[0x13];␊ |
754 | ␊ |
755 | bb.intno = 0x13;␊ |
756 | bb.eax.r.h = 0x4b;␊ |
757 | bb.eax.r.l = 0; // subfunc: terminate emulation ␊ |
758 | bb.esi.rr = NORMALIZED_OFFSET((unsigned)&cd_spec);␊ |
759 | bb.ds = NORMALIZED_SEGMENT((unsigned)&cd_spec);␊ |
760 | bios(&bb);␊ |
761 | return bb.eax.r.h;␊ |
762 | }␊ |
763 | ␊ |
764 | int readDriveParameters(int drive, struct driveParameters *dp)␊ |
765 | {␊ |
766 | bb.intno = 0x13;␊ |
767 | bb.edx.r.l = drive;␊ |
768 | bb.eax.r.h = 0x08;␊ |
769 | bios(&bb);␊ |
770 | if (bb.eax.r.h == 0) {␊ |
771 | dp->heads = bb.edx.r.h;␊ |
772 | dp->sectors = bb.ecx.r.l & 0x3F;␊ |
773 | dp->cylinders = bb.ecx.r.h | ((bb.ecx.r.l & 0xC0) << 2);␊ |
774 | dp->totalDrives = bb.edx.r.l;␊ |
775 | } else {␊ |
776 | bzero(dp, sizeof(*dp));␊ |
777 | }␊ |
778 | return bb.eax.r.h;␊ |
779 | ␊ |
780 | }␊ |
781 | #endif␊ |
782 | ␊ |
783 | #ifdef APM_SUPPORT␊ |
784 | ␊ |
785 | #define APM_INTNO 0x15␊ |
786 | #define APM_INTCODE 0x53␊ |
787 | ␊ |
788 | int␊ |
789 | APMPresent(void)␊ |
790 | {␊ |
791 | bb.intno = APM_INTNO;␊ |
792 | bb.eax.r.h = APM_INTCODE;␊ |
793 | bb.eax.r.l = 0x00;␊ |
794 | bb.ebx.rr = 0x0000;␊ |
795 | bios(&bb);␊ |
796 | if ((bb.flags.cf == 0) &&␊ |
797 | (bb.ebx.r.h == 'P') &&␊ |
798 | (bb.ebx.r.l == 'M')) {␊ |
799 | /* Success */␊ |
800 | bootArgs->apmConfig.major_vers = bb.eax.r.h;␊ |
801 | bootArgs->apmConfig.minor_vers = bb.eax.r.l;␊ |
802 | bootArgs->apmConfig.flags.data = bb.ecx.rr;␊ |
803 | return 1;␊ |
804 | }␊ |
805 | return 0;␊ |
806 | }␊ |
807 | ␊ |
808 | int␊ |
809 | APMConnect32(void)␊ |
810 | {␊ |
811 | bb.intno = APM_INTNO;␊ |
812 | bb.eax.r.h = APM_INTCODE;␊ |
813 | bb.eax.r.l = 0x03;␊ |
814 | bb.ebx.rr = 0x0000;␊ |
815 | bios(&bb);␊ |
816 | if (bb.flags.cf == 0) {␊ |
817 | /* Success */␊ |
818 | bootArgs->apmConfig.cs32_base = (bb.eax.rr) << 4;␊ |
819 | bootArgs->apmConfig.entry_offset = bb.ebx.rx;␊ |
820 | bootArgs->apmConfig.cs16_base = (bb.ecx.rr) << 4;␊ |
821 | bootArgs->apmConfig.ds_base = (bb.edx.rr) << 4;␊ |
822 | if (bootArgs->apmConfig.major_vers >= 1 &&␊ |
823 | bootArgs->apmConfig.minor_vers >= 1) {␊ |
824 | bootArgs->apmConfig.cs_length = bb.esi.rr;␊ |
825 | bootArgs->apmConfig.ds_length = bb.edi.rr;␊ |
826 | } else {␊ |
827 | bootArgs->apmConfig.cs_length = ␊ |
828 | bootArgs->apmConfig.ds_length = 64 * 1024;␊ |
829 | }␊ |
830 | bootArgs->apmConfig.connected = 1;␊ |
831 | return 1;␊ |
832 | }␊ |
833 | return 0;␊ |
834 | }␊ |
835 | ␊ |
836 | #endif /* APM_SUPPORT */␊ |
837 | ␊ |
838 | #ifdef EISA_SUPPORT␊ |
839 | bool eisa_present(void)␊ |
840 | {␊ |
841 | static bool checked = false;␊ |
842 | static bool isEISA;␊ |
843 | ␊ |
844 | if (!checked) {␊ |
845 | if (strncmp((char *)0xfffd9, "EISA", 4) == 0)␊ |
846 | isEISA = true;␊ |
847 | ␊ |
848 | checked = true;␊ |
849 | }␊ |
850 | ␊ |
851 | return (isEISA);␊ |
852 | }␊ |
853 | ␊ |
854 | int␊ |
855 | ReadEISASlotInfo(EISA_slot_info_t *ep, int slot)␊ |
856 | {␊ |
857 | union {␊ |
858 | struct {␊ |
859 | unsigned char char2h :2;␊ |
860 | unsigned char char1 :5;␊ |
861 | unsigned char char3 :5;␊ |
862 | unsigned char char2l :3;␊ |
863 | unsigned char d2 :4;␊ |
864 | unsigned char d1 :4;␊ |
865 | unsigned char d4 :4;␊ |
866 | unsigned char d3 :4;␊ |
867 | } s;␊ |
868 | unsigned char data[4];␊ |
869 | } u;␊ |
870 | static char hex[0x10] = "0123456789ABCDEF";␊ |
871 | ␊ |
872 | ␊ |
873 | bb.intno = 0x15;␊ |
874 | bb.eax.r.h = 0xd8;␊ |
875 | bb.eax.r.l = 0x00;␊ |
876 | bb.ecx.r.l = slot;␊ |
877 | bios(&bb);␊ |
878 | if (bb.flags.cf)␊ |
879 | return bb.eax.r.h;␊ |
880 | ep->u_ID.d = bb.eax.r.l;␊ |
881 | ep->configMajor = bb.ebx.r.h;␊ |
882 | ep->configMinor = bb.ebx.r.l;␊ |
883 | ep->checksum = bb.ecx.rr;␊ |
884 | ep->numFunctions = bb.edx.r.h;␊ |
885 | ep->u_resources.d = bb.edx.r.l;␊ |
886 | u.data[0] = bb.edi.r.l;␊ |
887 | u.data[1] = bb.edi.r.h;␊ |
888 | u.data[2] = bb.esi.r.l;␊ |
889 | u.data[3] = bb.esi.r.h;␊ |
890 | ep->id[0] = u.s.char1 + ('A' - 1);␊ |
891 | ep->id[1] = (u.s.char2l | (u.s.char2h << 3)) + ('A' - 1);␊ |
892 | ep->id[2] = u.s.char3 + ('A' - 1);␊ |
893 | ep->id[3] = hex[u.s.d1];␊ |
894 | ep->id[4] = hex[u.s.d2];␊ |
895 | ep->id[5] = hex[u.s.d3];␊ |
896 | ep->id[6] = hex[u.s.d4];␊ |
897 | ep->id[7] = 0;␊ |
898 | return 0;␊ |
899 | }␊ |
900 | ␊ |
901 | /*␊ |
902 | * Note: ep must point to an address below 64k.␊ |
903 | */␊ |
904 | ␊ |
905 | int␊ |
906 | ReadEISAFuncInfo(EISA_func_info_t *ep, int slot, int function)␊ |
907 | {␊ |
908 | bb.intno = 0x15;␊ |
909 | bb.eax.r.h = 0xd8;␊ |
910 | bb.eax.r.l = 0x01;␊ |
911 | bb.ecx.r.l = slot;␊ |
912 | bb.ecx.r.h = function;␊ |
913 | bb.esi.rr = (unsigned int)ep->data;␊ |
914 | bios(&bb);␊ |
915 | if (bb.eax.r.h == 0) {␊ |
916 | ep->slot = slot;␊ |
917 | ep->function = function;␊ |
918 | }␊ |
919 | return bb.eax.r.h;␊ |
920 | }␊ |
921 | #endif /* EISA_SUPPORT */␊ |
922 | ␊ |
923 | #define PCI_SIGNATURE 0x20494350 /* "PCI " */␊ |
924 | ␊ |
925 | int␊ |
926 | ReadPCIBusInfo(PCI_bus_info_t *pp)␊ |
927 | {␊ |
928 | bb.intno = 0x1a;␊ |
929 | bb.eax.r.h = 0xb1;␊ |
930 | bb.eax.r.l = 0x01;␊ |
931 | bios(&bb);␊ |
932 | if ((bb.eax.r.h == 0) && (bb.edx.rx == PCI_SIGNATURE)) {␊ |
933 | pp->BIOSPresent = 1;␊ |
934 | pp->u_bus.d = bb.eax.r.l;␊ |
935 | pp->majorVersion = bb.ebx.r.h;␊ |
936 | pp->minorVersion = bb.ebx.r.l;␊ |
937 | pp->maxBusNum = bb.ecx.r.l;␊ |
938 | return 0;␊ |
939 | }␊ |
940 | return -1;␊ |
941 | }␊ |
942 | ␊ |
943 | void sleep(int n)␊ |
944 | {␊ |
945 | unsigned int endtime = (time18() + 18*n);␊ |
946 | while (time18() < endtime);␊ |
947 | }␊ |
948 | ␊ |
949 | void delay(int ms)␊ |
950 | {␊ |
951 | bb.intno = 0x15;␊ |
952 | bb.eax.r.h = 0x86;␊ |
953 | bb.ecx.rr = ms >> 16;␊ |
954 | bb.edx.rr = ms & 0xFFFF;␊ |
955 | bios(&bb);␊ |
956 | }␊ |
957 | ␊ |
958 | |