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

Last change on this file since 605 was 605, checked in by krille_n_, 3 years ago

Changes:

  • The "Remove other hard drives" option in the Boot settings menu in XTIDECFG is now exposed in all BIOS builds. This is needed because the system BIOS in at least two Zenith computer models (Z-161 and Z-171) does not clear the BDA HD count which causes it to increment on warm boot. Running "Auto Configure" in XTIDECFG now also tries to identify these machines by doing a CRC check on the system BIOS and sets the option to YES if a match is found.
  • WORD_ALIGN is now 2 for XT builds. This should benefit XT class machines with 8086 and NEC V30 CPU:s and the cost is negligible (1 byte for the XT BIOS builds and 12 bytes for XTIDECFG.COM).
  • Other minor optimizations.
File size: 11.6 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     SHORT .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      SHORT .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     SHORT .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; This is a hack to get Windows 9x to load its built-in protected mode IDE driver.
99;
100; The Windows hack has two parts. First part is to try to alter CMOS address 12h as that
101; is what Windows 9x driver reads to detect IDE drives. Altering is not possible on all
102; systems since CMOS has a checksum but its location is not standardized. We will first
103; try to detect valid checksum. If it succeeds, then it is safe to assume this system
104; has compatible CMOS and we can alter it.
105; If verify fails, we do the more dirty hack to zero BDA drive count. Then Windows 9x works
106; as long as user has configured at least one drive in the BIOS setup.
107%ifdef MODULE_WIN9X_CMOS_HACK
108    mov     dl, HARD_DISK_TYPES
109    call    CMOS_ReadFromIndexInDLtoAL
110    test    al, 0F0h
111    jnz     SHORT .ClearBdaDriveCount       ; CMOS byte 12h is ready for Windows 9x
112    call    CMOS_Verify10hTo2Dh             ; Can we modify CMOS?
113    jnz     SHORT .ClearBdaDriveCount       ; Unsupported BIOS, use plan B
114
115    ; Now we can alter CMOS location 12h. Award BIOS locks if we set drive 0 type to Fh
116    ; (but accept changes to drive type 1). Windows 9x requires that the drive 0 type is
117    ; non zero and ignores drive 1 type. So if we only set drive 1, then Award BIOS
118    ; won't give problems but Windows 9x stays in MS-DOS compatibility mode.
119    ;
120    ; For Award BIOSes we could set the Drive 0 type to 1 and then clear the BDA drive count.
121    ; So essentially we could automatically do what user needs to do manually to get Windows 9x
122    ; working on Award BIOSes. However, I think that should be left to do manually since
123    ; there may be SCSI drives on the system or FLG_ROMVARS_CLEAR_BDA_HD_COUNT could
124    ; be intentionally cleared and forcing the dummy drive might cause only trouble.
125
126    ; Try to detect Award BIOS (Seems to work on a tested 128k BIOS so hopefully
127    ; there will be no need to scan E000h segment)
128    mov     cx, 65536 - 4
129    mov     eax, 'Awar'                     ; Four characters should be enough
130    mov     di, 0F000h                      ; Scan 64k starting from segment F000h
131    mov     fs, di                          ; No need to preserve FS since we set it to zero soon when we boot
132    xor     di, di
133.ScanNextCharacters:
134    cmp     [fs:di], eax
135    je      SHORT .ClearBdaDriveCount       ; Award detected, cannot modify CMOS
136    inc     di                              ; Increment offset by one character (not four)
137    loop    .ScanNextCharacters
138
139    ; Now it should be safe to write
140    mov     dl, HARD_DISK_TYPES
141    mov     al, 0F0h                        ; Drive 0 type 16...47 (supposed to be defined elsewhere in the CMOS)
142    call    CMOS_WriteALtoIndexInDL
143    call    CMOS_StoreNewChecksumFor10hto2Dh
144.ClearBdaDriveCount:
145%endif ; MODULE_WIN9X_CMOS_HACK
146
147    mov     cx, [RAMVARS.wDrvCntAndFlopCnt] ; Our count of hard disks
148    mov     al, [es:BDA.bHDCount]
149%ifndef MODULE_MFM_COMPATIBILITY
150    ; This is excluded when MODULE_MFM_COMPATIBILITY is included because it doesn't make sense to use both at the same time anyway.
151    ; Note that the option to "Remove other hard drives" is still visible in XTIDECFG.COM for builds including MODULE_MFM_COMPATIBILITY.
152    ; Changing that option just won't do anything. We might want to remove the option for builds that contain MODULE_MFM_COMPATIBILITY
153    ; but that would require a ROMVARS flag that is probably better spent on other things.
154    test    BYTE [cs:ROMVARS.wFlags], FLG_ROMVARS_CLEAR_BDA_HD_COUNT    ; Clears CF
155    jz      SHORT .ContinueInitialization
156%ifdef USE_UNDOC_INTEL
157    salc
158%else
159    xor     al, al
160%endif
161.ContinueInitialization:
162%endif ; MODULE_MFM_COMPATIBILITY
163    add     cl, al
164    mov     [es:BDA.bHDCount], cl           ; Update the system count
165    or      al, 80h                         ; Or in hard disk flag
166    mov     [RAMVARS.bFirstDrv], al         ; Store first drive number
167
168.AddFloppies:
169%ifdef MODULE_SERIAL_FLOPPY
170;----------------------------------------------------------------------
171;
172; Add in any emulated serial floppy drives, finalize our packed Count and First variables
173;
174    dec     ch
175    mov     al, ch
176    js      SHORT .NoFloppies               ; If no drives are present, we store 0FFh
177
178    call    FloppyDrive_GetCountFromBIOS_or_BDA
179
180    push    ax
181
182    add     al, ch                          ; Add our drives to existing drive count
183    cmp     al, 3                           ; For BDA, max out at 4 drives (ours is zero based)
184    jb      SHORT .MaxBDAFloppiesExceeded
185    mov     al, 3
186.MaxBDAFloppiesExceeded:
187    eROR_IM al, 2                           ; move to bits 6-7
188    inc     ax                              ; low order bit, indicating floppy drive exists
189
190    mov     ah, 3Eh                         ; AND mask to AH (all bits set except floppy drive count/present)
191    and     ah, [es:BDA.wEquipment]         ; Load Equipment WORD low byte and mask off drive number and drives present bit
192    or      al, ah                          ; Or in new values
193    mov     [es:BDA.wEquipment], al         ; and store
194
195    mov     al, 1Eh                         ; BDA pointer to Floppy DPT
196    mov     si, AH8h_FloppyDPT
197    call    Interrupts_InstallHandlerToVectorInALFromCSSI
198
199    pop     ax
200
201    shr     ch, 1                           ; number of drives, 1 or 2 only, to CF flag (clear=1, set=2)
202    eRCL_IM al, 1                           ; starting drive number in upper 7 bits, number of drives in low bit
203.NoFloppies:
204    mov     [RAMVARS.xlateVars+XLATEVARS.bFlopCntAndFirst], al
205%endif
206    ret
207
208%ifndef CHECK_FOR_UNUSED_ENTRYPOINTS
209    %if FLG_ROMVARS_SERIAL_SCANDETECT != 8
210        %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."
211    %endif
212%endif
213
214
215;--------------------------------------------------------------------
216; StartDetectionWithDriveSelectByteInBHandStringInCX
217;   Parameters:
218;       BH:     Drive Select byte for Drive and Head Register
219;       CX:     Offset to "Master" or "Slave" string
220;       CS:BP:  Ptr to IDEVARS for the drive
221;       DS:     RAMVARS segment
222;       ES:     Zero (BDA segment)
223;   Returns:
224;       Nothing
225;   Corrupts registers:
226;       AX, BL, CX, DX, SI, DI
227;--------------------------------------------------------------------
228StartDetectionWithDriveSelectByteInBHandStringInCX:
229    call    DetectPrint_StartDetectWithMasterOrSlaveStringInCXandIdeVarsInCSBP
230    ; Fall to .ReadAtaInfoFromHardDisk
231
232
233;--------------------------------------------------------------------
234; .ReadAtaInfoFromHardDisk
235;   Parameters:
236;       BH:     Drive Select byte for Drive and Head Register
237;       DX:     Autodetected port (for devices that support autodetection)
238;       CS:BP:  Ptr to IDEVARS for the drive
239;       DS:     RAMVARS segment
240;       ES:     Zero (BDA segment)
241;   Returns:
242;       CF:     Cleared if ATA-information read successfully
243;               Set if any error
244;   Corrupts registers:
245;       AX, BL, CX, DX, SI, DI
246;--------------------------------------------------------------------
247.ReadAtaInfoFromHardDisk:
248    mov     si, BOOTVARS.rgbAtaInfo         ; ES:SI now points to ATA info location
249    push    es
250    push    si
251    push    dx
252    push    bx
253    call    Device_IdentifyToBufferInESSIwithDriveSelectByteInBH
254    pop     bx
255    pop     dx
256    pop     si
257    pop     es
258    jnc     SHORT CreateBiosTablesForHardDisk
259    ; Fall to .ReadAtapiInfoFromDrive
260
261.ReadAtapiInfoFromDrive:                    ; Not yet implemented
262    ;call   ReadAtapiInfoFromDrive          ; Assume CD-ROM
263    ;jnc    SHORT _CreateBiosTablesForCDROM
264
265    ;jmp    short DetectDrives_DriveNotFound
266;;; fall-through instead of previous jmp instruction
267;--------------------------------------------------------------------
268; DetectDrives_DriveNotFound
269;   Parameters:
270;       Nothing
271;   Returns:
272;       CF:     Set (from DetectPrint_NullTerminatedStringFromCSSIandSetCF)
273;   Corrupts registers:
274;       AX, SI
275;--------------------------------------------------------------------
276DetectDrives_DriveNotFound:
277    mov     si, g_szNotFound
278    jmp     DetectPrint_NullTerminatedStringFromCSSIandSetCF
279
280
281;--------------------------------------------------------------------
282; CreateBiosTablesForHardDisk
283;   Parameters:
284;       BH:     Drive Select byte for Drive and Head Register
285;       DX:     Autodetected port (for devices that support autodetection)
286;       CS:BP:  Ptr to IDEVARS for the drive
287;       ES:SI   Ptr to ATA information for the drive
288;       DS:     RAMVARS segment
289;       ES:     BDA segment
290;   Returns:
291;       Nothing
292;   Corrupts registers:
293;       AX, BX, CX, DX, SI, DI
294;--------------------------------------------------------------------
295CreateBiosTablesForHardDisk:
296%ifndef NO_ATAID_VALIDATION
297    push    bx
298    call    AtaID_VerifyFromESSI
299    pop     bx
300    jnz     SHORT DetectDrives_DriveNotFound
301%endif
302    call    CreateDPT_FromAtaInformation
303    jc      SHORT DetectDrives_DriveNotFound
304    call    DriveDetectInfo_CreateForHardDisk
305    jmp     SHORT DetectPrint_DriveNameFromDrvDetectInfoInESBX
Note: See TracBrowser for help on using the repository browser.