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 cmp dl,[cs:DriveShiftLimit]
151 jae .nomatch ; Drive > The maximum drive
152 ; number that we will shift for.
153 ; This leaves any higher-up BIOS
154 ; drives alone, such as an optical
155 ; disc drive 0xA0 or 0xE0
156 dec dl ; Drive > Our drive, adjust drive #
163 inc word [cs:Recursive]
165 call far [cs:OldInt13]
167 dec word [cs:Recursive]
170 cmp byte [cs:SavedAX+1],08h ; Get drive params function?
171 je .norestoredl ; DL = number of drives
172 cmp byte [cs:SavedAX+1],15h ; Get disk type function?
173 je .norestoredl ; CX:DX = size of device
180 mov ax,[bp+2] ; Flags
182 mov [bx+4],al ; Arithmetic flags
193 jmp far [cs:OldInt13]
196 ; Set up standard entry frame
203 mov bp,sp ; Point BP to the entry stack frame
206 ; Note: AX == P_AX here
207 cmp ah,Int13FuncsCnt-1
209 xor al,al ; AL = 0 is standard entry condition
211 shr di,7 ; Convert AH to an offset in DI
214 Done: ; Standard routine for return
221 mov [es:bx],ah ; Save status
225 ; This sets the low byte (the arithmetic flags) of the
226 ; FLAGS on stack to either 00h (no flags) or 01h (CF)
227 ; depending on if AH was zero or not.
228 setnz [bx+4] ; Set CF iff error
236 ; Reset affects multiple drives, so we need to pass it on
238 xor ax,ax ; Bottom of memory
240 test dl,dl ; Always pass it on if we are
242 js .hard_disk ; Bit 7 set
243 ; Some BIOSes get very unhappy if we pass a reset floppy
244 ; command to them and don't actually have any floppies.
245 ; This is a bug, but we have to deal with it nontheless.
246 ; Therefore, if we are the *ONLY* floppy drive, and the
247 ; user didn't request HD reset, then just drop the command.
248 ; BIOS equipment byte, top two bits + 1 == total # of floppies
249 test byte [es:0x410],0C0h
251 jmp .pass_on ; ... otherwise pass it to the BIOS
253 ; ... same thing for hard disks, sigh ...
254 cmp byte [es:0x475],1 ; BIOS variable for number of hard
259 pop ax ; Drop return address
260 popad ; Restore all registers
263 lss esp,[cs:Stack] ; Restore the stack
264 and dl,80h ; Clear all but the type bit
269 pop dx ; Drop return address
272 mov ah,01h ; Unsupported function
276 test byte [DriveNo],80h
277 mov bl,02h ; Type 02h = floppy with changeline
279 ; Hard disks only! DO NOT set CX:DX for floppies...
280 ; it apparently causes Win98SE DOS to go into an loop
281 ; resetting the drive over and over. Sigh.
283 mov dx,[DiskSize] ; Return the disk size in sectors
288 mov P_AH,bl ; 02h floppy, 03h hard disk
289 pop ax ; Drop return address
290 xor ax,ax ; Success...
291 jmp DoneWeird ; But don't stick it into P_AX
297 mov ah,[bx] ; Copy last status
309 movzx ax,P_AL ; AH = 0, AL = transfer count
316 test byte [ConfigFlags],CONFIG_READONLY
319 xchg esi,edi ; Opposite direction of a Read!
321 .readonly: mov ah,03h ; Write protected medium
324 ; Verify integrity; just bounds-check
327 call setup_regs ; Returns error if appropriate
328 ; And fall through to success
330 CheckIfReady: ; These are always-successful noop functions
338 xor ax,ax ; Always successful
343 mov dl,[DriveCnt] ; Cached data
345 test byte [DriveNo],80h
353 dec ax ; We report the highest #, not the count
363 ; Is this MEMDISK installation check?
374 ; MEMDISK installation check...
380 mov P_DI,MemDisk_Info
386 ; EDD functions -- only if enabled
395 mov P_BX,0AA55h ; EDD signature
396 mov P_AX,03000h ; EDD 3.0
397 mov P_CX,0003h ; Bit 0 - Fixed disk access subset
398 ; Bit 1 - Locking and ejecting subset
399 pop ax ; Drop return address
401 jmp DoneWeird ; Success, but AH != 0, sigh...
417 xchg esi,edi ; Opposite direction of a Read!
424 call edd_setup_regs ; Just bounds checking
436 lodsw ; Length of our DPT
438 cmp cx,26 ; Minimum size
460 ; Set up registers as for a "Read", and compares against disk
462 ; WARNING: This fails immediately, even if we can transfer some
463 ; sectors. This isn't really the correct behaviour.
466 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
468 ; CL[0:5] = sector (1-based) CL[7:6] = cyl[9:8]
471 movzx ebx,cl ; Sector number
473 dec ebx ; Sector number is 1-based
476 movzx edi,P_DH ; Head number
477 movzx eax,word [Heads]
481 xchg cl,ch ; Now (E)CX <- cylinder number
482 mul ecx ; eax <- Heads*cyl# (edx <- 0)
486 ; Now eax = LBA, edx = 0
489 ; setup_regs continues...
491 ; Note: edi[31:16] and ecx[31:16] = 0 already
492 mov di,P_BX ; Get linear address of target buffer
495 add edi,ecx ; EDI = address to fetch to
496 movzx ecx,P_AL ; Sector count
498 add eax,ecx ; LBA of final sector + 1
499 shl esi,SECTORSIZE_LG2 ; LBA -> byte offset
500 add esi,[DiskBuf] ; Get address in high memory
501 cmp eax,[DiskSize] ; Check the high mark against limit
503 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
506 .overrun: pop ax ; Drop setup_regs return address
507 mov ax,0200h ; Missing address mark
510 ; Set up registers as for an EDD Read, and compares against disk size.
514 mov si,P_SI ; DS:SI -> DAPA
521 cmp dword [es:si+4],-1
524 movzx ebx,word [es:si+4] ; Offset
525 movzx edi,word [es:si+6] ; Segment
531 cmp dx,24 ; Must be large enough to hold
535 cmp dword [es:si+20],0 ; > 4 GB addresses not supported
536 mov ax,0900h ; "Data boundary error" - bogus, but
537 ; no really better code available
543 cmp dword [es:si+12],0 ; LBA too large?
546 movzx ecx, word [es:si+2] ; Sectors to transfer
547 mov esi,[es:si+8] ; Starting sector
554 shl ecx,SECTORSIZE_LG2-2 ; Convert to dwords
555 shl esi,SECTORSIZE_LG2 ; Convert to an offset
562 mov ax,0100h ; Invalid command
564 pop ax ; Drop setup_regs return address
568 mov ax,0200h ; "Address mark not found" =
569 ; LBA beyond end of disk
571 and word [es:si+2],0 ; No sectors transferred
577 mov ax,0B200h ; Volume Not Removable
584 ; INT 15h intercept routines
587 cmp edx,534D4150h ; "SMAP"
589 cmp ecx,20 ; Need 20 bytes
599 add bx,12 ; Advance to next
600 mov eax,[bx-4] ; Type
601 and eax,eax ; Null type?
602 jz .renew ; If so advance to next
604 mov eax,[bx-12] ; Start addr (low)
605 mov edx,[bx-8] ; Start addr (high)
608 mov eax,[bx] ; End addr (low)
609 mov edx,[bx+4] ; End addr (high)
610 sub eax,[bx-12] ; Derive the length
612 mov [es:di+8],eax ; Length (low)
613 mov [es:di+12],edx ; Length (high)
614 cmp dword [bx+8],-1 ; Type of next = end?
616 xor ebx,ebx ; Done with table
619 mov edx,eax ; Some systems expect eax = edx = SMAP
620 mov ecx,20 ; Bytes loaded
623 mov byte [bp+6], 02h ; Clear CF
628 mov byte [bp+6], 03h ; Set CF
645 jmp far [cs:OldInt15]
647 int15_e801: ; Get mem size for > 64 MB config
652 jmp short int15_success
654 int15_e881: ; Get mem size for > 64 MB config
660 jmp short int15_success
662 int15_88: ; Get extended mem size
663 mov ax,[cs:MemInt1588]
664 jmp short int15_success
667 ; Routine to copy in/out of high memory
668 ; esi = linear source address
669 ; edi = linear target address
670 ; ecx = 32-bit word count
672 ; Assumes cs = ds = es
680 mov bx, real_int15_stub
682 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
683 jz .anymode ; Always do the real INT 15h
685 smsw ax ; Unprivileged!
687 jnz .protmode ; Protmode -> do real INT 15h
690 ; Raw or Safeint mode, and we're in real mode...
692 test byte [ConfigFlags], CONFIG_SAFEINT
697 ; We're in real mode, do it outselves
712 ; Test to see if A20 is enabled or not
729 push dx ; <D> Save A20 status
732 mov ax,2401h ; Enable A20
738 ; DX = 16 for BIGRAW, 8 for RAW
739 ; 8 is selector for a 64K flat segment,
740 ; 16 is selector for a 4GB flat segment.
747 mov bx,16 ; Large flat segment
753 ; DX has the appropriate value to put in
754 ; the registers on return
761 pop dx ; <D> A20 status
767 mov ax,2400h ; Disable A20
774 ; We're in real mode with CONFIG_SAFEINT, invoke the
775 ; original INT 15h vector. We used to test for the
776 ; INT 15h vector being unchanged here, but that is
777 ; *us*; however, the test was wrong for years (always
778 ; negative) so instead of fixing the test do what we
779 ; tested and don't bother probing.
780 mov bx, fake_int15_stub
794 push ecx ; Transfer size this cycle
798 mov [Mover_src1+2], al
803 mov [Mover_dst1+2], al
807 shl cx,1 ; Convert to 16-bit words
808 call bx ; INT 15h stub
809 pop eax ; Transfer size this cycle
829 cli ; Some BIOSes enable interrupts on INT 15h
888 Int13Funcs dw Reset ; 00h - RESET
889 dw GetStatus ; 01h - GET STATUS
891 dw Write ; 03h - WRITE
892 dw Verify ; 04h - VERIFY
893 dw Invalid ; 05h - FORMAT TRACK
894 dw Invalid ; 06h - FORMAT TRACK AND SET BAD FLAGS
895 dw Invalid ; 07h - FORMAT DRIVE AT TRACK
896 dw GetParms ; 08h - GET PARAMETERS
897 dw InitWithParms ; 09h - INITIALIZE CONTROLLER WITH
901 dw Seek ; 0Ch - SEEK TO CYLINDER
902 dw Reset ; 0Dh - RESET HARD DISKS
905 dw CheckIfReady ; 10h - CHECK IF READY
906 dw Recalibrate ; 11h - RECALIBRATE
910 dw GetDriveType ; 15h - GET DRIVE TYPE
911 dw DetectChange ; 16h - DETECT DRIVE CHANGE
923 dw ReadMult ; 21h - READ MULTIPLE
924 dw WriteMult ; 22h - WRITE MULTIPLE
925 dw SetMode ; 23h - SET CONTROLLER FEATURES
926 dw SetMode ; 24h - SET MULTIPLE MODE
927 dw Invalid ; 25h - IDENTIFY DRIVE
955 dw EDDPresence ; 41h - EDD PRESENCE DETECT
956 dw EDDRead ; 42h - EDD READ
957 dw EDDWrite ; 43h - EDD WRITE
958 dw EDDVerify ; 44h - EDD VERIFY
959 dw EDDLock ; 45h - EDD LOCK/UNLOCK MEDIA
960 dw EDDEject ; 46h - EDD EJECT
961 dw EDDSeek ; 47h - EDD SEEK
962 dw EDDGetParms ; 48h - EDD GET PARAMETERS
963 dw EDDDetectChange ; 49h - EDD MEDIA CHANGE STATUS
967 Int13FuncsCnt equ (Int13FuncsEnd-Int13Funcs) >> 1
971 Shaker dw ShakerEnd-$-1 ; Descriptor table limit
972 dd 0 ; Pointer to self
975 Shaker_RMDS: dd 0x0000ffff ; 64K data segment
978 Shaker_DS: dd 0x0000ffff ; 4GB data segment
985 Mover dd 0, 0, 0, 0 ; Must be zero
986 dw 0ffffh ; 64 K segment size
987 Mover_src1: db 0, 0, 0 ; Low 24 bits of source addy
988 db 93h ; Access rights
989 db 00h ; Extended access rights
990 Mover_src2: db 0 ; High 8 bits of source addy
991 dw 0ffffh ; 64 K segment size
992 Mover_dst1: db 0, 0, 0 ; Low 24 bits of target addy
993 db 93h ; Access rights
994 db 00h ; Extended access rights
995 Mover_dst2: db 0 ; High 8 bits of source addy
996 Mover_dummy2: dd 0, 0, 0, 0 ; More space for the BIOS
999 MemDisk_Info equ $ ; Pointed to by installation check
1000 MDI_Bytes dw MDI_Len ; Total bytes in MDI structure
1001 MDI_Version db VERSION_MINOR, VERSION_MAJOR ; MEMDISK version
1003 PatchArea equ $ ; This gets filled in by the installer
1005 DiskBuf dd 0 ; Linear address of high memory disk
1006 DiskSize dd 0 ; Size of disk in blocks
1007 CommandLine dw 0, 0 ; Far pointer to saved command line
1009 OldInt13 dd 0 ; INT 13h in chain
1010 OldInt15 dd 0 ; INT 15h in chain
1012 OldDosMem dw 0 ; Old position of DOS mem end
1013 BootLoaderID db 0 ; Boot loader ID from header
1016 DPT_ptr dw 0 ; If nonzero, pointer to DPT
1017 ; Original DPT pointer follows
1019 MDI_Len equ $-MemDisk_Info
1021 ; ---- MDI structure ends here ---
1022 DriveShiftLimit db 0ffh ; Installer will [soon] probe for
1023 ; a range of contiguous drives.
1024 ; Any BIOS drives above this region
1025 ; shall not be impacted by our
1026 ; shifting behaviour
1027 db 0 ; pad to a DWORD
1028 dw 0 ; pad to a QWORD
1029 MemInt1588 dw 0 ; 1MB-65MB memory amount (1K)
1031 Cylinders dw 0 ; Cylinder count
1032 Heads dw 0 ; Head count
1033 Sectors dd 0 ; Sector count (zero-extended)
1035 Mem1MB dd 0 ; 1MB-16MB memory amount (1K)
1036 Mem16MB dd 0 ; 16MB-4G memory amount (64K)
1038 DriveNo db 0 ; Our drive number
1039 DriveType db 0 ; Our drive type (floppies)
1040 DriveCnt db 0 ; Drive count (from the BIOS)
1042 ConfigFlags db 0 ; Bit 0 - readonly
1044 MyStack dw 0 ; Offset of stack
1045 StatusPtr dw 0 ; Where to save status (zeroseg ptr)
1047 DPT times 16 db 0 ; BIOS parameter table pointer (floppies)
1048 OldInt1E dd 0 ; Previous INT 1E pointer (DPT)
1054 ; Bit 0 - DMA boundaries handled transparently
1055 ; Bit 3 - Device supports write verify
1056 ; Bit 5 - Media is lockable
1057 .cylinders dd 0 ; Filled in by installer
1058 .heads dd 0 ; Filled in by installer
1059 .sectors dd 0 ; Filled in by installer
1060 .totalsize dd 0, 0 ; Filled in by installer
1061 .bytespersec dw SECTORSIZE
1062 .eddtable dw -1, -1 ; Invalid DPTE pointer
1063 .dpikey dw 0BEDDh ; Device Path Info magic
1064 .dpilen db 2ch ; DPI len
1065 .res1 db 0 ; Reserved
1066 .res2 dw 0 ; Reserved
1067 .bustype dd 'MEM ' ; Host bus type (4 bytes, space padded)
1068 .inttype dd 'MEMORY ' ; Interface type (8 bytes, spc. padded)
1069 .intpath dd 0, 0 ; Interface path
1070 .devpath dd 0, 0, 0, 0 ; Device path
1071 .res3 db 0 ; Reserved
1072 .chksum db 0 ; DPI checksum
1078 Stack dd 0 ; Saved SS:ESP on invocation
1080 SavedAX dw 0 ; AX saved on invocation
1081 Recursive dw 0 ; Recursion counter
1083 alignb 4, db 0 ; We *MUST* end on a dword boundary
1085 E820Table equ $ ; The installer loads the E820 table here
1086 TotalSize equ $ ; End pointer