source: trunk/bootcode/regular/driveio.asm@ 149

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

Fixed bug for new LVM sector load method [v1.1.1-testing]

Getting the LVM_SPT was done before DL was loaded with the correct
BIOS disk number. Fixed.

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: 68.4 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 / DRIVE I/O
20;---------------------------------------------------------------------------
21
22
23
24IFDEF MODULE_NAMES
25DB 'DRIVEIO',0
26ENDIF
27
28;
29; Check if INT13X extensions are supported.
30; AirBoot requires these extensions, and will halt if they are not available.
31; Modified: [CurIO_UseExtension]
32DriveIO_CheckFor13extensions Proc Near Uses ax bx cx dx
33 mov ah, 41h
34 mov bx, 55AAh
35 mov dl, [BIOS_BootDisk] ; We check using the boot-disk
36 int 13h
37
38IFDEF AUX_DEBUG
39 IF 0
40 DBG_TEXT_OUT_AUX 'DriveIO_CheckFor13extensions:'
41 PUSHRF
42 call DEBUG_DumpRegisters
43 ;~ call AuxIO_DumpParagraph
44 ;~ call AuxIO_TeletypeNL
45 mov si, offset [Scratch]
46 mov word ptr [si], 50h
47 mov ah, 48h
48 int 13h
49 call DEBUG_DumpRegisters
50 ;~ call AuxIO_DumpSector
51 POPRF
52 ENDIF
53ENDIF
54
55 jc PCCF13E_NotFound ; Error occured
56 cmp bx, 0AA55h
57 je PCCF13E_Found
58 PCCF13E_NotFound:
59 ret
60 PCCF13E_Found:
61 and cx, 1 ; Check 42h-44h,47h,48h supported
62 jz PCCF13E_NotFound ; Sig OK but no support, strange beast
63 mov byte ptr [CurIO_UseExtension], 1
64 ret
65DriveIO_CheckFor13extensions EndP
66
67
68; Note: Some routines set DS/ES to CS or even address via CS, even if its not
69; needed. This was done for SECURITY. So DO NOT remove it.
70; Its there to make sure the correct data is loaded/written to/from
71; harddrive.
72;
73; IF YOU MODIFY ANYTHING IN HERE, YOU MAY EASILY BREAK YOUR HARDDRIVE!
74
75; Will only load base-configuration, will NOT load IPT nor Hide-Config
76; Those are originally loaded on startup and will NOT get reloaded.
77DriveIO_LoadConfiguration Proc Near Uses ax bx cx dx es
78
79IFDEF AUX_DEBUG
80 IF 0
81 DBG_TEXT_OUT_AUX 'DriveIO_LoadConfiguration:'
82 PUSHRF
83 ;~ call DEBUG_DumpRegisters
84 ;~ call AuxIO_DumpParagraph
85 ;~ call AuxIO_TeletypeNL
86 POPRF
87 ENDIF
88ENDIF
89
90;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
91 mov ax, cs
92 mov es, ax
93 mov bx, offset Configuration
94 xor dh, dh
95 mov dl, [BIOS_BootDisk] ; Disk we booted from
96 mov cx, 0037h ; Sector 55 (CHS)
97 mov ax, 0201h ; Function 02, read 1 sector...
98 int 13h
99 jnc DIOLC_NoError
100 call MBR_LoadError ; Will Abort BootUp
101
102
103 DIOLC_NoError:
104 ret
105DriveIO_LoadConfiguration EndP
106
107DriveIO_SaveConfiguration Proc Near Uses ax bx cx dx ds es si
108
109IFDEF AUX_DEBUG
110 IF 0
111 DBG_TEXT_OUT_AUX 'DriveIO_SaveConfiguration:'
112 PUSHRF
113 ;~ call DEBUG_DumpRegisters
114 ;~ call AuxIO_DumpParagraph
115 ;~ call AuxIO_TeletypeNL
116 POPRF
117 ENDIF
118ENDIF
119
120 mov ax, cs
121 mov ds, ax
122 mov es, ax ; Safety first (CS==DS==ES)
123 ; --- Overwrite Floppy-Name with "FloppyDrive"
124 mov si, offset TXT_Floppy_Drive
125 mov di, offset PartitionTable
126 sub di, 30 ; Adjust to Floppy-Name
127 mov cx, 11
128 rep movsb
129 mov si, offset Configuration ; Calculate new checksum
130 xor bx, bx
131
132 ; Changed from 5 to calculated value (not here, see compat. issue below)
133 ; Fixes issue: #2987 -- "air-boot doesn't remember drive letter"
134 ; Size of the ab-configuration in 512 byte sectors
135 ;mov cx, (MBR_BackUpMBR - Configuration) / 200h
136
137 ; AB v1.07 stores a 5 sector configuration with a 5 sector checksum.
138 ; AB v1.0.8+ *should* stores a 7 sector configuration with a
139 ; 7 sector checksum.
140 ; Because 5 was hardcoded here, SET(A)BOOT v1.07 will see see an AB v1.0.8+
141 ; config as corrupted, while this is not the case.
142 ; So, for compatibility reasons, in v1.0.8+, the checksum stored is over
143 ; 5 sectors, to be compatible with v1.07.
144 ; This may change (be corrected) in future versions !
145 mov cx,5
146
147 mov dx, [CFG_CheckConfig]
148 mov [CFG_CheckConfig], bx
149 DIOSC_Loop:
150 call MBR_GetCheckOfSector
151 loop DIOSC_Loop
152 mov [CFG_CheckConfig], bx
153 ; --------------------------------------------------------------------
154 ; ES == CS
155 mov bx, offset Configuration
156 xor dh, dh
157 mov dl, [BIOS_BootDisk] ; Disk we booted from
158 mov cx, 0037h ; Sector 55 (CHS)
159
160 ; Changed from 5 to calculated value
161 ; Fixes issue: #2987 -- "air-boot doesn't remember drive letter"
162 ; Size of the ab-configuration in 512 byte sectors
163 mov al, (MBR_BackUpMBR - Configuration) / 200h
164 mov ah,03h
165;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
166 int 13h
167 jnc DIOSC_NoError
168 call MBR_SaveError ; Will Abort BootUp
169 DIOSC_NoError:
170 ret
171DriveIO_SaveConfiguration EndP
172
173DriveIO_UpdateFloppyName Proc Near Uses bx cx dx ds si es di
174 mov ax, cs
175 mov ds, ax
176 mov es, ax
177
178 mov ah, 00h ; Function 0 - Reset Drive
179 xor dl, dl
180 int 13h
181 xor dx, dx ; Cylinder=0, Head=0
182 mov cx, 1 ; Sector=1, Drive=0
183 mov bx, offset TmpSector ; ES:BX - TmpSector
184 mov ax, 0201h ; Function 2 - Load Sector
185 int 13h
186 jnc DIOUFN_AllFine
187
188 ; --- Overwrite Floppy-Name with "No Disc"
189 mov si, offset TXT_Floppy_NoDisc
190 xor ax, ax
191 DIOUFN_WriteFloppyName:
192 mov di, offset PartitionTable
193 sub di, 30 ; Adjust to Floppy-Name
194 mov cl, 11
195 rep movsb
196 ret ; AX=-1 -> GotDisc, =0 -> NoDisc
197
198 ; --- Floppy found and read, data in TempSector
199 DIOUFN_AllFine:
200 mov ax, -1
201 mov si, offset TXT_Floppy_NoName
202 cmp wptr es:[bx+54], 'AF'
203 jne DIOUFN_WriteFloppyName
204 cmp wptr es:[bx+56], '1T'
205 jne DIOUFN_WriteFloppyName
206 cmp bptr es:[bx+58], '2'
207 jne DIOUFN_WriteFloppyName
208 mov si, bx
209 add si, 43 ; FAT12 - Volume Label Location
210 jmp DIOUFN_WriteFloppyName
211DriveIO_UpdateFloppyName EndP
212
213; =============================================================================
214; HARDDRIVE / GENERAL ACCESS
215; =============================================================================
216; The following routines are used for harddisc/floppy access.
217; The access is done via INT 13h/CHS or INT 13h/LBA.
218; Access will be done prefered by INT 13h/CHS, because it's (I wonder!) much
219; faster, than the LBA-method. I don't know, why LBA is so slow. Perhaps BIOS.
220;
221; Internal access (to AiR-BOOT) is always done via INT 13h/CHS.
222
223DriveIO_GetHardDriveCount Proc Near Uses ds si
224 push ds
225 push si
226 push 0040h
227 pop ds
228 mov si, 0075h
229 mov dh, ds:[si] ; 40:75 -> POST: Total Harddiscs == DL
230 pop si
231 pop ds
232 mov [TotalHarddiscs], dh
233 ret
234DriveIO_GetHardDriveCount EndP
235
236
237; Fills our LBA-Usage table. It holds the LBA-address, where BIOS/CHS access is
238; stopped and BIOS/LBA access is started.
239; This is calculated by Sector*Heads. Comparing will get done with Bit 25-10
240; on LBA sectors, so we actually divide sector number by 1024.
241DriveIO_InitLBASwitchTable Proc Near Uses es di
242 mov di, offset LBASwitchTable
243 mov dh, [TotalHarddiscs]
244 mov dl, 80h ; First disk to process
245 DIOILUT_DriveLoop:
246 push dx
247 push di
248 mov ah, 08h
249 int 13h ; DISK - GET DRIVE PARAMETERS
250 mov ah, 0FBh ; Assume 255 heads/63 sectors, if error
251 jc DIOILUT_Error
252 and cl, 111111b ; Isolate lower 6 bits of CL -> sector count
253;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
254 ;movzx ax, cl
255 mov al,cl
256 mov ah,0
257
258 mov bl, dh ; DH -> max head number
259 mul bl ; AX = Sectors*Heads
260 shl ah, 1
261 shl ah, 1 ; Shift 2 bits, so we are able to compare to
262 ; bit 16-23 of the LBA address
263 DIOILUT_Error:
264 pop di
265 pop dx
266 mov bptr ds:[di], ah ; Save that value
267 inc di ; Go to next BYTE
268 inc dl ; Next disk
269 dec dh ; Decrease disks to process
270 jnz DIOILUT_DriveLoop ; Next disk if DH != 0
271 ret
272DriveIO_InitLBASwitchTable EndP
273
274
275
276
277;FIXME: Only LBA gets updated, need to update CHS too !!!!!!!
278
279; Adjusts BX:AX / CX:DX to meet LVM sector location
280; BX:AX / CX:DX point to MBR or EBR !
281; Destroys SI
282; Rousseau: Enhanced to handle sector-numbers 127 and 255 besides 63 for LVM-info sectors.
283; Ugly, need to cleanup.
284DriveIO_LVMAdjustToInfoSector Proc Near
285
286IFDEF AUX_DEBUG
287 IF 0
288 DBG_TEXT_OUT_AUX 'DriveIO_LVMAdjustToInfoSector:'
289 PUSHRF
290 call DEBUG_DumpRegisters
291 ;~ call AuxIO_DumpParagraph
292 ;~ call AuxIO_TeletypeNL
293 POPRF
294 ENDIF
295ENDIF
296
297 push cx ; Save Cyl/Sec part
298 xor ch,ch ; Clear low Cyl part
299 and cl,63 ; Clear high Cyl part
300 push bx ; We need BX...
301 push dx ; and DX temoraily
302 mov bx,offset [TrueSecs] ; Offset of sector table
303 xor dh,dh ; Clear DH because we use DL as index
304 and dl,01111111b ; Remove high bit of BIOS disk-nr
305 shl dx,2 ; Index to DWORD table
306 add bx,dx ; Point to TrueSecs for this disk
307 mov si,[bx] ; Get SPT for this disk
308 pop dx ; Restore DX...
309 pop bx ; and BX
310 ;~ sub si,cx ; Adjust offset !! INCORRECT FOR LBA (TP CX != 0 !!)
311 dec si
312 pop cx ; Restore Cyl/Sec part
313 add ax,si ; Add offset to low part...
314 adc bx,0 ; and high part of LBA address
315 or cl,63 ; Adjust CHS part !FIX ME for > 63! !! FIX HUGE DRIVE !!
316
317IFDEF AUX_DEBUG
318 IF 0
319 DBG_TEXT_OUT_AUX 'adjusted'
320 PUSHRF
321 call DEBUG_DumpRegisters
322 ;~ call AuxIO_DumpParagraph
323 ;~ call AuxIO_TeletypeNL
324 POPRF
325 ENDIF
326ENDIF
327
328 ret
329
330DriveIO_LVMAdjustToInfoSector EndP
331
332
333
334
335
336
337; #########################################################################
338; Routine: Loads partition to ExecBase and checks for validity
339; #########################################################################
340; Calling : bx:ax - Absolute sector
341; cx:dx - Cylinder/Sector, Side/Drive (hi/lo-byte)
342; Returns : Carry Set if invalid partition encountered
343; Preserve: all registers
344; #########################################################################
345DriveIO_LoadPartition Proc Near Uses si
346
347IFDEF AUX_DEBUG
348 IF 0
349 DBG_TEXT_OUT_AUX 'DriveIO_LoadPartition:'
350 PUSHRF
351 call DEBUG_DumpRegisters
352 ;~ call AuxIO_DumpParagraph
353 ;~ call AuxIO_TeletypeNL
354 POPRF
355 ENDIF
356ENDIF
357
358 mov wptr cs:[CurPartition_Location+0], ax
359 mov wptr cs:[CurPartition_Location+2], bx
360 mov wptr cs:[CurPartition_Location+4], dx
361 mov wptr cs:[CurPartition_Location+6], cx ; Saves the location
362 mov si, offset PartitionSector ; DS:SI - ExecBase
363
364
365;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
366 call DriveIO_LoadSector
367
368 clc
369 cmp wptr [si+LocBR_Magic], 0AA55h
370 je DIOLP_Success
371 ; We check, if we are scanning partitions. In that case, if CHS is not 0/0/1
372 ; we will display a "bad partition table" message and halt the system.
373 cmp cx, 0001h
374 jne DIOLP_Failed
375 or dh, dh
376 jnz DIOLP_Failed
377 stc ; Set carry, so no partition table
378 DIOLP_Success:
379
380
381
382 ret
383 DIOLP_Failed:
384 jmp DriveIO_GotLoadError
385DriveIO_LoadPartition EndP
386
387; #########################################################################
388; Routine: Writes a partition from ExecBase to its original sector
389; #########################################################################
390; Calling : none
391; Returns : none
392; Preserve: all registers
393; #########################################################################
394DriveIO_SavePartition Proc Near Uses ax bx cx dx si
395
396IFDEF AUX_DEBUG
397 IF 0
398 DBG_TEXT_OUT_AUX 'DriveIO_SavePartition:'
399 PUSHRF
400 ;~ call DEBUG_DumpRegisters
401 ;~ call AuxIO_DumpParagraph
402 ;~ call AuxIO_TeletypeNL
403 POPRF
404 ENDIF
405ENDIF
406
407 mov ax, wptr cs:[CurPartition_Location+0]
408 mov bx, wptr cs:[CurPartition_Location+2]
409 mov dx, wptr cs:[CurPartition_Location+4]
410 mov cx, wptr cs:[CurPartition_Location+6] ; Gets prev. saved location
411 mov si, offset PartitionSector ; DS:SI - ExecBase
412 cmp wptr [si+LocBR_Magic], 0AA55h ; Checks for signature, if not found
413 jne DIOSP_SevereError ; we assume a really bad error
414 call DriveIO_SaveSector
415 DIOSP_SevereError:
416 ret
417DriveIO_SavePartition EndP
418
419
420
421;##############################################################################
422;# The location of LVM sectors depends on the OS/2 geometry used when the disk
423;# was prepared. This geometry is present in the Master LVM sector, which has
424;# already been located if it exists. All partitions, whether primary or
425;# logical, have an entry in a partition table. For primary partitions this
426;# table is located in the MBR, while for logical partitions this table is
427;# located in the EBR for that logical partition. An LVM record is located
428;# LVM_SPT-1 sectors above an MBR or EBR. The Master LVM record contains the
429;# information for all primary partitions. For logical partitions, the LVM
430;# sector only has one entry, because EBRs are chained. The global LVM info,
431;# like disk-name, sectors per track, etc. is replicated between the Master
432;# LVM sector and LVM sectors corresponding to logical partitions. This info
433;# is kept in sync by the OS/2 LVM Engine.
434;##############################################################################
435;# ACTION : Attempts to load the corresponding LVM sector for a partition
436;# ----------------------------------------------------------------------------
437;# EFFECTS : Modifies DAP structure and fills or clears sector buffer
438;# ----------------------------------------------------------------------------
439;# IN : MEM - Location info is in [CurPartition_Location]
440;# ----------------------------------------------------------------------------
441;# OUT : CF=1 - failure, no valid LVM sector was loaded
442;# : SI - Points to the sector buffer ([LVMSector])
443;##############################################################################
444DriveIO_LoadLVMSector Proc Near Uses ax bx cx dx di
445
446IFDEF AUX_DEBUG
447 IF 1
448 DBG_TEXT_OUT_AUX 'DriveIO_LoadLVMSector:'
449 PUSHRF
450 call DEBUG_DumpRegisters
451 ;~ call AuxIO_DumpSector
452 ;~ call AuxIO_DumpParagraph
453 ;~ call AuxIO_TeletypeNL
454 POPRF
455 ENDIF
456ENDIF
457
458 ; Clear the sector buffer
459 mov si, offset [LVMSector]
460 call ClearSectorBuffer
461
462 ; Quit with CY if LVM is ignored in SETUP
463 test byte ptr [CFG_IgnoreLVM], 1 ; ZF=0 means ignore LVM
464 jnz DIOLLVMS_NoLVMSector ; Quit if so
465
466 ; Load the location of the current partition being acted upon.
467 ; Note that this is not the actual LBA of the partition, but the
468 ; sector that has the partition table that contains the entry
469 ; for the partition. In other words, for primary partitions the LBA
470 ; address points to the MBR while for extended partitions it points
471 ; to an EBR. In both cases the LVM sector is located LVM_SPT-1 above.
472 ; Also note that the BIOS CHS values (DH and CX) below are not used,
473 ; because we explicitly use LBA sector loading.
474 mov ax, wptr cs:[CurPartition_Location+0] ; LBA lo of MBR/EBR
475 mov bx, wptr cs:[CurPartition_Location+2] ; LBA hi of MBR/EBR
476 mov dx, wptr cs:[CurPartition_Location+4] ; BIOS disk num & head
477 mov cx, wptr cs:[CurPartition_Location+6] ; BIOS cyl & sec
478
479 ; Calculate the entry in the DISKINFO array for this disk,
480 ; and put the LVM_SPT in DI
481 push bx
482 call DriveIO_CalcDiskInfoPointer
483 mov di, [bx+LocDISKINFO_LVM_Secs]
484 pop bx
485
486 ; If the LVM_SPT is ZERO, no LVM info is present and we quit with CY
487 test di, di ; See if it is 0
488 jz DIOLLVMS_NoLVMSector ; Quit if so
489
490 ; Adjust the location to point to the LVM sector
491 add ax, di ; Add the LVM sectors-per-track
492 adc bx, 0 ; Propagate LBA lo overflow to LBA hi
493 sub ax, 1 ; LVM sector is located one sector below
494 sbb bx, 0 ; Propagate borrow to LBA hi
495
496 ; Load the LVM sector into [LVMSector]
497 mov si, offset [LVMSector] ; Points to sector buffer
498 mov di, ds ; Segment of that buffer
499 call DriveIO_ReadSectorLBA ; Read the LVM sector
500
501IFDEF AUX_DEBUG
502 IF 1
503 DBG_TEXT_OUT_AUX 'LVMSecLoaded'
504 PUSHRF
505 call DEBUG_DumpRegisters
506 call AuxIO_DumpSector
507 ;~ call AuxIO_DumpParagraph
508 ;~ call AuxIO_TeletypeNL
509 POPRF
510 ENDIF
511ENDIF
512
513 jc DIOLLVMS_NoLVMSector ; Quit on error
514
515 ; Check the validity of the LVM sector, quit with CY if invalid
516 call LVM_ValidateSector ; Check signature and CRC
517 jnc DIOLLVMS_NoLVMSector ; Quit if not valid
518
519 ; We're done, indicate success and return
520 clc
521 jmp DIOLLVMS_Done
522
523 DIOLLVMS_NoLVMSector:
524
525 ; Clear the sector buffer
526 mov si, offset [LVMSector]
527 call ClearSectorBuffer
528 mov bptr [si+LocLVM_SignatureStart], 0
529
530 ; Indicate no valid LVM sector was loaded
531 stc
532
533 DIOLLVMS_Done:
534
535 ret
536DriveIO_LoadLVMSector EndP
537
538
539
540; Keeps DS:SI for caller, saves at anytime w/o checks (!)
541DriveIO_SaveLVMSector Proc Near Uses ax bx cx dx
542
543IFDEF AUX_DEBUG
544 IF 0
545 DBG_TEXT_OUT_AUX 'DriveIO_SaveLVMSector:'
546 PUSHRF
547 ;~ call DEBUG_DumpRegisters
548 ;~ call AuxIO_DumpParagraph
549 ;~ call AuxIO_TeletypeNL
550 POPRF
551 ENDIF
552ENDIF
553
554 test byte ptr [CFG_IgnoreLVM], 1 ; We are supposed to ignore LVM, so
555 jnz DIOSLVMS_SevereError ; don't save at anytime (security!)
556 mov ax, wptr cs:[CurPartition_Location+0]
557 mov bx, wptr cs:[CurPartition_Location+2]
558 mov dx, wptr cs:[CurPartition_Location+4]
559 mov cx, wptr cs:[CurPartition_Location+6] ; Gets cur. partition location
560 call LVM_CheckSectorSignature
561 jnc DIOSLVMS_SevereError ; LVM Signature must be there
562
563IFDEF AUX_DEBUG
564 IF 0
565 pushf
566 pusha
567 ;~ dioatlvm db 'DriveIO_LVMAdjustToInfoSector',10,0
568 ;~ pushf
569 ;~ pusha
570 ;~ mov si,offset dioatlvm
571 ;~ call AuxIO_Print
572 ;~ popa
573 ;~ popf
574 call DEBUG_DumpRegisters
575 call DEBUG_DumpCHS
576 popa
577 popf
578 ENDIF
579ENDIF
580
581 call DriveIO_LVMAdjustToInfoSector
582
583IFDEF AUX_DEBUG
584 IF 0
585 pushf
586 pusha
587 call DEBUG_DumpRegisters
588 call DEBUG_DumpCHS
589 popa
590 popf
591 ENDIF
592ENDIF
593
594 mov si, offset LVMSector
595 call DriveIO_SaveSector
596 DIOSLVMS_SevereError:
597 ret
598DriveIO_SaveLVMSector EndP
599
600
601
602; Special error message instead of "LOAD ERROR" during partition scanning,
603; so users will notice that something is bad with their partition table(s)
604DriveIO_GotLoadError Proc Near
605 test byte ptr cs:[CurIO_Scanning], 1 ; Must be CS:, cause DS!=CS maybe here
606 jnz InScanMode
607 jmp MBR_LoadError
608 InScanMode:
609 mov si, offset TXT_BrokenPartitionTable
610 push cs
611 pop ds
612 call MBR_Teletype
613 mov si, offset BrokenHDD
614 sub dl, 50h ; 80h -> '0'
615 cmp dl, 39h
616 jbe DIOGLE_BelowA
617 add dl, 7 ; 3Ah -> 'A'
618 DIOGLE_BelowA:
619 mov bptr [si+5], dl
620 call MBR_Teletype
621
622 ; JWasm: cannot jump to local label in other procedure.
623 ; Changed to halt here.
624 ;jmp MBRLE_Halt
625 DriveIO_GotLoadError_halt:
626 jmp DriveIO_GotLoadError_halt
627DriveIO_GotLoadError EndP
628
629; #########################################################################
630; Routine: Loads a specified sector to DS:DI
631; #########################################################################
632; Calling : bx:ax - Absolute sector
633; cx:dx - Cylinder/Sector, Side/Drive (hi/lo-byte)
634; ds:si - Destination-Adress
635; Returns : none
636; Preserve: all registers
637; #########################################################################
638DriveIO_LoadSector Proc Near Uses ax bx cx dx ds si es di
639
640IFDEF AUX_DEBUG
641 IF 0
642 DBG_TEXT_OUT_AUX 'DriveIO_LoadSector:'
643 PUSHRF
644 call DEBUG_DumpRegisters
645 ;~ call AuxIO_DumpParagraph
646 ;~ call AuxIO_TeletypeNL
647 POPRF
648 ENDIF
649ENDIF
650
651 ; Is the drive not a harddrive?
652 cmp dl, 80h
653 jb DIOLS_UseNormal
654
655 test byte ptr cs:[CurIO_UseExtension], 1
656 jz DIOLS_UseNormal
657 ; Are we forced do use LBA via Setting?
658 jnz DIOLS_UseExtension
659
660 ; Upper 8 bits of LBA-address set?
661 ; Then use LBA (maximum boundary is 16320x16x63 = FB0400h)
662 or bh, bh
663 jnz DIOLS_UseExtension
664 ; Compare Switch-Table value to bit 16-23 of LBA-address
665 mov di, dx
666 and di, 007Fh
667 cmp bptr cs:[LBASwitchTable+di], bl
668 jbe DIOLS_UseExtension
669
670 DIOLS_UseNormal:
671
672IFDEF AUX_DEBUG
673 IF 0
674 DBG_TEXT_OUT_AUX 'DriveIO_ReadSectorCHS:'
675 PUSHRF
676 call DEBUG_DumpRegisters
677 call AuxIO_DumpParagraph
678 call AuxIO_TeletypeNL
679 POPRF
680 ENDIF
681ENDIF
682
683 mov di, 3 ; retry count
684 DIOLS_ErrorLoop:
685 push ds
686 pop es
687 mov bx, si ; ES:BX - Destination
688 mov ax, 0201h ; Function 2 - Load Sector
689 int 13h
690 jnc DIOLS_Success
691 dec di ; decrement retry count
692 jnz DIOLS_ErrorLoop
693
694 ; Sector load failed...
695 jmp DriveIO_GotLoadError
696
697 DIOLS_UseExtension:
698
699 mov di, ds ; segment for transfer address
700 call DriveIO_ReadSectorLBA ; extended read
701 jc DriveIO_GotLoadError ; halt on error
702
703 ;~ push cx
704 ;~ mov cs:[INT13X_DAP_NumBlocks], 1 ; Copy ONE sector
705 ;~ mov wptr cs:[INT13X_DAP_Transfer+0], si
706 ;~ mov cx, ds
707 ;~ mov wptr cs:[INT13X_DAP_Transfer+2], cx ; Fill out Transfer Adress
708 ;~ mov wptr cs:[INT13X_DAP_Absolute+0], ax
709 ;~ mov wptr cs:[INT13X_DAP_Absolute+2], bx ; Fill out Absolute Sector
710 ;~ push cs
711 ;~ pop ds
712 ;~ mov si, offset [INT13X_DAP]
713 ;~ mov ah, 42h ; Extended Read
714 ;~ int 13h
715 ;~ pop cx
716 ;~ jnc DIOLS_Success
717
718 ; Sector load failed...
719 ;~ jmp DriveIO_GotLoadError
720
721 DIOLS_Success:
722
723IFDEF AUX_DEBUG
724 IF 0
725 DBG_TEXT_OUT_AUX 'sector loaded'
726 PUSHRF
727 ;~ call DEBUG_DumpRegisters
728 ;~ call AuxIO_DumpSector
729 mov cx, 32
730 @@:
731 call AuxIO_DumpParagraph
732 call AuxIO_TeletypeNL
733 add si, 16
734 loop @B
735 POPRF
736 ENDIF
737ENDIF
738 ret
739DriveIO_LoadSector EndP
740
741
742;##############################################################################
743;# ACTION : Loads the Master Boot Record from the specified drive into buffer
744;# ----------------------------------------------------------------------------
745;# EFFECTS : Modifies DAP structure and fills or clears transfer buffer
746;# ----------------------------------------------------------------------------
747;# IN : DL - BIOS disk number (80h,81h,etc)
748;# : SI - Pointer to transfer buffer
749;# ----------------------------------------------------------------------------
750;# OUT : CF=1 - failure
751;# : AL.0 - MBR signature present
752;# : AL.1 - Primary partitions present
753;# : AL.2 - Extended partitions present
754;# : AL.3 - AiR-BOOT signature present
755;# : AL.4:7 - Reserved, returned as 0
756;# : AH.0:7 - Reserved, returned as 0
757;##############################################################################
758DriveIO_LoadMBR Proc Near uses bx cx dx si di
759
760 ; Always clear the transfer buffer first
761 call ClearSectorBuffer
762
763 ; Assume an invalid MBR
764 xor ax, ax
765
766 ; Accept only valid harddisks
767 call DriveIO_IsValidHarddisk
768 jc DriveIO_LoadMBR_exit
769
770 ; Save the address of the transfer buffer
771 mov di, si
772
773 ; Read the MBR from disk
774 xor ax, ax ; LBA low
775 xor bx, bx ; LBA high
776 xor dh, dh ; Head 0
777 mov cx, 1 ; Sector 1
778 call DriveIO_LoadSector ; Read the sector from disk
779
780 ; Check the loaded MBR for a signature
781 xor ax, ax ; Assume an invalid MBR
782 mov dx, [si+LocBR_Magic] ; Get word from MBR signature location
783 cmp dx, 0aa55h ; Is it the magic value ?
784 jne DriveIO_LoadMBR_exit ; Nope, no need to test anything else
785
786 ; Indicate we have a MBR signature
787 or al, 01h
788
789 ; Advance to the partition table
790 add si, 01beh
791
792 ; Total of 4 entries to check
793 mov cx, 4
794
795 DriveIO_LoadMBR_next_entry:
796 mov dl, [si+LocBRPT_SystemID] ; Get partition-type / system-id
797 add si, 10h ; Point to next entry
798 test dl, dl ; Nothing in this one ?
799 loopz DriveIO_LoadMBR_next_entry ; Then check next entry
800
801 ; All entries checked and last one was also empty, we're done
802 jz DriveIO_LoadMBR_check_ab
803
804 ; Found a non-empty entry, set bits according to its type
805 cmp dl, 05h ; Old style extended container ?
806 jne @F ; Nope...
807 or al, 04h ; Yep, mark ext. container present
808 @@: cmp dl, 0fh ; New style extended container ?
809 jne @F ; Nope...
810 or al, 04h ; Yep, mark ext. container present
811 @@: or al, 02h ; Then is must be a primary
812 jcxz DriveIO_LoadMBR_check_ab ; CX=0? Then all entries processed,
813 jmp DriveIO_LoadMBR_next_entry ; otherwise check next entry
814
815 ; Check if an AiR-BOOT signature is present
816 DriveIO_LoadMBR_check_ab:
817 mov si, offset [MBR_ABSig] ; Offset of AiR-BOOT signature
818 inc di ; Advance buffer pointer
819 inc di ; to AiR-BOOT signature location
820 mov cx, 7 ; Length of AiR-BOOT signature
821 cld ; Direction upwards
822 repe cmpsb ; Compare 7 bytes
823 jne DriveIO_LoadMBR_exit ; Nope, no AiR-BOOT on this disk
824 or al, 08h ; Yep, AiR-BOOT is on this disk
825 ;~ jmp DriveIO_LoadMBR_exit
826
827 DriveIO_LoadMBR_exit:
828 ret
829DriveIO_LoadMBR EndP
830
831
832;##############################################################################
833;# ACTION : Reads a sector from disk using INT13 extensions
834;# ----------------------------------------------------------------------------
835;# EFFECTS : Modifies DAP structure and fills transfer buffer
836;# ----------------------------------------------------------------------------
837;# IN : BX:AX - LBA address of sector
838;# : DI:SI - SEG:OFF of transfer buffer
839;# ----------------------------------------------------------------------------
840;# OUT : CF=1 - failure
841;##############################################################################
842DriveIO_ReadSectorLBA Proc Near
843
844IFDEF AUX_DEBUG
845 IF 0
846 DBG_TEXT_OUT_AUX 'DriveIO_ReadSectorLBA:'
847 PUSHRF
848 call DEBUG_DumpRegisters
849 call AuxIO_DumpParagraph
850 call AuxIO_TeletypeNL
851 POPRF
852 ENDIF
853ENDIF
854
855 ; Push all registers
856 pusha
857 push ds
858 push es
859
860 ; One sector to read
861 mov cs:[INT13X_DAP_NumBlocks], 1
862
863 ; Setup transfer address
864 mov wptr cs:[INT13X_DAP_Transfer+0], si ; offset
865 mov wptr cs:[INT13X_DAP_Transfer+2], di ; segment
866
867 ; Setup LBA64 address of requested sector
868 mov wptr cs:[INT13X_DAP_Absolute+0], ax ; low word lower part
869 mov wptr cs:[INT13X_DAP_Absolute+2], bx ; high word lower part
870 mov wptr cs:[INT13X_DAP_Absolute+4], 0 ; low word upper part
871 mov wptr cs:[INT13X_DAP_Absolute+6], 0 ; high word upper part
872
873 ; Address of packet
874 mov si, offset [INT13X_DAP] ; disk address packet
875
876 ; Do the extended read
877 mov ah, 42h ; read function
878 int 13h ; transfer to bios
879
880 ; Error occured
881 jc DriveIO_ReadSectorLBA_exit
882
883 ; AH should also be zero
884 test ah, ah
885 stc
886 jnz DriveIO_ReadSectorLBA_exit
887
888 ; Disk read succeeded, clear CF
889 clc
890
891 DriveIO_ReadSectorLBA_exit:
892
893 ; Pop all registers
894 pop es
895 pop ds
896 popa
897
898 ret
899DriveIO_ReadSectorLBA EndP
900
901
902
903;##############################################################################
904;# ACTION : Writes a sector to disk using INT13 extensions
905;# ----------------------------------------------------------------------------
906;# EFFECTS : Modifies DAP structure and mofifies the disk
907;# ----------------------------------------------------------------------------
908;# IN : BX:AX - LBA address of sector
909;# : DI:SI - SEG:OFF of transfer buffer
910;# ----------------------------------------------------------------------------
911;# OUT : CF=1 - failure
912;##############################################################################
913DriveIO_WriteSectorLBA Proc Near
914
915IFDEF AUX_DEBUG
916 IF 0
917 DBG_TEXT_OUT_AUX 'DriveIO_WriteSectorLBA:'
918 PUSHRF
919 call DEBUG_DumpRegisters
920 call AuxIO_DumpParagraph
921 call AuxIO_TeletypeNL
922 POPRF
923 ENDIF
924ENDIF
925
926 ; Push all registers
927 pusha
928 push ds
929 push es
930
931 ; One sector to read
932 mov cs:[INT13X_DAP_NumBlocks], 1
933
934 ; Setup transfer address
935 mov wptr cs:[INT13X_DAP_Transfer+0], si ; offset
936 mov wptr cs:[INT13X_DAP_Transfer+2], di ; segment
937
938 ; Setup LBA64 address of requested sector
939 mov wptr cs:[INT13X_DAP_Absolute+0], ax ; low word lower part
940 mov wptr cs:[INT13X_DAP_Absolute+2], bx ; high word lower part
941 mov wptr cs:[INT13X_DAP_Absolute+4], 0 ; low word upper part
942 mov wptr cs:[INT13X_DAP_Absolute+6], 0 ; high word upper part
943
944 ; Address of packet
945 mov si, offset [INT13X_DAP] ; disk address packet
946
947 ; Do the extended write
948 xor al, al ; no write verify
949 mov ah, 43h ; write function
950 int 13h ; transfer to bios
951
952 ; Error occured
953 jc DriveIO_WriteSectorLBA_exit
954
955 ; AH should also be zero
956 test ah, ah
957 stc
958 jnz DriveIO_WriteSectorLBA_exit
959
960 ; Disk write succeeded, clear CF
961 clc
962
963 DriveIO_WriteSectorLBA_exit:
964
965 ; Pop all registers
966 pop es
967 pop ds
968 popa
969
970 ret
971DriveIO_WriteSectorLBA EndP
972
973
974
975
976;##############################################################################
977;# The Master LVM sector is *not* necessarily located at the end of the BIOS
978;# view of TRACK0. Its location depends on the *OS/2 geometry* active when the
979;# disk was partitioned. For disks < 502MiB this will most likely be LBA sector
980;# 62, but for disks >502MiB, *extended* OS/2 geometry was used and DANIS506
981;# uses SPT=127 for disks < 1TiB while IBMS506 uses SPT=255.
982;# When a huge disk < 1TiB was partitioned with IBMS506, thus using SPT=255,
983;# and the driver was later changed to DANIS506, DANI uses SPT=255, eventhough
984;# the disk < 1TiB. Whether it is DANI that is LVM aware or something else
985;# (maybe LVM itself) that makes DANI use the correct geometry has yet to be
986;# investigated.
987;#
988;# Related geometry issues are also present with USB sticks, which can get
989;# assigned a geometry by OS/2, which can depend if the stick was partitioned
990;# on foreign systems or not, or even OS/2 manufacturing a geometry that is not
991;# the same as the BIOS reports to us here. In both cases, fixed disks and
992;# removable disks, the geometry recorded in the BPB of a partition can also
993;# influence the geometry that OS/2 assigns. This is the case when 'preparing'
994;# disks for LVM use, in which case BPB values could be incorporated.
995;#
996;# What this all boils down to, is that the geometry reported by the BIOS is
997;# of no practical use, especially not when taking BIOS USB MSD emulation into
998;# account. These are among the reasons why AirBoot needs to use LBA addressing
999;# when handling LVM stuff and why LBA use cannot be disabled in the SETUP
1000;# anymore.
1001;#
1002;# So, a Master LVM sector can be present on any sector from LBA 254 downwards
1003;# and the only way to locate the correct one is to scan all the way down and,
1004;# if one is found, do proper validation on its values, because it may also be
1005;# a 'phantom' LVM sector left over from previous partition layouts.
1006;# Most of such 'phantoms' can be filtered out by verifying the location of
1007;# the found sector against the OS/2 geometry it specifies itself, which means
1008;# it must be located at the LBA of the SPT-1 it specifies.
1009;##############################################################################
1010;# ACTION : Locates the Master LVM sector on the specified disk
1011;# ----------------------------------------------------------------------------
1012;# EFFECTS : Leaves [Scratch] with last sector read or cleared
1013;# ----------------------------------------------------------------------------
1014;# IN : DL - BIOS disk number of drive to search
1015;# ----------------------------------------------------------------------------
1016;# OUT : CF=1 - found
1017;# : BX:AX - LBA address of LVM sector if found, 0 otherwise
1018;##############################################################################
1019DriveIO_LocateMasterLVMSector Proc Near uses cx dx si di ds es
1020
1021IFDEF AUX_DEBUG
1022 IF 1
1023 DBG_TEXT_OUT_AUX 'DriveIO_LocateMasterLVMSector:'
1024 PUSHRF
1025 call DEBUG_DumpRegisters
1026 ;~ call AuxIO_DumpSector
1027 ;~ call AuxIO_DumpParagraph
1028 ;~ call AuxIO_TeletypeNL
1029 POPRF
1030 ENDIF
1031ENDIF
1032
1033 ; LBA address to start scanning down from
1034 mov cx, 255
1035
1036 ; Make sure ES==DS
1037 push ds
1038 pop es
1039
1040 ; Because JCXZ is used, LBA sector 0 is never loaded and checked.
1041 ; This is of course no problem since it is the MBR.
1042 DriveIO_LocateMasterLVMSector_next:
1043 mov si, offset [Scratch] ; Use scratch area to load sectors
1044 call ClearSectorBuffer ; Clear the scratch area
1045 clc ; Indicate Master LVM sector not found
1046 jcxz DriveIO_LocateMasterLVMSector_done
1047
1048 ; Read the LBA sector specified in CX
1049 mov ax, cx ; LBA low
1050 xor bx, bx ; LBA high
1051 mov di, ds ; Segment of scratch buffer
1052 mov si, offset [Scratch] ; Offset of scratch buffer
1053 call DriveIO_ReadSectorLBA ; Read the sector
1054 lahf ; Save CF
1055 dec cx ; Prepare LBA of next sector to read
1056 sahf ; Restore CF
1057 ; No need to do any LVM sector validation when read error, read next
1058 jc DriveIO_LocateMasterLVMSector_next
1059
1060 ; See if the read sector has a valid signature and checksum
1061 call LVM_ValidateSector
1062
1063 ; NC indicates invalid or none-LVM sector, read next
1064 jnc DriveIO_LocateMasterLVMSector_next
1065
1066 ; We have found a valid LVM sector !
1067 ; So it contains the OS/2 geometry for the disk.
1068 ; That means this LVM sector itself must be located on the last sector
1069 ; of the SPT value its OS/2 geometery specifies, which, in LBA terms
1070 ; is LVM SPT-1 -- let's check that...
1071 mov bx, offset [Scratch] ; Offset of the loaded LVM sector
1072 mov al, [bx+LocLVM_Secs] ; Get the LVM SPT value (<=255)
1073 dec al ; Adjust to LVM LBA
1074 mov ah, cl ; Get next LVM LBA to search
1075 inc ah ; This one was found here
1076 cmp al, ah ; If same, LVM LBA location OK
1077 jne DriveIO_LocateMasterLVMSector_next
1078
1079 ; The LVM sector we found is at the location it should be on disk,
1080 ; so it's almost 99% sure this is the correct one.
1081 ; Now we should compare the start and sizes of the partitions in the
1082 ; MBR with the partitions specified in this LVM record.
1083 ; We'll implement that later after some more research.
1084 ; For now we assume this is the correct Master LVM sector for the disk.
1085 inc cx ; CX was prepared to read next, correct that
1086 stc ; Indicate we have found the Master LVM sector
1087
1088 DriveIO_LocateMasterLVMSector_done:
1089 mov bx, 0 ; A Master LVM sector always has high LBA=0
1090 mov ax, cx ; Low LBA of Master LVM sector
1091
1092 ; We leave it up to the caller to store the value in a proper place
1093 ret
1094DriveIO_LocateMasterLVMSector EndP
1095
1096
1097
1098;
1099; ############################################################
1100; # Check for a valid MBR-sector to be written to disk #
1101; ############################################################
1102;
1103; In
1104; --
1105; DL = Physical Disk
1106; BX:CX = LBA sector
1107; DI:SI = Source buffer
1108;
1109; Out
1110; ---
1111; CY = 1 if invalid MBR in source buffer, 0 if valid
1112;
1113; This routine is called when DriveIO_SaveSector attempts to write to the MBR.
1114; It checks if the sector to be written has some sensible values in certain
1115; places. In fact, if the sector is written to the boot-disk, the AiR-BOOT
1116; signature should be present and the partition table should be the same
1117; as the one at the start of the AiR-BOOT code in memory, except maybe for the
1118; active flags.
1119; For other disks, only the active flags are checked to be 00h or 80h and
1120; the AA55h MBR signature.
1121;
1122DriveIO_ProtectMBR Proc Near
1123 pusha ; Push all registers
1124 push es ; Push ES because we need it for string instructions
1125 push cs ; Make ES point...
1126 pop es ; to CS
1127
1128 ; Save the pointer to the sector to write in BX
1129 mov bx,si
1130
1131 ;
1132 ; If the sector to be written is not the boot-disk, then skip
1133 ; checking the AiR-BOOT MBR.
1134 ;
1135 cmp dl, [BIOS_BootDisk]
1136 jne DriveIO_ProtectMBR_is_not_bootdisk
1137
1138 ;
1139 ; The boot-disk is accessed so the sector to be written must be
1140 ; the AiR-BOOT MBR. This is the same as the first 512 bytes
1141 ; relocated to 8000:0000 and this the start of the AB-code.
1142 ;
1143 mov si,bx ; Get pointer to sector to write
1144 xor di,di ; Point DI to start of AB-code (MBR)
1145 mov cx, offset [MBR_PartTable] ; Bytes upto P-table must be same
1146 cld ; Compare upwards
1147 repe cmpsb ; Compare upto P-table
1148
1149 ; If not the same this is not the an AiR-BOOT boot-disk MBR !
1150 jne DriveIO_ProtectMBR_not_valid_MBR ; SEVERE ERROR !
1151
1152 ; Continue with signature check
1153 jmp DriveIO_ProtectMBR_check_signature
1154
1155
1156 ;
1157 ; The disk is not the boot-disk so we don't know what kind of MBR is on it.
1158 ; Some sanity checks should be here.
1159 ;
1160 DriveIO_ProtectMBR_is_not_bootdisk:
1161
1162 ;
1163 ; sanity checks...
1164 ;
1165
1166 ; Continue with signature check
1167 jmp DriveIO_ProtectMBR_check_signature
1168
1169
1170 DriveIO_ProtectMBR_check_signature:
1171 ; See if the sector to write contains a valid signature
1172 mov si,bx ; Get pointer to sector to write
1173 mov di, offset [MBR_Sig] ; Offset to MBR signature
1174 add si,di ; Make SI point to it in sec to write
1175 lodsw ; Load it
1176 cmp ax,0aa55h ; See if it is valid
1177
1178 ; If no signature this cannot be a valid MBR !
1179 jne DriveIO_ProtectMBR_not_valid_MBR ; SEVERE ERROR !
1180
1181
1182 ;
1183 ; The sector to be written seems to be valid.
1184 ; Set CY=0 to indicate a valid MBR.
1185 ;
1186 DriveIO_ProtectMBR_is_valid_MBR:
1187 clc
1188 jmp DriveIO_ProtectMBR_end
1189
1190 ;
1191 ; Something is terribly wrong; a non-MBR sector seems about to be written.
1192 ; Set CY=1 and let the calling code handle this situation.
1193 ;
1194 DriveIO_ProtectMBR_not_valid_MBR:
1195 stc
1196 jmp DriveIO_ProtectMBR_end
1197
1198 ;
1199 ; Return to the caller with no registers modyfied except FLAGS.
1200 ;
1201 DriveIO_ProtectMBR_end:
1202 pop es
1203 popa
1204 ret
1205DriveIO_ProtectMBR Endp
1206
1207
1208
1209; #########################################################################
1210; Routine: Checks if the MBR is addressed by either CHS or LBA
1211; #########################################################################
1212; Calling : bx:ax - Absolute sector
1213; cx:dx - Cylinder/Sector, Side/Drive (hi/lo-byte)
1214; Returns : ZF=1 if MBR is addressed, else ZF=0
1215; Preserve: all registers
1216; #########################################################################
1217DriveIO_MBR_Addressed Proc
1218 push ax
1219 push bx
1220
1221 or bx,ax ; Results in 0 in BX if MBR is addressed by LBA
1222 jz DriveIO_MBR_Addressed_done
1223
1224 mov ax,cx ; Results in 1 in AX if CYL 0, SEC 1 is addressed
1225 add al,dh ; Results in 1 in AX if HEAD 0 is addressed
1226 dec ax ; Results in 0 in AX if MBR is addressed by CHS
1227
1228 DriveIO_MBR_Addressed_done:
1229 pop bx
1230 pop ax
1231 ret
1232DriveIO_MBR_Addressed EndP
1233
1234
1235
1236
1237; #########################################################################
1238; Routine: Writes DS:SI to a specified sector
1239; #########################################################################
1240; Calling : bx:ax - Absolute sector
1241; cx:dx - Cylinder/Sector, Side/Drive (hi/lo-byte)
1242; ds:si - Source-Adress
1243; Returns : none
1244; Preserve: all registers
1245; #########################################################################
1246DriveIO_SaveSector Proc Near Uses ax bx cx dx ds si es di
1247
1248IFDEF AUX_DEBUG
1249 IF 0
1250 DBG_TEXT_OUT_AUX 'DriveIO_SaveSector:'
1251 PUSHRF
1252 ;~ call DEBUG_DumpRegisters
1253 ;~ call AuxIO_DumpParagraph
1254 ;~ call AuxIO_TeletypeNL
1255 POPRF
1256 ENDIF
1257ENDIF
1258
1259
1260;!
1261;! DEBUG_BLOCK
1262;! Force write to LBA0 to test interception routine.
1263;! Do *NOT* enable unless you are debugging, will overwrite MBR !
1264;!
1265IFDEF AUX_DEBUG
1266 IF 0
1267 pushf
1268 pusha
1269 xor ax,ax
1270 xor bx,bx
1271 xor cx,cx
1272 inc cx
1273 xor dh,dh
1274 popa
1275 popf
1276 ENDIF
1277ENDIF
1278
1279 ;
1280 ; Check if the MBR is the destination for the write.
1281 ; ZF=1 if so.
1282 ;
1283 call DriveIO_MBR_Addressed
1284 jnz DriveIO_SaveSector_continue_write
1285
1286
1287 ; MBR is addressed, check the sector that is requested to be written.
1288 ; For the bootdisk it should contain the AiR-BOOT signature, valid
1289 ; partition-table entries and the AA55h signature.
1290 ; If not, something is terribly wrong in some piece of the AB code.
1291 ; For any other disk (80h+) at least a valid partition table should
1292 ; be present together with the AA55h signature.
1293 call DriveIO_ProtectMBR
1294 jnc DriveIO_SaveSector_continue_write
1295
1296
1297 ;
1298 ; WE HAVE A SEVERE ERROR CONDITION !
1299 ; SOME AB CODE TRIES TO WRITE A NON-MBR TO THE DISK !
1300 ; ASK THE USER TO REPORT THIS !
1301 ; HALT THE SYSTEM !
1302 ;
1303
1304 ; Show error-box
1305 mov cx, 0C04h
1306 mov si, offset NonMBRwrite
1307 call SETUP_ShowErrorBox
1308 mov cx, 0C04h
1309 mov si, offset NonMBRwrite_rep
1310 call SETUP_ShowErrorBox
1311
1312
1313IFDEF AUX_DEBUG
1314 IF 0
1315 pushf
1316 pusha
1317 mov si, offset [NonMBRwrite]
1318 call AuxIO_TeletypeNL
1319 call AuxIO_Print
1320 call AuxIO_TeletypeNL
1321 popa
1322 popf
1323 ENDIF
1324ENDIF
1325
1326 ; Show popup and halt the system.
1327 jmp HaltSystem
1328
1329
1330
1331 ;
1332 ; Continue the write if not MBR sector or MBR to write is validated.
1333 ;
1334 DriveIO_SaveSector_continue_write:
1335 test byte ptr cs:[CurIO_UseExtension], 1
1336 jz DIOSS_UseNormal
1337 ; Are we forced do use LBA via Setting?
1338 ; Always use INT13X on v1.0.8+.
1339 ;~ test byte ptr cs:[CFG_ForceLBAUsage], 1
1340 ;~ jnz DIOSS_UseExtension
1341 jmp DIOSS_UseExtension
1342 ; Is the drive not a harddrive?
1343 cmp dl, 80h
1344 jb DIOSS_UseNormal
1345 ; Upper 8 bits of LBA-address set? -> Use LBA (maximum boundary is FB0400h)
1346 or bh, bh
1347 jnz DIOSS_UseExtension
1348 ; Compare Switch-Table value to bit 16-23 of LBA-address
1349 mov di, dx
1350 and di, 007Fh
1351 cmp bptr cs:[LBASwitchTable+di], bl
1352 jbe DIOSS_UseExtension
1353
1354 DIOSS_UseNormal:
1355
1356IFDEF AUX_DEBUG
1357 IF 0
1358 DBG_TEXT_OUT_AUX 'DriveIO_WriteSectorCHS:'
1359 PUSHRF
1360 call DEBUG_DumpRegisters
1361 call AuxIO_DumpParagraph
1362 call AuxIO_TeletypeNL
1363 POPRF
1364 ENDIF
1365ENDIF
1366
1367 mov di, 3 ; retry count
1368 DIOSS_ErrorLoop:
1369 push ds
1370 pop es
1371 mov bx, si ; ES:BX - Destination
1372 mov ax, 0301h ; Function 3 - Write Sector
1373 int 13h
1374 jnc DIOSS_Success
1375 dec di ; decrement retry count
1376 jnz DIOSS_ErrorLoop
1377 call MBR_SaveError
1378
1379 DIOSS_UseExtension:
1380
1381 mov di, ds ; segment for transfer address
1382 call DriveIO_WriteSectorLBA ; extended write
1383 jc MBR_SaveError ; halt on error
1384
1385 ;~ push cx
1386 ;~ mov cs:[INT13X_DAP_NumBlocks], 1 ; Copy ONE sector
1387 ;~ mov wptr cs:[INT13X_DAP_Transfer+0], si
1388 ;~ mov cx, ds
1389 ;~ mov wptr cs:[INT13X_DAP_Transfer+2], cx ; Fill out Transfer Adress
1390 ;~ mov wptr cs:[INT13X_DAP_Absolute+0], ax
1391 ;~ mov wptr cs:[INT13X_DAP_Absolute+2], bx ; Fill out Absolute Sector
1392 ;~ push cs
1393 ;~ pop ds
1394 ;~ mov si, offset [INT13X_DAP]
1395 ;~ mov ax, 4300h ; Extended Write (No Verify)
1396 ;~ int 13h
1397 ;~ pop cx
1398 ;~ jnc DIOSS_Success
1399 ;~ call MBR_SaveError
1400
1401 DIOSS_Success:
1402 ret
1403DriveIO_SaveSector EndP
1404
1405
1406
1407
1408;##############################################################################
1409;# When a disk has a Master LVM sector, it means it has been prepared for use
1410;# by OS/2 and contains important information about how OS/2 views its geometry
1411;# and other disk related properties. This function assumes the LBA address
1412;# of the Master LVM sector has already been located and simply loads the
1413;# sector into [LVMSector].
1414;#
1415;# Note that because this is an operation similar to the regular loading of
1416;# sectors, the disk I/O semantics are used here. This means CF=0 when an LVM
1417;# sector is successfully loaded and CF=1 otherwise.
1418;##############################################################################
1419;# ACTION : Loads the Master LVM sector if one exists
1420;# ----------------------------------------------------------------------------
1421;# EFFECTS : Modifies DAP structure and [LVMSector]
1422;# ----------------------------------------------------------------------------
1423;# IN : DL - BIOS disk number (80h,81h,etc)
1424;# ----------------------------------------------------------------------------
1425;# OUT : CF=0 - Valid Master LVM sector found and loaded
1426;##############################################################################
1427DriveIO_LoadMasterLVMSector Proc Near
1428
1429IFDEF AUX_DEBUG
1430 IF 0
1431 DBG_TEXT_OUT_AUX 'DriveIO_LoadMasterLVMSector:'
1432 PUSHRF
1433 ;~ call DEBUG_DumpRegisters
1434 ;~ call AuxIO_DumpParagraph
1435 ;~ call AuxIO_TeletypeNL
1436 POPRF
1437 ENDIF
1438ENDIF
1439
1440 ; Save all registers
1441 pusha
1442
1443 ; Check if BIOS disk number is valid
1444 call DriveIO_IsValidHarddisk
1445 jc DriveIO_LoadMasterLVMSector_error
1446
1447 ; Calculate the entry in the DISKINFO array for this disk
1448 call DriveIO_CalcDiskInfoPointer
1449
1450 ; Save the entry for later recalls
1451 mov bp, bx
1452
1453 ; Get the LBA address of the Master LVM sector
1454 mov ax, [bx+LocDISKINFO_LVM_MasterLBA+00h]
1455 mov bx, [bx+LocDISKINFO_LVM_MasterLBA+02h]
1456
1457 ; LBA of Master LVM sector cannot be 0, so none was found during
1458 ; the gathering of disk information.
1459 mov cx, ax
1460 or cx, bx
1461 jz DriveIO_LoadMasterLVMSector_error
1462
1463 ; Load it into [LVMSector]
1464 mov di, ds
1465 mov si, offset [LVMSector]
1466 call DriveIO_ReadSectorLBA
1467 jc DriveIO_LoadMasterLVMSector_error
1468
1469 ; Validate the Master LVM sector
1470 call LVM_ValidateSector
1471
1472 ; Complement success indicator to conform to semantics of this function
1473 cmc
1474
1475 ; Master LVM sector was valid and is now loaded in [LVMSector]
1476 jnc DriveIO_LoadMasterLVMSector_ret
1477
1478 DriveIO_LoadMasterLVMSector_error:
1479
1480 ; Clear the sector buffer for safety reasons
1481 mov si, offset [LVMSector]
1482 call ClearSectorBuffer
1483
1484 ; Indicate no Master LVM sector loaded
1485 stc
1486
1487 DriveIO_LoadMasterLVMSector_ret:
1488
1489 ; Restore all registers
1490 popa
1491
1492 ret
1493DriveIO_LoadMasterLVMSector Endp
1494
1495
1496
1497
1498;##############################################################################
1499;# There is much information to know about the connected disks.
1500;# We also want this information clustered per disk and available before
1501;# further disk and partition scanning takes place.
1502;# This function gathers such information like INT13, INT13X, MBR, LVM, more.
1503;# Especially important is the LVM information, because that contains the
1504;# geometry OS/2 uses to access the disk. Other important information is the
1505;# presence of valid MBRs, logical partitions and whatnot.
1506;# This function gathers such information and stores it in a DISKINFO structure
1507;# for which an instance exists for every disk found.
1508;##############################################################################
1509;# ACTION : Gather disk information and store this in the BSS
1510;# ----------------------------------------------------------------------------
1511;# EFFECTS : Modifies DAP structure and the buffers it uses, fills DISKINFO[n]
1512;# ----------------------------------------------------------------------------
1513;# IN : DL - BIOS disk number (80h,81h,etc)
1514;# ----------------------------------------------------------------------------
1515;# OUT : CF=1 - failure
1516;##############################################################################
1517DriveIO_GatherDiskInfo Proc Near
1518
1519IFDEF AUX_DEBUG
1520 IF 1
1521 DBG_TEXT_OUT_AUX 'DriveIO_GatherDiskInfo:'
1522 PUSHRF
1523 call DEBUG_DumpRegisters
1524 ;~ call AuxIO_DumpParagraph
1525 ;~ call AuxIO_TeletypeNL
1526 POPRF
1527 ENDIF
1528ENDIF
1529
1530 ; Push all registers we use
1531 pusha
1532 push ds
1533 push es
1534
1535 ; Make sure ES=DS
1536 push ds
1537 pop es
1538
1539 ; Check if BIOS disk number is valid
1540 call DriveIO_IsValidHarddisk
1541 jc DriveIO_GatherDiskInfo_error
1542
1543 ; Calculate the entry in the DISKINFO array for this disk
1544 call DriveIO_CalcDiskInfoPointer
1545
1546 ; Save the entry for later recalls
1547 mov bp, bx
1548
1549 ; Store the BIOS disk number in the structure
1550 mov [bx+LocDISKINFO_DiskNum], dl
1551
1552; ------------------------------------------------------------------- [ INT13 ]
1553
1554 ; Get BIOS Disk Parameters (legacy method)
1555 mov ah, 08h ; Get Disk Parameters
1556 int 13h ; Call BIOS
1557
1558 ; CF=1 or AH!=0 indicates error
1559 jc DriveIO_GatherDiskInfo_error
1560 test ah, ah
1561 jnz DriveIO_GatherDiskInfo_error
1562
1563 ; Recall DISKINFO entry
1564 mov bx, bp
1565
1566 ; Store SPT (WORD)
1567 xor ah, ah ; Zero extend SPT to 16 bits
1568 mov al, cl ; Hi 2 bits max cyl and max sec
1569 and al, 3fh ; Mask max sec (1-based)
1570 mov [bx+LocDISKINFO_I13_Secs], ax ; Store SPT
1571
1572 ; Store HEADS (WORD)
1573 xor dl, dl ; Zero extend HEADS to 16 bits
1574 xchg dl, dh ; Get max head (0-based)
1575 inc dx ; Head count
1576 mov [bx+LocDISKINFO_I13_Heads], dx ; Store HEADS
1577
1578 ; Store CYLS (WORD)
1579 shr cl, 6 ; Hi 2 bits of max cyl to 1:0
1580 xchg cl, ch ; Max cyl (0-based)
1581 inc cx ; Cyl count
1582 mov [bx+LocDISKINFO_I13_Cyls], cx ; Store CYLS
1583
1584 ; Recall BIOS disk number
1585 mov dl, [bx+LocDISKINFO_DiskNum]
1586
1587; ------------------------------------------------------------------ [ INT13X ]
1588
1589 ; Get BIOS Disk Parameters (extended method)
1590 mov si, offset [Scratch] ; Buffer to return disk info
1591 mov ax, 80h ; Size of buffer
1592 mov [si], ax ; Store it in first word
1593 mov ah, 48h ; Get Extended Disk Parameters
1594 int 13h ; Call BIOS
1595
1596 ; CF=1 or AH!=0 indicates error
1597 jc DriveIO_GatherDiskInfo_error
1598 test ah, ah
1599 jnz DriveIO_GatherDiskInfo_error
1600
1601 ; Store flags (WORD)
1602 cld ; Direction up
1603 lodsw ; Buffersize, discard
1604 lodsw ; Flags (CHS valid etc)
1605 mov [bx+LocDISKINFO_I13X_Flags], ax ; Store them
1606
1607 ; Store CYLS (DWORD)
1608 lodsw ; Cyl count low
1609 mov [bx+LocDISKINFO_I13X_Cyls+00h], ax ; Store CYLS low
1610 lodsw ; Cyl count high
1611 mov [bx+LocDISKINFO_I13X_Cyls+02h], ax ; Store CYLS high
1612
1613 ; Store HEADS (DWORD)
1614 lodsw ; Head count low
1615 mov [bx+LocDISKINFO_I13X_Heads+00h], ax ; Store HEADS low
1616 lodsw ; Head count high
1617 mov [bx+LocDISKINFO_I13X_Heads+02h], ax ; Store HEADS high
1618
1619 ; Store SPT (DWORD)
1620 lodsw ; Secs per track low
1621 mov [bx+LocDISKINFO_I13X_Secs+00h], ax ; Store SPT low
1622 lodsw ; Secs per track high
1623 mov [bx+LocDISKINFO_I13X_Secs+02h], ax ; Store SPT high
1624
1625 ; Store total LBA sectors (QWORD)
1626 lea di, [bx+LocDISKINFO_I13X_SecsLBA]
1627 mov cx, 4
1628 rep movsw
1629
1630 ; Store sector size (WORD)
1631 lodsw
1632 mov [bx+LocDISKINFO_I13X_SecSize], ax
1633
1634 ; Store bus name (4 bytes, space padded, v3.0+)
1635 lea si, [Scratch+24h]
1636 lea di, [bx+LocDISKINFO_I13X_HostBus]
1637 movsw
1638 movsw
1639
1640 ; Store interface name (8 bytes, space padded, v3.0+)
1641 lea di, [bx+LocDISKINFO_I13X_Interface]
1642 mov cx, 4
1643 rep movsw
1644
1645 ; Should gather some more INT13X info here,
1646 ; like maybe Advanced Format stuff or so.
1647 ; We'll investigate that at a later time.
1648
1649; --------------------------------------------------------------------- [ MBR ]
1650
1651 ; Load the MBR
1652 mov si, offset [TmpSector]
1653 call DriveIO_LoadMBR
1654
1655 ; Store MBR flags (valid sig, partitions present, airboot installed)
1656 mov [bx+LocDISKINFO_MbrFlags], al
1657
1658 ; Recall BIOS disk number
1659 mov dl, [bx+LocDISKINFO_DiskNum]
1660
1661; --------------------------------------------------------------------- [ LVM ]
1662
1663 ; Locate the Master LVM sector, if any
1664 call DriveIO_LocateMasterLVMSector
1665
1666 ; Save Master LVM sector LBA high
1667 mov cx, bx
1668
1669 ; Recall DISKINFO entry
1670 mov bx, bp
1671
1672 ; Store Master LVM sector LBA
1673 mov [bx+LocDISKINFO_LVM_MasterLBA+00h], ax
1674 mov [bx+LocDISKINFO_LVM_MasterLBA+02h], cx
1675
1676 ; No Master LVM sector found, so skip storing LVM info for this disk
1677 jnc DriveIO_GatherDiskInfo_no_master_lvm
1678
1679 ; Load the Master LVM sector into [LVMSector]
1680 call DriveIO_LoadMasterLVMSector
1681
1682 ; No valid Master LVM sector, so skip storing LVM info for this disk
1683 jc DriveIO_GatherDiskInfo_no_master_lvm
1684
1685 ; A valid Master LVM sector has been loaded into [LVMSector]
1686 mov si, offset [LVMSector]
1687
1688 ; Get the number of sectors per track (OS/2 geometry)
1689 mov ax, [si+LocLVM_Secs+00h]
1690 mov cx, [si+LocLVM_Secs+02h]
1691
1692 ; Store it
1693 mov [bx+LocDISKINFO_LVM_Secs+00h], ax
1694 mov [bx+LocDISKINFO_LVM_Secs+02h], cx
1695
1696 ; Get the number of heads (OS/2 geometry)
1697 mov ax, [si+LocLVM_Heads+00h]
1698 mov cx, [si+LocLVM_Heads+02h]
1699
1700 ; Store it
1701 mov [bx+LocDISKINFO_LVM_Heads+00h], ax
1702 mov [bx+LocDISKINFO_LVM_Heads+02h], cx
1703
1704 ; Should gather some more LVM info here,
1705 ; like OS/2 extended geometry and other flags.
1706 ; We'll implement that at a later time.
1707
1708 DriveIO_GatherDiskInfo_no_master_lvm:
1709
1710 ; When no Master LVM sector was found,
1711 ; the LVM info in the DISKINFO structure for the disk
1712 ; will be ZERO because the area was cleared in PRECRAP.
1713
1714 ; Indicate success
1715 clc
1716
1717 jmp DriveIO_GatherDiskInfo_ret
1718
1719 DriveIO_GatherDiskInfo_error:
1720 stc
1721 DriveIO_GatherDiskInfo_ret:
1722
1723
1724IFDEF AUX_DEBUG
1725 IF 1
1726 DBG_TEXT_OUT_AUX '[DISKINFO]'
1727 PUSHRF
1728 call DEBUG_DumpRegisters
1729 ;~ call AuxIO_DumpParagraph
1730 ;~ call AuxIO_TeletypeNL
1731 mov si, bp
1732 mov cx, 4
1733 @@:
1734 call AuxIO_DumpParagraph
1735 call AuxIO_TeletypeNL
1736 add si, 16
1737 loop @B
1738 mov si, offset [Scratch]
1739 mov cx, 4
1740 @@:
1741 call AuxIO_DumpParagraph
1742 call AuxIO_TeletypeNL
1743 add si, 16
1744 loop @B
1745 mov si, offset [LVMSector]
1746 mov cx, 7
1747 @@:
1748 call AuxIO_DumpParagraph
1749 call AuxIO_TeletypeNL
1750 add si, 16
1751 loop @B
1752 POPRF
1753 ENDIF
1754ENDIF
1755
1756 ; Restore registers
1757 pop es
1758 pop ds
1759 popa
1760
1761 ret
1762DriveIO_GatherDiskInfo EndP
1763
1764;------------------------------------------------------------------------------
1765; Scan all disks to gather information
1766;------------------------------------------------------------------------------
1767; IN : None
1768; OUT : CF=1 - some failure occured
1769; : ZF=1 - no harddisks
1770; NOTE : This does the preliminary gathering of disk information
1771;------------------------------------------------------------------------------
1772DriveIO_ScanDisks Proc Near
1773
1774IFDEF AUX_DEBUG
1775 IF 1
1776 DBG_TEXT_OUT_AUX 'DriveIO_ScanDisks:'
1777 PUSHRF
1778 call DEBUG_DumpRegisters
1779 ;~ call AuxIO_DumpParagraph
1780 ;~ call AuxIO_TeletypeNL
1781 POPRF
1782 ENDIF
1783ENDIF
1784
1785 ; Save all registers
1786 pusha
1787
1788 ; Get number of disks in DH
1789 call DriveIO_GetHardDriveCount
1790
1791 ; Check if there are any disks to scan
1792 xor cx, cx ; Prepare 16-bit counter
1793 mov cl, dh ; Number of disks now in CX
1794 jcxz DriveIO_ScanDisks_end ; Quit if no disks
1795
1796 ; Scan disks from 80h upward
1797 mov dl, 80h ; BIOS number of first disk
1798 DriveIO_ScanDisks_next:
1799 call DriveIO_GatherDiskInfo ; Gather info for this disk
1800 jc DriveIO_ScanDisks_end ; Quit if some error occured
1801 inc dl ; Advance to next disk
1802 loop DriveIO_ScanDisks_next ; Scan next disk if there is one
1803 test dl, dl ; Set ZF=0
1804 clc ; Indicate success
1805
1806 DriveIO_ScanDisks_end:
1807 ; Restore all registers
1808 popa
1809
1810 ret
1811DriveIO_ScanDisks EndP
1812
1813;------------------------------------------------------------------------------
1814; Calculate pointer to entry in DISKINFO structure
1815;------------------------------------------------------------------------------
1816; IN : DL BIOS disk number (80h etc)
1817; OUT : BX Pointer to entry
1818; NOTE : BIOS disk number must be valid
1819;------------------------------------------------------------------------------
1820DriveIO_CalcDiskInfoPointer Proc Near
1821 xchg bx, ax ; AX is used for calculation
1822 mov al, DISKINFO_Size ; Size of DISKINFO structure
1823 mov ah, dl ; BIOS disk number
1824 sub ah, 80h ; Now 0-based index
1825 mul ah ; Now offset into DISKINFO array
1826 add ax, offset [DiskInformation] ; Base of DISKINFO array
1827 xchg bx, ax ; BX now points to entry for disk
1828 ret
1829DriveIO_CalcDiskInfoPointer EndP
1830
1831;------------------------------------------------------------------------------
1832; Check if the BIOS disk number in DL is a harddisk and in range
1833;------------------------------------------------------------------------------
1834; IN : DL BIOS disk number (80h etc)
1835; OUT : CF=1 if invalid disk number or out of range
1836; NOTE : Only modifies flags
1837;------------------------------------------------------------------------------
1838DriveIO_IsValidHarddisk Proc Near Uses dx
1839 cmp dl, 80h ; BIOS disk number must be at least 80h
1840 jb @F ; Not a harddisk, exit with CY
1841 mov dh, dl ; Save to do compare
1842 sub dh, 80h ; Now 0 based disk number
1843 inc dh ; Now 1 based disk number
1844 cmp [TotalHarddiscs], dh ; Out of range, exit with CY
1845 @@: ret
1846DriveIO_IsValidHarddisk EndP
1847
1848
1849; Values for sectors per track table corresponding to DriveIO_IsHugeDrive return value.
1850;~ secs_per_track_table db 63,127,255,255,255,255
1851
1852;~ db_lmlvm db 'Load Master LVM -- disk: ',0
Note: See TracBrowser for help on using the repository browser.