Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;; RAMDISK functions                                            ;;
  7. ;; (C) 2004 Ville Turjanmaa, License: GPL                       ;;
  8. ;; Addings by M.Lisovin                                         ;;
  9. ;; LFN support by diamond                                       ;;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11.  
  12. $Revision: 846 $
  13.  
  14.  
  15. ; calculate fat chain
  16.  
  17. calculatefatchain:
  18.  
  19.    pushad
  20.  
  21.    mov esi, [_rd_fat]
  22.    mov  edi,RAMDISK_FAT
  23.  
  24.  fcnew:
  25.    mov  eax,dword [esi]
  26.    mov  ebx,dword [esi+4]
  27.    mov  ecx,dword [esi+8]
  28.    mov  edx,ecx
  29.    shr  edx,4   ;8 ok
  30.    shr  dx,4    ;7 ok
  31.    xor  ch,ch
  32.    shld ecx,ebx,20 ;6 ok
  33.    shr  cx,4     ;5 ok
  34.    shld ebx,eax,12
  35.    and  ebx,0x0fffffff  ;4 ok
  36.    shr  bx,4    ;3 ok
  37.    shl  eax,4
  38.    and  eax,0x0fffffff  ;2 ok
  39.    shr  ax,4  ;1 ok
  40.    mov  dword [edi],eax
  41.    mov  dword [edi+4],ebx
  42.    mov  dword [edi+8],ecx
  43.    mov  dword [edi+12],edx
  44.    add  edi,16
  45.    add  esi,12
  46.  
  47.    cmp  edi,RAMDISK_FAT+2856*2   ;2849 clusters
  48.    jnz  fcnew
  49.  
  50.    popad
  51.    ret
  52.  
  53.  
  54. restorefatchain:   ; restore fat chain
  55.  
  56.    pushad
  57.  
  58.    mov  esi,RAMDISK_FAT
  59.    mov  edi, [_rd_fat]
  60.  
  61.   fcnew2:
  62.    mov  eax,dword [esi]
  63.    mov  ebx,dword [esi+4]
  64.    shl  ax,4
  65.    shl  eax,4
  66.    shl  bx,4
  67.    shr  ebx,4
  68.    shrd eax,ebx,8
  69.    shr  ebx,8
  70.    mov  dword [edi],eax
  71.    mov  word [edi+4],bx
  72.    add  edi,6
  73.    add  esi,8
  74.  
  75.    cmp  edi, [_rd_fat_end]        ;4274 bytes - all used FAT
  76.    jb   fcnew2
  77.  
  78.    mov  esi,[_rd_fat]             ; duplicate fat chain
  79.    lea  edi,[esi+0x1200]
  80.    mov  ecx,1069        ;4274/4
  81.    cld
  82.    rep  movsd
  83.  
  84.    popad
  85.    ret
  86.  
  87.  
  88. ramdisk_free_space:
  89. ;---------------------------------------------
  90. ;
  91. ; returns free space in edi
  92. ; rewr.by Mihasik
  93. ;---------------------------------------------
  94.  
  95.         push   eax ebx ecx
  96.  
  97.         mov  edi,RAMDISK_FAT ;start of FAT
  98.         xor  ax,ax    ;Free cluster=0x0000 in FAT
  99.         xor  ebx,ebx  ;counter
  100.         mov  ecx,2849 ;2849 clusters
  101.         cld
  102.     rdfs1:
  103.         repne scasw
  104.         jnz  rdfs2    ;if last cluster not 0
  105.         inc  ebx
  106.         test    ecx, ecx
  107.         jnz     rdfs1
  108.     rdfs2:
  109.         shl  ebx,9    ;free clusters*512
  110.         mov  edi,ebx
  111.  
  112.         pop    ecx ebx eax
  113.         ret
  114.  
  115.  
  116. expand_filename:
  117. ;---------------------------------------------
  118. ;
  119. ; exapand filename with '.' to 11 character
  120. ; eax - pointer to filename
  121. ;---------------------------------------------
  122.  
  123.         push esi edi ebx
  124.  
  125.         mov  edi,esp                  ; check for '.' in the name
  126.         add  edi,12+8
  127.  
  128.         mov  esi,eax
  129.  
  130.         mov  eax,edi
  131.         mov  [eax+0],dword '    '
  132.         mov  [eax+4],dword '    '
  133.         mov  [eax+8],dword '    '
  134.  
  135.       flr1:
  136.  
  137.         cmp  [esi],byte '.'
  138.         jne  flr2
  139.         mov  edi,eax
  140.         add  edi,7
  141.         jmp  flr3
  142.  
  143.       flr2:
  144.  
  145.         mov  bl,[esi]
  146.         mov  [edi],bl
  147.  
  148.       flr3:
  149.  
  150.         inc  esi
  151.         inc  edi
  152.  
  153.         mov  ebx,eax
  154.         add  ebx,11
  155.  
  156.         cmp  edi,ebx
  157.         jbe  flr1
  158.  
  159.         pop  ebx edi esi
  160.         ret
  161.  
  162. fileread:
  163. ;----------------------------------------------------------------
  164. ;
  165. ;  fileread - sys floppy
  166. ;
  167. ;  eax  points to filename 11 chars
  168. ;  ebx  first wanted block       ; 1+ ; if 0 then set to 1
  169. ;  ecx  number of blocks to read ; 1+ ; if 0 then set to 1
  170. ;  edx  mem location to return data
  171. ;  esi  length of filename 12*X 0=root
  172. ;
  173. ;  ret ebx = size or 0xffffffff file not found
  174. ;      eax = 0 ok read or other = errormsg
  175. ;
  176. ;--------------------------------------------------------------
  177.         test   ebx,ebx ;if ebx=0 - set to 1
  178.         jnz    frfl5
  179.         inc    ebx
  180.       frfl5:
  181.         test   ecx,ecx ;if ecx=0 - set to 1
  182.         jnz    frfl6
  183.         inc    ecx
  184.       frfl6:
  185.         test   esi,esi          ; return ramdisk root
  186.         jnz    fr_noroot        ;if not root
  187.         cmp    ebx,14           ;14 clusters=root dir
  188.         ja     oorr
  189.         cmp    ecx,14
  190.         ja     oorr
  191.         jmp    fr_do
  192.       oorr:
  193.         mov    eax,5            ;out of root range (fnf)
  194.         xor    ebx,ebx
  195.         dec    ebx              ;0xffffffff
  196.         ret
  197.  
  198.       fr_do:                    ;reading rootdir
  199.         mov    edi,edx
  200.         dec    ebx
  201.         push   edx
  202.         mov    edx,ecx
  203.         add    edx,ebx
  204.         cmp    edx,15     ;ebx+ecx=14+1
  205.         pushf
  206.         jbe    fr_do1
  207.         sub    edx,14
  208.         sub    ecx,edx
  209.       fr_do1:
  210.         shl    ebx,9
  211.         mov    esi, [_rd_root]
  212.         add    esi,ebx
  213.         shl    ecx,7
  214.         cld
  215.         rep    movsd
  216.         popf
  217.         pop    edx
  218.         jae    fr_do2
  219.         xor    eax,eax ; ok read
  220.         xor    ebx,ebx
  221.         ret
  222.       fr_do2:        ;if last cluster
  223.         mov    eax,6  ;end of file
  224.         xor    ebx,ebx
  225.         ret
  226.  
  227.      fr_noroot:
  228.  
  229.         sub    esp,32
  230.         call   expand_filename
  231.  
  232.         dec    ebx
  233.  
  234.         push   eax
  235.  
  236.         push   eax ebx ecx edx esi edi
  237.         call   rd_findfile
  238.         je     fifound
  239.         add    esp,32+28   ;if file not found
  240.         ret
  241.  
  242.      fifound:
  243.  
  244.         mov    ebx,[edi-11+28]          ;file size
  245.         mov    [esp+20],ebx
  246.         mov    [esp+24],ebx
  247.         add    edi,0xf
  248.         movzx  eax,word [edi]
  249.         mov    edi,eax                  ;edi=cluster
  250.  
  251.       frnew:
  252.  
  253.         add    eax,31                   ;bootsector+2*fat+filenames
  254.         shl    eax,9                    ;*512
  255.         add    eax, [_rd_base]          ;image base
  256.         mov    ebx,[esp+8]
  257.         mov    ecx,512                  ;[esp+4]
  258.  
  259.         cmp    [esp+16],dword 0         ; wanted cluster ?
  260.         jne    frfl7
  261.         call   memmove
  262.         add    [esp+8],dword 512
  263.         dec    dword [esp+12]           ; last wanted cluster ?
  264.         je     frnoread
  265.         jmp    frfl8
  266.       frfl7:
  267.         dec    dword [esp+16]
  268.       frfl8:
  269.         movzx  eax,word [edi*2+RAMDISK_FAT]        ; find next cluster from FAT
  270.         mov    edi,eax
  271.         cmp    edi,4095                 ;eof  - cluster
  272.         jz     frnoread2
  273.  
  274.         cmp    [esp+24],dword 512       ;eof  - size
  275.         jb     frnoread
  276.         sub    [esp+24],dword 512
  277.  
  278.         jmp    frnew
  279.  
  280.       frnoread2:
  281.  
  282.         cmp    [esp+16],dword 0         ; eof without read ?
  283.         je     frnoread
  284.  
  285.         pop    edi esi edx ecx
  286.         add    esp,4
  287.         pop    ebx     ; ebx <- eax : size of file
  288.         add    esp,36
  289.         mov    eax,6   ; end of file
  290.         ret
  291.  
  292.       frnoread:
  293.  
  294.         pop    edi esi edx ecx
  295.         add    esp,4
  296.         pop    ebx     ; ebx <- eax : size of file
  297.         add    esp,36
  298.         xor    eax,eax  ;read ok
  299.         ret
  300.  
  301.  
  302.  
  303.    rd_findfile:
  304.    ;by Mihasik
  305.    ;IN: eax - pointer to filename OUT: filestring+11 in edi or notZero in flags and fnf in eax,ebx
  306.  
  307.         mov    edi, [_rd_root]      ;Point at directory
  308.         cld
  309.     rd_newsearch:
  310.         mov    esi,eax
  311.         mov    ecx,11
  312.         rep    cmpsb
  313.         je     rd_ff
  314.         add    cl,21
  315.         add    edi,ecx
  316.         cmp    edi, [_rd_root_end]
  317.         jb     rd_newsearch
  318.         mov    eax,5      ;if file not found - eax=5
  319.         xor    ebx,ebx
  320.         dec    ebx    ;ebx=0xffffffff and zf=0
  321.      rd_ff:
  322.         ret
  323.  
  324. ; \begin{diamond}
  325.  
  326. uni2ansi_str:
  327. ; convert UNICODE zero-terminated string to ASCII-string (codepage 866)
  328. ; in: esi->source, edi->buffer (may be esi=edi)
  329. ; destroys: eax,esi,edi
  330.         lodsw
  331.         test    ax, ax
  332.         jz      .done
  333.         cmp     ax, 0x80
  334.         jb      .ascii
  335.         cmp     ax, 0x401
  336.         jz      .yo1
  337.         cmp     ax, 0x451
  338.         jz      .yo2
  339.         cmp     ax, 0x410
  340.         jb      .unk
  341.         cmp     ax, 0x440
  342.         jb      .rus1
  343.         cmp     ax, 0x450
  344.         jb      .rus2
  345. .unk:
  346.         mov     al, '_'
  347.         jmp     .doit
  348. .yo1:
  349.         mov     al, 'ð'
  350.         jmp     .doit
  351. .yo2:
  352.         mov     al, 'ñ'
  353.         jmp     .doit
  354. .rus1:
  355. ; 0x410-0x43F -> 0x80-0xAF
  356.         add     al, 0x70
  357.         jmp     .doit
  358. .rus2:
  359. ; 0x440-0x44F -> 0xE0-0xEF
  360.         add     al, 0xA0
  361. .ascii:
  362. .doit:
  363.         stosb
  364.         jmp     uni2ansi_str
  365. .done:
  366.         mov     byte [edi], 0
  367.         ret
  368.  
  369. ansi2uni_char:
  370. ; convert ANSI character in al to UNICODE character in ax, using cp866 encoding
  371.         mov     ah, 0
  372. ; 0x00-0x7F - trivial map
  373.         cmp     al, 0x80
  374.         jb      .ret
  375. ; 0x80-0xAF -> 0x410-0x43F
  376.         cmp     al, 0xB0
  377.         jae     @f
  378.         add     ax, 0x410-0x80
  379. .ret:
  380.         ret
  381. @@:
  382. ; 0xE0-0xEF -> 0x440-0x44F
  383.         cmp     al, 0xE0
  384.         jb      .unk
  385.         cmp     al, 0xF0
  386.         jae     @f
  387.         add     ax, 0x440-0xE0
  388.         ret
  389. ; 0xF0 -> 0x401
  390. ; 0xF1 -> 0x451
  391. @@:
  392.         cmp     al, 'ð'
  393.         jz      .yo1
  394.         cmp     al, 'ñ'
  395.         jz      .yo2
  396. .unk:
  397.         mov     al, '_'         ; ah=0
  398.         ret
  399. .yo1:
  400.         mov     ax, 0x401
  401.         ret
  402. .yo2:
  403.         mov     ax, 0x451
  404.         ret
  405.  
  406. char_toupper:
  407. ; convert character to uppercase, using cp866 encoding
  408. ; in: al=symbol
  409. ; out: al=converted symbol
  410.         cmp     al, 'a'
  411.         jb      .ret
  412.         cmp     al, 'z'
  413.         jbe     .az
  414.         cmp     al, ' '
  415.         jb      .ret
  416.         cmp     al, 'à'
  417.         jb      .rus1
  418.         cmp     al, 'ï'
  419.         ja      .ret
  420. ; 0xE0-0xEF -> 0x90-0x9F
  421.         sub     al, 'à'-''
  422. .ret:
  423.         ret
  424. .rus1:
  425. ; 0xA0-0xAF -> 0x80-0x8F
  426. .az:
  427.         and     al, not 0x20
  428.         ret
  429.  
  430. fat_get_name:
  431. ; in: edi->FAT entry
  432. ; out: CF=1 - no valid entry
  433. ; else CF=0 and ebp->ASCIIZ-name
  434. ; (maximum length of filename is 255 (wide) symbols without trailing 0,
  435. ;  but implementation requires buffer 261 words)
  436. ; destroys eax
  437.         cmp     byte [edi], 0
  438.         jz      .no
  439.         cmp     byte [edi], 0xE5
  440.         jnz     @f
  441. .no:
  442.         stc
  443.         ret
  444. @@:
  445.         cmp     byte [edi+11], 0xF
  446.         jz      .longname
  447.         test    byte [edi+11], 8
  448.         jnz     .no
  449.         push    ecx
  450.         push    edi ebp
  451.         test    byte [ebp-4], 1
  452.         jnz     .unicode_short
  453.  
  454.         mov     eax, [edi]
  455.         mov     ecx, [edi+4]
  456.         mov     [ebp], eax
  457.         mov     [ebp+4], ecx
  458.  
  459.         mov     ecx, 8
  460. @@:
  461.         cmp     byte [ebp+ecx-1], ' '
  462.         loope    @b
  463.  
  464.         mov     eax, [edi+8]
  465.         cmp     al, ' '
  466.         je      .done
  467.         shl     eax, 8
  468.         mov     al, '.'
  469.  
  470.         lea ebp, [ebp+ecx+1]
  471.         mov     [ebp], eax
  472.         mov     ecx, 3
  473. @@:
  474.         rol eax, 8
  475.         cmp al, ' '
  476.         jne .done
  477.         loop   @b
  478.         dec ebp
  479. .done:
  480.         and     byte [ebp+ecx+1], 0   ; CF=0
  481.         pop     ebp edi ecx
  482.         ret
  483. .unicode_short:
  484.         mov     ecx, 8
  485.         push    ecx
  486. @@:
  487.         mov     al, [edi]
  488.         inc     edi
  489.         call    ansi2uni_char
  490.         mov     [ebp], ax
  491.         inc     ebp
  492.         inc     ebp
  493.         loop    @b
  494.         pop     ecx
  495. @@:
  496.         cmp     word [ebp-2], ' '
  497.         jnz     @f
  498.         dec     ebp
  499.         dec     ebp
  500.         loop    @b
  501. @@:
  502.         mov     word [ebp], '.'
  503.         inc     ebp
  504.         inc     ebp
  505.         mov     ecx, 3
  506.         push    ecx
  507. @@:
  508.         mov     al, [edi]
  509.         inc     edi
  510.         call    ansi2uni_char
  511.         mov     [ebp], ax
  512.         inc     ebp
  513.         inc     ebp
  514.         loop    @b
  515.         pop     ecx
  516. @@:
  517.         cmp     word [ebp-2], ' '
  518.         jnz     @f
  519.         dec     ebp
  520.         dec     ebp
  521.         loop    @b
  522.         dec     ebp
  523.         dec     ebp
  524. @@:
  525.         and     word [ebp], 0   ; CF=0
  526.         pop     ebp edi ecx
  527.         ret
  528. .longname:
  529. ; LFN
  530.         mov     al, byte [edi]
  531.         and     eax, 0x3F
  532.         dec     eax
  533.         cmp     al, 20
  534.         jae     .no     ; ignore invalid entries
  535.         mov     word [ebp+260*2], 0     ; force null-terminating for orphans
  536.         imul    eax, 13*2
  537.         add     ebp, eax
  538.         test    byte [edi], 0x40
  539.         jz      @f
  540.         mov     word [ebp+13*2], 0
  541. @@:
  542.         push    eax
  543. ; now copy name from edi to ebp ...
  544.         mov     eax, [edi+1]
  545.         mov     [ebp], eax      ; symbols 1,2
  546.         mov     eax, [edi+5]
  547.         mov     [ebp+4], eax    ; 3,4
  548.         mov     eax, [edi+9]
  549.         mov     [ebp+8], ax     ; 5
  550.         mov     eax, [edi+14]
  551.         mov     [ebp+10], eax   ; 6,7
  552.         mov     eax, [edi+18]
  553.         mov     [ebp+14], eax   ; 8,9
  554.         mov     eax, [edi+22]
  555.         mov     [ebp+18], eax   ; 10,11
  556.         mov     eax, [edi+28]
  557.         mov     [ebp+22], eax   ; 12,13
  558. ; ... done
  559.         pop     eax
  560.         sub     ebp, eax
  561.         test    eax, eax
  562.         jz      @f
  563. ; if this is not first entry, more processing required
  564.         stc
  565.         ret
  566. @@:
  567. ; if this is first entry:
  568.         test    byte [ebp-4], 1
  569.         jnz     .ret
  570. ; buffer at ebp contains UNICODE name, convert it to ANSI
  571.         push    esi edi
  572.         mov     esi, ebp
  573.         mov     edi, ebp
  574.         call    uni2ansi_str
  575.         pop     edi esi
  576. .ret:
  577.         clc
  578.         ret
  579.  
  580. fat_compare_name:
  581. ; compares ASCIIZ-names, case-insensitive (cp866 encoding)
  582. ; in: esi->name, ebp->name
  583. ; out: if names match: ZF=1 and esi->next component of name
  584. ;      else: ZF=0, esi is not changed
  585. ; destroys eax
  586.         push    ebp esi
  587. .loop:
  588.         mov     al, [ebp]
  589.         inc     ebp
  590.         call    char_toupper
  591.         push    eax
  592.         lodsb
  593.         call    char_toupper
  594.         cmp     al, [esp]
  595.         jnz     .done
  596.         pop     eax
  597.         test    al, al
  598.         jnz     .loop
  599.         dec     esi
  600.         pop     eax
  601.         pop     ebp
  602.         xor     eax, eax        ; set ZF flag
  603.         ret
  604. .done:
  605.         cmp     al, '/'
  606.         jnz     @f
  607.         cmp     byte [esp], 0
  608.         jnz     @f
  609.         mov     [esp+4], esi
  610. @@:
  611.         pop     eax
  612.         pop     esi ebp
  613.         ret
  614.  
  615. fat_time_to_bdfe:
  616. ; in: eax=FAT time
  617. ; out: eax=BDFE time
  618.         push    ecx edx
  619.         mov     ecx, eax
  620.         mov     edx, eax
  621.         shr     eax, 11
  622.         shl     eax, 16 ; hours
  623.         and     edx, 0x1F
  624.         add     edx, edx
  625.         mov     al, dl  ; seconds
  626.         shr     ecx, 5
  627.         and     ecx, 0x3F
  628.         mov     ah, cl  ; minutes
  629.         pop     edx ecx
  630.         ret
  631.  
  632. fat_date_to_bdfe:
  633.         push    ecx edx
  634.         mov     ecx, eax
  635.         mov     edx, eax
  636.         shr     eax, 9
  637.         add     ax, 1980
  638.         shl     eax, 16 ; year
  639.         and     edx, 0x1F
  640.         mov     al, dl  ; day
  641.         shr     ecx, 5
  642.         and     ecx, 0xF
  643.         mov     ah, cl  ; month
  644.         pop     edx ecx
  645.         ret
  646.  
  647. bdfe_to_fat_time:
  648.         push    edx
  649.         mov     edx, eax
  650.         shr     eax, 16
  651.         and     dh, 0x3F
  652.         shl     eax, 6
  653.         or      al, dh
  654.         shr     dl, 1
  655.         and     dl, 0x1F
  656.         shl     eax, 5
  657.         or      al, dl
  658.         pop     edx
  659.         ret
  660.  
  661. bdfe_to_fat_date:
  662.         push    edx
  663.         mov     edx, eax
  664.         shr     eax, 16
  665.         sub     ax, 1980
  666.         and     dh, 0xF
  667.         shl     eax, 4
  668.         or      al, dh
  669.         and     dl, 0x1F
  670.         shl     eax, 5
  671.         or      al, dl
  672.         pop     edx
  673.         ret
  674.  
  675. fat_entry_to_bdfe:
  676. ; convert FAT entry at edi to BDFE (block of data of folder entry) at esi, advance esi
  677. ; destroys eax
  678.         mov     eax, [ebp-4]
  679.         mov     [esi+4], eax    ; ASCII/UNICODE name
  680. fat_entry_to_bdfe2:
  681.         movzx   eax, byte [edi+11]
  682.         mov     [esi], eax      ; attributes
  683.         movzx   eax, word [edi+14]
  684.         call    fat_time_to_bdfe
  685.         mov     [esi+8], eax    ; creation time
  686.         movzx   eax, word [edi+16]
  687.         call    fat_date_to_bdfe
  688.         mov     [esi+12], eax   ; creation date
  689.         and     dword [esi+16], 0       ; last access time is not supported on FAT
  690.         movzx   eax, word [edi+18]
  691.         call    fat_date_to_bdfe
  692.         mov     [esi+20], eax   ; last access date
  693.         movzx   eax, word [edi+22]
  694.         call    fat_time_to_bdfe
  695.         mov     [esi+24], eax   ; last write time
  696.         movzx   eax, word [edi+24]
  697.         call    fat_date_to_bdfe
  698.         mov     [esi+28], eax   ; last write date
  699.         mov     eax, [edi+28]
  700.         mov     [esi+32], eax   ; file size (low dword)
  701.         xor     eax, eax
  702.         mov     [esi+36], eax   ; file size (high dword)
  703.         test    ebp, ebp
  704.         jz      .ret
  705.         push    ecx edi
  706.         lea     edi, [esi+40]
  707.         mov     esi, ebp
  708.         test    byte [esi-4], 1
  709.         jz      .ansi
  710.         mov     ecx, 260/2
  711.         rep     movsd
  712.         mov     [edi-2], ax
  713. @@:
  714.         mov     esi, edi
  715.         pop     edi ecx
  716. .ret:
  717.         ret
  718. .ansi:
  719.         mov     ecx, 264/4
  720.         rep     movsd
  721.         mov     [edi-1], al
  722.         jmp     @b
  723.  
  724. bdfe_to_fat_entry:
  725. ; convert BDFE at edx to FAT entry at edi
  726. ; destroys eax
  727. ; attributes byte
  728.         test    byte [edi+11], 8        ; volume label?
  729.         jnz     @f
  730.         mov     al, [edx]
  731.         and     al, 0x27
  732.         and     byte [edi+11], 0x10
  733.         or      byte [edi+11], al
  734. @@:
  735.         mov     eax, [edx+8]
  736.         call    bdfe_to_fat_time
  737.         mov     [edi+14], ax            ; creation time
  738.         mov     eax, [edx+12]
  739.         call    bdfe_to_fat_date
  740.         mov     [edi+16], ax            ; creation date
  741.         mov     eax, [edx+20]
  742.         call    bdfe_to_fat_date
  743.         mov     [edi+18], ax            ; last access date
  744.         mov     eax, [edx+24]
  745.         call    bdfe_to_fat_time
  746.         mov     [edi+22], ax            ; last write time
  747.         mov     eax, [edx+28]
  748.         call    bdfe_to_fat_date
  749.         mov     [edi+24], ax            ; last write date
  750.         ret
  751.  
  752. ramdisk_root_first:
  753.         mov     edi, [_rd_root]
  754.         clc
  755.         ret
  756. ramdisk_root_next:
  757.         add     edi, 0x20
  758.         cmp     edi, [_rd_root_end]
  759.         cmc
  760.         ret
  761.  
  762. ramdisk_root_extend_dir:
  763.         stc
  764.         ret
  765.  
  766. uglobal
  767. ; this is for delete support
  768. rd_prev_sector          dd      ?
  769. rd_prev_prev_sector     dd      ?
  770. endg
  771.  
  772. ramdisk_notroot_next:
  773.         add     edi, 0x20
  774.         test    edi, 0x1FF
  775.         jz      ramdisk_notroot_next_sector
  776.         ret     ; CF=0
  777. ramdisk_notroot_next_sector:
  778.         push    ecx
  779.         mov     ecx, [eax]
  780.         push    [rd_prev_sector]
  781.         pop     [rd_prev_prev_sector]
  782.         mov     [rd_prev_sector], ecx
  783.         mov     ecx, [ecx*2+RAMDISK_FAT]
  784.         and     ecx, 0xFFF
  785.         cmp     ecx, 2849
  786.         jae     ramdisk_notroot_first.err2
  787.         mov     [eax], ecx
  788.         pop     ecx
  789. ramdisk_notroot_first:
  790.         mov     eax, [eax]
  791.         cmp     eax, 2
  792.         jb      .err
  793.         cmp     eax, 2849
  794.         jae     .err
  795.         shl     eax, 9
  796.         lea     edi, [eax+(31 shl 9)]
  797.         add     edi, [_rd_base]
  798.         clc
  799.         ret
  800. .err2:
  801.         pop     ecx
  802. .err:
  803.         stc
  804.         ret
  805. ramdisk_notroot_next_write:
  806.         test    edi, 0x1FF
  807.         jz      ramdisk_notroot_next_sector
  808. ramdisk_root_next_write:
  809.         ret
  810.  
  811. ramdisk_notroot_extend_dir:
  812.         pusha
  813.         xor     eax, eax
  814.         mov     edi, RAMDISK_FAT
  815.         mov     ecx, 2849
  816.         repnz   scasw
  817.         jnz     .notfound
  818.         mov     word [edi-2], 0xFFF
  819.         sub     edi, RAMDISK_FAT
  820.         shr     edi, 1
  821.         dec     edi
  822.         mov     eax, [esp+28]
  823.         mov     ecx, [eax]
  824.         mov     [RAMDISK_FAT+ecx*2], di
  825.         mov     [eax], edi
  826.         shl     edi, 9
  827.         add     edi, (31 shl 9)
  828.         add     edi, [_rd_base]
  829.         mov     [esp], edi
  830.         xor     eax, eax
  831.         mov     ecx, 128
  832.         rep     stosd
  833.         popa
  834.         clc
  835.         ret
  836. .notfound:
  837.         popa
  838.         stc
  839.         ret
  840.  
  841. rd_find_lfn:
  842. ; in: esi+ebp -> name
  843. ; out: CF=1 - file not found
  844. ;      else CF=0 and edi->direntry
  845.         push    esi edi
  846.         push    0
  847.         push    ramdisk_root_first
  848.         push    ramdisk_root_next
  849. .loop:
  850.         call    fat_find_lfn
  851.         jc      .notfound
  852.         cmp     byte [esi], 0
  853.         jz      .found
  854. .continue:
  855.         test    byte [edi+11], 10h
  856.         jz      .notfound
  857.         movzx   eax, word [edi+26]
  858.         mov     [esp+8], eax
  859.         mov     dword [esp+4], ramdisk_notroot_first
  860.         mov     dword [esp], ramdisk_notroot_next
  861.         jmp     .loop
  862. .notfound:
  863.         add     esp, 12
  864.         pop     edi esi
  865.         stc
  866.         ret
  867. .found:
  868.         test    ebp, ebp
  869.         jz      @f
  870.         mov     esi, ebp
  871.         xor     ebp, ebp
  872.         jmp     .continue
  873. @@:
  874.         mov     eax, [esp+8]
  875.         add     esp, 16         ; CF=0
  876.         pop     esi
  877.         ret
  878.  
  879. ;----------------------------------------------------------------
  880. ;
  881. ;  fs_RamdiskRead - LFN variant for reading sys floppy
  882. ;
  883. ;  esi  points to filename
  884. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  885. ;       may be ebx=0 - start from first byte
  886. ;  ecx  number of bytes to read, 0+
  887. ;  edx  mem location to return data
  888. ;
  889. ;  ret ebx = bytes read or 0xffffffff file not found
  890. ;      eax = 0 ok read or other = errormsg
  891. ;
  892. ;--------------------------------------------------------------
  893. fs_RamdiskRead:
  894.         cmp     byte [esi], 0
  895.         jnz     @f
  896.         or      ebx, -1
  897.         mov     eax, 10         ; access denied
  898.         ret
  899. @@:
  900.         push    edi
  901.         call    rd_find_lfn
  902.         jnc     .found
  903.         pop     edi
  904.         or      ebx, -1
  905.         mov     eax, 5          ; file not found
  906.         ret
  907. .found:
  908.         test    ebx, ebx
  909.         jz      .l1
  910.         cmp     dword [ebx+4], 0
  911.         jz      @f
  912.         xor     ebx, ebx
  913. .reteof:
  914.         mov     eax, 6          ; EOF
  915.         pop     edi
  916.         ret
  917. @@:
  918.         mov     ebx, [ebx]
  919. .l1:
  920.         push    ecx edx
  921.         push    0
  922.         mov     eax, [edi+28]
  923.         sub     eax, ebx
  924.         jb      .eof
  925.         cmp     eax, ecx
  926.         jae     @f
  927.         mov     ecx, eax
  928.         mov     byte [esp], 6           ; EOF
  929. @@:
  930.         movzx   edi, word [edi+26]      ; cluster
  931. .new:
  932.         jecxz   .done
  933.         test    edi, edi
  934.         jz      .eof
  935.         cmp     edi, 0xFF8
  936.         jae     .eof
  937.         lea     eax, [edi+31]           ; bootsector+2*fat+filenames
  938.         shl     eax, 9                  ; *512
  939.         add     eax, [_rd_base]         ; image base
  940. ; now eax points to data of cluster
  941.         sub     ebx, 512
  942.         jae     .skip
  943.         lea     eax, [eax+ebx+512]
  944.         neg     ebx
  945.         push    ecx
  946.         cmp     ecx, ebx
  947.         jbe     @f
  948.         mov     ecx, ebx
  949. @@:
  950.         mov     ebx, edx
  951.         call    memmove
  952.         add     edx, ecx
  953.         sub     [esp], ecx
  954.         pop     ecx
  955.         xor     ebx, ebx
  956. .skip:
  957.         movzx   edi, word [edi*2+RAMDISK_FAT]      ; find next cluster from FAT
  958.         jmp     .new
  959. .eof:
  960.         mov     ebx, edx
  961.         pop     eax edx ecx
  962.         sub     ebx, edx
  963.         jmp     .reteof
  964. .done:
  965.         mov     ebx, edx
  966.         pop     eax edx ecx edi
  967.         sub     ebx, edx
  968.         ret
  969.  
  970. ;----------------------------------------------------------------
  971. ;
  972. ;  fs_RamdiskReadFolder - LFN variant for reading sys floppy folder
  973. ;
  974. ;  esi  points to filename; only root is folder on ramdisk
  975. ;  ebx  pointer to structure 32-bit number = first wanted block
  976. ;                          & flags (bitfields)
  977. ; flags: bit 0: 0=ANSI names, 1=UNICODE names
  978. ;  ecx  number of blocks to read, 0+
  979. ;  edx  mem location to return data
  980. ;
  981. ;  ret ebx = size or 0xffffffff file not found
  982. ;      eax = 0 ok read or other = errormsg
  983. ;
  984. ;--------------------------------------------------------------
  985. fs_RamdiskReadFolder:
  986.         push    edi
  987.         cmp     byte [esi], 0
  988.         jz      .root
  989.         call    rd_find_lfn
  990.         jnc     .found
  991.         pop     edi
  992.         or      ebx, -1
  993.         mov     eax, ERROR_FILE_NOT_FOUND
  994.         ret
  995. .found:
  996.         test    byte [edi+11], 0x10
  997.         jnz     .found_dir
  998.         pop     edi
  999.         or      ebx, -1
  1000.         mov     eax, ERROR_ACCESS_DENIED
  1001.         ret
  1002. .found_dir:
  1003.         movzx   eax, word [edi+26]
  1004.         add     eax, 31
  1005.         push    0
  1006.         jmp     .doit
  1007. .root:
  1008.         mov     eax, 19
  1009.         push    14
  1010. .doit:
  1011.         push    esi ecx ebp
  1012.         sub     esp, 262*2      ; reserve space for LFN
  1013.         mov     ebp, esp
  1014.         push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE names
  1015.         mov     ebx, [ebx]
  1016. ; init header
  1017.         push    eax ecx
  1018.         mov     edi, edx
  1019.         mov     ecx, 32/4
  1020.         xor     eax, eax
  1021.         rep     stosd
  1022.         mov     byte [edx], 1   ; version
  1023.         pop     ecx eax
  1024.         mov     esi, edi        ; esi points to block of data of folder entry (BDFE)
  1025. .main_loop:
  1026.         mov     edi, eax
  1027.         shl     edi, 9
  1028.         add     edi, [_rd_base]
  1029.         push    eax
  1030. .l1:
  1031.         call    fat_get_name
  1032.         jc      .l2
  1033.         cmp     byte [edi+11], 0xF
  1034.         jnz     .do_bdfe
  1035.         add     edi, 0x20
  1036.         test    edi, 0x1FF
  1037.         jnz     .do_bdfe
  1038.         pop     eax
  1039.         inc     eax
  1040.         dec     byte [esp+262*2+16]
  1041.         jz      .done
  1042.         jns     @f
  1043. ; read next sector from FAT
  1044.         mov     eax, [(eax-31-1)*2+RAMDISK_FAT]
  1045.         and     eax, 0xFFF
  1046.         cmp     eax, 0xFF8
  1047.         jae     .done
  1048.         add     eax, 31
  1049.         mov     byte [esp+262*2+16], 0
  1050. @@:
  1051.         mov     edi, eax
  1052.         shl     edi, 9
  1053.         add     edi, [_rd_base]
  1054.         push    eax
  1055. .do_bdfe:
  1056.         inc     dword [edx+8]   ; new file found
  1057.         dec     ebx
  1058.         jns     .l2
  1059.         dec     ecx
  1060.         js      .l2
  1061.         inc     dword [edx+4]  ; new file block copied
  1062.         call    fat_entry_to_bdfe
  1063. .l2:
  1064.         add     edi, 0x20
  1065.         test    edi, 0x1FF
  1066.         jnz     .l1
  1067.         pop     eax
  1068.         inc     eax
  1069.         dec     byte [esp+262*2+16]
  1070.         jz      .done
  1071.         jns     @f
  1072. ; read next sector from FAT
  1073.         mov     eax, [(eax-31-1)*2+RAMDISK_FAT]
  1074.         and     eax, 0xFFF
  1075.         cmp     eax, 0xFF8
  1076.         jae     .done
  1077.         add     eax, 31
  1078.         mov     byte [esp+262*2+16], 0
  1079. @@:
  1080.         jmp     .main_loop
  1081. .done:
  1082.         add     esp, 262*2+4
  1083.         pop     ebp
  1084.         mov     ebx, [edx+4]
  1085.         xor     eax, eax
  1086.         dec     ecx
  1087.         js      @f
  1088.         mov     al, ERROR_END_OF_FILE
  1089. @@:
  1090.         pop     ecx esi edi edi
  1091.         ret
  1092.  
  1093. iglobal
  1094. label fat_legal_chars byte
  1095. ; 0 = not allowed
  1096. ; 1 = allowed only in long names
  1097. ; 3 = allowed
  1098.         times 32 db 0
  1099. ;                 ! " # $ % & ' ( ) * + , - . /
  1100.         db      1,3,0,3,3,3,3,3,3,3,0,1,1,3,3,0
  1101. ;               0 1 2 3 4 5 6 7 8 9 : ; < = > ?
  1102.         db      3,3,3,3,3,3,3,3,3,3,0,1,0,1,0,0
  1103. ;               @ A B C D E F G H I J K L M N O
  1104.         db      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
  1105. ;               P Q R S T U V W X Y Z [ \ ] ^ _
  1106.         db      3,3,3,3,3,3,3,3,3,3,3,1,0,1,3,3
  1107. ;               ` a b c d e f g h i j k l m n o
  1108.         db      3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
  1109. ;               p q r s t u v w x y z { | } ~
  1110.         db      3,3,3,3,3,3,3,3,3,3,3,3,0,3,3,0
  1111. endg
  1112.  
  1113. fat_name_is_legal:
  1114. ; in: esi->(long) name
  1115. ; out: CF set <=> legal
  1116. ; destroys eax
  1117.         push    esi
  1118.         xor     eax, eax
  1119. @@:
  1120.         lodsb
  1121.         test    al, al
  1122.         jz      .done
  1123.         cmp     al, 80h
  1124.         jae     .big
  1125.         test    [fat_legal_chars+eax], 1
  1126.         jnz     @b
  1127. .err:
  1128.         pop     esi
  1129.         clc
  1130.         ret
  1131. .big:
  1132. ; 0x80-0xAF, 0xE0-0xEF
  1133.         cmp     al, 0xB0
  1134.         jb      @b
  1135.         cmp     al, 0xE0
  1136.         jb      .err
  1137.         cmp     al, 0xF0
  1138.         jb      @b
  1139.         jmp     .err
  1140. .done:
  1141.         sub     esi, [esp]
  1142.         cmp     esi, 257
  1143.         pop     esi
  1144.         ret
  1145.  
  1146. fat_next_short_name:
  1147. ; in: edi->8+3 name
  1148. ; out: name corrected
  1149. ;      CF=1 <=> error
  1150.         pushad
  1151.         mov     ecx, 8
  1152.         mov     al, '~'
  1153.         std
  1154.         push    edi
  1155.         add     edi, 7
  1156.         repnz   scasb
  1157.         pop     edi
  1158.         cld
  1159.         jz      .tilde
  1160. ; tilde is not found, insert "~1" at end
  1161.         add     edi, 6
  1162.         cmp     word [edi], '  '
  1163.         jnz     .insert_tilde
  1164. @@:     dec     edi
  1165.         cmp     byte [edi], ' '
  1166.         jz      @b
  1167.         inc     edi
  1168. .insert_tilde:
  1169.         mov     word [edi], '~1'
  1170.         popad
  1171.         clc
  1172.         ret
  1173. .tilde:
  1174.         push    edi
  1175.         add     edi, 7
  1176.         xor     ecx, ecx
  1177. @@:
  1178. ; after tilde may be only digits and trailing spaces
  1179.         cmp     byte [edi], '~'
  1180.         jz      .break
  1181.         cmp     byte [edi], ' '
  1182.         jz      .space
  1183.         cmp     byte [edi], '9'
  1184.         jnz     .found
  1185.         dec     edi
  1186.         jmp     @b
  1187. .space:
  1188.         dec     edi
  1189.         inc     ecx
  1190.         jmp     @b
  1191. .found:
  1192.         inc     byte [edi]
  1193.         add     dword [esp], 8
  1194.         jmp     .zerorest
  1195. .break:
  1196.         jecxz   .noplace
  1197.         inc     edi
  1198.         mov     al, '1'
  1199. @@:
  1200.         xchg    al, [edi]
  1201.         inc     edi
  1202.         cmp     al, ' '
  1203.         mov     al, '0'
  1204.         jnz     @b
  1205. .succ:
  1206.         pop     edi
  1207.         popad
  1208.         clc
  1209.         ret
  1210. .noplace:
  1211.         dec     edi
  1212.         cmp     edi, [esp]
  1213.         jz      .err
  1214.         add     dword [esp], 8
  1215.         mov     word [edi], '~1'
  1216.         inc     edi
  1217.         inc     edi
  1218. @@:
  1219.         mov     byte [edi], '0'
  1220. .zerorest:
  1221.         inc     edi
  1222.         cmp     edi, [esp]
  1223.         jb      @b
  1224.         pop     edi
  1225.         popad
  1226.         ;clc    ; automatically
  1227.         ret
  1228. .err:
  1229.         pop     edi
  1230.         popad
  1231.         stc
  1232.         ret
  1233.  
  1234. fat_gen_short_name:
  1235. ; in: esi->long name
  1236. ;     edi->buffer (8+3=11 chars)
  1237. ; out: buffer filled
  1238.         pushad
  1239.         mov     eax, '    '
  1240.         push    edi
  1241.         stosd
  1242.         stosd
  1243.         stosd
  1244.         pop     edi
  1245.         xor     eax, eax
  1246.         push    8
  1247.         pop     ebx
  1248.         lea     ecx, [edi+8]
  1249. .loop:
  1250.         lodsb
  1251.         test    al, al
  1252.         jz      .done
  1253.         call    char_toupper
  1254.         cmp     al, ' '
  1255.         jz      .space
  1256.         cmp     al, 80h
  1257.         ja      .big
  1258.         test    [fat_legal_chars+eax], 2
  1259.         jnz     .symbol
  1260. .inv_symbol:
  1261.         mov     al, '_'
  1262.         or      bh, 1
  1263. .symbol:
  1264.         cmp     al, '.'
  1265.         jz      .dot
  1266. .normal_symbol:
  1267.         dec     bl
  1268.         jns     .store
  1269.         mov     bl, 0
  1270. .space:
  1271.         or      bh, 1
  1272.         jmp     .loop
  1273. .store:
  1274.         stosb
  1275.         jmp     .loop
  1276. .big:
  1277.         cmp     al, 0xB0
  1278.         jb      .normal_symbol
  1279.         cmp     al, 0xE0
  1280.         jb      .inv_symbol
  1281.         cmp     al, 0xF0
  1282.         jb      .normal_symbol
  1283.         jmp     .inv_symbol
  1284. .dot:
  1285.         test    bh, 2
  1286.         jz      .firstdot
  1287.         pop     ebx
  1288.         add     ebx, edi
  1289.         sub     ebx, ecx
  1290.         push    ebx
  1291.         cmp     ebx, ecx
  1292.         jb      @f
  1293.         pop     ebx
  1294.         push    ecx
  1295. @@:
  1296.         cmp     edi, ecx
  1297.         jbe     .skip
  1298. @@:
  1299.         dec     edi
  1300.         mov     al, [edi]
  1301.         dec     ebx
  1302.         mov     [ebx], al
  1303.         mov     byte [edi], ' '
  1304.         cmp     edi, ecx
  1305.         ja      @b
  1306. .skip:
  1307.         mov     bh, 3
  1308.         jmp     @f
  1309. .firstdot:
  1310.         cmp     bl, 8
  1311.         jz      .space
  1312.         push    edi
  1313.         or      bh, 2
  1314. @@:
  1315.         mov     edi, ecx
  1316.         mov     bl, 3
  1317.         jmp     .loop
  1318. .done:
  1319.         test    bh, 2
  1320.         jz      @f
  1321.         pop     edi
  1322. @@:
  1323.         lea     edi, [ecx-8]
  1324.         test    bh, 1
  1325.         jz      @f
  1326.         call    fat_next_short_name
  1327. @@:
  1328.         popad
  1329.         ret
  1330.  
  1331. ;----------------------------------------------------------------
  1332. ;
  1333. ;  fs_RamdiskRewrite - LFN variant for writing ramdisk
  1334. ;  fs_RamdiskCreateFolder - create folder on ramdisk
  1335. ;
  1336. ;  esi  points to file/folder name
  1337. ;  ebx  ignored (reserved)
  1338. ;  ecx  number of bytes to write, 0+ (ignored for folders)
  1339. ;  edx  mem location to data (ignored for folders)
  1340. ;
  1341. ;  ret ebx = number of written bytes
  1342. ;      eax = 0 ok read or other = errormsg
  1343. ;
  1344. ;--------------------------------------------------------------
  1345. @@:
  1346.         mov     eax, ERROR_ACCESS_DENIED
  1347.         xor     ebx, ebx
  1348.         ret
  1349.  
  1350. fs_RamdiskCreateFolder:
  1351.         mov     al, 1           ; create folder
  1352.         jmp     fs_RamdiskRewrite.common
  1353.  
  1354. fs_RamdiskRewrite:
  1355.         xor     eax, eax        ; create file
  1356. .common:
  1357.         cmp     byte [esi], 0
  1358.         jz      @b
  1359.         pushad
  1360.         xor     edi, edi
  1361.         push    esi
  1362.         test    ebp, ebp
  1363.         jz      @f
  1364.         mov     esi, ebp
  1365. @@:
  1366.         lodsb
  1367.         test    al, al
  1368.         jz      @f
  1369.         cmp     al, '/'
  1370.         jnz     @b
  1371.         lea     edi, [esi-1]
  1372.         jmp     @b
  1373. @@:
  1374.         pop     esi
  1375.         test    edi, edi
  1376.         jnz     .noroot
  1377.         test    ebp, ebp
  1378.         jnz     .hasebp
  1379.         push    ramdisk_root_extend_dir
  1380.         push    ramdisk_root_next_write
  1381.         push    edi
  1382.         push    ramdisk_root_first
  1383.         push    ramdisk_root_next
  1384.         jmp     .common1
  1385. .hasebp:
  1386.         mov     eax, ERROR_ACCESS_DENIED
  1387.         cmp     byte [ebp], 0
  1388.         jz      .ret1
  1389.         push    ebp
  1390.         xor     ebp, ebp
  1391.         call    rd_find_lfn
  1392.         pop     esi
  1393.         jc      .notfound0
  1394.         jmp     .common0
  1395. .noroot:
  1396.         mov     eax, ERROR_ACCESS_DENIED
  1397.         cmp     byte [edi+1], 0
  1398.         jz      .ret1
  1399. ; check existence
  1400.         mov     byte [edi], 0
  1401.         push    edi
  1402.         call    rd_find_lfn
  1403.         pop     esi
  1404.         mov     byte [esi], '/'
  1405.         jnc     @f
  1406. .notfound0:
  1407.         mov     eax, ERROR_FILE_NOT_FOUND
  1408. .ret1:
  1409.         mov     [esp+28], eax
  1410.         popad
  1411.         xor     ebx, ebx
  1412.         ret
  1413. @@:
  1414.         inc     esi
  1415. .common0:
  1416.         test    byte [edi+11], 0x10     ; must be directory
  1417.         mov     eax, ERROR_ACCESS_DENIED
  1418.         jz      .ret1
  1419.         movzx   ebp, word [edi+26]      ; ebp=cluster
  1420.         mov     eax, ERROR_FAT_TABLE
  1421.         cmp     ebp, 2
  1422.         jb      .ret1
  1423.         cmp     ebp, 2849
  1424.         jae     .ret1
  1425.         push    ramdisk_notroot_extend_dir
  1426.         push    ramdisk_notroot_next_write
  1427.         push    ebp
  1428.         push    ramdisk_notroot_first
  1429.         push    ramdisk_notroot_next
  1430. .common1:
  1431.         call    fat_find_lfn
  1432.         jc      .notfound
  1433. ; found
  1434.         test    byte [edi+11], 10h
  1435.         jz      .exists_file
  1436. ; found directory; if we are creating directory, return OK,
  1437. ;                  if we are creating file, say "access denied"
  1438.         add     esp, 20
  1439.         popad
  1440.         test    al, al
  1441.         mov     eax, ERROR_ACCESS_DENIED
  1442.         jz      @f
  1443.         mov     al, 0
  1444. @@:
  1445.         xor     ebx, ebx
  1446.         ret
  1447. .exists_file:
  1448. ; found file; if we are creating directory, return "access denied",
  1449. ;             if we are creating file, delete existing file and continue
  1450.         cmp     byte [esp+20+28], 0
  1451.         jz      @f
  1452.         add     esp, 20
  1453.         popad
  1454.         mov     eax, ERROR_ACCESS_DENIED
  1455.         xor     ebx, ebx
  1456.         ret
  1457. @@:
  1458. ; delete FAT chain
  1459.         push    edi
  1460.         xor     eax, eax
  1461.         mov     dword [edi+28], eax     ; zero size
  1462.         xchg    ax, word [edi+26]       ; start cluster
  1463.         test    eax, eax
  1464.         jz      .done1
  1465. @@:
  1466.         cmp     eax, 0xFF8
  1467.         jae     .done1
  1468.         lea     edi, [RAMDISK_FAT + eax*2] ; position in FAT
  1469.         xor     eax, eax
  1470.         xchg    ax, [edi]
  1471.         jmp     @b
  1472. .done1:
  1473.         pop     edi
  1474.         call    get_time_for_file
  1475.         mov     [edi+22], ax
  1476.         call    get_date_for_file
  1477.         mov     [edi+24], ax
  1478.         mov     [edi+18], ax
  1479.         or      byte [edi+11], 20h      ; set 'archive' attribute
  1480.         jmp     .doit
  1481. .notfound:
  1482. ; file is not found; generate short name
  1483.         call    fat_name_is_legal
  1484.         jc      @f
  1485.         add     esp, 20
  1486.         popad
  1487.         mov     eax, ERROR_FILE_NOT_FOUND
  1488.         xor     ebx, ebx
  1489.         ret
  1490. @@:
  1491.         sub     esp, 12
  1492.         mov     edi, esp
  1493.         call    fat_gen_short_name
  1494. .test_short_name_loop:
  1495.         push    esi edi ecx
  1496.         mov     esi, edi
  1497.         lea     eax, [esp+12+12+8]
  1498.         mov     [eax], ebp
  1499.         call    dword [eax-4]
  1500.         jc      .found
  1501. .test_short_name_entry:
  1502.         cmp     byte [edi+11], 0xF
  1503.         jz      .test_short_name_cont
  1504.         mov     ecx, 11
  1505.         push    esi edi
  1506.         repz    cmpsb
  1507.         pop     edi esi
  1508.         jz      .short_name_found
  1509. .test_short_name_cont:
  1510.         lea     eax, [esp+12+12+8]
  1511.         call    dword [eax-8]
  1512.         jnc     .test_short_name_entry
  1513.         jmp     .found
  1514. .short_name_found:
  1515.         pop     ecx edi esi
  1516.         call    fat_next_short_name
  1517.         jnc     .test_short_name_loop
  1518. .disk_full:
  1519.         add     esp, 12+20
  1520.         popad
  1521.         mov     eax, ERROR_DISK_FULL
  1522.         xor     ebx, ebx
  1523.         ret
  1524. .found:
  1525.         pop     ecx edi esi
  1526. ; now find space in directory
  1527. ; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
  1528.         mov     al, '~'
  1529.         push    ecx edi
  1530.         mov     ecx, 8
  1531.         repnz   scasb
  1532.         push    1
  1533.         pop     eax     ; 1 entry
  1534.         jnz     .notilde
  1535. ; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
  1536.         xor     eax, eax
  1537. @@:
  1538.         cmp     byte [esi], 0
  1539.         jz      @f
  1540.         inc     esi
  1541.         inc     eax
  1542.         jmp     @b
  1543. @@:
  1544.         sub     esi, eax
  1545.         add     eax, 12+13
  1546.         mov     ecx, 13
  1547.         push    edx
  1548.         cdq
  1549.         div     ecx
  1550.         pop     edx
  1551. .notilde:
  1552.         push    -1
  1553.         push    -1
  1554. ; find <eax> successive entries in directory
  1555.         xor     ecx, ecx
  1556.         push    eax
  1557.         lea     eax, [esp+12+8+12+8]
  1558.         mov     [eax], ebp
  1559.         call    dword [eax-4]
  1560.         pop     eax
  1561. .scan_dir:
  1562.         cmp     byte [edi], 0
  1563.         jz      .free
  1564.         cmp     byte [edi], 0xE5
  1565.         jz      .free
  1566.         xor     ecx, ecx
  1567. .scan_cont:
  1568.         push    eax
  1569.         lea     eax, [esp+12+8+12+8]
  1570.         call    dword [eax-8]
  1571.         pop     eax
  1572.         jnc     .scan_dir
  1573.         push    eax
  1574.         lea     eax, [esp+12+8+12+8]
  1575.         call    dword [eax+8]           ; extend directory
  1576.         pop     eax
  1577.         jnc     .scan_dir
  1578.         add     esp, 8+8+12+20
  1579.         popad
  1580.         mov     eax, ERROR_DISK_FULL
  1581.         xor     ebx, ebx
  1582.         ret
  1583. .free:
  1584.         test    ecx, ecx
  1585.         jnz     @f
  1586.         mov     [esp], edi
  1587.         mov     ecx, [esp+8+8+12+8]
  1588.         mov     [esp+4], ecx
  1589.         xor     ecx, ecx
  1590. @@:
  1591.         inc     ecx
  1592.         cmp     ecx, eax
  1593.         jb      .scan_cont
  1594. ; found!
  1595. ; calculate name checksum
  1596.         push    esi ecx
  1597.         mov     esi, [esp+8+8]
  1598.         mov     ecx, 11
  1599.         xor     eax, eax
  1600. @@:
  1601.         ror     al, 1
  1602.         add     al, [esi]
  1603.         inc     esi
  1604.         loop    @b
  1605.         pop     ecx esi
  1606.         pop     edi
  1607.         pop     dword [esp+8+12+8]
  1608. ; edi points to last entry in free chunk
  1609.         dec     ecx
  1610.         jz      .nolfn
  1611.         push    esi
  1612.         push    eax
  1613.         mov     al, 40h
  1614. .writelfn:
  1615.         or      al, cl
  1616.         mov     esi, [esp+4]
  1617.         push    ecx
  1618.         dec     ecx
  1619.         imul    ecx, 13
  1620.         add     esi, ecx
  1621.         stosb
  1622.         mov     cl, 5
  1623.         call    .read_symbols
  1624.         mov     ax, 0xF
  1625.         stosw
  1626.         mov     al, [esp+4]
  1627.         stosb
  1628.         mov     cl, 6
  1629.         call    .read_symbols
  1630.         xor     eax, eax
  1631.         stosw
  1632.         mov     cl, 2
  1633.         call    .read_symbols
  1634.         pop     ecx
  1635.         lea     eax, [esp+8+8+12+8]
  1636.         call    dword [eax+4]   ; next write
  1637.         xor     eax, eax
  1638.         loop    .writelfn
  1639.         pop     eax
  1640.         pop     esi
  1641. .nolfn:
  1642.         xchg    esi, [esp]
  1643.         mov     ecx, 11
  1644.         rep     movsb
  1645.         mov     word [edi], 20h         ; attributes
  1646.         sub     edi, 11
  1647.         pop     esi ecx
  1648.         add     esp, 12
  1649.         mov     byte [edi+13], 0        ; tenths of a second at file creation time
  1650.         call    get_time_for_file
  1651.         mov     [edi+14], ax            ; creation time
  1652.         mov     [edi+22], ax            ; last write time
  1653.         call    get_date_for_file
  1654.         mov     [edi+16], ax            ; creation date
  1655.         mov     [edi+24], ax            ; last write date
  1656.         mov     [edi+18], ax            ; last access date
  1657.         and     word [edi+20], 0        ; high word of cluster
  1658.         and     word [edi+26], 0        ; low word of cluster - to be filled
  1659.         and     dword [edi+28], 0       ; file size - to be filled
  1660.         cmp     byte [esp+20+28], 0
  1661.         jz      .doit
  1662. ; create directory
  1663.         mov     byte [edi+11], 10h         ; attributes: folder
  1664.         mov     ecx, 32*2
  1665.         mov     edx, edi
  1666. .doit:
  1667.         push    edx
  1668.         push    ecx
  1669.         push    edi
  1670.         add     edi, 26         ; edi points to low word of cluster
  1671.         push    edi
  1672.         jecxz   .done
  1673.         mov     ecx, 2849
  1674.         mov     edi, RAMDISK_FAT
  1675. .write_loop:
  1676. ; allocate new cluster
  1677.         xor     eax, eax
  1678.         repnz   scasw
  1679.         jnz     .disk_full2
  1680.         dec     edi
  1681.         dec     edi
  1682.  
  1683.     ;    lea     eax, [edi-(RAMDISK_FAT)]
  1684.  
  1685.         mov eax, edi
  1686.         sub eax, RAMDISK_FAT
  1687.  
  1688.         shr     eax, 1                  ; eax = cluster
  1689.         mov     word [edi], 0xFFF       ; mark as last cluster
  1690.         xchg    edi, [esp]
  1691.         stosw
  1692.         pop     edi
  1693.         push    edi
  1694.         inc     ecx
  1695. ; write data
  1696.         cmp     byte [esp+16+20+28], 0
  1697.         jnz     .writedir
  1698.         shl     eax, 9
  1699.         add     eax, 31*512
  1700.         add     eax, [_rd_base]
  1701. .writefile:
  1702.         mov     ebx, edx
  1703.         xchg    eax, ebx
  1704.         push    ecx
  1705.         mov     ecx, 512
  1706.         cmp     dword [esp+12], ecx
  1707.         jae     @f
  1708.         mov     ecx, [esp+12]
  1709. @@:
  1710.         call    memmove
  1711.         add     edx, ecx
  1712.         sub     [esp+12], ecx
  1713.         pop     ecx
  1714.         jnz     .write_loop
  1715. .done:
  1716.         mov     ebx, edx
  1717.         pop     edi edi ecx edx
  1718.         sub     ebx, edx
  1719.         mov     [edi+28], ebx
  1720.         add     esp, 20
  1721.         mov     [esp+16], ebx
  1722.         popad
  1723.         xor     eax, eax
  1724.         ret
  1725. .disk_full2:
  1726.         mov     ebx, edx
  1727.         pop     edi edi ecx edx
  1728.         sub     ebx, edx
  1729.         mov     [edi+28], ebx
  1730.         add     esp, 20
  1731.         mov     [esp+16], ebx
  1732.         popad
  1733.         push    ERROR_DISK_FULL
  1734.         pop     eax
  1735.         ret
  1736. .writedir:
  1737.         mov     edi, eax
  1738.         shl     edi, 9
  1739.         add     edi, [_rd_base]
  1740.         add     edi, 31*512
  1741.         mov     esi, edx
  1742.         mov     ecx, 32/4
  1743.         push    ecx
  1744.         rep     movsd
  1745.         mov     dword [edi-32], '.   '
  1746.         mov     dword [edi-32+4], '    '
  1747.         mov     dword [edi-32+8], '    '
  1748.         mov     byte [edi-32+11], 10h
  1749.         mov     word [edi-32+26], ax
  1750.         mov     esi, edx
  1751.         pop     ecx
  1752.         rep     movsd
  1753.         mov     dword [edi-32], '..  '
  1754.         mov     dword [edi-32+4], '    '
  1755.         mov     dword [edi-32+8], '    '
  1756.         mov     byte [edi-32+11], 10h
  1757.         mov     eax, [esp+16+8]
  1758.         mov     word [edi-32+26], ax
  1759.         xor     eax, eax
  1760.         mov     ecx, (512-32*2)/4
  1761.         rep     stosd
  1762.         pop     edi edi ecx edx
  1763.         add     esp, 20
  1764.         popad
  1765.         xor     eax, eax
  1766.         xor     ebx, ebx
  1767.         ret
  1768.  
  1769. .read_symbol:
  1770.         or      ax, -1
  1771.         test    esi, esi
  1772.         jz      .retFFFF
  1773.         lodsb
  1774.         test    al, al
  1775.         jnz     ansi2uni_char
  1776.         xor     eax, eax
  1777.         xor     esi, esi
  1778. .retFFFF:
  1779.         ret
  1780.  
  1781. .read_symbols:
  1782.         call    .read_symbol
  1783.         stosw
  1784.         loop    .read_symbols
  1785.         ret
  1786.  
  1787. ;----------------------------------------------------------------
  1788. ;
  1789. ;  fs_RamdiskWrite - LFN variant for writing to sys floppy
  1790. ;
  1791. ;  esi  points to filename
  1792. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  1793. ;       may be ebx=0 - start from first byte
  1794. ;  ecx  number of bytes to write, 0+
  1795. ;  edx  mem location to data
  1796. ;
  1797. ;  ret ebx = bytes written (maybe 0)
  1798. ;      eax = 0 ok write or other = errormsg
  1799. ;
  1800. ;--------------------------------------------------------------
  1801. @@:
  1802.         push    ERROR_ACCESS_DENIED
  1803. fs_RamdiskWrite.ret0:
  1804.         pop     eax
  1805.         xor     ebx, ebx
  1806.         ret
  1807.  
  1808. fs_RamdiskWrite:
  1809.         cmp     byte [esi], 0
  1810.         jz      @b
  1811.         pushad
  1812.         call    rd_find_lfn
  1813.         jnc     .found
  1814.         popad
  1815.         push    ERROR_FILE_NOT_FOUND
  1816.         jmp     .ret0
  1817. .found:
  1818. ; must not be directory
  1819.         test    byte [edi+11], 10h
  1820.         jz      @f
  1821.         popad
  1822.         push    ERROR_ACCESS_DENIED
  1823.         jmp     .ret0
  1824. @@:
  1825. ; FAT does not support files larger than 4GB
  1826.         test    ebx, ebx
  1827.         jz      .l1
  1828.         cmp     dword [ebx+4], 0
  1829.         jz      @f
  1830. .eof:
  1831.         popad
  1832.         push    ERROR_END_OF_FILE
  1833.         jmp     .ret0
  1834. @@:
  1835.         mov     ebx, [ebx]
  1836. .l1:
  1837. ; now edi points to direntry, ebx=start byte to write,
  1838. ; ecx=number of bytes to write, edx=data pointer
  1839.         call    fat_update_datetime
  1840.  
  1841. ; extend file if needed
  1842.         add     ecx, ebx
  1843.         jc      .eof    ; FAT does not support files larger than 4GB
  1844.         push    0       ; return value=0
  1845.         cmp     ecx, [edi+28]
  1846.         jbe     .length_ok
  1847.         cmp     ecx, ebx
  1848.         jz      .length_ok
  1849.         call    ramdisk_extend_file
  1850.         jnc     .length_ok
  1851. ; ramdisk_extend_file can return two error codes: FAT table error or disk full.
  1852. ; First case is fatal error, in second case we may write some data
  1853.         mov     [esp], eax
  1854.         cmp     al, ERROR_DISK_FULL
  1855.         jz      .disk_full
  1856.         pop     eax
  1857.         mov     [esp+28], eax
  1858.         popad
  1859.         xor     ebx, ebx
  1860.         ret
  1861. .disk_full:
  1862. ; correct number of bytes to write
  1863.         mov     ecx, [edi+28]
  1864.         cmp     ecx, ebx
  1865.         ja      .length_ok
  1866. .ret:
  1867.         pop     eax
  1868.         mov     [esp+28], eax   ; eax=return value
  1869.         sub     edx, [esp+20]
  1870.         mov     [esp+16], edx   ; ebx=number of written bytes
  1871.         popad
  1872.         ret
  1873. .length_ok:
  1874. ; now ebx=start pos, ecx=end pos, both lie inside file
  1875.         sub     ecx, ebx
  1876.         jz      .ret
  1877.         movzx   edi, word [edi+26]      ; starting cluster
  1878. .write_loop:
  1879.         sub     ebx, 0x200
  1880.         jae     .next_cluster
  1881.         push    ecx
  1882.         neg     ebx
  1883.         cmp     ecx, ebx
  1884.         jbe     @f
  1885.         mov     ecx, ebx
  1886. @@:
  1887.         mov     eax, edi
  1888.         shl     eax, 9
  1889.         add     eax, [_rd_base]
  1890.         add     eax, 31*512+0x200
  1891.         sub     eax, ebx
  1892.         mov     ebx, eax
  1893.         mov     eax, edx
  1894.         call    memmove
  1895.         xor     ebx, ebx
  1896.         add     edx, ecx
  1897.         sub     [esp], ecx
  1898.         pop     ecx
  1899.         jz      .ret
  1900. .next_cluster:
  1901.         movzx   edi, word [edi*2+RAMDISK_FAT]
  1902.         jmp     .write_loop
  1903.  
  1904. ramdisk_extend_file.zero_size:
  1905.         xor     eax, eax
  1906.         jmp     ramdisk_extend_file.start_extend
  1907.  
  1908. ; extends file on ramdisk to given size, new data area is filled by 0
  1909. ; in: edi->direntry, ecx=new size
  1910. ; out: CF=0 => OK, eax=0
  1911. ;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL)
  1912. ramdisk_extend_file:
  1913.         push    ecx
  1914. ; find the last cluster of file
  1915.         movzx   eax, word [edi+26]      ; first cluster
  1916.         mov     ecx, [edi+28]
  1917.         jecxz   .zero_size
  1918. @@:
  1919.         sub     ecx, 0x200
  1920.         jbe     @f
  1921.         mov     eax, [eax*2+RAMDISK_FAT]
  1922.         and     eax, 0xFFF
  1923.         jz      .fat_err
  1924.         cmp     eax, 0xFF8
  1925.         jb      @b
  1926. .fat_err:
  1927.         pop     ecx
  1928.         push    ERROR_FAT_TABLE
  1929.         pop     eax
  1930.         stc
  1931.         ret
  1932. @@:
  1933.         push    eax
  1934.         mov     eax, [eax*2+RAMDISK_FAT]
  1935.         and     eax, 0xFFF
  1936.         cmp     eax, 0xFF8
  1937.         pop     eax
  1938.         jb      .fat_err
  1939. ; set length to full number of sectors and make sure that last sector is zero-padded
  1940.         sub     [edi+28], ecx
  1941.         push    eax edi
  1942.         mov     edi, eax
  1943.         shl     edi, 9
  1944.         add edi, [_rd_base]
  1945.         lea     edi, [edi+31*512+0x200+ecx]
  1946.         neg     ecx
  1947.         xor     eax, eax
  1948.         rep     stosb
  1949.         pop     edi eax
  1950. .start_extend:
  1951.         pop     ecx
  1952. ; now do extend
  1953.         push    edx esi
  1954.         mov     esi, RAMDISK_FAT+2*2       ; start scan from cluster 2
  1955.         mov     edx, 2847               ; number of clusters to scan
  1956. .extend_loop:
  1957.         cmp     [edi+28], ecx
  1958.         jae     .extend_done
  1959. ; add new sector
  1960.         push    ecx
  1961.         mov     ecx, edx
  1962.         push    edi
  1963.         mov     edi, esi
  1964.         jecxz   .disk_full
  1965.         push    eax
  1966.         xor     eax, eax
  1967.         repnz   scasw
  1968.         pop     eax
  1969.         jnz     .disk_full
  1970.         mov     word [edi-2], 0xFFF
  1971.         mov     esi, edi
  1972.         mov     edx, ecx
  1973.         sub     edi, RAMDISK_FAT
  1974.         shr     edi, 1
  1975.         dec     edi     ; now edi=new cluster
  1976.         test    eax, eax
  1977.         jz      .first_cluster
  1978.         mov     [RAMDISK_FAT+eax*2], di
  1979.         jmp     @f
  1980. .first_cluster:
  1981.         pop     eax     ; eax->direntry
  1982.         push    eax
  1983.         mov     [eax+26], di
  1984. @@:
  1985.         push    edi
  1986.         shl     edi, 9
  1987.         add edi, [_rd_base]
  1988.         add     edi, 31*512
  1989.         xor     eax, eax
  1990.         mov     ecx, 512/4
  1991.         rep     stosd
  1992.         pop     eax     ; eax=new cluster
  1993.         pop     edi     ; edi->direntry
  1994.         pop     ecx     ; ecx=required size
  1995.         add     dword [edi+28], 0x200
  1996.         jmp     .extend_loop
  1997. .extend_done:
  1998.         mov     [edi+28], ecx
  1999.         pop     esi edx
  2000.         xor     eax, eax        ; CF=0
  2001.         ret
  2002. .disk_full:
  2003.         pop     edi ecx
  2004.         pop     esi edx
  2005.         stc
  2006.         push    ERROR_DISK_FULL
  2007.         pop     eax
  2008.         ret
  2009.  
  2010. fat_update_datetime:
  2011.         call    get_time_for_file
  2012.         mov     [edi+22], ax            ; last write time
  2013.         call    get_date_for_file
  2014.         mov     [edi+24], ax            ; last write date
  2015.         mov     [edi+18], ax            ; last access date
  2016.         ret
  2017.  
  2018. ;----------------------------------------------------------------
  2019. ;
  2020. ;  fs_RamdiskSetFileEnd - set end of file on ramdisk
  2021. ;
  2022. ;  esi  points to filename
  2023. ;  ebx  points to 64-bit number = new file size
  2024. ;  ecx  ignored (reserved)
  2025. ;  edx  ignored (reserved)
  2026. ;
  2027. ;  ret eax = 0 ok or other = errormsg
  2028. ;
  2029. ;--------------------------------------------------------------
  2030. fs_RamdiskSetFileEnd:
  2031.         cmp     byte [esi], 0
  2032.         jnz     @f
  2033. .access_denied:
  2034.         push    ERROR_ACCESS_DENIED
  2035.         jmp     .ret
  2036. @@:
  2037.         push    edi
  2038.         call    rd_find_lfn
  2039.         jnc     @f
  2040.         pop     edi
  2041.         push    ERROR_FILE_NOT_FOUND
  2042. .ret:
  2043.         pop     eax
  2044.         ret
  2045. @@:
  2046. ; must not be directory
  2047.         test    byte [edi+11], 10h
  2048.         jz      @f
  2049.         pop     edi
  2050.         jmp     .access_denied
  2051. @@:
  2052. ; file size must not exceed 4Gb
  2053.         cmp     dword [ebx+4], 0
  2054.         jz      @f
  2055.         pop     edi
  2056.         push    ERROR_END_OF_FILE
  2057.         jmp     .ret
  2058. @@:
  2059. ; set file modification date/time to current
  2060.         call    fat_update_datetime
  2061.         mov     eax, [ebx]
  2062.         cmp     eax, [edi+28]
  2063.         jb      .truncate
  2064.         ja      .expand
  2065.         pop     edi
  2066.         xor     eax, eax
  2067.         ret
  2068. .expand:
  2069.         push    ecx
  2070.         mov     ecx, eax
  2071.         call    ramdisk_extend_file
  2072.         pop     ecx
  2073.         pop     edi
  2074.         ret
  2075. .truncate:
  2076.         mov     [edi+28], eax
  2077.         push    ecx
  2078.         movzx   ecx, word [edi+26]
  2079.         test    eax, eax
  2080.         jz      .zero_size
  2081. ; find new last sector
  2082. @@:
  2083.         sub     eax, 0x200
  2084.         jbe     @f
  2085.         movzx   ecx, word [RAMDISK_FAT+ecx*2]
  2086.         jmp     @b
  2087. @@:
  2088. ; zero data at the end of last sector
  2089.         push    ecx
  2090.         mov     edi, ecx
  2091.         shl     edi, 9
  2092.         add edi, [_rd_base]
  2093.         lea     edi, [edi+31*512+eax+0x200]
  2094.         mov     ecx, eax
  2095.         neg     ecx
  2096.         xor     eax, eax
  2097.         rep     stosb
  2098.         pop     ecx
  2099. ; terminate FAT chain
  2100.         lea     ecx, [RAMDISK_FAT+ecx+ecx]
  2101.         push    dword [ecx]
  2102.         mov     word [ecx], 0xFFF
  2103.         pop     ecx
  2104.         and     ecx, 0xFFF
  2105.         jmp     .delete
  2106. .zero_size:
  2107.         and     word [edi+26], 0
  2108. .delete:
  2109. ; delete FAT chain starting with ecx
  2110. ; mark all clusters as free
  2111.         cmp     ecx, 0xFF8
  2112.         jae     .deleted
  2113.         lea     ecx, [RAMDISK_FAT+ecx+ecx]
  2114.         push    dword [ecx]
  2115.         and     word [ecx], 0
  2116.         pop     ecx
  2117.         and     ecx, 0xFFF
  2118.         jmp     .delete
  2119. .deleted:
  2120.         pop     ecx
  2121.         pop     edi
  2122.         xor     eax, eax
  2123.         ret
  2124.  
  2125. fs_RamdiskGetFileInfo:
  2126.         cmp     byte [esi], 0
  2127.         jnz     @f
  2128.         mov     eax, 2  ; unsupported
  2129.         ret
  2130. @@:
  2131.         push    edi
  2132.         call    rd_find_lfn
  2133. fs_GetFileInfo_finish:
  2134.         jnc     @f
  2135.         pop     edi
  2136.         mov     eax, ERROR_FILE_NOT_FOUND
  2137.         ret
  2138. @@:
  2139.         push    esi ebp
  2140.         xor     ebp, ebp
  2141.         mov     esi, edx
  2142.         and     dword [esi+4], 0
  2143.         call    fat_entry_to_bdfe2
  2144.         pop     ebp esi
  2145.         pop     edi
  2146.         xor     eax, eax
  2147.         ret
  2148.  
  2149. fs_RamdiskSetFileInfo:
  2150.         cmp     byte [esi], 0
  2151.         jnz     @f
  2152.         mov     eax, 2  ; unsupported
  2153.         ret
  2154. @@:
  2155.         push    edi
  2156.         call    rd_find_lfn
  2157.         jnc     @f
  2158.         pop     edi
  2159.         mov     eax, ERROR_FILE_NOT_FOUND
  2160.         ret
  2161. @@:
  2162.         call    bdfe_to_fat_entry
  2163.         pop     edi
  2164.         xor     eax, eax
  2165.         ret
  2166.  
  2167. ;----------------------------------------------------------------
  2168. ;
  2169. ;  fs_RamdiskDelete - delete file or empty folder from ramdisk
  2170. ;
  2171. ;  esi  points to filename
  2172. ;
  2173. ;  ret  eax = 0 ok or other = errormsg
  2174. ;
  2175. ;--------------------------------------------------------------
  2176. fs_RamdiskDelete:
  2177.         cmp     byte [esi], 0
  2178.         jnz     @f
  2179. ; cannot delete root!
  2180. .access_denied:
  2181.         push    ERROR_ACCESS_DENIED
  2182. .pop_ret:
  2183.         pop     eax
  2184.         ret
  2185. @@:
  2186.         and     [rd_prev_sector], 0
  2187.         and     [rd_prev_prev_sector], 0
  2188.         push    edi
  2189.         call    rd_find_lfn
  2190.         jnc     .found
  2191.         pop     edi
  2192.         push    ERROR_FILE_NOT_FOUND
  2193.         jmp     .pop_ret
  2194. .found:
  2195.         cmp     dword [edi], '.   '
  2196.         jz      .access_denied2
  2197.         cmp     dword [edi], '..  '
  2198.         jz      .access_denied2
  2199.         test    byte [edi+11], 10h
  2200.         jz      .dodel
  2201. ; we can delete only empty folders!
  2202.         movzx   eax, word [edi+26]
  2203.         push    ebx
  2204.         mov     ebx, eax
  2205.         shl     ebx, 9
  2206.         add ebx, [_rd_base]
  2207.         add     ebx, 31*0x200 + 2*0x20
  2208. .checkempty:
  2209.         cmp     byte [ebx], 0
  2210.         jz      .empty
  2211.         cmp     byte [ebx], 0xE5
  2212.         jnz     .notempty
  2213.         add     ebx, 0x20
  2214.         test    ebx, 0x1FF
  2215.         jnz     .checkempty
  2216.         movzx   eax, word [RAMDISK_FAT + eax*2]
  2217.         test    eax, eax
  2218.         jz      .empty
  2219.         mov     ebx, eax
  2220.         shl     ebx, 9
  2221.         add ebx, [_rd_base]
  2222.         add     ebx, 31*0x200
  2223.         jmp     .checkempty
  2224. .notempty:
  2225.         pop     ebx
  2226. .access_denied2:
  2227.         pop     edi
  2228.         jmp     .access_denied
  2229. .empty:
  2230.         pop     ebx
  2231. .dodel:
  2232.         movzx   eax, word [edi+26]
  2233. ; delete folder entry
  2234.         mov     byte [edi], 0xE5
  2235. ; delete LFN (if present)
  2236. .lfndel:
  2237.         test    edi, 0x1FF
  2238.         jnz     @f
  2239.         cmp     [rd_prev_sector], 0
  2240.         jz      @f
  2241.         cmp     [rd_prev_sector], -1
  2242.         jz      .lfndone
  2243.         mov     edi, [rd_prev_sector]
  2244.         push    [rd_prev_prev_sector]
  2245.         pop     [rd_prev_sector]
  2246.         or      [rd_prev_prev_sector], -1
  2247.         shl     edi, 9
  2248.         add edi, [_rd_base]
  2249.         add     edi, 31*0x200 + 0x200
  2250. @@:
  2251.         sub     edi, 0x20
  2252.         cmp     byte [edi], 0xE5
  2253.         jz      .lfndone
  2254.         cmp     byte [edi+11], 0xF
  2255.         jnz     .lfndone
  2256.         mov     byte [edi], 0xE5
  2257.         jmp     .lfndel
  2258. .lfndone:
  2259. ; delete FAT chain
  2260.         test    eax, eax
  2261.         jz      .done
  2262.         lea     eax, [RAMDISK_FAT + eax*2]
  2263.         push    dword [eax]
  2264.         and     word [eax], 0
  2265.         pop     eax
  2266.         and     eax, 0xFFF
  2267.         jmp     .lfndone
  2268. .done:
  2269.         pop     edi
  2270.         xor     eax, eax
  2271.         ret
  2272.  
  2273. ; \end{diamond}
  2274.