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
10 ; Copyright 2009 Intel Corporation; author: H. Peter Anvin
12 ; This program is free software; you can redistribute it and/or modify
13 ; it under the terms of the GNU General Public License as published by
14 ; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
15 ; Boston MA 02111-1307, USA; either version 2 of the License, or
16 ; (at your option) any later version; incorporated herein by reference.
18 ; ****************************************************************************
20 %include "../version.gen"
22 ; %define DEBUG_TRACERS ; Uncomment to get debugging tracers
30 %macro WRITEHEX2 0-1 al
40 %macro WRITEHEX4 0-1 ax
50 %macro WRITEHEX8 0-1 eax
72 %endif ; DEBUG_TRACERS
74 ; Flags we test our configuration against
75 %define CONFIG_READONLY 0x01
76 %define CONFIG_RAW 0x02
77 %define CONFIG_SAFEINT 0x04
78 %define CONFIG_BIGRAW 0x08 ; MUST be 8!
82 %define SECTORSIZE_LG2 9 ; log2(sector size)
83 %define SECTORSIZE (1 << SECTORSIZE_LG2)
85 ; Parameter registers definition; this is the definition
87 %define P_DS word [bp+34]
88 %define P_ES word [bp+32]
89 %define P_EAX dword [bp+28]
90 %define P_HAX word [bp+30]
91 %define P_AX word [bp+28]
92 %define P_AL byte [bp+28]
93 %define P_AH byte [bp+29]
94 %define P_ECX dword [bp+24]
95 %define P_HCX word [bp+26]
96 %define P_CX word [bp+24]
97 %define P_CL byte [bp+24]
98 %define P_CH byte [bp+25]
99 %define P_EDX dword [bp+20]
100 %define P_HDX word [bp+22]
101 %define P_DX word [bp+20]
102 %define P_DL byte [bp+20]
103 %define P_DH byte [bp+21]
104 %define P_EBX dword [bp+16]
105 %define P_HBX word [bp+18]
106 %define P_HBXL byte [bp+18]
107 %define P_BX word [bp+16]
108 %define P_BL byte [bp+16]
109 %define P_BH byte [bp+17]
110 %define P_EBP dword [bp+8]
111 %define P_BP word [bp+8]
112 %define P_ESI dword [bp+4]
113 %define P_SI word [bp+4]
114 %define P_EDI dword [bp]
115 %define P_DI word [bp]
118 ; These pointers are used by the installer and
119 ; must be first in the binary
120 Pointers: dw Int13Start
126 IretPtr equ Int13Start.iret
128 cmp word [cs:Recursive],0
139 ; See if DL points to our class of device (FD, HD)
144 js .nomatch ; If SF=0, we have a class match here
145 ; 0x00 the sign bit for FD
146 ; 0x80 the sign bit for HD
147 jz our_drive ; If ZF=1, we have an exact match
149 jb .nomatch ; Drive < Our drive
150 dec dl ; Drive > Our drive, adjust drive #
157 inc word [cs:Recursive]
159 call far [cs:OldInt13]
161 dec word [cs:Recursive]
164 cmp byte [cs:SavedAX+1],08h ; Get drive params function?
165 je .norestoredl ; DL = number of drives
166 cmp byte [cs:SavedAX+1],15h ; Get disk type function?
167 je .norestoredl ; CX:DX = size of device
174 mov ax,[bp+2] ; Flags
176 mov [bx+4],al ; Arithmetic flags
187 jmp far [cs:OldInt13]
190 ; Set up standard entry frame
197 mov bp,sp ; Point BP to the entry stack frame
200 ; Note: AX == P_AX here
201 cmp ah,Int13FuncsCnt-1
203 xor al,al ; AL = 0 is standard entry condition
205 shr di,7 ; Convert AH to an offset in DI
208 Done: ; Standard routine for return
215 mov [es:bx],ah ; Save status
219 ; This sets the low byte (the arithmetic flags) of the
220 ; FLAGS on stack to either 00h (no flags) or 01h (CF)
221 ; depending on if AH was zero or not.
222 setnz [bx+4] ; Set CF iff error
230 ; Reset affects multiple drives, so we need to pass it on
232 xor ax,ax ; Bottom of memory
234 test dl,dl ; Always pass it on if we are
236 js .hard_disk ; Bit 7 set
237 ; Some BIOSes get very unhappy if we pass a reset floppy
238 ; command to them and don't actually have any floppies.
239 ; This is a bug, but we have to deal with it nontheless.
240 ; Therefore, if we are the *ONLY* floppy drive, and the
241 ; user didn't request HD reset, then just drop the command.
242 ; BIOS equipment byte, top two bits + 1 == total # of floppies
243 test byte [es:0x410],0C0h
245 jmp .pass_on ; ... otherwise pass it to the BIOS
247 ; ... same thing for hard disks, sigh ...
248 cmp byte [es:0x475],1 ; BIOS variable for number of hard
253 pop ax ; Drop return address
254 popad ; Restore all registers
257 lss esp,[cs:Stack] ; Restore the stack
258 and dl,80h ; Clear all but the type bit
263 pop dx ; Drop return address
266 mov ah,01h ; Unsupported function
270 test byte [DriveNo],80h
271 mov bl,02h ; Type 02h = floppy with changeline
273 ; Hard disks only! DO NOT set CX:DX for floppies...
274 ; it apparently causes Win98SE DOS to go into an loop
275 ; resetting the drive over and over. Sigh.
277 mov dx,[DiskSize] ; Return the disk size in sectors
282 mov P_AH,bl ; 02h floppy, 03h hard disk
283 pop ax ; Drop return address
284 xor ax,ax ; Success...
285 jmp DoneWeird ; But don't stick it into P_AX
291 mov ah,[bx] ; Copy last status
303 movzx ax,P_AL ; AH = 0, AL = transfer count
310 test byte [ConfigFlags],CONFIG_READONLY
313 xchg esi,edi ; Opposite direction of a Read!
315 .readonly: mov ah,03h ; Write protected medium
318 ; Verify integrity; just bounds-check
321 call setup_regs ; Returns error if appropriate
322 ; And fall through to success
324 CheckIfReady: ; These are always-successful noop functions
332 xor ax,ax ; Always successful
337 mov dl,[DriveCnt] ; Cached data
339 test byte [DriveNo],80h
347 dec ax ; We report the highest #, not the count
357 ; Is this MEMDISK installation check?
368 ; MEMDISK installation check...
374 mov P_DI,MemDisk_Info
380 ; EDD functions -- only if enabled
389 mov P_BX,0AA55h ; EDD signature
390 mov P_AX,03000h ; EDD 3.0
391 mov P_CX,0003h ; Bit 0 - Fixed disk access subset
392 ; Bit 1 - Locking and ejecting subset
393 pop ax ; Drop return address
395 jmp DoneWeird ; Success, but AH != 0, sigh...
411 xchg esi,edi ; Opposite direction of a Read!
418 call edd_setup_regs ; Just bounds checking
430 lodsw ; Length of our DPT
432 cmp cx,26 ; Minimum size
454 ; Set up registers as for a "Read", and compares against disk
456 ; WARNING: This fails immediately, even if we can transfer some
457 ; sectors. This isn't really the correct behaviour.
460 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
462 ; CL[0:5] = sector (1-based) CL[7:6] = cyl[9:8]
465 movzx ebx,cl ; Sector number
467 dec ebx ; Sector number is 1-based
470 movzx edi,P_DH ; Head number
471 movzx eax,word [Heads]
475 xchg cl,ch ; Now (E)CX <- cylinder number
476 mul ecx ; eax <- Heads*cyl# (edx <- 0)
480 ; Now eax = LBA, edx = 0
483 ; setup_regs continues...
485 ; Note: edi[31:16] and ecx[31:16] = 0 already
486 mov di,P_BX ; Get linear address of target buffer
489 add edi,ecx ; EDI = address to fetch to
490 movzx ecx,P_AL ; Sector count
492 add eax,ecx ; LBA of final sector + 1
493 shl esi,SECTORSIZE_LG2 ; LBA -> byte offset
494 add esi,[DiskBuf] ; Get address in high memory
495 cmp eax,[DiskSize] ; Check the high mark against limit
497 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
500 .overrun: pop ax ; Drop setup_regs return address
501 mov ax,0200h ; Missing address mark
504 ; Set up registers as for an EDD Read, and compares against disk size.
508 mov si,P_SI ; DS:SI -> DAPA
515 cmp dword [es:si+4],-1
518 movzx ebx,word [es:si+4] ; Offset
519 movzx edi,word [es:si+6] ; Segment
525 cmp dx,24 ; Must be large enough to hold
529 cmp dword [es:si+20],0 ; > 4 GB addresses not supported
530 mov ax,0900h ; "Data boundary error" - bogus, but
531 ; no really better code available
537 cmp dword [es:si+12],0 ; LBA too large?
540 movzx ecx, word [es:si+2] ; Sectors to transfer
541 mov esi,[es:si+8] ; Starting sector
548 shl ecx,SECTORSIZE_LG2-2 ; Convert to dwords
549 shl esi,SECTORSIZE_LG2 ; Convert to an offset
556 mov ax,0100h ; Invalid command
558 pop ax ; Drop setup_regs return address
562 mov ax,0200h ; "Address mark not found" =
563 ; LBA beyond end of disk
565 and word [es:si+2],0 ; No sectors transferred
571 mov ax,0B200h ; Volume Not Removable
578 ; INT 15h intercept routines
581 cmp edx,534D4150h ; "SMAP"
583 cmp ecx,20 ; Need 20 bytes
593 add bx,12 ; Advance to next
594 mov eax,[bx-4] ; Type
595 and eax,eax ; Null type?
596 jz .renew ; If so advance to next
598 mov eax,[bx-12] ; Start addr (low)
599 mov edx,[bx-8] ; Start addr (high)
602 mov eax,[bx] ; End addr (low)
603 mov edx,[bx+4] ; End addr (high)
604 sub eax,[bx-12] ; Derive the length
606 mov [es:di+8],eax ; Length (low)
607 mov [es:di+12],edx ; Length (high)
608 cmp dword [bx+8],-1 ; Type of next = end?
610 xor ebx,ebx ; Done with table
613 mov edx,eax ; Some systems expect eax = edx = SMAP
614 mov ecx,20 ; Bytes loaded
617 mov byte [bp+6], 02h ; Clear CF
622 mov byte [bp+6], 03h ; Set CF
639 jmp far [cs:OldInt15]
641 int15_e801: ; Get mem size for > 64 MB config
646 jmp short int15_success
648 int15_e881: ; Get mem size for > 64 MB config
654 jmp short int15_success
656 int15_88: ; Get extended mem size
657 mov ax,[cs:MemInt1588]
658 jmp short int15_success
661 ; Routine to copy in/out of high memory
662 ; esi = linear source address
663 ; edi = linear target address
664 ; ecx = 32-bit word count
666 ; Assumes cs = ds = es
674 mov bx, real_int15_stub
676 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
677 jz .anymode ; Always do the real INT 15h
679 smsw ax ; Unprivileged!
681 jnz .protmode ; Protmode -> do real INT 15h
684 ; Raw or Safeint mode, and we're in real mode...
686 test byte [ConfigFlags], CONFIG_SAFEINT
691 ; We're in real mode, do it outselves
706 ; Test to see if A20 is enabled or not
723 push dx ; <D> Save A20 status
726 mov ax,2401h ; Enable A20
732 ; DX = 16 for BIGRAW, 8 for RAW
733 ; 8 is selector for a 64K flat segment,
734 ; 16 is selector for a 4GB flat segment.
741 mov bx,16 ; Large flat segment
747 ; DX has the appropriate value to put in
748 ; the registers on return
755 pop dx ; <D> A20 status
761 mov ax,2400h ; Disable A20
768 ; We're in real mode with CONFIG_SAFEINT, invoke the
769 ; original INT 15h vector. We used to test for the
770 ; INT 15h vector being unchanged here, but that is
771 ; *us*; however, the test was wrong for years (always
772 ; negative) so instead of fixing the test do what we
773 ; tested and don't bother probing.
774 mov bx, fake_int15_stub
788 push ecx ; Transfer size this cycle
792 mov [Mover_src1+2], al
797 mov [Mover_dst1+2], al
801 shl cx,1 ; Convert to 16-bit words
802 call bx ; INT 15h stub
803 pop eax ; Transfer size this cycle
823 cli ; Some BIOSes enable interrupts on INT 15h
882 Int13Funcs dw Reset ; 00h - RESET
883 dw GetStatus ; 01h - GET STATUS
885 dw Write ; 03h - WRITE
886 dw Verify ; 04h - VERIFY
887 dw Invalid ; 05h - FORMAT TRACK
888 dw Invalid ; 06h - FORMAT TRACK AND SET BAD FLAGS
889 dw Invalid ; 07h - FORMAT DRIVE AT TRACK
890 dw GetParms ; 08h - GET PARAMETERS
891 dw InitWithParms ; 09h - INITIALIZE CONTROLLER WITH
895 dw Seek ; 0Ch - SEEK TO CYLINDER
896 dw Reset ; 0Dh - RESET HARD DISKS
899 dw CheckIfReady ; 10h - CHECK IF READY
900 dw Recalibrate ; 11h - RECALIBRATE
904 dw GetDriveType ; 15h - GET DRIVE TYPE
905 dw DetectChange ; 16h - DETECT DRIVE CHANGE
917 dw ReadMult ; 21h - READ MULTIPLE
918 dw WriteMult ; 22h - WRITE MULTIPLE
919 dw SetMode ; 23h - SET CONTROLLER FEATURES
920 dw SetMode ; 24h - SET MULTIPLE MODE
921 dw Invalid ; 25h - IDENTIFY DRIVE
949 dw EDDPresence ; 41h - EDD PRESENCE DETECT
950 dw EDDRead ; 42h - EDD READ
951 dw EDDWrite ; 43h - EDD WRITE
952 dw EDDVerify ; 44h - EDD VERIFY
953 dw EDDLock ; 45h - EDD LOCK/UNLOCK MEDIA
954 dw EDDEject ; 46h - EDD EJECT
955 dw EDDSeek ; 47h - EDD SEEK
956 dw EDDGetParms ; 48h - EDD GET PARAMETERS
957 dw EDDDetectChange ; 49h - EDD MEDIA CHANGE STATUS
961 Int13FuncsCnt equ (Int13FuncsEnd-Int13Funcs) >> 1
965 Shaker dw ShakerEnd-$-1 ; Descriptor table limit
966 dd 0 ; Pointer to self
969 Shaker_RMDS: dd 0x0000ffff ; 64K data segment
972 Shaker_DS: dd 0x0000ffff ; 4GB data segment
979 Mover dd 0, 0, 0, 0 ; Must be zero
980 dw 0ffffh ; 64 K segment size
981 Mover_src1: db 0, 0, 0 ; Low 24 bits of source addy
982 db 93h ; Access rights
983 db 00h ; Extended access rights
984 Mover_src2: db 0 ; High 8 bits of source addy
985 dw 0ffffh ; 64 K segment size
986 Mover_dst1: db 0, 0, 0 ; Low 24 bits of target addy
987 db 93h ; Access rights
988 db 00h ; Extended access rights
989 Mover_dst2: db 0 ; High 8 bits of source addy
990 Mover_dummy2: dd 0, 0, 0, 0 ; More space for the BIOS
993 MemDisk_Info equ $ ; Pointed to by installation check
994 MDI_Bytes dw MDI_Len ; Total bytes in MDI structure
995 MDI_Version db VERSION_MINOR, VERSION_MAJOR ; MEMDISK version
997 PatchArea equ $ ; This gets filled in by the installer
999 DiskBuf dd 0 ; Linear address of high memory disk
1000 DiskSize dd 0 ; Size of disk in blocks
1001 CommandLine dw 0, 0 ; Far pointer to saved command line
1003 OldInt13 dd 0 ; INT 13h in chain
1004 OldInt15 dd 0 ; INT 15h in chain
1006 OldDosMem dw 0 ; Old position of DOS mem end
1007 BootLoaderID db 0 ; Boot loader ID from header
1010 DPT_ptr dw 0 ; If nonzero, pointer to DPT
1011 ; Original DPT pointer follows
1013 MDI_Len equ $-MemDisk_Info
1015 ; ---- MDI structure ends here ---
1016 MemInt1588 dw 0 ; 1MB-65MB memory amount (1K)
1018 Cylinders dw 0 ; Cylinder count
1019 Heads dw 0 ; Head count
1020 Sectors dd 0 ; Sector count (zero-extended)
1022 Mem1MB dd 0 ; 1MB-16MB memory amount (1K)
1023 Mem16MB dd 0 ; 16MB-4G memory amount (64K)
1025 DriveNo db 0 ; Our drive number
1026 DriveType db 0 ; Our drive type (floppies)
1027 DriveCnt db 0 ; Drive count (from the BIOS)
1029 ConfigFlags db 0 ; Bit 0 - readonly
1031 MyStack dw 0 ; Offset of stack
1032 StatusPtr dw 0 ; Where to save status (zeroseg ptr)
1034 DPT times 16 db 0 ; BIOS parameter table pointer (floppies)
1035 OldInt1E dd 0 ; Previous INT 1E pointer (DPT)
1041 ; Bit 0 - DMA boundaries handled transparently
1042 ; Bit 3 - Device supports write verify
1043 ; Bit 5 - Media is lockable
1044 .cylinders dd 0 ; Filled in by installer
1045 .heads dd 0 ; Filled in by installer
1046 .sectors dd 0 ; Filled in by installer
1047 .totalsize dd 0, 0 ; Filled in by installer
1048 .bytespersec dw SECTORSIZE
1049 .eddtable dw -1, -1 ; Invalid DPTE pointer
1050 .dpikey dw 0BEDDh ; Device Path Info magic
1051 .dpilen db 2ch ; DPI len
1052 .res1 db 0 ; Reserved
1053 .res2 dw 0 ; Reserved
1054 .bustype dd 'MEM ' ; Host bus type (4 bytes, space padded)
1055 .inttype dd 'MEMORY ' ; Interface type (8 bytes, spc. padded)
1056 .intpath dd 0, 0 ; Interface path
1057 .devpath dd 0, 0, 0, 0 ; Device path
1058 .res3 db 0 ; Reserved
1059 .chksum db 0 ; DPI checksum
1065 Stack dd 0 ; Saved SS:ESP on invocation
1067 SavedAX dw 0 ; AX saved on invocation
1068 Recursive dw 0 ; Recursion counter
1070 alignb 4, db 0 ; We *MUST* end on a dword boundary
1072 E820Table equ $ ; The installer loads the E820 table here
1073 TotalSize equ $ ; End pointer