1 | ; Project name : XTIDE Universal BIOS
|
---|
2 | ; Description : Functions for generating L-CHS parameters for
|
---|
3 | ; drives with more than 1024 cylinders.
|
---|
4 | ;
|
---|
5 | ; These algorithms are taken from: http://www.mossywell.com/boot-sequence
|
---|
6 | ; Take a look at it for more detailed information.
|
---|
7 | ;
|
---|
8 | ; This file is shared with BIOS Drive Information Tool.
|
---|
9 |
|
---|
10 | ;
|
---|
11 | ; XTIDE Universal BIOS and Associated Tools
|
---|
12 | ; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
|
---|
13 | ;
|
---|
14 | ; This program is free software; you can redistribute it and/or modify
|
---|
15 | ; it under the terms of the GNU General Public License as published by
|
---|
16 | ; the Free Software Foundation; either version 2 of the License, or
|
---|
17 | ; (at your option) any later version.
|
---|
18 | ;
|
---|
19 | ; This program is distributed in the hope that it will be useful,
|
---|
20 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | ; GNU General Public License for more details.
|
---|
23 | ; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
24 | ;
|
---|
25 |
|
---|
26 | ; Section containing code
|
---|
27 | SECTION .text
|
---|
28 |
|
---|
29 | %ifdef MODULE_EBIOS
|
---|
30 | ;--------------------------------------------------------------------
|
---|
31 | ; AtaGeometry_GetLbaSectorCountToBXDXAXfromAtaInfoInESSI
|
---|
32 | ; Parameters:
|
---|
33 | ; ES:SI: Ptr to 512-byte ATA information read from the drive
|
---|
34 | ; Returns:
|
---|
35 | ; BX:DX:AX: 48-bit sector count
|
---|
36 | ; CL: FLGL_DPT_LBA48 if LBA48 supported
|
---|
37 | ; Zero if only LBA28 is supported
|
---|
38 | ; Corrupts registers:
|
---|
39 | ; Nothing
|
---|
40 | ;--------------------------------------------------------------------
|
---|
41 | AtaGeometry_GetLbaSectorCountToBXDXAXfromAtaInfoInESSI:
|
---|
42 | mov bx, Registers_ExchangeDSSIwithESDI
|
---|
43 | call bx ; ATA info now in DS:DI
|
---|
44 | push bx ; We will return via Registers_ExchangeDSSIwithESDI
|
---|
45 |
|
---|
46 | ; Check if LBA48 supported
|
---|
47 | test BYTE [di+ATA6.wSetSup83+1], A6_wSetSup83_LBA48>>8
|
---|
48 | jz SHORT .GetLba28SectorCount
|
---|
49 |
|
---|
50 | ; Get LBA48 sector count
|
---|
51 | mov cl, FLGL_DPT_LBA48
|
---|
52 | mov ax, [di+ATA6.qwLBACnt]
|
---|
53 | mov dx, [di+ATA6.qwLBACnt+2]
|
---|
54 | mov bx, [di+ATA6.qwLBACnt+4]
|
---|
55 | ret
|
---|
56 |
|
---|
57 | .GetLba28SectorCount:
|
---|
58 | xor cl, cl
|
---|
59 | xor bx, bx
|
---|
60 | mov ax, [di+ATA1.dwLBACnt]
|
---|
61 | mov dx, [di+ATA1.dwLBACnt+2]
|
---|
62 | ret
|
---|
63 | %endif ; MODULE_EBIOS
|
---|
64 |
|
---|
65 |
|
---|
66 | ;--------------------------------------------------------------------
|
---|
67 | ; AtaGeometry_GetLCHStoAXBLBHfromAtaInfoInESSIandTranslateModeInDX
|
---|
68 | ; AtaGeometry_GetLCHStoAXBLBHfromPCHSinAXBLBHandTranslateModeInDX
|
---|
69 | ; Parameters:
|
---|
70 | ; DX: Wanted translate mode or TRANSLATEMODE_AUTO to autodetect
|
---|
71 | ; ES:SI: Ptr to 512-byte ATA information read from the drive
|
---|
72 | ; Returns:
|
---|
73 | ; AX: Number of L-CHS cylinders (1...1027, yes 1027)
|
---|
74 | ; BL: Number of L-CHS heads (1...255)
|
---|
75 | ; BH: Number of L-CHS sectors per track (1...63)
|
---|
76 | ; CX: Number of bits shifted (0...3)
|
---|
77 | ; DL: CHS Translate Mode
|
---|
78 | ; Corrupts registers:
|
---|
79 | ; DH
|
---|
80 | ;--------------------------------------------------------------------
|
---|
81 | AtaGeometry_GetLCHStoAXBLBHfromAtaInfoInESSIandTranslateModeInDX:
|
---|
82 | call AtaGeometry_GetPCHStoAXBLBHfromAtaInfoInESSI
|
---|
83 | ; Fall to AtaGeometry_GetLCHStoAXBLBHfromPCHSinAXBLBH
|
---|
84 |
|
---|
85 | AtaGeometry_GetLCHStoAXBLBHfromPCHSinAXBLBHandTranslateModeInDX:
|
---|
86 | ; Check if user defined translate mode
|
---|
87 | test dx, dx
|
---|
88 | jnz SHORT .CheckIfLargeTranslationWanted
|
---|
89 | MIN_U ax, MAX_LCHS_CYLINDERS ; TRANSLATEMODE_NORMAL maximum cylinders
|
---|
90 | inc dx
|
---|
91 | .CheckIfLargeTranslationWanted:
|
---|
92 | dec dx ; Set ZF if TRANSLATEMODE_LARGE
|
---|
93 | jz SHORT ConvertPCHfromAXBLtoRevisedEnhancedCHinAXBL
|
---|
94 | dec dx ; Set ZF if TRANSLATEMODE_ASSISTED_LBA
|
---|
95 | jz SHORT .UseAssistedLBA
|
---|
96 | ; TRANSLATEMODE_AUTO set
|
---|
97 |
|
---|
98 | ; Generate L-CHS using simple bit shift algorithm (ECHS) if
|
---|
99 | ; 8192 or less cylinders.
|
---|
100 | cmp ax, 8192
|
---|
101 | jbe SHORT ConvertPCHfromAXBLtoEnhancedCHinAXBL
|
---|
102 |
|
---|
103 | ; We have 8193 or more cylinders so two algorithms are available:
|
---|
104 | ; Revised ECHS or Assisted LBA. The Assisted LBA provides larger
|
---|
105 | ; capacity but requires LBA support from drive (drives this large
|
---|
106 | ; always support LBA but user might have unintentionally set LBA).
|
---|
107 | .UseAssistedLBA:
|
---|
108 | test BYTE [es:si+ATA1.wCaps+1], A1_wCaps_LBA>>8
|
---|
109 | jz SHORT ConvertPCHfromAXBLtoRevisedEnhancedCHinAXBL
|
---|
110 |
|
---|
111 | ; Drive supports LBA
|
---|
112 | call GetSectorCountToDXAXfromCHSinAXBLBH
|
---|
113 | call ConvertChsSectorCountFromDXAXtoLbaAssistedLCHSinAXBLBH
|
---|
114 | xor cx, cx ; No bits to shift
|
---|
115 | mov dl, TRANSLATEMODE_ASSISTED_LBA
|
---|
116 | ret
|
---|
117 |
|
---|
118 |
|
---|
119 | ;--------------------------------------------------------------------
|
---|
120 | ; AtaGeometry_GetPCHStoAXBLBHfromAtaInfoInESSI
|
---|
121 | ; Parameters:
|
---|
122 | ; ES:SI: Ptr to 512-byte ATA information read from the drive
|
---|
123 | ; Returns:
|
---|
124 | ; AX: Number of P-CHS cylinders (1...16383)
|
---|
125 | ; BL: Number of P-CHS heads (1...16)
|
---|
126 | ; BH: Number of P-CHS sectors per track (1...63)
|
---|
127 | ; Corrupts registers:
|
---|
128 | ; Nothing
|
---|
129 | ;--------------------------------------------------------------------
|
---|
130 | AtaGeometry_GetPCHStoAXBLBHfromAtaInfoInESSI:
|
---|
131 | mov ax, [es:si+ATA1.wCylCnt] ; Cylinders (1...16383)
|
---|
132 | mov bl, [es:si+ATA1.wHeadCnt] ; Heads (1...16)
|
---|
133 | mov bh, [es:si+ATA1.wSPT] ; Sectors per Track (1...63)
|
---|
134 | ret
|
---|
135 |
|
---|
136 |
|
---|
137 | ;--------------------------------------------------------------------
|
---|
138 | ; GetSectorCountToDXAXfromCHSinAXBLBH
|
---|
139 | ; Parameters:
|
---|
140 | ; ES:SI: Ptr to 512-byte ATA information read from the drive
|
---|
141 | ; AX: Number of cylinders (1...16383)
|
---|
142 | ; BL: Number of heads (1...255)
|
---|
143 | ; BH: Number of sectors per track (1...63)
|
---|
144 | ; Returns:
|
---|
145 | ; DX:AX: Total number of CHS addressable sectors
|
---|
146 | ; Corrupts registers:
|
---|
147 | ; BX
|
---|
148 | ;--------------------------------------------------------------------
|
---|
149 | GetSectorCountToDXAXfromCHSinAXBLBH:
|
---|
150 | xchg ax, bx
|
---|
151 | mul ah ; AX = Heads * Sectors per track
|
---|
152 | mul bx
|
---|
153 | ret
|
---|
154 |
|
---|
155 |
|
---|
156 | ;--------------------------------------------------------------------
|
---|
157 | ; Revised Enhanced CHS calculation (Revised ECHS)
|
---|
158 | ;
|
---|
159 | ; This algorithm translates P-CHS sector count to L-CHS sector count
|
---|
160 | ; with bit shift algorithm. Since 256 heads are not allowed
|
---|
161 | ; (DOS limit), this algorithm makes translations so that maximum of
|
---|
162 | ; 240 L-CHS heads can be used. This makes the maximum addressable capacity
|
---|
163 | ; to 7,927,234,560 bytes ~ 7.38 GiB. LBA addressing needs to be used to
|
---|
164 | ; get more capacity.
|
---|
165 | ;
|
---|
166 | ; L-CHS parameters generated here require the drive to use CHS addressing.
|
---|
167 | ;
|
---|
168 | ; Here is the algorithm:
|
---|
169 | ; If cylinders > 8192 and heads = 16
|
---|
170 | ; Heads = 15
|
---|
171 | ; Cylinders = cylinders * 16 / 15 (losing the fraction component)
|
---|
172 | ; Do a standard ECHS translation
|
---|
173 | ;
|
---|
174 | ; ConvertPCHfromAXBLtoRevisedEnhancedCHinAXBL:
|
---|
175 | ; Parameters:
|
---|
176 | ; AX: Number of P-CHS cylinders (8193...16383)
|
---|
177 | ; BL: Number of P-CHS heads (1...16)
|
---|
178 | ; Returns:
|
---|
179 | ; AX: Number of L-CHS cylinders (?...1024)
|
---|
180 | ; BL: Number of L-CHS heads (?...240)
|
---|
181 | ; CX: Number of bits shifted (0...3)
|
---|
182 | ; DL: ADDRESSING_MODE_NORMAL or ADDRESSING_MODE_LARGE
|
---|
183 | ; Corrupts registers:
|
---|
184 | ; Nothing
|
---|
185 | ;--------------------------------------------------------------------
|
---|
186 | ConvertPCHfromAXBLtoRevisedEnhancedCHinAXBL:
|
---|
187 | ; Generate L-CHS using simple bit shift algorithm (ECHS) if
|
---|
188 | ; 8192 or less cylinders
|
---|
189 | cmp ax, 8192
|
---|
190 | jbe SHORT ConvertPCHfromAXBLtoEnhancedCHinAXBL
|
---|
191 | cmp bl, 16 ; Drives with 8193 or more cylinders can report 15 heads
|
---|
192 | jb SHORT ConvertPCHfromAXBLtoEnhancedCHinAXBL
|
---|
193 |
|
---|
194 | eMOVZX cx, bl ; CX = 16
|
---|
195 | dec bx ; Heads = 15
|
---|
196 | mul cx ; DX:AX = Cylinders * 16
|
---|
197 | dec cx ; CX = 15
|
---|
198 | div cx ; AX = (Cylinders * 16) / 15
|
---|
199 | ; Fall to ConvertPCHfromAXBXtoEnhancedCHinAXBX
|
---|
200 |
|
---|
201 |
|
---|
202 | ;--------------------------------------------------------------------
|
---|
203 | ; Enhanced CHS calculation (ECHS)
|
---|
204 | ;
|
---|
205 | ; This algorithm translates P-CHS sector count to L-CHS sector count
|
---|
206 | ; with simple bit shift algorithm. Since 256 heads are not allowed
|
---|
207 | ; (DOS limit), this algorithm require that there are at most 8192
|
---|
208 | ; P-CHS cylinders. This makes the maximum addressable capacity
|
---|
209 | ; to 4,227,858,432 bytes ~ 3.94 GiB. Use Revised ECHS or Assisted LBA
|
---|
210 | ; algorithms if there are more than 8192 P-CHS cylinders.
|
---|
211 | ;
|
---|
212 | ; L-CHS parameters generated here require the drive to use CHS addressing.
|
---|
213 | ;
|
---|
214 | ; Here is the algorithm:
|
---|
215 | ; Multiplier = 1
|
---|
216 | ; Cylinder = Cylinder - 1
|
---|
217 | ; Is Cylinder < 1024? If not:
|
---|
218 | ; Do a right bitwise rotation on the cylinder (i.e., divide by 2)
|
---|
219 | ; Do a left bitwise rotation on the multiplier (i.e., multiply by 2)
|
---|
220 | ; Use the multiplier on the Cylinder and Head values to obtain the translated values.
|
---|
221 | ;
|
---|
222 | ; ConvertPCHfromAXBLtoEnhancedCHinAXBL:
|
---|
223 | ; Parameters:
|
---|
224 | ; AX: Number of P-CHS cylinders (1...8192)
|
---|
225 | ; BL: Number of P-CHS heads (1...16)
|
---|
226 | ; Returns:
|
---|
227 | ; AX: Number of L-CHS cylinders (?...1024)
|
---|
228 | ; BL: Number of L-CHS heads (?...128)
|
---|
229 | ; CX: Number of bits shifted (0...3)
|
---|
230 | ; DL: TRANSLATEMODE_NORMAL or TRANSLATEMODE_LARGE
|
---|
231 | ; Corrupts registers:
|
---|
232 | ; Nothing
|
---|
233 | ;--------------------------------------------------------------------
|
---|
234 | ConvertPCHfromAXBLtoEnhancedCHinAXBL:
|
---|
235 | xor cx, cx ; No bits to shift initially
|
---|
236 | xor dl, dl ; Assume TRANSLATEMODE_NORMAL
|
---|
237 | .ShiftIfMoreThan1024Cylinder:
|
---|
238 | cmp ax, MAX_LCHS_CYLINDERS
|
---|
239 | jbe SHORT ReturnLCHSinAXBLBH
|
---|
240 | shr ax, 1 ; Halve cylinders
|
---|
241 | shl bl, 1 ; Double heads
|
---|
242 | inc cx ; Increment bit shift count
|
---|
243 | mov dl, TRANSLATEMODE_LARGE
|
---|
244 | jmp SHORT .ShiftIfMoreThan1024Cylinder
|
---|
245 |
|
---|
246 |
|
---|
247 | ;--------------------------------------------------------------------
|
---|
248 | ; LBA assist calculation (or Assisted LBA)
|
---|
249 | ;
|
---|
250 | ; This algorithm translates P-CHS sector count up to largest possible
|
---|
251 | ; L-CHS sector count (1024, 255, 63). Note that INT 13h interface allows
|
---|
252 | ; 256 heads but DOS supports up to 255 head. That is why BIOSes never
|
---|
253 | ; use 256 heads.
|
---|
254 | ;
|
---|
255 | ; L-CHS parameters generated here require the drive to use LBA addressing.
|
---|
256 | ;
|
---|
257 | ; Here is the algorithm:
|
---|
258 | ; If cylinders > 8192
|
---|
259 | ; Variable CH = Total CHS Sectors / 63
|
---|
260 | ; Divide (CH 1) by 1024 and add 1
|
---|
261 | ; Round the result up to the nearest of 16, 32, 64, 128 and 255. This is the value to be used for the number of heads.
|
---|
262 | ; Divide CH by the number of heads. This is the value to be used for the number of cylinders.
|
---|
263 | ;
|
---|
264 | ; ConvertChsSectorCountFromDXAXtoLbaAssistedLCHSinAXBLBH:
|
---|
265 | ; Parameters:
|
---|
266 | ; DX:AX: Total number of P-CHS sectors for CHS addressing
|
---|
267 | ; (max = 16383 * 16 * 63 = 16,514,064)
|
---|
268 | ; Returns:
|
---|
269 | ; AX: Number of cylinders (?...1027)
|
---|
270 | ; BL: Number of heads (16, 32, 64, 128 or 255)
|
---|
271 | ; BH: Number of sectors per track (always 63)
|
---|
272 | ; Corrupts registers:
|
---|
273 | ; CX, DX
|
---|
274 | ;--------------------------------------------------------------------
|
---|
275 | ConvertChsSectorCountFromDXAXtoLbaAssistedLCHSinAXBLBH:
|
---|
276 | ; Value CH = Total sector count / 63
|
---|
277 | ; Max = 16,514,064 / 63 = 262128
|
---|
278 | mov cx, LBA_ASSIST_SPT ; CX = 63
|
---|
279 | call Math_DivDXAXbyCX
|
---|
280 | push dx
|
---|
281 | push ax ; Value CH stored for later use
|
---|
282 |
|
---|
283 | ; BX:DX:AX = Value CH - 1
|
---|
284 | ; Max = 262128 - 1 = 262127
|
---|
285 | xor bx, bx
|
---|
286 | sub ax, BYTE 1
|
---|
287 | sbb dx, bx
|
---|
288 |
|
---|
289 | ; AX = Number of heads = ((Value CH - 1) / 1024) + 1
|
---|
290 | ; Max = (262127 / 1024) + 1 = 256
|
---|
291 | push si
|
---|
292 | call Size_DivideSizeInBXDXAXby1024andIncrementMagnitudeInCX
|
---|
293 | pop si
|
---|
294 | inc ax ; + 1
|
---|
295 |
|
---|
296 | ; Heads must be 16, 32, 64, 128 or 255 (round up to the nearest)
|
---|
297 | ; Max = 255
|
---|
298 | mov cx, 16 ; Min number of heads
|
---|
299 | .CompareNextValidNumberOfHeads:
|
---|
300 | cmp ax, cx
|
---|
301 | jbe SHORT .NumberOfHeadsNowInCX
|
---|
302 | shl cx, 1 ; Double number of heads
|
---|
303 | test ch, ch ; Reached 256 heads?
|
---|
304 | jz SHORT .CompareNextValidNumberOfHeads
|
---|
305 | dec cx ; If so, limit heads to 255
|
---|
306 | .NumberOfHeadsNowInCX:
|
---|
307 | mov bx, cx ; Number of heads are returned in BL
|
---|
308 | mov bh, LBA_ASSIST_SPT ; Sectors per Track
|
---|
309 |
|
---|
310 | ; DX:AX = Number of cylinders = Value CH (without - 1) / number of heads
|
---|
311 | ; Max = 262128 / 255 = 1027
|
---|
312 | pop ax
|
---|
313 | pop dx ; Value CH back to DX:AX
|
---|
314 | div cx
|
---|
315 |
|
---|
316 | ; Return L-CHS
|
---|
317 | ReturnLCHSinAXBLBH:
|
---|
318 | ret
|
---|