Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/biosfn.c

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
47static biosBuf_t bb;
48
49int 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
64int 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
76int readKeyboardShiftFlags(void)
77{
78 bb.intno = 0x16;
79 bb.eax.r.h = 0x02;
80 bios(&bb);
81 return bb.eax.r.l;
82}
83
84unsigned 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
103static unsigned long rerangeMemoryMap(unsigned long count)
104{
105int i, still_changing, newcount = count;
106
107MemoryRange * range = (MemoryRange *)BIOS_ADDR;
108struct MemoryRange change_tmp;
109
110/* sort map list by memory addresses (low -> high) */
111still_changing = 1;
112while (still_changing) {
113still_changing = 0;
114for (i=1; i<count; i++) {
115/* if <current_addr> > <last_addr>, swap */
116if (range[i].base < range[i-1].base) {
117change_tmp.base = range[i].base;
118change_tmp.length = range[i].length;
119change_tmp.type = range[i].type;
120
121range[i].base = range[i-1].base;
122range[i].length = range[i-1].length;
123range[i].type = range[i-1].type;
124
125range[i-1].base = change_tmp.base;
126range[i-1].length = change_tmp.length;
127range[i-1].type = change_tmp.type;
128
129still_changing=1;
130}
131}
132}
133
134/* clear overlaps */
135/* linux's arch/i386/kern/setup.c may have better algorithm */
136for (i=1; i<count; i++) {
137if ( range[i-1].base + range[i-1].length > range[i].base ) {
138range[newcount].base = range[i].base + range[i].length;
139range[newcount].length = range[i-1].base + range[i-1].length - range[newcount].base;
140range[newcount].type = range[i-1].type;
141newcount++;
142
143range[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 */
152range[newcount].base = 0xb0000000;
153range[newcount].length = 0x0f000000;
154range[newcount].type = kMemoryRangeUsable;
155newcount++;
156
157return newcount;
158}
159#endif
160
161unsigned 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 longcount = 0;
171 // unsigned longrerangedCount;
172 unsigned long longconMemSize = 0;
173 unsigned long longextMemSize = 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
264unsigned 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
315unsigned long getConventionalMemorySize()
316{
317 bb.intno = 0x12;
318 bios(&bb);
319 return bb.eax.rr; // kilobytes
320}
321
322void 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
330int 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
364int 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
406int 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
449void 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
459void 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. */
471int is_no_emulation(int drive)
472{
473struct packet {
474unsigned char packet_size;
475unsigned char media_type;
476unsigned char drive_num;
477unsigned char ctrlr_index;
478unsigned long lba;
479unsigned short device_spec;
480unsigned short buffer_segment;
481unsigned short load_segment;
482unsigned short sector_count;
483unsigned char cyl_count;
484unsigned char sec_count;
485unsigned char head_count;
486unsigned char reseved;
487} __attribute__((packed));
488static struct packet pkt;
489
490bzero(&pkt, sizeof(pkt));
491pkt.packet_size = 0x13;
492
493bb.intno= 0x13;
494bb.eax.r.h= 0x4b;
495bb.eax.r.l= 0x01; // subfunc: get info
496bb.edx.r.l= drive;
497bb.esi.rr= NORMALIZED_OFFSET((unsigned)&pkt);
498bb.ds= NORMALIZED_SEGMENT((unsigned)&pkt);
499
500bios(&bb);
501#if DEBUG
502printf("el_torito info drive %x\n", drive);
503
504printf("--> cf %x, eax %x\n", bb.flags.cf, bb.eax.rr);
505
506printf("pkt_size: %x\n", pkt.packet_size);
507printf("media_type: %x\n", pkt.media_type);
508printf("drive_num: %x\n", pkt.drive_num);
509printf("device_spec: %x\n", pkt.device_spec);
510printf("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. */
515if (pkt.drive_num == drive) {
516if ((pkt.media_type & 0x0F) == 0) {
517/* We are in no-emulation mode. */
518return 1;
519}
520}
521return 0;
522}
523
524#if DEBUG
525/*
526 * BIOS drive information.
527 */
528void print_drive_info(boot_drive_info_t *dp)
529{
530//printf("buf_size = %x\n", dp->params.buf_size);
531printf("info_flags = %x\n", dp->params.info_flags);
532printf("phys_cyls = %lx\n", dp->params. phys_cyls);
533printf("phys_heads = %lx\n", dp->params. phys_heads);
534printf("phys_spt = %lx\n", dp->params. phys_spt);
535printf("phys_sectors = %lx%lx\n", ((unsigned long *)(&dp->params.phys_sectors))[1],
536 ((unsigned long *)(&dp->params.phys_sectors))[0]);
537printf("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);
550printf("io_port_base = %x\n", dp->dpte.io_port_base);
551printf("control_port_base = %x\n", dp->dpte.control_port_base);
552printf("head_flags = %x\n", dp->dpte. head_flags);
553printf("vendor_info = %x\n", dp->dpte. vendor_info);
554printf("irq = %x\n", dp->dpte. irq);
555//printf("irq_unused = %x\n", dp->dpte. irq_unused);
556printf("block_count = %x\n", dp->dpte. block_count);
557printf("dma_channe = %x\n", dp->dpte. dma_channel);
558printf("dma_type = %x\n", dp->dpte. dma_type);
559printf("pio_type = %x\n", dp->dpte. pio_type);
560printf("pio_unused = %x\n", dp->dpte. pio_unused);
561printf("option_flags = %x\n", dp->dpte.option_flags);
562//printf("reserved = %x\n", dp->dpte.reserved);
563printf("revision = %x\n", dp->dpte. revision);
564//printf("checksum = %x\n", dp->dpte. checksum);
565}
566
567#endif
568
569int get_drive_info(int drive, struct driveInfo *dp)
570{
571boot_drive_info_t *di = &dp->di;
572int ret = 0;
573
574#if UNUSED
575if (maxhd == 0) {
576bb.intno = 0x13;
577bb.eax.r.h = 0x08;
578bb.edx.r.l = 0x80;
579bios(&bb);
580if (bb.flags.cf == 0)
581maxhd = 0x7f + bb.edx.r.l;
582};
583
584if (drive > maxhd)
585return 0;
586#endif
587
588bzero(dp, sizeof(struct driveInfo));
589dp->biosdev = drive;
590
591/* Check for El Torito no-emulation mode. */
592dp->no_emulation = is_no_emulation(drive);
593
594/* Check drive for EBIOS support. */
595bb.intno = 0x13;
596bb.eax.r.h = 0x41;
597bb.edx.r.l = drive;
598bb.ebx.rr = 0x55aa;
599bios(&bb);
600
601if ((bb.ebx.rr == 0xaa55) && (bb.flags.cf == 0)) {
602/* Get flags for supported operations. */
603dp->uses_ebios = bb.ecx.r.l;
604}
605
606if (dp->uses_ebios & (EBIOS_ENHANCED_DRIVE_INFO | EBIOS_LOCKING_ACCESS | EBIOS_FIXED_DISK_ACCESS)) {
607/* Get EBIOS drive info. */
608static struct drive_params params;
609
610params.buf_size = sizeof(params);
611bb.intno = 0x13;
612bb.eax.r.h = 0x48;
613bb.edx.r.l = drive;
614bb.esi.rr = NORMALIZED_OFFSET((unsigned)&params);
615bb.ds = NORMALIZED_SEGMENT((unsigned)&params);
616bios(&bb);
617
618if (bb.flags.cf != 0 /* || params.phys_sectors < 2097152 */) {
619dp->uses_ebios = 0;
620di->params.buf_size = 1;
621}
622else
623{
624bcopy(&params, &di->params, sizeof(params));
625
626if (drive >= BASE_HD_DRIVE &&
627(dp->uses_ebios & EBIOS_ENHANCED_DRIVE_INFO) &&
628di->params.buf_size >= 30 &&
629!(di->params.dpte_offset == 0xFFFF && di->params.dpte_segment == 0xFFFF)) {
630void *ptr = (void *)(di->params.dpte_offset + ((unsigned int)di->params.dpte_segment << 4));
631bcopy(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
665if (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 */
670dp->uses_ebios |= EBIOS_FIXED_DISK_ACCESS;
671}
672#if DEBUG
673print_drive_info(di);
674printf("uses_ebios = 0x%x\n", dp->uses_ebios);
675printf("result %d\n", ret);
676printf("press a key->\n");getc();
677#endif
678
679if (ret == 0) {
680dp->valid = 1;
681}
682return ret;
683}
684
685int 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
695void 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
705void setCursorType(int type)
706{
707 bb.intno = 0x10;
708 bb.eax.r.h = 0x01;
709 bb.ecx.rr = type;
710 bios(&bb);
711}
712
713void 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
723void 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
736void clearScreenRows( int y1, int y2 )
737{
738 scollPage( 0, y1, 80 - 1, y2, 0x07, y2 - y1 + 1, 1 );
739}
740
741void 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
751int 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
764int 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
788int
789APMPresent(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
808int
809APMConnect32(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
839bool 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
854int
855ReadEISASlotInfo(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
905int
906ReadEISAFuncInfo(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
925int
926ReadPCIBusInfo(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
943void sleep(int n)
944{
945 unsigned int endtime = (time18() + 18*n);
946 while (time18() < endtime);
947}
948
949void 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

Archive Download this file

Revision: 897