Debian Codegraph @

tools/firmware/rombios/rombios.c

raw
1 /////////////////////////////////////////////////////////////////////////
2 // $Id: rombios.c,v 1.221 2008/12/07 17:32:29 sshwarts Exp $
3 ////////////////////////////#/////////////////////////////////////////////
4 //
5 // Copyright (C) 2002 MandrakeSoft S.A.
6 //
7 // MandrakeSoft S.A.
8 // 43, rue d'Aboukir
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; If not, see <http://www.gnu.org/licenses/>.
25
26 // ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
27
28 #define uint8_t unsigned char
29 #define uint16_t unsigned short
30 #define uint32_t unsigned long
31 #include "config.h"
32
33 #define HVMASSIST
34 #undef HVMTEST
35
36 // Xen full virtualization does not handle unaligned IO with page crossing.
37 // Disable 32-bit PIO as a workaround.
38 #undef NO_PIO32
39
40
41 // ROM BIOS compatability entry points:
42 // ===================================
43 // $e05b ; POST Entry Point
44 // $e2c3 ; NMI Handler Entry Point
45 // $e3fe ; INT 13h Fixed Disk Services Entry Point
46 // $e401 ; Fixed Disk Parameter Table
47 // $e6f2 ; INT 19h Boot Load Service Entry Point
48 // $e6f5 ; Configuration Data Table
49 // $e729 ; Baud Rate Generator Table
50 // $e739 ; INT 14h Serial Communications Service Entry Point
51 // $e82e ; INT 16h Keyboard Service Entry Point
52 // $e987 ; INT 09h Keyboard Service Entry Point
53 // $ec59 ; INT 13h Diskette Service Entry Point
54 // $ef57 ; INT 0Eh Diskette Hardware ISR Entry Point
55 // $efc7 ; Diskette Controller Parameter Table
56 // $efd2 ; INT 17h Printer Service Entry Point
57 // $f045 ; INT 10 Functions 0-Fh Entry Point
58 // $f065 ; INT 10h Video Support Service Entry Point
59 // $f0a4 ; MDA/CGA Video Parameter Table (INT 1Dh)
60 // $f841 ; INT 12h Memory Size Service Entry Point
61 // $f84d ; INT 11h Equipment List Service Entry Point
62 // $f859 ; INT 15h System Services Entry Point
63 // $fa6e ; Character Font for 320x200 & 640x200 Graphics (lower 128 characters)
64 // $fe6e ; INT 1Ah Time-of-day Service Entry Point
65 // $fea5 ; INT 08h System Timer ISR Entry Point
66 // $fef3 ; Initial Interrupt Vector Offsets Loaded by POST
67 // $ff53 ; IRET Instruction for Dummy Interrupt Handler
68 // $ff54 ; INT 05h Print Screen Service Entry Point
69 // $fff0 ; Power-up Entry Point
70 // $fff5 ; ASCII Date ROM was built - 8 characters in MM/DD/YY
71 // $fffe ; System Model ID
72
73 // NOTES for ATA/ATAPI driver ([email protected])
74 // Features
75 // - supports up to 4 ATA interfaces
76 // - device/geometry detection
77 // - 16bits/32bits device access
78 // - pchs/lba access
79 // - datain/dataout/packet command support
80 //
81 // NOTES for El-Torito Boot ([email protected])
82 // - CD-ROM booting is only available if ATA/ATAPI Driver is available
83 // - Current code is only able to boot mono-session cds
84 // - Current code can not boot and emulate a hard-disk
85 // the bios will panic otherwise
86 // - Current code also use memory in EBDA segement.
87 // - I used cmos byte 0x3D to store extended information on boot-device
88 // - Code has to be modified modified to handle multiple cdrom drives
89 // - Here are the cdrom boot failure codes:
90 // 1 : no atapi device found
91 // 2 : no atapi cdrom found
92 // 3 : can not read cd - BRVD
93 // 4 : cd is not eltorito (BRVD)
94 // 5 : cd is not eltorito (ISO TAG)
95 // 6 : cd is not eltorito (ELTORITO TAG)
96 // 7 : can not read cd - boot catalog
97 // 8 : boot catalog : bad header
98 // 9 : boot catalog : bad platform
99 // 10 : boot catalog : bad signature
100 // 11 : boot catalog : bootable flag not set
101 // 12 : can not read cd - boot image
102 //
103 // ATA driver
104 // - EBDA segment.
105 // I used memory starting at 0x121 in the segment
106 // - the translation policy is defined in cmos regs 0x39 & 0x3a
107 //
108 // TODO :
109 //
110 // int74
111 // - needs to be reworked. Uses direct [bp] offsets. (?)
112 //
113 // int13:
114 // - f04 (verify sectors) isn't complete (?)
115 // - f02/03/04 should set current cyl,etc in BDA (?)
116 // - rewrite int13_relocated & clean up int13 entry code
117 //
118 // NOTES:
119 // - NMI access (bit7 of addr written to 70h)
120 //
121 // ATA driver
122 // - should handle the "don't detect" bit (cmos regs 0x3b & 0x3c)
123 // - could send the multiple-sector read/write commands
124 //
125 // El-Torito
126 // - Emulate a Hard-disk (currently only diskette can be emulated) see "FIXME ElTorito Harddisk"
127 // - Implement remaining int13_cdemu functions (as defined by El-Torito specs)
128 // - cdrom drive is hardcoded to ide 0 device 1 in several places. see "FIXME ElTorito Hardcoded"
129 // - int13 Fix DL when emulating a cd. In that case DL is decremented before calling real int13.
130 // This is ok. But DL should be reincremented afterwards.
131 // - Fix all "FIXME ElTorito Various"
132 // - should be able to boot any cdrom instead of the first one
133 //
134 // BCC Bug: find a generic way to handle the bug of #asm after an "if" (fixed in 0.16.7)
135
136 #include "rombios.h"
137
138 #define DEBUG_ATA 0
139 #define DEBUG_INT13_HD 0
140 #define DEBUG_INT13_CD 0
141 #define DEBUG_INT13_ET 0
142 #define DEBUG_INT13_FL 0
143 #define DEBUG_INT15 0
144 #define DEBUG_INT16 0
145 #define DEBUG_INT1A 0
146 #define DEBUG_INT74 0
147 #define DEBUG_APM 0
148
149 #define BX_CPU 3
150 #define BX_USE_PS2_MOUSE 1
151 #define BX_CALL_INT15_4F 1
152 #define BX_USE_EBDA 1
153 #define BX_SUPPORT_FLOPPY 1
154 #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
155 #define BX_PCIBIOS 1
156 #define BX_APM 1
157
158 #define BX_USE_ATADRV 1
159 #define BX_ELTORITO_BOOT 1
160
161 #define BX_TCGBIOS 0 /* main switch for TCG BIOS ext. */
162
163 #define BX_PMM 1 /* POST Memory Manager */
164
165 #define BX_MAX_ATA_INTERFACES 4
166 #define BX_MAX_ATA_DEVICES (BX_MAX_ATA_INTERFACES*2)
167
168 #define BX_VIRTUAL_PORTS 1 /* normal output to Bochs ports */
169 #define BX_DEBUG_SERIAL 0 /* output to COM1 */
170
171 /* model byte 0xFC = AT */
172 #define SYS_MODEL_ID 0xFC
173 #define SYS_SUBMODEL_ID 0x00
174 #define BIOS_REVISION 1
175 #define BIOS_CONFIG_TABLE 0xe6f5
176
177 #ifndef BIOS_BUILD_DATE
178 # define BIOS_BUILD_DATE "06/23/99"
179 #endif
180
181 #define E820_SEG (Bit16u)(E820_PHYSICAL_ADDRESS >> 4)
182
183 // 1K of base memory used for Extended Bios Data Area (EBDA)
184 // EBDA is used for PS/2 mouse support, and IDE BIOS, etc.
185 #define EBDA_SEG 0x9FC0
186 #define EBDA_SIZE 1 // In KiB
187 #define BASE_MEM_IN_K (640 - EBDA_SIZE)
188
189 /* 256 bytes at 0x9ff00 -- 0x9ffff is used for the IPL boot table. */
190 #define IPL_TABLE_OFFSET 0x0300 /* offset from EBDA */
191 #define IPL_TABLE_ENTRIES 8
192 #define IPL_COUNT_OFFSET 0x0380 /* u16: number of valid table entries */
193 #define IPL_SEQUENCE_OFFSET 0x0382 /* u16: next boot device */
194 #define IPL_BOOTFIRST_OFFSET 0x0384 /* u16: user selected device */
195 #define IPL_SIZE 0xff
196 #define IPL_TYPE_FLOPPY 0x01
197 #define IPL_TYPE_HARDDISK 0x02
198 #define IPL_TYPE_CDROM 0x03
199 #define IPL_TYPE_BEV 0x80
200
201
202 // Sanity Checks
203 #if BX_USE_ATADRV && BX_CPU<3
204 # error The ATA/ATAPI Driver can only to be used with a 386+ cpu
205 #endif
206 #if BX_USE_ATADRV && !BX_USE_EBDA
207 # error ATA/ATAPI Driver can only be used if EBDA is available
208 #endif
209 #if BX_ELTORITO_BOOT && !BX_USE_ATADRV
210 # error El-Torito Boot can only be use if ATA/ATAPI Driver is available
211 #endif
212 #if BX_PCIBIOS && BX_CPU<3
213 # error PCI BIOS can only be used with 386+ cpu
214 #endif
215 #if BX_APM && BX_CPU<3
216 # error APM BIOS can only be used with 386+ cpu
217 #endif
218
219 // define this if you want to make PCIBIOS working on a specific bridge only
220 // undef enables PCIBIOS when at least one PCI device is found
221 // i440FX is emulated by Bochs and QEMU
222 #define PCI_FIXED_HOST_BRIDGE 0x12378086 ;; i440FX PCI bridge
223
224 // #20 is dec 20
225 // #$20 is hex 20 = 32
226 // #0x20 is hex 20 = 32
227 // LDA #$20
228 // JSR $E820
229 // LDD .i,S
230 // JSR $C682
231 // mov al, #$20
232
233 // all hex literals should be prefixed with '0x'
234 // grep "#[0-9a-fA-F][0-9a-fA-F]" rombios.c
235 // no mov SEG-REG, #value, must mov register into seg-reg
236 // grep -i "mov[ ]*.s" rombios.c
237
238 // This is for compiling with gcc2 and gcc3
239 #define ASM_START #asm
240 #define ASM_END #endasm
241
242 ASM_START
243 .rom
244
245 .org 0x0000
246
247 #if BX_CPU >= 3
248 use16 386
249 #else
250 use16 286
251 #endif
252
253 MACRO HALT
254 ;; the HALT macro is called with the line number of the HALT call.
255 ;; The line number is then sent to the PANIC_PORT, causing Bochs/Plex
256 ;; to print a BX_PANIC message. This will normally halt the simulation
257 ;; with a message such as "BIOS panic at rombios.c, line 4091".
258 ;; However, users can choose to make panics non-fatal and continue.
259 #if BX_VIRTUAL_PORTS
260 mov dx,#PANIC_PORT
261 mov ax,#?1
262 out dx,ax
263 #else
264 mov dx,#0x80
265 mov ax,#?1
266 out dx,al
267 #endif
268 MEND
269
270 MACRO JMP_AP
271 db 0xea
272 dw ?2
273 dw ?1
274 MEND
275
276 MACRO SET_INT_VECTOR
277 mov ax, ?3
278 mov ?1*4, ax
279 mov ax, ?2
280 mov ?1*4+2, ax
281 MEND
282
283 ASM_END
284
285 typedef unsigned char Bit8u;
286 typedef unsigned short Bit16u;
287 typedef unsigned short bx_bool;
288 typedef unsigned long Bit32u;
289
290
291 void memsetb(seg,offset,value,count);
292 void memcpyb(dseg,doffset,sseg,soffset,count);
293 void memcpyd(dseg,doffset,sseg,soffset,count);
294
295 // memset of count bytes
296 void
297 memsetb(seg,offset,value,count)
298 Bit16u seg;
299 Bit16u offset;
300 Bit16u value;
301 Bit16u count;
302 {
303 ASM_START
304 push bp
305 mov bp, sp
306
307 push ax
308 push cx
309 push es
310 push di
311
312 mov cx, 10[bp] ; count
313 test cx, cx
314 je memsetb_end
315 mov ax, 4[bp] ; segment
316 mov es, ax
317 mov ax, 6[bp] ; offset
318 mov di, ax
319 mov al, 8[bp] ; value
320 cld
321 rep
322 stosb
323
324 memsetb_end:
325 pop di
326 pop es
327 pop cx
328 pop ax
329
330 pop bp
331 ASM_END
332 }
333
334 // memcpy of count bytes
335 void
336 memcpyb(dseg,doffset,sseg,soffset,count)
337 Bit16u dseg;
338 Bit16u doffset;
339 Bit16u sseg;
340 Bit16u soffset;
341 Bit16u count;
342 {
343 ASM_START
344 push bp
345 mov bp, sp
346
347 push ax
348 push cx
349 push es
350 push di
351 push ds
352 push si
353
354 mov cx, 12[bp] ; count
355 test cx, cx
356 je memcpyb_end
357 mov ax, 4[bp] ; dsegment
358 mov es, ax
359 mov ax, 6[bp] ; doffset
360 mov di, ax
361 mov ax, 8[bp] ; ssegment
362 mov ds, ax
363 mov ax, 10[bp] ; soffset
364 mov si, ax
365 cld
366 rep
367 movsb
368
369 memcpyb_end:
370 pop si
371 pop ds
372 pop di
373 pop es
374 pop cx
375 pop ax
376
377 pop bp
378 ASM_END
379 }
380
381 // memcpy of count dword
382 void
383 memcpyd(dseg,doffset,sseg,soffset,count)
384 Bit16u dseg;
385 Bit16u doffset;
386 Bit16u sseg;
387 Bit16u soffset;
388 Bit16u count;
389 {
390 ASM_START
391 push bp
392 mov bp, sp
393
394 push ax
395 push cx
396 push es
397 push di
398 push ds
399 push si
400
401 mov cx, 12[bp] ; count
402 test cx, cx
403 je memcpyd_end
404 mov ax, 4[bp] ; dsegment
405 mov es, ax
406 mov ax, 6[bp] ; doffset
407 mov di, ax
408 mov ax, 8[bp] ; ssegment
409 mov ds, ax
410 mov ax, 10[bp] ; soffset
411 mov si, ax
412 cld
413 rep
414 movsd
415
416 memcpyd_end:
417 pop si
418 pop ds
419 pop di
420 pop es
421 pop cx
422 pop ax
423
424 pop bp
425 ASM_END
426 }
427
428 // read_dword and write_dword functions
429 static Bit32u read_dword();
430 static void write_dword();
431
432 Bit32u
433 read_dword(seg, offset)
434 Bit16u seg;
435 Bit16u offset;
436 {
437 ASM_START
438 push bp
439 mov bp, sp
440
441 push bx
442 push ds
443 mov ax, 4[bp] ; segment
444 mov ds, ax
445 mov bx, 6[bp] ; offset
446 mov ax, [bx]
447 add bx, #2
448 mov dx, [bx]
449 ;; ax = return value (word)
450 ;; dx = return value (word)
451 pop ds
452 pop bx
453
454 pop bp
455 ASM_END
456 }
457
458 void
459 write_dword(seg, offset, data)
460 Bit16u seg;
461 Bit16u offset;
462 Bit32u data;
463 {
464 ASM_START
465 push bp
466 mov bp, sp
467
468 push ax
469 push bx
470 push ds
471 mov ax, 4[bp] ; segment
472 mov ds, ax
473 mov bx, 6[bp] ; offset
474 mov ax, 8[bp] ; data word
475 mov [bx], ax ; write data word
476 add bx, #2
477 mov ax, 10[bp] ; data word
478 mov [bx], ax ; write data word
479 pop ds
480 pop bx
481 pop ax
482
483 pop bp
484 ASM_END
485 }
486
487 // Bit32u (unsigned long) and long helper functions
488 ASM_START
489
490 ;; and function
491 landl:
492 landul:
493 SEG SS
494 and ax,[di]
495 SEG SS
496 and bx,2[di]
497 ret
498
499 ;; add function
500 laddl:
501 laddul:
502 SEG SS
503 add ax,[di]
504 SEG SS
505 adc bx,2[di]
506 ret
507
508 ;; cmp function
509 lcmpl:
510 lcmpul:
511 and eax, #0x0000FFFF
512 shl ebx, #16
513 or eax, ebx
514 shr ebx, #16
515 SEG SS
516 cmp eax, dword ptr [di]
517 ret
518
519 ;; sub function
520 lsubl:
521 lsubul:
522 SEG SS
523 sub ax,[di]
524 SEG SS
525 sbb bx,2[di]
526 ret
527
528 ;; mul function
529 lmull:
530 lmulul:
531 and eax, #0x0000FFFF
532 shl ebx, #16
533 or eax, ebx
534 SEG SS
535 mul eax, dword ptr [di]
536 mov ebx, eax
537 shr ebx, #16
538 ret
539
540 ;; dec function
541 ldecl:
542 ldecul:
543 SEG SS
544 dec dword ptr [bx]
545 ret
546
547 ;; or function
548 lorl:
549 lorul:
550 SEG SS
551 or ax,[di]
552 SEG SS
553 or bx,2[di]
554 ret
555
556 ;; inc function
557 lincl:
558 lincul:
559 SEG SS
560 inc dword ptr [bx]
561 ret
562
563 ;; tst function
564 ltstl:
565 ltstul:
566 and eax, #0x0000FFFF
567 shl ebx, #16
568 or eax, ebx
569 shr ebx, #16
570 test eax, eax
571 ret
572
573 ;; sr function
574 lsrul:
575 mov cx,di
576 jcxz lsr_exit
577 and eax, #0x0000FFFF
578 shl ebx, #16
579 or eax, ebx
580 lsr_loop:
581 shr eax, #1
582 loop lsr_loop
583 mov ebx, eax
584 shr ebx, #16
585 lsr_exit:
586 ret
587
588 ;; sl function
589 lsll:
590 lslul:
591 mov cx,di
592 jcxz lsl_exit
593 and eax, #0x0000FFFF
594 shl ebx, #16
595 or eax, ebx
596 lsl_loop:
597 shl eax, #1
598 loop lsl_loop
599 mov ebx, eax
600 shr ebx, #16
601 lsl_exit:
602 ret
603
604 idiv_:
605 cwd
606 idiv bx
607 ret
608
609 idiv_u:
610 xor dx,dx
611 div bx
612 ret
613
614 ldivul:
615 and eax, #0x0000FFFF
616 shl ebx, #16
617 or eax, ebx
618 xor edx, edx
619 SEG SS
620 mov bx, 2[di]
621 shl ebx, #16
622 SEG SS
623 mov bx, [di]
624 div ebx
625 mov ebx, eax
626 shr ebx, #16
627 ret
628
629 ASM_END
630
631 // for access to RAM area which is used by interrupt vectors
632 // and BIOS Data Area
633
634 typedef struct {
635 unsigned char filler1[0x400];
636 unsigned char filler2[0x6c];
637 Bit16u ticks_low;
638 Bit16u ticks_high;
639 Bit8u midnight_flag;
640 } bios_data_t;
641
642 #define BiosData ((bios_data_t *) 0)
643
644 #if BX_USE_ATADRV
645 typedef struct {
646 Bit16u heads; // # heads
647 Bit16u cylinders; // # cylinders
648 Bit16u spt; // # sectors / track
649 } chs_t;
650
651 // DPTE definition
652 typedef struct {
653 Bit16u iobase1;
654 Bit16u iobase2;
655 Bit8u prefix;
656 Bit8u unused;
657 Bit8u irq;
658 Bit8u blkcount;
659 Bit8u dma;
660 Bit8u pio;
661 Bit16u options;
662 Bit16u reserved;
663 Bit8u revision;
664 Bit8u checksum;
665 } dpte_t;
666
667 typedef struct {
668 Bit8u iface; // ISA or PCI
669 Bit16u iobase1; // IO Base 1
670 Bit16u iobase2; // IO Base 2
671 Bit8u irq; // IRQ
672 } ata_channel_t;
673
674 typedef struct {
675 Bit8u type; // Detected type of ata (ata/atapi/none/unknown)
676 Bit8u device; // Detected type of attached devices (hd/cd/none)
677 Bit8u removable; // Removable device flag
678 Bit8u lock; // Locks for removable devices
679 Bit8u mode; // transfer mode : PIO 16/32 bits - IRQ - ISADMA - PCIDMA
680 Bit16u blksize; // block size
681
682 Bit8u translation; // type of translation
683 chs_t lchs; // Logical CHS
684 chs_t pchs; // Physical CHS
685
686 Bit32u sectors_low; // Total sectors count
687 Bit32u sectors_high;
688 } ata_device_t;
689
690 typedef struct {
691 // ATA channels info
692 ata_channel_t channels[BX_MAX_ATA_INTERFACES];
693
694 // ATA devices info
695 ata_device_t devices[BX_MAX_ATA_DEVICES];
696 //
697 // map between (bios hd id - 0x80) and ata channels
698 Bit8u hdcount, hdidmap[BX_MAX_ATA_DEVICES];
699
700 // map between (bios cd id - 0xE0) and ata channels
701 Bit8u cdcount, cdidmap[BX_MAX_ATA_DEVICES];
702
703 // Buffer for DPTE table
704 dpte_t dpte;
705
706 // Count of transferred sectors and bytes
707 Bit16u trsfsectors;
708 Bit32u trsfbytes;
709
710 } ata_t;
711
712 #if BX_ELTORITO_BOOT
713 // ElTorito Device Emulation data
714 typedef struct {
715 Bit8u active;
716 Bit8u media;
717 Bit8u emulated_drive;
718 Bit8u controller_index;
719 Bit16u device_spec;
720 Bit32u ilba;
721 Bit16u buffer_segment;
722 Bit16u load_segment;
723 Bit16u sector_count;
724
725 // Virtual device
726 chs_t vdevice;
727 } cdemu_t;
728 #endif // BX_ELTORITO_BOOT
729
730 #define X(idx, ret, fn, arg...) ret fn ();
731 #include "32bitprotos.h"
732 #undef X
733
734 // for access to EBDA area
735 // The EBDA structure should conform to
736 // http://www.frontiernet.net/~fys/rombios.htm document
737 // I made the ata and cdemu structs begin at 0x121 in the EBDA seg
738 // EBDA must be at most 768 bytes; it lives at EBDA_SEG, and the boot
739 // device tables are at EBDA_SEG:IPL_TABLE_OFFSET
740 typedef struct {
741 unsigned char ebda_size;
742 unsigned char cmos_shutdown_status;
743 unsigned char filler1[0x3B];
744
745 // FDPT - Can be splitted in data members if needed
746 unsigned char fdpt0[0x10];
747 unsigned char fdpt1[0x10];
748
749 unsigned char filler2[0xC4];
750
751 // ATA Driver data
752 ata_t ata;
753
754 #if BX_ELTORITO_BOOT
755 // El Torito Emulation data
756 cdemu_t cdemu;
757 #endif // BX_ELTORITO_BOOT
758 } ebda_data_t;
759
760 #define EBDA_CMOS_SHUTDOWN_STATUS_OFFSET 1
761 #define EbdaData ((ebda_data_t *) 0)
762
763 // for access to the int13ext structure
764 typedef struct {
765 Bit8u size;
766 Bit8u reserved;
767 Bit16u count;
768 Bit16u offset;
769 Bit16u segment;
770 Bit32u lba1;
771 Bit32u lba2;
772 } int13ext_t;
773
774 #define Int13Ext ((int13ext_t *) 0)
775
776 // Disk Physical Table definition
777 typedef struct {
778 Bit16u size;
779 Bit16u infos;
780 Bit32u cylinders;
781 Bit32u heads;
782 Bit32u spt;
783 Bit32u sector_count1;
784 Bit32u sector_count2;
785 Bit16u blksize;
786 Bit16u dpte_offset;
787 Bit16u dpte_segment;
788 Bit16u key;
789 Bit8u dpi_length;
790 Bit8u reserved1;
791 Bit16u reserved2;
792 Bit8u host_bus[4];
793 Bit8u iface_type[8];
794 Bit8u iface_path[8];
795 Bit8u device_path[8];
796 Bit8u reserved3;
797 Bit8u checksum;
798 } dpt_t;
799
800 #define Int13DPT ((dpt_t *) 0)
801
802 #endif // BX_USE_ATADRV
803
804 typedef struct {
805 union {
806 struct {
807 Bit16u di, si, bp, sp;
808 Bit16u bx, dx, cx, ax;
809 } r16;
810 struct {
811 Bit16u filler[4];
812 Bit8u bl, bh, dl, dh, cl, ch, al, ah;
813 } r8;
814 } u;
815 } pusha_regs_t;
816
817 typedef struct {
818 union {
819 struct {
820 Bit32u edi, esi, ebp, esp;
821 Bit32u ebx, edx, ecx, eax;
822 } r32;
823 struct {
824 Bit16u di, filler1, si, filler2, bp, filler3, sp, filler4;
825 Bit16u bx, filler5, dx, filler6, cx, filler7, ax, filler8;
826 } r16;
827 struct {
828 Bit32u filler[4];
829 Bit8u bl, bh;
830 Bit16u filler1;
831 Bit8u dl, dh;
832 Bit16u filler2;
833 Bit8u cl, ch;
834 Bit16u filler3;
835 Bit8u al, ah;
836 Bit16u filler4;
837 } r8;
838 } u;
839 } pushad_regs_t;
840
841 typedef struct {
842 union {
843 struct {
844 Bit16u flags;
845 } r16;
846 struct {
847 Bit8u flagsl;
848 Bit8u flagsh;
849 } r8;
850 } u;
851 } flags_t;
852
853 #define SetCF(x) x.u.r8.flagsl |= 0x01
854 #define SetZF(x) x.u.r8.flagsl |= 0x40
855 #define ClearCF(x) x.u.r8.flagsl &= 0xfe
856 #define ClearZF(x) x.u.r8.flagsl &= 0xbf
857 #define GetCF(x) (x.u.r8.flagsl & 0x01)
858
859 typedef struct {
860 Bit16u ip;
861 Bit16u cs;
862 flags_t flags;
863 } iret_addr_t;
864
865 typedef struct {
866 Bit16u type;
867 Bit16u flags;
868 Bit32u vector;
869 Bit32u description;
870 Bit32u reserved;
871 } ipl_entry_t;
872
873
874
875 static Bit8u inb();
876 static Bit8u inb_cmos();
877 static void outb();
878 static void outb_cmos();
879 static Bit16u inw();
880 static void outw();
881 static void init_rtc();
882 static bx_bool rtc_updating();
883
884 static Bit8u read_byte();
885 static Bit16u read_word();
886 static void write_byte();
887 static void write_word();
888 static void bios_printf();
889
890 static Bit8u inhibit_mouse_int_and_events();
891 static void enable_mouse_int_and_events();
892 static Bit8u send_to_mouse_ctrl();
893 static Bit8u get_mouse_data();
894 static void set_kbd_command_byte();
895
896 static void int09_function();
897 static void int13_harddisk();
898 static void int13_cdrom();
899 static void int13_cdemu();
900 static void int13_eltorito();
901 static void int13_diskette_function();
902 static void int14_function();
903 static void int15_function();
904 static void int16_function();
905 static void int17_function();
906 static void int18_function();
907 static void int1a_function();
908 static void int70_function();
909 static void int74_function();
910 static Bit16u get_CS();
911 static Bit16u get_SS();
912 static unsigned int enqueue_key();
913 static unsigned int dequeue_key();
914 static void get_hd_geometry();
915 static void set_diskette_ret_status();
916 static void set_diskette_current_cyl();
917 static void determine_floppy_media();
918 static bx_bool floppy_drive_exists();
919 static bx_bool floppy_drive_recal();
920 static bx_bool floppy_media_known();
921 static bx_bool floppy_media_sense();
922 static bx_bool set_enable_a20();
923 static void debugger_on();
924 static void debugger_off();
925 static void keyboard_init();
926 static void keyboard_panic();
927 static void shutdown_status_panic();
928 static void nmi_handler_msg();
929 static void delay_ticks();
930 static void delay_ticks_and_check_for_keystroke();
931
932 static void interactive_bootkey();
933 static void print_bios_banner();
934 static void print_boot_device();
935 static void print_boot_failure();
936 static void print_cdromboot_failure();
937
938 # if BX_USE_ATADRV
939
940 // ATA / ATAPI driver
941 void ata_init();
942 void ata_detect();
943 void ata_reset();
944
945 Bit16u ata_cmd_non_data();
946 Bit16u ata_cmd_data_in();
947 Bit16u ata_cmd_data_out();
948 Bit16u ata_cmd_packet();
949
950 Bit16u atapi_get_sense();
951 Bit16u atapi_is_ready();
952 Bit16u atapi_is_cdrom();
953
954 #endif // BX_USE_ATADRV
955
956 #if BX_ELTORITO_BOOT
957
958 void cdemu_init();
959 Bit8u cdemu_isactive();
960 Bit8u cdemu_emulated_drive();
961
962 Bit16u cdrom_boot();
963
964 #endif // BX_ELTORITO_BOOT
965
966 static char bios_cvs_version_string[] = "$Revision: 1.221 $ $Date: 2008/12/07 17:32:29 $";
967
968 #define BIOS_COPYRIGHT_STRING "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
969
970 #if DEBUG_ATA
971 # define BX_DEBUG_ATA(a...) BX_DEBUG(a)
972 #else
973 # define BX_DEBUG_ATA(a...)
974 #endif
975 #if DEBUG_INT13_HD
976 # define BX_DEBUG_INT13_HD(a...) BX_DEBUG(a)
977 #else
978 # define BX_DEBUG_INT13_HD(a...)
979 #endif
980 #if DEBUG_INT13_CD
981 # define BX_DEBUG_INT13_CD(a...) BX_DEBUG(a)
982 #else
983 # define BX_DEBUG_INT13_CD(a...)
984 #endif
985 #if DEBUG_INT13_ET
986 # define BX_DEBUG_INT13_ET(a...) BX_DEBUG(a)
987 #else
988 # define BX_DEBUG_INT13_ET(a...)
989 #endif
990 #if DEBUG_INT13_FL
991 # define BX_DEBUG_INT13_FL(a...) BX_DEBUG(a)
992 #else
993 # define BX_DEBUG_INT13_FL(a...)
994 #endif
995 #if DEBUG_INT15
996 # define BX_DEBUG_INT15(a...) BX_DEBUG(a)
997 #else
998 # define BX_DEBUG_INT15(a...)
999 #endif
1000 #if DEBUG_INT16
1001 # define BX_DEBUG_INT16(a...) BX_DEBUG(a)
1002 #else
1003 # define BX_DEBUG_INT16(a...)
1004 #endif
1005 #if DEBUG_INT1A
1006 # define BX_DEBUG_INT1A(a...) BX_DEBUG(a)
1007 #else
1008 # define BX_DEBUG_INT1A(a...)
1009 #endif
1010 #if DEBUG_INT74
1011 # define BX_DEBUG_INT74(a...) BX_DEBUG(a)
1012 #else
1013 # define BX_DEBUG_INT74(a...)
1014 #endif
1015
1016 #define SET_AL(val8) AX = ((AX & 0xff00) | (val8))
1017 #define SET_BL(val8) BX = ((BX & 0xff00) | (val8))
1018 #define SET_CL(val8) CX = ((CX & 0xff00) | (val8))
1019 #define SET_DL(val8) DX = ((DX & 0xff00) | (val8))
1020 #define SET_AH(val8) AX = ((AX & 0x00ff) | ((val8) << 8))
1021 #define SET_BH(val8) BX = ((BX & 0x00ff) | ((val8) << 8))
1022 #define SET_CH(val8) CX = ((CX & 0x00ff) | ((val8) << 8))
1023 #define SET_DH(val8) DX = ((DX & 0x00ff) | ((val8) << 8))
1024
1025 #define GET_AL() ( AX & 0x00ff )
1026 #define GET_BL() ( BX & 0x00ff )
1027 #define GET_CL() ( CX & 0x00ff )
1028 #define GET_DL() ( DX & 0x00ff )
1029 #define GET_AH() ( AX >> 8 )
1030 #define GET_BH() ( BX >> 8 )
1031 #define GET_CH() ( CX >> 8 )
1032 #define GET_DH() ( DX >> 8 )
1033
1034 #define GET_ELDL() ( ELDX & 0x00ff )
1035 #define GET_ELDH() ( ELDX >> 8 )
1036
1037 #define SET_CF() FLAGS |= 0x0001
1038 #define CLEAR_CF() FLAGS &= 0xfffe
1039 #define GET_CF() (FLAGS & 0x0001)
1040
1041 #define SET_ZF() FLAGS |= 0x0040
1042 #define CLEAR_ZF() FLAGS &= 0xffbf
1043 #define GET_ZF() (FLAGS & 0x0040)
1044
1045 #define UNSUPPORTED_FUNCTION 0x86
1046
1047 #define none 0
1048 #define MAX_SCAN_CODE 0x58
1049
1050 static struct {
1051 Bit16u normal;
1052 Bit16u shift;
1053 Bit16u control;
1054 Bit16u alt;
1055 Bit8u lock_flags;
1056 } scan_to_scanascii[MAX_SCAN_CODE + 1] = {
1057 { none, none, none, none, none },
1058 { 0x011b, 0x011b, 0x011b, 0x0100, none }, /* escape */
1059 { 0x0231, 0x0221, none, 0x7800, none }, /* 1! */
1060 { 0x0332, 0x0340, 0x0300, 0x7900, none }, /* 2@ */
1061 { 0x0433, 0x0423, none, 0x7a00, none }, /* 3# */
1062 { 0x0534, 0x0524, none, 0x7b00, none }, /* 4$ */
1063 { 0x0635, 0x0625, none, 0x7c00, none }, /* 5% */
1064 { 0x0736, 0x075e, 0x071e, 0x7d00, none }, /* 6^ */
1065 { 0x0837, 0x0826, none, 0x7e00, none }, /* 7& */
1066 { 0x0938, 0x092a, none, 0x7f00, none }, /* 8* */
1067 { 0x0a39, 0x0a28, none, 0x8000, none }, /* 9( */
1068 { 0x0b30, 0x0b29, none, 0x8100, none }, /* 0) */
1069 { 0x0c2d, 0x0c5f, 0x0c1f, 0x8200, none }, /* -_ */
1070 { 0x0d3d, 0x0d2b, none, 0x8300, none }, /* =+ */
1071 { 0x0e08, 0x0e08, 0x0e7f, none, none }, /* backspace */
1072 { 0x0f09, 0x0f00, none, none, none }, /* tab */
1073 { 0x1071, 0x1051, 0x1011, 0x1000, 0x40 }, /* Q */
1074 { 0x1177, 0x1157, 0x1117, 0x1100, 0x40 }, /* W */
1075 { 0x1265, 0x1245, 0x1205, 0x1200, 0x40 }, /* E */
1076 { 0x1372, 0x1352, 0x1312, 0x1300, 0x40 }, /* R */
1077 { 0x1474, 0x1454, 0x1414, 0x1400, 0x40 }, /* T */
1078 { 0x1579, 0x1559, 0x1519, 0x1500, 0x40 }, /* Y */
1079 { 0x1675, 0x1655, 0x1615, 0x1600, 0x40 }, /* U */
1080 { 0x1769, 0x1749, 0x1709, 0x1700, 0x40 }, /* I */
1081 { 0x186f, 0x184f, 0x180f, 0x1800, 0x40 }, /* O */
1082 { 0x1970, 0x1950, 0x1910, 0x1900, 0x40 }, /* P */
1083 { 0x1a5b, 0x1a7b, 0x1a1b, none, none }, /* [{ */
1084 { 0x1b5d, 0x1b7d, 0x1b1d, none, none }, /* ]} */
1085 { 0x1c0d, 0x1c0d, 0x1c0a, none, none }, /* Enter */
1086 { none, none, none, none, none }, /* L Ctrl */
1087 { 0x1e61, 0x1e41, 0x1e01, 0x1e00, 0x40 }, /* A */
1088 { 0x1f73, 0x1f53, 0x1f13, 0x1f00, 0x40 }, /* S */
1089 { 0x2064, 0x2044, 0x2004, 0x2000, 0x40 }, /* D */
1090 { 0x2166, 0x2146, 0x2106, 0x2100, 0x40 }, /* F */
1091 { 0x2267, 0x2247, 0x2207, 0x2200, 0x40 }, /* G */
1092 { 0x2368, 0x2348, 0x2308, 0x2300, 0x40 }, /* H */
1093 { 0x246a, 0x244a, 0x240a, 0x2400, 0x40 }, /* J */
1094 { 0x256b, 0x254b, 0x250b, 0x2500, 0x40 }, /* K */
1095 { 0x266c, 0x264c, 0x260c, 0x2600, 0x40 }, /* L */
1096 { 0x273b, 0x273a, none, none, none }, /* ;: */
1097 { 0x2827, 0x2822, none, none, none }, /* '" */
1098 { 0x2960, 0x297e, none, none, none }, /* `~ */
1099 { none, none, none, none, none }, /* L shift */
1100 { 0x2b5c, 0x2b7c, 0x2b1c, none, none }, /* |\ */
1101 { 0x2c7a, 0x2c5a, 0x2c1a, 0x2c00, 0x40 }, /* Z */
1102 { 0x2d78, 0x2d58, 0x2d18, 0x2d00, 0x40 }, /* X */
1103 { 0x2e63, 0x2e43, 0x2e03, 0x2e00, 0x40 }, /* C */
1104 { 0x2f76, 0x2f56, 0x2f16, 0x2f00, 0x40 }, /* V */
1105 { 0x3062, 0x3042, 0x3002, 0x3000, 0x40 }, /* B */
1106 { 0x316e, 0x314e, 0x310e, 0x3100, 0x40 }, /* N */
1107 { 0x326d, 0x324d, 0x320d, 0x3200, 0x40 }, /* M */
1108 { 0x332c, 0x333c, none, none, none }, /* ,< */
1109 { 0x342e, 0x343e, none, none, none }, /* .> */
1110 { 0x352f, 0x353f, none, none, none }, /* /? */
1111 { none, none, none, none, none }, /* R Shift */
1112 { 0x372a, 0x372a, none, none, none }, /* * */
1113 { none, none, none, none, none }, /* L Alt */
1114 { 0x3920, 0x3920, 0x3920, 0x3920, none }, /* space */
1115 { none, none, none, none, none }, /* caps lock */
1116 { 0x3b00, 0x5400, 0x5e00, 0x6800, none }, /* F1 */
1117 { 0x3c00, 0x5500, 0x5f00, 0x6900, none }, /* F2 */
1118 { 0x3d00, 0x5600, 0x6000, 0x6a00, none }, /* F3 */
1119 { 0x3e00, 0x5700, 0x6100, 0x6b00, none }, /* F4 */
1120 { 0x3f00, 0x5800, 0x6200, 0x6c00, none }, /* F5 */
1121 { 0x4000, 0x5900, 0x6300, 0x6d00, none }, /* F6 */
1122 { 0x4100, 0x5a00, 0x6400, 0x6e00, none }, /* F7 */
1123 { 0x4200, 0x5b00, 0x6500, 0x6f00, none }, /* F8 */
1124 { 0x4300, 0x5c00, 0x6600, 0x7000, none }, /* F9 */
1125 { 0x4400, 0x5d00, 0x6700, 0x7100, none }, /* F10 */
1126 { none, none, none, none, none }, /* Num Lock */
1127 { none, none, none, none, none }, /* Scroll Lock */
1128 { 0x4700, 0x4737, 0x7700, none, 0x20 }, /* 7 Home */
1129 { 0x4800, 0x4838, none, none, 0x20 }, /* 8 UP */
1130 { 0x4900, 0x4939, 0x8400, none, 0x20 }, /* 9 PgUp */
1131 { 0x4a2d, 0x4a2d, none, none, none }, /* - */
1132 { 0x4b00, 0x4b34, 0x7300, none, 0x20 }, /* 4 Left */
1133 { 0x4c00, 0x4c35, none, none, 0x20 }, /* 5 */
1134 { 0x4d00, 0x4d36, 0x7400, none, 0x20 }, /* 6 Right */
1135 { 0x4e2b, 0x4e2b, none, none, none }, /* + */
1136 { 0x4f00, 0x4f31, 0x7500, none, 0x20 }, /* 1 End */
1137 { 0x5000, 0x5032, none, none, 0x20 }, /* 2 Down */
1138 { 0x5100, 0x5133, 0x7600, none, 0x20 }, /* 3 PgDn */
1139 { 0x5200, 0x5230, none, none, 0x20 }, /* 0 Ins */
1140 { 0x5300, 0x532e, none, none, 0x20 }, /* Del */
1141 { none, none, none, none, none },
1142 { none, none, none, none, none },
1143 { 0x565c, 0x567c, none, none, none }, /* \| */
1144 { 0x8500, 0x8700, 0x8900, 0x8b00, none }, /* F11 */
1145 { 0x8600, 0x8800, 0x8a00, 0x8c00, none }, /* F12 */
1146 };
1147
1148 Bit8u
1149 inb(port)
1150 Bit16u port;
1151 {
1152 ASM_START
1153 push bp
1154 mov bp, sp
1155
1156 push dx
1157 mov dx, 4[bp]
1158 in al, dx
1159 pop dx
1160
1161 pop bp
1162 ASM_END
1163 }
1164
1165 #if BX_USE_ATADRV
1166 Bit16u
1167 inw(port)
1168 Bit16u port;
1169 {
1170 ASM_START
1171 push bp
1172 mov bp, sp
1173
1174 push dx
1175 mov dx, 4[bp]
1176 in ax, dx
1177 pop dx
1178
1179 pop bp
1180 ASM_END
1181 }
1182 #endif
1183
1184 void
1185 outb(port, val)
1186 Bit16u port;
1187 Bit8u val;
1188 {
1189 ASM_START
1190 push bp
1191 mov bp, sp
1192
1193 push ax
1194 push dx
1195 mov dx, 4[bp]
1196 mov al, 6[bp]
1197 out dx, al
1198 pop dx
1199 pop ax
1200
1201 pop bp
1202 ASM_END
1203 }
1204
1205 #if BX_USE_ATADRV
1206 void
1207 outw(port, val)
1208 Bit16u port;
1209 Bit16u val;
1210 {
1211 ASM_START
1212 push bp
1213 mov bp, sp
1214
1215 push ax
1216 push dx
1217 mov dx, 4[bp]
1218 mov ax, 6[bp]
1219 out dx, ax
1220 pop dx
1221 pop ax
1222
1223 pop bp
1224 ASM_END
1225 }
1226 #endif
1227
1228 void
1229 outb_cmos(cmos_reg, val)
1230 Bit8u cmos_reg;
1231 Bit8u val;
1232 {
1233 ASM_START
1234 push bp
1235 mov bp, sp
1236
1237 mov al, 4[bp] ;; cmos_reg
1238 out 0x70, al
1239 mov al, 6[bp] ;; val
1240 out 0x71, al
1241
1242 pop bp
1243 ASM_END
1244 }
1245
1246 Bit8u
1247 inb_cmos(cmos_reg)
1248 Bit8u cmos_reg;
1249 {
1250 ASM_START
1251 push bp
1252 mov bp, sp
1253
1254 mov al, 4[bp] ;; cmos_reg
1255 out 0x70, al
1256 in al, 0x71
1257
1258 pop bp
1259 ASM_END
1260 }
1261
1262 void
1263 init_rtc()
1264 {
1265 outb_cmos(0x0a, 0x26);
1266 outb_cmos(0x0b, 0x02);
1267 inb_cmos(0x0c);
1268 inb_cmos(0x0d);
1269 }
1270
1271 bx_bool
1272 rtc_updating()
1273 {
1274 // This function checks to see if the update-in-progress bit
1275 // is set in CMOS Status Register A. If not, it returns 0.
1276 // If it is set, it tries to wait until there is a transition
1277 // to 0, and will return 0 if such a transition occurs. A 1
1278 // is returned only after timing out. The maximum period
1279 // that this bit should be set is constrained to 244useconds.
1280 // The count I use below guarantees coverage or more than
1281 // this time, with any reasonable IPS setting.
1282
1283 Bit16u count;
1284
1285 count = 25000;
1286 while (--count != 0) {
1287 if ( (inb_cmos(0x0a) & 0x80) == 0 )
1288 return(0);
1289 }
1290 return(1); // update-in-progress never transitioned to 0
1291 }
1292
1293
1294 Bit8u
1295 read_byte(seg, offset)
1296 Bit16u seg;
1297 Bit16u offset;
1298 {
1299 ASM_START
1300 push bp
1301 mov bp, sp
1302
1303 push bx
1304 push ds
1305 mov ax, 4[bp] ; segment
1306 mov ds, ax
1307 mov bx, 6[bp] ; offset
1308 mov al, [bx]
1309 ;; al = return value (byte)
1310 pop ds
1311 pop bx
1312
1313 pop bp
1314 ASM_END
1315 }
1316
1317 Bit16u
1318 read_word(seg, offset)
1319 Bit16u seg;
1320 Bit16u offset;
1321 {
1322 ASM_START
1323 push bp
1324 mov bp, sp
1325
1326 push bx
1327 push ds
1328 mov ax, 4[bp] ; segment
1329 mov ds, ax
1330 mov bx, 6[bp] ; offset
1331 mov ax, [bx]
1332 ;; ax = return value (word)
1333 pop ds
1334 pop bx
1335
1336 pop bp
1337 ASM_END
1338 }
1339
1340 void
1341 write_byte(seg, offset, data)
1342 Bit16u seg;
1343 Bit16u offset;
1344 Bit8u data;
1345 {
1346 ASM_START
1347 push bp
1348 mov bp, sp
1349
1350 push ax
1351 push bx
1352 push ds
1353 mov ax, 4[bp] ; segment
1354 mov ds, ax
1355 mov bx, 6[bp] ; offset
1356 mov al, 8[bp] ; data byte
1357 mov [bx], al ; write data byte
1358 pop ds
1359 pop bx
1360 pop ax
1361
1362 pop bp
1363 ASM_END
1364 }
1365
1366 void
1367 write_word(seg, offset, data)
1368 Bit16u seg;
1369 Bit16u offset;
1370 Bit16u data;
1371 {
1372 ASM_START
1373 push bp
1374 mov bp, sp
1375
1376 push ax
1377 push bx
1378 push ds
1379 mov ax, 4[bp] ; segment
1380 mov ds, ax
1381 mov bx, 6[bp] ; offset
1382 mov ax, 8[bp] ; data word
1383 mov [bx], ax ; write data word
1384 pop ds
1385 pop bx
1386 pop ax
1387
1388 pop bp
1389 ASM_END
1390 }
1391
1392 Bit16u
1393 get_CS()
1394 {
1395 ASM_START
1396 mov ax, cs
1397 ASM_END
1398 }
1399
1400 Bit16u
1401 get_SS()
1402 {
1403 ASM_START
1404 mov ax, ss
1405 ASM_END
1406 }
1407
1408 #ifdef HVMASSIST
1409 void
1410 fixup_base_mem_in_k()
1411 {
1412 /* Report the proper base memory size at address 0x0413: otherwise
1413 * non-e820 code will clobber things if BASE_MEM_IN_K is bigger than
1414 * the first e820 entry. Get the size by reading the second 64bit
1415 * field of the first e820 slot. */
1416 Bit32u base_mem = read_dword(E820_SEG, E820_OFFSET + 8);
1417 write_word(0x40, 0x13, base_mem >> 10);
1418 }
1419
1420 void enable_rom_write_access()
1421 {
1422 outb(XEN_PF_IOBASE, 0);
1423 }
1424
1425 void disable_rom_write_access()
1426 {
1427 outb(XEN_PF_IOBASE, PFFLAG_ROM_LOCK);
1428 }
1429
1430 #endif /* HVMASSIST */
1431
1432 #if BX_DEBUG_SERIAL
1433 /* serial debug port*/
1434 #define BX_DEBUG_PORT 0x03f8
1435
1436 /* data */
1437 #define UART_RBR 0x00
1438 #define UART_THR 0x00
1439
1440 /* control */
1441 #define UART_IER 0x01
1442 #define UART_IIR 0x02
1443 #define UART_FCR 0x02
1444 #define UART_LCR 0x03
1445 #define UART_MCR 0x04
1446 #define UART_DLL 0x00
1447 #define UART_DLM 0x01
1448
1449 /* status */
1450 #define UART_LSR 0x05
1451 #define UART_MSR 0x06
1452 #define UART_SCR 0x07
1453
1454 int uart_can_tx_byte(base_port)
1455 Bit16u base_port;
1456 {
1457 return inb(base_port + UART_LSR) & 0x20;
1458 }
1459
1460 void uart_wait_to_tx_byte(base_port)
1461 Bit16u base_port;
1462 {
1463 while (!uart_can_tx_byte(base_port));
1464 }
1465
1466 void uart_wait_until_sent(base_port)
1467 Bit16u base_port;
1468 {
1469 while (!(inb(base_port + UART_LSR) & 0x40));
1470 }
1471
1472 void uart_tx_byte(base_port, data)
1473 Bit16u base_port;
1474 Bit8u data;
1475 {
1476 uart_wait_to_tx_byte(base_port);
1477 outb(base_port + UART_THR, data);
1478 uart_wait_until_sent(base_port);
1479 }
1480 #endif
1481
1482 void
1483 wrch(c)
1484 Bit8u c;
1485 {
1486 ASM_START
1487 push bp
1488 mov bp, sp
1489
1490 push bx
1491 mov ah, #0x0e
1492 mov al, 4[bp]
1493 xor bx,bx
1494 int #0x10
1495 pop bx
1496
1497 pop bp
1498 ASM_END
1499 }
1500
1501 void
1502 send(action, c)
1503 Bit16u action;
1504 Bit8u c;
1505 {
1506 #if BX_DEBUG_SERIAL
1507 if (c == '\n') uart_tx_byte(BX_DEBUG_PORT, '\r');
1508 uart_tx_byte(BX_DEBUG_PORT, c);
1509 #endif
1510 #ifdef HVMASSIST
1511 outb(0xE9, c);
1512 #endif
1513 #if BX_VIRTUAL_PORTS
1514 if (action & BIOS_PRINTF_DEBUG) outb(DEBUG_PORT, c);
1515 if (action & BIOS_PRINTF_INFO) outb(INFO_PORT, c);
1516 #endif
1517 if (action & BIOS_PRINTF_SCREEN) {
1518 if (c == '\n') wrch('\r');
1519 wrch(c);
1520 }
1521 }
1522
1523 void
1524 put_int(action, val, width, neg)
1525 Bit16u action;
1526 short val, width;
1527 bx_bool neg;
1528 {
1529 short nval = val / 10;
1530 if (nval)
1531 put_int(action, nval, width - 1, neg);
1532 else {
1533 while (--width > 0) send(action, ' ');
1534 if (neg) send(action, '-');
1535 }
1536 send(action, val - (nval * 10) + '0');
1537 }
1538
1539 void
1540 put_uint(action, val, width, neg)
1541 Bit16u action;
1542 unsigned short val;
1543 short width;
1544 bx_bool neg;
1545 {
1546 unsigned short nval = val / 10;
1547 if (nval)
1548 put_uint(action, nval, width - 1, neg);
1549 else {
1550 while (--width > 0) send(action, ' ');
1551 if (neg) send(action, '-');
1552 }
1553 send(action, val - (nval * 10) + '0');
1554 }
1555
1556 void
1557 put_luint(action, val, width, neg)
1558 Bit16u action;
1559 unsigned long val;
1560 short width;
1561 bx_bool neg;
1562 {
1563 unsigned long nval = val / 10;
1564 if (nval)
1565 put_luint(action, nval, width - 1, neg);
1566 else {
1567 while (--width > 0) send(action, ' ');
1568 if (neg) send(action, '-');
1569 }
1570 send(action, val - (nval * 10) + '0');
1571 }
1572
1573 void put_str(action, segment, offset)
1574 Bit16u action;
1575 Bit16u segment;
1576 Bit16u offset;
1577 {
1578 Bit8u c;
1579
1580 while (c = read_byte(segment, offset)) {
1581 send(action, c);
1582 offset++;
1583 }
1584 }
1585
1586 void
1587 delay_ticks(ticks)
1588 Bit16u ticks;
1589 {
1590 long ticks_to_wait, delta;
1591 Bit32u prev_ticks, t;
1592
1593 /*
1594 * The 0:046c wraps around at 'midnight' according to a 18.2Hz clock.
1595 * We also have to be careful about interrupt storms.
1596 */
1597 ASM_START
1598 pushf
1599 sti
1600 ASM_END
1601 ticks_to_wait = ticks;
1602 prev_ticks = read_dword(0x0, 0x46c);
1603 do
1604 {
1605 ASM_START
1606 hlt
1607 ASM_END
1608 t = read_dword(0x0, 0x46c);
1609 if (t > prev_ticks)
1610 {
1611 delta = t - prev_ticks; /* The temp var is required or bcc screws up. */
1612 ticks_to_wait -= delta;
1613 }
1614 else if (t < prev_ticks)
1615 {
1616 ticks_to_wait -= t; /* wrapped */
1617 }
1618
1619 prev_ticks = t;
1620 } while (ticks_to_wait > 0);
1621 ASM_START
1622 cli
1623 popf
1624 ASM_END
1625 }
1626
1627 Bit8u
1628 check_for_keystroke()
1629 {
1630 ASM_START
1631 mov ax, #0x100
1632 int #0x16
1633 jz no_key
1634 mov al, #1
1635 jmp done
1636 no_key:
1637 xor al, al
1638 done:
1639 ASM_END
1640 }
1641
1642 Bit8u
1643 get_keystroke()
1644 {
1645 ASM_START
1646 mov ax, #0x0
1647 int #0x16
1648 xchg ah, al
1649 ASM_END
1650 }
1651
1652 void
1653 delay_ticks_and_check_for_keystroke(ticks, count)
1654 Bit16u ticks, count;
1655 {
1656 Bit16u i;
1657 for (i = 1; i <= count; i++) {
1658 delay_ticks(ticks);
1659 if (check_for_keystroke())
1660 break;
1661 }
1662 }
1663
1664 //--------------------------------------------------------------------------
1665 // bios_printf()
1666 // A compact variable argument printf function.
1667 //
1668 // Supports %[format_width][length]format
1669 // where format can be x,X,u,d,s,S,c
1670 // and the optional length modifier is l (ell)
1671 //--------------------------------------------------------------------------
1672 void
1673 bios_printf(action, s)
1674 Bit16u action;
1675 Bit8u *s;
1676 {
1677 Bit8u c, format_char;
1678 bx_bool in_format;
1679 short i;
1680 Bit16u *arg_ptr;
1681 Bit16u arg_seg, arg, nibble, hibyte, shift_count, format_width, hexadd;
1682
1683 arg_ptr = &s;
1684 arg_seg = get_SS();
1685
1686 in_format = 0;
1687 format_width = 0;
1688
1689 if ((action & BIOS_PRINTF_DEBHALT) == BIOS_PRINTF_DEBHALT) {
1690 #if BX_VIRTUAL_PORTS
1691 outb(PANIC_PORT2, 0x00);
1692 #endif
1693 bios_printf (BIOS_PRINTF_SCREEN, "FATAL: ");
1694 }
1695
1696 while (c = read_byte(get_CS(), s)) {
1697 if ( c == '%' ) {
1698 in_format = 1;
1699 format_width = 0;
1700 }
1701 else if (in_format) {
1702 if ( (c>='0') && (c<='9') ) {
1703 format_width = (format_width * 10) + (c - '0');
1704 }
1705 else {
1706 arg_ptr++; // increment to next arg
1707 arg = read_word(arg_seg, arg_ptr);
1708 if (c == 'x' || c == 'X') {
1709 if (format_width == 0)
1710 format_width = 4;
1711 if (c == 'x')
1712 hexadd = 'a';
1713 else
1714 hexadd = 'A';
1715 for (i=format_width-1; i>=0; i--) {
1716 nibble = (arg >> (4 * i)) & 0x000f;
1717 send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
1718 }
1719 }
1720 else if (c == 'u') {
1721 put_uint(action, arg, format_width, 0);
1722 }
1723 else if (c == 'l') {
1724 s++;
1725 c = read_byte(get_CS(), s); /* is it ld,lx,lu? */
1726 arg_ptr++; /* increment to next arg */
1727 hibyte = read_word(arg_seg, arg_ptr);
1728 if (c == 'd') {
1729 if (hibyte & 0x8000)
1730 put_luint(action, 0L-(((Bit32u) hibyte << 16) | arg), format_width-1, 1);
1731 else
1732 put_luint(action, ((Bit32u) hibyte << 16) | arg, format_width, 0);
1733 }
1734 else if (c == 'u') {
1735 put_luint(action, ((Bit32u) hibyte << 16) | arg, format_width, 0);
1736 }
1737 else if (c == 'x' || c == 'X')
1738 {
1739 if (format_width == 0)
1740 format_width = 8;
1741 if (c == 'x')
1742 hexadd = 'a';
1743 else
1744 hexadd = 'A';
1745 for (i=format_width-1; i>=0; i--) {
1746 nibble = ((((Bit32u) hibyte <<16) | arg) >> (4 * i)) & 0x000f;
1747 send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
1748 }
1749 }
1750 }
1751 else if (c == 'd') {
1752 if (arg & 0x8000)
1753 put_int(action, -arg, format_width - 1, 1);
1754 else
1755 put_int(action, arg, format_width, 0);
1756 }
1757 else if (c == 's') {
1758 put_str(action, get_CS(), arg);
1759 }
1760 else if (c == 'S') {
1761 hibyte = arg;
1762 arg_ptr++;
1763 arg = read_word(arg_seg, arg_ptr);
1764 put_str(action, hibyte, arg);
1765 }
1766 else if (c == 'c') {
1767 send(action, arg);
1768 }
1769 else
1770 BX_PANIC("bios_printf: unknown format\n");
1771 in_format = 0;
1772 }
1773 }
1774 else {
1775 send(action, c);
1776 }
1777 s ++;
1778 }
1779
1780 if (action & BIOS_PRINTF_HALT) {
1781 // freeze in a busy loop.
1782 ASM_START
1783 cli
1784 halt2_loop:
1785 hlt
1786 jmp halt2_loop
1787 ASM_END
1788 }
1789 }
1790
1791 //--------------------------------------------------------------------------
1792 // keyboard_init
1793 //--------------------------------------------------------------------------
1794 // this file is based on LinuxBIOS implementation of keyboard.c
1795 // could convert to #asm to gain space
1796 void
1797 keyboard_init()
1798 {
1799 Bit16u max;
1800
1801 /* ------------------- Flush buffers ------------------------*/
1802 /* Wait until buffer is empty */
1803 max=0xffff;
1804 while ( (inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x00);
1805
1806 /* flush incoming keys */
1807 max=2;
1808 while (--max > 0) {
1809 outb(0x80, 0x00);
1810 if (inb(0x64) & 0x01) {
1811 inb(0x60);
1812 max = 2;
1813 }
1814 }
1815
1816 // Due to timer issues, and if the IPS setting is > 15000000,
1817 // the incoming keys might not be flushed here. That will
1818 // cause a panic a few lines below. See sourceforge bug report :
1819 // [ 642031 ] FATAL: Keyboard RESET error:993
1820
1821 /* ------------------- controller side ----------------------*/
1822 /* send cmd = 0xAA, self test 8042 */
1823 outb(0x64, 0xaa);
1824
1825 /* Wait until buffer is empty */
1826 max=0xffff;
1827 while ( (inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x00);
1828 if (max==0x0) keyboard_panic(00);
1829
1830 /* Wait for data */
1831 max=0xffff;
1832 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x01);
1833 if (max==0x0) keyboard_panic(01);
1834
1835 /* read self-test result, 0x55 should be returned from 0x60 */
1836 if ((inb(0x60) != 0x55)){
1837 keyboard_panic(991);
1838 }
1839
1840 /* send cmd = 0xAB, keyboard interface test */
1841 outb(0x64,0xab);
1842
1843 /* Wait until buffer is empty */
1844 max=0xffff;
1845 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x10);
1846 if (max==0x0) keyboard_panic(10);
1847
1848 /* Wait for data */
1849 max=0xffff;
1850 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x11);
1851 if (max==0x0) keyboard_panic(11);
1852
1853 /* read keyboard interface test result, */
1854 /* 0x00 should be returned form 0x60 */
1855 if ((inb(0x60) != 0x00)) {
1856 keyboard_panic(992);
1857 }
1858
1859 /* Enable Keyboard clock */
1860 outb(0x64,0xae);
1861 outb(0x64,0xa8);
1862
1863 /* ------------------- keyboard side ------------------------*/
1864 /* reset kerboard and self test (keyboard side) */
1865 outb(0x60, 0xff);
1866
1867 /* Wait until buffer is empty */
1868 max=0xffff;
1869 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x20);
1870 if (max==0x0) keyboard_panic(20);
1871
1872 /* Wait for data */
1873 max=0xffff;
1874 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x21);
1875 if (max==0x0) keyboard_panic(21);
1876
1877 /* keyboard should return ACK */
1878 if ((inb(0x60) != 0xfa)) {
1879 keyboard_panic(993);
1880 }
1881
1882 /* Wait for data */
1883 max=0xffff;
1884 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x31);
1885 if (max==0x0) keyboard_panic(31);
1886
1887 if ((inb(0x60) != 0xaa)) {
1888 keyboard_panic(994);
1889 }
1890
1891 /* Disable keyboard */
1892 outb(0x60, 0xf5);
1893
1894 /* Wait until buffer is empty */
1895 max=0xffff;
1896 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x40);
1897 if (max==0x0) keyboard_panic(40);
1898
1899 /* Wait for data */
1900 max=0xffff;
1901 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x41);
1902 if (max==0x0) keyboard_panic(41);
1903
1904 /* keyboard should return ACK */
1905 if ((inb(0x60) != 0xfa)) {
1906 keyboard_panic(995);
1907 }
1908
1909 /* Write Keyboard Mode */
1910 outb(0x64, 0x60);
1911
1912 /* Wait until buffer is empty */
1913 max=0xffff;
1914 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x50);
1915 if (max==0x0) keyboard_panic(50);
1916
1917 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
1918 outb(0x60, 0x61);
1919
1920 /* Wait until buffer is empty */
1921 max=0xffff;
1922 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x60);
1923 if (max==0x0) keyboard_panic(60);
1924
1925 /* Enable keyboard */
1926 outb(0x60, 0xf4);
1927
1928 /* Wait until buffer is empty */
1929 max=0xffff;
1930 while ((inb(0x64) & 0x02) && (--max>0)) outb(0x80, 0x70);
1931 if (max==0x0) keyboard_panic(70);
1932
1933 /* Wait for data */
1934 max=0xffff;
1935 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x71);
1936 if (max==0x0) keyboard_panic(70);
1937
1938 /* keyboard should return ACK */
1939 if ((inb(0x60) != 0xfa)) {
1940 keyboard_panic(996);
1941 }
1942
1943 outb(0x80, 0x77);
1944 }
1945
1946 //--------------------------------------------------------------------------
1947 // keyboard_panic
1948 //--------------------------------------------------------------------------
1949 void
1950 keyboard_panic(status)
1951 Bit16u status;
1952 {
1953 // If you're getting a 993 keyboard panic here,
1954 // please see the comment in keyboard_init
1955
1956 BX_PANIC("Keyboard error:%u\n",status);
1957 }
1958
1959 #define CMOS_SHUTDOWN_S3 0xFE
1960 //--------------------------------------------------------------------------
1961 // machine_reset
1962 //--------------------------------------------------------------------------
1963 void
1964 machine_reset()
1965 {
1966 ASM_START
1967 ;we must check whether CMOS_SHUTDOWN_S3 is set or not
1968 ;if it is s3 resume, just jmp back to normal Post Entry
1969 ;below port io will prevent s3 resume
1970 mov al, #0x0f
1971 out 0x70, al
1972 in al, 0x71
1973 cmp al, #0xFE
1974 jz post
1975 ASM_END
1976 /* Frob the keyboard reset line to reset the processor */
1977 outb(0x64, 0x60); /* Map the flags register at data port (0x60) */
1978 outb(0x60, 0x14); /* Set the flags to system|disable */
1979 outb(0x64, 0xfe); /* Pulse output 0 (system reset) low */
1980 BX_PANIC("Couldn't reset the machine\n");
1981 }
1982
1983 //--------------------------------------------------------------------------
1984 // clobber_entry_point
1985 // Because PV drivers in HVM guests detach some of the emulated devices,
1986 // it is not safe to do a soft reboot by just dropping to real mode and
1987 // jumping at ffff:0000. -- the boot drives might have disappeared!
1988 // This rather foul function overwrites(!) the BIOS entry point
1989 // to point at machine-reset, which will cause the Xen tools to
1990 // rebuild the whole machine from scratch.
1991 //--------------------------------------------------------------------------
1992 void
1993 clobber_entry_point()
1994 {
1995 /* The instruction at the entry point is one byte (0xea) for the
1996 * jump opcode, then two bytes of address, then two of segment.
1997 * Overwrite the address bytes.*/
1998 write_word(0xffff, 0x0001, machine_reset);
1999 }
2000
2001
2002 //--------------------------------------------------------------------------
2003 // shutdown_status_panic
2004 // called when the shutdown statsu is not implemented, displays the status
2005 //--------------------------------------------------------------------------
2006 void
2007 shutdown_status_panic(status)
2008 Bit16u status;
2009 {
2010 BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u)status);
2011 }
2012
2013 void s3_resume_panic()
2014 {
2015 BX_PANIC("Returned from s3_resume.\n");
2016 }
2017
2018 //--------------------------------------------------------------------------
2019 // print_bios_banner
2020 // displays a the bios version
2021 //--------------------------------------------------------------------------
2022 void
2023 print_bios_banner()
2024 {
2025 printf(BX_APPNAME" BIOS - build: %s\n%s\nOptions: ",
2026 BIOS_BUILD_DATE, bios_cvs_version_string);
2027 printf(
2028 #if BX_APM
2029 "apmbios "
2030 #endif
2031 #if BX_PCIBIOS
2032 "pcibios "
2033 #endif
2034 #if BX_ELTORITO_BOOT
2035 "eltorito "
2036 #endif
2037 #if BX_ROMBIOS32
2038 "rombios32 "
2039 #endif
2040 #if BX_TCGBIOS
2041 "TCG-enabled "
2042 #endif
2043 #if BX_PMM
2044 "PMM "
2045 #endif
2046 "\n\n");
2047 }
2048
2049 //--------------------------------------------------------------------------
2050 // BIOS Boot Specification 1.0.1 compatibility
2051 //
2052 // Very basic support for the BIOS Boot Specification, which allows expansion
2053 // ROMs to register themselves as boot devices, instead of just stealing the
2054 // INT 19h boot vector.
2055 //
2056 // This is a hack: to do it properly requires a proper PnP BIOS and we aren't
2057 // one; we just lie to the option ROMs to make them behave correctly.
2058 // We also don't support letting option ROMs register as bootable disk
2059 // drives (BCVs), only as bootable devices (BEVs).
2060 //
2061 // http://www.phoenix.com/en/Customer+Services/White+Papers-Specs/pc+industry+specifications.htm
2062 //--------------------------------------------------------------------------
2063
2064 static char drivetypes[][10]={"", "Floppy","Hard Disk","CD-Rom", "Network"};
2065
2066 static void
2067 init_boot_vectors()
2068 {
2069 ipl_entry_t e;
2070 Bit16u count = 0;
2071 Bit16u ss = get_SS();
2072 Bit16u ebda_seg = read_word(0x0040, 0x000E);
2073
2074 /* Clear out the IPL table. */
2075 memsetb(ebda_seg, IPL_TABLE_OFFSET, 0, IPL_SIZE);
2076
2077 /* User selected device not set */
2078 write_word(ebda_seg, IPL_BOOTFIRST_OFFSET, 0xFFFF);
2079
2080 /* Floppy drive */
2081 e.type = IPL_TYPE_FLOPPY; e.flags = 0; e.vector = 0; e.description = 0; e.reserved = 0;
2082 memcpyb(ebda_seg, IPL_TABLE_OFFSET + count * sizeof (e), ss, &e, sizeof (e));
2083 count++;
2084
2085 /* First HDD */
2086 e.type = IPL_TYPE_HARDDISK; e.flags = 0; e.vector = 0; e.description = 0; e.reserved = 0;
2087 memcpyb(ebda_seg, IPL_TABLE_OFFSET + count * sizeof (e), ss, &e, sizeof (e));
2088 count++;
2089
2090 #if BX_ELTORITO_BOOT
2091 /* CDROM */
2092 e.type = IPL_TYPE_CDROM; e.flags = 0; e.vector = 0; e.description = 0; e.reserved = 0;
2093 memcpyb(ebda_seg, IPL_TABLE_OFFSET + count * sizeof (e), ss, &e, sizeof (e));
2094 count++;
2095 #endif
2096
2097 /* Remember how many devices we have */
2098 write_word(ebda_seg, IPL_COUNT_OFFSET, count);
2099 /* Not tried booting anything yet */
2100 write_word(ebda_seg, IPL_SEQUENCE_OFFSET, 0xffff);
2101 }
2102
2103 static Bit8u
2104 get_boot_vector(i, e)
2105 Bit16u i; ipl_entry_t *e;
2106 {
2107 Bit16u count;
2108 Bit16u ss = get_SS();
2109 Bit16u ebda_seg = read_word(0x0040, 0x000E);
2110 /* Get the count of boot devices, and refuse to overrun the array */
2111 count = read_word(ebda_seg, IPL_COUNT_OFFSET);
2112 if (i >= count) return 0;
2113 /* OK to read this device */
2114 memcpyb(ss, e, ebda_seg, IPL_TABLE_OFFSET + i * sizeof (*e), sizeof (*e));
2115 return 1;
2116 }
2117
2118 #if BX_ELTORITO_BOOT
2119 void
2120 interactive_bootkey()
2121 {
2122 ipl_entry_t e;
2123 Bit16u count;
2124 char description[33];
2125 Bit8u scan_code;
2126 Bit8u i;
2127 Bit16u ss = get_SS();
2128 Bit16u valid_choice = 0;
2129 Bit16u ebda_seg = read_word(0x0040, 0x000E);
2130
2131 printf("\n\nPress F12 for boot menu.\n\n");
2132
2133 while (check_for_keystroke())
2134 {
2135 scan_code = get_keystroke();
2136 if (scan_code != 0x86) /* F12 */
2137 continue;
2138
2139 while (check_for_keystroke())
2140 get_keystroke();
2141
2142 printf("Select boot device:\n\n");
2143
2144 count = read_word(ebda_seg, IPL_COUNT_OFFSET);
2145 for (i = 0; i < count; i++)
2146 {
2147 memcpyb(ss, &e, ebda_seg, IPL_TABLE_OFFSET + i * sizeof (e), sizeof (e));
2148 printf("%d. ", i+1);
2149 switch(e.type)
2150 {
2151 case IPL_TYPE_FLOPPY:
2152 case IPL_TYPE_HARDDISK:
2153 case IPL_TYPE_CDROM:
2154 printf("%s\n", drivetypes[e.type]);
2155 break;
2156 case IPL_TYPE_BEV:
2157 printf("%s", drivetypes[4]);
2158 if (e.description != 0)
2159 {
2160 memcpyb(ss, &description, (Bit16u)(e.description >> 16), (Bit16u)(e.description & 0xffff), 32);
2161 description[32] = 0;
2162 printf(" [%S]", ss, description);
2163 }
2164 printf("\n");
2165 break;
2166 }
2167 }
2168
2169 count++;
2170 while (!valid_choice) {
2171 scan_code = get_keystroke();
2172 if (scan_code == 0x01 || scan_code == 0x58) /* ESC or F12 */
2173 {
2174 valid_choice = 1;
2175 }
2176 else if (scan_code <= count)
2177 {
2178 valid_choice = 1;
2179 scan_code -= 1;
2180 /* Set user selected device */
2181 write_word(ebda_seg, IPL_BOOTFIRST_OFFSET, scan_code);
2182 }
2183 }
2184
2185 printf("\n");
2186 break;
2187 }
2188 }
2189 #endif // BX_ELTORITO_BOOT
2190
2191 //--------------------------------------------------------------------------
2192 // print_boot_device
2193 // displays the boot device
2194 //--------------------------------------------------------------------------
2195
2196 void
2197 print_boot_device(type, desc)
2198 Bit16u type; Bit32u desc;
2199 {
2200 char description[33];
2201 Bit16u ss = get_SS();
2202 /* NIC appears as type 0x80 */
2203 if (type == IPL_TYPE_BEV) type = 0x4;
2204 if (type == 0 || type > 0x4) BX_PANIC("Bad drive type\n");
2205 printf("Booting from %s", drivetypes[type]);
2206 /* print product string if BEV */
2207 if (type == 4 && desc != 0) {
2208 /* first 32 bytes are significant */
2209 memcpyb(ss, &description, (Bit16u)(desc >> 16), (Bit16u)(desc & 0xffff), 32);
2210 /* terminate string */
2211 description[32] = 0;
2212 printf(" [%S]", ss, description);
2213 }
2214 printf("...\n");
2215 }
2216
2217 //--------------------------------------------------------------------------
2218 // print_boot_failure
2219 // displays the reason why boot failed
2220 //--------------------------------------------------------------------------
2221 void
2222 print_boot_failure(type, reason)
2223 Bit16u type; Bit8u reason;
2224 {
2225 if (type == 0 || type > 0x3) BX_PANIC("Bad drive type\n");
2226
2227 printf("Boot from %s failed", drivetypes[type]);
2228 if (type < 4) {
2229 /* Report the reason too */
2230 if (reason==0)
2231 printf(": not a bootable disk");
2232 else
2233 printf(": could not read the boot disk");
2234 }
2235 printf("\n\n");
2236 }
2237
2238 //--------------------------------------------------------------------------
2239 // print_cdromboot_failure
2240 // displays the reason why boot failed
2241 //--------------------------------------------------------------------------
2242 void
2243 print_cdromboot_failure( code )
2244 Bit16u code;
2245 {
2246 bios_printf(BIOS_PRINTF_SCREEN | BIOS_PRINTF_INFO, "CDROM boot failure code : %04x\n",code);
2247
2248 return;
2249 }
2250
2251 void
2252 nmi_handler_msg()
2253 {
2254 BX_PANIC("NMI Handler called\n");
2255 }
2256
2257 void
2258 int18_panic_msg()
2259 {
2260 BX_PANIC("INT18: BOOT FAILURE\n");
2261 }
2262
2263 void
2264 log_bios_start()
2265 {
2266 #if BX_DEBUG_SERIAL
2267 outb(BX_DEBUG_PORT+UART_LCR, 0x03); /* setup for serial logging: 8N1 */
2268 #endif
2269 BX_INFO("%s\n", bios_cvs_version_string);
2270 }
2271
2272 bx_bool
2273 set_enable_a20(val)
2274 bx_bool val;
2275 {
2276 Bit8u oldval;
2277
2278 // Use PS2 System Control port A to set A20 enable
2279
2280 // get current setting first
2281 oldval = inb(0x92);
2282
2283 // change A20 status
2284 if (val)
2285 outb(0x92, oldval | 0x02);
2286 else
2287 outb(0x92, oldval & 0xfd);
2288
2289 return((oldval & 0x02) != 0);
2290 }
2291
2292 void
2293 debugger_on()
2294 {
2295 outb(0xfedc, 0x01);
2296 }
2297
2298 void
2299 debugger_off()
2300 {
2301 outb(0xfedc, 0x00);
2302 }
2303
2304 int
2305 s3_resume()
2306 {
2307 Bit32u s3_wakeup_vector;
2308 Bit8u s3_resume_flag;
2309
2310 s3_resume_flag = read_byte(0x40, 0xb0);
2311 #ifdef HVMASSIST
2312 s3_wakeup_vector = get_s3_waking_vector();
2313 #else
2314 s3_wakeup_vector = read_dword(0x40, 0xb2);
2315 #endif
2316
2317 BX_INFO("S3 resume called %x 0x%lx\n", s3_resume_flag, s3_wakeup_vector);
2318 if (s3_resume_flag != CMOS_SHUTDOWN_S3 || !s3_wakeup_vector)
2319 return 0;
2320
2321 write_byte(0x40, 0xb0, 0);
2322
2323 /* setup wakeup vector */
2324 write_word(0x40, 0xb6, (s3_wakeup_vector & 0xF)); /* IP */
2325 write_word(0x40, 0xb8, (s3_wakeup_vector >> 4)); /* CS */
2326
2327 BX_INFO("S3 resume jump to %x:%x\n", (s3_wakeup_vector >> 4),
2328 (s3_wakeup_vector & 0xF));
2329 ASM_START
2330 jmpf [0x04b6]
2331 ASM_END
2332 return 1;
2333 }
2334
2335 #if BX_USE_ATADRV
2336
2337 // ---------------------------------------------------------------------------
2338 // Start of ATA/ATAPI Driver
2339 // ---------------------------------------------------------------------------
2340
2341 // Global defines -- ATA register and register bits.
2342 // command block & control block regs
2343 #define ATA_CB_DATA 0 // data reg in/out pio_base_addr1+0
2344 #define ATA_CB_ERR 1 // error in pio_base_addr1+1
2345 #define ATA_CB_FR 1 // feature reg out pio_base_addr1+1
2346 #define ATA_CB_SC 2 // sector count in/out pio_base_addr1+2
2347 #define ATA_CB_SN 3 // sector number in/out pio_base_addr1+3
2348 #define ATA_CB_CL 4 // cylinder low in/out pio_base_addr1+4
2349 #define ATA_CB_CH 5 // cylinder high in/out pio_base_addr1+5
2350 #define ATA_CB_DH 6 // device head in/out pio_base_addr1+6
2351 #define ATA_CB_STAT 7 // primary status in pio_base_addr1+7
2352 #define ATA_CB_CMD 7 // command out pio_base_addr1+7
2353 #define ATA_CB_ASTAT 6 // alternate status in pio_base_addr2+6
2354 #define ATA_CB_DC 6 // device control out pio_base_addr2+6
2355 #define ATA_CB_DA 7 // device address in pio_base_addr2+7
2356
2357 #define ATA_CB_ER_ICRC 0x80 // ATA Ultra DMA bad CRC
2358 #define ATA_CB_ER_BBK 0x80 // ATA bad block
2359 #define ATA_CB_ER_UNC 0x40 // ATA uncorrected error
2360 #define ATA_CB_ER_MC 0x20 // ATA media change
2361 #define ATA_CB_ER_IDNF 0x10 // ATA id not found
2362 #define ATA_CB_ER_MCR 0x08 // ATA media change request
2363 #define ATA_CB_ER_ABRT 0x04 // ATA command aborted
2364 #define ATA_CB_ER_NTK0 0x02 // ATA track 0 not found
2365 #define ATA_CB_ER_NDAM 0x01 // ATA address mark not found
2366
2367 #define ATA_CB_ER_P_SNSKEY 0xf0 // ATAPI sense key (mask)
2368 #define ATA_CB_ER_P_MCR 0x08 // ATAPI Media Change Request
2369 #define ATA_CB_ER_P_ABRT 0x04 // ATAPI command abort
2370 #define ATA_CB_ER_P_EOM 0x02 // ATAPI End of Media
2371 #define ATA_CB_ER_P_ILI 0x01 // ATAPI Illegal Length Indication
2372
2373 // ATAPI Interrupt Reason bits in the Sector Count reg (CB_SC)
2374 #define ATA_CB_SC_P_TAG 0xf8 // ATAPI tag (mask)
2375 #define ATA_CB_SC_P_REL 0x04 // ATAPI release
2376 #define ATA_CB_SC_P_IO 0x02 // ATAPI I/O
2377 #define ATA_CB_SC_P_CD 0x01 // ATAPI C/D
2378
2379 // bits 7-4 of the device/head (CB_DH) reg
2380 #define ATA_CB_DH_DEV0 0xa0 // select device 0
2381 #define ATA_CB_DH_DEV1 0xb0 // select device 1
2382 #define ATA_CB_DH_LBA 0x40 // use LBA
2383
2384 // status reg (CB_STAT and CB_ASTAT) bits
2385 #define ATA_CB_STAT_BSY 0x80 // busy
2386 #define ATA_CB_STAT_RDY 0x40 // ready
2387 #define ATA_CB_STAT_DF 0x20 // device fault
2388 #define ATA_CB_STAT_WFT 0x20 // write fault (old name)
2389 #define ATA_CB_STAT_SKC 0x10 // seek complete
2390 #define ATA_CB_STAT_SERV 0x10 // service
2391 #define ATA_CB_STAT_DRQ 0x08 // data request
2392 #define ATA_CB_STAT_CORR 0x04 // corrected
2393 #define ATA_CB_STAT_IDX 0x02 // index
2394 #define ATA_CB_STAT_ERR 0x01 // error (ATA)
2395 #define ATA_CB_STAT_CHK 0x01 // check (ATAPI)
2396
2397 // device control reg (CB_DC) bits
2398 #define ATA_CB_DC_HD15 0x08 // bit should always be set to one
2399 #define ATA_CB_DC_SRST 0x04 // soft reset
2400 #define ATA_CB_DC_NIEN 0x02 // disable interrupts
2401
2402 // Most mandtory and optional ATA commands (from ATA-3),
2403 #define ATA_CMD_CFA_ERASE_SECTORS 0xC0
2404 #define ATA_CMD_CFA_REQUEST_EXT_ERR_CODE 0x03
2405 #define ATA_CMD_CFA_TRANSLATE_SECTOR 0x87
2406 #define ATA_CMD_CFA_WRITE_MULTIPLE_WO_ERASE 0xCD
2407 #define ATA_CMD_CFA_WRITE_SECTORS_WO_ERASE 0x38
2408 #define ATA_CMD_CHECK_POWER_MODE1 0xE5
2409 #define ATA_CMD_CHECK_POWER_MODE2 0x98
2410 #define ATA_CMD_DEVICE_RESET 0x08
2411 #define ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC 0x90
2412 #define ATA_CMD_FLUSH_CACHE 0xE7
2413 #define ATA_CMD_FORMAT_TRACK 0x50
2414 #define ATA_CMD_IDENTIFY_DEVICE 0xEC
2415 #define ATA_CMD_IDENTIFY_DEVICE_PACKET 0xA1
2416 #define ATA_CMD_IDENTIFY_PACKET_DEVICE 0xA1
2417 #define ATA_CMD_IDLE1 0xE3
2418 #define ATA_CMD_IDLE2 0x97
2419 #define ATA_CMD_IDLE_IMMEDIATE1 0xE1
2420 #define ATA_CMD_IDLE_IMMEDIATE2 0x95
2421 #define ATA_CMD_INITIALIZE_DRIVE_PARAMETERS 0x91
2422 #define ATA_CMD_INITIALIZE_DEVICE_PARAMETERS 0x91
2423 #define ATA_CMD_NOP 0x00
2424 #define ATA_CMD_PACKET 0xA0
2425 #define ATA_CMD_READ_BUFFER 0xE4
2426 #define ATA_CMD_READ_DMA 0xC8
2427 #define ATA_CMD_READ_DMA_QUEUED 0xC7
2428 #define ATA_CMD_READ_MULTIPLE 0xC4
2429 #define ATA_CMD_READ_SECTORS 0x20
2430 #define ATA_CMD_READ_VERIFY_SECTORS 0x40
2431 #define ATA_CMD_RECALIBRATE 0x10
2432 #define ATA_CMD_REQUEST_SENSE 0x03
2433 #define ATA_CMD_SEEK 0x70
2434 #define ATA_CMD_SET_FEATURES 0xEF
2435 #define ATA_CMD_SET_MULTIPLE_MODE 0xC6
2436 #define ATA_CMD_SLEEP1 0xE6
2437 #define ATA_CMD_SLEEP2 0x99
2438 #define ATA_CMD_STANDBY1 0xE2
2439 #define ATA_CMD_STANDBY2 0x96
2440 #define ATA_CMD_STANDBY_IMMEDIATE1 0xE0
2441 #define ATA_CMD_STANDBY_IMMEDIATE2 0x94
2442 #define ATA_CMD_WRITE_BUFFER 0xE8
2443 #define ATA_CMD_WRITE_DMA 0xCA
2444 #define ATA_CMD_WRITE_DMA_QUEUED 0xCC
2445 #define ATA_CMD_WRITE_MULTIPLE 0xC5
2446 #define ATA_CMD_WRITE_SECTORS 0x30
2447 #define ATA_CMD_WRITE_VERIFY 0x3C
2448
2449 #define ATA_IFACE_NONE 0x00
2450 #define ATA_IFACE_ISA 0x00
2451 #define ATA_IFACE_PCI 0x01
2452
2453 #define ATA_TYPE_NONE 0x00
2454 #define ATA_TYPE_UNKNOWN 0x01
2455 #define ATA_TYPE_ATA 0x02
2456 #define ATA_TYPE_ATAPI 0x03
2457
2458 #define ATA_DEVICE_NONE 0x00
2459 #define ATA_DEVICE_HD 0xFF
2460 #define ATA_DEVICE_CDROM 0x05
2461
2462 #define ATA_MODE_NONE 0x00
2463 #define ATA_MODE_PIO16 0x00
2464 #define ATA_MODE_PIO32 0x01
2465 #define ATA_MODE_ISADMA 0x02
2466 #define ATA_MODE_PCIDMA 0x03
2467 #define ATA_MODE_USEIRQ 0x10
2468
2469 #define ATA_TRANSLATION_NONE 0
2470 #define ATA_TRANSLATION_LBA 1
2471 #define ATA_TRANSLATION_LARGE 2
2472 #define ATA_TRANSLATION_RECHS 3
2473
2474 #define ATA_DATA_NO 0x00
2475 #define ATA_DATA_IN 0x01
2476 #define ATA_DATA_OUT 0x02
2477
2478 // ---------------------------------------------------------------------------
2479 // ATA/ATAPI driver : initialization
2480 // ---------------------------------------------------------------------------
2481 void ata_init( )
2482 {
2483 Bit16u ebda_seg=read_word(0x0040,0x000E);
2484 Bit8u channel, device;
2485
2486 // Channels info init.
2487 for (channel=0; channel<BX_MAX_ATA_INTERFACES; channel++) {
2488 write_byte(ebda_seg,&EbdaData->ata.channels[channel].iface,ATA_IFACE_NONE);
2489 write_word(ebda_seg,&EbdaData->ata.channels[channel].iobase1,0x0);
2490 write_word(ebda_seg,&EbdaData->ata.channels[channel].iobase2,0x0);
2491 write_byte(ebda_seg,&EbdaData->ata.channels[channel].irq,0);
2492 }
2493
2494 // Devices info init.
2495 for (device=0; device<BX_MAX_ATA_DEVICES; device++) {
2496 write_byte(ebda_seg,&EbdaData->ata.devices[device].type,ATA_TYPE_NONE);
2497 write_byte(ebda_seg,&EbdaData->ata.devices[device].device,ATA_DEVICE_NONE);
2498 write_byte(ebda_seg,&EbdaData->ata.devices[device].removable,0);
2499 write_byte(ebda_seg,&EbdaData->ata.devices[device].lock,0);
2500 write_byte(ebda_seg,&EbdaData->ata.devices[device].mode,ATA_MODE_NONE);
2501 write_word(ebda_seg,&EbdaData->ata.devices[device].blksize,0);
2502 write_byte(ebda_seg,&EbdaData->ata.devices[device].translation,ATA_TRANSLATION_NONE);
2503 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.heads,0);
2504 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.cylinders,0);
2505 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.spt,0);
2506 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.heads,0);
2507 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.cylinders,0);
2508 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.spt,0);
2509
2510 write_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_low,0L);
2511 write_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_high,0L);
2512 }
2513
2514 // hdidmap and cdidmap init.
2515 for (device=0; device<BX_MAX_ATA_DEVICES; device++) {
2516 write_byte(ebda_seg,&EbdaData->ata.hdidmap[device],BX_MAX_ATA_DEVICES);
2517 write_byte(ebda_seg,&EbdaData->ata.cdidmap[device],BX_MAX_ATA_DEVICES);
2518 }
2519
2520 write_byte(ebda_seg,&EbdaData->ata.hdcount,0);
2521 write_byte(ebda_seg,&EbdaData->ata.cdcount,0);
2522 }
2523
2524 #define TIMEOUT 0
2525 #define BSY 1
2526 #define NOT_BSY 2
2527 #define NOT_BSY_DRQ 3
2528 #define NOT_BSY_NOT_DRQ 4
2529 #define NOT_BSY_RDY 5
2530
2531 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
2532
2533 Bit8u await_ide();
2534 static Bit8u await_ide(when_done,base,timeout)
2535 Bit8u when_done;
2536 Bit16u base;
2537 Bit16u timeout;
2538 {
2539 Bit32u time=0,last=0;
2540 Bit8u status;
2541 Bit8u result;
2542 for(;;) {
2543 status = inb(base+ATA_CB_STAT);
2544 time++;
2545 if (when_done == BSY)
2546 result = status & ATA_CB_STAT_BSY;
2547 else if (when_done == NOT_BSY)
2548 result = !(status & ATA_CB_STAT_BSY);
2549 else if (when_done == NOT_BSY_DRQ)
2550 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
2551 else if (when_done == NOT_BSY_NOT_DRQ)
2552 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
2553 else if (when_done == NOT_BSY_RDY)
2554 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
2555 else if (when_done == TIMEOUT)
2556 result = 0;
2557
2558 if (result) return status;
2559 if (time>>16 != last) // mod 2048 each 16 ms
2560 {
2561 last = time >>16;
2562 BX_DEBUG_ATA("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %ld timeout= %d\n",when_done,time>>11, timeout);
2563 }
2564 if (status & ATA_CB_STAT_ERR)
2565 {
2566 BX_DEBUG_ATA("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %ld timeout= %d\n",when_done,time>>11, timeout);
2567 return status;
2568 }
2569 if ((timeout == 0) || ((time>>11) > timeout)) break;
2570 }
2571 BX_INFO("IDE time out\n");
2572 return status;
2573 }
2574
2575 // ---------------------------------------------------------------------------
2576 // ATA/ATAPI driver : device detection
2577 // ---------------------------------------------------------------------------
2578
2579 void ata_detect( )
2580 {
2581 Bit16u ebda_seg=read_word(0x0040,0x000E);
2582 Bit8u hdcount, cdcount, device, type;
2583 Bit8u buffer[0x0200];
2584
2585 #if BX_MAX_ATA_INTERFACES > 0
2586 write_byte(ebda_seg,&EbdaData->ata.channels[0].iface,ATA_IFACE_ISA);
2587 write_word(ebda_seg,&EbdaData->ata.channels[0].iobase1,0x1f0);
2588 write_word(ebda_seg,&EbdaData->ata.channels[0].iobase2,0x3f0);
2589 write_byte(ebda_seg,&EbdaData->ata.channels[0].irq,14);
2590 #endif
2591 #if BX_MAX_ATA_INTERFACES > 1
2592 write_byte(ebda_seg,&EbdaData->ata.channels[1].iface,ATA_IFACE_ISA);
2593 write_word(ebda_seg,&EbdaData->ata.channels[1].iobase1,0x170);
2594 write_word(ebda_seg,&EbdaData->ata.channels[1].iobase2,0x370);
2595 write_byte(ebda_seg,&EbdaData->ata.channels[1].irq,15);
2596 #endif
2597 #if BX_MAX_ATA_INTERFACES > 2
2598 write_byte(ebda_seg,&EbdaData->ata.channels[2].iface,ATA_IFACE_ISA);
2599 write_word(ebda_seg,&EbdaData->ata.channels[2].iobase1,0x1e8);
2600 write_word(ebda_seg,&EbdaData->ata.channels[2].iobase2,0x3e0);
2601 write_byte(ebda_seg,&EbdaData->ata.channels[2].irq,12);
2602 #endif
2603 #if BX_MAX_ATA_INTERFACES > 3
2604 write_byte(ebda_seg,&EbdaData->ata.channels[3].iface,ATA_IFACE_ISA);
2605 write_word(ebda_seg,&EbdaData->ata.channels[3].iobase1,0x168);
2606 write_word(ebda_seg,&EbdaData->ata.channels[3].iobase2,0x360);
2607 write_byte(ebda_seg,&EbdaData->ata.channels[3].irq,11);
2608 #endif
2609 #if BX_MAX_ATA_INTERFACES > 4
2610 #error Please fill the ATA interface information
2611 #endif
2612
2613 // Device detection
2614 hdcount=cdcount=0;
2615
2616 for(device=0; device<BX_MAX_ATA_DEVICES; device++) {
2617 Bit16u iobase1, iobase2;
2618 Bit8u channel, slave, shift;
2619 Bit8u sc, sn, cl, ch, st;
2620
2621 channel = device / 2;
2622 slave = device % 2;
2623
2624 iobase1 =read_word(ebda_seg,&EbdaData->ata.channels[channel].iobase1);
2625 iobase2 =read_word(ebda_seg,&EbdaData->ata.channels[channel].iobase2);
2626
2627 // Disable interrupts
2628 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
2629
2630 // Look for device
2631 outb(iobase1+ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
2632 outb(iobase1+ATA_CB_SC, 0x55);
2633 outb(iobase1+ATA_CB_SN, 0xaa);
2634 outb(iobase1+ATA_CB_SC, 0xaa);
2635 outb(iobase1+ATA_CB_SN, 0x55);
2636 outb(iobase1+ATA_CB_SC, 0x55);
2637 outb(iobase1+ATA_CB_SN, 0xaa);
2638
2639 // If we found something
2640 sc = inb(iobase1+ATA_CB_SC);
2641 sn = inb(iobase1+ATA_CB_SN);
2642
2643 if ( (sc == 0x55) && (sn == 0xaa) ) {
2644 write_byte(ebda_seg,&EbdaData->ata.devices[device].type,ATA_TYPE_UNKNOWN);
2645
2646 // reset the channel
2647 ata_reset(device);
2648
2649 // check for ATA or ATAPI
2650 outb(iobase1+ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
2651 sc = inb(iobase1+ATA_CB_SC);
2652 sn = inb(iobase1+ATA_CB_SN);
2653 if ((sc==0x01) && (sn==0x01)) {
2654 cl = inb(iobase1+ATA_CB_CL);
2655 ch = inb(iobase1+ATA_CB_CH);
2656 st = inb(iobase1+ATA_CB_STAT);
2657
2658 if ((cl==0x14) && (ch==0xeb)) {
2659 write_byte(ebda_seg,&EbdaData->ata.devices[device].type,ATA_TYPE_ATAPI);
2660 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
2661 write_byte(ebda_seg,&EbdaData->ata.devices[device].type,ATA_TYPE_ATA);
2662 } else if ((cl==0xff) && (ch==0xff)) {
2663 write_byte(ebda_seg,&EbdaData->ata.devices[device].type,ATA_TYPE_NONE);
2664 }
2665 }
2666 }
2667
2668 type=read_byte(ebda_seg,&EbdaData->ata.devices[device].type);
2669
2670 // Now we send a IDENTIFY command to ATA device
2671 if(type == ATA_TYPE_ATA) {
2672 Bit32u sectors_low, sectors_high;
2673 Bit16u cylinders, heads, spt, blksize;
2674 Bit8u translation, removable, mode;
2675
2676 // default mode to PIO16
2677 mode = ATA_MODE_PIO16;
2678
2679 //Temporary values to do the transfer
2680 write_byte(ebda_seg,&EbdaData->ata.devices[device].device,ATA_DEVICE_HD);
2681 write_byte(ebda_seg,&EbdaData->ata.devices[device].mode, ATA_MODE_PIO16);
2682
2683 if (ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE, 1, 0, 0, 0, 0L, 0L, get_SS(),buffer) !=0 )
2684 BX_PANIC("ata-detect: Failed to detect ATA device\n");
2685
2686 removable = (read_byte(get_SS(),buffer+0) & 0x80) ? 1 : 0;
2687 #ifndef NO_PIO32
2688 mode = read_byte(get_SS(),buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
2689 #endif
2690 blksize = read_word(get_SS(),buffer+10);
2691
2692 cylinders = read_word(get_SS(),buffer+(1*2)); // word 1
2693 heads = read_word(get_SS(),buffer+(3*2)); // word 3
2694 spt = read_word(get_SS(),buffer+(6*2)); // word 6
2695
2696 if (read_word(get_SS(),buffer+(83*2)) & (1 << 10)) { // word 83 - lba48 support
2697 sectors_low = read_dword(get_SS(),buffer+(100*2)); // word 100 and word 101
2698 sectors_high = read_dword(get_SS(),buffer+(102*2)); // word 102 and word 103
2699 } else {
2700 sectors_low = read_dword(get_SS(),buffer+(60*2)); // word 60 and word 61
2701 sectors_high = 0;
2702 }
2703
2704 write_byte(ebda_seg,&EbdaData->ata.devices[device].device,ATA_DEVICE_HD);
2705 write_byte(ebda_seg,&EbdaData->ata.devices[device].removable, removable);
2706 write_byte(ebda_seg,&EbdaData->ata.devices[device].mode, mode);
2707 write_word(ebda_seg,&EbdaData->ata.devices[device].blksize, blksize);
2708 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.heads, heads);
2709 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.cylinders, cylinders);
2710 write_word(ebda_seg,&EbdaData->ata.devices[device].pchs.spt, spt);
2711 write_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_low, sectors_low);
2712 write_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_high, sectors_high);
2713 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
2714
2715 translation = inb_cmos(0x39 + channel/2);
2716 for (shift=device%4; shift>0; shift--) translation >>= 2;
2717 translation &= 0x03;
2718
2719 write_byte(ebda_seg,&EbdaData->ata.devices[device].translation, translation);
2720
2721 switch (translation) {
2722 case ATA_TRANSLATION_NONE:
2723 BX_INFO("none");
2724 break;
2725 case ATA_TRANSLATION_LBA:
2726 BX_INFO("lba");
2727 break;
2728 case ATA_TRANSLATION_LARGE:
2729 BX_INFO("large");
2730 break;
2731 case ATA_TRANSLATION_RECHS:
2732 BX_INFO("r-echs");
2733 break;
2734 }
2735 switch (translation) {
2736 case ATA_TRANSLATION_NONE:
2737 break;
2738 case ATA_TRANSLATION_LBA:
2739 spt = 63;
2740 sectors_low /= 63;
2741 heads = sectors_low / 1024;
2742 if (heads>128) heads = 255;
2743 else if (heads>64) heads = 128;
2744 else if (heads>32) heads = 64;
2745 else if (heads>16) heads = 32;
2746 else heads=16;
2747 cylinders = sectors_low / heads;
2748 break;
2749 case ATA_TRANSLATION_RECHS:
2750 // Take care not to overflow
2751 if (heads==16) {
2752 if(cylinders>61439) cylinders=61439;
2753 heads=15;
2754 cylinders = (Bit16u)((Bit32u)(cylinders)*16/15);
2755 }
2756 // then go through the large bitshift process
2757 case ATA_TRANSLATION_LARGE:
2758 while(cylinders > 1024) {
2759 cylinders >>= 1;
2760 heads <<= 1;
2761
2762 // If we max out the head count
2763 if (heads > 127) break;
2764 }
2765 break;
2766 }
2767 // clip to 1024 cylinders in lchs
2768 if (cylinders > 1024) cylinders=1024;
2769 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
2770
2771 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.heads, heads);
2772 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.cylinders, cylinders);
2773 write_word(ebda_seg,&EbdaData->ata.devices[device].lchs.spt, spt);
2774
2775 // fill hdidmap
2776 write_byte(ebda_seg,&EbdaData->ata.hdidmap[hdcount], device);
2777 hdcount++;
2778 }
2779
2780 // Now we send a IDENTIFY command to ATAPI device
2781 if(type == ATA_TYPE_ATAPI) {
2782
2783 Bit8u type, removable, mode;
2784 Bit16u blksize;
2785
2786 // default mode to PIO16
2787 mode = ATA_MODE_PIO16;
2788
2789 //Temporary values to do the transfer
2790 write_byte(ebda_seg,&EbdaData->ata.devices[device].device,ATA_DEVICE_CDROM);
2791 write_byte(ebda_seg,&EbdaData->ata.devices[device].mode, ATA_MODE_PIO16);
2792
2793 if (ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET, 1, 0, 0, 0, 0L, 0L, get_SS(),buffer) != 0)
2794 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
2795
2796 type = read_byte(get_SS(),buffer+1) & 0x1f;
2797 removable = (read_byte(get_SS(),buffer+0) & 0x80) ? 1 : 0;
2798 #ifndef NO_PIO32
2799 mode = read_byte(get_SS(),buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
2800 #endif
2801 blksize = 2048;
2802
2803 write_byte(ebda_seg,&EbdaData->ata.devices[device].device, type);
2804 write_byte(ebda_seg,&EbdaData->ata.devices[device].removable, removable);
2805 write_byte(ebda_seg,&EbdaData->ata.devices[device].mode, mode);
2806 write_word(ebda_seg,&EbdaData->ata.devices[device].blksize, blksize);
2807
2808 // fill cdidmap
2809 write_byte(ebda_seg,&EbdaData->ata.cdidmap[cdcount], device);
2810 cdcount++;
2811 }
2812
2813 {
2814 Bit32u sizeinmb;
2815 Bit16u ataversion;
2816 Bit8u c, i, version, model[41];
2817
2818 switch (type) {
2819 case ATA_TYPE_ATA:
2820 sizeinmb = (read_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_high) << 21)
2821 | (read_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_low) >> 11);
2822 case ATA_TYPE_ATAPI:
2823 // Read ATA/ATAPI version
2824 ataversion=((Bit16u)(read_byte(get_SS(),buffer+161))<<8)|read_byte(get_SS(),buffer+160);
2825 for(version=15;version>0;version--) {
2826 if((ataversion&(1<<version))!=0)
2827 break;
2828 }
2829
2830 // Read model name
2831 for(i=0;i<20;i++){
2832 write_byte(get_SS(),model+(i*2),read_byte(get_SS(),buffer+(i*2)+54+1));
2833 write_byte(get_SS(),model+(i*2)+1,read_byte(get_SS(),buffer+(i*2)+54));
2834 }
2835
2836 // Reformat
2837 write_byte(get_SS(),model+40,0x00);
2838 for(i=39;i>0;i--){
2839 if(read_byte(get_SS(),model+i)==0x20)
2840 write_byte(get_SS(),model+i,0x00);
2841 else break;
2842 }
2843 if (i>36) {
2844 write_byte(get_SS(),model+36,0x00);
2845 for(i=35;i>32;i--){
2846 write_byte(get_SS(),model+i,0x2E);
2847 }
2848 }
2849 break;
2850 }
2851
2852 switch (type) {
2853 case ATA_TYPE_ATA:
2854 printf("ata%d %s: ",channel,slave?" slave":"master");
2855 i=0; while(c=read_byte(get_SS(),model+i++)) printf("%c",c);
2856 if (sizeinmb < (1UL<<16))
2857 printf(" ATA-%d Hard-Disk (%4u MBytes)\n", version, (Bit16u)sizeinmb);
2858 else
2859 printf(" ATA-%d Hard-Disk (%4u GBytes)\n", version, (Bit16u)(sizeinmb>>10));
2860 break;
2861 case ATA_TYPE_ATAPI:
2862 printf("ata%d %s: ",channel,slave?" slave":"master");
2863 i=0; while(c=read_byte(get_SS(),model+i++)) printf("%c",c);
2864 if(read_byte(ebda_seg,&EbdaData->ata.devices[device].device)==ATA_DEVICE_CDROM)
2865 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
2866 else
2867 printf(" ATAPI-%d Device\n",version);
2868 break;
2869 case ATA_TYPE_UNKNOWN:
2870 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
2871 break;
2872 }
2873 }
2874 }
2875
2876 // Store the devices counts
2877 write_byte(ebda_seg,&EbdaData->ata.hdcount, hdcount);
2878 write_byte(ebda_seg,&EbdaData->ata.cdcount, cdcount);
2879 write_byte(0x40,0x75, hdcount);
2880
2881 printf("\n");
2882
2883 // FIXME : should use bios=cmos|auto|disable bits
2884 // FIXME : should know about translation bits
2885 // FIXME : move hard_drive_post here
2886
2887 }
2888
2889 // ---------------------------------------------------------------------------
2890 // ATA/ATAPI driver : software reset
2891 // ---------------------------------------------------------------------------
2892 // ATA-3
2893 // 8.2.1 Software reset - Device 0
2894
2895 void ata_reset(device)
2896 Bit16u device;
2897 {
2898 Bit16u ebda_seg=read_word(0x0040,0x000E);
2899 Bit16u iobase1, iobase2;
2900 Bit8u channel, slave, sn, sc;
2901 Bit8u type;
2902 Bit16u max;
2903
2904 channel = device / 2;
2905 slave = device % 2;
2906
2907 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
2908 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
2909
2910 // Reset
2911
2912 // 8.2.1 (a) -- set SRST in DC
2913 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST);
2914
2915 // 8.2.1 (b) -- wait
2916 outb(0x80, 0x00);
2917
2918 // 8.2.1 (f) -- clear SRST
2919 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
2920
2921 type=read_byte(ebda_seg,&EbdaData->ata.devices[device].type);
2922 if (type != ATA_TYPE_NONE) {
2923
2924 // 8.2.1 (g) -- check for sc==sn==0x01
2925 // select device
2926 outb(iobase1+ATA_CB_DH, slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0);
2927 sc = inb(iobase1+ATA_CB_SC);
2928 sn = inb(iobase1+ATA_CB_SN);
2929
2930 if ( (sc==0x01) && (sn==0x01) ) {
2931 if (type == ATA_TYPE_ATA) //ATA
2932 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
2933 else //ATAPI
2934 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
2935 }
2936
2937 // 8.2.1 (h) -- wait for not BSY
2938 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
2939 }
2940
2941 // Enable interrupts
2942 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
2943 }
2944
2945 // ---------------------------------------------------------------------------
2946 // ATA/ATAPI driver : execute a non data command
2947 // ---------------------------------------------------------------------------
2948
2949 Bit16u ata_cmd_non_data()
2950 {return 0;}
2951
2952 // ---------------------------------------------------------------------------
2953 // ATA/ATAPI driver : execute a data-in command
2954 // ---------------------------------------------------------------------------
2955 // returns
2956 // 0 : no error
2957 // 1 : BUSY bit set
2958 // 2 : read error
2959 // 3 : expected DRQ=1
2960 // 4 : no sectors left to read/verify
2961 // 5 : more sectors to read/verify
2962 // 6 : no sectors left to write
2963 // 7 : more sectors to write
2964 Bit16u ata_cmd_data_in(device, command, count, cylinder, head, sector, lba_low, lba_high, segment, offset)
2965 Bit16u device, command, count, cylinder, head, sector, segment, offset;
2966 Bit32u lba_low, lba_high;
2967 {
2968 Bit16u ebda_seg=read_word(0x0040,0x000E);
2969 Bit16u iobase1, iobase2, blksize;
2970 Bit8u channel, slave;
2971 Bit8u status, current, mode;
2972
2973 channel = device / 2;
2974 slave = device % 2;
2975
2976 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
2977 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
2978 mode = read_byte(ebda_seg, &EbdaData->ata.devices[device].mode);
2979 blksize = 0x200; // was = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
2980 if (mode == ATA_MODE_PIO32) blksize>>=2;
2981 else blksize>>=1;
2982
2983 // Reset count of transferred data
2984 write_word(ebda_seg, &EbdaData->ata.trsfsectors,0);
2985 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,0L);
2986 current = 0;
2987
2988 status = inb(iobase1 + ATA_CB_STAT);
2989 if (status & ATA_CB_STAT_BSY) return 1;
2990
2991 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
2992
2993 // sector will be 0 only on lba access. Convert to lba-chs
2994 if (sector == 0) {
2995 if ((count >= 1 << 8) || lba_high || (lba_low + count >= 1UL << 28)) {
2996 outb(iobase1 + ATA_CB_FR, 0x00);
2997 outb(iobase1 + ATA_CB_SC, (count >> 8) & 0xff);
2998 outb(iobase1 + ATA_CB_SN, lba_low >> 24);
2999 outb(iobase1 + ATA_CB_CL, lba_high & 0xff);
3000 outb(iobase1 + ATA_CB_CH, lba_high >> 8);
3001 command |= 0x04;
3002 count &= (1UL << 8) - 1;
3003 lba_low &= (1UL << 24) - 1;
3004 }
3005 sector = (Bit16u) (lba_low & 0x000000ffL);
3006 cylinder = (Bit16u) ((lba_low>>8) & 0x0000ffffL);
3007 head = ((Bit16u) ((lba_low>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
3008 }
3009
3010 outb(iobase1 + ATA_CB_FR, 0x00);
3011 outb(iobase1 + ATA_CB_SC, count);
3012 outb(iobase1 + ATA_CB_SN, sector);
3013 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
3014 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
3015 outb(iobase1 + ATA_CB_DH, (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (Bit8u) head );
3016 outb(iobase1 + ATA_CB_CMD, command);
3017
3018 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
3019
3020 if (status & ATA_CB_STAT_ERR) {
3021 BX_DEBUG_ATA("ata_cmd_data_in : read error\n");
3022 return 2;
3023 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
3024 BX_DEBUG_ATA("ata_cmd_data_in : DRQ not set (status %02x)\n", (unsigned) status);
3025 return 3;
3026 }
3027
3028 // FIXME : move seg/off translation here
3029
3030 ASM_START
3031 sti ;; enable higher priority interrupts
3032 ASM_END
3033
3034 while (1) {
3035
3036 ASM_START
3037 push bp
3038 mov bp, sp
3039 mov di, _ata_cmd_data_in.offset + 2[bp]
3040 mov ax, _ata_cmd_data_in.segment + 2[bp]
3041 mov cx, _ata_cmd_data_in.blksize + 2[bp]
3042
3043 ;; adjust if there will be an overrun. 2K max sector size
3044 cmp di, #0xf800 ;;
3045 jbe ata_in_no_adjust
3046
3047 ata_in_adjust:
3048 sub di, #0x0800 ;; sub 2 kbytes from offset
3049 add ax, #0x0080 ;; add 2 Kbytes to segment
3050
3051 ata_in_no_adjust:
3052 mov es, ax ;; segment in es
3053
3054 mov dx, _ata_cmd_data_in.iobase1 + 2[bp] ;; ATA data read port
3055
3056 mov ah, _ata_cmd_data_in.mode + 2[bp]
3057 cmp ah, #ATA_MODE_PIO32
3058 je ata_in_32
3059
3060 ata_in_16:
3061 rep
3062 insw ;; CX words transfered from port(DX) to ES:[DI]
3063 jmp ata_in_done
3064
3065 ata_in_32:
3066 rep
3067 insd ;; CX dwords transfered from port(DX) to ES:[DI]
3068
3069 ata_in_done:
3070 mov _ata_cmd_data_in.offset + 2[bp], di
3071 mov _ata_cmd_data_in.segment + 2[bp], es
3072 pop bp
3073 ASM_END
3074
3075 current++;
3076 write_word(ebda_seg, &EbdaData->ata.trsfsectors,current);
3077 count--;
3078 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
3079 if (count == 0) {
3080 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
3081 != ATA_CB_STAT_RDY ) {
3082 BX_DEBUG_ATA("ata_cmd_data_in : no sectors left (status %02x)\n", (unsigned) status);
3083 return 4;
3084 }
3085 break;
3086 }
3087 else {
3088 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
3089 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
3090 BX_DEBUG_ATA("ata_cmd_data_in : more sectors left (status %02x)\n", (unsigned) status);
3091 return 5;
3092 }
3093 continue;
3094 }
3095 }
3096 // Enable interrupts
3097 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
3098 return 0;
3099 }
3100
3101 // ---------------------------------------------------------------------------
3102 // ATA/ATAPI driver : execute a data-out command
3103 // ---------------------------------------------------------------------------
3104 // returns
3105 // 0 : no error
3106 // 1 : BUSY bit set
3107 // 2 : read error
3108 // 3 : expected DRQ=1
3109 // 4 : no sectors left to read/verify
3110 // 5 : more sectors to read/verify
3111 // 6 : no sectors left to write
3112 // 7 : more sectors to write
3113 Bit16u ata_cmd_data_out(device, command, count, cylinder, head, sector, lba_low, lba_high, segment, offset)
3114 Bit16u device, command, count, cylinder, head, sector, segment, offset;
3115 Bit32u lba_low, lba_high;
3116 {
3117 Bit16u ebda_seg=read_word(0x0040,0x000E);
3118 Bit16u iobase1, iobase2, blksize;
3119 Bit8u channel, slave;
3120 Bit8u status, current, mode;
3121
3122 channel = device / 2;
3123 slave = device % 2;
3124
3125 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
3126 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
3127 mode = read_byte(ebda_seg, &EbdaData->ata.devices[device].mode);
3128 blksize = 0x200; // was = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
3129 if (mode == ATA_MODE_PIO32) blksize>>=2;
3130 else blksize>>=1;
3131
3132 // Reset count of transferred data
3133 write_word(ebda_seg, &EbdaData->ata.trsfsectors,0);
3134 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,0L);
3135 current = 0;
3136
3137 status = inb(iobase1 + ATA_CB_STAT);
3138 if (status & ATA_CB_STAT_BSY) return 1;
3139
3140 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
3141
3142 // sector will be 0 only on lba access. Convert to lba-chs
3143 if (sector == 0) {
3144 if ((count >= 1 << 8) || lba_high || (lba_low + count >= 1UL << 28)) {
3145 outb(iobase1 + ATA_CB_FR, 0x00);
3146 outb(iobase1 + ATA_CB_SC, (count >> 8) & 0xff);
3147 outb(iobase1 + ATA_CB_SN, lba_low >> 24);
3148 outb(iobase1 + ATA_CB_CL, lba_high & 0xff);
3149 outb(iobase1 + ATA_CB_CH, lba_high >> 8);
3150 command |= 0x04;
3151 count &= (1UL << 8) - 1;
3152 lba_low &= (1UL << 24) - 1;
3153 }
3154 sector = (Bit16u) (lba_low & 0x000000ffL);
3155 cylinder = (Bit16u) ((lba_low>>8) & 0x0000ffffL);
3156 head = ((Bit16u) ((lba_low>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
3157 }
3158
3159 outb(iobase1 + ATA_CB_FR, 0x00);
3160 outb(iobase1 + ATA_CB_SC, count);
3161 outb(iobase1 + ATA_CB_SN, sector);
3162 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
3163 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
3164 outb(iobase1 + ATA_CB_DH, (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (Bit8u) head );
3165 outb(iobase1 + ATA_CB_CMD, command);
3166
3167 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
3168
3169 if (status & ATA_CB_STAT_ERR) {
3170 BX_DEBUG_ATA("ata_cmd_data_out : read error\n");
3171 return 2;
3172 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
3173 BX_DEBUG_ATA("ata_cmd_data_out : DRQ not set (status %02x)\n", (unsigned) status);
3174 return 3;
3175 }
3176
3177 // FIXME : move seg/off translation here
3178
3179 ASM_START
3180 sti ;; enable higher priority interrupts
3181 ASM_END
3182
3183 while (1) {
3184
3185 ASM_START
3186 push bp
3187 mov bp, sp
3188 mov si, _ata_cmd_data_out.offset + 2[bp]
3189 mov ax, _ata_cmd_data_out.segment + 2[bp]
3190 mov cx, _ata_cmd_data_out.blksize + 2[bp]
3191
3192 ;; adjust if there will be an overrun. 2K max sector size
3193 cmp si, #0xf800 ;;
3194 jbe ata_out_no_adjust
3195
3196 ata_out_adjust:
3197 sub si, #0x0800 ;; sub 2 kbytes from offset
3198 add ax, #0x0080 ;; add 2 Kbytes to segment
3199
3200 ata_out_no_adjust:
3201 mov es, ax ;; segment in es
3202
3203 mov dx, _ata_cmd_data_out.iobase1 + 2[bp] ;; ATA data write port
3204
3205 mov ah, _ata_cmd_data_out.mode + 2[bp]
3206 cmp ah, #ATA_MODE_PIO32
3207 je ata_out_32
3208
3209 ata_out_16:
3210 seg ES
3211 rep
3212 outsw ;; CX words transfered from port(DX) to ES:[SI]
3213 jmp ata_out_done
3214
3215 ata_out_32:
3216 seg ES
3217 rep
3218 outsd ;; CX dwords transfered from port(DX) to ES:[SI]
3219
3220 ata_out_done:
3221 mov _ata_cmd_data_out.offset + 2[bp], si
3222 mov _ata_cmd_data_out.segment + 2[bp], es
3223 pop bp
3224 ASM_END
3225
3226 current++;
3227 write_word(ebda_seg, &EbdaData->ata.trsfsectors,current);
3228 count--;
3229 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
3230 if (count == 0) {
3231 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
3232 != ATA_CB_STAT_RDY ) {
3233 BX_DEBUG_ATA("ata_cmd_data_out : no sectors left (status %02x)\n", (unsigned) status);
3234 return 6;
3235 }
3236 break;
3237 }
3238 else {
3239 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
3240 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
3241 BX_DEBUG_ATA("ata_cmd_data_out : more sectors left (status %02x)\n", (unsigned) status);
3242 return 7;
3243 }
3244 continue;
3245 }
3246 }
3247 // Enable interrupts
3248 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
3249 return 0;
3250 }
3251
3252 // ---------------------------------------------------------------------------
3253 // ATA/ATAPI driver : execute a packet command
3254 // ---------------------------------------------------------------------------
3255 // returns
3256 // 0 : no error
3257 // 1 : error in parameters
3258 // 2 : BUSY bit set
3259 // 3 : error
3260 // 4 : not ready
3261 Bit16u ata_cmd_packet(device, cmdlen, cmdseg, cmdoff, header, length, inout, bufseg, bufoff)
3262 Bit8u cmdlen,inout;
3263 Bit16u device,cmdseg, cmdoff, bufseg, bufoff;
3264 Bit16u header;
3265 Bit32u length;
3266 {
3267 Bit16u ebda_seg=read_word(0x0040,0x000E);
3268 Bit16u iobase1, iobase2;
3269 Bit16u lcount, lbefore, lafter, count;
3270 Bit8u channel, slave;
3271 Bit8u status, mode, lmode;
3272 Bit32u total, transfer;
3273
3274 channel = device / 2;
3275 slave = device % 2;
3276
3277 // Data out is not supported yet
3278 if (inout == ATA_DATA_OUT) {
3279 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
3280 return 1;
3281 }
3282
3283 // The header length must be even
3284 if (header & 1) {
3285 BX_DEBUG_ATA("ata_cmd_packet : header must be even (%04x)\n",header);
3286 return 1;
3287 }
3288
3289 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
3290 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
3291 mode = read_byte(ebda_seg, &EbdaData->ata.devices[device].mode);
3292 transfer= 0L;
3293
3294 if (cmdlen < 12) cmdlen=12;
3295 if (cmdlen > 12) cmdlen=16;
3296 cmdlen>>=1;
3297
3298 // Reset count of transferred data
3299 write_word(ebda_seg, &EbdaData->ata.trsfsectors,0);
3300 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,0L);
3301
3302 status = inb(iobase1 + ATA_CB_STAT);
3303 if (status & ATA_CB_STAT_BSY) return 2;
3304
3305 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
3306 outb(iobase1 + ATA_CB_FR, 0x00);
3307 outb(iobase1 + ATA_CB_SC, 0x00);
3308 outb(iobase1 + ATA_CB_SN, 0x00);
3309 outb(iobase1 + ATA_CB_CL, 0xfff0 & 0x00ff);
3310 outb(iobase1 + ATA_CB_CH, 0xfff0 >> 8);
3311 outb(iobase1 + ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
3312 outb(iobase1 + ATA_CB_CMD, ATA_CMD_PACKET);
3313
3314 // Device should ok to receive command
3315 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
3316
3317 if (status & ATA_CB_STAT_ERR) {
3318 BX_DEBUG_ATA("ata_cmd_packet : error, status is %02x\n",status);
3319 return 3;
3320 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
3321 BX_DEBUG_ATA("ata_cmd_packet : DRQ not set (status %02x)\n", (unsigned) status);
3322 return 4;
3323 }
3324
3325 // Normalize address
3326 cmdseg += (cmdoff / 16);
3327 cmdoff %= 16;
3328
3329 // Send command to device
3330 ASM_START
3331 sti ;; enable higher priority interrupts
3332
3333 push bp
3334 mov bp, sp
3335
3336 mov si, _ata_cmd_packet.cmdoff + 2[bp]
3337 mov ax, _ata_cmd_packet.cmdseg + 2[bp]
3338 mov cx, _ata_cmd_packet.cmdlen + 2[bp]
3339 mov es, ax ;; segment in es
3340
3341 mov dx, _ata_cmd_packet.iobase1 + 2[bp] ;; ATA data write port
3342
3343 seg ES
3344 rep
3345 outsw ;; CX words transfered from port(DX) to ES:[SI]
3346
3347 pop bp
3348 ASM_END
3349
3350 if (inout == ATA_DATA_NO) {
3351 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
3352 }
3353 else {
3354 Bit16u loops = 0;
3355 Bit8u sc;
3356 while (1) {
3357
3358 if (loops == 0) {//first time through
3359 status = inb(iobase2 + ATA_CB_ASTAT);
3360 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
3361 }
3362 else
3363 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
3364 loops++;
3365
3366 sc = inb(iobase1 + ATA_CB_SC);
3367
3368 // Check if command completed
3369 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
3370 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY)) break;
3371
3372 if (status & ATA_CB_STAT_ERR) {
3373 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status);
3374 return 3;
3375 }
3376
3377 // Normalize address
3378 bufseg += (bufoff / 16);
3379 bufoff %= 16;
3380
3381 // Get the byte count
3382 lcount = ((Bit16u)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
3383
3384 // adjust to read what we want
3385 if(header>lcount) {
3386 lbefore=lcount;
3387 header-=lcount;
3388 lcount=0;
3389 }
3390 else {
3391 lbefore=header;
3392 header=0;
3393 lcount-=lbefore;
3394 }
3395
3396 if(lcount>length) {
3397 lafter=lcount-length;
3398 lcount=length;
3399 length=0;
3400 }
3401 else {
3402 lafter=0;
3403 length-=lcount;
3404 }
3405
3406 // Save byte count
3407 count = lcount;
3408
3409 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) ",lbefore+lcount+lafter,lbefore,lcount,lafter);
3410 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",bufseg,bufoff);
3411
3412 // If counts not dividable by 4, use 16bits mode
3413 lmode = mode;
3414 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
3415 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
3416 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
3417
3418 // adds an extra byte if count are odd. before is always even
3419 if (lcount & 0x01) {
3420 lcount+=1;
3421 if ((lafter > 0) && (lafter & 0x01)) {
3422 lafter-=1;
3423 }
3424 }
3425
3426 if (lmode == ATA_MODE_PIO32) {
3427 lcount>>=2; lbefore>>=2; lafter>>=2;
3428 }
3429 else {
3430 lcount>>=1; lbefore>>=1; lafter>>=1;
3431 }
3432
3433 ; // FIXME bcc bug
3434
3435 ASM_START
3436 push bp
3437 mov bp, sp
3438
3439 mov dx, _ata_cmd_packet.iobase1 + 2[bp] ;; ATA data read port
3440
3441 mov cx, _ata_cmd_packet.lbefore + 2[bp]
3442 jcxz ata_packet_no_before
3443
3444 mov ah, _ata_cmd_packet.lmode + 2[bp]
3445 cmp ah, #ATA_MODE_PIO32
3446 je ata_packet_in_before_32
3447
3448 ata_packet_in_before_16:
3449 in ax, dx
3450 loop ata_packet_in_before_16
3451 jmp ata_packet_no_before
3452
3453 ata_packet_in_before_32:
3454 push eax
3455 ata_packet_in_before_32_loop:
3456 in eax, dx
3457 loop ata_packet_in_before_32_loop
3458 pop eax
3459
3460 ata_packet_no_before:
3461 mov cx, _ata_cmd_packet.lcount + 2[bp]
3462 jcxz ata_packet_after
3463
3464 mov di, _ata_cmd_packet.bufoff + 2[bp]
3465 mov ax, _ata_cmd_packet.bufseg + 2[bp]
3466 mov es, ax
3467
3468 mov ah, _ata_cmd_packet.lmode + 2[bp]
3469 cmp ah, #ATA_MODE_PIO32
3470 je ata_packet_in_32
3471
3472 ata_packet_in_16:
3473 rep
3474 insw ;; CX words transfered tp port(DX) to ES:[DI]
3475 jmp ata_packet_after
3476
3477 ata_packet_in_32:
3478 rep
3479 insd ;; CX dwords transfered to port(DX) to ES:[DI]
3480
3481 ata_packet_after:
3482 mov cx, _ata_cmd_packet.lafter + 2[bp]
3483 jcxz ata_packet_done
3484
3485 mov ah, _ata_cmd_packet.lmode + 2[bp]
3486 cmp ah, #ATA_MODE_PIO32
3487 je ata_packet_in_after_32
3488
3489 ata_packet_in_after_16:
3490 in ax, dx
3491 loop ata_packet_in_after_16
3492 jmp ata_packet_done
3493
3494 ata_packet_in_after_32:
3495 push eax
3496 ata_packet_in_after_32_loop:
3497 in eax, dx
3498 loop ata_packet_in_after_32_loop
3499 pop eax
3500
3501 ata_packet_done:
3502 pop bp
3503 ASM_END
3504
3505 // Compute new buffer address
3506 bufoff += count;
3507
3508 // Save transferred bytes count
3509 transfer += count;
3510 write_dword(ebda_seg, &EbdaData->ata.trsfbytes,transfer);
3511 }
3512 }
3513
3514 // Final check, device must be ready
3515 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
3516 != ATA_CB_STAT_RDY ) {
3517 BX_DEBUG_ATA("ata_cmd_packet : not ready (status %02x)\n", (unsigned) status);
3518 return 4;
3519 }
3520
3521 // Enable interrupts
3522 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
3523 return 0;
3524 }
3525
3526 // ---------------------------------------------------------------------------
3527 // End of ATA/ATAPI Driver
3528 // ---------------------------------------------------------------------------
3529
3530 // ---------------------------------------------------------------------------
3531 // Start of ATA/ATAPI generic functions
3532 // ---------------------------------------------------------------------------
3533
3534 Bit16u
3535 atapi_get_sense(device, seg, asc, ascq)
3536 Bit16u device;
3537 {
3538 Bit8u atacmd[12];
3539 Bit8u buffer[18];
3540 Bit8u i;
3541
3542 memsetb(get_SS(),atacmd,0,12);
3543
3544 // Request SENSE
3545 atacmd[0]=ATA_CMD_REQUEST_SENSE;
3546 atacmd[4]=sizeof(buffer);
3547 if (ata_cmd_packet(device, 12, get_SS(), atacmd, 0, 18L, ATA_DATA_IN, get_SS(), buffer) != 0)
3548 return 0x0002;
3549
3550 write_byte(seg,asc,buffer[12]);
3551 write_byte(seg,ascq,buffer[13]);
3552
3553 return 0;
3554 }
3555
3556 Bit16u
3557 atapi_is_ready(device)
3558 Bit16u device;
3559 {
3560 Bit8u packet[12];
3561 Bit8u buf[8];
3562 Bit32u block_len;
3563 Bit32u sectors;
3564 Bit32u timeout; //measured in ms
3565 Bit32u time;
3566 Bit8u asc, ascq;
3567 Bit8u in_progress;
3568 Bit16u ebda_seg = read_word(0x0040,0x000E);
3569 if (read_byte(ebda_seg,&EbdaData->ata.devices[device].type) != ATA_TYPE_ATAPI) {
3570 printf("not implemented for non-ATAPI device\n");
3571 return -1;
3572 }
3573
3574 BX_DEBUG_ATA("ata_detect_medium: begin\n");
3575 memsetb(get_SS(),packet, 0, sizeof packet);
3576 packet[0] = 0x25; /* READ CAPACITY */
3577
3578 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
3579 * is reported by the device. If the device reports "IN PROGRESS",
3580 * 30 seconds is added. */
3581 timeout = 5000;
3582 time = 0;
3583 in_progress = 0;
3584 while (time < timeout) {
3585 if (ata_cmd_packet(device, sizeof(packet), get_SS(), packet, 0, 8L, ATA_DATA_IN, get_SS(), buf) == 0)
3586 goto ok;
3587
3588 if (atapi_get_sense(device, get_SS(), &asc, &ascq) == 0) {
3589 if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
3590 BX_DEBUG_ATA("Device reports MEDIUM NOT PRESENT\n");
3591 return -1;
3592 }
3593
3594 if (asc == 0x04 && ascq == 0x01 && !in_progress) {
3595 /* IN PROGRESS OF BECOMING READY */
3596 printf("Waiting for device to detect medium... ");
3597 /* Allow 30 seconds more */
3598 timeout = 30000;
3599 in_progress = 1;
3600 }
3601 }
3602 time += 100;
3603 }
3604 BX_DEBUG_ATA("read capacity failed\n");
3605 return -1;
3606 ok:
3607
3608 block_len = (Bit32u) buf[4] << 24
3609 | (Bit32u) buf[5] << 16
3610 | (Bit32u) buf[6] << 8
3611 | (Bit32u) buf[7] << 0;
3612 BX_DEBUG_ATA("block_len=%u\n", block_len);
3613
3614 if (block_len!= 2048 && block_len!= 512)
3615 {
3616 printf("Unsupported sector size %u\n", block_len);
3617 return -1;
3618 }
3619 write_dword(ebda_seg,&EbdaData->ata.devices[device].blksize, block_len);
3620
3621 sectors = (Bit32u) buf[0] << 24
3622 | (Bit32u) buf[1] << 16
3623 | (Bit32u) buf[2] << 8
3624 | (Bit32u) buf[3] << 0;
3625
3626 BX_DEBUG_ATA("sectors=%u\n", sectors);
3627 if (block_len == 2048)
3628 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
3629 if (sectors != read_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_low))
3630 printf("%dMB medium detected\n", sectors>>(20-9));
3631 write_dword(ebda_seg,&EbdaData->ata.devices[device].sectors_low, sectors);
3632 return 0;
3633 }
3634
3635 Bit16u
3636 atapi_is_cdrom(device)
3637 Bit8u device;
3638 {
3639 Bit16u ebda_seg=read_word(0x0040,0x000E);
3640
3641 if (device >= BX_MAX_ATA_DEVICES)
3642 return 0;
3643
3644 if (read_byte(ebda_seg,&EbdaData->ata.devices[device].type) != ATA_TYPE_ATAPI)
3645 return 0;
3646
3647 if (read_byte(ebda_seg,&EbdaData->ata.devices[device].device) != ATA_DEVICE_CDROM)
3648 return 0;
3649
3650 return 1;
3651 }
3652
3653 // ---------------------------------------------------------------------------
3654 // End of ATA/ATAPI generic functions
3655 // ---------------------------------------------------------------------------
3656
3657 #endif // BX_USE_ATADRV
3658
3659 #if BX_ELTORITO_BOOT
3660
3661 // ---------------------------------------------------------------------------
3662 // Start of El-Torito boot functions
3663 // ---------------------------------------------------------------------------
3664
3665 void
3666 cdemu_init()
3667 {
3668 Bit16u ebda_seg=read_word(0x0040,0x000E);
3669
3670 // the only important data is this one for now
3671 write_byte(ebda_seg,&EbdaData->cdemu.active,0x00);
3672 }
3673
3674 Bit8u
3675 cdemu_isactive()
3676 {
3677 Bit16u ebda_seg=read_word(0x0040,0x000E);
3678
3679 return(read_byte(ebda_seg,&EbdaData->cdemu.active));
3680 }
3681
3682 Bit8u
3683 cdemu_emulated_drive()
3684 {
3685 Bit16u ebda_seg=read_word(0x0040,0x000E);
3686
3687 return(read_byte(ebda_seg,&EbdaData->cdemu.emulated_drive));
3688 }
3689
3690 static char isotag[6]="CD001";
3691 static char eltorito[24]="EL TORITO SPECIFICATION";
3692 //
3693 // Returns ah: emulated drive, al: error code
3694 //
3695 Bit16u
3696 cdrom_boot()
3697 {
3698 Bit16u ebda_seg=read_word(0x0040,0x000E);
3699 Bit8u atacmd[12], buffer[2048];
3700 Bit32u lba;
3701 Bit16u boot_segment, nbsectors, i, error;
3702 Bit8u device;
3703
3704 // Find out the first cdrom
3705 for (device=0; device<BX_MAX_ATA_DEVICES;device++) {
3706 if (atapi_is_cdrom(device)) break;
3707 }
3708
3709 // if not found
3710 if(device >= BX_MAX_ATA_DEVICES) return 2;
3711
3712 if(error = atapi_is_ready(device) != 0)
3713 BX_INFO("ata_is_ready returned %d\n",error);
3714
3715 // Read the Boot Record Volume Descriptor
3716 memsetb(get_SS(),atacmd,0,12);
3717 atacmd[0]=0x28; // READ command
3718 atacmd[7]=(0x01 & 0xff00) >> 8; // Sectors
3719 atacmd[8]=(0x01 & 0x00ff); // Sectors
3720 atacmd[2]=(0x11 & 0xff000000) >> 24; // LBA
3721 atacmd[3]=(0x11 & 0x00ff0000) >> 16;
3722 atacmd[4]=(0x11 & 0x0000ff00) >> 8;
3723 atacmd[5]=(0x11 & 0x000000ff);
3724 if((error = ata_cmd_packet(device, 12, get_SS(), atacmd, 0, 2048L, ATA_DATA_IN, get_SS(), buffer)) != 0)
3725 return 3;
3726
3727 // Validity checks
3728 if(buffer[0]!=0)return 4;
3729 for(i=0;i<5;i++){
3730 if(read_byte(get_SS(),&buffer[1+i])!=read_byte(0xf000,&isotag[i]))return 5;
3731 }
3732 for(i=0;i<23;i++)
3733 if(read_byte(get_SS(),&buffer[7+i])!=read_byte(0xf000,&eltorito[i]))return 6;
3734
3735 // ok, now we calculate the Boot catalog address
3736 lba=buffer[0x4A]*0x1000000+buffer[0x49]*0x10000+buffer[0x48]*0x100+buffer[0x47];
3737
3738 // And we read the Boot Catalog
3739 memsetb(get_SS(),atacmd,0,12);
3740 atacmd[0]=0x28; // READ command
3741 atacmd[7]=(0x01 & 0xff00) >> 8; // Sectors
3742 atacmd[8]=(0x01 & 0x00ff); // Sectors
3743 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
3744 atacmd[3]=(lba & 0x00ff0000) >> 16;
3745 atacmd[4]=(lba & 0x0000ff00) >> 8;
3746 atacmd[5]=(lba & 0x000000ff);
3747 if((error = ata_cmd_packet(device, 12, get_SS(), atacmd, 0, 2048L, ATA_DATA_IN, get_SS(), buffer)) != 0)
3748 return 7;
3749
3750 // Validation entry
3751 if(buffer[0x00]!=0x01)return 8; // Header
3752 if(buffer[0x01]!=0x00)return 9; // Platform
3753 if(buffer[0x1E]!=0x55)return 10; // key 1
3754 if(buffer[0x1F]!=0xAA)return 10; // key 2
3755
3756 // Initial/Default Entry
3757 if(buffer[0x20]!=0x88)return 11; // Bootable
3758
3759 #if BX_TCGBIOS
3760 /* specs: 8.2.3 step 5 and 8.2.5.6, measure El Torito boot catalog */
3761 /* measure 2048 bytes (one sector) */
3762 tcpa_add_bootdevice((Bit32u)1L, (Bit32u)0L); /* bootcd = 1 */
3763 tcpa_ipl((Bit32u)2L,(Bit32u)get_SS(),(Bit32u)buffer,(Bit32u)2048L);
3764 #endif
3765
3766 write_byte(ebda_seg,&EbdaData->cdemu.media,buffer[0x21]);
3767 if(buffer[0x21]==0){
3768 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
3769 // Win2000 cd boot needs to know it booted from cd
3770 write_byte(ebda_seg,&EbdaData->cdemu.emulated_drive,0xE0);
3771 }
3772 else if(buffer[0x21]<4)
3773 write_byte(ebda_seg,&EbdaData->cdemu.emulated_drive,0x00);
3774 else
3775 write_byte(ebda_seg,&EbdaData->cdemu.emulated_drive,0x80);
3776
3777 write_byte(ebda_seg,&EbdaData->cdemu.controller_index,device/2);
3778 write_byte(ebda_seg,&EbdaData->cdemu.device_spec,device%2);
3779
3780 boot_segment=buffer[0x23]*0x100+buffer[0x22];
3781 if(boot_segment==0x0000)boot_segment=0x07C0;
3782
3783 write_word(ebda_seg,&EbdaData->cdemu.load_segment,boot_segment);
3784 write_word(ebda_seg,&EbdaData->cdemu.buffer_segment,0x0000);
3785
3786 nbsectors=buffer[0x27]*0x100+buffer[0x26];
3787 write_word(ebda_seg,&EbdaData->cdemu.sector_count,nbsectors);
3788
3789 lba=buffer[0x2B]*0x1000000+buffer[0x2A]*0x10000+buffer[0x29]*0x100+buffer[0x28];
3790 write_dword(ebda_seg,&EbdaData->cdemu.ilba,lba);
3791
3792 // And we read the image in memory
3793 memsetb(get_SS(),atacmd,0,12);
3794 atacmd[0]=0x28; // READ command
3795 atacmd[7]=((1+(nbsectors-1)/4) & 0xff00) >> 8; // Sectors
3796 atacmd[8]=((1+(nbsectors-1)/4) & 0x00ff); // Sectors
3797 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
3798 atacmd[3]=(lba & 0x00ff0000) >> 16;
3799 atacmd[4]=(lba & 0x0000ff00) >> 8;
3800 atacmd[5]=(lba & 0x000000ff);
3801 if((error = ata_cmd_packet(device, 12, get_SS(), atacmd, 0, nbsectors*512L, ATA_DATA_IN, boot_segment,0)) != 0)
3802 return 12;
3803
3804 #if BX_TCGBIOS
3805 /* specs: 8.2.3 step 4 and 8.2.5.6, measure El Torito boot image */
3806 /* measure 1st 512 bytes */
3807 tcpa_ipl((Bit32u)1L,(Bit32u)boot_segment,(Bit32u)0L,(Bit32u)512L);
3808 #endif
3809
3810 // Remember the media type
3811 switch(read_byte(ebda_seg,&EbdaData->cdemu.media)) {
3812 case 0x01: // 1.2M floppy
3813 write_word(ebda_seg,&EbdaData->cdemu.vdevice.spt,15);
3814 write_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders,80);
3815 write_word(ebda_seg,&EbdaData->cdemu.vdevice.heads,2);
3816 break;
3817 case 0x02: // 1.44M floppy
3818 write_word(ebda_seg,&EbdaData->cdemu.vdevice.spt,18);
3819 write_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders,80);
3820 write_word(ebda_seg,&EbdaData->cdemu.vdevice.heads,2);
3821 break;
3822 case 0x03: // 2.88M floppy
3823 write_word(ebda_seg,&EbdaData->cdemu.vdevice.spt,36);
3824 write_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders,80);
3825 write_word(ebda_seg,&EbdaData->cdemu.vdevice.heads,2);
3826 break;
3827 case 0x04: // Harddrive
3828 write_word(ebda_seg,&EbdaData->cdemu.vdevice.spt,read_byte(boot_segment,446+6)&0x3f);
3829 write_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders,
3830 (read_byte(boot_segment,446+6)<<2) + read_byte(boot_segment,446+7) + 1);
3831 write_word(ebda_seg,&EbdaData->cdemu.vdevice.heads,read_byte(boot_segment,446+5) + 1);
3832 break;
3833 }
3834
3835 if(read_byte(ebda_seg,&EbdaData->cdemu.media)!=0) {
3836 // Increase bios installed hardware number of devices
3837 if(read_byte(ebda_seg,&EbdaData->cdemu.emulated_drive)==0x00)
3838 write_byte(0x40,0x10,read_byte(0x40,0x10)|0x41);
3839 else
3840 write_byte(ebda_seg, &EbdaData->ata.hdcount, read_byte(ebda_seg, &EbdaData->ata.hdcount) + 1);
3841 }
3842
3843
3844 // everything is ok, so from now on, the emulation is active
3845 if(read_byte(ebda_seg,&EbdaData->cdemu.media)!=0)
3846 write_byte(ebda_seg,&EbdaData->cdemu.active,0x01);
3847
3848 // return the boot drive + no error
3849 return (read_byte(ebda_seg,&EbdaData->cdemu.emulated_drive)*0x100)+0;
3850 }
3851
3852 // ---------------------------------------------------------------------------
3853 // End of El-Torito boot functions
3854 // ---------------------------------------------------------------------------
3855 #endif // BX_ELTORITO_BOOT
3856
3857 void
3858 int14_function(regs, ds, iret_addr)
3859 pusha_regs_t regs; // regs pushed from PUSHA instruction
3860 Bit16u ds; // previous DS:, DS set to 0x0000 by asm wrapper
3861 iret_addr_t iret_addr; // CS,IP,Flags pushed from original INT call
3862 {
3863 Bit16u addr,timer,val16;
3864 Bit8u timeout;
3865
3866 ASM_START
3867 sti
3868 ASM_END
3869
3870 addr = read_word(0x0040, (regs.u.r16.dx << 1));
3871 timeout = read_byte(0x0040, 0x007C + regs.u.r16.dx);
3872 if ((regs.u.r16.dx < 4) && (addr > 0)) {
3873 switch (regs.u.r8.ah) {
3874 case 0:
3875 outb(addr+3, inb(addr+3) | 0x80);
3876 if (regs.u.r8.al & 0xE0 == 0) {
3877 outb(addr, 0x17);
3878 outb(addr+1, 0x04);
3879 } else {
3880 val16 = 0x600 >> ((regs.u.r8.al & 0xE0) >> 5);
3881 outb(addr, val16 & 0xFF);
3882 outb(addr+1, val16 >> 8);
3883 }
3884 outb(addr+3, regs.u.r8.al & 0x1F);
3885 regs.u.r8.ah = inb(addr+5);
3886 regs.u.r8.al = inb(addr+6);
3887 ClearCF(iret_addr.flags);
3888 break;
3889 case 1:
3890 timer = read_word(0x0040, 0x006C);
3891 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
3892 val16 = read_word(0x0040, 0x006C);
3893 if (val16 != timer) {
3894 timer = val16;
3895 timeout--;
3896 }
3897 }
3898 if (timeout) outb(addr, regs.u.r8.al);
3899 regs.u.r8.ah = inb(addr+5);
3900 if (!timeout) regs.u.r8.ah |= 0x80;
3901 ClearCF(iret_addr.flags);
3902 break;
3903 case 2:
3904 timer = read_word(0x0040, 0x006C);
3905 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
3906 val16 = read_word(0x0040, 0x006C);
3907 if (val16 != timer) {
3908 timer = val16;
3909 timeout--;
3910 }
3911 }
3912 if (timeout) {
3913 regs.u.r8.ah = 0;
3914 regs.u.r8.al = inb(addr);
3915 } else {
3916 regs.u.r8.ah = inb(addr+5);
3917 }
3918 ClearCF(iret_addr.flags);
3919 break;
3920 case 3:
3921 regs.u.r8.ah = inb(addr+5);
3922 regs.u.r8.al = inb(addr+6);
3923 ClearCF(iret_addr.flags);
3924 break;
3925 default:
3926 SetCF(iret_addr.flags); // Unsupported
3927 }
3928 } else {
3929 SetCF(iret_addr.flags); // Unsupported
3930 }
3931 }
3932
3933 void
3934 int15_function(regs, ES, DS, FLAGS)
3935 pusha_regs_t regs; // REGS pushed via pusha
3936 Bit16u ES, DS, FLAGS;
3937 {
3938 Bit16u ebda_seg=read_word(0x0040,0x000E);
3939 bx_bool prev_a20_enable;
3940 Bit16u base15_00;
3941 Bit8u base23_16;
3942 Bit16u ss;
3943 Bit16u CX,DX;
3944
3945 Bit16u bRegister;
3946 Bit8u irqDisable;
3947
3948 BX_DEBUG_INT15("int15 AX=%04x\n",regs.u.r16.ax);
3949
3950 switch (regs.u.r8.ah) {
3951 case 0x24: /* A20 Control */
3952 switch (regs.u.r8.al) {
3953 case 0x00:
3954 set_enable_a20(0);
3955 CLEAR_CF();
3956 regs.u.r8.ah = 0;
3957 break;
3958 case 0x01:
3959 set_enable_a20(1);
3960 CLEAR_CF();
3961 regs.u.r8.ah = 0;
3962 break;
3963 case 0x02:
3964 regs.u.r8.al = (inb(0x92) >> 1) & 0x01;
3965 CLEAR_CF();
3966 regs.u.r8.ah = 0;
3967 break;
3968 case 0x03:
3969 CLEAR_CF();
3970 regs.u.r8.ah = 0;
3971 regs.u.r16.bx = 3;
3972 break;
3973 default:
3974 BX_INFO("int15: Func 24h, subfunc %02xh, A20 gate control not supported\n", (unsigned) regs.u.r8.al);
3975 SET_CF();
3976 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
3977 }
3978 break;
3979
3980 case 0x41:
3981 SET_CF();
3982 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
3983 break;
3984
3985 case 0x4f:
3986 /* keyboard intercept */
3987 #if BX_CPU < 2
3988 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
3989 #else
3990 // nop
3991 #endif
3992 SET_CF();
3993 break;
3994
3995 case 0x52: // removable media eject
3996 CLEAR_CF();
3997 regs.u.r8.ah = 0; // "ok ejection may proceed"
3998 break;
3999
4000 case 0x83: {
4001 if( regs.u.r8.al == 0 ) {
4002 // Set Interval requested.
4003 if( ( read_byte( 0x40, 0xA0 ) & 1 ) == 0 ) {
4004 // Interval not already set.
4005 write_byte( 0x40, 0xA0, 1 ); // Set status byte.
4006 write_word( 0x40, 0x98, ES ); // Byte location, segment
4007 write_word( 0x40, 0x9A, regs.u.r16.bx ); // Byte location, offset
4008 write_word( 0x40, 0x9C, regs.u.r16.dx ); // Low word, delay
4009 write_word( 0x40, 0x9E, regs.u.r16.cx ); // High word, delay.
4010 CLEAR_CF( );
4011 irqDisable = inb( 0xA1 );
4012 outb( 0xA1, irqDisable & 0xFE );
4013 bRegister = inb_cmos( 0xB ); // Unmask IRQ8 so INT70 will get through.
4014 outb_cmos( 0xB, bRegister | 0x40 ); // Turn on the Periodic Interrupt timer
4015 } else {
4016 // Interval already set.
4017 BX_DEBUG_INT15("int15: Func 83h, failed, already waiting.\n" );
4018 SET_CF();
4019 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4020 }
4021 } else if( regs.u.r8.al == 1 ) {
4022 // Clear Interval requested
4023 write_byte( 0x40, 0xA0, 0 ); // Clear status byte
4024 CLEAR_CF( );
4025 bRegister = inb_cmos( 0xB );
4026 outb_cmos( 0xB, bRegister & ~0x40 ); // Turn off the Periodic Interrupt timer
4027 } else {
4028 BX_DEBUG_INT15("int15: Func 83h, failed.\n" );
4029 SET_CF();
4030 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4031 regs.u.r8.al--;
4032 }
4033
4034 break;
4035 }
4036
4037 case 0x87:
4038 #if BX_CPU < 3
4039 # error "Int15 function 87h not supported on < 80386"
4040 #endif
4041 // +++ should probably have descriptor checks
4042 // +++ should have exception handlers
4043
4044 // turn off interrupts
4045 ASM_START
4046 cli
4047 ASM_END
4048
4049 prev_a20_enable = set_enable_a20(1); // enable A20 line
4050
4051 // 128K max of transfer on 386+ ???
4052 // source == destination ???
4053
4054 // ES:SI points to descriptor table
4055 // offset use initially comments
4056 // ==============================================
4057 // 00..07 Unused zeros Null descriptor
4058 // 08..0f GDT zeros filled in by BIOS
4059 // 10..17 source ssssssss source of data
4060 // 18..1f dest dddddddd destination of data
4061 // 20..27 CS zeros filled in by BIOS
4062 // 28..2f SS zeros filled in by BIOS
4063
4064 //es:si
4065 //eeee0
4066 //0ssss
4067 //-----
4068
4069 // check for access rights of source & dest here
4070
4071 // Initialize GDT descriptor
4072 base15_00 = (ES << 4) + regs.u.r16.si;
4073 base23_16 = ES >> 12;
4074 if (base15_00 < (ES<<4))
4075 base23_16++;
4076 write_word(ES, regs.u.r16.si+0x08+0, 47); // limit 15:00 = 6 * 8bytes/descriptor
4077 write_word(ES, regs.u.r16.si+0x08+2, base15_00);// base 15:00
4078 write_byte(ES, regs.u.r16.si+0x08+4, base23_16);// base 23:16
4079 write_byte(ES, regs.u.r16.si+0x08+5, 0x93); // access
4080 write_word(ES, regs.u.r16.si+0x08+6, 0x0000); // base 31:24/reserved/limit 19:16
4081
4082 // Initialize CS descriptor
4083 write_word(ES, regs.u.r16.si+0x20+0, 0xffff);// limit 15:00 = normal 64K limit
4084 write_word(ES, regs.u.r16.si+0x20+2, 0x0000);// base 15:00
4085 write_byte(ES, regs.u.r16.si+0x20+4, 0x000f);// base 23:16
4086 write_byte(ES, regs.u.r16.si+0x20+5, 0x9b); // access
4087 write_word(ES, regs.u.r16.si+0x20+6, 0x0000);// base 31:24/reserved/limit 19:16
4088
4089 // Initialize SS descriptor
4090 ss = get_SS();
4091 base15_00 = ss << 4;
4092 base23_16 = ss >> 12;
4093 write_word(ES, regs.u.r16.si+0x28+0, 0xffff); // limit 15:00 = normal 64K limit
4094 write_word(ES, regs.u.r16.si+0x28+2, base15_00);// base 15:00
4095 write_byte(ES, regs.u.r16.si+0x28+4, base23_16);// base 23:16
4096 write_byte(ES, regs.u.r16.si+0x28+5, 0x93); // access
4097 write_word(ES, regs.u.r16.si+0x28+6, 0x0000); // base 31:24/reserved/limit 19:16
4098
4099 CX = regs.u.r16.cx;
4100 ASM_START
4101 // Compile generates locals offset info relative to SP.
4102 // Get CX (word count) from stack.
4103 mov bx, sp
4104 SEG SS
4105 mov cx, _int15_function.CX [bx]
4106
4107 // since we need to set SS:SP, save them to the BDA
4108 // for future restore
4109 push eax
4110 xor eax, eax
4111 mov ds, ax
4112 mov 0x0469, ss
4113 mov 0x0467, sp
4114
4115 SEG ES
4116 lgdt [si + 0x08]
4117 SEG CS
4118 lidt [pmode_IDT_info]
4119 ;; perhaps do something with IDT here
4120
4121 ;; set PE bit in CR0
4122 mov eax, cr0
4123 or al, #0x01
4124 mov cr0, eax
4125 ;; far jump to flush CPU queue after transition to protected mode
4126 JMP_AP(0x0020, protected_mode)
4127
4128 protected_mode:
4129 ;; GDT points to valid descriptor table, now load SS, DS, ES
4130 mov ax, #0x28 ;; 101 000 = 5th descriptor in table, TI=GDT, RPL=00
4131 mov ss, ax
4132 mov ax, #0x10 ;; 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
4133 mov ds, ax
4134 mov ax, #0x18 ;; 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
4135 mov es, ax
4136 xor si, si
4137 xor di, di
4138 cld
4139 rep
4140 movsw ;; move CX words from DS:SI to ES:DI
4141
4142 ;; make sure DS and ES limits are 64KB
4143 mov ax, #0x28
4144 mov ds, ax
4145 mov es, ax
4146
4147 ;; reset PG bit in CR0 ???
4148 mov eax, cr0
4149 and al, #0xFE
4150 mov cr0, eax
4151
4152 ;; far jump to flush CPU queue after transition to real mode
4153 JMP_AP(0xf000, real_mode)
4154
4155 real_mode:
4156 ;; restore IDT to normal real-mode defaults
4157 SEG CS
4158 lidt [rmode_IDT_info]
4159
4160 // restore SS:SP from the BDA
4161 xor ax, ax
4162 mov ds, ax
4163 mov ss, 0x0469
4164 mov sp, 0x0467
4165 pop eax
4166 ASM_END
4167
4168 set_enable_a20(prev_a20_enable);
4169
4170 // turn back on interrupts
4171 ASM_START
4172 sti
4173 ASM_END
4174
4175 regs.u.r8.ah = 0;
4176 CLEAR_CF();
4177 break;
4178
4179
4180 case 0x88:
4181 // Get the amount of extended memory (above 1M)
4182 #if BX_CPU < 2
4183 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4184 SET_CF();
4185 #else
4186 regs.u.r8.al = inb_cmos(0x30);
4187 regs.u.r8.ah = inb_cmos(0x31);
4188
4189 // According to Ralf Brown's interrupt the limit should be 15M,
4190 // but real machines mostly return max. 63M.
4191 if(regs.u.r16.ax > 0xffc0)
4192 regs.u.r16.ax = 0xffc0;
4193
4194 CLEAR_CF();
4195 #endif
4196 break;
4197
4198 case 0x90:
4199 /* Device busy interrupt. Called by Int 16h when no key available */
4200 break;
4201
4202 case 0x91:
4203 /* Interrupt complete. Called by Int 16h when key becomes available */
4204 break;
4205
4206 case 0xbf:
4207 BX_INFO("*** int 15h function AH=bf not yet supported!\n");
4208 SET_CF();
4209 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4210 break;
4211
4212 case 0xC0:
4213 #if 0
4214 SET_CF();
4215 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4216 break;
4217 #endif
4218 CLEAR_CF();
4219 regs.u.r8.ah = 0;
4220 regs.u.r16.bx = BIOS_CONFIG_TABLE;
4221 ES = 0xF000;
4222 break;
4223
4224 case 0xc1:
4225 ES = ebda_seg;
4226 CLEAR_CF();
4227 break;
4228
4229 case 0xd8:
4230 bios_printf(BIOS_PRINTF_DEBUG, "EISA BIOS not present\n");
4231 SET_CF();
4232 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4233 break;
4234
4235 default:
4236 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4237 (unsigned) regs.u.r16.ax, (unsigned) regs.u.r16.bx);
4238 SET_CF();
4239 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4240 break;
4241 }
4242 }
4243
4244 #if BX_USE_PS2_MOUSE
4245 void
4246 int15_function_mouse(regs, ES, DS, FLAGS)
4247 pusha_regs_t regs; // REGS pushed via pusha
4248 Bit16u ES, DS, FLAGS;
4249 {
4250 Bit16u ebda_seg=read_word(0x0040,0x000E);
4251 Bit8u mouse_flags_1, mouse_flags_2;
4252 Bit16u mouse_driver_seg;
4253 Bit16u mouse_driver_offset;
4254 Bit8u comm_byte, prev_command_byte;
4255 Bit8u ret, mouse_data1, mouse_data2, mouse_data3;
4256
4257 BX_DEBUG_INT15("int15 AX=%04x\n",regs.u.r16.ax);
4258
4259 switch (regs.u.r8.ah) {
4260 case 0xC2:
4261 // Return Codes status in AH
4262 // =========================
4263 // 00: success
4264 // 01: invalid subfunction (AL > 7)
4265 // 02: invalid input value (out of allowable range)
4266 // 03: interface error
4267 // 04: resend command received from mouse controller,
4268 // device driver should attempt command again
4269 // 05: cannot enable mouse, since no far call has been installed
4270 // 80/86: mouse service not implemented
4271
4272 switch (regs.u.r8.al) {
4273 case 0: // Disable/Enable Mouse
4274 BX_DEBUG_INT15("case 0:\n");
4275 switch (regs.u.r8.bh) {
4276 case 0: // Disable Mouse
4277 BX_DEBUG_INT15("case 0: disable mouse\n");
4278 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4279 ret = send_to_mouse_ctrl(0xF5); // disable mouse command
4280 if (ret == 0) {
4281 ret = get_mouse_data(&mouse_data1);
4282 if ( (ret == 0) || (mouse_data1 == 0xFA) ) {
4283 CLEAR_CF();
4284 regs.u.r8.ah = 0;
4285 return;
4286 }
4287 }
4288
4289 // error
4290 SET_CF();
4291 regs.u.r8.ah = ret;
4292 return;
4293 break;
4294
4295 case 1: // Enable Mouse
4296 BX_DEBUG_INT15("case 1: enable mouse\n");
4297 mouse_flags_2 = read_byte(ebda_seg, 0x0027);
4298 if ( (mouse_flags_2 & 0x80) == 0 ) {
4299 BX_DEBUG_INT15("INT 15h C2 Enable Mouse, no far call handler\n");
4300 SET_CF(); // error
4301 regs.u.r8.ah = 5; // no far call installed
4302 return;
4303 }
4304 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4305 ret = send_to_mouse_ctrl(0xF4); // enable mouse command
4306 if (ret == 0) {
4307 ret = get_mouse_data(&mouse_data1);
4308 if ( (ret == 0) && (mouse_data1 == 0xFA) ) {
4309 enable_mouse_int_and_events(); // turn IRQ12 and packet generation on
4310 CLEAR_CF();
4311 regs.u.r8.ah = 0;
4312 return;
4313 }
4314 }
4315 SET_CF();
4316 regs.u.r8.ah = ret;
4317 return;
4318
4319 default: // invalid subfunction
4320 BX_DEBUG_INT15("INT 15h C2 AL=0, BH=%02x\n", (unsigned) regs.u.r8.bh);
4321 SET_CF(); // error
4322 regs.u.r8.ah = 1; // invalid subfunction
4323 return;
4324 }
4325 break;
4326
4327 case 1: // Reset Mouse
4328 case 5: // Initialize Mouse
4329 BX_DEBUG_INT15("case 1 or 5:\n");
4330 if (regs.u.r8.al == 5) {
4331 if (regs.u.r8.bh != 3) {
4332 SET_CF();
4333 regs.u.r8.ah = 0x02; // invalid input
4334 return;
4335 }
4336 mouse_flags_2 = read_byte(ebda_seg, 0x0027);
4337 mouse_flags_2 = (mouse_flags_2 & 0x00) | regs.u.r8.bh;
4338 mouse_flags_1 = 0x00;
4339 write_byte(ebda_seg, 0x0026, mouse_flags_1);
4340 write_byte(ebda_seg, 0x0027, mouse_flags_2);
4341 }
4342
4343 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4344 ret = send_to_mouse_ctrl(0xFF); // reset mouse command
4345 if (ret == 0) {
4346 ret = get_mouse_data(&mouse_data3);
4347 // if no mouse attached, it will return RESEND
4348 if (mouse_data3 == 0xfe) {
4349 SET_CF();
4350 return;
4351 }
4352 if (mouse_data3 != 0xfa)
4353 BX_PANIC("Mouse reset returned %02x (should be ack)\n", (unsigned)mouse_data3);
4354 if ( ret == 0 ) {
4355 ret = get_mouse_data(&mouse_data1);
4356 if ( ret == 0 ) {
4357 ret = get_mouse_data(&mouse_data2);
4358 if ( ret == 0 ) {
4359 // turn IRQ12 and packet generation on
4360 enable_mouse_int_and_events();
4361 CLEAR_CF();
4362 regs.u.r8.ah = 0;
4363 regs.u.r8.bl = mouse_data1;
4364 regs.u.r8.bh = mouse_data2;
4365 return;
4366 }
4367 }
4368 }
4369 }
4370
4371 // error
4372 SET_CF();
4373 regs.u.r8.ah = ret;
4374 return;
4375
4376 case 2: // Set Sample Rate
4377 BX_DEBUG_INT15("case 2:\n");
4378 switch (regs.u.r8.bh) {
4379 case 0: mouse_data1 = 10; break; // 10 reports/sec
4380 case 1: mouse_data1 = 20; break; // 20 reports/sec
4381 case 2: mouse_data1 = 40; break; // 40 reports/sec
4382 case 3: mouse_data1 = 60; break; // 60 reports/sec
4383 case 4: mouse_data1 = 80; break; // 80 reports/sec
4384 case 5: mouse_data1 = 100; break; // 100 reports/sec (default)
4385 case 6: mouse_data1 = 200; break; // 200 reports/sec
4386 default: mouse_data1 = 0;
4387 }
4388 if (mouse_data1 > 0) {
4389 ret = send_to_mouse_ctrl(0xF3); // set sample rate command
4390 if (ret == 0) {
4391 ret = get_mouse_data(&mouse_data2);
4392 ret = send_to_mouse_ctrl(mouse_data1);
4393 ret = get_mouse_data(&mouse_data2);
4394 CLEAR_CF();
4395 regs.u.r8.ah = 0;
4396 } else {
4397 // error
4398 SET_CF();
4399 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4400 }
4401 } else {
4402 // error
4403 SET_CF();
4404 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4405 }
4406 break;
4407
4408 case 3: // Set Resolution
4409 BX_DEBUG_INT15("case 3:\n");
4410 // BH:
4411 // 0 = 25 dpi, 1 count per millimeter
4412 // 1 = 50 dpi, 2 counts per millimeter
4413 // 2 = 100 dpi, 4 counts per millimeter
4414 // 3 = 200 dpi, 8 counts per millimeter
4415 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4416 if (regs.u.r8.bh < 4) {
4417 ret = send_to_mouse_ctrl(0xE8); // set resolution command
4418 if (ret == 0) {
4419 ret = get_mouse_data(&mouse_data1);
4420 if (mouse_data1 != 0xfa)
4421 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1);
4422 ret = send_to_mouse_ctrl(regs.u.r8.bh);
4423 ret = get_mouse_data(&mouse_data1);
4424 if (mouse_data1 != 0xfa)
4425 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1);
4426 CLEAR_CF();
4427 regs.u.r8.ah = 0;
4428 } else {
4429 // error
4430 SET_CF();
4431 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4432 }
4433 } else {
4434 // error
4435 SET_CF();
4436 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4437 }
4438 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
4439 break;
4440
4441 case 4: // Get Device ID
4442 BX_DEBUG_INT15("case 4:\n");
4443 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4444 ret = send_to_mouse_ctrl(0xF2); // get mouse ID command
4445 if (ret == 0) {
4446 ret = get_mouse_data(&mouse_data1);
4447 ret = get_mouse_data(&mouse_data2);
4448 CLEAR_CF();
4449 regs.u.r8.ah = 0;
4450 regs.u.r8.bh = mouse_data2;
4451 } else {
4452 // error
4453 SET_CF();
4454 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4455 }
4456 break;
4457
4458 case 6: // Return Status & Set Scaling Factor...
4459 BX_DEBUG_INT15("case 6:\n");
4460 switch (regs.u.r8.bh) {
4461 case 0: // Return Status
4462 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4463 ret = send_to_mouse_ctrl(0xE9); // get mouse info command
4464 if (ret == 0) {
4465 ret = get_mouse_data(&mouse_data1);
4466 if (mouse_data1 != 0xfa)
4467 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1);
4468 if (ret == 0) {
4469 ret = get_mouse_data(&mouse_data1);
4470 if ( ret == 0 ) {
4471 ret = get_mouse_data(&mouse_data2);
4472 if ( ret == 0 ) {
4473 ret = get_mouse_data(&mouse_data3);
4474 if ( ret == 0 ) {
4475 CLEAR_CF();
4476 regs.u.r8.ah = 0;
4477 regs.u.r8.bl = mouse_data1;
4478 regs.u.r8.cl = mouse_data2;
4479 regs.u.r8.dl = mouse_data3;
4480 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
4481 return;
4482 }
4483 }
4484 }
4485 }
4486 }
4487
4488 // error
4489 SET_CF();
4490 regs.u.r8.ah = ret;
4491 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
4492 return;
4493
4494 case 1: // Set Scaling Factor to 1:1
4495 case 2: // Set Scaling Factor to 2:1
4496 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4497 if (regs.u.r8.bh == 1) {
4498 ret = send_to_mouse_ctrl(0xE6);
4499 } else {
4500 ret = send_to_mouse_ctrl(0xE7);
4501 }
4502 if (ret == 0) {
4503 get_mouse_data(&mouse_data1);
4504 ret = (mouse_data1 != 0xFA);
4505 }
4506 if (ret == 0) {
4507 CLEAR_CF();
4508 regs.u.r8.ah = 0;
4509 } else {
4510 // error
4511 SET_CF();
4512 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4513 }
4514 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
4515 break;
4516
4517 default:
4518 BX_PANIC("INT 15h C2 AL=6, BH=%02x\n", (unsigned) regs.u.r8.bh);
4519 }
4520 break;
4521
4522 case 7: // Set Mouse Handler Address
4523 BX_DEBUG_INT15("case 7:\n");
4524 mouse_driver_seg = ES;
4525 mouse_driver_offset = regs.u.r16.bx;
4526 write_word(ebda_seg, 0x0022, mouse_driver_offset);
4527 write_word(ebda_seg, 0x0024, mouse_driver_seg);
4528 mouse_flags_2 = read_byte(ebda_seg, 0x0027);
4529 if (mouse_driver_offset == 0 && mouse_driver_seg == 0) {
4530 /* remove handler */
4531 if ( (mouse_flags_2 & 0x80) != 0 ) {
4532 mouse_flags_2 &= ~0x80;
4533 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4534 }
4535 }
4536 else {
4537 /* install handler */
4538 mouse_flags_2 |= 0x80;
4539 }
4540 write_byte(ebda_seg, 0x0027, mouse_flags_2);
4541 CLEAR_CF();
4542 regs.u.r8.ah = 0;
4543 break;
4544
4545 default:
4546 BX_DEBUG_INT15("case default:\n");
4547 regs.u.r8.ah = 1; // invalid function
4548 SET_CF();
4549 }
4550 break;
4551
4552 default:
4553 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4554 (unsigned) regs.u.r16.ax, (unsigned) regs.u.r16.bx);
4555 SET_CF();
4556 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4557 break;
4558 }
4559 }
4560 #endif // BX_USE_PS2_MOUSE
4561
4562
4563 void set_e820_range(ES, DI, start, end, type)
4564 Bit16u ES;
4565 Bit16u DI;
4566 Bit32u start;
4567 Bit32u end;
4568 Bit16u type;
4569 {
4570 write_word(ES, DI, start);
4571 write_word(ES, DI+2, start >> 16);
4572 write_word(ES, DI+4, 0x00);
4573 write_word(ES, DI+6, 0x00);
4574
4575 end -= start;
4576 write_word(ES, DI+8, end);
4577 write_word(ES, DI+10, end >> 16);
4578 write_word(ES, DI+12, 0x0000);
4579 write_word(ES, DI+14, 0x0000);
4580
4581 write_word(ES, DI+16, type);
4582 write_word(ES, DI+18, 0x0);
4583 }
4584
4585 void
4586 int15_function32(regs, ES, DS, FLAGS)
4587 pushad_regs_t regs; // REGS pushed via pushad
4588 Bit16u ES, DS, FLAGS;
4589 {
4590 Bit32u extended_memory_size=0; // 64bits long
4591 Bit16u CX,DX;
4592 #ifdef HVMASSIST
4593 Bit16u off, e820_table_size;
4594 Bit32u base, type, size;
4595 #endif
4596
4597 BX_DEBUG_INT15("int15 AX=%04x\n",regs.u.r16.ax);
4598
4599 switch (regs.u.r8.ah) {
4600 case 0x86:
4601 // Wait for CX:DX microseconds. currently using the
4602 // refresh request port 0x61 bit4, toggling every 15usec
4603
4604 CX = regs.u.r16.cx;
4605 DX = regs.u.r16.dx;
4606
4607 ASM_START
4608 sti
4609
4610 ;; Get the count in eax
4611 mov bx, sp
4612 SEG SS
4613 mov ax, _int15_function32.CX [bx]
4614 shl eax, #16
4615 SEG SS
4616 mov ax, _int15_function32.DX [bx]
4617
4618 ;; convert to numbers of 15usec ticks
4619 mov ebx, #15
4620 xor edx, edx
4621 div eax, ebx
4622 mov ecx, eax
4623
4624 ;; wait for ecx number of refresh requests
4625 in al, #0x61
4626 and al,#0x10
4627 mov ah, al
4628
4629 or ecx, ecx
4630 je int1586_tick_end
4631 int1586_tick:
4632 in al, #0x61
4633 and al,#0x10
4634 cmp al, ah
4635 je int1586_tick
4636 mov ah, al
4637 dec ecx
4638 jnz int1586_tick
4639 int1586_tick_end:
4640 ASM_END
4641
4642 break;
4643
4644 case 0xe8:
4645 switch(regs.u.r8.al)
4646 {
4647 #ifdef HVMASSIST
4648 case 0x20: {
4649 e820_table_size = read_word(E820_SEG, E820_NR_OFFSET) * 0x14;
4650
4651 if (regs.u.r32.edx != 0x534D4150) /* SMAP */
4652 goto int15_unimplemented;
4653
4654 if ((regs.u.r16.bx / 0x14) * 0x14 == regs.u.r16.bx) {
4655 if (regs.u.r16.bx + 0x14 <= e820_table_size)
4656 memcpyb(ES, regs.u.r16.di,
4657 E820_SEG, E820_OFFSET + regs.u.r16.bx, 0x14);
4658 regs.u.r32.ebx += 0x14;
4659 if ((regs.u.r32.ebx + 0x14 - 1) > e820_table_size)
4660 regs.u.r32.ebx = 0;
4661 } else if (regs.u.r16.bx == 1) {
4662 for (off = 0; off < e820_table_size; off += 0x14) {
4663 base = read_dword(E820_SEG, E820_OFFSET + off);
4664 type = read_dword(E820_SEG, E820_OFFSET + 0x10 + off);
4665 if ((base >= 0x100000) && (type == 1))
4666 break;
4667 }
4668 if (off == e820_table_size) {
4669 SET_CF();
4670 break;
4671 }
4672 memcpyb(ES, regs.u.r16.di, E820_SEG, E820_OFFSET + off, 0x14);
4673 regs.u.r32.ebx = 0;
4674 } else { /* AX=E820, DX=534D4150, BX unrecognized */
4675 goto int15_unimplemented;
4676 }
4677
4678 regs.u.r32.eax = 0x534D4150;
4679 regs.u.r32.ecx = 0x14;
4680 CLEAR_CF();
4681 break;
4682 }
4683
4684 case 0x01: {
4685 e820_table_size = read_word(E820_SEG, E820_NR_OFFSET) * 0x14;
4686
4687 // do we have any reason to fail here ?
4688 CLEAR_CF();
4689
4690 // Get the amount of extended memory (above 1M)
4691 regs.u.r8.cl = inb_cmos(0x30);
4692 regs.u.r8.ch = inb_cmos(0x31);
4693
4694 // limit to 15M
4695 if (regs.u.r16.cx > (15*1024))
4696 regs.u.r16.cx = 15*1024;
4697
4698 // Find first RAM E820 entry >= 1MB.
4699 for (off = 0; off < e820_table_size; off += 0x14) {
4700 base = read_dword(E820_SEG, E820_OFFSET + off);
4701 type = read_dword(E820_SEG, E820_OFFSET + 0x10 + off);
4702 if ((base >= 0x100000) && (type == 1))
4703 break;
4704 }
4705
4706 // If there is RAM above 16MB, return amount in 64kB chunks.
4707 regs.u.r16.dx = 0;
4708 if (off != e820_table_size) {
4709 size = base + read_dword(E820_SEG, E820_OFFSET + 0x8 + off);
4710 if (size > 0x1000000) {
4711 size -= 0x1000000;
4712 regs.u.r16.dx = (Bit16u)(size >> 16);
4713 }
4714 }
4715
4716 // Set configured memory equal to extended memory
4717 regs.u.r16.ax = regs.u.r16.cx;
4718 regs.u.r16.bx = regs.u.r16.dx;
4719 break;
4720 }
4721 default: /* AH=0xE8?? but not implemented */
4722 goto int15_unimplemented;
4723 }
4724 break;
4725 int15_unimplemented:
4726 // fall into the default
4727 default:
4728 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4729 (unsigned) regs.u.r16.ax, (unsigned) regs.u.r16.bx);
4730 SET_CF();
4731 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4732 break;
4733 }
4734 #else
4735 case 0x20: // coded by osmaker aka K.J.
4736 if(regs.u.r32.edx == 0x534D4150)
4737 {
4738 extended_memory_size = inb_cmos(0x35);
4739 extended_memory_size <<= 8;
4740 extended_memory_size |= inb_cmos(0x34);
4741 extended_memory_size *= 64;
4742 // greater than EFF00000???
4743 if(extended_memory_size > 0x3bc000) {
4744 extended_memory_size = 0x3bc000; // everything after this is reserved memory until we get to 0x100000000
4745 }
4746 extended_memory_size *= 1024;
4747 extended_memory_size += (16L * 1024 * 1024);
4748
4749 if(extended_memory_size <= (16L * 1024 * 1024)) {
4750 extended_memory_size = inb_cmos(0x31);
4751 extended_memory_size <<= 8;
4752 extended_memory_size |= inb_cmos(0x30);
4753 extended_memory_size *= 1024;
4754 extended_memory_size += (1L * 1024 * 1024);
4755 }
4756
4757 switch(regs.u.r16.bx)
4758 {
4759 case 0:
4760 set_e820_range(ES, regs.u.r16.di,
4761 0x0000000L, 0x0009f000L, 1);
4762 regs.u.r32.ebx = 1;
4763 regs.u.r32.eax = 0x534D4150;
4764 regs.u.r32.ecx = 0x14;
4765 CLEAR_CF();
4766 return;
4767 break;
4768 case 1:
4769 set_e820_range(ES, regs.u.r16.di,
4770 0x0009f000L, 0x000a0000L, 2);
4771 regs.u.r32.ebx = 2;
4772 regs.u.r32.eax = 0x534D4150;
4773 regs.u.r32.ecx = 0x14;
4774 CLEAR_CF();
4775 return;
4776 break;
4777 case 2:
4778 set_e820_range(ES, regs.u.r16.di,
4779 0x000e8000L, 0x00100000L, 2);
4780 regs.u.r32.ebx = 3;
4781 regs.u.r32.eax = 0x534D4150;
4782 regs.u.r32.ecx = 0x14;
4783 CLEAR_CF();
4784 return;
4785 break;
4786 case 3:
4787 #if BX_ROMBIOS32
4788 set_e820_range(ES, regs.u.r16.di,
4789 0x00100000L,
4790 extended_memory_size - ACPI_DATA_SIZE, 1);
4791 regs.u.r32.ebx = 4;
4792 #else
4793 set_e820_range(ES, regs.u.r16.di,
4794 0x00100000L,
4795 extended_memory_size, 1);
4796 regs.u.r32.ebx = 5;
4797 #endif
4798 regs.u.r32.eax = 0x534D4150;
4799 regs.u.r32.ecx = 0x14;
4800 CLEAR_CF();
4801 return;
4802 break;
4803 case 4:
4804 set_e820_range(ES, regs.u.r16.di,
4805 extended_memory_size - ACPI_DATA_SIZE,
4806 extended_memory_size, 3); // ACPI RAM
4807 regs.u.r32.ebx = 5;
4808 regs.u.r32.eax = 0x534D4150;
4809 regs.u.r32.ecx = 0x14;
4810 CLEAR_CF();
4811 return;
4812 break;
4813 case 5:
4814 /* 256KB BIOS area at the end of 4 GB */
4815 set_e820_range(ES, regs.u.r16.di,
4816 0xfffc0000L, 0x00000000L, 2);
4817 regs.u.r32.ebx = 0;
4818 regs.u.r32.eax = 0x534D4150;
4819 regs.u.r32.ecx = 0x14;
4820 CLEAR_CF();
4821 return;
4822 default: /* AX=E820, DX=534D4150, BX unrecognized */
4823 goto int15_unimplemented;
4824 break;
4825 }
4826 } else {
4827 // if DX != 0x534D4150)
4828 goto int15_unimplemented;
4829 }
4830 break;
4831
4832 case 0x01:
4833 // do we have any reason to fail here ?
4834 CLEAR_CF();
4835
4836 // my real system sets ax and bx to 0
4837 // this is confirmed by Ralph Brown list
4838 // but syslinux v1.48 is known to behave
4839 // strangely if ax is set to 0
4840 // regs.u.r16.ax = 0;
4841 // regs.u.r16.bx = 0;
4842
4843 // Get the amount of extended memory (above 1M)
4844 regs.u.r8.cl = inb_cmos(0x30);
4845 regs.u.r8.ch = inb_cmos(0x31);
4846
4847 // limit to 15M
4848 if(regs.u.r16.cx > 0x3c00)
4849 {
4850 regs.u.r16.cx = 0x3c00;
4851 }
4852
4853 // Get the amount of extended memory above 16M in 64k blocs
4854 regs.u.r8.dl = inb_cmos(0x34);
4855 regs.u.r8.dh = inb_cmos(0x35);
4856
4857 // Set configured memory equal to extended memory
4858 regs.u.r16.ax = regs.u.r16.cx;
4859 regs.u.r16.bx = regs.u.r16.dx;
4860 break;
4861 default: /* AH=0xE8?? but not implemented */
4862 goto int15_unimplemented;
4863 }
4864 break;
4865 int15_unimplemented:
4866 // fall into the default
4867 default:
4868 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4869 (unsigned) regs.u.r16.ax, (unsigned) regs.u.r16.bx);
4870 SET_CF();
4871 regs.u.r8.ah = UNSUPPORTED_FUNCTION;
4872 break;
4873 }
4874 #endif /* HVMASSIST */
4875 }
4876
4877 void
4878 int16_function(DI, SI, BP, SP, BX, DX, CX, AX, FLAGS)
4879 Bit16u DI, SI, BP, SP, BX, DX, CX, AX, FLAGS;
4880 {
4881 Bit8u scan_code, ascii_code, shift_flags, led_flags, count;
4882 Bit16u kbd_code, max;
4883
4884 BX_DEBUG_INT16("int16: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX, BX, CX, DX);
4885
4886 shift_flags = read_byte(0x0040, 0x17);
4887 led_flags = read_byte(0x0040, 0x97);
4888 if ((((shift_flags >> 4) & 0x07) ^ (led_flags & 0x07)) != 0) {
4889 ASM_START
4890 cli
4891 ASM_END
4892 outb(0x60, 0xed);
4893 while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
4894 if ((inb(0x60) == 0xfa)) {
4895 led_flags &= 0xf8;
4896 led_flags |= ((shift_flags >> 4) & 0x07);
4897 outb(0x60, led_flags & 0x07);
4898 while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
4899 inb(0x60);
4900 write_byte(0x0040, 0x97, led_flags);
4901 }
4902 ASM_START
4903 sti
4904 ASM_END
4905 }
4906
4907 switch (GET_AH()) {
4908 case 0x00: /* read keyboard input */
4909
4910 if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
4911 BX_PANIC("KBD: int16h: out of keyboard input\n");
4912 }
4913 if (scan_code !=0 && ascii_code == 0xF0) ascii_code = 0;
4914 else if (ascii_code == 0xE0) ascii_code = 0;
4915 AX = (scan_code << 8) | ascii_code;
4916 break;
4917
4918 case 0x01: /* check keyboard status */
4919 if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
4920 SET_ZF();
4921 return;
4922 }
4923 if (scan_code !=0 && ascii_code == 0xF0) ascii_code = 0;
4924 else if (ascii_code == 0xE0) ascii_code = 0;
4925 AX = (scan_code << 8) | ascii_code;
4926 CLEAR_ZF();
4927 break;
4928
4929 case 0x02: /* get shift flag status */
4930 shift_flags = read_byte(0x0040, 0x17);
4931 SET_AL(shift_flags);
4932 break;
4933
4934 case 0x05: /* store key-stroke into buffer */
4935 if ( !enqueue_key(GET_CH(), GET_CL()) ) {
4936 SET_AL(1);
4937 }
4938 else {
4939 SET_AL(0);
4940 }
4941 break;
4942
4943 case 0x09: /* GET KEYBOARD FUNCTIONALITY */
4944 // bit Bochs Description
4945 // 7 0 reserved
4946 // 6 0 INT 16/AH=20h-22h supported (122-key keyboard support)
4947 // 5 1 INT 16/AH=10h-12h supported (enhanced keyboard support)
4948 // 4 1 INT 16/AH=0Ah supported
4949 // 3 0 INT 16/AX=0306h supported
4950 // 2 0 INT 16/AX=0305h supported
4951 // 1 0 INT 16/AX=0304h supported
4952 // 0 0 INT 16/AX=0300h supported
4953 //
4954 SET_AL(0x30);
4955 break;
4956
4957 case 0x0A: /* GET KEYBOARD ID */
4958 count = 2;
4959 kbd_code = 0x0;
4960 outb(0x60, 0xf2);
4961 /* Wait for data */
4962 max=0xffff;
4963 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x00);
4964 if (max>0x0) {
4965 if ((inb(0x60) == 0xfa)) {
4966 do {
4967 max=0xffff;
4968 while ( ((inb(0x64) & 0x01) == 0) && (--max>0) ) outb(0x80, 0x00);
4969 if (max>0x0) {
4970 kbd_code >>= 8;
4971 kbd_code |= (inb(0x60) << 8);
4972 }
4973 } while (--count>0);
4974 }
4975 }
4976 BX=kbd_code;
4977 break;
4978
4979 case 0x10: /* read MF-II keyboard input */
4980
4981 if ( !dequeue_key(&scan_code, &ascii_code, 1) ) {
4982 BX_PANIC("KBD: int16h: out of keyboard input\n");
4983 }
4984 if (scan_code !=0 && ascii_code == 0xF0) ascii_code = 0;
4985 AX = (scan_code << 8) | ascii_code;
4986 break;
4987
4988 case 0x11: /* check MF-II keyboard status */
4989 if ( !dequeue_key(&scan_code, &ascii_code, 0) ) {
4990 SET_ZF();
4991 return;
4992 }
4993 if (scan_code !=0 && ascii_code == 0xF0) ascii_code = 0;
4994 AX = (scan_code << 8) | ascii_code;
4995 CLEAR_ZF();
4996 break;
4997
4998 case 0x12: /* get extended keyboard status */
4999 shift_flags = read_byte(0x0040, 0x17);
5000 SET_AL(shift_flags);
5001 shift_flags = read_byte(0x0040, 0x18) & 0x73;
5002 shift_flags |= read_byte(0x0040, 0x96) & 0x0c;
5003 SET_AH(shift_flags);
5004 BX_DEBUG_INT16("int16: func 12 sending %04x\n",AX);
5005 break;
5006
5007 case 0x92: /* keyboard capability check called by DOS 5.0+ keyb */
5008 SET_AH(0x80); // function int16 ah=0x10-0x12 supported
5009 break;
5010
5011 case 0xA2: /* 122 keys capability check called by DOS 5.0+ keyb */
5012 // don't change AH : function int16 ah=0x20-0x22 NOT supported
5013 break;
5014
5015 case 0x6F:
5016 if (GET_AL() == 0x08)
5017 SET_AH(0x02); // unsupported, aka normal keyboard
5018
5019 default:
5020 BX_INFO("KBD: unsupported int 16h function %02x\n", GET_AH());
5021 }
5022 }
5023
5024 unsigned int
5025 dequeue_key(scan_code, ascii_code, incr)
5026 Bit8u *scan_code;
5027 Bit8u *ascii_code;
5028 unsigned int incr;
5029 {
5030 Bit16u buffer_start, buffer_end, buffer_head, buffer_tail;
5031 Bit16u ss;
5032 Bit8u acode, scode;
5033
5034 #if BX_CPU < 2
5035 buffer_start = 0x001E;
5036 buffer_end = 0x003E;
5037 #else
5038 buffer_start = read_word(0x0040, 0x0080);
5039 buffer_end = read_word(0x0040, 0x0082);
5040 #endif
5041
5042 buffer_head = read_word(0x0040, 0x001a);
5043 buffer_tail = read_word(0x0040, 0x001c);
5044
5045 if (buffer_head != buffer_tail) {
5046 ss = get_SS();
5047 acode = read_byte(0x0040, buffer_head);
5048 scode = read_byte(0x0040, buffer_head+1);
5049 write_byte(ss, ascii_code, acode);
5050 write_byte(ss, scan_code, scode);
5051
5052 if (incr) {
5053 buffer_head += 2;
5054 if (buffer_head >= buffer_end)
5055 buffer_head = buffer_start;
5056 write_word(0x0040, 0x001a, buffer_head);
5057 }
5058 return(1);
5059 }
5060 else {
5061 return(0);
5062 }
5063 }
5064
5065 static char panic_msg_keyb_buffer_full[] = "%s: keyboard input buffer full\n";
5066
5067 Bit8u
5068 inhibit_mouse_int_and_events()
5069 {
5070 Bit8u command_byte, prev_command_byte;
5071
5072 // Turn off IRQ generation and aux data line
5073 if ( inb(0x64) & 0x02 )
5074 BX_PANIC(panic_msg_keyb_buffer_full,"inhibmouse");
5075 outb(0x64, 0x20); // get command byte
5076 while ( (inb(0x64) & 0x01) != 0x01 );
5077 prev_command_byte = inb(0x60);
5078 command_byte = prev_command_byte;
5079 //while ( (inb(0x64) & 0x02) );
5080 if ( inb(0x64) & 0x02 )
5081 BX_PANIC(panic_msg_keyb_buffer_full,"inhibmouse");
5082 command_byte &= 0xfd; // turn off IRQ 12 generation
5083 command_byte |= 0x20; // disable mouse serial clock line
5084 outb(0x64, 0x60); // write command byte
5085 outb(0x60, command_byte);
5086 return(prev_command_byte);
5087 }
5088
5089 void
5090 enable_mouse_int_and_events()
5091 {
5092 Bit8u command_byte;
5093
5094 // Turn on IRQ generation and aux data line
5095 if ( inb(0x64) & 0x02 )
5096 BX_PANIC(panic_msg_keyb_buffer_full,"enabmouse");
5097 outb(0x64, 0x20); // get command byte
5098 while ( (inb(0x64) & 0x01) != 0x01 );
5099 command_byte = inb(0x60);
5100 //while ( (inb(0x64) & 0x02) );
5101 if ( inb(0x64) & 0x02 )
5102 BX_PANIC(panic_msg_keyb_buffer_full,"enabmouse");
5103 command_byte |= 0x02; // turn on IRQ 12 generation
5104 command_byte &= 0xdf; // enable mouse serial clock line
5105 outb(0x64, 0x60); // write command byte
5106 outb(0x60, command_byte);
5107 }
5108
5109 Bit8u
5110 send_to_mouse_ctrl(sendbyte)
5111 Bit8u sendbyte;
5112 {
5113 Bit8u response;
5114
5115 // wait for chance to write to ctrl
5116 if ( inb(0x64) & 0x02 )
5117 BX_PANIC(panic_msg_keyb_buffer_full,"sendmouse");
5118 outb(0x64, 0xD4);
5119 outb(0x60, sendbyte);
5120 return(0);
5121 }
5122
5123
5124 Bit8u
5125 get_mouse_data(data)
5126 Bit8u *data;
5127 {
5128 Bit8u response;
5129 Bit16u ss;
5130
5131 while ( (inb(0x64) & 0x21) != 0x21 ) {
5132 }
5133
5134 response = inb(0x60);
5135
5136 ss = get_SS();
5137 write_byte(ss, data, response);
5138 return(0);
5139 }
5140
5141 void
5142 set_kbd_command_byte(command_byte)
5143 Bit8u command_byte;
5144 {
5145 if ( inb(0x64) & 0x02 )
5146 BX_PANIC(panic_msg_keyb_buffer_full,"setkbdcomm");
5147 outb(0x64, 0xD4);
5148
5149 outb(0x64, 0x60); // write command byte
5150 outb(0x60, command_byte);
5151 }
5152
5153 void
5154 int09_function(DI, SI, BP, SP, BX, DX, CX, AX)
5155 Bit16u DI, SI, BP, SP, BX, DX, CX, AX;
5156 {
5157 Bit8u scancode, asciicode, shift_flags;
5158 Bit8u mf2_flags, mf2_state;
5159
5160 //
5161 // DS has been set to F000 before call
5162 //
5163
5164
5165 scancode = GET_AL();
5166
5167 if (scancode == 0) {
5168 BX_INFO("KBD: int09 handler: AL=0\n");
5169 return;
5170 }
5171
5172
5173 shift_flags = read_byte(0x0040, 0x17);
5174 mf2_flags = read_byte(0x0040, 0x18);
5175 mf2_state = read_byte(0x0040, 0x96);
5176 asciicode = 0;
5177
5178 switch (scancode) {
5179 case 0x3a: /* Caps Lock press */
5180 shift_flags ^= 0x40;
5181 write_byte(0x0040, 0x17, shift_flags);
5182 mf2_flags |= 0x40;
5183 write_byte(0x0040, 0x18, mf2_flags);
5184 break;
5185 case 0xba: /* Caps Lock release */
5186 mf2_flags &= ~0x40;
5187 write_byte(0x0040, 0x18, mf2_flags);
5188 break;
5189
5190 case 0x2a: /* L Shift press */
5191 shift_flags |= 0x02;
5192 write_byte(0x0040, 0x17, shift_flags);
5193 break;
5194 case 0xaa: /* L Shift release */
5195 shift_flags &= ~0x02;
5196 write_byte(0x0040, 0x17, shift_flags);
5197 break;
5198
5199 case 0x36: /* R Shift press */
5200 shift_flags |= 0x01;
5201 write_byte(0x0040, 0x17, shift_flags);
5202 break;
5203 case 0xb6: /* R Shift release */
5204 shift_flags &= ~0x01;
5205 write_byte(0x0040, 0x17, shift_flags);
5206 break;
5207
5208 case 0x1d: /* Ctrl press */
5209 if ((mf2_state & 0x01) == 0) {
5210 shift_flags |= 0x04;
5211 write_byte(0x0040, 0x17, shift_flags);
5212 if (mf2_state & 0x02) {
5213 mf2_state |= 0x04;
5214 write_byte(0x0040, 0x96, mf2_state);
5215 } else {
5216 mf2_flags |= 0x01;
5217 write_byte(0x0040, 0x18, mf2_flags);
5218 }
5219 }
5220 break;
5221 case 0x9d: /* Ctrl release */
5222 if ((mf2_state & 0x01) == 0) {
5223 shift_flags &= ~0x04;
5224 write_byte(0x0040, 0x17, shift_flags);
5225 if (mf2_state & 0x02) {
5226 mf2_state &= ~0x04;
5227 write_byte(0x0040, 0x96, mf2_state);
5228 } else {
5229 mf2_flags &= ~0x01;
5230 write_byte(0x0040, 0x18, mf2_flags);
5231 }
5232 }
5233 break;
5234
5235 case 0x38: /* Alt press */
5236 shift_flags |= 0x08;
5237 write_byte(0x0040, 0x17, shift_flags);
5238 if (mf2_state & 0x02) {
5239 mf2_state |= 0x08;
5240 write_byte(0x0040, 0x96, mf2_state);
5241 } else {
5242 mf2_flags |= 0x02;
5243 write_byte(0x0040, 0x18, mf2_flags);
5244 }
5245 break;
5246 case 0xb8: /* Alt release */
5247 shift_flags &= ~0x08;
5248 write_byte(0x0040, 0x17, shift_flags);
5249 if (mf2_state & 0x02) {
5250 mf2_state &= ~0x08;
5251 write_byte(0x0040, 0x96, mf2_state);
5252 } else {
5253 mf2_flags &= ~0x02;
5254 write_byte(0x0040, 0x18, mf2_flags);
5255 }
5256 break;
5257
5258 case 0x45: /* Num Lock press */
5259 if ((mf2_state & 0x03) == 0) {
5260 mf2_flags |= 0x20;
5261 write_byte(0x0040, 0x18, mf2_flags);
5262 shift_flags ^= 0x20;
5263 write_byte(0x0040, 0x17, shift_flags);
5264 }
5265 break;
5266 case 0xc5: /* Num Lock release */
5267 if ((mf2_state & 0x03) == 0) {
5268 mf2_flags &= ~0x20;
5269 write_byte(0x0040, 0x18, mf2_flags);
5270 }
5271 break;
5272
5273 case 0x46: /* Scroll Lock press */
5274 mf2_flags |= 0x10;
5275 write_byte(0x0040, 0x18, mf2_flags);
5276 shift_flags ^= 0x10;
5277 write_byte(0x0040, 0x17, shift_flags);
5278 break;
5279
5280 case 0xc6: /* Scroll Lock release */
5281 mf2_flags &= ~0x10;
5282 write_byte(0x0040, 0x18, mf2_flags);
5283 break;
5284
5285 case 0x53: /* Del */
5286 if ((shift_flags & 0x0c) == 0x0c) /* Ctrl + Alt */
5287 machine_reset();
5288 /* Fall through */
5289 default:
5290 if (scancode & 0x80) {
5291 break; /* toss key releases ... */
5292 }
5293 if (scancode > MAX_SCAN_CODE) {
5294 BX_INFO("KBD: int09h_handler(): unknown scancode read: 0x%02x!\n", scancode);
5295 return;
5296 }
5297 if (shift_flags & 0x08) { /* ALT */
5298 asciicode = scan_to_scanascii[scancode].alt;
5299 scancode = scan_to_scanascii[scancode].alt >> 8;
5300 } else if (shift_flags & 0x04) { /* CONTROL */
5301 asciicode = scan_to_scanascii[scancode].control;
5302 scancode = scan_to_scanascii[scancode].control >> 8;
5303 } else if (((mf2_state & 0x02) > 0) && ((scancode >= 0x47) && (scancode <= 0x53))) {
5304 /* extended keys handling */
5305 asciicode = 0xe0;
5306 scancode = scan_to_scanascii[scancode].normal >> 8;
5307 } else if (shift_flags & 0x03) { /* LSHIFT + RSHIFT */
5308 /* check if lock state should be ignored
5309 * because a SHIFT key are pressed */
5310
5311 if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
5312 asciicode = scan_to_scanascii[scancode].normal;
5313 scancode = scan_to_scanascii[scancode].normal >> 8;
5314 } else {
5315 asciicode = scan_to_scanascii[scancode].shift;
5316 scancode = scan_to_scanascii[scancode].shift >> 8;
5317 }
5318 } else {
5319 /* check if lock is on */
5320 if (shift_flags & scan_to_scanascii[scancode].lock_flags) {
5321 asciicode = scan_to_scanascii[scancode].shift;
5322 scancode = scan_to_scanascii[scancode].shift >> 8;
5323 } else {
5324 asciicode = scan_to_scanascii[scancode].normal;
5325 scancode = scan_to_scanascii[scancode].normal >> 8;
5326 }
5327 }
5328 if (scancode==0 && asciicode==0) {
5329 BX_INFO("KBD: int09h_handler(): scancode & asciicode are zero?\n");
5330 }
5331 enqueue_key(scancode, asciicode);
5332 break;
5333 }
5334 if ((scancode & 0x7f) != 0x1d) {
5335 mf2_state &= ~0x01;
5336 }
5337 mf2_state &= ~0x02;
5338 write_byte(0x0040, 0x96, mf2_state);
5339 }
5340
5341 unsigned int
5342 enqueue_key(scan_code, ascii_code)
5343 Bit8u scan_code, ascii_code;
5344 {
5345 Bit16u buffer_start, buffer_end, buffer_head, buffer_tail, temp_tail;
5346
5347 #if BX_CPU < 2
5348 buffer_start = 0x001E;
5349 buffer_end = 0x003E;
5350 #else
5351 buffer_start = read_word(0x0040, 0x0080);
5352 buffer_end = read_word(0x0040, 0x0082);
5353 #endif
5354
5355 buffer_head = read_word(0x0040, 0x001A);
5356 buffer_tail = read_word(0x0040, 0x001C);
5357
5358 temp_tail = buffer_tail;
5359 buffer_tail += 2;
5360 if (buffer_tail >= buffer_end)
5361 buffer_tail = buffer_start;
5362
5363 if (buffer_tail == buffer_head) {
5364 return(0);
5365 }
5366
5367 write_byte(0x0040, temp_tail, ascii_code);
5368 write_byte(0x0040, temp_tail+1, scan_code);
5369 write_word(0x0040, 0x001C, buffer_tail);
5370 return(1);
5371 }
5372
5373
5374 void
5375 int74_function(make_farcall, Z, Y, X, status)
5376 Bit16u make_farcall, Z, Y, X, status;
5377 {
5378 Bit16u ebda_seg=read_word(0x0040,0x000E);
5379 Bit8u in_byte, index, package_count;
5380 Bit8u mouse_flags_1, mouse_flags_2;
5381
5382 BX_DEBUG_INT74("entering int74_function\n");
5383 make_farcall = 0;
5384
5385 in_byte = inb(0x64);
5386 if ( (in_byte & 0x21) != 0x21 ) {
5387 return;
5388 }
5389 in_byte = inb(0x60);
5390 BX_DEBUG_INT74("int74: read byte %02x\n", in_byte);
5391
5392 mouse_flags_1 = read_byte(ebda_seg, 0x0026);
5393 mouse_flags_2 = read_byte(ebda_seg, 0x0027);
5394
5395 if ( (mouse_flags_2 & 0x80) != 0x80 ) {
5396 return;
5397 }
5398
5399 package_count = mouse_flags_2 & 0x07;
5400 index = mouse_flags_1 & 0x07;
5401 write_byte(ebda_seg, 0x28 + index, in_byte);
5402
5403 if ( (index+1) >= package_count ) {
5404 BX_DEBUG_INT74("int74_function: make_farcall=1\n");
5405 status = read_byte(ebda_seg, 0x0028 + 0);
5406 X = read_byte(ebda_seg, 0x0028 + 1);
5407 Y = read_byte(ebda_seg, 0x0028 + 2);
5408 Z = 0;
5409 mouse_flags_1 = 0;
5410 // check if far call handler installed
5411 if (mouse_flags_2 & 0x80)
5412 make_farcall = 1;
5413 }
5414 else {
5415 mouse_flags_1++;
5416 }
5417 write_byte(ebda_seg, 0x0026, mouse_flags_1);
5418 }
5419
5420 #define SET_DISK_RET_STATUS(status) write_byte(0x0040, 0x0074, status)
5421
5422 #if BX_USE_ATADRV
5423
5424 void
5425 int13_harddisk(EHAX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS)
5426 Bit16u EHAX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS;
5427 {
5428 Bit32u lba_low, lba_high;
5429 Bit16u ebda_seg=read_word(0x0040,0x000E);
5430 Bit16u cylinder, head, sector;
5431 Bit16u segment, offset;
5432 Bit16u npc, nph, npspt, nlc, nlh, nlspt;
5433 Bit16u size, count;
5434 Bit8u device, status;
5435
5436 BX_DEBUG_INT13_HD("int13_harddisk: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
5437
5438 write_byte(0x0040, 0x008e, 0); // clear completion flag
5439
5440 // basic check : device has to be defined
5441 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_ATA_DEVICES) ) {
5442 BX_INFO("int13_harddisk: function %02x, ELDL out of range %02x\n", GET_AH(), GET_ELDL());
5443 goto int13_fail;
5444 }
5445
5446 // Get the ata channel
5447 device=read_byte(ebda_seg,&EbdaData->ata.hdidmap[GET_ELDL()-0x80]);
5448
5449 // basic check : device has to be valid
5450 if (device >= BX_MAX_ATA_DEVICES) {
5451 BX_INFO("int13_harddisk: function %02x, unmapped device for ELDL=%02x\n", GET_AH(), GET_ELDL());
5452 goto int13_fail;
5453 }
5454
5455 switch (GET_AH()) {
5456
5457 case 0x00: /* disk controller reset */
5458 ata_reset (device);
5459 goto int13_success;
5460 break;
5461
5462 case 0x01: /* read disk status */
5463 status = read_byte(0x0040, 0x0074);
5464 SET_AH(status);
5465 SET_DISK_RET_STATUS(0);
5466 /* set CF if error status read */
5467 if (status) goto int13_fail_nostatus;
5468 else goto int13_success_noah;
5469 break;
5470
5471 case 0x02: // read disk sectors
5472 case 0x03: // write disk sectors
5473 case 0x04: // verify disk sectors
5474
5475 count = GET_AL();
5476 cylinder = GET_CH();
5477 cylinder |= ( ((Bit16u) GET_CL()) << 2) & 0x300;
5478 sector = (GET_CL() & 0x3f);
5479 head = GET_DH();
5480
5481 segment = ES;
5482 offset = BX;
5483
5484 if ((count > 128) || (count == 0) || (sector == 0)) {
5485 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n",GET_AH());
5486 goto int13_fail;
5487 }
5488
5489 nlc = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.cylinders);
5490 nlh = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.heads);
5491 nlspt = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.spt);
5492
5493 // sanity check on cyl heads, sec
5494 if( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
5495 BX_INFO("int13_harddisk: function %02x, parameters out of range %04x/%04x/%04x!\n", GET_AH(), cylinder, head, sector);
5496 goto int13_fail;
5497 }
5498
5499 // FIXME verify
5500 if ( GET_AH() == 0x04 ) goto int13_success;
5501
5502 nph = read_word(ebda_seg, &EbdaData->ata.devices[device].pchs.heads);
5503 npspt = read_word(ebda_seg, &EbdaData->ata.devices[device].pchs.spt);
5504
5505 // if needed, translate lchs to lba, and execute command
5506 if ( (nph != nlh) || (npspt != nlspt)) {
5507 lba_low = ((((Bit32u)cylinder * (Bit32u)nlh) + (Bit32u)head) * (Bit32u)nlspt) + (Bit32u)sector - 1;
5508 lba_high = 0;
5509 sector = 0; // this forces the command to be lba
5510 }
5511
5512 if ( GET_AH() == 0x02 )
5513 status=ata_cmd_data_in(device, ATA_CMD_READ_SECTORS, count, cylinder, head, sector, lba_low, lba_high, segment, offset);
5514 else
5515 status=ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS, count, cylinder, head, sector, lba_low, lba_high, segment, offset);
5516
5517 // Set nb of sector transferred
5518 SET_AL(read_word(ebda_seg, &EbdaData->ata.trsfsectors));
5519
5520 if (status != 0) {
5521 BX_INFO("int13_harddisk: function %02x, error %02x !\n",GET_AH(),status);
5522 SET_AH(0x0c);
5523 goto int13_fail_noah;
5524 }
5525
5526 goto int13_success;
5527 break;
5528
5529 case 0x05: /* format disk track */
5530 BX_INFO("format disk track called\n");
5531 goto int13_success;
5532 return;
5533 break;
5534
5535 case 0x08: /* read disk drive parameters */
5536
5537 // Get logical geometry from table
5538 nlc = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.cylinders);
5539 nlh = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.heads);
5540 nlspt = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.spt);
5541 count = read_byte(ebda_seg, &EbdaData->ata.hdcount);
5542
5543 nlc = nlc - 2; /* 0 based , last sector not used */
5544 SET_AL(0);
5545 SET_CH(nlc & 0xff);
5546 SET_CL(((nlc >> 2) & 0xc0) | (nlspt & 0x3f));
5547 SET_DH(nlh - 1);
5548 SET_DL(count); /* FIXME returns 0, 1, or n hard drives */
5549
5550 // FIXME should set ES & DI
5551
5552 goto int13_success;
5553 break;
5554
5555 case 0x10: /* check drive ready */
5556 // should look at 40:8E also???
5557
5558 // Read the status from controller
5559 status = inb(read_word(ebda_seg, &EbdaData->ata.channels[device/2].iobase1) + ATA_CB_STAT);
5560 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY ) {
5561 goto int13_success;
5562 }
5563 else {
5564 SET_AH(0xAA);
5565 goto int13_fail_noah;
5566 }
5567 break;
5568
5569 case 0x15: /* read disk drive size */
5570
5571 // Get logical geometry from table
5572 nlc = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.cylinders);
5573 nlh = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.heads);
5574 nlspt = read_word(ebda_seg, &EbdaData->ata.devices[device].lchs.spt);
5575
5576 // Compute sector count seen by int13
5577 lba_low = (Bit32u)(nlc - 1) * (Bit32u)nlh * (Bit32u)nlspt;
5578 CX = lba_low >> 16;
5579 DX = lba_low & 0xffff;
5580
5581 SET_AH(3); // hard disk accessible
5582 goto int13_success_noah;
5583 break;
5584
5585 case 0x41: // IBM/MS installation check
5586 BX=0xaa55; // install check
5587 SET_AH(0x30); // EDD 3.0
5588 CX=0x0007; // ext disk access and edd, removable supported
5589 goto int13_success_noah;
5590 break;
5591
5592 case 0x42: // IBM/MS extended read
5593 case 0x43: // IBM/MS extended write
5594 case 0x44: // IBM/MS verify
5595 case 0x47: // IBM/MS extended seek
5596
5597 count=read_word(DS, SI+(Bit16u)&Int13Ext->count);
5598 segment=read_word(DS, SI+(Bit16u)&Int13Ext->segment);
5599 offset=read_word(DS, SI+(Bit16u)&Int13Ext->offset);
5600
5601 // Get 32 msb lba and check
5602 lba_high=read_dword(DS, SI+(Bit16u)&Int13Ext->lba2);
5603 if (lba_high > read_dword(ebda_seg, &EbdaData->ata.devices[device].sectors_high) ) {
5604 BX_INFO("int13_harddisk: function %02x. LBA out of range\n",GET_AH());
5605 goto int13_fail;
5606 }
5607
5608 // Get 32 lsb lba and check
5609 lba_low=read_dword(DS, SI+(Bit16u)&Int13Ext->lba1);
5610 if (lba_high == read_dword(ebda_seg, &EbdaData->ata.devices[device].sectors_high)
5611 && lba_low >= read_dword(ebda_seg, &EbdaData->ata.devices[device].sectors_low) ) {
5612 BX_INFO("int13_harddisk: function %02x. LBA out of range\n",GET_AH());
5613 goto int13_fail;
5614 }
5615
5616 // If verify or seek
5617 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
5618 goto int13_success;
5619
5620 // Execute the command
5621 if ( GET_AH() == 0x42 )
5622 status=ata_cmd_data_in(device, ATA_CMD_READ_SECTORS, count, 0, 0, 0, lba_low, lba_high, segment, offset);
5623 else
5624 status=ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS, count, 0, 0, 0, lba_low, lba_high, segment, offset);
5625
5626 count=read_word(ebda_seg, &EbdaData->ata.trsfsectors);
5627 write_word(DS, SI+(Bit16u)&Int13Ext->count, count);
5628
5629 if (status != 0) {
5630 BX_INFO("int13_harddisk: function %02x, error %02x !\n",GET_AH(),status);
5631 SET_AH(0x0c);
5632 goto int13_fail_noah;
5633 }
5634
5635 goto int13_success;
5636 break;
5637
5638 case 0x45: // IBM/MS lock/unlock drive
5639 case 0x49: // IBM/MS extended media change
5640 goto int13_success; // Always success for HD
5641 break;
5642
5643 case 0x46: // IBM/MS eject media
5644 SET_AH(0xb2); // Volume Not Removable
5645 goto int13_fail_noah; // Always fail for HD
5646 break;
5647
5648 case 0x48: // IBM/MS get drive parameters
5649 size=read_word(DS,SI+(Bit16u)&Int13DPT->size);
5650
5651 // Buffer is too small
5652 if(size < 0x1a)
5653 goto int13_fail;
5654
5655 // EDD 1.x
5656 if(size >= 0x1a) {
5657 Bit16u blksize;
5658
5659 npc = read_word(ebda_seg, &EbdaData->ata.devices[device].pchs.cylinders);
5660 nph = read_word(ebda_seg, &EbdaData->ata.devices[device].pchs.heads);
5661 npspt = read_word(ebda_seg, &EbdaData->ata.devices[device].pchs.spt);
5662 lba_low = read_dword(ebda_seg, &EbdaData->ata.devices[device].sectors_low);
5663 lba_high = read_dword(ebda_seg, &EbdaData->ata.devices[device].sectors_high);
5664 blksize = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
5665
5666 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x1a);
5667 if (lba_high || (lba_low/npspt)/nph > 0x3fff)
5668 {
5669 write_word(DS, SI+(Bit16u)&Int13DPT->infos, 0x00); // geometry is invalid
5670 write_dword(DS, SI+(Bit16u)&Int13DPT->cylinders, 0x3fff);
5671 }
5672 else
5673 {
5674 write_word(DS, SI+(Bit16u)&Int13DPT->infos, 0x02); // geometry is valid
5675 write_dword(DS, SI+(Bit16u)&Int13DPT->cylinders, (Bit32u)npc);
5676 }
5677 write_dword(DS, SI+(Bit16u)&Int13DPT->heads, (Bit32u)nph);
5678 write_dword(DS, SI+(Bit16u)&Int13DPT->spt, (Bit32u)npspt);
5679 write_dword(DS, SI+(Bit16u)&Int13DPT->sector_count1, lba_low);
5680 write_dword(DS, SI+(Bit16u)&Int13DPT->sector_count2, lba_high);
5681 write_word(DS, SI+(Bit16u)&Int13DPT->blksize, blksize);
5682 }
5683
5684 // EDD 2.x
5685 if(size >= 0x1e) {
5686 Bit8u channel, dev, irq, mode, checksum, i, translation;
5687 Bit16u iobase1, iobase2, options;
5688
5689 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x1e);
5690
5691 write_word(DS, SI+(Bit16u)&Int13DPT->dpte_segment, ebda_seg);
5692 write_word(DS, SI+(Bit16u)&Int13DPT->dpte_offset, &EbdaData->ata.dpte);
5693
5694 // Fill in dpte
5695 channel = device / 2;
5696 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
5697 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
5698 irq = read_byte(ebda_seg, &EbdaData->ata.channels[channel].irq);
5699 mode = read_byte(ebda_seg, &EbdaData->ata.devices[device].mode);
5700 translation = read_byte(ebda_seg, &EbdaData->ata.devices[device].translation);
5701
5702 options = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
5703 options |= (1<<4); // lba translation
5704 options |= (mode==ATA_MODE_PIO32?1:0)<<7;
5705 options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
5706 options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
5707
5708 write_word(ebda_seg, &EbdaData->ata.dpte.iobase1, iobase1);
5709 write_word(ebda_seg, &EbdaData->ata.dpte.iobase2, iobase2 + ATA_CB_DC);
5710 write_byte(ebda_seg, &EbdaData->ata.dpte.prefix, (0xe | (device % 2))<<4 );
5711 write_byte(ebda_seg, &EbdaData->ata.dpte.unused, 0xcb );
5712 write_byte(ebda_seg, &EbdaData->ata.dpte.irq, irq );
5713 write_byte(ebda_seg, &EbdaData->ata.dpte.blkcount, 1 );
5714 write_byte(ebda_seg, &EbdaData->ata.dpte.dma, 0 );
5715 write_byte(ebda_seg, &EbdaData->ata.dpte.pio, 0 );
5716 write_word(ebda_seg, &EbdaData->ata.dpte.options, options);
5717 write_word(ebda_seg, &EbdaData->ata.dpte.reserved, 0);
5718 if (size >=0x42)
5719 write_byte(ebda_seg, &EbdaData->ata.dpte.revision, 0x11);
5720 else
5721 write_byte(ebda_seg, &EbdaData->ata.dpte.revision, 0x10);
5722
5723 checksum=0;
5724 for (i=0; i<15; i++) checksum+=read_byte(ebda_seg, ((Bit8u*)(&EbdaData->ata.dpte)) + i);
5725 checksum = ~checksum;
5726 write_byte(ebda_seg, &EbdaData->ata.dpte.checksum, checksum);
5727 }
5728
5729 // EDD 3.x
5730 if(size >= 0x42) {
5731 Bit8u channel, iface, checksum, i;
5732 Bit16u iobase1;
5733
5734 channel = device / 2;
5735 iface = read_byte(ebda_seg, &EbdaData->ata.channels[channel].iface);
5736 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
5737
5738 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x42);
5739 write_word(DS, SI+(Bit16u)&Int13DPT->key, 0xbedd);
5740 write_byte(DS, SI+(Bit16u)&Int13DPT->dpi_length, 0x24);
5741 write_byte(DS, SI+(Bit16u)&Int13DPT->reserved1, 0);
5742 write_word(DS, SI+(Bit16u)&Int13DPT->reserved2, 0);
5743
5744 if (iface==ATA_IFACE_ISA) {
5745 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[0], 'I');
5746 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[1], 'S');
5747 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[2], 'A');
5748 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[3], 0);
5749 }
5750 else {
5751 // FIXME PCI
5752 }
5753 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[0], 'A');
5754 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[1], 'T');
5755 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[2], 'A');
5756 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[3], 0);
5757
5758 if (iface==ATA_IFACE_ISA) {
5759 write_word(DS, SI+(Bit16u)&Int13DPT->iface_path[0], iobase1);
5760 write_word(DS, SI+(Bit16u)&Int13DPT->iface_path[2], 0);
5761 write_dword(DS, SI+(Bit16u)&Int13DPT->iface_path[4], 0L);
5762 }
5763 else {
5764 // FIXME PCI
5765 }
5766 write_byte(DS, SI+(Bit16u)&Int13DPT->device_path[0], device%2);
5767 write_byte(DS, SI+(Bit16u)&Int13DPT->device_path[1], 0);
5768 write_word(DS, SI+(Bit16u)&Int13DPT->device_path[2], 0);
5769 write_dword(DS, SI+(Bit16u)&Int13DPT->device_path[4], 0L);
5770
5771 checksum=0;
5772 for (i=30; i<64; i++) checksum+=read_byte(DS, SI + i);
5773 checksum = ~checksum;
5774 write_byte(DS, SI+(Bit16u)&Int13DPT->checksum, checksum);
5775 }
5776
5777 goto int13_success;
5778 break;
5779
5780 case 0x4e: // // IBM/MS set hardware configuration
5781 // DMA, prefetch, PIO maximum not supported
5782 switch (GET_AL()) {
5783 case 0x01:
5784 case 0x03:
5785 case 0x04:
5786 case 0x06:
5787 goto int13_success;
5788 break;
5789 default :
5790 goto int13_fail;
5791 }
5792 break;
5793
5794 case 0x09: /* initialize drive parameters */
5795 case 0x0c: /* seek to specified cylinder */
5796 case 0x0d: /* alternate disk reset */
5797 case 0x11: /* recalibrate */
5798 case 0x14: /* controller internal diagnostic */
5799 BX_INFO("int13_harddisk: function %02xh unimplemented, returns success\n", GET_AH());
5800 goto int13_success;
5801 break;
5802
5803 case 0x0a: /* read disk sectors with ECC */
5804 case 0x0b: /* write disk sectors with ECC */
5805 case 0x18: // set media type for format
5806 case 0x50: // IBM/MS send packet command
5807 default:
5808 BX_INFO("int13_harddisk: function %02xh unsupported, returns fail\n", GET_AH());
5809 goto int13_fail;
5810 break;
5811 }
5812
5813 int13_fail:
5814 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
5815 int13_fail_noah:
5816 SET_DISK_RET_STATUS(GET_AH());
5817 int13_fail_nostatus:
5818 SET_CF(); // error occurred
5819 return;
5820
5821 int13_success:
5822 SET_AH(0x00); // no error
5823 int13_success_noah:
5824 SET_DISK_RET_STATUS(0x00);
5825 CLEAR_CF(); // no error
5826 return;
5827 }
5828
5829 // ---------------------------------------------------------------------------
5830 // Start of int13 for cdrom
5831 // ---------------------------------------------------------------------------
5832
5833 void
5834 int13_cdrom(EHBX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS)
5835 Bit16u EHBX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS;
5836 {
5837 Bit16u ebda_seg=read_word(0x0040,0x000E);
5838 Bit8u device, status, locks;
5839 Bit8u atacmd[12];
5840 Bit32u lba;
5841 Bit16u count, segment, offset, i, size;
5842
5843 BX_DEBUG_INT13_CD("int13_cdrom: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
5844
5845 SET_DISK_RET_STATUS(0x00);
5846
5847 /* basic check : device should be 0xE0+ */
5848 if( (GET_ELDL() < 0xE0) || (GET_ELDL() >= 0xE0+BX_MAX_ATA_DEVICES) ) {
5849 BX_INFO("int13_cdrom: function %02x, ELDL out of range %02x\n", GET_AH(), GET_ELDL());
5850 goto int13_fail;
5851 }
5852
5853 // Get the ata channel
5854 device=read_byte(ebda_seg,&EbdaData->ata.cdidmap[GET_ELDL()-0xE0]);
5855
5856 /* basic check : device has to be valid */
5857 if (device >= BX_MAX_ATA_DEVICES) {
5858 BX_INFO("int13_cdrom: function %02x, unmapped device for ELDL=%02x\n", GET_AH(), GET_ELDL());
5859 goto int13_fail;
5860 }
5861
5862 switch (GET_AH()) {
5863
5864 // all those functions return SUCCESS
5865 case 0x00: /* disk controller reset */
5866 case 0x09: /* initialize drive parameters */
5867 case 0x0c: /* seek to specified cylinder */
5868 case 0x0d: /* alternate disk reset */
5869 case 0x10: /* check drive ready */
5870 case 0x11: /* recalibrate */
5871 case 0x14: /* controller internal diagnostic */
5872 case 0x16: /* detect disk change */
5873 goto int13_success;
5874 break;
5875
5876 // all those functions return disk write-protected
5877 case 0x03: /* write disk sectors */
5878 case 0x05: /* format disk track */
5879 case 0x43: // IBM/MS extended write
5880 SET_AH(0x03);
5881 goto int13_fail_noah;
5882 break;
5883
5884 case 0x01: /* read disk status */
5885 status = read_byte(0x0040, 0x0074);
5886 SET_AH(status);
5887 SET_DISK_RET_STATUS(0);
5888
5889 /* set CF if error status read */
5890 if (status) goto int13_fail_nostatus;
5891 else goto int13_success_noah;
5892 break;
5893
5894 case 0x15: /* read disk drive size */
5895 SET_AH(0x02);
5896 goto int13_fail_noah;
5897 break;
5898
5899 case 0x41: // IBM/MS installation check
5900 BX=0xaa55; // install check
5901 SET_AH(0x30); // EDD 2.1
5902 CX=0x0007; // ext disk access, removable and edd
5903 goto int13_success_noah;
5904 break;
5905
5906 case 0x42: // IBM/MS extended read
5907 case 0x44: // IBM/MS verify sectors
5908 case 0x47: // IBM/MS extended seek
5909
5910 count=read_word(DS, SI+(Bit16u)&Int13Ext->count);
5911 segment=read_word(DS, SI+(Bit16u)&Int13Ext->segment);
5912 offset=read_word(DS, SI+(Bit16u)&Int13Ext->offset);
5913
5914 // Can't use 64 bits lba
5915 lba=read_dword(DS, SI+(Bit16u)&Int13Ext->lba2);
5916 if (lba != 0L) {
5917 BX_PANIC("int13_cdrom: function %02x. Can't use 64bits lba\n",GET_AH());
5918 goto int13_fail;
5919 }
5920
5921 // Get 32 bits lba
5922 lba=read_dword(DS, SI+(Bit16u)&Int13Ext->lba1);
5923
5924 // If verify or seek
5925 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
5926 goto int13_success;
5927
5928 memsetb(get_SS(),atacmd,0,12);
5929 atacmd[0]=0x28; // READ command
5930 atacmd[7]=(count & 0xff00) >> 8; // Sectors
5931 atacmd[8]=(count & 0x00ff); // Sectors
5932 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
5933 atacmd[3]=(lba & 0x00ff0000) >> 16;
5934 atacmd[4]=(lba & 0x0000ff00) >> 8;
5935 atacmd[5]=(lba & 0x000000ff);
5936 status = ata_cmd_packet(device, 12, get_SS(), atacmd, 0, count*2048L, ATA_DATA_IN, segment,offset);
5937
5938 count = (Bit16u)(read_dword(ebda_seg, &EbdaData->ata.trsfbytes) >> 11);
5939 write_word(DS, SI+(Bit16u)&Int13Ext->count, count);
5940
5941 if (status != 0) {
5942 BX_INFO("int13_cdrom: function %02x, status %02x !\n",GET_AH(),status);
5943 SET_AH(0x0c);
5944 goto int13_fail_noah;
5945 }
5946
5947 goto int13_success;
5948 break;
5949
5950 case 0x45: // IBM/MS lock/unlock drive
5951 if (GET_AL() > 2) goto int13_fail;
5952
5953 locks = read_byte(ebda_seg, &EbdaData->ata.devices[device].lock);
5954
5955 switch (GET_AL()) {
5956 case 0 : // lock
5957 if (locks == 0xff) {
5958 SET_AH(0xb4);
5959 SET_AL(1);
5960 goto int13_fail_noah;
5961 }
5962 write_byte(ebda_seg, &EbdaData->ata.devices[device].lock, ++locks);
5963 SET_AL(1);
5964 break;
5965 case 1 : // unlock
5966 if (locks == 0x00) {
5967 SET_AH(0xb0);
5968 SET_AL(0);
5969 goto int13_fail_noah;
5970 }
5971 write_byte(ebda_seg, &EbdaData->ata.devices[device].lock, --locks);
5972 SET_AL(locks==0?0:1);
5973 break;
5974 case 2 : // status
5975 SET_AL(locks==0?0:1);
5976 break;
5977 }
5978 goto int13_success;
5979 break;
5980
5981 case 0x46: // IBM/MS eject media
5982 locks = read_byte(ebda_seg, &EbdaData->ata.devices[device].lock);
5983
5984 if (locks != 0) {
5985 SET_AH(0xb1); // media locked
5986 goto int13_fail_noah;
5987 }
5988 // FIXME should handle 0x31 no media in device
5989 // FIXME should handle 0xb5 valid request failed
5990
5991 // Call removable media eject
5992 ASM_START
5993 push bp
5994 mov bp, sp
5995
5996 mov ah, #0x52
5997 int #0x15
5998 mov _int13_cdrom.status + 2[bp], ah
5999 jnc int13_cdrom_rme_end
6000 mov _int13_cdrom.status, #1
6001 int13_cdrom_rme_end:
6002 pop bp
6003 ASM_END
6004
6005 if (status != 0) {
6006 SET_AH(0xb1); // media locked
6007 goto int13_fail_noah;
6008 }
6009
6010 goto int13_success;
6011 break;
6012
6013 case 0x48: // IBM/MS get drive parameters
6014 size = read_word(DS,SI+(Bit16u)&Int13Ext->size);
6015
6016 // Buffer is too small
6017 if(size < 0x1a)
6018 goto int13_fail;
6019
6020 // EDD 1.x
6021 if(size >= 0x1a) {
6022 Bit16u cylinders, heads, spt, blksize;
6023
6024 blksize = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
6025
6026 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x1a);
6027 write_word(DS, SI+(Bit16u)&Int13DPT->infos, 0x74); // removable, media change, lockable, max values
6028 write_dword(DS, SI+(Bit16u)&Int13DPT->cylinders, 0xffffffff);
6029 write_dword(DS, SI+(Bit16u)&Int13DPT->heads, 0xffffffff);
6030 write_dword(DS, SI+(Bit16u)&Int13DPT->spt, 0xffffffff);
6031 write_dword(DS, SI+(Bit16u)&Int13DPT->sector_count1, 0xffffffff); // FIXME should be Bit64
6032 write_dword(DS, SI+(Bit16u)&Int13DPT->sector_count2, 0xffffffff);
6033 write_word(DS, SI+(Bit16u)&Int13DPT->blksize, blksize);
6034 }
6035
6036 // EDD 2.x
6037 if(size >= 0x1e) {
6038 Bit8u channel, dev, irq, mode, checksum, i;
6039 Bit16u iobase1, iobase2, options;
6040
6041 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x1e);
6042
6043 write_word(DS, SI+(Bit16u)&Int13DPT->dpte_segment, ebda_seg);
6044 write_word(DS, SI+(Bit16u)&Int13DPT->dpte_offset, &EbdaData->ata.dpte);
6045
6046 // Fill in dpte
6047 channel = device / 2;
6048 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
6049 iobase2 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase2);
6050 irq = read_byte(ebda_seg, &EbdaData->ata.channels[channel].irq);
6051 mode = read_byte(ebda_seg, &EbdaData->ata.devices[device].mode);
6052
6053 // FIXME atapi device
6054 options = (1<<4); // lba translation
6055 options |= (1<<5); // removable device
6056 options |= (1<<6); // atapi device
6057 options |= (mode==ATA_MODE_PIO32?1:0<<7);
6058
6059 write_word(ebda_seg, &EbdaData->ata.dpte.iobase1, iobase1);
6060 write_word(ebda_seg, &EbdaData->ata.dpte.iobase2, iobase2 + ATA_CB_DC);
6061 write_byte(ebda_seg, &EbdaData->ata.dpte.prefix, (0xe | (device % 2))<<4 );
6062 write_byte(ebda_seg, &EbdaData->ata.dpte.unused, 0xcb );
6063 write_byte(ebda_seg, &EbdaData->ata.dpte.irq, irq );
6064 write_byte(ebda_seg, &EbdaData->ata.dpte.blkcount, 1 );
6065 write_byte(ebda_seg, &EbdaData->ata.dpte.dma, 0 );
6066 write_byte(ebda_seg, &EbdaData->ata.dpte.pio, 0 );
6067 write_word(ebda_seg, &EbdaData->ata.dpte.options, options);
6068 write_word(ebda_seg, &EbdaData->ata.dpte.reserved, 0);
6069 write_byte(ebda_seg, &EbdaData->ata.dpte.revision, 0x11);
6070
6071 checksum=0;
6072 for (i=0; i<15; i++) checksum+=read_byte(ebda_seg, ((Bit8u*)(&EbdaData->ata.dpte)) + i);
6073 checksum = ~checksum;
6074 write_byte(ebda_seg, &EbdaData->ata.dpte.checksum, checksum);
6075 }
6076
6077 // EDD 3.x
6078 if(size >= 0x42) {
6079 Bit8u channel, iface, checksum, i;
6080 Bit16u iobase1;
6081
6082 channel = device / 2;
6083 iface = read_byte(ebda_seg, &EbdaData->ata.channels[channel].iface);
6084 iobase1 = read_word(ebda_seg, &EbdaData->ata.channels[channel].iobase1);
6085
6086 write_word(DS, SI+(Bit16u)&Int13DPT->size, 0x42);
6087 write_word(DS, SI+(Bit16u)&Int13DPT->key, 0xbedd);
6088 write_byte(DS, SI+(Bit16u)&Int13DPT->dpi_length, 0x24);
6089 write_byte(DS, SI+(Bit16u)&Int13DPT->reserved1, 0);
6090 write_word(DS, SI+(Bit16u)&Int13DPT->reserved2, 0);
6091
6092 if (iface==ATA_IFACE_ISA) {
6093 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[0], 'I');
6094 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[1], 'S');
6095 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[2], 'A');
6096 write_byte(DS, SI+(Bit16u)&Int13DPT->host_bus[3], 0);
6097 }
6098 else {
6099 // FIXME PCI
6100 }
6101 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[0], 'A');
6102 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[1], 'T');
6103 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[2], 'A');
6104 write_byte(DS, SI+(Bit16u)&Int13DPT->iface_type[3], 0);
6105
6106 if (iface==ATA_IFACE_ISA) {
6107 write_word(DS, SI+(Bit16u)&Int13DPT->iface_path[0], iobase1);
6108 write_word(DS, SI+(Bit16u)&Int13DPT->iface_path[2], 0);
6109 write_dword(DS, SI+(Bit16u)&Int13DPT->iface_path[4], 0L);
6110 }
6111 else {
6112 // FIXME PCI
6113 }
6114 write_byte(DS, SI+(Bit16u)&Int13DPT->device_path[0], device%2);
6115 write_byte(DS, SI+(Bit16u)&Int13DPT->device_path[1], 0);
6116 write_word(DS, SI+(Bit16u)&Int13DPT->device_path[2], 0);
6117 write_dword(DS, SI+(Bit16u)&Int13DPT->device_path[4], 0L);
6118
6119 checksum=0;
6120 for (i=30; i<64; i++) checksum+=read_byte(DS, SI + i);
6121 checksum = ~checksum;
6122 write_byte(DS, SI+(Bit16u)&Int13DPT->checksum, checksum);
6123 }
6124
6125 goto int13_success;
6126 break;
6127
6128 case 0x49: // IBM/MS extended media change
6129 // always send changed ??
6130 SET_AH(06);
6131 goto int13_fail_nostatus;
6132 break;
6133
6134 case 0x4e: // // IBM/MS set hardware configuration
6135 // DMA, prefetch, PIO maximum not supported
6136 switch (GET_AL()) {
6137 case 0x01:
6138 case 0x03:
6139 case 0x04:
6140 case 0x06:
6141 goto int13_success;
6142 break;
6143 default :
6144 goto int13_fail;
6145 }
6146 break;
6147
6148 // all those functions return unimplemented
6149 case 0x02: /* read sectors */
6150 case 0x04: /* verify sectors */
6151 case 0x08: /* read disk drive parameters */
6152 case 0x0a: /* read disk sectors with ECC */
6153 case 0x0b: /* write disk sectors with ECC */
6154 case 0x18: /* set media type for format */
6155 case 0x50: // ? - send packet command
6156 default:
6157 BX_INFO("int13_cdrom: unsupported AH=%02x\n", GET_AH());
6158 goto int13_fail;
6159 break;
6160 }
6161
6162 int13_fail:
6163 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
6164 int13_fail_noah:
6165 SET_DISK_RET_STATUS(GET_AH());
6166 int13_fail_nostatus:
6167 SET_CF(); // error occurred
6168 return;
6169
6170 int13_success:
6171 SET_AH(0x00); // no error
6172 int13_success_noah:
6173 SET_DISK_RET_STATUS(0x00);
6174 CLEAR_CF(); // no error
6175 return;
6176 }
6177
6178 // ---------------------------------------------------------------------------
6179 // End of int13 for cdrom
6180 // ---------------------------------------------------------------------------
6181
6182 #if BX_ELTORITO_BOOT
6183 // ---------------------------------------------------------------------------
6184 // Start of int13 for eltorito functions
6185 // ---------------------------------------------------------------------------
6186
6187 void
6188 int13_eltorito(DS, ES, DI, SI, BP, SP, BX, DX, CX, AX, IP, CS, FLAGS)
6189 Bit16u DS, ES, DI, SI, BP, SP, BX, DX, CX, AX, IP, CS, FLAGS;
6190 {
6191 Bit16u ebda_seg=read_word(0x0040,0x000E);
6192
6193 BX_DEBUG_INT13_ET("int13_eltorito: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
6194 // BX_DEBUG_INT13_ET("int13_eltorito: SS=%04x DS=%04x ES=%04x DI=%04x SI=%04x\n",get_SS(), DS, ES, DI, SI);
6195
6196 switch (GET_AH()) {
6197
6198 // FIXME ElTorito Various. Should be implemented
6199 case 0x4a: // ElTorito - Initiate disk emu
6200 case 0x4c: // ElTorito - Initiate disk emu and boot
6201 case 0x4d: // ElTorito - Return Boot catalog
6202 BX_PANIC("Int13 eltorito call with AX=%04x. Please report\n",AX);
6203 goto int13_fail;
6204 break;
6205
6206 case 0x4b: // ElTorito - Terminate disk emu
6207 // FIXME ElTorito Hardcoded
6208 write_byte(DS,SI+0x00,0x13);
6209 write_byte(DS,SI+0x01,read_byte(ebda_seg,&EbdaData->cdemu.media));
6210 write_byte(DS,SI+0x02,read_byte(ebda_seg,&EbdaData->cdemu.emulated_drive));
6211 write_byte(DS,SI+0x03,read_byte(ebda_seg,&EbdaData->cdemu.controller_index));
6212 write_dword(DS,SI+0x04,read_dword(ebda_seg,&EbdaData->cdemu.ilba));
6213 write_word(DS,SI+0x08,read_word(ebda_seg,&EbdaData->cdemu.device_spec));
6214 write_word(DS,SI+0x0a,read_word(ebda_seg,&EbdaData->cdemu.buffer_segment));
6215 write_word(DS,SI+0x0c,read_word(ebda_seg,&EbdaData->cdemu.load_segment));
6216 write_word(DS,SI+0x0e,read_word(ebda_seg,&EbdaData->cdemu.sector_count));
6217 write_byte(DS,SI+0x10,read_byte(ebda_seg,&EbdaData->cdemu.vdevice.cylinders));
6218 write_byte(DS,SI+0x11,read_byte(ebda_seg,&EbdaData->cdemu.vdevice.spt));
6219 write_byte(DS,SI+0x12,read_byte(ebda_seg,&EbdaData->cdemu.vdevice.heads));
6220
6221 // If we have to terminate emulation
6222 if(GET_AL() == 0x00) {
6223 // FIXME ElTorito Various. Should be handled accordingly to spec
6224 write_byte(ebda_seg,&EbdaData->cdemu.active, 0x00); // bye bye
6225 }
6226
6227 goto int13_success;
6228 break;
6229
6230 default:
6231 BX_INFO("int13_eltorito: unsupported AH=%02x\n", GET_AH());
6232 goto int13_fail;
6233 break;
6234 }
6235
6236 int13_fail:
6237 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
6238 SET_DISK_RET_STATUS(GET_AH());
6239 SET_CF(); // error occurred
6240 return;
6241
6242 int13_success:
6243 SET_AH(0x00); // no error
6244 SET_DISK_RET_STATUS(0x00);
6245 CLEAR_CF(); // no error
6246 return;
6247 }
6248
6249 // ---------------------------------------------------------------------------
6250 // End of int13 for eltorito functions
6251 // ---------------------------------------------------------------------------
6252
6253 // ---------------------------------------------------------------------------
6254 // Start of int13 when emulating a device from the cd
6255 // ---------------------------------------------------------------------------
6256
6257 void
6258 int13_cdemu(DS, ES, DI, SI, BP, SP, BX, DX, CX, AX, IP, CS, FLAGS)
6259 Bit16u DS, ES, DI, SI, BP, SP, BX, DX, CX, AX, IP, CS, FLAGS;
6260 {
6261 Bit16u ebda_seg=read_word(0x0040,0x000E);
6262 Bit8u device, status;
6263 Bit16u vheads, vspt, vcylinders;
6264 Bit16u head, sector, cylinder, nbsectors;
6265 Bit32u vlba, ilba, slba, elba;
6266 Bit16u before, segment, offset;
6267 Bit8u atacmd[12];
6268
6269 BX_DEBUG_INT13_ET("int13_cdemu: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
6270
6271 /* at this point, we are emulating a floppy/harddisk */
6272
6273 // Recompute the device number
6274 device = read_byte(ebda_seg,&EbdaData->cdemu.controller_index) * 2;
6275 device += read_byte(ebda_seg,&EbdaData->cdemu.device_spec);
6276
6277 SET_DISK_RET_STATUS(0x00);
6278
6279 /* basic checks : emulation should be active, dl should equal the emulated drive */
6280 if( (read_byte(ebda_seg,&EbdaData->cdemu.active) ==0 )
6281 || (read_byte(ebda_seg,&EbdaData->cdemu.emulated_drive ) != GET_DL())) {
6282 BX_INFO("int13_cdemu: function %02x, emulation not active for DL= %02x\n", GET_AH(), GET_DL());
6283 goto int13_fail;
6284 }
6285
6286 switch (GET_AH()) {
6287
6288 // all those functions return SUCCESS
6289 case 0x00: /* disk controller reset */
6290 case 0x09: /* initialize drive parameters */
6291 case 0x0c: /* seek to specified cylinder */
6292 case 0x0d: /* alternate disk reset */ // FIXME ElTorito Various. should really reset ?
6293 case 0x10: /* check drive ready */ // FIXME ElTorito Various. should check if ready ?
6294 case 0x11: /* recalibrate */
6295 case 0x14: /* controller internal diagnostic */
6296 case 0x16: /* detect disk change */
6297 goto int13_success;
6298 break;
6299
6300 // all those functions return disk write-protected
6301 case 0x03: /* write disk sectors */
6302 case 0x05: /* format disk track */
6303 SET_AH(0x03);
6304 goto int13_fail_noah;
6305 break;
6306
6307 case 0x01: /* read disk status */
6308 status=read_byte(0x0040, 0x0074);
6309 SET_AH(status);
6310 SET_DISK_RET_STATUS(0);
6311
6312 /* set CF if error status read */
6313 if (status) goto int13_fail_nostatus;
6314 else goto int13_success_noah;
6315 break;
6316
6317 case 0x02: // read disk sectors
6318 case 0x04: // verify disk sectors
6319 vspt = read_word(ebda_seg,&EbdaData->cdemu.vdevice.spt);
6320 vcylinders = read_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders);
6321 vheads = read_word(ebda_seg,&EbdaData->cdemu.vdevice.heads);
6322
6323 ilba = read_dword(ebda_seg,&EbdaData->cdemu.ilba);
6324
6325 sector = GET_CL() & 0x003f;
6326 cylinder = (GET_CL() & 0x00c0) << 2 | GET_CH();
6327 head = GET_DH();
6328 nbsectors = GET_AL();
6329 segment = ES;
6330 offset = BX;
6331
6332 // no sector to read ?
6333 if(nbsectors==0) goto int13_success;
6334
6335 // sanity checks sco openserver needs this!
6336 if ((sector > vspt)
6337 || (cylinder >= vcylinders)
6338 || (head >= vheads)) {
6339 goto int13_fail;
6340 }
6341
6342 // After controls, verify do nothing
6343 if (GET_AH() == 0x04) goto int13_success;
6344
6345 segment = ES+(BX / 16);
6346 offset = BX % 16;
6347
6348 // calculate the virtual lba inside the image
6349 vlba=((((Bit32u)cylinder*(Bit32u)vheads)+(Bit32u)head)*(Bit32u)vspt)+((Bit32u)(sector-1));
6350
6351 // In advance so we don't loose the count
6352 SET_AL(nbsectors);
6353
6354 // start lba on cd
6355 slba = (Bit32u)vlba/4;
6356 before= (Bit16u)vlba%4;
6357
6358 // end lba on cd
6359 elba = (Bit32u)(vlba+nbsectors-1)/4;
6360
6361 memsetb(get_SS(),atacmd,0,12);
6362 atacmd[0]=0x28; // READ command
6363 atacmd[7]=((Bit16u)(elba-slba+1) & 0xff00) >> 8; // Sectors
6364 atacmd[8]=((Bit16u)(elba-slba+1) & 0x00ff); // Sectors
6365 atacmd[2]=(ilba+slba & 0xff000000) >> 24; // LBA
6366 atacmd[3]=(ilba+slba & 0x00ff0000) >> 16;
6367 atacmd[4]=(ilba+slba & 0x0000ff00) >> 8;
6368 atacmd[5]=(ilba+slba & 0x000000ff);
6369 if((status = ata_cmd_packet(device, 12, get_SS(), atacmd, before*512, nbsectors*512L, ATA_DATA_IN, segment,offset)) != 0) {
6370 BX_INFO("int13_cdemu: function %02x, error %02x !\n",GET_AH(),status);
6371 SET_AH(0x02);
6372 SET_AL(0);
6373 goto int13_fail_noah;
6374 }
6375
6376 goto int13_success;
6377 break;
6378
6379 case 0x08: /* read disk drive parameters */
6380 vspt=read_word(ebda_seg,&EbdaData->cdemu.vdevice.spt);
6381 vcylinders=read_word(ebda_seg,&EbdaData->cdemu.vdevice.cylinders) - 1;
6382 vheads=read_word(ebda_seg,&EbdaData->cdemu.vdevice.heads) - 1;
6383
6384 SET_AL( 0x00 );
6385 SET_BL( 0x00 );
6386 SET_CH( vcylinders & 0xff );
6387 SET_CL((( vcylinders >> 2) & 0xc0) | ( vspt & 0x3f ));
6388 SET_DH( vheads );
6389 SET_DL( 0x02 ); // FIXME ElTorito Various. should send the real count of drives 1 or 2
6390 // FIXME ElTorito Harddisk. should send the HD count
6391
6392 switch(read_byte(ebda_seg,&EbdaData->cdemu.media)) {
6393 case 0x01: SET_BL( 0x02 ); break;
6394 case 0x02: SET_BL( 0x04 ); break;
6395 case 0x03: SET_BL( 0x06 ); break;
6396 }
6397
6398 ASM_START
6399 push bp
6400 mov bp, sp
6401 mov ax, #diskette_param_table2
6402 mov _int13_cdemu.DI+2[bp], ax
6403 mov _int13_cdemu.ES+2[bp], cs
6404 pop bp
6405 ASM_END
6406 goto int13_success;
6407 break;
6408
6409 case 0x15: /* read disk drive size */
6410 // FIXME ElTorito Harddisk. What geometry to send ?
6411 SET_AH(0x03);
6412 goto int13_success_noah;
6413 break;
6414
6415 // all those functions return unimplemented
6416 case 0x0a: /* read disk sectors with ECC */
6417 case 0x0b: /* write disk sectors with ECC */
6418 case 0x18: /* set media type for format */
6419 case 0x41: // IBM/MS installation check
6420 // FIXME ElTorito Harddisk. Darwin would like to use EDD
6421 case 0x42: // IBM/MS extended read
6422 case 0x43: // IBM/MS extended write
6423 case 0x44: // IBM/MS verify sectors
6424 case 0x45: // IBM/MS lock/unlock drive
6425 case 0x46: // IBM/MS eject media
6426 case 0x47: // IBM/MS extended seek
6427 case 0x48: // IBM/MS get drive parameters
6428 case 0x49: // IBM/MS extended media change
6429 case 0x4e: // ? - set hardware configuration
6430 case 0x50: // ? - send packet command
6431 default:
6432 BX_INFO("int13_cdemu function AH=%02x unsupported, returns fail\n", GET_AH());
6433 goto int13_fail;
6434 break;
6435 }
6436
6437 int13_fail:
6438 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
6439 int13_fail_noah:
6440 SET_DISK_RET_STATUS(GET_AH());
6441 int13_fail_nostatus:
6442 SET_CF(); // error occurred
6443 return;
6444
6445 int13_success:
6446 SET_AH(0x00); // no error
6447 int13_success_noah:
6448 SET_DISK_RET_STATUS(0x00);
6449 CLEAR_CF(); // no error
6450 return;
6451 }
6452
6453 // ---------------------------------------------------------------------------
6454 // End of int13 when emulating a device from the cd
6455 // ---------------------------------------------------------------------------
6456
6457 #endif // BX_ELTORITO_BOOT
6458
6459 #else //BX_USE_ATADRV
6460
6461 void
6462 outLBA(cylinder,hd_heads,head,hd_sectors,sector,dl)
6463 Bit16u cylinder;
6464 Bit16u hd_heads;
6465 Bit16u head;
6466 Bit16u hd_sectors;
6467 Bit16u sector;
6468 Bit16u dl;
6469 {
6470 ASM_START
6471 push bp
6472 mov bp, sp
6473 push eax
6474 push ebx
6475 push edx
6476 xor eax,eax
6477 mov ax,4[bp] // cylinder
6478 xor ebx,ebx
6479 mov bl,6[bp] // hd_heads
6480 imul ebx
6481
6482 mov bl,8[bp] // head
6483 add eax,ebx
6484 mov bl,10[bp] // hd_sectors
6485 imul ebx
6486 mov bl,12[bp] // sector
6487 add eax,ebx
6488
6489 dec eax
6490 mov dx,#0x1f3
6491 out dx,al
6492 mov dx,#0x1f4
6493 mov al,ah
6494 out dx,al
6495 shr eax,#16
6496 mov dx,#0x1f5
6497 out dx,al
6498 and ah,#0xf
6499 mov bl,14[bp] // dl
6500 and bl,#1
6501 shl bl,#4
6502 or ah,bl
6503 or ah,#0xe0
6504 mov al,ah
6505 mov dx,#0x01f6
6506 out dx,al
6507 pop edx
6508 pop ebx
6509 pop eax
6510 pop bp
6511 ASM_END
6512 }
6513
6514 void
6515 int13_harddisk(EHAX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS)
6516 Bit16u EHAX, DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS;
6517 {
6518 Bit8u drive, num_sectors, sector, head, status, mod;
6519 Bit8u drive_map;
6520 Bit8u n_drives;
6521 Bit16u cyl_mod, ax;
6522 Bit16u max_cylinder, cylinder, total_sectors;
6523 Bit16u hd_cylinders;
6524 Bit8u hd_heads, hd_sectors;
6525 Bit16u val16;
6526 Bit8u sector_count;
6527 unsigned int i;
6528 Bit16u tempbx;
6529 Bit16u dpsize;
6530
6531 Bit16u count, segment, offset;
6532 Bit32u lba;
6533 Bit16u error;
6534
6535 BX_DEBUG_INT13_HD("int13 harddisk: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
6536
6537 write_byte(0x0040, 0x008e, 0); // clear completion flag
6538
6539 /* at this point, DL is >= 0x80 to be passed from the floppy int13h
6540 handler code */
6541 /* check how many disks first (cmos reg 0x12), return an error if
6542 drive not present */
6543 drive_map = inb_cmos(0x12);
6544 drive_map = (((drive_map & 0xf0)==0) ? 0 : 1) |
6545 (((drive_map & 0x0f)==0) ? 0 : 2);
6546 n_drives = (drive_map==0) ? 0 :
6547 ((drive_map==3) ? 2 : 1);
6548
6549 if (!(drive_map & (1<<(GET_ELDL()&0x7f)))) { /* allow 0, 1, or 2 disks */
6550 SET_AH(0x01);
6551 SET_DISK_RET_STATUS(0x01);
6552 SET_CF(); /* error occurred */
6553 return;
6554 }
6555
6556 switch (GET_AH()) {
6557
6558 case 0x00: /* disk controller reset */
6559 BX_DEBUG_INT13_HD("int13_f00\n");
6560
6561 SET_AH(0);
6562 SET_DISK_RET_STATUS(0);
6563 set_diskette_ret_status(0);
6564 set_diskette_current_cyl(0, 0); /* current cylinder, diskette 1 */
6565 set_diskette_current_cyl(1, 0); /* current cylinder, diskette 2 */
6566 CLEAR_CF(); /* successful */
6567 return;
6568 break;
6569
6570 case 0x01: /* read disk status */
6571 BX_DEBUG_INT13_HD("int13_f01\n");
6572 status = read_byte(0x0040, 0x0074);
6573 SET_AH(status);
6574 SET_DISK_RET_STATUS(0);
6575 /* set CF if error status read */
6576 if (status) SET_CF();
6577 else CLEAR_CF();
6578 return;
6579 break;
6580
6581 case 0x04: // verify disk sectors
6582 case 0x02: // read disk sectors
6583 drive = GET_ELDL();
6584 get_hd_geometry(drive, &hd_cylinders, &hd_heads, &hd_sectors);
6585
6586 num_sectors = GET_AL();
6587 cylinder = (GET_CL() & 0x00c0) << 2 | GET_CH();
6588 sector = (GET_CL() & 0x3f);
6589 head = GET_DH();
6590
6591
6592 if (hd_cylinders > 1024) {
6593 if (hd_cylinders <= 2048) {
6594 cylinder <<= 1;
6595 }
6596 else if (hd_cylinders <= 4096) {
6597 cylinder <<= 2;
6598 }
6599 else if (hd_cylinders <= 8192) {
6600 cylinder <<= 3;
6601 }
6602 else { // hd_cylinders <= 16384
6603 cylinder <<= 4;
6604 }
6605
6606 ax = head / hd_heads;
6607 cyl_mod = ax & 0xff;
6608 head = ax >> 8;
6609 cylinder |= cyl_mod;
6610 }
6611
6612 if ( (cylinder >= hd_cylinders) ||
6613 (sector > hd_sectors) ||
6614 (head >= hd_heads) ) {
6615 SET_AH(1);
6616 SET_DISK_RET_STATUS(1);
6617 SET_CF(); /* error occurred */
6618 return;
6619 }
6620
6621 if ( (num_sectors > 128) || (num_sectors == 0) )
6622 BX_PANIC("int13_harddisk: num_sectors out of range!\n");
6623
6624 if (head > 15)
6625 BX_PANIC("hard drive BIOS:(read/verify) head > 15\n");
6626
6627 if ( GET_AH() == 0x04 ) {
6628 SET_AH(0);
6629 SET_DISK_RET_STATUS(0);
6630 CLEAR_CF();
6631 return;
6632 }
6633
6634 status = inb(0x1f7);
6635 if (status & 0x80) {
6636 BX_PANIC("hard drive BIOS:(read/verify) BUSY bit set\n");
6637 }
6638 outb(0x01f2, num_sectors);
6639 /* activate LBA? (tomv) */
6640 if (hd_heads > 16) {
6641 BX_DEBUG_INT13_HD("CHS: %x %x %x\n", cylinder, head, sector);
6642 outLBA(cylinder,hd_heads,head,hd_sectors,sector,drive);
6643 }
6644 else {
6645 outb(0x01f3, sector);
6646 outb(0x01f4, cylinder & 0x00ff);
6647 outb(0x01f5, cylinder >> 8);
6648 outb(0x01f6, 0xa0 | ((drive & 0x01)<<4) | (head & 0x0f));
6649 }
6650 outb(0x01f7, 0x20);
6651
6652 while (1) {
6653 status = inb(0x1f7);
6654 if ( !(status & 0x80) ) break;
6655 }
6656
6657 if (status & 0x01) {
6658 BX_PANIC("hard drive BIOS:(read/verify) read error\n");
6659 } else if ( !(status & 0x08) ) {
6660 BX_DEBUG_INT13_HD("status was %02x\n", (unsigned) status);
6661 BX_PANIC("hard drive BIOS:(read/verify) expected DRQ=1\n");
6662 }
6663
6664 sector_count = 0;
6665 tempbx = BX;
6666
6667 ASM_START
6668 sti ;; enable higher priority interrupts
6669 ASM_END
6670
6671 while (1) {
6672 ASM_START
6673 ;; store temp bx in real DI register
6674 push bp
6675 mov bp, sp
6676 mov di, _int13_harddisk.tempbx + 2 [bp]
6677 pop bp
6678
6679 ;; adjust if there will be an overrun
6680 cmp di, #0xfe00
6681 jbe i13_f02_no_adjust
6682 i13_f02_adjust:
6683 sub di, #0x0200 ; sub 512 bytes from offset
6684 mov ax, es
6685 add ax, #0x0020 ; add 512 to segment
6686 mov es, ax
6687
6688 i13_f02_no_adjust:
6689 mov cx, #0x0100 ;; counter (256 words = 512b)
6690 mov dx, #0x01f0 ;; AT data read port
6691
6692 rep
6693 insw ;; CX words transfered from port(DX) to ES:[DI]
6694
6695 i13_f02_done:
6696 ;; store real DI register back to temp bx
6697 push bp
6698 mov bp, sp
6699 mov _int13_harddisk.tempbx + 2 [bp], di
6700 pop bp
6701 ASM_END
6702
6703 sector_count++;
6704 num_sectors--;
6705 if (num_sectors == 0) {
6706 status = inb(0x1f7);
6707 if ( (status & 0xc9) != 0x40 )
6708 BX_PANIC("no sectors left to read/verify, status is %02x\n", (unsigned) status);
6709 break;
6710 }
6711 else {
6712 status = inb(0x1f7);
6713 if ( (status & 0xc9) != 0x48 )
6714 BX_PANIC("more sectors left to read/verify, status is %02x\n", (unsigned) status);
6715 continue;
6716 }
6717 }
6718
6719 SET_AH(0);
6720 SET_DISK_RET_STATUS(0);
6721 SET_AL(sector_count);
6722 CLEAR_CF(); /* successful */
6723 return;
6724 break;
6725
6726
6727 case 0x03: /* write disk sectors */
6728 BX_DEBUG_INT13_HD("int13_f03\n");
6729 drive = GET_ELDL ();
6730 get_hd_geometry(drive, &hd_cylinders, &hd_heads, &hd_sectors);
6731
6732 num_sectors = GET_AL();
6733 cylinder = GET_CH();
6734 cylinder |= ( ((Bit16u) GET_CL()) << 2) & 0x300;
6735 sector = (GET_CL() & 0x3f);
6736 head = GET_DH();
6737
6738 if (hd_cylinders > 1024) {
6739 if (hd_cylinders <= 2048) {
6740 cylinder <<= 1;
6741 }
6742 else if (hd_cylinders <= 4096) {
6743 cylinder <<= 2;
6744 }
6745 else if (hd_cylinders <= 8192) {
6746 cylinder <<= 3;
6747 }
6748 else { // hd_cylinders <= 16384
6749 cylinder <<= 4;
6750 }
6751
6752 ax = head / hd_heads;
6753 cyl_mod = ax & 0xff;
6754 head = ax >> 8;
6755 cylinder |= cyl_mod;
6756 }
6757
6758 if ( (cylinder >= hd_cylinders) ||
6759 (sector > hd_sectors) ||
6760 (head >= hd_heads) ) {
6761 SET_AH( 1);
6762 SET_DISK_RET_STATUS(1);
6763 SET_CF(); /* error occurred */
6764 return;
6765 }
6766
6767 if ( (num_sectors > 128) || (num_sectors == 0) )
6768 BX_PANIC("int13_harddisk: num_sectors out of range!\n");
6769
6770 if (head > 15)
6771 BX_PANIC("hard drive BIOS:(read) head > 15\n");
6772
6773 status = inb(0x1f7);
6774 if (status & 0x80) {
6775 BX_PANIC("hard drive BIOS:(read) BUSY bit set\n");
6776 }
6777 // should check for Drive Ready Bit also in status reg
6778 outb(0x01f2, num_sectors);
6779
6780 /* activate LBA? (tomv) */
6781 if (hd_heads > 16) {
6782 BX_DEBUG_INT13_HD("CHS (write): %x %x %x\n", cylinder, head, sector);
6783 outLBA(cylinder,hd_heads,head,hd_sectors,sector,GET_ELDL());
6784 }
6785 else {
6786 outb(0x01f3, sector);
6787 outb(0x01f4, cylinder & 0x00ff);
6788 outb(0x01f5, cylinder >> 8);
6789 outb(0x01f6, 0xa0 | ((GET_ELDL() & 0x01)<<4) | (head & 0x0f));
6790 }
6791 outb(0x01f7, 0x30);
6792
6793 // wait for busy bit to turn off after seeking
6794 while (1) {
6795 status = inb(0x1f7);
6796 if ( !(status & 0x80) ) break;
6797 }
6798
6799 if ( !(status & 0x08) ) {
6800 BX_DEBUG_INT13_HD("status was %02x\n", (unsigned) status);
6801 BX_PANIC("hard drive BIOS:(write) data-request bit not set\n");
6802 }
6803
6804 sector_count = 0;
6805 tempbx = BX;
6806
6807 ASM_START
6808 sti ;; enable higher priority interrupts
6809 ASM_END
6810
6811 while (1) {
6812 ASM_START
6813 ;; store temp bx in real SI register
6814 push bp
6815 mov bp, sp
6816 mov si, _int13_harddisk.tempbx + 2 [bp]
6817 pop bp
6818
6819 ;; adjust if there will be an overrun
6820 cmp si, #0xfe00
6821 jbe i13_f03_no_adjust
6822 i13_f03_adjust:
6823 sub si, #0x0200 ; sub 512 bytes from offset
6824 mov ax, es
6825 add ax, #0x0020 ; add 512 to segment
6826 mov es, ax
6827
6828 i13_f03_no_adjust:
6829 mov cx, #0x0100 ;; counter (256 words = 512b)
6830 mov dx, #0x01f0 ;; AT data read port
6831
6832 seg ES
6833 rep
6834 outsw ;; CX words tranfered from ES:[SI] to port(DX)
6835
6836 ;; store real SI register back to temp bx
6837 push bp
6838 mov bp, sp
6839 mov _int13_harddisk.tempbx + 2 [bp], si
6840 pop bp
6841 ASM_END
6842
6843 sector_count++;
6844 num_sectors--;
6845 if (num_sectors == 0) {
6846 status = inb(0x1f7);
6847 if ( (status & 0xe9) != 0x40 )
6848 BX_PANIC("no sectors left to write, status is %02x\n", (unsigned) status);
6849 break;
6850 }
6851 else {
6852 status = inb(0x1f7);
6853 if ( (status & 0xc9) != 0x48 )
6854 BX_PANIC("more sectors left to write, status is %02x\n", (unsigned) status);
6855 continue;
6856 }
6857 }
6858
6859 SET_AH(0);
6860 SET_DISK_RET_STATUS(0);
6861 SET_AL(sector_count);
6862 CLEAR_CF(); /* successful */
6863 return;
6864 break;
6865
6866 case 0x05: /* format disk track */
6867 BX_DEBUG_INT13_HD("int13_f05\n");
6868 BX_PANIC("format disk track called\n");
6869 /* nop */
6870 SET_AH(0);
6871 SET_DISK_RET_STATUS(0);
6872 CLEAR_CF(); /* successful */
6873 return;
6874 break;
6875
6876 case 0x08: /* read disk drive parameters */
6877 BX_DEBUG_INT13_HD("int13_f08\n");
6878
6879 drive = GET_ELDL ();
6880 get_hd_geometry(drive, &hd_cylinders, &hd_heads, &hd_sectors);
6881
6882 // translate CHS
6883 //
6884 if (hd_cylinders <= 1024) {
6885 // hd_cylinders >>= 0;
6886 // hd_heads <<= 0;
6887 }
6888 else if (hd_cylinders <= 2048) {
6889 hd_cylinders >>= 1;
6890 hd_heads <<= 1;
6891 }
6892 else if (hd_cylinders <= 4096) {
6893 hd_cylinders >>= 2;
6894 hd_heads <<= 2;
6895 }
6896 else if (hd_cylinders <= 8192) {
6897 hd_cylinders >>= 3;
6898 hd_heads <<= 3;
6899 }
6900 else { // hd_cylinders <= 16384
6901 hd_cylinders >>= 4;
6902 hd_heads <<= 4;
6903 }
6904
6905 max_cylinder = hd_cylinders - 2; /* 0 based */
6906 SET_AL(0);
6907 SET_CH(max_cylinder & 0xff);
6908 SET_CL(((max_cylinder >> 2) & 0xc0) | (hd_sectors & 0x3f));
6909 SET_DH(hd_heads - 1);
6910 SET_DL(n_drives); /* returns 0, 1, or 2 hard drives */
6911 SET_AH(0);
6912 SET_DISK_RET_STATUS(0);
6913 CLEAR_CF(); /* successful */
6914
6915 return;
6916 break;
6917
6918 case 0x09: /* initialize drive parameters */
6919 BX_DEBUG_INT13_HD("int13_f09\n");
6920 SET_AH(0);
6921 SET_DISK_RET_STATUS(0);
6922 CLEAR_CF(); /* successful */
6923 return;
6924 break;
6925
6926 case 0x0a: /* read disk sectors with ECC */
6927 BX_DEBUG_INT13_HD("int13_f0a\n");
6928 case 0x0b: /* write disk sectors with ECC */
6929 BX_DEBUG_INT13_HD("int13_f0b\n");
6930 BX_PANIC("int13h Functions 0Ah & 0Bh not implemented!\n");
6931 return;
6932 break;
6933
6934 case 0x0c: /* seek to specified cylinder */
6935 BX_DEBUG_INT13_HD("int13_f0c\n");
6936 BX_INFO("int13h function 0ch (seek) not implemented!\n");
6937 SET_AH(0);
6938 SET_DISK_RET_STATUS(0);
6939 CLEAR_CF(); /* successful */
6940 return;
6941 break;
6942
6943 case 0x0d: /* alternate disk reset */
6944 BX_DEBUG_INT13_HD("int13_f0d\n");
6945 SET_AH(0);
6946 SET_DISK_RET_STATUS(0);
6947 CLEAR_CF(); /* successful */
6948 return;
6949 break;
6950
6951 case 0x10: /* check drive ready */
6952 BX_DEBUG_INT13_HD("int13_f10\n");
6953 //SET_AH(0);
6954 //SET_DISK_RET_STATUS(0);
6955 //CLEAR_CF(); /* successful */
6956 //return;
6957 //break;
6958
6959 // should look at 40:8E also???
6960 status = inb(0x01f7);
6961 if ( (status & 0xc0) == 0x40 ) {
6962 SET_AH(0);
6963 SET_DISK_RET_STATUS(0);
6964 CLEAR_CF(); // drive ready
6965 return;
6966 }
6967 else {
6968 SET_AH(0xAA);
6969 SET_DISK_RET_STATUS(0xAA);
6970 SET_CF(); // not ready
6971 return;
6972 }
6973 break;
6974
6975 case 0x11: /* recalibrate */
6976 BX_DEBUG_INT13_HD("int13_f11\n");
6977 SET_AH(0);
6978 SET_DISK_RET_STATUS(0);
6979 CLEAR_CF(); /* successful */
6980 return;
6981 break;
6982
6983 case 0x14: /* controller internal diagnostic */
6984 BX_DEBUG_INT13_HD("int13_f14\n");
6985 SET_AH(0);
6986 SET_DISK_RET_STATUS(0);
6987 CLEAR_CF(); /* successful */
6988 SET_AL(0);
6989 return;
6990 break;
6991
6992 case 0x15: /* read disk drive size */
6993 drive = GET_ELDL();
6994 get_hd_geometry(drive, &hd_cylinders, &hd_heads, &hd_sectors);
6995 ASM_START
6996 push bp
6997 mov bp, sp
6998 mov al, _int13_harddisk.hd_heads + 2 [bp]
6999 mov ah, _int13_harddisk.hd_sectors + 2 [bp]
7000 mul al, ah ;; ax = heads * sectors
7001 mov bx, _int13_harddisk.hd_cylinders + 2 [bp]
7002 dec bx ;; use (cylinders - 1) ???
7003 mul ax, bx ;; dx:ax = (cylinders -1) * (heads * sectors)
7004 ;; now we need to move the 32bit result dx:ax to what the
7005 ;; BIOS wants which is cx:dx.
7006 ;; and then into CX:DX on the stack
7007 mov _int13_harddisk.CX + 2 [bp], dx
7008 mov _int13_harddisk.DX + 2 [bp], ax
7009 pop bp
7010 ASM_END
7011 SET_AH(3); // hard disk accessible
7012 SET_DISK_RET_STATUS(0); // ??? should this be 0
7013 CLEAR_CF(); // successful
7014 return;
7015 break;
7016
7017 case 0x18: // set media type for format
7018 case 0x41: // IBM/MS
7019 case 0x42: // IBM/MS
7020 case 0x43: // IBM/MS
7021 case 0x44: // IBM/MS
7022 case 0x45: // IBM/MS lock/unlock drive
7023 case 0x46: // IBM/MS eject media
7024 case 0x47: // IBM/MS extended seek
7025 case 0x49: // IBM/MS extended media change
7026 case 0x50: // IBM/MS send packet command
7027 default:
7028 BX_INFO("int13_harddisk: unsupported AH=%02x\n", GET_AH());
7029
7030 SET_AH(1); // code=invalid function in AH or invalid parameter
7031 SET_DISK_RET_STATUS(1);
7032 SET_CF(); /* unsuccessful */
7033 return;
7034 break;
7035 }
7036 }
7037
7038 static char panic_msg_reg12h[] = "HD%d cmos reg 12h not type F\n";
7039 static char panic_msg_reg19h[] = "HD%d cmos reg %02xh not user definable type 47\n";
7040
7041 void
7042 get_hd_geometry(drive, hd_cylinders, hd_heads, hd_sectors)
7043 Bit8u drive;
7044 Bit16u *hd_cylinders;
7045 Bit8u *hd_heads;
7046 Bit8u *hd_sectors;
7047 {
7048 Bit8u hd_type;
7049 Bit16u ss;
7050 Bit16u cylinders;
7051 Bit8u iobase;
7052
7053 ss = get_SS();
7054 if (drive == 0x80) {
7055 hd_type = inb_cmos(0x12) & 0xf0;
7056 if (hd_type != 0xf0)
7057 BX_INFO(panic_msg_reg12h,0);
7058 hd_type = inb_cmos(0x19); // HD0: extended type
7059 if (hd_type != 47)
7060 BX_INFO(panic_msg_reg19h,0,0x19);
7061 iobase = 0x1b;
7062 } else {
7063 hd_type = inb_cmos(0x12) & 0x0f;
7064 if (hd_type != 0x0f)
7065 BX_INFO(panic_msg_reg12h,1);
7066 hd_type = inb_cmos(0x1a); // HD1: extended type
7067 if (hd_type != 47)
7068 BX_INFO(panic_msg_reg19h,0,0x1a);
7069 iobase = 0x24;
7070 }
7071
7072 // cylinders
7073 cylinders = inb_cmos(iobase) | (inb_cmos(iobase+1) << 8);
7074 write_word(ss, hd_cylinders, cylinders);
7075
7076 // heads
7077 write_byte(ss, hd_heads, inb_cmos(iobase+2));
7078
7079 // sectors per track
7080 write_byte(ss, hd_sectors, inb_cmos(iobase+8));
7081 }
7082
7083 #endif //else BX_USE_ATADRV
7084
7085 #if BX_SUPPORT_FLOPPY
7086
7087 //////////////////////
7088 // FLOPPY functions //
7089 //////////////////////
7090
7091 void floppy_reset_controller()
7092 {
7093 Bit8u val8;
7094
7095 // Reset controller
7096 val8 = inb(0x03f2);
7097 outb(0x03f2, val8 & ~0x04);
7098 outb(0x03f2, val8 | 0x04);
7099
7100 // Wait for controller to come out of reset
7101 do {
7102 val8 = inb(0x3f4);
7103 } while ( (val8 & 0xc0) != 0x80 );
7104 }
7105
7106 void floppy_prepare_controller(drive)
7107 Bit16u drive;
7108 {
7109 Bit8u val8, dor, prev_reset;
7110
7111 // set 40:3e bit 7 to 0
7112 val8 = read_byte(0x0040, 0x003e);
7113 val8 &= 0x7f;
7114 write_byte(0x0040, 0x003e, val8);
7115
7116 // turn on motor of selected drive, DMA & int enabled, normal operation
7117 prev_reset = inb(0x03f2) & 0x04;
7118 if (drive)
7119 dor = 0x20;
7120 else
7121 dor = 0x10;
7122 dor |= 0x0c;
7123 dor |= drive;
7124 outb(0x03f2, dor);
7125
7126 // reset the disk motor timeout value of INT 08
7127 write_byte(0x40,0x40, BX_FLOPPY_ON_CNT);
7128
7129 // wait for drive readiness
7130 do {
7131 val8 = inb(0x3f4);
7132 } while ( (val8 & 0xc0) != 0x80 );
7133
7134 if (prev_reset == 0) {
7135 // turn on interrupts
7136 ASM_START
7137 sti
7138 ASM_END
7139 // wait on 40:3e bit 7 to become 1
7140 do {
7141 val8 = read_byte(0x0040, 0x003e);
7142 } while ( (val8 & 0x80) == 0 );
7143 val8 &= 0x7f;
7144 ASM_START
7145 cli
7146 ASM_END
7147 write_byte(0x0040, 0x003e, val8);
7148 }
7149 }
7150
7151 bx_bool
7152 floppy_media_known(drive)
7153 Bit16u drive;
7154 {
7155 Bit8u val8;
7156 Bit16u media_state_offset;
7157
7158 val8 = read_byte(0x0040, 0x003e); // diskette recal status
7159 if (drive)
7160 val8 >>= 1;
7161 val8 &= 0x01;
7162 if (val8 == 0)
7163 return(0);
7164
7165 media_state_offset = 0x0090;
7166 if (drive)
7167 media_state_offset += 1;
7168
7169 val8 = read_byte(0x0040, media_state_offset);
7170 val8 = (val8 >> 4) & 0x01;
7171 if (val8 == 0)
7172 return(0);
7173
7174 // check pass, return KNOWN
7175 return(1);
7176 }
7177
7178 bx_bool
7179 floppy_media_sense(drive)
7180 Bit16u drive;
7181 {
7182 bx_bool retval;
7183 Bit16u media_state_offset;
7184 Bit8u drive_type, config_data, media_state;
7185
7186 if (floppy_drive_recal(drive) == 0) {
7187 return(0);
7188 }
7189
7190 // for now cheat and get drive type from CMOS,
7191 // assume media is same as drive type
7192
7193 // ** config_data **
7194 // Bitfields for diskette media control:
7195 // Bit(s) Description (Table M0028)
7196 // 7-6 last data rate set by controller
7197 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
7198 // 5-4 last diskette drive step rate selected
7199 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
7200 // 3-2 {data rate at start of operation}
7201 // 1-0 reserved
7202
7203 // ** media_state **
7204 // Bitfields for diskette drive media state:
7205 // Bit(s) Description (Table M0030)
7206 // 7-6 data rate
7207 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
7208 // 5 double stepping required (e.g. 360kB in 1.2MB)
7209 // 4 media type established
7210 // 3 drive capable of supporting 4MB media
7211 // 2-0 on exit from BIOS, contains
7212 // 000 trying 360kB in 360kB
7213 // 001 trying 360kB in 1.2MB
7214 // 010 trying 1.2MB in 1.2MB
7215 // 011 360kB in 360kB established
7216 // 100 360kB in 1.2MB established
7217 // 101 1.2MB in 1.2MB established
7218 // 110 reserved
7219 // 111 all other formats/drives
7220
7221 drive_type = inb_cmos(0x10);
7222 if (drive == 0)
7223 drive_type >>= 4;
7224 else
7225 drive_type &= 0x0f;
7226 if ( drive_type == 1 ) {
7227 // 360K 5.25" drive
7228 config_data = 0x00; // 0000 0000
7229 media_state = 0x25; // 0010 0101
7230 retval = 1;
7231 }
7232 else if ( drive_type == 2 ) {
7233 // 1.2 MB 5.25" drive
7234 config_data = 0x00; // 0000 0000
7235 media_state = 0x25; // 0010 0101 // need double stepping??? (bit 5)
7236 retval = 1;
7237 }
7238 else if ( drive_type == 3 ) {
7239 // 720K 3.5" drive
7240 config_data = 0x00; // 0000 0000 ???
7241 media_state = 0x17; // 0001 0111
7242 retval = 1;
7243 }
7244 else if ( drive_type == 4 ) {
7245 // 1.44 MB 3.5" drive
7246 config_data = 0x00; // 0000 0000
7247 media_state = 0x17; // 0001 0111
7248 retval = 1;
7249 }
7250 else if ( drive_type == 5 ) {
7251 // 2.88 MB 3.5" drive
7252 config_data = 0xCC; // 1100 1100
7253 media_state = 0xD7; // 1101 0111
7254 retval = 1;
7255 }
7256 //
7257 // Extended floppy size uses special cmos setting
7258 else if ( drive_type == 6 ) {
7259 // 160k 5.25" drive
7260 config_data = 0x00; // 0000 0000
7261 media_state = 0x27; // 0010 0111
7262 retval = 1;
7263 }
7264 else if ( drive_type == 7 ) {
7265 // 180k 5.25" drive
7266 config_data = 0x00; // 0000 0000
7267 media_state = 0x27; // 0010 0111
7268 retval = 1;
7269 }
7270 else if ( drive_type == 8 ) {
7271 // 320k 5.25" drive
7272 config_data = 0x00; // 0000 0000
7273 media_state = 0x27; // 0010 0111
7274 retval = 1;
7275 }
7276
7277 else {
7278 // not recognized
7279 config_data = 0x00; // 0000 0000
7280 media_state = 0x00; // 0000 0000
7281 retval = 0;
7282 }
7283
7284 if (drive == 0)
7285 media_state_offset = 0x90;
7286 else
7287 media_state_offset = 0x91;
7288 write_byte(0x0040, 0x008B, config_data);
7289 write_byte(0x0040, media_state_offset, media_state);
7290
7291 return(retval);
7292 }
7293
7294 bx_bool
7295 floppy_drive_recal(drive)
7296 Bit16u drive;
7297 {
7298 Bit8u val8;
7299 Bit16u curr_cyl_offset;
7300
7301 floppy_prepare_controller(drive);
7302
7303 // send Recalibrate command (2 bytes) to controller
7304 outb(0x03f5, 0x07); // 07: Recalibrate
7305 outb(0x03f5, drive); // 0=drive0, 1=drive1
7306
7307 // turn on interrupts
7308 ASM_START
7309 sti
7310 ASM_END
7311
7312 // wait on 40:3e bit 7 to become 1
7313 do {
7314 val8 = (read_byte(0x0040, 0x003e) & 0x80);
7315 } while ( val8 == 0 );
7316
7317 val8 = 0; // separate asm from while() loop
7318 // turn off interrupts
7319 ASM_START
7320 cli
7321 ASM_END
7322
7323 // set 40:3e bit 7 to 0, and calibrated bit
7324 val8 = read_byte(0x0040, 0x003e);
7325 val8 &= 0x7f;
7326 if (drive) {
7327 val8 |= 0x02; // Drive 1 calibrated
7328 curr_cyl_offset = 0x0095;
7329 } else {
7330 val8 |= 0x01; // Drive 0 calibrated
7331 curr_cyl_offset = 0x0094;
7332 }
7333 write_byte(0x0040, 0x003e, val8);
7334 write_byte(0x0040, curr_cyl_offset, 0); // current cylinder is 0
7335
7336 return(1);
7337 }
7338
7339
7340
7341 bx_bool
7342 floppy_drive_exists(drive)
7343 Bit16u drive;
7344 {
7345 Bit8u drive_type;
7346
7347 // check CMOS to see if drive exists
7348 drive_type = inb_cmos(0x10);
7349 if (drive == 0)
7350 drive_type >>= 4;
7351 else
7352 drive_type &= 0x0f;
7353 if ( drive_type == 0 )
7354 return(0);
7355 else
7356 return(1);
7357 }
7358
7359 void
7360 int13_diskette_function(DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS)
7361 Bit16u DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS;
7362 {
7363 Bit8u drive, num_sectors, track, sector, head, status;
7364 Bit16u base_address, base_count, base_es;
7365 Bit8u page, mode_register, val8, dor;
7366 Bit8u return_status[7];
7367 Bit8u drive_type, num_floppies, ah;
7368 Bit16u es, last_addr;
7369
7370 BX_DEBUG_INT13_FL("int13_diskette: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX, BX, CX, DX, ES);
7371
7372 ah = GET_AH();
7373
7374 switch ( ah ) {
7375 case 0x00: // diskette controller reset
7376 BX_DEBUG_INT13_FL("floppy f00\n");
7377 drive = GET_ELDL();
7378 if (drive > 1) {
7379 SET_AH(1); // invalid param
7380 set_diskette_ret_status(1);
7381 SET_CF();
7382 return;
7383 }
7384 drive_type = inb_cmos(0x10);
7385
7386 if (drive == 0)
7387 drive_type >>= 4;
7388 else
7389 drive_type &= 0x0f;
7390 if (drive_type == 0) {
7391 SET_AH(0x80); // drive not responding
7392 set_diskette_ret_status(0x80);
7393 SET_CF();
7394 return;
7395 }
7396 SET_AH(0);
7397 set_diskette_ret_status(0);
7398 CLEAR_CF(); // successful
7399 set_diskette_current_cyl(drive, 0); // current cylinder
7400 return;
7401
7402 case 0x01: // Read Diskette Status
7403 CLEAR_CF();
7404 val8 = read_byte(0x0000, 0x0441);
7405 SET_AH(val8);
7406 if (val8) {
7407 SET_CF();
7408 }
7409 return;
7410
7411 case 0x02: // Read Diskette Sectors
7412 case 0x03: // Write Diskette Sectors
7413 case 0x04: // Verify Diskette Sectors
7414 num_sectors = GET_AL();
7415 track = GET_CH();
7416 sector = GET_CL();
7417 head = GET_DH();
7418 drive = GET_ELDL();
7419
7420 if ((drive > 1) || (head > 1) || (sector == 0) ||
7421 (num_sectors == 0) || (num_sectors > 72)) {
7422 BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
7423 SET_AH(1);
7424 set_diskette_ret_status(1);
7425 SET_AL(0); // no sectors read
7426 SET_CF(); // error occurred
7427 return;
7428 }
7429
7430 // see if drive exists
7431 if (floppy_drive_exists(drive) == 0) {
7432 SET_AH(0x80); // not responding
7433 set_diskette_ret_status(0x80);
7434 SET_AL(0); // no sectors read
7435 SET_CF(); // error occurred
7436 return;
7437 }
7438
7439 // see if media in drive, and type is known
7440 if (floppy_media_known(drive) == 0) {
7441 if (floppy_media_sense(drive) == 0) {
7442 SET_AH(0x0C); // Media type not found
7443 set_diskette_ret_status(0x0C);
7444 SET_AL(0); // no sectors read
7445 SET_CF(); // error occurred
7446 return;
7447 }
7448 }
7449
7450 if (ah == 0x02) {
7451 // Read Diskette Sectors
7452
7453 //-----------------------------------
7454 // set up DMA controller for transfer
7455 //-----------------------------------
7456
7457 // es:bx = pointer to where to place information from diskette
7458 // port 04: DMA-1 base and current address, channel 2
7459 // port 05: DMA-1 base and current count, channel 2
7460 page = (ES >> 12); // upper 4 bits
7461 base_es = (ES << 4); // lower 16bits contributed by ES
7462 base_address = base_es + BX; // lower 16 bits of address
7463 // contributed by ES:BX
7464 if ( base_address < base_es ) {
7465 // in case of carry, adjust page by 1
7466 page++;
7467 }
7468 base_count = (num_sectors * 512) - 1;
7469
7470 // check for 64K boundary overrun
7471 last_addr = base_address + base_count;
7472 if (last_addr < base_address) {
7473 SET_AH(0x09);
7474 set_diskette_ret_status(0x09);
7475 SET_AL(0); // no sectors read
7476 SET_CF(); // error occurred
7477 return;
7478 }
7479
7480 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
7481 outb(0x000a, 0x06);
7482
7483 BX_DEBUG_INT13_FL("clear flip-flop\n");
7484 outb(0x000c, 0x00); // clear flip-flop
7485 outb(0x0004, base_address);
7486 outb(0x0004, base_address>>8);
7487 BX_DEBUG_INT13_FL("clear flip-flop\n");
7488 outb(0x000c, 0x00); // clear flip-flop
7489 outb(0x0005, base_count);
7490 outb(0x0005, base_count>>8);
7491
7492 // port 0b: DMA-1 Mode Register
7493 mode_register = 0x46; // single mode, increment, autoinit disable,
7494 // transfer type=write, channel 2
7495 BX_DEBUG_INT13_FL("setting mode register\n");
7496 outb(0x000b, mode_register);
7497
7498 BX_DEBUG_INT13_FL("setting page register\n");
7499 // port 81: DMA-1 Page Register, channel 2
7500 outb(0x0081, page);
7501
7502 BX_DEBUG_INT13_FL("unmask chan 2\n");
7503 outb(0x000a, 0x02); // unmask channel 2
7504
7505 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
7506 outb(0x000a, 0x02);
7507
7508 //--------------------------------------
7509 // set up floppy controller for transfer
7510 //--------------------------------------
7511 floppy_prepare_controller(drive);
7512
7513 // send read-normal-data command (9 bytes) to controller
7514 outb(0x03f5, 0xe6); // e6: read normal data
7515 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
7516 outb(0x03f5, track);
7517 outb(0x03f5, head);
7518 outb(0x03f5, sector);
7519 outb(0x03f5, 2); // 512 byte sector size
7520 outb(0x03f5, sector + num_sectors - 1); // last sector to read on track
7521 outb(0x03f5, 0); // Gap length
7522 outb(0x03f5, 0xff); // Gap length
7523
7524 // turn on interrupts
7525 ASM_START
7526 sti
7527 ASM_END
7528
7529 // wait on 40:3e bit 7 to become 1
7530 do {
7531 val8 = read_byte(0x0040, 0x0040);
7532 if (val8 == 0) {
7533 floppy_reset_controller();
7534 SET_AH(0x80); // drive not ready (timeout)
7535 set_diskette_ret_status(0x80);
7536 SET_AL(0); // no sectors read
7537 SET_CF(); // error occurred
7538 return;
7539 }
7540 val8 = (read_byte(0x0040, 0x003e) & 0x80);
7541 } while ( val8 == 0 );
7542
7543 val8 = 0; // separate asm from while() loop
7544 // turn off interrupts
7545 ASM_START
7546 cli
7547 ASM_END
7548
7549 // set 40:3e bit 7 to 0
7550 val8 = read_byte(0x0040, 0x003e);
7551 val8 &= 0x7f;
7552 write_byte(0x0040, 0x003e, val8);
7553
7554 // check port 3f4 for accessibility to status bytes
7555 val8 = inb(0x3f4);
7556 if ( (val8 & 0xc0) != 0xc0 )
7557 BX_PANIC("int13_diskette: ctrl not ready\n");
7558
7559 // read 7 return status bytes from controller
7560 // using loop index broken, have to unroll...
7561 return_status[0] = inb(0x3f5);
7562 return_status[1] = inb(0x3f5);
7563 return_status[2] = inb(0x3f5);
7564 return_status[3] = inb(0x3f5);
7565 return_status[4] = inb(0x3f5);
7566 return_status[5] = inb(0x3f5);
7567 return_status[6] = inb(0x3f5);
7568 // record in BIOS Data Area
7569 write_byte(0x0040, 0x0042, return_status[0]);
7570 write_byte(0x0040, 0x0043, return_status[1]);
7571 write_byte(0x0040, 0x0044, return_status[2]);
7572 write_byte(0x0040, 0x0045, return_status[3]);
7573 write_byte(0x0040, 0x0046, return_status[4]);
7574 write_byte(0x0040, 0x0047, return_status[5]);
7575 write_byte(0x0040, 0x0048, return_status[6]);
7576
7577 if ( (return_status[0] & 0xc0) != 0 ) {
7578 SET_AH(0x20);
7579 set_diskette_ret_status(0x20);
7580 SET_AL(0); // no sectors read
7581 SET_CF(); // error occurred
7582 return;
7583 }
7584
7585 // ??? should track be new val from return_status[3] ?
7586 set_diskette_current_cyl(drive, track);
7587 // AL = number of sectors read (same value as passed)
7588 SET_AH(0x00); // success
7589 CLEAR_CF(); // success
7590 return;
7591 } else if (ah == 0x03) {
7592 // Write Diskette Sectors
7593
7594 //-----------------------------------
7595 // set up DMA controller for transfer
7596 //-----------------------------------
7597
7598 // es:bx = pointer to where to place information from diskette
7599 // port 04: DMA-1 base and current address, channel 2
7600 // port 05: DMA-1 base and current count, channel 2
7601 page = (ES >> 12); // upper 4 bits
7602 base_es = (ES << 4); // lower 16bits contributed by ES
7603 base_address = base_es + BX; // lower 16 bits of address
7604 // contributed by ES:BX
7605 if ( base_address < base_es ) {
7606 // in case of carry, adjust page by 1
7607 page++;
7608 }
7609 base_count = (num_sectors * 512) - 1;
7610
7611 // check for 64K boundary overrun
7612 last_addr = base_address + base_count;
7613 if (last_addr < base_address) {
7614 SET_AH(0x09);
7615 set_diskette_ret_status(0x09);
7616 SET_AL(0); // no sectors read
7617 SET_CF(); // error occurred
7618 return;
7619 }
7620
7621 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
7622 outb(0x000a, 0x06);
7623
7624 outb(0x000c, 0x00); // clear flip-flop
7625 outb(0x0004, base_address);
7626 outb(0x0004, base_address>>8);
7627 outb(0x000c, 0x00); // clear flip-flop
7628 outb(0x0005, base_count);
7629 outb(0x0005, base_count>>8);
7630
7631 // port 0b: DMA-1 Mode Register
7632 mode_register = 0x4a; // single mode, increment, autoinit disable,
7633 // transfer type=read, channel 2
7634 outb(0x000b, mode_register);
7635
7636 // port 81: DMA-1 Page Register, channel 2
7637 outb(0x0081, page);
7638
7639 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
7640 outb(0x000a, 0x02);
7641
7642 //--------------------------------------
7643 // set up floppy controller for transfer
7644 //--------------------------------------
7645 floppy_prepare_controller(drive);
7646
7647 // send write-normal-data command (9 bytes) to controller
7648 outb(0x03f5, 0xc5); // c5: write normal data
7649 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
7650 outb(0x03f5, track);
7651 outb(0x03f5, head);
7652 outb(0x03f5, sector);
7653 outb(0x03f5, 2); // 512 byte sector size
7654 outb(0x03f5, sector + num_sectors - 1); // last sector to write on track
7655 outb(0x03f5, 0); // Gap length
7656 outb(0x03f5, 0xff); // Gap length
7657
7658 // turn on interrupts
7659 ASM_START
7660 sti
7661 ASM_END
7662
7663 // wait on 40:3e bit 7 to become 1
7664 do {
7665 val8 = read_byte(0x0040, 0x0040);
7666 if (val8 == 0) {
7667 floppy_reset_controller();
7668 SET_AH(0x80); // drive not ready (timeout)
7669 set_diskette_ret_status(0x80);
7670 SET_AL(0); // no sectors written
7671 SET_CF(); // error occurred
7672 return;
7673 }
7674 val8 = (read_byte(0x0040, 0x003e) & 0x80);
7675 } while ( val8 == 0 );
7676
7677 val8 = 0; // separate asm from while() loop
7678 // turn off interrupts
7679 ASM_START
7680 cli
7681 ASM_END
7682
7683 // set 40:3e bit 7 to 0
7684 val8 = read_byte(0x0040, 0x003e);
7685 val8 &= 0x7f;
7686 write_byte(0x0040, 0x003e, val8);
7687
7688 // check port 3f4 for accessibility to status bytes
7689 val8 = inb(0x3f4);
7690 if ( (val8 & 0xc0) != 0xc0 )
7691 BX_PANIC("int13_diskette: ctrl not ready\n");
7692
7693 // read 7 return status bytes from controller
7694 // using loop index broken, have to unroll...
7695 return_status[0] = inb(0x3f5);
7696 return_status[1] = inb(0x3f5);
7697 return_status[2] = inb(0x3f5);
7698 return_status[3] = inb(0x3f5);
7699 return_status[4] = inb(0x3f5);
7700 return_status[5] = inb(0x3f5);
7701 return_status[6] = inb(0x3f5);
7702 // record in BIOS Data Area
7703 write_byte(0x0040, 0x0042, return_status[0]);
7704 write_byte(0x0040, 0x0043, return_status[1]);
7705 write_byte(0x0040, 0x0044, return_status[2]);
7706 write_byte(0x0040, 0x0045, return_status[3]);
7707 write_byte(0x0040, 0x0046, return_status[4]);
7708 write_byte(0x0040, 0x0047, return_status[5]);
7709 write_byte(0x0040, 0x0048, return_status[6]);
7710
7711 if ( (return_status[0] & 0xc0) != 0 ) {
7712 if ( (return_status[1] & 0x02) != 0 ) {
7713 // diskette not writable.
7714 // AH=status code=0x03 (tried to write on write-protected disk)
7715 // AL=number of sectors written=0
7716 AX = 0x0300;
7717 SET_CF();
7718 return;
7719 } else {
7720 BX_PANIC("int13_diskette_function: read error\n");
7721 }
7722 }
7723
7724 // ??? should track be new val from return_status[3] ?
7725 set_diskette_current_cyl(drive, track);
7726 // AL = number of sectors read (same value as passed)
7727 SET_AH(0x00); // success
7728 CLEAR_CF(); // success
7729 return;
7730 } else { // if (ah == 0x04)
7731 // Verify Diskette Sectors
7732
7733 // ??? should track be new val from return_status[3] ?
7734 set_diskette_current_cyl(drive, track);
7735 // AL = number of sectors verified (same value as passed)
7736 CLEAR_CF(); // success
7737 SET_AH(0x00); // success
7738 return;
7739 }
7740 break;
7741
7742 case 0x05: // format diskette track
7743 BX_DEBUG_INT13_FL("floppy f05\n");
7744
7745 num_sectors = GET_AL();
7746 track = GET_CH();
7747 head = GET_DH();
7748 drive = GET_ELDL();
7749
7750 if ((drive > 1) || (head > 1) || (track > 79) ||
7751 (num_sectors == 0) || (num_sectors > 18)) {
7752 SET_AH(1);
7753 set_diskette_ret_status(1);
7754 SET_CF(); // error occurred
7755 }
7756
7757 // see if drive exists
7758 if (floppy_drive_exists(drive) == 0) {
7759 SET_AH(0x80); // drive not responding
7760 set_diskette_ret_status(0x80);
7761 SET_CF(); // error occurred
7762 return;
7763 }
7764
7765 // see if media in drive, and type is known
7766 if (floppy_media_known(drive) == 0) {
7767 if (floppy_media_sense(drive) == 0) {
7768 SET_AH(0x0C); // Media type not found
7769 set_diskette_ret_status(0x0C);
7770 SET_AL(0); // no sectors read
7771 SET_CF(); // error occurred
7772 return;
7773 }
7774 }
7775
7776 // set up DMA controller for transfer
7777 page = (ES >> 12); // upper 4 bits
7778 base_es = (ES << 4); // lower 16bits contributed by ES
7779 base_address = base_es + BX; // lower 16 bits of address
7780 // contributed by ES:BX
7781 if ( base_address < base_es ) {
7782 // in case of carry, adjust page by 1
7783 page++;
7784 }
7785 base_count = (num_sectors * 4) - 1;
7786
7787 // check for 64K boundary overrun
7788 last_addr = base_address + base_count;
7789 if (last_addr < base_address) {
7790 SET_AH(0x09);
7791 set_diskette_ret_status(0x09);
7792 SET_AL(0); // no sectors read
7793 SET_CF(); // error occurred
7794 return;
7795 }
7796
7797 outb(0x000a, 0x06);
7798 outb(0x000c, 0x00); // clear flip-flop
7799 outb(0x0004, base_address);
7800 outb(0x0004, base_address>>8);
7801 outb(0x000c, 0x00); // clear flip-flop
7802 outb(0x0005, base_count);
7803 outb(0x0005, base_count>>8);
7804 mode_register = 0x4a; // single mode, increment, autoinit disable,
7805 // transfer type=read, channel 2
7806 outb(0x000b, mode_register);
7807 // port 81: DMA-1 Page Register, channel 2
7808 outb(0x0081, page);
7809 outb(0x000a, 0x02);
7810
7811 // set up floppy controller for transfer
7812 floppy_prepare_controller(drive);
7813
7814 // send format-track command (6 bytes) to controller
7815 outb(0x03f5, 0x4d); // 4d: format track
7816 outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
7817 outb(0x03f5, 2); // 512 byte sector size
7818 outb(0x03f5, num_sectors); // number of sectors per track
7819 outb(0x03f5, 0); // Gap length
7820 outb(0x03f5, 0xf6); // Fill byte
7821 // turn on interrupts
7822 ASM_START
7823 sti
7824 ASM_END
7825
7826 // wait on 40:3e bit 7 to become 1
7827 do {
7828 val8 = read_byte(0x0040, 0x0040);
7829 if (val8 == 0) {
7830 floppy_reset_controller();
7831 SET_AH(0x80); // drive not ready (timeout)
7832 set_diskette_ret_status(0x80);
7833 SET_CF(); // error occurred
7834 return;
7835 }
7836 val8 = (read_byte(0x0040, 0x003e) & 0x80);
7837 } while ( val8 == 0 );
7838
7839 val8 = 0; // separate asm from while() loop
7840 // turn off interrupts
7841 ASM_START
7842 cli
7843 ASM_END
7844 // set 40:3e bit 7 to 0
7845 val8 = read_byte(0x0040, 0x003e);
7846 val8 &= 0x7f;
7847 write_byte(0x0040, 0x003e, val8);
7848 // check port 3f4 for accessibility to status bytes
7849 val8 = inb(0x3f4);
7850 if ( (val8 & 0xc0) != 0xc0 )
7851 BX_PANIC("int13_diskette: ctrl not ready\n");
7852
7853 // read 7 return status bytes from controller
7854 // using loop index broken, have to unroll...
7855 return_status[0] = inb(0x3f5);
7856 return_status[1] = inb(0x3f5);
7857 return_status[2] = inb(0x3f5);
7858 return_status[3] = inb(0x3f5);
7859 return_status[4] = inb(0x3f5);
7860 return_status[5] = inb(0x3f5);
7861 return_status[6] = inb(0x3f5);
7862 // record in BIOS Data Area
7863 write_byte(0x0040, 0x0042, return_status[0]);
7864 write_byte(0x0040, 0x0043, return_status[1]);
7865 write_byte(0x0040, 0x0044, return_status[2]);
7866 write_byte(0x0040, 0x0045, return_status[3]);
7867 write_byte(0x0040, 0x0046, return_status[4]);
7868 write_byte(0x0040, 0x0047, return_status[5]);
7869 write_byte(0x0040, 0x0048, return_status[6]);
7870
7871 if ( (return_status[0] & 0xc0) != 0 ) {
7872 if ( (return_status[1] & 0x02) != 0 ) {
7873 // diskette not writable.
7874 // AH=status code=0x03 (tried to write on write-protected disk)
7875 // AL=number of sectors written=0
7876 AX = 0x0300;
7877 SET_CF();
7878 return;
7879 } else {
7880 BX_PANIC("int13_diskette_function: write error\n");
7881 }
7882 }
7883
7884 SET_AH(0);
7885 set_diskette_ret_status(0);
7886 set_diskette_current_cyl(drive, 0);
7887 CLEAR_CF(); // successful
7888 return;
7889
7890
7891 case 0x08: // read diskette drive parameters
7892 BX_DEBUG_INT13_FL("floppy f08\n");
7893 drive = GET_ELDL();
7894
7895 if (drive > 1) {
7896 AX = 0;
7897 BX = 0;
7898 CX = 0;
7899 DX = 0;
7900 ES = 0;
7901 DI = 0;
7902 SET_DL(num_floppies);
7903 SET_CF();
7904 return;
7905 }
7906
7907 drive_type = inb_cmos(0x10);
7908 num_floppies = 0;
7909 if (drive_type & 0xf0)
7910 num_floppies++;
7911 if (drive_type & 0x0f)
7912 num_floppies++;
7913
7914 if (drive == 0)
7915 drive_type >>= 4;
7916 else
7917 drive_type &= 0x0f;
7918
7919 SET_BH(0);
7920 SET_BL(drive_type);
7921 SET_AH(0);
7922 SET_AL(0);
7923 SET_DL(num_floppies);
7924
7925 switch (drive_type) {
7926 case 0: // none
7927 CX = 0;
7928 SET_DH(0); // max head #
7929 break;
7930
7931 case 1: // 360KB, 5.25"
7932 CX = 0x2709; // 40 tracks, 9 sectors
7933 SET_DH(1); // max head #
7934 break;
7935
7936 case 2: // 1.2MB, 5.25"
7937 CX = 0x4f0f; // 80 tracks, 15 sectors
7938 SET_DH(1); // max head #
7939 break;
7940
7941 case 3: // 720KB, 3.5"
7942 CX = 0x4f09; // 80 tracks, 9 sectors
7943 SET_DH(1); // max head #
7944 break;
7945
7946 case 4: // 1.44MB, 3.5"
7947 CX = 0x4f12; // 80 tracks, 18 sectors
7948 SET_DH(1); // max head #
7949 break;
7950
7951 case 5: // 2.88MB, 3.5"
7952 CX = 0x4f24; // 80 tracks, 36 sectors
7953 SET_DH(1); // max head #
7954 break;
7955
7956 case 6: // 160k, 5.25"
7957 CX = 0x2708; // 40 tracks, 8 sectors
7958 SET_DH(0); // max head #
7959 break;
7960
7961 case 7: // 180k, 5.25"
7962 CX = 0x2709; // 40 tracks, 9 sectors
7963 SET_DH(0); // max head #
7964 break;
7965
7966 case 8: // 320k, 5.25"
7967 CX = 0x2708; // 40 tracks, 8 sectors
7968 SET_DH(1); // max head #
7969 break;
7970
7971 default: // ?
7972 BX_PANIC("floppy: int13: bad floppy type\n");
7973 }
7974
7975 /* set es & di to point to 11 byte diskette param table in ROM */
7976 ASM_START
7977 push bp
7978 mov bp, sp
7979 mov ax, #diskette_param_table2
7980 mov _int13_diskette_function.DI+2[bp], ax
7981 mov _int13_diskette_function.ES+2[bp], cs
7982 pop bp
7983 ASM_END
7984 CLEAR_CF(); // success
7985 /* disk status not changed upon success */
7986 return;
7987
7988
7989 case 0x15: // read diskette drive type
7990 BX_DEBUG_INT13_FL("floppy f15\n");
7991 drive = GET_ELDL();
7992 if (drive > 1) {
7993 SET_AH(0); // only 2 drives supported
7994 // set_diskette_ret_status here ???
7995 SET_CF();
7996 return;
7997 }
7998 drive_type = inb_cmos(0x10);
7999
8000 if (drive == 0)
8001 drive_type >>= 4;
8002 else
8003 drive_type &= 0x0f;
8004 CLEAR_CF(); // successful, not present
8005 if (drive_type==0) {
8006 SET_AH(0); // drive not present
8007 }
8008 else {
8009 SET_AH(1); // drive present, does not support change line
8010 }
8011
8012 return;
8013
8014 case 0x16: // get diskette change line status
8015 BX_DEBUG_INT13_FL("floppy f16\n");
8016 drive = GET_ELDL();
8017 if (drive > 1) {
8018 SET_AH(0x01); // invalid drive
8019 set_diskette_ret_status(0x01);
8020 SET_CF();
8021 return;
8022 }
8023
8024 SET_AH(0x06); // change line not supported
8025 set_diskette_ret_status(0x06);
8026 SET_CF();
8027 return;
8028
8029 case 0x17: // set diskette type for format(old)
8030 BX_DEBUG_INT13_FL("floppy f17\n");
8031 /* not used for 1.44M floppies */
8032 SET_AH(0x01); // not supported
8033 set_diskette_ret_status(1); /* not supported */
8034 SET_CF();
8035 return;
8036
8037 case 0x18: // set diskette type for format(new)
8038 BX_DEBUG_INT13_FL("floppy f18\n");
8039 SET_AH(0x01); // do later
8040 set_diskette_ret_status(1);
8041 SET_CF();
8042 return;
8043
8044 default:
8045 BX_INFO("int13_diskette: unsupported AH=%02x\n", GET_AH());
8046
8047 // if ( (ah==0x20) || ((ah>=0x41) && (ah<=0x49)) || (ah==0x4e) ) {
8048 SET_AH(0x01); // ???
8049 set_diskette_ret_status(1);
8050 SET_CF();
8051 return;
8052 // }
8053 }
8054 }
8055 #else // #if BX_SUPPORT_FLOPPY
8056 void
8057 int13_diskette_function(DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS)
8058 Bit16u DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS;
8059 {
8060 Bit8u val8;
8061
8062 switch ( GET_AH() ) {
8063
8064 case 0x01: // Read Diskette Status
8065 CLEAR_CF();
8066 val8 = read_byte(0x0000, 0x0441);
8067 SET_AH(val8);
8068 if (val8) {
8069 SET_CF();
8070 }
8071 return;
8072
8073 default:
8074 SET_CF();
8075 write_byte(0x0000, 0x0441, 0x01);
8076 SET_AH(0x01);
8077 }
8078 }
8079 #endif // #if BX_SUPPORT_FLOPPY
8080
8081 void
8082 set_diskette_ret_status(value)
8083 Bit8u value;
8084 {
8085 write_byte(0x0040, 0x0041, value);
8086 }
8087
8088 void
8089 set_diskette_current_cyl(drive, cyl)
8090 Bit8u drive;
8091 Bit8u cyl;
8092 {
8093 if (drive > 1)
8094 BX_PANIC("set_diskette_current_cyl(): drive > 1\n");
8095 write_byte(0x0040, 0x0094+drive, cyl);
8096 }
8097
8098 void
8099 determine_floppy_media(drive)
8100 Bit16u drive;
8101 {
8102 #if 0
8103 Bit8u val8, DOR, ctrl_info;
8104
8105 ctrl_info = read_byte(0x0040, 0x008F);
8106 if (drive==1)
8107 ctrl_info >>= 4;
8108 else
8109 ctrl_info &= 0x0f;
8110
8111 #if 0
8112 if (drive == 0) {
8113 DOR = 0x1c; // DOR: drive0 motor on, DMA&int enabled, normal op, drive select 0
8114 }
8115 else {
8116 DOR = 0x2d; // DOR: drive1 motor on, DMA&int enabled, normal op, drive select 1
8117 }
8118 #endif
8119
8120 if ( (ctrl_info & 0x04) != 0x04 ) {
8121 // Drive not determined means no drive exists, done.
8122 return;
8123 }
8124
8125 #if 0
8126 // check Main Status Register for readiness
8127 val8 = inb(0x03f4) & 0x80; // Main Status Register
8128 if (val8 != 0x80)
8129 BX_PANIC("d_f_m: MRQ bit not set\n");
8130
8131 // change line
8132
8133 // existing BDA values
8134
8135 // turn on drive motor
8136 outb(0x03f2, DOR); // Digital Output Register
8137 //
8138 #endif
8139 BX_PANIC("d_f_m: OK so far\n");
8140 #endif
8141 }
8142
8143 void
8144 int17_function(regs, ds, iret_addr)
8145 pusha_regs_t regs; // regs pushed from PUSHA instruction
8146 Bit16u ds; // previous DS:, DS set to 0x0000 by asm wrapper
8147 iret_addr_t iret_addr; // CS,IP,Flags pushed from original INT call
8148 {
8149 Bit16u addr,timeout;
8150 Bit8u val8;
8151
8152 ASM_START
8153 sti
8154 ASM_END
8155
8156 addr = read_word(0x0040, (regs.u.r16.dx << 1) + 8);
8157 if ((regs.u.r8.ah < 3) && (regs.u.r16.dx < 3) && (addr > 0)) {
8158 timeout = read_byte(0x0040, 0x0078 + regs.u.r16.dx) << 8;
8159 if (regs.u.r8.ah == 0) {
8160 outb(addr, regs.u.r8.al);
8161 val8 = inb(addr+2);
8162 outb(addr+2, val8 | 0x01); // send strobe
8163 ASM_START
8164 nop
8165 ASM_END
8166 outb(addr+2, val8 & ~0x01);
8167 while (((inb(addr+1) & 0x40) == 0x40) && (timeout)) {
8168 timeout--;
8169 }
8170 }
8171 if (regs.u.r8.ah == 1) {
8172 val8 = inb(addr+2);
8173 outb(addr+2, val8 & ~0x04); // send init
8174 ASM_START
8175 nop
8176 ASM_END
8177 outb(addr+2, val8 | 0x04);
8178 }
8179 val8 = inb(addr+1);
8180 regs.u.r8.ah = (val8 ^ 0x48);
8181 if (!timeout) regs.u.r8.ah |= 0x01;
8182 ClearCF(iret_addr.flags);
8183 } else {
8184 SetCF(iret_addr.flags); // Unsupported
8185 }
8186 }
8187
8188 void
8189 int18_function(seq_nr)
8190 Bit16u seq_nr;
8191 {
8192 Bit16u ebda_seg=read_word(0x0040,0x000E);
8193 Bit16u bootdev;
8194 Bit8u bootdrv;
8195 Bit8u bootchk;
8196 Bit16u bootseg;
8197 Bit16u bootip;
8198 Bit16u status;
8199 Bit16u bootfirst;
8200
8201 ipl_entry_t e;
8202
8203 // if BX_ELTORITO_BOOT is not defined, old behavior
8204 // check bit 5 in CMOS reg 0x2d. load either 0x00 or 0x80 into DL
8205 // in preparation for the intial INT 13h (0=floppy A:, 0x80=C:)
8206 // 0: system boot sequence, first drive C: then A:
8207 // 1: system boot sequence, first drive A: then C:
8208 // else BX_ELTORITO_BOOT is defined
8209 // CMOS regs 0x3D and 0x38 contain the boot sequence:
8210 // CMOS reg 0x3D & 0x0f : 1st boot device
8211 // CMOS reg 0x3D & 0xf0 : 2nd boot device
8212 // CMOS reg 0x38 & 0xf0 : 3rd boot device
8213 // boot device codes:
8214 // 0x00 : not defined
8215 // 0x01 : first floppy
8216 // 0x02 : first harddrive
8217 // 0x03 : first cdrom
8218 // 0x04 - 0x0f : PnP expansion ROMs (e.g. Etherboot)
8219 // else : boot failure
8220
8221 // Get the boot sequence
8222 #if BX_ELTORITO_BOOT
8223 bootdev = inb_cmos(0x3d);
8224 bootdev |= ((inb_cmos(0x38) & 0xf0) << 4);
8225 bootdev >>= 4 * seq_nr;
8226 bootdev &= 0xf;
8227
8228 /* Read user selected device */
8229 bootfirst = read_word(ebda_seg, IPL_BOOTFIRST_OFFSET);
8230 if (bootfirst != 0xFFFF) {
8231 bootdev = bootfirst;
8232 /* User selected device not set */
8233 write_word(ebda_seg, IPL_BOOTFIRST_OFFSET, 0xFFFF);
8234 /* Reset boot sequence */
8235 write_word(ebda_seg, IPL_SEQUENCE_OFFSET, 0xFFFF);
8236 } else if (bootdev == 0) {
8237 printf("\nNo bootable device.\n");
8238 printf("Powering off in 30 seconds.\n");
8239 ASM_START
8240 sti
8241 mov cx, #0x01c9
8242 mov dx, #0xc380
8243 mov ah, #0x86 ;; INT 15/86: wait CX:DX usec.
8244 int #0x15
8245 ASM_END
8246 bios_printf(BIOS_PRINTF_HALT, "");
8247 }
8248
8249 /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
8250 bootdev -= 1;
8251 #else
8252 if (seq_nr ==2) BX_PANIC("No more boot devices.");
8253 if (!!(inb_cmos(0x2d) & 0x20) ^ (seq_nr == 1))
8254 /* Boot from floppy if the bit is set or it's the second boot */
8255 bootdev = 0x00;
8256 else
8257 bootdev = 0x01;
8258 #endif
8259
8260 /* Read the boot device from the IPL table */
8261 if (get_boot_vector(bootdev, &e) == 0) {
8262 BX_INFO("Invalid boot device (0x%x)\n", bootdev);
8263 return;
8264 }
8265
8266 /* Do the loading, and set up vector as a far pointer to the boot
8267 * address, and bootdrv as the boot drive */
8268 print_boot_device(e.type, e.description);
8269
8270 switch(e.type) {
8271 case IPL_TYPE_FLOPPY: /* FDD */
8272 case IPL_TYPE_HARDDISK: /* HDD */
8273
8274 bootdrv = (e.type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
8275 bootseg = 0x07c0;
8276 status = 0;
8277
8278 ASM_START
8279 push bp
8280 mov bp, sp
8281 push ax
8282 push bx
8283 push cx
8284 push dx
8285
8286 mov dl, _int18_function.bootdrv + 2[bp]
8287 mov ax, _int18_function.bootseg + 2[bp]
8288 mov es, ax ;; segment
8289 xor bx, bx ;; offset
8290 mov ah, #0x02 ;; function 2, read diskette sector
8291 mov al, #0x01 ;; read 1 sector
8292 mov ch, #0x00 ;; track 0
8293 mov cl, #0x01 ;; sector 1
8294 mov dh, #0x00 ;; head 0
8295 int #0x13 ;; read sector
8296 jnc int19_load_done
8297 mov ax, #0x0001
8298 mov _int18_function.status + 2[bp], ax
8299
8300 int19_load_done:
8301 pop dx
8302 pop cx
8303 pop bx
8304 pop ax
8305 pop bp
8306 ASM_END
8307
8308 if (status != 0) {
8309 print_boot_failure(e.type, 1);
8310 return;
8311 }
8312
8313 /* Always check the signature on a HDD boot sector; on FDD, only do
8314 * the check if the CMOS doesn't tell us to skip it */
8315 if ((e.type != IPL_TYPE_FLOPPY) || !((inb_cmos(0x38) & 0x01))) {
8316 if (read_word(bootseg,0x1fe) != 0xaa55) {
8317 print_boot_failure(e.type, 0);
8318 return;
8319 }
8320 }
8321
8322 #if BX_TCGBIOS
8323 tcpa_add_bootdevice((Bit32u)0L, (Bit32u)bootdrv);
8324 tcpa_ipl((Bit32u)0L,(Bit32u)bootseg,(Bit32u)0L,(Bit32u)512L); /* specs: 8.2.3 steps 4 and 5 */
8325 #endif
8326
8327 /* Canonicalize bootseg:bootip */
8328 bootip = (bootseg & 0x0fff) << 4;
8329 bootseg &= 0xf000;
8330 break;
8331
8332 #if BX_ELTORITO_BOOT
8333 case IPL_TYPE_CDROM: /* CD-ROM */
8334 status = cdrom_boot();
8335
8336 // If failure
8337 if ( (status & 0x00ff) !=0 ) {
8338 print_cdromboot_failure(status);
8339 print_boot_failure(e.type, 1);
8340 return;
8341 }
8342
8343 bootdrv = (Bit8u)(status>>8);
8344 bootseg = read_word(ebda_seg,&EbdaData->cdemu.load_segment);
8345
8346 /* Canonicalize bootseg:bootip */
8347 bootip = (bootseg & 0x0fff) << 4;
8348 bootseg &= 0xf000;
8349 break;
8350 #endif
8351
8352 case IPL_TYPE_BEV: /* Expansion ROM with a Bootstrap Entry Vector (a far pointer) */
8353 bootseg = e.vector >> 16;
8354 bootip = e.vector & 0xffff;
8355 break;
8356
8357 default: return;
8358 }
8359
8360 /* Debugging info */
8361 BX_INFO("Booting from %x:%x\n", bootseg, bootip);
8362
8363 /* Jump to the boot vector */
8364 ASM_START
8365 mov bp, sp
8366 push cs
8367 push #int18_handler
8368 ;; Build an iret stack frame that will take us to the boot vector.
8369 ;; iret pops ip, then cs, then flags, so push them in the opposite order.
8370 pushf
8371 mov ax, _int18_function.bootseg + 0[bp]
8372 push ax
8373 mov ax, _int18_function.bootip + 0[bp]
8374 push ax
8375 ;; Set the magic number in ax and the boot drive in dl.
8376 mov ax, #0xaa55
8377 mov dl, _int18_function.bootdrv + 0[bp]
8378 ;; Zero some of the other registers.
8379 xor bx, bx
8380 mov ds, bx
8381 mov es, bx
8382 mov bp, bx
8383 ;; Go!
8384 iret
8385 ASM_END
8386 }
8387
8388 void
8389 int1a_function(regs, ds, iret_addr)
8390 pusha_regs_t regs; // regs pushed from PUSHA instruction
8391 Bit16u ds; // previous DS:, DS set to 0x0000 by asm wrapper
8392 iret_addr_t iret_addr; // CS,IP,Flags pushed from original INT call
8393 {
8394 Bit8u val8;
8395
8396 BX_DEBUG_INT1A("int1a: AX=%04x BX=%04x CX=%04x DX=%04x DS=%04x\n", regs.u.r16.ax, regs.u.r16.bx, regs.u.r16.cx, regs.u.r16.dx, ds);
8397
8398 ASM_START
8399 sti
8400 ASM_END
8401
8402 switch (regs.u.r8.ah) {
8403 case 0: // get current clock count
8404 ASM_START
8405 cli
8406 ASM_END
8407 regs.u.r16.cx = BiosData->ticks_high;
8408 regs.u.r16.dx = BiosData->ticks_low;
8409 regs.u.r8.al = BiosData->midnight_flag;
8410 BiosData->midnight_flag = 0; // reset flag
8411 ASM_START
8412 sti
8413 ASM_END
8414 // AH already 0
8415 ClearCF(iret_addr.flags); // OK
8416 break;
8417
8418 case 1: // Set Current Clock Count
8419 ASM_START
8420 cli
8421 ASM_END
8422 BiosData->ticks_high = regs.u.r16.cx;
8423 BiosData->ticks_low = regs.u.r16.dx;
8424 BiosData->midnight_flag = 0; // reset flag
8425 ASM_START
8426 sti
8427 ASM_END
8428 regs.u.r8.ah = 0;
8429 ClearCF(iret_addr.flags); // OK
8430 break;
8431
8432
8433 case 2: // Read CMOS Time
8434 if (rtc_updating()) {
8435 SetCF(iret_addr.flags);
8436 break;
8437 }
8438
8439 regs.u.r8.dh = inb_cmos(0x00); // Seconds
8440 regs.u.r8.cl = inb_cmos(0x02); // Minutes
8441 regs.u.r8.ch = inb_cmos(0x04); // Hours
8442 regs.u.r8.dl = inb_cmos(0x0b) & 0x01; // Stat Reg B
8443 regs.u.r8.ah = 0;
8444 regs.u.r8.al = regs.u.r8.ch;
8445 ClearCF(iret_addr.flags); // OK
8446 break;
8447
8448 case 3: // Set CMOS Time
8449 // Using a debugger, I notice the following masking/setting
8450 // of bits in Status Register B, by setting Reg B to
8451 // a few values and getting its value after INT 1A was called.
8452 //
8453 // try#1 try#2 try#3
8454 // before 1111 1101 0111 1101 0000 0000
8455 // after 0110 0010 0110 0010 0000 0010
8456 //
8457 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8458 // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
8459 if (rtc_updating()) {
8460 init_rtc();
8461 // fall through as if an update were not in progress
8462 }
8463 outb_cmos(0x00, regs.u.r8.dh); // Seconds
8464 outb_cmos(0x02, regs.u.r8.cl); // Minutes
8465 outb_cmos(0x04, regs.u.r8.ch); // Hours
8466 // Set Daylight Savings time enabled bit to requested value
8467 val8 = (inb_cmos(0x0b) & 0x60) | 0x02 | (regs.u.r8.dl & 0x01);
8468 // (reg B already selected)
8469 outb_cmos(0x0b, val8);
8470 regs.u.r8.ah = 0;
8471 regs.u.r8.al = val8; // val last written to Reg B
8472 ClearCF(iret_addr.flags); // OK
8473 break;
8474
8475 case 4: // Read CMOS Date
8476 regs.u.r8.ah = 0;
8477 if (rtc_updating()) {
8478 SetCF(iret_addr.flags);
8479 break;
8480 }
8481 regs.u.r8.cl = inb_cmos(0x09); // Year
8482 regs.u.r8.dh = inb_cmos(0x08); // Month
8483 regs.u.r8.dl = inb_cmos(0x07); // Day of Month
8484 regs.u.r8.ch = inb_cmos(0x32); // Century
8485 regs.u.r8.al = regs.u.r8.ch;
8486 ClearCF(iret_addr.flags); // OK
8487 break;
8488
8489 case 5: // Set CMOS Date
8490 // Using a debugger, I notice the following masking/setting
8491 // of bits in Status Register B, by setting Reg B to
8492 // a few values and getting its value after INT 1A was called.
8493 //
8494 // try#1 try#2 try#3 try#4
8495 // before 1111 1101 0111 1101 0000 0010 0000 0000
8496 // after 0110 1101 0111 1101 0000 0010 0000 0000
8497 //
8498 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8499 // My assumption: RegB = (RegB & 01111111b)
8500 if (rtc_updating()) {
8501 init_rtc();
8502 SetCF(iret_addr.flags);
8503 break;
8504 }
8505 outb_cmos(0x09, regs.u.r8.cl); // Year
8506 outb_cmos(0x08, regs.u.r8.dh); // Month
8507 outb_cmos(0x07, regs.u.r8.dl); // Day of Month
8508 outb_cmos(0x32, regs.u.r8.ch); // Century
8509 val8 = inb_cmos(0x0b) & 0x7f; // clear halt-clock bit
8510 outb_cmos(0x0b, val8);
8511 regs.u.r8.ah = 0;
8512 regs.u.r8.al = val8; // AL = val last written to Reg B
8513 ClearCF(iret_addr.flags); // OK
8514 break;
8515
8516 case 6: // Set Alarm Time in CMOS
8517 // Using a debugger, I notice the following masking/setting
8518 // of bits in Status Register B, by setting Reg B to
8519 // a few values and getting its value after INT 1A was called.
8520 //
8521 // try#1 try#2 try#3
8522 // before 1101 1111 0101 1111 0000 0000
8523 // after 0110 1111 0111 1111 0010 0000
8524 //
8525 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8526 // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
8527 val8 = inb_cmos(0x0b); // Get Status Reg B
8528 regs.u.r16.ax = 0;
8529 if (val8 & 0x20) {
8530 // Alarm interrupt enabled already
8531 SetCF(iret_addr.flags); // Error: alarm in use
8532 break;
8533 }
8534 if (rtc_updating()) {
8535 init_rtc();
8536 // fall through as if an update were not in progress
8537 }
8538 outb_cmos(0x01, regs.u.r8.dh); // Seconds alarm
8539 outb_cmos(0x03, regs.u.r8.cl); // Minutes alarm
8540 outb_cmos(0x05, regs.u.r8.ch); // Hours alarm
8541 outb(0xa1, inb(0xa1) & 0xfe); // enable IRQ 8
8542 // enable Status Reg B alarm bit, clear halt clock bit
8543 outb_cmos(0x0b, (val8 & 0x7f) | 0x20);
8544 ClearCF(iret_addr.flags); // OK
8545 break;
8546
8547 case 7: // Turn off Alarm
8548 // Using a debugger, I notice the following masking/setting
8549 // of bits in Status Register B, by setting Reg B to
8550 // a few values and getting its value after INT 1A was called.
8551 //
8552 // try#1 try#2 try#3 try#4
8553 // before 1111 1101 0111 1101 0010 0000 0010 0010
8554 // after 0100 0101 0101 0101 0000 0000 0000 0010
8555 //
8556 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8557 // My assumption: RegB = (RegB & 01010111b)
8558 val8 = inb_cmos(0x0b); // Get Status Reg B
8559 // clear clock-halt bit, disable alarm bit
8560 outb_cmos(0x0b, val8 & 0x57); // disable alarm bit
8561 regs.u.r8.ah = 0;
8562 regs.u.r8.al = val8; // val last written to Reg B
8563 ClearCF(iret_addr.flags); // OK
8564 break;
8565 #if BX_PCIBIOS
8566 case 0xb1:
8567 // real mode PCI BIOS functions now handled in assembler code
8568 // this C code handles the error code for information only
8569 if (regs.u.r8.bl == 0xff) {
8570 BX_INFO("PCI BIOS: PCI not present\n");
8571 } else if (regs.u.r8.bl == 0x81) {
8572 BX_INFO("unsupported PCI BIOS function 0x%02x\n", regs.u.r8.al);
8573 } else if (regs.u.r8.bl == 0x83) {
8574 BX_INFO("bad PCI vendor ID %04x\n", regs.u.r16.dx);
8575 } else if (regs.u.r8.bl == 0x86) {
8576 if (regs.u.r8.al == 0x02) {
8577 BX_INFO("PCI device %04x:%04x not found at index %d\n", regs.u.r16.dx, regs.u.r16.cx, regs.u.r16.si);
8578 } else {
8579 BX_INFO("no PCI device with class code 0x%02x%04x found at index %d\n", regs.u.r8.cl, regs.u.r16.dx, regs.u.r16.si);
8580 }
8581 }
8582 regs.u.r8.ah = regs.u.r8.bl;
8583 SetCF(iret_addr.flags);
8584 break;
8585 #endif
8586
8587 default:
8588 SetCF(iret_addr.flags); // Unsupported
8589 }
8590 }
8591
8592 void
8593 int70_function(regs, ds, iret_addr)
8594 pusha_regs_t regs; // regs pushed from PUSHA instruction
8595 Bit16u ds; // previous DS:, DS set to 0x0000 by asm wrapper
8596 iret_addr_t iret_addr; // CS,IP,Flags pushed from original INT call
8597 {
8598 // INT 70h: IRQ 8 - CMOS RTC interrupt from periodic or alarm modes
8599 Bit8u registerB = 0, registerC = 0;
8600
8601 // Check which modes are enabled and have occurred.
8602 registerB = inb_cmos( 0xB );
8603 registerC = inb_cmos( 0xC );
8604
8605 if( ( registerB & 0x60 ) != 0 ) {
8606 if( ( registerC & 0x20 ) != 0 ) {
8607 // Handle Alarm Interrupt.
8608 ASM_START
8609 sti
8610 int #0x4a
8611 cli
8612 ASM_END
8613 }
8614 if( ( registerC & 0x40 ) != 0 ) {
8615 // Handle Periodic Interrupt.
8616
8617 if( read_byte( 0x40, 0xA0 ) != 0 ) {
8618 // Wait Interval (Int 15, AH=83) active.
8619 Bit32u time, toggle;
8620
8621 time = read_dword( 0x40, 0x9C ); // Time left in microseconds.
8622 if( time < 0x3D1 ) {
8623 // Done waiting.
8624 Bit16u segment, offset;
8625
8626 segment = read_word( 0x40, 0x98 );
8627 offset = read_word( 0x40, 0x9A );
8628 write_byte( 0x40, 0xA0, 0 ); // Turn of status byte.
8629 outb_cmos( 0xB, registerB & 0x37 ); // Clear the Periodic Interrupt.
8630 write_byte(segment, offset, read_byte(segment, offset) | 0x80 ); // Write to specified flag byte.
8631 } else {
8632 // Continue waiting.
8633 time -= 0x3D1;
8634 write_dword( 0x40, 0x9C, time );
8635 }
8636 }
8637 }
8638 }
8639
8640 ASM_START
8641 call eoi_both_pics
8642 ASM_END
8643 }
8644
8645
8646 ASM_START
8647 ;------------------------------------------
8648 ;- INT74h : PS/2 mouse hardware interrupt -
8649 ;------------------------------------------
8650 int74_handler:
8651 sti
8652 pusha
8653 push ds ;; save DS
8654 push #0x00 ;; placeholder for status
8655 push #0x00 ;; placeholder for X
8656 push #0x00 ;; placeholder for Y
8657 push #0x00 ;; placeholder for Z
8658 push #0x00 ;; placeholder for make_far_call boolean
8659 call _int74_function
8660 pop cx ;; remove make_far_call from stack
8661 jcxz int74_done
8662
8663 ;; make far call to EBDA:0022
8664 push #0x00
8665 pop ds
8666 push 0x040E ;; push 0000:040E (opcodes 0xff, 0x36, 0x0E, 0x04)
8667 pop ds
8668 //CALL_EP(0x0022) ;; call far routine (call_Ep DS:0022 :opcodes 0xff, 0x1e, 0x22, 0x00)
8669 call far ptr[0x22]
8670 int74_done:
8671 cli
8672 call eoi_both_pics
8673 add sp, #8 ;; pop status, x, y, z
8674
8675 pop ds ;; restore DS
8676 popa
8677 iret
8678
8679
8680 ;; This will perform an IRET, but will retain value of current CF
8681 ;; by altering flags on stack. Better than RETF #02.
8682 iret_modify_cf:
8683 jc carry_set
8684 push bp
8685 mov bp, sp
8686 and BYTE [bp + 0x06], #0xfe
8687 pop bp
8688 iret
8689 carry_set:
8690 push bp
8691 mov bp, sp
8692 or BYTE [bp + 0x06], #0x01
8693 pop bp
8694 iret
8695
8696
8697 ;----------------------
8698 ;- INT13h (relocated) -
8699 ;----------------------
8700 ;
8701 ; int13_relocated is a little bit messed up since I played with it
8702 ; I have to rewrite it:
8703 ; - call a function that detect which function to call
8704 ; - make all called C function get the same parameters list
8705 ;
8706 int13_relocated:
8707
8708 #if BX_ELTORITO_BOOT
8709 ;; check for an eltorito function
8710 cmp ah,#0x4a
8711 jb int13_not_eltorito
8712 cmp ah,#0x4d
8713 ja int13_not_eltorito
8714
8715 pusha
8716 push es
8717 push ds
8718 push ss
8719 pop ds
8720
8721 push #int13_out
8722 jmp _int13_eltorito ;; ELDX not used
8723
8724 int13_not_eltorito:
8725 push ax
8726 push bx
8727 push cx
8728 push dx
8729
8730 ;; check if emulation active
8731 call _cdemu_isactive
8732 cmp al,#0x00
8733 je int13_cdemu_inactive
8734
8735 ;; check if access to the emulated drive
8736 call _cdemu_emulated_drive
8737 pop dx
8738 push dx
8739 cmp al,dl ;; int13 on emulated drive
8740 jne int13_nocdemu
8741
8742 pop dx
8743 pop cx
8744 pop bx
8745 pop ax
8746
8747 pusha
8748 push es
8749 push ds
8750 push ss
8751 pop ds
8752
8753 push #int13_out
8754 jmp _int13_cdemu ;; ELDX not used
8755
8756 int13_nocdemu:
8757 and dl,#0xE0 ;; mask to get device class, including cdroms
8758 cmp al,dl ;; al is 0x00 or 0x80
8759 jne int13_cdemu_inactive ;; inactive for device class
8760
8761 pop dx
8762 pop cx
8763 pop bx
8764 pop ax
8765
8766 push ax
8767 push cx
8768 push dx
8769 push bx
8770
8771 dec dl ;; real drive is dl - 1
8772 jmp int13_legacy
8773
8774 int13_cdemu_inactive:
8775 pop dx
8776 pop cx
8777 pop bx
8778 pop ax
8779
8780 #endif // BX_ELTORITO_BOOT
8781
8782 int13_noeltorito:
8783
8784 push ax
8785 push cx
8786 push dx
8787 push bx
8788
8789 int13_legacy:
8790
8791 push dx ;; push eltorito value of dx instead of sp
8792
8793 push bp
8794 push si
8795 push di
8796
8797 push es
8798 push ds
8799 push ss
8800 pop ds
8801
8802 ;; now the 16-bit registers can be restored with:
8803 ;; pop ds; pop es; popa; iret
8804 ;; arguments passed to functions should be
8805 ;; DS, ES, DI, SI, BP, ELDX, BX, DX, CX, AX, IP, CS, FLAGS
8806
8807 test dl, #0x80
8808 jnz int13_notfloppy
8809
8810 push #int13_out
8811 jmp _int13_diskette_function
8812
8813 int13_notfloppy:
8814
8815 #if BX_USE_ATADRV
8816
8817 cmp dl, #0xE0
8818 jb int13_notcdrom
8819
8820 // ebx is modified: BSD 5.2.1 boot loader problem
8821 // someone should figure out which 32 bit register that actually are used
8822
8823 shr ebx, #16
8824 push bx
8825
8826 call _int13_cdrom
8827
8828 pop bx
8829 shl ebx, #16
8830
8831 jmp int13_out
8832
8833 int13_notcdrom:
8834
8835 #endif
8836
8837 int13_disk:
8838 ;; int13_harddisk modifies high word of EAX
8839 shr eax, #16
8840 push ax
8841 call _int13_harddisk
8842 pop ax
8843 shl eax, #16
8844
8845 int13_out:
8846 pop ds
8847 pop es
8848 popa
8849 iret
8850
8851 ;----------
8852 ;- INT18h -
8853 ;----------
8854 int18_handler: ;; Boot Failure recovery: try the next device.
8855
8856 ;; Reset SP and SS
8857 mov ax, #0x0ffe
8858 mov sp, ax
8859 mov ax, #0x9e00
8860 mov ss, ax
8861
8862 ;; The first time we do this it will have been set to -1 so
8863 ;; we will start from device 0.
8864 xor ax, ax
8865 mov ds, ax
8866 mov bx, word ptr [0x40E] ;; EBDA segment
8867 mov ds, bx ;; Set segment
8868 mov bx, IPL_SEQUENCE_OFFSET ;; BX is now the sequence number
8869 inc bx ;; ++
8870 mov IPL_SEQUENCE_OFFSET, bx ;; Write it back
8871 mov ds, ax ;; and reset the segment to zero.
8872
8873 ;; Call the C code for the next boot device
8874 push bx
8875
8876 call _int18_function
8877
8878 ;; Boot failed: invoke the boot recovery function...
8879 int #0x18
8880
8881 ;----------
8882 ;- INT19h -
8883 ;----------
8884 int19_relocated: ;; Boot function, relocated
8885
8886 ;;
8887 ;; *** Warning: INT 19h resets the whole machine ***
8888 ;;
8889 ;; Because PV drivers in HVM guests detach some of the emulated devices,
8890 ;; it is not safe to do a soft reboot by just dropping to real mode and
8891 ;; invoking INT 19h -- the boot drives might have disappeared!
8892 ;; If the user asks for a soft reboot, the only thing we can do is
8893 ;; reset the whole machine. When it comes back up, the normal BIOS
8894 ;; boot sequence will start, which is more or less the required behaviour.
8895 ;;
8896 ;; Reset SP and SS
8897
8898 mov ax, #0x0ffe
8899 mov sp, ax
8900 mov ax, #0x9e00
8901 mov ss, ax
8902
8903 call _machine_reset
8904
8905 ;----------
8906 ;- INT1Ch -
8907 ;----------
8908 int1c_handler: ;; User Timer Tick
8909 iret
8910
8911
8912 ;----------------------
8913 ;- POST: Floppy Drive -
8914 ;----------------------
8915 floppy_drive_post:
8916 xor ax, ax
8917 mov ds, ax
8918
8919 mov al, #0x00
8920 mov 0x043e, al ;; drive 0 & 1 uncalibrated, no interrupt has occurred
8921
8922 mov 0x043f, al ;; diskette motor status: read op, drive0, motors off
8923
8924 mov 0x0440, al ;; diskette motor timeout counter: not active
8925 mov 0x0441, al ;; diskette controller status return code
8926
8927 mov 0x0442, al ;; disk & diskette controller status register 0
8928 mov 0x0443, al ;; diskette controller status register 1
8929 mov 0x0444, al ;; diskette controller status register 2
8930 mov 0x0445, al ;; diskette controller cylinder number
8931 mov 0x0446, al ;; diskette controller head number
8932 mov 0x0447, al ;; diskette controller sector number
8933 mov 0x0448, al ;; diskette controller bytes written
8934
8935 mov 0x048b, al ;; diskette configuration data
8936
8937 ;; -----------------------------------------------------------------
8938 ;; (048F) diskette controller information
8939 ;;
8940 mov al, #0x10 ;; get CMOS diskette drive type
8941 out 0x70, AL
8942 in AL, 0x71
8943 mov ah, al ;; save byte to AH
8944
8945 look_drive0:
8946 shr al, #4 ;; look at top 4 bits for drive 0
8947 jz f0_missing ;; jump if no drive0
8948 mov bl, #0x07 ;; drive0 determined, multi-rate, has changed line
8949 jmp look_drive1
8950 f0_missing:
8951 mov bl, #0x00 ;; no drive0
8952
8953 look_drive1:
8954 mov al, ah ;; restore from AH
8955 and al, #0x0f ;; look at bottom 4 bits for drive 1
8956 jz f1_missing ;; jump if no drive1
8957 or bl, #0x70 ;; drive1 determined, multi-rate, has changed line
8958 f1_missing:
8959 ;; leave high bits in BL zerod
8960 mov 0x048f, bl ;; put new val in BDA (diskette controller information)
8961 ;; -----------------------------------------------------------------
8962
8963 mov al, #0x00
8964 mov 0x0490, al ;; diskette 0 media state
8965 mov 0x0491, al ;; diskette 1 media state
8966
8967 ;; diskette 0,1 operational starting state
8968 ;; drive type has not been determined,
8969 ;; has no changed detection line
8970 mov 0x0492, al
8971 mov 0x0493, al
8972
8973 mov 0x0494, al ;; diskette 0 current cylinder
8974 mov 0x0495, al ;; diskette 1 current cylinder
8975
8976 mov al, #0x02
8977 out #0x0a, al ;; clear DMA-1 channel 2 mask bit
8978
8979 SET_INT_VECTOR(0x1E, #0xF000, #diskette_param_table2)
8980 SET_INT_VECTOR(0x40, #0xF000, #int13_diskette)
8981 SET_INT_VECTOR(0x0E, #0xF000, #int0e_handler) ;; IRQ 6
8982
8983 ret
8984
8985
8986 ;--------------------
8987 ;- POST: HARD DRIVE -
8988 ;--------------------
8989 ; relocated here because the primary POST area isnt big enough.
8990 hard_drive_post:
8991 // IRQ 14 = INT 76h
8992 // INT 76h calls INT 15h function ax=9100
8993
8994 mov al, #0x0a ; 0000 1010 = reserved, disable IRQ 14
8995 mov dx, #0x03f6
8996 out dx, al
8997
8998 xor ax, ax
8999 mov ds, ax
9000 mov 0x0474, al /* hard disk status of last operation */
9001 mov 0x0477, al /* hard disk port offset (XT only ???) */
9002 mov 0x048c, al /* hard disk status register */
9003 mov 0x048d, al /* hard disk error register */
9004 mov 0x048e, al /* hard disk task complete flag */
9005 mov al, #0x01
9006 mov 0x0475, al /* hard disk number attached */
9007 mov al, #0xc0
9008 mov 0x0476, al /* hard disk control byte */
9009 SET_INT_VECTOR(0x13, #0xF000, #int13_handler)
9010 SET_INT_VECTOR(0x76, #0xF000, #int76_handler)
9011 ;; INT 41h: hard disk 0 configuration pointer
9012 ;; INT 46h: hard disk 1 configuration pointer
9013 SET_INT_VECTOR(0x41, word ptr [0x40E], #0x003D) /* EBDA:003D */
9014 SET_INT_VECTOR(0x46, word ptr [0x40E], #0x004D) /* EBDA:004D */
9015
9016 ;; move disk geometry data from CMOS to EBDA disk parameter table(s)
9017 mov al, #0x12
9018 out #0x70, al
9019 in al, #0x71
9020 and al, #0xf0
9021 cmp al, #0xf0
9022 je post_d0_extended
9023 jmp check_for_hd1
9024 post_d0_extended:
9025 mov al, #0x19
9026 out #0x70, al
9027 in al, #0x71
9028 cmp al, #47 ;; decimal 47 - user definable
9029 je post_d0_type47
9030 HALT(__LINE__)
9031 post_d0_type47:
9032 ;; CMOS purpose param table offset
9033 ;; 1b cylinders low 0
9034 ;; 1c cylinders high 1
9035 ;; 1d heads 2
9036 ;; 1e write pre-comp low 5
9037 ;; 1f write pre-comp high 6
9038 ;; 20 retries/bad map/heads>8 8
9039 ;; 21 landing zone low C
9040 ;; 22 landing zone high D
9041 ;; 23 sectors/track E
9042
9043 xor ax, ax
9044 mov ds, ax
9045 mov ax, word ptr [0x40E] ;; EBDA segment
9046 mov ds, ax
9047
9048 ;;; Filling EBDA table for hard disk 0.
9049 mov al, #0x1f
9050 out #0x70, al
9051 in al, #0x71
9052 mov ah, al
9053 mov al, #0x1e
9054 out #0x70, al
9055 in al, #0x71
9056 mov (0x003d + 0x05), ax ;; write precomp word
9057
9058 mov al, #0x20
9059 out #0x70, al
9060 in al, #0x71
9061 mov (0x003d + 0x08), al ;; drive control byte
9062
9063 mov al, #0x22
9064 out #0x70, al
9065 in al, #0x71
9066 mov ah, al
9067 mov al, #0x21
9068 out #0x70, al
9069 in al, #0x71
9070 mov (0x003d + 0x0C), ax ;; landing zone word
9071
9072 mov al, #0x1c ;; get cylinders word in AX
9073 out #0x70, al
9074 in al, #0x71 ;; high byte
9075 mov ah, al
9076 mov al, #0x1b
9077 out #0x70, al
9078 in al, #0x71 ;; low byte
9079 mov bx, ax ;; BX = cylinders
9080
9081 mov al, #0x1d
9082 out #0x70, al
9083 in al, #0x71
9084 mov cl, al ;; CL = heads
9085
9086 mov al, #0x23
9087 out #0x70, al
9088 in al, #0x71
9089 mov dl, al ;; DL = sectors
9090
9091 cmp bx, #1024
9092 jnbe hd0_post_logical_chs ;; if cylinders > 1024, use translated style CHS
9093
9094 hd0_post_physical_chs:
9095 ;; no logical CHS mapping used, just physical CHS
9096 ;; use Standard Fixed Disk Parameter Table (FDPT)
9097 mov (0x003d + 0x00), bx ;; number of physical cylinders
9098 mov (0x003d + 0x02), cl ;; number of physical heads
9099 mov (0x003d + 0x0E), dl ;; number of physical sectors
9100 jmp check_for_hd1
9101
9102 hd0_post_logical_chs:
9103 ;; complies with Phoenix style Translated Fixed Disk Parameter Table (FDPT)
9104 mov (0x003d + 0x09), bx ;; number of physical cylinders
9105 mov (0x003d + 0x0b), cl ;; number of physical heads
9106 mov (0x003d + 0x04), dl ;; number of physical sectors
9107 mov (0x003d + 0x0e), dl ;; number of logical sectors (same)
9108 mov al, #0xa0
9109 mov (0x003d + 0x03), al ;; A0h signature, indicates translated table
9110
9111 cmp bx, #2048
9112 jnbe hd0_post_above_2048
9113 ;; 1024 < c <= 2048 cylinders
9114 shr bx, #0x01
9115 shl cl, #0x01
9116 jmp hd0_post_store_logical
9117
9118 hd0_post_above_2048:
9119 cmp bx, #4096
9120 jnbe hd0_post_above_4096
9121 ;; 2048 < c <= 4096 cylinders
9122 shr bx, #0x02
9123 shl cl, #0x02
9124 jmp hd0_post_store_logical
9125
9126 hd0_post_above_4096:
9127 cmp bx, #8192
9128 jnbe hd0_post_above_8192
9129 ;; 4096 < c <= 8192 cylinders
9130 shr bx, #0x03
9131 shl cl, #0x03
9132 jmp hd0_post_store_logical
9133
9134 hd0_post_above_8192:
9135 ;; 8192 < c <= 16384 cylinders
9136 shr bx, #0x04
9137 shl cl, #0x04
9138
9139 hd0_post_store_logical:
9140 mov (0x003d + 0x00), bx ;; number of physical cylinders
9141 mov (0x003d + 0x02), cl ;; number of physical heads
9142 ;; checksum
9143 mov cl, #0x0f ;; repeat count
9144 mov si, #0x003d ;; offset to disk0 FDPT
9145 mov al, #0x00 ;; sum
9146 hd0_post_checksum_loop:
9147 add al, [si]
9148 inc si
9149 dec cl
9150 jnz hd0_post_checksum_loop
9151 not al ;; now take 2s complement
9152 inc al
9153 mov [si], al
9154 ;;; Done filling EBDA table for hard disk 0.
9155
9156
9157 check_for_hd1:
9158 ;; is there really a second hard disk? if not, return now
9159 mov al, #0x12
9160 out #0x70, al
9161 in al, #0x71
9162 and al, #0x0f
9163 jnz post_d1_exists
9164 ret
9165 post_d1_exists:
9166 ;; check that the hd type is really 0x0f.
9167 cmp al, #0x0f
9168 jz post_d1_extended
9169 HALT(__LINE__)
9170 post_d1_extended:
9171 ;; check that the extended type is 47 - user definable
9172 mov al, #0x1a
9173 out #0x70, al
9174 in al, #0x71
9175 cmp al, #47 ;; decimal 47 - user definable
9176 je post_d1_type47
9177 HALT(__LINE__)
9178 post_d1_type47:
9179 ;; Table for disk1.
9180 ;; CMOS purpose param table offset
9181 ;; 0x24 cylinders low 0
9182 ;; 0x25 cylinders high 1
9183 ;; 0x26 heads 2
9184 ;; 0x27 write pre-comp low 5
9185 ;; 0x28 write pre-comp high 6
9186 ;; 0x29 heads>8 8
9187 ;; 0x2a landing zone low C
9188 ;; 0x2b landing zone high D
9189 ;; 0x2c sectors/track E
9190 ;;; Fill EBDA table for hard disk 1.
9191 xor ax, ax
9192 mov ds, ax
9193 mov ax, word ptr [0x40E] ;; EBDA segment
9194 mov ds, ax
9195 mov al, #0x28
9196 out #0x70, al
9197 in al, #0x71
9198 mov ah, al
9199 mov al, #0x27
9200 out #0x70, al
9201 in al, #0x71
9202 mov (0x004d + 0x05), ax ;; write precomp word
9203
9204 mov al, #0x29
9205 out #0x70, al
9206 in al, #0x71
9207 mov (0x004d + 0x08), al ;; drive control byte
9208
9209 mov al, #0x2b
9210 out #0x70, al
9211 in al, #0x71
9212 mov ah, al
9213 mov al, #0x2a
9214 out #0x70, al
9215 in al, #0x71
9216 mov (0x004d + 0x0C), ax ;; landing zone word
9217
9218 mov al, #0x25 ;; get cylinders word in AX
9219 out #0x70, al
9220 in al, #0x71 ;; high byte
9221 mov ah, al
9222 mov al, #0x24
9223 out #0x70, al
9224 in al, #0x71 ;; low byte
9225 mov bx, ax ;; BX = cylinders
9226
9227 mov al, #0x26
9228 out #0x70, al
9229 in al, #0x71
9230 mov cl, al ;; CL = heads
9231
9232 mov al, #0x2c
9233 out #0x70, al
9234 in al, #0x71
9235 mov dl, al ;; DL = sectors
9236
9237 cmp bx, #1024
9238 jnbe hd1_post_logical_chs ;; if cylinders > 1024, use translated style CHS
9239
9240 hd1_post_physical_chs:
9241 ;; no logical CHS mapping used, just physical CHS
9242 ;; use Standard Fixed Disk Parameter Table (FDPT)
9243 mov (0x004d + 0x00), bx ;; number of physical cylinders
9244 mov (0x004d + 0x02), cl ;; number of physical heads
9245 mov (0x004d + 0x0E), dl ;; number of physical sectors
9246 ret
9247
9248 hd1_post_logical_chs:
9249 ;; complies with Phoenix style Translated Fixed Disk Parameter Table (FDPT)
9250 mov (0x004d + 0x09), bx ;; number of physical cylinders
9251 mov (0x004d + 0x0b), cl ;; number of physical heads
9252 mov (0x004d + 0x04), dl ;; number of physical sectors
9253 mov (0x004d + 0x0e), dl ;; number of logical sectors (same)
9254 mov al, #0xa0
9255 mov (0x004d + 0x03), al ;; A0h signature, indicates translated table
9256
9257 cmp bx, #2048
9258 jnbe hd1_post_above_2048
9259 ;; 1024 < c <= 2048 cylinders
9260 shr bx, #0x01
9261 shl cl, #0x01
9262 jmp hd1_post_store_logical
9263
9264 hd1_post_above_2048:
9265 cmp bx, #4096
9266 jnbe hd1_post_above_4096
9267 ;; 2048 < c <= 4096 cylinders
9268 shr bx, #0x02
9269 shl cl, #0x02
9270 jmp hd1_post_store_logical
9271
9272 hd1_post_above_4096:
9273 cmp bx, #8192
9274 jnbe hd1_post_above_8192
9275 ;; 4096 < c <= 8192 cylinders
9276 shr bx, #0x03
9277 shl cl, #0x03
9278 jmp hd1_post_store_logical
9279
9280 hd1_post_above_8192:
9281 ;; 8192 < c <= 16384 cylinders
9282 shr bx, #0x04
9283 shl cl, #0x04
9284
9285 hd1_post_store_logical:
9286 mov (0x004d + 0x00), bx ;; number of physical cylinders
9287 mov (0x004d + 0x02), cl ;; number of physical heads
9288 ;; checksum
9289 mov cl, #0x0f ;; repeat count
9290 mov si, #0x004d ;; offset to disk0 FDPT
9291 mov al, #0x00 ;; sum
9292 hd1_post_checksum_loop:
9293 add al, [si]
9294 inc si
9295 dec cl
9296 jnz hd1_post_checksum_loop
9297 not al ;; now take 2s complement
9298 inc al
9299 mov [si], al
9300 ;;; Done filling EBDA table for hard disk 1.
9301
9302 ret
9303
9304 ;--------------------
9305 ;- POST: EBDA segment
9306 ;--------------------
9307 ; relocated here because the primary POST area isnt big enough.
9308 ebda_post:
9309 #if BX_USE_EBDA
9310 mov ax, #EBDA_SEG
9311 mov ds, ax
9312 mov byte ptr [0x0], #EBDA_SIZE
9313 #endif
9314 xor ax, ax ; mov EBDA seg into 40E
9315 mov ds, ax
9316 mov word ptr [0x40E], #EBDA_SEG
9317 ret;;
9318
9319 ;--------------------
9320 ;- POST: EOI + jmp via [0x40:67)
9321 ;--------------------
9322 ; relocated here because the primary POST area isnt big enough.
9323 eoi_jmp_post:
9324 mov al, #0x20
9325 out #0xA0, al ;; slave PIC EOI
9326 mov al, #0x20
9327 out #0x20, al ;; master PIC EOI
9328
9329 jmp_post_0x467:
9330 xor ax, ax
9331 mov ds, ax
9332
9333 jmp far ptr [0x467]
9334
9335 iret_post_0x467:
9336 xor ax, ax
9337 mov ds, ax
9338
9339 mov sp, [0x467]
9340 mov ss, [0x469]
9341 iret
9342
9343 retf_post_0x467:
9344 xor ax, ax
9345 mov ds, ax
9346
9347 mov sp, [0x467]
9348 mov ss, [0x469]
9349 retf
9350
9351 s3_post:
9352 #if BX_ROMBIOS32
9353 call rombios32_init
9354 #endif
9355 call _s3_resume
9356 mov bl, #0x00
9357 and ax, ax
9358 jz normal_post
9359 call _s3_resume_panic
9360
9361 ;--------------------
9362 eoi_both_pics:
9363 mov al, #0x20
9364 out #0xA0, al ;; slave PIC EOI
9365 eoi_master_pic:
9366 mov al, #0x20
9367 out #0x20, al ;; master PIC EOI
9368 ret
9369
9370 ;--------------------
9371 BcdToBin:
9372 ;; in: AL in BCD format
9373 ;; out: AL in binary format, AH will always be 0
9374 ;; trashes BX
9375 mov bl, al
9376 and bl, #0x0f ;; bl has low digit
9377 shr al, #4 ;; al has high digit
9378 mov bh, #10
9379 mul al, bh ;; multiply high digit by 10 (result in AX)
9380 add al, bl ;; then add low digit
9381 ret
9382
9383 ;--------------------
9384 timer_tick_post:
9385 ;; Setup the Timer Ticks Count (0x46C:dword) and
9386 ;; Timer Ticks Roller Flag (0x470:byte)
9387 ;; The Timer Ticks Count needs to be set according to
9388 ;; the current CMOS time, as if ticks have been occurring
9389 ;; at 18.2hz since midnight up to this point. Calculating
9390 ;; this is a little complicated. Here are the factors I gather
9391 ;; regarding this. 14,318,180 hz was the original clock speed,
9392 ;; chosen so it could be divided by either 3 to drive the 5Mhz CPU
9393 ;; at the time, or 4 to drive the CGA video adapter. The div3
9394 ;; source was divided again by 4 to feed a 1.193Mhz signal to
9395 ;; the timer. With a maximum 16bit timer count, this is again
9396 ;; divided down by 65536 to 18.2hz.
9397 ;;
9398 ;; 14,318,180 Hz clock
9399 ;; /3 = 4,772,726 Hz fed to orginal 5Mhz CPU
9400 ;; /4 = 1,193,181 Hz fed to timer
9401 ;; /65536 (maximum timer count) = 18.20650736 ticks/second
9402 ;; 1 second = 18.20650736 ticks
9403 ;; 1 minute = 1092.390442 ticks
9404 ;; 1 hour = 65543.42651 ticks
9405 ;;
9406 ;; Given the values in the CMOS clock, one could calculate
9407 ;; the number of ticks by the following:
9408 ;; ticks = (BcdToBin(seconds) * 18.206507) +
9409 ;; (BcdToBin(minutes) * 1092.3904)
9410 ;; (BcdToBin(hours) * 65543.427)
9411 ;; To get a little more accuracy, since Im using integer
9412 ;; arithmatic, I use:
9413 ;; ticks = (BcdToBin(seconds) * 18206507) / 1000000 +
9414 ;; (BcdToBin(minutes) * 10923904) / 10000 +
9415 ;; (BcdToBin(hours) * 65543427) / 1000
9416
9417 ;; assuming DS=0000
9418
9419 ;; get CMOS seconds
9420 xor eax, eax ;; clear EAX
9421 mov al, #0x00
9422 out #0x70, al
9423 in al, #0x71 ;; AL has CMOS seconds in BCD
9424 call BcdToBin ;; EAX now has seconds in binary
9425 mov edx, #18206507
9426 mul eax, edx
9427 mov ebx, #1000000
9428 xor edx, edx
9429 div eax, ebx
9430 mov ecx, eax ;; ECX will accumulate total ticks
9431
9432 ;; get CMOS minutes
9433 xor eax, eax ;; clear EAX
9434 mov al, #0x02
9435 out #0x70, al
9436 in al, #0x71 ;; AL has CMOS minutes in BCD
9437 call BcdToBin ;; EAX now has minutes in binary
9438 mov edx, #10923904
9439 mul eax, edx
9440 mov ebx, #10000
9441 xor edx, edx
9442 div eax, ebx
9443 add ecx, eax ;; add to total ticks
9444
9445 ;; get CMOS hours
9446 xor eax, eax ;; clear EAX
9447 mov al, #0x04
9448 out #0x70, al
9449 in al, #0x71 ;; AL has CMOS hours in BCD
9450 call BcdToBin ;; EAX now has hours in binary
9451 mov edx, #65543427
9452 mul eax, edx
9453 mov ebx, #1000
9454 xor edx, edx
9455 div eax, ebx
9456 add ecx, eax ;; add to total ticks
9457
9458 mov 0x46C, ecx ;; Timer Ticks Count
9459 xor al, al
9460 mov 0x470, al ;; Timer Ticks Rollover Flag
9461 ret
9462
9463 ;--------------------
9464 int76_handler:
9465 ;; record completion in BIOS task complete flag
9466 push ax
9467 push ds
9468 mov ax, #0x0040
9469 mov ds, ax
9470 mov 0x008E, #0xff
9471 call eoi_both_pics
9472 pop ds
9473 pop ax
9474 iret
9475
9476
9477 ;--------------------
9478 #if BX_APM
9479
9480 use32 386
9481 #define APM_PROT32
9482 #include "apmbios.S"
9483
9484 use16 386
9485 #define APM_PROT16
9486 #include "apmbios.S"
9487
9488 #define APM_REAL
9489 #include "apmbios.S"
9490
9491 #endif
9492
9493 #include "32bitgateway.c"
9494 ASM_END
9495 #include "tcgbios.c"
9496 ASM_START
9497
9498 ;--------------------
9499 #if BX_PCIBIOS
9500 use32 386
9501 .align 16
9502 bios32_structure:
9503 db 0x5f, 0x33, 0x32, 0x5f ;; "_32_" signature
9504 dw bios32_entry_point, 0xf ;; 32 bit physical address
9505 db 0 ;; revision level
9506 ;; length in paragraphs and checksum stored in a word to prevent errors
9507 dw (~(((bios32_entry_point >> 8) + (bios32_entry_point & 0xff) + 0x32) \
9508 & 0xff) << 8) + 0x01
9509 db 0,0,0,0,0 ;; reserved
9510
9511 .align 16
9512 bios32_entry_point:
9513 pushfd
9514 cmp eax, #0x49435024 ;; "$PCI"
9515 jne unknown_service
9516 mov eax, #0x80000000
9517 mov dx, #0x0cf8
9518 out dx, eax
9519 mov dx, #0x0cfc
9520 in eax, dx
9521 #ifdef PCI_FIXED_HOST_BRIDGE
9522 cmp eax, #PCI_FIXED_HOST_BRIDGE
9523 jne unknown_service
9524 #else
9525 ;; say ok if a device is present
9526 cmp eax, #0xffffffff
9527 je unknown_service
9528 #endif
9529 mov ebx, #0x000f0000
9530 mov ecx, #0
9531 mov edx, #pcibios_protected
9532 xor al, al
9533 jmp bios32_end
9534 unknown_service:
9535 mov al, #0x80
9536 bios32_end:
9537 #ifdef BX_QEMU
9538 and dword ptr[esp+8],0xfffffffc ;; reset CS.RPL for kqemu
9539 #endif
9540 popfd
9541 retf
9542
9543 .align 16
9544 pcibios_protected:
9545 pushfd
9546 cli
9547 push esi
9548 push edi
9549 cmp al, #0x01 ;; installation check
9550 jne pci_pro_f02
9551 mov bx, #0x0210
9552 mov cx, #0
9553 mov edx, #0x20494350 ;; "PCI "
9554 mov al, #0x01
9555 jmp pci_pro_ok
9556 pci_pro_f02: ;; find pci device
9557 cmp al, #0x02
9558 jne pci_pro_f03
9559 shl ecx, #16
9560 mov cx, dx
9561 xor bx, bx
9562 mov di, #0x00
9563 pci_pro_devloop:
9564 call pci_pro_select_reg
9565 mov dx, #0x0cfc
9566 in eax, dx
9567 cmp eax, ecx
9568 jne pci_pro_nextdev
9569 cmp si, #0
9570 je pci_pro_ok
9571 dec si
9572 pci_pro_nextdev:
9573 inc bx
9574 cmp bx, #0x0100
9575 jne pci_pro_devloop
9576 mov ah, #0x86
9577 jmp pci_pro_fail
9578 pci_pro_f03: ;; find class code
9579 cmp al, #0x03
9580 jne pci_pro_f08
9581 xor bx, bx
9582 mov di, #0x08
9583 pci_pro_devloop2:
9584 call pci_pro_select_reg
9585 mov dx, #0x0cfc
9586 in eax, dx
9587 shr eax, #8
9588 cmp eax, ecx
9589 jne pci_pro_nextdev2
9590 cmp si, #0
9591 je pci_pro_ok
9592 dec si
9593 pci_pro_nextdev2:
9594 inc bx
9595 cmp bx, #0x0100
9596 jne pci_pro_devloop2
9597 mov ah, #0x86
9598 jmp pci_pro_fail
9599 pci_pro_f08: ;; read configuration byte
9600 cmp al, #0x08
9601 jne pci_pro_f09
9602 call pci_pro_select_reg
9603 push edx
9604 mov dx, di
9605 and dx, #0x03
9606 add dx, #0x0cfc
9607 in al, dx
9608 pop edx
9609 mov cl, al
9610 jmp pci_pro_ok
9611 pci_pro_f09: ;; read configuration word
9612 cmp al, #0x09
9613 jne pci_pro_f0a
9614 call pci_pro_select_reg
9615 push edx
9616 mov dx, di
9617 and dx, #0x02
9618 add dx, #0x0cfc
9619 in ax, dx
9620 pop edx
9621 mov cx, ax
9622 jmp pci_pro_ok
9623 pci_pro_f0a: ;; read configuration dword
9624 cmp al, #0x0a
9625 jne pci_pro_f0b
9626 call pci_pro_select_reg
9627 push edx
9628 mov dx, #0x0cfc
9629 in eax, dx
9630 pop edx
9631 mov ecx, eax
9632 jmp pci_pro_ok
9633 pci_pro_f0b: ;; write configuration byte
9634 cmp al, #0x0b
9635 jne pci_pro_f0c
9636 call pci_pro_select_reg
9637 push edx
9638 mov dx, di
9639 and dx, #0x03
9640 add dx, #0x0cfc
9641 mov al, cl
9642 out dx, al
9643 pop edx
9644 jmp pci_pro_ok
9645 pci_pro_f0c: ;; write configuration word
9646 cmp al, #0x0c
9647 jne pci_pro_f0d
9648 call pci_pro_select_reg
9649 push edx
9650 mov dx, di
9651 and dx, #0x02
9652 add dx, #0x0cfc
9653 mov ax, cx
9654 out dx, ax
9655 pop edx
9656 jmp pci_pro_ok
9657 pci_pro_f0d: ;; write configuration dword
9658 cmp al, #0x0d
9659 jne pci_pro_unknown
9660 call pci_pro_select_reg
9661 push edx
9662 mov dx, #0x0cfc
9663 mov eax, ecx
9664 out dx, eax
9665 pop edx
9666 jmp pci_pro_ok
9667 pci_pro_unknown:
9668 mov ah, #0x81
9669 pci_pro_fail:
9670 pop edi
9671 pop esi
9672 #ifdef BX_QEMU
9673 and dword ptr[esp+8],0xfffffffc ;; reset CS.RPL for kqemu
9674 #endif
9675 popfd
9676 stc
9677 retf
9678 pci_pro_ok:
9679 xor ah, ah
9680 pop edi
9681 pop esi
9682 #ifdef BX_QEMU
9683 and dword ptr[esp+8],0xfffffffc ;; reset CS.RPL for kqemu
9684 #endif
9685 popfd
9686 clc
9687 retf
9688
9689 pci_pro_select_reg:
9690 push edx
9691 mov eax, #0x800000
9692 mov ax, bx
9693 shl eax, #8
9694 and di, #0xff
9695 or ax, di
9696 and al, #0xfc
9697 mov dx, #0x0cf8
9698 out dx, eax
9699 pop edx
9700 ret
9701
9702 use16 386
9703
9704 pcibios_real:
9705 push eax
9706 push dx
9707 mov eax, #0x80000000
9708 mov dx, #0x0cf8
9709 out dx, eax
9710 mov dx, #0x0cfc
9711 in eax, dx
9712 #ifdef PCI_FIXED_HOST_BRIDGE
9713 cmp eax, #PCI_FIXED_HOST_BRIDGE
9714 je pci_present
9715 #else
9716 ;; say ok if a device is present
9717 cmp eax, #0xffffffff
9718 jne pci_present
9719 #endif
9720 pop dx
9721 pop eax
9722 mov ah, #0xff
9723 stc
9724 ret
9725 pci_present:
9726 pop dx
9727 pop eax
9728 cmp al, #0x01 ;; installation check
9729 jne pci_real_f02
9730 mov ax, #0x0001
9731 mov bx, #0x0210
9732 mov cx, #0
9733 mov edx, #0x20494350 ;; "PCI "
9734 mov edi, #0xf0000
9735 mov di, #pcibios_protected
9736 clc
9737 ret
9738 pci_real_f02: ;; find pci device
9739 push esi
9740 push edi
9741 cmp al, #0x02
9742 jne pci_real_f03
9743 shl ecx, #16
9744 mov cx, dx
9745 xor bx, bx
9746 mov di, #0x00
9747 pci_real_devloop:
9748 call pci_real_select_reg
9749 mov dx, #0x0cfc
9750 in eax, dx
9751 cmp eax, ecx
9752 jne pci_real_nextdev
9753 cmp si, #0
9754 je pci_real_ok
9755 dec si
9756 pci_real_nextdev:
9757 inc bx
9758 cmp bx, #0x0100
9759 jne pci_real_devloop
9760 mov dx, cx
9761 shr ecx, #16
9762 mov ax, #0x8602
9763 jmp pci_real_fail
9764 pci_real_f03: ;; find class code
9765 cmp al, #0x03
9766 jne pci_real_f08
9767 xor bx, bx
9768 mov di, #0x08
9769 pci_real_devloop2:
9770 call pci_real_select_reg
9771 mov dx, #0x0cfc
9772 in eax, dx
9773 shr eax, #8
9774 cmp eax, ecx
9775 jne pci_real_nextdev2
9776 cmp si, #0
9777 je pci_real_ok
9778 dec si
9779 pci_real_nextdev2:
9780 inc bx
9781 cmp bx, #0x0100
9782 jne pci_real_devloop2
9783 mov dx, cx
9784 shr ecx, #16
9785 mov ax, #0x8603
9786 jmp pci_real_fail
9787 pci_real_f08: ;; read configuration byte
9788 cmp al, #0x08
9789 jne pci_real_f09
9790 call pci_real_select_reg
9791 push dx
9792 mov dx, di
9793 and dx, #0x03
9794 add dx, #0x0cfc
9795 in al, dx
9796 pop dx
9797 mov cl, al
9798 jmp pci_real_ok
9799 pci_real_f09: ;; read configuration word
9800 cmp al, #0x09
9801 jne pci_real_f0a
9802 call pci_real_select_reg
9803 push dx
9804 mov dx, di
9805 and dx, #0x02
9806 add dx, #0x0cfc
9807 in ax, dx
9808 pop dx
9809 mov cx, ax
9810 jmp pci_real_ok
9811 pci_real_f0a: ;; read configuration dword
9812 cmp al, #0x0a
9813 jne pci_real_f0b
9814 call pci_real_select_reg
9815 push dx
9816 mov dx, #0x0cfc
9817 in eax, dx
9818 pop dx
9819 mov ecx, eax
9820 jmp pci_real_ok
9821 pci_real_f0b: ;; write configuration byte
9822 cmp al, #0x0b
9823 jne pci_real_f0c
9824 call pci_real_select_reg
9825 push dx
9826 mov dx, di
9827 and dx, #0x03
9828 add dx, #0x0cfc
9829 mov al, cl
9830 out dx, al
9831 pop dx
9832 jmp pci_real_ok
9833 pci_real_f0c: ;; write configuration word
9834 cmp al, #0x0c
9835 jne pci_real_f0d
9836 call pci_real_select_reg
9837 push dx
9838 mov dx, di
9839 and dx, #0x02
9840 add dx, #0x0cfc
9841 mov ax, cx
9842 out dx, ax
9843 pop dx
9844 jmp pci_real_ok
9845 pci_real_f0d: ;; write configuration dword
9846 cmp al, #0x0d
9847 jne pci_real_f0e
9848 call pci_real_select_reg
9849 push dx
9850 mov dx, #0x0cfc
9851 mov eax, ecx
9852 out dx, eax
9853 pop dx
9854 jmp pci_real_ok
9855 pci_real_f0e: ;; get irq routing options
9856 cmp al, #0x0e
9857 jne pci_real_unknown
9858 SEG ES
9859 cmp word ptr [di], #pci_routing_table_structure_end - pci_routing_table_structure_start
9860 jb pci_real_too_small
9861 SEG ES
9862 mov word ptr [di], #pci_routing_table_structure_end - pci_routing_table_structure_start
9863 pushf
9864 push ds
9865 push es
9866 push cx
9867 push si
9868 push di
9869 cld
9870 mov si, #pci_routing_table_structure_start
9871 push cs
9872 pop ds
9873 SEG ES
9874 mov cx, [di+2]
9875 SEG ES
9876 mov es, [di+4]
9877 mov di, cx
9878 mov cx, #pci_routing_table_structure_end - pci_routing_table_structure_start
9879 rep
9880 movsb
9881 pop di
9882 pop si
9883 pop cx
9884 pop es
9885 pop ds
9886 popf
9887 mov bx, #(1 << 9) | (1 << 11) ;; irq 9 and 11 are used
9888 jmp pci_real_ok
9889 pci_real_too_small:
9890 SEG ES
9891 mov word ptr [di], #pci_routing_table_structure_end - pci_routing_table_structure_start
9892 mov ah, #0x89
9893 jmp pci_real_fail
9894
9895 pci_real_unknown:
9896 mov ah, #0x81
9897 pci_real_fail:
9898 pop edi
9899 pop esi
9900 stc
9901 ret
9902 pci_real_ok:
9903 xor ah, ah
9904 pop edi
9905 pop esi
9906 clc
9907 ret
9908
9909 pci_real_select_reg:
9910 push dx
9911 mov eax, #0x800000
9912 mov ax, bx
9913 shl eax, #8
9914 and di, #0xff
9915 or ax, di
9916 and al, #0xfc
9917 mov dx, #0x0cf8
9918 out dx, eax
9919 pop dx
9920 ret
9921
9922 .align 16
9923 pci_routing_table_structure:
9924 db 0x24, 0x50, 0x49, 0x52 ;; "$PIR" signature
9925 db 0, 1 ;; version
9926 dw 32 + (6 * 16) ;; table size
9927 db 0 ;; PCI interrupt router bus
9928 db 0x08 ;; PCI interrupt router DevFunc
9929 dw 0x0000 ;; PCI exclusive IRQs
9930 dw 0x8086 ;; compatible PCI interrupt router vendor ID
9931 dw 0x122e ;; compatible PCI interrupt router device ID
9932 dw 0,0 ;; Miniport data
9933 db 0,0,0,0,0,0,0,0,0,0,0 ;; reserved
9934 db 0x37 ;; checksum
9935 pci_routing_table_structure_start:
9936 ;; first slot entry PCI-to-ISA (embedded)
9937 db 0 ;; pci bus number
9938 db 0x08 ;; pci device number (bit 7-3)
9939 db 0x61 ;; link value INTA#: pointer into PCI2ISA config space
9940 dw 0x0c20 ;; IRQ bitmap INTA#
9941 db 0x62 ;; link value INTB#
9942 dw 0x0c20 ;; IRQ bitmap INTB#
9943 db 0x63 ;; link value INTC#
9944 dw 0x0c20 ;; IRQ bitmap INTC#
9945 db 0x60 ;; link value INTD#
9946 dw 0x0c20 ;; IRQ bitmap INTD#
9947 db 0 ;; physical slot (0 = embedded)
9948 db 0 ;; reserved
9949 ;; second slot entry: 1st PCI slot
9950 db 0 ;; pci bus number
9951 db 0x10 ;; pci device number (bit 7-3)
9952 db 0x62 ;; link value INTA#
9953 dw 0x0c20 ;; IRQ bitmap INTA#
9954 db 0x63 ;; link value INTB#
9955 dw 0x0c20 ;; IRQ bitmap INTB#
9956 db 0x60 ;; link value INTC#
9957 dw 0x0c20 ;; IRQ bitmap INTC#
9958 db 0x61 ;; link value INTD#
9959 dw 0x0c20 ;; IRQ bitmap INTD#
9960 db 1 ;; physical slot (0 = embedded)
9961 db 0 ;; reserved
9962 ;; third slot entry: 2nd PCI slot
9963 db 0 ;; pci bus number
9964 db 0x18 ;; pci device number (bit 7-3)
9965 db 0x63 ;; link value INTA#
9966 dw 0x0c20 ;; IRQ bitmap INTA#
9967 db 0x60 ;; link value INTB#
9968 dw 0x0c20 ;; IRQ bitmap INTB#
9969 db 0x61 ;; link value INTC#
9970 dw 0x0c20 ;; IRQ bitmap INTC#
9971 db 0x62 ;; link value INTD#
9972 dw 0x0c20 ;; IRQ bitmap INTD#
9973 db 2 ;; physical slot (0 = embedded)
9974 db 0 ;; reserved
9975 ;; 4th slot entry: 3rd PCI slot
9976 db 0 ;; pci bus number
9977 db 0x20 ;; pci device number (bit 7-3)
9978 db 0x60 ;; link value INTA#
9979 dw 0x0c20 ;; IRQ bitmap INTA#
9980 db 0x61 ;; link value INTB#
9981 dw 0x0c20 ;; IRQ bitmap INTB#
9982 db 0x62 ;; link value INTC#
9983 dw 0x0c20 ;; IRQ bitmap INTC#
9984 db 0x63 ;; link value INTD#
9985 dw 0x0c20 ;; IRQ bitmap INTD#
9986 db 3 ;; physical slot (0 = embedded)
9987 db 0 ;; reserved
9988 ;; 5th slot entry: 4rd PCI slot
9989 db 0 ;; pci bus number
9990 db 0x28 ;; pci device number (bit 7-3)
9991 db 0x61 ;; link value INTA#
9992 dw 0x0c20 ;; IRQ bitmap INTA#
9993 db 0x62 ;; link value INTB#
9994 dw 0x0c20 ;; IRQ bitmap INTB#
9995 db 0x63 ;; link value INTC#
9996 dw 0x0c20 ;; IRQ bitmap INTC#
9997 db 0x60 ;; link value INTD#
9998 dw 0x0c20 ;; IRQ bitmap INTD#
9999 db 4 ;; physical slot (0 = embedded)
10000 db 0 ;; reserved
10001 ;; 6th slot entry: 5rd PCI slot
10002 db 0 ;; pci bus number
10003 db 0x30 ;; pci device number (bit 7-3)
10004 db 0x62 ;; link value INTA#
10005 dw 0x0c20 ;; IRQ bitmap INTA#
10006 db 0x63 ;; link value INTB#
10007 dw 0x0c20 ;; IRQ bitmap INTB#
10008 db 0x60 ;; link value INTC#
10009 dw 0x0c20 ;; IRQ bitmap INTC#
10010 db 0x61 ;; link value INTD#
10011 dw 0x0c20 ;; IRQ bitmap INTD#
10012 db 5 ;; physical slot (0 = embedded)
10013 db 0 ;; reserved
10014 pci_routing_table_structure_end:
10015
10016 #if !BX_ROMBIOS32 && !defined(HVMASSIST)
10017 pci_irq_list:
10018 db 11, 10, 9, 5;
10019
10020 pcibios_init_sel_reg:
10021 push eax
10022 mov eax, #0x800000
10023 mov ax, bx
10024 shl eax, #8
10025 and dl, #0xfc
10026 or al, dl
10027 mov dx, #0x0cf8
10028 out dx, eax
10029 pop eax
10030 ret
10031
10032 pcibios_init_iomem_bases:
10033 push bp
10034 mov bp, sp
10035 mov eax, #0xe0000000 ;; base for memory init
10036 push eax
10037 mov ax, #0xc000 ;; base for i/o init
10038 push ax
10039 mov ax, #0x0010 ;; start at base address #0
10040 push ax
10041 mov bx, #0x0008
10042 pci_init_io_loop1:
10043 mov dl, #0x00
10044 call pcibios_init_sel_reg
10045 mov dx, #0x0cfc
10046 in ax, dx
10047 cmp ax, #0xffff
10048 jz next_pci_dev
10049 mov dl, #0x04 ;; disable i/o and memory space access
10050 call pcibios_init_sel_reg
10051 mov dx, #0x0cfc
10052 in al, dx
10053 and al, #0xfc
10054 out dx, al
10055 pci_init_io_loop2:
10056 mov dl, [bp-8]
10057 call pcibios_init_sel_reg
10058 mov dx, #0x0cfc
10059 in eax, dx
10060 test al, #0x01
10061 jnz init_io_base
10062 mov ecx, eax
10063 mov eax, #0xffffffff
10064 out dx, eax
10065 in eax, dx
10066 cmp eax, ecx
10067 je next_pci_base
10068 xor eax, #0xffffffff
10069 mov ecx, eax
10070 mov eax, [bp-4]
10071 out dx, eax
10072 add eax, ecx ;; calculate next free mem base
10073 add eax, #0x01000000
10074 and eax, #0xff000000
10075 mov [bp-4], eax
10076 jmp next_pci_base
10077 init_io_base:
10078 mov cx, ax
10079 mov ax, #0xffff
10080 out dx, ax
10081 in ax, dx
10082 cmp ax, cx
10083 je next_pci_base
10084 xor ax, #0xfffe
10085 mov cx, ax
10086 mov ax, [bp-6]
10087 out dx, ax
10088 add ax, cx ;; calculate next free i/o base
10089 add ax, #0x0100
10090 and ax, #0xff00
10091 mov [bp-6], ax
10092 next_pci_base:
10093 mov al, [bp-8]
10094 add al, #0x04
10095 cmp al, #0x28
10096 je enable_iomem_space
10097 mov byte ptr[bp-8], al
10098 jmp pci_init_io_loop2
10099 enable_iomem_space:
10100 mov dl, #0x04 ;; enable i/o and memory space access if available
10101 call pcibios_init_sel_reg
10102 mov dx, #0x0cfc
10103 in al, dx
10104 or al, #0x07
10105 out dx, al
10106 next_pci_dev:
10107 mov byte ptr[bp-8], #0x10
10108 inc bx
10109 cmp bx, #0x0100
10110 jne pci_init_io_loop1
10111 mov sp, bp
10112 pop bp
10113 ret
10114
10115 pcibios_init_set_elcr:
10116 push ax
10117 push cx
10118 mov dx, #0x04d0
10119 test al, #0x08
10120 jz is_master_pic
10121 inc dx
10122 and al, #0x07
10123 is_master_pic:
10124 mov cl, al
10125 mov bl, #0x01
10126 shl bl, cl
10127 in al, dx
10128 or al, bl
10129 out dx, al
10130 pop cx
10131 pop ax
10132 ret
10133
10134 pcibios_init_irqs:
10135 push ds
10136 push bp
10137 mov ax, #0xf000
10138 mov ds, ax
10139 mov dx, #0x04d0 ;; reset ELCR1 + ELCR2
10140 mov al, #0x00
10141 out dx, al
10142 inc dx
10143 out dx, al
10144 mov si, #pci_routing_table_structure
10145 mov bh, [si+8]
10146 mov bl, [si+9]
10147 mov dl, #0x00
10148 call pcibios_init_sel_reg
10149 mov dx, #0x0cfc
10150 in eax, dx
10151 cmp eax, [si+12] ;; check irq router
10152 jne pci_init_end
10153 mov dl, [si+34]
10154 call pcibios_init_sel_reg
10155 push bx ;; save irq router bus + devfunc
10156 mov dx, #0x0cfc
10157 mov ax, #0x8080
10158 out dx, ax ;; reset PIRQ route control
10159 add dx, #2
10160 out dx, ax
10161 mov ax, [si+6]
10162 sub ax, #0x20
10163 shr ax, #4
10164 mov cx, ax
10165 add si, #0x20 ;; set pointer to 1st entry
10166 mov bp, sp
10167 mov ax, #pci_irq_list
10168 push ax
10169 xor ax, ax
10170 push ax
10171 pci_init_irq_loop1:
10172 mov bh, [si]
10173 mov bl, [si+1]
10174 pci_init_irq_loop2:
10175 mov dl, #0x00
10176 call pcibios_init_sel_reg
10177 mov dx, #0x0cfc
10178 in ax, dx
10179 cmp ax, #0xffff
10180 jnz pci_test_int_pin
10181 test bl, #0x07
10182 jz next_pir_entry
10183 jmp next_pci_func
10184 pci_test_int_pin:
10185 mov dl, #0x3c
10186 call pcibios_init_sel_reg
10187 mov dx, #0x0cfd
10188 in al, dx
10189 and al, #0x07
10190 jz next_pci_func
10191 dec al ;; determine pirq reg
10192 mov dl, #0x03
10193 mul al, dl
10194 add al, #0x02
10195 xor ah, ah
10196 mov bx, ax
10197 mov al, [si+bx]
10198 mov dl, al
10199 mov bx, [bp]
10200 call pcibios_init_sel_reg
10201 mov dx, #0x0cfc
10202 and al, #0x03
10203 add dl, al
10204 in al, dx
10205 cmp al, #0x80
10206 jb pirq_found
10207 mov bx, [bp-2] ;; pci irq list pointer
10208 mov al, [bx]
10209 out dx, al
10210 inc bx
10211 mov [bp-2], bx
10212 call pcibios_init_set_elcr
10213 pirq_found:
10214 mov bh, [si]
10215 mov bl, [si+1]
10216 add bl, [bp-3] ;; pci function number
10217 mov dl, #0x3c
10218 call pcibios_init_sel_reg
10219 mov dx, #0x0cfc
10220 out dx, al
10221 next_pci_func:
10222 inc byte ptr[bp-3]
10223 inc bl
10224 test bl, #0x07
10225 jnz pci_init_irq_loop2
10226 next_pir_entry:
10227 add si, #0x10
10228 mov byte ptr[bp-3], #0x00
10229 loop pci_init_irq_loop1
10230 mov sp, bp
10231 pop bx
10232 pci_init_end:
10233 pop bp
10234 pop ds
10235 ret
10236 #endif // !BX_ROMBIOS32
10237 #endif // BX_PCIBIOS
10238
10239 #if BX_ROMBIOS32
10240 rombios32_init:
10241 ;; save a20 and enable it
10242 in al, 0x92
10243 push ax
10244 or al, #0x02
10245 out 0x92, al
10246
10247 ;; save SS:SP to the BDA
10248 xor ax, ax
10249 mov ds, ax
10250 mov 0x0469, ss
10251 mov 0x0467, sp
10252
10253 SEG CS
10254 lidt [pmode_IDT_info]
10255 SEG CS
10256 lgdt [rombios32_gdt_48]
10257 ;; set PE bit in CR0
10258 mov eax, cr0
10259 or al, #0x01
10260 mov cr0, eax
10261 ;; start protected mode code: ljmpl 0x10:rombios32_init1
10262 db 0x66, 0xea
10263 dw rombios32_05
10264 dw 0x000f ;; high 16 bit address
10265 dw 0x0010
10266
10267 use32 386
10268 rombios32_05:
10269 ;; init data segments
10270 mov eax, #0x18
10271 mov ds, ax
10272 mov es, ax
10273 mov ss, ax
10274 xor eax, eax
10275 mov fs, ax
10276 mov gs, ax
10277 cld
10278
10279 ;; init the stack pointer to point below EBDA
10280 mov ax, [0x040e]
10281 shl eax, #4
10282 mov esp, #-0x10
10283 add esp, eax
10284
10285 ;; pass pointer to s3_resume_flag and s3_resume_vector to rombios32
10286 push #0x04b0
10287 push #0x04b2
10288
10289 ;; call rombios32 code
10290 mov eax, #0x000e0000
10291 call eax
10292
10293 ;; return to 16 bit protected mode first
10294 db 0xea
10295 dd rombios32_10
10296 dw 0x20
10297
10298 use16 386
10299 rombios32_10:
10300 ;; restore data segment limits to 0xffff
10301 mov ax, #0x28
10302 mov ds, ax
10303 mov es, ax
10304 mov ss, ax
10305 mov fs, ax
10306 mov gs, ax
10307
10308 ;; reset PE bit in CR0
10309 mov eax, cr0
10310 and al, #0xFE
10311 mov cr0, eax
10312
10313 ;; far jump to flush CPU queue after transition to real mode
10314 JMP_AP(0xf000, rombios32_real_mode)
10315
10316 rombios32_real_mode:
10317 ;; restore IDT to normal real-mode defaults
10318 SEG CS
10319 lidt [rmode_IDT_info]
10320
10321 xor ax, ax
10322 mov ds, ax
10323 mov es, ax
10324 mov fs, ax
10325 mov gs, ax
10326
10327 ;; restore SS:SP from the BDA
10328 mov ss, 0x0469
10329 xor esp, esp
10330 mov sp, 0x0467
10331 ;; restore a20
10332 pop ax
10333 out 0x92, al
10334 ret
10335
10336 rombios32_gdt_48:
10337 dw 0x30
10338 dw rombios32_gdt
10339 dw 0x000f
10340
10341 rombios32_gdt:
10342 dw 0, 0, 0, 0
10343 dw 0, 0, 0, 0
10344 dw 0xffff, 0, 0x9b00, 0x00cf ; 32 bit flat code segment (0x10)
10345 dw 0xffff, 0, 0x9300, 0x00cf ; 32 bit flat data segment (0x18)
10346 dw 0xffff, 0, 0x9b0f, 0x0000 ; 16 bit code segment base=0xf0000 limit=0xffff
10347 dw 0xffff, 0, 0x9300, 0x0000 ; 16 bit data segment base=0x0 limit=0xffff
10348 #endif // BX_ROMBIOS32
10349
10350 #if BX_PMM
10351 ; according to POST Memory Manager Specification Version 1.01
10352 .align 16
10353 pmm_structure:
10354 db 0x24,0x50,0x4d,0x4d ;; "$PMM" signature
10355 db 0x01 ;; revision
10356 db 16 ;; length
10357 db (-((pmm_entry_point>>8)+pmm_entry_point+0x20f))&0xff;; checksum
10358 dw pmm_entry_point,0xf000 ;; far call entrypoint
10359 db 0,0,0,0,0 ;; reserved
10360
10361 pmm_entry_point:
10362 pushf
10363 pushad
10364 ; Calculate protected-mode address of PMM function args
10365 xor eax, eax
10366 mov ax, sp
10367 xor ebx, ebx
10368 mov bx, ss
10369 shl ebx, 4
10370 lea ebx, [eax+ebx+38] ;; ebx=(ss<<4)+sp+4(far call)+2(pushf)+32(pushad)
10371 push ebx
10372 ;
10373 ; Stack layout at this point:
10374 ;
10375 ; : +0x0 +0x2 +0x4 +0x6 +0x8 +0xa +0xc +0xe
10376 ; -----------------------------------------------------------------------
10377 ; sp : [&arg1 ][edi ][esi ][ebp ]
10378 ; sp+0x10: [esp ][ebx ][edx ][ecx ]
10379 ; sp+0x20: [eax ][flags ][ip ][cs ][arg1 ][arg2, ...
10380 ;
10381 call _pmm
10382 mov bx, sp
10383 SEG SS
10384 mov [bx+0x20], ax
10385 SEG SS
10386 mov [bx+0x18], dx
10387 pop ebx
10388 popad
10389 popf
10390 retf
10391 #endif // BX_PMM
10392
10393 ; parallel port detection: base address in DX, index in BX, timeout in CL
10394 detect_parport:
10395 push dx
10396 add dx, #2
10397 in al, dx
10398 and al, #0xdf ; clear input mode
10399 out dx, al
10400 pop dx
10401 mov al, #0xaa
10402 out dx, al
10403 in al, dx
10404 cmp al, #0xaa
10405 jne no_parport
10406 push bx
10407 shl bx, #1
10408 mov [bx+0x408], dx ; Parallel I/O address
10409 pop bx
10410 mov [bx+0x478], cl ; Parallel printer timeout
10411 inc bx
10412 no_parport:
10413 ret
10414
10415 ; serial port detection: base address in DX, index in BX, timeout in CL
10416 detect_serial:
10417 push dx
10418 inc dx
10419 mov al, #0x02
10420 out dx, al
10421 in al, dx
10422 cmp al, #0x02
10423 jne no_serial
10424 inc dx
10425 in al, dx
10426 cmp al, #0x02
10427 jne no_serial
10428 dec dx
10429 xor al, al
10430 out dx, al
10431 pop dx
10432 push bx
10433 shl bx, #1
10434 mov [bx+0x400], dx ; Serial I/O address
10435 pop bx
10436 mov [bx+0x47c], cl ; Serial timeout
10437 inc bx
10438 ret
10439 no_serial:
10440 pop dx
10441 ret
10442
10443 rom_checksum:
10444 pusha
10445 push ds
10446
10447 xor ax, ax
10448 xor bx, bx
10449 xor cx, cx
10450 xor dx, dx
10451
10452 mov ch, [2]
10453 shl cx, #1
10454
10455 jnc checksum_loop
10456 jz checksum_loop
10457 xchg dx, cx
10458 dec cx
10459
10460 checksum_loop:
10461 add al, [bx]
10462 inc bx
10463 loop checksum_loop
10464
10465 test dx, dx
10466 je checksum_out
10467
10468 add al, [bx]
10469 mov cx, dx
10470 mov dx, ds
10471 add dh, #0x10
10472 mov ds, dx
10473 xor dx, dx
10474 xor bx, bx
10475
10476 jmp checksum_loop
10477
10478 checksum_out:
10479 and al, #0xff
10480 pop ds
10481 popa
10482 ret
10483
10484
10485 ;; We need a copy of this string, but we are not actually a PnP BIOS,
10486 ;; so make sure it is *not* aligned, so OSes will not see it if they scan.
10487 .align 16
10488 db 0
10489 pnp_string:
10490 .ascii "$PnP"
10491
10492 rom_scan:
10493 ;; Scan for existence of valid expansion ROMS.
10494 ;; Video ROM: from 0xC0000..0xC7FFF in 2k increments
10495 ;; General ROM: from 0xC8000..0xE9FFF in 2k increments
10496 ;; System ROM: only 0xF0000
10497 ;;
10498 ;; Header:
10499 ;; Offset Value
10500 ;; 0 0x55
10501 ;; 1 0xAA
10502 ;; 2 ROM length in 512-byte blocks
10503 ;; 3 ROM initialization entry point (FAR CALL)
10504
10505 #if BX_TCGBIOS
10506 push ax
10507 call _tcpa_start_option_rom_scan /* specs: 3.2.3.3 + 10.4.3 */
10508 pop ax
10509 #endif
10510
10511 rom_scan_loop:
10512 push ax ;; Save AX
10513 mov ds, cx
10514 mov ax, #0x0004 ;; start with increment of 4 (512-byte) blocks = 2k
10515 cmp [0], #0xAA55 ;; look for signature
10516 jne rom_scan_increment
10517 call rom_checksum
10518 jnz rom_scan_increment
10519 mov al, [2] ;; change increment to ROM length in 512-byte blocks
10520
10521 ;; We want our increment in 512-byte quantities, rounded to
10522 ;; the nearest 2k quantity, since we only scan at 2k intervals.
10523 test al, #0x03
10524 jz block_count_rounded
10525 and al, #0xfc ;; needs rounding up
10526 add al, #0x04
10527 block_count_rounded:
10528
10529 #if BX_TCGBIOS
10530 push ax
10531 push ds
10532 push ecx
10533 xor ax, ax
10534 mov ds, ax
10535 and ecx, #0xffff
10536 push ecx ;; segment where option rom is located at
10537 call _tcpa_option_rom /* specs: 3.2.3.3 */
10538 add sp, #4 ;; pop segment
10539 pop ecx ;; original ecx
10540 pop ds
10541 pop ax
10542 #endif
10543 push ax ;; Save AX
10544 push di ;; Save DI
10545 ;; Push addr of ROM entry point
10546 push cx ;; Push seg
10547 push #0x0003 ;; Push offset
10548
10549 ;; Get the BDF into ax before invoking the option ROM
10550 mov bl, [2]
10551 mov al, bl
10552 shr al, #7
10553 cmp al, #1
10554 jne fetch_bdf
10555 mov ax, ds ;; Increment the DS since rom size larger than an segment
10556 add ax, #0x1000
10557 mov ds, ax
10558 fetch_bdf:
10559 shl bx, #9
10560 xor ax, ax
10561 mov al, [bx]
10562
10563 ;; Point ES:DI at "$PnP", which tells the ROM that we are a PnP BIOS.
10564 ;; That should stop it grabbing INT 19h; we will use its BEV instead.
10565 mov bx, #0xf000
10566 mov es, bx
10567 lea di, pnp_string
10568
10569 xor bx, bx ;; Restore DS back to 0000:
10570 mov ds, bx
10571
10572 mov bp, sp ;; Call ROM init routine using seg:off on stack
10573 db 0xff ;; call_far ss:[bp+0]
10574 db 0x5e
10575 db 0
10576 cli ;; In case expansion ROM BIOS turns IF on
10577 add sp, #2 ;; Pop offset value
10578 pop cx ;; Pop seg value (restore CX)
10579
10580 ;; Look at the ROM's PnP Expansion header. Properly, we're supposed
10581 ;; to init all the ROMs and then go back and build an IPL table of
10582 ;; all the bootable devices, but we can get away with one pass.
10583 mov ds, cx ;; ROM base
10584 mov bx, 0x001a ;; 0x1A is the offset into ROM header that contains...
10585 mov ax, [bx] ;; the offset of PnP expansion header, where...
10586 cmp ax, #0x5024 ;; we look for signature "$PnP"
10587 jne no_bev
10588 mov ax, 2[bx]
10589 cmp ax, #0x506e
10590 jne no_bev
10591
10592 mov ax, 0x16[bx] ;; 0x16 is the offset of Boot Connection Vector
10593 cmp ax, #0x0000
10594 je no_bcv
10595
10596 ;; Option ROM has BCV. Run it now.
10597 push cx ;; Push seg
10598 push ax ;; Push offset
10599
10600 ;; Point ES:DI at "$PnP", which tells the ROM that we are a PnP BIOS.
10601 mov bx, #0xf000
10602 mov es, bx
10603 lea di, pnp_string
10604 /* jump to BCV function entry pointer */
10605 mov bp, sp ;; Call ROM BCV routine using seg:off on stack
10606 db 0xff ;; call_far ss:[bp+0]
10607 db 0x5e
10608 db 0
10609 cli ;; In case expansion ROM BIOS turns IF on
10610 add sp, #2 ;; Pop offset value
10611 pop cx ;; Pop seg value (restore CX)
10612 jmp no_bev
10613
10614 no_bcv:
10615 mov ax, 0x1a[bx] ;; 0x1A is also the offset into the expansion header of...
10616 cmp ax, #0x0000 ;; the Bootstrap Entry Vector, or zero if there is none.
10617 je no_bev
10618
10619 ;; Found a device that thinks it can boot the system. Record its BEV and product name string.
10620 mov di, 0x10[bx] ;; Pointer to the product name string or zero if none
10621 xor bx, bx
10622 mov ds, bx
10623 mov bx, word ptr [0x40E] ;; EBDA segment
10624 mov ds, bx ;; Go to the segment where the IPL table lives
10625 mov bx, IPL_COUNT_OFFSET ;; Read the number of entries so far
10626 cmp bx, #IPL_TABLE_ENTRIES
10627 je no_bev ;; Get out if the table is full
10628 shl bx, #0x4 ;; Turn count into offset (entries are 16 bytes)
10629 mov IPL_TABLE_OFFSET+0[bx], #IPL_TYPE_BEV ;; This entry is a BEV device
10630 mov IPL_TABLE_OFFSET+6[bx], cx ;; Build a far pointer from the segment...
10631 mov IPL_TABLE_OFFSET+4[bx], ax ;; and the offset
10632 cmp di, #0x0000
10633 je no_prod_str
10634 mov 0xA[bx], cx ;; Build a far pointer from the segment...
10635 mov 8[bx], di ;; and the offset
10636 no_prod_str:
10637 shr bx, #0x4 ;; Turn the offset back into a count
10638 inc bx ;; We have one more entry now
10639 mov IPL_COUNT_OFFSET, bx ;; Remember that.
10640
10641 no_bev:
10642 pop di ;; Restore DI
10643 pop ax ;; Restore AX
10644 rom_scan_increment:
10645 shl ax, #5 ;; convert 512-bytes blocks to 16-byte increments
10646 ;; because the segment selector is shifted left 4 bits.
10647 add cx, ax
10648 pop ax ;; Restore AX
10649 cmp cx, ax
10650 jbe rom_scan_loop
10651
10652 xor ax, ax ;; Restore DS back to 0000:
10653 mov ds, ax
10654 ret
10655
10656 #ifdef HVMASSIST
10657
10658 ; Copy the SMBIOS entry point from where hvmloader left it.
10659 ; The entry point must be somewhere in 0xf0000-0xfffff on a 16-byte boundary,
10660 ; but the tables themselves can be elsewhere.
10661 smbios_init:
10662 push ax
10663 push cx
10664 push es
10665 push ds
10666 push di
10667 push si
10668
10669 mov cx, #0x001f ; 0x1f bytes to copy
10670 mov ax, #0xf000
10671 mov es, ax ; destination segment is 0xf0000
10672 mov di, #smbios_entry_point ; destination offset
10673 mov ax, #(SMBIOS_PHYSICAL_ADDRESS>>4)
10674 mov ds, ax
10675 mov si, #(SMBIOS_PHYSICAL_ADDRESS&15)
10676 cld
10677 rep
10678 movsb
10679
10680 pop si
10681 pop di
10682 pop ds
10683 pop es
10684 pop cx
10685 pop ax
10686
10687 ret
10688
10689 #endif
10690
10691 #if BX_TCGBIOS
10692 ; The section between the POST entry and the NMI entry is filling up
10693 ; and causes crashes if this code was directly there
10694 tcpa_post_part1:
10695 call _tcpa_acpi_init
10696
10697 push dword #0
10698 call _tcpa_initialize_tpm
10699 add sp, #4
10700
10701 call _tcpa_do_measure_POSTs
10702 call _tcpa_wake_event /* specs: 3.2.3.7 */
10703 ret
10704
10705 tcpa_post_part2:
10706 call _tcpa_calling_int19h /* specs: 8.2.3 step 1 */
10707 call _tcpa_add_event_separators /* specs: 8.2.3 step 2 */
10708 /* we do not call int 19h handler but keep following eventlog */
10709 call _tcpa_returned_int19h /* specs: 8.2.3 step 3/7 */
10710 ret
10711 #endif
10712
10713
10714 post_init_pic:
10715 mov al, #0x11 ; send initialisation commands
10716 out 0x20, al
10717 out 0xa0, al
10718 mov al, #0x08
10719 out 0x21, al
10720 mov al, #0x70
10721 out 0xa1, al
10722 mov al, #0x04
10723 out 0x21, al
10724 mov al, #0x02
10725 out 0xa1, al
10726 mov al, #0x01
10727 out 0x21, al
10728 out 0xa1, al
10729 mov al, #0xb8
10730 out 0x21, AL ;master pic: unmask IRQ 0, 1, 2, 6
10731 #if BX_USE_PS2_MOUSE
10732 mov al, #0x8f
10733 #else
10734 mov al, #0x9f
10735 #endif
10736 out 0xa1, AL ;slave pic: unmask IRQ 12, 13, 14
10737 ret
10738
10739
10740 .align 16
10741 smbios_entry_point:
10742 db 0,0,0,0,0,0,0,0 ; 8 bytes
10743 db 0,0,0,0,0,0,0,0 ; 16 bytes
10744 db 0,0,0,0,0,0,0,0 ; 24 bytes
10745 db 0,0,0,0,0,0,0 ; 31 bytes
10746
10747 ;; the following area can be used to write dynamically generated tables
10748 .align 16
10749 bios_table_area_start:
10750 db 0x5F, 0x5F, 0x5F, 0x48, 0x56, 0x4D, 0x4D, 0x50 ;; ___HVMMP
10751 dd bios_table_area_end - bios_table_area_start
10752
10753 ;--------
10754 ;- POST -
10755 ;--------
10756 .org 0xe05b ; POST Entry Point
10757 post:
10758
10759 xor ax, ax
10760
10761 ;; first reset the DMA controllers
10762 out 0x0d,al
10763 out 0xda,al
10764
10765 ;; then initialize the DMA controllers
10766 mov al, #0xC0
10767 out 0xD6, al ; cascade mode of channel 4 enabled
10768 mov al, #0x00
10769 out 0xD4, al ; unmask channel 4
10770
10771 ;; Examine CMOS shutdown status.
10772 mov AL, #0x0f
10773 out 0x70, AL
10774 in AL, 0x71
10775
10776 ;; backup status
10777 mov bl, al
10778
10779 ;; Reset CMOS shutdown status.
10780 mov AL, #0x0f
10781 out 0x70, AL ; select CMOS register Fh
10782 mov AL, #0x00
10783 out 0x71, AL ; set shutdown action to normal
10784
10785 ;; Examine CMOS shutdown status.
10786 mov al, bl
10787
10788 ;; 0x00, 0x09, 0x0D+ = normal startup
10789 cmp AL, #0x00
10790 jz normal_post
10791 cmp AL, #0x0d
10792 jae normal_post
10793 cmp AL, #0x09
10794 je normal_post
10795
10796 ;; 0x05 = eoi + jmp via [0x40:0x67] jump
10797 cmp al, #0x05
10798 je eoi_jmp_post
10799
10800 ;; 0x0A = jmp via [0x40:0x67] jump
10801 cmp al, #0x0a
10802 je jmp_post_0x467
10803
10804 ;; 0x0B = iret via [0x40:0x67]
10805 cmp al, #0x0b
10806 je iret_post_0x467
10807
10808 ;; 0x0C = retf via [0x40:0x67]
10809 cmp al, #0x0c
10810 je retf_post_0x467
10811
10812 ;; Examine CMOS shutdown status.
10813 ;; 0x01,0x02,0x03,0x04,0x06,0x07,0x08 = Unimplemented shutdown status.
10814 push bx
10815 call _shutdown_status_panic
10816
10817 #if 0
10818 HALT(__LINE__)
10819 ;
10820 ;#if 0
10821 ; 0xb0, 0x20, /* mov al, #0x20 */
10822 ; 0xe6, 0x20, /* out 0x20, al ;send EOI to PIC */
10823 ;#endif
10824 ;
10825 pop es
10826 pop ds
10827 popa
10828 iret
10829 #endif
10830
10831 normal_post:
10832 ; case 0: normal startup
10833
10834 cli
10835 mov ax, #0x0ffe
10836 mov sp, ax
10837 mov ax, #0x9e00
10838 mov ss, ax
10839
10840 ;; Save shutdown status
10841 mov 0x04b0, bl
10842
10843 cmp bl, #0xfe
10844 jz s3_post
10845
10846 ;; zero out BIOS data area (40:00..40:ff)
10847 mov es, ax
10848 mov cx, #0x0080 ;; 128 words
10849 mov di, #0x0400
10850 cld
10851 rep
10852 stosw
10853
10854 call _log_bios_start
10855
10856 ;; set all interrupts to default handler
10857 xor bx, bx ;; offset index
10858 mov cx, #0x0100 ;; counter (256 interrupts)
10859 mov ax, #dummy_iret_handler
10860 mov dx, #0xF000
10861
10862 post_default_ints:
10863 mov [bx], ax
10864 add bx, #2
10865 mov [bx], dx
10866 add bx, #2
10867 loop post_default_ints
10868
10869 ;; set vector 0x79 to zero
10870 ;; this is used by 'gardian angel' protection system
10871 SET_INT_VECTOR(0x79, #0, #0)
10872
10873 ;; base memory in K 40:13 (word)
10874 mov ax, #BASE_MEM_IN_K
10875 mov 0x0413, ax
10876
10877 ;; Manufacturing Test 40:12
10878 ;; zerod out above
10879
10880 ;; Warm Boot Flag 0040:0072
10881 ;; value of 1234h = skip memory checks
10882 ;; zerod out above
10883
10884
10885 ;; Printer Services vector
10886 SET_INT_VECTOR(0x17, #0xF000, #int17_handler)
10887
10888 ;; Bootstrap failure vector
10889 SET_INT_VECTOR(0x18, #0xF000, #int18_handler)
10890
10891 ;; Bootstrap Loader vector
10892 SET_INT_VECTOR(0x19, #0xF000, #int19_handler)
10893
10894 ;; User Timer Tick vector
10895 SET_INT_VECTOR(0x1c, #0xF000, #int1c_handler)
10896
10897 ;; Memory Size Check vector
10898 SET_INT_VECTOR(0x12, #0xF000, #int12_handler)
10899
10900 ;; Equipment Configuration Check vector
10901 SET_INT_VECTOR(0x11, #0xF000, #int11_handler)
10902
10903 ;; System Services
10904 SET_INT_VECTOR(0x15, #0xF000, #int15_handler)
10905
10906 ;; EBDA setup
10907 call ebda_post
10908
10909 ;; PIT setup
10910 SET_INT_VECTOR(0x08, #0xF000, #int08_handler)
10911 ;; int 1C already points at dummy_iret_handler (above)
10912 mov al, #0x34 ; timer0: binary count, 16bit count, mode 2
10913 out 0x43, al
10914 #ifdef HVMASSIST
10915 mov al, #0x0b ; #0xe90b = 20 Hz (temporary, until we fix xen/vmx support)
10916 out 0x40, al ; lsb
10917 mov al, #0xe9
10918 out 0x40, al ; msb
10919 #else
10920 mov al, #0x00 ; maximum count of 0000H = 18.2Hz
10921 out 0x40, al
10922 out 0x40, al
10923 #endif
10924
10925 ;; Keyboard
10926 SET_INT_VECTOR(0x09, #0xF000, #int09_handler)
10927 SET_INT_VECTOR(0x16, #0xF000, #int16_handler)
10928
10929 xor ax, ax
10930 mov ds, ax
10931 mov 0x0417, al /* keyboard shift flags, set 1 */
10932 mov 0x0418, al /* keyboard shift flags, set 2 */
10933 mov 0x0419, al /* keyboard alt-numpad work area */
10934 mov 0x0471, al /* keyboard ctrl-break flag */
10935 mov 0x0497, al /* keyboard status flags 4 */
10936 mov al, #0x10
10937 mov 0x0496, al /* keyboard status flags 3 */
10938
10939
10940 /* keyboard head of buffer pointer */
10941 mov bx, #0x001E
10942 mov 0x041A, bx
10943
10944 /* keyboard end of buffer pointer */
10945 mov 0x041C, bx
10946
10947 /* keyboard pointer to start of buffer */
10948 mov bx, #0x001E
10949 mov 0x0480, bx
10950
10951 /* keyboard pointer to end of buffer */
10952 mov bx, #0x003E
10953 mov 0x0482, bx
10954
10955 /* init the keyboard */
10956 call _keyboard_init
10957
10958 ;; mov CMOS Equipment Byte to BDA Equipment Word
10959 mov ax, 0x0410
10960 mov al, #0x14
10961 out 0x70, al
10962 in al, 0x71
10963 mov 0x0410, ax
10964
10965 #if BX_TCGBIOS
10966 call tcpa_post_part1
10967 #endif
10968
10969 ;; Parallel setup
10970 SET_INT_VECTOR(0x0F, #0xF000, #dummy_iret_handler)
10971 xor ax, ax
10972 mov ds, ax
10973 xor bx, bx
10974 mov cl, #0x14 ; timeout value
10975 mov dx, #0x378 ; Parallel I/O address, port 1
10976 call detect_parport
10977 mov dx, #0x278 ; Parallel I/O address, port 2
10978 call detect_parport
10979 shl bx, #0x0e
10980 mov ax, 0x410 ; Equipment word bits 14..15 determing # parallel ports
10981 and ax, #0x3fff
10982 or ax, bx ; set number of parallel ports
10983 mov 0x410, ax
10984
10985 ;; Serial setup
10986 SET_INT_VECTOR(0x0C, #0xF000, #dummy_iret_handler)
10987 SET_INT_VECTOR(0x14, #0xF000, #int14_handler)
10988 xor bx, bx
10989 mov cl, #0x0a ; timeout value
10990 mov dx, #0x03f8 ; Serial I/O address, port 1
10991 call detect_serial
10992 mov dx, #0x02f8 ; Serial I/O address, port 2
10993 call detect_serial
10994 mov dx, #0x03e8 ; Serial I/O address, port 3
10995 call detect_serial
10996 mov dx, #0x02e8 ; Serial I/O address, port 4
10997 call detect_serial
10998 shl bx, #0x09
10999 mov ax, 0x410 ; Equipment word bits 9..11 determing # serial ports
11000 and ax, #0xf1ff
11001 or ax, bx ; set number of serial port
11002 mov 0x410, ax
11003
11004 ;; CMOS RTC
11005 SET_INT_VECTOR(0x1A, #0xF000, #int1a_handler)
11006 SET_INT_VECTOR(0x4A, #0xF000, #dummy_iret_handler)
11007 SET_INT_VECTOR(0x70, #0xF000, #int70_handler)
11008 ;; BIOS DATA AREA 0x4CE ???
11009 call timer_tick_post
11010
11011 ;; PS/2 mouse setup
11012 SET_INT_VECTOR(0x74, #0xF000, #int74_handler)
11013
11014 ;; IRQ13 (FPU exception) setup
11015 SET_INT_VECTOR(0x75, #0xF000, #int75_handler)
11016
11017 ;; Video setup
11018 SET_INT_VECTOR(0x10, #0xF000, #int10_handler)
11019
11020 ;; PIC
11021 call post_init_pic
11022
11023 mov cx, #0xc000 ;; init vga bios
11024 mov ax, #0xc780
11025 call rom_scan
11026
11027 call _print_bios_banner
11028
11029 #if BX_ROMBIOS32
11030 call rombios32_init
11031 #else
11032 #if BX_PCIBIOS && !defined(HVMASSIST)
11033 call pcibios_init_iomem_bases
11034 call pcibios_init_irqs
11035 #endif //BX_PCIBIOS
11036 #endif
11037
11038 ;;
11039 ;; Floppy setup
11040 ;;
11041 call floppy_drive_post
11042
11043 ;;
11044 ;; Hard Drive setup
11045 ;;
11046 call hard_drive_post
11047
11048 #if BX_USE_ATADRV
11049
11050 ;;
11051 ;; ATA/ATAPI driver setup
11052 ;;
11053 call _ata_init
11054 call _ata_detect
11055 ;;
11056
11057 #endif // BX_USE_ATADRV
11058
11059 #if BX_ELTORITO_BOOT
11060 ;;
11061 ;; eltorito floppy/harddisk emulation from cd
11062 ;;
11063 call _cdemu_init
11064 ;;
11065 #endif // BX_ELTORITO_BOOT
11066
11067 #ifdef HVMASSIST
11068 call _enable_rom_write_access
11069 call _clobber_entry_point
11070 call _fixup_base_mem_in_k
11071 call smbios_init
11072 #endif
11073
11074 call _init_boot_vectors
11075
11076 mov cx, #(OPTIONROM_PHYSICAL_ADDRESS >> 4) ;; init option roms
11077 mov ax, #(OPTIONROM_PHYSICAL_END >> 4)
11078 call rom_scan
11079
11080 #ifdef HVMASSIST
11081 call _disable_rom_write_access
11082 #endif
11083
11084 #if BX_ELTORITO_BOOT
11085 call _interactive_bootkey
11086 #endif // BX_ELTORITO_BOOT
11087
11088 #if BX_TCGBIOS
11089 call tcpa_post_part2
11090 #endif
11091
11092 sti ;; enable interrupts
11093 ;; Start the boot sequence. See the comments in int19_relocated
11094 ;; for why we use INT 18h instead of INT 19h here.
11095 int #0x18
11096
11097 .org 0xe2c3 ; NMI Handler Entry Point
11098 nmi:
11099 ;; FIXME the NMI handler should not panic
11100 ;; but iret when called from int75 (fpu exception)
11101 call _nmi_handler_msg
11102 iret
11103
11104 int75_handler:
11105 out 0xf0, al // clear irq13
11106 call eoi_both_pics // clear interrupt
11107 int 2 // legacy nmi call
11108 iret
11109
11110 ;-------------------------------------------
11111 ;- INT 13h Fixed Disk Services Entry Point -
11112 ;-------------------------------------------
11113 .org 0xe3fe ; INT 13h Fixed Disk Services Entry Point
11114 int13_handler:
11115 //JMPL(int13_relocated)
11116 jmp int13_relocated
11117
11118 .org 0xe401 ; Fixed Disk Parameter Table
11119
11120 ;----------
11121 ;- INT19h -
11122 ;----------
11123 .org 0xe6f2 ; INT 19h Boot Load Service Entry Point
11124 int19_handler:
11125
11126 jmp int19_relocated
11127 ;-------------------------------------------
11128 ;- System BIOS Configuration Data Table
11129 ;-------------------------------------------
11130 .org BIOS_CONFIG_TABLE
11131 db 0x08 ; Table size (bytes) -Lo
11132 db 0x00 ; Table size (bytes) -Hi
11133 db SYS_MODEL_ID
11134 db SYS_SUBMODEL_ID
11135 db BIOS_REVISION
11136 ; Feature byte 1
11137 ; b7: 1=DMA channel 3 used by hard disk
11138 ; b6: 1=2 interrupt controllers present
11139 ; b5: 1=RTC present
11140 ; b4: 1=BIOS calls int 15h/4Fh every key
11141 ; b3: 1=wait for extern event supported (Int 15h/41h)
11142 ; b2: 1=extended BIOS data area used
11143 ; b1: 0=AT or ESDI bus, 1=MicroChannel
11144 ; b0: 1=Dual bus (MicroChannel + ISA)
11145 db (0 << 7) | \
11146 (1 << 6) | \
11147 (1 << 5) | \
11148 (BX_CALL_INT15_4F << 4) | \
11149 (0 << 3) | \
11150 (BX_USE_EBDA << 2) | \
11151 (0 << 1) | \
11152 (0 << 0)
11153 ; Feature byte 2
11154 ; b7: 1=32-bit DMA supported
11155 ; b6: 1=int16h, function 9 supported
11156 ; b5: 1=int15h/C6h (get POS data) supported
11157 ; b4: 1=int15h/C7h (get mem map info) supported
11158 ; b3: 1=int15h/C8h (en/dis CPU) supported
11159 ; b2: 1=non-8042 kb controller
11160 ; b1: 1=data streaming supported
11161 ; b0: reserved
11162 db (0 << 7) | \
11163 (1 << 6) | \
11164 (0 << 5) | \
11165 (0 << 4) | \
11166 (0 << 3) | \
11167 (0 << 2) | \
11168 (0 << 1) | \
11169 (0 << 0)
11170 ; Feature byte 3
11171 ; b7: not used
11172 ; b6: reserved
11173 ; b5: reserved
11174 ; b4: POST supports ROM-to-RAM enable/disable
11175 ; b3: SCSI on system board
11176 ; b2: info panel installed
11177 ; b1: Initial Machine Load (IML) system - BIOS on disk
11178 ; b0: SCSI supported in IML
11179 db 0x00
11180 ; Feature byte 4
11181 ; b7: IBM private
11182 ; b6: EEPROM present
11183 ; b5-3: ABIOS presence (011 = not supported)
11184 ; b2: private
11185 ; b1: memory split above 16Mb supported
11186 ; b0: POSTEXT directly supported by POST
11187 db 0x00
11188 ; Feature byte 5 (IBM)
11189 ; b1: enhanced mouse
11190 ; b0: flash EPROM
11191 db 0x00
11192
11193
11194
11195 .org 0xe729 ; Baud Rate Generator Table
11196
11197 ;----------
11198 ;- INT14h -
11199 ;----------
11200 .org 0xe739 ; INT 14h Serial Communications Service Entry Point
11201 int14_handler:
11202 push ds
11203 pusha
11204 xor ax, ax
11205 mov ds, ax
11206 call _int14_function
11207 popa
11208 pop ds
11209 iret
11210
11211
11212 ;----------------------------------------
11213 ;- INT 16h Keyboard Service Entry Point -
11214 ;----------------------------------------
11215 .org 0xe82e
11216 int16_handler:
11217
11218 sti
11219 push ds
11220 pushf
11221 pusha
11222
11223 cmp ah, #0x00
11224 je int16_F00
11225 cmp ah, #0x10
11226 je int16_F00
11227
11228 mov bx, #0xf000
11229 mov ds, bx
11230 call _int16_function
11231 popa
11232 popf
11233 pop ds
11234 jz int16_zero_set
11235
11236 int16_zero_clear:
11237 push bp
11238 mov bp, sp
11239 //SEG SS
11240 and BYTE [bp + 0x06], #0xbf
11241 pop bp
11242 iret
11243
11244 int16_zero_set:
11245 push bp
11246 mov bp, sp
11247 //SEG SS
11248 or BYTE [bp + 0x06], #0x40
11249 pop bp
11250 iret
11251
11252 int16_F00:
11253 mov bx, #0x0040
11254 mov ds, bx
11255
11256 int16_wait_for_key:
11257 cli
11258 mov bx, 0x001a
11259 cmp bx, 0x001c
11260 jne int16_key_found
11261 sti
11262 hlt
11263 #if 0
11264 /* no key yet, call int 15h, function AX=9002 */
11265 0x50, /* push AX */
11266 0xb8, 0x02, 0x90, /* mov AX, #0x9002 */
11267 0xcd, 0x15, /* int 15h */
11268 0x58, /* pop AX */
11269 0xeb, 0xea, /* jmp WAIT_FOR_KEY */
11270 #endif
11271 jmp int16_wait_for_key
11272
11273 int16_key_found:
11274 mov bx, #0xf000
11275 mov ds, bx
11276 call _int16_function
11277 popa
11278 popf
11279 pop ds
11280 #if 0
11281 /* notify int16 complete w/ int 15h, function AX=9102 */
11282 0x50, /* push AX */
11283 0xb8, 0x02, 0x91, /* mov AX, #0x9102 */
11284 0xcd, 0x15, /* int 15h */
11285 0x58, /* pop AX */
11286 #endif
11287 iret
11288
11289
11290
11291 ;-------------------------------------------------
11292 ;- INT09h : Keyboard Hardware Service Entry Point -
11293 ;-------------------------------------------------
11294 .org 0xe987
11295 int09_handler:
11296 cli
11297 push ax
11298
11299 mov al, #0xAD ;;disable keyboard
11300 out #0x64, al
11301
11302 mov al, #0x0B
11303 out #0x20, al
11304 in al, #0x20
11305 and al, #0x02
11306 jz int09_finish
11307
11308 in al, #0x60 ;;read key from keyboard controller
11309 sti
11310 push ds
11311 pusha
11312 #ifdef BX_CALL_INT15_4F
11313 mov ah, #0x4f ;; allow for keyboard intercept
11314 stc
11315 int #0x15
11316 jnc int09_done
11317 #endif
11318
11319 ;; check for extended key
11320 cmp al, #0xe0
11321 jne int09_check_pause
11322 xor ax, ax
11323 mov ds, ax
11324 mov al, BYTE [0x496] ;; mf2_state |= 0x02
11325 or al, #0x02
11326 mov BYTE [0x496], al
11327 jmp int09_done
11328
11329 int09_check_pause: ;; check for pause key
11330 cmp al, #0xe1
11331 jne int09_process_key
11332 xor ax, ax
11333 mov ds, ax
11334 mov al, BYTE [0x496] ;; mf2_state |= 0x01
11335 or al, #0x01
11336 mov BYTE [0x496], al
11337 jmp int09_done
11338
11339 int09_process_key:
11340 mov bx, #0xf000
11341 mov ds, bx
11342 call _int09_function
11343
11344 int09_done:
11345 popa
11346 pop ds
11347 cli
11348 call eoi_master_pic
11349
11350 int09_finish:
11351 mov al, #0xAE ;;enable keyboard
11352 out #0x64, al
11353 pop ax
11354 iret
11355
11356
11357 ;----------------------------------------
11358 ;- INT 13h Diskette Service Entry Point -
11359 ;----------------------------------------
11360 .org 0xec59
11361 int13_diskette:
11362 jmp int13_noeltorito
11363
11364 ;---------------------------------------------
11365 ;- INT 0Eh Diskette Hardware ISR Entry Point -
11366 ;---------------------------------------------
11367 .org 0xef57 ; INT 0Eh Diskette Hardware ISR Entry Point
11368 int0e_handler:
11369 push ax
11370 push dx
11371 mov dx, #0x03f4
11372 in al, dx
11373 and al, #0xc0
11374 cmp al, #0xc0
11375 je int0e_normal
11376 mov dx, #0x03f5
11377 mov al, #0x08 ; sense interrupt status
11378 out dx, al
11379 int0e_loop1:
11380 mov dx, #0x03f4
11381 in al, dx
11382 and al, #0xc0
11383 cmp al, #0xc0
11384 jne int0e_loop1
11385 int0e_loop2:
11386 mov dx, #0x03f5
11387 in al, dx
11388 mov dx, #0x03f4
11389 in al, dx
11390 and al, #0xc0
11391 cmp al, #0xc0
11392 je int0e_loop2
11393 int0e_normal:
11394 push ds
11395 xor ax, ax ;; segment 0000
11396 mov ds, ax
11397 call eoi_master_pic
11398 mov al, 0x043e
11399 or al, #0x80 ;; diskette interrupt has occurred
11400 mov 0x043e, al
11401 pop ds
11402 pop dx
11403 pop ax
11404 iret
11405
11406
11407 .org 0xefc7 ; Diskette Controller Parameter Table
11408 diskette_param_table:
11409 ;; Since no provisions are made for multiple drive types, most
11410 ;; values in this table are ignored. I set parameters for 1.44M
11411 ;; floppy here
11412 db 0xAF
11413 db 0x02 ;; head load time 0000001, DMA used
11414 db 0x25
11415 db 0x02
11416 db 18
11417 db 0x1B
11418 db 0xFF
11419 db 0x6C
11420 db 0xF6
11421 db 0x0F
11422 db 0x08
11423
11424
11425 ;----------------------------------------
11426 ;- INT17h : Printer Service Entry Point -
11427 ;----------------------------------------
11428 .org 0xefd2
11429 int17_handler:
11430 push ds
11431 pusha
11432 xor ax, ax
11433 mov ds, ax
11434 call _int17_function
11435 popa
11436 pop ds
11437 iret
11438
11439 diskette_param_table2:
11440 ;; New diskette parameter table adding 3 parameters from IBM
11441 ;; Since no provisions are made for multiple drive types, most
11442 ;; values in this table are ignored. I set parameters for 1.44M
11443 ;; floppy here
11444 db 0xAF
11445 db 0x02 ;; head load time 0000001, DMA used
11446 db 0x25
11447 db 0x02
11448 db 18
11449 db 0x1B
11450 db 0xFF
11451 db 0x6C
11452 db 0xF6
11453 db 0x0F
11454 db 0x08
11455 db 79 ;; maximum track
11456 db 0 ;; data transfer rate
11457 db 4 ;; drive type in cmos
11458
11459 .org 0xf045 ; INT 10 Functions 0-Fh Entry Point
11460 HALT(__LINE__)
11461 iret
11462
11463 ;----------
11464 ;- INT10h -
11465 ;----------
11466 .org 0xf065 ; INT 10h Video Support Service Entry Point
11467 int10_handler:
11468 ;; dont do anything, since the VGA BIOS handles int10h requests
11469 iret
11470
11471 .org 0xf0a4 ; MDA/CGA Video Parameter Table (INT 1Dh)
11472
11473 ;----------
11474 ;- INT12h -
11475 ;----------
11476 .org 0xf841 ; INT 12h Memory Size Service Entry Point
11477 ; ??? different for Pentium (machine check)?
11478 int12_handler:
11479 push ds
11480 mov ax, #0x0040
11481 mov ds, ax
11482 mov ax, 0x0013
11483 pop ds
11484 iret
11485
11486 ;----------
11487 ;- INT11h -
11488 ;----------
11489 .org 0xf84d ; INT 11h Equipment List Service Entry Point
11490 int11_handler:
11491 push ds
11492 mov ax, #0x0040
11493 mov ds, ax
11494 mov ax, 0x0010
11495 pop ds
11496 iret
11497
11498 ;----------
11499 ;- INT15h -
11500 ;----------
11501 .org 0xf859 ; INT 15h System Services Entry Point
11502 int15_handler:
11503 pushf
11504 #if BX_APM
11505 cmp ah, #0x53
11506 je apm_call
11507 #endif
11508 push ds
11509 push es
11510 cmp ah, #0x86
11511 je int15_handler32
11512 cmp ah, #0xE8
11513 je int15_handler32
11514 pusha
11515 #if BX_USE_PS2_MOUSE
11516 cmp ah, #0xC2
11517 je int15_handler_mouse
11518 #endif
11519 call _int15_function
11520 int15_handler_mouse_ret:
11521 popa
11522 int15_handler32_ret:
11523 pop es
11524 pop ds
11525 popf
11526 jmp iret_modify_cf
11527 #if BX_APM
11528 apm_call:
11529 jmp _apmreal_entry
11530 #endif
11531
11532 #if BX_USE_PS2_MOUSE
11533 int15_handler_mouse:
11534 call _int15_function_mouse
11535 jmp int15_handler_mouse_ret
11536 #endif
11537
11538 int15_handler32:
11539 pushad
11540 call _int15_function32
11541 popad
11542 jmp int15_handler32_ret
11543
11544 ;; Protected mode IDT descriptor
11545 ;;
11546 ;; I just make the limit 0, so the machine will shutdown
11547 ;; if an exception occurs during protected mode memory
11548 ;; transfers.
11549 ;;
11550 ;; Set base to f0000 to correspond to beginning of BIOS,
11551 ;; in case I actually define an IDT later
11552 ;; Set limit to 0
11553
11554 pmode_IDT_info:
11555 dw 0x0000 ;; limit 15:00
11556 dw 0x0000 ;; base 15:00
11557 db 0x0f ;; base 23:16
11558
11559 ;; Real mode IDT descriptor
11560 ;;
11561 ;; Set to typical real-mode values.
11562 ;; base = 000000
11563 ;; limit = 03ff
11564
11565 rmode_IDT_info:
11566 dw 0x03ff ;; limit 15:00
11567 dw 0x0000 ;; base 15:00
11568 db 0x00 ;; base 23:16
11569
11570
11571 ;----------
11572 ;- INT1Ah -
11573 ;----------
11574 .org 0xfe6e ; INT 1Ah Time-of-day Service Entry Point
11575 int1a_handler:
11576 #if BX_TCGBIOS
11577 cmp ah, #0xbb
11578 jne no_tcg
11579 pushf
11580 push ds
11581 push es
11582 pushad
11583 call _int1a_function32
11584 popad
11585 pop es
11586 pop ds
11587 popf
11588 iret
11589 no_tcg:
11590 #endif
11591 #if BX_PCIBIOS
11592 cmp ah, #0xb1
11593 jne int1a_normal
11594 call pcibios_real
11595 jc pcibios_error
11596 retf 2
11597 pcibios_error:
11598 mov bl, ah
11599 mov ah, #0xb1
11600 push ds
11601 pusha
11602 mov ax, ss ; set readable descriptor to ds, for calling pcibios
11603 mov ds, ax ; on 16bit protected mode.
11604 jmp int1a_callfunction
11605 int1a_normal:
11606 #endif
11607 push ds
11608 pusha
11609 xor ax, ax
11610 mov ds, ax
11611 int1a_callfunction:
11612 call _int1a_function
11613 popa
11614 pop ds
11615 iret
11616
11617 ;;
11618 ;; int70h: IRQ8 - CMOS RTC
11619 ;;
11620 int70_handler:
11621 push ds
11622 pushad
11623 xor ax, ax
11624 mov ds, ax
11625 call _int70_function
11626 popad
11627 pop ds
11628 iret
11629
11630 ;---------
11631 ;- INT08 -
11632 ;---------
11633 .org 0xfea5 ; INT 08h System Timer ISR Entry Point
11634 int08_handler:
11635 sti
11636 push eax
11637 push ds
11638 xor ax, ax
11639 mov ds, ax
11640
11641 ;; time to turn off drive(s)?
11642 mov al,0x0440
11643 or al,al
11644 jz int08_floppy_off
11645 dec al
11646 mov 0x0440,al
11647 jnz int08_floppy_off
11648 ;; turn motor(s) off
11649 push dx
11650 mov dx,#0x03f2
11651 in al,dx
11652 and al,#0xcf
11653 out dx,al
11654 pop dx
11655 int08_floppy_off:
11656
11657 mov eax, 0x046c ;; get ticks dword
11658 inc eax
11659
11660 ;; compare eax to one days worth of timer ticks at 18.2 hz
11661 cmp eax, #0x001800B0
11662 jb int08_store_ticks
11663 ;; there has been a midnight rollover at this point
11664 xor eax, eax ;; zero out counter
11665 inc BYTE 0x0470 ;; increment rollover flag
11666
11667 int08_store_ticks:
11668 mov 0x046c, eax ;; store new ticks dword
11669 ;; chain to user timer tick INT #0x1c
11670 //pushf
11671 //;; call_ep [ds:loc]
11672 //CALL_EP( 0x1c << 2 )
11673 int #0x1c
11674 cli
11675 call eoi_master_pic
11676 pop ds
11677 pop eax
11678 iret
11679
11680 .org 0xfef3 ; Initial Interrupt Vector Offsets Loaded by POST
11681
11682
11683 .org 0xff00
11684 .ascii BIOS_COPYRIGHT_STRING
11685
11686 ;------------------------------------------------
11687 ;- IRET Instruction for Dummy Interrupt Handler -
11688 ;------------------------------------------------
11689 .org 0xff53 ; IRET Instruction for Dummy Interrupt Handler
11690 dummy_iret_handler:
11691 iret
11692
11693 .org 0xff54 ; INT 05h Print Screen Service Entry Point
11694 HALT(__LINE__)
11695 iret
11696
11697 #ifdef HVMTEST
11698 .org 0xffe0
11699 jmp 0xf000:post;
11700 #endif
11701
11702 .org 0xfff0 ; Power-up Entry Point
11703 #ifdef HVMTEST
11704 jmp 0xd000:0x0003;
11705 #else
11706 jmp 0xf000:post
11707 #endif
11708
11709 .org 0xfff5 ; ASCII Date ROM was built - 8 characters in MM/DD/YY
11710 .ascii BIOS_BUILD_DATE
11711
11712 .org 0xfffe ; System Model ID
11713 db SYS_MODEL_ID
11714 db 0x00 ; filler
11715
11716 .org 0xfa6e ;; Character Font for 320x200 & 640x200 Graphics (lower 128 characters)
11717 ASM_END
11718 /*
11719 * This font comes from the fntcol16.zip package (c) by Joseph Gil
11720 * found at ftp://ftp.simtel.net/pub/simtelnet/msdos/screen/fntcol16.zip
11721 * This font is public domain
11722 */
11723 static Bit8u vgafont8[128*8]=
11724 {
11725 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11726 0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e,
11727 0x7e, 0xff, 0xdb, 0xff, 0xc3, 0xe7, 0xff, 0x7e,
11728 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00,
11729 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00,
11730 0x38, 0x7c, 0x38, 0xfe, 0xfe, 0x7c, 0x38, 0x7c,
11731 0x10, 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x7c,
11732 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x18, 0x00, 0x00,
11733 0xff, 0xff, 0xe7, 0xc3, 0xc3, 0xe7, 0xff, 0xff,
11734 0x00, 0x3c, 0x66, 0x42, 0x42, 0x66, 0x3c, 0x00,
11735 0xff, 0xc3, 0x99, 0xbd, 0xbd, 0x99, 0xc3, 0xff,
11736 0x0f, 0x07, 0x0f, 0x7d, 0xcc, 0xcc, 0xcc, 0x78,
11737 0x3c, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x7e, 0x18,
11738 0x3f, 0x33, 0x3f, 0x30, 0x30, 0x70, 0xf0, 0xe0,
11739 0x7f, 0x63, 0x7f, 0x63, 0x63, 0x67, 0xe6, 0xc0,
11740 0x99, 0x5a, 0x3c, 0xe7, 0xe7, 0x3c, 0x5a, 0x99,
11741 0x80, 0xe0, 0xf8, 0xfe, 0xf8, 0xe0, 0x80, 0x00,
11742 0x02, 0x0e, 0x3e, 0xfe, 0x3e, 0x0e, 0x02, 0x00,
11743 0x18, 0x3c, 0x7e, 0x18, 0x18, 0x7e, 0x3c, 0x18,
11744 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00,
11745 0x7f, 0xdb, 0xdb, 0x7b, 0x1b, 0x1b, 0x1b, 0x00,
11746 0x3e, 0x63, 0x38, 0x6c, 0x6c, 0x38, 0xcc, 0x78,
11747 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x7e, 0x00,
11748 0x18, 0x3c, 0x7e, 0x18, 0x7e, 0x3c, 0x18, 0xff,
11749 0x18, 0x3c, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x00,
11750 0x18, 0x18, 0x18, 0x18, 0x7e, 0x3c, 0x18, 0x00,
11751 0x00, 0x18, 0x0c, 0xfe, 0x0c, 0x18, 0x00, 0x00,
11752 0x00, 0x30, 0x60, 0xfe, 0x60, 0x30, 0x00, 0x00,
11753 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xfe, 0x00, 0x00,
11754 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00,
11755 0x00, 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x00, 0x00,
11756 0x00, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00,
11757 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11758 0x30, 0x78, 0x78, 0x30, 0x30, 0x00, 0x30, 0x00,
11759 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00,
11760 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00,
11761 0x30, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x30, 0x00,
11762 0x00, 0xc6, 0xcc, 0x18, 0x30, 0x66, 0xc6, 0x00,
11763 0x38, 0x6c, 0x38, 0x76, 0xdc, 0xcc, 0x76, 0x00,
11764 0x60, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
11765 0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,
11766 0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,
11767 0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00,
11768 0x00, 0x30, 0x30, 0xfc, 0x30, 0x30, 0x00, 0x00,
11769 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x60,
11770 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00,
11771 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00,
11772 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x80, 0x00,
11773 0x7c, 0xc6, 0xce, 0xde, 0xf6, 0xe6, 0x7c, 0x00,
11774 0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00,
11775 0x78, 0xcc, 0x0c, 0x38, 0x60, 0xcc, 0xfc, 0x00,
11776 0x78, 0xcc, 0x0c, 0x38, 0x0c, 0xcc, 0x78, 0x00,
11777 0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x1e, 0x00,
11778 0xfc, 0xc0, 0xf8, 0x0c, 0x0c, 0xcc, 0x78, 0x00,
11779 0x38, 0x60, 0xc0, 0xf8, 0xcc, 0xcc, 0x78, 0x00,
11780 0xfc, 0xcc, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00,
11781 0x78, 0xcc, 0xcc, 0x78, 0xcc, 0xcc, 0x78, 0x00,
11782 0x78, 0xcc, 0xcc, 0x7c, 0x0c, 0x18, 0x70, 0x00,
11783 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00,
11784 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x60,
11785 0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x00,
11786 0x00, 0x00, 0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00,
11787 0x60, 0x30, 0x18, 0x0c, 0x18, 0x30, 0x60, 0x00,
11788 0x78, 0xcc, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00,
11789 0x7c, 0xc6, 0xde, 0xde, 0xde, 0xc0, 0x78, 0x00,
11790 0x30, 0x78, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0x00,
11791 0xfc, 0x66, 0x66, 0x7c, 0x66, 0x66, 0xfc, 0x00,
11792 0x3c, 0x66, 0xc0, 0xc0, 0xc0, 0x66, 0x3c, 0x00,
11793 0xf8, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0xf8, 0x00,
11794 0xfe, 0x62, 0x68, 0x78, 0x68, 0x62, 0xfe, 0x00,
11795 0xfe, 0x62, 0x68, 0x78, 0x68, 0x60, 0xf0, 0x00,
11796 0x3c, 0x66, 0xc0, 0xc0, 0xce, 0x66, 0x3e, 0x00,
11797 0xcc, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0xcc, 0x00,
11798 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11799 0x1e, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78, 0x00,
11800 0xe6, 0x66, 0x6c, 0x78, 0x6c, 0x66, 0xe6, 0x00,
11801 0xf0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xfe, 0x00,
11802 0xc6, 0xee, 0xfe, 0xfe, 0xd6, 0xc6, 0xc6, 0x00,
11803 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00,
11804 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00,
11805 0xfc, 0x66, 0x66, 0x7c, 0x60, 0x60, 0xf0, 0x00,
11806 0x78, 0xcc, 0xcc, 0xcc, 0xdc, 0x78, 0x1c, 0x00,
11807 0xfc, 0x66, 0x66, 0x7c, 0x6c, 0x66, 0xe6, 0x00,
11808 0x78, 0xcc, 0xe0, 0x70, 0x1c, 0xcc, 0x78, 0x00,
11809 0xfc, 0xb4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11810 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xfc, 0x00,
11811 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
11812 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00,
11813 0xc6, 0xc6, 0x6c, 0x38, 0x38, 0x6c, 0xc6, 0x00,
11814 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x30, 0x78, 0x00,
11815 0xfe, 0xc6, 0x8c, 0x18, 0x32, 0x66, 0xfe, 0x00,
11816 0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,
11817 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, 0x00,
11818 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
11819 0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00,
11820 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
11821 0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
11822 0x00, 0x00, 0x78, 0x0c, 0x7c, 0xcc, 0x76, 0x00,
11823 0xe0, 0x60, 0x60, 0x7c, 0x66, 0x66, 0xdc, 0x00,
11824 0x00, 0x00, 0x78, 0xcc, 0xc0, 0xcc, 0x78, 0x00,
11825 0x1c, 0x0c, 0x0c, 0x7c, 0xcc, 0xcc, 0x76, 0x00,
11826 0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x78, 0x00,
11827 0x38, 0x6c, 0x60, 0xf0, 0x60, 0x60, 0xf0, 0x00,
11828 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
11829 0xe0, 0x60, 0x6c, 0x76, 0x66, 0x66, 0xe6, 0x00,
11830 0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
11831 0x0c, 0x00, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78,
11832 0xe0, 0x60, 0x66, 0x6c, 0x78, 0x6c, 0xe6, 0x00,
11833 0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11834 0x00, 0x00, 0xcc, 0xfe, 0xfe, 0xd6, 0xc6, 0x00,
11835 0x00, 0x00, 0xf8, 0xcc, 0xcc, 0xcc, 0xcc, 0x00,
11836 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x00,
11837 0x00, 0x00, 0xdc, 0x66, 0x66, 0x7c, 0x60, 0xf0,
11838 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0x1e,
11839 0x00, 0x00, 0xdc, 0x76, 0x66, 0x60, 0xf0, 0x00,
11840 0x00, 0x00, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x00,
11841 0x10, 0x30, 0x7c, 0x30, 0x30, 0x34, 0x18, 0x00,
11842 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x76, 0x00,
11843 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
11844 0x00, 0x00, 0xc6, 0xd6, 0xfe, 0xfe, 0x6c, 0x00,
11845 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00,
11846 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
11847 0x00, 0x00, 0xfc, 0x98, 0x30, 0x64, 0xfc, 0x00,
11848 0x1c, 0x30, 0x30, 0xe0, 0x30, 0x30, 0x1c, 0x00,
11849 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,
11850 0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00,
11851 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11852 0x00, 0x10, 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0x00,
11853 };
11854
11855 ASM_START
11856 .org 0xcff0
11857 bios_table_area_end:
11858 // bcc-generated data will be placed here
11859 ASM_END
11860