memdisk: don't set CX:DX for INT 13h AH=15h for floppies
[profile/ivi/syslinux.git] / memdisk / memdisk.inc
1 ; -*- fundamental -*- (asm-mode sucks)
2 ; ****************************************************************************
3 ;
4 ;  memdisk.inc
5 ;
6 ;  A program to emulate an INT 13h disk BIOS from a "disk" in extended
7 ;  memory.
8 ;
9 ;   Copyright 2001-2009 H. Peter Anvin - All Rights Reserved
10 ;   Copyright 2009 Intel Corporation; author: H. Peter Anvin
11 ;
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.
17 ;
18 ; ****************************************************************************
19
20 %include "../version.gen"
21
22 ; %define DEBUG_TRACERS                 ; Uncomment to get debugging tracers
23
24 %ifdef DEBUG_TRACERS
25
26 %macro TRACER   1
27         call debug_tracer
28         db %1
29 %endmacro
30 %macro WRITEHEX2 0-1 al
31 %ifnidni %1,al
32         push ax
33         mov al,%1
34         call writehex2
35         pop ax
36 %else
37         call writehex2
38 %endif
39 %endmacro
40 %macro WRITEHEX4 0-1 ax
41 %ifnidni %1,ax
42         push ax
43         mov ax,%1
44         call writehex4
45         pop ax
46 %else
47         call writehex4
48 %endif
49 %endmacro
50 %macro WRITEHEX8 0-1 eax
51 %ifnidni %1,eax
52         push eax
53         mov eax,%1
54         call writehex8
55         pop eax
56 %else
57         call writehex8
58 %endif
59 %endmacro
60
61 %else   ; DEBUG_TRACERS
62
63 %macro  TRACER  1
64 %endmacro
65 %macro WRITEHEX2 0-1
66 %endmacro
67 %macro WRITEHEX4 0-1
68 %endmacro
69 %macro WRITEHEX8 0-1
70 %endmacro
71
72 %endif  ; DEBUG_TRACERS
73
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!
79
80                 org 0h
81
82 %define SECTORSIZE_LG2  9               ; log2(sector size)
83 %define SECTORSIZE      (1 << SECTORSIZE_LG2)
84
85                 ; Parameter registers definition; this is the definition
86                 ; of the stack frame.
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]
116
117                 section .text
118                 ; These pointers are used by the installer and
119                 ; must be first in the binary
120 Pointers:       dw Int13Start
121                 dw Int15Start
122                 dw PatchArea
123                 dw TotalSize
124                 dw IretPtr
125
126 IretPtr         equ Int13Start.iret
127 Int13Start:
128                 cmp word [cs:Recursive],0
129                 jne recursive
130
131                 ; Swap stack
132                 mov [cs:Stack],esp
133                 mov [cs:Stack+4],ss
134                 mov [cs:SavedAX],ax
135                 mov ax,cs
136                 mov ss,ax
137                 mov sp,[cs:MyStack]
138
139                 ; See if DL points to our class of device (FD, HD)
140                 push dx
141                 push dx
142                 xor dl,[cs:DriveNo]
143                 pop dx
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
148                 cmp dl,[cs:DriveNo]
149                 jb .nomatch             ; Drive < Our drive
150                 dec dl                  ; Drive > Our drive, adjust drive #
151 .nomatch:
152 %ifdef DEBUG_TRACERS
153                 TRACER '!'
154                 WRITEHEX2 dl
155                 TRACER ','
156                 mov ax,[cs:SavedAX]
157                 WRITEHEX4
158 %else
159                 mov ax,[cs:SavedAX]
160 %endif
161                 inc word [cs:Recursive]
162                 pushf
163                 call far [cs:OldInt13]
164                 pushf
165                 dec word [cs:Recursive]
166                 push bp
167                 mov bp,sp
168                 cmp byte [cs:SavedAX+1],08h     ; Get drive params function?
169                 je .norestoredl                 ; DL = number of drives
170                 cmp byte [cs:SavedAX+1],15h     ; Get disk type function?
171                 je .norestoredl                 ; CX:DX = size of device
172 .restoredl:
173                 mov dl,[bp+4]
174 .norestoredl:
175                 push ax
176                 push ebx
177                 push ds
178                 mov ax,[bp+2]           ; Flags
179                 lds ebx,[cs:Stack]
180                 mov [bx+4],al           ; Arithmetic flags
181                 pop ds
182                 pop ebx
183                 pop ax
184                 pop bp
185                 lss esp,[cs:Stack]
186 .iret:          iret
187
188 recursive:
189                 TRACER '@'
190 jmp_oldint13:
191                 jmp far [cs:OldInt13]
192
193 our_drive:
194                 ; Set up standard entry frame
195                 push ds
196                 push es
197                 mov ds,ax
198                 mov es,ax
199                 mov ax,[SavedAX]
200                 pushad
201                 mov bp,sp               ; Point BP to the entry stack frame
202                 TRACER 'F'
203                 WRITEHEX4
204                 ; Note: AX == P_AX here
205                 cmp ah,Int13FuncsCnt-1
206                 ja Invalid_jump
207                 xor al,al               ; AL = 0 is standard entry condition
208                 mov di,ax
209                 shr di,7                ; Convert AH to an offset in DI
210                 call [Int13Funcs+di]
211
212 Done:           ; Standard routine for return
213                 mov P_AX,ax
214 DoneWeird:
215                 TRACER 'D'
216                 xor bx,bx
217                 mov es,bx
218                 mov bx,[StatusPtr]
219                 mov [es:bx],ah          ; Save status
220                 and ah,ah
221
222                 lds ebx,[Stack]
223                 ; This sets the low byte (the arithmetic flags) of the
224                 ; FLAGS on stack to either 00h (no flags) or 01h (CF)
225                 ; depending on if AH was zero or not.
226                 setnz [bx+4]            ; Set CF iff error
227                 popad
228                 pop es
229                 pop ds
230                 lss esp,[cs:Stack]
231                 iret
232
233 Reset:
234                 ; Reset affects multiple drives, so we need to pass it on
235                 TRACER 'R'
236                 xor ax,ax               ; Bottom of memory
237                 mov es,ax
238                 test dl,dl              ; Always pass it on if we are
239                                         ; resetting HD
240                 js .hard_disk           ; Bit 7 set
241                 ; Some BIOSes get very unhappy if we pass a reset floppy
242                 ; command to them and don't actually have any floppies.
243                 ; This is a bug, but we have to deal with it nontheless.
244                 ; Therefore, if we are the *ONLY* floppy drive, and the
245                 ; user didn't request HD reset, then just drop the command.
246                 ; BIOS equipment byte, top two bits + 1 == total # of floppies
247                 test byte [es:0x410],0C0h
248                 jz success
249                 jmp .pass_on            ; ... otherwise pass it to the BIOS
250 .hard_disk:
251                 ; ... same thing for hard disks, sigh ...
252                 cmp byte [es:0x475],1   ; BIOS variable for number of hard
253                                         ; disks
254                 jbe success
255
256 .pass_on:
257                 pop ax                  ; Drop return address
258                 popad                   ; Restore all registers
259                 pop es
260                 pop ds
261                 lss esp,[cs:Stack]      ; Restore the stack
262                 and dl,80h              ; Clear all but the type bit
263                 jmp jmp_oldint13
264
265
266 Invalid:
267                 pop dx                  ; Drop return address
268 Invalid_jump:
269                 TRACER 'I'
270                 mov ah,01h              ; Unsupported function
271                 jmp short Done
272
273 GetDriveType:
274                 test byte [DriveNo],80h
275                 mov bl,02h              ; Type 02h = floppy with changeline
276                 jz .floppy
277                 ; Hard disks only!  DO NOT set CX:DX for floppies...
278                 ; it apparently causes Win98SE DOS to go into an loop
279                 ; resetting the drive over and over.  Sigh.
280                 inc bx                  ; Type = 03h
281                 mov dx,[DiskSize]       ; Return the disk size in sectors
282                 mov P_DX,dx
283                 mov cx,[DiskSize+2]
284                 mov P_CX,cx
285 .floppy:
286                 mov P_AH,bl             ; 02h floppy, 03h hard disk
287                 pop ax                  ; Drop return address
288                 xor ax,ax               ; Success...
289                 jmp DoneWeird           ; But don't stick it into P_AX
290
291 GetStatus:
292                 xor ax,ax
293                 mov es,ax
294                 mov bx,[StatusPtr]
295                 mov ah,[bx]             ; Copy last status
296                 ret
297
298 ReadMult:
299                 TRACER 'm'
300 Read:
301                 TRACER 'R'
302                 call setup_regs
303 do_copy:
304                 TRACER '<'
305                 call bcopy
306                 TRACER '>'
307                 movzx ax,P_AL           ; AH = 0, AL = transfer count
308                 ret
309
310 WriteMult:
311                 TRACER 'M'
312 Write:
313                 TRACER 'W'
314                 test byte [ConfigFlags],CONFIG_READONLY
315                 jnz .readonly
316                 call setup_regs
317                 xchg esi,edi            ; Opposite direction of a Read!
318                 jmp short do_copy
319 .readonly:      mov ah,03h              ; Write protected medium
320                 ret
321
322                 ; Verify integrity; just bounds-check
323 Seek:
324 Verify:
325                 call setup_regs         ; Returns error if appropriate
326                 ; And fall through to success
327
328 CheckIfReady:                           ; These are always-successful noop functions
329 Recalibrate:
330 InitWithParms:
331 DetectChange:
332 EDDDetectChange:
333 EDDLock:
334 SetMode:
335 success:
336                 xor ax,ax               ; Always successful
337                 ret
338
339 GetParms:
340                 TRACER 'G'
341                 mov dl,[DriveCnt]       ; Cached data
342                 mov P_DL,dl
343                 test byte [DriveNo],80h
344                 jnz .hd
345                 mov P_DI,DPT
346                 mov P_ES,cs
347                 mov bl,[DriveType]
348                 mov P_BL,bl
349 .hd:
350                 mov ax,[Cylinders]
351                 dec ax                  ; We report the highest #, not the count
352                 xchg al,ah
353                 shl al,6
354                 or al,[Sectors]
355                 mov P_CX,ax
356                 mov ax,[Heads]
357                 dec ax
358                 mov P_DH,al
359
360                 ;
361                 ; Is this MEMDISK installation check?
362                 ;
363                 cmp P_HAX,'ME'
364                 jne .notic
365                 cmp P_HCX,'MD'
366                 jne .notic
367                 cmp P_HDX,'IS'
368                 jne .notic
369                 cmp P_HBX,'K?'
370                 jne .notic
371
372                 ; MEMDISK installation check...
373                 mov P_HAX,'!M'
374                 mov P_HCX,'EM'
375                 mov P_HDX,'DI'
376                 mov P_HBX,'SK'
377                 mov P_ES,cs
378                 mov P_DI,MemDisk_Info
379
380 .notic:
381                 xor ax,ax
382                 ret
383 ;
384 ; EDD functions -- only if enabled
385 ;
386 %if EDD
387 EDDPresence:
388                 TRACER 'E'
389                 TRACER 'c'
390
391                 cmp P_BX,55AAh
392                 jne Invalid
393                 mov P_BX,0AA55h         ; EDD signature
394                 mov P_AX,03000h         ; EDD 3.0
395                 mov P_CX,0003h          ; Bit 0 - Fixed disk access subset
396                                         ; Bit 1 - Locking and ejecting subset
397                 pop ax                  ; Drop return address
398                 xor ax,ax               ; Success
399                 jmp DoneWeird           ; Success, but AH != 0, sigh...
400
401 EDDRead:
402                 TRACER 'E'
403                 TRACER 'r'
404
405                 call edd_setup_regs
406                 call bcopy
407                 xor ax,ax
408                 ret
409
410 EDDWrite:
411                 TRACER 'E'
412                 TRACER 'w'
413
414                 call edd_setup_regs
415                 xchg esi,edi            ; Opposite direction of a Read!
416                 call bcopy
417                 xor ax,ax
418                 ret
419
420 EDDVerify:
421 EDDSeek:
422                 call edd_setup_regs     ; Just bounds checking
423                 xor ax,ax
424                 ret
425
426 EDDGetParms:
427                 TRACER 'E'
428                 TRACER 'p'
429
430                 mov es,P_DS
431                 mov di,P_SI
432                 mov si,EDD_DPT
433
434                 lodsw                   ; Length of our DPT
435                 mov cx,[es:di]
436                 cmp cx,26               ; Minimum size
437                 jb .overrun
438
439                 cmp cx,ax
440                 jb .oksize
441                 mov cx,ax
442
443 .oksize:
444                 mov ax,cx
445                 stosw
446                 dec cx
447                 dec cx
448                 rep movsb
449
450                 xor ax,ax
451                 ret
452
453 .overrun:
454                 mov ax,0100h
455                 ret
456 %endif ; EDD
457
458                 ; Set up registers as for a "Read", and compares against disk
459                 ; size.
460                 ; WARNING: This fails immediately, even if we can transfer some
461                 ; sectors.  This isn't really the correct behaviour.
462 setup_regs:
463
464                 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
465                 ; CH = cyl[7:0]
466                 ; CL[0:5] = sector (1-based)  CL[7:6] = cyl[9:8]
467                 ; DH = head
468                 movzx ecx,P_CX
469                 movzx ebx,cl            ; Sector number
470                 and bl,3Fh
471                 dec ebx                 ; Sector number is 1-based
472                 cmp bx,[Sectors]
473                 jae .overrun
474                 movzx edi,P_DH          ; Head number
475                 movzx eax,word [Heads]
476                 cmp di,ax
477                 jae .overrun
478                 shr cl,6
479                 xchg cl,ch              ; Now (E)CX <- cylinder number
480                 mul ecx                 ; eax <- Heads*cyl# (edx <- 0)
481                 add eax,edi
482                 mul dword [Sectors]
483                 add eax,ebx
484                 ; Now eax = LBA, edx = 0
485
486                 ;
487                 ; setup_regs continues...
488                 ;
489                 ; Note: edi[31:16] and ecx[31:16] = 0 already
490                 mov di,P_BX             ; Get linear address of target buffer
491                 mov cx,P_ES
492                 shl ecx,4
493                 add edi,ecx             ; EDI = address to fetch to
494                 movzx ecx,P_AL          ; Sector count
495                 mov esi,eax
496                 add eax,ecx             ; LBA of final sector + 1
497                 shl esi,SECTORSIZE_LG2  ; LBA -> byte offset
498                 add esi,[DiskBuf]       ; Get address in high memory
499                 cmp eax,[DiskSize]      ; Check the high mark against limit
500                 ja .overrun
501                 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
502                 ret
503
504 .overrun:       pop ax                  ; Drop setup_regs return address
505                 mov ax,0200h            ; Missing address mark
506                 ret                     ; Return to Done
507
508                 ; Set up registers as for an EDD Read, and compares against disk size.
509 %if EDD
510 edd_setup_regs:
511                 push es
512                 mov si,P_SI             ; DS:SI -> DAPA
513                 mov es,P_DS
514
515                 mov dx,[es:si]
516                 cmp dx,16
517                 jb .baddapa
518
519                 cmp dword [es:si+4],-1
520                 je .linear_address
521
522                 movzx ebx,word [es:si+4]        ; Offset
523                 movzx edi,word [es:si+6]        ; Segment
524                 shl edi,4
525                 add ebx,edi
526                 jmp .got_address
527
528 .linear_address:
529                 cmp dx,24               ; Must be large enough to hold
530                                         ; linear address
531                 jb .baddapa
532
533                 cmp dword [es:si+20],0  ; > 4 GB addresses not supported
534                 mov ax,0900h            ; "Data boundary error" - bogus, but
535                                         ; no really better code available
536                 jne .error
537
538                 mov ebx,[es:si+16]
539
540 .got_address:
541                 cmp dword [es:si+12],0          ; LBA too large?
542                 jne .overrun
543
544                 movzx ecx, word [es:si+2]       ; Sectors to transfer
545                 mov esi,[es:si+8]               ; Starting sector
546                 mov eax,esi
547                 add eax,ecx
548                 jc .overrun
549                 cmp eax,[DiskSize]
550                 ja .overrun
551
552                 shl ecx,SECTORSIZE_LG2-2        ; Convert to dwords
553                 shl esi,SECTORSIZE_LG2          ; Convert to an offset
554                 add esi,[DiskBuf]
555                 mov edi,ebx
556                 pop es
557                 ret
558
559 .baddapa:
560                 mov ax,0100h            ; Invalid command
561                 pop es
562                 pop ax                  ; Drop setup_regs return address
563                 ret
564
565 .overrun:
566                 mov ax,0200h            ; "Address mark not found" =
567                                         ; LBA beyond end of disk
568 .error:
569                 and word [es:si+2],0    ; No sectors transferred
570                 pop es
571                 pop ax
572                 ret
573
574 EDDEject:
575                 mov ax,0B200h           ; Volume Not Removable
576                 ret
577
578 %endif ; EDD
579
580
581 ;
582 ; INT 15h intercept routines
583 ;
584 int15_e820:
585                 cmp edx,534D4150h       ; "SMAP"
586                 jne oldint15
587                 cmp ecx,20              ; Need 20 bytes
588                 jb err86
589                 push ds
590                 push cs
591                 pop ds
592                 push edx                ; "SMAP"
593                 and ebx,ebx
594                 jne .renew
595                 mov ebx,E820Table
596 .renew:
597                 add bx,12               ; Advance to next
598                 mov eax,[bx-4]          ; Type
599                 and eax,eax             ; Null type?
600                 jz .renew               ; If so advance to next
601                 mov [es:di+16],eax
602                 mov eax,[bx-12]         ; Start addr (low)
603                 mov edx,[bx-8]          ; Start addr (high)
604                 mov [es:di],eax
605                 mov [es:di+4],edx
606                 mov eax,[bx]            ; End addr (low)
607                 mov edx,[bx+4]          ; End addr (high)
608                 sub eax,[bx-12]         ; Derive the length
609                 sbb edx,[bx-8]
610                 mov [es:di+8],eax       ; Length (low)
611                 mov [es:di+12],edx      ; Length (high)
612                 cmp dword [bx+8],-1     ; Type of next = end?
613                 jne .notdone
614                 xor ebx,ebx             ; Done with table
615 .notdone:
616                 pop eax                 ; "SMAP"
617                 mov edx,eax             ; Some systems expect eax = edx = SMAP
618                 mov ecx,20              ; Bytes loaded
619                 pop ds
620 int15_success:
621                 mov byte [bp+6], 02h    ; Clear CF
622                 pop bp
623                 iret
624
625 err86:
626                 mov byte [bp+6], 03h    ; Set CF
627                 mov ah,86h
628                 pop bp
629                 iret
630
631 Int15Start:
632                 push bp
633                 mov bp,sp
634                 cmp ax,0E820h
635                 je near int15_e820
636                 cmp ax,0E801h
637                 je int15_e801
638                 cmp ax,0E881h
639                 je int15_e881
640                 cmp ah,88h
641                 je int15_88
642 oldint15:       pop bp
643                 jmp far [cs:OldInt15]
644
645 int15_e801:                             ; Get mem size for > 64 MB config
646                 mov ax,[cs:Mem1MB]
647                 mov cx,ax
648                 mov bx,[cs:Mem16MB]
649                 mov dx,bx
650                 jmp short int15_success
651
652 int15_e881:                             ; Get mem size for > 64 MB config
653                                         ; 32-bit code
654                 mov eax,[cs:Mem1MB]
655                 mov ecx,eax
656                 mov ebx,[cs:Mem16MB]
657                 mov edx,ebx
658                 jmp short int15_success
659
660 int15_88:                               ; Get extended mem size
661                 mov ax,[cs:MemInt1588]
662                 jmp short int15_success
663
664 ;
665 ; Routine to copy in/out of high memory
666 ; esi = linear source address
667 ; edi = linear target address
668 ; ecx = 32-bit word count
669 ;
670 ; Assumes cs = ds = es
671 ;
672 bcopy:
673                 push eax
674                 push ebx
675                 push edx
676                 push ebp
677
678                 mov bx, real_int15_stub
679
680                 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
681                 jz .anymode             ; Always do the real INT 15h
682
683                 smsw ax                 ; Unprivileged!
684                 test al,01h
685                 jnz .protmode           ; Protmode -> do real INT 15h
686
687 .realmode:
688                 ; Raw or Safeint mode, and we're in real mode...
689
690                 test byte [ConfigFlags], CONFIG_SAFEINT
691                 jnz .fakeint15
692
693 .raw:
694                 TRACER 'r'
695                 ; We're in real mode, do it outselves
696
697                 pushfd                  ; <A>
698                 push ds                 ; <B>
699                 push es                 ; <C>
700
701                 cli
702                 cld
703
704                 xor ebx,ebx
705                 mov bx,cs
706                 shl ebx,4
707                 lea edx,[Shaker+ebx]
708                 mov [Shaker+2],edx
709
710                 ; Test to see if A20 is enabled or not
711                 xor ax,ax
712                 mov ds,ax
713                 dec ax
714                 mov es,ax
715
716                 mov ax,[0]
717                 mov bx,ax
718                 xor bx,[es:10h]
719                 not ax
720                 mov [0],ax
721                 mov dx,ax
722                 xor dx,[es:10h]
723                 not ax
724                 mov [0],ax
725
726                 or dx,bx
727                 push dx                 ; <D> Save A20 status
728                 jnz .skip_a20e
729
730                 mov ax,2401h            ; Enable A20
731                 int 15h
732 .skip_a20e:
733                 mov dl,[ConfigFlags]
734                 and dx,CONFIG_BIGRAW
735                 add dx,8
736                 ; DX = 16 for BIGRAW, 8 for RAW
737                 ;  8 is selector for a 64K flat segment,
738                 ; 16 is selector for a 4GB flat segment.
739
740                 lgdt [cs:Shaker]
741                 mov eax,cr0
742                 or al,01h
743                 mov cr0,eax
744
745                 mov bx,16               ; Large flat segment
746                 mov ds,bx
747                 mov es,bx
748
749                 a32 rep movsd
750
751                 ; DX has the appropriate value to put in
752                 ; the registers on return
753                 mov ds,dx
754                 mov es,dx
755
756                 and al,~01h
757                 mov cr0,eax
758
759                 pop dx                  ; <D> A20 status
760                 pop es                  ; <C>
761                 pop ds                  ; <B>
762
763                 and dx,dx
764                 jnz .skip_a20d
765                 mov ax,2400h            ; Disable A20
766                 int 15h
767 .skip_a20d:
768                 popfd                   ; <A>
769                 jmp .done
770
771 .fakeint15:
772                 ; We're in real mode with CONFIG_SAFEINT, invoke the
773                 ; original INT 15h vector.  We used to test for the
774                 ; INT 15h vector being unchanged here, but that is
775                 ; *us*; however, the test was wrong for years (always
776                 ; negative) so instead of fixing the test do what we
777                 ; tested and don't bother probing.
778                 mov bx, fake_int15_stub
779
780 .protmode:
781                 TRACER 'p'
782 .anymode:
783
784 .copy_loop:
785                 push esi
786                 push edi
787                 push ecx
788                 cmp ecx,4000h
789                 jna .safe_size
790                 mov ecx,4000h
791 .safe_size:
792                 push ecx        ; Transfer size this cycle
793                 mov eax, esi
794                 mov [Mover_src1], si
795                 shr eax, 16
796                 mov [Mover_src1+2], al
797                 mov [Mover_src2], ah
798                 mov eax, edi
799                 mov [Mover_dst1], di
800                 shr eax, 16
801                 mov [Mover_dst1+2], al
802                 mov [Mover_dst2], ah
803                 mov si,Mover
804                 mov ah, 87h
805                 shl cx,1        ; Convert to 16-bit words
806                 call bx         ; INT 15h stub
807                 pop eax         ; Transfer size this cycle
808                 pop ecx
809                 pop edi
810                 pop esi
811                 jc .error
812                 lea esi,[esi+4*eax]
813                 lea edi,[edi+4*eax]
814                 sub ecx, eax
815                 jnz .copy_loop
816                 ; CF = 0
817 .error:
818 .done:
819                 pop ebp
820                 pop edx
821                 pop ebx
822                 pop eax
823                 ret
824
825 real_int15_stub:
826                 int 15h
827                 cli             ; Some BIOSes enable interrupts on INT 15h
828                 ret
829
830 fake_int15_stub:
831                 pushf
832                 call far [OldInt15]
833                 cli
834                 ret
835
836 %ifdef DEBUG_TRACERS
837 debug_tracer:   pushad
838                 pushfd
839                 mov bp,sp
840                 mov bx,[bp+9*4]
841                 mov al,[cs:bx]
842                 inc word [bp+9*4]
843                 mov ah,0Eh
844                 mov bx,7
845                 int 10h
846                 popfd
847                 popad
848                 ret
849
850 writehex2:      pushad
851                 pushfd
852                 mov cx,2
853                 ror eax,4
854                 jmp writehex_common
855 writehex4:      pushad
856                 pushfd
857                 mov cx,4
858                 ror eax,12
859                 jmp writehex_common
860 writehex8:      pushad
861                 pushfd
862                 mov cx,8
863                 ror eax,28
864 writehex_common:
865 .loop:          push cx
866                 push eax
867                 and al,0Fh
868                 cmp al,10
869                 jb .isdec
870                 add al,'a'-'0'-10
871 .isdec:         add al,'0'
872                 mov ah,0Eh
873                 mov bx,7
874                 int 10h
875                 pop eax
876                 rol eax,4
877                 pop cx
878                 loop .loop
879                 popfd
880                 popad
881                 ret
882 %endif
883
884                 section .data
885                 alignb 2
886 Int13Funcs      dw Reset                ; 00h - RESET
887                 dw GetStatus            ; 01h - GET STATUS
888                 dw Read                 ; 02h - READ
889                 dw Write                ; 03h - WRITE
890                 dw Verify               ; 04h - VERIFY
891                 dw Invalid              ; 05h - FORMAT TRACK
892                 dw Invalid              ; 06h - FORMAT TRACK AND SET BAD FLAGS
893                 dw Invalid              ; 07h - FORMAT DRIVE AT TRACK
894                 dw GetParms             ; 08h - GET PARAMETERS
895                 dw InitWithParms        ; 09h - INITIALIZE CONTROLLER WITH
896                                         ;       DRIVE PARAMETERS
897                 dw Invalid              ; 0Ah
898                 dw Invalid              ; 0Bh
899                 dw Seek                 ; 0Ch - SEEK TO CYLINDER
900                 dw Reset                ; 0Dh - RESET HARD DISKS
901                 dw Invalid              ; 0Eh
902                 dw Invalid              ; 0Fh
903                 dw CheckIfReady         ; 10h - CHECK IF READY
904                 dw Recalibrate          ; 11h - RECALIBRATE
905                 dw Invalid              ; 12h
906                 dw Invalid              ; 13h
907                 dw Invalid              ; 14h
908                 dw GetDriveType         ; 15h - GET DRIVE TYPE
909                 dw DetectChange         ; 16h - DETECT DRIVE CHANGE
910 %if EDD
911                 dw Invalid              ; 17h
912                 dw Invalid              ; 18h
913                 dw Invalid              ; 19h
914                 dw Invalid              ; 1Ah
915                 dw Invalid              ; 1Bh
916                 dw Invalid              ; 1Ch
917                 dw Invalid              ; 1Dh
918                 dw Invalid              ; 1Eh
919                 dw Invalid              ; 1Fh
920                 dw Invalid              ; 20h
921                 dw ReadMult             ; 21h - READ MULTIPLE
922                 dw WriteMult            ; 22h - WRITE MULTIPLE
923                 dw SetMode              ; 23h - SET CONTROLLER FEATURES
924                 dw SetMode              ; 24h - SET MULTIPLE MODE
925                 dw Invalid              ; 25h - IDENTIFY DRIVE
926                 dw Invalid              ; 26h
927                 dw Invalid              ; 27h
928                 dw Invalid              ; 28h
929                 dw Invalid              ; 29h
930                 dw Invalid              ; 2Ah
931                 dw Invalid              ; 2Bh
932                 dw Invalid              ; 2Ch
933                 dw Invalid              ; 2Dh
934                 dw Invalid              ; 2Eh
935                 dw Invalid              ; 2Fh
936                 dw Invalid              ; 30h
937                 dw Invalid              ; 31h
938                 dw Invalid              ; 32h
939                 dw Invalid              ; 33h
940                 dw Invalid              ; 34h
941                 dw Invalid              ; 35h
942                 dw Invalid              ; 36h
943                 dw Invalid              ; 37h
944                 dw Invalid              ; 38h
945                 dw Invalid              ; 39h
946                 dw Invalid              ; 3Ah
947                 dw Invalid              ; 3Bh
948                 dw Invalid              ; 3Ch
949                 dw Invalid              ; 3Dh
950                 dw Invalid              ; 3Eh
951                 dw Invalid              ; 3Fh
952                 dw Invalid              ; 40h
953                 dw EDDPresence          ; 41h - EDD PRESENCE DETECT
954                 dw EDDRead              ; 42h - EDD READ
955                 dw EDDWrite             ; 43h - EDD WRITE
956                 dw EDDVerify            ; 44h - EDD VERIFY
957                 dw EDDLock              ; 45h - EDD LOCK/UNLOCK MEDIA
958                 dw EDDEject             ; 46h - EDD EJECT
959                 dw EDDSeek              ; 47h - EDD SEEK
960                 dw EDDGetParms          ; 48h - EDD GET PARAMETERS
961                 dw EDDDetectChange      ; 49h - EDD MEDIA CHANGE STATUS
962 %endif
963
964 Int13FuncsEnd   equ $
965 Int13FuncsCnt   equ (Int13FuncsEnd-Int13Funcs) >> 1
966
967
968                 alignb 8, db 0
969 Shaker          dw ShakerEnd-$-1        ; Descriptor table limit
970                 dd 0                    ; Pointer to self
971                 dw 0
972
973 Shaker_RMDS:    dd 0x0000ffff           ; 64K data segment
974                 dd 0x00009300
975
976 Shaker_DS:      dd 0x0000ffff           ; 4GB data segment
977                 dd 0x008f9300
978
979 ShakerEnd       equ $
980
981                 alignb 8, db 0
982
983 Mover           dd 0, 0, 0, 0           ; Must be zero
984                 dw 0ffffh               ; 64 K segment size
985 Mover_src1:     db 0, 0, 0              ; Low 24 bits of source addy
986                 db 93h                  ; Access rights
987                 db 00h                  ; Extended access rights
988 Mover_src2:     db 0                    ; High 8 bits of source addy
989                 dw 0ffffh               ; 64 K segment size
990 Mover_dst1:     db 0, 0, 0              ; Low 24 bits of target addy
991                 db 93h                  ; Access rights
992                 db 00h                  ; Extended access rights
993 Mover_dst2:     db 0                    ; High 8 bits of source addy
994 Mover_dummy2:   dd 0, 0, 0, 0           ; More space for the BIOS
995
996                 alignb 4, db 0
997 MemDisk_Info    equ $                   ; Pointed to by installation check
998 MDI_Bytes       dw MDI_Len              ; Total bytes in MDI structure
999 MDI_Version     db VERSION_MINOR, VERSION_MAJOR ; MEMDISK version
1000
1001 PatchArea       equ $                   ; This gets filled in by the installer
1002
1003 DiskBuf         dd 0                    ; Linear address of high memory disk
1004 DiskSize        dd 0                    ; Size of disk in blocks
1005 CommandLine     dw 0, 0                 ; Far pointer to saved command line
1006
1007 OldInt13        dd 0                    ; INT 13h in chain
1008 OldInt15        dd 0                    ; INT 15h in chain
1009
1010 OldDosMem       dw 0                    ; Old position of DOS mem end
1011 BootLoaderID    db 0                    ; Boot loader ID from header
1012                 db 0                    ; pad
1013
1014 DPT_ptr         dw 0                    ; If nonzero, pointer to DPT
1015                                         ; Original DPT pointer follows
1016
1017 MDI_Len         equ $-MemDisk_Info
1018
1019 ; ---- MDI structure ends here ---
1020 MemInt1588      dw 0                    ; 1MB-65MB memory amount (1K)
1021
1022 Cylinders       dw 0                    ; Cylinder count
1023 Heads           dw 0                    ; Head count
1024 Sectors         dd 0                    ; Sector count (zero-extended)
1025
1026 Mem1MB          dd 0                    ; 1MB-16MB memory amount (1K)
1027 Mem16MB         dd 0                    ; 16MB-4G memory amount (64K)
1028
1029 DriveNo         db 0                    ; Our drive number
1030 DriveType       db 0                    ; Our drive type (floppies)
1031 DriveCnt        db 0                    ; Drive count (from the BIOS)
1032
1033 ConfigFlags     db 0                    ; Bit 0 - readonly
1034
1035 MyStack         dw 0                    ; Offset of stack
1036 StatusPtr       dw 0                    ; Where to save status (zeroseg ptr)
1037
1038 DPT             times 16 db 0           ; BIOS parameter table pointer (floppies)
1039 OldInt1E        dd 0                    ; Previous INT 1E pointer (DPT)
1040
1041 %if EDD
1042 EDD_DPT:
1043 .length         dw 30
1044 .info           dw 0029h
1045                 ; Bit 0 - DMA boundaries handled transparently
1046                 ; Bit 3 - Device supports write verify
1047                 ; Bit 5 - Media is lockable
1048 .cylinders      dd 0                    ; Filled in by installer
1049 .heads          dd 0                    ; Filled in by installer
1050 .sectors        dd 0                    ; Filled in by installer
1051 .totalsize      dd 0, 0                 ; Filled in by installer
1052 .bytespersec    dw SECTORSIZE
1053 .eddtable       dw -1, -1               ; Invalid DPTE pointer
1054 .dpikey         dw 0BEDDh               ; Device Path Info magic
1055 .dpilen         db 2ch                  ; DPI len
1056 .res1           db 0                    ; Reserved
1057 .res2           dw 0                    ; Reserved
1058 .bustype        dd 'MEM '               ; Host bus type (4 bytes, space padded)
1059 .inttype        dd 'MEMORY  '           ; Interface type (8 bytes, spc. padded)
1060 .intpath        dd 0, 0                 ; Interface path
1061 .devpath        dd 0, 0, 0, 0           ; Device path
1062 .res3           db 0                    ; Reserved
1063 .chksum         db 0                    ; DPI checksum
1064
1065 %endif
1066
1067                 ; End patch area
1068                 alignb 4, db 0
1069 Stack           dd 0                    ; Saved SS:ESP on invocation
1070                 dw 0
1071 SavedAX         dw 0                    ; AX saved on invocation
1072 Recursive       dw 0                    ; Recursion counter
1073
1074                 alignb 4, db 0          ; We *MUST* end on a dword boundary
1075
1076 E820Table       equ $                   ; The installer loads the E820 table here
1077 TotalSize       equ $                   ; End pointer