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

📄 fat32.inc

📁 MenuetOS是一个用汇编开发的32/64位PC操作系统
💻 INC
📖 第 1 页 / 共 4 页
字号:
                or dx,ax
                xchg ax,dx   ;the result must be in AX
                pop dx
                ret

;-----------------------------------------------------
; Get time from CMOS and pack hour,minute,second in AX
; TIME   bits  0..4   : second (the low bit is lost)
;              5..10  : minute 0..59
;              11..15 : hour 0..23
;-----------------------------------------------------
get_time_for_file:
                push dx
                xor dx,dx
                mov al,0x0   ;second
                out 0x70,al
                in al,0x71
                call bcd2bin
                shr ax,1
                or dx,ax

                mov al,0x2   ;minute
                out 0x70,al
                in al,0x71
                call bcd2bin
                shl ax,5
                or dx,ax

                mov al,0x4   ;hour
                out 0x70,al
                in al,0x71
                call bcd2bin
                shl ax,11
                or dx,ax
                xchg ax,dx   ;the result must be in AX
                pop dx
                ret

file_write:
;--------------------------------------------------------------------------
;   INPUT :user-reg  register-in-this  meaning      symbol-in-this-routine
;
;            EAX        EDI            system call to write    /
;            EBX        EAX   (PAR0)   pointer to file-name    PAR0
;            EDX        ECX   (PAR1)   pointer to buffer       PAR1
;            ECX        EBX   (PAR2)   file size               PAR2
;            ESI        EDX   (PAR3)   pointer to path         PAR3
;
;   OUTPUT : eax = 0 - ok
;            eax = 1 - error at write
;            eax = 2 - bad partition
;
;--------------------------------------------------------------------------

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.19     *
;***********************
  mov  [file_or_directory],1
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.19     *
;***********************

;***********************
;*         Start       *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.2      *
;***********************
    mov [FILE_SIZE],ebx
;***********************
;*         End         *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.2      *
;***********************

    cmp  [valid_fat],byte 1
    jz   fat_ok_for_writing
    mov  eax,2
    ret
fat_ok_for_writing:

   cli
   cmp  [hd1_status],0
   je   fatokl1

   sti
   call change_task
   jmp  fat_ok_for_writing

  fatokl1:

   call reserve_hd1
   sti

   sub  esp,32
   call expand_filename

   pusha
   mov ecx,edx            ;try to delete the file first
   call file_delete
   popa

   pushad

PAR0 equ [esp+28]        ;EAX
PAR1 equ [esp+24]        ;ECX
PAR2 equ [esp+16]        ;EBX
PAR3 equ [esp+20]        ;EDX


   mov ebx,PAR3
   call get_cluster_of_a_path
   jnc found_directory_for_writing

exit_writing_with_error:

   mov   edi,1            ; write all of cache to hd
   call  write_cache

   popa
   stc

   add  esp,32
   mov  [hd1_status],0
   mov  eax,1
   ret

found_directory_for_writing:
   call analyze_directory_to_write

found1:
   push eax

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.20     *
;***********************
  xor eax,eax
  cmp [fs_type],2
  je correct3_15_1
   mov eax,2
correct3_15_1:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.20     *
;***********************

   call get_free_FAT
   mov [cluster],eax
   pop eax

   push ebx
   mov ebx,buffer
   call hd_read ;_new
   pop ebx

   mov esi,PAR0             ;file name
   mov edi,ebx
   mov ecx,11
   cld
   rep movsb

   mov ecx,PAR2
   mov [ebx+28],ecx         ;file size
   mov ecx,[cluster]
   mov [ebx+26],cx          ;16 bits low  of cluster
   shr ecx,16
   mov [ebx+20],cx          ;16 bits high of cluster
   mov byte [ebx+11],0x20   ;attrib
   push eax

   call get_time_for_file
   mov [ebx+22],ax

   call get_data_for_file
   mov [ebx+24],ax

   pop eax

   mov ebx,buffer           ;save the directory name,length,cluster
   call hd_write_new

   imul esi,[SECTOR_PER_CLUSTER],512

hd_new_block_write:

   mov  eax,[cluster]      ; eax = block
   mov  ebx,PAR1           ; ebx = buffer
   call set_data_cluster

   mov  ecx,esi            ; ecx = size to write in bytes
   cmp  ecx,PAR2
   jbe  adr3
   mov  ecx,PAR2
 adr3:

   add  PAR1,ecx
   sub  PAR2,ecx            ; sub wrote bytes in stack

   cmp  PAR2,dword 0
   je   file_saved_OK

   mov  eax,[cluster]
   inc  eax
   call get_free_FAT
   mov  [cluster1],eax      ; next free in FAT

   mov  eax,[cluster]
   mov  edx,[cluster1]
   call set_FAT

   mov  eax,[cluster1]
   mov  [cluster],eax

   jmp  hd_new_block_write     ; adr2

file_saved_OK:

   mov  eax,[cluster]
   mov  edx,0x0fffffff
   call set_FAT

   mov   edi,1            ; write all of cache to hd
   call  write_cache

   popad
   clc

   add  esp,32
;   mov  [hd1_status],0   ; free device
;   mov  eax,0
;   ret
;***********************
;*         Start       *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.3      *
;***********************
   cmp [fs_type],2          ;For Fat16 the correction is not necessary!
   je correct10_11
   call write_free_mem_hd   
correct10_11:
   mov  [hd1_status],0
   xor  eax, eax
   ret

write_free_mem_hd:
   mov  eax,[PARTITION_START]
   mov  ebx,buffer
   call hd_read ;_new
   movzx  ecx,word [ebx+48]
   add  eax,ecx
   mov  [ADR_FSINFO],eax
   call hd_read ;_new
   mov  eax,[FILE_SIZE]
   call  calc_file_size_claster
   mov  [FILE_SIZE],eax
   cmp  [change_mem],1
   je change_mem_1
   cmp  [change_mem],2
   je change_mem_2
   jmp change_mem_3
change_mem_2:
   mov  eax,[old_size_file]
   call  calc_file_size_claster
   add  [buffer+488],eax
change_mem_1:
   mov  eax,[FILE_SIZE]
   sub  [buffer+488],eax
   mov  eax,[ADR_FSINFO]
   mov  ebx,buffer
   call hd_write_new_sys
   call wait_for_hd_idle
change_mem_3:
   ret
calc_file_size_claster:
   mov  ebx,[SECTOR_PER_CLUSTER]
   call correct_calc
   mov  ebx,512
   call correct_calc
   ret
correct_calc:
   xor  edx,edx
   div  ebx
   cmp edx,0
   je not_correction
   inc eax
not_correction:
   ret
;***********************
;*          End        *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.3      *
;***********************

file_read:
;--------------------------------------------------------------------------
;   INPUT :  user-register register-in-this  meaning         symbol-in-this
;
;            EAX           EDI               system call to write   /
;            EBX           EAX   (PAR0)      pointer to file-name   PAR0
;            EDX           ECX   (PAR1)      pointer to buffer      PAR1
;            ECX           EBX   (PAR2)   vt file blocks to read    PAR2
;            ESI           EDX   (PAR3)      pointer to path        PAR3
;            EDI           ESI            vt first 512 block to read
;                          EDI               if 0 - read root
;
; OUT:
;
; eax = 0 command succesful
;       1 no hd base and/or partition defined
;       - 2 yet unsupported FS
;       - 3 unknown FS
;       - 4 partition not defined at hd
;       5 eof
;       6 file not found
; ebx = size of file
;--------------------------------------------------------------------------
   cmp  [valid_fat],byte 1
   jz   fat_ok_for_reading
   mov  ebx,0
   mov  eax,1 ; partition not defined
   ret

fat_ok_for_reading:

   cli
   cmp  [hd1_status],0
   je   fatokr1
   sti
   call change_task
   jmp  fat_ok_for_reading

  fatokr1:

   call reserve_hd1
   sti

   mov  [data_read],esi
   mov  [blocks_to_read],ebx
   mov  [return_data_pointer],ecx
   mov  [fat_in_cache],-1

   pushad

PAR0 equ [esp+28]        ;EAX
PAR1 equ [esp+24]        ;ECX
PAR2 equ [esp+16]        ;EBX
PAR3 equ [esp+20]        ;EDX

   mov ebx,PAR3
   call get_cluster_of_a_path
   jc file_to_read_not_found

   cmp  edi,0            ; read root
   jne  no_read_root

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

  mov eax,[ROOT_CLUSTER]
  cmp [fs_type],2
  je read_root_f16
  jmp read_root
read_root_f16:
  mov [file_or_directory],0
  jmp read_root
no_read_root:
  mov [file_or_directory],1

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

   mov ebx,PAR0
   call analyze_directory
   jc file_to_read_not_found

found1r:

   mov eax,[ebx+28]  ; file size
   mov [file_size],eax

   mov ax,[ebx+20]   ; FAT entry
   shl eax,16
   mov ax,[ebx+26]

read_root:

   imul esi,[SECTOR_PER_CLUSTER],512

   call get_data_cluster

adr2r:

   cmp [blocks_to_read],0
   je  file_read_OK

   call get_FAT_cache

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.22     *
;***********************
  cmp [fs_type],2
  je  correct5_4
   cmp  eax,0x0f000000   ; end of file
   jge  file_read_eof
   cmp  eax,0x00000000   ; incorrect fat chain
   je   file_read_eof
 correct6_4:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.22     *
;***********************

   call get_data_cluster

   jmp adr2r

file_read_OK:
   popad
   mov  [hd1_status],0
   mov  ebx,[file_size]
   mov  eax,0
   ret

file_read_eof:
   popad
   mov  [hd1_status],0
   mov  ebx,[file_size]
   mov  eax,5
   ret

file_to_read_not_found:
   popad
   mov  [hd1_status],0
   mov  ebx,0
   mov  eax,6
   ret

; OUT:
;
; eax = 0 - Command succesful
;       1 - No hd base and/or partition defined
;       2 - Unsupported FS
;       3 - Unknown FS
;       4 - Partition not defined at hd
;       5 - Eof ?
;       6 - File not found ?
; ebx = size of file


f_del db 0x0

file_delete:
;--------------------------------------------------------------------------
;EXPERIMENTAL !!!!!
;      INPUT :  user-reg  register-in-this meaning           symbol-in-this
;               EAX       EDI              system call to delete   /
;               EBX       EAX     (PAR0)   pointer to file-name    PAR0
;               EDX       ECX     (PAR1)   pointer to path         PAR1
;--------------------------------------------------------------------------

    cmp  [valid_fat],byte 1
    jz   fat_ok_for_deleting
    ret
fat_ok_for_deleting:

   pushad

   mov [f_del],1           ; delete on

PAR0 equ [esp+28] ;EAX
PAR1 equ [esp+24] ;ECX

   mov ebx,PAR1
   call get_cluster_of_a_path
   jc file_to_delete_not_found

   mov ebx,PAR0
   call analyze_directory
   jc file_to_delete_not_found

found_delete:

   mov byte [ebx],0xe5

;***********************
;*        Start        *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.4      *
;***********************
   mov edx,[ebx+28]
   mov [old_size_file],edx
;***********************
;*          End        *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.4      *
;***********************

   mov edx,ebx
   mov ebx,buffer
   call hd_write_new

   mov ebx,edx

   mov ax,[ebx+20]
   shl eax,16
   mov ax,[ebx+26]
   xor edx,edx

clean_new_chain:
   mov ebx,eax

   call get_FAT
   mov ecx,eax

   mov eax,ebx
   call set_FAT

   mov eax,ecx

;***********************
;*         Start       *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.23     *
;***********************
  cmp [fs_type],2
  je  correct5_3
   cmp eax,0x0f000000  ; end of file
   jge delete_OK
   cmp eax,0x00000000  ; unfinished fat chain
   je  delete_OK
 correct6_3:
;***********************
;*         End         *
;* Mario79 edited code *
;*        Fat16        *
;*     change 1.23     *
;***********************

   jmp clean_new_chain

delete_OK:

   popad
   clc
   mov [f_del],0
;   ret
;
;file_to_delete_not_found:
;
;   popad
;   mov  [f_del],0
;   stc
;   ret

;***********************
;*        Start        *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.5      *
;***********************
   mov [change_mem],2
   ret

file_to_delete_not_found:

   popad
   mov  [f_del],0
   stc
   mov [change_mem],1
   ret
;***********************
;*          End        *
;* Mario79 edited code *
;*  correct free mem   *
;*     change 1.5      *
;***********************


;**************************************************************************
;
;   0x600008  - first entry in cache list
;
;            +0   - lba cluster
;            +4   - state of cache cluster
;                   0 = empty
;                   1 = used for read  ( same as in hd )
;                   2 = used for write ( differs from hd )
;
;      +65536 - cache entries
;
;**************************************************************************

hd_read:

;  eax block to read
;  ebx destination

    pusha                 ; scan cache

    mov  ecx,cache_max    ; entries in cache
    mov  esi,0x600000+8
    mov  edi,1

  hdreadcache:

    cmp  [esi+4],dword 0  ; empty
    je   nohdcache

    cmp  [esi],eax        ; correct cluster
    jne  nohdcache

    mov  esi,edi
    shl  esi,9
    add  esi,0x600000+65536
    mov  edi,ebx
    mov  ecx,512
    cld
    rep  movsb
    popa

⌨️ 快捷键说明

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