Subversion Repositories Kolibri OS

Rev

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