source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Initialization/AtaID.asm @ 542

Last change on this file since 542 was 542, checked in by aitotat@…, 11 years ago

Changes to XTIDE Universal BIOS:

  • Fixed a bug that allowed EBIOS functions for user defined CHS.
  • Simplified user defined CHS and LBA setup a little.
File size: 8.7 KB
Line 
1; Project name  :   XTIDE Universal BIOS
2; Description   :   Functions for accessing ATA information read with
3;                   IDENTIFY DEVICE command.
4
5;
6; XTIDE Universal BIOS and Associated Tools
7; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
8;
9; This program is free software; you can redistribute it and/or modify
10; it under the terms of the GNU General Public License as published by
11; the Free Software Foundation; either version 2 of the License, or
12; (at your option) any later version.
13;
14; This program is distributed in the hope that it will be useful,
15; but WITHOUT ANY WARRANTY; without even the implied warranty of
16; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17; GNU General Public License for more details.
18; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19;
20
21; Section containing code
22SECTION .text
23
24;--------------------------------------------------------------------
25; AtaID_VerifyFromESSI
26;   Parameters:
27;       ES:SI:  Ptr to 512-byte ATA information read from the drive
28;   Returns:
29;       CF:     Set if failed to verify ATA-ID
30;               Cleared if ATA-ID verified successfully
31;   Corrupts registers:
32;       AX, BX, CX
33;--------------------------------------------------------------------
34AtaID_VerifyFromESSI:
35    ; We cannot start by reading ATA version since the ID might be
36    ; corrupted. We start by making sure P-CHS values are valid.
37    ; If they are, we assume the ATA ID to be valid. Fortunately we can do
38    ; further checking for ATA-5 and later since they contain signature and
39    ; checksum bytes. Those are not available for ATA-4 and older.
40
41    ; Verify P-CHS cylinders
42    mov     bx, ATA1.wCylCnt
43    mov     cx, MAX_VALID_PCHS_CYLINDERS
44    call    .CompareCHorSfromOffsetBXtoMaxValueInCX
45
46    mov     bl, ATA1.wHeadCnt & 0FFh
47    mov     cx, MAX_VALID_PCHS_HEADS
48    call    .CompareCHorSfromOffsetBXtoMaxValueInCX
49
50    mov     bl, ATA1.wSPT & 0FFh
51    mov     cl, MAX_VALID_PCHS_SECTORS_PER_TRACK
52    call    .CompareCHorSfromOffsetBXtoMaxValueInCX
53
54    ; Check signature byte. It is only found on ATA-5 and later. It should be zero on
55    ; ATA-4 and older.
56    mov     al, [es:si+ATA6.bSignature]
57    test    al, al
58    jz      SHORT .AtaIDverifiedSuccessfully    ; Old ATA so Signature and Checksum is not available
59    cmp     al, A6_wIntegrity_SIGNATURE
60    jne     SHORT .FailedToVerifyAtaID
61
62    ; Check checksum byte since signature was present
63    mov     cx, ATA6_size
64    call    Memory_SumCXbytesFromESSItoAL       ; Returns with ZF set according to result
65    jnz     SHORT .FailedToVerifyAtaID
66
67    ; ATA-ID is now verified to be valid
68.AtaIDverifiedSuccessfully:
69    clc
70    ret
71
72;--------------------------------------------------------------------
73; .CompareCHorSfromOffsetBXtoMaxValueInCX
74;   Parameters:
75;       BX:     C, H or S offset to ATA-ID
76;       CX:     Maximum valid C, H or S value
77;       ES:SI:  Ptr to 512-byte ATA information read from the drive
78;   Returns:
79;       Exits from AtaID_VerifyFromESSI with CF set if invalid value
80;   Corrupts registers:
81;       AX
82;--------------------------------------------------------------------
83.CompareCHorSfromOffsetBXtoMaxValueInCX:
84    mov     ax, [es:bx+si]
85    test    ax, ax
86    jz      SHORT .InvalidPCHorSinOffsetBX
87    cmp     ax, cx          ; Compare to max valid value
88    jbe     SHORT .ValidPCHorSinOffsetBX
89.InvalidPCHorSinOffsetBX:
90    add     sp, BYTE 2      ; Clear return address for this function
91.FailedToVerifyAtaID:
92    stc                     ; Set carry to indicate invalid ATA-ID
93.ValidPCHorSinOffsetBX:
94    ret
95
96
97;--------------------------------------------------------------------
98; Writes user defined limits from ROMVARS to ATA ID read from the drive.
99; Modifying the ATA ID reduces code and possibilites for bugs since
100; only little furher checks are needed elsewhere.
101;
102; AtaID_ModifyESSIforUserDefinedLimitsAndReturnTranslateModeInDX
103;   Parameters:
104;       DS:DI:  Ptr to incomplete Disk Parameter Table
105;       ES:SI:  Ptr to 512-byte ATA information read from the drive
106;       CS:BP:  Ptr to IDEVARS for the controller
107;   Returns:
108;       DX:     User defined P-CHS to L-CHS translate mode
109;   Corrupts registers:
110;       AX, BX, CX
111;--------------------------------------------------------------------
112AtaID_ModifyESSIforUserDefinedLimitsAndReturnTranslateModeInDX:
113    call    AccessDPT_GetPointerToDRVPARAMStoCSBX
114    push    ds
115    push    es
116    pop     ds      ; DS:SI now points to ATA information
117
118    ; Load User Defined CHS or LBA to CX:AX
119    mov     dx, [cs:bx+DRVPARAMS.wFlags]
120    mov     ax, [cs:bx+DRVPARAMS.wCylinders]        ; Or .dwMaximumLBA
121    mov     cx, [cs:bx+DRVPARAMS.wHeadsAndSectors]  ; Or .dwMaximumLBA+2
122
123    ; * User defined CHS *
124    test    dl, FLG_DRVPARAMS_USERCHS
125    jz      SHORT .NoUserDefinedCHS
126
127    ; Apply new CHS and disable LBA (we also want to set CHS addressing)
128    mov     [si+ATA1.wCylCnt], ax
129    eMOVZX  ax, cl
130    mov     [si+ATA1.wHeadCnt], ax
131    mov     al, ch
132    mov     [si+ATA1.wSPT], ax
133    and     BYTE [si+ATA1.wCaps+1], ~(A1_wCaps_LBA>>8)
134    and     BYTE [si+ATA6.wSetSup83+1], ~(A6_wSetSup83_LBA48>>8)
135.NoUserDefinedCHS:
136
137    ; * User defined LBA *
138    test    dl, FLG_DRVPARAMS_USERLBA
139    jz      SHORT .NoUserDefinedLBA
140
141    ; Apply new LBA and disable LBA48
142    cmp     cx, [si+ATA1.dwLBACnt+2]
143    ja      SHORT .NoUserDefinedLBA     ; Do not set larger than drive
144    jb      SHORT .StoreNewLBA
145    cmp     ax, [si+ATA1.dwLBACnt]
146    ja      SHORT .NoUserDefinedLBA     ; Allow same size to disable LBA48
147.StoreNewLBA:
148    mov     [si+ATA1.dwLBACnt], ax
149    mov     [si+ATA1.dwLBACnt+2], cx
150    and     BYTE [si+ATA6.wSetSup83+1], ~(A6_wSetSup83_LBA48>>8)
151.NoUserDefinedLBA:
152
153    ; * Disable Block Mode transfers *
154    test    dl, FLG_DRVPARAMS_BLOCKMODE
155    jnz     SHORT .NoNeedToDisableBlockMode
156    mov     BYTE [si+ATA1.bBlckSize], 1 ; sectors
157.NoNeedToDisableBlockMode:
158
159    ; * Load P-CHS to L-CHS translate mode to DX *
160    and     dx, BYTE MASK_DRVPARAMS_TRANSLATEMODE
161    eSHR_IM dx, TRANSLATEMODE_FIELD_POSITION
162
163    pop     ds
164    ret
165
166
167%ifdef MODULE_ADVANCED_ATA
168;--------------------------------------------------------------------
169; AtaID_GetMaxPioModeToAXandMinCycleTimeToCX
170;   Parameters:
171;       ES:SI:  Ptr to 512-byte ATA information read from the drive
172;   Returns:
173;       AL:     Max supported PIO mode
174;       AH:     FLGH_DPT_IORDY if IORDY supported, zero otherwise
175;       CX:     Minimum Cycle Time in nanosecs
176;   Corrupts registers:
177;       BX
178;--------------------------------------------------------------------
179AtaID_GetMaxPioModeToAXandMinCycleTimeToCX:
180    ; Get PIO mode and cycle time for PIO 0...2
181    mov     bx, [es:si+ATA1.bPioMode]
182    mov     ax, bx                  ; AH = 0, AL = PIO mode 0, 1 or 2
183    eSHL_IM bx, 1                   ; Shift for WORD lookup
184    mov     cx, [cs:bx+.rgwPio0to2CycleTimeInNanosecs]
185
186    ; Check if IORDY is supported
187    test    BYTE [es:si+ATA2.wCaps+1], A2_wCaps_IORDY >> 8
188    jz      SHORT .ReturnPioTimings ; No PIO 3 or higher if no IORDY
189    mov     ah, FLGH_DPT_IORDY
190
191    ; Check if Advanced PIO modes are supported (3 and above)
192    test    BYTE [es:si+ATA2.wFields], A2_wFields_64to70
193    jz      SHORT .ReturnPioTimings
194
195    ; Get Advanced PIO mode
196    ; (Hard Disks supports up to 4 but CF cards can support 5 and 6)
197    mov     bl, [es:si+ATA2.bPIOSupp]
198.CheckNextFlag:
199    inc     ax
200    shr     bl, 1
201    jnz     SHORT .CheckNextFlag
202    MIN_U   al, 6                       ; Make sure not above lookup tables
203    mov     cx, [es:si+ATA2.wPIOMinCyF] ; Advanced modes use IORDY
204.ReturnPioTimings:
205    ret
206
207.rgwPio0to2CycleTimeInNanosecs:
208    dw      PIO_0_MIN_CYCLE_TIME_NS
209    dw      PIO_1_MIN_CYCLE_TIME_NS
210    dw      PIO_2_MIN_CYCLE_TIME_NS
211
212
213;--------------------------------------------------------------------
214; AtaID_GetRecoveryTimeToAXfromPioModeInBXandCycleTimeInCX
215;   Parameters:
216;       BX:     PIO Mode
217;       CX:     PIO Cycle Time in nanosecs
218;   Returns:
219;       AX:     Active Time in nanosecs
220;   Corrupts registers:
221;       BX, CX
222;--------------------------------------------------------------------
223AtaID_GetRecoveryTimeToAXfromPioModeInBXandCycleTimeInCX:
224    call    AtaID_GetActiveTimeToAXfromPioModeInBX
225    mov     bl, [cs:bx+.rgbPioModeToAddressValidTimeNs]
226    sub     cx, bx  ; Cycle Time (t0) - Address Valid Time (t1)
227    sub     cx, ax  ; - Active Time (t2)
228    xchg    ax, cx  ; AX = Recovery Time (t2i)
229    ret
230
231.rgbPioModeToAddressValidTimeNs:
232    db      PIO_0_MIN_ADDRESS_VALID_NS
233    db      PIO_1_MIN_ADDRESS_VALID_NS
234    db      PIO_2_MIN_ADDRESS_VALID_NS
235    db      PIO_3_MIN_ADDRESS_VALID_NS
236    db      PIO_4_MIN_ADDRESS_VALID_NS
237    db      PIO_5_MIN_ADDRESS_VALID_NS
238    db      PIO_6_MIN_ADDRESS_VALID_NS
239
240
241;--------------------------------------------------------------------
242; AtaID_GetActiveTimeToAXfromPioModeInBX
243;   Parameters:
244;       BX:     PIO Mode
245;   Returns:
246;       AX:     Active Time in nanosecs
247;   Corrupts registers:
248;       Nothing
249;--------------------------------------------------------------------
250AtaID_GetActiveTimeToAXfromPioModeInBX:
251    eMOVZX  ax, [cs:bx+.rgbPioModeToActiveTimeNs]
252    ret
253
254.rgbPioModeToActiveTimeNs:
255    db      PIO_0_MIN_ACTIVE_TIME_NS
256    db      PIO_1_MIN_ACTIVE_TIME_NS
257    db      PIO_2_MIN_ACTIVE_TIME_NS
258    db      PIO_3_MIN_ACTIVE_TIME_NS
259    db      PIO_4_MIN_ACTIVE_TIME_NS
260    db      PIO_5_MIN_ACTIVE_TIME_NS
261    db      PIO_6_MIN_ACTIVE_TIME_NS
262
263%endif ; MODULE_ADVANCED_ATA
Note: See TracBrowser for help on using the repository browser.