home *** CD-ROM | disk | FTP | other *** search
/ Cuteskunk BBS / cuteskunk.zip / cuteskunk / Virus / Virus-Magazines / NuKE / nk-info6.zi2 / NK-INFO6.005 < prev    next >
Text File  |  1993-05-05  |  14KB  |  464 lines

  1. ================================================================================
  2. Volume 1, Issue 6, May 1993
  3. NuKE Info-Journal #6
  4.  
  5.             NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE
  6.             uK                                                  E-
  7.             KE  "Programming the NEC765 Floppy Disk Controller, -N
  8.             E-      and the DMA Chip to bypass the Int 13h      Nu 
  9.             -N                                                  uK
  10.             Nu                       By                         KE
  11.             uK                     Dr. X                        E-
  12.             KE                                                  -N
  13.             E-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-NuKE-Nu
  14.  
  15. % The Challenge %
  16. ~~~~~~~~~~~~~~~~~
  17.  
  18. The challenge was started by Dr. X in order to try to access the disk media
  19. without using any DOS or Bios Interrupt 13h calls. Surely a _very_ difficult
  20. challenge indeed, nevertheless Dr. X has succeeded in doing so, and he will
  21. explain the theory behind his development. This scholar does deserve a 
  22. `pat on the back' for his brain teaser work. Good work Dr. X, and welcome
  23. aboard.
  24.                          NuKE Members/Supporters
  25.           
  26. % Programming the Floppy Disk Controller & DMA chip to bypass the Int 13h %
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28.  
  29. The NEC 765 floppy disk controller chip controls floppy disk drives motors
  30. and heads. And it manages the flow of data to and from the disk sector(s).
  31.  
  32. The FDC (Floppy Disk Controller) performs 15 operations in all, of which 
  33. only three are discussed here. They are Seek, Read and Write.
  34.  
  35. The FDC operates in three phases:
  36.  
  37.   1) The command phase
  38.   2) The execution phase
  39.   3) The result phase
  40.  
  41. a) The command phase  : When one or more bytes are sent to the Data Registers
  42. b) The execution phase: When the FDC undertake the command
  43. c) The result phase   : A number of status byte(s) are read from the Data
  44.                         Register(s)
  45.  
  46. % I) The Ports %
  47.  
  48. The FDC is operated through only three I/O (Input/Output) ports:
  49. 3F2  - Digital Output Port
  50. 3F4  - Status Register
  51. 3F5  - Data Register
  52.  
  53.   1. Digital Output Port (3F2)
  54.      
  55.      Bits        Function
  56.      1-0         Drive # ; 00=A, 01=B, 10=C, 11=D
  57.      2           0=Reset the floppy disk controller (***)
  58.      3           1=Enable FDC interrupt and DMA access
  59.      7-4         1=Turn ON drive motors D to A (bit 4 = drive A)
  60.  
  61.   Warning: This register is WRITE ONLY
  62.   (***) Do not set bit 2 to 0 at any time (recelebrate)
  63.  
  64.   2. Data Register (3F5)
  65.      
  66.      Operation     Byte #       Function
  67.      Seek          1            Code number (Fh)
  68.                    2            Head & Drive : 00000HDD (h=head, DD=drive)
  69.      Read Sector   1            Code number (66h)
  70.                    2            Head & Drive : 00000HDD (h=head, DD=drive)
  71.                    3            Track number
  72.                    4            Head number
  73.                    5            Sector number
  74.                    6            Bytes in sector (2=512)
  75.                    7            End of track (09)
  76.                    8            GAP Length
  77.                    9            Data Length
  78.      Write Sector  1            Code number (45h)
  79.                    2-9          Same as READ SECTOR (above)
  80.  
  81.   Warning: You must be sure that the FDC is ready before you send or read a 
  82.            a byte from the data register. Bits 7-6 of the status register
  83.            provide this information.
  84.  
  85.   3. Status Register (3F4)
  86.      
  87.      Bits        Function
  88.      3-0         1=Disk drive D-A in Seek Mode
  89.      4           1=FDC read or write command in progress
  90.      5           1=FDC is not in DMA mode
  91.      6           1=FDC data register is ready to send data
  92.                  0=FDC data register is ready to receive data
  93.      7           1=FDC ready to send or receive data
  94.  
  95.   Warning: When a seek operation is complete, the FDC invokes a INT 6h 
  96.            (the disk interrupt). When the interrupt occurs, the BIOS 
  97.            interrupt handler sets the bit 7 of the seek status byte in 
  98.            the BIOS Data Area located at 0:043E. This is the sole result of
  99.            the interrupt.
  100.  
  101. % II) Initializing %
  102.  
  103. Before initializing a channel, the program must send a code to the chip
  104. telling it whether it is reading from or writing to the Floppy Disk 
  105. Controller. This one byte code is 46h for reading and 4Ah for writing. 
  106. The code must be sent to each of two separate port addresses: 0E & 0C.
  107. After that, you can send the parameters to the Data Register (3F5), 
  108. following the bellow steps:
  109.   1. Turn on the floppy disk (enable interrupts with a SLI first)
  110.      a) Out the code byte to the Digital Output Register (3F2)
  111.      b) Send 46h to read or 4Ah to write to each of two separate port
  112.        addresses 0B and 0Ch
  113.         (eg: Out 0B,46h
  114.              Out 0C,46h)
  115.  
  116.   2. Then you _must_ perform a seek operation to the concerned Head 
  117.      and Track;
  118.      a) Out the code for Seek operation (0F) to the FDC (3F5)
  119.      b) Out head & Drive code (00000HDDxB, H=head,DD=drive)
  120.      c) Out the track number
  121.      d) Wait for Int 6h
  122.  
  123.   3. After that you can perform the read or write operation(s):
  124.      a) Calculate the address of the buffer (see the program at the end
  125.         of this Article)
  126.      b) Send the address to the DMA
  127.      c) Out the value 66h for read or 45h for write to the FDC (3F5)
  128.      d) Out the Head & Drive number
  129.      e) Out the Track number
  130.      f) Out the Head number
  131.      g) Out the Sector number
  132.      h) Out the Sector Code; get this information with INT 21h
  133.      i) Out End-of-Track   ; with AX=1E35h
  134.      j) Out the GAP length
  135.      k) Out the data length 
  136.      l) Wait for INT 6h
  137.      m) Perform 7 INs from the Data Register (3F5) to get the status bytes.
  138.         (Refer to Part III)
  139.  
  140.   4. Finally, turn off the motor(s):
  141.      a) Out the code byte to the Digital Output Register (3F2)
  142.  
  143. % III) The Status Bytes %
  144.  
  145. After a read or write operation the FDC gives you 7 status bytes:
  146.  
  147. Byte #        Function
  148.   1           Status Byte 0
  149.   2           Status Byte 1
  150.   3           Status Byte 2
  151.   4           Track number
  152.   5           Head number
  153.   6           Sector number
  154.   7           Byte per sector code (0-3)
  155.  
  156.   1. Status Byte 0
  157.  
  158.      Bit #      Function
  159.      7-6        00=normal termination
  160.                 01=execution began, could not complete 
  161.                 10=invalid command
  162.                 11=failed because disk drive went offline
  163.      5          1=seek operation in progress
  164.      4          1=disk drive fault
  165.      3          1=disk drive not ready
  166.      2          number of selected head
  167.      1-0        number of selected drive
  168.  
  169.   2. Status Byte 1
  170.  
  171.      Bit #      Function
  172.      7          1=requested sector beyond last sector number
  173.      6          always 0
  174.      5          1=data transfer error
  175.      4          1=data overrun
  176.      3          always 0
  177.      2          1=cannot read or find sector
  178.      1          1=cannot write because of write protection tab
  179.      0          1=missing address mark in disk format
  180.   
  181.   3. Status Byte 2
  182.      
  183.      Bit #      Function
  184.      7          always 0
  185.      6          1=encountered delete-data address mark
  186.      5          1=CRC error in data
  187.      4          1=track identification problem
  188.      3          1=scan command condition satisfied
  189.      2          1=scan command condition NOT satisfied
  190.      1          1=bad track
  191.      0          1=missing address mark
  192.  
  193. % IV) Read Procedure in ASM (for A86 assembler) %
  194.  
  195.  
  196.  Jmp  TheCode
  197.  Buffer Db 512 dup (0)         ; For the sector
  198.  StatusBuffer Db 7 Dup (7)     ; For the status bytes
  199.  
  200.  TheCode Proc Near
  201.  ReadSector:
  202.  ; Turn ON the Motor
  203.    Sti
  204.    Mov Dx,03F2H
  205.    Mov Al,00101101xB    ; Set the Bits 0 , 2 ,3 , 4
  206.    Out Dx,Al        
  207.  ; Wait for motor to come to speed (1/2 second)
  208.    Call Motor_Delay
  209.    Mov Cx,2000
  210.    Loop $
  211.  ; Begin the initialization of DMA Chip
  212.    Mov Al,46H     ; Code for Read Datas
  213.    Out 11,Al      ; Send Datas
  214.    Out 12,Al
  215.  ; Now , Calculate buffer address
  216.    Lea Ax,Buffer  ;
  217.    Mov Bx,Ds      ;
  218.    Rol Bx,4       ;
  219.    Push Bx        ;
  220.    And Bl,0FH     ;
  221.    Mov Dl,Bl      ;  
  222.    Pop Bx         ;
  223.    Add Ax,Bx      ;
  224.    Jnc NoCarry    ;
  225.    Inc Dl         ;
  226.    NoCarry:       ;
  227.    Dec Al         ; justify 
  228.    Out 4,Al       ; Send Low Byte of adress to the DMA controller
  229.    Mov Al,Ah      ;
  230.    Out 4,Al       ; Send High byte of the adress   //  //  //  //
  231.    Mov Al,Dl      ;
  232.    Out 81h,Al     ; Send Page number  (Page register)
  233.  ; Finish initialization
  234.    Mov Ax,511     ;
  235.    Out 5,Al       ; DMA controller
  236.    Mov Al,Ah      ;
  237.    Out 5,Al       ;
  238.    Mov Al,2       ;
  239.    Out 10,Al      ; DMA controller
  240.  ; Get pointer to disk base
  241.    Mov Al,1EH     ;
  242.    Mov Ah,35H     ;
  243.    Int 021H       ;
  244.  ; Send read parameters.
  245.    Mov Ah,066H    ; Code for single sector read
  246.    Call Out_Fdc   ; Send It
  247.    Mov  Ah,2      ; Head&Drive #
  248.    Call Out_FDC   ; Send It
  249.    Mov  Ah,1      ; Track Number 
  250.    Call Out_FDC   ; Send It
  251.    Mov  Ah,0      ; Head #
  252.    Call Out_FDC   ; Send It
  253.    Mov  Ah,3      ; Sector #
  254.    Call Out_FDC   ; Send it
  255.    Mov  Ah,Es:[Bx]+3 ; Sector Size code (2=512 bytes)
  256.    Call Out_FDC   ; Send it
  257.    Mov  Ah,Es:[Bx]+4 ; End-of-track #
  258.    Call Out_FDC   ; Send It
  259.    Mov  Ah,Es:[Bx]+5 ; Gap length
  260.    Call Out_FDC   ; Send it
  261.    Mov  Ah,Es:[Bx]+6 ; Datas length
  262.    Call Out_FDC   ; Send
  263.    Call Wait_Interrupt ; Wait Int 6
  264.  ; Read the result bytes ..
  265.    Mov Cx,7
  266.    Lea Bx,StatusBuffer
  267.    Next:
  268.    Call In_FDC
  269.    Mov [BX],Al
  270.    Inc Bx
  271.    Loop Next
  272.  ; Turn OFF the motor
  273.    Mov Dx,03F2H
  274.    Mov Al,00001101xB   ; Turn Off the Drive B
  275.    Out Dx,Al
  276.    Ret                 ; Exit from the programm
  277.  Sector_REad Endp
  278.  
  279.  Wait_interrupt Proc 
  280.  ; Monitor the int 6 in bios status Byte
  281.    Mov Ax,40H
  282.    Mov Es,Ax
  283.    Mov Bx,3EH
  284.    Again:
  285.    Mov Dl,Es:[BX]
  286.    Test Dl,080H
  287.    Jz Again
  288.    And Al,127
  289.    Mov Es:[Bx],Dl
  290.    Ret
  291.  Wait_Interrupt EndP
  292.  
  293.  Out_FDC proc near
  294.    Mov Dx,03F4H
  295.    Keep_Trying:
  296.    In  Al,Dx
  297.    Test Al,128
  298.    Jz  Keep_Trying
  299.    Inc Dx
  300.    Mov Al,Ah
  301.    Out Dx,Al
  302.    RET
  303.  Out_FDC EndP
  304.  
  305.  In_FDC Proc Near
  306.    Mov Dx,03F4H
  307.    Keep_Trying2:
  308.    In  Al,Dx
  309.    Test Al,128
  310.    Jz  Keep_Trying2
  311.    Inc Dx
  312.    In  Al,Dx
  313.    Ret
  314.   In_FDC EndP
  315.  
  316.   Motor_Delay Proc
  317.    Mov Ah,15           ; Perform Seek Operation
  318.    Call Out_FDC        ; Out
  319.    Mov Ah,2            ; Head&Drive
  320.    Call Out_FDC        ; Out
  321.    Mov Ah,1            ; track#
  322.    Call Out_FDC        ;
  323.    Call Wait_interrupt ;
  324.    Ret
  325.   Motor_Delay endp
  326.  
  327. --------------------------------------------------------------------------------
  328.  ; The bellow is yet, another example for reading the first few beginning tracks
  329.  ; but this one is for the Hard Disk
  330.  ; By X
  331.  ; Not `fully completed', but enough to get the point.
  332.  
  333.  Jmp  TheCode
  334.  Buffer Db 512 dup (0)         ; For the sector
  335.  StatusBuffer Db 7 Dup (7)     ; For the status bytes
  336.  
  337.  TheCode Proc Near
  338.  ReadSector:
  339.  ; Turn ON the Motor
  340.    Sti
  341.    Mov Dx,03F2H
  342.    Mov Al,00101101xB    ; Set the Bits 0 , 2 ,3 , 4
  343.    Out Dx,Al        
  344.  ; Wait for motor to come to speed (1/2 second)
  345.    Call Motor_Delay
  346.    Mov Cx,2000
  347.    Loop $
  348.  ; Begin the initialization of DMA Chip
  349.    Mov Al,46H     ; Code for Read Data
  350.    Out 11,Al      ; Send Data
  351.    Out 12,Al
  352.  ; Now , Calculate buffer adress
  353.    Lea Ax,Buffer  ;
  354.    Mov Bx,Ds      ;
  355.    Rol Bx,4       ;
  356.    Push Bx        ;
  357.    And Bl,0FH     ;
  358.    Mov Dl,Bl      ;  
  359.    Pop Bx         ;
  360.    Add Ax,Bx      ;
  361.    Jnc NoCarry    ;
  362.    Inc Dl         ;
  363.    NoCarry:       ;
  364.    Dec Al         ; justify 
  365.    Out 4,Al       ; Send Low Byte of address to the DMA controller
  366.    Mov Al,Ah      ;
  367.    Out 4,Al       ; Send High byte of the address   //  //  //  //
  368.    Mov Al,Dl      ;
  369.    Out 81h,Al     ; Send Page number  (Page register)
  370.  ; Finish initialization
  371.    Mov Ax,511     ;
  372.    Out 5,Al       ; DMA controller
  373.    Mov Al,Ah      ;
  374.    Out 5,Al       ;
  375.    Mov Al,2       ;
  376.    Out 10,Al      ; DMA controller
  377.  ; Get pointer to disk base
  378.    Mov Al,1EH     ;
  379.    Mov Ah,35H     ;
  380.    Int 021H       ;
  381.  ; Send read parametres.
  382.    Mov Ah,066H    ; Code for single sector read
  383.    Call Out_Fdc   ; Send It
  384.    Mov  Ah,0      ; Head&Drive #
  385.    Call Out_FDC   ; Send It
  386.    Mov  Ah,12     ; Track Number 
  387.    Call Out_FDC   ; Send It
  388.    Mov  Ah,0      ; Head #
  389.    Call Out_FDC   ; Send It
  390.    Mov  Ah,3      ; Sector #
  391.    Call Out_FDC   ; Send it
  392.    Mov  Ah,Es:[Bx]+3 ; Sector Size code (2=512 bytes)
  393.    Call Out_FDC   ; Send it
  394.    Mov  Ah,Es:[Bx]+4 ; End-of-track #
  395.    Call Out_FDC   ; Send It
  396.    Mov  Ah,Es:[Bx]+5 ; Gap length
  397.    Call Out_FDC   ; Send it
  398.    Mov  Ah,Es:[Bx]+6 ; Datas length
  399.    Call Out_FDC   ; Send
  400.    Call Wait_Interrupt ; Wait Int 6
  401.  ; Read the result bytes ..
  402.    Mov Cx,7
  403.    Lea Bx,StatusBuffer
  404.    Next:
  405.    Call In_FDC
  406.    Mov [BX],Al
  407.    Inc Bx
  408.    Loop Next
  409.  ; Turn OFF the motor
  410.    Mov Dx,03F2H
  411.    Mov Al,12
  412.    Out Dx,Al
  413.    Ret                 ; Exit from the programm
  414.  Sector_REad Endp
  415.  
  416.  Wait_interrupt Proc 
  417.  ; Monitor the int 6 in bios status Byte
  418.    Mov Ax,40H
  419.    Mov Es,Ax
  420.    Mov Bx,3EH
  421.    Again:
  422.    Mov Dl,Es:[BX]
  423.    Test Dl,080H
  424.    Jz Again
  425.    And Al,127
  426.    Mov Es:[Bx],Dl
  427.    Ret
  428.  Wait_Interrupt EndP
  429.  
  430.  Out_FDC proc near
  431.    Mov Dx,03F4H
  432.    Keep_Trying:
  433.    In  Al,Dx
  434.    Test Al,128
  435.    Jz  Keep_Trying
  436.    Inc Dx
  437.    Mov Al,Ah
  438.    Out Dx,Al
  439.    RET
  440.  Out_FDC EndP
  441.  
  442.  In_FDC Proc Near
  443.    Mov Dx,03F4H
  444.    Keep_Trying2:
  445.    In  Al,Dx
  446.    Test Al,128
  447.    Jz  Keep_Trying2
  448.    Inc Dx
  449.    In  Al,Dx
  450.    Ret
  451.   In_FDC EndP
  452.  
  453.   Motor_Delay Proc
  454.    Mov Ah,15           ; Perform Seek Operation
  455.    Call Out_FDC        ; Out
  456.    Mov Ah,0            ; Head&Drive
  457.    Call Out_FDC        ; Out
  458.    Mov Ah,12           ; track#
  459.    Call Out_FDC        ;
  460.    Call Wait_interrupt ;
  461.    Ret
  462.   Motor_Delay endp
  463. ================================================================================
  464.