1 /////////////////////////////////////////////////////////////////////////
2 // $Id: rombios.c,v 1.182 2007/08/01 17:09:51 vruppert Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2002 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
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.
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.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 // ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
30 // ROM BIOS compatability entry points:
31 // ===================================
32 // $e05b ; POST Entry Point
33 // $e2c3 ; NMI Handler Entry Point
34 // $e3fe ; INT 13h Fixed Disk Services Entry Point
35 // $e401 ; Fixed Disk Parameter Table
36 // $e6f2 ; INT 19h Boot Load Service Entry Point
37 // $e6f5 ; Configuration Data Table
38 // $e729 ; Baud Rate Generator Table
39 // $e739 ; INT 14h Serial Communications Service Entry Point
40 // $e82e ; INT 16h Keyboard Service Entry Point
41 // $e987 ; INT 09h Keyboard Service Entry Point
42 // $ec59 ; INT 13h Diskette Service Entry Point
43 // $ef57 ; INT 0Eh Diskette Hardware ISR Entry Point
44 // $efc7 ; Diskette Controller Parameter Table
45 // $efd2 ; INT 17h Printer Service Entry Point
46 // $f045 ; INT 10 Functions 0-Fh Entry Point
47 // $f065 ; INT 10h Video Support Service Entry Point
48 // $f0a4 ; MDA/CGA Video Parameter Table (INT 1Dh)
49 // $f841 ; INT 12h Memory Size Service Entry Point
50 // $f84d ; INT 11h Equipment List Service Entry Point
51 // $f859 ; INT 15h System Services Entry Point
52 // $fa6e ; Character Font for 320x200 & 640x200 Graphics (lower 128 characters)
53 // $fe6e ; INT 1Ah Time-of-day Service Entry Point
54 // $fea5 ; INT 08h System Timer ISR Entry Point
55 // $fef3 ; Initial Interrupt Vector Offsets Loaded by POST
56 // $ff53 ; IRET Instruction for Dummy Interrupt Handler
57 // $ff54 ; INT 05h Print Screen Service Entry Point
58 // $fff0 ; Power-up Entry Point
59 // $fff5 ; ASCII Date ROM was built - 8 characters in MM/DD/YY
60 // $fffe ; System Model ID
62 // NOTES for ATA/ATAPI driver (cbbochs@free.fr)
64 // - supports up to 4 ATA interfaces
65 // - device/geometry detection
66 // - 16bits/32bits device access
68 // - datain/dataout/packet command support
70 // NOTES for El-Torito Boot (cbbochs@free.fr)
71 // - CD-ROM booting is only available if ATA/ATAPI Driver is available
72 // - Current code is only able to boot mono-session cds
73 // - Current code can not boot and emulate a hard-disk
74 // the bios will panic otherwise
75 // - Current code also use memory in EBDA segement.
76 // - I used cmos byte 0x3D to store extended information on boot-device
77 // - Code has to be modified modified to handle multiple cdrom drives
78 // - Here are the cdrom boot failure codes:
79 // 1 : no atapi device found
80 // 2 : no atapi cdrom found
81 // 3 : can not read cd - BRVD
82 // 4 : cd is not eltorito (BRVD)
83 // 5 : cd is not eltorito (ISO TAG)
84 // 6 : cd is not eltorito (ELTORITO TAG)
85 // 7 : can not read cd - boot catalog
86 // 8 : boot catalog : bad header
87 // 9 : boot catalog : bad platform
88 // 10 : boot catalog : bad signature
89 // 11 : boot catalog : bootable flag not set
90 // 12 : can not read cd - boot image
94 // I used memory starting at 0x121 in the segment
95 // - the translation policy is defined in cmos regs 0x39 & 0x3a
100 // - needs to be reworked. Uses direct [bp] offsets. (?)
103 // - f04 (verify sectors) isn't complete (?)
104 // - f02/03/04 should set current cyl,etc in BDA (?)
105 // - rewrite int13_relocated & clean up int13 entry code
108 // - NMI access (bit7 of addr written to 70h)
111 // - should handle the "don't detect" bit (cmos regs 0x3b & 0x3c)
112 // - could send the multiple-sector read/write commands
115 // - Emulate a Hard-disk (currently only diskette can be emulated) see "FIXME ElTorito Harddisk"
116 // - Implement remaining int13_cdemu functions (as defined by El-Torito specs)
117 // - cdrom drive is hardcoded to ide 0 device 1 in several places. see "FIXME ElTorito Hardcoded"
118 // - int13 Fix DL when emulating a cd. In that case DL is decremented before calling real int13.
119 // This is ok. But DL should be reincremented afterwards.
120 // - Fix all "FIXME ElTorito Various"
121 // - should be able to boot any cdrom instead of the first one
123 // BCC Bug: find a generic way to handle the bug of #asm after an "if" (fixed in 0.16.7)
128 #define DEBUG_INT13_HD 0
129 #define DEBUG_INT13_CD 0
130 #define DEBUG_INT13_ET 0
131 #define DEBUG_INT13_FL 0
132 #define DEBUG_INT15 0
133 #define DEBUG_INT16 0
134 #define DEBUG_INT1A 0
135 #define DEBUG_INT74 0
139 #define BX_USE_PS2_MOUSE 1
140 #define BX_CALL_INT15_4F 1
141 #define BX_USE_EBDA 1
142 #define BX_SUPPORT_FLOPPY 1
143 #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
147 #define BX_USE_ATADRV 1
148 #define BX_ELTORITO_BOOT 1
150 #define BX_MAX_ATA_INTERFACES 4
151 #define BX_MAX_ATA_DEVICES (BX_MAX_ATA_INTERFACES*2)
153 #define BX_VIRTUAL_PORTS 1 /* normal output to Bochs ports */
154 #define BX_DEBUG_SERIAL 0 /* output to COM1 */
156 /* model byte 0xFC = AT */
157 #define SYS_MODEL_ID 0xFC
158 #define SYS_SUBMODEL_ID 0x00
159 #define BIOS_REVISION 1
160 #define BIOS_CONFIG_TABLE 0xe6f5
162 #ifndef BIOS_BUILD_DATE
163 # define BIOS_BUILD_DATE "06/23/99"
166 // 1K of base memory used for Extended Bios Data Area (EBDA)
167 // EBDA is used for PS/2 mouse support, and IDE BIOS, etc.
168 #define EBDA_SEG 0x9FC0
169 #define EBDA_SIZE 1 // In KiB
170 #define BASE_MEM_IN_K (640 - EBDA_SIZE)
172 /* 256 bytes at 0x9ff00 -- 0x9ffff is used for the IPL boot table. */
173 #define IPL_SEG 0x9ff0
174 #define IPL_TABLE_OFFSET 0x0000
175 #define IPL_TABLE_ENTRIES 8
176 #define IPL_COUNT_OFFSET 0x0080 /* u16: number of valid table entries */
177 #define IPL_SEQUENCE_OFFSET 0x0082 /* u16: next boot device */
178 #define IPL_BOOTFIRST_OFFSET 0x0084 /* u16: user selected device */
179 #define IPL_SIZE 0xff
180 #define IPL_TYPE_FLOPPY 0x01
181 #define IPL_TYPE_HARDDISK 0x02
182 #define IPL_TYPE_CDROM 0x03
183 #define IPL_TYPE_BEV 0x80
186 #if BX_USE_ATADRV && BX_CPU<3
187 # error The ATA/ATAPI Driver can only to be used with a 386+ cpu
189 #if BX_USE_ATADRV && !BX_USE_EBDA
190 # error ATA/ATAPI Driver can only be used if EBDA is available
192 #if BX_ELTORITO_BOOT && !BX_USE_ATADRV
193 # error El-Torito Boot can only be use if ATA/ATAPI Driver is available
195 #if BX_PCIBIOS && BX_CPU<3
196 # error PCI BIOS can only be used with 386+ cpu
198 #if BX_APM && BX_CPU<3
199 # error APM BIOS can only be used with 386+ cpu
202 // define this if you want to make PCIBIOS working on a specific bridge only
203 // undef enables PCIBIOS when at least one PCI device is found
204 // i440FX is emulated by Bochs and QEMU
205 #define PCI_FIXED_HOST_BRIDGE 0x12378086 ;; i440FX PCI bridge
208 // #$20 is hex 20 = 32
209 // #0x20 is hex 20 = 32
216 // all hex literals should be prefixed with '0x'
217 // grep "#[0-9a-fA-F][0-9a-fA-F]" rombios.c
218 // no mov SEG-REG, #value, must mov register into seg-reg
219 // grep -i "mov[ ]*.s" rombios.c
221 // This is for compiling with gcc2 and gcc3
222 #define ASM_START #asm
223 #define ASM_END #endasm
237 ;; the HALT macro is called with the line number of the HALT call
.
238 ;; The line number is then sent to the PANIC_PORT
, causing Bochs
/Plex
239 ;; to print a BX_PANIC message
. This will normally halt the simulation
240 ;; with a message such as
"BIOS panic at rombios.c, line 4091".
241 ;; However
, users can choose to make panics non
-fatal
and continue.
268 typedef unsigned char Bit8u
;
269 typedef unsigned short Bit16u
;
270 typedef unsigned short bx_bool
;
271 typedef unsigned long Bit32u
;
274 void memsetb(seg
,offset
,value
,count
);
275 void memcpyb(dseg
,doffset
,sseg
,soffset
,count
);
276 void memcpyd(dseg
,doffset
,sseg
,soffset
,count
);
278 // memset of count bytes
280 memsetb(seg
,offset
,value
,count
)
295 mov cx
, 10[bp
] ; count
298 mov ax
, 4[bp
] ; segment
300 mov ax
, 6[bp
] ; offset
302 mov al
, 8[bp
] ; value
317 // memcpy of count bytes
319 memcpyb(dseg
,doffset
,sseg
,soffset
,count
)
337 mov cx
, 12[bp
] ; count
340 mov ax
, 4[bp
] ; dsegment
342 mov ax
, 6[bp
] ; doffset
344 mov ax
, 8[bp
] ; ssegment
346 mov ax
, 10[bp
] ; soffset
364 // memcpy of count dword
366 memcpyd(dseg
,doffset
,sseg
,soffset
,count
)
384 mov cx
, 12[bp
] ; count
387 mov ax
, 4[bp
] ; dsegment
389 mov ax
, 6[bp
] ; doffset
391 mov ax
, 8[bp
] ; ssegment
393 mov ax
, 10[bp
] ; soffset
411 // read_dword and write_dword functions
412 static Bit32u
read_dword();
413 static void write_dword();
416 read_dword(seg
, offset
)
426 mov ax
, 4[bp
] ; segment
428 mov bx
, 6[bp
] ; offset
432 ;; ax
= return value (word
)
433 ;; dx
= return value (word
)
442 write_dword(seg
, offset
, data
)
454 mov ax
, 4[bp
] ; segment
456 mov bx
, 6[bp
] ; offset
457 mov ax
, 8[bp
] ; data word
458 mov
[bx
], ax
; write data word
460 mov ax
, 10[bp
] ; data word
461 mov
[bx
], ax
; write data word
470 // Bit32u (unsigned long) and long helper functions
499 cmp eax
, dword ptr
[di
]
518 mul eax
, dword ptr
[di
]
614 // for access to RAM area which is used by interrupt vectors
615 // and BIOS Data Area
618 unsigned char filler1
[0x400];
619 unsigned char filler2
[0x6c];
625 #define BiosData ((bios_data_t *) 0)
629 Bit16u heads
; // # heads
630 Bit16u cylinders
; // # cylinders
631 Bit16u spt
; // # sectors / track
651 Bit8u iface
; // ISA or PCI
652 Bit16u iobase1
; // IO Base 1
653 Bit16u iobase2
; // IO Base 2
658 Bit8u type
; // Detected type of ata (ata/atapi/none/unknown)
659 Bit8u device
; // Detected type of attached devices (hd/cd/none)
660 Bit8u removable
; // Removable device flag
661 Bit8u lock
; // Locks for removable devices
662 Bit8u mode
; // transfer mode : PIO 16/32 bits - IRQ - ISADMA - PCIDMA
663 Bit16u blksize
; // block size
665 Bit8u translation
; // type of translation
666 chs_t lchs
; // Logical CHS
667 chs_t pchs
; // Physical CHS
669 Bit32u sectors_low
; // Total sectors count
675 ata_channel_t channels
[BX_MAX_ATA_INTERFACES
];
678 ata_device_t devices
[BX_MAX_ATA_DEVICES
];
680 // map between (bios hd id - 0x80) and ata channels
681 Bit8u hdcount
, hdidmap
[BX_MAX_ATA_DEVICES
];
683 // map between (bios cd id - 0xE0) and ata channels
684 Bit8u cdcount
, cdidmap
[BX_MAX_ATA_DEVICES
];
686 // Buffer for DPTE table
689 // Count of transferred sectors and bytes
696 // ElTorito Device Emulation data
700 Bit8u emulated_drive
;
701 Bit8u controller_index
;
704 Bit16u buffer_segment
;
711 #endif // BX_ELTORITO_BOOT
713 // for access to EBDA area
714 // The EBDA structure should conform to
715 // http://www.frontiernet.net/~fys/rombios.htm document
716 // I made the ata and cdemu structs begin at 0x121 in the EBDA seg
717 // EBDA must be at most 768 bytes; it lives at EBDA_SEG, and the boot
718 // device tables are at IPL_SEG
720 unsigned char filler1
[0x3D];
722 // FDPT - Can be splitted in data members if needed
723 unsigned char fdpt0
[0x10];
724 unsigned char fdpt1
[0x10];
726 unsigned char filler2
[0xC4];
732 // El Torito Emulation data
734 #endif // BX_ELTORITO_BOOT
738 #define EbdaData ((ebda_data_t *) 0)
740 // for access to the int13ext structure
751 #define Int13Ext ((int13ext_t *) 0)
753 // Disk Physical Table definition
760 Bit32u sector_count1
;
761 Bit32u sector_count2
;
772 Bit8u device_path
[8];
777 #define Int13DPT ((dpt_t *) 0)
779 #endif // BX_USE_ATADRV
784 Bit16u di
, si
, bp
, sp
;
785 Bit16u bx
, dx
, cx
, ax
;
789 Bit8u bl
, bh
, dl
, dh
, cl
, ch
, al
, ah
;
797 Bit32u edi
, esi
, ebp
, esp
;
798 Bit32u ebx
, edx
, ecx
, eax
;
801 Bit16u di
, filler1
, si
, filler2
, bp
, filler3
, sp
, filler4
;
802 Bit16u bx
, filler5
, dx
, filler6
, cx
, filler7
, ax
, filler8
;
830 #define SetCF(x) x.u.r8.flagsl |= 0x01
831 #define SetZF(x) x.u.r8.flagsl |= 0x40
832 #define ClearCF(x) x.u.r8.flagsl &= 0xfe
833 #define ClearZF(x) x.u.r8.flagsl &= 0xbf
834 #define GetCF(x) (x.u.r8.flagsl & 0x01)
853 static Bit8u
inb_cmos();
855 static void outb_cmos();
858 static void init_rtc();
859 static bx_bool
rtc_updating();
861 static Bit8u
read_byte();
862 static Bit16u
read_word();
863 static void write_byte();
864 static void write_word();
865 static void bios_printf();
867 static Bit8u
inhibit_mouse_int_and_events();
868 static void enable_mouse_int_and_events();
869 static Bit8u
send_to_mouse_ctrl();
870 static Bit8u
get_mouse_data();
871 static void set_kbd_command_byte();
873 static void int09_function();
874 static void int13_harddisk();
875 static void int13_cdrom();
876 static void int13_cdemu();
877 static void int13_eltorito();
878 static void int13_diskette_function();
879 static void int14_function();
880 static void int15_function();
881 static void int16_function();
882 static void int17_function();
883 static void int19_function();
884 static void int1a_function();
885 static void int70_function();
886 static void int74_function();
887 static Bit16u
get_CS();
888 static Bit16u
get_SS();
889 static unsigned int enqueue_key();
890 static unsigned int dequeue_key();
891 static void get_hd_geometry();
892 static void set_diskette_ret_status();
893 static void set_diskette_current_cyl();
894 static void determine_floppy_media();
895 static bx_bool
floppy_drive_exists();
896 static bx_bool
floppy_drive_recal();
897 static bx_bool
floppy_media_known();
898 static bx_bool
floppy_media_sense();
899 static bx_bool
set_enable_a20();
900 static void debugger_on();
901 static void debugger_off();
902 static void keyboard_init();
903 static void keyboard_panic();
904 static void shutdown_status_panic();
905 static void nmi_handler_msg();
906 static void delay_ticks();
907 static void delay_ticks_and_check_for_keystroke();
909 static void interactive_bootkey();
910 static void print_bios_banner();
911 static void print_boot_device();
912 static void print_boot_failure();
913 static void print_cdromboot_failure();
917 // ATA / ATAPI driver
922 Bit16u
ata_cmd_non_data();
923 Bit16u
ata_cmd_data_in();
924 Bit16u
ata_cmd_data_out();
925 Bit16u
ata_cmd_packet();
927 Bit16u
atapi_get_sense();
928 Bit16u
atapi_is_ready();
929 Bit16u
atapi_is_cdrom();
931 #endif // BX_USE_ATADRV
936 Bit8u
cdemu_isactive();
937 Bit8u
cdemu_emulated_drive();
941 #endif // BX_ELTORITO_BOOT
943 static char bios_cvs_version_string
[] = "$Revision: 1.182 $ $Date: 2007/08/01 17:09:51 $";
945 #define BIOS_COPYRIGHT_STRING "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
948 # define BX_DEBUG_ATA(a...) BX_DEBUG(a)
950 # define BX_DEBUG_ATA(a...)
953 # define BX_DEBUG_INT13_HD(a...) BX_DEBUG(a)
955 # define BX_DEBUG_INT13_HD(a...)
958 # define BX_DEBUG_INT13_CD(a...) BX_DEBUG(a)
960 # define BX_DEBUG_INT13_CD(a...)
963 # define BX_DEBUG_INT13_ET(a...) BX_DEBUG(a)
965 # define BX_DEBUG_INT13_ET(a...)
968 # define BX_DEBUG_INT13_FL(a...) BX_DEBUG(a)
970 # define BX_DEBUG_INT13_FL(a...)
973 # define BX_DEBUG_INT15(a...) BX_DEBUG(a)
975 # define BX_DEBUG_INT15(a...)
978 # define BX_DEBUG_INT16(a...) BX_DEBUG(a)
980 # define BX_DEBUG_INT16(a...)
983 # define BX_DEBUG_INT1A(a...) BX_DEBUG(a)
985 # define BX_DEBUG_INT1A(a...)
988 # define BX_DEBUG_INT74(a...) BX_DEBUG(a)
990 # define BX_DEBUG_INT74(a...)
993 #define SET_AL(val8) AX = ((AX & 0xff00) | (val8))
994 #define SET_BL(val8) BX = ((BX & 0xff00) | (val8))
995 #define SET_CL(val8) CX = ((CX & 0xff00) | (val8))
996 #define SET_DL(val8) DX = ((DX & 0xff00) | (val8))
997 #define SET_AH(val8) AX = ((AX & 0x00ff) | ((val8) << 8))
998 #define SET_BH(val8) BX = ((BX & 0x00ff) | ((val8) << 8))
999 #define SET_CH(val8) CX = ((CX & 0x00ff) | ((val8) << 8))
1000 #define SET_DH(val8) DX = ((DX & 0x00ff) | ((val8) << 8))
1002 #define GET_AL() ( AX & 0x00ff )
1003 #define GET_BL() ( BX & 0x00ff )
1004 #define GET_CL() ( CX & 0x00ff )
1005 #define GET_DL() ( DX & 0x00ff )
1006 #define GET_AH() ( AX >> 8 )
1007 #define GET_BH() ( BX >> 8 )
1008 #define GET_CH() ( CX >> 8 )
1009 #define GET_DH() ( DX >> 8 )
1011 #define GET_ELDL() ( ELDX & 0x00ff )
1012 #define GET_ELDH() ( ELDX >> 8 )
1014 #define SET_CF() FLAGS |= 0x0001
1015 #define CLEAR_CF() FLAGS &= 0xfffe
1016 #define GET_CF() (FLAGS & 0x0001)
1018 #define SET_ZF() FLAGS |= 0x0040
1019 #define CLEAR_ZF() FLAGS &= 0xffbf
1020 #define GET_ZF() (FLAGS & 0x0040)
1022 #define UNSUPPORTED_FUNCTION 0x86
1025 #define MAX_SCAN_CODE 0x58
1033 } scan_to_scanascii
[MAX_SCAN_CODE
+ 1] = {
1034 { none
, none
, none
, none
, none
},
1035 { 0x011b, 0x011b, 0x011b, 0x0100, none
}, /* escape */
1036 { 0x0231, 0x0221, none
, 0x7800, none
}, /* 1! */
1037 { 0x0332, 0x0340, 0x0300, 0x7900, none
}, /* 2@ */
1038 { 0x0433, 0x0423, none
, 0x7a00, none
}, /* 3# */
1039 { 0x0534, 0x0524, none
, 0x7b00, none
}, /* 4$ */
1040 { 0x0635, 0x0625, none
, 0x7c00, none
}, /* 5% */
1041 { 0x0736, 0x075e, 0x071e, 0x7d00, none
}, /* 6^ */
1042 { 0x0837, 0x0826, none
, 0x7e00, none
}, /* 7& */
1043 { 0x0938, 0x092a, none
, 0x7f00, none
}, /* 8* */
1044 { 0x0a39, 0x0a28, none
, 0x8000, none
}, /* 9( */
1045 { 0x0b30, 0x0b29, none
, 0x8100, none
}, /* 0) */
1046 { 0x0c2d, 0x0c5f, 0x0c1f, 0x8200, none
}, /* -_ */
1047 { 0x0d3d, 0x0d2b, none
, 0x8300, none
}, /* =+ */
1048 { 0x0e08, 0x0e08, 0x0e7f, none
, none
}, /* backspace */
1049 { 0x0f09, 0x0f00, none
, none
, none
}, /* tab */
1050 { 0x1071, 0x1051, 0x1011, 0x1000, 0x40 }, /* Q */
1051 { 0x1177, 0x1157, 0x1117, 0x1100, 0x40 }, /* W */
1052 { 0x1265, 0x1245, 0x1205, 0x1200, 0x40 }, /* E */
1053 { 0x1372, 0x1352, 0x1312, 0x1300, 0x40 }, /* R */
1054 { 0x1474, 0x1454, 0x1414, 0x1400, 0x40 }, /* T */
1055 { 0x1579, 0x1559, 0x1519, 0x1500, 0x40 }, /* Y */
1056 { 0x1675, 0x1655, 0x1615, 0x1600, 0x40 }, /* U */
1057 { 0x1769, 0x1749, 0x1709, 0x1700, 0x40 }, /* I */
1058 { 0x186f, 0x184f, 0x180f, 0x1800, 0x40 }, /* O */
1059 { 0x1970, 0x1950, 0x1910, 0x1900, 0x40 }, /* P */
1060 { 0x1a5b, 0x1a7b, 0x1a1b, none
, none
}, /* [{ */
1061 { 0x1b5d, 0x1b7d, 0x1b1d, none
, none
}, /* ]} */
1062 { 0x1c0d, 0x1c0d, 0x1c0a, none
, none
}, /* Enter */
1063 { none
, none
, none
, none
, none
}, /* L Ctrl */
1064 { 0x1e61, 0x1e41, 0x1e01, 0x1e00, 0x40 }, /* A */
1065 { 0x1f73, 0x1f53, 0x1f13, 0x1f00, 0x40 }, /* S */
1066 { 0x2064, 0x2044, 0x2004, 0x2000, 0x40 }, /* D */
1067 { 0x2166, 0x2146, 0x2106, 0x2100, 0x40 }, /* F */
1068 { 0x2267, 0x2247, 0x2207, 0x2200, 0x40 }, /* G */
1069 { 0x2368, 0x2348, 0x2308, 0x2300, 0x40 }, /* H */
1070 { 0x246a, 0x244a, 0x240a, 0x2400, 0x40 }, /* J */
1071 { 0x256b, 0x254b, 0x250b, 0x2500, 0x40 }, /* K */
1072 { 0x266c, 0x264c, 0x260c, 0x2600, 0x40 }, /* L */
1073 { 0x273b, 0x273a, none
, none
, none
}, /* ;: */
1074 { 0x2827, 0x2822, none
, none
, none
}, /* '" */
1075 { 0x2960, 0x297e, none
, none
, none
}, /* `~ */
1076 { none
, none
, none
, none
, none
}, /* L shift */
1077 { 0x2b5c, 0x2b7c, 0x2b1c, none
, none
}, /* |\ */
1078 { 0x2c7a, 0x2c5a, 0x2c1a, 0x2c00, 0x40 }, /* Z */
1079 { 0x2d78, 0x2d58, 0x2d18, 0x2d00, 0x40 }, /* X */
1080 { 0x2e63, 0x2e43, 0x2e03, 0x2e00, 0x40 }, /* C */
1081 { 0x2f76, 0x2f56, 0x2f16, 0x2f00, 0x40 }, /* V */
1082 { 0x3062, 0x3042, 0x3002, 0x3000, 0x40 }, /* B */
1083 { 0x316e, 0x314e, 0x310e, 0x3100, 0x40 }, /* N */
1084 { 0x326d, 0x324d, 0x320d, 0x3200, 0x40 }, /* M */
1085 { 0x332c, 0x333c, none
, none
, none
}, /* ,< */
1086 { 0x342e, 0x343e, none
, none
, none
}, /* .> */
1087 { 0x352f, 0x353f, none
, none
, none
}, /* /? */
1088 { none
, none
, none
, none
, none
}, /* R Shift */
1089 { 0x372a, 0x372a, none
, none
, none
}, /* * */
1090 { none
, none
, none
, none
, none
}, /* L Alt */
1091 { 0x3920, 0x3920, 0x3920, 0x3920, none
}, /* space */
1092 { none
, none
, none
, none
, none
}, /* caps lock */
1093 { 0x3b00, 0x5400, 0x5e00, 0x6800, none
}, /* F1 */
1094 { 0x3c00, 0x5500, 0x5f00, 0x6900, none
}, /* F2 */
1095 { 0x3d00, 0x5600, 0x6000, 0x6a00, none
}, /* F3 */
1096 { 0x3e00, 0x5700, 0x6100, 0x6b00, none
}, /* F4 */
1097 { 0x3f00, 0x5800, 0x6200, 0x6c00, none
}, /* F5 */
1098 { 0x4000, 0x5900, 0x6300, 0x6d00, none
}, /* F6 */
1099 { 0x4100, 0x5a00, 0x6400, 0x6e00, none
}, /* F7 */
1100 { 0x4200, 0x5b00, 0x6500, 0x6f00, none
}, /* F8 */
1101 { 0x4300, 0x5c00, 0x6600, 0x7000, none
}, /* F9 */
1102 { 0x4400, 0x5d00, 0x6700, 0x7100, none
}, /* F10 */
1103 { none
, none
, none
, none
, none
}, /* Num Lock */
1104 { none
, none
, none
, none
, none
}, /* Scroll Lock */
1105 { 0x4700, 0x4737, 0x7700, none
, 0x20 }, /* 7 Home */
1106 { 0x4800, 0x4838, none
, none
, 0x20 }, /* 8 UP */
1107 { 0x4900, 0x4939, 0x8400, none
, 0x20 }, /* 9 PgUp */
1108 { 0x4a2d, 0x4a2d, none
, none
, none
}, /* - */
1109 { 0x4b00, 0x4b34, 0x7300, none
, 0x20 }, /* 4 Left */
1110 { 0x4c00, 0x4c35, none
, none
, 0x20 }, /* 5 */
1111 { 0x4d00, 0x4d36, 0x7400, none
, 0x20 }, /* 6 Right */
1112 { 0x4e2b, 0x4e2b, none
, none
, none
}, /* + */
1113 { 0x4f00, 0x4f31, 0x7500, none
, 0x20 }, /* 1 End */
1114 { 0x5000, 0x5032, none
, none
, 0x20 }, /* 2 Down */
1115 { 0x5100, 0x5133, 0x7600, none
, 0x20 }, /* 3 PgDn */
1116 { 0x5200, 0x5230, none
, none
, 0x20 }, /* 0 Ins */
1117 { 0x5300, 0x532e, none
, none
, 0x20 }, /* Del */
1118 { none
, none
, none
, none
, none
},
1119 { none
, none
, none
, none
, none
},
1120 { 0x565c, 0x567c, none
, none
, none
}, /* \| */
1121 { 0x8500, 0x8700, 0x8900, 0x8b00, none
}, /* F11 */
1122 { 0x8600, 0x8800, 0x8a00, 0x8c00, none
}, /* F12 */
1206 outb_cmos(cmos_reg
, val
)
1214 mov al
, 4[bp
] ;; cmos_reg
1216 mov al
, 6[bp
] ;; val
1231 mov al
, 4[bp
] ;; cmos_reg
1242 outb_cmos(0x0a, 0x26);
1243 outb_cmos(0x0b, 0x02);
1251 // This function checks to see if the update-in-progress bit
1252 // is set in CMOS Status Register A. If not, it returns 0.
1253 // If it is set, it tries to wait until there is a transition
1254 // to 0, and will return 0 if such a transition occurs. A 1
1255 // is returned only after timing out. The maximum period
1256 // that this bit should be set is constrained to 244useconds.
1257 // The count I use below guarantees coverage or more than
1258 // this time, with any reasonable IPS setting.
1263 while (--count
!= 0) {
1264 if ( (inb_cmos(0x0a) & 0x80) == 0 )
1267 return(1); // update-in-progress never transitioned to 0
1272 read_byte(seg
, offset
)
1282 mov ax
, 4[bp
] ; segment
1284 mov bx
, 6[bp
] ; offset
1286 ;; al
= return value (byte
)
1295 read_word(seg
, offset
)
1305 mov ax
, 4[bp
] ; segment
1307 mov bx
, 6[bp
] ; offset
1309 ;; ax
= return value (word
)
1318 write_byte(seg
, offset
, data
)
1330 mov ax
, 4[bp
] ; segment
1332 mov bx
, 6[bp
] ; offset
1333 mov al
, 8[bp
] ; data byte
1334 mov
[bx
], al
; write data byte
1344 write_word(seg
, offset
, data
)
1356 mov ax
, 4[bp
] ; segment
1358 mov bx
, 6[bp
] ; offset
1359 mov ax
, 8[bp
] ; data word
1360 mov
[bx
], ax
; write data word
1386 /* serial debug port*/
1387 #define BX_DEBUG_PORT 0x03f8
1390 #define UART_RBR 0x00
1391 #define UART_THR 0x00
1394 #define UART_IER 0x01
1395 #define UART_IIR 0x02
1396 #define UART_FCR 0x02
1397 #define UART_LCR 0x03
1398 #define UART_MCR 0x04
1399 #define UART_DLL 0x00
1400 #define UART_DLM 0x01
1403 #define UART_LSR 0x05
1404 #define UART_MSR 0x06
1405 #define UART_SCR 0x07
1407 int uart_can_tx_byte(base_port
)
1410 return inb(base_port
+ UART_LSR
) & 0x20;
1413 void uart_wait_to_tx_byte(base_port
)
1416 while (!uart_can_tx_byte(base_port
));
1419 void uart_wait_until_sent(base_port
)
1422 while (!(inb(base_port
+ UART_LSR
) & 0x40));
1425 void uart_tx_byte(base_port
, data
)
1429 uart_wait_to_tx_byte(base_port
);
1430 outb(base_port
+ UART_THR
, data
);
1431 uart_wait_until_sent(base_port
);
1460 if (c
== '\n') uart_tx_byte(BX_DEBUG_PORT
, '\r');
1461 uart_tx_byte(BX_DEBUG_PORT
, c
);
1463 #if BX_VIRTUAL_PORTS
1464 if (action
& BIOS_PRINTF_DEBUG
) outb(DEBUG_PORT
, c
);
1465 if (action
& BIOS_PRINTF_INFO
) outb(INFO_PORT
, c
);
1467 if (action
& BIOS_PRINTF_SCREEN
) {
1468 if (c
== '\n') wrch('\r');
1474 put_int(action
, val
, width
, neg
)
1479 short nval
= val
/ 10;
1481 put_int(action
, nval
, width
- 1, neg
);
1483 while (--width
> 0) send(action
, ' ');
1484 if (neg
) send(action
, '-');
1486 send(action
, val
- (nval
* 10) + '0');
1490 put_uint(action
, val
, width
, neg
)
1496 unsigned short nval
= val
/ 10;
1498 put_uint(action
, nval
, width
- 1, neg
);
1500 while (--width
> 0) send(action
, ' ');
1501 if (neg
) send(action
, '-');
1503 send(action
, val
- (nval
* 10) + '0');
1507 put_luint(action
, val
, width
, neg
)
1513 unsigned long nval
= val
/ 10;
1515 put_luint(action
, nval
, width
- 1, neg
);
1517 while (--width
> 0) send(action
, ' ');
1518 if (neg
) send(action
, '-');
1520 send(action
, val
- (nval
* 10) + '0');
1523 void put_str(action
, segment
, offset
)
1530 while (c
= read_byte(segment
, offset
)) {
1540 long ticks_to_wait
, delta
;
1541 Bit32u prev_ticks
, t
;
1544 * The 0:046c wraps around at 'midnight' according to a 18.2Hz clock.
1545 * We also have to be careful about interrupt storms.
1551 ticks_to_wait
= ticks
;
1552 prev_ticks
= read_dword(0x0, 0x46c);
1558 t
= read_dword(0x0, 0x46c);
1561 delta
= t
- prev_ticks
; /* The temp var is required or bcc screws up. */
1562 ticks_to_wait
-= delta
;
1564 else if (t
< prev_ticks
)
1566 ticks_to_wait
-= t
; /* wrapped */
1570 } while (ticks_to_wait
> 0);
1578 check_for_keystroke()
1603 delay_ticks_and_check_for_keystroke(ticks
, count
)
1604 Bit16u ticks
, count
;
1607 for (i
= 1; i
<= count
; i
++) {
1609 if (check_for_keystroke())
1614 //--------------------------------------------------------------------------
1616 // A compact variable argument printf function.
1618 // Supports %[format_width][length]format
1619 // where format can be x,X,u,d,s,S,c
1620 // and the optional length modifier is l (ell)
1621 //--------------------------------------------------------------------------
1623 bios_printf(action
, s
)
1627 Bit8u c
, format_char
;
1631 Bit16u arg_seg
, arg
, nibble
, hibyte
, shift_count
, format_width
, hexadd
;
1639 if ((action
& BIOS_PRINTF_DEBHALT
) == BIOS_PRINTF_DEBHALT
) {
1640 #if BX_VIRTUAL_PORTS
1641 outb(PANIC_PORT2
, 0x00);
1643 bios_printf (BIOS_PRINTF_SCREEN
, "FATAL: ");
1646 while (c
= read_byte(get_CS(), s
)) {
1651 else if (in_format
) {
1652 if ( (c
>='0') && (c
<='9') ) {
1653 format_width
= (format_width
* 10) + (c
- '0');
1656 arg_ptr
++; // increment to next arg
1657 arg
= read_word(arg_seg
, arg_ptr
);
1658 if (c
== 'x' || c
== 'X') {
1659 if (format_width
== 0)
1665 for (i
=format_width
-1; i
>=0; i
--) {
1666 nibble
= (arg
>> (4 * i
)) & 0x000f;
1667 send (action
, (nibble
<=9)? (nibble
+'0') : (nibble
-10+hexadd
));
1670 else if (c
== 'u') {
1671 put_uint(action
, arg
, format_width
, 0);
1673 else if (c
== 'l') {
1675 c
= read_byte(get_CS(), s
); /* is it ld,lx,lu? */
1676 arg_ptr
++; /* increment to next arg */
1677 hibyte
= read_word(arg_seg
, arg_ptr
);
1679 if (hibyte
& 0x8000)
1680 put_luint(action
, 0L-(((Bit32u
) hibyte
<< 16) | arg
), format_width
-1, 1);
1682 put_luint(action
, ((Bit32u
) hibyte
<< 16) | arg
, format_width
, 0);
1684 else if (c
== 'u') {
1685 put_luint(action
, ((Bit32u
) hibyte
<< 16) | arg
, format_width
, 0);
1687 else if (c
== 'x' || c
== 'X')
1689 if (format_width
== 0)
1695 for (i
=format_width
-1; i
>=0; i
--) {
1696 nibble
= ((((Bit32u
) hibyte
<<16) | arg
) >> (4 * i
)) & 0x000f;
1697 send (action
, (nibble
<=9)? (nibble
+'0') : (nibble
-10+hexadd
));
1701 else if (c
== 'd') {
1703 put_int(action
, -arg
, format_width
- 1, 1);
1705 put_int(action
, arg
, format_width
, 0);
1707 else if (c
== 's') {
1708 put_str(action
, get_CS(), arg
);
1710 else if (c
== 'S') {
1713 arg
= read_word(arg_seg
, arg_ptr
);
1714 put_str(action
, hibyte
, arg
);
1716 else if (c
== 'c') {
1720 BX_PANIC("bios_printf: unknown format\n");
1730 if (action
& BIOS_PRINTF_HALT
) {
1731 // freeze in a busy loop.
1741 //--------------------------------------------------------------------------
1743 //--------------------------------------------------------------------------
1744 // this file is based on LinuxBIOS implementation of keyboard.c
1745 // could convert to #asm to gain space
1751 /* ------------------- Flush buffers ------------------------*/
1752 /* Wait until buffer is empty */
1754 while ( (inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x00);
1756 /* flush incoming keys */
1760 if (inb(0x64) & 0x01) {
1766 // Due to timer issues, and if the IPS setting is > 15000000,
1767 // the incoming keys might not be flushed here. That will
1768 // cause a panic a few lines below. See sourceforge bug report :
1769 // [ 642031 ] FATAL: Keyboard RESET error:993
1771 /* ------------------- controller side ----------------------*/
1772 /* send cmd = 0xAA, self test 8042 */
1775 /* Wait until buffer is empty */
1777 while ( (inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x00);
1778 if (max
==0x0) keyboard_panic(00);
1782 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x01);
1783 if (max
==0x0) keyboard_panic(01);
1785 /* read self-test result, 0x55 should be returned from 0x60 */
1786 if ((inb(0x60) != 0x55)){
1787 keyboard_panic(991);
1790 /* send cmd = 0xAB, keyboard interface test */
1793 /* Wait until buffer is empty */
1795 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x10);
1796 if (max
==0x0) keyboard_panic(10);
1800 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x11);
1801 if (max
==0x0) keyboard_panic(11);
1803 /* read keyboard interface test result, */
1804 /* 0x00 should be returned form 0x60 */
1805 if ((inb(0x60) != 0x00)) {
1806 keyboard_panic(992);
1809 /* Enable Keyboard clock */
1813 /* ------------------- keyboard side ------------------------*/
1814 /* reset kerboard and self test (keyboard side) */
1817 /* Wait until buffer is empty */
1819 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x20);
1820 if (max
==0x0) keyboard_panic(20);
1824 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x21);
1825 if (max
==0x0) keyboard_panic(21);
1827 /* keyboard should return ACK */
1828 if ((inb(0x60) != 0xfa)) {
1829 keyboard_panic(993);
1834 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x31);
1835 if (max
==0x0) keyboard_panic(31);
1837 if ((inb(0x60) != 0xaa)) {
1838 keyboard_panic(994);
1841 /* Disable keyboard */
1844 /* Wait until buffer is empty */
1846 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x40);
1847 if (max
==0x0) keyboard_panic(40);
1851 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x41);
1852 if (max
==0x0) keyboard_panic(41);
1854 /* keyboard should return ACK */
1855 if ((inb(0x60) != 0xfa)) {
1856 keyboard_panic(995);
1859 /* Write Keyboard Mode */
1862 /* Wait until buffer is empty */
1864 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x50);
1865 if (max
==0x0) keyboard_panic(50);
1867 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
1870 /* Wait until buffer is empty */
1872 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x60);
1873 if (max
==0x0) keyboard_panic(60);
1875 /* Enable keyboard */
1878 /* Wait until buffer is empty */
1880 while ((inb(0x64) & 0x02) && (--max
>0)) outb(0x80, 0x70);
1881 if (max
==0x0) keyboard_panic(70);
1885 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x71);
1886 if (max
==0x0) keyboard_panic(70);
1888 /* keyboard should return ACK */
1889 if ((inb(0x60) != 0xfa)) {
1890 keyboard_panic(996);
1896 //--------------------------------------------------------------------------
1898 //--------------------------------------------------------------------------
1900 keyboard_panic(status
)
1903 // If you're getting a 993 keyboard panic here,
1904 // please see the comment in keyboard_init
1906 BX_PANIC("Keyboard error:%u\n",status
);
1909 //--------------------------------------------------------------------------
1910 // shutdown_status_panic
1911 // called when the shutdown statsu is not implemented, displays the status
1912 //--------------------------------------------------------------------------
1914 shutdown_status_panic(status
)
1917 BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u
)status
);
1920 void s3_resume_panic()
1922 BX_PANIC("Returned from s3_resume.\n");
1925 //--------------------------------------------------------------------------
1926 // print_bios_banner
1927 // displays a the bios version
1928 //--------------------------------------------------------------------------
1932 printf(BX_APPNAME
" BIOS - build: %s\n%s\nOptions: ",
1933 BIOS_BUILD_DATE
, bios_cvs_version_string
);
1941 #if BX_ELTORITO_BOOT
1950 //--------------------------------------------------------------------------
1951 // BIOS Boot Specification 1.0.1 compatibility
1953 // Very basic support for the BIOS Boot Specification, which allows expansion
1954 // ROMs to register themselves as boot devices, instead of just stealing the
1955 // INT 19h boot vector.
1957 // This is a hack: to do it properly requires a proper PnP BIOS and we aren't
1958 // one; we just lie to the option ROMs to make them behave correctly.
1959 // We also don't support letting option ROMs register as bootable disk
1960 // drives (BCVs), only as bootable devices (BEVs).
1962 // http://www.phoenix.com/en/Customer+Services/White+Papers-Specs/pc+industry+specifications.htm
1963 //--------------------------------------------------------------------------
1965 static char drivetypes
[][10]={"", "Floppy","Hard Disk","CD-Rom", "Network"};
1972 Bit16u ss
= get_SS();
1974 /* Clear out the IPL table. */
1975 memsetb(IPL_SEG
, IPL_TABLE_OFFSET
, 0, IPL_SIZE
);
1977 /* User selected device not set */
1978 write_word(IPL_SEG
, IPL_BOOTFIRST_OFFSET
, 0xFFFF);
1981 e
.type
= IPL_TYPE_FLOPPY
; e
.flags
= 0; e
.vector
= 0; e
.description
= 0; e
.reserved
= 0;
1982 memcpyb(IPL_SEG
, IPL_TABLE_OFFSET
+ count
* sizeof (e
), ss
, &e
, sizeof (e
));
1986 e
.type
= IPL_TYPE_HARDDISK
; e
.flags
= 0; e
.vector
= 0; e
.description
= 0; e
.reserved
= 0;
1987 memcpyb(IPL_SEG
, IPL_TABLE_OFFSET
+ count
* sizeof (e
), ss
, &e
, sizeof (e
));
1990 #if BX_ELTORITO_BOOT
1992 e
.type
= IPL_TYPE_CDROM
; e
.flags
= 0; e
.vector
= 0; e
.description
= 0; e
.reserved
= 0;
1993 memcpyb(IPL_SEG
, IPL_TABLE_OFFSET
+ count
* sizeof (e
), ss
, &e
, sizeof (e
));
1997 /* Remember how many devices we have */
1998 write_word(IPL_SEG
, IPL_COUNT_OFFSET
, count
);
1999 /* Not tried booting anything yet */
2000 write_word(IPL_SEG
, IPL_SEQUENCE_OFFSET
, 0xffff);
2004 get_boot_vector(i
, e
)
2005 Bit16u i
; ipl_entry_t
*e
;
2008 Bit16u ss
= get_SS();
2009 /* Get the count of boot devices, and refuse to overrun the array */
2010 count
= read_word(IPL_SEG
, IPL_COUNT_OFFSET
);
2011 if (i
>= count
) return 0;
2012 /* OK to read this device */
2013 memcpyb(ss
, e
, IPL_SEG
, IPL_TABLE_OFFSET
+ i
* sizeof (*e
), sizeof (*e
));
2017 #if BX_ELTORITO_BOOT
2019 interactive_bootkey()
2023 char description
[33];
2026 Bit16u ss
= get_SS();
2027 Bit16u valid_choice
= 0;
2029 while (check_for_keystroke())
2032 printf("Press F12 for boot menu.\n\n");
2034 delay_ticks_and_check_for_keystroke(11, 5); /* ~3 seconds */
2035 if (check_for_keystroke())
2037 scan_code
= get_keystroke();
2038 if (scan_code
== 0x86) /* F12 */
2040 while (check_for_keystroke())
2043 printf("Select boot device:\n\n");
2045 count
= read_word(IPL_SEG
, IPL_COUNT_OFFSET
);
2046 for (i
= 0; i
< count
; i
++)
2048 memcpyb(ss
, &e
, IPL_SEG
, IPL_TABLE_OFFSET
+ i
* sizeof (e
), sizeof (e
));
2049 printf("%d. ", i
+1);
2052 case IPL_TYPE_FLOPPY
:
2053 case IPL_TYPE_HARDDISK
:
2054 case IPL_TYPE_CDROM
:
2055 printf("%s\n", drivetypes
[e
.type
]);
2058 printf("%s", drivetypes
[4]);
2059 if (e
.description
!= 0)
2061 memcpyb(ss
, &description
, (Bit16u
)(e
.description
>> 16), (Bit16u
)(e
.description
& 0xffff), 32);
2062 description
[32] = 0;
2063 printf(" [%S]", ss
, description
);
2071 while (!valid_choice
) {
2072 scan_code
= get_keystroke();
2073 if (scan_code
== 0x01 || scan_code
== 0x58) /* ESC or F12 */
2077 else if (scan_code
<= count
)
2081 /* Set user selected device */
2082 write_word(IPL_SEG
, IPL_BOOTFIRST_OFFSET
, scan_code
);
2089 #endif // BX_ELTORITO_BOOT
2091 //--------------------------------------------------------------------------
2092 // print_boot_device
2093 // displays the boot device
2094 //--------------------------------------------------------------------------
2097 print_boot_device(e
)
2101 char description
[33];
2102 Bit16u ss
= get_SS();
2104 /* NIC appears as type 0x80 */
2105 if (type
== IPL_TYPE_BEV
) type
= 0x4;
2106 if (type
== 0 || type
> 0x4) BX_PANIC("Bad drive type\n");
2107 printf("Booting from %s", drivetypes
[type
]);
2108 /* print product string if BEV */
2109 if (type
== 4 && e
->description
!= 0) {
2110 /* first 32 bytes are significant */
2111 memcpyb(ss
, &description
, (Bit16u
)(e
->description
>> 16), (Bit16u
)(e
->description
& 0xffff), 32);
2112 /* terminate string */
2113 description
[32] = 0;
2114 printf(" [%S]", ss
, description
);
2119 //--------------------------------------------------------------------------
2120 // print_boot_failure
2121 // displays the reason why boot failed
2122 //--------------------------------------------------------------------------
2124 print_boot_failure(type
, reason
)
2125 Bit16u type
; Bit8u reason
;
2127 if (type
== 0 || type
> 0x3) BX_PANIC("Bad drive type\n");
2129 printf("Boot failed");
2131 /* Report the reason too */
2133 printf(": not a bootable disk");
2135 printf(": could not read the boot disk");
2140 //--------------------------------------------------------------------------
2141 // print_cdromboot_failure
2142 // displays the reason why boot failed
2143 //--------------------------------------------------------------------------
2145 print_cdromboot_failure( code
)
2148 bios_printf(BIOS_PRINTF_SCREEN
| BIOS_PRINTF_INFO
, "CDROM boot failure code : %04x\n",code
);
2156 BX_PANIC("NMI Handler called\n");
2162 BX_PANIC("INT18: BOOT FAILURE\n");
2169 outb(BX_DEBUG_PORT
+UART_LCR
, 0x03); /* setup for serial logging: 8N1 */
2171 BX_INFO("%s\n", bios_cvs_version_string
);
2180 // Use PS2 System Control port A to set A20 enable
2182 // get current setting first
2185 // change A20 status
2187 outb(0x92, oldval
| 0x02);
2189 outb(0x92, oldval
& 0xfd);
2191 return((oldval
& 0x02) != 0);
2209 Bit32u s3_wakeup_vector
;
2210 Bit8u s3_resume_flag
;
2212 s3_resume_flag
= read_byte(0x40, 0xb0);
2213 s3_wakeup_vector
= read_dword(0x40, 0xb2);
2215 BX_INFO("S3 resume called %x 0x%lx\n", s3_resume_flag
, s3_wakeup_vector
);
2216 if (s3_resume_flag
!= 0xFE || !s3_wakeup_vector
)
2219 write_byte(0x40, 0xb0, 0);
2221 /* setup wakeup vector */
2222 write_word(0x40, 0xb6, (s3_wakeup_vector
& 0xF)); /* IP */
2223 write_word(0x40, 0xb8, (s3_wakeup_vector
>> 4)); /* CS */
2225 BX_INFO("S3 resume jump to %x:%x\n", (s3_wakeup_vector
>> 4),
2226 (s3_wakeup_vector
& 0xF));
2235 // ---------------------------------------------------------------------------
2236 // Start of ATA/ATAPI Driver
2237 // ---------------------------------------------------------------------------
2239 // Global defines -- ATA register and register bits.
2240 // command block & control block regs
2241 #define ATA_CB_DATA 0 // data reg in/out pio_base_addr1+0
2242 #define ATA_CB_ERR 1 // error in pio_base_addr1+1
2243 #define ATA_CB_FR 1 // feature reg out pio_base_addr1+1
2244 #define ATA_CB_SC 2 // sector count in/out pio_base_addr1+2
2245 #define ATA_CB_SN 3 // sector number in/out pio_base_addr1+3
2246 #define ATA_CB_CL 4 // cylinder low in/out pio_base_addr1+4
2247 #define ATA_CB_CH 5 // cylinder high in/out pio_base_addr1+5
2248 #define ATA_CB_DH 6 // device head in/out pio_base_addr1+6
2249 #define ATA_CB_STAT 7 // primary status in pio_base_addr1+7
2250 #define ATA_CB_CMD 7 // command out pio_base_addr1+7
2251 #define ATA_CB_ASTAT 6 // alternate status in pio_base_addr2+6
2252 #define ATA_CB_DC 6 // device control out pio_base_addr2+6
2253 #define ATA_CB_DA 7 // device address in pio_base_addr2+7
2255 #define ATA_CB_ER_ICRC 0x80 // ATA Ultra DMA bad CRC
2256 #define ATA_CB_ER_BBK 0x80 // ATA bad block
2257 #define ATA_CB_ER_UNC 0x40 // ATA uncorrected error
2258 #define ATA_CB_ER_MC 0x20 // ATA media change
2259 #define ATA_CB_ER_IDNF 0x10 // ATA id not found
2260 #define ATA_CB_ER_MCR 0x08 // ATA media change request
2261 #define ATA_CB_ER_ABRT 0x04 // ATA command aborted
2262 #define ATA_CB_ER_NTK0 0x02 // ATA track 0 not found
2263 #define ATA_CB_ER_NDAM 0x01 // ATA address mark not found
2265 #define ATA_CB_ER_P_SNSKEY 0xf0 // ATAPI sense key (mask)
2266 #define ATA_CB_ER_P_MCR 0x08 // ATAPI Media Change Request
2267 #define ATA_CB_ER_P_ABRT 0x04 // ATAPI command abort
2268 #define ATA_CB_ER_P_EOM 0x02 // ATAPI End of Media
2269 #define ATA_CB_ER_P_ILI 0x01 // ATAPI Illegal Length Indication
2271 // ATAPI Interrupt Reason bits in the Sector Count reg (CB_SC)
2272 #define ATA_CB_SC_P_TAG 0xf8 // ATAPI tag (mask)
2273 #define ATA_CB_SC_P_REL 0x04 // ATAPI release
2274 #define ATA_CB_SC_P_IO 0x02 // ATAPI I/O
2275 #define ATA_CB_SC_P_CD 0x01 // ATAPI C/D
2277 // bits 7-4 of the device/head (CB_DH) reg
2278 #define ATA_CB_DH_DEV0 0xa0 // select device 0
2279 #define ATA_CB_DH_DEV1 0xb0 // select device 1
2280 #define ATA_CB_DH_LBA 0x40 // use LBA
2282 // status reg (CB_STAT and CB_ASTAT) bits
2283 #define ATA_CB_STAT_BSY 0x80 // busy
2284 #define ATA_CB_STAT_RDY 0x40 // ready
2285 #define ATA_CB_STAT_DF 0x20 // device fault
2286 #define ATA_CB_STAT_WFT 0x20 // write fault (old name)
2287 #define ATA_CB_STAT_SKC 0x10 // seek complete
2288 #define ATA_CB_STAT_SERV 0x10 // service
2289 #define ATA_CB_STAT_DRQ 0x08 // data request
2290 #define ATA_CB_STAT_CORR 0x04 // corrected
2291 #define ATA_CB_STAT_IDX 0x02 // index
2292 #define ATA_CB_STAT_ERR 0x01 // error (ATA)
2293 #define ATA_CB_STAT_CHK 0x01 // check (ATAPI)
2295 // device control reg (CB_DC) bits
2296 #define ATA_CB_DC_HD15 0x08 // bit should always be set to one
2297 #define ATA_CB_DC_SRST 0x04 // soft reset
2298 #define ATA_CB_DC_NIEN 0x02 // disable interrupts
2300 // Most mandtory and optional ATA commands (from ATA-3),
2301 #define ATA_CMD_CFA_ERASE_SECTORS 0xC0
2302 #define ATA_CMD_CFA_REQUEST_EXT_ERR_CODE 0x03
2303 #define ATA_CMD_CFA_TRANSLATE_SECTOR 0x87
2304 #define ATA_CMD_CFA_WRITE_MULTIPLE_WO_ERASE 0xCD
2305 #define ATA_CMD_CFA_WRITE_SECTORS_WO_ERASE 0x38
2306 #define ATA_CMD_CHECK_POWER_MODE1 0xE5
2307 #define ATA_CMD_CHECK_POWER_MODE2 0x98
2308 #define ATA_CMD_DEVICE_RESET 0x08
2309 #define ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC 0x90
2310 #define ATA_CMD_FLUSH_CACHE 0xE7
2311 #define ATA_CMD_FORMAT_TRACK 0x50
2312 #define ATA_CMD_IDENTIFY_DEVICE 0xEC
2313 #define ATA_CMD_IDENTIFY_DEVICE_PACKET 0xA1
2314 #define ATA_CMD_IDENTIFY_PACKET_DEVICE 0xA1
2315 #define ATA_CMD_IDLE1 0xE3
2316 #define ATA_CMD_IDLE2 0x97
2317 #define ATA_CMD_IDLE_IMMEDIATE1 0xE1
2318 #define ATA_CMD_IDLE_IMMEDIATE2 0x95
2319 #define ATA_CMD_INITIALIZE_DRIVE_PARAMETERS 0x91
2320 #define ATA_CMD_INITIALIZE_DEVICE_PARAMETERS 0x91
2321 #define ATA_CMD_NOP 0x00
2322 #define ATA_CMD_PACKET 0xA0
2323 #define ATA_CMD_READ_BUFFER 0xE4
2324 #define ATA_CMD_READ_DMA 0xC8
2325 #define ATA_CMD_READ_DMA_QUEUED 0xC7
2326 #define ATA_CMD_READ_MULTIPLE 0xC4
2327 #define ATA_CMD_READ_SECTORS 0x20
2328 #define ATA_CMD_READ_VERIFY_SECTORS 0x40
2329 #define ATA_CMD_RECALIBRATE 0x10
2330 #define ATA_CMD_REQUEST_SENSE 0x03
2331 #define ATA_CMD_SEEK 0x70
2332 #define ATA_CMD_SET_FEATURES 0xEF
2333 #define ATA_CMD_SET_MULTIPLE_MODE 0xC6
2334 #define ATA_CMD_SLEEP1 0xE6
2335 #define ATA_CMD_SLEEP2 0x99
2336 #define ATA_CMD_STANDBY1 0xE2
2337 #define ATA_CMD_STANDBY2 0x96
2338 #define ATA_CMD_STANDBY_IMMEDIATE1 0xE0
2339 #define ATA_CMD_STANDBY_IMMEDIATE2 0x94
2340 #define ATA_CMD_WRITE_BUFFER 0xE8
2341 #define ATA_CMD_WRITE_DMA 0xCA
2342 #define ATA_CMD_WRITE_DMA_QUEUED 0xCC
2343 #define ATA_CMD_WRITE_MULTIPLE 0xC5
2344 #define ATA_CMD_WRITE_SECTORS 0x30
2345 #define ATA_CMD_WRITE_VERIFY 0x3C
2347 #define ATA_IFACE_NONE 0x00
2348 #define ATA_IFACE_ISA 0x00
2349 #define ATA_IFACE_PCI 0x01
2351 #define ATA_TYPE_NONE 0x00
2352 #define ATA_TYPE_UNKNOWN 0x01
2353 #define ATA_TYPE_ATA 0x02
2354 #define ATA_TYPE_ATAPI 0x03
2356 #define ATA_DEVICE_NONE 0x00
2357 #define ATA_DEVICE_HD 0xFF
2358 #define ATA_DEVICE_CDROM 0x05
2360 #define ATA_MODE_NONE 0x00
2361 #define ATA_MODE_PIO16 0x00
2362 #define ATA_MODE_PIO32 0x01
2363 #define ATA_MODE_ISADMA 0x02
2364 #define ATA_MODE_PCIDMA 0x03
2365 #define ATA_MODE_USEIRQ 0x10
2367 #define ATA_TRANSLATION_NONE 0
2368 #define ATA_TRANSLATION_LBA 1
2369 #define ATA_TRANSLATION_LARGE 2
2370 #define ATA_TRANSLATION_RECHS 3
2372 #define ATA_DATA_NO 0x00
2373 #define ATA_DATA_IN 0x01
2374 #define ATA_DATA_OUT 0x02
2376 // ---------------------------------------------------------------------------
2377 // ATA/ATAPI driver : initialization
2378 // ---------------------------------------------------------------------------
2381 Bit16u ebda_seg
=read_word(0x0040,0x000E);
2382 Bit8u channel
, device
;
2384 // Channels info init.
2385 for (channel
=0; channel
<BX_MAX_ATA_INTERFACES
; channel
++) {
2386 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[channel
].iface
,ATA_IFACE_NONE
);
2387 write_word(ebda_seg
,&EbdaData
->ata
.channels
[channel
].iobase1
,0x0);
2388 write_word(ebda_seg
,&EbdaData
->ata
.channels
[channel
].iobase2
,0x0);
2389 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[channel
].irq
,0);
2392 // Devices info init.
2393 for (device
=0; device
<BX_MAX_ATA_DEVICES
; device
++) {
2394 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
,ATA_TYPE_NONE
);
2395 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
,ATA_DEVICE_NONE
);
2396 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].removable
,0);
2397 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].lock
,0);
2398 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].mode
,ATA_MODE_NONE
);
2399 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].blksize
,0);
2400 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].translation
,ATA_TRANSLATION_NONE
);
2401 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.heads
,0);
2402 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.cylinders
,0);
2403 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.spt
,0);
2404 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.heads
,0);
2405 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.cylinders
,0);
2406 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.spt
,0);
2408 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_low
,0L);
2409 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_high
,0L);
2412 // hdidmap and cdidmap init.
2413 for (device
=0; device
<BX_MAX_ATA_DEVICES
; device
++) {
2414 write_byte(ebda_seg
,&EbdaData
->ata
.hdidmap
[device
],BX_MAX_ATA_DEVICES
);
2415 write_byte(ebda_seg
,&EbdaData
->ata
.cdidmap
[device
],BX_MAX_ATA_DEVICES
);
2418 write_byte(ebda_seg
,&EbdaData
->ata
.hdcount
,0);
2419 write_byte(ebda_seg
,&EbdaData
->ata
.cdcount
,0);
2425 #define NOT_BSY_DRQ 3
2426 #define NOT_BSY_NOT_DRQ 4
2427 #define NOT_BSY_RDY 5
2429 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
2432 static int await_ide(when_done
,base
,timeout
)
2437 Bit32u time
=0,last
=0;
2440 status
= inb(base
+ ATA_CB_STAT
); // for the times you're supposed to throw one away
2442 status
= inb(base
+ATA_CB_STAT
);
2444 if (when_done
== BSY
)
2445 result
= status
& ATA_CB_STAT_BSY
;
2446 else if (when_done
== NOT_BSY
)
2447 result
= !(status
& ATA_CB_STAT_BSY
);
2448 else if (when_done
== NOT_BSY_DRQ
)
2449 result
= !(status
& ATA_CB_STAT_BSY
) && (status
& ATA_CB_STAT_DRQ
);
2450 else if (when_done
== NOT_BSY_NOT_DRQ
)
2451 result
= !(status
& ATA_CB_STAT_BSY
) && !(status
& ATA_CB_STAT_DRQ
);
2452 else if (when_done
== NOT_BSY_RDY
)
2453 result
= !(status
& ATA_CB_STAT_BSY
) && (status
& ATA_CB_STAT_RDY
);
2454 else if (when_done
== TIMEOUT
)
2457 if (result
) return 0;
2458 if (time
>>16 != last
) // mod 2048 each 16 ms
2461 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
);
2463 if (status
& ATA_CB_STAT_ERR
)
2465 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
);
2468 if ((timeout
== 0) || ((time
>>11) > timeout
)) break;
2470 BX_INFO("IDE time out\n");
2474 // ---------------------------------------------------------------------------
2475 // ATA/ATAPI driver : device detection
2476 // ---------------------------------------------------------------------------
2480 Bit16u ebda_seg
=read_word(0x0040,0x000E);
2481 Bit8u hdcount
, cdcount
, device
, type
;
2482 Bit8u buffer
[0x0200];
2484 #if BX_MAX_ATA_INTERFACES > 0
2485 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[0].iface
,ATA_IFACE_ISA
);
2486 write_word(ebda_seg
,&EbdaData
->ata
.channels
[0].iobase1
,0x1f0);
2487 write_word(ebda_seg
,&EbdaData
->ata
.channels
[0].iobase2
,0x3f0);
2488 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[0].irq
,14);
2490 #if BX_MAX_ATA_INTERFACES > 1
2491 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[1].iface
,ATA_IFACE_ISA
);
2492 write_word(ebda_seg
,&EbdaData
->ata
.channels
[1].iobase1
,0x170);
2493 write_word(ebda_seg
,&EbdaData
->ata
.channels
[1].iobase2
,0x370);
2494 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[1].irq
,15);
2496 #if BX_MAX_ATA_INTERFACES > 2
2497 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[2].iface
,ATA_IFACE_ISA
);
2498 write_word(ebda_seg
,&EbdaData
->ata
.channels
[2].iobase1
,0x1e8);
2499 write_word(ebda_seg
,&EbdaData
->ata
.channels
[2].iobase2
,0x3e0);
2500 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[2].irq
,12);
2502 #if BX_MAX_ATA_INTERFACES > 3
2503 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[3].iface
,ATA_IFACE_ISA
);
2504 write_word(ebda_seg
,&EbdaData
->ata
.channels
[3].iobase1
,0x168);
2505 write_word(ebda_seg
,&EbdaData
->ata
.channels
[3].iobase2
,0x360);
2506 write_byte(ebda_seg
,&EbdaData
->ata
.channels
[3].irq
,11);
2508 #if BX_MAX_ATA_INTERFACES > 4
2509 #error Please fill the ATA interface informations
2515 for(device
=0; device
<BX_MAX_ATA_DEVICES
; device
++) {
2516 Bit16u iobase1
, iobase2
;
2517 Bit8u channel
, slave
, shift
;
2518 Bit8u sc
, sn
, cl
, ch
, st
;
2520 channel
= device
/ 2;
2523 iobase1
=read_word(ebda_seg
,&EbdaData
->ata
.channels
[channel
].iobase1
);
2524 iobase2
=read_word(ebda_seg
,&EbdaData
->ata
.channels
[channel
].iobase2
);
2526 // Disable interrupts
2527 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
);
2530 outb(iobase1
+ATA_CB_DH
, slave
? ATA_CB_DH_DEV1
: ATA_CB_DH_DEV0
);
2531 outb(iobase1
+ATA_CB_SC
, 0x55);
2532 outb(iobase1
+ATA_CB_SN
, 0xaa);
2533 outb(iobase1
+ATA_CB_SC
, 0xaa);
2534 outb(iobase1
+ATA_CB_SN
, 0x55);
2535 outb(iobase1
+ATA_CB_SC
, 0x55);
2536 outb(iobase1
+ATA_CB_SN
, 0xaa);
2538 // If we found something
2539 sc
= inb(iobase1
+ATA_CB_SC
);
2540 sn
= inb(iobase1
+ATA_CB_SN
);
2542 if ( (sc
== 0x55) && (sn
== 0xaa) ) {
2543 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
,ATA_TYPE_UNKNOWN
);
2545 // reset the channel
2548 // check for ATA or ATAPI
2549 outb(iobase1
+ATA_CB_DH
, slave
? ATA_CB_DH_DEV1
: ATA_CB_DH_DEV0
);
2550 sc
= inb(iobase1
+ATA_CB_SC
);
2551 sn
= inb(iobase1
+ATA_CB_SN
);
2552 if ((sc
==0x01) && (sn
==0x01)) {
2553 cl
= inb(iobase1
+ATA_CB_CL
);
2554 ch
= inb(iobase1
+ATA_CB_CH
);
2555 st
= inb(iobase1
+ATA_CB_STAT
);
2557 if ((cl
==0x14) && (ch
==0xeb)) {
2558 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
,ATA_TYPE_ATAPI
);
2559 } else if ((cl
==0x00) && (ch
==0x00) && (st
!=0x00)) {
2560 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
,ATA_TYPE_ATA
);
2561 } else if ((cl
==0xff) && (ch
==0xff)) {
2562 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
,ATA_TYPE_NONE
);
2567 type
=read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
);
2569 // Now we send a IDENTIFY command to ATA device
2570 if(type
== ATA_TYPE_ATA
) {
2571 Bit32u sectors_low
, sectors_high
;
2572 Bit16u cylinders
, heads
, spt
, blksize
;
2573 Bit8u translation
, removable
, mode
;
2575 //Temporary values to do the transfer
2576 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
,ATA_DEVICE_HD
);
2577 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].mode
, ATA_MODE_PIO16
);
2579 if (ata_cmd_data_in(device
,ATA_CMD_IDENTIFY_DEVICE
, 1, 0, 0, 0, 0L, 0L, get_SS(),buffer
) !=0 )
2580 BX_PANIC("ata-detect: Failed to detect ATA device\n");
2582 removable
= (read_byte(get_SS(),buffer
+0) & 0x80) ? 1 : 0;
2583 mode
= read_byte(get_SS(),buffer
+96) ? ATA_MODE_PIO32
: ATA_MODE_PIO16
;
2584 blksize
= read_word(get_SS(),buffer
+10);
2586 cylinders
= read_word(get_SS(),buffer
+(1*2)); // word 1
2587 heads
= read_word(get_SS(),buffer
+(3*2)); // word 3
2588 spt
= read_word(get_SS(),buffer
+(6*2)); // word 6
2590 if (read_word(get_SS(),buffer
+(83*2)) & (1 << 10)) { // word 83 - lba48 support
2591 sectors_low
= read_dword(get_SS(),buffer
+(100*2)); // word 100 and word 101
2592 sectors_high
= read_dword(get_SS(),buffer
+(102*2)); // word 102 and word 103
2594 sectors_low
= read_dword(get_SS(),buffer
+(60*2)); // word 60 and word 61
2598 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
,ATA_DEVICE_HD
);
2599 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].removable
, removable
);
2600 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].mode
, mode
);
2601 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].blksize
, blksize
);
2602 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.heads
, heads
);
2603 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.cylinders
, cylinders
);
2604 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].pchs
.spt
, spt
);
2605 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_low
, sectors_low
);
2606 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_high
, sectors_high
);
2607 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel
, slave
,cylinders
, heads
, spt
);
2609 translation
= inb_cmos(0x39 + channel
/2);
2610 for (shift
=device
%4; shift
>0; shift
--) translation
>>= 2;
2611 translation
&= 0x03;
2613 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].translation
, translation
);
2615 switch (translation
) {
2616 case ATA_TRANSLATION_NONE
:
2619 case ATA_TRANSLATION_LBA
:
2622 case ATA_TRANSLATION_LARGE
:
2625 case ATA_TRANSLATION_RECHS
:
2629 switch (translation
) {
2630 case ATA_TRANSLATION_NONE
:
2632 case ATA_TRANSLATION_LBA
:
2635 heads
= sectors_low
/ 1024;
2636 if (heads
>128) heads
= 255;
2637 else if (heads
>64) heads
= 128;
2638 else if (heads
>32) heads
= 64;
2639 else if (heads
>16) heads
= 32;
2641 cylinders
= sectors_low
/ heads
;
2643 case ATA_TRANSLATION_RECHS
:
2644 // Take care not to overflow
2646 if(cylinders
>61439) cylinders
=61439;
2648 cylinders
= (Bit16u
)((Bit32u
)(cylinders
)*16/15);
2650 // then go through the large bitshift process
2651 case ATA_TRANSLATION_LARGE
:
2652 while(cylinders
> 1024) {
2656 // If we max out the head count
2657 if (heads
> 127) break;
2661 // clip to 1024 cylinders in lchs
2662 if (cylinders
> 1024) cylinders
=1024;
2663 BX_INFO(" LCHS=%d/%d/%d\n", cylinders
, heads
, spt
);
2665 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.heads
, heads
);
2666 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.cylinders
, cylinders
);
2667 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].lchs
.spt
, spt
);
2670 write_byte(ebda_seg
,&EbdaData
->ata
.hdidmap
[hdcount
], device
);
2674 // Now we send a IDENTIFY command to ATAPI device
2675 if(type
== ATA_TYPE_ATAPI
) {
2677 Bit8u type
, removable
, mode
;
2680 //Temporary values to do the transfer
2681 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
,ATA_DEVICE_CDROM
);
2682 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].mode
, ATA_MODE_PIO16
);
2684 if (ata_cmd_data_in(device
,ATA_CMD_IDENTIFY_DEVICE_PACKET
, 1, 0, 0, 0, 0L, 0L, get_SS(),buffer
) != 0)
2685 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
2687 type
= read_byte(get_SS(),buffer
+1) & 0x1f;
2688 removable
= (read_byte(get_SS(),buffer
+0) & 0x80) ? 1 : 0;
2689 mode
= read_byte(get_SS(),buffer
+96) ? ATA_MODE_PIO32
: ATA_MODE_PIO16
;
2692 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
, type
);
2693 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].removable
, removable
);
2694 write_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].mode
, mode
);
2695 write_word(ebda_seg
,&EbdaData
->ata
.devices
[device
].blksize
, blksize
);
2698 write_byte(ebda_seg
,&EbdaData
->ata
.cdidmap
[cdcount
], device
);
2705 Bit8u c
, i
, version
, model
[41];
2709 sizeinmb
= (read_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_high
) << 21)
2710 | (read_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_low
) >> 11);
2711 case ATA_TYPE_ATAPI
:
2712 // Read ATA/ATAPI version
2713 ataversion
=((Bit16u
)(read_byte(get_SS(),buffer
+161))<<8)|read_byte(get_SS(),buffer
+160);
2714 for(version
=15;version
>0;version
--) {
2715 if((ataversion
&(1<<version
))!=0)
2721 write_byte(get_SS(),model
+(i
*2),read_byte(get_SS(),buffer
+(i
*2)+54+1));
2722 write_byte(get_SS(),model
+(i
*2)+1,read_byte(get_SS(),buffer
+(i
*2)+54));
2726 write_byte(get_SS(),model
+40,0x00);
2728 if(read_byte(get_SS(),model
+i
)==0x20)
2729 write_byte(get_SS(),model
+i
,0x00);
2733 write_byte(get_SS(),model
+36,0x00);
2735 write_byte(get_SS(),model
+i
,0x2E);
2743 printf("ata%d %s: ",channel
,slave
?" slave":"master");
2744 i
=0; while(c
=read_byte(get_SS(),model
+i
++)) printf("%c",c
);
2745 if (sizeinmb
< (1UL<<16))
2746 printf(" ATA-%d Hard-Disk (%4u MBytes)\n", version
, (Bit16u
)sizeinmb
);
2748 printf(" ATA-%d Hard-Disk (%4u GBytes)\n", version
, (Bit16u
)(sizeinmb
>>10));
2750 case ATA_TYPE_ATAPI
:
2751 printf("ata%d %s: ",channel
,slave
?" slave":"master");
2752 i
=0; while(c
=read_byte(get_SS(),model
+i
++)) printf("%c",c
);
2753 if(read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
)==ATA_DEVICE_CDROM
)
2754 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version
);
2756 printf(" ATAPI-%d Device\n",version
);
2758 case ATA_TYPE_UNKNOWN
:
2759 printf("ata%d %s: Unknown device\n",channel
,slave
?" slave":"master");
2765 // Store the devices counts
2766 write_byte(ebda_seg
,&EbdaData
->ata
.hdcount
, hdcount
);
2767 write_byte(ebda_seg
,&EbdaData
->ata
.cdcount
, cdcount
);
2768 write_byte(0x40,0x75, hdcount
);
2772 // FIXME : should use bios=cmos|auto|disable bits
2773 // FIXME : should know about translation bits
2774 // FIXME : move hard_drive_post here
2778 // ---------------------------------------------------------------------------
2779 // ATA/ATAPI driver : software reset
2780 // ---------------------------------------------------------------------------
2782 // 8.2.1 Software reset - Device 0
2784 void ata_reset(device
)
2787 Bit16u ebda_seg
=read_word(0x0040,0x000E);
2788 Bit16u iobase1
, iobase2
;
2789 Bit8u channel
, slave
, sn
, sc
;
2793 channel
= device
/ 2;
2796 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
2797 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
2801 // 8.2.1 (a) -- set SRST in DC
2802 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
| ATA_CB_DC_SRST
);
2804 // 8.2.1 (b) -- wait for BSY
2805 await_ide(BSY
, iobase1
, 20);
2807 // 8.2.1 (f) -- clear SRST
2808 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
);
2810 type
=read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
);
2811 if (type
!= ATA_TYPE_NONE
) {
2813 // 8.2.1 (g) -- check for sc==sn==0x01
2815 outb(iobase1
+ATA_CB_DH
, slave
?ATA_CB_DH_DEV1
:ATA_CB_DH_DEV0
);
2816 sc
= inb(iobase1
+ATA_CB_SC
);
2817 sn
= inb(iobase1
+ATA_CB_SN
);
2819 if ( (sc
==0x01) && (sn
==0x01) ) {
2820 if (type
== ATA_TYPE_ATA
) //ATA
2821 await_ide(NOT_BSY_RDY
, iobase1
, IDE_TIMEOUT
);
2823 await_ide(NOT_BSY
, iobase1
, IDE_TIMEOUT
);
2826 // 8.2.1 (h) -- wait for not BSY
2827 await_ide(NOT_BSY
, iobase1
, IDE_TIMEOUT
);
2830 // Enable interrupts
2831 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
);
2834 // ---------------------------------------------------------------------------
2835 // ATA/ATAPI driver : execute a non data command
2836 // ---------------------------------------------------------------------------
2838 Bit16u
ata_cmd_non_data()
2841 // ---------------------------------------------------------------------------
2842 // ATA/ATAPI driver : execute a data-in command
2843 // ---------------------------------------------------------------------------
2848 // 3 : expected DRQ=1
2849 // 4 : no sectors left to read/verify
2850 // 5 : more sectors to read/verify
2851 // 6 : no sectors left to write
2852 // 7 : more sectors to write
2853 Bit16u
ata_cmd_data_in(device
, command
, count
, cylinder
, head
, sector
, lba_low
, lba_high
, segment
, offset
)
2854 Bit16u device
, command
, count
, cylinder
, head
, sector
, segment
, offset
;
2855 Bit32u lba_low
, lba_high
;
2857 Bit16u ebda_seg
=read_word(0x0040,0x000E);
2858 Bit16u iobase1
, iobase2
, blksize
;
2859 Bit8u channel
, slave
;
2860 Bit8u status
, current
, mode
;
2862 channel
= device
/ 2;
2865 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
2866 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
2867 mode
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].mode
);
2868 blksize
= 0x200; // was = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
2869 if (mode
== ATA_MODE_PIO32
) blksize
>>=2;
2872 // Reset count of transferred data
2873 write_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
,0);
2874 write_dword(ebda_seg
, &EbdaData
->ata
.trsfbytes
,0L);
2877 status
= inb(iobase1
+ ATA_CB_STAT
);
2878 if (status
& ATA_CB_STAT_BSY
) return 1;
2880 outb(iobase2
+ ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
);
2882 // sector will be 0 only on lba access. Convert to lba-chs
2884 if ((count
>= 1 << 8) || lba_high
|| (lba_low
+ count
>= 1UL << 28)) {
2885 outb(iobase1
+ ATA_CB_FR
, 0x00);
2886 outb(iobase1
+ ATA_CB_SC
, (count
>> 8) & 0xff);
2887 outb(iobase1
+ ATA_CB_SN
, lba_low
>> 24);
2888 outb(iobase1
+ ATA_CB_CL
, lba_high
& 0xff);
2889 outb(iobase1
+ ATA_CB_CH
, lba_high
>> 8);
2891 count
&= (1UL << 8) - 1;
2892 lba_low
&= (1UL << 24) - 1;
2894 sector
= (Bit16u
) (lba_low
& 0x000000ffL
);
2895 cylinder
= (Bit16u
) ((lba_low
>>8) & 0x0000ffffL
);
2896 head
= ((Bit16u
) ((lba_low
>>24) & 0x0000000fL
)) | ATA_CB_DH_LBA
;
2899 outb(iobase1
+ ATA_CB_FR
, 0x00);
2900 outb(iobase1
+ ATA_CB_SC
, count
);
2901 outb(iobase1
+ ATA_CB_SN
, sector
);
2902 outb(iobase1
+ ATA_CB_CL
, cylinder
& 0x00ff);
2903 outb(iobase1
+ ATA_CB_CH
, cylinder
>> 8);
2904 outb(iobase1
+ ATA_CB_DH
, (slave
? ATA_CB_DH_DEV1
: ATA_CB_DH_DEV0
) | (Bit8u
) head
);
2905 outb(iobase1
+ ATA_CB_CMD
, command
);
2907 await_ide(NOT_BSY_DRQ
, iobase1
, IDE_TIMEOUT
);
2908 status
= inb(iobase1
+ ATA_CB_STAT
);
2910 if (status
& ATA_CB_STAT_ERR
) {
2911 BX_DEBUG_ATA("ata_cmd_data_in : read error\n");
2913 } else if ( !(status
& ATA_CB_STAT_DRQ
) ) {
2914 BX_DEBUG_ATA("ata_cmd_data_in : DRQ not set (status %02x)\n", (unsigned) status
);
2918 // FIXME : move seg/off translation here
2921 sti
;; enable higher priority interrupts
2929 mov di
, _ata_cmd_data_in
.offset
+ 2[bp
]
2930 mov ax
, _ata_cmd_data_in
.segment
+ 2[bp
]
2931 mov cx
, _ata_cmd_data_in
.blksize
+ 2[bp
]
2933 ;; adjust
if there will be an overrun
. 2K max sector size
2935 jbe ata_in_no_adjust
2938 sub di
, #0x0800 ;; sub 2 kbytes from offset
2939 add ax
, #0x0080 ;; add 2 Kbytes to segment
2942 mov es
, ax
;; segment in es
2944 mov dx
, _ata_cmd_data_in
.iobase1
+ 2[bp
] ;; ATA data read port
2946 mov ah
, _ata_cmd_data_in
.mode
+ 2[bp
]
2947 cmp ah
, #ATA_MODE_PIO32
2952 insw
;; CX words transfered from
port(DX
) to ES
:[DI
]
2957 insd
;; CX dwords transfered from
port(DX
) to ES
:[DI
]
2960 mov _ata_cmd_data_in
.offset
+ 2[bp
], di
2961 mov _ata_cmd_data_in
.segment
+ 2[bp
], es
2966 write_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
,current
);
2968 await_ide(NOT_BSY
, iobase1
, IDE_TIMEOUT
);
2969 status
= inb(iobase1
+ ATA_CB_STAT
);
2971 if ( (status
& (ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
| ATA_CB_STAT_DRQ
| ATA_CB_STAT_ERR
) )
2972 != ATA_CB_STAT_RDY
) {
2973 BX_DEBUG_ATA("ata_cmd_data_in : no sectors left (status %02x)\n", (unsigned) status
);
2979 if ( (status
& (ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
| ATA_CB_STAT_DRQ
| ATA_CB_STAT_ERR
) )
2980 != (ATA_CB_STAT_RDY
| ATA_CB_STAT_DRQ
) ) {
2981 BX_DEBUG_ATA("ata_cmd_data_in : more sectors left (status %02x)\n", (unsigned) status
);
2987 // Enable interrupts
2988 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
);
2992 // ---------------------------------------------------------------------------
2993 // ATA/ATAPI driver : execute a data-out command
2994 // ---------------------------------------------------------------------------
2999 // 3 : expected DRQ=1
3000 // 4 : no sectors left to read/verify
3001 // 5 : more sectors to read/verify
3002 // 6 : no sectors left to write
3003 // 7 : more sectors to write
3004 Bit16u
ata_cmd_data_out(device
, command
, count
, cylinder
, head
, sector
, lba_low
, lba_high
, segment
, offset
)
3005 Bit16u device
, command
, count
, cylinder
, head
, sector
, segment
, offset
;
3006 Bit32u lba_low
, lba_high
;
3008 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3009 Bit16u iobase1
, iobase2
, blksize
;
3010 Bit8u channel
, slave
;
3011 Bit8u status
, current
, mode
;
3013 channel
= device
/ 2;
3016 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
3017 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
3018 mode
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].mode
);
3019 blksize
= 0x200; // was = read_word(ebda_seg, &EbdaData->ata.devices[device].blksize);
3020 if (mode
== ATA_MODE_PIO32
) blksize
>>=2;
3023 // Reset count of transferred data
3024 write_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
,0);
3025 write_dword(ebda_seg
, &EbdaData
->ata
.trsfbytes
,0L);
3028 status
= inb(iobase1
+ ATA_CB_STAT
);
3029 if (status
& ATA_CB_STAT_BSY
) return 1;
3031 outb(iobase2
+ ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
);
3033 // sector will be 0 only on lba access. Convert to lba-chs
3035 if ((count
>= 1 << 8) || lba_high
|| (lba_low
+ count
>= 1UL << 28)) {
3036 outb(iobase1
+ ATA_CB_FR
, 0x00);
3037 outb(iobase1
+ ATA_CB_SC
, (count
>> 8) & 0xff);
3038 outb(iobase1
+ ATA_CB_SN
, lba_low
>> 24);
3039 outb(iobase1
+ ATA_CB_CL
, lba_high
& 0xff);
3040 outb(iobase1
+ ATA_CB_CH
, lba_high
>> 8);
3042 count
&= (1UL << 8) - 1;
3043 lba_low
&= (1UL << 24) - 1;
3045 sector
= (Bit16u
) (lba_low
& 0x000000ffL
);
3046 cylinder
= (Bit16u
) ((lba_low
>>8) & 0x0000ffffL
);
3047 head
= ((Bit16u
) ((lba_low
>>24) & 0x0000000fL
)) | ATA_CB_DH_LBA
;
3050 outb(iobase1
+ ATA_CB_FR
, 0x00);
3051 outb(iobase1
+ ATA_CB_SC
, count
);
3052 outb(iobase1
+ ATA_CB_SN
, sector
);
3053 outb(iobase1
+ ATA_CB_CL
, cylinder
& 0x00ff);
3054 outb(iobase1
+ ATA_CB_CH
, cylinder
>> 8);
3055 outb(iobase1
+ ATA_CB_DH
, (slave
? ATA_CB_DH_DEV1
: ATA_CB_DH_DEV0
) | (Bit8u
) head
);
3056 outb(iobase1
+ ATA_CB_CMD
, command
);
3058 await_ide(NOT_BSY_DRQ
, iobase1
, IDE_TIMEOUT
);
3059 status
= inb(iobase1
+ ATA_CB_STAT
);
3061 if (status
& ATA_CB_STAT_ERR
) {
3062 BX_DEBUG_ATA("ata_cmd_data_out : read error\n");
3064 } else if ( !(status
& ATA_CB_STAT_DRQ
) ) {
3065 BX_DEBUG_ATA("ata_cmd_data_out : DRQ not set (status %02x)\n", (unsigned) status
);
3069 // FIXME : move seg/off translation here
3072 sti
;; enable higher priority interrupts
3080 mov si
, _ata_cmd_data_out
.offset
+ 2[bp
]
3081 mov ax
, _ata_cmd_data_out
.segment
+ 2[bp
]
3082 mov cx
, _ata_cmd_data_out
.blksize
+ 2[bp
]
3084 ;; adjust
if there will be an overrun
. 2K max sector size
3086 jbe ata_out_no_adjust
3089 sub si
, #0x0800 ;; sub 2 kbytes from offset
3090 add ax
, #0x0080 ;; add 2 Kbytes to segment
3093 mov es
, ax
;; segment in es
3095 mov dx
, _ata_cmd_data_out
.iobase1
+ 2[bp
] ;; ATA data write port
3097 mov ah
, _ata_cmd_data_out
.mode
+ 2[bp
]
3098 cmp ah
, #ATA_MODE_PIO32
3104 outsw
;; CX words transfered from
port(DX
) to ES
:[SI
]
3110 outsd
;; CX dwords transfered from
port(DX
) to ES
:[SI
]
3113 mov _ata_cmd_data_out
.offset
+ 2[bp
], si
3114 mov _ata_cmd_data_out
.segment
+ 2[bp
], es
3119 write_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
,current
);
3121 status
= inb(iobase1
+ ATA_CB_STAT
);
3123 if ( (status
& (ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
| ATA_CB_STAT_DF
| ATA_CB_STAT_DRQ
| ATA_CB_STAT_ERR
) )
3124 != ATA_CB_STAT_RDY
) {
3125 BX_DEBUG_ATA("ata_cmd_data_out : no sectors left (status %02x)\n", (unsigned) status
);
3131 if ( (status
& (ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
| ATA_CB_STAT_DRQ
| ATA_CB_STAT_ERR
) )
3132 != (ATA_CB_STAT_RDY
| ATA_CB_STAT_DRQ
) ) {
3133 BX_DEBUG_ATA("ata_cmd_data_out : more sectors left (status %02x)\n", (unsigned) status
);
3139 // Enable interrupts
3140 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
);
3144 // ---------------------------------------------------------------------------
3145 // ATA/ATAPI driver : execute a packet command
3146 // ---------------------------------------------------------------------------
3149 // 1 : error in parameters
3153 Bit16u
ata_cmd_packet(device
, cmdlen
, cmdseg
, cmdoff
, header
, length
, inout
, bufseg
, bufoff
)
3155 Bit16u device
,cmdseg
, cmdoff
, bufseg
, bufoff
;
3159 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3160 Bit16u iobase1
, iobase2
;
3161 Bit16u lcount
, lbefore
, lafter
, count
;
3162 Bit8u channel
, slave
;
3163 Bit8u status
, mode
, lmode
;
3164 Bit32u total
, transfer
;
3166 channel
= device
/ 2;
3169 // Data out is not supported yet
3170 if (inout
== ATA_DATA_OUT
) {
3171 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
3175 // The header length must be even
3177 BX_DEBUG_ATA("ata_cmd_packet : header must be even (%04x)\n",header
);
3181 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
3182 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
3183 mode
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].mode
);
3186 if (cmdlen
< 12) cmdlen
=12;
3187 if (cmdlen
> 12) cmdlen
=16;
3190 // Reset count of transferred data
3191 write_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
,0);
3192 write_dword(ebda_seg
, &EbdaData
->ata
.trsfbytes
,0L);
3194 status
= inb(iobase1
+ ATA_CB_STAT
);
3195 if (status
& ATA_CB_STAT_BSY
) return 2;
3197 outb(iobase2
+ ATA_CB_DC
, ATA_CB_DC_HD15
| ATA_CB_DC_NIEN
);
3198 outb(iobase1
+ ATA_CB_FR
, 0x00);
3199 outb(iobase1
+ ATA_CB_SC
, 0x00);
3200 outb(iobase1
+ ATA_CB_SN
, 0x00);
3201 outb(iobase1
+ ATA_CB_CL
, 0xfff0 & 0x00ff);
3202 outb(iobase1
+ ATA_CB_CH
, 0xfff0 >> 8);
3203 outb(iobase1
+ ATA_CB_DH
, slave
? ATA_CB_DH_DEV1
: ATA_CB_DH_DEV0
);
3204 outb(iobase1
+ ATA_CB_CMD
, ATA_CMD_PACKET
);
3206 // Device should ok to receive command
3207 await_ide(NOT_BSY_DRQ
, iobase1
, IDE_TIMEOUT
);
3208 status
= inb(iobase1
+ ATA_CB_STAT
);
3210 if (status
& ATA_CB_STAT_ERR
) {
3211 BX_DEBUG_ATA("ata_cmd_packet : error, status is %02x\n",status
);
3213 } else if ( !(status
& ATA_CB_STAT_DRQ
) ) {
3214 BX_DEBUG_ATA("ata_cmd_packet : DRQ not set (status %02x)\n", (unsigned) status
);
3218 // Normalize address
3219 cmdseg
+= (cmdoff
/ 16);
3222 // Send command to device
3224 sti
;; enable higher priority interrupts
3229 mov si
, _ata_cmd_packet
.cmdoff
+ 2[bp
]
3230 mov ax
, _ata_cmd_packet
.cmdseg
+ 2[bp
]
3231 mov cx
, _ata_cmd_packet
.cmdlen
+ 2[bp
]
3232 mov es
, ax
;; segment in es
3234 mov dx
, _ata_cmd_packet
.iobase1
+ 2[bp
] ;; ATA data write port
3238 outsw
;; CX words transfered from
port(DX
) to ES
:[SI
]
3243 if (inout
== ATA_DATA_NO
) {
3244 await_ide(NOT_BSY
, iobase1
, IDE_TIMEOUT
);
3245 status
= inb(iobase1
+ ATA_CB_STAT
);
3252 if (loops
== 0) {//first time through
3253 status
= inb(iobase2
+ ATA_CB_ASTAT
);
3254 await_ide(NOT_BSY_DRQ
, iobase1
, IDE_TIMEOUT
);
3257 await_ide(NOT_BSY
, iobase1
, IDE_TIMEOUT
);
3260 status
= inb(iobase1
+ ATA_CB_STAT
);
3261 sc
= inb(iobase1
+ ATA_CB_SC
);
3263 // Check if command completed
3264 if(((inb(iobase1
+ ATA_CB_SC
)&0x7)==0x3) &&
3265 ((status
& (ATA_CB_STAT_RDY
| ATA_CB_STAT_ERR
)) == ATA_CB_STAT_RDY
)) break;
3267 if (status
& ATA_CB_STAT_ERR
) {
3268 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status
);
3272 // Normalize address
3273 bufseg
+= (bufoff
/ 16);
3276 // Get the byte count
3277 lcount
= ((Bit16u
)(inb(iobase1
+ ATA_CB_CH
))<<8)+inb(iobase1
+ ATA_CB_CL
);
3279 // adjust to read what we want
3292 lafter
=lcount
-length
;
3304 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) ",lbefore
+lcount
+lafter
,lbefore
,lcount
,lafter
);
3305 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",bufseg
,bufoff
);
3307 // If counts not dividable by 4, use 16bits mode
3309 if (lbefore
& 0x03) lmode
=ATA_MODE_PIO16
;
3310 if (lcount
& 0x03) lmode
=ATA_MODE_PIO16
;
3311 if (lafter
& 0x03) lmode
=ATA_MODE_PIO16
;
3313 // adds an extra byte if count are odd. before is always even
3314 if (lcount
& 0x01) {
3316 if ((lafter
> 0) && (lafter
& 0x01)) {
3321 if (lmode
== ATA_MODE_PIO32
) {
3322 lcount
>>=2; lbefore
>>=2; lafter
>>=2;
3325 lcount
>>=1; lbefore
>>=1; lafter
>>=1;
3334 mov dx
, _ata_cmd_packet
.iobase1
+ 2[bp
] ;; ATA data read port
3336 mov cx
, _ata_cmd_packet
.lbefore
+ 2[bp
]
3337 jcxz ata_packet_no_before
3339 mov ah
, _ata_cmd_packet
.lmode
+ 2[bp
]
3340 cmp ah
, #ATA_MODE_PIO32
3341 je ata_packet_in_before_32
3343 ata_packet_in_before_16
:
3345 loop ata_packet_in_before_16
3346 jmp ata_packet_no_before
3348 ata_packet_in_before_32
:
3350 ata_packet_in_before_32_loop
:
3352 loop ata_packet_in_before_32_loop
3355 ata_packet_no_before
:
3356 mov cx
, _ata_cmd_packet
.lcount
+ 2[bp
]
3357 jcxz ata_packet_after
3359 mov di
, _ata_cmd_packet
.bufoff
+ 2[bp
]
3360 mov ax
, _ata_cmd_packet
.bufseg
+ 2[bp
]
3363 mov ah
, _ata_cmd_packet
.lmode
+ 2[bp
]
3364 cmp ah
, #ATA_MODE_PIO32
3369 insw
;; CX words transfered tp
port(DX
) to ES
:[DI
]
3370 jmp ata_packet_after
3374 insd
;; CX dwords transfered to
port(DX
) to ES
:[DI
]
3377 mov cx
, _ata_cmd_packet
.lafter
+ 2[bp
]
3378 jcxz ata_packet_done
3380 mov ah
, _ata_cmd_packet
.lmode
+ 2[bp
]
3381 cmp ah
, #ATA_MODE_PIO32
3382 je ata_packet_in_after_32
3384 ata_packet_in_after_16
:
3386 loop ata_packet_in_after_16
3389 ata_packet_in_after_32
:
3391 ata_packet_in_after_32_loop
:
3393 loop ata_packet_in_after_32_loop
3400 // Compute new buffer address
3403 // Save transferred bytes count
3405 write_dword(ebda_seg
, &EbdaData
->ata
.trsfbytes
,transfer
);
3409 // Final check, device must be ready
3410 if ( (status
& (ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
| ATA_CB_STAT_DF
| ATA_CB_STAT_DRQ
| ATA_CB_STAT_ERR
) )
3411 != ATA_CB_STAT_RDY
) {
3412 BX_DEBUG_ATA("ata_cmd_packet : not ready (status %02x)\n", (unsigned) status
);
3416 // Enable interrupts
3417 outb(iobase2
+ATA_CB_DC
, ATA_CB_DC_HD15
);
3421 // ---------------------------------------------------------------------------
3422 // End of ATA/ATAPI Driver
3423 // ---------------------------------------------------------------------------
3425 // ---------------------------------------------------------------------------
3426 // Start of ATA/ATAPI generic functions
3427 // ---------------------------------------------------------------------------
3430 atapi_get_sense(device
, seg
, asc
, ascq
)
3437 memsetb(get_SS(),atacmd
,0,12);
3440 atacmd
[0]=ATA_CMD_REQUEST_SENSE
;
3441 atacmd
[4]=sizeof(buffer
);
3442 if (ata_cmd_packet(device
, 12, get_SS(), atacmd
, 0, 18L, ATA_DATA_IN
, get_SS(), buffer
) != 0)
3445 write_byte(seg
,asc
,buffer
[12]);
3446 write_byte(seg
,ascq
,buffer
[13]);
3452 atapi_is_ready(device
)
3459 Bit32u timeout
; //measured in ms
3463 Bit16u ebda_seg
= read_word(0x0040,0x000E);
3464 if (read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
) != ATA_TYPE_ATAPI
) {
3465 printf("not implemented for non-ATAPI device\n");
3469 BX_DEBUG_ATA("ata_detect_medium: begin\n");
3470 memsetb(get_SS(),packet
, 0, sizeof packet
);
3471 packet
[0] = 0x25; /* READ CAPACITY */
3473 /* Retry READ CAPACITY 50 times unless MEDIUM NOT PRESENT
3474 * is reported by the device. If the device reports "IN PROGRESS",
3475 * 30 seconds is added. */
3479 while (time
< timeout
) {
3480 if (ata_cmd_packet(device
, sizeof(packet
), get_SS(), packet
, 0, 8L, ATA_DATA_IN
, get_SS(), buf
) == 0)
3483 if (atapi_get_sense(device
, get_SS(), &asc
, &ascq
) == 0) {
3484 if (asc
== 0x3a) { /* MEDIUM NOT PRESENT */
3485 BX_DEBUG_ATA("Device reports MEDIUM NOT PRESENT\n");
3489 if (asc
== 0x04 && ascq
== 0x01 && !in_progress
) {
3490 /* IN PROGRESS OF BECOMING READY */
3491 printf("Waiting for device to detect medium... ");
3492 /* Allow 30 seconds more */
3499 BX_DEBUG_ATA("read capacity failed\n");
3503 block_len
= (Bit32u
) buf
[4] << 24
3504 | (Bit32u
) buf
[5] << 16
3505 | (Bit32u
) buf
[6] << 8
3506 | (Bit32u
) buf
[7] << 0;
3507 BX_DEBUG_ATA("block_len=%u\n", block_len
);
3509 if (block_len
!= 2048 && block_len
!= 512)
3511 printf("Unsupported sector size %u\n", block_len
);
3514 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].blksize
, block_len
);
3516 sectors
= (Bit32u
) buf
[0] << 24
3517 | (Bit32u
) buf
[1] << 16
3518 | (Bit32u
) buf
[2] << 8
3519 | (Bit32u
) buf
[3] << 0;
3521 BX_DEBUG_ATA("sectors=%u\n", sectors
);
3522 if (block_len
== 2048)
3523 sectors
<<= 2; /* # of sectors in 512-byte "soft" sector */
3524 if (sectors
!= read_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_low
))
3525 printf("%dMB medium detected\n", sectors
>>(20-9));
3526 write_dword(ebda_seg
,&EbdaData
->ata
.devices
[device
].sectors_low
, sectors
);
3531 atapi_is_cdrom(device
)
3534 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3536 if (device
>= BX_MAX_ATA_DEVICES
)
3539 if (read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].type
) != ATA_TYPE_ATAPI
)
3542 if (read_byte(ebda_seg
,&EbdaData
->ata
.devices
[device
].device
) != ATA_DEVICE_CDROM
)
3548 // ---------------------------------------------------------------------------
3549 // End of ATA/ATAPI generic functions
3550 // ---------------------------------------------------------------------------
3552 #endif // BX_USE_ATADRV
3554 #if BX_ELTORITO_BOOT
3556 // ---------------------------------------------------------------------------
3557 // Start of El-Torito boot functions
3558 // ---------------------------------------------------------------------------
3563 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3565 // the only important data is this one for now
3566 write_byte(ebda_seg
,&EbdaData
->cdemu
.active
,0x00);
3572 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3574 return(read_byte(ebda_seg
,&EbdaData
->cdemu
.active
));
3578 cdemu_emulated_drive()
3580 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3582 return(read_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
));
3585 static char isotag
[6]="CD001";
3586 static char eltorito
[24]="EL TORITO SPECIFICATION";
3588 // Returns ah: emulated drive, al: error code
3593 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3594 Bit8u atacmd
[12], buffer
[2048];
3596 Bit16u boot_segment
, nbsectors
, i
, error
;
3599 // Find out the first cdrom
3600 for (device
=0; device
<BX_MAX_ATA_DEVICES
;device
++) {
3601 if (atapi_is_cdrom(device
)) break;
3605 if(device
>= BX_MAX_ATA_DEVICES
) return 2;
3607 if(error
= atapi_is_ready(device
) != 0)
3608 BX_INFO("ata_is_ready returned %d\n",error
);
3610 // Read the Boot Record Volume Descriptor
3611 memsetb(get_SS(),atacmd
,0,12);
3612 atacmd
[0]=0x28; // READ command
3613 atacmd
[7]=(0x01 & 0xff00) >> 8; // Sectors
3614 atacmd
[8]=(0x01 & 0x00ff); // Sectors
3615 atacmd
[2]=(0x11 & 0xff000000) >> 24; // LBA
3616 atacmd
[3]=(0x11 & 0x00ff0000) >> 16;
3617 atacmd
[4]=(0x11 & 0x0000ff00) >> 8;
3618 atacmd
[5]=(0x11 & 0x000000ff);
3619 if((error
= ata_cmd_packet(device
, 12, get_SS(), atacmd
, 0, 2048L, ATA_DATA_IN
, get_SS(), buffer
)) != 0)
3623 if(buffer
[0]!=0)return 4;
3625 if(buffer
[1+i
]!=read_byte(0xf000,&isotag
[i
]))return 5;
3628 if(buffer
[7+i
]!=read_byte(0xf000,&eltorito
[i
]))return 6;
3630 // ok, now we calculate the Boot catalog address
3631 lba
=buffer
[0x4A]*0x1000000+buffer
[0x49]*0x10000+buffer
[0x48]*0x100+buffer
[0x47];
3633 // And we read the Boot Catalog
3634 memsetb(get_SS(),atacmd
,0,12);
3635 atacmd
[0]=0x28; // READ command
3636 atacmd
[7]=(0x01 & 0xff00) >> 8; // Sectors
3637 atacmd
[8]=(0x01 & 0x00ff); // Sectors
3638 atacmd
[2]=(lba
& 0xff000000) >> 24; // LBA
3639 atacmd
[3]=(lba
& 0x00ff0000) >> 16;
3640 atacmd
[4]=(lba
& 0x0000ff00) >> 8;
3641 atacmd
[5]=(lba
& 0x000000ff);
3642 if((error
= ata_cmd_packet(device
, 12, get_SS(), atacmd
, 0, 2048L, ATA_DATA_IN
, get_SS(), buffer
)) != 0)
3646 if(buffer
[0x00]!=0x01)return 8; // Header
3647 if(buffer
[0x01]!=0x00)return 9; // Platform
3648 if(buffer
[0x1E]!=0x55)return 10; // key 1
3649 if(buffer
[0x1F]!=0xAA)return 10; // key 2
3651 // Initial/Default Entry
3652 if(buffer
[0x20]!=0x88)return 11; // Bootable
3654 write_byte(ebda_seg
,&EbdaData
->cdemu
.media
,buffer
[0x21]);
3655 if(buffer
[0x21]==0){
3656 // FIXME ElTorito Hardcoded. cdrom is hardcoded as device 0xE0.
3657 // Win2000 cd boot needs to know it booted from cd
3658 write_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
,0xE0);
3660 else if(buffer
[0x21]<4)
3661 write_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
,0x00);
3663 write_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
,0x80);
3665 write_byte(ebda_seg
,&EbdaData
->cdemu
.controller_index
,device
/2);
3666 write_byte(ebda_seg
,&EbdaData
->cdemu
.device_spec
,device
%2);
3668 boot_segment
=buffer
[0x23]*0x100+buffer
[0x22];
3669 if(boot_segment
==0x0000)boot_segment
=0x07C0;
3671 write_word(ebda_seg
,&EbdaData
->cdemu
.load_segment
,boot_segment
);
3672 write_word(ebda_seg
,&EbdaData
->cdemu
.buffer_segment
,0x0000);
3674 nbsectors
=buffer
[0x27]*0x100+buffer
[0x26];
3675 write_word(ebda_seg
,&EbdaData
->cdemu
.sector_count
,nbsectors
);
3677 lba
=buffer
[0x2B]*0x1000000+buffer
[0x2A]*0x10000+buffer
[0x29]*0x100+buffer
[0x28];
3678 write_dword(ebda_seg
,&EbdaData
->cdemu
.ilba
,lba
);
3680 // And we read the image in memory
3681 memsetb(get_SS(),atacmd
,0,12);
3682 atacmd
[0]=0x28; // READ command
3683 atacmd
[7]=((1+(nbsectors
-1)/4) & 0xff00) >> 8; // Sectors
3684 atacmd
[8]=((1+(nbsectors
-1)/4) & 0x00ff); // Sectors
3685 atacmd
[2]=(lba
& 0xff000000) >> 24; // LBA
3686 atacmd
[3]=(lba
& 0x00ff0000) >> 16;
3687 atacmd
[4]=(lba
& 0x0000ff00) >> 8;
3688 atacmd
[5]=(lba
& 0x000000ff);
3689 if((error
= ata_cmd_packet(device
, 12, get_SS(), atacmd
, 0, nbsectors
*512L, ATA_DATA_IN
, boot_segment
,0)) != 0)
3692 // Remember the media type
3693 switch(read_byte(ebda_seg
,&EbdaData
->cdemu
.media
)) {
3694 case 0x01: // 1.2M floppy
3695 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
,15);
3696 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
,80);
3697 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
,2);
3699 case 0x02: // 1.44M floppy
3700 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
,18);
3701 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
,80);
3702 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
,2);
3704 case 0x03: // 2.88M floppy
3705 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
,36);
3706 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
,80);
3707 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
,2);
3709 case 0x04: // Harddrive
3710 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
,read_byte(boot_segment
,446+6)&0x3f);
3711 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
,
3712 (read_byte(boot_segment
,446+6)<<2) + read_byte(boot_segment
,446+7) + 1);
3713 write_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
,read_byte(boot_segment
,446+5) + 1);
3717 if(read_byte(ebda_seg
,&EbdaData
->cdemu
.media
)!=0) {
3718 // Increase bios installed hardware number of devices
3719 if(read_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
)==0x00)
3720 write_byte(0x40,0x10,read_byte(0x40,0x10)|0x41);
3722 write_byte(ebda_seg
, &EbdaData
->ata
.hdcount
, read_byte(ebda_seg
, &EbdaData
->ata
.hdcount
) + 1);
3726 // everything is ok, so from now on, the emulation is active
3727 if(read_byte(ebda_seg
,&EbdaData
->cdemu
.media
)!=0)
3728 write_byte(ebda_seg
,&EbdaData
->cdemu
.active
,0x01);
3730 // return the boot drive + no error
3731 return (read_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
)*0x100)+0;
3734 // ---------------------------------------------------------------------------
3735 // End of El-Torito boot functions
3736 // ---------------------------------------------------------------------------
3737 #endif // BX_ELTORITO_BOOT
3740 int14_function(regs
, ds
, iret_addr
)
3741 pusha_regs_t regs
; // regs pushed from PUSHA instruction
3742 Bit16u ds
; // previous DS:, DS set to 0x0000 by asm wrapper
3743 iret_addr_t iret_addr
; // CS,IP,Flags pushed from original INT call
3745 Bit16u addr
,timer
,val16
;
3752 addr
= read_word(0x0040, (regs
.u
.r16
.dx
<< 1));
3753 timeout
= read_byte(0x0040, 0x007C + regs
.u
.r16
.dx
);
3754 if ((regs
.u
.r16
.dx
< 4) && (addr
> 0)) {
3755 switch (regs
.u
.r8
.ah
) {
3757 outb(addr
+3, inb(addr
+3) | 0x80);
3758 if (regs
.u
.r8
.al
& 0xE0 == 0) {
3762 val16
= 0x600 >> ((regs
.u
.r8
.al
& 0xE0) >> 5);
3763 outb(addr
, val16
& 0xFF);
3764 outb(addr
+1, val16
>> 8);
3766 outb(addr
+3, regs
.u
.r8
.al
& 0x1F);
3767 regs
.u
.r8
.ah
= inb(addr
+5);
3768 regs
.u
.r8
.al
= inb(addr
+6);
3769 ClearCF(iret_addr
.flags
);
3772 timer
= read_word(0x0040, 0x006C);
3773 while (((inb(addr
+5) & 0x60) != 0x60) && (timeout
)) {
3774 val16
= read_word(0x0040, 0x006C);
3775 if (val16
!= timer
) {
3780 if (timeout
) outb(addr
, regs
.u
.r8
.al
);
3781 regs
.u
.r8
.ah
= inb(addr
+5);
3782 if (!timeout
) regs
.u
.r8
.ah
|= 0x80;
3783 ClearCF(iret_addr
.flags
);
3786 timer
= read_word(0x0040, 0x006C);
3787 while (((inb(addr
+5) & 0x01) == 0) && (timeout
)) {
3788 val16
= read_word(0x0040, 0x006C);
3789 if (val16
!= timer
) {
3796 regs
.u
.r8
.al
= inb(addr
);
3798 regs
.u
.r8
.ah
= inb(addr
+5);
3800 ClearCF(iret_addr
.flags
);
3803 regs
.u
.r8
.ah
= inb(addr
+5);
3804 regs
.u
.r8
.al
= inb(addr
+6);
3805 ClearCF(iret_addr
.flags
);
3808 SetCF(iret_addr
.flags
); // Unsupported
3811 SetCF(iret_addr
.flags
); // Unsupported
3816 int15_function(regs
, ES
, DS
, FLAGS
)
3817 pusha_regs_t regs
; // REGS pushed via pusha
3818 Bit16u ES
, DS
, FLAGS
;
3820 Bit16u ebda_seg
=read_word(0x0040,0x000E);
3821 bx_bool prev_a20_enable
;
3830 BX_DEBUG_INT15("int15 AX=%04x\n",regs
.u
.r16
.ax
);
3832 switch (regs
.u
.r8
.ah
) {
3833 case 0x24: /* A20 Control */
3834 switch (regs
.u
.r8
.al
) {
3846 regs
.u
.r8
.al
= (inb(0x92) >> 1) & 0x01;
3856 BX_INFO("int15: Func 24h, subfunc %02xh, A20 gate control not supported\n", (unsigned) regs
.u
.r8
.al
);
3858 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
3864 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
3868 /* keyboard intercept */
3870 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
3877 case 0x52: // removable media eject
3879 regs
.u
.r8
.ah
= 0; // "ok ejection may proceed"
3883 if( regs
.u
.r8
.al
== 0 ) {
3884 // Set Interval requested.
3885 if( ( read_byte( 0x40, 0xA0 ) & 1 ) == 0 ) {
3886 // Interval not already set.
3887 write_byte( 0x40, 0xA0, 1 ); // Set status byte.
3888 write_word( 0x40, 0x98, ES
); // Byte location, segment
3889 write_word( 0x40, 0x9A, regs
.u
.r16
.bx
); // Byte location, offset
3890 write_word( 0x40, 0x9C, regs
.u
.r16
.dx
); // Low word, delay
3891 write_word( 0x40, 0x9E, regs
.u
.r16
.cx
); // High word, delay.
3893 irqDisable
= inb( 0xA1 );
3894 outb( 0xA1, irqDisable
& 0xFE );
3895 bRegister
= inb_cmos( 0xB ); // Unmask IRQ8 so INT70 will get through.
3896 outb_cmos( 0xB, bRegister
| 0x40 ); // Turn on the Periodic Interrupt timer
3898 // Interval already set.
3899 BX_DEBUG_INT15("int15: Func 83h, failed, already waiting.\n" );
3901 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
3903 } else if( regs
.u
.r8
.al
== 1 ) {
3904 // Clear Interval requested
3905 write_byte( 0x40, 0xA0, 0 ); // Clear status byte
3907 bRegister
= inb_cmos( 0xB );
3908 outb_cmos( 0xB, bRegister
& ~0x40 ); // Turn off the Periodic Interrupt timer
3910 BX_DEBUG_INT15("int15: Func 83h, failed.\n" );
3912 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
3921 # error "Int15 function 87h not supported on < 80386"
3923 // +++ should probably have descriptor checks
3924 // +++ should have exception handlers
3926 // turn off interrupts
3931 prev_a20_enable
= set_enable_a20(1); // enable A20 line
3933 // 128K max of transfer on 386+ ???
3934 // source == destination ???
3936 // ES:SI points to descriptor table
3937 // offset use initially comments
3938 // ==============================================
3939 // 00..07 Unused zeros Null descriptor
3940 // 08..0f GDT zeros filled in by BIOS
3941 // 10..17 source ssssssss source of data
3942 // 18..1f dest dddddddd destination of data
3943 // 20..27 CS zeros filled in by BIOS
3944 // 28..2f SS zeros filled in by BIOS
3951 // check for access rights of source & dest here
3953 // Initialize GDT descriptor
3954 base15_00
= (ES
<< 4) + regs
.u
.r16
.si
;
3955 base23_16
= ES
>> 12;
3956 if (base15_00
< (ES
<<4))
3958 write_word(ES
, regs
.u
.r16
.si
+0x08+0, 47); // limit 15:00 = 6 * 8bytes/descriptor
3959 write_word(ES
, regs
.u
.r16
.si
+0x08+2, base15_00
);// base 15:00
3960 write_byte(ES
, regs
.u
.r16
.si
+0x08+4, base23_16
);// base 23:16
3961 write_byte(ES
, regs
.u
.r16
.si
+0x08+5, 0x93); // access
3962 write_word(ES
, regs
.u
.r16
.si
+0x08+6, 0x0000); // base 31:24/reserved/limit 19:16
3964 // Initialize CS descriptor
3965 write_word(ES
, regs
.u
.r16
.si
+0x20+0, 0xffff);// limit 15:00 = normal 64K limit
3966 write_word(ES
, regs
.u
.r16
.si
+0x20+2, 0x0000);// base 15:00
3967 write_byte(ES
, regs
.u
.r16
.si
+0x20+4, 0x000f);// base 23:16
3968 write_byte(ES
, regs
.u
.r16
.si
+0x20+5, 0x9b); // access
3969 write_word(ES
, regs
.u
.r16
.si
+0x20+6, 0x0000);// base 31:24/reserved/limit 19:16
3971 // Initialize SS descriptor
3973 base15_00
= ss
<< 4;
3974 base23_16
= ss
>> 12;
3975 write_word(ES
, regs
.u
.r16
.si
+0x28+0, 0xffff); // limit 15:00 = normal 64K limit
3976 write_word(ES
, regs
.u
.r16
.si
+0x28+2, base15_00
);// base 15:00
3977 write_byte(ES
, regs
.u
.r16
.si
+0x28+4, base23_16
);// base 23:16
3978 write_byte(ES
, regs
.u
.r16
.si
+0x28+5, 0x93); // access
3979 write_word(ES
, regs
.u
.r16
.si
+0x28+6, 0x0000); // base 31:24/reserved/limit 19:16
3983 // Compile generates locals offset info relative to SP.
3984 // Get CX (word count) from stack.
3987 mov cx
, _int15_function
.CX
[bx
]
3989 // since we need to set SS:SP, save them to the BDA
3990 // for future restore
4000 lidt
[pmode_IDT_info
]
4001 ;; perhaps
do something with IDT here
4003 ;; set PE bit in CR0
4007 ;; far jump to flush CPU queue after transition to
protected mode
4008 JMP_AP(0x0020, protected_mode
)
4011 ;; GDT points to valid descriptor table
, now load SS
, DS
, ES
4012 mov ax
, #0x28 ;; 101 000 = 5th descriptor in table, TI=GDT, RPL=00
4014 mov ax
, #0x10 ;; 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
4016 mov ax
, #0x18 ;; 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
4022 movsw
;; move CX words from DS
:SI to ES
:DI
4024 ;; make sure DS
and ES limits are
64KB
4029 ;; reset PG bit in CR0
???
4034 ;; far jump to flush CPU queue after transition to real mode
4035 JMP_AP(0xf000, real_mode
)
4038 ;; restore IDT to normal real
-mode defaults
4040 lidt
[rmode_IDT_info
]
4042 // restore SS:SP from the BDA
4050 set_enable_a20(prev_a20_enable
);
4052 // turn back on interrupts
4063 // Get the amount of extended memory (above 1M)
4065 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4068 regs
.u
.r8
.al
= inb_cmos(0x30);
4069 regs
.u
.r8
.ah
= inb_cmos(0x31);
4071 // According to Ralf Brown's interrupt the limit should be 15M,
4072 // but real machines mostly return max. 63M.
4073 if(regs
.u
.r16
.ax
> 0xffc0)
4074 regs
.u
.r16
.ax
= 0xffc0;
4081 /* Device busy interrupt. Called by Int 16h when no key available */
4085 /* Interrupt complete. Called by Int 16h when key becomes available */
4089 BX_INFO("*** int 15h function AH=bf not yet supported!\n");
4091 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4097 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4102 regs
.u
.r16
.bx
= BIOS_CONFIG_TABLE
;
4112 bios_printf(BIOS_PRINTF_DEBUG
, "EISA BIOS not present\n");
4114 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4118 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4119 (unsigned) regs
.u
.r16
.ax
, (unsigned) regs
.u
.r16
.bx
);
4121 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4126 #if BX_USE_PS2_MOUSE
4128 int15_function_mouse(regs
, ES
, DS
, FLAGS
)
4129 pusha_regs_t regs
; // REGS pushed via pusha
4130 Bit16u ES
, DS
, FLAGS
;
4132 Bit16u ebda_seg
=read_word(0x0040,0x000E);
4133 Bit8u mouse_flags_1
, mouse_flags_2
;
4134 Bit16u mouse_driver_seg
;
4135 Bit16u mouse_driver_offset
;
4136 Bit8u comm_byte
, prev_command_byte
;
4137 Bit8u ret
, mouse_data1
, mouse_data2
, mouse_data3
;
4139 BX_DEBUG_INT15("int15 AX=%04x\n",regs
.u
.r16
.ax
);
4141 switch (regs
.u
.r8
.ah
) {
4143 // Return Codes status in AH
4144 // =========================
4146 // 01: invalid subfunction (AL > 7)
4147 // 02: invalid input value (out of allowable range)
4148 // 03: interface error
4149 // 04: resend command received from mouse controller,
4150 // device driver should attempt command again
4151 // 05: cannot enable mouse, since no far call has been installed
4152 // 80/86: mouse service not implemented
4154 switch (regs
.u
.r8
.al
) {
4155 case 0: // Disable/Enable Mouse
4156 BX_DEBUG_INT15("case 0:\n");
4157 switch (regs
.u
.r8
.bh
) {
4158 case 0: // Disable Mouse
4159 BX_DEBUG_INT15("case 0: disable mouse\n");
4160 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4161 ret
= send_to_mouse_ctrl(0xF5); // disable mouse command
4163 ret
= get_mouse_data(&mouse_data1
);
4164 if ( (ret
== 0) || (mouse_data1
== 0xFA) ) {
4177 case 1: // Enable Mouse
4178 BX_DEBUG_INT15("case 1: enable mouse\n");
4179 mouse_flags_2
= read_byte(ebda_seg
, 0x0027);
4180 if ( (mouse_flags_2
& 0x80) == 0 ) {
4181 BX_DEBUG_INT15("INT 15h C2 Enable Mouse, no far call handler\n");
4183 regs
.u
.r8
.ah
= 5; // no far call installed
4186 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4187 ret
= send_to_mouse_ctrl(0xF4); // enable mouse command
4189 ret
= get_mouse_data(&mouse_data1
);
4190 if ( (ret
== 0) && (mouse_data1
== 0xFA) ) {
4191 enable_mouse_int_and_events(); // turn IRQ12 and packet generation on
4201 default: // invalid subfunction
4202 BX_DEBUG_INT15("INT 15h C2 AL=0, BH=%02x\n", (unsigned) regs
.u
.r8
.bh
);
4204 regs
.u
.r8
.ah
= 1; // invalid subfunction
4209 case 1: // Reset Mouse
4210 case 5: // Initialize Mouse
4211 BX_DEBUG_INT15("case 1 or 5:\n");
4212 if (regs
.u
.r8
.al
== 5) {
4213 if (regs
.u
.r8
.bh
!= 3) {
4215 regs
.u
.r8
.ah
= 0x02; // invalid input
4218 mouse_flags_2
= read_byte(ebda_seg
, 0x0027);
4219 mouse_flags_2
= (mouse_flags_2
& 0x00) | regs
.u
.r8
.bh
;
4220 mouse_flags_1
= 0x00;
4221 write_byte(ebda_seg
, 0x0026, mouse_flags_1
);
4222 write_byte(ebda_seg
, 0x0027, mouse_flags_2
);
4225 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4226 ret
= send_to_mouse_ctrl(0xFF); // reset mouse command
4228 ret
= get_mouse_data(&mouse_data3
);
4229 // if no mouse attached, it will return RESEND
4230 if (mouse_data3
== 0xfe) {
4234 if (mouse_data3
!= 0xfa)
4235 BX_PANIC("Mouse reset returned %02x (should be ack)\n", (unsigned)mouse_data3
);
4237 ret
= get_mouse_data(&mouse_data1
);
4239 ret
= get_mouse_data(&mouse_data2
);
4241 // turn IRQ12 and packet generation on
4242 enable_mouse_int_and_events();
4245 regs
.u
.r8
.bl
= mouse_data1
;
4246 regs
.u
.r8
.bh
= mouse_data2
;
4258 case 2: // Set Sample Rate
4259 BX_DEBUG_INT15("case 2:\n");
4260 switch (regs
.u
.r8
.bh
) {
4261 case 0: mouse_data1
= 10; break; // 10 reports/sec
4262 case 1: mouse_data1
= 20; break; // 20 reports/sec
4263 case 2: mouse_data1
= 40; break; // 40 reports/sec
4264 case 3: mouse_data1
= 60; break; // 60 reports/sec
4265 case 4: mouse_data1
= 80; break; // 80 reports/sec
4266 case 5: mouse_data1
= 100; break; // 100 reports/sec (default)
4267 case 6: mouse_data1
= 200; break; // 200 reports/sec
4268 default: mouse_data1
= 0;
4270 if (mouse_data1
> 0) {
4271 ret
= send_to_mouse_ctrl(0xF3); // set sample rate command
4273 ret
= get_mouse_data(&mouse_data2
);
4274 ret
= send_to_mouse_ctrl(mouse_data1
);
4275 ret
= get_mouse_data(&mouse_data2
);
4281 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4286 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4290 case 3: // Set Resolution
4291 BX_DEBUG_INT15("case 3:\n");
4293 // 0 = 25 dpi, 1 count per millimeter
4294 // 1 = 50 dpi, 2 counts per millimeter
4295 // 2 = 100 dpi, 4 counts per millimeter
4296 // 3 = 200 dpi, 8 counts per millimeter
4297 comm_byte
= inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4298 if (regs
.u
.r8
.bh
< 4) {
4299 ret
= send_to_mouse_ctrl(0xE8); // set resolution command
4301 ret
= get_mouse_data(&mouse_data1
);
4302 if (mouse_data1
!= 0xfa)
4303 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1
);
4304 ret
= send_to_mouse_ctrl(regs
.u
.r8
.bh
);
4305 ret
= get_mouse_data(&mouse_data1
);
4306 if (mouse_data1
!= 0xfa)
4307 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1
);
4313 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4318 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4320 set_kbd_command_byte(comm_byte
); // restore IRQ12 and serial enable
4323 case 4: // Get Device ID
4324 BX_DEBUG_INT15("case 4:\n");
4325 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4326 ret
= send_to_mouse_ctrl(0xF2); // get mouse ID command
4328 ret
= get_mouse_data(&mouse_data1
);
4329 ret
= get_mouse_data(&mouse_data2
);
4332 regs
.u
.r8
.bh
= mouse_data2
;
4336 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4340 case 6: // Return Status & Set Scaling Factor...
4341 BX_DEBUG_INT15("case 6:\n");
4342 switch (regs
.u
.r8
.bh
) {
4343 case 0: // Return Status
4344 comm_byte
= inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4345 ret
= send_to_mouse_ctrl(0xE9); // get mouse info command
4347 ret
= get_mouse_data(&mouse_data1
);
4348 if (mouse_data1
!= 0xfa)
4349 BX_PANIC("Mouse status returned %02x (should be ack)\n", (unsigned)mouse_data1
);
4351 ret
= get_mouse_data(&mouse_data1
);
4353 ret
= get_mouse_data(&mouse_data2
);
4355 ret
= get_mouse_data(&mouse_data3
);
4359 regs
.u
.r8
.bl
= mouse_data1
;
4360 regs
.u
.r8
.cl
= mouse_data2
;
4361 regs
.u
.r8
.dl
= mouse_data3
;
4362 set_kbd_command_byte(comm_byte
); // restore IRQ12 and serial enable
4373 set_kbd_command_byte(comm_byte
); // restore IRQ12 and serial enable
4376 case 1: // Set Scaling Factor to 1:1
4377 case 2: // Set Scaling Factor to 2:1
4378 comm_byte
= inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4379 if (regs
.u
.r8
.bh
== 1) {
4380 ret
= send_to_mouse_ctrl(0xE6);
4382 ret
= send_to_mouse_ctrl(0xE7);
4385 get_mouse_data(&mouse_data1
);
4386 ret
= (mouse_data1
!= 0xFA);
4394 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4396 set_kbd_command_byte(comm_byte
); // restore IRQ12 and serial enable
4400 BX_PANIC("INT 15h C2 AL=6, BH=%02x\n", (unsigned) regs
.u
.r8
.bh
);
4404 case 7: // Set Mouse Handler Address
4405 BX_DEBUG_INT15("case 7:\n");
4406 mouse_driver_seg
= ES
;
4407 mouse_driver_offset
= regs
.u
.r16
.bx
;
4408 write_word(ebda_seg
, 0x0022, mouse_driver_offset
);
4409 write_word(ebda_seg
, 0x0024, mouse_driver_seg
);
4410 mouse_flags_2
= read_byte(ebda_seg
, 0x0027);
4411 if (mouse_driver_offset
== 0 && mouse_driver_seg
== 0) {
4412 /* remove handler */
4413 if ( (mouse_flags_2
& 0x80) != 0 ) {
4414 mouse_flags_2
&= ~0x80;
4415 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
4419 /* install handler */
4420 mouse_flags_2
|= 0x80;
4422 write_byte(ebda_seg
, 0x0027, mouse_flags_2
);
4428 BX_DEBUG_INT15("case default:\n");
4429 regs
.u
.r8
.ah
= 1; // invalid function
4435 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4436 (unsigned) regs
.u
.r16
.ax
, (unsigned) regs
.u
.r16
.bx
);
4438 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4442 #endif // BX_USE_PS2_MOUSE
4445 void set_e820_range(ES
, DI
, start
, end
, extra_start
, extra_end
, type
)
4454 write_word(ES
, DI
, start
);
4455 write_word(ES
, DI
+2, start
>> 16);
4456 write_word(ES
, DI
+4, extra_start
);
4457 write_word(ES
, DI
+6, 0x00);
4460 extra_end
-= extra_start
;
4461 write_word(ES
, DI
+8, end
);
4462 write_word(ES
, DI
+10, end
>> 16);
4463 write_word(ES
, DI
+12, extra_end
);
4464 write_word(ES
, DI
+14, 0x0000);
4466 write_word(ES
, DI
+16, type
);
4467 write_word(ES
, DI
+18, 0x0);
4471 int15_function32(regs
, ES
, DS
, FLAGS
)
4472 pushad_regs_t regs
; // REGS pushed via pushad
4473 Bit16u ES
, DS
, FLAGS
;
4475 Bit32u extended_memory_size
=0; // 64bits long
4476 Bit32u extra_lowbits_memory_size
=0;
4478 Bit8u extra_highbits_memory_size
=0;
4480 BX_DEBUG_INT15("int15 AX=%04x\n",regs
.u
.r16
.ax
);
4482 switch (regs
.u
.r8
.ah
) {
4484 // Wait for CX:DX microseconds. currently using the
4485 // refresh request port 0x61 bit4, toggling every 15usec
4493 ;; Get the count in eax
4496 mov ax
, _int15_function32
.CX
[bx
]
4499 mov ax
, _int15_function32
.DX
[bx
]
4501 ;; convert to numbers of
15usec ticks
4507 ;; wait
for ecx number of refresh requests
4528 switch(regs
.u
.r8
.al
)
4530 case 0x20: // coded by osmaker aka K.J.
4531 if(regs
.u
.r32
.edx
== 0x534D4150)
4533 extended_memory_size
= inb_cmos(0x35);
4534 extended_memory_size
<<= 8;
4535 extended_memory_size
|= inb_cmos(0x34);
4536 extended_memory_size
*= 64;
4537 // greater than EFF00000???
4538 if(extended_memory_size
> 0x3bc000) {
4539 extended_memory_size
= 0x3bc000; // everything after this is reserved memory until we get to 0x100000000
4541 extended_memory_size
*= 1024;
4542 extended_memory_size
+= (16L * 1024 * 1024);
4544 if(extended_memory_size
<= (16L * 1024 * 1024)) {
4545 extended_memory_size
= inb_cmos(0x31);
4546 extended_memory_size
<<= 8;
4547 extended_memory_size
|= inb_cmos(0x30);
4548 extended_memory_size
*= 1024;
4549 extended_memory_size
+= (1L * 1024 * 1024);
4552 extra_lowbits_memory_size
= inb_cmos(0x5c);
4553 extra_lowbits_memory_size
<<= 8;
4554 extra_lowbits_memory_size
|= inb_cmos(0x5b);
4555 extra_lowbits_memory_size
*= 64;
4556 extra_lowbits_memory_size
*= 1024;
4557 extra_highbits_memory_size
= inb_cmos(0x5d);
4559 switch(regs
.u
.r16
.bx
)
4562 set_e820_range(ES
, regs
.u
.r16
.di
,
4563 0x0000000L
, 0x0009f000L
, 0, 0, 1);
4565 regs
.u
.r32
.eax
= 0x534D4150;
4566 regs
.u
.r32
.ecx
= 0x14;
4571 set_e820_range(ES
, regs
.u
.r16
.di
,
4572 0x0009f000L
, 0x000a0000L
, 0, 0, 2);
4574 regs
.u
.r32
.eax
= 0x534D4150;
4575 regs
.u
.r32
.ecx
= 0x14;
4580 set_e820_range(ES
, regs
.u
.r16
.di
,
4581 0x000e8000L
, 0x00100000L
, 0, 0, 2);
4583 regs
.u
.r32
.eax
= 0x534D4150;
4584 regs
.u
.r32
.ecx
= 0x14;
4590 set_e820_range(ES
, regs
.u
.r16
.di
,
4592 extended_memory_size
- ACPI_DATA_SIZE
,0, 0, 1);
4595 set_e820_range(ES
, regs
.u
.r16
.di
,
4597 extended_memory_size
, 1);
4600 regs
.u
.r32
.eax
= 0x534D4150;
4601 regs
.u
.r32
.ecx
= 0x14;
4606 set_e820_range(ES
, regs
.u
.r16
.di
,
4607 extended_memory_size
- ACPI_DATA_SIZE
,
4608 extended_memory_size
,0, 0, 3); // ACPI RAM
4610 regs
.u
.r32
.eax
= 0x534D4150;
4611 regs
.u
.r32
.ecx
= 0x14;
4616 /* 4 pages before the bios, 3 pages for vmx tss pages,
4617 * the other page for EPT real mode pagetable */
4618 set_e820_range(ES
, regs
.u
.r16
.di
, 0xfffbc000L
,
4619 0xfffc0000L
, 0, 0, 2);
4621 regs
.u
.r32
.eax
= 0x534D4150;
4622 regs
.u
.r32
.ecx
= 0x14;
4626 /* 256KB BIOS area at the end of 4 GB */
4627 set_e820_range(ES
, regs
.u
.r16
.di
,
4628 0xfffc0000L
, 0x00000000L
,0, 0, 2);
4629 if (extra_highbits_memory_size
|| extra_lowbits_memory_size
)
4633 regs
.u
.r32
.eax
= 0x534D4150;
4634 regs
.u
.r32
.ecx
= 0x14;
4638 /* Maping of memory above 4 GB */
4639 set_e820_range(ES
, regs
.u
.r16
.di
, 0x00000000L
,
4640 extra_lowbits_memory_size
, 1, extra_highbits_memory_size
4643 regs
.u
.r32
.eax
= 0x534D4150;
4644 regs
.u
.r32
.ecx
= 0x14;
4647 default: /* AX=E820, DX=534D4150, BX unrecognized */
4648 goto int15_unimplemented
;
4652 // if DX != 0x534D4150)
4653 goto int15_unimplemented
;
4658 // do we have any reason to fail here ?
4661 // my real system sets ax and bx to 0
4662 // this is confirmed by Ralph Brown list
4663 // but syslinux v1.48 is known to behave
4664 // strangely if ax is set to 0
4665 // regs.u.r16.ax = 0;
4666 // regs.u.r16.bx = 0;
4668 // Get the amount of extended memory (above 1M)
4669 regs
.u
.r8
.cl
= inb_cmos(0x30);
4670 regs
.u
.r8
.ch
= inb_cmos(0x31);
4673 if(regs
.u
.r16
.cx
> 0x3c00)
4675 regs
.u
.r16
.cx
= 0x3c00;
4678 // Get the amount of extended memory above 16M in 64k blocs
4679 regs
.u
.r8
.dl
= inb_cmos(0x34);
4680 regs
.u
.r8
.dh
= inb_cmos(0x35);
4682 // Set configured memory equal to extended memory
4683 regs
.u
.r16
.ax
= regs
.u
.r16
.cx
;
4684 regs
.u
.r16
.bx
= regs
.u
.r16
.dx
;
4686 default: /* AH=0xE8?? but not implemented */
4687 goto int15_unimplemented
;
4690 int15_unimplemented
:
4691 // fall into the default
4693 BX_INFO("*** int 15h function AX=%04x, BX=%04x not yet supported!\n",
4694 (unsigned) regs
.u
.r16
.ax
, (unsigned) regs
.u
.r16
.bx
);
4696 regs
.u
.r8
.ah
= UNSUPPORTED_FUNCTION
;
4702 int16_function(DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, FLAGS
)
4703 Bit16u DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, FLAGS
;
4705 Bit8u scan_code
, ascii_code
, shift_flags
, led_flags
, count
;
4706 Bit16u kbd_code
, max
;
4708 BX_DEBUG_INT16("int16: AX=%04x BX=%04x CX=%04x DX=%04x \n", AX
, BX
, CX
, DX
);
4710 shift_flags
= read_byte(0x0040, 0x17);
4711 led_flags
= read_byte(0x0040, 0x97);
4712 if ((((shift_flags
>> 4) & 0x07) ^ (led_flags
& 0x07)) != 0) {
4717 while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
4718 if ((inb(0x60) == 0xfa)) {
4720 led_flags
|= ((shift_flags
>> 4) & 0x07);
4721 outb(0x60, led_flags
& 0x07);
4722 while ((inb(0x64) & 0x01) == 0) outb(0x80, 0x21);
4724 write_byte(0x0040, 0x97, led_flags
);
4732 case 0x00: /* read keyboard input */
4734 if ( !dequeue_key(&scan_code
, &ascii_code
, 1) ) {
4735 BX_PANIC("KBD: int16h: out of keyboard input\n");
4737 if (scan_code
!=0 && ascii_code
== 0xF0) ascii_code
= 0;
4738 else if (ascii_code
== 0xE0) ascii_code
= 0;
4739 AX
= (scan_code
<< 8) | ascii_code
;
4742 case 0x01: /* check keyboard status */
4743 if ( !dequeue_key(&scan_code
, &ascii_code
, 0) ) {
4747 if (scan_code
!=0 && ascii_code
== 0xF0) ascii_code
= 0;
4748 else if (ascii_code
== 0xE0) ascii_code
= 0;
4749 AX
= (scan_code
<< 8) | ascii_code
;
4753 case 0x02: /* get shift flag status */
4754 shift_flags
= read_byte(0x0040, 0x17);
4755 SET_AL(shift_flags
);
4758 case 0x05: /* store key-stroke into buffer */
4759 if ( !enqueue_key(GET_CH(), GET_CL()) ) {
4767 case 0x09: /* GET KEYBOARD FUNCTIONALITY */
4768 // bit Bochs Description
4770 // 6 0 INT 16/AH=20h-22h supported (122-key keyboard support)
4771 // 5 1 INT 16/AH=10h-12h supported (enhanced keyboard support)
4772 // 4 1 INT 16/AH=0Ah supported
4773 // 3 0 INT 16/AX=0306h supported
4774 // 2 0 INT 16/AX=0305h supported
4775 // 1 0 INT 16/AX=0304h supported
4776 // 0 0 INT 16/AX=0300h supported
4781 case 0x0A: /* GET KEYBOARD ID */
4787 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x00);
4789 if ((inb(0x60) == 0xfa)) {
4792 while ( ((inb(0x64) & 0x01) == 0) && (--max
>0) ) outb(0x80, 0x00);
4795 kbd_code
|= (inb(0x60) << 8);
4797 } while (--count
>0);
4803 case 0x10: /* read MF-II keyboard input */
4805 if ( !dequeue_key(&scan_code
, &ascii_code
, 1) ) {
4806 BX_PANIC("KBD: int16h: out of keyboard input\n");
4808 if (scan_code
!=0 && ascii_code
== 0xF0) ascii_code
= 0;
4809 AX
= (scan_code
<< 8) | ascii_code
;
4812 case 0x11: /* check MF-II keyboard status */
4813 if ( !dequeue_key(&scan_code
, &ascii_code
, 0) ) {
4817 if (scan_code
!=0 && ascii_code
== 0xF0) ascii_code
= 0;
4818 AX
= (scan_code
<< 8) | ascii_code
;
4822 case 0x12: /* get extended keyboard status */
4823 shift_flags
= read_byte(0x0040, 0x17);
4824 SET_AL(shift_flags
);
4825 shift_flags
= read_byte(0x0040, 0x18) & 0x73;
4826 shift_flags
|= read_byte(0x0040, 0x96) & 0x0c;
4827 SET_AH(shift_flags
);
4828 BX_DEBUG_INT16("int16: func 12 sending %04x\n",AX
);
4831 case 0x92: /* keyboard capability check called by DOS 5.0+ keyb */
4832 SET_AH(0x80); // function int16 ah=0x10-0x12 supported
4835 case 0xA2: /* 122 keys capability check called by DOS 5.0+ keyb */
4836 // don't change AH : function int16 ah=0x20-0x22 NOT supported
4840 if (GET_AL() == 0x08)
4841 SET_AH(0x02); // unsupported, aka normal keyboard
4844 BX_INFO("KBD: unsupported int 16h function %02x\n", GET_AH());
4849 dequeue_key(scan_code
, ascii_code
, incr
)
4854 Bit16u buffer_start
, buffer_end
, buffer_head
, buffer_tail
;
4859 buffer_start
= 0x001E;
4860 buffer_end
= 0x003E;
4862 buffer_start
= read_word(0x0040, 0x0080);
4863 buffer_end
= read_word(0x0040, 0x0082);
4866 buffer_head
= read_word(0x0040, 0x001a);
4867 buffer_tail
= read_word(0x0040, 0x001c);
4869 if (buffer_head
!= buffer_tail
) {
4871 acode
= read_byte(0x0040, buffer_head
);
4872 scode
= read_byte(0x0040, buffer_head
+1);
4873 write_byte(ss
, ascii_code
, acode
);
4874 write_byte(ss
, scan_code
, scode
);
4878 if (buffer_head
>= buffer_end
)
4879 buffer_head
= buffer_start
;
4880 write_word(0x0040, 0x001a, buffer_head
);
4889 static char panic_msg_keyb_buffer_full
[] = "%s: keyboard input buffer full\n";
4892 inhibit_mouse_int_and_events()
4894 Bit8u command_byte
, prev_command_byte
;
4896 // Turn off IRQ generation and aux data line
4897 if ( inb(0x64) & 0x02 )
4898 BX_PANIC(panic_msg_keyb_buffer_full
,"inhibmouse");
4899 outb(0x64, 0x20); // get command byte
4900 while ( (inb(0x64) & 0x01) != 0x01 );
4901 prev_command_byte
= inb(0x60);
4902 command_byte
= prev_command_byte
;
4903 //while ( (inb(0x64) & 0x02) );
4904 if ( inb(0x64) & 0x02 )
4905 BX_PANIC(panic_msg_keyb_buffer_full
,"inhibmouse");
4906 command_byte
&= 0xfd; // turn off IRQ 12 generation
4907 command_byte
|= 0x20; // disable mouse serial clock line
4908 outb(0x64, 0x60); // write command byte
4909 outb(0x60, command_byte
);
4910 return(prev_command_byte
);
4914 enable_mouse_int_and_events()
4918 // Turn on IRQ generation and aux data line
4919 if ( inb(0x64) & 0x02 )
4920 BX_PANIC(panic_msg_keyb_buffer_full
,"enabmouse");
4921 outb(0x64, 0x20); // get command byte
4922 while ( (inb(0x64) & 0x01) != 0x01 );
4923 command_byte
= inb(0x60);
4924 //while ( (inb(0x64) & 0x02) );
4925 if ( inb(0x64) & 0x02 )
4926 BX_PANIC(panic_msg_keyb_buffer_full
,"enabmouse");
4927 command_byte
|= 0x02; // turn on IRQ 12 generation
4928 command_byte
&= 0xdf; // enable mouse serial clock line
4929 outb(0x64, 0x60); // write command byte
4930 outb(0x60, command_byte
);
4934 send_to_mouse_ctrl(sendbyte
)
4939 // wait for chance to write to ctrl
4940 if ( inb(0x64) & 0x02 )
4941 BX_PANIC(panic_msg_keyb_buffer_full
,"sendmouse");
4943 outb(0x60, sendbyte
);
4949 get_mouse_data(data
)
4955 while ( (inb(0x64) & 0x21) != 0x21 ) {
4958 response
= inb(0x60);
4961 write_byte(ss
, data
, response
);
4966 set_kbd_command_byte(command_byte
)
4969 if ( inb(0x64) & 0x02 )
4970 BX_PANIC(panic_msg_keyb_buffer_full
,"setkbdcomm");
4973 outb(0x64, 0x60); // write command byte
4974 outb(0x60, command_byte
);
4978 int09_function(DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
)
4979 Bit16u DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
;
4981 Bit8u scancode
, asciicode
, shift_flags
;
4982 Bit8u mf2_flags
, mf2_state
;
4985 // DS has been set to F000 before call
4989 scancode
= GET_AL();
4991 if (scancode
== 0) {
4992 BX_INFO("KBD: int09 handler: AL=0\n");
4997 shift_flags
= read_byte(0x0040, 0x17);
4998 mf2_flags
= read_byte(0x0040, 0x18);
4999 mf2_state
= read_byte(0x0040, 0x96);
5003 case 0x3a: /* Caps Lock press */
5004 shift_flags
^= 0x40;
5005 write_byte(0x0040, 0x17, shift_flags
);
5007 write_byte(0x0040, 0x18, mf2_flags
);
5009 case 0xba: /* Caps Lock release */
5011 write_byte(0x0040, 0x18, mf2_flags
);
5014 case 0x2a: /* L Shift press */
5015 shift_flags
|= 0x02;
5016 write_byte(0x0040, 0x17, shift_flags
);
5018 case 0xaa: /* L Shift release */
5019 shift_flags
&= ~0x02;
5020 write_byte(0x0040, 0x17, shift_flags
);
5023 case 0x36: /* R Shift press */
5024 shift_flags
|= 0x01;
5025 write_byte(0x0040, 0x17, shift_flags
);
5027 case 0xb6: /* R Shift release */
5028 shift_flags
&= ~0x01;
5029 write_byte(0x0040, 0x17, shift_flags
);
5032 case 0x1d: /* Ctrl press */
5033 if ((mf2_state
& 0x01) == 0) {
5034 shift_flags
|= 0x04;
5035 write_byte(0x0040, 0x17, shift_flags
);
5036 if (mf2_state
& 0x02) {
5038 write_byte(0x0040, 0x96, mf2_state
);
5041 write_byte(0x0040, 0x18, mf2_flags
);
5045 case 0x9d: /* Ctrl release */
5046 if ((mf2_state
& 0x01) == 0) {
5047 shift_flags
&= ~0x04;
5048 write_byte(0x0040, 0x17, shift_flags
);
5049 if (mf2_state
& 0x02) {
5051 write_byte(0x0040, 0x96, mf2_state
);
5054 write_byte(0x0040, 0x18, mf2_flags
);
5059 case 0x38: /* Alt press */
5060 shift_flags
|= 0x08;
5061 write_byte(0x0040, 0x17, shift_flags
);
5062 if (mf2_state
& 0x02) {
5064 write_byte(0x0040, 0x96, mf2_state
);
5067 write_byte(0x0040, 0x18, mf2_flags
);
5070 case 0xb8: /* Alt release */
5071 shift_flags
&= ~0x08;
5072 write_byte(0x0040, 0x17, shift_flags
);
5073 if (mf2_state
& 0x02) {
5075 write_byte(0x0040, 0x96, mf2_state
);
5078 write_byte(0x0040, 0x18, mf2_flags
);
5082 case 0x45: /* Num Lock press */
5083 if ((mf2_state
& 0x03) == 0) {
5085 write_byte(0x0040, 0x18, mf2_flags
);
5086 shift_flags
^= 0x20;
5087 write_byte(0x0040, 0x17, shift_flags
);
5090 case 0xc5: /* Num Lock release */
5091 if ((mf2_state
& 0x03) == 0) {
5093 write_byte(0x0040, 0x18, mf2_flags
);
5097 case 0x46: /* Scroll Lock press */
5099 write_byte(0x0040, 0x18, mf2_flags
);
5100 shift_flags
^= 0x10;
5101 write_byte(0x0040, 0x17, shift_flags
);
5104 case 0xc6: /* Scroll Lock release */
5106 write_byte(0x0040, 0x18, mf2_flags
);
5110 if (scancode
& 0x80) {
5111 break; /* toss key releases ... */
5113 if (scancode
> MAX_SCAN_CODE
) {
5114 BX_INFO("KBD: int09h_handler(): unknown scancode read: 0x%02x!\n", scancode
);
5117 if (shift_flags
& 0x08) { /* ALT */
5118 asciicode
= scan_to_scanascii
[scancode
].alt
;
5119 scancode
= scan_to_scanascii
[scancode
].alt
>> 8;
5120 } else if (shift_flags
& 0x04) { /* CONTROL */
5121 asciicode
= scan_to_scanascii
[scancode
].control
;
5122 scancode
= scan_to_scanascii
[scancode
].control
>> 8;
5123 } else if (((mf2_state
& 0x02) > 0) && ((scancode
>= 0x47) && (scancode
<= 0x53))) {
5124 /* extended keys handling */
5126 scancode
= scan_to_scanascii
[scancode
].normal
>> 8;
5127 } else if (shift_flags
& 0x03) { /* LSHIFT + RSHIFT */
5128 /* check if lock state should be ignored
5129 * because a SHIFT key are pressed */
5131 if (shift_flags
& scan_to_scanascii
[scancode
].lock_flags
) {
5132 asciicode
= scan_to_scanascii
[scancode
].normal
;
5133 scancode
= scan_to_scanascii
[scancode
].normal
>> 8;
5135 asciicode
= scan_to_scanascii
[scancode
].shift
;
5136 scancode
= scan_to_scanascii
[scancode
].shift
>> 8;
5139 /* check if lock is on */
5140 if (shift_flags
& scan_to_scanascii
[scancode
].lock_flags
) {
5141 asciicode
= scan_to_scanascii
[scancode
].shift
;
5142 scancode
= scan_to_scanascii
[scancode
].shift
>> 8;
5144 asciicode
= scan_to_scanascii
[scancode
].normal
;
5145 scancode
= scan_to_scanascii
[scancode
].normal
>> 8;
5148 if (scancode
==0 && asciicode
==0) {
5149 BX_INFO("KBD: int09h_handler(): scancode & asciicode are zero?\n");
5151 enqueue_key(scancode
, asciicode
);
5154 if ((scancode
& 0x7f) != 0x1d) {
5158 write_byte(0x0040, 0x96, mf2_state
);
5162 enqueue_key(scan_code
, ascii_code
)
5163 Bit8u scan_code
, ascii_code
;
5165 Bit16u buffer_start
, buffer_end
, buffer_head
, buffer_tail
, temp_tail
;
5168 buffer_start
= 0x001E;
5169 buffer_end
= 0x003E;
5171 buffer_start
= read_word(0x0040, 0x0080);
5172 buffer_end
= read_word(0x0040, 0x0082);
5175 buffer_head
= read_word(0x0040, 0x001A);
5176 buffer_tail
= read_word(0x0040, 0x001C);
5178 temp_tail
= buffer_tail
;
5180 if (buffer_tail
>= buffer_end
)
5181 buffer_tail
= buffer_start
;
5183 if (buffer_tail
== buffer_head
) {
5187 write_byte(0x0040, temp_tail
, ascii_code
);
5188 write_byte(0x0040, temp_tail
+1, scan_code
);
5189 write_word(0x0040, 0x001C, buffer_tail
);
5195 int74_function(make_farcall
, Z
, Y
, X
, status
)
5196 Bit16u make_farcall
, Z
, Y
, X
, status
;
5198 Bit16u ebda_seg
=read_word(0x0040,0x000E);
5199 Bit8u in_byte
, index
, package_count
;
5200 Bit8u mouse_flags_1
, mouse_flags_2
;
5202 BX_DEBUG_INT74("entering int74_function\n");
5205 in_byte
= inb(0x64);
5206 if ( (in_byte
& 0x21) != 0x21 ) {
5209 in_byte
= inb(0x60);
5210 BX_DEBUG_INT74("int74: read byte %02x\n", in_byte
);
5212 mouse_flags_1
= read_byte(ebda_seg
, 0x0026);
5213 mouse_flags_2
= read_byte(ebda_seg
, 0x0027);
5215 if ( (mouse_flags_2
& 0x80) != 0x80 ) {
5219 package_count
= mouse_flags_2
& 0x07;
5220 index
= mouse_flags_1
& 0x07;
5221 write_byte(ebda_seg
, 0x28 + index
, in_byte
);
5223 if ( (index
+1) >= package_count
) {
5224 BX_DEBUG_INT74("int74_function: make_farcall=1\n");
5225 status
= read_byte(ebda_seg
, 0x0028 + 0);
5226 X
= read_byte(ebda_seg
, 0x0028 + 1);
5227 Y
= read_byte(ebda_seg
, 0x0028 + 2);
5230 // check if far call handler installed
5231 if (mouse_flags_2
& 0x80)
5237 write_byte(ebda_seg
, 0x0026, mouse_flags_1
);
5240 #define SET_DISK_RET_STATUS(status) write_byte(0x0040, 0x0074, status)
5245 int13_harddisk(EHAX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
5246 Bit16u EHAX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
5248 Bit32u lba_low
, lba_high
;
5249 Bit16u ebda_seg
=read_word(0x0040,0x000E);
5250 Bit16u cylinder
, head
, sector
;
5251 Bit16u segment
, offset
;
5252 Bit16u npc
, nph
, npspt
, nlc
, nlh
, nlspt
;
5254 Bit8u device
, status
;
5256 BX_DEBUG_INT13_HD("int13_harddisk: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
5258 write_byte(0x0040, 0x008e, 0); // clear completion flag
5260 // basic check : device has to be defined
5261 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_ATA_DEVICES
) ) {
5262 BX_INFO("int13_harddisk: function %02x, ELDL out of range %02x\n", GET_AH(), GET_ELDL());
5266 // Get the ata channel
5267 device
=read_byte(ebda_seg
,&EbdaData
->ata
.hdidmap
[GET_ELDL()-0x80]);
5269 // basic check : device has to be valid
5270 if (device
>= BX_MAX_ATA_DEVICES
) {
5271 BX_INFO("int13_harddisk: function %02x, unmapped device for ELDL=%02x\n", GET_AH(), GET_ELDL());
5277 case 0x00: /* disk controller reset */
5282 case 0x01: /* read disk status */
5283 status
= read_byte(0x0040, 0x0074);
5285 SET_DISK_RET_STATUS(0);
5286 /* set CF if error status read */
5287 if (status
) goto int13_fail_nostatus
;
5288 else goto int13_success_noah
;
5291 case 0x02: // read disk sectors
5292 case 0x03: // write disk sectors
5293 case 0x04: // verify disk sectors
5296 cylinder
= GET_CH();
5297 cylinder
|= ( ((Bit16u
) GET_CL()) << 2) & 0x300;
5298 sector
= (GET_CL() & 0x3f);
5304 if ((count
> 128) || (count
== 0) || (sector
== 0)) {
5305 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n",GET_AH());
5309 nlc
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.cylinders
);
5310 nlh
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.heads
);
5311 nlspt
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.spt
);
5313 // sanity check on cyl heads, sec
5314 if( (cylinder
>= nlc
) || (head
>= nlh
) || (sector
> nlspt
)) {
5315 BX_INFO("int13_harddisk: function %02x, parameters out of range %04x/%04x/%04x!\n", GET_AH(), cylinder
, head
, sector
);
5320 if ( GET_AH() == 0x04 ) goto int13_success
;
5322 nph
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].pchs
.heads
);
5323 npspt
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].pchs
.spt
);
5325 // if needed, translate lchs to lba, and execute command
5326 if ( (nph
!= nlh
) || (npspt
!= nlspt
)) {
5327 lba_low
= ((((Bit32u
)cylinder
* (Bit32u
)nlh
) + (Bit32u
)head
) * (Bit32u
)nlspt
) + (Bit32u
)sector
- 1;
5329 sector
= 0; // this forces the command to be lba
5332 if ( GET_AH() == 0x02 )
5333 status
=ata_cmd_data_in(device
, ATA_CMD_READ_SECTORS
, count
, cylinder
, head
, sector
, lba_low
, lba_high
, segment
, offset
);
5335 status
=ata_cmd_data_out(device
, ATA_CMD_WRITE_SECTORS
, count
, cylinder
, head
, sector
, lba_low
, lba_high
, segment
, offset
);
5337 // Set nb of sector transferred
5338 SET_AL(read_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
));
5341 BX_INFO("int13_harddisk: function %02x, error %02x !\n",GET_AH(),status
);
5343 goto int13_fail_noah
;
5349 case 0x05: /* format disk track */
5350 BX_INFO("format disk track called\n");
5355 case 0x08: /* read disk drive parameters */
5357 // Get logical geometry from table
5358 nlc
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.cylinders
);
5359 nlh
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.heads
);
5360 nlspt
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.spt
);
5361 count
= read_byte(ebda_seg
, &EbdaData
->ata
.hdcount
);
5363 nlc
= nlc
- 2; /* 0 based , last sector not used */
5366 SET_CL(((nlc
>> 2) & 0xc0) | (nlspt
& 0x3f));
5368 SET_DL(count
); /* FIXME returns 0, 1, or n hard drives */
5370 // FIXME should set ES & DI
5375 case 0x10: /* check drive ready */
5376 // should look at 40:8E also???
5378 // Read the status from controller
5379 status
= inb(read_word(ebda_seg
, &EbdaData
->ata
.channels
[device
/2].iobase1
) + ATA_CB_STAT
);
5380 if ( (status
& ( ATA_CB_STAT_BSY
| ATA_CB_STAT_RDY
)) == ATA_CB_STAT_RDY
) {
5385 goto int13_fail_noah
;
5389 case 0x15: /* read disk drive size */
5391 // Get logical geometry from table
5392 nlc
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.cylinders
);
5393 nlh
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.heads
);
5394 nlspt
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].lchs
.spt
);
5396 // Compute sector count seen by int13
5397 lba_low
= (Bit32u
)(nlc
- 1) * (Bit32u
)nlh
* (Bit32u
)nlspt
;
5399 DX
= lba_low
& 0xffff;
5401 SET_AH(3); // hard disk accessible
5402 goto int13_success_noah
;
5405 case 0x41: // IBM/MS installation check
5406 BX
=0xaa55; // install check
5407 SET_AH(0x30); // EDD 3.0
5408 CX
=0x0007; // ext disk access and edd, removable supported
5409 goto int13_success_noah
;
5412 case 0x42: // IBM/MS extended read
5413 case 0x43: // IBM/MS extended write
5414 case 0x44: // IBM/MS verify
5415 case 0x47: // IBM/MS extended seek
5417 count
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->count
);
5418 segment
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->segment
);
5419 offset
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->offset
);
5421 // Get 32 msb lba and check
5422 lba_high
=read_dword(DS
, SI
+(Bit16u
)&Int13Ext
->lba2
);
5423 if (lba_high
> read_dword(ebda_seg
, &EbdaData
->ata
.devices
[device
].sectors_high
) ) {
5424 BX_INFO("int13_harddisk: function %02x. LBA out of range\n",GET_AH());
5428 // Get 32 lsb lba and check
5429 lba_low
=read_dword(DS
, SI
+(Bit16u
)&Int13Ext
->lba1
);
5430 if (lba_high
== read_dword(ebda_seg
, &EbdaData
->ata
.devices
[device
].sectors_high
)
5431 && lba_low
>= read_dword(ebda_seg
, &EbdaData
->ata
.devices
[device
].sectors_low
) ) {
5432 BX_INFO("int13_harddisk: function %02x. LBA out of range\n",GET_AH());
5436 // If verify or seek
5437 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
5440 // Execute the command
5441 if ( GET_AH() == 0x42 )
5442 status
=ata_cmd_data_in(device
, ATA_CMD_READ_SECTORS
, count
, 0, 0, 0, lba_low
, lba_high
, segment
, offset
);
5444 status
=ata_cmd_data_out(device
, ATA_CMD_WRITE_SECTORS
, count
, 0, 0, 0, lba_low
, lba_high
, segment
, offset
);
5446 count
=read_word(ebda_seg
, &EbdaData
->ata
.trsfsectors
);
5447 write_word(DS
, SI
+(Bit16u
)&Int13Ext
->count
, count
);
5450 BX_INFO("int13_harddisk: function %02x, error %02x !\n",GET_AH(),status
);
5452 goto int13_fail_noah
;
5458 case 0x45: // IBM/MS lock/unlock drive
5459 case 0x49: // IBM/MS extended media change
5460 goto int13_success
; // Always success for HD
5463 case 0x46: // IBM/MS eject media
5464 SET_AH(0xb2); // Volume Not Removable
5465 goto int13_fail_noah
; // Always fail for HD
5468 case 0x48: // IBM/MS get drive parameters
5469 size
=read_word(DS
,SI
+(Bit16u
)&Int13DPT
->size
);
5471 // Buffer is too small
5479 npc
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].pchs
.cylinders
);
5480 nph
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].pchs
.heads
);
5481 npspt
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].pchs
.spt
);
5482 lba_low
= read_dword(ebda_seg
, &EbdaData
->ata
.devices
[device
].sectors_low
);
5483 lba_high
= read_dword(ebda_seg
, &EbdaData
->ata
.devices
[device
].sectors_high
);
5484 blksize
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].blksize
);
5486 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x1a);
5487 if (lba_high
|| (lba_low
/npspt
)/nph
> 0x3fff)
5489 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->infos
, 0x00); // geometry is invalid
5490 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->cylinders
, 0x3fff);
5494 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->infos
, 0x02); // geometry is valid
5495 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->cylinders
, (Bit32u
)npc
);
5497 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->heads
, (Bit32u
)nph
);
5498 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->spt
, (Bit32u
)npspt
);
5499 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->sector_count1
, lba_low
);
5500 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->sector_count2
, lba_high
);
5501 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->blksize
, blksize
);
5506 Bit8u channel
, dev
, irq
, mode
, checksum
, i
, translation
;
5507 Bit16u iobase1
, iobase2
, options
;
5509 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x1e);
5511 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->dpte_segment
, ebda_seg
);
5512 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->dpte_offset
, &EbdaData
->ata
.dpte
);
5515 channel
= device
/ 2;
5516 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
5517 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
5518 irq
= read_byte(ebda_seg
, &EbdaData
->ata
.channels
[channel
].irq
);
5519 mode
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].mode
);
5520 translation
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].translation
);
5522 options
= (translation
==ATA_TRANSLATION_NONE
?0:1)<<3; // chs translation
5523 options
|= (1<<4); // lba translation
5524 options
|= (mode
==ATA_MODE_PIO32
?1:0)<<7;
5525 options
|= (translation
==ATA_TRANSLATION_LBA
?1:0)<<9;
5526 options
|= (translation
==ATA_TRANSLATION_RECHS
?3:0)<<9;
5528 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.iobase1
, iobase1
);
5529 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.iobase2
, iobase2
+ ATA_CB_DC
);
5530 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.prefix
, (0xe | (device
% 2))<<4 );
5531 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.unused
, 0xcb );
5532 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.irq
, irq
);
5533 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.blkcount
, 1 );
5534 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.dma
, 0 );
5535 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.pio
, 0 );
5536 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.options
, options
);
5537 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.reserved
, 0);
5539 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.revision
, 0x11);
5541 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.revision
, 0x10);
5544 for (i
=0; i
<15; i
++) checksum
+=read_byte(ebda_seg
, ((Bit8u
*)(&EbdaData
->ata
.dpte
)) + i
);
5545 checksum
= ~checksum
;
5546 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.checksum
, checksum
);
5551 Bit8u channel
, iface
, checksum
, i
;
5554 channel
= device
/ 2;
5555 iface
= read_byte(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iface
);
5556 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
5558 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x42);
5559 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->key
, 0xbedd);
5560 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->dpi_length
, 0x24);
5561 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->reserved1
, 0);
5562 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->reserved2
, 0);
5564 if (iface
==ATA_IFACE_ISA
) {
5565 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[0], 'I');
5566 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[1], 'S');
5567 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[2], 'A');
5568 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[3], 0);
5573 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[0], 'A');
5574 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[1], 'T');
5575 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[2], 'A');
5576 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[3], 0);
5578 if (iface
==ATA_IFACE_ISA
) {
5579 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[0], iobase1
);
5580 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[2], 0);
5581 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[4], 0L);
5586 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[0], device
%2);
5587 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[1], 0);
5588 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[2], 0);
5589 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[4], 0L);
5592 for (i
=30; i
<64; i
++) checksum
+=read_byte(DS
, SI
+ i
);
5593 checksum
= ~checksum
;
5594 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->checksum
, checksum
);
5600 case 0x4e: // // IBM/MS set hardware configuration
5601 // DMA, prefetch, PIO maximum not supported
5614 case 0x09: /* initialize drive parameters */
5615 case 0x0c: /* seek to specified cylinder */
5616 case 0x0d: /* alternate disk reset */
5617 case 0x11: /* recalibrate */
5618 case 0x14: /* controller internal diagnostic */
5619 BX_INFO("int13_harddisk: function %02xh unimplemented, returns success\n", GET_AH());
5623 case 0x0a: /* read disk sectors with ECC */
5624 case 0x0b: /* write disk sectors with ECC */
5625 case 0x18: // set media type for format
5626 case 0x50: // IBM/MS send packet command
5628 BX_INFO("int13_harddisk: function %02xh unsupported, returns fail\n", GET_AH());
5634 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
5636 SET_DISK_RET_STATUS(GET_AH());
5637 int13_fail_nostatus
:
5638 SET_CF(); // error occurred
5642 SET_AH(0x00); // no error
5644 SET_DISK_RET_STATUS(0x00);
5645 CLEAR_CF(); // no error
5649 // ---------------------------------------------------------------------------
5650 // Start of int13 for cdrom
5651 // ---------------------------------------------------------------------------
5654 int13_cdrom(EHBX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
5655 Bit16u EHBX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
5657 Bit16u ebda_seg
=read_word(0x0040,0x000E);
5658 Bit8u device
, status
, locks
;
5661 Bit16u count
, segment
, offset
, i
, size
;
5663 BX_DEBUG_INT13_CD("int13_cdrom: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
5665 SET_DISK_RET_STATUS(0x00);
5667 /* basic check : device should be 0xE0+ */
5668 if( (GET_ELDL() < 0xE0) || (GET_ELDL() >= 0xE0+BX_MAX_ATA_DEVICES
) ) {
5669 BX_INFO("int13_cdrom: function %02x, ELDL out of range %02x\n", GET_AH(), GET_ELDL());
5673 // Get the ata channel
5674 device
=read_byte(ebda_seg
,&EbdaData
->ata
.cdidmap
[GET_ELDL()-0xE0]);
5676 /* basic check : device has to be valid */
5677 if (device
>= BX_MAX_ATA_DEVICES
) {
5678 BX_INFO("int13_cdrom: function %02x, unmapped device for ELDL=%02x\n", GET_AH(), GET_ELDL());
5684 // all those functions return SUCCESS
5685 case 0x00: /* disk controller reset */
5686 case 0x09: /* initialize drive parameters */
5687 case 0x0c: /* seek to specified cylinder */
5688 case 0x0d: /* alternate disk reset */
5689 case 0x10: /* check drive ready */
5690 case 0x11: /* recalibrate */
5691 case 0x14: /* controller internal diagnostic */
5692 case 0x16: /* detect disk change */
5696 // all those functions return disk write-protected
5697 case 0x03: /* write disk sectors */
5698 case 0x05: /* format disk track */
5699 case 0x43: // IBM/MS extended write
5701 goto int13_fail_noah
;
5704 case 0x01: /* read disk status */
5705 status
= read_byte(0x0040, 0x0074);
5707 SET_DISK_RET_STATUS(0);
5709 /* set CF if error status read */
5710 if (status
) goto int13_fail_nostatus
;
5711 else goto int13_success_noah
;
5714 case 0x15: /* read disk drive size */
5716 goto int13_fail_noah
;
5719 case 0x41: // IBM/MS installation check
5720 BX
=0xaa55; // install check
5721 SET_AH(0x30); // EDD 2.1
5722 CX
=0x0007; // ext disk access, removable and edd
5723 goto int13_success_noah
;
5726 case 0x42: // IBM/MS extended read
5727 case 0x44: // IBM/MS verify sectors
5728 case 0x47: // IBM/MS extended seek
5730 count
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->count
);
5731 segment
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->segment
);
5732 offset
=read_word(DS
, SI
+(Bit16u
)&Int13Ext
->offset
);
5734 // Can't use 64 bits lba
5735 lba
=read_dword(DS
, SI
+(Bit16u
)&Int13Ext
->lba2
);
5737 BX_PANIC("int13_cdrom: function %02x. Can't use 64bits lba\n",GET_AH());
5742 lba
=read_dword(DS
, SI
+(Bit16u
)&Int13Ext
->lba1
);
5744 // If verify or seek
5745 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
5748 memsetb(get_SS(),atacmd
,0,12);
5749 atacmd
[0]=0x28; // READ command
5750 atacmd
[7]=(count
& 0xff00) >> 8; // Sectors
5751 atacmd
[8]=(count
& 0x00ff); // Sectors
5752 atacmd
[2]=(lba
& 0xff000000) >> 24; // LBA
5753 atacmd
[3]=(lba
& 0x00ff0000) >> 16;
5754 atacmd
[4]=(lba
& 0x0000ff00) >> 8;
5755 atacmd
[5]=(lba
& 0x000000ff);
5756 status
= ata_cmd_packet(device
, 12, get_SS(), atacmd
, 0, count
*2048L, ATA_DATA_IN
, segment
,offset
);
5758 count
= (Bit16u
)(read_dword(ebda_seg
, &EbdaData
->ata
.trsfbytes
) >> 11);
5759 write_word(DS
, SI
+(Bit16u
)&Int13Ext
->count
, count
);
5762 BX_INFO("int13_cdrom: function %02x, status %02x !\n",GET_AH(),status
);
5764 goto int13_fail_noah
;
5770 case 0x45: // IBM/MS lock/unlock drive
5771 if (GET_AL() > 2) goto int13_fail
;
5773 locks
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].lock
);
5777 if (locks
== 0xff) {
5780 goto int13_fail_noah
;
5782 write_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].lock
, ++locks
);
5786 if (locks
== 0x00) {
5789 goto int13_fail_noah
;
5791 write_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].lock
, --locks
);
5792 SET_AL(locks
==0?0:1);
5795 SET_AL(locks
==0?0:1);
5801 case 0x46: // IBM/MS eject media
5802 locks
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].lock
);
5805 SET_AH(0xb1); // media locked
5806 goto int13_fail_noah
;
5808 // FIXME should handle 0x31 no media in device
5809 // FIXME should handle 0xb5 valid request failed
5811 // Call removable media eject
5818 mov _int13_cdrom
.status
+ 2[bp
], ah
5819 jnc int13_cdrom_rme_end
5820 mov _int13_cdrom
.status
, #1
5821 int13_cdrom_rme_end
:
5826 SET_AH(0xb1); // media locked
5827 goto int13_fail_noah
;
5833 case 0x48: // IBM/MS get drive parameters
5834 size
= read_word(DS
,SI
+(Bit16u
)&Int13Ext
->size
);
5836 // Buffer is too small
5842 Bit16u cylinders
, heads
, spt
, blksize
;
5844 blksize
= read_word(ebda_seg
, &EbdaData
->ata
.devices
[device
].blksize
);
5846 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x1a);
5847 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->infos
, 0x74); // removable, media change, lockable, max values
5848 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->cylinders
, 0xffffffff);
5849 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->heads
, 0xffffffff);
5850 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->spt
, 0xffffffff);
5851 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->sector_count1
, 0xffffffff); // FIXME should be Bit64
5852 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->sector_count2
, 0xffffffff);
5853 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->blksize
, blksize
);
5858 Bit8u channel
, dev
, irq
, mode
, checksum
, i
;
5859 Bit16u iobase1
, iobase2
, options
;
5861 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x1e);
5863 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->dpte_segment
, ebda_seg
);
5864 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->dpte_offset
, &EbdaData
->ata
.dpte
);
5867 channel
= device
/ 2;
5868 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
5869 iobase2
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase2
);
5870 irq
= read_byte(ebda_seg
, &EbdaData
->ata
.channels
[channel
].irq
);
5871 mode
= read_byte(ebda_seg
, &EbdaData
->ata
.devices
[device
].mode
);
5873 // FIXME atapi device
5874 options
= (1<<4); // lba translation
5875 options
|= (1<<5); // removable device
5876 options
|= (1<<6); // atapi device
5877 options
|= (mode
==ATA_MODE_PIO32
?1:0<<7);
5879 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.iobase1
, iobase1
);
5880 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.iobase2
, iobase2
+ ATA_CB_DC
);
5881 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.prefix
, (0xe | (device
% 2))<<4 );
5882 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.unused
, 0xcb );
5883 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.irq
, irq
);
5884 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.blkcount
, 1 );
5885 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.dma
, 0 );
5886 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.pio
, 0 );
5887 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.options
, options
);
5888 write_word(ebda_seg
, &EbdaData
->ata
.dpte
.reserved
, 0);
5889 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.revision
, 0x11);
5892 for (i
=0; i
<15; i
++) checksum
+=read_byte(ebda_seg
, ((Bit8u
*)(&EbdaData
->ata
.dpte
)) + i
);
5893 checksum
= ~checksum
;
5894 write_byte(ebda_seg
, &EbdaData
->ata
.dpte
.checksum
, checksum
);
5899 Bit8u channel
, iface
, checksum
, i
;
5902 channel
= device
/ 2;
5903 iface
= read_byte(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iface
);
5904 iobase1
= read_word(ebda_seg
, &EbdaData
->ata
.channels
[channel
].iobase1
);
5906 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->size
, 0x42);
5907 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->key
, 0xbedd);
5908 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->dpi_length
, 0x24);
5909 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->reserved1
, 0);
5910 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->reserved2
, 0);
5912 if (iface
==ATA_IFACE_ISA
) {
5913 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[0], 'I');
5914 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[1], 'S');
5915 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[2], 'A');
5916 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->host_bus
[3], 0);
5921 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[0], 'A');
5922 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[1], 'T');
5923 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[2], 'A');
5924 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->iface_type
[3], 0);
5926 if (iface
==ATA_IFACE_ISA
) {
5927 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[0], iobase1
);
5928 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[2], 0);
5929 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->iface_path
[4], 0L);
5934 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[0], device
%2);
5935 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[1], 0);
5936 write_word(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[2], 0);
5937 write_dword(DS
, SI
+(Bit16u
)&Int13DPT
->device_path
[4], 0L);
5940 for (i
=30; i
<64; i
++) checksum
+=read_byte(DS
, SI
+ i
);
5941 checksum
= ~checksum
;
5942 write_byte(DS
, SI
+(Bit16u
)&Int13DPT
->checksum
, checksum
);
5948 case 0x49: // IBM/MS extended media change
5949 // always send changed ??
5951 goto int13_fail_nostatus
;
5954 case 0x4e: // // IBM/MS set hardware configuration
5955 // DMA, prefetch, PIO maximum not supported
5968 // all those functions return unimplemented
5969 case 0x02: /* read sectors */
5970 case 0x04: /* verify sectors */
5971 case 0x08: /* read disk drive parameters */
5972 case 0x0a: /* read disk sectors with ECC */
5973 case 0x0b: /* write disk sectors with ECC */
5974 case 0x18: /* set media type for format */
5975 case 0x50: // ? - send packet command
5977 BX_INFO("int13_cdrom: unsupported AH=%02x\n", GET_AH());
5983 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
5985 SET_DISK_RET_STATUS(GET_AH());
5986 int13_fail_nostatus
:
5987 SET_CF(); // error occurred
5991 SET_AH(0x00); // no error
5993 SET_DISK_RET_STATUS(0x00);
5994 CLEAR_CF(); // no error
5998 // ---------------------------------------------------------------------------
5999 // End of int13 for cdrom
6000 // ---------------------------------------------------------------------------
6002 #if BX_ELTORITO_BOOT
6003 // ---------------------------------------------------------------------------
6004 // Start of int13 for eltorito functions
6005 // ---------------------------------------------------------------------------
6008 int13_eltorito(DS
, ES
, DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
6009 Bit16u DS
, ES
, DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
6011 Bit16u ebda_seg
=read_word(0x0040,0x000E);
6013 BX_DEBUG_INT13_ET("int13_eltorito: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
6014 // BX_DEBUG_INT13_ET("int13_eltorito: SS=%04x DS=%04x ES=%04x DI=%04x SI=%04x\n",get_SS(), DS, ES, DI, SI);
6018 // FIXME ElTorito Various. Should be implemented
6019 case 0x4a: // ElTorito - Initiate disk emu
6020 case 0x4c: // ElTorito - Initiate disk emu and boot
6021 case 0x4d: // ElTorito - Return Boot catalog
6022 BX_PANIC("Int13 eltorito call with AX=%04x. Please report\n",AX
);
6026 case 0x4b: // ElTorito - Terminate disk emu
6027 // FIXME ElTorito Hardcoded
6028 write_byte(DS
,SI
+0x00,0x13);
6029 write_byte(DS
,SI
+0x01,read_byte(ebda_seg
,&EbdaData
->cdemu
.media
));
6030 write_byte(DS
,SI
+0x02,read_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
));
6031 write_byte(DS
,SI
+0x03,read_byte(ebda_seg
,&EbdaData
->cdemu
.controller_index
));
6032 write_dword(DS
,SI
+0x04,read_dword(ebda_seg
,&EbdaData
->cdemu
.ilba
));
6033 write_word(DS
,SI
+0x08,read_word(ebda_seg
,&EbdaData
->cdemu
.device_spec
));
6034 write_word(DS
,SI
+0x0a,read_word(ebda_seg
,&EbdaData
->cdemu
.buffer_segment
));
6035 write_word(DS
,SI
+0x0c,read_word(ebda_seg
,&EbdaData
->cdemu
.load_segment
));
6036 write_word(DS
,SI
+0x0e,read_word(ebda_seg
,&EbdaData
->cdemu
.sector_count
));
6037 write_byte(DS
,SI
+0x10,read_byte(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
));
6038 write_byte(DS
,SI
+0x11,read_byte(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
));
6039 write_byte(DS
,SI
+0x12,read_byte(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
));
6041 // If we have to terminate emulation
6042 if(GET_AL() == 0x00) {
6043 // FIXME ElTorito Various. Should be handled accordingly to spec
6044 write_byte(ebda_seg
,&EbdaData
->cdemu
.active
, 0x00); // bye bye
6051 BX_INFO("int13_eltorito: unsupported AH=%02x\n", GET_AH());
6057 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
6058 SET_DISK_RET_STATUS(GET_AH());
6059 SET_CF(); // error occurred
6063 SET_AH(0x00); // no error
6064 SET_DISK_RET_STATUS(0x00);
6065 CLEAR_CF(); // no error
6069 // ---------------------------------------------------------------------------
6070 // End of int13 for eltorito functions
6071 // ---------------------------------------------------------------------------
6073 // ---------------------------------------------------------------------------
6074 // Start of int13 when emulating a device from the cd
6075 // ---------------------------------------------------------------------------
6078 int13_cdemu(DS
, ES
, DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
6079 Bit16u DS
, ES
, DI
, SI
, BP
, SP
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
6081 Bit16u ebda_seg
=read_word(0x0040,0x000E);
6082 Bit8u device
, status
;
6083 Bit16u vheads
, vspt
, vcylinders
;
6084 Bit16u head
, sector
, cylinder
, nbsectors
;
6085 Bit32u vlba
, ilba
, slba
, elba
;
6086 Bit16u before
, segment
, offset
;
6089 BX_DEBUG_INT13_ET("int13_cdemu: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
6091 /* at this point, we are emulating a floppy/harddisk */
6093 // Recompute the device number
6094 device
= read_byte(ebda_seg
,&EbdaData
->cdemu
.controller_index
) * 2;
6095 device
+= read_byte(ebda_seg
,&EbdaData
->cdemu
.device_spec
);
6097 SET_DISK_RET_STATUS(0x00);
6099 /* basic checks : emulation should be active, dl should equal the emulated drive */
6100 if( (read_byte(ebda_seg
,&EbdaData
->cdemu
.active
) ==0 )
6101 || (read_byte(ebda_seg
,&EbdaData
->cdemu
.emulated_drive
) != GET_DL())) {
6102 BX_INFO("int13_cdemu: function %02x, emulation not active for DL= %02x\n", GET_AH(), GET_DL());
6108 // all those functions return SUCCESS
6109 case 0x00: /* disk controller reset */
6110 case 0x09: /* initialize drive parameters */
6111 case 0x0c: /* seek to specified cylinder */
6112 case 0x0d: /* alternate disk reset */ // FIXME ElTorito Various. should really reset ?
6113 case 0x10: /* check drive ready */ // FIXME ElTorito Various. should check if ready ?
6114 case 0x11: /* recalibrate */
6115 case 0x14: /* controller internal diagnostic */
6116 case 0x16: /* detect disk change */
6120 // all those functions return disk write-protected
6121 case 0x03: /* write disk sectors */
6122 case 0x05: /* format disk track */
6124 goto int13_fail_noah
;
6127 case 0x01: /* read disk status */
6128 status
=read_byte(0x0040, 0x0074);
6130 SET_DISK_RET_STATUS(0);
6132 /* set CF if error status read */
6133 if (status
) goto int13_fail_nostatus
;
6134 else goto int13_success_noah
;
6137 case 0x02: // read disk sectors
6138 case 0x04: // verify disk sectors
6139 vspt
= read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
);
6140 vcylinders
= read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
);
6141 vheads
= read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
);
6143 ilba
= read_dword(ebda_seg
,&EbdaData
->cdemu
.ilba
);
6145 sector
= GET_CL() & 0x003f;
6146 cylinder
= (GET_CL() & 0x00c0) << 2 | GET_CH();
6148 nbsectors
= GET_AL();
6152 // no sector to read ?
6153 if(nbsectors
==0) goto int13_success
;
6155 // sanity checks sco openserver needs this!
6157 || (cylinder
>= vcylinders
)
6158 || (head
>= vheads
)) {
6162 // After controls, verify do nothing
6163 if (GET_AH() == 0x04) goto int13_success
;
6165 segment
= ES
+(BX
/ 16);
6168 // calculate the virtual lba inside the image
6169 vlba
=((((Bit32u
)cylinder
*(Bit32u
)vheads
)+(Bit32u
)head
)*(Bit32u
)vspt
)+((Bit32u
)(sector
-1));
6171 // In advance so we don't loose the count
6175 slba
= (Bit32u
)vlba
/4;
6176 before
= (Bit16u
)vlba
%4;
6179 elba
= (Bit32u
)(vlba
+nbsectors
-1)/4;
6181 memsetb(get_SS(),atacmd
,0,12);
6182 atacmd
[0]=0x28; // READ command
6183 atacmd
[7]=((Bit16u
)(elba
-slba
+1) & 0xff00) >> 8; // Sectors
6184 atacmd
[8]=((Bit16u
)(elba
-slba
+1) & 0x00ff); // Sectors
6185 atacmd
[2]=(ilba
+slba
& 0xff000000) >> 24; // LBA
6186 atacmd
[3]=(ilba
+slba
& 0x00ff0000) >> 16;
6187 atacmd
[4]=(ilba
+slba
& 0x0000ff00) >> 8;
6188 atacmd
[5]=(ilba
+slba
& 0x000000ff);
6189 if((status
= ata_cmd_packet(device
, 12, get_SS(), atacmd
, before
*512, nbsectors
*512L, ATA_DATA_IN
, segment
,offset
)) != 0) {
6190 BX_INFO("int13_cdemu: function %02x, error %02x !\n",GET_AH(),status
);
6193 goto int13_fail_noah
;
6199 case 0x08: /* read disk drive parameters */
6200 vspt
=read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.spt
);
6201 vcylinders
=read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.cylinders
) - 1;
6202 vheads
=read_word(ebda_seg
,&EbdaData
->cdemu
.vdevice
.heads
) - 1;
6206 SET_CH( vcylinders
& 0xff );
6207 SET_CL((( vcylinders
>> 2) & 0xc0) | ( vspt
& 0x3f ));
6209 SET_DL( 0x02 ); // FIXME ElTorito Various. should send the real count of drives 1 or 2
6210 // FIXME ElTorito Harddisk. should send the HD count
6212 switch(read_byte(ebda_seg
,&EbdaData
->cdemu
.media
)) {
6213 case 0x01: SET_BL( 0x02 ); break;
6214 case 0x02: SET_BL( 0x04 ); break;
6215 case 0x03: SET_BL( 0x06 ); break;
6221 mov ax
, #diskette_param_table2
6222 mov _int13_cdemu
.DI
+2[bp
], ax
6223 mov _int13_cdemu
.ES
+2[bp
], cs
6229 case 0x15: /* read disk drive size */
6230 // FIXME ElTorito Harddisk. What geometry to send ?
6232 goto int13_success_noah
;
6235 // all those functions return unimplemented
6236 case 0x0a: /* read disk sectors with ECC */
6237 case 0x0b: /* write disk sectors with ECC */
6238 case 0x18: /* set media type for format */
6239 case 0x41: // IBM/MS installation check
6240 // FIXME ElTorito Harddisk. Darwin would like to use EDD
6241 case 0x42: // IBM/MS extended read
6242 case 0x43: // IBM/MS extended write
6243 case 0x44: // IBM/MS verify sectors
6244 case 0x45: // IBM/MS lock/unlock drive
6245 case 0x46: // IBM/MS eject media
6246 case 0x47: // IBM/MS extended seek
6247 case 0x48: // IBM/MS get drive parameters
6248 case 0x49: // IBM/MS extended media change
6249 case 0x4e: // ? - set hardware configuration
6250 case 0x50: // ? - send packet command
6252 BX_INFO("int13_cdemu function AH=%02x unsupported, returns fail\n", GET_AH());
6258 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
6260 SET_DISK_RET_STATUS(GET_AH());
6261 int13_fail_nostatus
:
6262 SET_CF(); // error occurred
6266 SET_AH(0x00); // no error
6268 SET_DISK_RET_STATUS(0x00);
6269 CLEAR_CF(); // no error
6273 // ---------------------------------------------------------------------------
6274 // End of int13 when emulating a device from the cd
6275 // ---------------------------------------------------------------------------
6277 #endif // BX_ELTORITO_BOOT
6279 #else //BX_USE_ATADRV
6282 outLBA(cylinder
,hd_heads
,head
,hd_sectors
,sector
,dl
)
6297 mov ax
,4[bp
] // cylinder
6299 mov bl
,6[bp
] // hd_heads
6302 mov bl
,8[bp
] // head
6304 mov bl
,10[bp
] // hd_sectors
6306 mov bl
,12[bp
] // sector
6335 int13_harddisk(EHAX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
6336 Bit16u EHAX
, DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
6338 Bit8u drive
, num_sectors
, sector
, head
, status
, mod
;
6342 Bit16u max_cylinder
, cylinder
, total_sectors
;
6343 Bit16u hd_cylinders
;
6344 Bit8u hd_heads
, hd_sectors
;
6351 Bit16u count
, segment
, offset
;
6355 BX_DEBUG_INT13_HD("int13 harddisk: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
6357 write_byte(0x0040, 0x008e, 0); // clear completion flag
6359 /* at this point, DL is >= 0x80 to be passed from the floppy int13h
6361 /* check how many disks first (cmos reg 0x12), return an error if
6362 drive not present */
6363 drive_map
= inb_cmos(0x12);
6364 drive_map
= (((drive_map
& 0xf0)==0) ? 0 : 1) |
6365 (((drive_map
& 0x0f)==0) ? 0 : 2);
6366 n_drives
= (drive_map
==0) ? 0 :
6367 ((drive_map
==3) ? 2 : 1);
6369 if (!(drive_map
& (1<<(GET_ELDL()&0x7f)))) { /* allow 0, 1, or 2 disks */
6371 SET_DISK_RET_STATUS(0x01);
6372 SET_CF(); /* error occurred */
6378 case 0x00: /* disk controller reset */
6379 BX_DEBUG_INT13_HD("int13_f00\n");
6382 SET_DISK_RET_STATUS(0);
6383 set_diskette_ret_status(0);
6384 set_diskette_current_cyl(0, 0); /* current cylinder, diskette 1 */
6385 set_diskette_current_cyl(1, 0); /* current cylinder, diskette 2 */
6386 CLEAR_CF(); /* successful */
6390 case 0x01: /* read disk status */
6391 BX_DEBUG_INT13_HD("int13_f01\n");
6392 status
= read_byte(0x0040, 0x0074);
6394 SET_DISK_RET_STATUS(0);
6395 /* set CF if error status read */
6396 if (status
) SET_CF();
6401 case 0x04: // verify disk sectors
6402 case 0x02: // read disk sectors
6404 get_hd_geometry(drive
, &hd_cylinders
, &hd_heads
, &hd_sectors
);
6406 num_sectors
= GET_AL();
6407 cylinder
= (GET_CL() & 0x00c0) << 2 | GET_CH();
6408 sector
= (GET_CL() & 0x3f);
6412 if (hd_cylinders
> 1024) {
6413 if (hd_cylinders
<= 2048) {
6416 else if (hd_cylinders
<= 4096) {
6419 else if (hd_cylinders
<= 8192) {
6422 else { // hd_cylinders <= 16384
6426 ax
= head
/ hd_heads
;
6427 cyl_mod
= ax
& 0xff;
6429 cylinder
|= cyl_mod
;
6432 if ( (cylinder
>= hd_cylinders
) ||
6433 (sector
> hd_sectors
) ||
6434 (head
>= hd_heads
) ) {
6436 SET_DISK_RET_STATUS(1);
6437 SET_CF(); /* error occurred */
6441 if ( (num_sectors
> 128) || (num_sectors
== 0) )
6442 BX_PANIC("int13_harddisk: num_sectors out of range!\n");
6445 BX_PANIC("hard drive BIOS:(read/verify) head > 15\n");
6447 if ( GET_AH() == 0x04 ) {
6449 SET_DISK_RET_STATUS(0);
6454 status
= inb(0x1f7);
6455 if (status
& 0x80) {
6456 BX_PANIC("hard drive BIOS:(read/verify) BUSY bit set\n");
6458 outb(0x01f2, num_sectors
);
6459 /* activate LBA? (tomv) */
6460 if (hd_heads
> 16) {
6461 BX_DEBUG_INT13_HD("CHS: %x %x %x\n", cylinder
, head
, sector
);
6462 outLBA(cylinder
,hd_heads
,head
,hd_sectors
,sector
,drive
);
6465 outb(0x01f3, sector
);
6466 outb(0x01f4, cylinder
& 0x00ff);
6467 outb(0x01f5, cylinder
>> 8);
6468 outb(0x01f6, 0xa0 | ((drive
& 0x01)<<4) | (head
& 0x0f));
6473 status
= inb(0x1f7);
6474 if ( !(status
& 0x80) ) break;
6477 if (status
& 0x01) {
6478 BX_PANIC("hard drive BIOS:(read/verify) read error\n");
6479 } else if ( !(status
& 0x08) ) {
6480 BX_DEBUG_INT13_HD("status was %02x\n", (unsigned) status
);
6481 BX_PANIC("hard drive BIOS:(read/verify) expected DRQ=1\n");
6488 sti
;; enable higher priority interrupts
6493 ;; store temp bx in real DI
register
6496 mov di
, _int13_harddisk
.tempbx
+ 2 [bp
]
6499 ;; adjust
if there will be an overrun
6501 jbe i13_f02_no_adjust
6503 sub di
, #0x0200 ; sub 512 bytes from offset
6505 add ax
, #0x0020 ; add 512 to segment
6509 mov cx
, #0x0100 ;; counter (256 words = 512b)
6510 mov dx
, #0x01f0 ;; AT data read port
6513 insw
;; CX words transfered from
port(DX
) to ES
:[DI
]
6516 ;; store real DI
register back to temp bx
6519 mov _int13_harddisk
.tempbx
+ 2 [bp
], di
6525 if (num_sectors
== 0) {
6526 status
= inb(0x1f7);
6527 if ( (status
& 0xc9) != 0x40 )
6528 BX_PANIC("no sectors left to read/verify, status is %02x\n", (unsigned) status
);
6532 status
= inb(0x1f7);
6533 if ( (status
& 0xc9) != 0x48 )
6534 BX_PANIC("more sectors left to read/verify, status is %02x\n", (unsigned) status
);
6540 SET_DISK_RET_STATUS(0);
6541 SET_AL(sector_count
);
6542 CLEAR_CF(); /* successful */
6547 case 0x03: /* write disk sectors */
6548 BX_DEBUG_INT13_HD("int13_f03\n");
6549 drive
= GET_ELDL ();
6550 get_hd_geometry(drive
, &hd_cylinders
, &hd_heads
, &hd_sectors
);
6552 num_sectors
= GET_AL();
6553 cylinder
= GET_CH();
6554 cylinder
|= ( ((Bit16u
) GET_CL()) << 2) & 0x300;
6555 sector
= (GET_CL() & 0x3f);
6558 if (hd_cylinders
> 1024) {
6559 if (hd_cylinders
<= 2048) {
6562 else if (hd_cylinders
<= 4096) {
6565 else if (hd_cylinders
<= 8192) {
6568 else { // hd_cylinders <= 16384
6572 ax
= head
/ hd_heads
;
6573 cyl_mod
= ax
& 0xff;
6575 cylinder
|= cyl_mod
;
6578 if ( (cylinder
>= hd_cylinders
) ||
6579 (sector
> hd_sectors
) ||
6580 (head
>= hd_heads
) ) {
6582 SET_DISK_RET_STATUS(1);
6583 SET_CF(); /* error occurred */
6587 if ( (num_sectors
> 128) || (num_sectors
== 0) )
6588 BX_PANIC("int13_harddisk: num_sectors out of range!\n");
6591 BX_PANIC("hard drive BIOS:(read) head > 15\n");
6593 status
= inb(0x1f7);
6594 if (status
& 0x80) {
6595 BX_PANIC("hard drive BIOS:(read) BUSY bit set\n");
6597 // should check for Drive Ready Bit also in status reg
6598 outb(0x01f2, num_sectors
);
6600 /* activate LBA? (tomv) */
6601 if (hd_heads
> 16) {
6602 BX_DEBUG_INT13_HD("CHS (write): %x %x %x\n", cylinder
, head
, sector
);
6603 outLBA(cylinder
,hd_heads
,head
,hd_sectors
,sector
,GET_ELDL());
6606 outb(0x01f3, sector
);
6607 outb(0x01f4, cylinder
& 0x00ff);
6608 outb(0x01f5, cylinder
>> 8);
6609 outb(0x01f6, 0xa0 | ((GET_ELDL() & 0x01)<<4) | (head
& 0x0f));
6613 // wait for busy bit to turn off after seeking
6615 status
= inb(0x1f7);
6616 if ( !(status
& 0x80) ) break;
6619 if ( !(status
& 0x08) ) {
6620 BX_DEBUG_INT13_HD("status was %02x\n", (unsigned) status
);
6621 BX_PANIC("hard drive BIOS:(write) data-request bit not set\n");
6628 sti
;; enable higher priority interrupts
6633 ;; store temp bx in real SI
register
6636 mov si
, _int13_harddisk
.tempbx
+ 2 [bp
]
6639 ;; adjust
if there will be an overrun
6641 jbe i13_f03_no_adjust
6643 sub si
, #0x0200 ; sub 512 bytes from offset
6645 add ax
, #0x0020 ; add 512 to segment
6649 mov cx
, #0x0100 ;; counter (256 words = 512b)
6650 mov dx
, #0x01f0 ;; AT data read port
6654 outsw
;; CX words tranfered from ES
:[SI
] to
port(DX
)
6656 ;; store real SI
register back to temp bx
6659 mov _int13_harddisk
.tempbx
+ 2 [bp
], si
6665 if (num_sectors
== 0) {
6666 status
= inb(0x1f7);
6667 if ( (status
& 0xe9) != 0x40 )
6668 BX_PANIC("no sectors left to write, status is %02x\n", (unsigned) status
);
6672 status
= inb(0x1f7);
6673 if ( (status
& 0xc9) != 0x48 )
6674 BX_PANIC("more sectors left to write, status is %02x\n", (unsigned) status
);
6680 SET_DISK_RET_STATUS(0);
6681 SET_AL(sector_count
);
6682 CLEAR_CF(); /* successful */
6686 case 0x05: /* format disk track */
6687 BX_DEBUG_INT13_HD("int13_f05\n");
6688 BX_PANIC("format disk track called\n");
6691 SET_DISK_RET_STATUS(0);
6692 CLEAR_CF(); /* successful */
6696 case 0x08: /* read disk drive parameters */
6697 BX_DEBUG_INT13_HD("int13_f08\n");
6699 drive
= GET_ELDL ();
6700 get_hd_geometry(drive
, &hd_cylinders
, &hd_heads
, &hd_sectors
);
6704 if (hd_cylinders
<= 1024) {
6705 // hd_cylinders >>= 0;
6708 else if (hd_cylinders
<= 2048) {
6712 else if (hd_cylinders
<= 4096) {
6716 else if (hd_cylinders
<= 8192) {
6720 else { // hd_cylinders <= 16384
6725 max_cylinder
= hd_cylinders
- 2; /* 0 based */
6727 SET_CH(max_cylinder
& 0xff);
6728 SET_CL(((max_cylinder
>> 2) & 0xc0) | (hd_sectors
& 0x3f));
6729 SET_DH(hd_heads
- 1);
6730 SET_DL(n_drives
); /* returns 0, 1, or 2 hard drives */
6732 SET_DISK_RET_STATUS(0);
6733 CLEAR_CF(); /* successful */
6738 case 0x09: /* initialize drive parameters */
6739 BX_DEBUG_INT13_HD("int13_f09\n");
6741 SET_DISK_RET_STATUS(0);
6742 CLEAR_CF(); /* successful */
6746 case 0x0a: /* read disk sectors with ECC */
6747 BX_DEBUG_INT13_HD("int13_f0a\n");
6748 case 0x0b: /* write disk sectors with ECC */
6749 BX_DEBUG_INT13_HD("int13_f0b\n");
6750 BX_PANIC("int13h Functions 0Ah & 0Bh not implemented!\n");
6754 case 0x0c: /* seek to specified cylinder */
6755 BX_DEBUG_INT13_HD("int13_f0c\n");
6756 BX_INFO("int13h function 0ch (seek) not implemented!\n");
6758 SET_DISK_RET_STATUS(0);
6759 CLEAR_CF(); /* successful */
6763 case 0x0d: /* alternate disk reset */
6764 BX_DEBUG_INT13_HD("int13_f0d\n");
6766 SET_DISK_RET_STATUS(0);
6767 CLEAR_CF(); /* successful */
6771 case 0x10: /* check drive ready */
6772 BX_DEBUG_INT13_HD("int13_f10\n");
6774 //SET_DISK_RET_STATUS(0);
6775 //CLEAR_CF(); /* successful */
6779 // should look at 40:8E also???
6780 status
= inb(0x01f7);
6781 if ( (status
& 0xc0) == 0x40 ) {
6783 SET_DISK_RET_STATUS(0);
6784 CLEAR_CF(); // drive ready
6789 SET_DISK_RET_STATUS(0xAA);
6790 SET_CF(); // not ready
6795 case 0x11: /* recalibrate */
6796 BX_DEBUG_INT13_HD("int13_f11\n");
6798 SET_DISK_RET_STATUS(0);
6799 CLEAR_CF(); /* successful */
6803 case 0x14: /* controller internal diagnostic */
6804 BX_DEBUG_INT13_HD("int13_f14\n");
6806 SET_DISK_RET_STATUS(0);
6807 CLEAR_CF(); /* successful */
6812 case 0x15: /* read disk drive size */
6814 get_hd_geometry(drive
, &hd_cylinders
, &hd_heads
, &hd_sectors
);
6818 mov al
, _int13_harddisk
.hd_heads
+ 2 [bp
]
6819 mov ah
, _int13_harddisk
.hd_sectors
+ 2 [bp
]
6820 mul al
, ah
;; ax
= heads
* sectors
6821 mov bx
, _int13_harddisk
.hd_cylinders
+ 2 [bp
]
6822 dec bx
;; use (cylinders
- 1) ???
6823 mul ax
, bx
;; dx
:ax
= (cylinders
-1) * (heads
* sectors
)
6824 ;; now we need to move the
32bit result dx
:ax to what the
6825 ;; BIOS wants which is cx
:dx
.
6826 ;; and then into CX
:DX on the stack
6827 mov _int13_harddisk
.CX
+ 2 [bp
], dx
6828 mov _int13_harddisk
.DX
+ 2 [bp
], ax
6831 SET_AH(3); // hard disk accessible
6832 SET_DISK_RET_STATUS(0); // ??? should this be 0
6833 CLEAR_CF(); // successful
6837 case 0x18: // set media type for format
6838 case 0x41: // IBM/MS
6839 case 0x42: // IBM/MS
6840 case 0x43: // IBM/MS
6841 case 0x44: // IBM/MS
6842 case 0x45: // IBM/MS lock/unlock drive
6843 case 0x46: // IBM/MS eject media
6844 case 0x47: // IBM/MS extended seek
6845 case 0x49: // IBM/MS extended media change
6846 case 0x50: // IBM/MS send packet command
6848 BX_INFO("int13_harddisk: unsupported AH=%02x\n", GET_AH());
6850 SET_AH(1); // code=invalid function in AH or invalid parameter
6851 SET_DISK_RET_STATUS(1);
6852 SET_CF(); /* unsuccessful */
6858 static char panic_msg_reg12h
[] = "HD%d cmos reg 12h not type F\n";
6859 static char panic_msg_reg19h
[] = "HD%d cmos reg %02xh not user definable type 47\n";
6862 get_hd_geometry(drive
, hd_cylinders
, hd_heads
, hd_sectors
)
6864 Bit16u
*hd_cylinders
;
6874 if (drive
== 0x80) {
6875 hd_type
= inb_cmos(0x12) & 0xf0;
6876 if (hd_type
!= 0xf0)
6877 BX_INFO(panic_msg_reg12h
,0);
6878 hd_type
= inb_cmos(0x19); // HD0: extended type
6880 BX_INFO(panic_msg_reg19h
,0,0x19);
6883 hd_type
= inb_cmos(0x12) & 0x0f;
6884 if (hd_type
!= 0x0f)
6885 BX_INFO(panic_msg_reg12h
,1);
6886 hd_type
= inb_cmos(0x1a); // HD1: extended type
6888 BX_INFO(panic_msg_reg19h
,0,0x1a);
6893 cylinders
= inb_cmos(iobase
) | (inb_cmos(iobase
+1) << 8);
6894 write_word(ss
, hd_cylinders
, cylinders
);
6897 write_byte(ss
, hd_heads
, inb_cmos(iobase
+2));
6899 // sectors per track
6900 write_byte(ss
, hd_sectors
, inb_cmos(iobase
+8));
6903 #endif //else BX_USE_ATADRV
6905 #if BX_SUPPORT_FLOPPY
6907 //////////////////////
6908 // FLOPPY functions //
6909 //////////////////////
6911 void floppy_reset_controller()
6917 outb(0x03f2, val8
& ~0x04);
6918 outb(0x03f2, val8
| 0x04);
6920 // Wait for controller to come out of reset
6923 } while ( (val8
& 0xc0) != 0x80 );
6926 void floppy_prepare_controller(drive
)
6929 Bit8u val8
, dor
, prev_reset
;
6931 // set 40:3e bit 7 to 0
6932 val8
= read_byte(0x0040, 0x003e);
6934 write_byte(0x0040, 0x003e, val8
);
6936 // turn on motor of selected drive, DMA & int enabled, normal operation
6937 prev_reset
= inb(0x03f2) & 0x04;
6946 // reset the disk motor timeout value of INT 08
6947 write_byte(0x40,0x40, BX_FLOPPY_ON_CNT
);
6949 // wait for drive readiness
6952 } while ( (val8
& 0xc0) != 0x80 );
6954 if (prev_reset
== 0) {
6955 // turn on interrupts
6959 // wait on 40:3e bit 7 to become 1
6961 val8
= read_byte(0x0040, 0x003e);
6962 } while ( (val8
& 0x80) == 0 );
6967 write_byte(0x0040, 0x003e, val8
);
6972 floppy_media_known(drive
)
6976 Bit16u media_state_offset
;
6978 val8
= read_byte(0x0040, 0x003e); // diskette recal status
6985 media_state_offset
= 0x0090;
6987 media_state_offset
+= 1;
6989 val8
= read_byte(0x0040, media_state_offset
);
6990 val8
= (val8
>> 4) & 0x01;
6994 // check pass, return KNOWN
6999 floppy_media_sense(drive
)
7003 Bit16u media_state_offset
;
7004 Bit8u drive_type
, config_data
, media_state
;
7006 if (floppy_drive_recal(drive
) == 0) {
7010 // for now cheat and get drive type from CMOS,
7011 // assume media is same as drive type
7013 // ** config_data **
7014 // Bitfields for diskette media control:
7015 // Bit(s) Description (Table M0028)
7016 // 7-6 last data rate set by controller
7017 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
7018 // 5-4 last diskette drive step rate selected
7019 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
7020 // 3-2 {data rate at start of operation}
7023 // ** media_state **
7024 // Bitfields for diskette drive media state:
7025 // Bit(s) Description (Table M0030)
7027 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
7028 // 5 double stepping required (e.g. 360kB in 1.2MB)
7029 // 4 media type established
7030 // 3 drive capable of supporting 4MB media
7031 // 2-0 on exit from BIOS, contains
7032 // 000 trying 360kB in 360kB
7033 // 001 trying 360kB in 1.2MB
7034 // 010 trying 1.2MB in 1.2MB
7035 // 011 360kB in 360kB established
7036 // 100 360kB in 1.2MB established
7037 // 101 1.2MB in 1.2MB established
7039 // 111 all other formats/drives
7041 drive_type
= inb_cmos(0x10);
7046 if ( drive_type
== 1 ) {
7048 config_data
= 0x00; // 0000 0000
7049 media_state
= 0x25; // 0010 0101
7052 else if ( drive_type
== 2 ) {
7053 // 1.2 MB 5.25" drive
7054 config_data
= 0x00; // 0000 0000
7055 media_state
= 0x25; // 0010 0101 // need double stepping??? (bit 5)
7058 else if ( drive_type
== 3 ) {
7060 config_data
= 0x00; // 0000 0000 ???
7061 media_state
= 0x17; // 0001 0111
7064 else if ( drive_type
== 4 ) {
7065 // 1.44 MB 3.5" drive
7066 config_data
= 0x00; // 0000 0000
7067 media_state
= 0x17; // 0001 0111
7070 else if ( drive_type
== 5 ) {
7071 // 2.88 MB 3.5" drive
7072 config_data
= 0xCC; // 1100 1100
7073 media_state
= 0xD7; // 1101 0111
7077 // Extended floppy size uses special cmos setting
7078 else if ( drive_type
== 6 ) {
7080 config_data
= 0x00; // 0000 0000
7081 media_state
= 0x27; // 0010 0111
7084 else if ( drive_type
== 7 ) {
7086 config_data
= 0x00; // 0000 0000
7087 media_state
= 0x27; // 0010 0111
7090 else if ( drive_type
== 8 ) {
7092 config_data
= 0x00; // 0000 0000
7093 media_state
= 0x27; // 0010 0111
7099 config_data
= 0x00; // 0000 0000
7100 media_state
= 0x00; // 0000 0000
7105 media_state_offset
= 0x90;
7107 media_state_offset
= 0x91;
7108 write_byte(0x0040, 0x008B, config_data
);
7109 write_byte(0x0040, media_state_offset
, media_state
);
7115 floppy_drive_recal(drive
)
7119 Bit16u curr_cyl_offset
;
7121 floppy_prepare_controller(drive
);
7123 // send Recalibrate command (2 bytes) to controller
7124 outb(0x03f5, 0x07); // 07: Recalibrate
7125 outb(0x03f5, drive
); // 0=drive0, 1=drive1
7127 // turn on interrupts
7132 // wait on 40:3e bit 7 to become 1
7134 val8
= (read_byte(0x0040, 0x003e) & 0x80);
7135 } while ( val8
== 0 );
7137 val8
= 0; // separate asm from while() loop
7138 // turn off interrupts
7143 // set 40:3e bit 7 to 0, and calibrated bit
7144 val8
= read_byte(0x0040, 0x003e);
7147 val8
|= 0x02; // Drive 1 calibrated
7148 curr_cyl_offset
= 0x0095;
7150 val8
|= 0x01; // Drive 0 calibrated
7151 curr_cyl_offset
= 0x0094;
7153 write_byte(0x0040, 0x003e, val8
);
7154 write_byte(0x0040, curr_cyl_offset
, 0); // current cylinder is 0
7162 floppy_drive_exists(drive
)
7167 // check CMOS to see if drive exists
7168 drive_type
= inb_cmos(0x10);
7173 if ( drive_type
== 0 )
7180 int13_diskette_function(DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
7181 Bit16u DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
7183 Bit8u drive
, num_sectors
, track
, sector
, head
, status
;
7184 Bit16u base_address
, base_count
, base_es
;
7185 Bit8u page
, mode_register
, val8
, dor
;
7186 Bit8u return_status
[7];
7187 Bit8u drive_type
, num_floppies
, ah
;
7188 Bit16u es
, last_addr
;
7190 BX_DEBUG_INT13_FL("int13_diskette: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", AX
, BX
, CX
, DX
, ES
);
7195 case 0x00: // diskette controller reset
7196 BX_DEBUG_INT13_FL("floppy f00\n");
7199 SET_AH(1); // invalid param
7200 set_diskette_ret_status(1);
7204 drive_type
= inb_cmos(0x10);
7210 if (drive_type
== 0) {
7211 SET_AH(0x80); // drive not responding
7212 set_diskette_ret_status(0x80);
7217 set_diskette_ret_status(0);
7218 CLEAR_CF(); // successful
7219 set_diskette_current_cyl(drive
, 0); // current cylinder
7222 case 0x01: // Read Diskette Status
7224 val8
= read_byte(0x0000, 0x0441);
7231 case 0x02: // Read Diskette Sectors
7232 case 0x03: // Write Diskette Sectors
7233 case 0x04: // Verify Diskette Sectors
7234 num_sectors
= GET_AL();
7240 if ((drive
> 1) || (head
> 1) || (sector
== 0) ||
7241 (num_sectors
== 0) || (num_sectors
> 72)) {
7242 BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
7244 set_diskette_ret_status(1);
7245 SET_AL(0); // no sectors read
7246 SET_CF(); // error occurred
7250 // see if drive exists
7251 if (floppy_drive_exists(drive
) == 0) {
7252 SET_AH(0x80); // not responding
7253 set_diskette_ret_status(0x80);
7254 SET_AL(0); // no sectors read
7255 SET_CF(); // error occurred
7259 // see if media in drive, and type is known
7260 if (floppy_media_known(drive
) == 0) {
7261 if (floppy_media_sense(drive
) == 0) {
7262 SET_AH(0x0C); // Media type not found
7263 set_diskette_ret_status(0x0C);
7264 SET_AL(0); // no sectors read
7265 SET_CF(); // error occurred
7271 // Read Diskette Sectors
7273 //-----------------------------------
7274 // set up DMA controller for transfer
7275 //-----------------------------------
7277 // es:bx = pointer to where to place information from diskette
7278 // port 04: DMA-1 base and current address, channel 2
7279 // port 05: DMA-1 base and current count, channel 2
7280 page
= (ES
>> 12); // upper 4 bits
7281 base_es
= (ES
<< 4); // lower 16bits contributed by ES
7282 base_address
= base_es
+ BX
; // lower 16 bits of address
7283 // contributed by ES:BX
7284 if ( base_address
< base_es
) {
7285 // in case of carry, adjust page by 1
7288 base_count
= (num_sectors
* 512) - 1;
7290 // check for 64K boundary overrun
7291 last_addr
= base_address
+ base_count
;
7292 if (last_addr
< base_address
) {
7294 set_diskette_ret_status(0x09);
7295 SET_AL(0); // no sectors read
7296 SET_CF(); // error occurred
7300 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
7303 BX_DEBUG_INT13_FL("clear flip-flop\n");
7304 outb(0x000c, 0x00); // clear flip-flop
7305 outb(0x0004, base_address
);
7306 outb(0x0004, base_address
>>8);
7307 BX_DEBUG_INT13_FL("clear flip-flop\n");
7308 outb(0x000c, 0x00); // clear flip-flop
7309 outb(0x0005, base_count
);
7310 outb(0x0005, base_count
>>8);
7312 // port 0b: DMA-1 Mode Register
7313 mode_register
= 0x46; // single mode, increment, autoinit disable,
7314 // transfer type=write, channel 2
7315 BX_DEBUG_INT13_FL("setting mode register\n");
7316 outb(0x000b, mode_register
);
7318 BX_DEBUG_INT13_FL("setting page register\n");
7319 // port 81: DMA-1 Page Register, channel 2
7322 BX_DEBUG_INT13_FL("unmask chan 2\n");
7323 outb(0x000a, 0x02); // unmask channel 2
7325 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
7328 //--------------------------------------
7329 // set up floppy controller for transfer
7330 //--------------------------------------
7331 floppy_prepare_controller(drive
);
7333 // send read-normal-data command (9 bytes) to controller
7334 outb(0x03f5, 0xe6); // e6: read normal data
7335 outb(0x03f5, (head
<< 2) | drive
); // HD DR1 DR2
7336 outb(0x03f5, track
);
7338 outb(0x03f5, sector
);
7339 outb(0x03f5, 2); // 512 byte sector size
7340 outb(0x03f5, sector
+ num_sectors
- 1); // last sector to read on track
7341 outb(0x03f5, 0); // Gap length
7342 outb(0x03f5, 0xff); // Gap length
7344 // turn on interrupts
7349 // wait on 40:3e bit 7 to become 1
7351 val8
= read_byte(0x0040, 0x0040);
7353 floppy_reset_controller();
7354 SET_AH(0x80); // drive not ready (timeout)
7355 set_diskette_ret_status(0x80);
7356 SET_AL(0); // no sectors read
7357 SET_CF(); // error occurred
7360 val8
= (read_byte(0x0040, 0x003e) & 0x80);
7361 } while ( val8
== 0 );
7363 val8
= 0; // separate asm from while() loop
7364 // turn off interrupts
7369 // set 40:3e bit 7 to 0
7370 val8
= read_byte(0x0040, 0x003e);
7372 write_byte(0x0040, 0x003e, val8
);
7374 // check port 3f4 for accessibility to status bytes
7376 if ( (val8
& 0xc0) != 0xc0 )
7377 BX_PANIC("int13_diskette: ctrl not ready\n");
7379 // read 7 return status bytes from controller
7380 // using loop index broken, have to unroll...
7381 return_status
[0] = inb(0x3f5);
7382 return_status
[1] = inb(0x3f5);
7383 return_status
[2] = inb(0x3f5);
7384 return_status
[3] = inb(0x3f5);
7385 return_status
[4] = inb(0x3f5);
7386 return_status
[5] = inb(0x3f5);
7387 return_status
[6] = inb(0x3f5);
7388 // record in BIOS Data Area
7389 write_byte(0x0040, 0x0042, return_status
[0]);
7390 write_byte(0x0040, 0x0043, return_status
[1]);
7391 write_byte(0x0040, 0x0044, return_status
[2]);
7392 write_byte(0x0040, 0x0045, return_status
[3]);
7393 write_byte(0x0040, 0x0046, return_status
[4]);
7394 write_byte(0x0040, 0x0047, return_status
[5]);
7395 write_byte(0x0040, 0x0048, return_status
[6]);
7397 if ( (return_status
[0] & 0xc0) != 0 ) {
7399 set_diskette_ret_status(0x20);
7400 SET_AL(0); // no sectors read
7401 SET_CF(); // error occurred
7405 // ??? should track be new val from return_status[3] ?
7406 set_diskette_current_cyl(drive
, track
);
7407 // AL = number of sectors read (same value as passed)
7408 SET_AH(0x00); // success
7409 CLEAR_CF(); // success
7411 } else if (ah
== 0x03) {
7412 // Write Diskette Sectors
7414 //-----------------------------------
7415 // set up DMA controller for transfer
7416 //-----------------------------------
7418 // es:bx = pointer to where to place information from diskette
7419 // port 04: DMA-1 base and current address, channel 2
7420 // port 05: DMA-1 base and current count, channel 2
7421 page
= (ES
>> 12); // upper 4 bits
7422 base_es
= (ES
<< 4); // lower 16bits contributed by ES
7423 base_address
= base_es
+ BX
; // lower 16 bits of address
7424 // contributed by ES:BX
7425 if ( base_address
< base_es
) {
7426 // in case of carry, adjust page by 1
7429 base_count
= (num_sectors
* 512) - 1;
7431 // check for 64K boundary overrun
7432 last_addr
= base_address
+ base_count
;
7433 if (last_addr
< base_address
) {
7435 set_diskette_ret_status(0x09);
7436 SET_AL(0); // no sectors read
7437 SET_CF(); // error occurred
7441 BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
7444 outb(0x000c, 0x00); // clear flip-flop
7445 outb(0x0004, base_address
);
7446 outb(0x0004, base_address
>>8);
7447 outb(0x000c, 0x00); // clear flip-flop
7448 outb(0x0005, base_count
);
7449 outb(0x0005, base_count
>>8);
7451 // port 0b: DMA-1 Mode Register
7452 mode_register
= 0x4a; // single mode, increment, autoinit disable,
7453 // transfer type=read, channel 2
7454 outb(0x000b, mode_register
);
7456 // port 81: DMA-1 Page Register, channel 2
7459 BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
7462 //--------------------------------------
7463 // set up floppy controller for transfer
7464 //--------------------------------------
7465 floppy_prepare_controller(drive
);
7467 // send write-normal-data command (9 bytes) to controller
7468 outb(0x03f5, 0xc5); // c5: write normal data
7469 outb(0x03f5, (head
<< 2) | drive
); // HD DR1 DR2
7470 outb(0x03f5, track
);
7472 outb(0x03f5, sector
);
7473 outb(0x03f5, 2); // 512 byte sector size
7474 outb(0x03f5, sector
+ num_sectors
- 1); // last sector to write on track
7475 outb(0x03f5, 0); // Gap length
7476 outb(0x03f5, 0xff); // Gap length
7478 // turn on interrupts
7483 // wait on 40:3e bit 7 to become 1
7485 val8
= read_byte(0x0040, 0x0040);
7487 floppy_reset_controller();
7488 SET_AH(0x80); // drive not ready (timeout)
7489 set_diskette_ret_status(0x80);
7490 SET_AL(0); // no sectors written
7491 SET_CF(); // error occurred
7494 val8
= (read_byte(0x0040, 0x003e) & 0x80);
7495 } while ( val8
== 0 );
7497 val8
= 0; // separate asm from while() loop
7498 // turn off interrupts
7503 // set 40:3e bit 7 to 0
7504 val8
= read_byte(0x0040, 0x003e);
7506 write_byte(0x0040, 0x003e, val8
);
7508 // check port 3f4 for accessibility to status bytes
7510 if ( (val8
& 0xc0) != 0xc0 )
7511 BX_PANIC("int13_diskette: ctrl not ready\n");
7513 // read 7 return status bytes from controller
7514 // using loop index broken, have to unroll...
7515 return_status
[0] = inb(0x3f5);
7516 return_status
[1] = inb(0x3f5);
7517 return_status
[2] = inb(0x3f5);
7518 return_status
[3] = inb(0x3f5);
7519 return_status
[4] = inb(0x3f5);
7520 return_status
[5] = inb(0x3f5);
7521 return_status
[6] = inb(0x3f5);
7522 // record in BIOS Data Area
7523 write_byte(0x0040, 0x0042, return_status
[0]);
7524 write_byte(0x0040, 0x0043, return_status
[1]);
7525 write_byte(0x0040, 0x0044, return_status
[2]);
7526 write_byte(0x0040, 0x0045, return_status
[3]);
7527 write_byte(0x0040, 0x0046, return_status
[4]);
7528 write_byte(0x0040, 0x0047, return_status
[5]);
7529 write_byte(0x0040, 0x0048, return_status
[6]);
7531 if ( (return_status
[0] & 0xc0) != 0 ) {
7532 if ( (return_status
[1] & 0x02) != 0 ) {
7533 // diskette not writable.
7534 // AH=status code=0x03 (tried to write on write-protected disk)
7535 // AL=number of sectors written=0
7540 BX_PANIC("int13_diskette_function: read error\n");
7544 // ??? should track be new val from return_status[3] ?
7545 set_diskette_current_cyl(drive
, track
);
7546 // AL = number of sectors read (same value as passed)
7547 SET_AH(0x00); // success
7548 CLEAR_CF(); // success
7550 } else { // if (ah == 0x04)
7551 // Verify Diskette Sectors
7553 // ??? should track be new val from return_status[3] ?
7554 set_diskette_current_cyl(drive
, track
);
7555 // AL = number of sectors verified (same value as passed)
7556 CLEAR_CF(); // success
7557 SET_AH(0x00); // success
7562 case 0x05: // format diskette track
7563 BX_DEBUG_INT13_FL("floppy f05\n");
7565 num_sectors
= GET_AL();
7570 if ((drive
> 1) || (head
> 1) || (track
> 79) ||
7571 (num_sectors
== 0) || (num_sectors
> 18)) {
7573 set_diskette_ret_status(1);
7574 SET_CF(); // error occurred
7577 // see if drive exists
7578 if (floppy_drive_exists(drive
) == 0) {
7579 SET_AH(0x80); // drive not responding
7580 set_diskette_ret_status(0x80);
7581 SET_CF(); // error occurred
7585 // see if media in drive, and type is known
7586 if (floppy_media_known(drive
) == 0) {
7587 if (floppy_media_sense(drive
) == 0) {
7588 SET_AH(0x0C); // Media type not found
7589 set_diskette_ret_status(0x0C);
7590 SET_AL(0); // no sectors read
7591 SET_CF(); // error occurred
7596 // set up DMA controller for transfer
7597 page
= (ES
>> 12); // upper 4 bits
7598 base_es
= (ES
<< 4); // lower 16bits contributed by ES
7599 base_address
= base_es
+ BX
; // lower 16 bits of address
7600 // contributed by ES:BX
7601 if ( base_address
< base_es
) {
7602 // in case of carry, adjust page by 1
7605 base_count
= (num_sectors
* 4) - 1;
7607 // check for 64K boundary overrun
7608 last_addr
= base_address
+ base_count
;
7609 if (last_addr
< base_address
) {
7611 set_diskette_ret_status(0x09);
7612 SET_AL(0); // no sectors read
7613 SET_CF(); // error occurred
7618 outb(0x000c, 0x00); // clear flip-flop
7619 outb(0x0004, base_address
);
7620 outb(0x0004, base_address
>>8);
7621 outb(0x000c, 0x00); // clear flip-flop
7622 outb(0x0005, base_count
);
7623 outb(0x0005, base_count
>>8);
7624 mode_register
= 0x4a; // single mode, increment, autoinit disable,
7625 // transfer type=read, channel 2
7626 outb(0x000b, mode_register
);
7627 // port 81: DMA-1 Page Register, channel 2
7631 // set up floppy controller for transfer
7632 floppy_prepare_controller(drive
);
7634 // send format-track command (6 bytes) to controller
7635 outb(0x03f5, 0x4d); // 4d: format track
7636 outb(0x03f5, (head
<< 2) | drive
); // HD DR1 DR2
7637 outb(0x03f5, 2); // 512 byte sector size
7638 outb(0x03f5, num_sectors
); // number of sectors per track
7639 outb(0x03f5, 0); // Gap length
7640 outb(0x03f5, 0xf6); // Fill byte
7641 // turn on interrupts
7646 // wait on 40:3e bit 7 to become 1
7648 val8
= read_byte(0x0040, 0x0040);
7650 floppy_reset_controller();
7651 SET_AH(0x80); // drive not ready (timeout)
7652 set_diskette_ret_status(0x80);
7653 SET_CF(); // error occurred
7656 val8
= (read_byte(0x0040, 0x003e) & 0x80);
7657 } while ( val8
== 0 );
7659 val8
= 0; // separate asm from while() loop
7660 // turn off interrupts
7664 // set 40:3e bit 7 to 0
7665 val8
= read_byte(0x0040, 0x003e);
7667 write_byte(0x0040, 0x003e, val8
);
7668 // check port 3f4 for accessibility to status bytes
7670 if ( (val8
& 0xc0) != 0xc0 )
7671 BX_PANIC("int13_diskette: ctrl not ready\n");
7673 // read 7 return status bytes from controller
7674 // using loop index broken, have to unroll...
7675 return_status
[0] = inb(0x3f5);
7676 return_status
[1] = inb(0x3f5);
7677 return_status
[2] = inb(0x3f5);
7678 return_status
[3] = inb(0x3f5);
7679 return_status
[4] = inb(0x3f5);
7680 return_status
[5] = inb(0x3f5);
7681 return_status
[6] = inb(0x3f5);
7682 // record in BIOS Data Area
7683 write_byte(0x0040, 0x0042, return_status
[0]);
7684 write_byte(0x0040, 0x0043, return_status
[1]);
7685 write_byte(0x0040, 0x0044, return_status
[2]);
7686 write_byte(0x0040, 0x0045, return_status
[3]);
7687 write_byte(0x0040, 0x0046, return_status
[4]);
7688 write_byte(0x0040, 0x0047, return_status
[5]);
7689 write_byte(0x0040, 0x0048, return_status
[6]);
7691 if ( (return_status
[0] & 0xc0) != 0 ) {
7692 if ( (return_status
[1] & 0x02) != 0 ) {
7693 // diskette not writable.
7694 // AH=status code=0x03 (tried to write on write-protected disk)
7695 // AL=number of sectors written=0
7700 BX_PANIC("int13_diskette_function: write error\n");
7705 set_diskette_ret_status(0);
7706 set_diskette_current_cyl(drive
, 0);
7707 CLEAR_CF(); // successful
7711 case 0x08: // read diskette drive parameters
7712 BX_DEBUG_INT13_FL("floppy f08\n");
7722 SET_DL(num_floppies
);
7727 drive_type
= inb_cmos(0x10);
7729 if (drive_type
& 0xf0)
7731 if (drive_type
& 0x0f)
7743 SET_DL(num_floppies
);
7745 switch (drive_type
) {
7748 SET_DH(0); // max head #
7751 case 1: // 360KB, 5.25"
7752 CX
= 0x2709; // 40 tracks, 9 sectors
7753 SET_DH(1); // max head #
7756 case 2: // 1.2MB, 5.25"
7757 CX
= 0x4f0f; // 80 tracks, 15 sectors
7758 SET_DH(1); // max head #
7761 case 3: // 720KB, 3.5"
7762 CX
= 0x4f09; // 80 tracks, 9 sectors
7763 SET_DH(1); // max head #
7766 case 4: // 1.44MB, 3.5"
7767 CX
= 0x4f12; // 80 tracks, 18 sectors
7768 SET_DH(1); // max head #
7771 case 5: // 2.88MB, 3.5"
7772 CX
= 0x4f24; // 80 tracks, 36 sectors
7773 SET_DH(1); // max head #
7776 case 6: // 160k, 5.25"
7777 CX
= 0x2708; // 40 tracks, 8 sectors
7778 SET_DH(0); // max head #
7781 case 7: // 180k, 5.25"
7782 CX
= 0x2709; // 40 tracks, 9 sectors
7783 SET_DH(0); // max head #
7786 case 8: // 320k, 5.25"
7787 CX
= 0x2708; // 40 tracks, 8 sectors
7788 SET_DH(1); // max head #
7792 BX_PANIC("floppy: int13: bad floppy type\n");
7795 /* set es & di to point to 11 byte diskette param table in ROM */
7799 mov ax
, #diskette_param_table2
7800 mov _int13_diskette_function
.DI
+2[bp
], ax
7801 mov _int13_diskette_function
.ES
+2[bp
], cs
7804 CLEAR_CF(); // success
7805 /* disk status not changed upon success */
7809 case 0x15: // read diskette drive type
7810 BX_DEBUG_INT13_FL("floppy f15\n");
7813 SET_AH(0); // only 2 drives supported
7814 // set_diskette_ret_status here ???
7818 drive_type
= inb_cmos(0x10);
7824 CLEAR_CF(); // successful, not present
7825 if (drive_type
==0) {
7826 SET_AH(0); // drive not present
7829 SET_AH(1); // drive present, does not support change line
7834 case 0x16: // get diskette change line status
7835 BX_DEBUG_INT13_FL("floppy f16\n");
7838 SET_AH(0x01); // invalid drive
7839 set_diskette_ret_status(0x01);
7844 SET_AH(0x06); // change line not supported
7845 set_diskette_ret_status(0x06);
7849 case 0x17: // set diskette type for format(old)
7850 BX_DEBUG_INT13_FL("floppy f17\n");
7851 /* not used for 1.44M floppies */
7852 SET_AH(0x01); // not supported
7853 set_diskette_ret_status(1); /* not supported */
7857 case 0x18: // set diskette type for format(new)
7858 BX_DEBUG_INT13_FL("floppy f18\n");
7859 SET_AH(0x01); // do later
7860 set_diskette_ret_status(1);
7865 BX_INFO("int13_diskette: unsupported AH=%02x\n", GET_AH());
7867 // if ( (ah==0x20) || ((ah>=0x41) && (ah<=0x49)) || (ah==0x4e) ) {
7868 SET_AH(0x01); // ???
7869 set_diskette_ret_status(1);
7875 #else // #if BX_SUPPORT_FLOPPY
7877 int13_diskette_function(DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
)
7878 Bit16u DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
;
7882 switch ( GET_AH() ) {
7884 case 0x01: // Read Diskette Status
7886 val8
= read_byte(0x0000, 0x0441);
7895 write_byte(0x0000, 0x0441, 0x01);
7899 #endif // #if BX_SUPPORT_FLOPPY
7902 set_diskette_ret_status(value
)
7905 write_byte(0x0040, 0x0041, value
);
7909 set_diskette_current_cyl(drive
, cyl
)
7914 BX_PANIC("set_diskette_current_cyl(): drive > 1\n");
7915 write_byte(0x0040, 0x0094+drive
, cyl
);
7919 determine_floppy_media(drive
)
7923 Bit8u val8
, DOR
, ctrl_info
;
7925 ctrl_info
= read_byte(0x0040, 0x008F);
7933 DOR
= 0x1c; // DOR: drive0 motor on, DMA&int enabled, normal op, drive select 0
7936 DOR
= 0x2d; // DOR: drive1 motor on, DMA&int enabled, normal op, drive select 1
7940 if ( (ctrl_info
& 0x04) != 0x04 ) {
7941 // Drive not determined means no drive exists, done.
7946 // check Main Status Register for readiness
7947 val8
= inb(0x03f4) & 0x80; // Main Status Register
7949 BX_PANIC("d_f_m: MRQ bit not set\n");
7953 // existing BDA values
7955 // turn on drive motor
7956 outb(0x03f2, DOR
); // Digital Output Register
7959 BX_PANIC("d_f_m: OK so far\n");
7964 int17_function(regs
, ds
, iret_addr
)
7965 pusha_regs_t regs
; // regs pushed from PUSHA instruction
7966 Bit16u ds
; // previous DS:, DS set to 0x0000 by asm wrapper
7967 iret_addr_t iret_addr
; // CS,IP,Flags pushed from original INT call
7969 Bit16u addr
,timeout
;
7976 addr
= read_word(0x0040, (regs
.u
.r16
.dx
<< 1) + 8);
7977 if ((regs
.u
.r8
.ah
< 3) && (regs
.u
.r16
.dx
< 3) && (addr
> 0)) {
7978 timeout
= read_byte(0x0040, 0x0078 + regs
.u
.r16
.dx
) << 8;
7979 if (regs
.u
.r8
.ah
== 0) {
7980 outb(addr
, regs
.u
.r8
.al
);
7982 outb(addr
+2, val8
| 0x01); // send strobe
7986 outb(addr
+2, val8
& ~0x01);
7987 while (((inb(addr
+1) & 0x40) == 0x40) && (timeout
)) {
7991 if (regs
.u
.r8
.ah
== 1) {
7993 outb(addr
+2, val8
& ~0x04); // send init
7997 outb(addr
+2, val8
| 0x04);
8000 regs
.u
.r8
.ah
= (val8
^ 0x48);
8001 if (!timeout
) regs
.u
.r8
.ah
|= 0x01;
8002 ClearCF(iret_addr
.flags
);
8004 SetCF(iret_addr
.flags
); // Unsupported
8009 int19_function(seq_nr
)
8012 Bit16u ebda_seg
=read_word(0x0040,0x000E);
8023 // if BX_ELTORITO_BOOT is not defined, old behavior
8024 // check bit 5 in CMOS reg 0x2d. load either 0x00 or 0x80 into DL
8025 // in preparation for the intial INT 13h (0=floppy A:, 0x80=C:)
8026 // 0: system boot sequence, first drive C: then A:
8027 // 1: system boot sequence, first drive A: then C:
8028 // else BX_ELTORITO_BOOT is defined
8029 // CMOS regs 0x3D and 0x38 contain the boot sequence:
8030 // CMOS reg 0x3D & 0x0f : 1st boot device
8031 // CMOS reg 0x3D & 0xf0 : 2nd boot device
8032 // CMOS reg 0x38 & 0xf0 : 3rd boot device
8033 // boot device codes:
8034 // 0x00 : not defined
8035 // 0x01 : first floppy
8036 // 0x02 : first harddrive
8037 // 0x03 : first cdrom
8038 // 0x04 - 0x0f : PnP expansion ROMs (e.g. Etherboot)
8039 // else : boot failure
8041 // Get the boot sequence
8042 #if BX_ELTORITO_BOOT
8043 bootdev
= inb_cmos(0x3d);
8044 bootdev
|= ((inb_cmos(0x38) & 0xf0) << 4);
8045 bootdev
>>= 4 * seq_nr
;
8048 /* Read user selected device */
8049 bootfirst
= read_word(IPL_SEG
, IPL_BOOTFIRST_OFFSET
);
8050 if (bootfirst
!= 0xFFFF) {
8051 bootdev
= bootfirst
;
8052 /* User selected device not set */
8053 write_word(IPL_SEG
, IPL_BOOTFIRST_OFFSET
, 0xFFFF);
8054 /* Reset boot sequence */
8055 write_word(IPL_SEG
, IPL_SEQUENCE_OFFSET
, 0xFFFF);
8056 } else if (bootdev
== 0) BX_PANIC("No bootable device.\n");
8058 /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
8061 if (seq_nr
==2) BX_PANIC("No more boot devices.");
8062 if (!!(inb_cmos(0x2d) & 0x20) ^ (seq_nr
== 1))
8063 /* Boot from floppy if the bit is set or it's the second boot */
8069 /* Read the boot device from the IPL table */
8070 if (get_boot_vector(bootdev
, &e
) == 0) {
8071 BX_INFO("Invalid boot device (0x%x)\n", bootdev
);
8075 /* Do the loading, and set up vector as a far pointer to the boot
8076 * address, and bootdrv as the boot drive */
8077 print_boot_device(&e
);
8080 case IPL_TYPE_FLOPPY
: /* FDD */
8081 case IPL_TYPE_HARDDISK
: /* HDD */
8083 bootdrv
= (e
.type
== IPL_TYPE_HARDDISK
) ? 0x80 : 0x00;
8095 mov dl
, _int19_function
.bootdrv
+ 2[bp
]
8096 mov ax
, _int19_function
.bootseg
+ 2[bp
]
8097 mov es
, ax
;; segment
8098 xor bx
, bx
;; offset
8099 mov ah
, #0x02 ;; function 2, read diskette sector
8100 mov al
, #0x01 ;; read 1 sector
8101 mov ch
, #0x00 ;; track 0
8102 mov cl
, #0x01 ;; sector 1
8103 mov dh
, #0x00 ;; head 0
8104 int #0x13 ;; read sector
8107 mov _int19_function
.status
+ 2[bp
], ax
8118 print_boot_failure(e
.type
, 1);
8122 /* Always check the signature on a HDD boot sector; on FDD, only do
8123 * the check if the CMOS doesn't tell us to skip it */
8124 if ((e
.type
!= IPL_TYPE_FLOPPY
) || !((inb_cmos(0x38) & 0x01))) {
8125 if (read_word(bootseg
,0x1fe) != 0xaa55) {
8126 print_boot_failure(e
.type
, 0);
8131 /* Canonicalize bootseg:bootip */
8132 bootip
= (bootseg
& 0x0fff) << 4;
8136 #if BX_ELTORITO_BOOT
8137 case IPL_TYPE_CDROM
: /* CD-ROM */
8138 status
= cdrom_boot();
8141 if ( (status
& 0x00ff) !=0 ) {
8142 print_cdromboot_failure(status
);
8143 print_boot_failure(e
.type
, 1);
8147 bootdrv
= (Bit8u
)(status
>>8);
8148 bootseg
= read_word(ebda_seg
,&EbdaData
->cdemu
.load_segment
);
8153 case IPL_TYPE_BEV
: /* Expansion ROM with a Bootstrap Entry Vector (a far pointer) */
8154 bootseg
= e
.vector
>> 16;
8155 bootip
= e
.vector
& 0xffff;
8161 /* Debugging info */
8162 BX_INFO("Booting from %x:%x\n", bootseg
, bootip
);
8164 /* Jump to the boot vector */
8169 ;; Build an iret stack frame that will take us to the boot vector
.
8170 ;; iret pops ip
, then cs
, then flags
, so push them in the opposite order
.
8172 mov ax
, _int19_function
.bootseg
+ 0[bp
]
8174 mov ax
, _int19_function
.bootip
+ 0[bp
]
8176 ;; Set the magic number in ax
and the boot drive in dl
.
8178 mov dl
, _int19_function
.bootdrv
+ 0[bp
]
8179 ;; Zero some of the other registers
.
8190 int1a_function(regs
, ds
, iret_addr
)
8191 pusha_regs_t regs
; // regs pushed from PUSHA instruction
8192 Bit16u ds
; // previous DS:, DS set to 0x0000 by asm wrapper
8193 iret_addr_t iret_addr
; // CS,IP,Flags pushed from original INT call
8197 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
);
8203 switch (regs
.u
.r8
.ah
) {
8204 case 0: // get current clock count
8208 regs
.u
.r16
.cx
= BiosData
->ticks_high
;
8209 regs
.u
.r16
.dx
= BiosData
->ticks_low
;
8210 regs
.u
.r8
.al
= BiosData
->midnight_flag
;
8211 BiosData
->midnight_flag
= 0; // reset flag
8216 ClearCF(iret_addr
.flags
); // OK
8219 case 1: // Set Current Clock Count
8223 BiosData
->ticks_high
= regs
.u
.r16
.cx
;
8224 BiosData
->ticks_low
= regs
.u
.r16
.dx
;
8225 BiosData
->midnight_flag
= 0; // reset flag
8230 ClearCF(iret_addr
.flags
); // OK
8234 case 2: // Read CMOS Time
8235 if (rtc_updating()) {
8236 SetCF(iret_addr
.flags
);
8240 regs
.u
.r8
.dh
= inb_cmos(0x00); // Seconds
8241 regs
.u
.r8
.cl
= inb_cmos(0x02); // Minutes
8242 regs
.u
.r8
.ch
= inb_cmos(0x04); // Hours
8243 regs
.u
.r8
.dl
= inb_cmos(0x0b) & 0x01; // Stat Reg B
8245 regs
.u
.r8
.al
= regs
.u
.r8
.ch
;
8246 ClearCF(iret_addr
.flags
); // OK
8249 case 3: // Set CMOS Time
8250 // Using a debugger, I notice the following masking/setting
8251 // of bits in Status Register B, by setting Reg B to
8252 // a few values and getting its value after INT 1A was called.
8254 // try#1 try#2 try#3
8255 // before 1111 1101 0111 1101 0000 0000
8256 // after 0110 0010 0110 0010 0000 0010
8258 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8259 // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
8260 if (rtc_updating()) {
8262 // fall through as if an update were not in progress
8264 outb_cmos(0x00, regs
.u
.r8
.dh
); // Seconds
8265 outb_cmos(0x02, regs
.u
.r8
.cl
); // Minutes
8266 outb_cmos(0x04, regs
.u
.r8
.ch
); // Hours
8267 // Set Daylight Savings time enabled bit to requested value
8268 val8
= (inb_cmos(0x0b) & 0x60) | 0x02 | (regs
.u
.r8
.dl
& 0x01);
8269 // (reg B already selected)
8270 outb_cmos(0x0b, val8
);
8272 regs
.u
.r8
.al
= val8
; // val last written to Reg B
8273 ClearCF(iret_addr
.flags
); // OK
8276 case 4: // Read CMOS Date
8278 if (rtc_updating()) {
8279 SetCF(iret_addr
.flags
);
8282 regs
.u
.r8
.cl
= inb_cmos(0x09); // Year
8283 regs
.u
.r8
.dh
= inb_cmos(0x08); // Month
8284 regs
.u
.r8
.dl
= inb_cmos(0x07); // Day of Month
8285 regs
.u
.r8
.ch
= inb_cmos(0x32); // Century
8286 regs
.u
.r8
.al
= regs
.u
.r8
.ch
;
8287 ClearCF(iret_addr
.flags
); // OK
8290 case 5: // Set CMOS Date
8291 // Using a debugger, I notice the following masking/setting
8292 // of bits in Status Register B, by setting Reg B to
8293 // a few values and getting its value after INT 1A was called.
8295 // try#1 try#2 try#3 try#4
8296 // before 1111 1101 0111 1101 0000 0010 0000 0000
8297 // after 0110 1101 0111 1101 0000 0010 0000 0000
8299 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8300 // My assumption: RegB = (RegB & 01111111b)
8301 if (rtc_updating()) {
8303 SetCF(iret_addr
.flags
);
8306 outb_cmos(0x09, regs
.u
.r8
.cl
); // Year
8307 outb_cmos(0x08, regs
.u
.r8
.dh
); // Month
8308 outb_cmos(0x07, regs
.u
.r8
.dl
); // Day of Month
8309 outb_cmos(0x32, regs
.u
.r8
.ch
); // Century
8310 val8
= inb_cmos(0x0b) & 0x7f; // clear halt-clock bit
8311 outb_cmos(0x0b, val8
);
8313 regs
.u
.r8
.al
= val8
; // AL = val last written to Reg B
8314 ClearCF(iret_addr
.flags
); // OK
8317 case 6: // Set Alarm Time in CMOS
8318 // Using a debugger, I notice the following masking/setting
8319 // of bits in Status Register B, by setting Reg B to
8320 // a few values and getting its value after INT 1A was called.
8322 // try#1 try#2 try#3
8323 // before 1101 1111 0101 1111 0000 0000
8324 // after 0110 1111 0111 1111 0010 0000
8326 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8327 // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
8328 val8
= inb_cmos(0x0b); // Get Status Reg B
8331 // Alarm interrupt enabled already
8332 SetCF(iret_addr
.flags
); // Error: alarm in use
8335 if (rtc_updating()) {
8337 // fall through as if an update were not in progress
8339 outb_cmos(0x01, regs
.u
.r8
.dh
); // Seconds alarm
8340 outb_cmos(0x03, regs
.u
.r8
.cl
); // Minutes alarm
8341 outb_cmos(0x05, regs
.u
.r8
.ch
); // Hours alarm
8342 outb(0xa1, inb(0xa1) & 0xfe); // enable IRQ 8
8343 // enable Status Reg B alarm bit, clear halt clock bit
8344 outb_cmos(0x0b, (val8
& 0x7f) | 0x20);
8345 ClearCF(iret_addr
.flags
); // OK
8348 case 7: // Turn off Alarm
8349 // Using a debugger, I notice the following masking/setting
8350 // of bits in Status Register B, by setting Reg B to
8351 // a few values and getting its value after INT 1A was called.
8353 // try#1 try#2 try#3 try#4
8354 // before 1111 1101 0111 1101 0010 0000 0010 0010
8355 // after 0100 0101 0101 0101 0000 0000 0000 0010
8357 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
8358 // My assumption: RegB = (RegB & 01010111b)
8359 val8
= inb_cmos(0x0b); // Get Status Reg B
8360 // clear clock-halt bit, disable alarm bit
8361 outb_cmos(0x0b, val8
& 0x57); // disable alarm bit
8363 regs
.u
.r8
.al
= val8
; // val last written to Reg B
8364 ClearCF(iret_addr
.flags
); // OK
8368 // real mode PCI BIOS functions now handled in assembler code
8369 // this C code handles the error code for information only
8370 if (regs
.u
.r8
.bl
== 0xff) {
8371 BX_INFO("PCI BIOS: PCI not present\n");
8372 } else if (regs
.u
.r8
.bl
== 0x81) {
8373 BX_INFO("unsupported PCI BIOS function 0x%02x\n", regs
.u
.r8
.al
);
8374 } else if (regs
.u
.r8
.bl
== 0x83) {
8375 BX_INFO("bad PCI vendor ID %04x\n", regs
.u
.r16
.dx
);
8376 } else if (regs
.u
.r8
.bl
== 0x86) {
8377 if (regs
.u
.r8
.al
== 0x02) {
8378 BX_INFO("PCI device %04x:%04x not found at index %d\n", regs
.u
.r16
.dx
, regs
.u
.r16
.cx
, regs
.u
.r16
.si
);
8380 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
);
8383 regs
.u
.r8
.ah
= regs
.u
.r8
.bl
;
8384 SetCF(iret_addr
.flags
);
8389 SetCF(iret_addr
.flags
); // Unsupported
8394 int70_function(regs
, ds
, iret_addr
)
8395 pusha_regs_t regs
; // regs pushed from PUSHA instruction
8396 Bit16u ds
; // previous DS:, DS set to 0x0000 by asm wrapper
8397 iret_addr_t iret_addr
; // CS,IP,Flags pushed from original INT call
8399 // INT 70h: IRQ 8 - CMOS RTC interrupt from periodic or alarm modes
8400 Bit8u registerB
= 0, registerC
= 0;
8402 // Check which modes are enabled and have occurred.
8403 registerB
= inb_cmos( 0xB );
8404 registerC
= inb_cmos( 0xC );
8406 if( ( registerB
& 0x60 ) != 0 ) {
8407 if( ( registerC
& 0x20 ) != 0 ) {
8408 // Handle Alarm Interrupt.
8415 if( ( registerC
& 0x40 ) != 0 ) {
8416 // Handle Periodic Interrupt.
8418 if( read_byte( 0x40, 0xA0 ) != 0 ) {
8419 // Wait Interval (Int 15, AH=83) active.
8420 Bit32u time
, toggle
;
8422 time
= read_dword( 0x40, 0x9C ); // Time left in microseconds.
8423 if( time
< 0x3D1 ) {
8425 Bit16u segment
, offset
;
8427 segment
= read_word( 0x40, 0x98 );
8428 offset
= read_word( 0x40, 0x9A );
8429 write_byte( 0x40, 0xA0, 0 ); // Turn of status byte.
8430 outb_cmos( 0xB, registerB
& 0x37 ); // Clear the Periodic Interrupt.
8431 write_byte(segment
, offset
, read_byte(segment
, offset
) | 0x80 ); // Write to specified flag byte.
8433 // Continue waiting.
8435 write_dword( 0x40, 0x9C, time
);
8448 ;------------------------------------------
8449 ;- INT74h
: PS
/2 mouse hardware interrupt
-
8450 ;------------------------------------------
8455 push
#0x00 ;; placeholder for status
8456 push
#0x00 ;; placeholder for X
8457 push
#0x00 ;; placeholder for Y
8458 push
#0x00 ;; placeholder for Z
8459 push
#0x00 ;; placeholder for make_far_call boolean
8460 call _int74_function
8461 pop cx
;; remove make_far_call from stack
8464 ;; make far call to EBDA
:0022
8467 push
0x040E ;; push
0000:040E (opcodes
0xff, 0x36, 0x0E, 0x04)
8469 //CALL_EP(0x0022) ;; call far routine (call_Ep DS:0022 :opcodes 0xff, 0x1e, 0x22, 0x00)
8474 add sp
, #8 ;; pop status, x, y, z
8476 pop ds
;; restore DS
8481 ;; This will perform an IRET
, but will retain value of current CF
8482 ;; by altering flags on stack
. Better than RETF
#02.
8487 and BYTE
[bp
+ 0x06], #0xfe
8493 or BYTE
[bp
+ 0x06], #0x01
8498 ;----------------------
8499 ;- INT13h (relocated
) -
8500 ;----------------------
8502 ; int13_relocated is a little bit messed up since I played with it
8503 ; I have to rewrite it
:
8504 ; - call a function that detect which function to call
8505 ; - make all called C function get the same parameters list
8509 #if BX_ELTORITO_BOOT
8510 ;; check
for an eltorito function
8512 jb int13_not_eltorito
8514 ja int13_not_eltorito
8523 jmp _int13_eltorito
;; ELDX
not used
8531 ;; check
if emulation active
8532 call _cdemu_isactive
8534 je int13_cdemu_inactive
8536 ;; check
if access to the emulated drive
8537 call _cdemu_emulated_drive
8540 cmp al
,dl
;; int13 on emulated drive
8555 jmp _int13_cdemu
;; ELDX
not used
8558 and dl
,#0xE0 ;; mask to get device class, including cdroms
8559 cmp al
,dl
;; al is
0x00 or 0x80
8560 jne int13_cdemu_inactive
;; inactive
for device
class
8572 dec dl
;; real drive is dl
- 1
8575 int13_cdemu_inactive
:
8581 #endif // BX_ELTORITO_BOOT
8592 push dx
;; push eltorito value of dx instead of sp
8603 ;; now the
16-bit registers can be restored with
:
8604 ;; pop ds
; pop es
; popa
; iret
8605 ;; arguments passed to functions should be
8606 ;; DS
, ES
, DI
, SI
, BP
, ELDX
, BX
, DX
, CX
, AX
, IP
, CS
, FLAGS
8612 jmp _int13_diskette_function
8621 // ebx is modified: BSD 5.2.1 boot loader problem
8622 // someone should figure out which 32 bit register that actually are used
8639 ;; int13_harddisk modifies high word of EAX
8642 call _int13_harddisk
8655 int18_handler
: ;; Boot Failure recovery
: try the next device
.
8663 ;; Get the boot sequence number out of the IPL memory
8665 mov ds
, bx
;; Set segment
8666 mov bx
, IPL_SEQUENCE_OFFSET
;; BX is now the sequence number
8668 mov IPL_SEQUENCE_OFFSET
, bx
;; Write it back
8669 mov ds
, ax
;; and reset the segment to zero
.
8671 ;; Carry on in the INT
19h handler
, using the
new sequence number
8679 int19_relocated
: ;; Boot function
, relocated
8681 ;; int19 was beginning to be really
complex, so now it
8682 ;; just calls a C function that does the work
8693 ;; Start from the first boot
device (0, in AX
)
8695 mov ds
, bx
;; Set segment to write to the IPL memory
8696 mov IPL_SEQUENCE_OFFSET
, ax
;; Save the sequence number
8697 mov ds
, ax
;; and reset the segment
.
8703 ;; Call the C code
for the next boot device
8704 call _int19_function
8706 ;; Boot failed
: invoke the boot recovery function
8712 int1c_handler
: ;; User Timer Tick
8716 ;----------------------
8717 ;- POST
: Floppy Drive
-
8718 ;----------------------
8724 mov
0x043e, al
;; drive
0 & 1 uncalibrated
, no interrupt has occurred
8726 mov
0x043f, al
;; diskette motor status
: read op
, drive0
, motors off
8728 mov
0x0440, al
;; diskette motor timeout counter
: not active
8729 mov
0x0441, al
;; diskette controller status
return code
8731 mov
0x0442, al
;; disk
& diskette controller status
register 0
8732 mov
0x0443, al
;; diskette controller status
register 1
8733 mov
0x0444, al
;; diskette controller status
register 2
8734 mov
0x0445, al
;; diskette controller cylinder number
8735 mov
0x0446, al
;; diskette controller head number
8736 mov
0x0447, al
;; diskette controller sector number
8737 mov
0x0448, al
;; diskette controller bytes written
8739 mov
0x048b, al
;; diskette configuration data
8741 ;; -----------------------------------------------------------------
8742 ;; (048F
) diskette controller information
8744 mov al
, #0x10 ;; get CMOS diskette drive type
8747 mov ah
, al
;; save byte to AH
8750 shr al
, #4 ;; look at top 4 bits for drive 0
8751 jz f0_missing
;; jump
if no drive0
8752 mov bl
, #0x07 ;; drive0 determined, multi-rate, has changed line
8755 mov bl
, #0x00 ;; no drive0
8758 mov al
, ah
;; restore from AH
8759 and al
, #0x0f ;; look at bottom 4 bits for drive 1
8760 jz f1_missing
;; jump
if no drive1
8761 or bl
, #0x70 ;; drive1 determined, multi-rate, has changed line
8763 ;; leave high bits in BL zerod
8764 mov
0x048f, bl
;; put
new val in
BDA (diskette controller information
)
8765 ;; -----------------------------------------------------------------
8768 mov
0x0490, al
;; diskette
0 media state
8769 mov
0x0491, al
;; diskette
1 media state
8771 ;; diskette
0,1 operational starting state
8772 ;; drive type has
not been determined
,
8773 ;; has no changed detection line
8777 mov
0x0494, al
;; diskette
0 current cylinder
8778 mov
0x0495, al
;; diskette
1 current cylinder
8781 out
#0x0a, al ;; clear DMA-1 channel 2 mask bit
8783 SET_INT_VECTOR(0x1E, #0xF000, #diskette_param_table2)
8784 SET_INT_VECTOR(0x40, #0xF000, #int13_diskette)
8785 SET_INT_VECTOR(0x0E, #0xF000, #int0e_handler) ;; IRQ 6
8790 ;--------------------
8791 ;- POST
: HARD DRIVE
-
8792 ;--------------------
8793 ; relocated here because the primary POST area isnt big enough
.
8796 // INT 76h calls INT 15h function ax=9100
8798 mov al
, #0x0a ; 0000 1010 = reserved, disable IRQ 14
8804 mov
0x0474, al
/* hard disk status of last operation */
8805 mov
0x0477, al
/* hard disk port offset (XT only ???) */
8806 mov
0x048c, al
/* hard disk status register */
8807 mov
0x048d, al
/* hard disk error register */
8808 mov
0x048e, al
/* hard disk task complete flag */
8810 mov
0x0475, al
/* hard disk number attached */
8812 mov
0x0476, al
/* hard disk control byte */
8813 SET_INT_VECTOR(0x13, #0xF000, #int13_handler)
8814 SET_INT_VECTOR(0x76, #0xF000, #int76_handler)
8815 ;; INT
41h
: hard disk
0 configuration pointer
8816 ;; INT
46h
: hard disk
1 configuration pointer
8817 SET_INT_VECTOR(0x41, #EBDA_SEG, #0x003D)
8818 SET_INT_VECTOR(0x46, #EBDA_SEG, #0x004D)
8820 ;; move disk geometry data from CMOS to EBDA disk parameter
table(s
)
8832 cmp al
, #47 ;; decimal 47 - user definable
8836 ;; CMOS purpose param table offset
8837 ;; 1b cylinders low
0
8838 ;; 1c cylinders high
1
8840 ;; 1e write pre
-comp low
5
8841 ;; 1f write pre
-comp high
6
8842 ;; 20 retries
/bad map
/heads
>8 8
8843 ;; 21 landing zone low C
8844 ;; 22 landing zone high D
8845 ;; 23 sectors
/track E
8850 ;;; Filling EBDA table
for hard disk
0.
8858 mov (0x003d + 0x05), ax
;; write precomp word
8863 mov (0x003d + 0x08), al
;; drive control byte
8872 mov (0x003d + 0x0C), ax
;; landing zone word
8874 mov al
, #0x1c ;; get cylinders word in AX
8876 in al
, #0x71 ;; high byte
8880 in al
, #0x71 ;; low byte
8881 mov bx
, ax
;; BX
= cylinders
8886 mov cl
, al
;; CL
= heads
8891 mov dl
, al
;; DL
= sectors
8894 jnbe hd0_post_logical_chs
;; if cylinders
> 1024, use translated style CHS
8896 hd0_post_physical_chs
:
8897 ;; no logical CHS mapping used
, just physical CHS
8898 ;; use Standard Fixed Disk Parameter
Table (FDPT
)
8899 mov (0x003d + 0x00), bx
;; number of physical cylinders
8900 mov (0x003d + 0x02), cl
;; number of physical heads
8901 mov (0x003d + 0x0E), dl
;; number of physical sectors
8904 hd0_post_logical_chs
:
8905 ;; complies with Phoenix style Translated Fixed Disk Parameter
Table (FDPT
)
8906 mov (0x003d + 0x09), bx
;; number of physical cylinders
8907 mov (0x003d + 0x0b), cl
;; number of physical heads
8908 mov (0x003d + 0x04), dl
;; number of physical sectors
8909 mov (0x003d + 0x0e), dl
;; number of logical
sectors (same
)
8911 mov (0x003d + 0x03), al
;; A0h signature
, indicates translated table
8914 jnbe hd0_post_above_2048
8915 ;; 1024 < c
<= 2048 cylinders
8918 jmp hd0_post_store_logical
8920 hd0_post_above_2048
:
8922 jnbe hd0_post_above_4096
8923 ;; 2048 < c
<= 4096 cylinders
8926 jmp hd0_post_store_logical
8928 hd0_post_above_4096
:
8930 jnbe hd0_post_above_8192
8931 ;; 4096 < c
<= 8192 cylinders
8934 jmp hd0_post_store_logical
8936 hd0_post_above_8192
:
8937 ;; 8192 < c
<= 16384 cylinders
8941 hd0_post_store_logical
:
8942 mov (0x003d + 0x00), bx
;; number of physical cylinders
8943 mov (0x003d + 0x02), cl
;; number of physical heads
8945 mov cl
, #0x0f ;; repeat count
8946 mov si
, #0x003d ;; offset to disk0 FDPT
8947 mov al
, #0x00 ;; sum
8948 hd0_post_checksum_loop
:
8952 jnz hd0_post_checksum_loop
8953 not al
;; now take
2s complement
8956 ;;; Done filling EBDA table
for hard disk
0.
8960 ;; is there really a second hard disk
? if not, return now
8968 ;; check that the hd type is really
0x0f.
8973 ;; check that the extended type is
47 - user definable
8977 cmp al
, #47 ;; decimal 47 - user definable
8982 ;; CMOS purpose param table offset
8983 ;; 0x24 cylinders low
0
8984 ;; 0x25 cylinders high
1
8986 ;; 0x27 write pre
-comp low
5
8987 ;; 0x28 write pre
-comp high
6
8989 ;; 0x2a landing zone low C
8990 ;; 0x2b landing zone high D
8991 ;; 0x2c sectors
/track E
8992 ;;; Fill EBDA table
for hard disk
1.
9002 mov (0x004d + 0x05), ax
;; write precomp word
9007 mov (0x004d + 0x08), al
;; drive control byte
9016 mov (0x004d + 0x0C), ax
;; landing zone word
9018 mov al
, #0x25 ;; get cylinders word in AX
9020 in al
, #0x71 ;; high byte
9024 in al
, #0x71 ;; low byte
9025 mov bx
, ax
;; BX
= cylinders
9030 mov cl
, al
;; CL
= heads
9035 mov dl
, al
;; DL
= sectors
9038 jnbe hd1_post_logical_chs
;; if cylinders
> 1024, use translated style CHS
9040 hd1_post_physical_chs
:
9041 ;; no logical CHS mapping used
, just physical CHS
9042 ;; use Standard Fixed Disk Parameter
Table (FDPT
)
9043 mov (0x004d + 0x00), bx
;; number of physical cylinders
9044 mov (0x004d + 0x02), cl
;; number of physical heads
9045 mov (0x004d + 0x0E), dl
;; number of physical sectors
9048 hd1_post_logical_chs
:
9049 ;; complies with Phoenix style Translated Fixed Disk Parameter
Table (FDPT
)
9050 mov (0x004d + 0x09), bx
;; number of physical cylinders
9051 mov (0x004d + 0x0b), cl
;; number of physical heads
9052 mov (0x004d + 0x04), dl
;; number of physical sectors
9053 mov (0x004d + 0x0e), dl
;; number of logical
sectors (same
)
9055 mov (0x004d + 0x03), al
;; A0h signature
, indicates translated table
9058 jnbe hd1_post_above_2048
9059 ;; 1024 < c
<= 2048 cylinders
9062 jmp hd1_post_store_logical
9064 hd1_post_above_2048
:
9066 jnbe hd1_post_above_4096
9067 ;; 2048 < c
<= 4096 cylinders
9070 jmp hd1_post_store_logical
9072 hd1_post_above_4096
:
9074 jnbe hd1_post_above_8192
9075 ;; 4096 < c
<= 8192 cylinders
9078 jmp hd1_post_store_logical
9080 hd1_post_above_8192
:
9081 ;; 8192 < c
<= 16384 cylinders
9085 hd1_post_store_logical
:
9086 mov (0x004d + 0x00), bx
;; number of physical cylinders
9087 mov (0x004d + 0x02), cl
;; number of physical heads
9089 mov cl
, #0x0f ;; repeat count
9090 mov si
, #0x004d ;; offset to disk0 FDPT
9091 mov al
, #0x00 ;; sum
9092 hd1_post_checksum_loop
:
9096 jnz hd1_post_checksum_loop
9097 not al
;; now take
2s complement
9100 ;;; Done filling EBDA table
for hard disk
1.
9104 ;--------------------
9105 ;- POST
: EBDA segment
9106 ;--------------------
9107 ; relocated here because the primary POST area isnt big enough
.
9112 mov byte ptr
[0x0], #EBDA_SIZE
9114 xor ax
, ax
; mov EBDA seg into
40E
9116 mov word ptr
[0x40E], #EBDA_SEG
9119 ;--------------------
9120 ;- POST
: EOI
+ jmp via
[0x40:67)
9121 ;--------------------
9122 ; relocated here because the primary POST area isnt big enough
.
9125 out
#0xA0, al ;; slave PIC EOI
9127 out
#0x20, al ;; master PIC EOI
9160 call _s3_resume_panic
9162 ;--------------------
9165 out
#0xA0, al ;; slave PIC EOI
9168 out
#0x20, al ;; master PIC EOI
9171 ;--------------------
9173 ;; in
: AL in BCD format
9174 ;; out
: AL in binary format
, AH will always be
0
9177 and bl
, #0x0f ;; bl has low digit
9178 shr al
, #4 ;; al has high digit
9180 mul al
, bh
;; multiply high digit by
10 (result in AX
)
9181 add al
, bl
;; then add low digit
9184 ;--------------------
9186 ;; Setup the Timer Ticks
Count (0x46C:dword
) and
9187 ;; Timer Ticks Roller
Flag (0x470:byte
)
9188 ;; The Timer Ticks Count needs to be set according to
9189 ;; the current CMOS time
, as
if ticks have been occurring
9190 ;; at
18.2hz since midnight up to
this point
. Calculating
9191 ;; this is a little complicated
. Here are the factors I gather
9192 ;; regarding
this. 14,318,180 hz was the original clock speed
,
9193 ;; chosen so it could be divided by either
3 to drive the
5Mhz CPU
9194 ;; at the time
, or 4 to drive the CGA video adapter
. The div3
9195 ;; source was divided again by
4 to feed a
1.193Mhz signal to
9196 ;; the timer
. With a maximum
16bit timer count
, this is again
9197 ;; divided down by
65536 to
18.2hz
.
9199 ;; 14,318,180 Hz clock
9200 ;; /3 = 4,772,726 Hz fed to orginal
5Mhz CPU
9201 ;; /4 = 1,193,181 Hz fed to timer
9202 ;; /65536 (maximum timer count
) = 18.20650736 ticks
/second
9203 ;; 1 second
= 18.20650736 ticks
9204 ;; 1 minute
= 1092.390442 ticks
9205 ;; 1 hour
= 65543.42651 ticks
9207 ;; Given the values in the CMOS clock
, one could calculate
9208 ;; the number of ticks by the following
:
9209 ;; ticks
= (BcdToBin(seconds
) * 18.206507) +
9210 ;; (BcdToBin(minutes
) * 1092.3904)
9211 ;; (BcdToBin(hours
) * 65543.427)
9212 ;; To get a little more accuracy
, since Im
using integer
9213 ;; arithmatic
, I use
:
9214 ;; ticks
= (BcdToBin(seconds
) * 18206507) / 1000000 +
9215 ;; (BcdToBin(minutes
) * 10923904) / 10000 +
9216 ;; (BcdToBin(hours
) * 65543427) / 1000
9221 xor eax
, eax
;; clear EAX
9224 in al
, #0x71 ;; AL has CMOS seconds in BCD
9225 call BcdToBin
;; EAX now has seconds in binary
9231 mov ecx
, eax
;; ECX will accumulate total ticks
9234 xor eax
, eax
;; clear EAX
9237 in al
, #0x71 ;; AL has CMOS minutes in BCD
9238 call BcdToBin
;; EAX now has minutes in binary
9244 add ecx
, eax
;; add to total ticks
9247 xor eax
, eax
;; clear EAX
9250 in al
, #0x71 ;; AL has CMOS hours in BCD
9251 call BcdToBin
;; EAX now has hours in binary
9257 add ecx
, eax
;; add to total ticks
9259 mov
0x46C, ecx
;; Timer Ticks Count
9261 mov
0x470, al
;; Timer Ticks Rollover Flag
9264 ;--------------------
9266 ;; record completion in BIOS task complete flag
9278 ;--------------------
9283 #include "apmbios.S"
9287 #include "apmbios.S"
9290 #include "apmbios.S"
9294 ;--------------------
9299 db
0x5f, 0x33, 0x32, 0x5f ;; "_32_" signature
9300 dw bios32_entry_point
, 0xf ;; 32 bit physical address
9301 db
0 ;; revision level
9302 ;; length in paragraphs
and checksum stored in a word to prevent errors
9303 dw (~(((bios32_entry_point
>> 8) + (bios32_entry_point
& 0xff) + 0x32) \
9304 & 0xff) << 8) + 0x01
9305 db
0,0,0,0,0 ;; reserved
9310 cmp eax
, #0x49435024 ;; "$PCI"
9312 mov eax
, #0x80000000
9317 #ifdef PCI_FIXED_HOST_BRIDGE
9318 cmp eax
, #PCI_FIXED_HOST_BRIDGE
9321 ;; say ok
if a device is present
9322 cmp eax
, #0xffffffff
9325 mov ebx
, #0x000f0000
9327 mov edx
, #pcibios_protected
9334 and dword ptr
[esp
+8],0xfffffffc ;; reset CS
.RPL
for kqemu
9345 cmp al
, #0x01 ;; installation check
9349 mov edx
, #0x20494350 ;; "PCI "
9352 pci_pro_f02
: ;; find pci device
9360 call pci_pro_select_reg
9374 pci_pro_f03
: ;; find
class code
9380 call pci_pro_select_reg
9385 jne pci_pro_nextdev2
9392 jne pci_pro_devloop2
9395 pci_pro_f08
: ;; read configuration byte
9398 call pci_pro_select_reg
9407 pci_pro_f09
: ;; read configuration word
9410 call pci_pro_select_reg
9419 pci_pro_f0a
: ;; read configuration dword
9422 call pci_pro_select_reg
9429 pci_pro_f0b
: ;; write configuration byte
9432 call pci_pro_select_reg
9441 pci_pro_f0c
: ;; write configuration word
9444 call pci_pro_select_reg
9453 pci_pro_f0d
: ;; write configuration dword
9456 call pci_pro_select_reg
9469 and dword ptr
[esp
+8],0xfffffffc ;; reset CS
.RPL
for kqemu
9479 and dword ptr
[esp
+8],0xfffffffc ;; reset CS
.RPL
for kqemu
9503 mov eax
, #0x80000000
9508 #ifdef PCI_FIXED_HOST_BRIDGE
9509 cmp eax
, #PCI_FIXED_HOST_BRIDGE
9512 ;; say ok
if a device is present
9513 cmp eax
, #0xffffffff
9524 cmp al
, #0x01 ;; installation check
9529 mov edx
, #0x20494350 ;; "PCI "
9531 mov di
, #pcibios_protected
9534 pci_real_f02
: ;; find pci device
9544 call pci_real_select_reg
9548 jne pci_real_nextdev
9555 jne pci_real_devloop
9560 pci_real_f03
: ;; find
class code
9566 call pci_real_select_reg
9571 jne pci_real_nextdev2
9578 jne pci_real_devloop2
9583 pci_real_f08
: ;; read configuration byte
9586 call pci_real_select_reg
9595 pci_real_f09
: ;; read configuration word
9598 call pci_real_select_reg
9607 pci_real_f0a
: ;; read configuration dword
9610 call pci_real_select_reg
9617 pci_real_f0b
: ;; write configuration byte
9620 call pci_real_select_reg
9629 pci_real_f0c
: ;; write configuration word
9632 call pci_real_select_reg
9641 pci_real_f0d
: ;; write configuration dword
9644 call pci_real_select_reg
9651 pci_real_f0e
: ;; get irq routing options
9653 jne pci_real_unknown
9655 cmp word ptr
[di
], #pci_routing_table_structure_end - pci_routing_table_structure_start
9656 jb pci_real_too_small
9658 mov word ptr
[di
], #pci_routing_table_structure_end - pci_routing_table_structure_start
9666 mov si
, #pci_routing_table_structure_start
9674 mov cx
, #pci_routing_table_structure_end - pci_routing_table_structure_start
9683 mov bx
, #(1 << 9) | (1 << 11) ;; irq 9 and 11 are used
9687 mov word ptr
[di
], #pci_routing_table_structure_end - pci_routing_table_structure_start
9705 pci_real_select_reg
:
9719 pci_routing_table_structure
:
9720 db
0x24, 0x50, 0x49, 0x52 ;; "$PIR" signature
9722 dw
32 + (6 * 16) ;; table size
9723 db
0 ;; PCI interrupt router bus
9724 db
0x08 ;; PCI interrupt router DevFunc
9725 dw
0x0000 ;; PCI exclusive IRQs
9726 dw
0x8086 ;; compatible PCI interrupt router vendor ID
9727 dw
0x122e ;; compatible PCI interrupt router device ID
9728 dw
0,0 ;; Miniport data
9729 db
0,0,0,0,0,0,0,0,0,0,0 ;; reserved
9731 pci_routing_table_structure_start
:
9732 ;; first slot entry PCI
-to
-ISA (embedded
)
9733 db
0 ;; pci bus number
9734 db
0x08 ;; pci device
number (bit
7-3)
9735 db
0x60 ;; link value INTA
#: pointer into PCI2ISA config space
9736 dw
0xdef8 ;; IRQ bitmap INTA
#
9737 db
0x61 ;; link value INTB
#
9738 dw
0xdef8 ;; IRQ bitmap INTB
#
9739 db
0x62 ;; link value INTC
#
9740 dw
0xdef8 ;; IRQ bitmap INTC
#
9741 db
0x63 ;; link value INTD
#
9742 dw
0xdef8 ;; IRQ bitmap INTD
#
9743 db
0 ;; physical
slot (0 = embedded
)
9745 ;; second slot entry
: 1st PCI slot
9746 db
0 ;; pci bus number
9747 db
0x10 ;; pci device
number (bit
7-3)
9748 db
0x61 ;; link value INTA
#
9749 dw
0xdef8 ;; IRQ bitmap INTA
#
9750 db
0x62 ;; link value INTB
#
9751 dw
0xdef8 ;; IRQ bitmap INTB
#
9752 db
0x63 ;; link value INTC
#
9753 dw
0xdef8 ;; IRQ bitmap INTC
#
9754 db
0x60 ;; link value INTD
#
9755 dw
0xdef8 ;; IRQ bitmap INTD
#
9756 db
1 ;; physical
slot (0 = embedded
)
9758 ;; third slot entry
: 2nd PCI slot
9759 db
0 ;; pci bus number
9760 db
0x18 ;; pci device
number (bit
7-3)
9761 db
0x62 ;; link value INTA
#
9762 dw
0xdef8 ;; IRQ bitmap INTA
#
9763 db
0x63 ;; link value INTB
#
9764 dw
0xdef8 ;; IRQ bitmap INTB
#
9765 db
0x60 ;; link value INTC
#
9766 dw
0xdef8 ;; IRQ bitmap INTC
#
9767 db
0x61 ;; link value INTD
#
9768 dw
0xdef8 ;; IRQ bitmap INTD
#
9769 db
2 ;; physical
slot (0 = embedded
)
9771 ;; 4th slot entry
: 3rd PCI slot
9772 db
0 ;; pci bus number
9773 db
0x20 ;; pci device
number (bit
7-3)
9774 db
0x63 ;; link value INTA
#
9775 dw
0xdef8 ;; IRQ bitmap INTA
#
9776 db
0x60 ;; link value INTB
#
9777 dw
0xdef8 ;; IRQ bitmap INTB
#
9778 db
0x61 ;; link value INTC
#
9779 dw
0xdef8 ;; IRQ bitmap INTC
#
9780 db
0x62 ;; link value INTD
#
9781 dw
0xdef8 ;; IRQ bitmap INTD
#
9782 db
3 ;; physical
slot (0 = embedded
)
9784 ;; 5th slot entry
: 4rd PCI slot
9785 db
0 ;; pci bus number
9786 db
0x28 ;; pci device
number (bit
7-3)
9787 db
0x60 ;; link value INTA
#
9788 dw
0xdef8 ;; IRQ bitmap INTA
#
9789 db
0x61 ;; link value INTB
#
9790 dw
0xdef8 ;; IRQ bitmap INTB
#
9791 db
0x62 ;; link value INTC
#
9792 dw
0xdef8 ;; IRQ bitmap INTC
#
9793 db
0x63 ;; link value INTD
#
9794 dw
0xdef8 ;; IRQ bitmap INTD
#
9795 db
4 ;; physical
slot (0 = embedded
)
9797 ;; 6th slot entry
: 5rd PCI slot
9798 db
0 ;; pci bus number
9799 db
0x30 ;; pci device
number (bit
7-3)
9800 db
0x61 ;; link value INTA
#
9801 dw
0xdef8 ;; IRQ bitmap INTA
#
9802 db
0x62 ;; link value INTB
#
9803 dw
0xdef8 ;; IRQ bitmap INTB
#
9804 db
0x63 ;; link value INTC
#
9805 dw
0xdef8 ;; IRQ bitmap INTC
#
9806 db
0x60 ;; link value INTD
#
9807 dw
0xdef8 ;; IRQ bitmap INTD
#
9808 db
5 ;; physical
slot (0 = embedded
)
9810 pci_routing_table_structure_end
:
9816 pcibios_init_sel_reg
:
9828 pcibios_init_iomem_bases
:
9831 mov eax
, #0xe0000000 ;; base for memory init
9833 mov ax
, #0xc000 ;; base for i/o init
9835 mov ax
, #0x0010 ;; start at base address #0
9840 call pcibios_init_sel_reg
9845 mov dl
, #0x04 ;; disable i/o and memory space access
9846 call pcibios_init_sel_reg
9853 call pcibios_init_sel_reg
9859 mov eax
, #0xffffffff
9864 xor eax
, #0xffffffff
9868 add eax
, ecx
;; calculate next free mem base
9869 add eax
, #0x01000000
9870 and eax
, #0xff000000
9884 add ax
, cx
;; calculate next free i
/o base
9892 je enable_iomem_space
9893 mov byte ptr
[bp
-8], al
9894 jmp pci_init_io_loop2
9896 mov dl
, #0x04 ;; enable i/o and memory space access if available
9897 call pcibios_init_sel_reg
9903 mov byte ptr
[bp
-8], #0x10
9906 jne pci_init_io_loop1
9911 pcibios_init_set_elcr
:
9935 mov dx
, #0x04d0 ;; reset ELCR1 + ELCR2
9940 mov si
, #pci_routing_table_structure
9944 call pcibios_init_sel_reg
9947 cmp eax
, [si
+12] ;; check irq router
9950 call pcibios_init_sel_reg
9951 push bx
;; save irq router bus
+ devfunc
9954 out dx
, ax
;; reset PIRQ route control
9961 add si
, #0x20 ;; set pointer to 1st entry
9963 mov ax
, #pci_irq_list
9972 call pcibios_init_sel_reg
9976 jnz pci_test_int_pin
9982 call pcibios_init_sel_reg
9987 dec al
;; determine pirq reg
9996 call pcibios_init_sel_reg
10003 mov bx
, [bp
-2] ;; pci irq list pointer
10008 call pcibios_init_set_elcr
10012 add bl
, [bp
-3] ;; pci function number
10014 call pcibios_init_sel_reg
10021 jnz pci_init_irq_loop2
10024 mov byte ptr
[bp
-3], #0x00
10025 loop pci_init_irq_loop1
10032 #endif // !BX_ROMBIOS32
10033 #endif // BX_PCIBIOS
10037 ;; save a20
and enable it
10043 ;; save SS
:SP to the BDA
10050 lidt
[pmode_IDT_info
]
10052 lgdt
[rombios32_gdt_48
]
10053 ;; set PE bit in CR0
10057 ;; start
protected mode code
: ljmpl
0x10:rombios32_init1
10060 dw
0x000f ;; high
16 bit address
10065 ;; init data segments
10075 ;; init the stack pointer to point below EBDA
10081 ;; pass pointer to s3_resume_flag
and s3_resume_vector to rombios32
10085 ;; call rombios32 code
10086 mov eax
, #0x000e0000
10089 ;; return to
16 bit
protected mode first
10096 ;; restore data segment limits to
0xffff
10104 ;; reset PE bit in CR0
10109 ;; far jump to flush CPU queue after transition to real mode
10110 JMP_AP(0xf000, rombios32_real_mode
)
10112 rombios32_real_mode
:
10113 ;; restore IDT to normal real
-mode defaults
10115 lidt
[rmode_IDT_info
]
10123 ;; restore SS
:SP from the BDA
10140 dw
0xffff, 0, 0x9b00, 0x00cf ; 32 bit flat code
segment (0x10)
10141 dw
0xffff, 0, 0x9300, 0x00cf ; 32 bit flat data
segment (0x18)
10142 dw
0xffff, 0, 0x9b0f, 0x0000 ; 16 bit code segment base
=0xf0000 limit
=0xffff
10143 dw
0xffff, 0, 0x9300, 0x0000 ; 16 bit data segment base
=0x0 limit
=0xffff
10144 #endif // BX_ROMBIOS32
10147 ; parallel port detection
: base address in DX
, index in BX
, timeout in CL
10152 and al
, #0xdf ; clear input mode
10162 mov
[bx
+0x408], dx
; Parallel I
/O address
10164 mov
[bx
+0x478], cl
; Parallel printer timeout
10169 ; serial port detection
: base address in DX
, index in BX
, timeout in CL
10188 mov
[bx
+0x400], dx
; Serial I
/O address
10190 mov
[bx
+0x47c], cl
; Serial timeout
10217 ;; We need a copy of
this string
, but we are
not actually a PnP BIOS
,
10218 ;; so make sure it is
*not* aligned
, so OSes will
not see it
if they scan
.
10226 ;; Scan
for existence of valid expansion ROMS
.
10227 ;; Video ROM
: from
0xC0000..0xC7FFF in
2k increments
10228 ;; General ROM
: from
0xC8000..0xDFFFF in
2k increments
10229 ;; System ROM
: only
0xE0000
10235 ;; 2 ROM length in
512-byte blocks
10236 ;; 3 ROM initialization entry
point (FAR CALL
)
10241 mov ax
, #0x0004 ;; start with increment of 4 (512-byte) blocks = 2k
10242 cmp
[0], #0xAA55 ;; look for signature
10243 jne rom_scan_increment
10245 jnz rom_scan_increment
10246 mov al
, [2] ;; change increment to ROM length in
512-byte blocks
10248 ;; We want our increment in
512-byte quantities
, rounded to
10249 ;; the nearest
2k quantity
, since we only scan at
2k intervals
.
10251 jz block_count_rounded
10252 and al
, #0xfc ;; needs rounding up
10254 block_count_rounded
:
10256 xor bx
, bx
;; Restore DS back to
0000:
10260 ;; Push addr of ROM entry point
10261 push cx
;; Push seg
10262 push
#0x0003 ;; Push offset
10264 ;; Point ES
:DI at
"$PnP", which tells the ROM that we are a PnP BIOS
.
10265 ;; That should stop it grabbing INT
19h
; we will use its BEV instead
.
10270 mov bp
, sp
;; Call ROM init routine
using seg
:off on stack
10271 db
0xff ;; call_far ss
:[bp
+0]
10274 cli
;; In
case expansion ROM BIOS turns IF on
10275 add sp
, #2 ;; Pop offset value
10276 pop cx
;; Pop seg
value (restore CX
)
10278 ;; Look at the ROM
's PnP Expansion header. Properly, we're supposed
10279 ;; to init all the ROMs
and then go back
and build an IPL table of
10280 ;; all the bootable devices
, but we can get away with one pass
.
10281 mov ds
, cx
;; ROM base
10282 mov bx
, 0x001a ;; 0x1A is the offset into ROM header that contains
...
10283 mov ax
, [bx
] ;; the offset of PnP expansion header
, where
...
10284 cmp ax
, #0x5024 ;; we look for signature "$PnP"
10290 mov ax
, 0x16[bx
] ;; 0x16 is the offset of Boot Connection Vector
10294 ;; Option ROM has BCV
. Run it now
.
10295 push cx
;; Push seg
10296 push ax
;; Push offset
10298 ;; Point ES
:DI at
"$PnP", which tells the ROM that we are a PnP BIOS
.
10302 /* jump to BCV function entry pointer */
10303 mov bp
, sp
;; Call ROM BCV routine
using seg
:off on stack
10304 db
0xff ;; call_far ss
:[bp
+0]
10307 cli
;; In
case expansion ROM BIOS turns IF on
10308 add sp
, #2 ;; Pop offset value
10309 pop cx
;; Pop seg
value (restore CX
)
10313 mov ax
, 0x1a[bx
] ;; 0x1A is also the offset into the expansion header of
...
10314 cmp ax
, #0x0000 ;; the Bootstrap Entry Vector, or zero if there is none.
10317 ;; Found a device that thinks it can boot the system
. Record its BEV
and product name string
.
10318 mov di
, 0x10[bx
] ;; Pointer to the product name string
or zero
if none
10319 mov bx
, #IPL_SEG ;; Go to the segment where the IPL table lives
10321 mov bx
, IPL_COUNT_OFFSET
;; Read the number of entries so far
10322 cmp bx
, #IPL_TABLE_ENTRIES
10323 je no_bev
;; Get out
if the table is full
10324 shl bx
, #0x4 ;; Turn count into offset (entries are 16 bytes)
10325 mov
0[bx
], #IPL_TYPE_BEV ;; This entry is a BEV device
10326 mov
6[bx
], cx
;; Build a far pointer from the segment
...
10327 mov
4[bx
], ax
;; and the offset
10330 mov
0xA[bx
], cx
;; Build a far pointer from the segment
...
10331 mov
8[bx
], di
;; and the offset
10333 shr bx
, #0x4 ;; Turn the offset back into a count
10334 inc bx
;; We have one more entry now
10335 mov IPL_COUNT_OFFSET
, bx
;; Remember that
.
10338 pop di
;; Restore DI
10339 pop ax
;; Restore AX
10340 rom_scan_increment
:
10341 shl ax
, #5 ;; convert 512-bytes blocks to 16-byte increments
10342 ;; because the segment selector is shifted left
4 bits
.
10344 pop ax
;; Restore AX
10348 xor ax
, ax
;; Restore DS back to
0000:
10355 and eax
, #0x9fffffff
10357 jmp post_enable_cache_done
10360 mov al
, #0x11 ; send initialisation commands
10375 out
0x21, AL
;master pic
: unmask IRQ
0, 1, 2, 6
10376 #if BX_USE_PS2_MOUSE
10381 out
0xa1, AL
;slave pic
: unmask IRQ
12, 13, 14
10384 ;; the following area can be used to write dynamically generated tables
10386 bios_table_area_start
:
10388 dd bios_table_area_end
- bios_table_area_start
- 8;
10394 .org
0xe05b ; POST Entry Point
10396 jmp post_enable_cache
; hack
: we have limited space before next
.org
,
10397 ; so take
this bit out
-of
-line
10398 post_enable_cache_done
:
10401 ;; first reset the DMA controllers
10405 ;; then initialize the DMA controllers
10407 out
0xD6, al
; cascade mode of channel
4 enabled
10409 out
0xD4, al
; unmask channel
4
10411 ;; Examine CMOS shutdown status
.
10419 ;; Reset CMOS shutdown status
.
10421 out
0x70, AL
; select CMOS
register Fh
10423 out
0x71, AL
; set shutdown action to normal
10425 ;; Examine CMOS shutdown status
.
10428 ;; 0x00, 0x09, 0x0D+ = normal startup
10436 ;; 0x05 = eoi
+ jmp via
[0x40:0x67] jump
10440 ;; 0x0A = jmp via
[0x40:0x67] jump
10444 ;; 0x0B = iret via
[0x40:0x67]
10448 ;; 0x0C = retf via
[0x40:0x67]
10452 ;; Examine CMOS shutdown status
.
10453 ;; 0x01,0x02,0x03,0x04,0x06,0x07,0x08 = Unimplemented shutdown status
.
10455 call _shutdown_status_panic
10461 ; 0xb0, 0x20, /* mov al, #0x20 */
10462 ; 0xe6, 0x20, /* out 0x20, al ;send EOI to PIC */
10472 ; case 0: normal startup
10481 ;; Save shutdown status
10487 ;; zero out BIOS data
area (40:00..40:ff
)
10489 mov cx
, #0x0080 ;; 128 words
10495 call _log_bios_start
10497 ;; set all interrupts to
default handler
10498 xor bx
, bx
;; offset index
10499 mov cx
, #0x0100 ;; counter (256 interrupts)
10500 mov ax
, #dummy_iret_handler
10508 loop post_default_ints
10510 ;; set vector
0x79 to zero
10511 ;; this is used by
'gardian angel' protection system
10512 SET_INT_VECTOR(0x79, #0, #0)
10514 ;; base memory in K
40:13 (word
)
10515 mov ax
, #BASE_MEM_IN_K
10519 ;; Manufacturing Test
40:12
10522 ;; Warm Boot Flag
0040:0072
10523 ;; value of
1234h
= skip memory checks
10527 ;; Printer Services vector
10528 SET_INT_VECTOR(0x17, #0xF000, #int17_handler)
10530 ;; Bootstrap failure vector
10531 SET_INT_VECTOR(0x18, #0xF000, #int18_handler)
10533 ;; Bootstrap Loader vector
10534 SET_INT_VECTOR(0x19, #0xF000, #int19_handler)
10536 ;; User Timer Tick vector
10537 SET_INT_VECTOR(0x1c, #0xF000, #int1c_handler)
10539 ;; Memory Size Check vector
10540 SET_INT_VECTOR(0x12, #0xF000, #int12_handler)
10542 ;; Equipment Configuration Check vector
10543 SET_INT_VECTOR(0x11, #0xF000, #int11_handler)
10546 SET_INT_VECTOR(0x15, #0xF000, #int15_handler)
10552 SET_INT_VECTOR(0x08, #0xF000, #int08_handler)
10553 ;; int 1C already points at
dummy_iret_handler (above
)
10554 mov al
, #0x34 ; timer0: binary count, 16bit count, mode 2
10556 mov al
, #0x00 ; maximum count of 0000H = 18.2Hz
10561 SET_INT_VECTOR(0x09, #0xF000, #int09_handler)
10562 SET_INT_VECTOR(0x16, #0xF000, #int16_handler)
10566 mov
0x0417, al
/* keyboard shift flags, set 1 */
10567 mov
0x0418, al
/* keyboard shift flags, set 2 */
10568 mov
0x0419, al
/* keyboard alt-numpad work area */
10569 mov
0x0471, al
/* keyboard ctrl-break flag */
10570 mov
0x0497, al
/* keyboard status flags 4 */
10572 mov
0x0496, al
/* keyboard status flags 3 */
10575 /* keyboard head of buffer pointer */
10579 /* keyboard end of buffer pointer */
10582 /* keyboard pointer to start of buffer */
10586 /* keyboard pointer to end of buffer */
10590 /* init the keyboard */
10591 call _keyboard_init
10593 ;; mov CMOS Equipment Byte to BDA Equipment Word
10602 SET_INT_VECTOR(0x0F, #0xF000, #dummy_iret_handler)
10606 mov cl
, #0x14 ; timeout value
10607 mov dx
, #0x378 ; Parallel I/O address, port 1
10608 call detect_parport
10609 mov dx
, #0x278 ; Parallel I/O address, port 2
10610 call detect_parport
10612 mov ax
, 0x410 ; Equipment word bits
14..15 determing
# parallel ports
10614 or ax
, bx
; set number of parallel ports
10618 SET_INT_VECTOR(0x0C, #0xF000, #dummy_iret_handler)
10619 SET_INT_VECTOR(0x14, #0xF000, #int14_handler)
10621 mov cl
, #0x0a ; timeout value
10622 mov dx
, #0x03f8 ; Serial I/O address, port 1
10624 mov dx
, #0x02f8 ; Serial I/O address, port 2
10626 mov dx
, #0x03e8 ; Serial I/O address, port 3
10628 mov dx
, #0x02e8 ; Serial I/O address, port 4
10631 mov ax
, 0x410 ; Equipment word bits
9..11 determing
# serial ports
10633 or ax
, bx
; set number of serial port
10637 SET_INT_VECTOR(0x1A, #0xF000, #int1a_handler)
10638 SET_INT_VECTOR(0x4A, #0xF000, #dummy_iret_handler)
10639 SET_INT_VECTOR(0x70, #0xF000, #int70_handler)
10640 ;; BIOS DATA AREA
0x4CE ???
10641 call timer_tick_post
10643 ;; PS
/2 mouse setup
10644 SET_INT_VECTOR(0x74, #0xF000, #int74_handler)
10646 ;; IRQ13 (FPU exception
) setup
10647 SET_INT_VECTOR(0x75, #0xF000, #int75_handler)
10650 SET_INT_VECTOR(0x10, #0xF000, #int10_handler)
10655 mov cx
, #0xc000 ;; init vga bios
10659 call _print_bios_banner
10662 call rombios32_init
10665 call pcibios_init_iomem_bases
10666 call pcibios_init_irqs
10667 #endif //BX_PCIBIOS
10673 call floppy_drive_post
10676 ;; Hard Drive setup
10678 call hard_drive_post
10683 ;; ATA
/ATAPI driver setup
10689 #endif // BX_USE_ATADRV
10691 #if BX_ELTORITO_BOOT
10693 ;; eltorito floppy
/harddisk emulation from cd
10697 #endif // BX_ELTORITO_BOOT
10699 call _init_boot_vectors
10701 mov cx
, #0xc800 ;; init option roms
10705 #if BX_ELTORITO_BOOT
10706 call _interactive_bootkey
10707 #endif // BX_ELTORITO_BOOT
10709 sti
;; enable interrupts
10712 .org
0xe2c3 ; NMI Handler Entry Point
10714 ;; FIXME the NMI handler should
not panic
10715 ;; but iret when called from
int75 (fpu exception
)
10716 call _nmi_handler_msg
10720 out
0xf0, al
// clear irq13
10721 call eoi_both_pics
// clear interrupt
10722 int 2 // legacy nmi call
10725 ;-------------------------------------------
10726 ;- INT
13h Fixed Disk Services Entry Point
-
10727 ;-------------------------------------------
10728 .org
0xe3fe ; INT
13h Fixed Disk Services Entry Point
10730 //JMPL(int13_relocated)
10731 jmp int13_relocated
10733 .org
0xe401 ; Fixed Disk Parameter Table
10738 .org
0xe6f2 ; INT
19h Boot Load Service Entry Point
10741 jmp int19_relocated
10742 ;-------------------------------------------
10743 ;- System BIOS Configuration Data Table
10744 ;-------------------------------------------
10745 .org BIOS_CONFIG_TABLE
10746 db
0x08 ; Table
size (bytes
) -Lo
10747 db
0x00 ; Table
size (bytes
) -Hi
10752 ; b7
: 1=DMA channel
3 used by hard disk
10753 ; b6
: 1=2 interrupt controllers present
10754 ; b5
: 1=RTC present
10755 ; b4
: 1=BIOS calls
int 15h
/4Fh every key
10756 ; b3
: 1=wait
for extern event
supported (Int
15h
/41h
)
10757 ; b2
: 1=extended BIOS data area used
10758 ; b1
: 0=AT
or ESDI bus
, 1=MicroChannel
10759 ; b0
: 1=Dual
bus (MicroChannel
+ ISA
)
10763 (BX_CALL_INT15_4F
<< 4) | \
10765 (BX_USE_EBDA
<< 2) | \
10769 ; b7
: 1=32-bit DMA supported
10770 ; b6
: 1=int16h
, function
9 supported
10771 ; b5
: 1=int15h
/C6h (get POS data
) supported
10772 ; b4
: 1=int15h
/C7h (get mem map info
) supported
10773 ; b3
: 1=int15h
/C8h (en
/dis CPU
) supported
10774 ; b2
: 1=non
-8042 kb controller
10775 ; b1
: 1=data streaming supported
10789 ; b4
: POST supports ROM
-to
-RAM enable
/disable
10790 ; b3
: SCSI on system board
10791 ; b2
: info panel installed
10792 ; b1
: Initial Machine
Load (IML
) system
- BIOS on disk
10793 ; b0
: SCSI supported in IML
10797 ; b6
: EEPROM present
10798 ; b5
-3: ABIOS
presence (011 = not supported
)
10800 ; b1
: memory split above
16Mb supported
10801 ; b0
: POSTEXT directly supported by POST
10803 ; Feature byte
5 (IBM
)
10804 ; b1
: enhanced mouse
10810 .org
0xe729 ; Baud Rate Generator Table
10815 .org
0xe739 ; INT
14h Serial Communications Service Entry Point
10821 call _int14_function
10827 ;----------------------------------------
10828 ;- INT
16h Keyboard Service Entry Point
-
10829 ;----------------------------------------
10845 call _int16_function
10855 and BYTE
[bp
+ 0x06], #0xbf
10863 or BYTE
[bp
+ 0x06], #0x40
10871 int16_wait_for_key
:
10875 jne int16_key_found
10879 /* no key yet, call int 15h, function AX=9002 */
10880 0x50, /* push AX */
10881 0xb8, 0x02, 0x90, /* mov AX, #0x9002 */
10882 0xcd, 0x15, /* int 15h */
10884 0xeb, 0xea, /* jmp WAIT_FOR_KEY */
10886 jmp int16_wait_for_key
10891 call _int16_function
10896 /* notify int16 complete w/ int 15h, function AX=9102 */
10897 0x50, /* push AX */
10898 0xb8, 0x02, 0x91, /* mov AX, #0x9102 */
10899 0xcd, 0x15, /* int 15h */
10906 ;-------------------------------------------------
10907 ;- INT09h
: Keyboard Hardware Service Entry Point
-
10908 ;-------------------------------------------------
10914 mov al
, #0xAD ;;disable keyboard
10923 in al
, #0x60 ;;read key from keyboard controller
10927 #ifdef BX_CALL_INT15_4F
10928 mov ah
, #0x4f ;; allow for keyboard intercept
10934 ;; check
for extended key
10936 jne int09_check_pause
10939 mov al
, BYTE
[0x496] ;; mf2_state
|= 0x02
10941 mov BYTE
[0x496], al
10944 int09_check_pause
: ;; check
for pause key
10946 jne int09_process_key
10949 mov al
, BYTE
[0x496] ;; mf2_state
|= 0x01
10951 mov BYTE
[0x496], al
10957 call _int09_function
10963 call eoi_master_pic
10966 mov al
, #0xAE ;;enable keyboard
10972 ;----------------------------------------
10973 ;- INT
13h Diskette Service Entry Point
-
10974 ;----------------------------------------
10977 jmp int13_noeltorito
10979 ;---------------------------------------------
10980 ;- INT
0Eh Diskette Hardware ISR Entry Point
-
10981 ;---------------------------------------------
10982 .org
0xef57 ; INT
0Eh Diskette Hardware ISR Entry Point
10992 mov al
, #0x08 ; sense interrupt status
11010 xor ax
, ax
;; segment
0000
11012 call eoi_master_pic
11014 or al
, #0x80 ;; diskette interrupt has occurred
11022 .org
0xefc7 ; Diskette Controller Parameter Table
11023 diskette_param_table
:
11024 ;; Since no provisions are made
for multiple drive types
, most
11025 ;; values in
this table are ignored
. I set parameters
for 1.44M
11028 db
0x02 ;; head load time
0000001, DMA used
11040 ;----------------------------------------
11041 ;- INT17h
: Printer Service Entry Point
-
11042 ;----------------------------------------
11049 call _int17_function
11054 diskette_param_table2
:
11055 ;; New diskette parameter table adding
3 parameters from IBM
11056 ;; Since no provisions are made
for multiple drive types
, most
11057 ;; values in
this table are ignored
. I set parameters
for 1.44M
11060 db
0x02 ;; head load time
0000001, DMA used
11070 db
79 ;; maximum track
11071 db
0 ;; data transfer rate
11072 db
4 ;; drive type in cmos
11074 .org
0xf045 ; INT
10 Functions
0-Fh Entry Point
11081 .org
0xf065 ; INT
10h Video Support Service Entry Point
11083 ;; dont
do anything
, since the VGA BIOS handles int10h requests
11086 .org
0xf0a4 ; MDA
/CGA Video Parameter
Table (INT
1Dh
)
11091 .org
0xf841 ; INT
12h Memory Size Service Entry Point
11092 ; ??? different
for Pentium (machine check
)?
11104 .org
0xf84d ; INT
11h Equipment List Service Entry Point
11116 .org
0xf859 ; INT
15h System Services Entry Point
11130 #if BX_USE_PS2_MOUSE
11132 je int15_handler_mouse
11134 call _int15_function
11135 int15_handler_mouse_ret
:
11137 int15_handler32_ret
:
11147 #if BX_USE_PS2_MOUSE
11148 int15_handler_mouse
:
11149 call _int15_function_mouse
11150 jmp int15_handler_mouse_ret
11155 call _int15_function32
11157 jmp int15_handler32_ret
11159 ;; Protected mode IDT descriptor
11161 ;; I just make the limit
0, so the machine will shutdown
11162 ;; if an exception occurs during
protected mode memory
11165 ;; Set base to f0000 to correspond to beginning of BIOS
,
11166 ;; in
case I actually define an IDT later
11170 dw
0x0000 ;; limit
15:00
11171 dw
0x0000 ;; base
15:00
11172 db
0x0f ;; base
23:16
11174 ;; Real mode IDT descriptor
11176 ;; Set to typical real
-mode values
.
11181 dw
0x03ff ;; limit
15:00
11182 dw
0x0000 ;; base
15:00
11183 db
0x00 ;; base
23:16
11189 .org
0xfe6e ; INT
1Ah Time
-of
-day Service Entry Point
11202 mov ax
, ss
; set readable descriptor to ds
, for calling pcibios
11203 mov ds
, ax
; on
16bit
protected mode
.
11204 jmp int1a_callfunction
11211 int1a_callfunction
:
11212 call _int1a_function
11218 ;; int70h
: IRQ8
- CMOS RTC
11225 call _int70_function
11233 .org
0xfea5 ; INT
08h System Timer ISR Entry Point
11241 ;; time to turn off
drive(s
)?
11244 jz int08_floppy_off
11247 jnz int08_floppy_off
11248 ;; turn
motor(s
) off
11257 mov eax
, 0x046c ;; get ticks dword
11260 ;; compare eax to one days worth of timer ticks at
18.2 hz
11261 cmp eax
, #0x001800B0
11262 jb int08_store_ticks
11263 ;; there has been a midnight rollover at
this point
11264 xor eax
, eax
;; zero out counter
11265 inc BYTE
0x0470 ;; increment rollover flag
11268 mov
0x046c, eax
;; store
new ticks dword
11269 ;; chain to user timer tick INT
#0x1c
11271 //;; call_ep [ds:loc]
11272 //CALL_EP( 0x1c << 2 )
11275 call eoi_master_pic
11280 .org
0xfef3 ; Initial Interrupt Vector Offsets Loaded by POST
11284 .ascii BIOS_COPYRIGHT_STRING
11286 ;------------------------------------------------
11287 ;- IRET Instruction
for Dummy Interrupt Handler
-
11288 ;------------------------------------------------
11289 .org
0xff53 ; IRET Instruction
for Dummy Interrupt Handler
11290 dummy_iret_handler
:
11293 .org
0xff54 ; INT
05h Print Screen Service Entry Point
11297 .org
0xfff0 ; Power
-up Entry Point
11300 .org
0xfff5 ; ASCII Date ROM was built
- 8 characters in MM
/DD
/YY
11301 .ascii BIOS_BUILD_DATE
11303 .org
0xfffe ; System Model ID
11307 .org
0xfa6e ;; Character Font
for 320x200
& 640x200
Graphics (lower
128 characters
)
11310 * This font comes from the fntcol16.zip package (c) by Joseph Gil
11311 * found at ftp://ftp.simtel.net/pub/simtelnet/msdos/screen/fntcol16.zip
11312 * This font is public domain
11314 static Bit8u vgafont8
[128*8]=
11316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11317 0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e,
11318 0x7e, 0xff, 0xdb, 0xff, 0xc3, 0xe7, 0xff, 0x7e,
11319 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00,
11320 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00,
11321 0x38, 0x7c, 0x38, 0xfe, 0xfe, 0x7c, 0x38, 0x7c,
11322 0x10, 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x7c,
11323 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x18, 0x00, 0x00,
11324 0xff, 0xff, 0xe7, 0xc3, 0xc3, 0xe7, 0xff, 0xff,
11325 0x00, 0x3c, 0x66, 0x42, 0x42, 0x66, 0x3c, 0x00,
11326 0xff, 0xc3, 0x99, 0xbd, 0xbd, 0x99, 0xc3, 0xff,
11327 0x0f, 0x07, 0x0f, 0x7d, 0xcc, 0xcc, 0xcc, 0x78,
11328 0x3c, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x7e, 0x18,
11329 0x3f, 0x33, 0x3f, 0x30, 0x30, 0x70, 0xf0, 0xe0,
11330 0x7f, 0x63, 0x7f, 0x63, 0x63, 0x67, 0xe6, 0xc0,
11331 0x99, 0x5a, 0x3c, 0xe7, 0xe7, 0x3c, 0x5a, 0x99,
11332 0x80, 0xe0, 0xf8, 0xfe, 0xf8, 0xe0, 0x80, 0x00,
11333 0x02, 0x0e, 0x3e, 0xfe, 0x3e, 0x0e, 0x02, 0x00,
11334 0x18, 0x3c, 0x7e, 0x18, 0x18, 0x7e, 0x3c, 0x18,
11335 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00,
11336 0x7f, 0xdb, 0xdb, 0x7b, 0x1b, 0x1b, 0x1b, 0x00,
11337 0x3e, 0x63, 0x38, 0x6c, 0x6c, 0x38, 0xcc, 0x78,
11338 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x7e, 0x00,
11339 0x18, 0x3c, 0x7e, 0x18, 0x7e, 0x3c, 0x18, 0xff,
11340 0x18, 0x3c, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x00,
11341 0x18, 0x18, 0x18, 0x18, 0x7e, 0x3c, 0x18, 0x00,
11342 0x00, 0x18, 0x0c, 0xfe, 0x0c, 0x18, 0x00, 0x00,
11343 0x00, 0x30, 0x60, 0xfe, 0x60, 0x30, 0x00, 0x00,
11344 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xfe, 0x00, 0x00,
11345 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00,
11346 0x00, 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x00, 0x00,
11347 0x00, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00,
11348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11349 0x30, 0x78, 0x78, 0x30, 0x30, 0x00, 0x30, 0x00,
11350 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00,
11351 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00,
11352 0x30, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x30, 0x00,
11353 0x00, 0xc6, 0xcc, 0x18, 0x30, 0x66, 0xc6, 0x00,
11354 0x38, 0x6c, 0x38, 0x76, 0xdc, 0xcc, 0x76, 0x00,
11355 0x60, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
11356 0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,
11357 0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,
11358 0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00,
11359 0x00, 0x30, 0x30, 0xfc, 0x30, 0x30, 0x00, 0x00,
11360 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x60,
11361 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00,
11362 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00,
11363 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x80, 0x00,
11364 0x7c, 0xc6, 0xce, 0xde, 0xf6, 0xe6, 0x7c, 0x00,
11365 0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00,
11366 0x78, 0xcc, 0x0c, 0x38, 0x60, 0xcc, 0xfc, 0x00,
11367 0x78, 0xcc, 0x0c, 0x38, 0x0c, 0xcc, 0x78, 0x00,
11368 0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x1e, 0x00,
11369 0xfc, 0xc0, 0xf8, 0x0c, 0x0c, 0xcc, 0x78, 0x00,
11370 0x38, 0x60, 0xc0, 0xf8, 0xcc, 0xcc, 0x78, 0x00,
11371 0xfc, 0xcc, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00,
11372 0x78, 0xcc, 0xcc, 0x78, 0xcc, 0xcc, 0x78, 0x00,
11373 0x78, 0xcc, 0xcc, 0x7c, 0x0c, 0x18, 0x70, 0x00,
11374 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00,
11375 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x60,
11376 0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x00,
11377 0x00, 0x00, 0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00,
11378 0x60, 0x30, 0x18, 0x0c, 0x18, 0x30, 0x60, 0x00,
11379 0x78, 0xcc, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00,
11380 0x7c, 0xc6, 0xde, 0xde, 0xde, 0xc0, 0x78, 0x00,
11381 0x30, 0x78, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0x00,
11382 0xfc, 0x66, 0x66, 0x7c, 0x66, 0x66, 0xfc, 0x00,
11383 0x3c, 0x66, 0xc0, 0xc0, 0xc0, 0x66, 0x3c, 0x00,
11384 0xf8, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0xf8, 0x00,
11385 0xfe, 0x62, 0x68, 0x78, 0x68, 0x62, 0xfe, 0x00,
11386 0xfe, 0x62, 0x68, 0x78, 0x68, 0x60, 0xf0, 0x00,
11387 0x3c, 0x66, 0xc0, 0xc0, 0xce, 0x66, 0x3e, 0x00,
11388 0xcc, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0xcc, 0x00,
11389 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11390 0x1e, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78, 0x00,
11391 0xe6, 0x66, 0x6c, 0x78, 0x6c, 0x66, 0xe6, 0x00,
11392 0xf0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xfe, 0x00,
11393 0xc6, 0xee, 0xfe, 0xfe, 0xd6, 0xc6, 0xc6, 0x00,
11394 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00,
11395 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00,
11396 0xfc, 0x66, 0x66, 0x7c, 0x60, 0x60, 0xf0, 0x00,
11397 0x78, 0xcc, 0xcc, 0xcc, 0xdc, 0x78, 0x1c, 0x00,
11398 0xfc, 0x66, 0x66, 0x7c, 0x6c, 0x66, 0xe6, 0x00,
11399 0x78, 0xcc, 0xe0, 0x70, 0x1c, 0xcc, 0x78, 0x00,
11400 0xfc, 0xb4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11401 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xfc, 0x00,
11402 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
11403 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00,
11404 0xc6, 0xc6, 0x6c, 0x38, 0x38, 0x6c, 0xc6, 0x00,
11405 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x30, 0x78, 0x00,
11406 0xfe, 0xc6, 0x8c, 0x18, 0x32, 0x66, 0xfe, 0x00,
11407 0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,
11408 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, 0x00,
11409 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
11410 0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00,
11411 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
11412 0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
11413 0x00, 0x00, 0x78, 0x0c, 0x7c, 0xcc, 0x76, 0x00,
11414 0xe0, 0x60, 0x60, 0x7c, 0x66, 0x66, 0xdc, 0x00,
11415 0x00, 0x00, 0x78, 0xcc, 0xc0, 0xcc, 0x78, 0x00,
11416 0x1c, 0x0c, 0x0c, 0x7c, 0xcc, 0xcc, 0x76, 0x00,
11417 0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x78, 0x00,
11418 0x38, 0x6c, 0x60, 0xf0, 0x60, 0x60, 0xf0, 0x00,
11419 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
11420 0xe0, 0x60, 0x6c, 0x76, 0x66, 0x66, 0xe6, 0x00,
11421 0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
11422 0x0c, 0x00, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78,
11423 0xe0, 0x60, 0x66, 0x6c, 0x78, 0x6c, 0xe6, 0x00,
11424 0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
11425 0x00, 0x00, 0xcc, 0xfe, 0xfe, 0xd6, 0xc6, 0x00,
11426 0x00, 0x00, 0xf8, 0xcc, 0xcc, 0xcc, 0xcc, 0x00,
11427 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x00,
11428 0x00, 0x00, 0xdc, 0x66, 0x66, 0x7c, 0x60, 0xf0,
11429 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0x1e,
11430 0x00, 0x00, 0xdc, 0x76, 0x66, 0x60, 0xf0, 0x00,
11431 0x00, 0x00, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x00,
11432 0x10, 0x30, 0x7c, 0x30, 0x30, 0x34, 0x18, 0x00,
11433 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x76, 0x00,
11434 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
11435 0x00, 0x00, 0xc6, 0xd6, 0xfe, 0xfe, 0x6c, 0x00,
11436 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00,
11437 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
11438 0x00, 0x00, 0xfc, 0x98, 0x30, 0x64, 0xfc, 0x00,
11439 0x1c, 0x30, 0x30, 0xe0, 0x30, 0x30, 0x1c, 0x00,
11440 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,
11441 0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00,
11442 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11443 0x00, 0x10, 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0x00,
11448 bios_table_area_end
:
11449 // bcc-generated data will be placed here