Subversion Repositories Kolibri OS

Rev

Rev 837 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  FAT32.INC                                                      ;;
  7. ;;                                                                 ;;
  8. ;;  FAT16/32 functions for KolibriOS                               ;;
  9. ;;                                                                 ;;
  10. ;;  Copyright 2002 Paolo Minazzi, paolo.minazzi@inwind.it          ;;
  11. ;;                                                                 ;;
  12. ;;  See file COPYING for details                                   ;;
  13. ;;  04.02.2007 LFN create folder - diamond                         ;;
  14. ;;  08.10.2006 LFN delete file/folder - diamond                    ;;
  15. ;;  20.08.2006 LFN set file size (truncate/extend) - diamond       ;;
  16. ;;  17.08.2006 LFN write/append to file - diamond                  ;;
  17. ;;  23.06.2006 LFN start application - diamond                     ;;
  18. ;;  15.06.2006 LFN get/set file/folder info - diamond              ;;
  19. ;;  27.05.2006 LFN create/rewrite file - diamond                   ;;
  20. ;;  04.05.2006 LFN read folder - diamond                           ;;
  21. ;;  29.04.2006 Elimination of hangup after the                     ;;
  22. ;;             expiration hd_wait_timeout -  Mario79               ;;
  23. ;;  23.04.2006 LFN read file - diamond                             ;;
  24. ;;  28.01.2006 find all Fat16/32 partition in all input point      ;;
  25. ;;             to MBR, see file part_set.inc - Mario79             ;;
  26. ;;  15.01.2005 get file size/attr/date, file_append - ATV          ;;
  27. ;;  04.12.2004 skip volume label, file delete bug fixed - ATV      ;;
  28. ;;  29.11.2004 get_free_FAT changed, append dir bug fixed - ATV    ;;
  29. ;;  23.11.2004 don't allow overwrite dir with file - ATV           ;;
  30. ;;  18.11.2004 get_disk_info and more error codes - ATV            ;;
  31. ;;  17.11.2004 set_FAT/get_FAT and disk cache rewritten - ATV      ;;
  32. ;;  10.11.2004 removedir clear whole directory structure - ATV     ;;
  33. ;;  08.11.2004 rename - ATV                                        ;;
  34. ;;  30.10.2004 file_read return also dirsize in bytes - ATV        ;;
  35. ;;  20.10.2004 Makedir/Removedir - ATV                             ;;
  36. ;;  14.10.2004 Partition chain/Fat16 - ATV (thanks drh3xx)         ;;
  37. ;;  06.9.2004  Fix free space by Mario79 added - MH                ;;
  38. ;;  24.5.2004  Write back buffer for File_write -VT                ;;
  39. ;;  20.5.2004  File_read function to work with syscall 58 - VT     ;;
  40. ;;  30.3.2004  Error parameters at function return - VT            ;;
  41. ;;  01.5.2002  Bugfix in device write - VT                         ;;
  42. ;;  20.5.2002  Hd status check - VT                                ;;
  43. ;;  29.6.2002  Improved fat32 verification - VT                    ;;
  44. ;;                                                                 ;;
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46.  
  47. $Revision: 848 $
  48.  
  49.  
  50. cache_max equ 1919      ; max. is 1919*512+0x610000=0x6ffe00
  51.  
  52. ERROR_SUCCESS        = 0
  53. ERROR_DISK_BASE      = 1
  54. ERROR_UNSUPPORTED_FS = 2
  55. ERROR_UNKNOWN_FS     = 3
  56. ERROR_PARTITION      = 4
  57. ERROR_FILE_NOT_FOUND = 5
  58. ERROR_END_OF_FILE    = 6
  59. ERROR_MEMORY_POINTER = 7
  60. ERROR_DISK_FULL      = 8
  61. ERROR_FAT_TABLE      = 9
  62. ERROR_ACCESS_DENIED  = 10
  63.  
  64. PUSHAD_EAX equ [esp+28]
  65. PUSHAD_ECX equ [esp+24]
  66. PUSHAD_EDX equ [esp+20]
  67. PUSHAD_EBX equ [esp+16]
  68. PUSHAD_EBP equ [esp+8]
  69. PUSHAD_ESI equ [esp+4]
  70. PUSHAD_EDI equ [esp+0]
  71.  
  72. uglobal
  73. align 4
  74. partition_count      dd 0       ; partitions found by set_FAT32_variables
  75. longname_sec1        dd 0       ; used by analyze_directory to save 2 previous
  76. longname_sec2        dd 0       ; directory sectors for delete long filename
  77.  
  78. hd_error             dd 0       ; set by wait_for_sector_buffer
  79. hd_setup             dd 0
  80. hd_wait_timeout      dd 0
  81.  
  82. cluster_tmp          dd 0       ; used by analyze_directory
  83.                                 ; and analyze_directory_to_write
  84.  
  85. file_size            dd 0       ; used by file_read
  86.  
  87. cache_search_start   dd 0       ; used by find_empty_slot
  88. endg
  89.  
  90. iglobal
  91. fat_in_cache         dd -1
  92. endg
  93.  
  94. uglobal
  95. align 4
  96. fat_cache:           times 512 db 0
  97.  Sector512:                      ; label for dev_hdcd.inc
  98.   buffer:              times 512 db 0
  99.   fsinfo_buffer:       times 512 db 0
  100. endg
  101.  
  102. uglobal
  103.   fat16_root           db 0       ; flag for fat16 rootdir
  104.   fat_change           db 0       ; 1=fat has changed
  105. endg
  106.  
  107. reserve_hd1:
  108.  
  109.     pushfd
  110.     cli
  111.  
  112.     cmp   [hd1_status],0                          ;FIXME  use mutex
  113.     je    reserve_ok1
  114.  
  115.     popfd
  116.  
  117.     call  change_task
  118.     jmp   reserve_hd1
  119.  
  120.   reserve_ok1:
  121.  
  122.     push  eax
  123.     mov   eax,[CURRENT_TASK]
  124.     shl   eax,5
  125.     mov   eax,[eax+CURRENT_TASK+TASKDATA.pid]
  126.     mov   [hd1_status],eax
  127.     pop   eax
  128.     popfd
  129.     ret
  130. ;********************************************
  131.  
  132. uglobal
  133. hd_in_cache db ?
  134. endg
  135.  
  136. reserve_hd_channel:
  137. ; BIOS disk accesses are protected with common mutex hd1_status
  138. ; This must be modified when hd1_status will not be valid!
  139.         cmp     [hdpos], 0x80
  140.         jae     .ret
  141.     cmp   [hdbase], 0x1F0
  142.     jne   .IDE_Channel_2
  143. .IDE_Channel_1:
  144.     pushfd
  145.     cli
  146.     cmp   [IDE_Channel_1],0
  147.     je    .reserve_ok_1
  148.     popfd
  149.     call  change_task
  150.     jmp   .IDE_Channel_1
  151. .IDE_Channel_2:
  152.     pushfd
  153.     cli
  154.     cmp   [IDE_Channel_2],0
  155.     je    .reserve_ok_2
  156.     popfd
  157.     call  change_task
  158.     jmp   .IDE_Channel_2
  159. .reserve_ok_1:
  160.         mov     [IDE_Channel_1], 1
  161.         popfd
  162.         push    eax
  163.         mov     al, 1
  164.         jmp     @f
  165. .reserve_ok_2:
  166.         mov     [IDE_Channel_2], 1
  167.         popfd
  168.         push    eax
  169.         mov     al, 3
  170. @@:
  171.         cmp     [hdid], 1
  172.         sbb     al, -1
  173.         cmp     al, [hd_in_cache]
  174.         jz      @f
  175.         mov     [hd_in_cache], al
  176.         call    clear_hd_cache
  177. @@:
  178.         pop     eax
  179. .ret:
  180.         ret
  181.  
  182. free_hd_channel:
  183. ; see comment at reserve_hd_channel
  184.         cmp     [hdpos], 0x80
  185.         jae     .ret
  186.     cmp   [hdbase], 0x1F0
  187.     jne   .IDE_Channel_2
  188. .IDE_Channel_1:
  189.     mov [IDE_Channel_1],0
  190. .ret:
  191.     ret
  192. .IDE_Channel_2:
  193.     mov [IDE_Channel_2],0
  194.     ret
  195. ;********************************************
  196. problem_partition db 0  ; used for partitions search
  197.  
  198. include  'part_set.inc'
  199.  
  200. set_FAT:
  201. ;--------------------------------
  202. ; input  : EAX = cluster
  203. ;          EDX = value to save
  204. ; output : EDX = old value
  205. ;--------------------------------
  206.     push  eax ebx esi
  207.  
  208.     cmp   eax,2
  209.     jb    sfc_error
  210.     cmp   eax,[LAST_CLUSTER]
  211.     ja    sfc_error
  212.     cmp   [fs_type],16
  213.     je    sfc_1
  214.     add   eax,eax
  215.   sfc_1:
  216.     add   eax,eax
  217.     mov   esi,511
  218.     and   esi,eax               ; esi = position in fat sector
  219.     shr   eax,9                 ; eax = fat sector
  220.     add   eax,[FAT_START]
  221.     mov   ebx,fat_cache
  222.  
  223.     cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
  224.     je    sfc_in_cache          ; yes
  225.  
  226.     cmp   [fat_change],0        ; is fat changed?
  227.     je    sfc_no_change         ; no
  228.     call  write_fat_sector      ; yes. write it into disk
  229.     cmp   [hd_error],0
  230.     jne   sfc_error
  231.  
  232.   sfc_no_change:
  233.     mov   [fat_in_cache],eax    ; save fat sector
  234.     call  hd_read
  235.     cmp  [hd_error],0
  236.     jne  sfc_error
  237.  
  238.  
  239.   sfc_in_cache:
  240.     cmp   [fs_type],16
  241.     jne   sfc_test32
  242.  
  243.   sfc_set16:
  244.     xchg  [ebx+esi],dx          ; save new value and get old value
  245.     jmp   sfc_write
  246.  
  247.   sfc_test32:
  248.     mov   eax,[fatMASK]
  249.  
  250.   sfc_set32:
  251.     and   edx,eax
  252.     xor   eax,-1                ; mask for high bits
  253.     and   eax,[ebx+esi]         ; get high 4 bits
  254.     or    eax,edx
  255.     mov   edx,[ebx+esi]         ; get old value
  256.     mov   [ebx+esi],eax         ; save new value
  257.  
  258.   sfc_write:
  259.     mov   [fat_change],1        ; fat has changed
  260.  
  261.   sfc_nonzero:
  262.     and   edx,[fatMASK]
  263.  
  264.   sfc_error:
  265.     pop   esi ebx eax
  266.     ret
  267.  
  268.  
  269. get_FAT:
  270. ;--------------------------------
  271. ; input  : EAX = cluster
  272. ; output : EAX = next cluster
  273. ;--------------------------------
  274.     push  ebx esi
  275.  
  276.     cmp   [fs_type],16
  277.     je    gfc_1
  278.     add   eax,eax
  279.   gfc_1:
  280.     add   eax,eax
  281.     mov   esi,511
  282.     and   esi,eax               ; esi = position in fat sector
  283.     shr   eax,9                 ; eax = fat sector
  284.     add   eax,[FAT_START]
  285.     mov   ebx,fat_cache
  286.  
  287.     cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
  288.     je    gfc_in_cache
  289.  
  290.     cmp   [fat_change],0        ; is fat changed?
  291.     je    gfc_no_change         ; no
  292.     call  write_fat_sector      ; yes. write it into disk
  293.     cmp  [hd_error],0
  294.     jne  hd_error_01
  295.  
  296.   gfc_no_change:
  297.     mov   [fat_in_cache],eax
  298.     call  hd_read
  299.     cmp  [hd_error],0
  300.     jne  hd_error_01
  301.  
  302.   gfc_in_cache:
  303.     mov   eax,[ebx+esi]
  304.     and   eax,[fatMASK]
  305.  hd_error_01:
  306.     pop   esi ebx
  307.     ret
  308.  
  309.  
  310. get_free_FAT:
  311. ;-----------------------------------------------------------
  312. ; output : if CARRY=0 EAX = # first cluster found free
  313. ;          if CARRY=1 disk full
  314. ; Note   : for more speed need to use fat_cache directly
  315. ;-----------------------------------------------------------
  316.     push  ecx
  317.     mov   ecx,[LAST_CLUSTER]    ; counter for full disk
  318.     sub   ecx,2
  319.     mov   eax,[fatStartScan]
  320.     cmp   eax,2
  321.     jb    gff_reset
  322.  
  323.   gff_test:
  324.     cmp   eax,[LAST_CLUSTER]    ; if above last cluster start at cluster 2
  325.     jbe   gff_in_range
  326.   gff_reset:
  327.     mov   eax,2
  328.  
  329.   gff_in_range:
  330.     push  eax
  331.     call  get_FAT               ; get cluster state
  332.     cmp   [hd_error],0
  333.     jne   gff_not_found_1
  334.  
  335.     test  eax,eax               ; is it free?
  336.     pop   eax
  337.     je    gff_found             ; yes
  338.     inc   eax                   ; next cluster
  339.     dec   ecx                   ; is all checked?
  340.     jns   gff_test              ; no
  341.  
  342.   gff_not_found_1:
  343.     add   esp,4
  344.   gff_not_found:
  345.     pop   ecx                   ; yes. disk is full
  346.     stc
  347.     ret
  348.  
  349.   gff_found:
  350.     lea   ecx,[eax+1]
  351.     mov   [fatStartScan],ecx
  352.     pop   ecx
  353.     clc
  354.     ret
  355.  
  356.  
  357. write_fat_sector:
  358. ;-----------------------------------------------------------
  359. ; write changed fat to disk
  360. ;-----------------------------------------------------------
  361.     push  eax ebx ecx
  362.  
  363.     mov   [fat_change],0
  364.     mov   eax,[fat_in_cache]
  365.     cmp   eax,-1
  366.     jz    write_fat_not_used
  367.     mov   ebx,fat_cache
  368.     mov   ecx,[NUMBER_OF_FATS]
  369.  
  370.   write_next_fat:
  371.     call  hd_write
  372.     cmp   [hd_error],0
  373.     jne   write_fat_not_used
  374.  
  375.     add   eax,[SECTORS_PER_FAT]
  376.     dec   ecx
  377.     jnz   write_next_fat
  378.  
  379.   write_fat_not_used:
  380.     pop   ecx ebx eax
  381.     ret
  382.  
  383.  
  384. analyze_directory:
  385. ;-----------------------------------------------------------
  386. ; input  : EAX = first cluster of the directory
  387. ;          EBX = pointer to filename
  388. ; output : IF CARRY=0 EAX = sector where th file is found
  389. ;                     EBX = pointer in buffer
  390. ;                     [buffer .. buffer+511]
  391. ;                     ECX,EDX,ESI,EDI not changed
  392. ;          IF CARRY=1 filename not found
  393. ; Note   : if cluster=0 it's changed to read rootdir
  394. ;          save 2 previous directory sectors in longname_sec
  395. ;-----------------------------------------------------------
  396.     push  ecx edx esi edi ebx   ; ebx = [esp+0]
  397.     mov   [longname_sec1],0
  398.     mov   [longname_sec2],0
  399.  
  400.   adr_new_cluster:
  401.     mov   [cluster_tmp],eax
  402.     mov   [fat16_root],0
  403.     cmp   eax,[LAST_CLUSTER]
  404.     ja    adr_not_found         ; too big cluster number, something is wrong
  405.     cmp   eax,2
  406.     jnb   adr_data_cluster
  407.  
  408.     mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
  409.     cmp   [fs_type],16
  410.     jne   adr_data_cluster
  411.     mov   eax,[ROOT_START]
  412.     mov   edx,[ROOT_SECTORS]
  413.     mov   [fat16_root],1        ; flag for fat16 rootdir
  414.     jmp   adr_new_sector
  415.  
  416.   adr_data_cluster:
  417.     sub   eax,2
  418.     mov   edx,[SECTORS_PER_CLUSTER]
  419.     imul  eax,edx
  420.     add   eax,[DATA_START]
  421.  
  422.   adr_new_sector:
  423.     mov   ebx,buffer
  424.     call  hd_read
  425.     cmp  [hd_error],0
  426.     jne  adr_not_found
  427.  
  428.     mov   ecx,512/32            ; count of dir entrys per sector = 16
  429.  
  430.   adr_analyze:
  431.     mov   edi,[ebx+11]          ; file attribute
  432.     and   edi,0xf
  433.     cmp   edi,0xf
  434.     je    adr_long_filename
  435.     test  edi,0x8               ; skip over volume label
  436.     jne   adr_long_filename     ; Note: label can be same name as file/dir
  437.  
  438.     mov   esi,[esp+0]           ; filename need to be uppercase
  439.     mov   edi,ebx
  440.     push  ecx
  441.     mov   ecx,11
  442.     cld
  443.     rep   cmpsb                 ; compare 8+3 filename
  444.     pop   ecx
  445.     je    adr_found
  446.  
  447.   adr_long_filename:
  448.     add   ebx,32                ; position of next dir entry
  449.     dec   ecx
  450.     jnz   adr_analyze
  451.  
  452.     mov   ecx,[longname_sec1]   ; save 2 previous directory sectors
  453.     mov   [longname_sec1],eax   ; for delete long filename
  454.     mov   [longname_sec2],ecx
  455.     inc   eax                   ; next sector
  456.     dec   edx
  457.     jne   adr_new_sector
  458.     cmp   [fat16_root],1        ; end of fat16 rootdir
  459.     je    adr_not_found
  460.  
  461.   adr_next_cluster:
  462.     mov   eax,[cluster_tmp]
  463.     call  get_FAT               ; get next cluster
  464.     cmp  [hd_error],0
  465.     jne  adr_not_found
  466.  
  467.     cmp   eax,2                 ; incorrect fat chain?
  468.     jb    adr_not_found         ; yes
  469.     cmp   eax,[fatRESERVED]     ; is it end of directory?
  470.     jb    adr_new_cluster       ; no. analyse it
  471.  
  472.   adr_not_found:
  473.     pop   edi edi esi edx ecx   ; first edi will remove ebx
  474.     stc                         ; file not found
  475.     ret
  476.  
  477.   adr_found:
  478.     pop   edi edi esi edx ecx   ; first edi will remove ebx
  479.     clc                         ; file found
  480.     ret
  481.  
  482.  
  483. get_data_cluster:
  484. ;-----------------------------------------------------------
  485. ; input  : EAX = cluster
  486. ;          EBX = pointer to buffer
  487. ;          EDX = # blocks to read in buffer
  488. ;          ESI = # blocks to skip over
  489. ; output : if CARRY=0 ok EBX/EDX/ESI updated
  490. ;          if CARRY=1 cluster out of range
  491. ; Note   : if cluster=0 it's changed to read rootdir
  492. ;-----------------------------------------------------------
  493.     push  eax ecx
  494.  
  495.     mov   [fat16_root],0
  496.     cmp   eax,[LAST_CLUSTER]
  497.     ja    gdc_error             ; too big cluster number, something is wrong
  498.     cmp   eax,2
  499.     jnb   gdc_cluster
  500.  
  501.     mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
  502.     cmp   [fs_type],16
  503.     jne   gdc_cluster
  504.     mov   eax,[ROOT_START]
  505.     mov   ecx,[ROOT_SECTORS]    ; Note: not cluster size
  506.     mov   [fat16_root],1        ; flag for fat16 rootdir
  507.     jmp   gdc_read
  508.  
  509.   gdc_cluster:
  510.     sub   eax,2
  511.     mov   ecx,[SECTORS_PER_CLUSTER]
  512.     imul  eax,ecx
  513.     add   eax,[DATA_START]
  514.  
  515.   gdc_read:
  516.     test  esi,esi               ; first wanted block
  517.     je    gdcl1                 ; yes, skip count is 0
  518.     dec   esi
  519.     jmp   gdcl2
  520.  
  521.   gdcl1:
  522.     call  hd_read
  523.     cmp  [hd_error],0
  524.     jne  gdc_error
  525.  
  526.     add   ebx,512               ; update pointer
  527.     dec   edx
  528.  
  529.   gdcl2:
  530.     test  edx,edx               ; is all read?
  531.     je    out_of_read
  532.  
  533.     inc   eax                   ; next sector
  534.     dec   ecx
  535.     jnz   gdc_read
  536.  
  537.   out_of_read:
  538.     pop   ecx eax
  539.     clc
  540.     ret
  541.  
  542.   gdc_error:
  543.     pop   ecx eax
  544.     stc
  545.     ret
  546.  
  547.  
  548. get_cluster_of_a_path:
  549. ;---------------------------------------------------------
  550. ; input  : EBX = pointer to a path string
  551. ;          (example: the path "/files/data/document" become
  552. ;                             "files......data.......document...0"
  553. ;          '.' = space char
  554. ;          '0' = char(0) (ASCII=0) !!! )
  555. ; output : if (CARRY=1) -> ERROR in the PATH
  556. ;          if (CARRY=0) -> EAX=cluster
  557. ;---------------------------------------------------------
  558.     push  ebx edx
  559.  
  560.     mov   eax,[ROOT_CLUSTER]
  561.     mov   edx,ebx
  562.  
  563. search_end_of_path:
  564.     cmp   byte [edx],0
  565.     je    found_end_of_path
  566.  
  567.     inc   edx ; '/'
  568.     mov   ebx,edx
  569.     call  analyze_directory
  570.     jc    directory_not_found
  571.  
  572.     mov   eax,[ebx+20-2]        ; read the HIGH 16bit cluster field
  573.     mov   ax,[ebx+26]           ; read the LOW 16bit cluster field
  574.     and   eax,[fatMASK]
  575.     add   edx,11                ; 8+3 (name+extension)
  576.     jmp   search_end_of_path
  577.  
  578. found_end_of_path:
  579.     pop   edx ebx
  580.     clc                         ; no errors
  581.     ret
  582.  
  583. directory_not_found:
  584.     pop   edx ebx
  585.     stc                         ; errors occour
  586.     ret
  587.  
  588.  
  589. bcd2bin:
  590. ;----------------------------------
  591. ; input  : AL=BCD number (eg. 0x11)
  592. ; output : AH=0
  593. ;          AL=decimal number (eg. 11)
  594. ;----------------------------------
  595.     xor   ah,ah
  596.     shl   ax,4
  597.     shr   al,4
  598.     aad
  599.     ret
  600.  
  601.  
  602. get_date_for_file:
  603. ;-----------------------------------------------------
  604. ; Get date from CMOS and pack day,month,year in AX
  605. ; DATE   bits  0..4   : day of month 0..31
  606. ;              5..8   : month of year 1..12
  607. ;              9..15  : count of years from 1980
  608. ;-----------------------------------------------------
  609.     mov   al,0x7        ;day
  610.     out   0x70,al
  611.     in    al,0x71
  612.     call  bcd2bin
  613.     ror   eax,5
  614.  
  615.     mov   al,0x8        ;month
  616.     out   0x70,al
  617.     in    al,0x71
  618.     call  bcd2bin
  619.     ror   eax,4
  620.  
  621.     mov   al,0x9        ;year
  622.     out   0x70,al
  623.     in    al,0x71
  624.     call  bcd2bin
  625.     add   ax,20         ;because CMOS return only the two last
  626.                         ;digit (eg. 2000 -> 00 , 2001 -> 01) and we
  627.     rol   eax,9         ;need the difference with 1980 (eg. 2001-1980)
  628.     ret
  629.  
  630.  
  631. get_time_for_file:
  632. ;-----------------------------------------------------
  633. ; Get time from CMOS and pack hour,minute,second in AX
  634. ; TIME   bits  0..4   : second (the low bit is lost)
  635. ;              5..10  : minute 0..59
  636. ;              11..15 : hour 0..23
  637. ;-----------------------------------------------------
  638.     mov   al,0x0        ;second
  639.     out   0x70,al
  640.     in    al,0x71
  641.     call  bcd2bin
  642.     ror   eax,6
  643.  
  644.     mov   al,0x2        ;minute
  645.     out   0x70,al
  646.     in    al,0x71
  647.     call  bcd2bin
  648.     ror   eax,6
  649.  
  650.     mov   al,0x4        ;hour
  651.     out   0x70,al
  652.     in    al,0x71
  653.     call  bcd2bin
  654.     rol   eax,11
  655.     ret
  656.  
  657.  
  658. set_current_time_for_entry:
  659. ;-----------------------------------------------------
  660. ; Set current time/date for file entry
  661. ; input  : ebx = file entry pointer
  662. ;-----------------------------------------------------
  663.     push  eax
  664.     call  get_time_for_file     ; update files date/time
  665.     mov   [ebx+22],ax
  666.     call  get_date_for_file
  667.     mov   [ebx+24],ax
  668.     pop   eax
  669.     ret
  670.  
  671.  
  672.  
  673. add_disk_free_space:
  674. ;-----------------------------------------------------
  675. ; input  : ecx = cluster count
  676. ; Note   : negative = remove clusters from free space
  677. ;          positive = add clusters to free space
  678. ;-----------------------------------------------------
  679.     test  ecx,ecx               ; no change
  680.     je    add_dfs_no
  681.     cmp   [fs_type],32         ; free disk space only used by fat32
  682.     jne   add_dfs_no
  683.  
  684.     push  eax ebx
  685.     mov   eax,[ADR_FSINFO]
  686.     mov   ebx,fsinfo_buffer
  687.     call  hd_read
  688.     cmp  [hd_error],0
  689.     jne  add_not_fs
  690.  
  691.     cmp   dword [ebx+0x1fc],0xaa550000 ; check sector id
  692.     jne   add_not_fs
  693.  
  694.     add   [ebx+0x1e8],ecx
  695.     push  [fatStartScan]
  696.     pop   dword [ebx+0x1ec]
  697.     call  hd_write
  698. ;    cmp   [hd_error],0
  699. ;    jne   add_not_fs
  700.  
  701.   add_not_fs:
  702.     pop   ebx eax
  703.  
  704.   add_dfs_no:
  705.     ret
  706.  
  707.  
  708. file_read:
  709. ;--------------------------------------------------------------------------
  710. ;   INPUT :  user-register register-in-this  meaning         symbol-in-this
  711. ;
  712. ;            EAX           EDI               system call to write   /
  713. ;            EBX           EAX   (PAR0)      pointer to file-name   PAR0
  714. ;            EDX           ECX   (PAR1)      pointer to buffer      PAR1
  715. ;            ECX           EBX   (PAR2)   vt file blocks to read    PAR2
  716. ;            ESI           EDX   (PAR3)      pointer to path        PAR3
  717. ;            EDI           ESI            vt first 512 block to read
  718. ;                          EDI               if 0 - read root
  719. ;
  720. ; output : eax = 0 - ok
  721. ;                3 - unknown FS
  722. ;                5 - file not found
  723. ;                6 - end of file
  724. ;                9 - fat table corrupted
  725. ;               10 - access denied
  726. ;          ebx = size of file/directory
  727. ;--------------------------------------------------------------------------
  728.         cmp     [fs_type], 16
  729.         jz      fat_ok_for_reading
  730.         cmp     [fs_type], 32
  731.         jz      fat_ok_for_reading
  732.     xor   ebx,ebx
  733.     mov   eax,ERROR_UNKNOWN_FS
  734.     mov   [hd1_status], ebx
  735.     ret
  736.  
  737.   fat_ok_for_reading:
  738. ;    call  reserve_hd1
  739.  
  740.     pushad
  741.  
  742.     mov   ebx,edx
  743.     call  get_cluster_of_a_path
  744.     jc    file_to_read_not_found
  745.  
  746.     test  edi,edi               ; read rootdir
  747.     jne   no_read_root
  748.  
  749.     xor   eax,eax
  750.     call  get_dir_size          ; return rootdir size
  751.     cmp   [hd_error],0
  752.     jne   file_access_denied
  753.  
  754.     mov   [file_size],eax
  755.     mov   eax,[ROOT_CLUSTER]
  756.     jmp   file_read_start
  757.  
  758.   no_read_root:
  759.     mov   ebx,PUSHAD_EAX        ; file name
  760.     call  analyze_directory
  761.     jc    file_to_read_not_found
  762.  
  763.     mov   eax,[ebx+28]          ; file size
  764.     test  byte [ebx+11],0x10    ; is it directory?
  765.     jz    read_set_size         ; no
  766.  
  767.     mov   eax,[ebx+20-2]        ; FAT entry
  768.     mov   ax,[ebx+26]
  769.     and   eax,[fatMASK]
  770.     call  get_dir_size
  771.     cmp   [hd_error],0
  772.     jne   file_access_denied
  773.  
  774.   read_set_size:
  775.     mov   [file_size],eax
  776.  
  777.     mov   eax,[ebx+20-2]        ; FAT entry
  778.     mov   ax,[ebx+26]
  779.     and   eax,[fatMASK]
  780.  
  781.   file_read_start:
  782.     mov   ebx,PUSHAD_ECX        ; pointer to buffer
  783.     mov   edx,PUSHAD_EBX        ; file blocks to read
  784.     mov   esi,PUSHAD_ESI        ; first 512 block to read
  785.  
  786.   file_read_new_cluster:
  787.     call  get_data_cluster
  788.     jc    file_read_eof         ; end of file or cluster out of range
  789.  
  790.     test  edx,edx               ; is all read?
  791.     je    file_read_OK          ; yes
  792.  
  793.     call  get_FAT               ; get next cluster
  794.     cmp   [hd_error],0
  795.     jne   file_access_denied
  796.  
  797.     cmp   eax,[fatRESERVED]     ; end of file
  798.     jnb   file_read_eof
  799.     cmp   eax,2                 ; incorrect fat chain
  800.     jnb   file_read_new_cluster
  801.  
  802.     popad
  803.     mov   [hd1_status],0
  804.     mov   ebx,[file_size]
  805.     mov   eax,ERROR_FAT_TABLE
  806.     ret
  807.  
  808.   file_read_eof:
  809.     cmp   [hd_error],0
  810.     jne   file_access_denied
  811.     popad
  812.     mov   [hd1_status],0
  813.     mov   ebx,[file_size]
  814.     mov   eax,ERROR_END_OF_FILE
  815.     ret
  816.  
  817.   file_read_OK:
  818.     popad
  819.     mov   [hd1_status],0
  820.     mov   ebx,[file_size]
  821.     xor   eax,eax
  822.     ret
  823.  
  824.   file_to_read_not_found:
  825.     cmp   [hd_error],0
  826.     jne   file_access_denied
  827.     popad
  828.     mov   [hd1_status],0
  829.     xor   ebx,ebx
  830.     mov   eax,ERROR_FILE_NOT_FOUND
  831.     ret
  832.  
  833.   file_access_denied:
  834.     popad
  835.     mov   [hd1_status],0
  836.     xor   ebx,ebx
  837.     mov   eax,ERROR_ACCESS_DENIED
  838.     ret
  839.  
  840. get_dir_size:
  841. ;-----------------------------------------------------
  842. ; input  : eax = first cluster (0=rootdir)
  843. ; output : eax = directory size in bytes
  844. ;-----------------------------------------------------
  845.     push  edx
  846.     xor   edx,edx               ; count of directory clusters
  847.     test  eax,eax
  848.     jnz   dir_size_next
  849.  
  850.     mov   eax,[ROOT_SECTORS]
  851.     shl   eax,9                 ; fat16 rootdir size in bytes
  852.     cmp   [fs_type],16
  853.     je    dir_size_ret
  854.     mov   eax,[ROOT_CLUSTER]
  855.  
  856.   dir_size_next:
  857.     cmp   eax,2                 ; incorrect fat chain
  858.     jb    dir_size_end
  859.     cmp   eax,[fatRESERVED]     ; end of directory
  860.     ja    dir_size_end
  861.     call  get_FAT               ; get next cluster
  862.     cmp   [hd_error],0
  863.     jne   dir_size_ret
  864.  
  865.     inc   edx
  866.     jmp   dir_size_next
  867.  
  868.   dir_size_end:
  869.     imul  eax,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
  870.     imul  eax,edx
  871.  
  872.   dir_size_ret:
  873.     pop   edx
  874.     ret
  875.  
  876.  
  877. clear_cluster_chain:
  878. ;-----------------------------------------------------
  879. ; input  : eax = first cluster
  880. ;-----------------------------------------------------
  881.     push  eax ecx edx
  882.     xor   ecx,ecx               ; cluster count
  883.  
  884.   clean_new_chain:
  885.     cmp   eax,[LAST_CLUSTER]    ; end of file
  886.     ja    delete_OK
  887.     cmp   eax,2                 ; unfinished fat chain or zero length file
  888.     jb    delete_OK
  889.     cmp   eax,[ROOT_CLUSTER]    ; don't remove root cluster
  890.     jz    delete_OK
  891.  
  892.     xor   edx,edx
  893.     call  set_FAT               ; clear fat entry
  894.     cmp  [hd_error],0
  895.     jne  access_denied_01
  896.  
  897.     inc   ecx                   ; update cluster count
  898.     mov   eax,edx               ; old cluster
  899.     jmp   clean_new_chain
  900.  
  901.   delete_OK:
  902.     call  add_disk_free_space   ; add clusters to free disk space
  903.   access_denied_01:
  904.     pop   edx ecx eax
  905.     ret
  906.  
  907.  
  908. get_hd_info:
  909. ;-----------------------------------------------------------
  910. ; output : eax = 0 - ok
  911. ;                3 - unknown FS
  912. ;               10 - access denied
  913. ;          edx = cluster size in bytes
  914. ;          ebx = total clusters on disk
  915. ;          ecx = free clusters on disk
  916. ;-----------------------------------------------------------
  917.         cmp     [fs_type], 16
  918.         jz      info_fat_ok
  919.         cmp     [fs_type], 32
  920.         jz      info_fat_ok
  921.     xor   edx,edx
  922.     xor   ebx,ebx
  923.     xor   ecx,ecx
  924.     mov   eax,ERROR_UNKNOWN_FS
  925.     ret
  926.  
  927.   info_fat_ok:
  928. ;    call  reserve_hd1
  929.  
  930.     xor   ecx,ecx               ; count of free clusters
  931.     mov   eax,2
  932.     mov   ebx,[LAST_CLUSTER]
  933.  
  934.   info_cluster:
  935.     push  eax
  936.     call  get_FAT               ; get cluster info
  937.     cmp   [hd_error],0
  938.     jne   info_access_denied
  939.  
  940.     test  eax,eax               ; is it free?
  941.     jnz   info_used             ; no
  942.     inc   ecx
  943.  
  944.   info_used:
  945.     pop   eax
  946.     inc   eax
  947.     cmp   eax,ebx               ; is above last cluster?
  948.     jbe   info_cluster          ; no. test next cluster
  949.  
  950.     dec   ebx                   ; cluster count
  951.     imul  edx,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
  952.     mov   [hd1_status],0
  953.     xor   eax,eax
  954.     ret
  955.  
  956.   info_access_denied:
  957.     add   esp,4
  958.     xor   edx,edx
  959.     xor   ebx,ebx
  960.     xor   ecx,ecx
  961.     mov   eax,ERROR_ACCESS_DENIED
  962.     ret
  963.  
  964. update_disk:
  965. ;-----------------------------------------------------------
  966. ; write changed fat and cache to disk
  967. ;-----------------------------------------------------------
  968.     cmp   [fat_change],0        ; is fat changed?
  969.     je    upd_no_change
  970.  
  971.     call  write_fat_sector
  972.     cmp   [hd_error],0
  973.     jne   update_disk_acces_denied
  974.  
  975.   upd_no_change:
  976.  
  977.     call  write_cache
  978.   update_disk_acces_denied:
  979.     ret
  980.  
  981.  
  982. ; \begin{diamond}
  983. hd_find_lfn:
  984. ; in: esi+ebp -> name
  985. ; out: CF=1 - file not found
  986. ;      else CF=0 and edi->direntry, eax=sector
  987. ; destroys eax
  988.         push    esi edi
  989.         push    0
  990.         push    0
  991.         push    fat16_root_first
  992.         push    fat16_root_next
  993.         mov     eax, [ROOT_CLUSTER]
  994.         cmp     [fs_type], 32
  995.         jz      .fat32
  996. .loop:
  997.         call    fat_find_lfn
  998.         jc      .notfound
  999.         cmp     byte [esi], 0
  1000.         jz      .found
  1001. .continue:
  1002.         test    byte [edi+11], 10h
  1003.         jz      .notfound
  1004.         and     dword [esp+12], 0
  1005.         mov     eax, [edi+20-2]
  1006.         mov     ax, [edi+26]    ; cluster
  1007. .fat32:
  1008.         mov     [esp+8], eax
  1009.         mov     dword [esp+4], fat_notroot_first
  1010.         mov     dword [esp], fat_notroot_next
  1011.         jmp     .loop
  1012. .notfound:
  1013.         add     esp, 16
  1014.         pop     edi esi
  1015.         stc
  1016.         ret
  1017. .found:
  1018.         test    ebp, ebp
  1019.         jz      @f
  1020.         mov     esi, ebp
  1021.         xor     ebp, ebp
  1022.         jmp     .continue
  1023. @@:
  1024.         lea     eax, [esp+8]
  1025.         cmp     dword [eax], 0
  1026.         jz      .root
  1027.         call    fat_get_sector
  1028.         jmp     .cmn
  1029. .root:
  1030.         mov     eax, [eax+4]
  1031.         add     eax, [ROOT_START]
  1032. .cmn:
  1033.         add     esp, 20         ; CF=0
  1034.         pop     esi
  1035.         ret
  1036.  
  1037. ;----------------------------------------------------------------
  1038. ;
  1039. ;  fs_HdRead - LFN variant for reading hard disk
  1040. ;
  1041. ;  esi  points to filename
  1042. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  1043. ;       may be ebx=0 - start from first byte
  1044. ;  ecx  number of bytes to read, 0+
  1045. ;  edx  mem location to return data
  1046. ;
  1047. ;  ret ebx = bytes read or 0xffffffff file not found
  1048. ;      eax = 0 ok read or other = errormsg
  1049. ;
  1050. ;--------------------------------------------------------------
  1051. fs_HdRead:
  1052.         cmp     [fs_type], 16
  1053.         jz      @f
  1054.         cmp     [fs_type], 32
  1055.         jz      @f
  1056.         cmp     [fs_type], 1
  1057.         jz      ntfs_HdRead
  1058.         or      ebx, -1
  1059.         mov     eax, ERROR_UNKNOWN_FS
  1060.         ret
  1061. @@:
  1062.     push    edi
  1063.     cmp    byte [esi], 0
  1064.     jnz    @f
  1065. .noaccess:
  1066.     pop    edi
  1067. .noaccess_2:
  1068.     or    ebx, -1
  1069.     mov    eax, ERROR_ACCESS_DENIED
  1070.     ret
  1071.  
  1072. .noaccess_3:
  1073.     add esp,4
  1074. .noaccess_1:
  1075.     add esp,4
  1076. .noaccess_4:
  1077.     add esp,4*5
  1078.     jmp  .noaccess_2
  1079.  
  1080. @@:
  1081.     call    hd_find_lfn
  1082.     jnc    .found
  1083.     pop    edi
  1084.     cmp   [hd_error],0
  1085.     jne   .noaccess_2
  1086.     or    ebx, -1
  1087.     mov    eax, ERROR_FILE_NOT_FOUND
  1088.     ret
  1089.  
  1090. .found:
  1091.     test    byte [edi+11], 0x10    ; do not allow read directories
  1092.     jnz    .noaccess
  1093.     test    ebx, ebx
  1094.     jz    .l1
  1095.     cmp    dword [ebx+4], 0
  1096.     jz    @f
  1097.         xor     ebx, ebx
  1098. .reteof:
  1099.     mov    eax, 6
  1100.     pop    edi
  1101.     ret
  1102. @@:
  1103.     mov    ebx, [ebx]
  1104. .l1:
  1105.         push    ecx edx
  1106.         push    0
  1107.         mov     eax, [edi+28]
  1108.         sub     eax, ebx
  1109.         jb      .eof
  1110.         cmp     eax, ecx
  1111.         jae     @f
  1112.         mov     ecx, eax
  1113.         mov     byte [esp], 6
  1114. @@:
  1115.     mov    eax, [edi+20-2]
  1116.     mov    ax, [edi+26]
  1117. ; now eax=cluster, ebx=position, ecx=count, edx=buffer for data
  1118. .new_cluster:
  1119.     jecxz    .new_sector
  1120.     test    eax, eax
  1121.     jz    .eof
  1122.     cmp    eax, [fatRESERVED]
  1123.     jae    .eof
  1124.     mov    [cluster_tmp], eax
  1125.     dec    eax
  1126.     dec    eax
  1127.     mov    edi, [SECTORS_PER_CLUSTER]
  1128.     imul    eax, edi
  1129.     add    eax, [DATA_START]
  1130. .new_sector:
  1131.     test    ecx, ecx
  1132.     jz    .done
  1133.     sub    ebx, 512
  1134.     jae    .skip
  1135.     add    ebx, 512
  1136.     jnz    .force_buf
  1137.     cmp    ecx, 512
  1138.     jb    .force_buf
  1139. ; we may read directly to given buffer
  1140.     push    ebx
  1141.     mov    ebx, edx
  1142.     call    hd_read
  1143.     cmp  [hd_error],0
  1144.     jne  .noaccess_1
  1145.     pop    ebx
  1146.     add    edx, 512
  1147.     sub    ecx, 512
  1148.     jmp    .skip
  1149. .force_buf:
  1150. ; we must read sector to temporary buffer and then copy it to destination
  1151.     push    eax ebx
  1152.     mov    ebx, buffer
  1153.     call    hd_read
  1154.     cmp  [hd_error],0
  1155.     jne  .noaccess_3
  1156.  
  1157.     mov    eax, ebx
  1158.     pop    ebx
  1159.     add    eax, ebx
  1160.     push    ecx
  1161.     add    ecx, ebx
  1162.     cmp    ecx, 512
  1163.     jbe    @f
  1164.     mov    ecx, 512
  1165. @@:
  1166.     sub    ecx, ebx
  1167.     mov    ebx, edx
  1168.     call    memmove
  1169.     add    edx, ecx
  1170.     sub    [esp], ecx
  1171.     pop    ecx
  1172.     pop    eax
  1173.     xor    ebx, ebx
  1174. .skip:
  1175.     inc    eax
  1176.     dec    edi
  1177.     jnz    .new_sector
  1178.     mov    eax, [cluster_tmp]
  1179.     call    get_FAT
  1180.     cmp   [hd_error],0
  1181.     jne   .noaccess_4
  1182.  
  1183.     jmp    .new_cluster
  1184. .done:
  1185.         mov     ebx, edx
  1186.         pop     eax edx ecx edi
  1187.         sub     ebx, edx
  1188.         ret
  1189. .eof:
  1190.         mov     ebx, edx
  1191.         pop     eax edx ecx
  1192.         sub     ebx, edx
  1193.         jmp     .reteof
  1194.  
  1195. ;----------------------------------------------------------------
  1196. ;
  1197. ;  fs_HdReadFolder - LFN variant for reading hard disk folder
  1198. ;
  1199. ;  esi  points to filename
  1200. ;  ebx  pointer to structure 32-bit number = first wanted block, 0+
  1201. ;                          & flags (bitfields)
  1202. ; flags: bit 0: 0=ANSI names, 1=UNICODE names
  1203. ;  ecx  number of blocks to read, 0+
  1204. ;  edx  mem location to return data
  1205. ;
  1206. ;  ret ebx = blocks read or 0xffffffff folder not found
  1207. ;      eax = 0 ok read or other = errormsg
  1208. ;
  1209. ;--------------------------------------------------------------
  1210. fs_HdReadFolder:
  1211.         cmp     [fs_type], 1
  1212.         jz      ntfs_HdReadFolder
  1213.         cmp     [fs_type], 16
  1214.         jz      @f
  1215.         cmp     [fs_type], 32
  1216.         jz      @f
  1217.         push    ERROR_UNSUPPORTED_FS
  1218.         pop     eax
  1219.         or      ebx, -1
  1220.         ret
  1221. @@:
  1222.         mov     eax, [ROOT_CLUSTER]
  1223.         push    edi
  1224.         cmp     byte [esi], 0
  1225.         jz      .doit
  1226.         call    hd_find_lfn
  1227.         jnc     .found
  1228.         pop     edi
  1229.         or      ebx, -1
  1230.         mov     eax, ERROR_FILE_NOT_FOUND
  1231.         ret
  1232. .found:
  1233.         test    byte [edi+11], 0x10     ; do not allow read files
  1234.         jnz     .found_dir
  1235.         pop     edi
  1236.         or      ebx, -1
  1237.         mov     eax, ERROR_ACCESS_DENIED
  1238.         ret
  1239. .found_dir:
  1240.         mov     eax, [edi+20-2]
  1241.         mov     ax, [edi+26]    ; eax=cluster
  1242. .doit:
  1243.         push    esi ecx
  1244.         push    ebp
  1245.         sub     esp, 262*2      ; reserve space for LFN
  1246.         mov     ebp, esp
  1247.         push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE name
  1248.         mov     ebx, [ebx]
  1249. ; init header
  1250.         push    eax ecx
  1251.         mov     edi, edx
  1252.         mov     ecx, 32/4
  1253.         xor     eax, eax
  1254.         rep     stosd
  1255.         pop     ecx eax
  1256.         mov     byte [edx], 1   ; version
  1257.         mov     esi, edi        ; esi points to BDFE
  1258. .new_cluster:
  1259.         mov     [cluster_tmp], eax
  1260.         test    eax, eax
  1261.         jnz     @f
  1262.         cmp     [fs_type], 32
  1263.         jz      .notfound
  1264.         mov     eax, [ROOT_START]
  1265.         push    [ROOT_SECTORS]
  1266.         push    ebx
  1267.         jmp     .new_sector
  1268. @@:
  1269.         dec     eax
  1270.         dec     eax
  1271.         imul    eax, [SECTORS_PER_CLUSTER]
  1272.         push    [SECTORS_PER_CLUSTER]
  1273.         add     eax, [DATA_START]
  1274.         push    ebx
  1275. .new_sector:
  1276.         mov     ebx, buffer
  1277.         mov     edi, ebx
  1278.         call    hd_read
  1279.         cmp     [hd_error], 0
  1280.         jnz     .notfound2
  1281.         add     ebx, 512
  1282.         push    eax
  1283. .l1:
  1284.         call    fat_get_name
  1285.         jc      .l2
  1286.         cmp     byte [edi+11], 0xF
  1287.         jnz     .do_bdfe
  1288.         add     edi, 0x20
  1289.         cmp     edi, ebx
  1290.         jb      .do_bdfe
  1291.         pop     eax
  1292.         inc     eax
  1293.         dec     dword [esp+4]
  1294.         jnz     @f
  1295.         mov     eax, [cluster_tmp]
  1296.         test    eax, eax
  1297.         jz      .done
  1298.         call    get_FAT
  1299.         cmp     [hd_error], 0
  1300.         jnz     .notfound2
  1301.         cmp     eax, 2
  1302.         jb      .done
  1303.         cmp     eax, [fatRESERVED]
  1304.         jae     .done
  1305.         push    eax
  1306.         mov     eax, [SECTORS_PER_CLUSTER]
  1307.         mov     [esp+8], eax
  1308.         pop     eax
  1309.         mov     [cluster_tmp], eax
  1310.         dec     eax
  1311.         dec     eax
  1312.         imul    eax, [SECTORS_PER_CLUSTER]
  1313.         add     eax, [DATA_START]
  1314. @@:
  1315.         mov     ebx, buffer
  1316.         mov     edi, ebx
  1317.         call    hd_read
  1318.         cmp     [hd_error], 0
  1319.         jnz     .notfound2
  1320.         add     ebx, 512
  1321.         push    eax
  1322. .do_bdfe:
  1323.         inc     dword [edx+8]   ; new file found
  1324.         dec     dword [esp+4]
  1325.         jns     .l2
  1326.         dec     ecx
  1327.         js      .l2
  1328.         inc     dword [edx+4]   ; new file block copied
  1329.         call    fat_entry_to_bdfe
  1330. .l2:
  1331.         add     edi, 0x20
  1332.         cmp     edi, ebx
  1333.         jb      .l1
  1334.         pop     eax
  1335.         inc     eax
  1336.         dec     dword [esp+4]
  1337.         jnz     .new_sector
  1338.         mov     eax, [cluster_tmp]
  1339.         test    eax, eax
  1340.         jz      .done
  1341.         call    get_FAT
  1342.         cmp     [hd_error], 0
  1343.         jnz     .notfound2
  1344.         cmp     eax, 2
  1345.         jb      .done
  1346.         cmp     eax, [fatRESERVED]
  1347.         jae     .done
  1348.         push    eax
  1349.         mov     eax, [SECTORS_PER_CLUSTER]
  1350.         mov     [esp+8], eax
  1351.         pop     eax
  1352.         pop     ebx
  1353.         add     esp, 4
  1354.         jmp     .new_cluster
  1355. .notfound2:
  1356.         add     esp, 8
  1357. .notfound:
  1358.         add     esp, 262*2+4
  1359.         pop     ebp ecx esi edi
  1360.         mov     eax, ERROR_FILE_NOT_FOUND
  1361.         or      ebx, -1
  1362.         ret
  1363. .done:
  1364.         add     esp, 262*2+4+8
  1365.         pop     ebp
  1366.         mov     ebx, [edx+4]
  1367.         xor     eax, eax
  1368.         dec     ecx
  1369.         js      @f
  1370.         mov     al, ERROR_END_OF_FILE
  1371. @@:
  1372.         pop     ecx esi edi
  1373.         ret
  1374.  
  1375. fat16_root_next:
  1376.         cmp     edi, buffer+0x200-0x20
  1377.         jae     fat16_root_next_sector
  1378.         add     edi, 0x20
  1379.         ret     ; CF=0
  1380. fat16_root_next_sector:
  1381. ; read next sector
  1382.         push    [longname_sec2]
  1383.         pop     [longname_sec1]
  1384.         push    ecx
  1385.         mov     ecx, [eax+4]
  1386.         push    ecx
  1387.         add     ecx, [ROOT_START]
  1388.         mov     [longname_sec2], ecx
  1389.         pop     ecx
  1390.         inc     ecx
  1391.         mov     [eax+4], ecx
  1392.         cmp     ecx, [ROOT_SECTORS]
  1393.         pop     ecx
  1394.         jae     fat16_root_first.readerr
  1395. fat16_root_first:
  1396.         mov     eax, [eax+4]
  1397.         add     eax, [ROOT_START]
  1398.         push    ebx
  1399.         mov     edi, buffer
  1400.         mov     ebx, edi
  1401.         call    hd_read
  1402.         pop     ebx
  1403.         cmp     [hd_error], 0
  1404.         jnz     .readerr
  1405.         ret     ; CF=0
  1406. .readerr:
  1407.         stc
  1408.         ret
  1409. fat16_root_begin_write:
  1410.         push    edi eax
  1411.         call    fat16_root_first
  1412.         pop     eax edi
  1413.         ret
  1414. fat16_root_end_write:
  1415.         pusha
  1416.         mov     eax, [eax+4]
  1417.         add     eax, [ROOT_START]
  1418.         mov     ebx, buffer
  1419.         call    hd_write
  1420.         popa
  1421.         ret
  1422. fat16_root_next_write:
  1423.         cmp     edi, buffer+0x200
  1424.         jae     @f
  1425.         ret
  1426. @@:
  1427.         call    fat16_root_end_write
  1428.         jmp     fat16_root_next_sector
  1429. fat16_root_extend_dir:
  1430.         stc
  1431.         ret
  1432.  
  1433. fat_notroot_next:
  1434.         cmp     edi, buffer+0x200-0x20
  1435.         jae     fat_notroot_next_sector
  1436.         add     edi, 0x20
  1437.         ret     ; CF=0
  1438. fat_notroot_next_sector:
  1439.         push    [longname_sec2]
  1440.         pop     [longname_sec1]
  1441.         push    eax
  1442.         call    fat_get_sector
  1443.         mov     [longname_sec2], eax
  1444.         pop     eax
  1445.         push    ecx
  1446.         mov     ecx, [eax+4]
  1447.         inc     ecx
  1448.         cmp     ecx, [SECTORS_PER_CLUSTER]
  1449.         jae     fat_notroot_next_cluster
  1450.         mov     [eax+4], ecx
  1451.         jmp     @f
  1452. fat_notroot_next_cluster:
  1453.         push    eax
  1454.         mov     eax, [eax]
  1455.         call    get_FAT
  1456.         mov     ecx, eax
  1457.         pop     eax
  1458.         cmp     [hd_error], 0
  1459.         jnz     fat_notroot_next_err
  1460.         cmp     ecx, [fatRESERVED]
  1461.         jae     fat_notroot_next_err
  1462.         mov     [eax], ecx
  1463.         and     dword [eax+4], 0
  1464. @@:
  1465.         pop     ecx
  1466. fat_notroot_first:
  1467.         call    fat_get_sector
  1468.         push    ebx
  1469.         mov     edi, buffer
  1470.         mov     ebx, edi
  1471.         call    hd_read
  1472.         pop     ebx
  1473.         cmp     [hd_error], 0
  1474.         jnz     @f
  1475.         ret     ; CF=0
  1476. fat_notroot_next_err:
  1477.         pop     ecx
  1478. @@:
  1479.         stc
  1480.         ret
  1481. fat_notroot_begin_write:
  1482.         push    eax edi
  1483.         call    fat_notroot_first
  1484.         pop     edi eax
  1485.         ret
  1486. fat_notroot_end_write:
  1487.         call    fat_get_sector
  1488.         push    ebx
  1489.         mov     ebx, buffer
  1490.         call    hd_write
  1491.         pop     ebx
  1492.         ret
  1493. fat_notroot_next_write:
  1494.         cmp     edi, buffer+0x200
  1495.         jae     @f
  1496.         ret
  1497. @@:
  1498.         push    eax
  1499.         call    fat_notroot_end_write
  1500.         pop     eax
  1501.         jmp     fat_notroot_next_sector
  1502. fat_notroot_extend_dir:
  1503.         push    eax
  1504.         call    get_free_FAT
  1505.         jnc     .found
  1506.         pop     eax
  1507.         ret     ; CF=1
  1508. .found:
  1509.         push    edx
  1510.         mov     edx, [fatEND]
  1511.         call    set_FAT
  1512.         mov     edx, eax
  1513.         mov     eax, [esp+4]
  1514.         mov     eax, [eax]
  1515.         push    edx
  1516.         call    set_FAT
  1517.         pop     edx
  1518.         cmp     [hd_error], 0
  1519.         jz      @f
  1520.         pop     edx
  1521.         pop     eax
  1522.         stc
  1523.         ret
  1524. @@:
  1525.         push    ecx
  1526.         or      ecx, -1
  1527.         call    add_disk_free_space
  1528. ; zero new cluster
  1529.         mov     ecx, 512/4
  1530.         mov     edi, buffer
  1531.         push    edi
  1532.         xor     eax, eax
  1533.         rep     stosd
  1534.         pop     edi
  1535.         pop     ecx
  1536.         mov     eax, [esp+4]
  1537.         mov     [eax], edx
  1538.         and     dword [eax+4], 0
  1539.         pop     edx
  1540.         mov     eax, [eax]
  1541.         dec     eax
  1542.         dec     eax
  1543.         push    ebx ecx
  1544.         mov     ecx, [SECTORS_PER_CLUSTER]
  1545.         imul    eax, ecx
  1546.         add     eax, [DATA_START]
  1547.         mov     ebx, edi
  1548. @@:
  1549.         call    hd_write
  1550.         inc     eax
  1551.         loop    @b
  1552.         pop     ecx ebx eax
  1553.         clc
  1554.         ret
  1555.  
  1556. fat_get_sector:
  1557.         push    ecx
  1558.         mov     ecx, [eax]
  1559.         dec     ecx
  1560.         dec     ecx
  1561.         imul    ecx, [SECTORS_PER_CLUSTER]
  1562.         add     ecx, [DATA_START]
  1563.         add     ecx, [eax+4]
  1564.         mov     eax, ecx
  1565.         pop     ecx
  1566.         ret
  1567.  
  1568. ;----------------------------------------------------------------
  1569. ;
  1570. ;  fs_HdRewrite - LFN variant for writing hard disk
  1571. ;
  1572. ;  esi  points to filename
  1573. ;  ebx  ignored (reserved)
  1574. ;  ecx  number of bytes to write, 0+
  1575. ;  edx  mem location to data
  1576. ;
  1577. ;  ret ebx = number of written bytes
  1578. ;      eax = 0 ok read or other = errormsg
  1579. ;
  1580. ;--------------------------------------------------------------
  1581. fshrad:
  1582.         mov     eax, ERROR_ACCESS_DENIED
  1583.         xor     ebx, ebx
  1584.         ret
  1585. fshrfs:
  1586.         mov     eax, ERROR_UNKNOWN_FS
  1587.         xor     ebx, ebx
  1588.         ret
  1589.  
  1590. fs_HdCreateFolder:
  1591.         mov     al, 1
  1592.         jmp     fs_HdRewrite.common
  1593.  
  1594. fs_HdRewrite:
  1595.         xor     eax, eax
  1596. .common:
  1597.         cmp     [fs_type], 1
  1598.         jz      ntfs_HdRewrite
  1599.         cmp     [fs_type], 16
  1600.         jz      @f
  1601.         cmp     [fs_type], 32
  1602.         jnz     fshrfs
  1603. @@:
  1604.         cmp     byte [esi], 0
  1605.         jz      fshrad
  1606.         pushad
  1607.         xor     edi, edi
  1608.         push    esi
  1609.         test    ebp, ebp
  1610.         jz      @f
  1611.         mov     esi, ebp
  1612. @@:
  1613.         lodsb
  1614.         test    al, al
  1615.         jz      @f
  1616.         cmp     al, '/'
  1617.         jnz     @b
  1618.         lea     edi, [esi-1]
  1619.         jmp     @b
  1620. @@:
  1621.         pop     esi
  1622.         test    edi, edi
  1623.         jnz     .noroot
  1624.         test    ebp, ebp
  1625.         jnz     .hasebp
  1626.         mov     ebp, [ROOT_CLUSTER]
  1627.         cmp     [fs_type], 32
  1628.         jz      .pushnotroot
  1629.         push    fat16_root_extend_dir
  1630.         push    fat16_root_end_write
  1631.         push    fat16_root_next_write
  1632.         push    fat16_root_begin_write
  1633.         xor     ebp, ebp
  1634.         push    ebp
  1635.         push    ebp
  1636.         push    fat16_root_first
  1637.         push    fat16_root_next
  1638.         jmp     .common1
  1639. .hasebp:
  1640.         mov     eax, ERROR_ACCESS_DENIED
  1641.         cmp     byte [ebp], 0
  1642.         jz      .ret1
  1643.         push    ebp
  1644.         xor     ebp, ebp
  1645.         call    hd_find_lfn
  1646.         pop     esi
  1647.         jc      .notfound0
  1648.         jmp     .common0
  1649. .noroot:
  1650.         mov     eax, ERROR_ACCESS_DENIED
  1651.         cmp     byte [edi+1], 0
  1652.         jz      .ret1
  1653. ; check existence
  1654.         mov     byte [edi], 0
  1655.         push    edi
  1656.         call    hd_find_lfn
  1657.         pop     esi
  1658.         mov     byte [esi], '/'
  1659.         jnc     @f
  1660. .notfound0:
  1661.         mov     eax, ERROR_FILE_NOT_FOUND
  1662. .ret1:
  1663.         mov     [esp+28], eax
  1664.         popad
  1665.         xor     ebx, ebx
  1666.         ret
  1667. @@:
  1668.         inc     esi
  1669. .common0:
  1670.         test    byte [edi+11], 0x10     ; must be directory
  1671.         mov     eax, ERROR_ACCESS_DENIED
  1672.         jz      .ret1
  1673.         mov     ebp, [edi+20-2]
  1674.         mov     bp, [edi+26]            ; ebp=cluster
  1675.         mov     eax, ERROR_FAT_TABLE
  1676.         cmp     ebp, 2
  1677.         jb      .ret1
  1678. .pushnotroot:
  1679.         push    fat_notroot_extend_dir
  1680.         push    fat_notroot_end_write
  1681.         push    fat_notroot_next_write
  1682.         push    fat_notroot_begin_write
  1683.         push    0
  1684.         push    ebp
  1685.         push    fat_notroot_first
  1686.         push    fat_notroot_next
  1687. .common1:
  1688.         call    fat_find_lfn
  1689.         jc      .notfound
  1690. ; found
  1691.         test    byte [edi+11], 10h
  1692.         jz      .exists_file
  1693. ; found directory; if we are creating directory, return OK,
  1694. ;                  if we are creating file, say "access denied"
  1695.         add     esp, 32
  1696.         popad
  1697.         test    al, al
  1698.         mov     eax, ERROR_ACCESS_DENIED
  1699.         jz      @f
  1700.         mov     al, 0
  1701. @@:
  1702.         xor     ebx, ebx
  1703.         ret
  1704. .exists_file:
  1705. ; found file; if we are creating directory, return "access denied",
  1706. ;             if we are creating file, delete existing file and continue
  1707.         cmp     byte [esp+32+28], 0
  1708.         jz      @f
  1709.         add     esp, 32
  1710.         popad
  1711.         mov     eax, ERROR_ACCESS_DENIED
  1712.         xor     ebx, ebx
  1713.         ret
  1714. @@:
  1715. ; delete FAT chain
  1716.         push    edi
  1717.         xor     eax, eax
  1718.         mov     dword [edi+28], eax     ; zero size
  1719.         xor     ecx, ecx
  1720.         mov     eax, [edi+20-2]
  1721.         mov     ax, [edi+26]
  1722.         mov     word [edi+20], cx
  1723.         mov     word [edi+26], cx
  1724.         test    eax, eax
  1725.         jz      .done1
  1726. @@:
  1727.         cmp     eax, [fatRESERVED]
  1728.         jae     .done1
  1729.         push    edx
  1730.         xor     edx, edx
  1731.         call    set_FAT
  1732.         mov     eax, edx
  1733.         pop     edx
  1734.         inc     ecx
  1735.         jmp     @b
  1736. .done1:
  1737.         pop     edi
  1738.         call    get_time_for_file
  1739.         mov     [edi+22], ax
  1740.         call    get_date_for_file
  1741.         mov     [edi+24], ax
  1742.         mov     [edi+18], ax
  1743.         or      byte [edi+11], 20h      ; set 'archive' attribute
  1744.         jmp     .doit
  1745. .notfound:
  1746. ; file is not found; generate short name
  1747.         call    fat_name_is_legal
  1748.         jc      @f
  1749.         add     esp, 32
  1750.         popad
  1751.         mov     eax, ERROR_FILE_NOT_FOUND
  1752.         xor     ebx, ebx
  1753.         ret
  1754. @@:
  1755.         sub     esp, 12
  1756.         mov     edi, esp
  1757.         call    fat_gen_short_name
  1758. .test_short_name_loop:
  1759.         push    esi edi ecx
  1760.         mov     esi, edi
  1761.         lea     eax, [esp+12+12+8]
  1762.         mov     [eax], ebp
  1763.         and     dword [eax+4], 0
  1764.         call    dword [eax-4]
  1765.         jc      .found
  1766. .test_short_name_entry:
  1767.         cmp     byte [edi+11], 0xF
  1768.         jz      .test_short_name_cont
  1769.         mov     ecx, 11
  1770.         push    esi edi
  1771.         repz    cmpsb
  1772.         pop     edi esi
  1773.         jz      .short_name_found
  1774. .test_short_name_cont:
  1775.         lea     eax, [esp+12+12+8]
  1776.         call    dword [eax-8]
  1777.         jnc     .test_short_name_entry
  1778.         jmp     .found
  1779. .short_name_found:
  1780.         pop     ecx edi esi
  1781.         call    fat_next_short_name
  1782.         jnc     .test_short_name_loop
  1783. .disk_full:
  1784.         add     esp, 12+32
  1785.         popa
  1786.         mov     eax, ERROR_DISK_FULL
  1787.         xor     ebx, ebx
  1788.         ret
  1789. .found:
  1790.         pop     ecx edi esi
  1791. ; now find space in directory
  1792. ; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
  1793.         mov     al, '~'
  1794.         push    ecx edi
  1795.         mov     ecx, 8
  1796.         repnz   scasb
  1797.         push    1
  1798.         pop     eax     ; 1 entry
  1799.         jnz     .notilde
  1800. ; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
  1801.         xor     eax, eax
  1802. @@:
  1803.         cmp     byte [esi], 0
  1804.         jz      @f
  1805.         inc     esi
  1806.         inc     eax
  1807.         jmp     @b
  1808. @@:
  1809.         sub     esi, eax
  1810.         add     eax, 12+13
  1811.         mov     ecx, 13
  1812.         push    edx
  1813.         cdq
  1814.         div     ecx
  1815.         pop     edx
  1816. .notilde:
  1817.         push    -1
  1818.         push    -1
  1819.         push    -1
  1820. ; find <eax> successive entries in directory
  1821.         xor     ecx, ecx
  1822.         push    eax
  1823.         lea     eax, [esp+16+8+12+8]
  1824.         mov     [eax], ebp
  1825.         and     dword [eax+4], 0
  1826.         call    dword [eax-4]
  1827.         pop     eax
  1828.         jnc     .scan_dir
  1829. .fsfrfe3:
  1830.         add     esp, 12+8+12+32
  1831.         popad
  1832.         mov     eax, 11
  1833.         xor     ebx, ebx
  1834.         ret
  1835. .scan_dir:
  1836.         cmp     byte [edi], 0
  1837.         jz      .free
  1838.         cmp     byte [edi], 0xE5
  1839.         jz      .free
  1840.         xor     ecx, ecx
  1841. .scan_cont:
  1842.         push    eax
  1843.         lea     eax, [esp+16+8+12+8]
  1844.         call    dword [eax-8]
  1845.         pop     eax
  1846.         jnc     .scan_dir
  1847.         cmp     [hd_error], 0
  1848.         jnz     .fsfrfe3
  1849.         push    eax
  1850.         lea     eax, [esp+16+8+12+8]
  1851.         call    dword [eax+20]          ; extend directory
  1852.         pop     eax
  1853.         jnc     .scan_dir
  1854.         add     esp, 12+8+12+32
  1855.         popad
  1856.         mov     eax, ERROR_DISK_FULL
  1857.         xor     ebx, ebx
  1858.         ret
  1859. .free:
  1860.         test    ecx, ecx
  1861.         jnz     @f
  1862.         mov     [esp], edi
  1863.         mov     ecx, [esp+12+8+12+8]
  1864.         mov     [esp+4], ecx
  1865.         mov     ecx, [esp+12+8+12+12]
  1866.         mov     [esp+8], ecx
  1867.         xor     ecx, ecx
  1868. @@:
  1869.         inc     ecx
  1870.         cmp     ecx, eax
  1871.         jb      .scan_cont
  1872. ; found!
  1873. ; calculate name checksum
  1874.         push    esi ecx
  1875.         mov     esi, [esp+8+12]
  1876.         mov     ecx, 11
  1877.         xor     eax, eax
  1878. @@:
  1879.         ror     al, 1
  1880.         add     al, [esi]
  1881.         inc     esi
  1882.         loop    @b
  1883.         pop     ecx esi
  1884.         pop     edi
  1885.         pop     dword [esp+8+12+12]
  1886.         pop     dword [esp+8+12+12]
  1887. ; edi points to first entry in free chunk
  1888.         dec     ecx
  1889.         jz      .nolfn
  1890.         push    esi
  1891.         push    eax
  1892.         lea     eax, [esp+8+8+12+8]
  1893.         call    dword [eax+8]         ; begin write
  1894.         mov     al, 40h
  1895. .writelfn:
  1896.         or      al, cl
  1897.         mov     esi, [esp+4]
  1898.         push    ecx
  1899.         dec     ecx
  1900.         imul    ecx, 13
  1901.         add     esi, ecx
  1902.         stosb
  1903.         mov     cl, 5
  1904.         call    fs_RamdiskRewrite.read_symbols
  1905.         mov     ax, 0xF
  1906.         stosw
  1907.         mov     al, [esp+4]
  1908.         stosb
  1909.         mov     cl, 6
  1910.         call    fs_RamdiskRewrite.read_symbols
  1911.         xor     eax, eax
  1912.         stosw
  1913.         mov     cl, 2
  1914.         call    fs_RamdiskRewrite.read_symbols
  1915.         pop     ecx
  1916.         lea     eax, [esp+8+8+12+8]
  1917.         call    dword [eax+12]         ; next write
  1918.         xor     eax, eax
  1919.         loop    .writelfn
  1920.         pop     eax
  1921.         pop     esi
  1922. ;        lea     eax, [esp+8+12+8]
  1923. ;        call    dword [eax+16]          ; end write
  1924. .nolfn:
  1925.         xchg    esi, [esp]
  1926.         mov     ecx, 11
  1927.         rep     movsb
  1928.         mov     word [edi], 20h         ; attributes
  1929.         sub     edi, 11
  1930.         pop     esi ecx
  1931.         add     esp, 12
  1932.         mov     byte [edi+13], 0        ; tenths of a second at file creation time
  1933.         call    get_time_for_file
  1934.         mov     [edi+14], ax            ; creation time
  1935.         mov     [edi+22], ax            ; last write time
  1936.         call    get_date_for_file
  1937.         mov     [edi+16], ax            ; creation date
  1938.         mov     [edi+24], ax            ; last write date
  1939.         mov     [edi+18], ax            ; last access date
  1940.         xor     ecx, ecx
  1941.         mov     word [edi+20], cx       ; high word of cluster
  1942.         mov     word [edi+26], cx       ; low word of cluster - to be filled
  1943.         mov     dword [edi+28], ecx     ; file size - to be filled
  1944.         cmp     byte [esp+32+28], cl
  1945.         jz      .doit
  1946. ; create directory
  1947.         mov     byte [edi+11], 10h      ; attributes: folder
  1948.         mov     edx, edi
  1949.         lea     eax, [esp+8]
  1950.         call    dword [eax+16]  ; flush directory
  1951.         push    ecx
  1952.         mov     ecx, [SECTORS_PER_CLUSTER]
  1953.         shl     ecx, 9
  1954.         jmp     .doit2
  1955. .doit:
  1956.         lea     eax, [esp+8]
  1957.         call    dword [eax+16]  ; flush directory
  1958.         push    ecx
  1959.         mov     ecx, [esp+4+32+24]
  1960. .doit2:
  1961.         push    ecx
  1962.         push    edi
  1963.         mov     esi, edx
  1964.         test    ecx, ecx
  1965.         jz      .done
  1966.         call    get_free_FAT
  1967.         jc      .diskfull
  1968.         push    eax
  1969.         mov     [edi+26], ax
  1970.         shr     eax, 16
  1971.         mov     [edi+20], ax
  1972.         lea     eax, [esp+16+8]
  1973.         call    dword [eax+16]  ; flush directory
  1974.         pop     eax
  1975.         push    edx
  1976.         mov     edx, [fatEND]
  1977.         call    set_FAT
  1978.         pop     edx
  1979. .write_cluster:
  1980.         push    eax
  1981.         dec     eax
  1982.         dec     eax
  1983.         mov     ebp, [SECTORS_PER_CLUSTER]
  1984.         imul    eax, ebp
  1985.         add     eax, [DATA_START]
  1986. ; write data
  1987. .write_sector:
  1988.         cmp     byte [esp+16+32+28], 0
  1989.         jnz     .writedir
  1990.         mov     ecx, 512
  1991.         cmp     dword [esp+8], ecx
  1992.         jb      .writeshort
  1993. ; we can write directly from given buffer
  1994.         mov     ebx, esi
  1995.         add     esi, ecx
  1996.         jmp     .writecommon
  1997. .writeshort:
  1998.         mov     ecx, [esp+8]
  1999.         push    ecx
  2000.         mov     edi, buffer
  2001.         mov     ebx, edi
  2002.         rep     movsb
  2003. .writedircont:
  2004.         mov     ecx, buffer+0x200
  2005.         sub     ecx, edi
  2006.         push    eax
  2007.         xor     eax, eax
  2008.         rep     stosb
  2009.         pop     eax
  2010.         pop     ecx
  2011. .writecommon:
  2012.         call    hd_write
  2013.         cmp     [hd_error], 0
  2014.         jnz     .writeerr
  2015.         inc     eax
  2016.         sub     dword [esp+8], ecx
  2017.         jz      .writedone
  2018.         dec     ebp
  2019.         jnz     .write_sector
  2020. ; allocate new cluster
  2021.         pop     eax
  2022.         mov     ecx, eax
  2023.         call    get_free_FAT
  2024.         jc      .diskfull
  2025.         push    edx
  2026.         mov     edx, [fatEND]
  2027.         call    set_FAT
  2028.         xchg    eax, ecx
  2029.         mov     edx, ecx
  2030.         call    set_FAT
  2031.         pop     edx
  2032.         xchg    eax, ecx
  2033.         jmp     .write_cluster
  2034. .diskfull:
  2035.         mov     eax, ERROR_DISK_FULL
  2036.         jmp     .ret
  2037. .writeerr:
  2038.         pop     eax
  2039.         sub     esi, ecx
  2040.         mov     eax, 11
  2041.         jmp     .ret
  2042. .writedone:
  2043.         pop     eax
  2044. .done:
  2045.         xor     eax, eax
  2046. .ret:
  2047.         pop     edi ecx
  2048.         mov     ebx, esi
  2049.         sub     ebx, edx
  2050.         pop     ebp
  2051.         mov     [esp+32+28], eax
  2052.         lea     eax, [esp+8]
  2053.         call    dword [eax+8]
  2054.         mov     [edi+28], ebx
  2055.         call    dword [eax+16]
  2056.         mov     [esp+32+16], ebx
  2057.         lea     eax, [ebx+511]
  2058.         shr     eax, 9
  2059.         mov     ecx, [SECTORS_PER_CLUSTER]
  2060.         lea     eax, [eax+ecx-1]
  2061.         xor     edx, edx
  2062.         div     ecx
  2063.         mov     ecx, ebp
  2064.         sub     ecx, eax
  2065.         call    add_disk_free_space
  2066.         add     esp, 32
  2067.         call    update_disk
  2068.         popad
  2069.         ret
  2070. .writedir:
  2071.         push    512
  2072.         mov     edi, buffer
  2073.         mov     ebx, edi
  2074.         mov     ecx, [SECTORS_PER_CLUSTER]
  2075.         shl     ecx, 9
  2076.         cmp     ecx, [esp+12]
  2077.         jnz     .writedircont
  2078.         dec     dword [esp+16]
  2079.         push    esi
  2080.         mov     ecx, 32/4
  2081.         rep     movsd
  2082.         pop     esi
  2083.         mov     dword [edi-32], '.   '
  2084.         mov     dword [edi-32+4], '    '
  2085.         mov     dword [edi-32+8], '    '
  2086.         mov     byte [edi-32+11], 10h
  2087.         push    esi
  2088.         mov     ecx, 32/4
  2089.         rep     movsd
  2090.         pop     esi
  2091.         mov     dword [edi-32], '..  '
  2092.         mov     dword [edi-32+4], '    '
  2093.         mov     dword [edi-32+8], '    '
  2094.         mov     byte [edi-32+11], 10h
  2095.         mov     ecx, [esp+20+8]
  2096.         cmp     ecx, [ROOT_CLUSTER]
  2097.         jnz     @f
  2098.         xor     ecx, ecx
  2099. @@:
  2100.         mov     word [edi-32+26], cx
  2101.         shr     ecx, 16
  2102.         mov     [edi-32+20], cx
  2103.         jmp     .writedircont
  2104.  
  2105. ;----------------------------------------------------------------
  2106. ;
  2107. ;  fs_HdWrite - LFN variant for writing to hard disk
  2108. ;
  2109. ;  esi  points to filename
  2110. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  2111. ;       may be ebx=0 - start from first byte
  2112. ;  ecx  number of bytes to write, 0+
  2113. ;  edx  mem location to data
  2114. ;
  2115. ;  ret ebx = bytes written (maybe 0)
  2116. ;      eax = 0 ok write or other = errormsg
  2117. ;
  2118. ;--------------------------------------------------------------
  2119. fs_HdWrite.access_denied:
  2120.         push    ERROR_ACCESS_DENIED
  2121. fs_HdWrite.ret0:
  2122.         pop     eax
  2123.         xor     ebx, ebx
  2124.         ret
  2125.  
  2126. fs_HdWrite.ret11:
  2127.         push    11
  2128.         jmp     fs_HdWrite.ret0
  2129.  
  2130. fs_HdWrite:
  2131.         cmp     [fs_type], 1
  2132.         jz      ntfs_HdWrite
  2133.         cmp     [fs_type], 16
  2134.         jz      @f
  2135.         cmp     [fs_type], 32
  2136.         jz      @f
  2137.         push    ERROR_UNKNOWN_FS
  2138.         jmp     .ret0
  2139. @@:
  2140.         cmp     byte [esi], 0
  2141.         jz      .access_denied
  2142.         pushad
  2143.         call    hd_find_lfn
  2144.         pushfd
  2145.         cmp     [hd_error], 0
  2146.         jz      @f
  2147.         popfd
  2148.         popad
  2149.         push    11
  2150.         jmp     .ret0
  2151. @@:
  2152.         popfd
  2153.         jnc     .found
  2154.         popad
  2155.         push    ERROR_FILE_NOT_FOUND
  2156.         jmp     .ret0
  2157. .found:
  2158. ; FAT does not support files larger than 4GB
  2159.         test    ebx, ebx
  2160.         jz      .l1
  2161.         cmp     dword [ebx+4], 0
  2162.         jz      @f
  2163. .eof:
  2164.         popad
  2165.         push    ERROR_END_OF_FILE
  2166.         jmp     .ret0
  2167. @@:
  2168.         mov     ebx, [ebx]
  2169. .l1:
  2170. ; now edi points to direntry, ebx=start byte to write,
  2171. ; ecx=number of bytes to write, edx=data pointer
  2172.  
  2173. ; extend file if needed
  2174.         add     ecx, ebx
  2175.         jc      .eof    ; FAT does not support files larger than 4GB
  2176.         push    eax     ; save directory sector
  2177.         push    0       ; return value=0
  2178.  
  2179.         call    get_time_for_file
  2180.         mov     [edi+22], ax            ; last write time
  2181.         call    get_date_for_file
  2182.         mov     [edi+24], ax            ; last write date
  2183.         mov     [edi+18], ax            ; last access date
  2184.  
  2185.         push    dword [edi+28]          ; save current file size
  2186.         cmp     ecx, [edi+28]
  2187.         jbe     .length_ok
  2188.         cmp     ecx, ebx
  2189.         jz      .length_ok
  2190.         call    hd_extend_file
  2191.         jnc     .length_ok
  2192.         mov     [esp+4], eax
  2193. ; hd_extend_file can return three error codes: FAT table error, device error or disk full.
  2194. ; First two cases are fatal errors, in third case we may write some data
  2195.         cmp     al, ERROR_DISK_FULL
  2196.         jz      .disk_full
  2197.         pop     eax
  2198.         pop     eax
  2199.         mov     [esp+4+28], eax
  2200.         pop     eax
  2201.         popad
  2202.         xor     ebx, ebx
  2203.         ret
  2204. .disk_full:
  2205. ; correct number of bytes to write
  2206.         mov     ecx, [edi+28]
  2207.         cmp     ecx, ebx
  2208.         ja      .length_ok
  2209. .ret:
  2210.         call    update_disk
  2211.         cmp     [hd_error], 0
  2212.         jz      @f
  2213.         mov     byte [esp+4], 11
  2214. @@:
  2215.         pop     eax
  2216.         pop     eax
  2217.         mov     [esp+4+28], eax ; eax=return value
  2218.         pop     eax
  2219.         sub     edx, [esp+20]
  2220.         mov     [esp+16], edx   ; ebx=number of written bytes
  2221.         popad
  2222.         ret
  2223. .length_ok:
  2224.         mov     esi, [edi+28]
  2225.         mov     eax, [edi+20-2]
  2226.         mov     ax, [edi+26]
  2227.         mov     edi, eax        ; edi=current cluster
  2228.         xor     ebp, ebp        ; ebp=current sector in cluster
  2229. ; save directory
  2230.         mov     eax, [esp+8]
  2231.         push    ebx
  2232.         mov     ebx, buffer
  2233.         call    hd_write
  2234.         pop     ebx
  2235.         cmp     [hd_error], 0
  2236.         jz      @f
  2237. .device_err:
  2238.         mov     byte [esp+4], 11
  2239.         jmp     .ret
  2240. @@:
  2241.  
  2242. ; now ebx=start pos, ecx=end pos, both lie inside file
  2243.         sub     ecx, ebx
  2244.         jz      .ret
  2245. .write_loop:
  2246. ; skip unmodified sectors
  2247.         cmp     dword [esp], 0x200
  2248.         jb      .modify
  2249.         sub     ebx, 0x200
  2250.         jae     .skip
  2251.         add     ebx, 0x200
  2252. .modify:
  2253. ; get length of data in current sector
  2254.         push    ecx
  2255.         sub     ebx, 0x200
  2256.         jb      .hasdata
  2257.         neg     ebx
  2258.         xor     ecx, ecx
  2259.         jmp     @f
  2260. .hasdata:
  2261.         neg     ebx
  2262.         cmp     ecx, ebx
  2263.         jbe     @f
  2264.         mov     ecx, ebx
  2265. @@:
  2266. ; get current sector number
  2267.         mov     eax, edi
  2268.         dec     eax
  2269.         dec     eax
  2270.         imul    eax, [SECTORS_PER_CLUSTER]
  2271.         add     eax, [DATA_START]
  2272.         add     eax, ebp
  2273. ; load sector if needed
  2274.         cmp     dword [esp+4], 0        ; we don't need to read uninitialized data
  2275.         jz      .noread
  2276.         cmp     ecx, 0x200      ; we don't need to read sector if it is fully rewritten
  2277.         jz      .noread
  2278.         cmp     ecx, esi        ; (same for the last sector)
  2279.         jz      .noread
  2280.         push    ebx
  2281.         mov     ebx, buffer
  2282.         call    hd_read
  2283.         pop     ebx
  2284.         cmp     [hd_error], 0
  2285.         jz      @f
  2286. .device_err2:
  2287.         pop     ecx
  2288.         jmp     .device_err
  2289. @@:
  2290. .noread:
  2291. ; zero uninitialized data if file was extended (because hd_extend_file does not this)
  2292.         push    eax ecx edi
  2293.         xor     eax, eax
  2294.         mov     ecx, 0x200
  2295.         sub     ecx, [esp+4+12]
  2296.         jbe     @f
  2297.         mov     edi, buffer
  2298.         add     edi, [esp+4+12]
  2299.         rep     stosb
  2300. @@:
  2301. ; zero uninitialized data in the last sector
  2302.         mov     ecx, 0x200
  2303.         sub     ecx, esi
  2304.         jbe     @f
  2305.         mov     edi, buffer
  2306.         add     edi, esi
  2307.         rep     stosb
  2308. @@:
  2309.         pop     edi ecx
  2310. ; copy new data
  2311.         mov     eax, edx
  2312.         neg     ebx
  2313.         jecxz   @f
  2314.         add     ebx, buffer+0x200
  2315.         call    memmove
  2316.         xor     ebx, ebx
  2317. @@:
  2318.         pop     eax
  2319. ; save sector
  2320.         push    ebx
  2321.         mov     ebx, buffer
  2322.         call    hd_write
  2323.         pop     ebx
  2324.         cmp     [hd_error], 0
  2325.         jnz     .device_err2
  2326.         add     edx, ecx
  2327.         sub     [esp], ecx
  2328.         pop     ecx
  2329.         jz      .ret
  2330. .skip:
  2331. ; next sector
  2332.         inc     ebp
  2333.         cmp     ebp, [SECTORS_PER_CLUSTER]
  2334.         jb      @f
  2335.         xor     ebp, ebp
  2336.         mov     eax, edi
  2337.         call    get_FAT
  2338.         mov     edi, eax
  2339.         cmp     [hd_error], 0
  2340.         jnz     .device_err
  2341. @@:
  2342.         sub     esi, 0x200
  2343.         jae     @f
  2344.         xor     esi, esi
  2345. @@:
  2346.         sub     dword [esp], 0x200
  2347.         jae     @f
  2348.         and     dword [esp], 0
  2349. @@:     jmp     .write_loop
  2350.  
  2351. hd_extend_file.zero_size:
  2352.         xor     eax, eax
  2353.         jmp     hd_extend_file.start_extend
  2354.  
  2355. ; extends file on hd to given size (new data area is undefined)
  2356. ; in: edi->direntry, ecx=new size
  2357. ; out: CF=0 => OK, eax=0
  2358. ;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL or 11)
  2359. hd_extend_file:
  2360.         push    ebp
  2361.         mov     ebp, [SECTORS_PER_CLUSTER]
  2362.         imul    ebp, [BYTES_PER_SECTOR]
  2363.         push    ecx
  2364. ; find the last cluster of file
  2365.         mov     eax, [edi+20-2]
  2366.         mov     ax, [edi+26]
  2367.         mov     ecx, [edi+28]
  2368.         jecxz   .zero_size
  2369. .last_loop:
  2370.         sub     ecx, ebp
  2371.         jbe     .last_found
  2372.         call    get_FAT
  2373.         cmp     [hd_error], 0
  2374.         jz      @f
  2375. .device_err:
  2376.         pop     ecx
  2377. .device_err2:
  2378.         pop     ebp
  2379.         push    11
  2380. .ret_err:
  2381.         pop     eax
  2382.         stc
  2383.         ret
  2384. @@:
  2385.         cmp     eax, 2
  2386.         jb      .fat_err
  2387.         cmp     eax, [fatRESERVED]
  2388.         jb      .last_loop
  2389. .fat_err:
  2390.         pop     ecx ebp
  2391.         push    ERROR_FAT_TABLE
  2392.         jmp     .ret_err
  2393. .last_found:
  2394.         push    eax
  2395.         call    get_FAT
  2396.         cmp     [hd_error], 0
  2397.         jz      @f
  2398.         pop     eax
  2399.         jmp     .device_err
  2400. @@:
  2401.         cmp     eax, [fatRESERVED]
  2402.         pop     eax
  2403.         jb      .fat_err
  2404. ; set length to full number of clusters
  2405.         sub     [edi+28], ecx
  2406. .start_extend:
  2407.         pop     ecx
  2408. ; now do extend
  2409.         push    edx
  2410.         mov     edx, 2          ; start scan from cluster 2
  2411. .extend_loop:
  2412.         cmp     [edi+28], ecx
  2413.         jae     .extend_done
  2414. ; add new cluster
  2415.         push    eax
  2416.         call    get_free_FAT
  2417.         jc      .disk_full
  2418.         mov     edx, [fatEND]
  2419.         call    set_FAT
  2420.         mov     edx, eax
  2421.         pop     eax
  2422.         test    eax, eax
  2423.         jz      .first_cluster
  2424.         push    edx
  2425.         call    set_FAT
  2426.         pop     edx
  2427.         jmp     @f
  2428. .first_cluster:
  2429.         ror     edx, 16
  2430.         mov     [edi+20], dx
  2431.         ror     edx, 16
  2432.         mov     [edi+26], dx
  2433. @@:
  2434.         push    ecx
  2435.         mov     ecx, -1
  2436.         call    add_disk_free_space
  2437.         pop     ecx
  2438.         mov     eax, edx
  2439.         cmp     [hd_error], 0
  2440.         jnz     .device_err3
  2441.         add     [edi+28], ebp
  2442.         jmp     .extend_loop
  2443. .extend_done:
  2444.         mov     [edi+28], ecx
  2445.         pop     edx ebp
  2446.         xor     eax, eax        ; CF=0
  2447.         ret
  2448. .device_err3:
  2449.         pop     edx
  2450.         jmp     .device_err2
  2451. .disk_full:
  2452.         pop     eax edx ebp
  2453.         push    ERROR_DISK_FULL
  2454.         pop     eax
  2455.         cmp     [hd_error], 0
  2456.         jz      @f
  2457.         mov     al, 11
  2458. @@:     stc
  2459.         ret
  2460.  
  2461. ;----------------------------------------------------------------
  2462. ;
  2463. ;  fs_HdSetFileEnd - set end of file on hard disk
  2464. ;
  2465. ;  esi  points to filename
  2466. ;  ebx  points to 64-bit number = new file size
  2467. ;  ecx  ignored (reserved)
  2468. ;  edx  ignored (reserved)
  2469. ;
  2470. ;  ret eax = 0 ok or other = errormsg
  2471. ;
  2472. ;--------------------------------------------------------------
  2473. fs_HdSetFileEnd:
  2474.         cmp     [fs_type], 1
  2475.         jz      ntfs_HdSetFileEnd
  2476.         cmp     [fs_type], 16
  2477.         jz      @f
  2478.         cmp     [fs_type], 32
  2479.         jz      @f
  2480.         push    ERROR_UNKNOWN_FS
  2481. .ret:
  2482.         pop     eax
  2483.         ret
  2484. @@:
  2485.         cmp     byte [esi], 0
  2486.         jnz     @f
  2487. .access_denied:
  2488.         push    ERROR_ACCESS_DENIED
  2489.         jmp     .ret
  2490. @@:
  2491.         push    edi
  2492.         call    hd_find_lfn
  2493.         pushfd
  2494.         cmp     [hd_error], 0
  2495.         jz      @f
  2496.         popfd
  2497.         push    11
  2498.         jmp     .ret
  2499. @@:
  2500.         popfd
  2501.         jnc     @f
  2502.         pop     edi
  2503.         push    ERROR_FILE_NOT_FOUND
  2504.         jmp     .ret
  2505. @@:
  2506. ; must not be directory
  2507.         test    byte [edi+11], 10h
  2508.         jz      @f
  2509.         pop     edi
  2510.         jmp     .access_denied
  2511. @@:
  2512. ; file size must not exceed 4 Gb
  2513.         cmp     dword [ebx+4], 0
  2514.         jz      @f
  2515.         pop     edi
  2516.         push    ERROR_END_OF_FILE
  2517.         jmp     .ret
  2518. @@:
  2519.         push    eax     ; save directory sector
  2520. ; set file modification date/time to current
  2521.         call    fat_update_datetime
  2522.         mov     eax, [ebx]
  2523.         cmp     eax, [edi+28]
  2524.         jb      .truncate
  2525.         ja      .expand
  2526.         pop     eax
  2527.         mov     ebx, buffer
  2528.         call    hd_write
  2529.         pop     edi
  2530.         xor     eax, eax
  2531.         cmp     [hd_error], 0
  2532.         jz      @f
  2533.         mov     al, 11
  2534. @@:
  2535.         ret
  2536. .expand:
  2537.         push    ebx ebp ecx
  2538.         push    dword [edi+28]  ; save old size
  2539.         mov     ecx, eax
  2540.         call    hd_extend_file
  2541.         push    eax             ; return code
  2542.         jnc     .expand_ok
  2543.         cmp     al, ERROR_DISK_FULL
  2544.         jz      .disk_full
  2545. .pop_ret:
  2546.         call    update_disk
  2547.         pop     eax ecx ebp ebx ecx edi edi
  2548.         ret
  2549. .expand_ok:
  2550. .disk_full:
  2551. ; save directory
  2552.         mov     eax, [edi+28]
  2553.         xchg    eax, [esp+20]
  2554.         mov     ebx, buffer
  2555.         call    hd_write
  2556.         mov     eax, [edi+20-2]
  2557.         mov     ax, [edi+26]
  2558.         mov     edi, eax
  2559.         cmp     [hd_error], 0
  2560.         jz      @f
  2561. .pop_ret11:
  2562.         mov     byte [esp], 11
  2563.         jmp     .pop_ret
  2564. @@:
  2565. ; now zero new data
  2566.         xor     ebp, ebp
  2567. ; edi=current cluster, ebp=sector in cluster
  2568. ; [esp+20]=new size, [esp+4]=old size, [esp]=return code
  2569. .zero_loop:
  2570.         sub     dword [esp+4], 0x200
  2571.         jae     .next_cluster
  2572.         lea     eax, [edi-2]
  2573.         imul    eax, [SECTORS_PER_CLUSTER]
  2574.         add     eax, [DATA_START]
  2575.         add     eax, ebp
  2576.         cmp     dword [esp+4], -0x200
  2577.         jz      .noread
  2578.         mov     ebx, buffer
  2579.         call    hd_read
  2580.         cmp     [hd_error], 0
  2581.         jnz     .err_next
  2582. .noread:
  2583.         mov     ecx, [esp+4]
  2584.         neg     ecx
  2585.         push    edi
  2586.         mov     edi, buffer+0x200
  2587.         add     edi, [esp+8]
  2588.         push    eax
  2589.         xor     eax, eax
  2590.         mov     [esp+12], eax
  2591.         rep     stosb
  2592.         pop     eax
  2593.         pop     edi
  2594.         call    hd_write
  2595.         cmp     [hd_error], 0
  2596.         jz      .next_cluster
  2597. .err_next:
  2598.         mov     byte [esp], 11
  2599. .next_cluster:
  2600.         sub     dword [esp+20], 0x200
  2601.         jbe     .pop_ret
  2602.         inc     ebp
  2603.         cmp     ebp, [SECTORS_PER_CLUSTER]
  2604.         jb      .zero_loop
  2605.         xor     ebp, ebp
  2606.         mov     eax, edi
  2607.         call    get_FAT
  2608.         mov     edi, eax
  2609.         cmp     [hd_error], 0
  2610.         jnz     .pop_ret11
  2611.         jmp     .zero_loop
  2612. .truncate:
  2613.         mov     [edi+28], eax
  2614.         push    ecx
  2615.         mov     ecx, [edi+20-2]
  2616.         mov     cx, [edi+26]
  2617.         push    eax
  2618.         test    eax, eax
  2619.         jz      .zero_size
  2620. ; find new last cluster
  2621. @@:
  2622.         mov     eax, [SECTORS_PER_CLUSTER]
  2623.         shl     eax, 9
  2624.         sub     [esp], eax
  2625.         jbe     @f
  2626.         mov     eax, ecx
  2627.         call    get_FAT
  2628.         mov     ecx, eax
  2629.         cmp     [hd_error], 0
  2630.         jz      @b
  2631. .device_err3:
  2632.         pop     eax ecx eax edi
  2633.         push    11
  2634.         pop     eax
  2635.         ret
  2636. @@:
  2637. ; we will zero data at the end of last sector - remember it
  2638.         push    ecx
  2639. ; terminate FAT chain
  2640.         push    edx
  2641.         mov     eax, ecx
  2642.         mov     edx, [fatEND]
  2643.         call    set_FAT
  2644.         mov     eax, edx
  2645.         pop     edx
  2646.         cmp     [hd_error], 0
  2647.         jz      @f
  2648. .device_err4:
  2649.         pop     ecx
  2650.         jmp     .device_err3
  2651. .zero_size:
  2652.         and     word [edi+20], 0
  2653.         and     word [edi+26], 0
  2654.         push    0
  2655.         mov     eax, ecx
  2656. @@:
  2657. ; delete FAT chain
  2658.         call    clear_cluster_chain
  2659.         cmp     [hd_error], 0
  2660.         jnz     .device_err4
  2661. ; save directory
  2662.         mov     eax, [esp+12]
  2663.         push    ebx
  2664.         mov     ebx, buffer
  2665.         call    hd_write
  2666.         pop     ebx
  2667.         cmp     [hd_error], 0
  2668.         jnz     .device_err4
  2669. ; zero last sector, ignore errors
  2670.         pop     ecx
  2671.         pop     eax
  2672.         dec     ecx
  2673.         imul    ecx, [SECTORS_PER_CLUSTER]
  2674.         add     ecx, [DATA_START]
  2675.         push    eax
  2676.         sar     eax, 9
  2677.         add     ecx, eax
  2678.         pop     eax
  2679.         and     eax, 0x1FF
  2680.         jz      .truncate_done
  2681.         push    ebx eax
  2682.         mov     eax, ecx
  2683.         mov     ebx, buffer
  2684.         call    hd_read
  2685.         pop     eax
  2686.         lea     edi, [buffer+eax]
  2687.         push    ecx
  2688.         mov     ecx, 0x200
  2689.         sub     ecx, eax
  2690.         xor     eax, eax
  2691.         rep     stosb
  2692.         pop     eax
  2693.         call    hd_write
  2694.         pop     ebx
  2695. .truncate_done:
  2696.         pop     ecx eax edi
  2697.         call    update_disk
  2698.         xor     eax, eax
  2699.         cmp     [hd_error], 0
  2700.         jz      @f
  2701.         mov     al, 11
  2702. @@:
  2703.         ret
  2704.  
  2705. fs_HdGetFileInfo:
  2706.         cmp     [fs_type], 1
  2707.         jz      ntfs_HdGetFileInfo
  2708.         cmp     [fs_type], 16
  2709.         jz      @f
  2710.         cmp     [fs_type], 32
  2711.         jz      @f
  2712.         mov     eax, ERROR_UNKNOWN_FS
  2713.         ret
  2714. @@:
  2715.         cmp     byte [esi], 0
  2716.         jnz     @f
  2717.         mov     eax, 2
  2718.         ret
  2719. @@:
  2720.         push    edi
  2721.         call    hd_find_lfn
  2722.         pushfd
  2723.         cmp     [hd_error], 0
  2724.         jz      @f
  2725.         popfd
  2726.         pop     edi
  2727.         mov     eax, 11
  2728.         ret
  2729. @@:
  2730.         popfd
  2731.         jmp     fs_GetFileInfo_finish
  2732.  
  2733. fs_HdSetFileInfo:
  2734.         cmp     [fs_type], 1
  2735.         jz      ntfs_HdSetFileInfo
  2736.         cmp     [fs_type], 16
  2737.         jz      @f
  2738.         cmp     [fs_type], 32
  2739.         jz      @f
  2740.         mov     eax, ERROR_UNKNOWN_FS
  2741.         ret
  2742. @@:
  2743.         cmp     byte [esi], 0
  2744.         jnz     @f
  2745.         mov     eax, 2
  2746.         ret
  2747. @@:
  2748.         push    edi
  2749.         call    hd_find_lfn
  2750.         pushfd
  2751.         cmp     [hd_error], 0
  2752.         jz      @f
  2753.         popfd
  2754.         pop     edi
  2755.         mov     eax, 11
  2756.         ret
  2757. @@:
  2758.         popfd
  2759.         jnc     @f
  2760.         pop     edi
  2761.         mov     eax, ERROR_FILE_NOT_FOUND
  2762.         ret
  2763. @@:
  2764.         push    eax
  2765.         call    bdfe_to_fat_entry
  2766.         pop     eax
  2767.         mov     ebx, buffer
  2768.         call    hd_write
  2769.         call    update_disk
  2770.         pop     edi
  2771.         xor     eax, eax
  2772.         ret
  2773.  
  2774. ;----------------------------------------------------------------
  2775. ;
  2776. ;  fs_HdDelete - delete file or empty folder from hard disk
  2777. ;
  2778. ;  esi  points to filename
  2779. ;
  2780. ;  ret  eax = 0 ok or other = errormsg
  2781. ;
  2782. ;--------------------------------------------------------------
  2783. fs_HdDelete:
  2784.         cmp     [fs_type], 1
  2785.         jz      ntfs_HdDelete
  2786.         cmp     [fs_type], 16
  2787.         jz      @f
  2788.         cmp     [fs_type], 32
  2789.         jz      @f
  2790.         push    ERROR_UNKNOWN_FS
  2791. .pop_ret:
  2792.         pop     eax
  2793.         ret
  2794. @@:
  2795.         cmp     byte [esi], 0
  2796.         jnz     @f
  2797. ; cannot delete root!
  2798. .access_denied:
  2799.         push    ERROR_ACCESS_DENIED
  2800.         jmp     .pop_ret
  2801. @@:
  2802.         and     [longname_sec1], 0
  2803.         and     [longname_sec2], 0
  2804.         push    edi
  2805.         call    hd_find_lfn
  2806.         jnc     .found
  2807.         pop     edi
  2808.         push    ERROR_FILE_NOT_FOUND
  2809.         jmp     .pop_ret
  2810. .found:
  2811.         cmp     dword [edi], '.   '
  2812.         jz      .access_denied2
  2813.         cmp     dword [edi], '..  '
  2814.         jz      .access_denied2
  2815.         test    byte [edi+11], 10h
  2816.         jz      .dodel
  2817. ; we can delete only empty folders!
  2818.         pushad
  2819.         mov     ebp, [edi+20-2]
  2820.         mov     bp, [edi+26]
  2821.         xor     ecx, ecx
  2822.         lea     eax, [ebp-2]
  2823.         imul    eax, [SECTORS_PER_CLUSTER]
  2824.         add     eax, [DATA_START]
  2825.         mov     ebx, buffer
  2826.         call    hd_read
  2827.         cmp     [hd_error], 0
  2828.         jnz     .err1
  2829.         add     ebx, 2*0x20
  2830. .checkempty:
  2831.         cmp     byte [ebx], 0
  2832.         jz      .empty
  2833.         cmp     byte [ebx], 0xE5
  2834.         jnz     .notempty
  2835.         add     ebx, 0x20
  2836.         cmp     ebx, buffer+0x200
  2837.         jb      .checkempty
  2838.         inc     ecx
  2839.         cmp     ecx, [SECTORS_PER_CLUSTER]
  2840.         jb      @f
  2841.         mov     eax, ebp
  2842.         call    get_FAT
  2843.         cmp     [hd_error], 0
  2844.         jnz     .err1
  2845.         mov     ebp, eax
  2846.         xor     ecx, ecx
  2847. @@:
  2848.         lea     eax, [ebp-2]
  2849.         imul    eax, [SECTORS_PER_CLUSTER]
  2850.         add     eax, [DATA_START]
  2851.         add     eax, ecx
  2852.         mov     ebx, buffer
  2853.         call    hd_read
  2854.         cmp     [hd_error], 0
  2855.         jz      .checkempty
  2856. .err1:
  2857.         popad
  2858. .err2:
  2859.         pop     edi
  2860.         push    11
  2861.         pop     eax
  2862.         ret
  2863. .notempty:
  2864.         popad
  2865. .access_denied2:
  2866.         pop     edi
  2867.         push    ERROR_ACCESS_DENIED
  2868.         pop     eax
  2869.         ret
  2870. .empty:
  2871.         popad
  2872.         push    ebx
  2873.         mov     ebx, buffer
  2874.         call    hd_read
  2875.         pop     ebx
  2876.         cmp     [hd_error], 0
  2877.         jnz     .err2
  2878. .dodel:
  2879.         push    eax
  2880.         mov     eax, [edi+20-2]
  2881.         mov     ax, [edi+26]
  2882.         xchg    eax, [esp]
  2883. ; delete folder entry
  2884.         mov     byte [edi], 0xE5
  2885. ; delete LFN (if present)
  2886. .lfndel:
  2887.         cmp     edi, buffer
  2888.         ja      @f
  2889.         cmp     [longname_sec2], 0
  2890.         jz      .lfndone
  2891.         push    [longname_sec2]
  2892.         push    [longname_sec1]
  2893.         pop     [longname_sec2]
  2894.         and     [longname_sec1], 0
  2895.         push    ebx
  2896.         mov     ebx, buffer
  2897.         call    hd_write
  2898.         mov     eax, [esp+4]
  2899.         call    hd_read
  2900.         pop     ebx
  2901.         pop     eax
  2902.         mov     edi, buffer+0x200
  2903. @@:
  2904.         sub     edi, 0x20
  2905.         cmp     byte [edi], 0xE5
  2906.         jz      .lfndone
  2907.         cmp     byte [edi+11], 0xF
  2908.         jnz     .lfndone
  2909.         mov     byte [edi], 0xE5
  2910.         jmp     .lfndel
  2911. .lfndone:
  2912.         push    ebx
  2913.         mov     ebx, buffer
  2914.         call    hd_write
  2915.         pop     ebx
  2916. ; delete FAT chain
  2917.         pop     eax
  2918.         call    clear_cluster_chain
  2919.         call    update_disk
  2920.         pop     edi
  2921.         xor     eax, eax
  2922.         cmp     [hd_error], 0
  2923.         jz      @f
  2924.         mov     al, 11
  2925. @@:
  2926.         ret
  2927.  
  2928. ; \end{diamond}
  2929.