Subversion Repositories Kolibri OS

Rev

Rev 247 | Rev 335 | 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 ramdisk
  1470. ;  fs_RamdiskCreateFolder - create folder on ramdisk
  1471. ;
  1472. ;  esi  points to file/folder name
  1473. ;  ebx  ignored (reserved)
  1474. ;  ecx  number of bytes to write, 0+ (ignored for folders)
  1475. ;  edx  mem location to data (ignored for folders)
  1476. ;
  1477. ;  ret ebx = number of written bytes
  1478. ;      eax = 0 ok read or other = errormsg
  1479. ;
  1480. ;--------------------------------------------------------------
  1481. @@:
  1482.         mov     eax, ERROR_ACCESS_DENIED
  1483.         xor     ebx, ebx
  1484.         ret
  1485.  
  1486. fs_RamdiskCreateFolder:
  1487.         mov     al, 1           ; create folder
  1488.         jmp     fs_RamdiskRewrite.common
  1489.  
  1490. fs_RamdiskRewrite:
  1491.         xor     eax, eax        ; create file
  1492. .common:
  1493.         cmp     byte [esi], 0
  1494.         jz      @b
  1495.         pushad
  1496.         xor     ebp, ebp
  1497.         push    esi
  1498. @@:
  1499.         lodsb
  1500.         test    al, al
  1501.         jz      @f
  1502.         cmp     al, '/'
  1503.         jnz     @b
  1504.         lea     ebp, [esi-1]
  1505.         jmp     @b
  1506. @@:
  1507.         pop     esi
  1508.         test    ebp, ebp
  1509.         jnz     .noroot
  1510.         push    ramdisk_root_extend_dir
  1511.         push    ramdisk_root_next_write
  1512.         push    ebp
  1513.         push    ramdisk_root_first
  1514.         push    ramdisk_root_next
  1515.         jmp     .common1
  1516. .noroot:
  1517. ; check existence
  1518.         mov     byte [ebp], 0
  1519.         call    rd_find_lfn
  1520.         mov     byte [ebp], '/'
  1521.         lea     esi, [ebp+1]
  1522.         jnc     @f
  1523.         mov     eax, ERROR_FILE_NOT_FOUND
  1524. .ret1:
  1525.         mov     [esp+28], eax
  1526.         popad
  1527.         xor     ebx, ebx
  1528.         ret
  1529. @@:
  1530.         test    byte [edi+11], 0x10     ; must be directory
  1531.         mov     eax, ERROR_ACCESS_DENIED
  1532.         jz      .ret1
  1533.         movzx   ebp, word [edi+26]      ; ebp=cluster
  1534.         mov     eax, ERROR_FAT_TABLE
  1535.         cmp     ebp, 2
  1536.         jb      .ret1
  1537.         cmp     ebp, 2849
  1538.         jae     .ret1
  1539.         push    ramdisk_notroot_extend_dir
  1540.         push    ramdisk_notroot_next_write
  1541.         push    ebp
  1542.         push    ramdisk_notroot_first
  1543.         push    ramdisk_notroot_next
  1544. .common1:
  1545.         call    fat_find_lfn
  1546.         jc      .notfound
  1547. ; found
  1548.         test    byte [edi+11], 10h
  1549.         jz      .exists_file
  1550. ; found directory; if we are creating directory, return OK,
  1551. ;                  if we are creating file, say "access denied"
  1552.         add     esp, 20
  1553.         popad
  1554.         test    al, al
  1555.         mov     eax, ERROR_ACCESS_DENIED
  1556.         jz      @f
  1557.         mov     al, 0
  1558. @@:
  1559.         xor     ebx, ebx
  1560.         ret
  1561. .exists_file:
  1562. ; found file; if we are creating directory, return "access denied",
  1563. ;             if we are creating file, delete existing file and continue
  1564.         cmp     byte [esp+20+28], 0
  1565.         jz      @f
  1566.         add     esp, 20
  1567.         popad
  1568.         mov     eax, ERROR_ACCESS_DENIED
  1569.         xor     ebx, ebx
  1570.         ret
  1571. @@:
  1572. ; delete FAT chain
  1573.         push    edi
  1574.         xor     eax, eax
  1575.         mov     dword [edi+28], eax     ; zero size
  1576.         xchg    ax, word [edi+26]       ; start cluster
  1577.         test    eax, eax
  1578.         jz      .done1
  1579. @@:
  1580.         cmp     eax, 0xFF8
  1581.         jae     .done1
  1582.         lea     edi, [RAMDISK_FAT + eax*2] ; position in FAT
  1583.         xor     eax, eax
  1584.         xchg    ax, [edi]
  1585.         jmp     @b
  1586. .done1:
  1587.         pop     edi
  1588.         call    get_time_for_file
  1589.         mov     [edi+22], ax
  1590.         call    get_date_for_file
  1591.         mov     [edi+24], ax
  1592.         mov     [edi+18], ax
  1593.         or      byte [edi+11], 20h      ; set 'archive' attribute
  1594.         jmp     .doit
  1595. .notfound:
  1596. ; file is not found; generate short name
  1597.         call    fat_name_is_legal
  1598.         jc      @f
  1599.         add     esp, 20
  1600.         popad
  1601.         mov     eax, ERROR_FILE_NOT_FOUND
  1602.         xor     ebx, ebx
  1603.         ret
  1604. @@:
  1605.         sub     esp, 12
  1606.         mov     edi, esp
  1607.         call    fat_gen_short_name
  1608. .test_short_name_loop:
  1609.         push    esi edi ecx
  1610.         mov     esi, edi
  1611.         lea     eax, [esp+12+12+8]
  1612.         mov     [eax], ebp
  1613.         call    dword [eax-4]
  1614.         jc      .found
  1615. .test_short_name_entry:
  1616.         cmp     byte [edi+11], 0xF
  1617.         jz      .test_short_name_cont
  1618.         mov     ecx, 11
  1619.         push    esi edi
  1620.         repz    cmpsb
  1621.         pop     edi esi
  1622.         jz      .short_name_found
  1623. .test_short_name_cont:
  1624.         lea     eax, [esp+12+12+8]
  1625.         call    dword [eax-8]
  1626.         jnc     .test_short_name_entry
  1627.         jmp     .found
  1628. .short_name_found:
  1629.         pop     ecx edi esi
  1630.         call    fat_next_short_name
  1631.         jnc     .test_short_name_loop
  1632. .disk_full:
  1633.         add     esp, 12+20
  1634.         popad
  1635.         mov     eax, ERROR_DISK_FULL
  1636.         xor     ebx, ebx
  1637.         ret
  1638. .found:
  1639.         pop     ecx edi esi
  1640. ; now find space in directory
  1641. ; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
  1642.         mov     al, '~'
  1643.         push    ecx edi
  1644.         mov     ecx, 8
  1645.         repnz   scasb
  1646.         push    1
  1647.         pop     eax     ; 1 entry
  1648.         jnz     .notilde
  1649. ; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
  1650.         xor     eax, eax
  1651. @@:
  1652.         cmp     byte [esi], 0
  1653.         jz      @f
  1654.         inc     esi
  1655.         inc     eax
  1656.         jmp     @b
  1657. @@:
  1658.         sub     esi, eax
  1659.         add     eax, 12+13
  1660.         mov     ecx, 13
  1661.         push    edx
  1662.         cdq
  1663.         div     ecx
  1664.         pop     edx
  1665. .notilde:
  1666.         push    -1
  1667.         push    -1
  1668. ; find <eax> successive entries in directory
  1669.         xor     ecx, ecx
  1670.         push    eax
  1671.         lea     eax, [esp+12+8+12+8]
  1672.         mov     [eax], ebp
  1673.         call    dword [eax-4]
  1674.         pop     eax
  1675. .scan_dir:
  1676.         cmp     byte [edi], 0
  1677.         jz      .free
  1678.         cmp     byte [edi], 0xE5
  1679.         jz      .free
  1680.         xor     ecx, ecx
  1681. .scan_cont:
  1682.         push    eax
  1683.         lea     eax, [esp+12+8+12+8]
  1684.         call    dword [eax-8]
  1685.         pop     eax
  1686.         jnc     .scan_dir
  1687.         push    eax
  1688.         lea     eax, [esp+12+8+12+8]
  1689.         call    dword [eax+8]           ; extend directory
  1690.         pop     eax
  1691.         jnc     .scan_dir
  1692.         add     esp, 8+8+12+20
  1693.         popad
  1694.         mov     eax, ERROR_DISK_FULL
  1695.         xor     ebx, ebx
  1696.         ret
  1697. .free:
  1698.         test    ecx, ecx
  1699.         jnz     @f
  1700.         mov     [esp], edi
  1701.         mov     ecx, [esp+8+8+12+8]
  1702.         mov     [esp+4], ecx
  1703.         xor     ecx, ecx
  1704. @@:
  1705.         inc     ecx
  1706.         cmp     ecx, eax
  1707.         jb      .scan_cont
  1708. ; found!
  1709. ; calculate name checksum
  1710.         push    esi ecx
  1711.         mov     esi, [esp+8+8]
  1712.         mov     ecx, 11
  1713.         xor     eax, eax
  1714. @@:
  1715.         ror     al, 1
  1716.         add     al, [esi]
  1717.         inc     esi
  1718.         loop    @b
  1719.         pop     ecx esi
  1720.         pop     edi
  1721.         pop     dword [esp+8+12+8]
  1722. ; edi points to last entry in free chunk
  1723.         dec     ecx
  1724.         jz      .nolfn
  1725.         push    esi
  1726.         push    eax
  1727.         mov     al, 40h
  1728. .writelfn:
  1729.         or      al, cl
  1730.         mov     esi, [esp+4]
  1731.         push    ecx
  1732.         dec     ecx
  1733.         imul    ecx, 13
  1734.         add     esi, ecx
  1735.         stosb
  1736.         mov     cl, 5
  1737.         call    .read_symbols
  1738.         mov     ax, 0xF
  1739.         stosw
  1740.         mov     al, [esp+4]
  1741.         stosb
  1742.         mov     cl, 6
  1743.         call    .read_symbols
  1744.         xor     eax, eax
  1745.         stosw
  1746.         mov     cl, 2
  1747.         call    .read_symbols
  1748.         pop     ecx
  1749.         lea     eax, [esp+8+8+12+8]
  1750.         call    dword [eax+4]   ; next write
  1751.         xor     eax, eax
  1752.         loop    .writelfn
  1753.         pop     eax
  1754.         pop     esi
  1755. .nolfn:
  1756.         xchg    esi, [esp]
  1757.         mov     ecx, 11
  1758.         rep     movsb
  1759.         mov     word [edi], 20h         ; attributes
  1760.         sub     edi, 11
  1761.         pop     esi ecx
  1762.         add     esp, 12
  1763.         mov     byte [edi+13], 0        ; tenths of a second at file creation time
  1764.         call    get_time_for_file
  1765.         mov     [edi+14], ax            ; creation time
  1766.         mov     [edi+22], ax            ; last write time
  1767.         call    get_date_for_file
  1768.         mov     [edi+16], ax            ; creation date
  1769.         mov     [edi+24], ax            ; last write date
  1770.         mov     [edi+18], ax            ; last access date
  1771.         and     word [edi+20], 0        ; high word of cluster
  1772.         and     word [edi+26], 0        ; low word of cluster - to be filled
  1773.         and     dword [edi+28], 0       ; file size - to be filled
  1774.         cmp     byte [esp+20+28], 0
  1775.         jz      .doit
  1776. ; create directory
  1777.         mov     byte [edi+11], 10h         ; attributes: folder
  1778.         mov     ecx, 32*2
  1779.         mov     edx, edi
  1780. .doit:
  1781.         push    edx
  1782.         push    ecx
  1783.         push    edi
  1784.         add     edi, 26         ; edi points to low word of cluster
  1785.         push    edi
  1786.         jecxz   .done
  1787.         mov     ecx, 2849
  1788.         mov     edi, RAMDISK_FAT
  1789. .write_loop:
  1790. ; allocate new cluster
  1791.         xor     eax, eax
  1792.         repnz   scasw
  1793.         jnz     .disk_full2
  1794.         dec     edi
  1795.         dec     edi
  1796.         lea     eax, [edi-(RAMDISK_FAT)]
  1797.         shr     eax, 1                  ; eax = cluster
  1798.         mov     word [edi], 0xFFF       ; mark as last cluster
  1799.         xchg    edi, [esp]
  1800.         stosw
  1801.         pop     edi
  1802.         push    edi
  1803.         inc     ecx
  1804. ; write data
  1805.         cmp     byte [esp+16+20+28], 0
  1806.         jnz     .writedir
  1807.         shl     eax, 9
  1808.         add     eax, RAMDISK+31*512
  1809. .writefile:
  1810.         mov     ebx, edx
  1811.         xchg    eax, ebx
  1812.         push    ecx
  1813.         mov     ecx, 512
  1814.         cmp     dword [esp+12], ecx
  1815.         jae     @f
  1816.         mov     ecx, [esp+12]
  1817. @@:
  1818.         call    memmove
  1819.         add     edx, ecx
  1820.         sub     [esp+12], ecx
  1821.         pop     ecx
  1822.         jnz     .write_loop
  1823. .done:
  1824.         mov     ebx, edx
  1825.         pop     edi edi ecx edx
  1826.         sub     ebx, edx
  1827.         mov     [edi+28], ebx
  1828.         add     esp, 20
  1829.         mov     [esp+16], ebx
  1830.         popad
  1831.         xor     eax, eax
  1832.         ret
  1833. .disk_full2:
  1834.         mov     ebx, edx
  1835.         pop     edi edi ecx edx
  1836.         sub     ebx, edx
  1837.         mov     [edi+28], ebx
  1838.         add     esp, 20
  1839.         mov     [esp+16], ebx
  1840.         popad
  1841.         push    ERROR_DISK_FULL
  1842.         pop     eax
  1843.         ret
  1844. .writedir:
  1845.         mov     edi, eax
  1846.         shl     edi, 9
  1847.         add     edi, RAMDISK+31*512
  1848.         mov     esi, edx
  1849.         mov     ecx, 32/4
  1850.         push    ecx
  1851.         rep     movsd
  1852.         mov     dword [edi-32], '.   '
  1853.         mov     dword [edi-32+4], '    '
  1854.         mov     dword [edi-32+8], '    '
  1855.         mov     byte [edi-32+11], 10h
  1856.         mov     word [edi-32+26], ax
  1857.         mov     esi, edx
  1858.         pop     ecx
  1859.         rep     movsd
  1860.         mov     dword [edi-32], '..  '
  1861.         mov     dword [edi-32+4], '    '
  1862.         mov     dword [edi-32+8], '    '
  1863.         mov     byte [edi-32+11], 10h
  1864.         mov     eax, [esp+16+8]
  1865.         mov     word [edi-32+26], ax
  1866.         pop     edi edi ecx edx
  1867.         add     esp, 20
  1868.         popad
  1869.         xor     eax, eax
  1870.         xor     ebx, ebx
  1871.         ret
  1872.  
  1873. .read_symbol:
  1874.         or      ax, -1
  1875.         test    esi, esi
  1876.         jz      .retFFFF
  1877.         lodsb
  1878.         test    al, al
  1879.         jnz     ansi2uni_char
  1880.         xor     eax, eax
  1881.         xor     esi, esi
  1882. .retFFFF:
  1883.         ret
  1884.  
  1885. .read_symbols:
  1886.         call    .read_symbol
  1887.         stosw
  1888.         loop    .read_symbols
  1889.         ret
  1890.  
  1891. ;----------------------------------------------------------------
  1892. ;
  1893. ;  fs_RamdiskWrite - LFN variant for writing to sys floppy
  1894. ;
  1895. ;  esi  points to filename
  1896. ;  ebx  pointer to 64-bit number = first wanted byte, 0+
  1897. ;       may be ebx=0 - start from first byte
  1898. ;  ecx  number of bytes to write, 0+
  1899. ;  edx  mem location to data
  1900. ;
  1901. ;  ret ebx = bytes written (maybe 0)
  1902. ;      eax = 0 ok write or other = errormsg
  1903. ;
  1904. ;--------------------------------------------------------------
  1905. @@:
  1906.         push    ERROR_ACCESS_DENIED
  1907. fs_RamdiskWrite.ret0:
  1908.         pop     eax
  1909.         xor     ebx, ebx
  1910.         ret
  1911.  
  1912. fs_RamdiskWrite:
  1913.         cmp     byte [esi], 0
  1914.         jz      @b
  1915.         pushad
  1916.         call    rd_find_lfn
  1917.         jnc     .found
  1918.         popad
  1919.         push    ERROR_FILE_NOT_FOUND
  1920.         jmp     .ret0
  1921. .found:
  1922. ; must not be directory
  1923.         test    byte [edi+11], 10h
  1924.         jz      @f
  1925.         popad
  1926.         push    ERROR_ACCESS_DENIED
  1927.         jmp     .ret0
  1928. @@:
  1929. ; FAT does not support files larger than 4GB
  1930.         test    ebx, ebx
  1931.         jz      .l1
  1932.         cmp     dword [ebx+4], 0
  1933.         jz      @f
  1934. .eof:
  1935.         popad
  1936.         push    ERROR_END_OF_FILE
  1937.         jmp     .ret0
  1938. @@:
  1939.         mov     ebx, [ebx]
  1940. .l1:
  1941. ; now edi points to direntry, ebx=start byte to write,
  1942. ; ecx=number of bytes to write, edx=data pointer
  1943.         call    fat_update_datetime
  1944.  
  1945. ; extend file if needed
  1946.         add     ecx, ebx
  1947.         jc      .eof    ; FAT does not support files larger than 4GB
  1948.         push    0       ; return value=0
  1949.         cmp     ecx, [edi+28]
  1950.         jbe     .length_ok
  1951.         cmp     ecx, ebx
  1952.         jz      .length_ok
  1953.         call    ramdisk_extend_file
  1954.         jnc     .length_ok
  1955. ; ramdisk_extend_file can return two error codes: FAT table error or disk full.
  1956. ; First case is fatal error, in second case we may write some data
  1957.         mov     [esp], eax
  1958.         cmp     al, ERROR_DISK_FULL
  1959.         jz      .disk_full
  1960.         pop     eax
  1961.         mov     [esp+28], eax
  1962.         popad
  1963.         xor     ebx, ebx
  1964.         ret
  1965. .disk_full:
  1966. ; correct number of bytes to write
  1967.         mov     ecx, [edi+28]
  1968.         cmp     ecx, ebx
  1969.         ja      .length_ok
  1970. .ret:
  1971.         pop     eax
  1972.         mov     [esp+28], eax   ; eax=return value
  1973.         sub     edx, [esp+20]
  1974.         mov     [esp+16], edx   ; ebx=number of written bytes
  1975.         popad
  1976.         ret
  1977. .length_ok:
  1978. ; now ebx=start pos, ecx=end pos, both lie inside file
  1979.         sub     ecx, ebx
  1980.         jz      .ret
  1981.         movzx   edi, word [edi+26]      ; starting cluster
  1982. .write_loop:
  1983.         sub     ebx, 0x200
  1984.         jae     .next_cluster
  1985.         push    ecx
  1986.         neg     ebx
  1987.         cmp     ecx, ebx
  1988.         jbe     @f
  1989.         mov     ecx, ebx
  1990. @@:
  1991.         mov     eax, edi
  1992.         shl     eax, 9
  1993.         add     eax, 0x100000+31*512+0x200
  1994.         sub     eax, ebx
  1995.         mov     ebx, eax
  1996.         mov     eax, edx
  1997.         call    memmove
  1998.         xor     ebx, ebx
  1999.         add     edx, ecx
  2000.         sub     [esp], ecx
  2001.         pop     ecx
  2002.         jz      .ret
  2003. .next_cluster:
  2004.         movzx   edi, word [edi*2+0x280000]
  2005.         jmp     .write_loop
  2006.  
  2007. ramdisk_extend_file.zero_size:
  2008.         xor     eax, eax
  2009.         jmp     ramdisk_extend_file.start_extend
  2010.  
  2011. ; extends file on ramdisk to given size, new data area is filled by 0
  2012. ; in: edi->direntry, ecx=new size
  2013. ; out: CF=0 => OK, eax=0
  2014. ;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL)
  2015. ramdisk_extend_file:
  2016.         push    ecx
  2017. ; find the last cluster of file
  2018.         movzx   eax, word [edi+26]      ; first cluster
  2019.         mov     ecx, [edi+28]
  2020.         jecxz   .zero_size
  2021. @@:
  2022.         sub     ecx, 0x200
  2023.         jbe     @f
  2024.         mov     eax, [eax*2+0x280000]
  2025.         and     eax, 0xFFF
  2026.         jz      .fat_err
  2027.         cmp     eax, 0xFF8
  2028.         jb      @b
  2029. .fat_err:
  2030.         pop     ecx
  2031.         push    ERROR_FAT_TABLE
  2032.         pop     eax
  2033.         stc
  2034.         ret
  2035. @@:
  2036.         push    eax
  2037.         mov     eax, [eax*2+0x280000]
  2038.         and     eax, 0xFFF
  2039.         cmp     eax, 0xFF8
  2040.         pop     eax
  2041.         jb      .fat_err
  2042. ; set length to full number of sectors and make sure that last sector is zero-padded
  2043.         sub     [edi+28], ecx
  2044.         push    eax edi
  2045.         mov     edi, eax
  2046.         shl     edi, 9
  2047.         lea     edi, [edi+0x100000+31*512+0x200+ecx]
  2048.         neg     ecx
  2049.         xor     eax, eax
  2050.         rep     stosb
  2051.         pop     edi eax
  2052. .start_extend:
  2053.         pop     ecx
  2054. ; now do extend
  2055.         push    edx esi
  2056.         mov     esi, 0x280000+2*2       ; start scan from cluster 2
  2057.         mov     edx, 2847               ; number of clusters to scan
  2058. .extend_loop:
  2059.         cmp     [edi+28], ecx
  2060.         jae     .extend_done
  2061. ; add new sector
  2062.         push    ecx
  2063.         mov     ecx, edx
  2064.         push    edi
  2065.         mov     edi, esi
  2066.         jecxz   .disk_full
  2067.         push    eax
  2068.         xor     eax, eax
  2069.         repnz   scasw
  2070.         pop     eax
  2071.         jnz     .disk_full
  2072.         mov     word [edi-2], 0xFFF
  2073.         mov     esi, edi
  2074.         mov     edx, ecx
  2075.         sub     edi, 0x280000
  2076.         shr     edi, 1
  2077.         dec     edi     ; now edi=new cluster
  2078.         test    eax, eax
  2079.         jz      .first_cluster
  2080.         mov     [0x280000+eax*2], di
  2081.         jmp     @f
  2082. .first_cluster:
  2083.         pop     eax     ; eax->direntry
  2084.         push    eax
  2085.         mov     [eax+26], di
  2086. @@:
  2087.         push    edi
  2088.         shl     edi, 9
  2089.         add     edi, 0x100000+31*512
  2090.         xor     eax, eax
  2091.         mov     ecx, 512/4
  2092.         rep     stosd
  2093.         pop     eax     ; eax=new cluster
  2094.         pop     edi     ; edi->direntry
  2095.         pop     ecx     ; ecx=required size
  2096.         add     dword [edi+28], 0x200
  2097.         jmp     .extend_loop
  2098. .extend_done:
  2099.         mov     [edi+28], ecx
  2100.         pop     esi edx
  2101.         xor     eax, eax        ; CF=0
  2102.         ret
  2103. .disk_full:
  2104.         pop     edi ecx
  2105.         pop     esi edx
  2106.         stc
  2107.         push    ERROR_DISK_FULL
  2108.         pop     eax
  2109.         ret
  2110.  
  2111. fat_update_datetime:
  2112.         call    get_time_for_file
  2113.         mov     [edi+22], ax            ; last write time
  2114.         call    get_date_for_file
  2115.         mov     [edi+24], ax            ; last write date
  2116.         mov     [edi+18], ax            ; last access date
  2117.         ret
  2118.  
  2119. ;----------------------------------------------------------------
  2120. ;
  2121. ;  fs_RamdiskSetFileEnd - set end of file on ramdisk
  2122. ;
  2123. ;  esi  points to filename
  2124. ;  ebx  points to 64-bit number = new file size
  2125. ;  ecx  ignored (reserved)
  2126. ;  edx  ignored (reserved)
  2127. ;
  2128. ;  ret eax = 0 ok or other = errormsg
  2129. ;
  2130. ;--------------------------------------------------------------
  2131. fs_RamdiskSetFileEnd:
  2132.         cmp     byte [esi], 0
  2133.         jnz     @f
  2134. .access_denied:
  2135.         push    ERROR_ACCESS_DENIED
  2136.         jmp     .ret
  2137. @@:
  2138.         push    edi
  2139.         call    rd_find_lfn
  2140.         jnc     @f
  2141.         pop     edi
  2142.         push    ERROR_FILE_NOT_FOUND
  2143. .ret:
  2144.         pop     eax
  2145.         ret
  2146. @@:
  2147. ; must not be directory
  2148.         test    byte [edi+11], 10h
  2149.         jz      @f
  2150.         pop     edi
  2151.         jmp     .access_denied
  2152. @@:
  2153. ; file size must not exceed 4Gb
  2154.         cmp     dword [ebx+4], 0
  2155.         jz      @f
  2156.         pop     edi
  2157.         push    ERROR_END_OF_FILE
  2158.         jmp     .ret
  2159. @@:
  2160. ; set file modification date/time to current
  2161.         call    fat_update_datetime
  2162.         mov     eax, [ebx]
  2163.         cmp     eax, [edi+28]
  2164.         jb      .truncate
  2165.         ja      .expand
  2166.         pop     edi
  2167.         xor     eax, eax
  2168.         ret
  2169. .expand:
  2170.         push    ecx
  2171.         mov     ecx, eax
  2172.         call    ramdisk_extend_file
  2173.         pop     ecx
  2174.         pop     edi
  2175.         ret
  2176. .truncate:
  2177.         mov     [edi+28], eax
  2178.         push    ecx
  2179.         movzx   ecx, word [edi+26]
  2180.         test    eax, eax
  2181.         jz      .zero_size
  2182. ; find new last sector
  2183. @@:
  2184.         sub     eax, 0x200
  2185.         jbe     @f
  2186.         movzx   ecx, word [0x280000+ecx*2]
  2187.         jmp     @b
  2188. @@:
  2189. ; zero data at the end of last sector
  2190.         push    ecx
  2191.         mov     edi, ecx
  2192.         shl     edi, 9
  2193.         lea     edi, [edi+0x100000+31*512+eax+0x200]
  2194.         mov     ecx, eax
  2195.         neg     ecx
  2196.         xor     eax, eax
  2197.         rep     stosb
  2198.         pop     ecx
  2199. ; terminate FAT chain
  2200.         lea     ecx, [0x280000+ecx+ecx]
  2201.         push    dword [ecx]
  2202.         mov     word [ecx], 0xFFF
  2203.         pop     ecx
  2204.         and     ecx, 0xFFF
  2205.         jmp     .delete
  2206. .zero_size:
  2207.         and     word [edi+26], 0
  2208. .delete:
  2209. ; delete FAT chain starting with ecx
  2210. ; mark all clusters as free
  2211.         cmp     ecx, 0xFF8
  2212.         jae     .deleted
  2213.         lea     ecx, [0x280000+ecx+ecx]
  2214.         push    dword [ecx]
  2215.         and     word [ecx], 0
  2216.         pop     ecx
  2217.         and     ecx, 0xFFF
  2218.         jmp     .delete
  2219. .deleted:
  2220.         pop     ecx
  2221.         pop     edi
  2222.         xor     eax, eax
  2223.         ret
  2224.  
  2225. fs_RamdiskGetFileInfo:
  2226.         cmp     byte [esi], 0
  2227.         jnz     @f
  2228.         mov     eax, 2  ; unsupported
  2229.         ret
  2230. @@:
  2231.         push    edi
  2232.         call    rd_find_lfn
  2233. fs_GetFileInfo_finish:
  2234.         jnc     @f
  2235.         pop     edi
  2236.         mov     eax, ERROR_FILE_NOT_FOUND
  2237.         ret
  2238. @@:
  2239.         push    esi ebp
  2240.         xor     ebp, ebp
  2241.         mov     esi, edx
  2242.         and     dword [esi+4], 0
  2243.         call    fat_entry_to_bdfe2
  2244.         pop     ebp esi
  2245.         pop     edi
  2246.         xor     eax, eax
  2247.         ret
  2248.  
  2249. fs_RamdiskSetFileInfo:
  2250.         cmp     byte [esi], 0
  2251.         jnz     @f
  2252.         mov     eax, 2  ; unsupported
  2253.         ret
  2254. @@:
  2255.         push    edi
  2256.         call    rd_find_lfn
  2257.         jnc     @f
  2258.         pop     edi
  2259.         mov     eax, ERROR_FILE_NOT_FOUND
  2260.         ret
  2261. @@:
  2262.         call    bdfe_to_fat_entry
  2263.         pop     edi
  2264.         xor     eax, eax
  2265.         ret
  2266.  
  2267. ;----------------------------------------------------------------
  2268. ;
  2269. ;  fs_RamdiskExecute - LFN variant for executing on sys floppy
  2270. ;
  2271. ;  esi  points to ramdisk filename (e.g. 'launcher')
  2272. ;  ebp  points to full filename (e.g. '/rd/1/launcher')
  2273. ;  dword [ebx] = flags
  2274. ;  dword [ebx+4] = cmdline
  2275. ;
  2276. ;  ret ebx,edx destroyed
  2277. ;      eax > 0 - PID, < 0 - error
  2278. ;
  2279. ;--------------------------------------------------------------
  2280. fs_RamdiskExecute:
  2281.         mov     edx, [ebx]
  2282.         mov     ebx, [ebx+4]
  2283.         test    ebx, ebx
  2284.         jz      @f
  2285.         add     ebx, std_application_base_address
  2286. @@:
  2287.  
  2288. ;----------------------------------------------------------------
  2289. ;
  2290. ; fs_RamdiskExecute.flags - second entry
  2291. ;
  2292. ;  esi  points to ramdisk filename (kernel address)
  2293. ;  ebp  points to full filename
  2294. ;  edx  flags
  2295. ;  ebx  cmdline (kernel address)
  2296. ;
  2297. ;  ret  eax > 0 - PID, < 0 - error
  2298. ;
  2299. ;--------------------------------------------------------------
  2300.  
  2301. .flags:
  2302.         cmp     byte [esi], 0
  2303.         jnz     @f
  2304. ; cannot execute root!
  2305.         mov     eax, -ERROR_ACCESS_DENIED
  2306.         ret
  2307. @@:
  2308.         push    edi
  2309.         call    rd_find_lfn
  2310.         jnc     .found
  2311.         pop     edi
  2312.         mov     eax, -ERROR_FILE_NOT_FOUND
  2313.         ret
  2314. .found:
  2315.         movzx   eax, word [edi+26]      ; cluster
  2316.         push    eax
  2317.         push    dword [edi+28]          ; size
  2318.         push    .DoRead
  2319.         call    fs_execute
  2320.         add     esp, 12
  2321.         pop     edi
  2322.         ret
  2323.  
  2324. .DoRead:
  2325. ; read next block
  2326. ; in: eax->parameters, edi->buffer
  2327. ; out: eax = error code
  2328.         pushad
  2329.         cmp     dword [eax], 0  ; file size
  2330.         jz      .eof
  2331.         mov     edx, [eax+4]    ; cluster
  2332.         lea     esi, [edx+31]
  2333.         shl     esi, 9
  2334.         add     esi, 0x100000
  2335.         mov     ecx, 512/4
  2336.         rep     movsd
  2337.         mov     ecx, [eax]
  2338.         sub     ecx, 512
  2339.         jae     @f
  2340.         add     edi, ecx
  2341.         neg     ecx
  2342.         push    eax
  2343.         xor     eax, eax
  2344.         rep     stosb
  2345.         pop     eax
  2346. @@:
  2347.         mov     [eax], ecx
  2348.         mov     dx, [edx*2+0x280000]
  2349.         mov     [eax+4], dx     ; high word is already zero
  2350.         popad
  2351.         xor     eax, eax
  2352.         ret
  2353. .eof:
  2354.         popad
  2355.         mov     eax, 6
  2356.         ret
  2357.  
  2358. ;----------------------------------------------------------------
  2359. ;
  2360. ;  fs_RamdiskDelete - delete file or empty folder from ramdisk
  2361. ;
  2362. ;  esi  points to filename
  2363. ;
  2364. ;  ret  eax = 0 ok or other = errormsg
  2365. ;
  2366. ;--------------------------------------------------------------
  2367. fs_RamdiskDelete:
  2368.         cmp     byte [esi], 0
  2369.         jnz     @f
  2370. ; cannot delete root!
  2371. .access_denied:
  2372.         push    ERROR_ACCESS_DENIED
  2373. .pop_ret:
  2374.         pop     eax
  2375.         ret
  2376. @@:
  2377.         and     [rd_prev_sector], 0
  2378.         and     [rd_prev_prev_sector], 0
  2379.         push    edi
  2380.         call    rd_find_lfn
  2381.         jnc     .found
  2382.         pop     edi
  2383.         push    ERROR_FILE_NOT_FOUND
  2384.         jmp     .pop_ret
  2385. .found:
  2386.         cmp     dword [edi], '.   '
  2387.         jz      .access_denied2
  2388.         cmp     dword [edi], '..  '
  2389.         jz      .access_denied2
  2390.         test    byte [edi+11], 10h
  2391.         jz      .dodel
  2392. ; we can delete only empty folders!
  2393.         movzx   eax, word [edi+26]
  2394.         push    ebx
  2395.         mov     ebx, eax
  2396.         shl     ebx, 9
  2397.         add     ebx, RAMDISK + 31*0x200 + 2*0x20
  2398. .checkempty:
  2399.         cmp     byte [ebx], 0
  2400.         jz      .empty
  2401.         cmp     byte [ebx], 0xE5
  2402.         jnz     .notempty
  2403.         add     ebx, 0x20
  2404.         test    ebx, 0x1FF
  2405.         jnz     .checkempty
  2406.         movzx   eax, word [RAMDISK_FAT + eax*2]
  2407.         test    eax, eax
  2408.         jz      .empty
  2409.         mov     ebx, eax
  2410.         shl     ebx, 9
  2411.         add     ebx, RAMDISK + 31*0x200
  2412.         jmp     .checkempty
  2413. .notempty:
  2414.         pop     ebx
  2415. .access_denied2:
  2416.         pop     edi
  2417.         jmp     .access_denied
  2418. .empty:
  2419.         pop     ebx
  2420. .dodel:
  2421.         movzx   eax, word [edi+26]
  2422. ; delete folder entry
  2423.         mov     byte [edi], 0xE5
  2424. ; delete LFN (if present)
  2425. .lfndel:
  2426.         test    edi, 0x1FF
  2427.         jnz     @f
  2428.         cmp     [rd_prev_sector], 0
  2429.         jz      @f
  2430.         cmp     [rd_prev_sector], -1
  2431.         jz      .lfndone
  2432.         mov     edi, [rd_prev_sector]
  2433.         push    [rd_prev_prev_sector]
  2434.         pop     [rd_prev_sector]
  2435.         or      [rd_prev_prev_sector], -1
  2436.         shl     edi, 9
  2437.         add     edi, RAMDISK + 31*0x200 + 0x200
  2438. @@:
  2439.         sub     edi, 0x20
  2440.         cmp     byte [edi], 0xE5
  2441.         jz      .lfndone
  2442.         cmp     byte [edi+11], 0xF
  2443.         jnz     .lfndone
  2444.         mov     byte [edi], 0xE5
  2445.         jmp     .lfndel
  2446. .lfndone:
  2447. ; delete FAT chain
  2448.         test    eax, eax
  2449.         jz      .done
  2450.         lea     eax, [RAMDISK_FAT + eax*2]
  2451.         push    dword [eax]
  2452.         and     word [eax], 0
  2453.         pop     eax
  2454.         and     eax, 0xFFF
  2455.         jmp     .lfndone
  2456. .done:
  2457.         pop     edi
  2458.         xor     eax, eax
  2459.         ret
  2460.  
  2461. ; \end{diamond}
  2462.