home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d725 / snoopdos.lha / SnoopDos / src / snoopglue.s < prev    next >
Text File  |  1992-09-05  |  7KB  |  261 lines

  1. ***************************************************************** :ts=8 *****
  2. *
  3. *    SNOOPGLUE.S
  4. *
  5. *    Assembly support routines for snoopdos.c
  6. *
  7. *    This file contains routines to patch/unpatch dos library to point
  8. *    to a C routine, and to call the old routines.
  9. *
  10. *    Does some special tricks to patch into dos.library, in such a way
  11. *    as to work properly under both 1.3 and 2.0.
  12. *
  13. *    Also includes a tiny sprintf function.
  14. *
  15. *****************************************************************************
  16.  
  17.     INCLUDE "exec/types.i"
  18.     INCLUDE "exec/libraries.i"
  19.     INCLUDE    "libraries/dosextens.i"
  20.  
  21.     XDEF    _installdospatch
  22.     XDEF    _uninstalldospatch
  23.     XDEF    _CallLock
  24.     XDEF    _CallOpen
  25.     XDEF    _CallLoadSeg
  26.     XDEF    _CallExecute
  27.     XDEF    _CallCurrentDir
  28.     XDEF    _CallDeleteFile
  29.     XDEF    _sprintf
  30.  
  31.     XREF    _DOSBase
  32.     XREF    _SysBase
  33.  
  34.     XREF    _NewOpen
  35.     XREF    _NewLock
  36.     XREF    _NewLoadSeg
  37.     XREF    _NewExecute
  38.     XREF    _NewCurrentDir
  39.     XREF    _NewDeleteFile
  40.  
  41.     XREF    _LVOOpen
  42.     XREF    _LVOLock
  43.     XREF    _LVOLoadSeg
  44.     XREF    _LVOExecute
  45.     XREF    _LVOCurrentDir
  46.     XREF    _LVODeleteFile
  47.     XREF    _LVORawDoFmt
  48.  
  49. CALL    MACRO
  50.     XREF    _LVO\1
  51.     JSR    _LVO\1(A6)
  52.     ENDM
  53.  
  54.     SECTION    SnoopDos,CODE
  55.  
  56. *
  57. *    Modify DOS library to call our functions for Open() and Lock();
  58. *    We can't just use SetFunction() because DOS library is non-standard.
  59. *    Instead of using JMP $123456 type instructions, it uses (mostly)
  60. *    MOVEQ #funcnum,D0; BRA.L Dispatch. So, we replace this entire
  61. *    sequence with a standard JMP instruction, steal the number from
  62. *    the MOVEQ instruction and use it when we want to call the real
  63. *    DOS library later on.
  64. *
  65. _installdospatch:
  66.     MOVEM.L    A0-A3/A6,-(A7)        * Save regs
  67.     MOVE.L    4,A6            * Get ExecBase
  68.     MOVE.L    _DOSBase,A0        * Get DOSbase
  69.     BSET    #LIBB_CHANGED,LIB_FLAGS(A0) * Indicate changing DOS library
  70.  
  71. SetFunc    MACRO
  72.     MOVEM.W    _LVO\1(A0),A1-A3    * Read in current value for func
  73.     MOVEM.W    A1-A3,\1Save        * Save the appropriate data
  74.     MOVEM.W CallOur\1,A1-A3        * Read in replacement code
  75.     MOVEM.W    A1-A3,_LVO\1(A0)    * And modify library
  76.     ENDM
  77.  
  78.     SetFunc    Open
  79.     SetFunc Lock
  80.     SetFunc LoadSeg
  81.     SetFunc Execute
  82.     SetFunc CurrentDir
  83.     SetFunc DeleteFile
  84.  
  85.     MOVE.L    A0,A1            * Finally,
  86.     CALL    SumLibrary        * Recalculate library checksum
  87.     MOVEM.L    (A7)+,A0-A3/A6        * Save regs
  88.     RTS
  89.  
  90. *
  91. *    Reinstall previous DOS patch. If someone has already altered the
  92. *    function, we can't actually exit immediately so return a FALSE
  93. *    result. While there is a very small window which could cause
  94. *    problems (if someone is in the middle of a SetFunction() when
  95. *    this is called), but we'll ignore this mostly.
  96. *
  97. _uninstalldospatch:
  98.     MOVEM.L    A0-A3/A6,-(A7)        * Save regs
  99.     MOVE.L    4,A6            * Get sysbase
  100.     MOVE.L    _DOSBase,A0        * Get DOSBase
  101.  
  102. TestFunc MACRO
  103.     MOVE.L    CallOur\1+2,D0        * Get old address of function
  104.     CMP.L    _LVO\1+2(A0),D0        * See if changed
  105.     BNE    changed            * If it has, exit with no action
  106.     ENDM
  107.  
  108.     TestFunc Open
  109.     TestFunc Lock
  110.     TestFunc LoadSeg
  111.     TestFunc Execute
  112.     TestFunc CurrentDir
  113.     TestFunc DeleteFile
  114.  
  115.     BSET    #LIBB_CHANGED,LIB_FLAGS(A0) * Indicate changing DOS library
  116.  
  117. RestFunc MACRO
  118.     MOVEM.W    \1Save,A1-A3        * Read in prev. value of vec.
  119.     MOVEM.W    A1-A3,_LVO\1(A0)    * Atomically restore it
  120.     ENDM
  121.  
  122.     RestFunc Open
  123.     RestFunc Lock
  124.     RestFunc LoadSeg
  125.     RestFunc Execute
  126.     RestFunc CurrentDir
  127.     RestFunc DeleteFile
  128.  
  129.     MOVE.L    A0,A1            * Finally,
  130.     CALL    SumLibrary        * Update library checksum
  131.     MOVEQ    #1,D0            * Indicate success
  132.     BRA.S    unexit            * And exit
  133.  
  134. changed:
  135.     MOVEQ    #0,D0            * Indicate failure to uninstall
  136. unexit:
  137.     MOVEM.L    (A7)+,A0-A3/A6        * Restore regs
  138.     RTS                * And exit with return in D0
  139.  
  140. *
  141. *    This code is the code that gets moved directly into the DOS
  142. *    library vectors
  143. *
  144. CallOurOpen:        JMP    NewOpen
  145. CallOurLock:        JMP    NewLock
  146. CallOurLoadSeg:        JMP    NewLoadSeg
  147. CallOurExecute:        JMP    NewExecute
  148. CallOurCurrentDir:    JMP    NewCurrentDir
  149. CallOurDeleteFile:    JMP    NewDeleteFile
  150.  
  151. *
  152. *    These are the replacement DOS routines.
  153. *
  154. NewFunc    MACRO
  155.     MOVEM.L    A2-A6/D2-D7,-(A7)    * Save all registers (to be safe)
  156.     JSR    _New\1            * Call C version with params in D1-D3
  157.     MOVEM.L    (A7)+,A2-A6/D2-D7    * Restore registers
  158.     MOVE.L    D0,D1            * Copy return value into D1
  159. *                    * [some routines expect this :-( ]
  160.     RTS                * Return to caller, exit value in D0
  161.     ENDM
  162.  
  163. NewOpen:    NewFunc Open
  164. NewLock:    NewFunc Lock
  165. NewLoadSeg:    NewFunc LoadSeg
  166. NewExecute:    NewFunc Execute
  167. NewCurrentDir:    NewFunc CurrentDir
  168. NewDeleteFile:    NewFunc    DeleteFile
  169.  
  170. *
  171. *    Allow's C to call the old DOS functions. If we're running Kikstart 2.0
  172. *    then just call the original function directly. Otherwise, calculate
  173. *    where to go from the code stored in the vector.
  174. *
  175. OldDosCall MACRO
  176.     LEA.L    \1Save,A0    * Get pointer to func save vector
  177.     MOVE.W    #_LVO\1,D0    * Get library offset vector
  178.     BRA.S    CallDos        * Skip to execute it
  179.     ENDM
  180.  
  181. _CallOpen:        OldDosCall Open
  182. _CallLock:        OldDosCall Lock
  183. _CallLoadSeg:        OldDosCall LoadSeg
  184. _CallExecute:        OldDosCall Execute
  185. _CallCurrentDir:    OldDosCall CurrentDir
  186. _CallDeleteFile:    OldDosCall DeleteFile
  187.  
  188.     NOP                * Stop Lattice messing up final branch
  189. CallDos:
  190.     MOVEM.L    D1-D3/D6/D7/A6,-(A7)    * Save registers
  191.     MOVE.W    (A0),D6            * Get previous instruction
  192.     CMP.W    #$4ef9,D6        * Is it a JMP instruction?
  193.     BNE    dos_exec        * If not, then call using special code
  194.     MOVE.L    2(A0),A0        * Else it's 2.0 or SetFunction()
  195.     MOVE.L    _DOSBase,A6        * Put DOSBase into A6 as expected
  196.     JSR    (A0)            * Call original function
  197.     BRA.S    callexit        * And exit
  198.  
  199. dos_exec:
  200.     MOVE.W    (A0),D6            * Get MOVEQ instruction
  201.     EXT.W    D6            * Convert data to word
  202.     EXT.L    D6            * and then longword
  203.     EXG.L    D0,D6            * Swap with library vector offset
  204.     MOVE.W    4(A0),D7        * Get offset to DOS routine from LVO
  205.     MOVE.L    _DOSBase,A6        * Get DOSBase 
  206.     ADD.W    D6,D7            * Calculate offset value
  207.     JSR    4(A6,D7.W)        * Call DOS function dispatcher
  208.  
  209. callexit:
  210.     MOVEM.L    (A7)+,D1-D3/D6/D7/A6    * Restore registers
  211.     RTS                * And return to C caller
  212.  
  213. *****************************************************************************
  214. *
  215. *    Taken straight from the Rom Kernel Manuals (V1.3 ed.)
  216. *
  217. *    Simple version of the C sprintf() function. Assumes C-style
  218. *    stack-based function conventions.
  219. *
  220. *        long eyecount;
  221. *        eyecount=2;
  222. *        sprintf(string, "%s have %ld eyes.", "Fish", eyecount);
  223. *
  224. *    would produce "Fish have 2 eyes." in the string buffer. Note that
  225. *    integer arguments must be explicitly given as long, otherwise they
  226. *    default to 16 bits.
  227. *    
  228. *****************************************************************************
  229.  
  230. stuffChar:
  231.     move.b    d0,(a3)+        ; Store data in output string
  232.     rts
  233.  
  234. _sprintf:
  235.     movem.l a2-a3/a6,-(sp)        ; Save registers
  236.     move.l    4*4(sp),a3        ; Get the output string pointer
  237.     move.l    5*4(sp),a0        ; Get the FormatString pointer
  238.     lea.l    6*4(sp),a1        ; Get the pointer to the DataStream
  239.     lea.l    stuffChar(pc),a2    ; Get pointer to char stuff routine
  240.     move.l    _SysBase,a6        ; Get ExecBase
  241.     jsr    _LVORawDoFmt(a6)    ; Format string
  242.     movem.l (sp)+,a2-a3/a6        ; Restore registers
  243.     rts
  244.  
  245. *****************************************************************************
  246. *
  247. *    Globals used by SnoopDos
  248. *
  249. *****************************************************************************
  250.  
  251.     SECTION SnoopData,DATA
  252.  
  253. OpenSave    DS.W    3
  254. LockSave    DS.W    3
  255. LoadSegSave    DS.W    3
  256. ExecuteSave    DS.W    3
  257. CurrentDirSave    DS.W    3
  258. DeleteFileSave    DS.W    3
  259.  
  260.     END
  261.