memdisk varieties: Allow for multiple memdisk installable hooks
[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-2008 H. Peter Anvin - All Rights Reserved
10 ;
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.
16 ;
17 ; ****************************************************************************
18
19 %ifndef DEPEND
20 %include        "../version.gen"
21 %endif
22
23 ; %define DEBUG_TRACERS                 ; Uncomment to get debugging tracers
24
25 %ifdef WITH_EDD
26 %define EDD 1
27 %else
28 %define EDD 0
29 %endif
30
31 %ifdef DEBUG_TRACERS
32
33 %macro TRACER   1
34         call debug_tracer
35         db %1
36 %endmacro
37
38 %else   ; DEBUG_TRACERS
39
40 %macro  TRACER  1
41 %endmacro
42
43 %endif  ; DEBUG_TRACERS
44
45 %define CONFIG_READONLY 0x01
46 %define CONFIG_RAW      0x02
47 %define CONFIG_SAFEINT  0x04
48 %define CONFIG_BIGRAW   0x08            ; MUST be 8!
49
50                 org 0h
51
52 %define SECTORSIZE_LG2  9               ; log2(sector size)
53 %define SECTORSIZE      (1 << SECTORSIZE_LG2)
54
55                 ; Parameter registers definition; this is the definition
56                 ; of the stack frame.
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]
86
87                 section .text
88                 ; These pointers are used by the installer and
89                 ; must be first in the binary
90 Pointers:       dw Int13Start
91                 dw Int15Start
92                 dw PatchArea
93                 dw TotalSize
94                 dw IretPtr
95
96 IretPtr         equ Int13Start.iret
97 Int13Start:
98                 ; Swap stack
99                 mov [cs:Stack],esp
100                 mov [cs:SavedAX],ax
101                 mov ax,ss
102                 mov [cs:Stack+4],ax
103                 mov ax,cs
104                 mov ss,ax
105                 mov sp,[cs:MyStack]
106
107                 ; See if DL points to our class of device (FD, HD)
108                 push dx
109                 push dx
110                 xor dl,[cs:DriveNo]
111                 pop dx
112                 js .nomatch             ; If SF=0, we have a class match here
113                 jz .our_drive           ; If ZF=1, we have an exact match
114                 cmp dl,[cs:DriveNo]
115                 jb .nomatch             ; Drive < Our drive
116                 dec dl                  ; Drive > Our drive, adjust drive #
117 .nomatch:
118                 mov ax,[cs:SavedAX]
119                 pushf
120                 call far [cs:OldInt13]
121                 pushf
122                 push bp
123                 mov bp,sp
124                 cmp byte [cs:SavedAX+1],08h
125                 je .norestoredl
126                 cmp byte [cs:SavedAX+1],15h
127                 jne .restoredl
128                 test byte [bp+4],80h    ; Hard disk?
129                 jnz .norestoredl
130 .restoredl:
131                 mov dl,[bp+4]
132 .norestoredl:
133                 push ax
134                 push ebx
135                 push ds
136                 mov ax,[bp+2]           ; Flags
137                 lds ebx,[cs:Stack]
138                 mov [bx+4],al           ; Arithmetric flags
139                 pop ds
140                 pop ebx
141                 pop ax
142                 pop bp
143                 lss esp,[cs:Stack]
144 .iret:          iret
145
146 .our_drive:
147                 ; Set up standard entry frame
148                 push ds
149                 push es
150                 mov ds,ax
151                 mov es,ax
152                 mov ax,[SavedAX]
153                 pushad
154                 mov bp,sp               ; Point BP to the entry stack frame
155                 TRACER 'F'
156                 ; Note: AH == P_AH here
157                 cmp ah,[Int13MaxFunc]
158                 ja Invalid_jump
159                 xor al,al               ; AL = 0 is standard entry condition
160                 mov di,ax
161                 shr di,7                ; Convert AH to an offset in DI
162                 call [Int13Funcs+di]
163
164 Done:           ; Standard routine for return
165                 mov P_AX,ax
166 DoneWeird:
167                 TRACER 'D'
168                 xor bx,bx
169                 mov es,bx
170                 mov bx,[StatusPtr]
171                 mov [es:bx],ah          ; Save status
172                 and ah,ah
173
174                 lds ebx,[Stack]
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
179                 popad
180                 pop es
181                 pop ds
182                 lss esp,[cs:Stack]
183                 iret
184
185 Reset:
186                 ; Reset affects multiple drives, so we need to pass it on
187                 TRACER 'R'
188                 xor ax,ax               ; Bottom of memory
189                 mov es,ax
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
199                 jz success
200                 jmp .pass_on            ; ... otherwise pass it to the BIOS
201 .hard_disk:
202                 ; ... same thing for hard disks, sigh ...
203                 cmp byte [es:0x475],1   ; BIOS variable for number of hard disks
204                 jbe success
205
206 .pass_on:
207                 pop ax                  ; Drop return address
208                 popad                   ; Restore all registers
209                 pop es
210                 pop ds
211                 lss esp,[cs:Stack]      ; Restore the stack
212                 and dl,80h              ; Clear all but the type bit
213                 jmp far [cs:OldInt13]
214
215
216 Invalid:
217                 pop dx                  ; Drop return address
218 Invalid_jump:
219                 TRACER 'I'
220                 mov ah,01h              ; Unsupported function
221                 jmp short Done
222
223 GetDriveType:
224                 test byte [DriveNo],80h
225                 mov bl,02h              ; Type 02h = floppy with changeline
226                 jz .floppy
227                 ; Hard disks only...
228                 inc bx                  ; Type = 03h
229                 mov dx,[DiskSize]       ; Return the disk size in sectors
230                 mov P_DX,dx
231                 mov cx,[DiskSize+2]
232                 mov P_CX,cx
233 .floppy:
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
238
239 GetStatus:
240                 xor ax,ax
241                 mov es,ax
242                 mov bx,[StatusPtr]
243                 mov ah,[bx]             ; Copy last status
244                 ret
245
246 ReadMult:
247                 TRACER 'm'
248 Read:
249                 TRACER 'R'
250                 call setup_regs
251 do_copy:
252                 TRACER '<'
253                 call bcopy
254                 TRACER '>'
255                 movzx ax,P_AL           ; AH = 0, AL = transfer count
256                 ret
257
258 WriteMult:
259                 TRACER 'M'
260 Write:
261                 TRACER 'W'
262                 test byte [ConfigFlags],CONFIG_READONLY
263                 jnz .readonly
264                 call setup_regs
265                 xchg esi,edi            ; Opposite direction of a Read!
266                 jmp short do_copy
267 .readonly:      mov ah,03h              ; Write protected medium
268                 ret
269
270                 ; Verify integrity; just bounds-check
271 Seek:
272 Verify:
273                 call setup_regs         ; Returns error if appropriate
274                 ; And fall through to success
275
276 CheckIfReady:                           ; These are always-successful noop functions
277 Recalibrate:
278 InitWithParms:
279 DetectChange:
280 EDDDetectChange:
281 EDDLock:
282 SetMode:
283 success:
284                 xor ax,ax               ; Always successful
285                 ret
286
287 GetParms:
288                 TRACER 'G'
289                 mov dl,[DriveCnt]       ; Cached data
290                 mov P_DL,dl
291                 test byte [DriveNo],80h
292                 jnz .hd
293                 mov P_DI,DPT
294                 mov P_ES,cs
295                 mov bl,[DriveType]
296                 mov P_BL,bl
297 .hd:
298                 mov ax,[Cylinders]
299                 dec ax                  ; We report the highest #, not the count
300                 xchg al,ah
301                 shl al,6
302                 or al,[Sectors]
303                 mov P_CX,ax
304                 mov ax,[Heads]
305                 dec ax
306                 mov P_DH,al
307
308                 ;
309                 ; Is this MEMDISK installation check?
310                 ;
311                 cmp P_HAX,'ME'
312                 jne .notic
313                 cmp P_HCX,'MD'
314                 jne .notic
315                 cmp P_HDX,'IS'
316                 jne .notic
317                 cmp P_HBX,'K?'
318                 jne .notic
319
320                 ; MEMDISK installation check...
321                 mov P_HAX,'!M'
322                 mov P_HCX,'EM'
323                 mov P_HDX,'DI'
324                 mov P_HBX,'SK'
325                 mov P_ES,cs
326                 mov P_DI,MemDisk_Info
327
328 .notic:
329                 xor ax,ax
330                 ret
331 ;
332 ; EDD functions -- only if enabled
333 ;
334 %if EDD
335 EDDPresence:
336                 TRACER 'E'
337                 TRACER 'c'
338
339                 cmp P_BX,55AAh
340                 jne Invalid
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
346                 xor ax,ax               ; Success
347                 jmp DoneWeird           ; Success, but AH != 0, sigh...
348
349 EDDRead:
350                 TRACER 'E'
351                 TRACER 'r'
352
353                 call edd_setup_regs
354                 call bcopy
355                 xor ax,ax
356                 ret
357
358 EDDWrite:
359                 TRACER 'E'
360                 TRACER 'w'
361
362                 call edd_setup_regs
363                 xchg esi,edi
364                 call bcopy
365                 xor ax,ax
366                 ret
367
368 EDDVerify:
369 EDDSeek:
370                 call edd_setup_regs     ; Just bounds checking
371                 xor ax,ax
372                 ret
373
374 EDDGetParms:
375                 TRACER 'E'
376                 TRACER 'p'
377
378                 mov es,P_DS
379                 mov di,P_SI
380                 mov si,EDD_DPT
381
382                 lodsw                   ; Length of our DPT
383                 mov cx,[es:di]
384                 cmp cx,26               ; Minimum size
385                 jb .overrun
386
387                 cmp cx,ax
388                 jb .oksize
389                 mov cx,ax
390
391 .oksize:
392                 mov ax,cx
393                 stosw
394                 dec cx
395                 dec cx
396                 rep movsb
397
398                 xor ax,ax
399                 ret
400
401 .overrun:
402                 mov ax,0100h
403                 ret
404 %endif ; EDD
405
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.
409 setup_regs:
410
411                 ; Convert a CHS address in P_CX/P_DH into an LBA in eax
412                 ; CH = cyl[7:0]
413                 ; CL[0:5] = sector (1-based)  CL[7:6] = cyl[9:8]
414                 ; DH = head
415                 movzx ecx,P_CX
416                 movzx ebx,cl            ; Sector number
417                 and bl,3Fh
418                 dec ebx                 ; Sector number is 1-based
419                 cmp bx,[Sectors]
420                 jae .overrun
421                 movzx edi,P_DH          ; Head number
422                 movzx eax,word [Heads]
423                 cmp di,ax
424                 jae .overrun
425                 shr cl,6
426                 xchg cl,ch              ; Now (E)CX <- cylinder number
427                 mul ecx                 ; eax <- Heads*cyl# (edx <- 0)
428                 add eax,edi
429                 mul dword [Sectors]
430                 add eax,ebx
431                 ; Now eax = LBA, edx = 0
432
433                 ;
434                 ; setup_regs continues...
435                 ;
436                 ; Note: edi[31:16] and ecx[31:16] = 0 already
437                 mov di,P_BX             ; Get linear address of target buffer
438                 mov cx,P_ES
439                 shl ecx,4
440                 add edi,ecx             ; EDI = address to fetch to
441                 movzx ecx,P_AL          ; Sector count
442                 mov esi,eax
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
447                 ja .overrun
448                 shl ecx,SECTORSIZE_LG2-2 ; Convert count to dwords
449                 ret
450
451 .overrun:       pop ax                  ; Drop setup_regs return address
452                 mov ax,0200h            ; Missing address mark
453                 ret                     ; Return to Done
454
455                 ; Set up registers as for an EDD Read, and compares against disk size.
456 %if EDD
457 edd_setup_regs:
458                 push es
459                 mov si,P_SI             ; DS:SI -> DAPA
460                 mov es,P_DS
461
462                 mov dx,[es:si]
463                 cmp dx,16
464                 jb .baddapa
465
466                 cmp dword [es:si+4],-1
467                 je .linear_address
468
469                 movzx ebx,word [es:si+4]        ; Offset
470                 movzx edi,word [es:si+6]        ; Segment
471                 shl edi,4
472                 add ebx,edi
473                 jmp .got_address
474
475 .linear_address:
476                 cmp dx,24               ; Must be large enough to hold
477                                         ; linear address
478                 jb .baddapa
479
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
483                 jne .error
484
485                 mov ebx,[es:si+16]
486
487 .got_address:
488                 cmp dword [es:si+12],0          ; LBA too large?
489                 jne .overrun
490
491                 movzx ecx, word [es:si+2]       ; Sectors to transfer
492                 mov esi,[es:si+8]               ; Starting sector
493                 mov eax,esi
494                 add eax,ecx
495                 jc .overrun
496                 cmp eax,[DiskSize]
497                 ja .overrun
498
499                 shl ecx,SECTORSIZE_LG2-2        ; Convert to dwords
500                 shl esi,SECTORSIZE_LG2          ; Convert to an offset
501                 add esi,[DiskBuf]
502                 mov edi,ebx
503                 pop es
504                 ret
505
506 .baddapa:
507                 mov ax,0100h            ; Invalid command
508                 pop es
509                 pop ax                  ; Drop setup_regs return address
510                 ret
511
512 .overrun:
513                 mov ax,0200h            ; "Address mark not found" =
514                                         ; LBA beyond end of disk
515 .error:
516                 and word [es:si+2],0    ; No sectors transferred
517                 pop es
518                 pop ax
519                 ret
520
521 EDDEject:
522                 mov ax,0B200h           ; Volume Not Removable
523                 ret
524
525 %endif ; EDD
526
527
528 ;
529 ; INT 15h intercept routines
530 ;
531 int15_e820:
532                 cmp edx,534D4150h       ; "SMAP"
533                 jne oldint15
534                 cmp ecx,20              ; Need 20 bytes
535                 jb err86
536                 push ds
537                 push cs
538                 pop ds
539                 push edx                ; "SMAP"
540                 and ebx,ebx
541                 jne .renew
542                 mov ebx,E820Table
543 .renew:
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
548                 mov [es:di+16],eax
549                 and cl,~3
550                 cmp ecx,24
551                 jb .no_extattr
552                 mov eax,[bx-4]          ; Extended attributes
553                 mov [es:di+20],eax
554                 mov ecx,24              ; Bytes loaded
555 .no_extattr:
556                 mov eax,[bx-16]         ; Start addr (low)
557                 mov edx,[bx-12]         ; Start addr (high)
558                 mov [es:di],eax
559                 mov [es:di+4],edx
560                 mov eax,[bx]            ; End addr (low)
561                 mov edx,[bx+4]          ; End addr (high)
562                 sub eax,[bx-16]         ; Derive the length
563                 sbb edx,[bx-12]
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?
567                 jne .notdone
568                 xor ebx,ebx             ; Done with table
569 .notdone:
570                 pop eax                 ; "SMAP"
571                 mov edx,eax             ; Some systems expect eax = edx = SMAP
572                 pop ds
573 int15_success:
574                 mov byte [bp+6], 02h    ; Clear CF
575                 pop bp
576                 iret
577
578 err86:
579                 mov byte [bp+6], 03h    ; Set CF
580                 mov ah,86h
581                 pop bp
582                 iret
583
584 Int15Start:
585                 push bp
586                 mov bp,sp
587                 cmp ax,0E820h
588                 je near int15_e820
589                 cmp ax,0E801h
590                 je int15_e801
591                 cmp ax,0E881h
592                 je int15_e881
593                 cmp ah,88h
594                 je int15_88
595 oldint15:       pop bp
596                 jmp far [cs:OldInt15]
597
598 int15_e801:
599                 mov ax,[cs:Mem1MB]
600                 mov cx,ax
601                 mov bx,[cs:Mem16MB]
602                 mov dx,bx
603                 jmp short int15_success
604
605 int15_e881:
606                 mov eax,[cs:Mem1MB]
607                 mov ecx,eax
608                 mov ebx,[cs:Mem16MB]
609                 mov edx,ebx
610                 jmp short int15_success
611
612 int15_88:
613                 mov ax,[cs:MemInt1588]
614                 jmp short int15_success
615
616 ;
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
621 ;
622 ; Assumes cs = ds = es
623 ;
624 bcopy:
625                 push eax
626                 push ebx
627                 push edx
628                 push ebp
629
630                 mov bx, real_int15_stub
631
632                 test byte [ConfigFlags], CONFIG_RAW|CONFIG_SAFEINT
633                 jz .anymode             ; Always do the real INT 15h
634
635                 smsw ax                 ; Unprivileged!
636                 test al,01h
637                 jnz .protmode           ; Protmode -> do real INT 15h
638
639 .realmode:
640                 ; Raw or Safeint mode, and we're in real mode...
641
642                 test byte [ConfigFlags], CONFIG_SAFEINT
643                 jnz .fakeint15
644
645 .raw:
646                 TRACER 'r'
647                 ; We're in real mode, do it outselves
648
649                 pushfd                  ; <A>
650                 push ds                 ; <B>
651                 push es                 ; <C>
652
653                 cli
654                 cld
655
656                 xor ebx,ebx
657                 mov bx,cs
658                 shl ebx,4
659                 lea edx,[Shaker+ebx]
660                 mov [Shaker+2],edx
661
662                 ; Test to see if A20 is enabled or not
663                 xor ax,ax
664                 mov ds,ax
665                 dec ax
666                 mov es,ax
667
668                 mov ax,[0]
669                 mov bx,ax
670                 xor bx,[es:10h]
671                 not ax
672                 mov [0],ax
673                 mov dx,ax
674                 xor dx,[es:10h]
675                 not ax
676                 mov [0],ax
677
678                 or dx,bx
679                 push dx                 ; <D> Save A20 status
680                 jnz .skip_a20e
681
682                 mov ax,2401h            ; Enable A20
683                 int 15h
684 .skip_a20e:
685                 mov dl,[ConfigFlags]
686                 and dx,CONFIG_BIGRAW
687                 add dx,8
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.
691
692                 lgdt [cs:Shaker]
693                 mov eax,cr0
694                 or al,01h
695                 mov cr0,eax
696
697                 mov bx,16               ; Large flat segment
698                 mov ds,bx
699                 mov es,bx
700
701                 a32 rep movsd
702
703                 ; DX has the appropriate value to put in
704                 ; the registers on return
705                 mov ds,dx
706                 mov es,dx
707
708                 and al,~01h
709                 mov cr0,eax
710
711                 pop dx                  ; <D> A20 status
712                 pop es                  ; <C>
713                 pop ds                  ; <B>
714
715                 and dx,dx
716                 jnz .skip_a20d
717                 mov ax,2400h            ; Disable A20
718                 int 15h
719 .skip_a20d:
720                 popfd                   ; <A>
721                 jmp .done
722
723 .fakeint15:
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
731
732 .protmode:
733                 TRACER 'p'
734 .anymode:
735
736 .copy_loop:
737                 push esi
738                 push edi
739                 push ecx
740                 cmp ecx,4000h
741                 jna .safe_size
742                 mov ecx,4000h
743 .safe_size:
744                 push ecx        ; Transfer size this cycle
745                 mov eax, esi
746                 mov [Mover_src1], si
747                 shr eax, 16
748                 mov [Mover_src1+2], al
749                 mov [Mover_src2], ah
750                 mov eax, edi
751                 mov [Mover_dst1], di
752                 shr eax, 16
753                 mov [Mover_dst1+2], al
754                 mov [Mover_dst2], ah
755                 mov si,Mover
756                 mov ah, 87h
757                 shl cx,1        ; Convert to 16-bit words
758                 call bx         ; INT 15h stub
759                 pop eax         ; Transfer size this cycle
760                 pop ecx
761                 pop edi
762                 pop esi
763                 jc .error
764                 lea esi,[esi+4*eax]
765                 lea edi,[edi+4*eax]
766                 sub ecx, eax
767                 jnz .copy_loop
768                 ; CF = 0
769 .error:
770 .done:
771                 pop ebp
772                 pop edx
773                 pop ebx
774                 pop eax
775                 ret
776
777 real_int15_stub:
778                 int 15h
779                 cli             ; Some BIOSes enable interrupts on INT 15h
780                 ret
781
782 fake_int15_stub:
783                 pushf
784                 call far [OldInt15]
785                 cli
786                 ret
787
788 %ifdef DEBUG_TRACERS
789 debug_tracer:   pushad
790                 pushfd
791                 mov bp,sp
792                 mov bx,[bp+9*4]
793                 mov al,[cs:bx]
794                 inc word [bp+9*4]
795                 mov ah,0Eh
796                 mov bx,7
797                 int 10h
798                 popfd
799                 popad
800                 ret
801 %endif
802
803                 section .data
804                 alignb 2
805 Int13Funcs      dw Reset                ; 00h - RESET
806                 dw GetStatus            ; 01h - GET STATUS
807                 dw Read                 ; 02h - READ
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
815                 dw Invalid              ; 0Ah
816                 dw Invalid              ; 0Bh
817                 dw Seek                 ; 0Ch - SEEK TO CYLINDER
818                 dw Reset                ; 0Dh - RESET HARD DISKS
819                 dw Invalid              ; 0Eh
820                 dw Invalid              ; 0Fh
821                 dw CheckIfReady         ; 10h - CHECK IF READY
822                 dw Recalibrate          ; 11h - RECALIBRATE
823                 dw Invalid              ; 12h
824                 dw Invalid              ; 13h
825                 dw Invalid              ; 14h
826                 dw GetDriveType         ; 15h - GET DRIVE TYPE
827                 dw DetectChange         ; 16h - DETECT DRIVE CHANGE
828 %if EDD
829                 dw Invalid              ; 17h
830                 dw Invalid              ; 18h
831                 dw Invalid              ; 19h
832                 dw Invalid              ; 1Ah
833                 dw Invalid              ; 1Bh
834                 dw Invalid              ; 1Ch
835                 dw Invalid              ; 1Dh
836                 dw Invalid              ; 1Eh
837                 dw Invalid              ; 1Fh
838                 dw Invalid              ; 20h
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
844                 dw Invalid              ; 26h
845                 dw Invalid              ; 27h
846                 dw Invalid              ; 28h
847                 dw Invalid              ; 29h
848                 dw Invalid              ; 2Ah
849                 dw Invalid              ; 2Bh
850                 dw Invalid              ; 2Ch
851                 dw Invalid              ; 2Dh
852                 dw Invalid              ; 2Eh
853                 dw Invalid              ; 2Fh
854                 dw Invalid              ; 30h
855                 dw Invalid              ; 31h
856                 dw Invalid              ; 32h
857                 dw Invalid              ; 33h
858                 dw Invalid              ; 34h
859                 dw Invalid              ; 35h
860                 dw Invalid              ; 36h
861                 dw Invalid              ; 37h
862                 dw Invalid              ; 38h
863                 dw Invalid              ; 39h
864                 dw Invalid              ; 3Ah
865                 dw Invalid              ; 3Bh
866                 dw Invalid              ; 3Ch
867                 dw Invalid              ; 3Dh
868                 dw Invalid              ; 3Eh
869                 dw Invalid              ; 3Fh
870                 dw Invalid              ; 40h
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
880 %endif
881
882 Int13FuncsEnd   equ $
883 Int13FuncsCnt   equ (Int13FuncsEnd-Int13Funcs) >> 1
884
885
886                 alignb 8, db 0
887 Shaker          dw ShakerEnd-$
888                 dd 0                    ; Pointer to self
889                 dw 0
890
891 Shaker_RMDS:    dd 0x0000ffff           ; 64K data segment
892                 dd 0x00009300
893
894 Shaker_DS:      dd 0x0000ffff           ; 4GB data segment
895                 dd 0x008f9300
896
897 ShakerEnd       equ $
898
899                 alignb 8, db 0
900
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
913
914                 alignb 4, db 0
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
918
919 PatchArea       equ $                   ; This gets filled in by the installer
920
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
924
925 OldInt13        dd 0                    ; INT 13h in chain
926 OldInt15        dd 0                    ; INT 15h in chain
927
928 OldDosMem       dw 0                    ; Old position of DOS mem end
929 BootLoaderID    db 0                    ; Boot loader ID from header
930                 db 0                    ; pad
931
932 DPT_ptr         dw 0                    ; If nonzero, pointer to DPT
933                                         ; Original DPT pointer follows
934
935 MDI_Len         equ $-MemDisk_Info
936
937 ; ---- MDI structure ends here ---
938 Int13MaxFunc    db Int13FuncsCnt-1      ; Max INT 13h function (to disable EDD)
939                 db 0                    ; pad
940
941                 dw 0                    ; pad
942 MemInt1588      dw 0                    ; 1MB-65MB memory amount (1K)
943
944 Cylinders       dw 0                    ; Cylinder count
945 Heads           dw 0                    ; Head count
946 Sectors         dd 0                    ; Sector count (zero-extended)
947
948 Mem1MB          dd 0                    ; 1MB-16MB memory amount (1K)
949 Mem16MB         dd 0                    ; 16MB-4G memory amount (64K)
950
951 DriveNo         db 0                    ; Our drive number
952 DriveType       db 0                    ; Our drive type (floppies)
953 DriveCnt        db 0                    ; Drive count (from the BIOS)
954
955 ConfigFlags     db 0                    ; Bit 0 - readonly
956
957 MyStack         dw 0                    ; Offset of stack
958 StatusPtr       dw 0                    ; Where to save status (zeroseg ptr)
959
960 DPT             times 16 db 0           ; BIOS parameter table pointer (floppies)
961 OldInt1E        dd 0                    ; Previous INT 1E pointer (DPT)
962
963 %if EDD
964 EDD_DPT:
965 .length         dw 30
966 .info           dw 0029h
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
976
977 %endif
978
979                 ; End patch area
980 Stack           dd 0                    ; Saved SS:ESP on invocation
981                 dw 0
982 SavedAX         dw 0                    ; AX saved on invocation
983
984                 alignb 4, db 0          ; We *MUST* end on a dword boundary
985
986 E820Table       equ $                   ; The installer loads the E820 table here
987 TotalSize       equ $                   ; End pointer