1 ; -*- fundamental -*- (asm-mode sucks)
2 ; ****************************************************************************
6 ; A program to emulate an INT 13h disk BIOS from a "disk" in extended
9 ; Copyright 2001-2008 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
43 %endif ; DEBUG_TRACERS
45 %define CONFIG_READONLY 0x01
46 %define CONFIG_RAW 0x02
47 %define CONFIG_SAFEINT 0x04
48 %define CONFIG_BIGRAW 0x08 ; MUST be 8!
52 %define SECTORSIZE_LG2 9 ; log2(sector size)
53 %define SECTORSIZE (1 << SECTORSIZE_LG2)
55 ; Parameter registers definition; this is the definition
57 %define P_DS word [bp+34]
58 %define P_ES word [bp+32]
59 %define P_EAX dword [bp+28]
60 %define P_HAX word [bp+30]
61 %define P_AX word [bp+28]
62 %define P_AL byte [bp+28]
63 %define P_AH byte [bp+29]
64 %define P_ECX dword [bp+24]
65 %define P_HCX word [bp+26]
66 %define P_CX word [bp+24]
67 %define P_CL byte [bp+24]
68 %define P_CH byte [bp+25]
69 %define P_EDX dword [bp+20]
70 %define P_HDX word [bp+22]
71 %define P_DX word [bp+20]
72 %define P_DL byte [bp+20]
73 %define P_DH byte [bp+21]
74 %define P_EBX dword [bp+16]
75 %define P_HBX word [bp+18]
76 %define P_HBXL byte [bp+18]
77 %define P_BX word [bp+16]
78 %define P_BL byte [bp+16]
79 %define P_BH byte [bp+17]
80 %define P_EBP dword [bp+8]
81 %define P_BP word [bp+8]
82 %define P_ESI dword [bp+4]
83 %define P_SI word [bp+4]
84 %define P_EDI dword [bp]
85 %define P_DI word [bp]
88 ; These pointers are used by the installer and
89 ; must be first in the binary
90 Pointers: dw Int13Start
96 IretPtr equ Int13Start.iret
107 ; See if DL points to our class of device (FD, HD)
112 js .nomatch ; If SF=0, we have a class match here
113 jz .our_drive ; If ZF=1, we have an exact match
115 jb .nomatch ; Drive < Our drive
116 dec dl ; Drive > Our drive, adjust drive #
120 call far [cs:OldInt13]
124 cmp byte [cs:SavedAX+1],08h
126 cmp byte [cs:SavedAX+1],15h
128 test byte [bp+4],80h ; Hard disk?
136 mov ax,[bp+2] ; Flags
138 mov [bx+4],al ; Arithmetric flags
147 ; Set up standard entry frame
154 mov bp,sp ; Point BP to the entry stack frame
156 ; Note: AH == P_AH here
157 cmp ah,[Int13MaxFunc]
159 xor al,al ; AL = 0 is standard entry condition
161 shr di,7 ; Convert AH to an offset in DI
164 Done: ; Standard routine for return
171 mov [es:bx],ah ; Save status
175 ; This sets the low byte (the arithmetric flags) of the
176 ; FLAGS on stack to either 00h (no flags) or 01h (CF)
177 ; depending on if AH was zero or not.
178 setnz [bx+4] ; Set CF iff error
186 ; Reset affects multiple drives, so we need to pass it on
188 xor ax,ax ; Bottom of memory
190 test dl,dl ; Always pass it on if we are resetting HD
191 js .hard_disk ; Bit 7 set
192 ; Some BIOSes get very unhappy if we pass a reset floppy
193 ; command to them and don't actually have any floppies.
194 ; This is a bug, but we have to deal with it nontheless.
195 ; Therefore, if we are the *ONLY* floppy drive, and the
196 ; user didn't request HD reset, then just drop the command.
197 ; BIOS equipment byte, top two bits + 1 == total # of floppies
198 test byte [es:0x410],0C0h
200 jmp .pass_on ; ... otherwise pass it to the BIOS
202 ; ... same thing for hard disks, sigh ...
203 cmp byte [es:0x475],1 ; BIOS variable for number of hard disks
207 pop ax ; Drop return address
208 popad ; Restore all registers
211 lss esp,[cs:Stack] ; Restore the stack
212 and dl,80h ; Clear all but the type bit
213 jmp far [cs:OldInt13]
217 pop dx ; Drop return address
220 mov ah,01h ; Unsupported function
224 test byte [DriveNo],80h
225 mov bl,02h ; Type 02h = floppy with changeline
229 mov dx,[DiskSize] ; Return the disk size in sectors
234 mov P_AH,bl ; 02h floppy, 03h hard disk
235 pop ax ; Drop return address
236 xor ax,ax ; Success...
237 jmp DoneWeird ; But don't stick it into P_AX
243 mov ah,[bx] ; Copy last status
255 movzx ax,P_AL ; AH = 0, AL = transfer count
262 test byte [ConfigFlags],CONFIG_READONLY
265 xchg esi,edi ; Opposite direction of a Read!
267 .readonly: mov ah,03h ; Write protected medium
270 ; Verify integrity; just bounds-check
273 call setup_regs ; Returns error if appropriate
274 ; And fall through to success
276 CheckIfReady: ; These are always-successful noop functions
284 xor ax,ax ; Always successful
289 mov dl,[DriveCnt] ; Cached data
291 test byte [DriveNo],80h
299 dec ax ; We report the highest #, not the count
309 ; Is this MEMDISK installation check?
320 ; MEMDISK installation check...
326 mov P_DI,MemDisk_Info
332 ; EDD functions -- only if enabled
341 mov P_BX,0AA55h ; EDD signature
342 mov P_AX,03000h ; EDD 3.0
343 mov P_CX,0003h ; Bit 0 - Fixed disk access subset
344 ; Bit 1 - Locking and ejecting subset
345 pop ax ; Drop return address
347 jmp DoneWeird ; Success, but AH != 0, sigh...
370 call edd_setup_regs ; Just bounds checking
382 lodsw ; Length of our DPT
384 cmp cx,26 ; Minimum size
406 ; Set up registers as for a "Read", and compares against disk size.
407 ; WARNING: This fails immediately, even if we can transfer some
408 ; sectors. This isn't really the correct behaviour.
411 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
413 ; CL[0:5] = sector (1-based) CL[7:6] = cyl[9:8]
416 movzx ebx,cl ; Sector number
418 dec ebx ; Sector number is 1-based
421 movzx edi,P_DH ; Head number
422 movzx eax,word [Heads]
426 xchg cl,ch ; Now (E)CX <- cylinder number
427 mul ecx ; eax <- Heads*cyl# (edx <- 0)
431 ; Now eax = LBA, edx = 0
434 ; setup_regs continues...
436 ; Note: edi[31:16] and ecx[31:16] = 0 already
437 mov di,P_BX ; Get linear address of target buffer
440 add edi,ecx ; EDI = address to fetch to
441 movzx ecx,P_AL ; Sector count
443 add eax,ecx ; LBA of final sector + 1
444 shl esi,SECTORSIZE_LG2 ; LBA -> byte offset
445 add esi,[DiskBuf] ; Get address in high memory
446 cmp eax,[DiskSize] ; Check the high mark against limit
448 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
451 .overrun: pop ax ; Drop setup_regs return address
452 mov ax,0200h ; Missing address mark
455 ; Set up registers as for an EDD Read, and compares against disk size.
459 mov si,P_SI ; DS:SI -> DAPA
466 cmp dword [es:si+4],-1
469 movzx ebx,word [es:si+4] ; Offset
470 movzx edi,word [es:si+6] ; Segment
476 cmp dx,24 ; Must be large enough to hold
480 cmp dword [es:si+20],0 ; > 4 GB addresses not supported
481 mov ax,0900h ; "Data boundary error" - bogus, but
482 ; no really better code available
488 cmp dword [es:si+12],0 ; LBA too large?
491 movzx ecx, word [es:si+2] ; Sectors to transfer
492 mov esi,[es:si+8] ; Starting sector
499 shl ecx,SECTORSIZE_LG2-2 ; Convert to dwords
500 shl esi,SECTORSIZE_LG2 ; Convert to an offset
507 mov ax,0100h ; Invalid command
509 pop ax ; Drop setup_regs return address
513 mov ax,0200h ; "Address mark not found" =
514 ; LBA beyond end of disk
516 and word [es:si+2],0 ; No sectors transferred
522 mov ax,0B200h ; Volume Not Removable
529 ; INT 15h intercept routines
532 cmp edx,534D4150h ; "SMAP"
534 cmp ecx,20 ; Need 20 bytes
544 add bx,16 ; Advance to next
545 mov eax,[bx-8] ; Type
546 and eax,eax ; Null type?
547 jz .renew ; If so advance to next
552 mov eax,[bx-4] ; Extended attributes
554 mov ecx,24 ; Bytes loaded
556 mov eax,[bx-16] ; Start addr (low)
557 mov edx,[bx-12] ; Start addr (high)
560 mov eax,[bx] ; End addr (low)
561 mov edx,[bx+4] ; End addr (high)
562 sub eax,[bx-16] ; Derive the length
564 mov [es:di+8],eax ; Length (low)
565 mov [es:di+12],edx ; Length (high)
566 cmp dword [bx+8],-1 ; Type of next = end?
568 xor ebx,ebx ; Done with table
571 mov edx,eax ; Some systems expect eax = edx = SMAP
574 mov byte [bp+6], 02h ; Clear CF
579 mov byte [bp+6], 03h ; Set CF
596 jmp far [cs:OldInt15]
603 jmp short int15_success
610 jmp short int15_success
613 mov ax,[cs:MemInt1588]
614 jmp short int15_success
617 ; Routine to copy in/out of high memory
618 ; esi = linear source address
619 ; edi = linear target address
620 ; ecx = 32-bit word count
622 ; Assumes cs = ds = es
630 mov bx, real_int15_stub
632 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
633 jz .anymode ; Always do the real INT 15h
635 smsw ax ; Unprivileged!
637 jnz .protmode ; Protmode -> do real INT 15h
640 ; Raw or Safeint mode, and we're in real mode...
642 test byte [ConfigFlags], CONFIG_SAFEINT
647 ; We're in real mode, do it outselves
662 ; Test to see if A20 is enabled or not
679 push dx ; <D> Save A20 status
682 mov ax,2401h ; Enable A20
688 ; DX = 16 for BIGRAW, 8 for RAW
689 ; 8 is selector for a 64K flat segment,
690 ; 16 is selector for a 4GB flat segment.
697 mov bx,16 ; Large flat segment
703 ; DX has the appropriate value to put in
704 ; the registers on return
711 pop dx ; <D> A20 status
717 mov ax,2400h ; Disable A20
724 ; We're in real mode with CONFIG_SAFEINT, invoke the
725 ; original INT 15h vector. We used to test for the
726 ; INT 15h vector being unchanged here, but that is
727 ; *us*; however, the test was wrong for years (always
728 ; negative) so instead of fixing the test do what we
729 ; tested and don't bother probing.
730 mov bx, fake_int15_stub
744 push ecx ; Transfer size this cycle
748 mov [Mover_src1+2], al
753 mov [Mover_dst1+2], al
757 shl cx,1 ; Convert to 16-bit words
758 call bx ; INT 15h stub
759 pop eax ; Transfer size this cycle
779 cli ; Some BIOSes enable interrupts on INT 15h
805 Int13Funcs dw Reset ; 00h - RESET
806 dw GetStatus ; 01h - GET STATUS
808 dw Write ; 03h - WRITE
809 dw Verify ; 04h - VERIFY
810 dw Invalid ; 05h - FORMAT TRACK
811 dw Invalid ; 06h - FORMAT TRACK AND SET BAD FLAGS
812 dw Invalid ; 07h - FORMAT DRIVE AT TRACK
813 dw GetParms ; 08h - GET PARAMETERS
814 dw InitWithParms ; 09h - INITIALIZE CONTROLLER WITH DRIVE PARAMETERS
817 dw Seek ; 0Ch - SEEK TO CYLINDER
818 dw Reset ; 0Dh - RESET HARD DISKS
821 dw CheckIfReady ; 10h - CHECK IF READY
822 dw Recalibrate ; 11h - RECALIBRATE
826 dw GetDriveType ; 15h - GET DRIVE TYPE
827 dw DetectChange ; 16h - DETECT DRIVE CHANGE
839 dw ReadMult ; 21h - READ MULTIPLE
840 dw WriteMult ; 22h - WRITE MULTIPLE
841 dw SetMode ; 23h - SET CONTROLLER FEATURES
842 dw SetMode ; 24h - SET MULTIPLE MODE
843 dw Invalid ; 25h - IDENTIFY DRIVE
871 dw EDDPresence ; 41h - EDD PRESENCE DETECT
872 dw EDDRead ; 42h - EDD READ
873 dw EDDWrite ; 43h - EDD WRITE
874 dw EDDVerify ; 44h - EDD VERIFY
875 dw EDDLock ; 45h - EDD LOCK/UNLOCK MEDIA
876 dw EDDEject ; 46h - EDD EJECT
877 dw EDDSeek ; 47h - EDD SEEK
878 dw EDDGetParms ; 48h - EDD GET PARAMETERS
879 dw EDDDetectChange ; 49h - EDD MEDIA CHANGE STATUS
883 Int13FuncsCnt equ (Int13FuncsEnd-Int13Funcs) >> 1
887 Shaker dw ShakerEnd-$
888 dd 0 ; Pointer to self
891 Shaker_RMDS: dd 0x0000ffff ; 64K data segment
894 Shaker_DS: dd 0x0000ffff ; 4GB data segment
901 Mover dd 0, 0, 0, 0 ; Must be zero
902 dw 0ffffh ; 64 K segment size
903 Mover_src1: db 0, 0, 0 ; Low 24 bits of source addy
904 db 93h ; Access rights
905 db 00h ; Extended access rights
906 Mover_src2: db 0 ; High 8 bits of source addy
907 dw 0ffffh ; 64 K segment size
908 Mover_dst1: db 0, 0, 0 ; Low 24 bits of target addy
909 db 93h ; Access rights
910 db 00h ; Extended access rights
911 Mover_dst2: db 0 ; High 8 bits of source addy
912 Mover_dummy2: dd 0, 0, 0, 0 ; More space for the BIOS
915 MemDisk_Info equ $ ; Pointed to by installation check
916 MDI_Bytes dw MDI_Len ; Total bytes in MDI structure
917 MDI_Version db VERSION_MINOR, VERSION_MAJOR ; MEMDISK version
919 PatchArea equ $ ; This gets filled in by the installer
921 DiskBuf dd 0 ; Linear address of high memory disk
922 DiskSize dd 0 ; Size of disk in blocks
923 CommandLine dw 0, 0 ; Far pointer to saved command line
925 OldInt13 dd 0 ; INT 13h in chain
926 OldInt15 dd 0 ; INT 15h in chain
928 OldDosMem dw 0 ; Old position of DOS mem end
929 BootLoaderID db 0 ; Boot loader ID from header
932 DPT_ptr dw 0 ; If nonzero, pointer to DPT
933 ; Original DPT pointer follows
935 MDI_Len equ $-MemDisk_Info
937 ; ---- MDI structure ends here ---
938 Int13MaxFunc db Int13FuncsCnt-1 ; Max INT 13h function (to disable EDD)
942 MemInt1588 dw 0 ; 1MB-65MB memory amount (1K)
944 Cylinders dw 0 ; Cylinder count
945 Heads dw 0 ; Head count
946 Sectors dd 0 ; Sector count (zero-extended)
948 Mem1MB dd 0 ; 1MB-16MB memory amount (1K)
949 Mem16MB dd 0 ; 16MB-4G memory amount (64K)
951 DriveNo db 0 ; Our drive number
952 DriveType db 0 ; Our drive type (floppies)
953 DriveCnt db 0 ; Drive count (from the BIOS)
955 ConfigFlags db 0 ; Bit 0 - readonly
957 MyStack dw 0 ; Offset of stack
958 StatusPtr dw 0 ; Where to save status (zeroseg ptr)
960 DPT times 16 db 0 ; BIOS parameter table pointer (floppies)
961 OldInt1E dd 0 ; Previous INT 1E pointer (DPT)
967 ; Bit 0 - DMA boundaries handled transparently
968 ; Bit 3 - Device supports write verify
969 ; Bit 5 - Media is lockable
970 .cylinders dd 0 ; Filled in by installer
971 .heads dd 0 ; Filled in by installer
972 .sectors dd 0 ; Filled in by installer
973 .totalsize dd 0, 0 ; Filled in by installer
974 .bytespersec dw SECTORSIZE
975 .eddtable dw -1, -1 ; Invalid DPTE pointer
980 Stack dd 0 ; Saved SS:ESP on invocation
982 SavedAX dw 0 ; AX saved on invocation
984 alignb 4, db 0 ; We *MUST* end on a dword boundary
986 E820Table equ $ ; The installer loads the E820 table here
987 TotalSize equ $ ; End pointer