Subversion Repositories Kolibri OS

Rev

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

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