Advertisement
Guest User

Ethan

a guest
Jun 13th, 2009
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;*******************************************************
  2. ;
  3. ;   Fat12.inc
  4. ;       FAT12 filesystem for 3-1/2 floppies
  5. ;
  6. ;   OS Development Series
  7. ;*******************************************************
  8.  
  9. %ifndef __FAT12_INC_67343546FDCC56AAB872_INCLUDED__
  10. %define __FAT12_INC_67343546FDCC56AAB872_INCLUDED__
  11.  
  12. bits    16
  13.  
  14. %include "Floppy16.inc"                 ; the erm.. floppy driver
  15.  
  16. %define ROOT_OFFSET 0x2e00
  17. %define FAT_SEG 0x2c0
  18. %define ROOT_SEG 0x2e0
  19.  
  20. ;*******************************************
  21. ; LoadRoot ()
  22. ;   - Load Root Directory Table to 0x7e00
  23. ;*******************************************
  24.  
  25. LoadRoot:
  26.  
  27.     pusha                           ; store registers
  28.     push    es
  29.  
  30.      ; compute size of root directory and store in "cx"
  31.      
  32.     xor     cx, cx                      ; clear registers
  33.     xor     dx, dx
  34.     mov     ax, 32                  ; 32 byte directory entry
  35.     mul     WORD [bpbRootEntries]               ; total size of directory
  36.     div     WORD [bpbBytesPerSector]            ; sectors used by directory
  37.     xchg    ax, cx                      ; move into AX
  38.  
  39.      ; compute location of root directory and store in "ax"
  40.      
  41.     mov     al, BYTE [bpbNumberOfFATs]          ; number of FATs
  42.     mul     WORD [bpbSectorsPerFAT]             ; sectors used by FATs
  43.     add     ax, WORD [bpbReservedSectors]
  44.     mov     WORD [datasector], ax               ; base of root directory
  45.     add     WORD [datasector], cx
  46.  
  47.      ; read root directory into 0x7e00
  48.  
  49.     push    word ROOT_SEG
  50.     pop     es
  51.     mov     bx, 0                               ; copy root dir
  52.     call    ReadSectors                         ; read in directory table
  53.     pop     es
  54.     popa                                        ; restore registers and return
  55.     ret
  56.  
  57. ;*******************************************
  58. ; LoadFAT ()
  59. ;   - Loads FAT table to 0x7c00
  60. ;
  61. ;   Parm/ ES:DI => Root Directory Table
  62. ;*******************************************
  63.  
  64. LoadFAT:
  65.  
  66.     pusha                           ; store registers
  67.     push    es
  68.  
  69.      ; compute size of FAT and store in "cx"
  70.      
  71.     xor     ax, ax
  72.     mov     al, BYTE [bpbNumberOfFATs]          ; number of FATs
  73.     mul     WORD [bpbSectorsPerFAT]             ; sectors used by FATs
  74.     mov     cx, ax
  75.  
  76.      ; compute location of FAT and store in "ax"
  77.  
  78.     mov     ax, WORD [bpbReservedSectors]
  79.  
  80.      ; read FAT into memory (Overwrite our bootloader at 0x7c00)
  81.  
  82.     push    word FAT_SEG
  83.     pop     es
  84.     xor     bx, bx
  85.     call    ReadSectors
  86.     pop     es
  87.     popa                            ; restore registers and return
  88.     ret
  89.    
  90. ;*******************************************
  91. ; FindFile ()
  92. ;   - Search for filename in root table
  93. ;
  94. ; parm/ DS:SI => File name
  95. ; ret/ AX => File index number in directory table. -1 if error
  96. ;*******************************************
  97.  
  98. FindFile:
  99.  
  100.     push    cx                      ; store registers
  101.     push    dx
  102.     push    bx
  103.     mov bx, si                      ; copy filename for later
  104.  
  105.      ; browse root directory for binary image
  106.  
  107.     mov     cx, WORD [bpbRootEntries]           ; load loop counter
  108.     mov     di, ROOT_OFFSET                     ; locate first root entry at 1 MB mark
  109.     cld                         ; clear direction flag
  110.  
  111. .LOOP:
  112.     push    cx
  113.     mov     cx, 11                  ; eleven character name. Image name is in SI
  114.     mov si, bx                      ; image name is in BX
  115.     push    di
  116.      rep  cmpsb                         ; test for entry match
  117.     pop     di
  118.     je      .Found
  119.     pop     cx
  120.     add     di, 32                  ; queue next directory entry
  121.     loop    .LOOP
  122.  
  123. .NotFound:
  124.     pop bx                      ; restore registers and return
  125.     pop dx
  126.     pop cx
  127.     mov ax, -1                      ; set error code
  128.     ret
  129.  
  130. .Found:
  131.     pop ax                      ; return value into AX contains entry of file
  132.     pop bx                      ; restore registers and return
  133.     pop dx
  134.     pop cx
  135.     ret
  136.  
  137. ;*******************************************
  138. ; LoadFile ()
  139. ;   - Load file
  140. ; parm/ ES:SI => File to load
  141. ; parm/ EBX:BP => Buffer to load file to
  142. ; ret/ AX => -1 on error, 0 on success
  143. ;*******************************************
  144.  
  145. LoadFile:
  146.  
  147.     xor     ecx, ecx                    ; size of file in sectors
  148.     push    ecx
  149.  
  150. .FIND_FILE:
  151.  
  152.     push    bx                      ; BX=>BP points to buffer to write to; store it for later
  153.     push    bp
  154.     call    FindFile                ; find our file. ES:SI contains our filename
  155.     cmp     ax, -1
  156.     jne     .LOAD_IMAGE_PRE
  157.     pop     bp
  158.     pop     bx
  159.     pop     ecx
  160.     mov     ax, -1
  161.     ret
  162.  
  163. .LOAD_IMAGE_PRE:
  164.  
  165.     sub     edi, ROOT_OFFSET
  166.     sub     eax, ROOT_OFFSET
  167.  
  168.     ; get starting cluster
  169.  
  170.     push    word ROOT_SEG               ;root segment loc
  171.     pop     es
  172.     mov     dx, WORD [es:di + 0x001A]   ; DI points to file entry in root directory table. Refrence the table...
  173.     mov     WORD [cluster], dx          ; file's first cluster
  174.     pop     bx                          ; get location to write to so we dont screw up the stack
  175.     pop     es
  176.     push    bx                          ; store location for later again
  177.     push    es
  178.     call    LoadFAT
  179.  
  180. .LOAD_IMAGE:
  181.  
  182.     ; load the cluster
  183.  
  184.     mov     ax, WORD [cluster]      ; cluster to read
  185.     pop     es                      ; bx:bp=es:bx
  186.     pop     bx
  187.     call    ClusterLBA
  188.     xor     cx, cx
  189.     mov     cl, BYTE [bpbSectorsPerCluster]
  190.  
  191.     call    ReadSectors
  192.  
  193.     pop     ecx
  194.     inc     ecx
  195.     push    ecx
  196.  
  197.     push    bx
  198.     push    es
  199.  
  200.     mov     ax, FAT_SEG                     ;start reading from fat
  201.     mov     es, ax
  202.     xor     bx, bx
  203.  
  204.     ; get next cluster
  205.  
  206.     mov     ax, WORD [cluster]              ; identify current cluster
  207.     mov     cx, ax                          ; copy current cluster
  208.     mov     dx, ax                          ; copy current cluster
  209.     shr     dx, 0x0001                      ; divide by two
  210.     add     cx, dx                          ; sum for (3/2)
  211.  
  212.     mov     bx, 0                           ;location of fat in memory
  213.     add     bx, cx
  214.     mov     dx, WORD [es:bx]
  215.     test    ax, 0x0001                      ; test for odd or even cluster
  216.     jnz     .ODD_CLUSTER
  217.  
  218. .EVEN_CLUSTER:
  219.  
  220.     and     dx, 0000111111111111b   ; take low 12 bits
  221.     jmp     .DONE
  222.  
  223. .ODD_CLUSTER:
  224.  
  225.     shr     dx, 0x0004              ; take high 12 bits
  226.  
  227. .DONE:
  228.  
  229.     mov     WORD [cluster], dx
  230.     cmp     dx, 0x0ff0              ; test for end of file marker
  231.     jb      .LOAD_IMAGE
  232.  
  233. .SUCCESS:
  234.     pop     es
  235.     pop     bx
  236.     pop     ecx
  237.     xor     ax, ax
  238.     ret
  239.  
  240. %endif      ;__FAT12_INC_67343546FDCC56AAB872_INCLUDED__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement