Subversion Repositories Kolibri OS

Rev

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

  1. $Revision: 442 $
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;                                                              ;;
  4. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  5. ;; Distributed under terms of the GNU General Public License    ;;
  6. ;;                                                              ;;
  7. ;; RAMDISK functions                                            ;;
  8. ;; (C) 2004 Ville Turjanmaa, License: GPL                       ;;
  9. ;; Addings by M.Lisovin                                         ;;
  10. ;; LFN support by diamond                                       ;;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12.  
  13. ; calculate fat chain
  14.  
  15. calculatefatchain:
  16.  
  17.    pushad
  18.  
  19.    mov  esi,RAMDISK+512
  20.    mov  edi,RAMDISK_FAT
  21.  
  22.  fcnew:
  23.    mov  eax,dword [esi]
  24.    mov  ebx,dword [esi+4]
  25.    mov  ecx,dword [esi+8]
  26.    mov  edx,ecx
  27.    shr  edx,4   ;8 ok
  28.    shr  dx,4    ;7 ok
  29.    xor  ch,ch
  30.    shld ecx,ebx,20 ;6 ok
  31.    shr  cx,4     ;5 ok
  32.    shld ebx,eax,12
  33.    and  ebx,0x0fffffff  ;4 ok
  34.    shr  bx,4    ;3 ok
  35.    shl  eax,4
  36.    and  eax,0x0fffffff  ;2 ok
  37.    shr  ax,4  ;1 ok
  38.    mov  dword [edi],eax
  39.    mov  dword [edi+4],ebx
  40.    mov  dword [edi+8],ecx
  41.    mov  dword [edi+12],edx
  42.    add  edi,16
  43.    add  esi,12
  44.  
  45.    cmp  edi,RAMDISK_FAT+2856*2   ;2849 clusters
  46.    jnz  fcnew
  47.  
  48.    popad
  49.    ret
  50.  
  51.  
  52. restorefatchain:   ; restore fat chain
  53.  
  54.    pushad
  55.  
  56.    mov  esi,RAMDISK_FAT
  57.    mov  edi,RAMDISK+512
  58.  
  59.   fcnew2:
  60.    mov  eax,dword [esi]
  61.    mov  ebx,dword [esi+4]
  62.    shl  ax,4
  63.    shl  eax,4
  64.    shl  bx,4
  65.    shr  ebx,4
  66.    shrd eax,ebx,8
  67.    shr  ebx,8
  68.    mov  dword [edi],eax
  69.    mov  word [edi+4],bx
  70.    add  edi,6
  71.    add  esi,8
  72.  
  73.    cmp  edi,RAMDISK+512+4278     ;4274 bytes - all used FAT
  74.    jb   fcnew2
  75.  
  76.    mov  esi,RAMDISK+512           ; duplicate fat chain
  77.    mov  edi,RAMDISK+512+0x1200
  78.    mov  ecx,1069        ;4274/4
  79.    cld
  80.    rep  movsd
  81.  
  82.    popad
  83.    ret
  84.  
  85.  
  86. ramdisk_free_space:
  87. ;---------------------------------------------
  88. ;
  89. ; returns free space in edi
  90. ; rewr.by Mihasik
  91. ;---------------------------------------------
  92.  
  93.         push   eax ebx ecx
  94.  
  95.         mov  edi,RAMDISK_FAT ;start of FAT
  96.         xor  ax,ax    ;Free cluster=0x0000 in FAT
  97.         xor  ebx,ebx  ;counter
  98.         mov  ecx,2849 ;2849 clusters
  99.         cld
  100.     rdfs1:
  101.         repne scasw
  102.         jnz  rdfs2    ;if last cluster not 0
  103.         inc  ebx
  104.         test    ecx, ecx
  105.         jnz     rdfs1
  106.     rdfs2:
  107.         shl  ebx,9    ;free clusters*512
  108.         mov  edi,ebx
  109.  
  110.         pop    ecx ebx eax
  111.         ret
  112.  
  113.  
  114. expand_filename:
  115. ;---------------------------------------------
  116. ;
  117. ; exapand filename with '.' to 11 character
  118. ; eax - pointer to filename
  119. ;---------------------------------------------
  120.  
  121.         push esi edi ebx
  122.  
  123.         mov  edi,esp                  ; check for '.' in the name
  124.         add  edi,12+8
  125.  
  126.         mov  esi,eax
  127.  
  128.         mov  eax,edi
  129.         mov  [eax+0],dword '    '
  130.         mov  [eax+4],dword '    '
  131.         mov  [eax+8],dword '    '
  132.  
  133.       flr1:
  134.  
  135.         cmp  [esi],byte '.'
  136.         jne  flr2
  137.         mov  edi,eax
  138.         add  edi,7
  139.         jmp  flr3
  140.  
  141.       flr2:
  142.  
  143.         mov  bl,[esi]
  144.         mov  [edi],bl
  145.  
  146.       flr3:
  147.  
  148.         inc  esi
  149.         inc  edi
  150.  
  151.         mov  ebx,eax
  152.         add  ebx,11
  153.  
  154.         cmp  edi,ebx
  155.         jbe  flr1
  156.  
  157.         pop  ebx edi esi
  158.         ret
  159.  
  160. fileread:
  161. ;----------------------------------------------------------------
  162. ;
  163. ;  fileread - sys floppy
  164. ;
  165. ;  eax  points to filename 11 chars
  166. ;  ebx  first wanted block       ; 1+ ; if 0 then set to 1
  167. ;  ecx  number of blocks to read ; 1+ ; if 0 then set to 1
  168. ;  edx  mem location to return data
  169. ;  esi  length of filename 12*X 0=root
  170. ;
  171. ;  ret ebx = size or 0xffffffff file not found
  172. ;      eax = 0 ok read or other = errormsg
  173. ;
  174. ;--------------------------------------------------------------
  175.         test   ebx,ebx ;if ebx=0 - set to 1
  176.         jnz    frfl5
  177.         inc    ebx
  178.       frfl5:
  179.         test   ecx,ecx ;if ecx=0 - set to 1
  180.         jnz    frfl6
  181.         inc    ecx
  182.       frfl6:
  183.         test   esi,esi          ; return ramdisk root
  184.         jnz    fr_noroot        ;if not root
  185.         cmp    ebx,14           ;14 clusters=root dir
  186.         ja     oorr
  187.         cmp    ecx,14
  188.         ja     oorr
  189.         jmp    fr_do
  190.       oorr:
  191.         mov    eax,5            ;out of root range (fnf)
  192.         xor    ebx,ebx
  193.         dec    ebx              ;0xffffffff
  194.         ret
  195.  
  196.       fr_do:                    ;reading rootdir
  197.         mov    edi,edx
  198.         dec    ebx
  199.         push   edx
  200.         mov    edx,ecx
  201.         add    edx,ebx
  202.         cmp    edx,15     ;ebx+ecx=14+1
  203.         pushf
  204.         jbe    fr_do1
  205.         sub    edx,14
  206.         sub    ecx,edx
  207.       fr_do1:
  208.         shl    ebx,9
  209.         mov    esi,RAMDISK+512*19
  210.         add    esi,ebx
  211.         shl    ecx,7
  212.         cld
  213.         rep    movsd
  214.         popf
  215.         pop    edx
  216.         jae    fr_do2
  217.         xor    eax,eax ; ok read
  218.         xor    ebx,ebx
  219.         ret
  220.       fr_do2:        ;if last cluster
  221.         mov    eax,6  ;end of file
  222.         xor    ebx,ebx
  223.         ret
  224.  
  225.      fr_noroot:
  226.  
  227.         sub    esp,32
  228.         call   expand_filename
  229.  
  230.         dec    ebx
  231.  
  232.         push   eax
  233.  
  234.         push   eax ebx ecx edx esi edi
  235.         call   rd_findfile
  236.         je     fifound
  237.         add    esp,32+28   ;if file not found
  238.         ret
  239.  
  240.      fifound:
  241.  
  242.         mov    ebx,[edi-11+28]          ;file size
  243.         mov    [esp+20],ebx
  244.         mov    [esp+24],ebx
  245.         add    edi,0xf
  246.         movzx  eax,word [edi]
  247.         mov    edi,eax                  ;edi=cluster
  248.  
  249.       frnew:
  250.  
  251.         add    eax,31                   ;bootsector+2*fat+filenames
  252.         shl    eax,9                    ;*512
  253.         add    eax,RAMDISK             ;image base
  254.         mov    ebx,[esp+8]
  255.         mov    ecx,512                  ;[esp+4]
  256.  
  257.         cmp    [esp+16],dword 0         ; wanted cluster ?
  258.         jne    frfl7
  259.         call   memmove
  260.         add    [esp+8],dword 512
  261.         dec    dword [esp+12]           ; last wanted cluster ?
  262.         je     frnoread
  263.         jmp    frfl8
  264.       frfl7:
  265.         dec    dword [esp+16]
  266.       frfl8:
  267.         movzx  eax,word [edi*2+RAMDISK_FAT]        ; find next cluster from FAT
  268.         mov    edi,eax
  269.         cmp    edi,4095                 ;eof  - cluster
  270.         jz     frnoread2
  271.  
  272.         cmp    [esp+24],dword 512       ;eof  - size
  273.         jb     frnoread
  274.         sub    [esp+24],dword 512
  275.  
  276.         jmp    frnew
  277.  
  278.       frnoread2:
  279.  
  280.         cmp    [esp+16],dword 0         ; eof without read ?
  281.         je     frnoread
  282.  
  283.         pop    edi esi edx ecx
  284.         add    esp,4
  285.         pop    ebx     ; ebx <- eax : size of file
  286.         add    esp,36
  287.         mov    eax,6   ; end of file
  288.         ret
  289.  
  290.       frnoread:
  291.  
  292.         pop    edi esi edx ecx
  293.         add    esp,4
  294.         pop    ebx     ; ebx <- eax : size of file
  295.         add    esp,36
  296.         xor    eax,eax  ;read ok
  297.         ret
  298.  
  299. filedelete:
  300. ;--------------------------------------------
  301. ;
  302. ; filedelete - sys floppy
  303. ; in:
  304. ; eax -  pointer to filename 11 chars
  305. ;
  306. ; out:
  307. ; eax - 0 = successful, 5 = file not found
  308. ;
  309. ;--------------------------------------------
  310.  
  311.         sub    esp,32
  312.         call   expand_filename
  313.  
  314.         push   eax ebx ecx edx esi edi
  315.  
  316.         call   rd_findfile
  317.         je     fifoundd
  318.         pop    edi esi edx ecx ebx eax ;file not found
  319.         add    esp,32
  320.         mov    eax,5
  321.         ret
  322.  
  323.      fifoundd:
  324.  
  325.         mov    [edi-11],byte 0xE5       ;mark filename deleted
  326.         add    edi,0xf
  327.         movzx  eax,word [edi]
  328.         mov    edi,eax                  ;edi = cluster
  329.  
  330.       frnewd:
  331.  
  332.         shl    edi,1                    ;find next cluster from FAT
  333.         add    edi,RAMDISK_FAT
  334.         movzx  eax,word [edi]
  335.         mov    [edi],word 0x0           ;clear fat chain cluster
  336.         mov    edi,eax
  337.         cmp    edi,dword 0xff8          ;last cluster ?
  338.         jb     frnewd
  339.  
  340.         pop    edi esi edx ecx ebx eax
  341.         add    esp,32
  342.         xor    eax,eax       ; file found
  343.         ret
  344.  
  345.  
  346.  
  347. filesave:
  348. ;----------------------------------------------------------
  349. ;
  350. ; filesave - sys floppy
  351. ;
  352. ; eax points to filename 11 chars
  353. ;
  354. ;        eax      ; pointer to file name
  355. ;        ebx      ; buffer
  356. ;        ecx      ; count to write in bytes
  357. ;        edx      ; 0 create new , 1 append
  358. ;
  359. ;-----------------------------------------------------------
  360.  
  361.         sub  esp,32
  362.         call expand_filename
  363.         test edx,edx
  364.         jnz  fsdel
  365.         pusha
  366.         call filedelete
  367.         popa
  368.  
  369.       fsdel:
  370.  
  371.         call   ramdisk_free_space
  372.         cmp    ecx,edi
  373.         jbe    rd_do_save
  374.         add    esp,32
  375.         mov    eax,8    ;disk full
  376.         ret
  377.  
  378.       rd_do_save:
  379.  
  380.         push   eax ebx ecx edx esi edi
  381.  
  382.         mov    edi,RAMDISK+512*18+512  ;Point at directory
  383.         mov    edx,224 +1
  384.         ; find an empty spot for filename in the root dir
  385.      l20ds:
  386.         dec    edx
  387.         jz     frnoreadds
  388.      l21ds:
  389.         cmp    [edi],byte 0xE5
  390.         jz     fifoundds
  391.         cmp    [edi],byte 0x0
  392.         jz     fifoundds
  393.         add    edi,32                   ; Advance to next entry
  394.         jmp    l20ds
  395.      fifoundds:
  396.  
  397.         push   edi                      ; move the filename to root dir
  398.         mov    esi,[esp+4+20]
  399.         mov    ecx,11
  400.         cld
  401.         rep    movsb
  402.         pop    edi
  403.         mov    edx,edi
  404.         add    edx,11+0xf               ; edx <- cluster save position
  405.         mov    ebx,[esp+12]             ; save file size
  406.         mov    [edi+28],ebx
  407.         mov    [edi+11],byte 0x20       ; attribute
  408. ; Ivan Poddubny 11/12/2003:
  409. call get_date_for_file   ; from FAT32.INC
  410. mov [edi+24],ax          ; date
  411. call get_time_for_file   ; from FAT32.INC
  412. mov [edi+22],ax          ; time
  413. ; End
  414.         mov    edi,RAMDISK_FAT            ;pointer to first cluster
  415.         mov    ecx,2849
  416.         cld
  417.       frnewds:
  418.         xor    ax,ax
  419.         repne  scasw
  420.         mov    ebx,2848
  421.         sub    ebx,ecx
  422.         mov    [edx],bx                 ; save next cluster pos. to prev cl.
  423.         mov    edx,edi                  ; next save pos abs mem add
  424.         dec    edx
  425.         dec    edx
  426.         call   fdc_filesave
  427.         pusha                           ; move save to floppy cluster
  428.         add    ebx,31
  429.         shl    ebx,9
  430.         add    ebx,RAMDISK
  431.         mov    eax,[esp+32+16]
  432.         mov    ecx,512
  433.         call   memmove
  434.         popa
  435.  
  436.         mov    eax,[esp+12]
  437.         cmp    eax,512
  438.         jbe    flnsa
  439.         sub    eax,512
  440.         mov    [esp+12],eax
  441.         add     dword [esp+16], 512
  442.         jmp    frnewds
  443.  
  444.      flnsa:
  445.         mov    [edi-2],word 4095          ; mark end of file - last cluster
  446.  
  447.       frnoreadds:
  448.  
  449.         pop    edi esi edx ecx ebx eax
  450.         add    esp,32
  451.  
  452. ;        pusha
  453. ;        cli
  454. ;        call   fdc_commitfile
  455. ;        sti
  456. ;        popa
  457.  
  458.         xor    eax,eax ;ok write
  459.         ret
  460.  
  461.    rd_findfile:
  462.    ;by Mihasik
  463.    ;IN: eax - pointer to filename OUT: filestring+11 in edi or notZero in flags and fnf in eax,ebx
  464.  
  465.         mov    edi,RAMDISK+512*18+512  ;Point at directory
  466.         cld
  467.     rd_newsearch:
  468.         mov    esi,eax
  469.         mov    ecx,11
  470.         rep    cmpsb
  471.         je     rd_ff
  472.         add    cl,21
  473.         add    edi,ecx
  474.         cmp    edi,RAMDISK+512*33
  475.         jb     rd_newsearch
  476.         mov    eax,5      ;if file not found - eax=5
  477.         xor    ebx,ebx
  478.         dec    ebx    ;ebx=0xffffffff and zf=0
  479.      rd_ff:
  480.         ret
  481.  
  482. ; \begin{diamond}
  483.  
  484. uni2ansi_str:
  485. ; convert UNICODE zero-terminated string to ASCII-string (codepage 866)
  486. ; in: esi->source, edi->buffer (may be esi=edi)
  487. ; destroys: eax,esi,edi
  488.         lodsw
  489.         test    ax, ax
  490.         jz      .done
  491.         cmp     ax, 0x80
  492.         jb      .ascii
  493.         cmp     ax, 0x401
  494.         jz      .yo1
  495.         cmp     ax, 0x451
  496.         jz      .yo2
  497.         cmp     ax, 0x410
  498.         jb      .unk
  499.         cmp     ax, 0x440
  500.         jb      .rus1
  501.         cmp     ax, 0x450
  502.         jb      .rus2
  503. .unk:
  504.         mov     al, '_'
  505.         jmp     .doit
  506. .yo1:
  507.         mov     al, 'ð'
  508.         jmp     .doit
  509. .yo2:
  510.         mov     al, 'ñ'
  511.         jmp     .doit
  512. .rus1:
  513. ; 0x410-0x43F -> 0x80-0xAF
  514.         add     al, 0x70
  515.         jmp     .doit
  516. .rus2:
  517. ; 0x440-0x44F -> 0xE0-0xEF
  518.         add     al, 0xA0
  519. .ascii:
  520. .doit:
  521.         stosb
  522.         jmp     uni2ansi_str
  523. .done:
  524.         mov     byte [edi], 0
  525.         ret
  526.  
  527. ansi2uni_char:
  528. ; convert ANSI character in al to UNICODE character in ax, using cp866 encoding
  529.         mov     ah, 0
  530. ; 0x00-0x7F - trivial map
  531.         cmp     al, 0x80
  532.         jb      .ret
  533. ; 0x80-0xAF -> 0x410-0x43F
  534.         cmp     al, 0xB0
  535.         jae     @f
  536.         add     ax, 0x410-0x80
  537. .ret:
  538.         ret
  539. @@:
  540. ; 0xE0-0xEF -> 0x440-0x44F
  541.         cmp     al, 0xE0
  542.         jb      .unk
  543.         cmp     al, 0xF0
  544.         jae     @f
  545.         add     ax, 0x440-0xE0
  546.         ret
  547. ; 0xF0 -> 0x401
  548. ; 0xF1 -> 0x451
  549. @@:
  550.         cmp     al, 'ð'
  551.         jz      .yo1
  552.         cmp     al, 'ñ'
  553.         jz      .yo2
  554. .unk:
  555.         mov     al, '_'         ; ah=0
  556.         ret
  557. .yo1: