source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Handlers/Int13h.asm

Last change on this file was 618, checked in by krille_n_, 3 years ago

Changes:

  • Updated the BIOS makefile. Added the NO_ATAID_CORRECTION define back to the Tiny build as I've realized that the correction code should not be needed for builds without MODULE_EBIOS. Also added a new makefile target 'custom' to make it easier for people to make custom builds.
  • Fixed a bug where calling INT 13h/AH=15h for drives not handled by XUB (floppy drives for example) would return an error due to the fact that any non-zero return value in AH from the other BIOS would cause the CF to be set in Int13h_SetErrorCodeToIntpackInSSBPfromAH. The return path is now via Int13h_ReturnFromHandlerWithoutStoringErrorCode which means that no status/error code will be returned in the BDA but that should not be a problem as the other BIOS should do that anyway. This change also fixed another potential problem where return values in DL from the other BIOS were assumed to be drive numbers when MODULE_SERIAL_FLOPPY is included in the build.
  • Minor optimizations and fixes.
File size: 16.7 KB
Line 
1; Project name  :   XTIDE Universal BIOS
2; Description   :   Int 13h BIOS functions (Floppy and Hard disk).
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
7;
8; This program is free software; you can redistribute it and/or modify
9; it under the terms of the GNU General Public License as published by
10; the Free Software Foundation; either version 2 of the License, or
11; (at your option) any later version.
12;
13; This program is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16; GNU General Public License for more details.
17; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18;
19
20; Section containing code
21SECTION .text
22
23;--------------------------------------------------------------------
24; Int 13h software interrupt handler.
25; This handler changes stack to top of stolen conventional memory
26; and then calls the actual INT 13h handler (Int13h_DiskFunctionsHandler).
27;
28; Int13h_DiskFunctionsHandlerWithStackChange
29;   Parameters:
30;       AH:     Bios function
31;       DL:     Drive number
32;       Other:  Depends on function
33;   Returns:
34;       Depends on function
35;--------------------------------------------------------------------
36%ifdef RELOCATE_INT13H_STACK
37ALIGN JUMP_ALIGN
38Int13h_DiskFunctionsHandlerWithStackChange:
39    sti         ; Enable interrupts
40    ; TODO: Maybe we need to save Flags (DF) as well?
41    push    ds  ; Save DS:DI on the original stack
42    push    di
43    call    RamVars_GetSegmentToDS
44
45    ; Store entry registers to RAMVARS
46%ifdef USE_386
47    pop     DWORD [RAMVARS.dwStackChangeDSDI]
48%else
49    pop     WORD [RAMVARS.wStackChangeDI]   ; Pop DS:DI to the top of what
50    pop     WORD [RAMVARS.wStackChangeDS]   ; is to become the new stack
51%endif
52    mov     [RAMVARS.fpInt13hEntryStack], sp
53    mov     [RAMVARS.fpInt13hEntryStack+2], ss
54
55    ; Load new stack and restore DS and DI
56    mov     di, ds      ; We can save 2 bytes by using PUSH/POP but it's slower
57    mov     ss, di      ; No need to wrap with CLI/STI since this is for AT only (286+)
58    mov     sp, RAMVARS.rgbTopOfStack-4
59    pop     di          ; DI before stack change
60    pop     ds          ; DS before stack change
61
62    ; Call INT 13h
63    pushf
64    push    cs
65    call    Int13h_DiskFunctionsHandler
66
67    ; Restore stack (we must not corrupt FLAGS!)
68%ifdef USE_386
69    lss     sp, [ss:RAMVARS.fpInt13hEntryStack]
70%else
71    cli
72    mov     sp, [ss:RAMVARS.fpInt13hEntryStack]
73    mov     ss, [ss:RAMVARS.fpInt13hEntryStack+2]
74    sti
75%endif
76    retf    2           ; Skip FLAGS from stack
77%endif ; RELOCATE_INT13H_STACK
78
79
80;--------------------------------------------------------------------
81; Int 13h software interrupt handler.
82; Jumps to specific function defined in AH.
83;
84; Note to developers: Do not make recursive INT 13h calls!
85;
86; Int13h_DiskFunctionsHandler
87;   Parameters:
88;       AH:     Bios function
89;       DL:     Drive number
90;       Other:  Depends on function
91;   Returns:
92;       Depends on function
93;--------------------------------------------------------------------
94ALIGN JUMP_ALIGN
95Int13h_DiskFunctionsHandler:
96%ifndef RELOCATE_INT13H_STACK
97    sti                                 ; Enable interrupts
98%endif
99%ifdef CLD_NEEDED
100    cld                                 ; String instructions to increment pointers
101%endif
102    ePUSHA
103    push    ds
104    push    es
105%ifdef USE_386
106;   push    fs
107;   push    gs
108%endif
109    sub     sp, BYTE SIZE_OF_IDEPACK_WITHOUT_INTPACK
110    mov     bp, sp
111    call    RamVars_GetSegmentToDS
112
113%ifdef MODULE_DRIVEXLATE
114    call    DriveXlate_ToOrBack
115%endif
116    call    FindDPT_ForDriveNumberInDL  ; DS:DI points to our DPT, or NULL if not our drive
117    jc      SHORT .NotOurDrive          ; DPT not found so this is not one of our drives
118
119.OurFunction:
120    ; Jump to correct BIOS function
121    eMOVZX  bx, ah
122    eSHL_IM bx, 1
123    cmp     ah, 25h                     ; Possible EBIOS function?
124%ifndef MODULE_EBIOS
125    ja      SHORT UnsupportedFunction
126    jmp     [cs:bx+g_rgw13hFuncJump]    ; Jump to BIOS function
127
128%else ; If using MODULE_EBIOS
129    ja      SHORT .JumpToEbiosFunction
130    jmp     [cs:bx+g_rgw13hFuncJump]    ; Jump to BIOS function
131
132ALIGN JUMP_ALIGN
133.JumpToEbiosFunction:
134    test    BYTE [di+DPT.bFlagsLow], FLGL_DPT_LBA
135    jz      SHORT UnsupportedFunction   ; No eINT 13h for CHS drives
136    sub     bl, 41h<<1                  ; BX = Offset to eINT 13h jump table
137    jb      SHORT UnsupportedFunction
138    cmp     ah, 48h
139    ja      SHORT UnsupportedFunction
140    jmp     [cs:bx+g_rgwEbiosFunctionJumpTable]
141%endif  ; MODULE_EBIOS
142
143
144ALIGN JUMP_ALIGN
145.NotOurDrive:
146    test    ah, ah
147    jz      SHORT .OurFunction          ; We handle all function 0h requests (resets)
148
149%ifndef MODULE_SERIAL_FLOPPY
150; Without floppy support, we handle only hard disk traffic for function 08h.
151    test    dl, dl
152    jns     SHORT Int13h_DirectCallToAnotherBios
153%endif
154; With floppy support, we handle all traffic for function 08h, as we need to wrap both hard disk and floppy drive counts.
155    cmp     ah, GET_DRIVE_PARAMETERS
156    je      SHORT .OurFunction
157    ; Fall to Int13h_DirectCallToAnotherBios
158
159
160;--------------------------------------------------------------------
161; UnsupportedFunction
162; Int13h_DirectCallToAnotherBios
163;   Parameters:
164;       DL:     Translated drive number
165;       DS:     RAMVARS segment
166;       SS:BP:  Ptr to IDEPACK
167;       BX, DI: Corrupted on Int13h_DiskFunctionsHandler
168;       Other:  Function specific INT 13h parameters
169;   Returns:
170;       Depends on function
171;   Corrupts registers:
172;       Flags
173;--------------------------------------------------------------------
174ALIGN JUMP_ALIGN
175UnsupportedFunction:
176Int13h_DirectCallToAnotherBios:
177%ifdef MODULE_DRIVEXLATE
178    ; Disable drive number translations in case of recursive INT 13h calls
179    mov     [RAMVARS.xlateVars+XLATEVARS.bXlatedDrv], dl
180    push    WORD [RAMVARS.xlateVars+XLATEVARS.wFDandHDswap]
181    call    DriveXlate_Reset            ; No translation
182%endif
183
184    push    bp                          ; Store offset to IDEPACK (SS:SP now points it)
185
186    ; Simulate INT by pushing flags and return address
187    push    WORD [bp+IDEPACK.intpack+INTPACK.flags]
188%if 0
189    ; No standard INT 13h function uses FLAGS as parameters so no need to restore them
190    popf
191    pushf
192%endif
193    push    cs
194    ePUSH_T di, .ReturnFromAnotherBios  ; Can not corrupt flags
195
196    ; Push old INT 13h handler and restore registers
197%ifdef USE_386
198    push    DWORD [RAMVARS.fpOldI13h]
199%else
200    push    WORD [RAMVARS.fpOldI13h+2]
201    push    WORD [RAMVARS.fpOldI13h]
202%endif
203    mov     bx, [bp+IDEPACK.intpack+INTPACK.bx]
204    mov     di, [bp+IDEPACK.intpack+INTPACK.di]
205    mov     ds, [bp+IDEPACK.intpack+INTPACK.ds]
206    mov     bp, [bp+IDEPACK.intpack+INTPACK.bp]
207    retf                                ; "Return" to old INT 13h
208.ReturnFromAnotherBios:
209
210%if 0
211    ; We need to restore our pointer to IDEPACK but we cannot corrupt any register
212    push    ax                          ; Dummy WORD
213    cli
214    xchg    bp, sp
215    mov     [bp], sp                    ; Replace dummy WORD with returned BP
216    mov     sp, [bp+2]                  ; Load offset to IDEPACK
217    xchg    sp, bp
218    sti                                 ; We would have set IF anyway when exiting INT 13h
219    pop     WORD [bp+IDEPACK.intpack+INTPACK.bp]
220%endif
221    ; Actually we can corrupt BP since no standard INT 13h function uses it as return
222    ; register. Above code is kept here just in case if there is some non-standard function.
223    ; POP BP below also belongs to the above code.
224    pop     bp                          ; Clean IDEPACK offset from stack
225
226    ; Store remaining returned values to INTPACK
227%ifdef USE_386
228; We do not use GS or FS at the moment
229;   mov     [bp+IDEPACK.intpack+INTPACK.gs], gs
230;   mov     [bp+IDEPACK.intpack+INTPACK.fs], fs
231%endif
232    mov     [bp+IDEPACK.intpack+INTPACK.es], es
233    mov     [bp+IDEPACK.intpack+INTPACK.ds], ds
234    mov     [bp+IDEPACK.intpack+INTPACK.di], di
235    mov     [bp+IDEPACK.intpack+INTPACK.si], si
236    mov     [bp+IDEPACK.intpack+INTPACK.bx], bx
237%ifdef MODULE_DRIVEXLATE
238    mov     [bp+IDEPACK.intpack+INTPACK.dh], dh
239%else
240    mov     [bp+IDEPACK.intpack+INTPACK.dx], dx
241%endif
242    mov     [bp+IDEPACK.intpack+INTPACK.cx], cx
243    mov     [bp+IDEPACK.intpack+INTPACK.ax], ax
244    pushf
245    pop     WORD [bp+IDEPACK.intpack+INTPACK.flags]
246    call    RamVars_GetSegmentToDS
247
248%ifdef MODULE_DRIVEXLATE
249    ; Restore drive number translation back to what it was
250    pop     WORD [RAMVARS.xlateVars+XLATEVARS.wFDandHDswap]
251    cmp     dl, [RAMVARS.xlateVars+XLATEVARS.bXlatedDrv]    ; DL is still drive number?
252    je      SHORT Int13h_ReturnFromHandlerWithoutStoringErrorCode
253    mov     [bp+IDEPACK.intpack+INTPACK.dl], dl ; Something is returned in DL
254%endif
255    jmp     SHORT Int13h_ReturnFromHandlerWithoutStoringErrorCode
256    ; We cannot return via Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH!
257    ; 1. If the other BIOS returns something in DL then that is assumed to be a drive number
258    ;    (if MODULE_SERIAL_FLOPPY is included) even though it could be anything.
259    ; 2. Any non-zero value in AH will cause the CF to be set on return from the handler.
260    ;    This breaks INT 13h/AH=15h for drives handled by the other BIOS.
261
262
263%ifdef MODULE_SERIAL_FLOPPY
264;--------------------------------------------------------------------
265; Int13h_ReturnSuccessForFloppy
266;
267; Some operations, such as format of a floppy disk track, should just
268; return success, while for hard disks it should be treated as unsupported.
269;--------------------------------------------------------------------
270ALIGN JUMP_ALIGN
271Int13h_ReturnSuccessForFloppy:
272    test    dl, dl
273    js      SHORT UnsupportedFunction
274    xor     ah, ah
275    jmp     SHORT Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH
276%endif
277
278
279;--------------------------------------------------------------------
280; Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAHandTransferredSectorsFromCL
281;   Parameters:
282;       AH:     BIOS Error code
283;       CL:     Number of sectors actually transferred
284;       SS:BP:  Ptr to IDEPACK
285;   Returns:
286;       All registers are loaded from INTPACK
287;--------------------------------------------------------------------
288ALIGN JUMP_ALIGN
289Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAHandTransferredSectorsFromCL:
290    mov     [bp+IDEPACK.intpack+INTPACK.al], cl
291    ; Fall to Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH
292
293
294;--------------------------------------------------------------------
295; Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH
296; Int13h_ReturnFromHandlerWithoutStoringErrorCode
297;   Parameters:
298;       AH:     BIOS Error code
299;       SS:BP:  Ptr to IDEPACK
300;   Returns:
301;       All registers are loaded from INTPACK
302;--------------------------------------------------------------------
303ALIGN JUMP_ALIGN
304Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH:
305%ifdef MODULE_SERIAL_FLOPPY
306    mov     al, [bp+IDEPACK.intpack+INTPACK.dl]
307Int13h_ReturnFromHandlerAfterStoringErrorCodeFromAH_ALHasDriveNumber:
308    call    Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH_ALHasDriveNumber
309
310%else
311    call    Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH
312%endif
313
314Int13h_ReturnFromHandlerWithoutStoringErrorCode:
315    ; Always return with interrupts enabled since there are programs that rely
316    ; on INT 13h to enable interrupts.
317    or      BYTE [bp+IDEPACK.intpack+INTPACK.flags+1], (FLG_FLAGS_IF>>8)
318
319    lea     sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK]
320%ifdef USE_386
321;   pop     gs
322;   pop     fs
323%endif
324    pop     es
325    pop     ds
326    ePOPA
327    iret
328
329
330;--------------------------------------------------------------------
331; Int13h_CallPreviousInt13hHandler
332;   Parameters:
333;       AH:     INT 13h function to call
334;       DL:     Drive number
335;       DS:     RAMVARS segment
336;   Returns:
337;       Depends on function
338;       NOTE: ES:DI needs to be returned from the previous interrupt
339;             handler, for floppy DPT in function 08h
340;   Corrupts registers:
341;       None
342;--------------------------------------------------------------------
343ALIGN JUMP_ALIGN
344Int13h_CallPreviousInt13hHandler:
345    pushf                       ; Simulate INT by pushing flags
346    call    FAR [RAMVARS.fpOldI13h]
347    ret
348
349
350;--------------------------------------------------------------------
351; Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH_ALHasDriveNumber
352; Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH
353; Int13h_SetErrorCodeToIntpackInSSBPfromAH
354;   Parameters:
355;       AH:     BIOS error code (00h = no error)
356;       SS:BP:  Ptr to IDEPACK
357;   Returns:
358;       SS:BP:  Ptr to IDEPACK with error condition set
359;   Corrupts registers:
360;       DS, BX, DI
361;--------------------------------------------------------------------
362ALIGN JUMP_ALIGN
363%ifdef MODULE_SERIAL_FLOPPY
364Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH_ALHasDriveNumber:
365    ; Store error code to BDA
366    mov     bx, BDA.bHDLastSt
367    test    al, al
368    js      SHORT .HardDisk
369    mov     bl, BDA.bFDRetST & 0xff
370.HardDisk:
371    LOAD_BDA_SEGMENT_TO ds, di
372    mov     [bx], ah
373    ; Fall to Int13h_SetErrorCodeToIntpackInSSBPfromAH
374
375%else
376Int13h_SetErrorCodeToBdaAndToIntpackInSSBPfromAH:
377    ; Store error code to BDA
378    LOAD_BDA_SEGMENT_TO ds, di
379    mov     [BDA.bHDLastSt], ah
380    ; Fall to Int13h_SetErrorCodeToIntpackInSSBPfromAH
381%endif
382
383    ; Store error code to INTPACK
384Int13h_SetErrorCodeToIntpackInSSBPfromAH:
385    mov     [bp+IDEPACK.intpack+INTPACK.ah], ah
386    test    ah, ah
387    jnz     SHORT .SetCFtoIntpack
388    and     BYTE [bp+IDEPACK.intpack+INTPACK.flags], ~FLG_FLAGS_CF
389    ret
390.SetCFtoIntpack:
391    or      BYTE [bp+IDEPACK.intpack+INTPACK.flags], FLG_FLAGS_CF
392    ret
393
394
395; Jump table for correct BIOS function
396ALIGN WORD_ALIGN
397g_rgw13hFuncJump:
398    dw  AH0h_HandlerForDiskControllerReset          ; 00h, Disk Controller Reset (All)
399    dw  AH1h_HandlerForReadDiskStatus               ; 01h, Read Disk Status (All)
400    dw  AH2h_HandlerForReadDiskSectors              ; 02h, Read Disk Sectors (All)
401    dw  AH3h_HandlerForWriteDiskSectors             ; 03h, Write Disk Sectors (All)
402    dw  AH4h_HandlerForVerifyDiskSectors            ; 04h, Verify Disk Sectors (All)
403%ifdef MODULE_SERIAL_FLOPPY
404    dw  Int13h_ReturnSuccessForFloppy               ; 05h, Format Disk Track (XT, AT, EISA)
405%else
406    dw  UnsupportedFunction                         ; 05h, Format Disk Track (XT, AT, EISA)
407%endif
408    dw  UnsupportedFunction                         ; 06h, Format Disk Track with Bad Sectors (XT)
409    dw  UnsupportedFunction                         ; 07h, Format Multiple Cylinders (XT)
410    dw  AH8h_HandlerForReadDiskDriveParameters      ; 08h, Read Disk Drive Parameters (All)
411    dw  AH9h_HandlerForInitializeDriveParameters    ; 09h, Initialize Drive Parameters (All)
412    dw  UnsupportedFunction                         ; 0Ah, Read Disk Sectors with ECC (XT, AT, EISA)
413    dw  UnsupportedFunction                         ; 0Bh, Write Disk Sectors with ECC (XT, AT, EISA)
414    dw  AHCh_HandlerForSeek                         ; 0Ch, Seek (All)
415    dw  AH9h_HandlerForInitializeDriveParameters    ; 0Dh, Alternate Disk Reset (All)
416    dw  UnsupportedFunction                         ; 0Eh, Read Sector Buffer (XT, PS/1), ESDI Undocumented Diagnostic (PS/2)
417    dw  UnsupportedFunction                         ; 0Fh, Write Sector Buffer (XT, PS/1), ESDI Undocumented Diagnostic (PS/2)
418    dw  AH10h_HandlerForCheckDriveReady             ; 10h, Check Drive Ready (All)
419    dw  AH11h_HandlerForRecalibrate                 ; 11h, Recalibrate (All)
420    dw  UnsupportedFunction                         ; 12h, Controller RAM Diagnostic (XT)
421    dw  UnsupportedFunction                         ; 13h, Drive Diagnostic (XT)
422    dw  AH10h_HandlerForCheckDriveReady             ; 14h, Controller Internal Diagnostic (All)
423    dw  AH15h_HandlerForReadDiskDriveSize           ; 15h, Read Disk Drive Size (AT+)
424    dw  UnsupportedFunction                         ; 16h,
425    dw  UnsupportedFunction                         ; 17h,
426    dw  UnsupportedFunction                         ; 18h,
427    dw  UnsupportedFunction                         ; 19h, Park Heads (PS/2)
428    dw  UnsupportedFunction                         ; 1Ah, Format ESDI Drive (PS/2)
429    dw  UnsupportedFunction                         ; 1Bh, Get ESDI Manufacturing Header (PS/2)
430    dw  UnsupportedFunction                         ; 1Ch, ESDI Special Functions (PS/2)
431    dw  UnsupportedFunction                         ; 1Dh,
432%ifdef MODULE_8BIT_IDE_ADVANCED
433    dw  AH1Eh_HandlerForXTCFfeatures                ; 1Eh, Lo-tech XT-CF features (XTIDE Universal BIOS)
434%else
435    dw  UnsupportedFunction                         ; 1Eh,
436%endif
437    dw  UnsupportedFunction                         ; 1Fh,
438    dw  UnsupportedFunction                         ; 20h,
439    dw  UnsupportedFunction                         ; 21h, Read Disk Sectors, Multiple Blocks (PS/1)
440    dw  UnsupportedFunction                         ; 22h, Write Disk Sectors, Multiple Blocks (PS/1)
441    dw  AH23h_HandlerForSetControllerFeatures       ; 23h, Set Controller Features Register (PS/1)
442    dw  AH24h_HandlerForSetMultipleBlocks           ; 24h, Set Multiple Blocks (PS/1)
443    dw  AH25h_HandlerForGetDriveInformation         ; 25h, Get Drive Information (PS/1)
444
445%ifdef MODULE_EBIOS
446g_rgwEbiosFunctionJumpTable:
447    dw  AH41h_HandlerForCheckIfExtensionsPresent    ; 41h, Check if Extensions Present (EBIOS)*
448    dw  AH42h_HandlerForExtendedReadSectors         ; 42h, Extended Read Sectors (EBIOS)*
449    dw  AH43h_HandlerForExtendedWriteSectors        ; 43h, Extended Write Sectors (EBIOS)*
450    dw  AH44h_HandlerForExtendedVerifySectors       ; 44h, Extended Verify Sectors (EBIOS)*
451    dw  UnsupportedFunction                         ; 45h, Lock and Unlock Drive (EBIOS)***
452    dw  UnsupportedFunction                         ; 46h, Eject Media Request (EBIOS)***
453    dw  AH47h_HandlerForExtendedSeek                ; 47h, Extended Seek (EBIOS)*
454    dw  AH48h_HandlerForGetExtendedDriveParameters  ; 48h, Get Extended Drive Parameters (EBIOS)*
455;   dw  UnsupportedFunction                         ; 49h, Get Extended Disk Change Status (EBIOS)***
456;   dw  UnsupportedFunction                         ; 4Ah, Initiate Disk Emulation (Bootable CD-ROM)
457;   dw  UnsupportedFunction                         ; 4Bh, Terminate Disk Emulation (Bootable CD-ROM)
458;   dw  UnsupportedFunction                         ; 4Ch, Initiate Disk Emulation and Boot (Bootable CD-ROM)
459;   dw  UnsupportedFunction                         ; 4Dh, Return Boot Catalog (Bootable CD-ROM)
460;   dw  UnsupportedFunction                         ; 4Eh, Set Hardware Configuration (EBIOS)**
461;
462;   * = Enhanced Drive Access Support (minimum required EBIOS functions)
463;  ** = Enhanced Disk Drive (EDD) Support
464; *** = Drive Locking and Ejecting Support
465%endif
Note: See TracBrowser for help on using the repository browser.