1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
; -*- fundamental -*- (asm-mode sucks)
; $Id$
; ****************************************************************************
;
; memdisk.asm
;
; A program to emulate an INT 13h disk BIOS from a "disk" in extended
; memory.
;
; Copyright (C) 2001 H. Peter Anvin
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
; USA; either version 2 of the License, or (at your option) any later
; version; incorporated herein by reference.
;
; ****************************************************************************
org 0h
%define SECTORSIZE_LG2 9 ; log2(sector size)
%define SECTORSIZE (1 << SECTORSIZE_LG2)
MyStack equ 1024
; Parameter registers definition; this is the definition
; of the stack frame.
%define P_DS word [bp+34]
%define P_ES word [bp+32]
%define P_EAX dword [bp+28]
%define P_AX word [bp+28]
%define P_AL byte [bp+28]
%define P_AH byte [bp+29]
%define P_ECX dword [bp+24]
%define P_CX word [bp+24]
%define P_CL byte [bp+24]
%define P_CH byte [bp+25]
%define P_EDX dword [bp+20]
%define P_DX word [bp+20]
%define P_DL byte [bp+20]
%define P_DH byte [bp+21]
%define P_EBX dword [bp+16]
%define P_BX word [bp+16]
%define P_BL byte [bp+16]
%define P_BH byte [bp+17]
%define P_EBP dword [bp+8]
%define P_BP word [bp+8]
%define P_ESI dword [bp+4]
%define P_SI word [bp+4]
%define P_EDI dword [bp]
%define P_DI word [bp]
section .text
; These pointers are used by the installer and
; must be first in the binary
Pointers: dw Int13Start
dw Int15Start
dw PatchArea
dw TotalSize
Int13Start:
; See if DL points to our class of device (FD, HD)
push dx
xor dl,[cs:DriveNo]
pop dx
js .nomatch ; If SF=0, we have a match here
cmp dl,[cs:DriveNo]
je .our_drive
jb .nomatch ; Drive < Our drive
dec dl ; Drive > Our drive, adjust drive #
.nomatch:
jmp far [OldInt13]
.our_drive:
mov [cs:Stack],esp
mov [cs:SavedAX],ax
mov ax,ss
mov [cs:Stack+4],ax
mov ax,cs
mov ss,ax
mov sp,MyStack
push ds
push es
mov ds,ax
mov es,ax
mov ax,[SavedAX]
pushad
mov bp,sp
cmp ah,Int13FuncsMax
jae Invalid
xor al,al ; AL = 0 is standard entry condition
mov di,ax
shr di,7
call [Int13Funcs+di]
Done: ; Standard routine for return
mov [LastStatus],ah
mov P_AX,ax
cmp ah,1
DoneWeird:
setnb al ; AL <- (AH > 0) ? 1 : 0 (CF)
lds ebx,[Stack] ; DS:EBX <- Old stack pointer
mov [ebx+4],al ; Low byte of old FLAGS -> arithmetric flags
popad
pop es
pop ds
lss esp,[cs:Stack]
iret
Reset:
; Reset affects multiple drives, so we need to pass it on
pop ax ; Drop return address
mov [LastStatus], byte 0
popad
pop es
pop ds
lss esp,[cs:Stack]
and dl,80h ; Clear all but the type bit
jmp far [OldInt13]
Invalid:
mov ax,0100h ; Unsupported function
ret
GetDriveType:
pop ax ; Drop return address
mov ah,[DriveNo]
shr ah,7
or ah,02h ; CF = 0
mov P_AH,ah
mov [LastStatus],byte 0 ; Success, but AH returns a value
jmp short DoneWeird
GetStatus:
mov ah,[LastStatus] ; Copy last status
ret
CheckIfReady: ; These are always-successful noop functions
Recalibrate:
InitWithParms:
DetectChange:
success:
xor ax,ax ; Always successful
ret
Read:
call setup_regs
do_copy:
call bcopy
movzx ax,P_AL ; AH = 0, AL = transfer count
ret
Write:
call setup_regs
xchg esi,edi
jmp short do_copy
; These verify one sector only
Seek:
mov P_AL,1
; Verify integrity; just bounds-check
Verify:
call setup_regs ; Returns error if appropriate
jmp short success
GetParms:
; We need to get the "number of drives" from the BIOS
mov dl,P_DL
inc dl ; The drive whose number we're stealing
mov ah,08h
int 13h
inc dl ; Add ourselves to the count
mov P_DL,dl ; Drive count
mov P_DI,di ; Steal the diskette parameter table if applicable
mov ax,es
mov P_ES,ax
mov bl,[DriveType]
mov P_BL,bl
mov ax,[Cylinders]
dec ax ; We report the highest #, not the count
or ah,[Sectors]
xchg al,ah
mov P_CX,ax
mov al,[Heads]
dec al
mov P_DH,al
xor ax,ax
ret
; Convert a CHS address in CX/DH into an LBA in EAX
chstolba:
xor ebx,ebx
mov bl,cl ; Sector number
and bl,3Fh
dec bx
mov si,dx
mov ax,[Heads]
shr cl,6
xchg cl,ch ; Now CX <- cylinder number
mul cx ; DX:AX <- AX*CX
shr si,8 ; SI <- head number
add ax,si
adc dx,byte 0
shl edx,16
or eax,edx
mul dword [Sectors]
add eax,ebx
ret
; Set up registers as for a "Read", and compares against disk size
setup_regs:
call chstolba
movzx edi,P_BX ; Get linear address of target buffer
movzx ecx,P_ES
shr ecx,4
add edi,ecx
movzx ecx,P_AL
lea ebx,[eax+ecx]
mov esi,eax
shr esi,SECTORSIZE_LG2
add esi,[DiskBuf]
cmp ebx,[DiskSize]
jae .overrun
shr ecx,SECTORSIZE_LG2-1
ret
.overrun: pop ax ; Drop return address
mov ax,0400h ; Sector not found
ret
int15_e820:
cmp edx,534D4150h
jne near oldint15
cmp ecx,20 ; Need 20 bytes
jb err86
push edx ; "SMAP"
push esi
push edi
and ebx,ebx
jne .renew
mov ebx,[E820Table]
.renew: mov esi,ebx
xor edi,edi
mov di,cs
shr di,4
add edi,E820Buf
mov ecx,24/2
call bcopy
add ebx, byte 12
pop edi
pop esi
mov eax,[cs:E820Buf]
mov [es:di],eax
mov eax,[cs:E820Buf+4]
mov [es:di+4],eax
mov eax,[cs:E820Buf+12]
mov [es:di+8],eax
mov eax,[cs:E820Buf+16]
mov [es:di+12],eax
mov eax,[cs:E820Buf+8]
mov [es:di+16],eax
cmp dword [cs:E820Buf+20], byte -1
jne .notdone
xor ebx,ebx ; Done with table
.notdone:
pop eax ; "SMAP"
mov ecx,20 ; Bytes loaded
int15_success:
mov byte [bp+12], 02h ; Clear CF
pop bp
iret
err86:
mov byte [bp+12], 03h ; Set CF
mov ah,86h
pop bp
iret
Int15Start:
push bp
mov bp,sp
cmp ax,0E820h
je near int15_e820
cmp ax,0E801h
je int15_e801
cmp ax,0E881h
je int15_e881
cmp ah,88h
je int15_88
oldint15: pop bp
jmp far [cs:OldInt15]
int15_e801:
mov ax,[cs:Mem1MB]
mov cx,ax
mov bx,[cs:Mem16MB]
mov dx,ax
jmp short int15_success
int15_e881:
mov eax,[cs:Mem1MB]
mov ecx,eax
mov ebx,[cs:Mem16MB]
mov edx,eax
jmp short int15_success
int15_88:
mov ax,[cs:MemInt1588]
jmp short int15_success
;
; Routine to copy in/out of high memory
; esi = linear source address
; edi = linear target address
; ecx = 16-bit word count
;
; Assumes cs = ds = es
;
bcopy:
push eax
push ebx
push edx
push ebp
push esi
push edi
.copy_loop:
push ecx
cmp ecx,8000h
jna .safe_size
mov ecx,8000h
.safe_size:
push ecx
mov eax, esi
mov [Mover_src1], si
shr eax, 16
mov [Mover_src1+2], al
mov [Mover_src2], ah
mov eax, edi
mov [Mover_dst1], di
shr eax, 16
mov [Mover_dst1+2], al
mov [Mover_dst2], ah
mov si,Mover
mov ah, 87h
int 15h
pop eax
pop ecx
pop edi
pop esi
jc .error
lea esi,[esi+2*eax]
lea edi,[edi+2*eax]
sub ecx, eax
jnz .copy_loop
; CF = 0
.error:
pop ebp
pop edx
pop ebx
pop eax
ret
section .data
alignb 8
Int13Funcs dw Reset ; 00h - RESET
dw GetStatus ; 01h - GET STATUS
dw Read ; 02h - READ
dw Write ; 03h - WRITE
dw Verify ; 04h - VERIFY
dw Invalid ; 05h - FORMAT TRACK
dw Invalid ; 06h - FORMAT TRACK AND SET BAD FLAGS
dw Invalid ; 07h - FORMAT DRIVE AT TRACK
dw GetParms ; 08h - GET PARAMETERS
dw InitWithParms ; 09h - INITIALIZE CONTROLLER WITH DRIVE PARAMETERS
dw Invalid ; 0Ah
dw Invalid ; 0Bh
dw Seek ; 0Ch - SEEK TO CYLINDER
dw Reset ; 0Dh - RESET HARD DISKS
dw Invalid ; 0Eh
dw Invalid ; 0Fh
dw CheckIfReady ; 10h - CHECK IF READY
dw Recalibrate ; 11h - RECALIBRATE
dw Invalid ; 12h
dw Invalid ; 13h
dw Invalid ; 14h
dw GetDriveType ; 15h - GET DRIVE TYPE
dw DetectChange ; 16h - DETECT DRIVE CHANGE
Int13FuncsEnd equ $
Int13FuncsMax equ (Int13FuncsEnd-Int13Funcs) >> 1
alignb 8, db 0
Mover dd 0, 0, 0, 0 ; Must be zero
dw 0ffffh ; 64 K segment size
Mover_src1: db 0, 0, 0 ; Low 24 bits of source addy
db 93h ; Access rights
db 00h ; Extended access rights
Mover_src2: db 0 ; High 8 bits of source addy
Mover_dst1: db 0, 0, 0 ; Low 24 bits of target addy
db 93h ; Access rights
db 00h ; Extended access rights
Mover_dst2: db 0 ; High 8 bits of source addy
section .bss
alignb 4
PatchArea equ $ ; This gets filled in by the installer
DriveNo resb 1 ; Our drive number
DriveType resb 1 ; Our drive type (floppies)
LastStatus resb 1 ; Last return status
resb 1 ; pad
Cylinders resw 1 ; Cylinder count
Heads resw 1 ; Head count
Sectors resd 1 ; Sector count (zero-extended)
DiskSize resd 1 ; Size of disk in blocks
DiskBuf resd 1 ; Linear address of high memory disk
E820Table resd 1 ; E820 table in high memory
Mem1MB resd 1 ; 1MB-16MB memory amount (1K)
Mem16MB resd 1 ; 16MB-4G memory amount (64K)
MemInt1588 resd 1 ; 1MB-65MB memory amount (1K)
OldInt13 resd 1 ; INT 13h in chain
OldInt15 resd 1 ; INT 15h in chain
; End patch area
Stack resd 2 ; Saved SS:ESP on invocation
E820Buf resd 6 ; E820 fetch buffer
SavedAX resw 1 ; AX saved on invocation
TotalSize equ $ ; End pointer
|