Subversion Repositories Kolibri OS

Rev

Rev 131 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;;  FAT32.INC                                                      ;;
  4. ;;                                                                 ;;
  5. ;;  FAT16/32 functions for KolibriOS                               ;;
  6. ;;                                                                 ;;
  7. ;;  Copyright 2002 Paolo Minazzi, paolo.minazzi@inwind.it          ;;
  8. ;;                                                                 ;;
  9. ;;  See file COPYING for details                                   ;;
  10. ;;  17.08.2006 LFN write/append to file - diamond                  ;;
  11. ;;  23.06.2006 LFN start application - diamond                     ;;
  12. ;;  15.06.2006 LFN get/set file/folder info - diamond              ;;
  13. ;;  27.05.2006 LFN create/rewrite file - diamond                   ;;
  14. ;;  04.05.2006 LFN read folder - diamond                           ;;
  15. ;;  29.04.2006 Elimination of hangup after the                     ;;
  16. ;;             expiration hd_wait_timeout -  Mario79               ;;          
  17. ;;  23.04.2006 LFN read file - diamond                             ;;
  18. ;;  28.01.2006 find all Fat16/32 partition in all input point      ;;
  19. ;;             to MBR, see file part_set.inc - Mario79             ;;
  20. ;;  15.01.2005 get file size/attr/date, file_append - ATV          ;;
  21. ;;  04.12.2004 skip volume label, file delete bug fixed - ATV      ;;
  22. ;;  29.11.2004 get_free_FAT changed, append dir bug fixed - ATV    ;;
  23. ;;  23.11.2004 don't allow overwrite dir with file - ATV           ;;
  24. ;;  18.11.2004 get_disk_info and more error codes - ATV            ;;
  25. ;;  17.11.2004 set_FAT/get_FAT and disk cache rewritten - ATV      ;;
  26. ;;  10.11.2004 removedir clear whole directory structure - ATV     ;;
  27. ;;  08.11.2004 rename - ATV                                        ;;
  28. ;;  30.10.2004 file_read return also dirsize in bytes - ATV        ;;
  29. ;;  20.10.2004 Makedir/Removedir - ATV                             ;;
  30. ;;  14.10.2004 Partition chain/Fat16 - ATV (thanks drh3xx)         ;;
  31. ;;  06.9.2004  Fix free space by Mario79 added - MH                ;;
  32. ;;  24.5.2004  Write back buffer for File_write -VT                ;;
  33. ;;  20.5.2004  File_read function to work with syscall 58 - VT     ;;
  34. ;;  30.3.2004  Error parameters at function return - VT            ;;
  35. ;;  01.5.2002  Bugfix in device write - VT                         ;;
  36. ;;  20.5.2002  Hd status check - VT                                ;;
  37. ;;  29.6.2002  Improved fat32 verification - VT                    ;;
  38. ;;                                                                 ;;
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40.  
  41. cache_max equ 1919      ; max. is 1919*512+0x610000=0x6ffe00
  42.  
  43. ERROR_SUCCESS        = 0
  44. ERROR_DISK_BASE      = 1
  45. ERROR_UNSUPPORTED_FS = 2
  46. ERROR_UNKNOWN_FS     = 3
  47. ERROR_PARTITION      = 4
  48. ERROR_FILE_NOT_FOUND = 5
  49. ERROR_END_OF_FILE    = 6
  50. ERROR_MEMORY_POINTER = 7
  51. ERROR_DISK_FULL      = 8
  52. ERROR_FAT_TABLE      = 9
  53. ERROR_ACCESS_DENIED  = 10
  54.  
  55. PUSHAD_EAX equ [esp+28]
  56. PUSHAD_ECX equ [esp+24]
  57. PUSHAD_EDX equ [esp+20]
  58. PUSHAD_EBX equ [esp+16]
  59. PUSHAD_EBP equ [esp+8]
  60. PUSHAD_ESI equ [esp+4]
  61. PUSHAD_EDI equ [esp+0]
  62.  
  63. cluster              dd 0       ; used by file_write,makedir,append
  64. partition_count      dd 0       ; partitions found by set_FAT32_variables
  65. longname_sec1        dd 0       ; used by analyze_directory to save 2 previous
  66. longname_sec2        dd 0       ; directory sectors for delete long filename
  67.  
  68. hd_error             dd 0       ; set by wait_for_sector_buffer
  69. hd_setup             dd 0
  70. hd_wait_timeout      dd 0
  71.  
  72. cluster_tmp          dd 0       ; used by analyze_directory
  73.                                 ; and analyze_directory_to_write
  74.  
  75. file_size            dd 0       ; used by file_read
  76.  
  77. sector_tmp           dd 0       ; used by rename,append,file_write
  78. entry_pos            dd 0       ; used by rename,append,file_write
  79.  
  80. old_filesize         dd 0       ; used by append
  81. new_filepos          dd 0       ; used by append
  82. bytes2write          dd 0       ; used by append
  83.  
  84. cache_search_start   dd 0       ; used by find_empty_slot
  85.  
  86. fat_in_cache         dd -1
  87. fat_cache:           times 512 db 0
  88.  
  89. uglobal
  90.  Sector512:                      ; label for dev_hdcd.inc
  91.   buffer:              times 512 db 0
  92.   deltree_buffer:      times 512 db 0
  93.   fsinfo_buffer:       times 512 db 0
  94. endg
  95.  
  96. iglobal
  97.   NewDirEntry1         db ".          ",0x10
  98.                      times 20 db 0
  99.   NewDirEntry2         db "..         ",0x10
  100.                      times 20 db 0
  101. endg
  102.  
  103. uglobal
  104.   dir_entry:           times 32 db 0
  105.  
  106.   startpath:           times 255 db 0
  107.  
  108.   fat16_root           db 0       ; flag for fat16 rootdir
  109.   fat_change           db 0       ; 1=fat has changed
  110.  
  111. endg
  112.  
  113. reserve_hd1:
  114.  
  115.     cli
  116.     cmp   [hd1_status],0
  117.     je    reserve_ok1
  118.  
  119.     sti
  120.     call  change_task
  121.     jmp   reserve_hd1
  122.  
  123.   reserve_ok1:
  124.  
  125.     push  eax
  126.     mov   eax,[0x3000]
  127.     shl   eax,5
  128.     mov   eax,[eax+0x3000+TASKDATA.pid]
  129.     mov   [hd1_status],eax
  130.     pop   eax
  131.     sti
  132.     ret
  133. ;********************************************
  134. reserve_hd_channel:
  135.     cmp   [hdbase], 0x1F0
  136.     jne   .IDE_Channel_2
  137. .IDE_Channel_1:
  138.     cli
  139.     cmp   [IDE_Channel_1],0
  140.     je    .reserve_ok_1
  141.     sti
  142.     call  change_task
  143.     jmp   .IDE_Channel_1
  144. .IDE_Channel_2:
  145.     cli
  146.     cmp   [IDE_Channel_2],0
  147.     je    .reserve_ok_2
  148.     sti
  149.     call  change_task
  150.     jmp   .IDE_Channel_1
  151. .reserve_ok_1:
  152.     mov [IDE_Channel_1],1
  153.     ret
  154. .reserve_ok_2:
  155.     mov [IDE_Channel_2],1
  156.     ret
  157.    
  158. free_hd_channel:
  159.     cmp   [hdbase], 0x1F0
  160.     jne   .IDE_Channel_2
  161. .IDE_Channel_1:
  162.     mov [IDE_Channel_1],0
  163.     ret
  164. .IDE_Channel_2:
  165.     mov [IDE_Channel_2],0
  166.     ret
  167. ;********************************************
  168. clear_hd_cache:
  169.  
  170.     push  eax ecx edi
  171.     mov   edi,0x600000
  172.     mov   ecx,16384
  173.     xor   eax,eax
  174.     cld
  175.     rep   stosd                 ; clear hd cache with 0
  176.     mov   [cache_search_start],eax
  177.     mov   [fat_in_cache],-1
  178.     mov   [fat_change],0
  179.     pop   edi ecx eax
  180.     ret
  181.  
  182. problem_partition db 0  ; used for partitions search
  183.  
  184. include  'part_set.inc'
  185.  
  186. set_FAT:
  187. ;--------------------------------
  188. ; input  : EAX = cluster
  189. ;          EDX = value to save
  190. ; output : EDX = old value
  191. ;--------------------------------
  192.     push  eax ebx esi
  193.  
  194.     cmp   eax,2
  195.     jb    sfc_error
  196.     cmp   eax,[LAST_CLUSTER]
  197.     ja    sfc_error
  198.     cmp   [fat_type],16
  199.     je    sfc_1
  200.     add   eax,eax
  201.   sfc_1:
  202.     add   eax,eax
  203.     mov   esi,511
  204.     and   esi,eax               ; esi = position in fat sector
  205.     shr   eax,9                 ; eax = fat sector
  206.     add   eax,[FAT_START]
  207.     mov   ebx,fat_cache
  208.  
  209.     cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
  210.     je    sfc_in_cache          ; yes
  211.  
  212.     cmp   [fat_change],0        ; is fat changed?
  213.     je    sfc_no_change         ; no
  214.     call  write_fat_sector      ; yes. write it into disk
  215.     cmp   [hd_error],0
  216.     jne   sfc_error
  217.  
  218.   sfc_no_change:
  219.     mov   [fat_in_cache],eax    ; save fat sector
  220.     call  hd_read
  221.     cmp  [hd_error],0
  222.     jne  sfc_error
  223.    
  224.  
  225.   sfc_in_cache:
  226.     cmp   [fat_type],16
  227.     jne   sfc_test32
  228.  
  229.   sfc_set16:
  230.     xchg  [ebx+esi],dx          ; save new value and get old value
  231.     jmp   sfc_write
  232.  
  233.   sfc_test32:
  234.     mov   eax,[fatMASK]
  235.  
  236.   sfc_set32:
  237.     and   edx,eax
  238.     xor   eax,-1                ; mask for high bits
  239.     and   eax,[ebx+esi]         ; get high 4 bits
  240.     or    eax,edx
  241.     mov   edx,[ebx+esi]         ; get old value
  242.     mov   [ebx+esi],eax         ; save new value
  243.  
  244.   sfc_write:
  245.     mov   [fat_change],1        ; fat has changed
  246.  
  247.   sfc_nonzero:
  248.     and   edx,[fatMASK]
  249.  
  250.   sfc_error:
  251.     pop   esi ebx eax
  252.     ret
  253.  
  254.  
  255. get_FAT:
  256. ;--------------------------------
  257. ; input  : EAX = cluster
  258. ; output : EAX = next cluster
  259. ;--------------------------------
  260.     push  ebx esi
  261.  
  262.     cmp   [fat_type],16
  263.     je    gfc_1
  264.     add   eax,eax
  265.   gfc_1:
  266.     add   eax,eax
  267.     mov   esi,511
  268.     and   esi,eax               ; esi = position in fat sector
  269.     shr   eax,9                 ; eax = fat sector
  270.     add   eax,[FAT_START]
  271.     mov   ebx,fat_cache
  272.  
  273.     cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
  274.     je    gfc_in_cache
  275.  
  276.     cmp   [fat_change],0        ; is fat changed?
  277.     je    gfc_no_change         ; no
  278.     call  write_fat_sector      ; yes. write it into disk
  279.     cmp  [hd_error],0
  280.     jne  hd_error_01
  281.  
  282.   gfc_no_change:
  283.     mov   [fat_in_cache],eax
  284.     call  hd_read
  285.     cmp  [hd_error],0
  286.     jne  hd_error_01
  287.  
  288.   gfc_in_cache:
  289.     mov   eax,[ebx+esi]
  290.     and   eax,[fatMASK]
  291.  hd_error_01:
  292.     pop   esi ebx
  293.     ret
  294.  
  295.  
  296. get_free_FAT:
  297. ;-----------------------------------------------------------
  298. ; input  : EAX = # cluster for start the searching
  299. ; output : if CARRY=0 EAX = # first cluster found free
  300. ;          if CARRY=1 disk full
  301. ; Note   : for more speed need to use fat_cache directly
  302. ;-----------------------------------------------------------
  303.     push  ecx
  304.     mov   ecx,[LAST_CLUSTER]    ; counter for full disk
  305.     sub   ecx,2
  306.  
  307.   gff_test:
  308.     cmp   eax,[LAST_CLUSTER]    ; if above last cluster start at cluster 2
  309.     jbe   gff_in_range
  310.     mov   eax,2
  311.  
  312.   gff_in_range:
  313.     push  eax
  314.     call  get_FAT               ; get cluster state
  315.     cmp   [hd_error],0
  316.     jne   gff_not_found_1
  317.  
  318.     test  eax,eax               ; is it free?
  319.     pop   eax
  320.     je    gff_found             ; yes
  321.     inc   eax                   ; next cluster
  322.     dec   ecx                   ; is all checked?
  323.     jns   gff_test              ; no
  324.  
  325.   gff_not_found_1:
  326.     add   esp,4
  327.   gff_not_found:
  328.     pop   ecx                   ; yes. disk is full
  329.     stc
  330.     ret
  331.  
  332.   gff_found:
  333.     pop   ecx
  334.     clc
  335.     ret
  336.  
  337.  
  338. write_fat_sector:
  339. ;-----------------------------------------------------------
  340. ; write changed fat to disk
  341. ;-----------------------------------------------------------
  342.     push  eax ebx ecx
  343.  
  344.     mov   [fat_change],0
  345.     mov   eax,[fat_in_cache]
  346.     cmp   eax,-1
  347.     jz    write_fat_not_used
  348.     mov   ebx,fat_cache
  349.     mov   ecx,[NUMBER_OF_FATS]
  350.  
  351.   write_next_fat:
  352.     call  hd_write
  353.     cmp   [hd_error],0
  354.     jne   write_fat_not_used
  355.  
  356.     add   eax,[SECTORS_PER_FAT]
  357.     dec   ecx
  358.     jnz   write_next_fat
  359.  
  360.   write_fat_not_used:
  361.     pop   ecx ebx eax
  362.     ret
  363.  
  364.  
  365. analyze_directory:
  366. ;-----------------------------------------------------------
  367. ; input  : EAX = first cluster of the directory
  368. ;          EBX = pointer to filename
  369. ; output : IF CARRY=0 EAX = sector where th file is found
  370. ;                     EBX = pointer in buffer
  371. ;                     [buffer .. buffer+511]
  372. ;                     ECX,EDX,ESI,EDI not changed
  373. ;          IF CARRY=1 filename not found
  374. ; Note   : if cluster=0 it's changed to read rootdir
  375. ;          save 2 previous directory sectors in longname_sec
  376. ;-----------------------------------------------------------
  377.     push  ecx edx esi edi ebx   ; ebx = [esp+0]
  378.     mov   [longname_sec1],0
  379.     mov   [longname_sec2],0
  380.  
  381.   adr_new_cluster:
  382.     mov   [cluster_tmp],eax
  383.     mov   [fat16_root],0
  384.     cmp   eax,[LAST_CLUSTER]
  385.     ja    adr_not_found         ; too big cluster number, something is wrong
  386.     cmp   eax,2
  387.     jnb   adr_data_cluster
  388.  
  389.     mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
  390.     cmp   [fat_type],16
  391.     jne   adr_data_cluster
  392.     mov   eax,[ROOT_START]
  393.     mov   edx,[ROOT_SECTORS]
  394.     mov   [fat16_root],1        ; flag for fat16 rootdir
  395.     jmp   adr_new_sector
  396.  
  397.   adr_data_cluster:
  398.     sub   eax,2
  399.     mov   edx,[SECTORS_PER_CLUSTER]
  400.     imul  eax,edx
  401.     add   eax,[DATA_START]
  402.  
  403.   adr_new_sector:
  404.     mov   ebx,buffer
  405.     call  hd_read
  406.     cmp  [hd_error],0
  407.     jne  adr_not_found
  408.  
  409.     mov   ecx,512/32            ; count of dir entrys per sector = 16
  410.  
  411.   adr_analyze:
  412.     mov   edi,[ebx+11]          ; file attribute
  413.     and   edi,0xf
  414.     cmp   edi,0xf
  415.     je    adr_long_filename
  416.     test  edi,0x8               ; skip over volume label
  417.     jne   adr_long_filename     ; Note: label can be same name as file/dir
  418.  
  419.     mov   esi,[esp+0]           ; filename need to be uppercase
  420.     mov   edi,ebx
  421.     push  ecx
  422.     mov   ecx,11
  423.     cld
  424.     rep   cmpsb                 ; compare 8+3 filename
  425.     pop   ecx
  426.     je    adr_found
  427.  
  428.   adr_long_filename:
  429.     add   ebx,32                ; position of next dir entry
  430.     dec   ecx
  431.     jnz   adr_analyze
  432.  
  433.     mov   ecx,[longname_sec1]   ; save 2 previous directory sectors
  434.     mov   [longname_sec1],eax   ; for delete long filename
  435.     mov   [longname_sec2],ecx
  436.     inc   eax                   ; next sector
  437.     dec   edx
  438.     jne   adr_new_sector
  439.     cmp   [fat16_root],1        ; end of fat16 rootdir
  440.     je    adr_not_found
  441.  
  442.   adr_next_cluster:
  443.     mov   eax,[cluster_tmp]
  444.     call  get_FAT               ; get next cluster
  445.     cmp  [hd_error],0
  446.     jne  adr_not_found
  447.  
  448.     cmp   eax,2                 ; incorrect fat chain?
  449.     jb    adr_not_found         ; yes
  450.     cmp   eax,[fatRESERVED]     ; is it end of directory?
  451.     jb    adr_new_cluster       ; no. analyse it
  452.  
  453.   adr_not_found:
  454.     pop   edi edi esi edx ecx   ; first edi will remove ebx
  455.     stc                         ; file not found
  456.     ret
  457.  
  458.   adr_found:
  459.     pop   edi edi esi edx ecx   ; first edi will remove ebx
  460.     clc                         ; file found
  461.     ret
  462.  
  463.  
  464. analyze_directory_to_write:
  465. ;-----------------------------------------------------------
  466. ; input  : EAX = first cluster of the directory
  467. ; output : IF CARRY=0 EAX = sector where the empty pos is found
  468. ;                     EBX = pointer in buffer
  469. ;                     [buffer .. buffer+511]
  470. ;                     ECX,EDX,ESI,EDI not changed
  471. ;          IF CARRY=1 disk full or fat corrupted
  472. ; Note   : if cluster=0 it's changed to read rootdir
  473. ;-----------------------------------------------------------
  474.     push  ecx edx edi
  475.  
  476.   adw_new_cluster:
  477.     mov   [cluster_tmp],eax
  478.     mov   [fat16_root],0
  479.     cmp   eax,[LAST_CLUSTER]
  480.     ja    adw_not_found         ; too big cluster number, something is wrong
  481.     cmp   eax,2
  482.     jnb   adw_data_cluster
  483.  
  484.     mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
  485.     cmp   [fat_type],16
  486.     jne   adw_data_cluster
  487.     mov   eax,[ROOT_START]
  488.     mov   edx,[ROOT_SECTORS]
  489.     mov   [fat16_root],1        ; flag for fat16 rootdir
  490.     jmp   adw_new_sector
  491.  
  492.   adw_data_cluster:
  493.     sub   eax,2
  494.     mov   edx,[SECTORS_PER_CLUSTER]
  495.     imul  eax,edx
  496.     add   eax,[DATA_START]
  497.  
  498.   adw_new_sector:
  499.     mov   ebx,buffer
  500.     call  hd_read
  501.     cmp  [hd_error],0
  502.     jne  adw_not_found
  503.  
  504.     mov   ecx,512/32            ; count of dir entrys per sector = 16
  505.  
  506.   adw_analyze:
  507.     cmp   byte [ebx],0x00       ; is free entry?
  508.     je    adw_found             ; yes
  509.     cmp   byte [ebx],0xe5       ; is deleted entry?
  510.     je    adw_found             ; yes
  511.     add   ebx,32                ; position of next dir entry
  512.     dec   ecx
  513.     jnz   adw_analyze
  514.  
  515.     inc   eax                   ; next sector
  516.     dec   edx
  517.     jne   adw_new_sector
  518.     cmp   [fat16_root],1        ; end of fat16 rootdir
  519.     je    adw_not_found
  520.  
  521.     mov   eax,[cluster_tmp]
  522.     call  get_FAT               ; get next cluster
  523.     cmp   [hd_error],0
  524.     jne   adw_not_found
  525.  
  526.     cmp   eax,2                 ; incorrect fat chain?
  527.     jb    adw_not_found         ; yes
  528.     cmp   eax,[fatRESERVED]     ; is it end of directory?
  529.     jb    adw_new_cluster       ; no. analyse it
  530.  
  531.     mov   eax,2                 ; this block of code add a new cluster
  532.     call  get_free_FAT          ; for the directory because the directory
  533.     jc    adw_not_found         ; is full
  534.  
  535.     mov   edx,[fatEND]          ; new end for directory
  536.     call  set_FAT
  537.     cmp  [hd_error],0
  538.     jne  adw_not_found
  539.  
  540.     push  eax                   ; save new cluster
  541.     mov   edx,eax
  542.     mov   eax,[cluster_tmp]     ; change last cluster to point new cluster
  543.     call  set_FAT
  544.     cmp  [hd_error],0
  545.     jne  adw_not_found_1
  546.  
  547.     mov   ecx,-1                ; remove 1 cluster from free disk space
  548.     call  add_disk_free_space
  549.     cmp  [hd_error],0
  550.     jne    adw_not_found_1
  551.  
  552.     mov   ecx,512/4
  553.     xor   eax,eax
  554.     mov   edi,buffer
  555.     cld
  556.     rep   stosd                 ; clear new directory cluster
  557.     pop   eax
  558.  
  559.     sub   eax,2
  560.     mov   ecx,[SECTORS_PER_CLUSTER]
  561.     imul  eax,ecx
  562.     add   eax,[DATA_START]
  563.     mov   ebx,buffer
  564.     push  eax                   ; save sector number
  565.  
  566.   adw_set_empty_directory:
  567.     call  hd_write
  568.     cmp   [hd_error],0
  569.     jne   adw_not_found_1
  570.  
  571.     inc   eax                   ; next sector
  572.     dec   ecx
  573.     jnz   adw_set_empty_directory
  574.  
  575.     pop   eax
  576.  
  577.   adw_found:
  578.     pop   edi edx ecx
  579.     clc                         ; free space found
  580.     ret
  581.   adw_not_found_1:
  582.     add  esp,4
  583.   adw_not_found:
  584.     pop   edi edx ecx
  585.     stc                         ; free space not found
  586.     ret
  587.  
  588.  
  589. get_data_cluster:
  590. ;-----------------------------------------------------------
  591. ; input  : EAX = cluster
  592. ;          EBX = pointer to buffer
  593. ;          EDX = # blocks to read in buffer
  594. ;          ESI = # blocks to skip over
  595. ; output : if CARRY=0 ok EBX/EDX/ESI updated
  596. ;          if CARRY=1 cluster out of range
  597. ; Note   : if cluster=0 it's changed to read rootdir
  598. ;-----------------------------------------------------------
  599.     push  eax ecx
  600.  
  601.     mov   [fat16_root],0
  602.     cmp   eax,[LAST_CLUSTER]
  603.     ja    gdc_error             ; too big cluster number, something is wrong
  604.     cmp   eax,2
  605.     jnb   gdc_cluster
  606.  
  607.     mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
  608.     cmp   [fat_type],16
  609.     jne   gdc_cluster
  610.     mov   eax,[ROOT_START]
  611.     mov   ecx,[ROOT_SECTORS]    ; Note: not cluster size
  612.     mov   [fat16_root],1        ; flag for fat16 rootdir
  613.     jmp   gdc_read
  614.  
  615.   gdc_cluster:
  616.     sub   eax,2
  617.     mov   ecx,[SECTORS_PER_CLUSTER]
  618.     imul  eax,ecx
  619.     add   eax,[DATA_START]
  620.  
  621.   gdc_read:
  622.     test  esi,esi               ; first wanted block
  623.     je    gdcl1                 ; yes, skip count is 0
  624.     dec   esi
  625.     jmp   gdcl2
  626.  
  627.   gdcl1:
  628.     call  hd_read
  629.     cmp  [hd_error],0
  630.     jne  gdc_error    
  631.  
  632.     add   ebx,512               ; update pointer
  633.     dec   edx
  634.  
  635.   gdcl2:
  636.     test  edx,edx               ; is all read?
  637.     je    out_of_read
  638.  
  639.     inc   eax                   ; next sector
  640.     dec   ecx
  641.     jnz   gdc_read
  642.  
  643.   out_of_read:
  644.     pop   ecx eax
  645.     clc
  646.     ret
  647.  
  648.   gdc_error:
  649.     pop   ecx eax
  650.     stc
  651.     ret
  652.  
  653.  
  654. set_data_cluster:
  655. ;-----------------------------------------------------------
  656. ; input  : EAX = cluster
  657. ;          EBX = pointer to buffer
  658. ; output : if CARRY=0 ok
  659. ;          if CARRY=1 cluster out of range
  660. ;-----------------------------------------------------------
  661.     push  eax ebx edx
  662.  
  663.     cmp   eax,[LAST_CLUSTER]
  664.     ja    sdc_error             ; too big cluster number, something is wrong
  665.     sub   eax,2
  666.     jb    sdc_error             ; don't allow rootdir write
  667.  
  668.     mov   edx,[SECTORS_PER_CLUSTER]
  669.     imul  eax,edx
  670.     add   eax,[DATA_START]
  671.  
  672.   sdc_write:
  673.     call  hd_write
  674.     cmp   [hd_error],0
  675.     jne   sdc_error
  676.  
  677.     add   ebx,512               ; update pointer
  678.     inc   eax
  679.     dec   edx
  680.     jnz   sdc_write
  681.     pop   edx ebx eax
  682.     clc
  683.     ret
  684.  
  685.   sdc_error:
  686.     pop   edx ebx eax
  687.     stc
  688.     ret
  689.  
  690.  
  691. get_cluster_of_a_path:
  692. ;---------------------------------------------------------
  693. ; input  : EBX = pointer to a path string
  694. ;          (example: the path "/files/data/document" become
  695. ;                             "files......data.......document...0"
  696. ;          '.' = space char
  697. ;          '0' = char(0) (ASCII=0) !!! )
  698. ; output : if (CARRY=1) -> ERROR in the PATH
  699. ;          if (CARRY=0) -> EAX=cluster
  700. ;---------------------------------------------------------
  701.     push  ebx edx
  702.  
  703.     mov   eax,[ROOT_CLUSTER]
  704.     mov   edx,ebx
  705.  
  706. search_end_of_path:
  707.     cmp   byte [edx],0
  708.     je    found_end_of_path
  709.  
  710.     inc   edx ; '/'
  711.     mov   ebx,edx
  712.     call  analyze_directory
  713.     jc    directory_not_found
  714.  
  715.     mov   eax,[ebx+20-2]        ; read the HIGH 16bit cluster field
  716.     mov   ax,[ebx+26]           ; read the LOW 16bit cluster field
  717.     and   eax,[fatMASK]
  718.     add   edx,11                ; 8+3 (name+extension)
  719.     jmp   search_end_of_path
  720.  
  721. found_end_of_path:
  722.     pop   edx ebx
  723.     clc                         ; no errors
  724.     ret
  725.  
  726. directory_not_found:
  727.     pop   edx ebx
  728.     stc                         ; errors occour
  729.     ret
  730.  
  731.  
  732. bcd2bin:
  733. ;----------------------------------
  734. ; input  : AL=BCD number (eg. 0x11)
  735. ; output : AH=0
  736. ;          AL=decimal number (eg. 11)
  737. ;----------------------------------
  738.     xor   ah,ah
  739.     shl   ax,4
  740.     shr   al,4
  741.     aad
  742.     ret
  743.  
  744.  
  745. get_date_for_file:
  746. ;-----------------------------------------------------
  747. ; Get date from CMOS and pack day,month,year in AX
  748. ; DATE   bits  0..4   : day of month 0..31
  749. ;              5..8   : month of year 1..12
  750. ;              9..15  : count of years from 1980
  751. ;-----------------------------------------------------
  752.     mov   al,0x7        ;day
  753.     out   0x70,al
  754.     in    al,0x71
  755.     call  bcd2bin
  756.     ror   eax,5
  757.  
  758.     mov   al,0x8        ;month
  759.     out   0x70,al
  760.     in    al,0x71
  761.     call  bcd2bin
  762.     ror   eax,4
  763.  
  764.     mov   al,0x9        ;year
  765.     out   0x70,al
  766.     in    al,0x71
  767.     call  bcd2bin
  768.     add   ax,20         ;because CMOS return only the two last
  769.                         ;digit (eg. 2000 -> 00 , 2001 -> 01) and we
  770.     rol   eax,9         ;need the difference with 1980 (eg. 2001-1980)
  771.     ret
  772.  
  773.  
  774. get_time_for_file:
  775. ;-----------------------------------------------------
  776. ; Get time from CMOS and pack hour,minute,second in AX
  777. ; TIME   bits  0..4   : second (the low bit is lost)
  778. ;              5..10  : minute 0..59
  779. ;              11..15 : hour 0..23
  780. ;-----------------------------------------------------
  781.     mov   al,0x0        ;second
  782.     out   0x70,al
  783.     in    al,0x71
  784.     call  bcd2bin
  785.     ror   eax,6
  786.  
  787.     mov   al,0x2        ;minute
  788.     out   0x70,al
  789.     in    al,0x71
  790.     call  bcd2bin
  791.     ror   eax,6
  792.  
  793.     mov   al,0x4        ;hour
  794.     out   0x70,al
  795.     in    al,0x71
  796.     call  bcd2bin
  797.     rol   eax,11
  798.     ret
  799.  
  800.  
  801. set_current_time_for_entry:
  802. ;-----------------------------------------------------
  803. ; Set current time/date for file entry
  804. ; input  : ebx = file entry pointer
  805. ;-----------------------------------------------------
  806.     push  eax
  807.     call  get_time_for_file     ; update files date/time
  808.     mov   [ebx+22],ax
  809.     call  get_date_for_file
  810.     mov   [ebx+24],ax
  811.     pop   eax
  812.     ret
  813.  
  814.  
  815. makedir:
  816. ;-----------------------------------------------------
  817. ; input  : eax = directory name
  818. ;          edx = path
  819. ; output : eax = 0 - ok
  820. ;                3 - unknown FS
  821. ;                5 - file not found
  822. ;                8 - disk full
  823. ;               10 - access denied
  824. ; Note   : can only make one directory at time
  825. ;-----------------------------------------------------
  826.     cmp   [fat_type],0
  827.     jnz   make_dir_fat_ok
  828.     mov   eax,ERROR_UNKNOWN_FS
  829.     ret
  830.  
  831.   make_dir_fat_ok:
  832. ;    call  reserve_hd1
  833.  
  834.     pushad
  835.  
  836.     mov   ebx,edx
  837.     call  get_cluster_of_a_path
  838.     jnc   make_dir_found_path
  839.     cmp   [hd_error],0
  840.     jne   make_dir_error_1
  841.  
  842.   make_dir_path_not_found:
  843.     popad
  844.     call  update_disk           ; write all of cache and fat to hd
  845.     cmp   [hd_error],0
  846.     jne   make_dir_error_2
  847.  
  848.     mov   [hd1_status],0
  849.     mov   eax,ERROR_FILE_NOT_FOUND
  850.     ret
  851.  
  852.   make_dir_disk_full:
  853.     cmp  [hd_error],0
  854.     jne   make_dir_error_1
  855.     popad
  856.     call  update_disk           ; write all of cache and fat to hd
  857.     cmp   [hd_error],0
  858.     jne   make_dir_error_2
  859.  
  860.     mov   [hd1_status],0
  861.     mov   eax,ERROR_DISK_FULL
  862.     ret
  863.  
  864.   make_dir_already_exist:
  865.     cmp  [hd_error],0
  866.     jne   make_dir_error_1    
  867.     mov   eax,[cluster]         ; directory cluster
  868.     xor   edx,edx               ; free
  869.     call  set_FAT
  870.     cmp  [hd_error],0
  871.     jne   make_dir_error_1
  872.  
  873.     popad
  874.     call  update_disk           ; write all of cache and fat to hd
  875.   make_dir_error_2:
  876.     mov   [hd1_status],0
  877.     mov   eax,ERROR_ACCESS_DENIED
  878.     ret
  879.  
  880.   make_dir_error_1:
  881.     popad
  882.     jmp   make_dir_error_2
  883.  
  884.   make_dir_error_3:
  885.     add  esp,4
  886.     jmp   make_dir_error_1
  887.  
  888.   make_dir_found_path:
  889.     cmp   eax,[ROOT_CLUSTER]
  890.     jnz   make_dir_not_root
  891.     xor   eax,eax
  892.  
  893.   make_dir_not_root:
  894.     mov   ecx,eax               ; directorys start cluster
  895.     mov   word [NewDirEntry2+26],cx ; 16 bits low of cluster
  896.     shr   ecx,16
  897.     mov   word [NewDirEntry2+20],cx ; 16 bits high of cluster (=0 fat16)
  898.  
  899.     push  eax                   ; save parent directory cluster
  900.     mov   eax,2
  901.     call  get_free_FAT
  902.     mov   [cluster],eax         ; first free cluster
  903.     pop   eax
  904.     jc    make_dir_disk_full
  905.  
  906.     push  eax
  907.     mov   eax,[cluster]         ; directory cluster
  908.     mov   edx,[fatEND]          ; end for directory
  909.     call  set_FAT
  910.     cmp  [hd_error],0
  911.     jne   make_dir_error_3
  912.     pop   eax
  913.  
  914.     mov   ebx,PUSHAD_EAX        ; dir name
  915.     push  eax
  916.     call  analyze_directory     ; check if directory already exist
  917.     cmp   [hd_error],0
  918.     jne   make_dir_error_1
  919.  
  920.     pop   eax
  921.     jnc   make_dir_already_exist ; need to free allocated cluster!
  922.  
  923.     call  analyze_directory_to_write
  924.     jc    make_dir_already_exist ; need to free allocated cluster!
  925.  
  926.     mov   esi,PUSHAD_EAX        ; dir name
  927.     mov   edi,ebx               ; pointer in buffer
  928.     mov   ecx,11
  929.     cld
  930.     rep   movsb
  931.  
  932.     mov   dword [ebx+28],0      ; dir size is always 0
  933.     mov   ecx,[cluster]
  934.     mov   [ebx+26],cx           ; 16 bits low of cluster
  935.     mov   word [NewDirEntry1+26],cx
  936.     shr   ecx,16
  937.     mov   [ebx+20],cx           ; 16 bits high of cluster (=0 fat16)
  938.     mov   word [NewDirEntry1+20],cx
  939.     mov   byte [ebx+11],0x10    ; attribute = directory
  940.  
  941.     call  set_current_time_for_entry
  942.     mov   ecx,[ebx+22]
  943.     mov   dword [NewDirEntry1+22],ecx
  944.     mov   dword [NewDirEntry2+22],ecx
  945.  
  946.     mov   ebx,buffer            ; save the directory name,length,cluster
  947.     call  hd_write
  948.     cmp   [hd_error],0
  949.     jne   make_dir_error_1
  950.  
  951.     mov   ecx,512/4
  952.     xor   eax,eax
  953.     mov   edi,buffer
  954.     cld
  955.     rep   stosd                 ; clear new directory cluster
  956.  
  957.     mov   eax,[cluster]         ; new directory cluster
  958.     sub   eax,2
  959.     mov   edx,[SECTORS_PER_CLUSTER]
  960.     imul  eax,edx
  961.     add   eax,[DATA_START]
  962.     mov   ebx,buffer
  963.     add   eax,edx               ; start from last sector
  964.  
  965.   dir_set_empty_directory:
  966.     dec   eax                   ; next sector
  967.     cmp   edx,1                 ; is first directory sector?
  968.     jnz   not_first_sector      ; no. write empty sector
  969.     mov   esi,NewDirEntry1
  970.     mov   edi,buffer
  971.     mov   ecx,64/4
  972.     cld
  973.     rep   movsd                 ; copy 2 first directory entrys "." and ".."
  974.  
  975.   not_first_sector:
  976.     call  hd_write
  977.     cmp   [hd_error],0
  978.     jne   make_dir_error_1
  979.  
  980.     dec   edx
  981.     jnz   dir_set_empty_directory
  982.  
  983.     mov   ecx,-1                ; remove 1 cluster from free disk space
  984.     call  add_disk_free_space
  985.     cmp  [hd_error],0
  986.     jne   make_dir_error_1
  987.  
  988.     popad
  989.     call  update_disk           ; write all of cache and fat to hd
  990.     cmp   [hd_error],0
  991.     jne   make_dir_error_2
  992.     mov   [hd1_status],0
  993.     xor   eax,eax
  994.     ret
  995.  
  996.  
  997. removedir:
  998. ;-----------------------------------------------------
  999. ; input  : eax = file/directory name
  1000. ;          edx = path
  1001. ; output : eax = 0 - ok
  1002. ;                3 - unknown FS
  1003. ;                5 - file not found
  1004. ;               10 - access denied
  1005. ;-----------------------------------------------------
  1006.     cmp   [fat_type],0
  1007.     jnz   remove_dir_fat_ok
  1008.     mov   eax,ERROR_UNKNOWN_FS
  1009.     ret
  1010.  
  1011.   remove_dir_fat_ok:
  1012. ;    call  reserve_hd1
  1013.  
  1014.     push  edi
  1015.     mov   edi,1                 ; allow directory remove
  1016.     call  file_delete
  1017.     cmp   [hd_error],0
  1018.     jne   @f
  1019.  
  1020.     pop   edi
  1021.  
  1022.     call  update_disk           ; write all of cache and fat to hd
  1023.   @@:
  1024.     mov   [hd1_status],0
  1025.     ret
  1026.    
  1027.  
  1028. add_disk_free_space:
  1029. ;-----------------------------------------------------
  1030. ; input  : ecx = cluster count
  1031. ; Note   : negative = remove clusters from free space
  1032. ;          positive = add clusters to free space
  1033. ;-----------------------------------------------------
  1034.     test  ecx,ecx               ; no change
  1035.     je    add_dfs_no
  1036.     cmp   [fat_type],32         ; free disk space only used by fat32
  1037.     jne   add_dfs_no
  1038.  
  1039.     push  eax ebx
  1040.     mov   eax,[ADR_FSINFO]
  1041.     mov   ebx,fsinfo_buffer
  1042.     call  hd_read
  1043.     cmp  [hd_error],0
  1044.     jne  add_not_fs
  1045.  
  1046.     cmp   dword [ebx+0x1fc],0xaa550000 ; check sector id
  1047.     jne   add_not_fs
  1048.  
  1049.     add   [ebx+0x1e8],ecx
  1050.     call  hd_write
  1051. ;    cmp   [hd_error],0
  1052. ;    jne   add_not_fs
  1053.  
  1054.   add_not_fs:
  1055.     pop   ebx eax
  1056.  
  1057.   add_dfs_no:
  1058.     ret
  1059.  
  1060.  
  1061. file_write:
  1062. ;--------------------------------------------------------------------------
  1063. ;   INPUT : user-reg  register-in-this  meaning      symbol-in-this-routine
  1064. ;
  1065. ;            EAX        EDI            system call to write    /
  1066. ;            EBX        EAX   (PAR0)   pointer to file-name    PAR0
  1067. ;            EDX        ECX   (PAR1)   pointer to buffer       PAR1
  1068. ;            ECX        EBX   (PAR2)   file size               PAR2
  1069. ;            ESI        EDX   (PAR3)   pointer to path         PAR3
  1070. ;
  1071. ; output : eax = 0 - ok
  1072. ;                3 - unknown FS
  1073. ;                5 - file not found
  1074. ;                8 - disk full
  1075. ;               10 - access denied
  1076. ;--------------------------------------------------------------------------
  1077.     cmp   [fat_type],0
  1078.     jnz   fat_ok_for_writing
  1079.     mov   eax,ERROR_UNKNOWN_FS
  1080.     ret
  1081.  
  1082.   fat_ok_for_writing:
  1083. ;    call  reserve_hd1
  1084.  
  1085.     pushad
  1086.  
  1087.     xor   edi,edi               ; don't allow directory remove
  1088.     call  file_delete           ; try to delete the file first
  1089.     cmp   [hd_error],0
  1090.     jne   exit_write_access_1
  1091.  
  1092.     test  eax,eax
  1093.     jz    old_deleted           ; deleted ok
  1094.     cmp   eax,ERROR_FILE_NOT_FOUND
  1095.     jnz   exit_write_access     ; it exist but can't delete
  1096.  
  1097.   old_deleted:
  1098.     mov   ebx,PUSHAD_EDX
  1099.     call  get_cluster_of_a_path
  1100.     jnc   found_directory_for_writing
  1101.     cmp   [hd_error],0
  1102.     jne   exit_write_access
  1103.  
  1104.   exit_writing_with_error:
  1105.     popad
  1106.     call  update_disk           ; write all of cache and fat to hd
  1107.     cmp   [hd_error],0
  1108.     jne   exit_write_access_2
  1109.  
  1110.     mov   [hd1_status],0
  1111.     mov   eax,ERROR_FILE_NOT_FOUND
  1112.     ret
  1113.  
  1114.   exit_writing_disk_full_clear:
  1115.     cmp  [hd_error],0
  1116.     jne  exit_write_access_1
  1117.     mov   eax,[sector_tmp]
  1118.     mov   ebx,buffer
  1119.     call  hd_read               ; read directory sector
  1120.     cmp  [hd_error],0
  1121.     jne  exit_write_access_1
  1122.  
  1123.     mov   edx,[entry_pos]
  1124.     mov   byte [edx],0xe5       ; mark as deleted
  1125.     call  hd_write
  1126.     cmp   [hd_error],0
  1127.     jne   exit_write_access_1
  1128.  
  1129.     mov   eax,[edx+20-2]        ; FAT entry
  1130.     mov   ax,[edx+26]
  1131.     and   eax,[fatMASK]
  1132.     call  clear_cluster_chain
  1133.  
  1134.   exit_writing_disk_full:
  1135.     cmp  [hd_error],0
  1136.     jne  exit_write_access_1
  1137.     popad
  1138.     call  update_disk           ; write all of cache and fat to hd
  1139.     cmp   [hd_error],0
  1140.     jne   exit_write_access_2
  1141.     mov   [hd1_status],0
  1142.     mov   eax,ERROR_DISK_FULL
  1143.     ret
  1144.  
  1145.   exit_write_access:
  1146.     popad
  1147.     call  update_disk           ; write all of cache and fat to hd
  1148.     mov   [hd1_status],0
  1149.     mov   eax,ERROR_ACCESS_DENIED
  1150.     ret
  1151.  
  1152.   exit_write_access_1:
  1153.     popad
  1154.   exit_write_access_2:
  1155.     mov   [hd1_status],0
  1156.     mov   eax,ERROR_ACCESS_DENIED
  1157.     ret
  1158.  
  1159. found_directory_for_writing:
  1160.     call  analyze_directory_to_write
  1161.     jc    exit_writing_disk_full
  1162.  
  1163.     mov   [sector_tmp],eax
  1164.     mov   [entry_pos],ebx
  1165.     push  eax                   ; save directory sector
  1166.     mov   eax,2
  1167.     call  get_free_FAT
  1168.     mov   [cluster],eax         ; first free cluster
  1169.     pop   eax
  1170.     jc    exit_writing_disk_full
  1171.  
  1172.     mov   esi,PUSHAD_EAX        ; file name
  1173.     mov   edi,ebx               ; pointer in buffer
  1174.     mov   ecx,11
  1175.     cld
  1176.     rep   movsb
  1177.  
  1178.     mov   esi,PUSHAD_EBX        ; file size (bytes left)
  1179.     mov   [ebx+28],esi          ; file size
  1180.     mov   ecx,[cluster]
  1181.     mov   [ebx+26],cx           ; 16 bits low of cluster
  1182.     shr   ecx,16
  1183.     mov   [ebx+20],cx           ; 16 bits high of cluster (=0 fat16)
  1184.     mov   byte [ebx+11],0x20    ; attribute = archive
  1185.  
  1186.     call  set_current_time_for_entry
  1187.  
  1188.     mov   ebx,buffer            ; save the directory name,length,cluster
  1189.     call  hd_write
  1190.     cmp   [hd_error],0
  1191.     jne   exit_write_access_1
  1192.  
  1193.     imul  edi,[SECTORS_PER_CLUSTER],512 ; edi = cluster size in bytes
  1194.     xor   ecx,ecx               ; cluster count
  1195.     mov   ebx,PUSHAD_ECX        ; ebx = buffer
  1196.  
  1197. hd_new_block_write:
  1198.  
  1199.     mov   eax,[cluster]         ; eax = block
  1200.     call  set_data_cluster
  1201.     cmp   [hd_error],0
  1202.     jne   exit_write_access_1
  1203.  
  1204.     sub   esi,edi               ; sub wrote bytes
  1205.     jbe   file_saved_OK         ; end if all done
  1206.     add   ebx,edi               ; update buffer position
  1207.  
  1208.     inc   eax
  1209.     call  get_free_FAT          ; next free in FAT
  1210.     jc    exit_writing_disk_full_clear
  1211.  
  1212.     mov   edx,eax
  1213.     xchg  eax,[cluster]         ; get old cluster and save new cluster
  1214.     call  set_FAT               ; add it in cluster chain
  1215.     cmp  [hd_error],0
  1216.     jne   exit_write_access_1
  1217.  
  1218.     dec   ecx                   ; update cluster count
  1219.     jmp   hd_new_block_write
  1220.  
  1221. file_saved_OK:
  1222.  
  1223.     mov   edx,[fatEND]          ; new end for cluster chain
  1224.     call  set_FAT
  1225.     cmp  [hd_error],0
  1226.     jne   exit_write_access_1
  1227.  
  1228.     dec   ecx                   ; update cluster count
  1229.  
  1230.     call  add_disk_free_space   ; remove clusters from free disk space
  1231.     cmp  [hd_error],0
  1232.     jne   exit_write_access_1
  1233.  
  1234.     popad
  1235.     call  update_disk           ; write all of cache and fat to hd
  1236.     cmp   [hd_error],0
  1237.     jne   exit_write_access_2
  1238.     mov   [hd1_status],0
  1239.     xor   eax,eax
  1240.     ret
  1241.  
  1242.  
  1243. file_read:
  1244. ;--------------------------------------------------------------------------
  1245. ;   INPUT :  user-register register-in-this  meaning         symbol-in-this
  1246. ;
  1247. ;            EAX           EDI               system call to write   /
  1248. ;            EBX           EAX   (PAR0)      pointer to file-name   PAR0
  1249. ;            EDX           ECX   (PAR1)      pointer to buffer      PAR1
  1250. ;            ECX           EBX   (PAR2)   vt file blocks to read    PAR2
  1251. ;            ESI           EDX   (PAR3)      pointer to path        PAR3
  1252. ;            EDI           ESI            vt first 512 block to read
  1253. ;                          EDI               if 0 - read root
  1254. ;
  1255. ; output : eax = 0 - ok
  1256. ;                3 - unknown FS
  1257. ;                5 - file not found
  1258. ;                6 - end of file
  1259. ;                9 - fat table corrupted
  1260. ;               10 - access denied
  1261. ;          ebx = size of file/directory
  1262. ;--------------------------------------------------------------------------
  1263.     cmp   [fat_type],0
  1264.     jnz   fat_ok_for_reading
  1265.     xor   ebx,ebx
  1266.     mov   eax,ERROR_UNKNOWN_FS
  1267.     mov   [hd1_status], ebx
  1268.     ret
  1269.  
  1270.   fat_ok_for_reading:
  1271. ;    call  reserve_hd1
  1272.  
  1273.     pushad
  1274.  
  1275.     mov   ebx,edx
  1276.     call  get_cluster_of_a_path
  1277.     jc    file_to_read_not_found
  1278.  
  1279.     test  edi,edi               ; read rootdir
  1280.     jne   no_read_root
  1281.  
  1282.     xor   eax,eax
  1283.     call  get_dir_size          ; return rootdir size
  1284.     cmp   [hd_error],0
  1285.     jne   file_access_denied
  1286.  
  1287.     mov   [file_size],eax
  1288.     mov   eax,[ROOT_CLUSTER]
  1289.     jmp   file_read_start
  1290.  
  1291.   no_read_root:
  1292.     mov   ebx,PUSHAD_EAX        ; file name
  1293.     call  analyze_directory
  1294.     jc    file_to_read_not_found
  1295.  
  1296.     mov   eax,[ebx+28]          ; file size
  1297.     test  byte [ebx+11],0x10    ; is it directory?
  1298.     jz    read_set_size         ; no
  1299.  
  1300.     mov   eax,[ebx+20-2]        ; FAT entry
  1301.     mov   ax,[ebx+26]
  1302.     and   eax,[fatMASK]
  1303.     call  get_dir_size
  1304.     cmp   [hd_error],0
  1305.     jne   file_access_denied
  1306.  
  1307.   read_set_size:
  1308.     mov   [file_size],eax
  1309.  
  1310.     mov   eax,[ebx+20-2]        ; FAT entry
  1311.     mov   ax,[ebx+26]
  1312.     and   eax,[fatMASK]
  1313.  
  1314.   file_read_start:
  1315.     mov   ebx,PUSHAD_ECX        ; pointer to buffer
  1316.     mov   edx,PUSHAD_EBX        ; file blocks to read
  1317.     mov   esi,PUSHAD_ESI        ; first 512 block to read
  1318.  
  1319.   file_read_new_cluster:
  1320.     call  get_data_cluster
  1321.     jc    file_read_eof         ; end of file or cluster out of range
  1322.  
  1323.     test  edx,edx               ; is all read?
  1324.     je    file_read_OK          ; yes
  1325.  
  1326.     call  get_FAT               ; get next cluster
  1327.     cmp   [hd_error],0
  1328.     jne   file_access_denied
  1329.  
  1330.     cmp   eax,[fatRESERVED]     ; end of file
  1331.     jnb   file_read_eof
  1332.     cmp   eax,2                 ; incorrect fat chain
  1333.     jnb   file_read_new_cluster
  1334.  
  1335.     popad
  1336.     mov   [hd1_status],0
  1337.     mov   ebx,[file_size]
  1338.     mov   eax,ERROR_FAT_TABLE
  1339.     ret
  1340.  
  1341.   file_read_eof:
  1342.     cmp   [hd_error],0
  1343.     jne   file_access_denied
  1344.     popad
  1345.     mov   [hd1_status],0
  1346.     mov   ebx,[file_size]
  1347.     mov   eax,ERROR_END_OF_FILE
  1348.     ret
  1349.  
  1350.   file_read_OK:
  1351.     popad
  1352.     mov   [hd1_status],0
  1353.     mov   ebx,[file_size]
  1354.     xor   eax,eax
  1355.     ret
  1356.  
  1357.   file_to_read_not_found:
  1358.     cmp   [hd_error],0
  1359.     jne   file_access_denied
  1360.     popad
  1361.     mov   [hd1_status],0
  1362.     xor   ebx,ebx
  1363.     mov   eax,ERROR_FILE_NOT_FOUND
  1364.     ret
  1365.  
  1366.   file_access_denied:
  1367.     popad
  1368.     mov   [hd1_status],0
  1369.     xor   ebx,ebx
  1370.     mov   eax,ERROR_ACCESS_DENIED
  1371.     ret
  1372.  
  1373. get_dir_size:
  1374. ;-----------------------------------------------------
  1375. ; input  : eax = first cluster (0=rootdir)
  1376. ; output : eax = directory size in bytes
  1377. ;-----------------------------------------------------
  1378.     push  edx
  1379.     xor   edx,edx               ; count of directory clusters
  1380.     test  eax,eax
  1381.     jnz   dir_size_next
  1382.  
  1383.     mov   eax,[ROOT_SECTORS]
  1384.     shl   eax,9                 ; fat16 rootdir size in bytes
  1385.     cmp   [fat_type],16
  1386.     je    dir_size_ret
  1387.     mov   eax,[ROOT_CLUSTER]
  1388.  
  1389.   dir_size_next:
  1390.     cmp   eax,2                 ; incorrect fat chain
  1391.     jb    dir_size_end
  1392.     cmp   eax,[fatRESERVED]     ; end of directory
  1393.     ja    dir_size_end
  1394.     call  get_FAT               ; get next cluster
  1395.     cmp   [hd_error],0
  1396.     jne   dir_size_ret
  1397.  
  1398.     inc   edx
  1399.     jmp   dir_size_next
  1400.  
  1401.   dir_size_end:
  1402.     imul  eax,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
  1403.     imul  eax,edx
  1404.  
  1405.   dir_size_ret:
  1406.     pop   edx
  1407.     ret
  1408.  
  1409.  
  1410. file_delete:
  1411. ;-----------------------------------------------------
  1412. ; input  : eax = file/directory name
  1413. ;          edx = path
  1414. ;          edi = 1 - allow directory remove else don't remove directory
  1415. ; output : eax = 0 - ok
  1416. ;                3 - unknown FS
  1417. ;                5 - file not found
  1418. ;               10 - access denied
  1419. ;-----------------------------------------------------
  1420.     cmp   [fat_type],0
  1421.     jnz   file_del_fat_ok
  1422.     mov   eax,ERROR_UNKNOWN_FS
  1423.     ret
  1424.  
  1425.   file_del_fat_ok:
  1426.     pushad
  1427.  
  1428.     mov   ebx,edx
  1429.     call  get_cluster_of_a_path
  1430.     jc    file_to_delete_not_found
  1431.  
  1432.     mov   ebx,PUSHAD_EAX        ; file/directory name
  1433.     call  analyze_directory
  1434.     jc    file_to_delete_not_found
  1435.  
  1436.     test  byte [ebx+11],0x10    ; is it directory?
  1437.     jz    delete_notdir         ; no. it's file
  1438.     cmp   edi,1                 ; allow directory remove
  1439.     jnz   delete_no_access      ; no
  1440.  
  1441.     push  eax                   ; save directory sector
  1442.     mov   eax,[ebx+20-2]        ; first cluster of file
  1443.     mov   ax,[ebx+26]           ; 0 length files start cluster = 0
  1444.     and   eax,[fatMASK]
  1445.     xor   ebp,ebp               ; counter for directory deepnes
  1446.     call  clear_directory
  1447.     pop   eax
  1448.     jc    delete_no_access
  1449.  
  1450.     push  ebx                   ; save directory pointer in buffer
  1451.     mov   ebx,buffer
  1452.     call  hd_read               ; read directory sector
  1453.     cmp  [hd_error],0
  1454.     jne  delete_no_access_1
  1455.     pop   ebx
  1456.  
  1457.   delete_notdir:
  1458.     call  delete_entry_name
  1459.     cmp   [hd_error],0
  1460.     jne   delete_no_access
  1461.  
  1462.     mov   eax,ecx               ; first cluster of file
  1463.     call  clear_cluster_chain
  1464.     cmp   [hd_error],0
  1465.     jne   delete_no_access
  1466.  
  1467.     popad
  1468.     xor   eax,eax
  1469.     ret
  1470.  
  1471.   delete_no_access_1:
  1472.      add esp,4
  1473.   delete_no_access:
  1474.     popad
  1475.     mov   eax,ERROR_ACCESS_DENIED
  1476.     ret
  1477.  
  1478.   file_to_delete_not_found:
  1479.     cmp  [hd_error],0
  1480.     jne  delete_no_access
  1481.     popad
  1482.     mov   eax,ERROR_FILE_NOT_FOUND
  1483.     ret
  1484.  
  1485.  
  1486. clear_cluster_chain:
  1487. ;-----------------------------------------------------
  1488. ; input  : eax = first cluster
  1489. ;-----------------------------------------------------
  1490.     push  eax ecx edx
  1491.     xor   ecx,ecx               ; cluster count
  1492.  
  1493.   clean_new_chain:
  1494.     cmp   eax,[LAST_CLUSTER]    ; end of file
  1495.     ja    delete_OK
  1496.     cmp   eax,2                 ; unfinished fat chain or zero length file
  1497.     jb    delete_OK
  1498.     cmp   eax,[ROOT_CLUSTER]    ; don't remove root cluster
  1499.     jz    delete_OK
  1500.  
  1501.     xor   edx,edx
  1502.     call  set_FAT               ; clear fat entry
  1503.     cmp  [hd_error],0
  1504.     jne  access_denied_01
  1505.  
  1506.     inc   ecx                   ; update cluster count
  1507.     mov   eax,edx               ; old cluster
  1508.     jmp   clean_new_chain
  1509.  
  1510.   delete_OK:
  1511.     call  add_disk_free_space   ; add clusters to free disk space
  1512.   access_denied_01:
  1513.     pop   edx ecx eax
  1514.     ret
  1515.  
  1516.  
  1517. clear_directory:
  1518. ;-----------------------------------------------------
  1519. ; input  : eax = directory cluster
  1520. ;          ebp = directory deepnes
  1521. ; Note   : use recursive call
  1522. ;-----------------------------------------------------
  1523.     pushad
  1524.     inc   ebp
  1525.     cmp   ebp,64                ; if over 63 directory deep
  1526.     jnb   clear_error           ; something must be wrong
  1527.  
  1528.   clear_new_cluster:
  1529.     cmp   eax,[LAST_CLUSTER]
  1530.     ja    clear_end
  1531.     cmp   eax,[ROOT_CLUSTER]    ; don't remove root cluster
  1532.     jz    clear_end
  1533.     mov   esi,eax               ; esi = current directory cluster
  1534.     sub   eax,2
  1535.     jb    clear_end
  1536.     mov   ecx,[SECTORS_PER_CLUSTER]
  1537.     imul  eax,ecx
  1538.     add   eax,[DATA_START]
  1539.  
  1540.   clear_new_sector:
  1541.     mov   edi,eax               ; edi = current directory sector
  1542.     mov   ebx,deltree_buffer
  1543.     call  hd_read
  1544.     cmp  [hd_error],0
  1545.     jne  clear_error    
  1546.  
  1547.     mov   edx,512/32            ; count of dir entrys per sector = 16
  1548.  
  1549.   clear_analyze:
  1550.     mov   al,[ebx+11]           ; file attribute
  1551.     and   al,0xf
  1552.     cmp   al,0xf
  1553.     je    clear_long_filename
  1554.  
  1555.     cmp   byte [ebx],'.'        ; parent or current directory
  1556.     je    clear_next_entry
  1557.     cmp   byte [ebx],0xe5       ; deleted
  1558.     je    clear_next_entry
  1559.     cmp   byte [ebx],0          ; empty
  1560.     je    clear_write_last
  1561.     ;je    clear_next_entry
  1562.  
  1563.     mov   eax,[ebx+20-2]        ; first cluster of entry
  1564.     mov   ax,[ebx+26]
  1565.     and   eax,[fatMASK]
  1566.  
  1567.     test  byte [ebx+11],0x10    ; is it directory?
  1568.     jz    clear_file            ; no
  1569.  
  1570.     push  eax ebx
  1571.     mov   eax,edi
  1572.     mov   ebx,deltree_buffer    ; save buffer over recursive call
  1573.     call  hd_write              ; write directory sector to disk
  1574.     cmp   [hd_error],0
  1575.     jne   clear_error    
  1576.  
  1577.     pop   ebx eax
  1578.  
  1579.     call  clear_directory       ; recursive call !!!
  1580.     jc    clear_error           ; exit if error found
  1581.  
  1582.     push  eax ebx
  1583.     mov   eax,edi
  1584.     mov   ebx,deltree_buffer
  1585.     call  hd_read               ; read directory sector again
  1586.     cmp  [hd_error],0
  1587.     jne   clear_error_1
  1588.  
  1589.     pop   ebx eax
  1590.  
  1591.   clear_file:
  1592.     call  clear_cluster_chain
  1593.     cmp  [hd_error],0
  1594.     jne   clear_error
  1595.  
  1596.   clear_long_filename:
  1597.     mov   byte [ebx],0xe5
  1598.  
  1599.   clear_next_entry:
  1600.     add   ebx,32                ; position of next dir entry
  1601.     dec   edx
  1602.     jnz   clear_analyze
  1603.  
  1604.     mov   eax,edi
  1605.     mov   ebx,deltree_buffer
  1606.     call  hd_write              ; write directory sector to disk
  1607.     cmp   [hd_error],0
  1608.     jne   clear_error
  1609.  
  1610.     inc   eax                   ; next sector
  1611.     dec   ecx
  1612.     jnz   clear_new_sector
  1613.  
  1614.     mov   eax,esi
  1615.     call  get_FAT               ; get next cluster
  1616.     cmp   [hd_error],0
  1617.     jne   clear_error
  1618.  
  1619.     jmp   clear_new_cluster     ; clear it
  1620.  
  1621.   clear_write_last:
  1622.     mov   eax,edi
  1623.     mov   ebx,deltree_buffer
  1624.     call  hd_write              ; write directory sector to disk
  1625.     cmp   [hd_error],0
  1626.     jne   clear_error
  1627.  
  1628.   clear_end:
  1629.     popad
  1630.     clc
  1631.     ret
  1632. clear_error_1:
  1633.     add  esp,8
  1634.   clear_error:
  1635.     popad
  1636.     stc
  1637.     ret
  1638.  
  1639.  
  1640. delete_entry_name:
  1641. ;-----------------------------------------------------
  1642. ; input  : eax = directory sector
  1643. ;          ebx = directory pointer in buffer
  1644. ;          longname_sec = 2 previous directory sectors
  1645. ; output : ecx = first cluster
  1646. ; change : eax,ebx,edx
  1647. ;-----------------------------------------------------
  1648.     mov   byte [ebx],0xe5
  1649.     mov   ecx,[ebx+20-2]        ; first cluster of file
  1650.     mov   cx,[ebx+26]           ; 0 length files start cluster = 0
  1651.     and   ecx,[fatMASK]
  1652.  
  1653.   delete_empty:
  1654.     sub   ebx,32
  1655.     cmp   ebx,buffer
  1656.     jnb   delete_test_long
  1657.  
  1658.     mov   ebx,buffer
  1659.     call  hd_write              ; write directory sector back
  1660.     cmp   [hd_error],0
  1661.     jne   delete_name_end
  1662.  
  1663.     xor   eax,eax
  1664.     xchg  eax,[longname_sec2]
  1665.     xchg  eax,[longname_sec1]
  1666.     test  eax,eax               ; is there previous directory sector?
  1667.     jz    delete_name_end       ; no
  1668.  
  1669.     mov   ebx,buffer
  1670.     call  hd_read               ; read previous sector
  1671.     cmp  [hd_error],0
  1672.     jne  delete_name_end    
  1673.  
  1674.     mov   ebx,buffer+0x1e0      ; start from last entry
  1675.  
  1676.   delete_test_long:
  1677.     mov   dh,[ebx+11]           ; file attribute
  1678.     and   dh,0xf
  1679.     cmp   dh,0xf
  1680.     jne   delete_write_buffer
  1681.  
  1682.     cmp   byte [ebx],0x40       ; end of long dir entry?
  1683.     mov   byte [ebx],0xe5
  1684.     jb    delete_empty
  1685.  
  1686.   delete_write_buffer:
  1687.     mov   ebx,buffer
  1688.     call  hd_write              ; write directory sector back
  1689.  
  1690.   delete_name_end:
  1691.     ret
  1692.  
  1693.  
  1694. rename:
  1695. ;-----------------------------------------------------------
  1696. ; input  : eax = source directory name
  1697. ;          edx = source path
  1698. ;          ebx = dest directory name
  1699. ;          edi = dest path
  1700. ; output : eax = 0 - ok
  1701. ;                3 - unknown FS
  1702. ;                5 - file not found
  1703. ;                8 - disk full
  1704. ;               10 - access denied
  1705. ;-----------------------------------------------------------
  1706.     cmp   [fat_type],0
  1707.     jnz   fat_ok_for_rename
  1708.     mov   eax,ERROR_UNKNOWN_FS
  1709.     ret
  1710.  
  1711.   fat_ok_for_rename:
  1712. ;    call  reserve_hd1
  1713.  
  1714.     pushad
  1715.  
  1716.     mov   ebx,edx               ; source path
  1717.     call  get_cluster_of_a_path
  1718.     jc    rename_entry_not_found
  1719.  
  1720.     mov   ebx,PUSHAD_EAX        ; source directory name
  1721.     call  analyze_directory
  1722.     jc    rename_entry_not_found
  1723.  
  1724.     mov   [sector_tmp],eax      ; save source sector
  1725.     mov   [entry_pos],ebx
  1726.     mov   esi,ebx
  1727.     mov   edi,dir_entry
  1728.     mov   ecx,32/4
  1729.     cld
  1730.     rep   movsd                 ; save entry
  1731.  
  1732.     mov   ebx,PUSHAD_EDI        ; dest path
  1733.     call  get_cluster_of_a_path
  1734.     jc    rename_entry_not_found
  1735.  
  1736.     mov   edx,eax               ; save dest directory cluster
  1737.     mov   ebx,PUSHAD_EBX        ; dest directory name
  1738.     push  [longname_sec1]
  1739.     push  [longname_sec2]
  1740.     call  analyze_directory     ; check if entry already exist
  1741.     cmp  [hd_error],0
  1742.     jne  rename_entry_already_exist_1
  1743.  
  1744.     pop   [longname_sec2]
  1745.     pop   [longname_sec1]
  1746.     jnc   rename_entry_already_exist
  1747.  
  1748.     mov   eax,edx
  1749.     call  analyze_directory_to_write
  1750.     jc    rename_disk_full
  1751.  
  1752.     mov   esi,dir_entry
  1753.     mov   edi,ebx
  1754.     mov   ecx,32/4
  1755.     cld
  1756.     rep   movsd                 ; copy entry
  1757.     mov   esi,PUSHAD_EBX        ; dest directory name
  1758.     mov   edi,ebx
  1759.     mov   ecx,11
  1760.     rep   movsb                 ; copy name
  1761.  
  1762.     mov   ebx,buffer            ; save the directory name,length,cluster
  1763.     call  hd_write
  1764.  
  1765.     test  byte [dir_entry+11],0x10 ; is it directory?
  1766.     jz    rename_not_dir           ; no
  1767.     mov   eax,[dir_entry+20-2]     ; FAT entry
  1768.     mov   ax,[dir_entry+26]
  1769.     and   eax,[fatMASK]
  1770.     call  change_2dot_cluster
  1771.     cmp   [hd_error],0
  1772.     jne   rename_entry_already_exist
  1773.  
  1774.   rename_not_dir:
  1775.     cmp   [hd_error],0
  1776.     jne   rename_entry_already_exist
  1777.     mov   eax,[sector_tmp]
  1778.     mov   ebx,buffer
  1779.     call  hd_read               ; read source directory sector
  1780.     cmp  [hd_error],0
  1781.     jne  rename_entry_already_exist
  1782.  
  1783.     mov   ebx,[entry_pos]
  1784.     call  delete_entry_name
  1785.     cmp   [hd_error],0
  1786.     jne   rename_entry_already_exist
  1787.  
  1788.     popad
  1789.     call  update_disk           ; write all of cache and fat to hd
  1790.     cmp   [hd_error],0
  1791.     jne   rename_entry_already_exist_2
  1792.     mov   [hd1_status],0
  1793.     xor   eax,eax
  1794.     ret
  1795.  
  1796.   rename_entry_not_found:
  1797.     cmp  [hd_error],0
  1798.     jne  rename_entry_already_exist
  1799.     popad
  1800.     mov   [hd1_status],0
  1801.     mov   eax,ERROR_FILE_NOT_FOUND
  1802.     ret
  1803.  
  1804.   rename_entry_already_exist_1:
  1805.     add   esp,8
  1806.   rename_entry_already_exist:
  1807.     popad
  1808.   rename_entry_already_exist_2:
  1809.     mov   [hd1_status],0
  1810.     mov   eax,ERROR_ACCESS_DENIED
  1811.     ret
  1812.  
  1813.   rename_disk_full:
  1814.     cmp  [hd_error],0
  1815.     jne  rename_entry_already_exist
  1816.     popad
  1817.     mov   [hd1_status],0
  1818.     mov   eax,ERROR_DISK_FULL
  1819.     ret
  1820.  
  1821.  
  1822. change_2dot_cluster:
  1823. ;-----------------------------------------------------------
  1824. ; input  : eax = directory cluster
  1825. ;          edx = value to save
  1826. ; change : eax,ebx,edx
  1827. ;-----------------------------------------------------------
  1828.     cmp   eax,[LAST_CLUSTER]
  1829.     ja    not_2dot              ; too big cluster number, something is wrong
  1830.     sub   eax,2
  1831.     jb    not_2dot
  1832.  
  1833.     imul  eax,[SECTORS_PER_CLUSTER]
  1834.     add   eax,[DATA_START]
  1835.     mov   ebx,buffer
  1836.     call  hd_read
  1837.     cmp  [hd_error],0
  1838.     jne  not_2dot
  1839.  
  1840.     cmp   dword [ebx+32],'..  '
  1841.     jnz   not_2dot
  1842.  
  1843.     cmp   edx,[ROOT_CLUSTER]    ; is rootdir cluster?
  1844.     jne   not_2dot_root
  1845.     xor   edx,edx               ; yes. set it zero
  1846.  
  1847.   not_2dot_root:
  1848.     mov   [ebx+32+26],dx        ; 16 bits low of cluster
  1849.     shr   edx,16
  1850.     mov   [ebx+32+20],dx        ; 16 bits high of cluster (=0 fat16)
  1851.     call  hd_write
  1852.  
  1853.   not_2dot:
  1854.     ret
  1855.  
  1856.  
  1857. get_hd_info:
  1858. ;-----------------------------------------------------------
  1859. ; output : eax = 0 - ok
  1860. ;                3 - unknown FS
  1861. ;               10 - access denied
  1862. ;          edx = cluster size in bytes
  1863. ;          ebx = total clusters on disk
  1864. ;          ecx = free clusters on disk
  1865. ;-----------------------------------------------------------
  1866.     cmp   [fat_type],0
  1867.     jnz   info_fat_ok
  1868.     xor   edx,edx
  1869.     xor   ebx,ebx
  1870.     xor   ecx,ecx
  1871.     mov   eax,ERROR_UNKNOWN_FS
  1872.     ret
  1873.  
  1874.   info_fat_ok:
  1875. ;    call  reserve_hd1
  1876.  
  1877.     xor   ecx,ecx               ; count of free clusters
  1878.     mov   eax,2
  1879.     mov   ebx,[LAST_CLUSTER]
  1880.  
  1881.   info_cluster:
  1882.     push  eax
  1883.     call  get_FAT               ; get cluster info
  1884.     cmp   [hd_error],0
  1885.     jne   info_access_denied
  1886.  
  1887.     test  eax,eax               ; is it free?
  1888.     jnz   info_used             ; no
  1889.     inc   ecx
  1890.  
  1891.   info_used:
  1892.     pop   eax
  1893.     inc   eax
  1894.     cmp   eax,ebx               ; is above last cluster?
  1895.     jbe   info_cluster          ; no. test next cluster
  1896.  
  1897.     dec   ebx                   ; cluster count
  1898.     imul  edx,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
  1899.     mov   [hd1_status],0
  1900.     xor   eax,eax
  1901.     ret
  1902.  
  1903.   info_access_denied:
  1904.     add   esp,4
  1905.     xor   edx,edx
  1906.     xor   ebx,ebx
  1907.     xor   ecx,ecx
  1908.     mov   eax,ERROR_ACCESS_DENIED
  1909.     ret
  1910.  
  1911. update_disk:
  1912. ;-----------------------------------------------------------
  1913. ; write changed fat and cache to disk
  1914. ;-----------------------------------------------------------
  1915.     cmp   [fat_change],0        ; is fat changed?
  1916.     je    upd_no_change
  1917.  
  1918.     call  write_fat_sector
  1919.     cmp   [hd_error],0
  1920.     jne   update_disk_acces_denied
  1921.  
  1922.   upd_no_change:
  1923.  
  1924.     call  write_cache
  1925.   update_disk_acces_denied:
  1926.     ret
  1927.  
  1928.  
  1929. ;**************************************************************************
  1930. ;
  1931. ;   0x600008  - first entry in cache list
  1932. ;
  1933. ;            +0   - lba sector
  1934. ;            +4   - state of cache sector
  1935. ;                   0 = empty
  1936. ;                   1 = used for read  ( same as in hd )
  1937. ;                   2 = used for write ( differs from hd )
  1938. ;
  1939. ;      +65536 - cache entries
  1940. ;
  1941. ;**************************************************************************
  1942.  
  1943.  
  1944. hd_read:
  1945. ;-----------------------------------------------------------
  1946. ; input  : eax = block to read
  1947. ;          ebx = destination
  1948. ;-----------------------------------------------------------
  1949.     push  ecx esi edi           ; scan cache
  1950.  
  1951.     mov   ecx,cache_max         ; entries in cache
  1952.     mov   esi,0x600000+8
  1953.     mov   edi,1
  1954.  
  1955.   hdreadcache:
  1956.  
  1957.     cmp   dword [esi+4],0       ; empty
  1958.     je    nohdcache
  1959.  
  1960.     cmp   [esi],eax             ; correct sector
  1961.     je    yeshdcache
  1962.  
  1963.   nohdcache:
  1964.  
  1965.     add   esi,8
  1966.     inc   edi
  1967.     dec   ecx
  1968.     jnz   hdreadcache
  1969.  
  1970.     call  find_empty_slot       ; ret in edi
  1971.     cmp   [hd_error],0
  1972.     jne   return_01
  1973.  
  1974.     push  eax edx
  1975.  
  1976.     call  wait_for_hd_idle
  1977.     cmp   [hd_error],0
  1978.     jne   hd_read_error    
  1979.  
  1980.     cli
  1981.     xor   eax,eax
  1982.     mov   edx,[hdbase]
  1983.     inc   edx
  1984.     out   dx,al   ; ATAFeatures ॣ¨áâà "®á®¡¥­­®á⥩"
  1985.     inc   edx
  1986.     inc   eax
  1987.     out   dx,al   ; ATASectorCount áç¥â稪 ᥪâ®à®¢
  1988.     inc   edx
  1989.     mov   eax,[esp+4]
  1990.     out   dx,al   ; ATASectorNumber ॣ¨áâà ­®¬¥à  ᥪâ®à 
  1991.     shr   eax,8
  1992.     inc   edx
  1993.     out   dx,al   ; ATACylinder ­®¬¥à 樫¨­¤à  (¬« ¤è¨© ¡ ©â)
  1994.     shr   eax,8
  1995.     inc   edx
  1996.     out   dx,al   ; ­®¬¥à 樫¨­¤à  (áâ à訩 ¡ ©â)
  1997.     shr   eax,8
  1998.     inc   edx
  1999.     and   al,1+2+4+8
  2000.     add   al,byte [hdid]
  2001.     add   al,128+64+32
  2002.     out   dx,al   ; ­®¬¥à £®«®¢ª¨/­®¬¥à ¤¨áª 
  2003.     inc   edx
  2004.     mov   al,20h
  2005.     out   dx,al   ; ATACommand ॣ¨áâà ª®¬ ­¤
  2006.     sti
  2007.  
  2008.     call  wait_for_sector_buffer
  2009.  
  2010.     cmp   [hd_error],0
  2011.     jne   hd_read_error
  2012.  
  2013.     cli
  2014.     push  edi
  2015.     shl   edi,9
  2016.     add   edi,0x600000+65536
  2017.     mov   ecx,256
  2018.     mov   edx,[hdbase]
  2019.     cld
  2020.     rep   insw
  2021.     pop   edi
  2022.     sti
  2023.  
  2024.     pop   edx eax
  2025.  blok_read_2:
  2026.     lea   esi,[edi*8+0x600000]
  2027.     mov   [esi],eax             ; sector number
  2028.     mov   dword [esi+4],1       ; hd read - mark as same as in hd
  2029.  
  2030.   yeshdcache:
  2031.  
  2032.     mov   esi,edi
  2033.     shl   esi,9
  2034.     add   esi,0x600000+65536
  2035.     mov   edi,ebx
  2036.     mov   ecx,512/4
  2037.     cld
  2038.     rep   movsd                 ; move data
  2039.  return_01:
  2040.     pop   edi esi ecx
  2041.     ret
  2042.    
  2043.  
  2044. hd_write:
  2045. ;-----------------------------------------------------------
  2046. ; input  : eax = block
  2047. ;          ebx = pointer to memory
  2048. ;-----------------------------------------------------------
  2049.     push  ecx esi edi
  2050.  
  2051.     ; check if the cache already has the sector and overwrite it
  2052.  
  2053.     mov   ecx,cache_max
  2054.     mov   esi,0x600000+8
  2055.     mov   edi,1
  2056.  
  2057.   hdwritecache:
  2058.  
  2059.     cmp   dword [esi+4],0       ; if cache slot is empty
  2060.     je    not_in_cache_write
  2061.  
  2062.     cmp   [esi],eax             ; if the slot has the sector
  2063.     je    yes_in_cache_write
  2064.  
  2065.   not_in_cache_write:
  2066.  
  2067.     add   esi,8
  2068.     inc   edi
  2069.     dec   ecx
  2070.     jnz   hdwritecache
  2071.  
  2072.     ; sector not found in cache
  2073.     ; write the block to a new location
  2074.  
  2075.     call  find_empty_slot       ; ret in edi
  2076.     cmp   [hd_error],0
  2077.     jne   hd_write_access_denied
  2078.  
  2079.     lea   esi,[edi*8+0x600000]
  2080.     mov   [esi],eax             ; sector number
  2081.  
  2082.   yes_in_cache_write:
  2083.  
  2084.     mov   dword [esi+4],2       ; write - differs from hd
  2085.  
  2086.     shl   edi,9
  2087.     add   edi,0x600000+65536
  2088.     mov   esi,ebx
  2089.     mov   ecx,512/4
  2090.     cld
  2091.     rep   movsd                 ; move data
  2092.  hd_write_access_denied:
  2093.     pop   edi esi ecx
  2094.     ret
  2095.  
  2096.  
  2097. write_cache:
  2098. ;-----------------------------------------------------------
  2099. ; write all changed sectors to disk
  2100. ;-----------------------------------------------------------
  2101.     push  eax ecx edx esi edi
  2102.  
  2103.     ; write difference ( 2 ) from cache to hd
  2104.  
  2105.     mov   ecx,cache_max
  2106.     mov   esi,0x600000+8
  2107.     mov   edi,1
  2108.  
  2109.   write_cache_more:
  2110.  
  2111.     cmp   dword [esi+4],2       ; if cache slot is not different
  2112.     jne   does_not_need_writing
  2113.  
  2114.     mov   dword [esi+4],1       ; same as in hd
  2115.     mov   eax,[esi]             ; eax = sector to write
  2116.  
  2117.     cmp   eax,[PARTITION_START]
  2118.     jb    danger
  2119.     cmp   eax,[PARTITION_END]
  2120.     ja    danger
  2121.  
  2122.     call  wait_for_hd_idle
  2123.     cmp   [hd_error],0
  2124.     jne   hd_write_error
  2125.  
  2126.     cli
  2127.     xor   eax,eax
  2128.     mov   edx,[hdbase]
  2129.     inc   edx
  2130.     out   dx,al
  2131.     inc   edx
  2132.     inc   eax
  2133.     out   dx,al
  2134.     inc   edx
  2135.     mov   eax,[esi]             ; eax = sector to write
  2136.     out   dx,al
  2137.     shr   eax,8
  2138.     inc   edx
  2139.     out   dx,al
  2140.     shr   eax,8
  2141.     inc   edx
  2142.     out   dx,al
  2143.     shr   eax,8
  2144.     inc   edx
  2145.     and   al,1+2+4+8
  2146.     add   al,byte [hdid]
  2147.     add   al,128+64+32
  2148.     out   dx,al
  2149.     inc   edx
  2150.     mov   al,30h
  2151.     out   dx,al
  2152.     sti
  2153.  
  2154.     call  wait_for_sector_buffer
  2155.  
  2156.     cmp   [hd_error],0
  2157.     jne   hd_write_error
  2158.  
  2159.     push  ecx esi
  2160.  
  2161.     cli
  2162.     mov   esi,edi
  2163.     shl   esi,9
  2164.     add   esi,0x600000+65536    ; esi = from memory position
  2165.     mov   ecx,256
  2166.     mov   edx,[hdbase]
  2167.     cld
  2168.     rep   outsw
  2169.     sti
  2170.  
  2171.     pop   esi ecx
  2172.  
  2173.   danger:
  2174.   does_not_need_writing:
  2175.  
  2176.     add   esi,8
  2177.     inc   edi
  2178.     dec   ecx
  2179.     jnz   write_cache_more
  2180.  return_02:
  2181.     pop   edi esi edx ecx eax
  2182.     ret
  2183.  
  2184.  
  2185. find_empty_slot:
  2186. ;-----------------------------------------------------------
  2187. ; find empty or read slot, flush cache if next 10% is used by write
  2188. ; output : edi = cache slot
  2189. ;-----------------------------------------------------------
  2190.     push  ecx esi
  2191.  
  2192.   search_again:
  2193.  
  2194.     mov   ecx,cache_max*10/100
  2195.     mov   edi,[cache_search_start]
  2196.  
  2197.   search_for_empty:
  2198.  
  2199.     inc   edi
  2200.     cmp   edi,cache_max
  2201.     jbe   inside_cache
  2202.     mov   edi,1
  2203.  
  2204.   inside_cache:
  2205.  
  2206.     cmp   dword [edi*8+0x600000+4],2    ; get cache slot info
  2207.     jb    found_slot                    ; it's empty or read
  2208.     dec   ecx
  2209.     jnz   search_for_empty
  2210.  
  2211.     call  write_cache                   ; no empty slots found, write all
  2212.     cmp   [hd_error],0
  2213.     jne   found_slot_access_denied
  2214.  
  2215.     jmp   search_again                  ; and start again
  2216.  
  2217.   found_slot:
  2218.  
  2219.     mov   [cache_search_start],edi
  2220.   found_slot_access_denied:
  2221.     pop   esi ecx
  2222.     ret
  2223.  
  2224.  
  2225. save_hd_wait_timeout:
  2226.  
  2227.     push  eax
  2228.     mov   eax,[timer_ticks];[0xfdf0]
  2229.     add   eax,300               ; 3 sec timeout
  2230.     mov   [hd_wait_timeout],eax
  2231.     pop   eax
  2232.     ret
  2233.  
  2234.  
  2235. check_hd_wait_timeout:
  2236.  
  2237.     push  eax
  2238.     mov   eax,[hd_wait_timeout]
  2239.     cmp   [timer_ticks], eax ;[0xfdf0],eax
  2240.     jg    hd_timeout_error
  2241.     pop   eax
  2242.     mov   [hd_error],0
  2243.     ret
  2244.  
  2245. iglobal
  2246.   hd_timeout_str   db 'K : FS - HD timeout',13,10,0
  2247.   hd_read_str      db 'K : FS - HD read error',13,10,0
  2248.   hd_write_str     db 'K : FS - HD write error',13,10,0
  2249.   hd_lba_str       db 'K : FS - HD LBA error',13,10,0
  2250. endg
  2251.  
  2252. hd_timeout_error:
  2253.  
  2254.     call  clear_hd_cache
  2255.     call  clear_application_table_status
  2256.     mov   esi,hd_timeout_str
  2257.     call  sys_msg_board_str
  2258. ;    jmp   $
  2259.     mov   [hd_error],1
  2260.     pop   eax
  2261.     ret
  2262.  
  2263. hd_read_error:
  2264.  
  2265.     call  clear_hd_cache
  2266.     call  clear_application_table_status
  2267.     mov   esi,hd_read_str
  2268.     call  sys_msg_board_str
  2269.     pop   edx eax
  2270.     jmp    return_01
  2271. ;    jmp   $
  2272.  
  2273. hd_write_error:
  2274.  
  2275.     call  clear_hd_cache
  2276.     call  clear_application_table_status
  2277.     mov   esi,hd_write_str
  2278.     call  sys_msg_board_str
  2279.     jmp    return_02
  2280. ;    jmp   $
  2281.  
  2282. hd_lba_error:
  2283.     call  clear_hd_cache
  2284.     call  clear_application_table_status
  2285.     mov   esi,hd_lba_str
  2286.     call  sys_msg_board_str
  2287.     jmp   LBA_read_ret
  2288.  
  2289.  
  2290. wait_for_hd_idle:
  2291.  
  2292.     push  eax edx
  2293.  
  2294.     call  save_hd_wait_timeout
  2295.  
  2296.     mov   edx,[hdbase]
  2297.     add   edx,0x7
  2298.  
  2299.   wfhil1:
  2300.  
  2301.     call  check_hd_wait_timeout
  2302.     cmp   [hd_error],0
  2303.     jne   @f
  2304.  
  2305.     in    al,dx
  2306.     test  al,128
  2307.     jnz   wfhil1
  2308.  
  2309.  @@:
  2310.  
  2311.     pop   edx eax
  2312.     ret
  2313.  
  2314.  
  2315.  
  2316. wait_for_sector_buffer:
  2317.  
  2318.     push  eax edx
  2319.  
  2320.     mov   edx,[hdbase]
  2321.     add   edx,0x7
  2322.  
  2323.     call  save_hd_wait_timeout
  2324.  
  2325.   hdwait_sbuf:                  ; wait for sector buffer to be ready
  2326.  
  2327.     call  check_hd_wait_timeout
  2328.     cmp   [hd_error],0
  2329.     jne   @f
  2330.  
  2331.     in    al,dx
  2332.     test  al,8
  2333.     jz    hdwait_sbuf
  2334.  
  2335.     mov   [hd_error],0
  2336.  
  2337.     cmp   [hd_setup],1          ; do not mark error for setup request
  2338.     je    buf_wait_ok
  2339.  
  2340.     test  al,1                  ; previous command ended up with an error
  2341.     jz    buf_wait_ok
  2342.  @@:
  2343.     mov   [hd_error],1
  2344.  
  2345.   buf_wait_ok:
  2346.  
  2347.     pop   edx eax
  2348.     ret
  2349.  
  2350.  
  2351.  
  2352. read_hd_file:
  2353. ;-----------------------------------------------------------------
  2354. ;
  2355. ; Converting old reading function for hd-application start.
  2356. ;
  2357. ; IN:
  2358. ;
  2359. ; eax - pointer to file (0 = read only first sector of drive: eg 'label')
  2360. ; ebx - file lenght
  2361. ; ecx - start 512 byte block number
  2362. ; edx - number of blocks to read
  2363. ; esi - pointer to return/work area (atleast 20 000 bytes)
  2364. ;
  2365. ; For new read function
  2366. ;
  2367. ; EAX   (PAR0)      pointer to file-name
  2368. ; ECX   (PAR1)      pointer to buffer
  2369. ; EBX   (PAR2)   vt file blocks to read
  2370. ; EDX   (PAR3)      pointer to path
  2371. ; ESI            vt first 512 block to read
  2372. ; EDI               if 0 - return root
  2373. ;--------------------------------------------------------------------------
  2374.  
  2375.     push  ecx esi edi
  2376.     mov   esi,eax
  2377.     mov   edi,startpath
  2378.     mov   ecx,250
  2379.     cld
  2380.     rep   movsb
  2381.     pop   edi esi ecx
  2382.  
  2383.     mov   eax,startpath
  2384.     mov   [eax+ebx-12],byte 0
  2385.  
  2386.     push  eax ebx ecx edx esi
  2387.  
  2388.     pop   ecx ; pointer to buffer
  2389.     add   ecx,1024
  2390.     pop   ebx ; number of blocks to read
  2391.     pop   esi ; first block to read
  2392.     dec   esi
  2393.     pop   eax ; file length
  2394.     pop   edx ; pointer to path
  2395.  
  2396.     mov   edi,12
  2397.     lea   eax,[eax+edx-12+1]
  2398.     call  file_read
  2399.  
  2400.     ret
  2401.  
  2402. ; \begin{diamond}
  2403. hd_find_lfn:
  2404. ; in: esi->name
  2405. ; out: CF=1 - file not found
  2406. ;      else CF=0 and edi->direntry, eax=sector
  2407. ; destroys eax
  2408.         push    esi edi
  2409.         push    0
  2410.         push    0
  2411.         push    fat16_root_first
  2412.         push    fat16_root_next
  2413.         mov     eax, [ROOT_CLUSTER]
  2414.         cmp     [fat_type], 32
  2415.         jz      .fat32
  2416. .loop:
  2417.         call    fat_find_lfn
  2418.         jc      .notfound
  2419.         cmp     byte [esi], 0
  2420.         jz      .found
  2421.         test    byte [edi+11], 10h
  2422.         jz      .notfound
  2423.         and     dword [esp+12], 0
  2424.         mov     eax, [edi+20-2]
  2425.         mov     ax, [edi+26]    ; cluster
  2426. .fat32:
  2427.         mov     [esp+8], eax
  2428.         mov     dword [esp+4], fat_notroot_first
  2429.         mov     dword [esp], fat_notroot_next
  2430.         jmp     .loop
  2431. .notfound:
  2432.         add     esp, 16
  2433.         pop     edi esi
  2434.         stc
  2435.         ret
  2436. .found:
  2437.         lea     eax, [esp+8]
  2438.         cmp     dword [eax], 0
  2439.         jz      .root
  2440.         call    fat_get_sector
  2441.         jmp     .cmn
  2442. .root:
  2443.         mov     eax, [eax+4]
  2444.         add     eax, [ROOT_START]
  2445. .cmn:
  2446.         add     esp, 20         ; CF=0
  2447.         pop     esi
  2448.         ret
  2449.  
  2450. ;----------------------------------------------------------------
  2451. ;
  2452. ;  fs_HdRead - LFN variant for reading hard disk
  2453. ;
  2454. ;  esi  points to filename
  2455. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  2456. ;       may be ebx=0 - start from first byte
  2457. ;  ecx  number of bytes to read, 0+
  2458. ;  edx  mem location to return data
  2459. ;
  2460. ;  ret ebx = bytes read or 0xffffffff file not found
  2461. ;      eax = 0 ok read or other = errormsg
  2462. ;
  2463. ;--------------------------------------------------------------
  2464. fs_HdRead:
  2465.     cmp    [fat_type], 0
  2466.     jnz    @f
  2467.     or    ebx, -1
  2468.     mov    eax, ERROR_UNKNOWN_FS
  2469.     ret
  2470. @@:
  2471.     push    edi
  2472.     cmp    byte [esi], 0
  2473.     jnz    @f
  2474. .noaccess:
  2475.     pop    edi
  2476. .noaccess_2:
  2477.     or    ebx, -1
  2478.     mov    eax, ERROR_ACCESS_DENIED
  2479.     ret
  2480.  
  2481. .noaccess_3:
  2482.     add esp,4
  2483. .noaccess_1:
  2484.     add esp,4
  2485. .noaccess_4:
  2486.     add esp,4*5
  2487.     jmp  .noaccess_2
  2488.  
  2489. @@:
  2490.     call    hd_find_lfn
  2491.     jnc    .found
  2492.     pop    edi
  2493.     cmp   [hd_error],0
  2494.     jne   .noaccess_2
  2495.     or    ebx, -1
  2496.     mov    eax, ERROR_FILE_NOT_FOUND
  2497.     ret
  2498.  
  2499. .found:
  2500.     test    byte [edi+11], 0x10    ; do not allow read directories
  2501.     jnz    .noaccess
  2502.     test    ebx, ebx
  2503.     jz    .l1
  2504.     cmp    dword [ebx+4], 0
  2505.     jz    @f
  2506.         xor     ebx, ebx
  2507. .reteof:
  2508.     mov    eax, 6
  2509.     pop    edi
  2510.     ret
  2511. @@:
  2512.     mov    ebx, [ebx]
  2513. .l1:
  2514.         push    ecx edx
  2515.         push    0
  2516.         mov     eax, [edi+28]
  2517.         sub     eax, ebx
  2518.         jb      .eof
  2519.         cmp     eax, ecx
  2520.         jae     @f
  2521.         mov     ecx, eax
  2522.         mov     byte [esp], 6
  2523. @@:
  2524.     mov    eax, [edi+20-2]
  2525.     mov    ax, [edi+26]
  2526. ; now eax=cluster, ebx=position, ecx=count, edx=buffer for data
  2527. .new_cluster:
  2528.     jecxz    .new_sector
  2529.     test    eax, eax
  2530.     jz    .eof
  2531.     cmp    eax, [fatRESERVED]
  2532.     jae    .eof
  2533.     mov    [cluster_tmp], eax
  2534.     dec    eax
  2535.     dec    eax
  2536.     mov    edi, [SECTORS_PER_CLUSTER]
  2537.     imul    eax, edi
  2538.     add    eax, [DATA_START]
  2539. .new_sector:
  2540.     test    ecx, ecx
  2541.     jz    .done
  2542.     sub    ebx, 512
  2543.     jae    .skip
  2544.     add    ebx, 512
  2545.     jnz    .force_buf
  2546.     cmp    ecx, 512
  2547.     jb    .force_buf
  2548. ; we may read directly to given buffer
  2549.     push    ebx
  2550.     mov    ebx, edx
  2551.     call    hd_read
  2552.     cmp  [hd_error],0
  2553.     jne  .noaccess_1    
  2554.     pop    ebx
  2555.     add    edx, 512
  2556.     sub    ecx, 512
  2557.     jmp    .skip
  2558. .force_buf:
  2559. ; we must read sector to temporary buffer and then copy it to destination
  2560.     push    eax ebx
  2561.     mov    ebx, buffer
  2562.     call    hd_read
  2563.     cmp  [hd_error],0
  2564.     jne  .noaccess_3
  2565.  
  2566.     mov    eax, ebx
  2567.     pop    ebx
  2568.     add    eax, ebx
  2569.     push    ecx
  2570.     add    ecx, ebx
  2571.     cmp    ecx, 512
  2572.     jbe    @f
  2573.     mov    ecx, 512
  2574. @@:
  2575.     sub    ecx, ebx
  2576.     mov    ebx, edx
  2577.     call    memmove
  2578.     add    edx, ecx
  2579.     sub    [esp], ecx
  2580.     pop    ecx
  2581.     pop    eax
  2582.     xor    ebx, ebx
  2583. .skip:
  2584.     inc    eax
  2585.     dec    edi
  2586.     jnz    .new_sector
  2587.     mov    eax, [cluster_tmp]
  2588.     call    get_FAT
  2589.     cmp   [hd_error],0
  2590.     jne   .noaccess_4
  2591.  
  2592.     jmp    .new_cluster
  2593. .done:
  2594.         mov     ebx, edx
  2595.         pop     eax edx ecx edi
  2596.         sub     ebx, edx
  2597.         ret
  2598. .eof:
  2599.         mov     ebx, edx
  2600.         pop     eax edx ecx
  2601.         sub     ebx, edx
  2602.         jmp     .reteof
  2603.  
  2604. ;----------------------------------------------------------------
  2605. ;
  2606. ;  fs_HdReadFolder - LFN variant for reading hard disk folder
  2607. ;
  2608. ;  esi  points to filename
  2609. ;  ebx  pointer to structure 32-bit number = first wanted block, 0+
  2610. ;                          & flags (bitfields)
  2611. ; flags: bit 0: 0=ANSI names, 1=UNICODE names
  2612. ;  ecx  number of blocks to read, 0+
  2613. ;  edx  mem location to return data
  2614. ;
  2615. ;  ret ebx = blocks read or 0xffffffff folder not found
  2616. ;      eax = 0 ok read or other = errormsg
  2617. ;
  2618. ;--------------------------------------------------------------
  2619. fs_HdReadFolder:
  2620.         mov     eax, [ROOT_CLUSTER]
  2621.         push    edi
  2622.         cmp     byte [esi], 0
  2623.         jz      .doit
  2624.         call    hd_find_lfn
  2625.         jnc     .found
  2626.         pop     edi
  2627.         or      ebx, -1
  2628.         mov     eax, ERROR_FILE_NOT_FOUND
  2629.         ret
  2630. .found:
  2631.         test    byte [edi+11], 0x10     ; do not allow read files
  2632.         jnz     .found_dir
  2633.         pop     edi
  2634.         or      ebx, -1
  2635.         mov     eax, ERROR_ACCESS_DENIED
  2636.         ret
  2637. .found_dir:
  2638.         mov     eax, [edi+20-2]
  2639.         mov     ax, [edi+26]    ; eax=cluster
  2640. .doit:
  2641.         push    esi ecx
  2642.         push    ebp
  2643.         sub     esp, 262*2      ; reserve space for LFN
  2644.         mov     ebp, esp
  2645.         push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE name
  2646.         mov     ebx, [ebx]
  2647. ; init header
  2648.         push    eax ecx
  2649.         mov     edi, edx
  2650.         mov     ecx, 32/4
  2651.         xor     eax, eax
  2652.         rep     stosd
  2653.         pop     ecx eax
  2654.         mov     byte [edx], 1   ; version
  2655.         mov     esi, edi        ; esi points to BDFE
  2656. .new_cluster:
  2657.         mov     [cluster_tmp], eax
  2658.         test    eax, eax
  2659.         jnz     @f
  2660.         cmp     [fat_type], 32
  2661.         jz      .notfound
  2662.         mov     eax, [ROOT_START]
  2663.         push    [ROOT_SECTORS]
  2664.         push    ebx
  2665.         jmp     .new_sector
  2666. @@:
  2667.         dec     eax
  2668.         dec     eax
  2669.         imul    eax, [SECTORS_PER_CLUSTER]
  2670.         push    [SECTORS_PER_CLUSTER]
  2671.         add     eax, [DATA_START]
  2672.         push    ebx
  2673. .new_sector:
  2674.         mov     ebx, buffer
  2675.         mov     edi, ebx
  2676.         call    hd_read
  2677.         cmp     [hd_error], 0
  2678.         jnz     .notfound2
  2679.         add     ebx, 512
  2680.         push    eax
  2681. .l1:
  2682.         call    fat_get_name
  2683.         jc      .l2
  2684.         cmp     byte [edi+11], 0xF
  2685.         jnz     .do_bdfe
  2686.         add     edi, 0x20
  2687.         cmp     edi, ebx
  2688.         jb      .do_bdfe
  2689.         pop     eax
  2690.         inc     eax
  2691.         dec     dword [esp+4]
  2692.         jnz     @f
  2693.         mov     eax, [cluster_tmp]
  2694.         test    eax, eax
  2695.         jz      .done
  2696.         call    get_FAT
  2697.         cmp     [hd_error], 0
  2698.         jnz     .notfound2
  2699.         cmp     eax, 2
  2700.         jb      .done
  2701.         cmp     eax, [fatRESERVED]
  2702.         jae     .done
  2703.         push    eax
  2704.         mov     eax, [SECTORS_PER_CLUSTER]
  2705.         mov     [esp+8], eax
  2706.         pop     eax
  2707.         mov     [cluster_tmp], eax
  2708.         dec     eax
  2709.         dec     eax
  2710.         imul    eax, [SECTORS_PER_CLUSTER]
  2711.         add     eax, [DATA_START]
  2712. @@:
  2713.         mov     ebx, buffer
  2714.         mov     edi, ebx
  2715.         call    hd_read
  2716.         cmp     [hd_error], 0
  2717.         jnz     .notfound2
  2718.         add     ebx, 512
  2719.         push    eax
  2720. .do_bdfe:
  2721.         inc     dword [edx+8]   ; new file found
  2722.         dec     dword [esp+4]
  2723.         jns     .l2
  2724.         dec     ecx
  2725.         js      .l2
  2726.         inc     dword [edx+4]   ; new file block copied
  2727.         call    fat_entry_to_bdfe
  2728. .l2:
  2729.         add     edi, 0x20
  2730.         cmp     edi, ebx
  2731.         jb      .l1
  2732.         pop     eax
  2733.         inc     eax
  2734.         dec     dword [esp+4]
  2735.         jnz     .new_sector
  2736.         mov     eax, [cluster_tmp]
  2737.         test    eax, eax
  2738.         jz      .done
  2739.         call    get_FAT
  2740.         cmp     [hd_error], 0
  2741.         jnz     .notfound2
  2742.         cmp     eax, 2
  2743.         jb      .done
  2744.         cmp     eax, [fatRESERVED]
  2745.         jae     .done
  2746.         push    eax
  2747.         mov     eax, [SECTORS_PER_CLUSTER]
  2748.         mov     [esp+8], eax
  2749.         pop     eax
  2750.         pop     ebx
  2751.         add     esp, 4
  2752.         jmp     .new_cluster
  2753. .notfound2:
  2754.         add     esp, 8
  2755. .notfound:
  2756.         add     esp, 262*2+4
  2757.         pop     ebp ecx esi edi
  2758.         mov     eax, ERROR_FILE_NOT_FOUND
  2759.         or      ebx, -1
  2760.         ret
  2761. .done:
  2762.         add     esp, 262*2+4+8
  2763.         pop     ebp
  2764.         mov     ebx, [edx+4]
  2765.         xor     eax, eax
  2766.         dec     ecx
  2767.         js      @f
  2768.         mov     al, ERROR_END_OF_FILE
  2769. @@:
  2770.         pop     ecx esi edi
  2771.         ret
  2772.  
  2773. fat16_root_next:
  2774.         cmp     edi, buffer+0x200-0x20
  2775.         jae     fat16_root_next_sector
  2776.         add     edi, 0x20
  2777.         ret     ; CF=0
  2778. fat16_root_next_sector:
  2779. ; read next sector
  2780.         push    ecx
  2781.         mov     ecx, [eax+4]
  2782.         inc     ecx
  2783.         mov     [eax+4], ecx
  2784.         cmp     ecx, [ROOT_SECTORS]
  2785.         pop     ecx
  2786.         jae     fat16_root_first.readerr
  2787. fat16_root_first:
  2788.         mov     eax, [eax+4]
  2789.         add     eax, [ROOT_START]
  2790.         push    ebx
  2791.         mov     edi, buffer
  2792.         mov     ebx, edi
  2793.         call    hd_read
  2794.         pop     ebx
  2795.         cmp     [hd_error], 0
  2796.         jnz     .readerr
  2797.         ret     ; CF=0
  2798. .readerr:
  2799.         stc
  2800.         ret
  2801. fat16_root_begin_write:
  2802.         push    edi eax
  2803.         call    fat16_root_first
  2804.         pop     eax edi
  2805.         ret
  2806. fat16_root_end_write:
  2807.         pusha
  2808.         mov     eax, [eax+4]
  2809.         add     eax, [ROOT_START]
  2810.         mov     ebx, buffer
  2811.         call    hd_write
  2812.         popa
  2813.         ret
  2814. fat16_root_next_write:
  2815.         cmp     edi, buffer+0x200
  2816.         jae     @f
  2817.         ret
  2818. @@:
  2819.         call    fat16_root_end_write
  2820.         jmp     fat16_root_next_sector
  2821. fat16_root_extend_dir:
  2822.         stc
  2823.         ret
  2824.  
  2825. fat_notroot_next:
  2826.         cmp     edi, buffer+0x200-0x20
  2827.         jae     fat_notroot_next_sector
  2828.         add     edi, 0x20
  2829.         ret     ; CF=0
  2830. fat_notroot_next_sector:
  2831.         push    ecx
  2832.         mov     ecx, [eax+4]
  2833.         inc     ecx
  2834.         cmp     ecx, [SECTORS_PER_CLUSTER]
  2835.         jae     fat_notroot_next_cluster
  2836.         mov     [eax+4], ecx
  2837.         jmp     @f
  2838. fat_notroot_next_cluster:
  2839.         push    eax
  2840.         mov     eax, [eax]
  2841.         call    get_FAT
  2842.         mov     ecx, eax
  2843.         pop     eax
  2844.         cmp     [hd_error], 0
  2845.         jnz     fat_notroot_next_err
  2846.         cmp     ecx, [fatRESERVED]
  2847.         jae     fat_notroot_next_err
  2848.         mov     [eax], ecx
  2849.         and     dword [eax+4], 0
  2850. @@:
  2851.         pop     ecx
  2852. fat_notroot_first:
  2853.         call    fat_get_sector
  2854.         push    ebx
  2855.         mov     edi, buffer
  2856.         mov     ebx, edi
  2857.         call    hd_read
  2858.         pop     ebx
  2859.         cmp     [hd_error], 0
  2860.         jnz     @f
  2861.         ret     ; CF=0
  2862. fat_notroot_next_err:
  2863.         pop     ecx
  2864. @@:
  2865.         stc
  2866.         ret
  2867. fat_notroot_begin_write:
  2868.         push    eax edi
  2869.         call    fat_notroot_first
  2870.         pop     edi eax
  2871.         ret
  2872. fat_notroot_end_write:
  2873.         call    fat_get_sector
  2874.         push    ebx
  2875.         mov     ebx, buffer
  2876.         call    hd_write
  2877.         pop     ebx
  2878.         ret
  2879. fat_notroot_next_write:
  2880.         cmp     edi, buffer+0x200
  2881.         jae     @f
  2882.         ret
  2883. @@:
  2884.         push    eax
  2885.         call    fat_notroot_end_write
  2886.         pop     eax
  2887.         jmp     fat_notroot_next_sector
  2888. fat_notroot_extend_dir:
  2889.         push    eax
  2890.         mov     eax, [eax]
  2891.         call    get_free_FAT
  2892.         jnc     .found
  2893.         pop     eax
  2894.         ret     ; CF=1
  2895. .found:
  2896.         push    edx
  2897.         mov     edx, [fatEND]
  2898.         call    set_FAT
  2899.         mov     edx, eax
  2900.         mov     eax, [esp+4]
  2901.         mov     eax, [eax]
  2902.         push    edx
  2903.         call    set_FAT
  2904.         pop     edx
  2905.         cmp     [hd_error], 0
  2906.         jz      @f
  2907.         pop     edx
  2908.         pop     eax
  2909.         stc
  2910.         ret
  2911. @@:
  2912.         push    ecx
  2913.         or      ecx, -1
  2914.         call    add_disk_free_space
  2915. ; zero new cluster
  2916.         mov     ecx, 512/4
  2917.         mov     edi, buffer
  2918.         push    edi
  2919.         xor     eax, eax
  2920.         rep     stosd
  2921.         pop     edi
  2922.         pop     ecx
  2923.         mov     eax, [esp+4]
  2924.         mov     [eax], edx
  2925.         and     dword [eax+4], 0
  2926.         pop     edx
  2927.         mov     eax, [eax]
  2928.         dec     eax
  2929.         dec     eax
  2930.         push    ebx ecx
  2931.         mov     ecx, [SECTORS_PER_CLUSTER]
  2932.         imul    eax, ecx
  2933.         add     eax, [DATA_START]
  2934.         mov     ebx, edi
  2935. @@:
  2936.         call    hd_write
  2937.         inc     eax
  2938.         loop    @b
  2939.         pop     ecx ebx eax
  2940.         clc
  2941.         ret
  2942.  
  2943. fat_get_sector:
  2944.         push    ecx
  2945.         mov     ecx, [eax]
  2946.         dec     ecx
  2947.         dec     ecx
  2948.         imul    ecx, [SECTORS_PER_CLUSTER]
  2949.         add     ecx, [DATA_START]
  2950.         add     ecx, [eax+4]
  2951.         mov     eax, ecx
  2952.         pop     ecx
  2953.         ret
  2954.  
  2955. ;----------------------------------------------------------------
  2956. ;
  2957. ;  fs_HdRewrite - LFN variant for writing hard disk
  2958. ;
  2959. ;  esi  points to filename
  2960. ;  ebx  ignored (reserved)
  2961. ;  ecx  number of bytes to write, 0+
  2962. ;  edx  mem location to data
  2963. ;
  2964. ;  ret ebx = number of written bytes
  2965. ;      eax = 0 ok read or other = errormsg
  2966. ;
  2967. ;--------------------------------------------------------------
  2968. fshrad:
  2969.         mov     eax, ERROR_ACCESS_DENIED
  2970.         xor     ebx, ebx
  2971.         ret
  2972. fshrfs:
  2973.         mov     eax, ERROR_UNKNOWN_FS
  2974.         xor     ebx, ebx
  2975.         ret
  2976.  
  2977. fs_HdRewrite:
  2978.         cmp     [fat_type], 0
  2979.         jz      fshrfs
  2980.         cmp     byte [esi], 0
  2981.         jz      fshrad
  2982.         pushad
  2983.         xor     ebp, ebp
  2984.         push    esi
  2985. @@:
  2986.         lodsb
  2987.         test    al, al
  2988.         jz      @f
  2989.         cmp     al, '/'
  2990.         jnz     @b
  2991.         lea     ebp, [esi-1]
  2992.         jmp     @b
  2993. @@:
  2994.         pop     esi
  2995.         test    ebp, ebp
  2996.         jnz     .noroot
  2997.         mov     ebp, [ROOT_CLUSTER]
  2998.         cmp     [fat_type], 32
  2999.         jz      .pushnotroot
  3000.         push    fat16_root_extend_dir
  3001.         push    fat16_root_end_write
  3002.         push    fat16_root_next_write
  3003.         push    fat16_root_begin_write
  3004.         xor     ebp, ebp
  3005.         push    ebp
  3006.         push    ebp
  3007.         push    fat16_root_first
  3008.         push    fat16_root_next
  3009.         jmp     .common1
  3010. .noroot:
  3011. ; check existence
  3012.         mov     byte [ebp], 0
  3013.         call    hd_find_lfn
  3014.         mov     byte [ebp], '/'
  3015.         lea     esi, [ebp+1]
  3016.         jnc     @f
  3017.         mov     eax, ERROR_FILE_NOT_FOUND
  3018. .ret1:
  3019.         mov     [esp+28], eax
  3020.         popad
  3021.         xor     ebx, ebx
  3022.         ret
  3023. @@:
  3024.         test    byte [edi+11], 0x10     ; must be directory
  3025.         mov     eax, ERROR_ACCESS_DENIED
  3026.         jz      .ret1
  3027.         mov     ebp, [edi+20-2]
  3028.         mov     bp, [edi+26]            ; ebp=cluster
  3029.         mov     eax, ERROR_FAT_TABLE
  3030.         cmp     ebp, 2
  3031.         jb      .ret1
  3032. .pushnotroot:
  3033.         push    fat_notroot_extend_dir
  3034.         push    fat_notroot_end_write
  3035.         push    fat_notroot_next_write
  3036.         push    fat_notroot_begin_write
  3037.         push    0
  3038.         push    ebp
  3039.         push    fat_notroot_first
  3040.         push    fat_notroot_next
  3041. .common1:
  3042.         call    fat_find_lfn
  3043.         jc      .notfound
  3044. ; found; must not be directory
  3045.         test    byte [edi+11], 10h
  3046.         jz      @f
  3047.         add     esp, 32
  3048.         popad
  3049.         mov     eax, ERROR_ACCESS_DENIED
  3050.         xor     ebx, ebx
  3051.         ret
  3052. @@:
  3053. ; delete FAT chain
  3054.         push    edi
  3055.         xor     eax, eax
  3056.         mov     dword [edi+28], eax     ; zero size
  3057.         xor     ecx, ecx
  3058.         mov     eax, [edi+20-2]
  3059.         mov     ax, [edi+26]
  3060.         mov     word [edi+20], cx
  3061.         mov     word [edi+26], cx
  3062.         test    eax, eax
  3063.         jz      .done1
  3064. @@:
  3065.         cmp     eax, [fatRESERVED]
  3066.         jae     .done1
  3067.         push    edx
  3068.         xor     edx, edx
  3069.         call    set_FAT
  3070.         mov     eax, edx
  3071.         pop     edx
  3072.         inc     ecx
  3073.         jmp     @b
  3074. .done1:
  3075.         pop     edi
  3076.         call    get_time_for_file
  3077.         mov     [edi+22], ax
  3078.         call    get_date_for_file
  3079.         mov     [edi+24], ax
  3080.         mov     [edi+18], ax
  3081.         or      byte [edi+11], 20h      ; set 'archive' attribute
  3082.         jmp     .doit
  3083. .notfound:
  3084. ; file is not found; generate short name
  3085.         call    fat_name_is_legal
  3086.         jc      @f
  3087.         add     esp, 32
  3088.         popad
  3089.         mov     eax, ERROR_FILE_NOT_FOUND
  3090.         xor     ebx, ebx
  3091.         ret
  3092. @@:
  3093.         sub     esp, 12
  3094.         mov     edi, esp
  3095.         call    fat_gen_short_name
  3096. .test_short_name_loop:
  3097.         push    esi edi ecx
  3098.         mov     esi, edi
  3099.         lea     eax, [esp+12+12+8]
  3100.         mov     [eax], ebp
  3101.         and     dword [eax+4], 0
  3102.         call    dword [eax-4]
  3103.         jc      .found
  3104. .test_short_name_entry:
  3105.         cmp     byte [edi+11], 0xF
  3106.         jz      .test_short_name_cont
  3107.         mov     ecx, 11
  3108.         push    esi edi
  3109.         repz    cmpsb
  3110.         pop     edi esi
  3111.         jz      .short_name_found
  3112. .test_short_name_cont:
  3113.         lea     eax, [esp+12+12+8]
  3114.         call    dword [eax-8]
  3115.         jnc     .test_short_name_entry
  3116.         jmp     .found
  3117. .short_name_found:
  3118.         pop     ecx edi esi
  3119.         call    fat_next_short_name
  3120.         jnc     .test_short_name_loop
  3121. .disk_full:
  3122.         add     esp, 12+32
  3123.         popa
  3124.         mov     eax, ERROR_DISK_FULL
  3125.         xor     ebx, ebx
  3126.         ret
  3127. .found:
  3128.         pop     ecx edi esi
  3129. ; now find space in directory
  3130. ; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
  3131.         mov     al, '~'
  3132.         push    ecx edi
  3133.         mov     ecx, 8
  3134.         repnz   scasb
  3135.         push    1
  3136.         pop     eax     ; 1 entry
  3137.         jnz     .notilde
  3138. ; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
  3139.         xor     eax, eax
  3140. @@:
  3141.         cmp     byte [esi], 0
  3142.         jz      @f
  3143.         inc     esi
  3144.         inc     eax
  3145.         jmp     @b
  3146. @@:
  3147.         sub     esi, eax
  3148.         add     eax, 12+13
  3149.         mov     ecx, 13
  3150.         push    edx
  3151.         cdq
  3152.         div     ecx
  3153.         pop     edx
  3154. .notilde:
  3155.         push    -1
  3156.         push    -1
  3157.         push    -1
  3158. ; find <eax> successive entries in directory
  3159.         xor     ecx, ecx
  3160.         push    eax
  3161.         lea     eax, [esp+16+8+12+8]
  3162.         mov     [eax], ebp
  3163.         and     dword [eax+4], 0
  3164.         call    dword [eax-4]
  3165.         pop     eax
  3166.         jnc     .scan_dir
  3167. .fsfrfe3:
  3168.         add     esp, 12+8+12+32
  3169.         popad
  3170.         mov     eax, 11
  3171.         xor     ebx, ebx
  3172.         ret
  3173. .scan_dir:
  3174.         cmp     byte [edi], 0
  3175.         jz      .free
  3176.         cmp     byte [edi], 0xE5
  3177.         jz      .free
  3178.         xor     ecx, ecx
  3179. .scan_cont:
  3180.         push    eax
  3181.         lea     eax, [esp+16+8+12+8]
  3182.         call    dword [eax-8]
  3183.         pop     eax
  3184.         jnc     .scan_dir
  3185.         cmp     [hd_error], 0
  3186.         jnz     .fsfrfe3
  3187.         push    eax
  3188.         lea     eax, [esp+16+8+12+8]
  3189.         call    dword [eax+20]          ; extend directory
  3190.         pop     eax
  3191.         jnc     .scan_dir
  3192.         add     esp, 12+8+12+32
  3193.         popad
  3194.         mov     eax, ERROR_DISK_FULL
  3195.         xor     ebx, ebx
  3196.         ret
  3197. .free:
  3198.         test    ecx, ecx
  3199.         jnz     @f
  3200.         mov     [esp], edi
  3201.         mov     ecx, [esp+12+8+12+8]
  3202.         mov     [esp+4], ecx
  3203.         mov     ecx, [esp+12+8+12+12]
  3204.         mov     [esp+8], ecx
  3205.         xor     ecx, ecx
  3206. @@:
  3207.         inc     ecx
  3208.         cmp     ecx, eax
  3209.         jb      .scan_cont
  3210. ; found!
  3211. ; calculate name checksum
  3212.         push    esi ecx
  3213.         mov     esi, [esp+8+12]
  3214.         mov     ecx, 11
  3215.         xor     eax, eax
  3216. @@:
  3217.         ror     al, 1
  3218.         add     al, [esi]
  3219.         inc     esi
  3220.         loop    @b
  3221.         pop     ecx esi
  3222.         pop     edi
  3223.         pop     dword [esp+8+12+12]
  3224.         pop     dword [esp+8+12+12]
  3225. ; edi points to first entry in free chunk
  3226.         dec     ecx
  3227.         jz      .nolfn
  3228.         push    esi
  3229.         push    eax
  3230.         lea     eax, [esp+8+8+12+8]
  3231.         call    dword [eax+8]         ; begin write
  3232.         mov     al, 40h
  3233. .writelfn:
  3234.         or      al, cl
  3235.         mov     esi, [esp+4]
  3236.         push    ecx
  3237.         dec     ecx
  3238.         imul    ecx, 13
  3239.         add     esi, ecx
  3240.         stosb
  3241.         mov     cl, 5
  3242.         call    fs_RamdiskRewrite.read_symbols
  3243.         mov     ax, 0xF
  3244.         stosw
  3245.         mov     al, [esp+4]
  3246.         stosb
  3247.         mov     cl, 6
  3248.         call    fs_RamdiskRewrite.read_symbols
  3249.         xor     eax, eax
  3250.         stosw
  3251.         mov     cl, 2
  3252.         call    fs_RamdiskRewrite.read_symbols
  3253.         pop     ecx
  3254.         lea     eax, [esp+8+8+12+8]
  3255.         call    dword [eax+12]         ; next write
  3256.         xor     eax, eax
  3257.         loop    .writelfn
  3258.         pop     eax
  3259.         pop     esi
  3260. ;        lea     eax, [esp+8+12+8]
  3261. ;        call    dword [eax+16]          ; end write
  3262. .nolfn:
  3263.         xchg    esi, [esp]
  3264.         mov     ecx, 11
  3265.         rep     movsb
  3266.         mov     word [edi], 20h         ; attributes
  3267.         sub     edi, 11
  3268.         pop     esi ecx
  3269.         add     esp, 12
  3270.         mov     byte [edi+13], 0        ; tenths of a second at file creation time
  3271.         call    get_time_for_file
  3272.         mov     [edi+14], ax            ; creation time
  3273.         mov     [edi+22], ax            ; last write time
  3274.         call    get_date_for_file
  3275.         mov     [edi+16], ax            ; creation date
  3276.         mov     [edi+24], ax            ; last write date
  3277.         mov     [edi+18], ax            ; last access date
  3278.         xor     ecx, ecx
  3279.         mov     word [edi+20], cx       ; high word of cluster
  3280.         mov     word [edi+26], cx       ; low word of cluster - to be filled
  3281.         mov     dword [edi+28], ecx     ; file size - to be filled
  3282. .doit:
  3283.         lea     eax, [esp+8]
  3284.         call    dword [eax+16]  ; flush directory
  3285.         push    ecx
  3286.         mov     ecx, [esp+4+32+24]
  3287.         push    ecx
  3288.         push    edi
  3289.         mov     esi, edx
  3290.         test    ecx, ecx
  3291.         jz      .done
  3292.         mov     eax, 2
  3293.         call    get_free_FAT
  3294.         jc      .diskfull
  3295.         push    eax
  3296.         mov     [edi+26], ax
  3297.         shr     eax, 16
  3298.         mov     [edi+20], ax
  3299.         lea     eax, [esp+16+8]
  3300.         call    dword [eax+16]  ; flush directory
  3301.         pop     eax
  3302.         push    edx
  3303.         mov     edx, [fatEND]
  3304.         call    set_FAT
  3305.         pop     edx
  3306. .write_cluster:
  3307.         push    eax
  3308.         dec     eax
  3309.         dec     eax
  3310.         mov     ebp, [SECTORS_PER_CLUSTER]
  3311.         imul    eax, ebp
  3312.         add     eax, [DATA_START]
  3313. ; write data
  3314. .write_sector:
  3315.         mov     ecx, 512
  3316.         cmp     dword [esp+8], ecx
  3317.         jb      .writeshort
  3318. ; we can write directly from given buffer
  3319.         mov     ebx, esi
  3320.         add     esi, ecx
  3321.         jmp     .writecommon
  3322. .writeshort:
  3323.         mov     ecx, [esp+8]
  3324.         push    ecx
  3325.         mov     edi, buffer
  3326.         mov     ebx, edi
  3327.         rep     movsb
  3328.         mov     ecx, buffer+0x200
  3329.         sub     ecx, edi
  3330.         push    eax
  3331.         xor     eax, eax
  3332.         rep     stosb
  3333.         pop     eax
  3334.         pop     ecx
  3335. .writecommon:
  3336.         call    hd_write
  3337.         cmp     [hd_error], 0
  3338.         jnz     .writeerr
  3339.         inc     eax
  3340.         sub     dword [esp+8], ecx
  3341.         jz      .writedone
  3342.         dec     ebp
  3343.         jnz     .write_sector
  3344. ; allocate new cluster
  3345.         pop     eax
  3346.         mov     ecx, eax
  3347.         call    get_free_FAT
  3348.         jc      .diskfull
  3349.         push    edx
  3350.         mov     edx, [fatEND]
  3351.         call    set_FAT
  3352.         xchg    eax, ecx
  3353.         mov     edx, ecx
  3354.         call    set_FAT
  3355.         pop     edx
  3356.         xchg    eax, ecx
  3357.         jmp     .write_cluster
  3358. .diskfull:
  3359.         mov     eax, ERROR_DISK_FULL
  3360.         jmp     .ret
  3361. .writeerr:
  3362.         pop     eax
  3363.         sub     esi, ecx
  3364.         mov     eax, 11
  3365.         jmp     .ret
  3366. .writedone:
  3367.         pop     eax
  3368. .done:
  3369.         xor     eax, eax
  3370. .ret:
  3371.         pop     edi ecx
  3372.         mov     ebx, esi
  3373.         sub     ebx, edx
  3374.         pop     ebp
  3375.         mov     [esp+32+28], eax
  3376.         lea     eax, [esp+8]
  3377.         call    dword [eax+8]
  3378.         mov     [edi+28], ebx
  3379.         call    dword [eax+16]
  3380.         mov     [esp+32+16], ebx
  3381.         lea     eax, [ebx+511]
  3382.         shr     eax, 9
  3383.         mov     ecx, [SECTORS_PER_CLUSTER]
  3384.         lea     eax, [eax+ecx-1]
  3385.         xor     edx, edx
  3386.         div     ecx
  3387.         mov     ecx, ebp
  3388.         sub     ecx, eax
  3389.         call    add_disk_free_space
  3390.         add     esp, 32
  3391.         call    update_disk
  3392.         popad
  3393.         ret
  3394.  
  3395. ;----------------------------------------------------------------
  3396. ;
  3397. ;  fs_HdWrite - LFN variant for writing to floppy
  3398. ;
  3399. ;  esi  points to filename
  3400. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  3401. ;       may be ebx=0 - start from first byte
  3402. ;  ecx  number of bytes to write, 0+
  3403. ;  edx  mem location to data
  3404. ;
  3405. ;  ret ebx = bytes written (maybe 0)
  3406. ;      eax = 0 ok write or other = errormsg
  3407. ;
  3408. ;--------------------------------------------------------------
  3409. fs_HdWrite.access_denied:
  3410.         push    ERROR_ACCESS_DENIED
  3411. fs_HdWrite.ret0:
  3412.         pop     eax
  3413.         xor     ebx, ebx
  3414.         ret
  3415.  
  3416. fs_HdWrite.ret11:
  3417.         push    11
  3418.         jmp     fs_HdWrite.ret0
  3419.  
  3420. fs_HdWrite:
  3421.         cmp     [fat_type], 0
  3422.         jnz     @f
  3423.         push    ERROR_UNKNOWN_FS
  3424.         jmp     .ret0
  3425. @@:
  3426.         cmp     byte [esi], 0
  3427.         jz      .access_denied
  3428.         pushad
  3429.         call    hd_find_lfn
  3430.         pushfd
  3431.         cmp     [hd_error], 0
  3432.         jz      @f
  3433.         popfd
  3434.         popad
  3435.         push    11
  3436.         jmp     .ret0
  3437. @@:
  3438.         popfd
  3439.         jnc     .found
  3440.         popad
  3441.         push    ERROR_FILE_NOT_FOUND
  3442.         jmp     .ret0
  3443. .found:
  3444. ; FAT does not support files larger than 4GB
  3445.         test    ebx, ebx
  3446.         jz      .l1
  3447.         cmp     dword [ebx+4], 0
  3448.         jz      @f
  3449. .eof:
  3450.         popad
  3451.         push    ERROR_END_OF_FILE
  3452.         jmp     .ret0
  3453. @@:
  3454.         mov     ebx, [ebx]
  3455. .l1:
  3456. ; now edi points to direntry, ebx=start byte to write,
  3457. ; ecx=number of bytes to write, edx=data pointer
  3458.  
  3459. ; extend file if needed
  3460.         add     ecx, ebx
  3461.         jc      .eof    ; FAT does not support files larger than 4GB
  3462.         push    eax     ; save directory sector
  3463.         push    0       ; return value=0
  3464.  
  3465.         call    get_time_for_file
  3466.         mov     [edi+22], ax            ; last write time
  3467.         call    get_date_for_file
  3468.         mov     [edi+24], ax            ; last write date
  3469.         mov     [edi+18], ax            ; last access date
  3470.  
  3471.         push    dword [edi+28]          ; save current file size
  3472.         cmp     ecx, [edi+28]
  3473.         jbe     .length_ok
  3474.         cmp     ecx, ebx
  3475.         jz      .length_ok
  3476.         call    hd_extend_file
  3477.         jnc     .length_ok
  3478.         mov     [esp+4], eax
  3479. ; hd_extend_file can return three error codes: FAT table error, device error or disk full.
  3480. ; First two cases are fatal errors, in third case we may write some data
  3481.         cmp     al, ERROR_DISK_FULL
  3482.         jz      .disk_full
  3483.         pop     eax
  3484.         pop     eax
  3485.         mov     [esp+4+28], eax
  3486.         pop     eax
  3487.         popad
  3488.         xor     ebx, ebx
  3489.         ret
  3490. .disk_full:
  3491. ; correct number of bytes to write
  3492.         mov     ecx, [edi+28]
  3493.         cmp     ecx, ebx
  3494.         ja      .length_ok
  3495. .ret:
  3496.         call    update_disk
  3497.         cmp     [hd_error], 0
  3498.         jz      @f
  3499.         mov     byte [esp+4], 11
  3500. @@:
  3501.         pop     eax
  3502.         pop     eax
  3503.         mov     [esp+4+28], eax ; eax=return value
  3504.         pop     eax
  3505.         sub     edx, [esp+20]
  3506.         mov     [esp+16], edx   ; ebx=number of written bytes
  3507.         popad
  3508.         ret
  3509. .length_ok:
  3510.         mov     esi, [edi+28]
  3511.         mov     eax, [edi+20-2]
  3512.         mov     ax, [edi+26]
  3513.         mov     edi, eax        ; edi=current cluster
  3514.         xor     ebp, ebp        ; ebp=current sector in cluster
  3515. ; save directory
  3516.         mov     eax, [esp+8]
  3517.         push    ebx
  3518.         mov     ebx, buffer
  3519.         call    hd_write
  3520.         pop     ebx
  3521.         cmp     [hd_error], 0
  3522.         jz      @f
  3523. .device_err:
  3524.         mov     byte [esp+4], 11
  3525.         jmp     .ret
  3526. @@:
  3527.  
  3528. ; now ebx=start pos, ecx=end pos, both lie inside file
  3529.         sub     ecx, ebx
  3530.         jz      .ret
  3531. .write_loop:
  3532. ; get length of data in current sector
  3533.         push    ecx
  3534.         sub     ebx, 0x200
  3535.         jb      .hasdata
  3536.         neg     ebx
  3537.         xor     ecx, ecx
  3538.         jmp     @f
  3539. .hasdata:
  3540.         neg     ebx
  3541.         cmp     ecx, ebx
  3542.         jbe     @f
  3543.         mov     ecx, ebx
  3544. @@:
  3545. ; get current sector number
  3546.         mov     eax, edi
  3547.         dec     eax
  3548.         dec     eax
  3549.         imul    eax, [SECTORS_PER_CLUSTER]
  3550.         add     eax, [DATA_START]
  3551.         add     eax, ebp
  3552. ; load sector if needed
  3553.         cmp     dword [esp+4], 0        ; we don't need to read uninitialized data
  3554.         jz      .noread
  3555.         cmp     ecx, 0x200      ; we don't need to read sector if it is fully rewritten
  3556.         jz      .noread
  3557.         cmp     ecx, esi        ; (same for the last sector)
  3558.         jz      .noread
  3559.         push    ebx
  3560.         mov     ebx, buffer
  3561.         call    hd_read
  3562.         pop     ebx
  3563.         cmp     [hd_error], 0
  3564.         jz      @f
  3565. .device_err2:
  3566.         pop     ecx
  3567.         jmp     .device_err
  3568. @@:
  3569. .noread:
  3570. ; zero uninitialized data if file was extended (because hd_extend_file does not this)
  3571.         push    eax ecx edi
  3572.         xor     eax, eax
  3573.         mov     ecx, 0x200
  3574.         sub     ecx, [esp+4+12]
  3575.         jbe     @f
  3576.         mov     edi, buffer
  3577.         add     edi, [esp+4+12]
  3578.         rep     stosb
  3579. @@:
  3580. ; zero uninitialized data in the last sector
  3581.         mov     ecx, 0x200
  3582.         sub     ecx, esi
  3583.         jbe     @f
  3584.         mov     edi, buffer
  3585.         add     edi, esi
  3586.         rep     stosb
  3587. @@:
  3588.         pop     edi ecx eax
  3589. ; copy new data
  3590.         push    eax
  3591.         mov     eax, edx
  3592.         neg     ebx
  3593.         jecxz   @f
  3594.         add     ebx, buffer+0x200
  3595.         call    memmove
  3596.         xor     ebx, ebx
  3597. @@:
  3598.         pop     eax
  3599. ; save sector
  3600.         push    ebx
  3601.         mov     ebx, buffer
  3602.         call    hd_write
  3603.         pop     ebx
  3604.         cmp     [hd_error], 0
  3605.         jnz     .device_err2
  3606.         add     edx, ecx
  3607.         sub     [esp], ecx
  3608.         pop     ecx
  3609.         jz      .ret
  3610. ; next sector
  3611.         inc     ebp
  3612.         cmp     ebp, [SECTORS_PER_CLUSTER]
  3613.         jb      @f
  3614.         xor     ebp, ebp
  3615.         mov     eax, edi
  3616.         call    get_FAT
  3617.         mov     edi, eax
  3618.         cmp     [hd_error], 0
  3619.         jnz     .device_err
  3620. @@:
  3621.         sub     esi, 0x200
  3622.         jae     @f
  3623.         xor     esi, esi
  3624. @@:
  3625.         sub     dword [esp], 0x200
  3626.         jae     @f
  3627.         and     dword [esp], 0
  3628. @@:     jmp     .write_loop
  3629.  
  3630. hd_extend_file.zero_size:
  3631.         xor     eax, eax
  3632.         jmp     hd_extend_file.start_extend
  3633.  
  3634. ; extends file on hd to given size (new data area is undefined)
  3635. ; in: edi->direntry, ecx=new size
  3636. ; out: CF=0 => OK, eax=0
  3637. ;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL or 11)
  3638. hd_extend_file:
  3639.         push    ebp
  3640.         mov     ebp, [SECTORS_PER_CLUSTER]
  3641.         imul    ebp, [BYTES_PER_SECTOR]
  3642.         push    ecx
  3643. ; find the last cluster of file
  3644.         mov     eax, [edi+20-2]
  3645.         mov     ax, [edi+26]
  3646.         mov     ecx, [edi+28]
  3647.         jecxz   .zero_size
  3648. .last_loop:
  3649.         sub     ecx, ebp
  3650.         jbe     .last_found
  3651.         call    get_FAT
  3652.         cmp     [hd_error], 0
  3653.         jz      @f
  3654. .device_err:
  3655.         pop     ecx
  3656. .device_err2:
  3657.         pop     ebp
  3658.         push    11
  3659. .ret_err:
  3660.         pop     eax
  3661.         stc
  3662.         ret
  3663. @@:
  3664.         cmp     eax, 2
  3665.         jb      .fat_err
  3666.         cmp     eax, [fatRESERVED]
  3667.         jb      .last_loop
  3668. .fat_err:
  3669.         pop     ecx ebp
  3670.         push    ERROR_FAT_TABLE
  3671.         jmp     .ret_err
  3672. .last_found:
  3673.         push    eax
  3674.         call    get_FAT
  3675.         cmp     [hd_error], 0
  3676.         jz      @f
  3677.         pop     eax
  3678.         jmp     .device_err
  3679. @@:
  3680.         cmp     eax, [fatRESERVED]
  3681.         pop     eax
  3682.         jb      .fat_err
  3683. ; set length to full number of clusters
  3684.         sub     [edi+28], ecx
  3685. .start_extend:
  3686.         pop     ecx
  3687. ; now do extend
  3688.         push    edx
  3689.         mov     edx, 2          ; start scan from cluster 2
  3690. .extend_loop:
  3691.         cmp     [edi+28], ecx
  3692.         jae     .extend_done
  3693. ; add new cluster
  3694.         push    eax
  3695.         mov     eax, edx
  3696.         call    get_free_FAT
  3697.         jc      .disk_full
  3698.         mov     edx, [fatEND]
  3699.         call    set_FAT
  3700.         mov     edx, eax
  3701.         pop     eax
  3702.         test    eax, eax
  3703.         jz      .first_cluster
  3704.         push    edx
  3705.         call    set_FAT
  3706.         pop     edx
  3707.         jmp     @f
  3708. .first_cluster:
  3709.         ror     edx, 16
  3710.         mov     [edi+20], dx
  3711.         ror     edx, 16
  3712.         mov     [edi+26], dx
  3713. @@:
  3714.         push    ecx
  3715.         mov     ecx, -1
  3716.         call    add_disk_free_space
  3717.         pop     ecx
  3718.         mov     eax, edx
  3719.         cmp     [hd_error], 0
  3720.         jnz     .device_err3
  3721.         add     [edi+28], ebp
  3722.         jmp     .extend_loop
  3723. .extend_done:
  3724.         mov     [edi+28], ecx
  3725.         pop     edx ebp
  3726.         xor     eax, eax        ; CF=0
  3727.         ret
  3728. .device_err3:
  3729.         pop     edx
  3730.         jmp     .device_err2
  3731. .disk_full:
  3732.         pop     eax edx ebp
  3733.         push    ERROR_DISK_FULL
  3734.         pop     eax
  3735.         cmp     [hd_error], 0
  3736.         jz      @f
  3737.         mov     al, 11
  3738. @@:     stc
  3739.         ret
  3740.  
  3741. ;----------------------------------------------------------------
  3742. ;
  3743. ;  fs_HdSetFileEnd - set end of file on hard disk
  3744. ;
  3745. ;  esi  points to filename
  3746. ;  ebx  points to 64-bit number = new file size
  3747. ;  ecx  ignored (reserved)
  3748. ;  edx  ignored (reserved)
  3749. ;
  3750. ;  ret eax = 0 ok or other = errormsg
  3751. ;
  3752. ;--------------------------------------------------------------
  3753. fs_HdSetFileEnd:
  3754.         cmp     [fat_type], 0
  3755.         jnz     @f
  3756.         push    ERROR_UNKNOWN_FS
  3757. .ret:
  3758.         pop     eax
  3759.         ret
  3760. @@:
  3761.         cmp     byte [esi], 0
  3762.         jnz     @f
  3763. .access_denied:
  3764.         push    ERROR_ACCESS_DENIED
  3765.         jmp     .ret
  3766. @@:
  3767.         push    edi
  3768.         call    hd_find_lfn
  3769.         pushfd
  3770.         cmp     [hd_error], 0
  3771.         jz      @f
  3772.         popfd
  3773.         push    11
  3774.         jmp     .ret
  3775. @@:
  3776.         popfd
  3777.         jnc     @f
  3778.         pop     edi
  3779.         push    ERROR_FILE_NOT_FOUND
  3780.         jmp     .ret
  3781. @@:
  3782. ; must not be directory
  3783.         test    byte [edi+11], 10h
  3784.         jz      @f
  3785.         pop     edi
  3786.         jmp     .access_denied
  3787. @@:
  3788. ; file size must not exceed 4 Gb
  3789.         cmp     dword [ebx+4], 0
  3790.         jz      @f
  3791.         pop     edi
  3792.         push    ERROR_END_OF_FILE
  3793.         jmp     .ret
  3794. @@:
  3795.         push    eax     ; save directory sector
  3796. ; set file modification date/time to current
  3797.         call    fat_update_datetime
  3798.         mov     eax, [ebx]
  3799.         cmp     eax, [edi+28]
  3800.         jb      .truncate
  3801.         ja      .expand
  3802.         pop     eax
  3803.         mov     ebx, buffer
  3804.         call    hd_write
  3805.         pop     edi
  3806.         xor     eax, eax
  3807.         cmp     [hd_error], 0
  3808.         jz      @f
  3809.         mov     al, 11
  3810. @@:
  3811.         ret
  3812. .expand:
  3813.         push    ebx ebp ecx
  3814.         push    dword [edi+28]  ; save old size
  3815.         mov     ecx, eax
  3816.         call    hd_extend_file
  3817.         push    eax             ; return code
  3818.         jnc     .expand_ok
  3819.         cmp     al, ERROR_DISK_FULL
  3820.         jz      .disk_full
  3821. .pop_ret:
  3822.         call    update_disk
  3823.         pop     eax ecx ebp ebx ecx edi edi
  3824.         ret
  3825. .expand_ok:
  3826. .disk_full:
  3827. ; save directory
  3828.         mov     eax, [edi+28]
  3829.         xchg    eax, [esp+20]
  3830.         mov     ebx, buffer
  3831.         call    hd_write
  3832.         mov     eax, [edi+20-2]
  3833.         mov     ax, [edi+26]
  3834.         mov     edi, eax
  3835.         cmp     [hd_error], 0
  3836.         jz      @f
  3837. .pop_ret11:
  3838.         mov     byte [esp], 11
  3839.         jmp     .pop_ret
  3840. @@:
  3841. ; now zero new data
  3842.         xor     ebp, ebp
  3843. ; edi=current cluster, ebp=sector in cluster
  3844. ; [esp+20]=new size, [esp+4]=old size, [esp]=return code
  3845. .zero_loop:
  3846.         sub     dword [esp+4], 0x200
  3847.         jae     .next_cluster
  3848.         lea     eax, [edi-2]
  3849.         imul    eax, [SECTORS_PER_CLUSTER]
  3850.         add     eax, [DATA_START]
  3851.         add     eax, ebp
  3852.         cmp     dword [esp+4], -0x200
  3853.         jz      .noread
  3854.         mov     ebx, buffer
  3855.         call    hd_read
  3856.         cmp     [hd_error], 0
  3857.         jnz     .err_next
  3858. .noread:
  3859.         mov     ecx, [esp+4]
  3860.         neg     ecx
  3861.         push    edi
  3862.         mov     edi, buffer+0x200
  3863.         add     edi, [esp+8]
  3864.         push    eax
  3865.         xor     eax, eax
  3866.         mov     [esp+12], eax
  3867.         rep     stosb
  3868.         pop     eax
  3869.         pop     edi
  3870.         call    hd_write
  3871.         cmp     [hd_error], 0
  3872.         jz      .next_cluster
  3873. .err_next:
  3874.         mov     byte [esp], 11
  3875. .next_cluster:
  3876.         sub     dword [esp+20], 0x200
  3877.         jbe     .pop_ret
  3878.         inc     ebp
  3879.         cmp     ebp, [SECTORS_PER_CLUSTER]
  3880.         jb      .zero_loop
  3881.         xor     ebp, ebp
  3882.         mov     eax, edi
  3883.         call    get_FAT
  3884.         mov     edi, eax
  3885.         cmp     [hd_error], 0
  3886.         jnz     .pop_ret11
  3887.         jmp     .zero_loop
  3888. .truncate:
  3889.         mov     [edi+28], eax
  3890.         push    ecx
  3891.         mov     ecx, [edi+20-2]
  3892.         mov     cx, [edi+26]
  3893.         push    eax
  3894.         test    eax, eax
  3895.         jz      .zero_size
  3896. ; find new last cluster
  3897. @@:
  3898.         mov     eax, [SECTORS_PER_CLUSTER]
  3899.         shl     eax, 9
  3900.         sub     [esp], eax
  3901.         jbe     @f
  3902.         mov     eax, ecx
  3903.         call    get_FAT
  3904.         mov     ecx, eax
  3905.         cmp     [hd_error], 0
  3906.         jz      @b
  3907. .device_err3:
  3908.         pop     eax ecx eax edi
  3909.         push    11
  3910.         pop     eax
  3911.         ret
  3912. @@:
  3913. ; we will zero data at the end of last sector - remember it
  3914.         push    ecx
  3915. ; terminate FAT chain
  3916.         push    edx
  3917.         mov     eax, ecx
  3918.         mov     edx, [fatEND]
  3919.         call    set_FAT
  3920.         mov     eax, edx
  3921.         pop     edx
  3922.         cmp     [hd_error], 0
  3923.         jz      @f
  3924. .device_err4:
  3925.         pop     ecx
  3926.         jmp     .device_err3
  3927. .zero_size:
  3928.         and     word [edi+20], 0
  3929.         and     word [edi+26], 0
  3930.         push    0
  3931.         mov     eax, ecx
  3932. @@:
  3933. ; delete FAT chain
  3934.         call    clear_cluster_chain
  3935.         cmp     [hd_error], 0
  3936.         jnz     .device_err4
  3937. ; save directory
  3938.         mov     eax, [esp+12]
  3939.         push    ebx
  3940.         mov     ebx, buffer
  3941.         call    hd_write
  3942.         pop     ebx
  3943.         cmp     [hd_error], 0
  3944.         jnz     .device_err4
  3945. ; zero last sector, ignore errors
  3946.         pop     ecx
  3947.         pop     eax
  3948.         dec     ecx
  3949.         imul    ecx, [SECTORS_PER_CLUSTER]
  3950.         add     ecx, [DATA_START]
  3951.         push    eax
  3952.         sar     eax, 9
  3953.         add     ecx, eax
  3954.         pop     eax
  3955.         and     eax, 0x1FF
  3956.         jz      .truncate_done
  3957.         push    ebx eax
  3958.         mov     eax, ecx
  3959.         mov     ebx, buffer
  3960.         call    hd_read
  3961.         pop     eax
  3962.         lea     edi, [buffer+eax]
  3963.         push    ecx
  3964.         mov     ecx, 0x200
  3965.         sub     ecx, eax
  3966.         xor     eax, eax
  3967.         rep     stosb
  3968.         pop     eax
  3969.         call    hd_write
  3970.         pop     ebx
  3971. .truncate_done:
  3972.         pop     ecx eax edi
  3973.         call    update_disk
  3974.         xor     eax, eax
  3975.         cmp     [hd_error], 0
  3976.         jz      @f
  3977.         mov     al, 11
  3978. @@:
  3979.         ret
  3980.  
  3981. fs_HdGetFileInfo:
  3982.         cmp     [fat_type], 0
  3983.         jnz     @f
  3984.         mov     eax, ERROR_UNKNOWN_FS
  3985.         ret
  3986. @@:
  3987.         cmp     byte [esi], 0
  3988.         jnz     @f
  3989.         mov     eax, 2
  3990.         ret
  3991. @@:
  3992.         push    edi
  3993.         call    hd_find_lfn
  3994.         pushfd
  3995.         cmp     [hd_error], 0
  3996.         jz      @f
  3997.         popfd
  3998.         pop     edi
  3999.         mov     eax, 11
  4000.         ret
  4001. @@:
  4002.         popfd
  4003.         jmp     fs_GetFileInfo_finish
  4004.  
  4005. fs_HdSetFileInfo:
  4006.         cmp     [fat_type], 0
  4007.         jnz     @f
  4008.         mov     eax, ERROR_UNKNOWN_FS
  4009.         ret
  4010. @@:
  4011.         cmp     byte [esi], 0
  4012.         jnz     @f
  4013.         mov     eax, 2
  4014.         ret
  4015. @@:
  4016.         push    edi
  4017.         call    hd_find_lfn
  4018.         pushfd
  4019.         cmp     [hd_error], 0
  4020.         jz      @f
  4021.         popfd
  4022.         pop     edi
  4023.         mov     eax, 11
  4024.         ret
  4025. @@:
  4026.         popfd
  4027.         jnc     @f
  4028.         pop     edi
  4029.         mov     eax, ERROR_FILE_NOT_FOUND
  4030.         ret
  4031. @@:
  4032.         push    eax
  4033.         call    bdfe_to_fat_entry
  4034.         pop     eax
  4035.         mov     ebx, buffer
  4036.         call    hd_write
  4037.         call    update_disk
  4038.         pop     edi
  4039.         xor     eax, eax
  4040.         ret
  4041.  
  4042. ;----------------------------------------------------------------
  4043. ;
  4044. ;  fs_HdExecute - LFN variant for executing from harddisk
  4045. ;
  4046. ;  esi  points to hd filename (e.g. 'dir1/name')
  4047. ;  ebp  points to full filename (e.g. '/hd0/1/dir1/name')
  4048. ;  dword [ebx] = flags
  4049. ;  dword [ebx+4] = cmdline
  4050. ;
  4051. ;  ret ebx,edx destroyed
  4052. ;      eax > 0 - PID, < 0 - error
  4053. ;
  4054. ;--------------------------------------------------------------
  4055. fs_HdExecute:
  4056.         mov     edx, [ebx]
  4057.         mov     ebx, [ebx+4]
  4058.         test    ebx, ebx
  4059.         jz      @f
  4060.         add     ebx, std_application_base_address
  4061. @@:
  4062.  
  4063. ;----------------------------------------------------------------
  4064. ;
  4065. ; fs_HdExecute.flags - second entry
  4066. ;
  4067. ;  esi  points to floppy filename (kernel address)
  4068. ;  ebp  points to full filename
  4069. ;  edx  flags
  4070. ;  ebx  cmdline (kernel address)
  4071. ;
  4072. ;  ret  eax > 0 - PID, < 0 - error
  4073. ;
  4074. ;--------------------------------------------------------------
  4075.  
  4076. .flags:
  4077.         cmp     [fat_type], 0
  4078.         jnz     @f
  4079.         mov     eax, ERROR_UNKNOWN_FS
  4080.         ret
  4081. @@:
  4082.         cmp     byte [esi], 0
  4083.         jnz     @f
  4084. ; cannot execute root!
  4085.         mov     eax, -ERROR_ACCESS_DENIED
  4086.         ret
  4087. @@:
  4088.         push    edi
  4089.         call    hd_find_lfn
  4090.         jnc     .found
  4091.         pop     edi
  4092.         mov     eax, -ERROR_FILE_NOT_FOUND
  4093.         cmp     [hd_error], 0
  4094.         jz      @f
  4095.         mov     al, -11
  4096. @@:
  4097.         ret
  4098. .found:
  4099.         mov     eax, [edi+20-2]
  4100.         mov     ax, [edi+26]
  4101.         push    0
  4102.         push    eax
  4103.         push    dword [edi+28]          ; size
  4104.         push    .DoRead
  4105.         call    fs_execute
  4106.         add     esp, 16
  4107.         pop     edi
  4108.         ret
  4109.  
  4110. .DoRead:
  4111. ; read next block
  4112. ; in: eax->parameters, edi->buffer
  4113. ; out: eax = error code
  4114.         pushad
  4115.         cmp     dword [eax], 0  ; file size
  4116.         jz      .eof
  4117.         add     eax, 4
  4118.         call    fat_get_sector
  4119.         mov     ebx, edi
  4120.         call    hd_read
  4121.         cmp     [hd_error], 0
  4122.         jnz     .err
  4123.         mov     eax, [esp+28]
  4124.         mov     ecx, [eax]
  4125.         sub     ecx, 512
  4126.         jae     @f
  4127.         lea     edi, [edi+ecx+512]
  4128.         neg     ecx
  4129.         push    eax
  4130.         xor     eax, eax
  4131.         rep     stosb
  4132.         pop     eax
  4133. @@:
  4134.         mov     [eax], ecx
  4135.         mov     edx, [eax+8]
  4136.         inc     edx
  4137.         cmp     edx, [SECTORS_PER_CLUSTER]
  4138.         jb      @f
  4139.         push    eax
  4140.         mov     eax, [eax+4]
  4141.         call    get_FAT
  4142.         cmp     [hd_error], 0
  4143.         jnz     .err
  4144.         mov     ecx, eax
  4145.         pop     eax
  4146.         mov     [eax+4], ecx
  4147.         xor     edx, edx
  4148. @@:
  4149.         mov     [eax+8], edx
  4150.         popad
  4151.         xor     eax, eax
  4152.         ret
  4153. .eof:
  4154.         popad
  4155.         mov     eax, 6
  4156.         ret
  4157. .err:
  4158.         popad
  4159.         mov     eax, 11
  4160.         ret
  4161.  
  4162. ; \end{diamond}
  4163.