source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Initialization/DetectDrives.asm @ 599

Last change on this file since 599 was 599, checked in by aitotat, 6 years ago

Hotkey bar is now updated and drawn from system timer tick handler 1Ch. This gives much more responsive key input and makes possible to implement some simple detection animation to show that system has not frozen.

File size: 11.2 KB
Line 
1; Project name  :   XTIDE Universal BIOS
2; Description   :   Functions for detecting drive for the BIOS.
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
7;
8; This program is free software; you can redistribute it and/or modify
9; it under the terms of the GNU General Public License as published by
10; the Free Software Foundation; either version 2 of the License, or
11; (at your option) any later version.
12;
13; This program is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16; GNU General Public License for more details.
17; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18;
19
20; Section containing code
21SECTION .text
22
23;--------------------------------------------------------------------
24; Detects all IDE hard disks to be controlled by this BIOS.
25;
26; DetectDrives_FromAllIDEControllers
27;   Parameters:
28;       DS:     RAMVARS segment
29;       ES:     BDA segment (zero)
30;   Returns:
31;       Nothing
32;   Corrupts registers:
33;       All (not segments)
34;--------------------------------------------------------------------
35DetectDrives_FromAllIDEControllers:
36    call    RamVars_GetIdeControllerCountToCX
37    mov     bp, ROMVARS.ideVars0            ; CS:BP now points to first IDEVARS
38
39.DriveDetectLoop:                           ; Loop through IDEVARS
40    push    cx
41
42    mov     cx, g_szDetectMaster
43    mov     bh, MASK_DRVNHEAD_SET                               ; Select Master drive
44    call    StartDetectionWithDriveSelectByteInBHandStringInCX  ; Detect and create DPT + BOOTNFO
45
46    mov     cx, g_szDetectSlave
47    mov     bh, MASK_DRVNHEAD_SET | FLG_DRVNHEAD_DRV
48    call    StartDetectionWithDriveSelectByteInBHandStringInCX
49
50%ifdef MODULE_HOTKEYS
51%ifdef MODULE_SERIAL
52    ; This is only needed for hotkey F6 (ComDtct) to work
53    call    ScanHotkeysFromKeyBufferAndStoreToBootvars          ; Done here while CX is still protected
54%endif
55%endif
56
57    pop     cx
58
59    add     bp, BYTE IDEVARS_size           ; Point to next IDEVARS
60
61%ifdef MODULE_SERIAL
62    jcxz    .AddHardDisks                   ; Set to zero on .ideVarsSerialAuto iteration (if any)
63%endif
64    loop    .DriveDetectLoop
65
66%ifdef MODULE_SERIAL
67;----------------------------------------------------------------------
68;
69; if serial drive detected, do not scan (avoids duplicate drives and isn't needed - we already have a connection)
70;
71    call    FindDPT_ToDSDIforSerialDevice   ; Does not modify AX
72    jnc     .AddHardDisks
73
74    mov     bp, ROMVARS.ideVarsSerialAuto   ; Point to our special IDEVARS structure, just for serial scans
75
76%ifdef MODULE_HOTKEYS
77    cmp     al, COM_DETECT_HOTKEY_SCANCODE  ; Set by last call to ScanHotkeysFromKeyBufferAndStoreToBootvars above
78    je      .DriveDetectLoop
79%endif
80
81    mov     al, [cs:ROMVARS.wFlags]         ; Configurator set to always scan?
82    or      al, [es:BDA.bKBFlgs1]           ; Or, did the user hold down the ALT key?
83    and     al, 8                           ; 8 = alt key depressed, same as FLG_ROMVARS_SERIAL_SCANDETECT
84    jnz     .DriveDetectLoop
85%endif ; MODULE_SERIAL
86
87.AddHardDisks:
88;----------------------------------------------------------------------
89;
90; Add in hard disks to BDA, finalize our Count and First variables
91;
92; Note that we perform the add to bHDCount and store bFirstDrv even if the count is zero.
93; This is done because we use the value of .bFirstDrv to know how many drives were in the system
94; at the time of boot, and to return that number on int13h/8h calls.  Because the count is zero,
95; FindDPT_ForDriveNumber will not find any drives that are ours.
96;
97
98; Here we might want to replace BIOS configured drives with the ones we detected.
99; Primary reason is to support dynamic overlay feature in the future. Second reason
100; is a hack to get Windows 95 load proper IDE drivers.
101;
102; The Windows hack has two parts. First part is to try to alter CMOS address 12h as that
103; is what Windows 95 driver reads to detect IDE drives. Altering is not possible on all
104; systems since CMOS has a checksum but its location is not standardized. We will first
105; try to detect valid checksum. If it succeeds, then it is safe to assume this system
106; has compatible CMOS and we can alter it.
107; If verify fails, we do the more dirty hack to zero BDA drive count. Then Windows 95 works
108; as long as user has configured at least one drive in the BIOS setup.
109
110%ifdef USE_AT   ; FLG_ROMVARS_IGNORE_MOTHERBOARD_DRIVES is for AT builds only
111
112    %ifdef MODULE_WIN95_CMOS_HACK
113        mov     dl, HARD_DISK_TYPES
114        call    CMOS_ReadFromIndexInDLtoAL
115        test    al, 0F0h
116        jnz     SHORT .ClearBdaDriveCount       ; CMOS byte 12h is ready for Windows 95
117        call    CMOS_Verify10hTo2Dh             ; Can we modify CMOS?
118        jnz     SHORT .ClearBdaDriveCount       ; Unsupported BIOS, use plan B
119
120        ; Now we can alter CMOS location 12h. Award BIOS locks if we set drive 0 type to Fh
121        ; (but accept changes to drive type 1). Windows 95 requires that the drive 0 type is
122        ; non zero and ignores drive 1 type. So if we only set drive 1, then Award BIOS
123        ; won't give problems but Windows 95 stays in MS-DOS compatibility mode.
124        ;
125        ; For Award BIOSes we could set the Drive 0 type to 1 and then clear the BDA drive count.
126        ; So essentially we could automatically do what user needs to do manually to get Windows 95
127        ; working on Award BIOSes. However, I think that should be left to do manually since
128        ; there may be SCSI drives on the system or FLG_ROMVARS_IGNORE_MOTHERBOARD_DRIVES could
129        ; be intentionally cleared and forcing the dummy drive might cause only trouble.
130
131        ; Try to detect Award BIOS (Seems to work on a tested 128k BIOS so hopefully
132        ; there will be no need to scan E000h segment)
133        mov     cx, 65536 - 4
134        mov     eax, 'Awar'     ; Four characters should be enough
135        mov     di, 0F000h      ; Scan 64k starting from segment F000h
136        mov     fs, di          ; No need to preserve FS since we set it to zero soon when we boot
137        xor     di, di
138    .ScanNextCharacters:
139        cmp     [fs:di], eax
140        je      SHORT .ClearBdaDriveCount   ; Award detected, cannot modify CMOS
141        inc     di              ; Increment offset by one character (not four)
142        loop    .ScanNextCharacters
143
144        ; Now it should be safe to write
145        mov     dl, HARD_DISK_TYPES
146        mov     al, 0F0h    ; Drive 0 type 16...47 (supposed to be defined elsewhere in the CMOS)
147        call    CMOS_WriteALtoIndexInDL
148        call    CMOS_StoreNewChecksumFor10hto2Dh
149.ClearBdaDriveCount:
150    %endif  ; MODULE_WIN95_CMOS_HACK
151
152    test    BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_IGNORE_MOTHERBOARD_DRIVES
153    jz      SHORT .ContinueInitialization
154    mov     BYTE [es:BDA.bHDCount], 0   ; Set hard disk count to zero
155.ContinueInitialization:
156%endif
157
158    mov     cx, [RAMVARS.wDrvCntAndFlopCnt]     ; Our count of hard disks
159    mov     al, [es:BDA.bHDCount]
160    add     [es:BDA.bHDCount], cl       ; Add our drives to the system count
161    or      al, 80h                     ; Or in hard disk flag
162    mov     [RAMVARS.bFirstDrv], al     ; Store first drive number
163
164.AddFloppies:
165%ifdef MODULE_SERIAL_FLOPPY
166;----------------------------------------------------------------------
167;
168; Add in any emulated serial floppy drives, finalize our packed Count and First variables
169;
170    dec     ch
171    mov     al, ch
172    js      .NoFloppies                     ; if no drives are present, we store 0ffh
173
174    call    FloppyDrive_GetCountFromBIOS_or_BDA
175
176    push    ax
177
178    add     al, ch                          ; Add our drives to existing drive count
179    cmp     al, 3                           ; For BDA, max out at 4 drives (ours is zero based)
180    jb      .MaxBDAFloppiesExceeded
181    mov     al, 3
182.MaxBDAFloppiesExceeded:
183    eROR_IM al, 2                           ; move to bits 6-7
184    inc     ax                              ; low order bit, indicating floppy drive exists
185
186    mov     ah, 3Eh                         ; AND mask to AH (all bits set except floppy drive count/present)
187    and     ah, [es:BDA.wEquipment]         ; Load Equipment WORD low byte and mask off drive number and drives present bit
188    or      al, ah                          ; Or in new values
189    mov     [es:BDA.wEquipment], al         ; and store
190
191    mov     al, 1Eh                         ; BDA pointer to Floppy DPT
192    mov     si, AH8h_FloppyDPT
193    call    Interrupts_InstallHandlerToVectorInALFromCSSI
194
195    pop     ax
196
197    shr     ch, 1                           ; number of drives, 1 or 2 only, to CF flag (clear=1, set=2)
198    eRCL_IM al, 1                           ; starting drive number in upper 7 bits, number of drives in low bit
199.NoFloppies:
200    mov     [RAMVARS.xlateVars+XLATEVARS.bFlopCntAndFirst], al
201%endif
202    ret
203
204%ifndef CHECK_FOR_UNUSED_ENTRYPOINTS
205    %if FLG_ROMVARS_SERIAL_SCANDETECT != 8
206        %error "DetectDrives is currently coded to assume that FLG_ROMVARS_SERIAL_SCANDETECT is the same bit as the ALT key code in the BDA.  Changes in the code will be needed if these values are no longer the same."
207    %endif
208%endif
209
210
211;--------------------------------------------------------------------
212; StartDetectionWithDriveSelectByteInBHandStringInCX
213;   Parameters:
214;       BH:     Drive Select byte for Drive and Head Register
215;       CX:     Offset to "Master" or "Slave" string
216;       CS:BP:  Ptr to IDEVARS for the drive
217;       DS:     RAMVARS segment
218;       ES:     Zero (BDA segment)
219;   Returns:
220;       Nothing
221;   Corrupts registers:
222;       AX, BL, CX, DX, SI, DI
223;--------------------------------------------------------------------
224StartDetectionWithDriveSelectByteInBHandStringInCX:
225    call    DetectPrint_StartDetectWithMasterOrSlaveStringInCXandIdeVarsInCSBP
226    ; Fall to .ReadAtaInfoFromHardDisk
227
228
229;--------------------------------------------------------------------
230; .ReadAtaInfoFromHardDisk
231;   Parameters:
232;       BH:     Drive Select byte for Drive and Head Register
233;       DX:     Autodetected port (for devices that support autodetection)
234;       CS:BP:  Ptr to IDEVARS for the drive
235;       DS:     RAMVARS segment
236;       ES:     Zero (BDA segment)
237;   Returns:
238;       CF:     Cleared if ATA-information read successfully
239;               Set if any error
240;   Corrupts registers:
241;       AX, BL, CX, DX, SI, DI
242;--------------------------------------------------------------------
243.ReadAtaInfoFromHardDisk:
244    mov     si, BOOTVARS.rgbAtaInfo     ; ES:SI now points to ATA info location
245    push    es
246    push    si
247    push    dx
248    push    bx
249    call    Device_IdentifyToBufferInESSIwithDriveSelectByteInBH
250    pop     bx
251    pop     dx
252    pop     si
253    pop     es
254    jnc     SHORT CreateBiosTablesForHardDisk
255    ; Fall to .ReadAtapiInfoFromDrive
256
257.ReadAtapiInfoFromDrive:                ; Not yet implemented
258    ;call   ReadAtapiInfoFromDrive      ; Assume CD-ROM
259    ;jnc    SHORT _CreateBiosTablesForCDROM
260
261    ;jmp    short DetectDrives_DriveNotFound
262;;; fall-through instead of previous jmp instruction
263;--------------------------------------------------------------------
264; DetectDrives_DriveNotFound
265;   Parameters:
266;       Nothing
267;   Returns:
268;       CF:     Set (from DetectPrint_NullTerminatedStringFromCSSIandSetCF)
269;   Corrupts registers:
270;       AX, SI
271;--------------------------------------------------------------------
272DetectDrives_DriveNotFound:
273    mov     si, g_szNotFound
274    jmp     DetectPrint_NullTerminatedStringFromCSSIandSetCF
275
276
277;--------------------------------------------------------------------
278; CreateBiosTablesForHardDisk
279;   Parameters:
280;       BH:     Drive Select byte for Drive and Head Register
281;       DX:     Autodetected port (for devices that support autodetection)
282;       CS:BP:  Ptr to IDEVARS for the drive
283;       ES:SI   Ptr to ATA information for the drive
284;       DS:     RAMVARS segment
285;       ES:     BDA segment
286;   Returns:
287;       Nothing
288;   Corrupts registers:
289;       AX, BX, CX, DX, SI, DI
290;--------------------------------------------------------------------
291CreateBiosTablesForHardDisk:
292%ifndef NO_ATAID_VALIDATION
293    push    bx
294    call    AtaID_VerifyFromESSI
295    pop     bx
296    jnz     SHORT DetectDrives_DriveNotFound
297%endif
298    call    CreateDPT_FromAtaInformation
299    jc      SHORT DetectDrives_DriveNotFound
300    call    DriveDetectInfo_CreateForHardDisk
301    jmp     SHORT DetectPrint_DriveNameFromDrvDetectInfoInESBX
Note: See TracBrowser for help on using the repository browser.