1 ; -*- fundamental -*- (asm-mode sucks)
2 ; ****************************************************************************
6 ; A program to emulate an INT 13h disk BIOS from a "disk" in extended
9 ; Copyright 2001-2009 H. Peter Anvin - All Rights Reserved
11 ; This program is free software; you can redistribute it and/or modify
12 ; it under the terms of the GNU General Public License as published by
13 ; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
14 ; Boston MA 02111-1307, USA; either version 2 of the License, or
15 ; (at your option) any later version; incorporated herein by reference.
17 ; ****************************************************************************
20 %include "../version.gen"
23 ; %define DEBUG_TRACERS ; Uncomment to get debugging tracers
37 %endif ; DEBUG_TRACERS
39 %define CONFIG_READONLY 0x01
40 %define CONFIG_RAW 0x02
41 %define CONFIG_SAFEINT 0x04
42 %define CONFIG_BIGRAW 0x08 ; MUST be 8!
46 %define SECTORSIZE_LG2 9 ; log2(sector size)
47 %define SECTORSIZE (1 << SECTORSIZE_LG2)
49 ; Parameter registers definition; this is the definition
51 %define P_DS word [bp+34]
52 %define P_ES word [bp+32]
53 %define P_EAX dword [bp+28]
54 %define P_HAX word [bp+30]
55 %define P_AX word [bp+28]
56 %define P_AL byte [bp+28]
57 %define P_AH byte [bp+29]
58 %define P_ECX dword [bp+24]
59 %define P_HCX word [bp+26]
60 %define P_CX word [bp+24]
61 %define P_CL byte [bp+24]
62 %define P_CH byte [bp+25]
63 %define P_EDX dword [bp+20]
64 %define P_HDX word [bp+22]
65 %define P_DX word [bp+20]
66 %define P_DL byte [bp+20]
67 %define P_DH byte [bp+21]
68 %define P_EBX dword [bp+16]
69 %define P_HBX word [bp+18]
70 %define P_HBXL byte [bp+18]
71 %define P_BX word [bp+16]
72 %define P_BL byte [bp+16]
73 %define P_BH byte [bp+17]
74 %define P_EBP dword [bp+8]
75 %define P_BP word [bp+8]
76 %define P_ESI dword [bp+4]
77 %define P_SI word [bp+4]
78 %define P_EDI dword [bp]
79 %define P_DI word [bp]
82 ; These pointers are used by the installer and
83 ; must be first in the binary
84 Pointers: dw Int13Start
90 IretPtr equ Int13Start.iret
101 ; See if DL points to our class of device (FD, HD)
106 js .nomatch ; If SF=0, we have a class match here
107 jz .our_drive ; If ZF=1, we have an exact match
109 jb .nomatch ; Drive < Our drive
110 dec dl ; Drive > Our drive, adjust drive #
114 call far [cs:OldInt13]
118 cmp byte [cs:SavedAX+1],08h
120 cmp byte [cs:SavedAX+1],15h
122 test byte [bp+4],80h ; Hard disk?
130 mov ax,[bp+2] ; Flags
132 mov [bx+4],al ; Arithmetric flags
141 ; Set up standard entry frame
148 mov bp,sp ; Point BP to the entry stack frame
150 ; Note: AH == P_AH here
151 cmp ah,[Int13MaxFunc]
153 xor al,al ; AL = 0 is standard entry condition
155 shr di,7 ; Convert AH to an offset in DI
158 Done: ; Standard routine for return
165 mov [es:bx],ah ; Save status
169 ; This sets the low byte (the arithmetric flags) of the
170 ; FLAGS on stack to either 00h (no flags) or 01h (CF)
171 ; depending on if AH was zero or not.
172 setnz [bx+4] ; Set CF iff error
180 ; Reset affects multiple drives, so we need to pass it on
182 xor ax,ax ; Bottom of memory
184 test dl,dl ; Always pass it on if we are resetting HD
185 js .hard_disk ; Bit 7 set
186 ; Some BIOSes get very unhappy if we pass a reset floppy
187 ; command to them and don't actually have any floppies.
188 ; This is a bug, but we have to deal with it nontheless.
189 ; Therefore, if we are the *ONLY* floppy drive, and the
190 ; user didn't request HD reset, then just drop the command.
191 ; BIOS equipment byte, top two bits + 1 == total # of floppies
192 test byte [es:0x410],0C0h
194 jmp .pass_on ; ... otherwise pass it to the BIOS
196 ; ... same thing for hard disks, sigh ...
197 cmp byte [es:0x475],1 ; BIOS variable for number of hard disks
201 pop ax ; Drop return address
202 popad ; Restore all registers
205 lss esp,[cs:Stack] ; Restore the stack
206 and dl,80h ; Clear all but the type bit
207 jmp far [cs:OldInt13]
211 pop dx ; Drop return address
214 mov ah,01h ; Unsupported function
218 test byte [DriveNo],80h
219 mov bl,02h ; Type 02h = floppy with changeline
223 mov dx,[DiskSize] ; Return the disk size in sectors
228 mov P_AH,bl ; 02h floppy, 03h hard disk
229 pop ax ; Drop return address
230 xor ax,ax ; Success...
231 jmp DoneWeird ; But don't stick it into P_AX
237 mov ah,[bx] ; Copy last status
249 movzx ax,P_AL ; AH = 0, AL = transfer count
256 test byte [ConfigFlags],CONFIG_READONLY
259 xchg esi,edi ; Opposite direction of a Read!
261 .readonly: mov ah,03h ; Write protected medium
264 ; Verify integrity; just bounds-check
267 call setup_regs ; Returns error if appropriate
268 ; And fall through to success
270 CheckIfReady: ; These are always-successful noop functions
278 xor ax,ax ; Always successful
283 mov dl,[DriveCnt] ; Cached data
285 test byte [DriveNo],80h
293 dec ax ; We report the highest #, not the count
303 ; Is this MEMDISK installation check?
314 ; MEMDISK installation check...
320 mov P_DI,MemDisk_Info
326 ; EDD functions -- only if enabled
335 mov P_BX,0AA55h ; EDD signature
336 mov P_AX,03000h ; EDD 3.0
337 mov P_CX,0003h ; Bit 0 - Fixed disk access subset
338 ; Bit 1 - Locking and ejecting subset
339 pop ax ; Drop return address
341 jmp DoneWeird ; Success, but AH != 0, sigh...
364 call edd_setup_regs ; Just bounds checking
376 lodsw ; Length of our DPT
378 cmp cx,26 ; Minimum size
400 ; Set up registers as for a "Read", and compares against disk size.
401 ; WARNING: This fails immediately, even if we can transfer some
402 ; sectors. This isn't really the correct behaviour.
405 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
407 ; CL[0:5] = sector (1-based) CL[7:6] = cyl[9:8]
410 movzx ebx,cl ; Sector number
412 dec ebx ; Sector number is 1-based
415 movzx edi,P_DH ; Head number
416 movzx eax,word [Heads]
420 xchg cl,ch ; Now (E)CX <- cylinder number
421 mul ecx ; eax <- Heads*cyl# (edx <- 0)
425 ; Now eax = LBA, edx = 0
428 ; setup_regs continues...
430 ; Note: edi[31:16] and ecx[31:16] = 0 already
431 mov di,P_BX ; Get linear address of target buffer
434 add edi,ecx ; EDI = address to fetch to
435 movzx ecx,P_AL ; Sector count
437 add eax,ecx ; LBA of final sector + 1
438 shl esi,SECTORSIZE_LG2 ; LBA -> byte offset
439 add esi,[DiskBuf] ; Get address in high memory
440 cmp eax,[DiskSize] ; Check the high mark against limit
442 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
445 .overrun: pop ax ; Drop setup_regs return address
446 mov ax,0200h ; Missing address mark
449 ; Set up registers as for an EDD Read, and compares against disk size.
453 mov si,P_SI ; DS:SI -> DAPA
460 cmp dword [es:si+4],-1
463 movzx ebx,word [es:si+4] ; Offset
464 movzx edi,word [es:si+6] ; Segment
470 cmp dx,24 ; Must be large enough to hold
474 cmp dword [es:si+20],0 ; > 4 GB addresses not supported
475 mov ax,0900h ; "Data boundary error" - bogus, but
476 ; no really better code available
482 cmp dword [es:si+12],0 ; LBA too large?
485 movzx ecx, word [es:si+2] ; Sectors to transfer
486 mov esi,[es:si+8] ; Starting sector
493 shl ecx,SECTORSIZE_LG2-2 ; Convert to dwords
494 shl esi,SECTORSIZE_LG2 ; Convert to an offset
501 mov ax,0100h ; Invalid command
503 pop ax ; Drop setup_regs return address
507 mov ax,0200h ; "Address mark not found" =
508 ; LBA beyond end of disk
510 and word [es:si+2],0 ; No sectors transferred
516 mov ax,0B200h ; Volume Not Removable
523 ; INT 15h intercept routines
526 cmp edx,534D4150h ; "SMAP"
528 cmp ecx,20 ; Need 20 bytes
538 add bx,16 ; Advance to next
539 mov eax,[bx-8] ; Type
540 and eax,eax ; Null type?
541 jz .renew ; If so advance to next
546 mov eax,[bx-4] ; Extended attributes
548 mov ecx,24 ; Bytes loaded
550 mov eax,[bx-16] ; Start addr (low)
551 mov edx,[bx-12] ; Start addr (high)
554 mov eax,[bx] ; End addr (low)
555 mov edx,[bx+4] ; End addr (high)
556 sub eax,[bx-16] ; Derive the length
558 mov [es:di+8],eax ; Length (low)
559 mov [es:di+12],edx ; Length (high)
560 cmp dword [bx+8],-1 ; Type of next = end?
562 xor ebx,ebx ; Done with table
565 mov edx,eax ; Some systems expect eax = edx = SMAP
568 mov byte [bp+6], 02h ; Clear CF
573 mov byte [bp+6], 03h ; Set CF
590 jmp far [cs:OldInt15]
597 jmp short int15_success
604 jmp short int15_success
607 mov ax,[cs:MemInt1588]
608 jmp short int15_success
611 ; Routine to copy in/out of high memory
612 ; esi = linear source address
613 ; edi = linear target address
614 ; ecx = 32-bit word count
616 ; Assumes cs = ds = es
624 mov bx, real_int15_stub
626 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
627 jz .anymode ; Always do the real INT 15h
629 smsw ax ; Unprivileged!
631 jnz .protmode ; Protmode -> do real INT 15h
634 ; Raw or Safeint mode, and we're in real mode...
636 test byte [ConfigFlags], CONFIG_SAFEINT
641 ; We're in real mode, do it outselves
656 ; Test to see if A20 is enabled or not
673 push dx ; <D> Save A20 status
676 mov ax,2401h ; Enable A20
682 ; DX = 16 for BIGRAW, 8 for RAW
683 ; 8 is selector for a 64K flat segment,
684 ; 16 is selector for a 4GB flat segment.
691 mov bx,16 ; Large flat segment
697 ; DX has the appropriate value to put in
698 ; the registers on return
705 pop dx ; <D> A20 status
711 mov ax,2400h ; Disable A20
718 ; We're in real mode with CONFIG_SAFEINT, invoke the
719 ; original INT 15h vector. We used to test for the
720 ; INT 15h vector being unchanged here, but that is
721 ; *us*; however, the test was wrong for years (always
722 ; negative) so instead of fixing the test do what we
723 ; tested and don't bother probing.
724 mov bx, fake_int15_stub
738 push ecx ; Transfer size this cycle
742 mov [Mover_src1+2], al
747 mov [Mover_dst1+2], al
751 shl cx,1 ; Convert to 16-bit words
752 call bx ; INT 15h stub
753 pop eax ; Transfer size this cycle
773 cli ; Some BIOSes enable interrupts on INT 15h
799 Int13Funcs dw Reset ; 00h - RESET
800 dw GetStatus ; 01h - GET STATUS
802 dw Write ; 03h - WRITE
803 dw Verify ; 04h - VERIFY
804 dw Invalid ; 05h - FORMAT TRACK
805 dw Invalid ; 06h - FORMAT TRACK AND SET BAD FLAGS
806 dw Invalid ; 07h - FORMAT DRIVE AT TRACK
807 dw GetParms ; 08h - GET PARAMETERS
808 dw InitWithParms ; 09h - INITIALIZE CONTROLLER WITH DRIVE PARAMETERS
811 dw Seek ; 0Ch - SEEK TO CYLINDER
812 dw Reset ; 0Dh - RESET HARD DISKS
815 dw CheckIfReady ; 10h - CHECK IF READY
816 dw Recalibrate ; 11h - RECALIBRATE
820 dw GetDriveType ; 15h - GET DRIVE TYPE
821 dw DetectChange ; 16h - DETECT DRIVE CHANGE
833 dw ReadMult ; 21h - READ MULTIPLE
834 dw WriteMult ; 22h - WRITE MULTIPLE
835 dw SetMode ; 23h - SET CONTROLLER FEATURES
836 dw SetMode ; 24h - SET MULTIPLE MODE
837 dw Invalid ; 25h - IDENTIFY DRIVE
865 dw EDDPresence ; 41h - EDD PRESENCE DETECT
866 dw EDDRead ; 42h - EDD READ
867 dw EDDWrite ; 43h - EDD WRITE
868 dw EDDVerify ; 44h - EDD VERIFY
869 dw EDDLock ; 45h - EDD LOCK/UNLOCK MEDIA
870 dw EDDEject ; 46h - EDD EJECT
871 dw EDDSeek ; 47h - EDD SEEK
872 dw EDDGetParms ; 48h - EDD GET PARAMETERS
873 dw EDDDetectChange ; 49h - EDD MEDIA CHANGE STATUS
877 Int13FuncsCnt equ (Int13FuncsEnd-Int13Funcs) >> 1
881 Shaker dw ShakerEnd-$-1 ; Descriptor table limit
882 dd 0 ; Pointer to self
885 Shaker_RMDS: dd 0x0000ffff ; 64K data segment
888 Shaker_DS: dd 0x0000ffff ; 4GB data segment
895 Mover dd 0, 0, 0, 0 ; Must be zero
896 dw 0ffffh ; 64 K segment size
897 Mover_src1: db 0, 0, 0 ; Low 24 bits of source addy
898 db 93h ; Access rights
899 db 00h ; Extended access rights
900 Mover_src2: db 0 ; High 8 bits of source addy
901 dw 0ffffh ; 64 K segment size
902 Mover_dst1: db 0, 0, 0 ; Low 24 bits of target addy
903 db 93h ; Access rights
904 db 00h ; Extended access rights
905 Mover_dst2: db 0 ; High 8 bits of source addy
906 Mover_dummy2: dd 0, 0, 0, 0 ; More space for the BIOS
909 MemDisk_Info equ $ ; Pointed to by installation check
910 MDI_Bytes dw MDI_Len ; Total bytes in MDI structure
911 MDI_Version db VERSION_MINOR, VERSION_MAJOR ; MEMDISK version
913 PatchArea equ $ ; This gets filled in by the installer
915 DiskBuf dd 0 ; Linear address of high memory disk
916 DiskSize dd 0 ; Size of disk in blocks
917 CommandLine dw 0, 0 ; Far pointer to saved command line
919 OldInt13 dd 0 ; INT 13h in chain
920 OldInt15 dd 0 ; INT 15h in chain
922 OldDosMem dw 0 ; Old position of DOS mem end
923 BootLoaderID db 0 ; Boot loader ID from header
926 DPT_ptr dw 0 ; If nonzero, pointer to DPT
927 ; Original DPT pointer follows
929 MDI_Len equ $-MemDisk_Info
931 ; ---- MDI structure ends here ---
932 Int13MaxFunc db Int13FuncsCnt-1 ; Max INT 13h function (to disable EDD)
936 MemInt1588 dw 0 ; 1MB-65MB memory amount (1K)
938 Cylinders dw 0 ; Cylinder count
939 Heads dw 0 ; Head count
940 Sectors dd 0 ; Sector count (zero-extended)
942 Mem1MB dd 0 ; 1MB-16MB memory amount (1K)
943 Mem16MB dd 0 ; 16MB-4G memory amount (64K)
945 DriveNo db 0 ; Our drive number
946 DriveType db 0 ; Our drive type (floppies)
947 DriveCnt db 0 ; Drive count (from the BIOS)
949 ConfigFlags db 0 ; Bit 0 - readonly
951 MyStack dw 0 ; Offset of stack
952 StatusPtr dw 0 ; Where to save status (zeroseg ptr)
954 DPT times 16 db 0 ; BIOS parameter table pointer (floppies)
955 OldInt1E dd 0 ; Previous INT 1E pointer (DPT)
961 ; Bit 0 - DMA boundaries handled transparently
962 ; Bit 3 - Device supports write verify
963 ; Bit 5 - Media is lockable
964 .cylinders dd 0 ; Filled in by installer
965 .heads dd 0 ; Filled in by installer
966 .sectors dd 0 ; Filled in by installer
967 .totalsize dd 0, 0 ; Filled in by installer
968 .bytespersec dw SECTORSIZE
969 .eddtable dw -1, -1 ; Invalid DPTE pointer
974 Stack dd 0 ; Saved SS:ESP on invocation
976 SavedAX dw 0 ; AX saved on invocation
978 alignb 4, db 0 ; We *MUST* end on a dword boundary
980 E820Table equ $ ; The installer loads the E820 table here
981 TotalSize equ $ ; End pointer