⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32.inc

📁 MenuetOS是一个用汇编开发的32/64位PC操作系统
💻 INC
📖 第 1 页 / 共 4 页
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                 ;;
;;  FAT32.INC                                                      ;;
;;                                                                 ;;
;;  FAT32 functions for MenuetOS                                   ;;
;;                                                                 ;;
;;  Copyright 2002 Paolo Minazzi, paolo.minazzi@inwind.it          ;;
;;                                                                 ;;
;;  See file COPYING for details                                   ;;
;;                                                                 ;;
;;  10.9.2004  Fat16 file system - Mario79                         ;;
;;  06.9.2004  Fix free space by Mario79 added - MH                ;;
;;  24.5.2004  Write back buffer for File_write - VT               ;;
;;  20.5.2004  File_read function to work with syscall 58 - VT     ;;
;;  30.3.2004  Error parameters at function return - VT            ;;
;;  01.5.2002  Bugfix in device write - VT                         ;;
;;  20.5.2002  Hd status check - VT                                ;;
;;  29.6.2002  Improved fat32 verification - VT                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;       CLUSTER[0]  CLUSTER[1]  CLUSTER[2] CLUSTER[3]
;
;###    ######## -->######## -->########   ########
;#2#--> #      # |  #      # |  #      #   #      #
;###    #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #          CLUSTER
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       ######## |  ######## |  ########   ########
;                |           |
;                |           ------------------
;                |                            |
;                --------------------         |
;                                   |         |
;      ########    ########    ########   ########
;      # xxxx #    # xxxx #    #   3  #   #   4  #
;      ########    ########    ########   ########          FAT
;
;       FAT[0]       FAT[1]      FAT[2]    FAT[3]
;

cache_max equ 1000

RESERVED_SECTOR:     dd 0x20
PARTITION_START:     dd 0x3f
FAT32_SIZE:          dd 0x1f3a
NUMBER_OF_FATS:      dd 0x2
SECTOR_PER_CLUSTER:  dd 0x8
ROOT_CLUSTER:        dd 2
buffer:              times 64 dd 0,0
cluster:             dd 0
cluster1:            dd 0
valid_fat:           db 0
PARTITION_SIZE:      dd 0
PARTITION_END:       dd 0

hd_error             dd 0
hd_setup             dd 0

;***********************
;*         Start       *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.1      *
;***********************
FILE_SIZE:           dd 0
ADR_FSINFO:          dd 0
change_mem           db 0
old_size_file:       dd 0
;***********************
;*         End         *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.1      *
;***********************

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.1      *
;***********************
fs_type              dd 0
file_or_directory    dd 0
analyze_f16          db 0
correct_fat16        dd 0
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.1      *
;***********************

reserve_hd1:

    push  eax
    mov   eax,[0x3000]
    imul  eax,32
    add   eax,0x3000
    mov   eax,[eax+4]
    mov   [hd1_status],eax
    pop   eax

    ret

clear_hd_cache:

    pusha
    mov   edi,0x600000
    mov   ecx,65535
    mov   eax,0
    cld
    rep   stosb
    popa

    ret


set_FAT32_variables:

    cli
    cmp   [hd1_status],0
    je    set_fat32_l1

    sti
    call  change_task
    jmp   set_FAT32_variables

  set_fat32_l1:

    call  reserve_hd1

    sti

    call  clear_hd_cache

    cmp   dword [hdpos],0
    je    problem_hd_or_partition_or_fat32_ret

    push  eax
    push  ebx

    xor   eax,eax                        ; Read MBR
    mov   ebx,buffer
    call  hd_read

    cmp   dword [fat32part],0
    jnz   hd_and_partition_ok

problem_hd_or_partition_or_fat32:

    pop   ebx
    pop   eax

problem_hd_or_partition_or_fat32_ret:

    mov   [valid_fat],byte 0
    mov   [hd1_status],0 ; free

    ret

hd_and_partition_ok:

    mov   ecx,[fat32part]
    mov   eax,0
    mov   ebx,buffer+0x1be+8  -16
  hdapl1:
    add   ebx,16
    add   eax,[ebx]
    loop  hdapl1

    mov   [PARTITION_START],eax
    mov   ebx,[ebx+4]
    mov   [PARTITION_SIZE],ebx

    mov   [hd_setup],1
    mov   ebx,buffer
    call  hd_read ;_new        ; Read boot sector of partition
    mov   [hd_setup],0

    cmp   [ebx+0x53],dword 'AT32'
    ; jnz  problem_hd_or_partition_or_fat32
    jnz   problem_hd_or_partition_or_fat32_1

    mov   [valid_fat],byte 1
    movzx eax,word [ebx+0xe]
    mov   [RESERVED_SECTOR],eax
    mov   eax,[ebx+0x24]
    mov   [FAT32_SIZE],eax
    movzx eax,byte [ebx+0x10]
    mov   [NUMBER_OF_FATS],eax
    movzx eax,byte [ebx+0xd]
    mov   [SECTOR_PER_CLUSTER],eax
    mov   eax,[ebx+44]
    mov   [ROOT_CLUSTER],eax
    mov   eax,[PARTITION_START]
    add   eax,[PARTITION_SIZE]
    dec   eax
    mov   [PARTITION_END],eax
    pop   ebx
    pop   eax

;    mov [hd1_status],0   ; free
;    ret
;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.2      *
;***********************

    mov   [fs_type],1      ; 1 -Fat32
    mov   [hd1_status],0   ; free
    ret

problem_hd_or_partition_or_fat32_1:

    cmp   [ebx+55],dword 'AT16'
    je    par_fat16
    jmp   problem_hd_or_partition_or_fat32

par_fat16:

    mov   [valid_fat],byte 1
    movzx eax,word [ebx+0xe]
    mov   [RESERVED_SECTOR],eax
    movzx eax,word [ebx+0x16]
    mov   [FAT32_SIZE],eax
    movzx eax,byte [ebx+0x10]
    mov   [NUMBER_OF_FATS],eax
    movzx eax,byte [ebx+0xd]
    mov   [SECTOR_PER_CLUSTER],eax

    xor   eax,eax
    mov   [ROOT_CLUSTER],eax
    mov   eax,[PARTITION_START]
    add   eax,[PARTITION_SIZE]
    dec   eax
    mov   [PARTITION_END],eax

    mov   eax,[SECTOR_PER_CLUSTER]
    shl   eax,1
    mov   ebx,32
    sub   ebx,eax
    mov   [correct_fat16],ebx

    pop   ebx
    pop   eax

    mov   [fs_type],2      ; 2 - Fat16
    mov   [hd1_status],0   ; Free
    ret

;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.2      *
;***********************

set_FAT:
;--------------------------------
; set_FAT
;
; input :  EAX = #block
;          EDX = value to save
; output:  nothing
;--------------------------------

    push  eax  ; [esp+16]
    push  ebx  ; [esp+12]
    push  ecx  ; [esp+8]
    push  edx  ; [esp+4]
    push  esi  ; [esp]

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.3      *
;***********************

    cmp [fs_type],1
    je correct3_11
    shl eax,1
    jmp  correct3_12
correct3_11:
    shl eax,2
correct3_12:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.3      *
;***********************

    xor edx,edx
    mov ebx,512
    div ebx
    mov esi,edx

    add eax,[RESERVED_SECTOR]
    add eax,[PARTITION_START]
    mov ebx,buffer

    mov ecx,[NUMBER_OF_FATS]
    mov edx,[esp+4]

set_an_other_FAT:

    call hd_read ;_new

    cmp  [f_del],1
    je   fat_zero

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.4      *
;***********************

    cmp [fs_type],1
    je correct3_11_1
    cmp  [buffer+esi],word 0
    je   fat_zero
    jmp  correct3_12_1
correct3_11_1:
   cmp  [buffer+esi],dword 0
   je   fat_zero
correct3_12_1:
   mov  [8*0x100000],dword 0xffffff
   jmp  fat_nonzero
fat_zero:
  cmp [fs_type],1
  je correct3_13_3
   mov [buffer+esi],dx
   jmp correct3_13_4
correct3_13_3:
   mov [buffer+esi],edx
correct3_13_4:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.4      *
;***********************

   call hd_write_new

  fat_nonzero:

   add eax,[FAT32_SIZE]
   loop set_an_other_FAT

   pop esi
   pop edx
   pop ecx
   pop ebx
   pop eax
   ret


get_FAT:
;--------------------------------
; get_FAT
;
; input :  EAX = #block
; output:  EAX = #block
;--------------------------------
   push ebx
   push edx

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.5      *
;***********************
  cmp [fs_type],1
  je correct3_13_5
  shl eax,1
  jmp  correct3_14_1
correct3_13_5:
   shl eax,2
correct3_14_1:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.5      *
;***********************

   xor edx,edx
   mov ebx,512
   div ebx

   add eax,[RESERVED_SECTOR]
   add eax,[PARTITION_START]
   mov ebx,buffer

   call hd_read ;_new

   mov  eax,[buffer+edx]

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.6      *
;***********************
  cmp [fs_type],1
  je correct3_13_6
  and  eax,0xffff
correct3_13_6:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.6      *
;***********************

   pop edx
   pop ebx
   ret


get_FAT_cache:
;--------------------------------
; get_FAT_cache - used for reading only : internal cache
;
; input :  EAX = #block
; output:  EAX = #block
;--------------------------------
   push ebx
   push edx

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.7      *
;***********************
  cmp [fs_type],1
  je correct3_13
  shl eax,1
  jmp  correct3_14
correct3_13:
   shl eax,2
correct3_14:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.7      *
;***********************

   xor edx,edx
   mov ebx,512
   div ebx

   add eax,[RESERVED_SECTOR]
   add eax,[PARTITION_START]
   mov ebx,fat_cache

   cmp  eax,[fat_in_cache]
   je   fat_found_in_cache
   mov  [fat_in_cache],eax
   call hd_read
 fat_found_in_cache:

   mov  eax,[fat_cache+edx]

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.8      *
;***********************
  cmp [fs_type],1
  je correct3_13_1
  and  eax,0xffff
correct3_13_1:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.8      *
;***********************

   pop edx
   pop ebx
   ret


fat_in_cache dd -1
fat_cache: times 512 db 0



;find_pointer_in_FAT:
;--------------------------------
; For each item of FAT do
;   if (item read == EAX)
;     return #item
;     carry=0
;   else
;     carry=1;
;
; input :  EAX = #block
; output:  if CARRY=0 then EAX = #block
;          if CARRY=1 then EAX = distroyed
;--------------------------------
;  add eax,2
;  push eax
;  push ecx
;  push edx

;  mov ecx,[FAT32_SIZE]
;  shl ecx,9-2                 ; *512 for sector   /4 for dword
;  mov edx,2

;adr78:
;  mov eax,edx
;  call get_FAT
;  cmp eax,[esp+8]
;  je found_pointer
;  inc edx
;  loop adr78

;  pop edx
;  pop ecx
;  add esp,4
;  stc
;  ret

;found_pointer:
;  mov eax,edx
;  pop edx
;  pop ecx
;  add esp,4
;  clc
;  ret

analyze_directory:
;--------------------------------
; input  : EAX = first cluster of the directory
;          EBX = pointer to filename
; output : IF CARRY=0 EAX = sector where th file is found
;                     EBX = pointer in buffer
;                     [buffer .. buffer+511]
;                     ECX,EDX,EDI,EDI not changed
;          IF CARRY=1
;--------------------------------
   push ebx ;[esp+16]
   push ecx
   push edx
   push esi
   push edi


adr56:
   mov [cluster_tmp],eax

   mov ecx,[FAT32_SIZE]
   imul ecx,[NUMBER_OF_FATS]
   add ecx,[RESERVED_SECTOR]

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.9      *
;***********************
  cmp [fs_type],2
  je correct2_11
  sub  eax,2
correct2_11:
   imul eax,[SECTOR_PER_CLUSTER]
  cmp [fs_type],1
  je correct2_13
  cmp [file_or_directory],0
  je  correct2_13
  cmp [analyze_f16],0
  je  correct2_13
  add eax,[correct_fat16]
correct2_13:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.9      *
;***********************

   add eax,[PARTITION_START]
   add eax,ecx

   mov edx,[SECTOR_PER_CLUSTER]

new_sector_in_cluster:
   mov ebx,buffer
   call hd_read ;_new

   mov ecx,512/32

adr1_analyze:

   mov edi,[ebx+11]
   and edi,0xf
   cmp edi,0xf
   je  long_filename_data

   mov esi,[esp+16]
   mov edi,ebx
   cld
   push ecx
   mov ecx,11
   rep cmpsb
   pop ecx
   je found_file_analyze

 long_filename_data:

   add ebx,32
   loop adr1_analyze

   inc eax
   dec edx
   jne new_sector_in_cluster

   mov eax,[cluster_tmp]
   call get_FAT
   mov [cluster_tmp],eax

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.10     *
;***********************
  cmp [fs_type],2
  je  correct5_2
   cmp eax,0x0f000000
   jb  adr56
 correct6_2:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -