source: trunk/bootcode/special/lvm.asm@ 153

Last change on this file since 153 was 153, checked in by Ben Rietbroek, 8 years ago

Prepared 'LVM_GetDriveLetter' for reimplementation [v1.1.1-testing]

This is one ugly mthrfscker of which we won't even record the diff with
its reimplementation.

CAUTION:
This is a testbuild !
AirBoot uses the BIOS to access disks and a small coding error can trash
partition tables or other vital disk structures. You are advised to make
backups of TRACK0 and EBRs before using this testbuild. More info at:
https://rousseaux.github.io/netlabs.air-boot/pdf/AirBoot-v1.1.0-manual.pdf

File size: 18.9 KB
Line 
1; AiR-BOOT (c) Copyright 1998-2008 M. Kiewitz
2;
3; This file is part of AiR-BOOT
4;
5; AiR-BOOT is free software: you can redistribute it and/or modify it under
6; the terms of the GNU General Public License as published by the Free
7; Software Foundation, either version 3 of the License, or (at your option)
8; any later version.
9;
10; AiR-BOOT is distributed in the hope that it will be useful, but WITHOUT ANY
11; WARRANTY: without even the implied warranty of MERCHANTABILITY or FITNESS
12; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13; details.
14;
15; You should have received a copy of the GNU General Public License along with
16; AiR-BOOT. If not, see <http://www.gnu.org/licenses/>.
17;
18;---------------------------------------------------------------------------
19; AiR-BOOT / LVM
20;---------------------------------------------------------------------------
21
22IFDEF MODULE_NAMES
23DB 'LVM',0
24ENDIF
25
26LVM_InitCRCTable Proc Near
27 ; Initializes our LVM-CRC-Table
28 xor cl, cl
29 mov di, offset [LVM_CRCTable]
30 LVM_ICRCT_Loop:
31 ;movzx ax, cl
32 mov al,cl
33 mov ah,0
34 xor dx, dx ; DX:AX - CRC-Value
35 mov ch, 8
36 LVM_ICRCT_Loop2:
37 shr dx, 1
38 rcr ax, 1 ; Shift value 1 to the right
39 jnc LVM_ICRCT_NoXOR
40 xor dx, 0EDB8h
41 xor ax, 8320h
42 LVM_ICRCT_NoXOR:
43 dec ch
44 jnz LVM_ICRCT_Loop2
45 mov wptr [di+0], ax
46 mov wptr [di+2], dx
47 add di, 4
48 add cl, 1
49 jnc LVM_ICRCT_Loop
50 ret
51LVM_InitCRCTable EndP
52
53; Calculates an LVM-Sector CRC of a given sector
54; In: DS:SI - Points to Sector...
55; Out: DX:AX - LVM CRC
56; Destroyed: None
57LVM_GetSectorCRC Proc Near Uses bx cx
58
59IFDEF AUX_DEBUG
60 IF 0
61 DBG_TEXT_OUT_AUX 'LVM_GetSectorCRC:'
62 PUSHRF
63 ;~ call DEBUG_DumpRegisters
64 ;~ call AuxIO_DumpParagraph
65 ;~ call AuxIO_TeletypeNL
66 POPRF
67 ENDIF
68ENDIF
69
70 push word ptr [si+LocLVM_CRC+00]
71 push word ptr [si+LocLVM_CRC+02]
72 push si
73 mov word ptr [si+LocLVM_CRC], 0
74 mov word ptr [si+LocLVM_CRC+2], 0
75 mov ax, -1
76 mov dx, -1
77 mov cx, 512
78 LVM_GSCRC_Loop:
79 xor bh, bh
80 mov bl, al ; Save last byte to BL
81 mov al, ah
82 mov ah, dl
83 mov dl, dh
84 xor dh, dh ; SHR DX:AX, 8
85 xor bl, [si]
86 inc si ; XOR last byte with [data]
87 shl bx, 1
88 shl bx, 1
89 xor ax, word ptr [LVM_CRCTable+bx+0]
90 xor dx, word ptr [LVM_CRCTable+bx+2] ; XOR with CRC-Table
91 loop LVM_GSCRC_Loop
92 pop si
93 pop word ptr [si+LocLVM_CRC+2]
94 pop word ptr [si+LocLVM_CRC]
95 ret
96LVM_GetSectorCRC EndP
97
98; Checks ds:[SI], if a valid LVM Signature is found (sets carry in that case)
99; This does not check for valid LVM CRC (which also needs to be done)
100; In: DS:SI - Sector that needs to get checked...
101; Out: Carry set, if valid LVM signature found
102; Destroyed: None
103LVM_CheckSectorSignature Proc Near
104 test byte ptr [CFG_IgnoreLVM], 1 ; We are supposed to ignore LVM, so
105 jnz LVMCSS_InvalidSignature ; any sector is bad!
106 cmp word ptr [si+LocLVM_SignatureStart], 5202h
107 jne LVMCSS_InvalidSignature
108 cmp word ptr [si+LocLVM_SignatureStart+2], 'BM'
109 jne LVMCSS_InvalidSignature
110 cmp word ptr [si+LocLVM_SignatureStart+4], 'MP'
111 jne LVMCSS_InvalidSignature
112 cmp word ptr [si+LocLVM_SignatureStart+6], 'DF'
113 jne LVMCSS_InvalidSignature
114 stc
115 ret
116 LVMCSS_InvalidSignature:
117 clc
118 ret
119LVM_CheckSectorSignature EndP
120
121; Checks Sector for a valid LVM CRC is encountered
122; First one should check for a valid signature and call this later.
123; In: DS:SI - Sector that needs to get checked...
124; Out: Carry set, if LVM CRC valid
125; Destroyed: None
126LVM_CheckSectorCRC Proc Near Uses ax bx dx
127 call IsSectorBufferZero ; Zero sector implies bad CRC
128 jz LVMCSCRC_BadCRC
129 call LVM_GetSectorCRC ; Only use after CRC table is valid
130 cmp ax, word ptr [si+LocLVM_CRC]
131 jne LVMCSCRC_BadCRC
132 cmp dx, word ptr [si+LocLVM_CRC+2]
133 jne LVMCSCRC_BadCRC
134 stc ; Indicate CRC is OK
135 ret
136 LVMCSCRC_BadCRC:
137 clc ; Indicate BAD CRC
138 ret
139LVM_CheckSectorCRC EndP
140
141; Checks if a sector is a valid LVM-sector
142; Sector is considered valid LVM-sector if both signature and CRC are correct.
143; IN : DS:SI - Buffer with LVM-sector that needs to be checked...
144; OUT : AL.0 - 1 -> LVM Signature found
145; AL.1 - 1 -> CRC OK
146; CY - Signature and CRC OK, otherwise none or invalid LVM sector
147; Destroyed: None
148LVM_ValidateSector Proc Near
149 xor ax, ax ; Assume no Signature or valid CRC
150 call LVM_CheckSectorSignature ; CF=1 -> Signature OK
151 rcl al, 1 ; Store CF in AL.0
152 call LVM_CheckSectorCRC ; CF=1 -> CRC OK
153 rcl ah, 1 ; Store CF in AH.0
154 shl ah, 1 ; Move it to AH.1
155 or al, ah ; Merge CY results to AL
156 cmp al, 3 ; AH=3 -> Signature and CRC OK
157 clc ; Assume invalid LVM-sector
158 jne @F
159 stc ; AH=3 -> Indicate valid LVM-sector
160 @@:
161 mov ah, 0 ; Don't leave garbage in AH
162 ret
163LVM_ValidateSector EndP
164
165; Updates Sector with valid LVM CRC
166; This one doesn't check, if it's really an LVM sector, so check before!
167; In: DS:SI - Sector that needs to get checked...
168; Out: None, CRC updated
169; Destroyed: None
170LVM_UpdateSectorCRC Proc Near Uses ax dx
171 call LVM_GetSectorCRC
172 mov word ptr [si+LocLVM_CRC], ax
173 mov word ptr [si+LocLVM_CRC+2], dx
174 ret
175LVM_UpdateSectorCRC EndP
176
177; Searches for a partition in LVM Information Sector and sets SI to point to
178; the LVM-entry. It will also set CARRY then.
179; In: DX:AX - LBA starting sector of partition to be searched
180; DS:SI - Valid (previously checked) LVM-Information-Sector
181; Out: Carry set, if partition found
182; DS:SI - points to LVM information entry
183; Destroyed: None
184
185; INVALID LVM RECORD WHEN STICK INSERTED !
186
187LVM_SearchForPartition Proc Near Uses cx
188
189IFDEF AUX_DEBUG
190 IF 0
191 DBG_TEXT_OUT_AUX 'LVM_SearchForPartition:'
192 PUSHRF
193 call DEBUG_DumpRegisters
194 ;~ call AuxIO_DumpParagraph
195 ;~ call AuxIO_TeletypeNL
196 POPRF
197 ENDIF
198ENDIF
199
200 cmp byte ptr [si+LocLVM_SignatureStart], LocLVM_SignatureByte0
201 jne LVMSFP_NotFound ; Quick Check, if LVM sector there
202 add si, LocLVM_StartOfEntries
203 mov cl, LocLVM_MaxEntries
204 LVMSFP_Loop:
205 cmp ax, [si+LocLVM_PartitionStart]
206 jne LVMSFP_NextEntry
207 cmp dx, [si+LocLVM_PartitionStart+2]
208 je LVMSFP_FoundIt
209 LVMSFP_NextEntry:
210 add si, LocLVM_LenOfEntry
211 dec cl
212 jnz LVMSFP_Loop
213 LVMSFP_NotFound:
214 clc
215 ret
216 LVMSFP_FoundIt:
217 stc
218 ret
219LVM_SearchForPartition EndP
220
221
222
223LVM_GetDriveLetter Proc Near Uses bx cx dx si di ds es
224 xor al, al
225 clc
226 ret
227LVM_GetDriveLetter EndP
228
229
230
231; Sets a drive-letter in the LVM-info of a partition. (if it exists)
232; In: BX:CX - LBA starting sector of partition to be searched
233; DL = Physical Disk in BIOS notation. (80h+)
234; AL = DriveLetter to set (can be zero to hide partition from LVM)
235; Out: CY=1 if LVM-info found, 0 if no LVM-info.
236LVM_SetDriveLetter Proc Near Uses bx cx dx si di ds es
237
238 local disk:byte
239 local drive_letter:byte
240 local pri_ind:byte
241 local lvm_log_high:word
242 local lvm_log_low:word
243 ; For primary partitions this information is stored in the last
244 ; sector of track0; for all four partition entries in case they
245 ; they are all primary ones.
246 ;
247 ; LVM DLAT info for logical partitions is stored in the sector
248 ; preceding the start of the partition.
249 ;
250 ; Because the LVM info of a logical partition is the easiest to find,
251 ; we do that first. The LVM info for primary partitions is located
252 ; dependent on the geometry in use, so we use a special locater
253 ; call for that. Also, since the LVM info for primaries contains
254 ; info on all 4 entries, we need the partition index to obtain the
255 ; correct drive-letter.
256 ;
257
258IFDEF AUX_DEBUG
259 IF 0
260 DBG_TEXT_OUT_AUX 'LVM_SetDriveLetter:'
261 PUSHRF
262 ;~ call DEBUG_DumpRegisters
263 ;~ call AuxIO_DumpParagraph
264 ;~ call AuxIO_TeletypeNL
265 POPRF
266 ENDIF
267ENDIF
268
269 mov [disk], dl
270
271 ; Store the drive-letter for later use
272 mov [drive_letter], al
273
274
275 ; See if this is a primary partition
276 ; CY will be set if it is and AL will contain the 0-based
277 ; index in the P-table.
278 ; If it's a logical partition, CY will be clear and AL
279 ; will be set to 0ffh indicating an invalid index.
280 call PART_IsPrimaryPartition
281 mov al,0
282 rcl al,1 ; CY if primary
283 mov dh,al ; Save PRI or LOG
284 mov [pri_ind],al
285
286 ; Save PRI/LOG indicator for later use
287 push dx
288
289 ; Load *possible* LVM sector
290 ; This load is only valid if the partition is logical, in which case
291 ; the LVM sector is below the start of the partition.
292 ; If primary, the LVM sector is at a location that
293 ; DriveIO_LoadMasterLVMSector will find out.
294
295 ; Push LBA address
296 push bx
297 push cx
298
299 ; Adjust for logical LVM-sector
300 sub cx,1
301 sbb bx,0
302
303 ; Store LBA address of LVM-sector
304 mov [lvm_log_low],cx
305 mov [lvm_log_high],bx
306
307 ; Load the LVM sector
308 push si
309 push di
310 mov si,offset [LVMSector]
311 mov di,ds
312 mov ax, cx ; LBA low is now in AX
313 call DriveIO_ReadSectorLBA
314 pop di
315 pop si
316
317 ; Restore LBA address
318 pop cx
319 pop bx
320
321 ; Restore PRI/LOG partition indicator in DH
322 pop dx
323
324 ; Test PRI or not
325 test dh,dh
326 ; It's not a PRI so we can use the previously loaded LVM sector
327 jz LVM_SetDriveLetter_is_not_pri
328
329 ;
330 ; It's a PRI so we use the special locator function.
331 ; This locator takes care of extended OS/2 geometry should that be used
332 ;
333 call DriveIO_LoadMasterLVMSector
334 jnc LVM_SetDriveLetter_null_lvm_dl
335
336 mov ax, word ptr [MasterLVMLBA] ; ARRAY VAN MAKEN !
337 mov [lvm_log_low], ax
338 mov [lvm_log_high], 0
339
340 LVM_SetDriveLetter_is_not_pri:
341
342 ;
343 ; At this stage the LVM-info sector has been loaded at [LVMSector].
344 ; From here we look for an LVM entry for the partition.
345 ; If one is found, based on it's LBA-start, it's driveletter is used
346 ; in case byte 25h in the BPB is zero.
347 ;
348
349 ; Search for the partition in the LVM info.
350 ; If found, CY is set and SI points to LVM entry.
351 push si
352 mov ax,cx
353 mov dx,bx
354 mov si,offset [LVMSector]
355 call LVM_SearchForPartition
356 mov bx,si ; BX now points to LVM entry
357 pop si
358
359 mov al,0 ; Setup null driveletter
360 ; Oops, no valid LVM record was used so we have a null driveletter.
361 jnc LVM_SetDriveLetter_null_lvm_dl
362
363 ;
364 ; At this point BX points to the LVM-entry related to the
365 ; partition, whether it was a logical or a primary one.
366 ;
367 mov al, [drive_letter]
368 mov [bx+LocLVM_VolumeLetter],al
369
370 mov si, offset [LVMSector]
371 call LVM_UpdateSectorCRC
372
373 mov dl, [disk]
374 mov bx, [lvm_log_high]
375 mov ax, [lvm_log_low]
376
377 call DriveIO_SaveSector
378
379 LVM_SetDriveLetter_null_lvm_dl:
380 ret
381LVM_SetDriveLetter EndP
382
383
384
385; Removes a given drive-letter from the whole LVM information sector
386; In: CH - drive-letter (ascii)
387; DS:SI - LVM-Information-Sector
388; Out: LVM-Information-Sector updated (including LVM CRC)
389; Destroyed: None
390LVM_RemoveVolLetterFromSector Proc Near Uses cx
391
392IFDEF AUX_DEBUG
393 IF 0
394 DBG_TEXT_OUT_AUX 'LVM_RemoveVolLetterFromSector:'
395 PUSHRF
396 ;~ call DEBUG_DumpRegisters
397 ;~ call AuxIO_DumpParagraph
398 ;~ call AuxIO_TeletypeNL
399 POPRF
400 ENDIF
401ENDIF
402
403 cmp bptr [si+LocLVM_SignatureStart], LocLVM_SignatureByte0
404 jne LVMRVLFS_Done ; Quick Check, if LVM sector there
405 push si
406 add si, LocLVM_StartOfEntries
407 mov cl, LocLVM_MaxEntries
408 LVMRVLFS_Loop:
409 cmp ch, [si+LocLVM_VolumeLetter]
410 jne LVMRVLFS_NextEntry
411 ; Reset drive-letter, if matched
412 mov bptr [si+LocLVM_VolumeLetter], 0 ; ASSIGN NEXT FREE HERE... (DOET DUBBEL ALS ZELFDE DL ALS SYS)
413 LVMRVLFS_NextEntry:
414 add si, LocLVM_LenOfEntry
415 dec cl
416 jnz LVMRVLFS_Loop
417 pop si
418 call LVM_UpdateSectorCRC
419 LVMRVLFS_Done:
420 ret
421LVM_RemoveVolLetterFromSector EndP
422
423; Reassigns LVM volume driveletter
424; Will remove the drive-letter from any volume that got it currently
425; and finally change the drive-letter of the given partition
426; In: AL - drive-letter
427; DS:SI - points to partition, that needs that driveletter
428; Out: None
429; Destroyed: AX
430
431LVM_DoLetterReassignment Proc Near Uses bx cx dx si di
432
433IFDEF AUX_DEBUG
434 IF 0
435 DBG_TEXT_OUT_AUX 'LVM_DoLetterReassignment:'
436 PUSHRF
437 ;~ call DEBUG_DumpRegisters
438 ;~ call AuxIO_DumpParagraph
439 ;~ call AuxIO_TeletypeNL
440 POPRF
441 ENDIF
442ENDIF
443
444 mov di, si ; Save SI in DI (Partition-pointer)
445 mov ch, al ; and AL in CH (drive-letter)
446 xor bx, bx
447 mov cl, CFG_Partitions
448 or cl, cl
449 jz LVMDLR_SkipRemove
450
451 LVMDLR_RemoveLoop:
452 cmp bptr [PartitionVolumeLetters+bx], ch
453 jne LVMDLR_NextPartition
454 ; One volume that has our wanted drive-letter, so remove it!
455 mov dl, bl
456 call PART_GetPartitionPointer ; DL - partition -> SI
457 ; Now set CurPartition_Location for the DriveIO-functions to work
458 mov ax, wptr [si+LocIPT_AbsolutePartTable]
459 mov wptr [CurPartition_Location+0], ax
460 mov ax, wptr [si+LocIPT_AbsolutePartTable+2]
461 mov wptr [CurPartition_Location+2], ax
462 mov ax, wptr [si+LocIPT_LocationPartTable+1]
463 mov wptr [CurPartition_Location+6], ax
464 mov ah, bptr [si+LocIPT_LocationPartTable+0]
465 mov al, [si+LocIPT_Drive]
466 mov wptr [CurPartition_Location+4], ax
467 call DriveIO_LoadLVMSector ; SI points now to LVM-Sector
468 call LVM_RemoveVolLetterFromSector
469
470 call DriveIO_SaveLVMSector ; Save sector
471
472 LVMDLR_NextPartition:
473 inc bx
474 dec cl
475 jnz LVMDLR_RemoveLoop
476
477 LVMDLR_SkipRemove:
478 ; Set CurPartition_Location information of destination partition
479 mov ax, wptr [di+LocIPT_AbsolutePartTable]
480 mov wptr [CurPartition_Location+0], ax
481 mov ax, wptr [di+LocIPT_AbsolutePartTable+2]
482 mov wptr [CurPartition_Location+2], ax
483 mov ah, bptr [di+LocIPT_LocationPartTable+0]
484 mov al, [di+LocIPT_Drive]
485 mov wptr [CurPartition_Location+4], ax
486 mov ax, wptr [di+LocIPT_LocationPartTable+1]
487 mov wptr [CurPartition_Location+6], ax
488 call DriveIO_LoadLVMSector ; SI points now to LVM-Sector
489 mov ax, wptr [di+LocIPT_AbsoluteBegin]
490 mov dx, wptr [di+LocIPT_AbsoluteBegin+2]
491 mov di, si ; Save SI in DI
492 call LVM_SearchForPartition
493 jnc LVMDLR_DestPartNotFound
494 ; Set new volume letter
495 mov bptr [si+LocLVM_VolumeLetter], ch
496 mov si, di ; SI - LVM Sector again
497 call LVM_UpdateSectorCRC ; Update LVM-CRC now
498
499 call DriveIO_SaveLVMSector ; Save sector
500
501 LVMDLR_DestPartNotFound:
502 ; This here is done for safety, because we misuse CurPartition_Location
503 xor ax, ax
504 mov di, offset CurPartition_Location
505 mov cx, 4
506 rep stosw ; NUL out CurPartition_Location
507 ret
508LVM_DoLetterReassignment EndP
509
510
511; This walks the IPT and for each partition it obtains the LVM drive-letter
512; if available. This drive-letter is then marked as in-use in the Map.
513; The FreeDriveletterMap is used by the drive-letter reassignment function
514; to assign a new drive to a data-partition when a system-partition is booted
515; with the same drive-letter. The original drive-letter for the data-partition
516; is saved so it can be restored later when a system is booted that does not
517; use the drive-letter. Note that there can be multiple system-partitions
518; using the same drive-letter and data-partitions can become system-partition
519; by making them bootable. (and vice versa)
520LVM_ComposeFreeDriveletterMap Proc
521
522; get nr of partitions in IPT
523; for each partition get LVM drive-letter and reset bit in map.
524
525LVM_ComposeFreeDriveletterMap EndP
526
Note: See TracBrowser for help on using the repository browser.