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

📄 fat32.inc

📁 Menuet 操作系统源代码。 非常难得的东西
💻 INC
📖 第 1 页 / 共 2 页
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                                 ;;
;;  FAT32.INC                                                      ;;
;;                                                                 ;;
;;  FAT32 functions for MenuetOS                                   ;;
;;                                                                 ;;
;;  Copyright 2002 Paolo Minazzi, paolo.minazzi@inwind.it          ;;
;;                                                                 ;;
;;  See file COPYING for details                                   ;;
;;                                                                 ;;
;;                                                                 ;;
;;  1.5.2002  bugfix in device write , vt                          ;;
;; 20.5.2002  hd status check, vt                                  ;;
;; 29.6.2002  improved fat32 verification, 'valid_fat'             ;;
;;                                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   
   
   
   
;       CLUSTER[0]  CLUSTER[1]  CLUSTER[2] CLUSTER[3]
;
;###    ######## -->######## -->########   ########
;#2#--> #      # |  #      # |  #      #   #      #
;###    #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #          CLUSTER
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       #      # |  #      # |  #      #   #      #
;       ######## |  ######## |  ########   ########
;                |           |
;                |           ------------------
;                |                            |
;                --------------------         |
;                                   |         |
;      ########    ########    ########   ########
;      # xxxx #    # xxxx #    #   3  #   #   4  #
;      ########    ########    ########   ########          FAT
;
;       FAT[0]       FAT[1]      FAT[2]    FAT[3]
;
   
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 ; resb 512
cluster:             dd 0
cluster1:            dd 0
valid_fat:           db 0
PARTITION_SIZE:      dd 0
PARTITION_END:       dd 0
   
set_FAT32_variables:
   
    cli
    cmp [hd1_status],0
    je  set_fat32_l1
   
    sti
    call change_task
    jmp  set_FAT32_variables
   
  set_fat32_l1:
   
    mov [hd1_status],1  ; busy
    sti
   
   
    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_new
   
    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]
    xor  eax,eax
    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 ebx,buffer
    call hd_read_new                       ;read boot sector of partition
   
    cmp [ebx+0x53],dword 'AT32'
    jnz  problem_hd_or_partition_or_fat32
   
    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
   
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]
   
   shl eax,2
   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
   
   cmp  [buffer+esi],dword 0
   je   fat_zero
   mov  [8*0x100000],dword 0xffffff
   jmp  fat_nonzero
   
  fat_zero:
   
   mov [buffer+esi],edx
   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
   
   shl eax,2
   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]
   
   pop edx
   pop ebx
   ret
   
;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]
   
   sub eax,2
   imul eax,[SECTOR_PER_CLUSTER]
   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 esi,[esp+16]
   mov edi,ebx
   cld
   push ecx
   mov ecx,11
   rep cmpsb
   pop ecx
   je found_file_analyze
   
   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
   cmp eax,0x0f000000
   jb  adr56
   
   
   pop edi
   pop esi
   pop edx
   pop ecx
   add esp,4
   stc        ;file not found
   ret
   
found_file_analyze:
   pop edi
   pop esi
   pop edx
   pop ecx
   add esp,4
   clc        ;file found
   ret
   
cluster_tmp: dd 0 ; common variable used by
                  ; analyze_directory and analyze_directory_to_write
cluster_tmp2: dd 0
   
   
analyze_directory_to_write:
;--------------------------------
; input  : EAX = first cluster of the directory
; output : IF CARRY=0 EAX = sector where the file is found
;                     EBX = pointer in buffer
;                     [buffer .. buffer+511]
;                     ECX,EDX,EDI,EDI not changed
;          IF CARRY=1
;--------------------------------
   
   push ecx
   push edx
   push esi
   push edi
   
adr561:
   mov [cluster_tmp],eax
   mov [cluster_tmp2],eax
   
   mov ecx,[FAT32_SIZE]
   imul ecx,[NUMBER_OF_FATS]
   add ecx,[RESERVED_SECTOR]
   
   sub eax,2
   imul eax,[SECTOR_PER_CLUSTER]
   add eax,[PARTITION_START]
   add eax,ecx
   
   mov edx,[SECTOR_PER_CLUSTER]
   
new_sector_in_cluster1:
   mov ebx,buffer
   call hd_read_new
   
   mov ecx,512/32
   
adr1_analyze1:
   cmp byte [ebx],0x00
   je  found_file_analyze1
   cmp byte [ebx],0xe5
   je  found_file_analyze1
   
avanti:
   add ebx,32
   loop adr1_analyze1
   
   inc eax
   dec edx
   jne new_sector_in_cluster1
   
   mov eax,[cluster_tmp]
   call get_FAT
   mov [cluster_tmp],eax
   cmp eax,0x0f000000
   jb  adr561
   
   mov eax,2                       ;this block of code add a new cluster
   call get_free_FAT               ;for the directory because the directory
                                   ;is full
   
   mov edx,0x0fffffff
   call set_FAT
   
   mov edx,eax
   mov eax,[cluster_tmp2]
   call set_FAT
   
   mov ecx,512/4
   xor eax,eax
   mov edi,buffer
   cld
   rep stosd
   
   sub edx,2
   
   mov eax,[SECTOR_PER_CLUSTER]
   imul eax,edx
   add eax,[PARTITION_START]
   add eax,[RESERVED_SECTOR]
   mov ecx,[FAT32_SIZE]
   imul ecx,[NUMBER_OF_FATS]
   add eax,ecx
   mov ebx,buffer
   mov ecx,[SECTOR_PER_CLUSTER]
   
set_empty_directory:
   call hd_write_new
   inc eax
   loop set_empty_directory
   
   sub eax,[SECTOR_PER_CLUSTER]
   
found_file_analyze1:
   
   pop edi
   pop esi
   pop edx
   pop ecx
   clc        ;file found
   ret
   
get_data_cluster:
;--------------------------------
; input :  EAX = # block
;          EBX = pointer to buffer
;          ECX = # bytes to read in buffer
; output:  nothing
;--------------------------------
ORG_EAX equ [esp+20]
ORG_EBX equ [esp+16]
ORG_ECX equ [esp+12]
ORG_EDX equ [esp+8]
ORG_ESI equ [esp+4]
ORG_EDI equ [esp]
   
   push  eax ; ORG_EAX
   push  ebx ; ORG_EBX
   push  ecx ; ORG_ECX
   push  edx ; ORG_EDX
   push  esi ; ORG_ESI
   push  edi ; ORG_EDI
   
   mov   edi,ebx
   
   xor   edx,edx
   mov   eax,ecx
   mov   ebx,512
   div   ebx
   
   mov   ecx,[FAT32_SIZE]
   imul  ecx,[NUMBER_OF_FATS]
   add   ecx,[RESERVED_SECTOR]
   
   mov   ebx,ORG_EAX
   sub   ebx,2
   imul  ebx,[SECTOR_PER_CLUSTER]
   add   ebx,[PARTITION_START]
   add   ebx,ecx
   
   mov ecx,eax
   
   mov eax,ebx
   mov ebx,buffer
   jecxz not_whole
   
read:
   call hd_read_new
   
   mov esi,ebx
   cld
   push ecx
   mov ecx,512
   rep movsb
   pop ecx
   
   inc eax
   loop read
   
not_whole:
   call hd_read_new
   mov esi,ebx
   mov ecx,edx
   cld
   rep movsb
   
   pop edi
   pop esi
   pop edx
   pop ecx
   pop ebx
   pop eax
   ret
   
set_data_cluster:
;--------------------------------
; input :  EAX = # block
;          EBX = pointer to buffer
;          ECX = # bytes to write in buffer
; output:  nothing
;--------------------------------
   push eax ; ORG_EAX
   push ebx ; ORG_EBX
   push ecx ; ORG_ECX
   push edx ; ORG_EDX
   push esi ; ORG_ESI
   push edi ; ORG_EDI
   
   mov  esi,ebx
   
   xor  edx,edx
   mov  eax,ecx
   mov  ebx,512
   div  ebx
   
   mov  ecx,[FAT32_SIZE]
   imul ecx,[NUMBER_OF_FATS]
   add  ecx,[RESERVED_SECTOR]
   
   mov  ebx,ORG_EAX
   sub  ebx,2
   imul ebx,[SECTOR_PER_CLUSTER]
   add  ebx,[PARTITION_START]
   add  ebx,ecx
   
   mov  ecx,[SECTOR_PER_CLUSTER]    ; vt
   
   mov  eax,ebx
   mov  ebx,buffer
   
write:
   push ecx
   mov  edi,buffer
   mov  ecx,512
   cld
   rep  movsb
   call hd_write_new
   inc  eax
   pop  ecx
   loop write
   
   pop  edi
   pop  esi
   pop  edx
   pop  ecx
   pop  ebx
   pop  eax
   ret
   
get_free_FAT:
;------------------------------------------
; input  :  EAX = # cluster for start the searching
; output :  EAX = # first cluster found free
;-------------------------------------------
   push ebx
   push ecx
   push edx
   
   mov ecx,eax
   
   shl eax,2
   xor edx,edx
   mov ebx,512
   div ebx
   
   add eax,[RESERVED_SECTOR]
   add eax,[PARTITION_START]
   mov ebx,buffer
   call hd_read_new
   
check_new:
   
   cmp  dword [buffer+edx],0
   jne  no_found
   cmp  dword [buffer+edx+4],0
   jne  no_found
   jmp  found
  no_found:
   add  edx,4

⌨️ 快捷键说明

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