Subversion Repositories Kolibri OS

Rev

Rev 1198 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  FAT32.INC                                                      ;;
7
;;                                                                 ;;
8
;;  FAT16/32 functions for KolibriOS                               ;;
9
;;                                                                 ;;
10
;;  Copyright 2002 Paolo Minazzi, paolo.minazzi@inwind.it          ;;
11
;;                                                                 ;;
12
;;  See file COPYING for details                                   ;;
13
;;  04.02.2007 LFN create folder - diamond                         ;;
14
;;  08.10.2006 LFN delete file/folder - diamond                    ;;
15
;;  20.08.2006 LFN set file size (truncate/extend) - diamond       ;;
16
;;  17.08.2006 LFN write/append to file - diamond                  ;;
17
;;  23.06.2006 LFN start application - diamond                     ;;
18
;;  15.06.2006 LFN get/set file/folder info - diamond              ;;
19
;;  27.05.2006 LFN create/rewrite file - diamond                   ;;
20
;;  04.05.2006 LFN read folder - diamond                           ;;
21
;;  29.04.2006 Elimination of hangup after the                     ;;
22
;;             expiration hd_wait_timeout -  Mario79               ;;
23
;;  23.04.2006 LFN read file - diamond                             ;;
24
;;  28.01.2006 find all Fat16/32 partition in all input point      ;;
25
;;             to MBR, see file part_set.inc - Mario79             ;;
26
;;  15.01.2005 get file size/attr/date, file_append - ATV          ;;
27
;;  04.12.2004 skip volume label, file delete bug fixed - ATV      ;;
28
;;  29.11.2004 get_free_FAT changed, append dir bug fixed - ATV    ;;
29
;;  23.11.2004 don't allow overwrite dir with file - ATV           ;;
30
;;  18.11.2004 get_disk_info and more error codes - ATV            ;;
31
;;  17.11.2004 set_FAT/get_FAT and disk cache rewritten - ATV      ;;
32
;;  10.11.2004 removedir clear whole directory structure - ATV     ;;
33
;;  08.11.2004 rename - ATV                                        ;;
34
;;  30.10.2004 file_read return also dirsize in bytes - ATV        ;;
35
;;  20.10.2004 Makedir/Removedir - ATV                             ;;
36
;;  14.10.2004 Partition chain/Fat16 - ATV (thanks drh3xx)         ;;
37
;;  06.9.2004  Fix free space by Mario79 added - MH                ;;
38
;;  24.5.2004  Write back buffer for File_write -VT                ;;
39
;;  20.5.2004  File_read function to work with syscall 58 - VT     ;;
40
;;  30.3.2004  Error parameters at function return - VT            ;;
41
;;  01.5.2002  Bugfix in device write - VT                         ;;
42
;;  20.5.2002  Hd status check - VT                                ;;
43
;;  29.6.2002  Improved fat32 verification - VT                    ;;
44
;;                                                                 ;;
45
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
 
1206 hidnplayr 47
$Revision: 1206 $
1159 hidnplayr 48
 
49
 
50
cache_max equ 1919      ; max. is 1919*512+0x610000=0x6ffe00
51
 
52
ERROR_SUCCESS        = 0
53
ERROR_DISK_BASE      = 1
54
ERROR_UNSUPPORTED_FS = 2
55
ERROR_UNKNOWN_FS     = 3
56
ERROR_PARTITION      = 4
57
ERROR_FILE_NOT_FOUND = 5
58
ERROR_END_OF_FILE    = 6
59
ERROR_MEMORY_POINTER = 7
60
ERROR_DISK_FULL      = 8
61
ERROR_FAT_TABLE      = 9
62
ERROR_ACCESS_DENIED  = 10
63
 
64
PUSHAD_EAX equ [esp+28]
65
PUSHAD_ECX equ [esp+24]
66
PUSHAD_EDX equ [esp+20]
67
PUSHAD_EBX equ [esp+16]
68
PUSHAD_EBP equ [esp+8]
69
PUSHAD_ESI equ [esp+4]
70
PUSHAD_EDI equ [esp+0]
71
 
72
uglobal
73
align 4
74
partition_count      dd 0       ; partitions found by set_FAT32_variables
75
longname_sec1        dd 0       ; used by analyze_directory to save 2 previous
76
longname_sec2        dd 0       ; directory sectors for delete long filename
77
 
78
hd_error             dd 0       ; set by wait_for_sector_buffer
79
hd_setup             dd 0
80
hd_wait_timeout      dd 0
81
 
82
cluster_tmp          dd 0       ; used by analyze_directory
83
                                ; and analyze_directory_to_write
84
 
85
file_size            dd 0       ; used by file_read
86
 
87
cache_search_start   dd 0       ; used by find_empty_slot
88
endg
89
 
90
iglobal
91
fat_in_cache         dd -1
92
endg
93
 
94
uglobal
95
align 4
96
fat_cache:           times 512 db 0
97
 Sector512:                      ; label for dev_hdcd.inc
98
  buffer:              times 512 db 0
99
  fsinfo_buffer:       times 512 db 0
100
endg
101
 
102
uglobal
103
  fat16_root           db 0       ; flag for fat16 rootdir
104
  fat_change           db 0       ; 1=fat has changed
105
endg
106
 
107
reserve_hd1:
108
 
109
    cli
110
    cmp   [hd1_status],0
111
    je    reserve_ok1
112
 
113
    sti
114
    call  change_task
115
    jmp   reserve_hd1
116
 
117
  reserve_ok1:
118
 
119
    push  eax
120
    mov   eax,[CURRENT_TASK]
121
    shl   eax,5
122
    mov   eax,[eax+CURRENT_TASK+TASKDATA.pid]
123
    mov   [hd1_status],eax
124
    pop   eax
125
    sti
126
    ret
127
;********************************************
128
 
129
uglobal
130
hd_in_cache db ?
131
endg
132
 
133
reserve_hd_channel:
134
; BIOS disk accesses are protected with common mutex hd1_status
135
; This must be modified when hd1_status will not be valid!
136
        cmp     [hdpos], 0x80
137
        jae     .ret
138
    cmp   [hdbase], 0x1F0
139
    jne   .IDE_Channel_2
140
.IDE_Channel_1:
141
    cli
142
    cmp   [IDE_Channel_1],0
143
    je    .reserve_ok_1
144
    sti
145
    call  change_task
146
    jmp   .IDE_Channel_1
147
.IDE_Channel_2:
148
    cli
149
    cmp   [IDE_Channel_2],0
150
    je    .reserve_ok_2
151
    sti
152
    call  change_task
153
    jmp   .IDE_Channel_2
154
.reserve_ok_1:
155
        mov     [IDE_Channel_1], 1
156
        push    eax
157
        mov     al, 1
158
        jmp     @f
159
.reserve_ok_2:
160
        mov     [IDE_Channel_2], 1
161
        push    eax
162
        mov     al, 3
163
@@:
164
        cmp     [hdid], 1
165
        sbb     al, -1
166
        cmp     al, [hd_in_cache]
167
        jz      @f
168
        mov     [hd_in_cache], al
169
        call    clear_hd_cache
170
@@:
171
	pop     eax
172
.ret:
173
        ret
174
 
175
free_hd_channel:
176
; see comment at reserve_hd_channel
177
        cmp     [hdpos], 0x80
178
        jae     .ret
179
    cmp   [hdbase], 0x1F0
180
    jne   .IDE_Channel_2
181
.IDE_Channel_1:
182
    mov [IDE_Channel_1],0
183
.ret:
184
    ret
185
.IDE_Channel_2:
186
    mov [IDE_Channel_2],0
187
    ret
188
;********************************************
189
problem_partition db 0  ; used for partitions search
190
 
191
include  'part_set.inc'
192
 
193
set_FAT:
194
;--------------------------------
195
; input  : EAX = cluster
196
;          EDX = value to save
197
; output : EDX = old value
198
;--------------------------------
199
    push  eax ebx esi
200
 
201
    cmp   eax,2
202
    jb    sfc_error
203
    cmp   eax,[LAST_CLUSTER]
204
    ja    sfc_error
205
    cmp   [fs_type],16
206
    je    sfc_1
207
    add   eax,eax
208
  sfc_1:
209
    add   eax,eax
210
    mov   esi,511
211
    and   esi,eax               ; esi = position in fat sector
212
    shr   eax,9                 ; eax = fat sector
213
    add   eax,[FAT_START]
214
    mov   ebx,fat_cache
215
 
216
    cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
217
    je    sfc_in_cache          ; yes
218
 
219
    cmp   [fat_change],0        ; is fat changed?
220
    je    sfc_no_change         ; no
221
    call  write_fat_sector      ; yes. write it into disk
222
    cmp   [hd_error],0
223
    jne   sfc_error
224
 
225
  sfc_no_change:
226
    mov   [fat_in_cache],eax    ; save fat sector
227
    call  hd_read
228
    cmp  [hd_error],0
229
    jne  sfc_error
230
 
231
 
232
  sfc_in_cache:
233
    cmp   [fs_type],16
234
    jne   sfc_test32
235
 
236
  sfc_set16:
237
    xchg  [ebx+esi],dx          ; save new value and get old value
238
    jmp   sfc_write
239
 
240
  sfc_test32:
241
    mov   eax,[fatMASK]
242
 
243
  sfc_set32:
244
    and   edx,eax
245
    xor   eax,-1                ; mask for high bits
246
    and   eax,[ebx+esi]         ; get high 4 bits
247
    or    eax,edx
248
    mov   edx,[ebx+esi]         ; get old value
249
    mov   [ebx+esi],eax         ; save new value
250
 
251
  sfc_write:
252
    mov   [fat_change],1        ; fat has changed
253
 
254
  sfc_nonzero:
255
    and   edx,[fatMASK]
256
 
257
  sfc_error:
258
    pop   esi ebx eax
259
    ret
260
 
261
 
262
get_FAT:
263
;--------------------------------
264
; input  : EAX = cluster
265
; output : EAX = next cluster
266
;--------------------------------
267
    push  ebx esi
268
 
269
    cmp   [fs_type],16
270
    je    gfc_1
271
    add   eax,eax
272
  gfc_1:
273
    add   eax,eax
274
    mov   esi,511
275
    and   esi,eax               ; esi = position in fat sector
276
    shr   eax,9                 ; eax = fat sector
277
    add   eax,[FAT_START]
278
    mov   ebx,fat_cache
279
 
280
    cmp   eax,[fat_in_cache]    ; is fat sector already in memory?
281
    je    gfc_in_cache
282
 
283
    cmp   [fat_change],0        ; is fat changed?
284
    je    gfc_no_change         ; no
285
    call  write_fat_sector      ; yes. write it into disk
286
    cmp  [hd_error],0
287
    jne  hd_error_01
288
 
289
  gfc_no_change:
290
    mov   [fat_in_cache],eax
291
    call  hd_read
292
    cmp  [hd_error],0
293
    jne  hd_error_01
294
 
295
  gfc_in_cache:
296
    mov   eax,[ebx+esi]
297
    and   eax,[fatMASK]
298
 hd_error_01:
299
    pop   esi ebx
300
    ret
301
 
302
 
303
get_free_FAT:
304
;-----------------------------------------------------------
305
; output : if CARRY=0 EAX = # first cluster found free
306
;          if CARRY=1 disk full
307
; Note   : for more speed need to use fat_cache directly
308
;-----------------------------------------------------------
309
    push  ecx
310
    mov   ecx,[LAST_CLUSTER]    ; counter for full disk
311
    sub   ecx,2
312
    mov   eax,[fatStartScan]
313
    cmp   eax,2
314
    jb    gff_reset
315
 
316
  gff_test:
317
    cmp   eax,[LAST_CLUSTER]    ; if above last cluster start at cluster 2
318
    jbe   gff_in_range
319
  gff_reset:
320
    mov   eax,2
321
 
322
  gff_in_range:
323
    push  eax
324
    call  get_FAT               ; get cluster state
325
    cmp   [hd_error],0
326
    jne   gff_not_found_1
327
 
328
    test  eax,eax               ; is it free?
329
    pop   eax
330
    je    gff_found             ; yes
331
    inc   eax                   ; next cluster
332
    dec   ecx                   ; is all checked?
333
    jns   gff_test              ; no
334
 
335
  gff_not_found_1:
336
    add   esp,4
337
  gff_not_found:
338
    pop   ecx                   ; yes. disk is full
339
    stc
340
    ret
341
 
342
  gff_found:
343
    lea   ecx,[eax+1]
344
    mov   [fatStartScan],ecx
345
    pop   ecx
346
    clc
347
    ret
348
 
349
 
350
write_fat_sector:
351
;-----------------------------------------------------------
352
; write changed fat to disk
353
;-----------------------------------------------------------
354
    push  eax ebx ecx
355
 
356
    mov   [fat_change],0
357
    mov   eax,[fat_in_cache]
358
    cmp   eax,-1
359
    jz    write_fat_not_used
360
    mov   ebx,fat_cache
361
    mov   ecx,[NUMBER_OF_FATS]
362
 
363
  write_next_fat:
364
    call  hd_write
365
    cmp   [hd_error],0
366
    jne   write_fat_not_used
367
 
368
    add   eax,[SECTORS_PER_FAT]
369
    dec   ecx
370
    jnz   write_next_fat
371
 
372
  write_fat_not_used:
373
    pop   ecx ebx eax
374
    ret
375
 
376
 
377
analyze_directory:
378
;-----------------------------------------------------------
379
; input  : EAX = first cluster of the directory
380
;          EBX = pointer to filename
381
; output : IF CARRY=0 EAX = sector where th file is found
382
;                     EBX = pointer in buffer
383
;                     [buffer .. buffer+511]
384
;                     ECX,EDX,ESI,EDI not changed
385
;          IF CARRY=1 filename not found
386
; Note   : if cluster=0 it's changed to read rootdir
387
;          save 2 previous directory sectors in longname_sec
388
;-----------------------------------------------------------
389
    push  ecx edx esi edi ebx   ; ebx = [esp+0]
390
    mov   [longname_sec1],0
391
    mov   [longname_sec2],0
392
 
393
  adr_new_cluster:
394
    mov   [cluster_tmp],eax
395
    mov   [fat16_root],0
396
    cmp   eax,[LAST_CLUSTER]
397
    ja    adr_not_found         ; too big cluster number, something is wrong
398
    cmp   eax,2
399
    jnb   adr_data_cluster
400
 
401
    mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
402
    cmp   [fs_type],16
403
    jne   adr_data_cluster
404
    mov   eax,[ROOT_START]
405
    mov   edx,[ROOT_SECTORS]
406
    mov   [fat16_root],1        ; flag for fat16 rootdir
407
    jmp   adr_new_sector
408
 
409
  adr_data_cluster:
410
    sub   eax,2
411
    mov   edx,[SECTORS_PER_CLUSTER]
412
    imul  eax,edx
413
    add   eax,[DATA_START]
414
 
415
  adr_new_sector:
416
    mov   ebx,buffer
417
    call  hd_read
418
    cmp  [hd_error],0
419
    jne  adr_not_found
420
 
421
    mov   ecx,512/32            ; count of dir entrys per sector = 16
422
 
423
  adr_analyze:
424
    mov   edi,[ebx+11]          ; file attribute
425
    and   edi,0xf
426
    cmp   edi,0xf
427
    je    adr_long_filename
428
    test  edi,0x8               ; skip over volume label
429
    jne   adr_long_filename     ; Note: label can be same name as file/dir
430
 
431
    mov   esi,[esp+0]           ; filename need to be uppercase
432
    mov   edi,ebx
433
    push  ecx
434
    mov   ecx,11
435
    cld
436
    rep   cmpsb                 ; compare 8+3 filename
437
    pop   ecx
438
    je    adr_found
439
 
440
  adr_long_filename:
441
    add   ebx,32                ; position of next dir entry
442
    dec   ecx
443
    jnz   adr_analyze
444
 
445
    mov   ecx,[longname_sec1]   ; save 2 previous directory sectors
446
    mov   [longname_sec1],eax   ; for delete long filename
447
    mov   [longname_sec2],ecx
448
    inc   eax                   ; next sector
449
    dec   edx
450
    jne   adr_new_sector
451
    cmp   [fat16_root],1        ; end of fat16 rootdir
452
    je    adr_not_found
453
 
454
  adr_next_cluster:
455
    mov   eax,[cluster_tmp]
456
    call  get_FAT               ; get next cluster
457
    cmp  [hd_error],0
458
    jne  adr_not_found
459
 
460
    cmp   eax,2                 ; incorrect fat chain?
461
    jb    adr_not_found         ; yes
462
    cmp   eax,[fatRESERVED]     ; is it end of directory?
463
    jb    adr_new_cluster       ; no. analyse it
464
 
465
  adr_not_found:
466
    pop   edi edi esi edx ecx   ; first edi will remove ebx
467
    stc                         ; file not found
468
    ret
469
 
470
  adr_found:
471
    pop   edi edi esi edx ecx   ; first edi will remove ebx
472
    clc                         ; file found
473
    ret
474
 
475
 
476
get_data_cluster:
477
;-----------------------------------------------------------
478
; input  : EAX = cluster
479
;          EBX = pointer to buffer
480
;          EDX = # blocks to read in buffer
481
;          ESI = # blocks to skip over
482
; output : if CARRY=0 ok EBX/EDX/ESI updated
483
;          if CARRY=1 cluster out of range
484
; Note   : if cluster=0 it's changed to read rootdir
485
;-----------------------------------------------------------
486
    push  eax ecx
487
 
488
    mov   [fat16_root],0
489
    cmp   eax,[LAST_CLUSTER]
490
    ja    gdc_error             ; too big cluster number, something is wrong
491
    cmp   eax,2
492
    jnb   gdc_cluster
493
 
494
    mov   eax,[ROOT_CLUSTER]    ; if cluster < 2 then read rootdir
495
    cmp   [fs_type],16
496
    jne   gdc_cluster
497
    mov   eax,[ROOT_START]
498
    mov   ecx,[ROOT_SECTORS]    ; Note: not cluster size
499
    mov   [fat16_root],1        ; flag for fat16 rootdir
500
    jmp   gdc_read
501
 
502
  gdc_cluster:
503
    sub   eax,2
504
    mov   ecx,[SECTORS_PER_CLUSTER]
505
    imul  eax,ecx
506
    add   eax,[DATA_START]
507
 
508
  gdc_read:
509
    test  esi,esi               ; first wanted block
510
    je    gdcl1                 ; yes, skip count is 0
511
    dec   esi
512
    jmp   gdcl2
513
 
514
  gdcl1:
515
    call  hd_read
516
    cmp  [hd_error],0
517
    jne  gdc_error
518
 
519
    add   ebx,512               ; update pointer
520
    dec   edx
521
 
522
  gdcl2:
523
    test  edx,edx               ; is all read?
524
    je    out_of_read
525
 
526
    inc   eax                   ; next sector
527
    dec   ecx
528
    jnz   gdc_read
529
 
530
  out_of_read:
531
    pop   ecx eax
532
    clc
533
    ret
534
 
535
  gdc_error:
536
    pop   ecx eax
537
    stc
538
    ret
539
 
540
 
541
get_cluster_of_a_path:
542
;---------------------------------------------------------
543
; input  : EBX = pointer to a path string
544
;          (example: the path "/files/data/document" become
545
;                             "files......data.......document...0"
546
;          '.' = space char
547
;          '0' = char(0) (ASCII=0) !!! )
548
; output : if (CARRY=1) -> ERROR in the PATH
549
;          if (CARRY=0) -> EAX=cluster
550
;---------------------------------------------------------
551
    push  ebx edx
552
 
553
    mov   eax,[ROOT_CLUSTER]
554
    mov   edx,ebx
555
 
556
search_end_of_path:
557
    cmp   byte [edx],0
558
    je    found_end_of_path
559
 
560
    inc   edx ; '/'
561
    mov   ebx,edx
562
    call  analyze_directory
563
    jc    directory_not_found
564
 
565
    mov   eax,[ebx+20-2]        ; read the HIGH 16bit cluster field
566
    mov   ax,[ebx+26]           ; read the LOW 16bit cluster field
567
    and   eax,[fatMASK]
568
    add   edx,11                ; 8+3 (name+extension)
569
    jmp   search_end_of_path
570
 
571
found_end_of_path:
572
    pop   edx ebx
573
    clc                         ; no errors
574
    ret
575
 
576
directory_not_found:
577
    pop   edx ebx
578
    stc                         ; errors occour
579
    ret
580
 
581
 
582
bcd2bin:
583
;----------------------------------
584
; input  : AL=BCD number (eg. 0x11)
585
; output : AH=0
586
;          AL=decimal number (eg. 11)
587
;----------------------------------
588
    xor   ah,ah
589
    shl   ax,4
590
    shr   al,4
591
    aad
592
    ret
593
 
594
 
595
get_date_for_file:
596
;-----------------------------------------------------
597
; Get date from CMOS and pack day,month,year in AX
598
; DATE   bits  0..4   : day of month 0..31
599
;              5..8   : month of year 1..12
600
;              9..15  : count of years from 1980
601
;-----------------------------------------------------
602
    mov   al,0x7        ;day
603
    out   0x70,al
604
    in    al,0x71
605
    call  bcd2bin
606
    ror   eax,5
607
 
608
    mov   al,0x8        ;month
609
    out   0x70,al
610
    in    al,0x71
611
    call  bcd2bin
612
    ror   eax,4
613
 
614
    mov   al,0x9        ;year
615
    out   0x70,al
616
    in    al,0x71
617
    call  bcd2bin
618
    add   ax,20         ;because CMOS return only the two last
619
                        ;digit (eg. 2000 -> 00 , 2001 -> 01) and we
620
    rol   eax,9         ;need the difference with 1980 (eg. 2001-1980)
621
    ret
622
 
623
 
624
get_time_for_file:
625
;-----------------------------------------------------
626
; Get time from CMOS and pack hour,minute,second in AX
627
; TIME   bits  0..4   : second (the low bit is lost)
628
;              5..10  : minute 0..59
629
;              11..15 : hour 0..23
630
;-----------------------------------------------------
631
    mov   al,0x0        ;second
632
    out   0x70,al
633
    in    al,0x71
634
    call  bcd2bin
635
    ror   eax,6
636
 
637
    mov   al,0x2        ;minute
638
    out   0x70,al
639
    in    al,0x71
640
    call  bcd2bin
641
    ror   eax,6
642
 
643
    mov   al,0x4        ;hour
644
    out   0x70,al
645
    in    al,0x71
646
    call  bcd2bin
647
    rol   eax,11
648
    ret
649
 
650
 
651
set_current_time_for_entry:
652
;-----------------------------------------------------
653
; Set current time/date for file entry
654
; input  : ebx = file entry pointer
655
;-----------------------------------------------------
656
    push  eax
657
    call  get_time_for_file     ; update files date/time
658
    mov   [ebx+22],ax
659
    call  get_date_for_file
660
    mov   [ebx+24],ax
661
    pop   eax
662
    ret
663
 
664
 
665
 
666
add_disk_free_space:
667
;-----------------------------------------------------
668
; input  : ecx = cluster count
669
; Note   : negative = remove clusters from free space
670
;          positive = add clusters to free space
671
;-----------------------------------------------------
672
    test  ecx,ecx               ; no change
673
    je    add_dfs_no
674
    cmp   [fs_type],32         ; free disk space only used by fat32
675
    jne   add_dfs_no
676
 
677
    push  eax ebx
678
    mov   eax,[ADR_FSINFO]
679
    mov   ebx,fsinfo_buffer
680
    call  hd_read
681
    cmp  [hd_error],0
682
    jne  add_not_fs
683
 
684
    cmp   dword [ebx+0x1fc],0xaa550000 ; check sector id
685
    jne   add_not_fs
686
 
687
    add   [ebx+0x1e8],ecx
688
    push  [fatStartScan]
689
    pop   dword [ebx+0x1ec]
690
    call  hd_write
691
;    cmp   [hd_error],0
692
;    jne   add_not_fs
693
 
694
  add_not_fs:
695
    pop   ebx eax
696
 
697
  add_dfs_no:
698
    ret
699
 
700
 
701
file_read:
702
;--------------------------------------------------------------------------
703
;   INPUT :  user-register register-in-this  meaning         symbol-in-this
704
;
705
;            EAX           EDI               system call to write   /
706
;            EBX           EAX   (PAR0)      pointer to file-name   PAR0
707
;            EDX           ECX   (PAR1)      pointer to buffer      PAR1
708
;            ECX           EBX   (PAR2)   vt file blocks to read    PAR2
709
;            ESI           EDX   (PAR3)      pointer to path        PAR3
710
;            EDI           ESI            vt first 512 block to read
711
;                          EDI               if 0 - read root
712
;
713
; output : eax = 0 - ok
714
;                3 - unknown FS
715
;                5 - file not found
716
;                6 - end of file
717
;                9 - fat table corrupted
718
;               10 - access denied
719
;          ebx = size of file/directory
720
;--------------------------------------------------------------------------
721
        cmp     [fs_type], 16
722
        jz      fat_ok_for_reading
723
        cmp     [fs_type], 32
724
        jz      fat_ok_for_reading
725
    xor   ebx,ebx
726
    mov   eax,ERROR_UNKNOWN_FS
727
    mov   [hd1_status], ebx
728
    ret
729
 
730
  fat_ok_for_reading:
731
;    call  reserve_hd1
732
 
733
    pushad
734
 
735
    mov   ebx,edx
736
    call  get_cluster_of_a_path
737
    jc    file_to_read_not_found
738
 
739
    test  edi,edi               ; read rootdir
740
    jne   no_read_root
741
 
742
    xor   eax,eax
743
    call  get_dir_size          ; return rootdir size
744
    cmp   [hd_error],0
745
    jne   file_access_denied
746
 
747
    mov   [file_size],eax
748
    mov   eax,[ROOT_CLUSTER]
749
    jmp   file_read_start
750
 
751
  no_read_root:
752
    mov   ebx,PUSHAD_EAX        ; file name
753
    call  analyze_directory
754
    jc    file_to_read_not_found
755
 
756
    mov   eax,[ebx+28]          ; file size
757
    test  byte [ebx+11],0x10    ; is it directory?
758
    jz    read_set_size         ; no
759
 
760
    mov   eax,[ebx+20-2]        ; FAT entry
761
    mov   ax,[ebx+26]
762
    and   eax,[fatMASK]
763
    call  get_dir_size
764
    cmp   [hd_error],0
765
    jne   file_access_denied
766
 
767
  read_set_size:
768
    mov   [file_size],eax
769
 
770
    mov   eax,[ebx+20-2]        ; FAT entry
771
    mov   ax,[ebx+26]
772
    and   eax,[fatMASK]
773
 
774
  file_read_start:
775
    mov   ebx,PUSHAD_ECX        ; pointer to buffer
776
    mov   edx,PUSHAD_EBX        ; file blocks to read
777
    mov   esi,PUSHAD_ESI        ; first 512 block to read
778
 
779
  file_read_new_cluster:
780
    call  get_data_cluster
781
    jc    file_read_eof         ; end of file or cluster out of range
782
 
783
    test  edx,edx               ; is all read?
784
    je    file_read_OK          ; yes
785
 
786
    call  get_FAT               ; get next cluster
787
    cmp   [hd_error],0
788
    jne   file_access_denied
789
 
790
    cmp   eax,[fatRESERVED]     ; end of file
791
    jnb   file_read_eof
792
    cmp   eax,2                 ; incorrect fat chain
793
    jnb   file_read_new_cluster
794
 
795
    popad
796
    mov   [hd1_status],0
797
    mov   ebx,[file_size]
798
    mov   eax,ERROR_FAT_TABLE
799
    ret
800
 
801
  file_read_eof:
802
    cmp   [hd_error],0
803
    jne   file_access_denied
804
    popad
805
    mov   [hd1_status],0
806
    mov   ebx,[file_size]
807
    mov   eax,ERROR_END_OF_FILE
808
    ret
809
 
810
  file_read_OK:
811
    popad
812
    mov   [hd1_status],0
813
    mov   ebx,[file_size]
814
    xor   eax,eax
815
    ret
816
 
817
  file_to_read_not_found:
818
    cmp   [hd_error],0
819
    jne   file_access_denied
820
    popad
821
    mov   [hd1_status],0
822
    xor   ebx,ebx
823
    mov   eax,ERROR_FILE_NOT_FOUND
824
    ret
825
 
826
  file_access_denied:
827
    popad
828
    mov   [hd1_status],0
829
    xor   ebx,ebx
830
    mov   eax,ERROR_ACCESS_DENIED
831
    ret
832
 
833
get_dir_size:
834
;-----------------------------------------------------
835
; input  : eax = first cluster (0=rootdir)
836
; output : eax = directory size in bytes
837
;-----------------------------------------------------
838
    push  edx
839
    xor   edx,edx               ; count of directory clusters
840
    test  eax,eax
841
    jnz   dir_size_next
842
 
843
    mov   eax,[ROOT_SECTORS]
844
    shl   eax,9                 ; fat16 rootdir size in bytes
845
    cmp   [fs_type],16
846
    je    dir_size_ret
847
    mov   eax,[ROOT_CLUSTER]
848
 
849
  dir_size_next:
850
    cmp   eax,2                 ; incorrect fat chain
851
    jb    dir_size_end
852
    cmp   eax,[fatRESERVED]     ; end of directory
853
    ja    dir_size_end
854
    call  get_FAT               ; get next cluster
855
    cmp   [hd_error],0
856
    jne   dir_size_ret
857
 
858
    inc   edx
859
    jmp   dir_size_next
860
 
861
  dir_size_end:
862
    imul  eax,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
863
    imul  eax,edx
864
 
865
  dir_size_ret:
866
    pop   edx
867
    ret
868
 
869
 
870
clear_cluster_chain:
871
;-----------------------------------------------------
872
; input  : eax = first cluster
873
;-----------------------------------------------------
874
    push  eax ecx edx
875
    xor   ecx,ecx               ; cluster count
876
 
877
  clean_new_chain:
878
    cmp   eax,[LAST_CLUSTER]    ; end of file
879
    ja    delete_OK
880
    cmp   eax,2                 ; unfinished fat chain or zero length file
881
    jb    delete_OK
882
    cmp   eax,[ROOT_CLUSTER]    ; don't remove root cluster
883
    jz    delete_OK
884
 
885
    xor   edx,edx
886
    call  set_FAT               ; clear fat entry
887
    cmp  [hd_error],0
888
    jne  access_denied_01
889
 
890
    inc   ecx                   ; update cluster count
891
    mov   eax,edx               ; old cluster
892
    jmp   clean_new_chain
893
 
894
  delete_OK:
895
    call  add_disk_free_space   ; add clusters to free disk space
896
  access_denied_01:
897
    pop   edx ecx eax
898
    ret
899
 
900
 
901
get_hd_info:
902
;-----------------------------------------------------------
903
; output : eax = 0 - ok
904
;                3 - unknown FS
905
;               10 - access denied
906
;          edx = cluster size in bytes
907
;          ebx = total clusters on disk
908
;          ecx = free clusters on disk
909
;-----------------------------------------------------------
910
        cmp     [fs_type], 16
911
        jz      info_fat_ok
912
        cmp     [fs_type], 32
913
        jz      info_fat_ok
914
    xor   edx,edx
915
    xor   ebx,ebx
916
    xor   ecx,ecx
917
    mov   eax,ERROR_UNKNOWN_FS
918
    ret
919
 
920
  info_fat_ok:
921
;    call  reserve_hd1
922
 
923
    xor   ecx,ecx               ; count of free clusters
924
    mov   eax,2
925
    mov   ebx,[LAST_CLUSTER]
926
 
927
  info_cluster:
928
    push  eax
929
    call  get_FAT               ; get cluster info
930
    cmp   [hd_error],0
931
    jne   info_access_denied
932
 
933
    test  eax,eax               ; is it free?
934
    jnz   info_used             ; no
935
    inc   ecx
936
 
937
  info_used:
938
    pop   eax
939
    inc   eax
940
    cmp   eax,ebx               ; is above last cluster?
941
    jbe   info_cluster          ; no. test next cluster
942
 
943
    dec   ebx                   ; cluster count
944
    imul  edx,[SECTORS_PER_CLUSTER],512 ; cluster size in bytes
945
    mov   [hd1_status],0
946
    xor   eax,eax
947
    ret
948
 
949
  info_access_denied:
950
    add   esp,4
951
    xor   edx,edx
952
    xor   ebx,ebx
953
    xor   ecx,ecx
954
    mov   eax,ERROR_ACCESS_DENIED
955
    ret
956
 
957
update_disk:
958
;-----------------------------------------------------------
959
; write changed fat and cache to disk
960
;-----------------------------------------------------------
961
    cmp   [fat_change],0        ; is fat changed?
962
    je    upd_no_change
963
 
964
    call  write_fat_sector
965
    cmp   [hd_error],0
966
    jne   update_disk_acces_denied
967
 
968
  upd_no_change:
969
 
970
    call  write_cache
971
  update_disk_acces_denied:
972
    ret
973
 
974
 
975
; \begin{diamond}
976
hd_find_lfn:
977
; in: esi+ebp -> name
978
; out: CF=1 - file not found
979
;      else CF=0 and edi->direntry, eax=sector
980
; destroys eax
981
        push    esi edi
982
        push    0
983
        push    0
984
        push    fat16_root_first
985
        push    fat16_root_next
986
        mov     eax, [ROOT_CLUSTER]
987
        cmp     [fs_type], 32
988
        jz      .fat32
989
.loop:
990
        call    fat_find_lfn
991
        jc      .notfound
992
        cmp     byte [esi], 0
993
        jz      .found
994
.continue:
995
        test    byte [edi+11], 10h
996
        jz      .notfound
997
        and     dword [esp+12], 0
998
        mov     eax, [edi+20-2]
999
        mov     ax, [edi+26]    ; cluster
1000
.fat32:
1001
        mov     [esp+8], eax
1002
        mov     dword [esp+4], fat_notroot_first
1003
        mov     dword [esp], fat_notroot_next
1004
        jmp     .loop
1005
.notfound:
1006
        add     esp, 16
1007
        pop     edi esi
1008
        stc
1009
        ret
1010
.found:
1011
        test    ebp, ebp
1012
        jz      @f
1013
        mov     esi, ebp
1014
        xor     ebp, ebp
1015
        jmp     .continue
1016
@@:
1017
        lea     eax, [esp+8]
1018
        cmp     dword [eax], 0
1019
        jz      .root
1020
        call    fat_get_sector
1021
        jmp     .cmn
1022
.root:
1023
        mov     eax, [eax+4]
1024
        add     eax, [ROOT_START]
1025
.cmn:
1026
        add     esp, 20         ; CF=0
1027
        pop     esi
1028
        ret
1029
 
1030
;----------------------------------------------------------------
1031
;
1032
;  fs_HdRead - LFN variant for reading hard disk
1033
;
1034
;  esi  points to filename
1035
;  ebx  pointer to 64-bit number = first wanted byte, 0+
1036
;       may be ebx=0 - start from first byte
1037
;  ecx  number of bytes to read, 0+
1038
;  edx  mem location to return data
1039
;
1040
;  ret ebx = bytes read or 0xffffffff file not found
1041
;      eax = 0 ok read or other = errormsg
1042
;
1043
;--------------------------------------------------------------
1044
fs_HdRead:
1045
        cmp     [fs_type], 16
1046
        jz      @f
1047
        cmp     [fs_type], 32
1048
        jz      @f
1049
        cmp     [fs_type], 1
1050
        jz      ntfs_HdRead
1051
        or      ebx, -1
1052
        mov     eax, ERROR_UNKNOWN_FS
1053
        ret
1054
@@:
1055
    push    edi
1056
    cmp    byte [esi], 0
1057
    jnz    @f
1058
.noaccess:
1059
    pop    edi
1060
.noaccess_2:
1061
    or    ebx, -1
1062
    mov    eax, ERROR_ACCESS_DENIED
1063
    ret
1064
 
1065
@@:
1066
    call    hd_find_lfn
1067
    jnc    .found
1068
    pop    edi
1069
    cmp   [hd_error],0
1070
    jne   .noaccess_2
1071
    or    ebx, -1
1072
    mov    eax, ERROR_FILE_NOT_FOUND
1073
    ret
1074
 
1075
.found:
1076
    test    byte [edi+11], 0x10    ; do not allow read directories
1077
    jnz    .noaccess
1078
    test    ebx, ebx
1079
    jz    .l1
1080
    cmp    dword [ebx+4], 0
1081
    jz    @f
1082
        xor     ebx, ebx
1083
.reteof:
1084
    mov    eax, 6
1085
    pop    edi
1086
    ret
1087
@@:
1088
    mov    ebx, [ebx]
1089
.l1:
1090
        push    ecx edx
1091
        push    0
1092
        mov     eax, [edi+28]
1093
        sub     eax, ebx
1094
        jb      .eof
1095
        cmp     eax, ecx
1096
        jae     @f
1097
        mov     ecx, eax
1098
        mov     byte [esp], 6
1099
@@:
1100
    mov    eax, [edi+20-2]
1101
    mov    ax, [edi+26]
1102
; now eax=cluster, ebx=position, ecx=count, edx=buffer for data
1103
.new_cluster:
1104
    jecxz    .new_sector
1105
    test    eax, eax
1106
    jz    .eof
1107
    cmp    eax, [fatRESERVED]
1108
    jae    .eof
1109
    mov    [cluster_tmp], eax
1110
    dec    eax
1111
    dec    eax
1112
    mov    edi, [SECTORS_PER_CLUSTER]
1113
    imul    eax, edi
1114
    add    eax, [DATA_START]
1115
.new_sector:
1116
    test    ecx, ecx
1117
    jz    .done
1118
    sub    ebx, 512
1119
    jae    .skip
1120
    add    ebx, 512
1121
    jnz    .force_buf
1122
    cmp    ecx, 512
1123
    jb    .force_buf
1124
; we may read directly to given buffer
1125
    push    ebx
1126
    mov    ebx, edx
1127
    call    hd_read
1198 clevermous 1128
    pop    ebx
1159 hidnplayr 1129
    cmp  [hd_error],0
1130
    jne  .noaccess_1
1131
    add    edx, 512
1132
    sub    ecx, 512
1133
    jmp    .skip
1134
.force_buf:
1135
; we must read sector to temporary buffer and then copy it to destination
1136
    push    eax ebx
1137
    mov    ebx, buffer
1138
    call    hd_read
1198 clevermous 1139
    mov    eax, ebx
1140
    pop    ebx
1159 hidnplayr 1141
    cmp  [hd_error],0
1142
    jne  .noaccess_3
1143
    add    eax, ebx
1144
    push    ecx
1145
    add    ecx, ebx
1146
    cmp    ecx, 512
1147
    jbe    @f
1148
    mov    ecx, 512
1149
@@:
1150
    sub    ecx, ebx
1151
    mov    ebx, edx
1152
    call    memmove
1153
    add    edx, ecx
1154
    sub    [esp], ecx
1155
    pop    ecx
1156
    pop    eax
1157
    xor    ebx, ebx
1158
.skip:
1159
    inc    eax
1160
    dec    edi
1161
    jnz    .new_sector
1162
    mov    eax, [cluster_tmp]
1163
    call    get_FAT
1164
    cmp   [hd_error],0
1198 clevermous 1165
    jne   .noaccess_1
1159 hidnplayr 1166
 
1167
    jmp    .new_cluster
1198 clevermous 1168
.noaccess_3:
1169
        pop     eax
1170
.noaccess_1:
1171
        pop     eax
1172
        push    11
1159 hidnplayr 1173
.done:
1174
        mov     ebx, edx
1175
        pop     eax edx ecx edi
1176
        sub     ebx, edx
1177
        ret
1178
.eof:
1179
        mov     ebx, edx
1180
        pop     eax edx ecx
1181
        sub     ebx, edx
1182
        jmp     .reteof
1183
 
1184
;----------------------------------------------------------------
1185
;
1186
;  fs_HdReadFolder - LFN variant for reading hard disk folder
1187
;
1188
;  esi  points to filename
1189
;  ebx  pointer to structure 32-bit number = first wanted block, 0+
1190
;                          & flags (bitfields)
1191
; flags: bit 0: 0=ANSI names, 1=UNICODE names
1192
;  ecx  number of blocks to read, 0+
1193
;  edx  mem location to return data
1194
;
1195
;  ret ebx = blocks read or 0xffffffff folder not found
1196
;      eax = 0 ok read or other = errormsg
1197
;
1198
;--------------------------------------------------------------
1199
fs_HdReadFolder:
1200
        cmp     [fs_type], 1
1201
        jz      ntfs_HdReadFolder
1202
        cmp     [fs_type], 16
1203
        jz      @f
1204
        cmp     [fs_type], 32
1205
        jz      @f
1206
        push    ERROR_UNSUPPORTED_FS
1207
        pop     eax
1208
        or      ebx, -1
1209
        ret
1210
@@:
1211
        mov     eax, [ROOT_CLUSTER]
1212
        push    edi
1213
        cmp     byte [esi], 0
1214
        jz      .doit
1215
        call    hd_find_lfn
1216
        jnc     .found
1217
        pop     edi
1218
        or      ebx, -1
1219
        mov     eax, ERROR_FILE_NOT_FOUND
1220
        ret
1221
.found:
1222
        test    byte [edi+11], 0x10     ; do not allow read files
1223
        jnz     .found_dir
1224
        pop     edi
1225
        or      ebx, -1
1226
        mov     eax, ERROR_ACCESS_DENIED
1227
        ret
1228
.found_dir:
1229
        mov     eax, [edi+20-2]
1230
        mov     ax, [edi+26]    ; eax=cluster
1231
.doit:
1232
        push    esi ecx
1233
        push    ebp
1234
        sub     esp, 262*2      ; reserve space for LFN
1235
        mov     ebp, esp
1236
        push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE name
1237
        mov     ebx, [ebx]
1238
; init header
1239
        push    eax ecx
1240
        mov     edi, edx
1241
        mov     ecx, 32/4
1242
        xor     eax, eax
1243
        rep     stosd
1244
        pop     ecx eax
1245
        mov     byte [edx], 1   ; version
1246
        mov     esi, edi        ; esi points to BDFE
1247
.new_cluster:
1248
        mov     [cluster_tmp], eax
1249
        test    eax, eax
1250
        jnz     @f
1251
        cmp     [fs_type], 32
1252
        jz      .notfound
1253
        mov     eax, [ROOT_START]
1254
        push    [ROOT_SECTORS]
1255
        push    ebx
1256
        jmp     .new_sector
1257
@@:
1258
        dec     eax
1259
        dec     eax
1260
        imul    eax, [SECTORS_PER_CLUSTER]
1261
        push    [SECTORS_PER_CLUSTER]
1262
        add     eax, [DATA_START]
1263
        push    ebx
1264
.new_sector:
1265
        mov     ebx, buffer
1266
        mov     edi, ebx
1267
        call    hd_read
1268
        cmp     [hd_error], 0
1269
        jnz     .notfound2
1270
        add     ebx, 512
1271
        push    eax
1272
.l1:
1273
        call    fat_get_name
1274
        jc      .l2
1275
        cmp     byte [edi+11], 0xF
1276
        jnz     .do_bdfe
1277
        add     edi, 0x20
1278
        cmp     edi, ebx
1279
        jb      .do_bdfe
1280
        pop     eax
1281
        inc     eax
1282
        dec     dword [esp+4]
1283
        jnz     @f
1284
        mov     eax, [cluster_tmp]
1285
        test    eax, eax
1286
        jz      .done
1287
        call    get_FAT
1288
        cmp     [hd_error], 0
1289
        jnz     .notfound2
1290
        cmp     eax, 2
1291
        jb      .done
1292
        cmp     eax, [fatRESERVED]
1293
        jae     .done
1294
        push    eax
1295
        mov     eax, [SECTORS_PER_CLUSTER]
1296
        mov     [esp+8], eax
1297
        pop     eax
1298
        mov     [cluster_tmp], eax
1299
        dec     eax
1300
        dec     eax
1301
        imul    eax, [SECTORS_PER_CLUSTER]
1302
        add     eax, [DATA_START]
1303
@@:
1304
        mov     ebx, buffer
1305
        mov     edi, ebx
1306
        call    hd_read
1307
        cmp     [hd_error], 0
1308
        jnz     .notfound2
1309
        add     ebx, 512
1310
        push    eax
1311
.do_bdfe:
1312
        inc     dword [edx+8]   ; new file found
1313
        dec     dword [esp+4]
1314
        jns     .l2
1315
        dec     ecx
1316
        js      .l2
1317
        inc     dword [edx+4]   ; new file block copied
1318
        call    fat_entry_to_bdfe
1319
.l2:
1320
        add     edi, 0x20
1321
        cmp     edi, ebx
1322
        jb      .l1
1323
        pop     eax
1324
        inc     eax
1325
        dec     dword [esp+4]
1326
        jnz     .new_sector
1327
        mov     eax, [cluster_tmp]
1328
        test    eax, eax
1329
        jz      .done
1330
        call    get_FAT
1331
        cmp     [hd_error], 0
1332
        jnz     .notfound2
1333
        cmp     eax, 2
1334
        jb      .done
1335
        cmp     eax, [fatRESERVED]
1336
        jae     .done
1337
        push    eax
1338
        mov     eax, [SECTORS_PER_CLUSTER]
1339
        mov     [esp+8], eax
1340
        pop     eax
1341
        pop     ebx
1342
        add     esp, 4
1343
        jmp     .new_cluster
1344
.notfound2:
1345
        add     esp, 8
1346
.notfound:
1347
        add     esp, 262*2+4
1348
        pop     ebp ecx esi edi
1349
        mov     eax, ERROR_FILE_NOT_FOUND
1350
        or      ebx, -1
1351
        ret
1352
.done:
1353
        add     esp, 262*2+4+8
1354
        pop     ebp
1355
        mov     ebx, [edx+4]
1356
        xor     eax, eax
1357
        dec     ecx
1358
        js      @f
1359
        mov     al, ERROR_END_OF_FILE
1360
@@:
1361
        pop     ecx esi edi
1362
        ret
1363
 
1364
fat16_root_next:
1365
        cmp     edi, buffer+0x200-0x20
1366
        jae     fat16_root_next_sector
1367
        add     edi, 0x20
1368
        ret     ; CF=0
1369
fat16_root_next_sector:
1370
; read next sector
1371
        push    [longname_sec2]
1372
        pop     [longname_sec1]
1373
        push    ecx
1374
        mov     ecx, [eax+4]
1375
        push    ecx
1376
        add     ecx, [ROOT_START]
1377
        mov     [longname_sec2], ecx
1378
        pop     ecx
1379
        inc     ecx
1380
        mov     [eax+4], ecx
1381
        cmp     ecx, [ROOT_SECTORS]
1382
        pop     ecx
1383
        jae     fat16_root_first.readerr
1384
fat16_root_first:
1385
        mov     eax, [eax+4]
1386
        add     eax, [ROOT_START]
1387
        push    ebx
1388
        mov     edi, buffer
1389
        mov     ebx, edi
1390
        call    hd_read
1391
        pop     ebx
1392
        cmp     [hd_error], 0
1393
        jnz     .readerr
1394
        ret     ; CF=0
1395
.readerr:
1396
        stc
1397
        ret
1398
fat16_root_begin_write:
1399
        push    edi eax
1400
        call    fat16_root_first
1401
        pop     eax edi
1402
        ret
1403
fat16_root_end_write:
1404
        pusha
1405
        mov     eax, [eax+4]
1406
        add     eax, [ROOT_START]
1407
        mov     ebx, buffer
1408
        call    hd_write
1409
        popa
1410
        ret
1411
fat16_root_next_write:
1412
        cmp     edi, buffer+0x200
1413
        jae     @f
1414
        ret
1415
@@:
1416
        call    fat16_root_end_write
1417
        jmp     fat16_root_next_sector
1418
fat16_root_extend_dir:
1419
        stc
1420
        ret
1421
 
1422
fat_notroot_next:
1423
        cmp     edi, buffer+0x200-0x20
1424
        jae     fat_notroot_next_sector
1425
        add     edi, 0x20
1426
        ret     ; CF=0
1427
fat_notroot_next_sector:
1428
        push    [longname_sec2]
1429
        pop     [longname_sec1]
1430
        push    eax
1431
        call    fat_get_sector
1432
        mov     [longname_sec2], eax
1433
        pop     eax
1434
        push    ecx
1435
        mov     ecx, [eax+4]
1436
        inc     ecx
1437
        cmp     ecx, [SECTORS_PER_CLUSTER]
1438
        jae     fat_notroot_next_cluster
1439
        mov     [eax+4], ecx
1440
        jmp     @f
1441
fat_notroot_next_cluster:
1442
        push    eax
1443
        mov     eax, [eax]
1444
        call    get_FAT
1445
        mov     ecx, eax
1446
        pop     eax
1447
        cmp     [hd_error], 0
1448
        jnz     fat_notroot_next_err
1449
        cmp     ecx, [fatRESERVED]
1450
        jae     fat_notroot_next_err
1451
        mov     [eax], ecx
1452
        and     dword [eax+4], 0
1453
@@:
1454
        pop     ecx
1455
fat_notroot_first:
1456
        call    fat_get_sector
1457
        push    ebx
1458
        mov     edi, buffer
1459
        mov     ebx, edi
1460
        call    hd_read
1461
        pop     ebx
1462
        cmp     [hd_error], 0
1463
        jnz     @f
1464
        ret     ; CF=0
1465
fat_notroot_next_err:
1466
        pop     ecx
1467
@@:
1468
        stc
1469
        ret
1470
fat_notroot_begin_write:
1471
        push    eax edi
1472
        call    fat_notroot_first
1473
        pop     edi eax
1474
        ret
1475
fat_notroot_end_write:
1476
        call    fat_get_sector
1477
        push    ebx
1478
        mov     ebx, buffer
1479
        call    hd_write
1480
        pop     ebx
1481
        ret
1482
fat_notroot_next_write:
1483
        cmp     edi, buffer+0x200
1484
        jae     @f
1485
        ret
1486
@@:
1487
        push    eax
1488
        call    fat_notroot_end_write
1489
        pop     eax
1490
        jmp     fat_notroot_next_sector
1491
fat_notroot_extend_dir:
1492
        push    eax
1493
        call    get_free_FAT
1494
        jnc     .found
1495
        pop     eax
1496
        ret     ; CF=1
1497
.found:
1498
        push    edx
1499
        mov     edx, [fatEND]
1500
        call    set_FAT
1501
        mov     edx, eax
1502
        mov     eax, [esp+4]
1503
        mov     eax, [eax]
1504
        push    edx
1505
        call    set_FAT
1506
        pop     edx
1507
        cmp     [hd_error], 0
1508
        jz      @f
1509
        pop     edx
1510
        pop     eax
1511
        stc
1512
        ret
1513
@@:
1514
        push    ecx
1515
        or      ecx, -1
1516
        call    add_disk_free_space
1517
; zero new cluster
1518
        mov     ecx, 512/4
1519
        mov     edi, buffer
1520
        push    edi
1521
        xor     eax, eax
1522
        rep     stosd
1523
        pop     edi
1524
        pop     ecx
1525
        mov     eax, [esp+4]
1526
        mov     [eax], edx
1527
        and     dword [eax+4], 0
1528
        pop     edx
1529
        mov     eax, [eax]
1530
        dec     eax
1531
        dec     eax
1532
        push    ebx ecx
1533
        mov     ecx, [SECTORS_PER_CLUSTER]
1534
        imul    eax, ecx
1535
        add     eax, [DATA_START]
1536
        mov     ebx, edi
1537
@@:
1538
        call    hd_write
1539
        inc     eax
1540
        loop    @b
1541
        pop     ecx ebx eax
1542
        clc
1543
        ret
1544
 
1545
fat_get_sector:
1546
        push    ecx
1547
        mov     ecx, [eax]
1548
        dec     ecx
1549
        dec     ecx
1550
        imul    ecx, [SECTORS_PER_CLUSTER]
1551
        add     ecx, [DATA_START]
1552
        add     ecx, [eax+4]
1553
        mov     eax, ecx
1554
        pop     ecx
1555
        ret
1556
 
1557
;----------------------------------------------------------------
1558
;
1559
;  fs_HdRewrite - LFN variant for writing hard disk
1560
;
1561
;  esi  points to filename
1562
;  ebx  ignored (reserved)
1563
;  ecx  number of bytes to write, 0+
1564
;  edx  mem location to data
1565
;
1566
;  ret ebx = number of written bytes
1567
;      eax = 0 ok read or other = errormsg
1568
;
1569
;--------------------------------------------------------------
1570
fshrad:
1571
        mov     eax, ERROR_ACCESS_DENIED
1572
        xor     ebx, ebx
1573
        ret
1574
fshrfs:
1575
        mov     eax, ERROR_UNKNOWN_FS
1576
        xor     ebx, ebx
1577
        ret
1578
 
1579
fs_HdCreateFolder:
1580
        mov     al, 1
1581
        jmp     fs_HdRewrite.common
1582
 
1583
fs_HdRewrite:
1584
        xor     eax, eax
1585
.common:
1586
        cmp     [fs_type], 1
1587
        jz      ntfs_HdRewrite
1588
        cmp     [fs_type], 16
1589
        jz      @f
1590
        cmp     [fs_type], 32
1591
        jnz     fshrfs
1592
@@:
1593
        cmp     byte [esi], 0
1594
        jz      fshrad
1595
        pushad
1596
        xor     edi, edi
1597
        push    esi
1598
        test    ebp, ebp
1599
        jz      @f
1600
        mov     esi, ebp
1601
@@:
1602
        lodsb
1603
        test    al, al
1604
        jz      @f
1605
        cmp     al, '/'
1606
        jnz     @b
1607
        lea     edi, [esi-1]
1608
        jmp     @b
1609
@@:
1610
        pop     esi
1611
        test    edi, edi
1612
        jnz     .noroot
1613
        test    ebp, ebp
1614
        jnz     .hasebp
1615
        mov     ebp, [ROOT_CLUSTER]
1616
        cmp     [fs_type], 32
1617
        jz      .pushnotroot
1618
        push    fat16_root_extend_dir
1619
        push    fat16_root_end_write
1620
        push    fat16_root_next_write
1621
        push    fat16_root_begin_write
1622
        xor     ebp, ebp
1623
        push    ebp
1624
        push    ebp
1625
        push    fat16_root_first
1626
        push    fat16_root_next
1627
        jmp     .common1
1628
.hasebp:
1629
        mov     eax, ERROR_ACCESS_DENIED
1630
        cmp     byte [ebp], 0
1631
        jz      .ret1
1632
        push    ebp
1633
        xor     ebp, ebp
1634
        call    hd_find_lfn
1635
        pop     esi
1636
        jc      .notfound0
1637
        jmp     .common0
1638
.noroot:
1639
        mov     eax, ERROR_ACCESS_DENIED
1640
        cmp     byte [edi+1], 0
1641
        jz      .ret1
1642
; check existence
1643
        mov     byte [edi], 0
1644
        push    edi
1645
        call    hd_find_lfn
1646
        pop     esi
1647
        mov     byte [esi], '/'
1648
        jnc     @f
1649
.notfound0:
1650
        mov     eax, ERROR_FILE_NOT_FOUND
1651
.ret1:
1652
        mov     [esp+28], eax
1653
        popad
1654
        xor     ebx, ebx
1655
        ret
1656
@@:
1657
        inc     esi
1658
.common0:
1659
        test    byte [edi+11], 0x10     ; must be directory
1660
        mov     eax, ERROR_ACCESS_DENIED
1661
        jz      .ret1
1662
        mov     ebp, [edi+20-2]
1663
        mov     bp, [edi+26]            ; ebp=cluster
1664
        mov     eax, ERROR_FAT_TABLE
1665
        cmp     ebp, 2
1666
        jb      .ret1
1667
.pushnotroot:
1668
        push    fat_notroot_extend_dir
1669
        push    fat_notroot_end_write
1670
        push    fat_notroot_next_write
1671
        push    fat_notroot_begin_write
1672
        push    0
1673
        push    ebp
1674
        push    fat_notroot_first
1675
        push    fat_notroot_next
1676
.common1:
1677
        call    fat_find_lfn
1678
        jc      .notfound
1679
; found
1680
        test    byte [edi+11], 10h
1681
        jz      .exists_file
1682
; found directory; if we are creating directory, return OK,
1683
;                  if we are creating file, say "access denied"
1684
        add     esp, 32
1685
        popad
1686
        test    al, al
1687
        mov     eax, ERROR_ACCESS_DENIED
1688
        jz      @f
1689
        mov     al, 0
1690
@@:
1691
        xor     ebx, ebx
1692
        ret
1693
.exists_file:
1694
; found file; if we are creating directory, return "access denied",
1695
;             if we are creating file, delete existing file and continue
1696
        cmp     byte [esp+32+28], 0
1697
        jz      @f
1698
        add     esp, 32
1699
        popad
1700
        mov     eax, ERROR_ACCESS_DENIED
1701
        xor     ebx, ebx
1702
        ret
1703
@@:
1704
; delete FAT chain
1705
        push    edi
1706
        xor     eax, eax
1707
        mov     dword [edi+28], eax     ; zero size
1708
        xor     ecx, ecx
1709
        mov     eax, [edi+20-2]
1710
        mov     ax, [edi+26]
1711
        mov     word [edi+20], cx
1712
        mov     word [edi+26], cx
1713
        test    eax, eax
1714
        jz      .done1
1715
@@:
1716
        cmp     eax, [fatRESERVED]
1717
        jae     .done1
1718
        push    edx
1719
        xor     edx, edx
1720
        call    set_FAT
1721
        mov     eax, edx
1722
        pop     edx
1723
        inc     ecx
1724
        jmp     @b
1725
.done1:
1726
        pop     edi
1727
        call    get_time_for_file
1728
        mov     [edi+22], ax
1729
        call    get_date_for_file
1730
        mov     [edi+24], ax
1731
        mov     [edi+18], ax
1732
        or      byte [edi+11], 20h      ; set 'archive' attribute
1733
        jmp     .doit
1734
.notfound:
1735
; file is not found; generate short name
1736
        call    fat_name_is_legal
1737
        jc      @f
1738
        add     esp, 32
1739
        popad
1740
        mov     eax, ERROR_FILE_NOT_FOUND
1741
        xor     ebx, ebx
1742
        ret
1743
@@:
1744
        sub     esp, 12
1745
        mov     edi, esp
1746
        call    fat_gen_short_name
1747
.test_short_name_loop:
1748
        push    esi edi ecx
1749
        mov     esi, edi
1750
        lea     eax, [esp+12+12+8]
1751
        mov     [eax], ebp
1752
        and     dword [eax+4], 0
1753
        call    dword [eax-4]
1754
        jc      .found
1755
.test_short_name_entry:
1756
        cmp     byte [edi+11], 0xF
1757
        jz      .test_short_name_cont
1758
        mov     ecx, 11
1759
        push    esi edi
1760
        repz    cmpsb
1761
        pop     edi esi
1762
        jz      .short_name_found
1763
.test_short_name_cont:
1764
        lea     eax, [esp+12+12+8]
1765
        call    dword [eax-8]
1766
        jnc     .test_short_name_entry
1767
        jmp     .found
1768
.short_name_found:
1769
        pop     ecx edi esi
1770
        call    fat_next_short_name
1771
        jnc     .test_short_name_loop
1772
.disk_full:
1773
        add     esp, 12+32
1774
        popa
1775
        mov     eax, ERROR_DISK_FULL
1776
        xor     ebx, ebx
1777
        ret
1778
.found:
1779
        pop     ecx edi esi
1780
; now find space in directory
1781
; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
1782
        mov     al, '~'
1783
        push    ecx edi
1784
        mov     ecx, 8
1785
        repnz   scasb
1786
        push    1
1787
        pop     eax     ; 1 entry
1788
        jnz     .notilde
1789
; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
1790
        xor     eax, eax
1791
@@:
1792
        cmp     byte [esi], 0
1793
        jz      @f
1794
        inc     esi
1795
        inc     eax
1796
        jmp     @b
1797
@@:
1798
        sub     esi, eax
1799
        add     eax, 12+13
1800
        mov     ecx, 13
1801
        push    edx
1802
        cdq
1803
        div     ecx
1804
        pop     edx
1805
.notilde:
1806
        push    -1
1807
        push    -1
1808
        push    -1
1809
; find  successive entries in directory
1810
        xor     ecx, ecx
1811
        push    eax
1812
        lea     eax, [esp+16+8+12+8]
1813
        mov     [eax], ebp
1814
        and     dword [eax+4], 0
1815
        call    dword [eax-4]
1816
        pop     eax
1817
        jnc     .scan_dir
1818
.fsfrfe3:
1819
        add     esp, 12+8+12+32
1820
        popad
1821
        mov     eax, 11
1822
        xor     ebx, ebx
1823
        ret
1824
.scan_dir:
1825
        cmp     byte [edi], 0
1826
        jz      .free
1827
        cmp     byte [edi], 0xE5
1828
        jz      .free
1829
        xor     ecx, ecx
1830
.scan_cont:
1831
        push    eax
1832
        lea     eax, [esp+16+8+12+8]
1833
        call    dword [eax-8]
1834
        pop     eax
1835
        jnc     .scan_dir
1836
        cmp     [hd_error], 0
1837
        jnz     .fsfrfe3
1838
        push    eax
1839
        lea     eax, [esp+16+8+12+8]
1840
        call    dword [eax+20]          ; extend directory
1841
        pop     eax
1842
        jnc     .scan_dir
1843
        add     esp, 12+8+12+32
1844
        popad
1845
        mov     eax, ERROR_DISK_FULL
1846
        xor     ebx, ebx
1847
        ret
1848
.free:
1849
        test    ecx, ecx
1850
        jnz     @f
1851
        mov     [esp], edi
1852
        mov     ecx, [esp+12+8+12+8]
1853
        mov     [esp+4], ecx
1854
        mov     ecx, [esp+12+8+12+12]
1855
        mov     [esp+8], ecx
1856
        xor     ecx, ecx
1857
@@:
1858
        inc     ecx
1859
        cmp     ecx, eax
1860
        jb      .scan_cont
1861
; found!
1862
; calculate name checksum
1863
        push    esi ecx
1864
        mov     esi, [esp+8+12]
1865
        mov     ecx, 11
1866
        xor     eax, eax
1867
@@:
1868
        ror     al, 1
1869
        add     al, [esi]
1870
        inc     esi
1871
        loop    @b
1872
        pop     ecx esi
1873
        pop     edi
1874
        pop     dword [esp+8+12+12]
1875
        pop     dword [esp+8+12+12]
1876
; edi points to first entry in free chunk
1877
        dec     ecx
1878
        jz      .nolfn
1879
        push    esi
1880
        push    eax
1881
        lea     eax, [esp+8+8+12+8]
1882
        call    dword [eax+8]         ; begin write
1883
        mov     al, 40h
1884
.writelfn:
1885
        or      al, cl
1886
        mov     esi, [esp+4]
1887
        push    ecx
1888
        dec     ecx
1889
        imul    ecx, 13
1890
        add     esi, ecx
1891
        stosb
1892
        mov     cl, 5
1893
        call    fs_RamdiskRewrite.read_symbols
1894
        mov     ax, 0xF
1895
        stosw
1896
        mov     al, [esp+4]
1897
        stosb
1898
        mov     cl, 6
1899
        call    fs_RamdiskRewrite.read_symbols
1900
        xor     eax, eax
1901
        stosw
1902
        mov     cl, 2
1903
        call    fs_RamdiskRewrite.read_symbols
1904
        pop     ecx
1905
        lea     eax, [esp+8+8+12+8]
1906
        call    dword [eax+12]         ; next write
1907
        xor     eax, eax
1908
        loop    .writelfn
1909
        pop     eax
1910
        pop     esi
1911
;        lea     eax, [esp+8+12+8]
1912
;        call    dword [eax+16]          ; end write
1913
.nolfn:
1914
        xchg    esi, [esp]
1915
        mov     ecx, 11
1916
        rep     movsb
1917
        mov     word [edi], 20h         ; attributes
1918
        sub     edi, 11
1919
        pop     esi ecx
1920
        add     esp, 12
1921
        mov     byte [edi+13], 0        ; tenths of a second at file creation time
1922
        call    get_time_for_file
1923
        mov     [edi+14], ax            ; creation time
1924
        mov     [edi+22], ax            ; last write time
1925
        call    get_date_for_file
1926
        mov     [edi+16], ax            ; creation date
1927
        mov     [edi+24], ax            ; last write date
1928
        mov     [edi+18], ax            ; last access date
1929
        xor     ecx, ecx
1930
        mov     word [edi+20], cx       ; high word of cluster
1931
        mov     word [edi+26], cx       ; low word of cluster - to be filled
1932
        mov     dword [edi+28], ecx     ; file size - to be filled
1933
        cmp     byte [esp+32+28], cl
1934
        jz      .doit
1935
; create directory
1936
        mov     byte [edi+11], 10h      ; attributes: folder
1937
        mov     edx, edi
1938
        lea     eax, [esp+8]
1939
        call    dword [eax+16]  ; flush directory
1940
        push    ecx
1941
        mov     ecx, [SECTORS_PER_CLUSTER]
1942
        shl     ecx, 9
1943
        jmp     .doit2
1944
.doit:
1945
        lea     eax, [esp+8]
1946
        call    dword [eax+16]  ; flush directory
1947
        push    ecx
1948
        mov     ecx, [esp+4+32+24]
1949
.doit2:
1950
        push    ecx
1951
        push    edi
1952
        mov     esi, edx
1953
        test    ecx, ecx
1954
        jz      .done
1955
        call    get_free_FAT
1956
        jc      .diskfull
1957
        push    eax
1958
        mov     [edi+26], ax
1959
        shr     eax, 16
1960
        mov     [edi+20], ax
1961
        lea     eax, [esp+16+8]
1962
        call    dword [eax+16]  ; flush directory
1963
        pop     eax
1964
        push    edx
1965
        mov     edx, [fatEND]
1966
        call    set_FAT
1967
        pop     edx
1968
.write_cluster:
1969
        push    eax
1970
        dec     eax
1971
        dec     eax
1972
        mov     ebp, [SECTORS_PER_CLUSTER]
1973
        imul    eax, ebp
1974
        add     eax, [DATA_START]
1975
; write data
1976
.write_sector:
1977
        cmp     byte [esp+16+32+28], 0
1978
        jnz     .writedir
1979
        mov     ecx, 512
1980
        cmp     dword [esp+8], ecx
1981
        jb      .writeshort
1982
; we can write directly from given buffer
1983
        mov     ebx, esi
1984
        add     esi, ecx
1985
        jmp     .writecommon
1986
.writeshort:
1987
        mov     ecx, [esp+8]
1988
        push    ecx
1989
        mov     edi, buffer
1990
        mov     ebx, edi
1991
        rep     movsb
1992
.writedircont:
1993
        mov     ecx, buffer+0x200
1994
        sub     ecx, edi
1995
        push    eax
1996
        xor     eax, eax
1997
        rep     stosb
1998
        pop     eax
1999
        pop     ecx
2000
.writecommon:
2001
        call    hd_write
2002
        cmp     [hd_error], 0
2003
        jnz     .writeerr
2004
        inc     eax
2005
        sub     dword [esp+8], ecx
2006
        jz      .writedone
2007
        dec     ebp
2008
        jnz     .write_sector
2009
; allocate new cluster
2010
        pop     eax
2011
        mov     ecx, eax
2012
        call    get_free_FAT
2013
        jc      .diskfull
2014
        push    edx
2015
        mov     edx, [fatEND]
2016
        call    set_FAT
2017
        xchg    eax, ecx
2018
        mov     edx, ecx
2019
        call    set_FAT
2020
        pop     edx
2021
        xchg    eax, ecx
2022
        jmp     .write_cluster
2023
.diskfull:
2024
        mov     eax, ERROR_DISK_FULL
2025
        jmp     .ret
2026
.writeerr:
2027
        pop     eax
2028
        sub     esi, ecx
2029
        mov     eax, 11
2030
        jmp     .ret
2031
.writedone:
2032
        pop     eax
2033
.done:
2034
        xor     eax, eax
2035
.ret:
2036
        pop     edi ecx
2037
        mov     ebx, esi
2038
        sub     ebx, edx
2039
        pop     ebp
2040
        mov     [esp+32+28], eax
2041
        lea     eax, [esp+8]
2042
        call    dword [eax+8]
2043
        mov     [edi+28], ebx
2044
        call    dword [eax+16]
2045
        mov     [esp+32+16], ebx
2046
        lea     eax, [ebx+511]
2047
        shr     eax, 9
2048
        mov     ecx, [SECTORS_PER_CLUSTER]
2049
        lea     eax, [eax+ecx-1]
2050
        xor     edx, edx
2051
        div     ecx
2052
        mov     ecx, ebp
2053
        sub     ecx, eax
2054
        call    add_disk_free_space
2055
        add     esp, 32
2056
        call    update_disk
2057
        popad
2058
        ret
2059
.writedir:
2060
        push    512
2061
        mov     edi, buffer
2062
        mov     ebx, edi
2063
        mov     ecx, [SECTORS_PER_CLUSTER]
2064
        shl     ecx, 9
2065
        cmp     ecx, [esp+12]
2066
        jnz     .writedircont
2067
        dec     dword [esp+16]
2068
        push    esi
2069
        mov     ecx, 32/4
2070
        rep     movsd
2071
        pop     esi
2072
        mov     dword [edi-32], '.   '
2073
        mov     dword [edi-32+4], '    '
2074
        mov     dword [edi-32+8], '    '
2075
        mov     byte [edi-32+11], 10h
2076
        push    esi
2077
        mov     ecx, 32/4
2078
        rep     movsd
2079
        pop     esi
2080
        mov     dword [edi-32], '..  '
2081
        mov     dword [edi-32+4], '    '
2082
        mov     dword [edi-32+8], '    '
2083
        mov     byte [edi-32+11], 10h
2084
        mov     ecx, [esp+20+8]
2085
        cmp     ecx, [ROOT_CLUSTER]
2086
        jnz     @f
2087
        xor     ecx, ecx
2088
@@:
2089
        mov     word [edi-32+26], cx
2090
        shr     ecx, 16
2091
        mov     [edi-32+20], cx
2092
        jmp     .writedircont
2093
 
2094
;----------------------------------------------------------------
2095
;
2096
;  fs_HdWrite - LFN variant for writing to hard disk
2097
;
2098
;  esi  points to filename
2099
;  ebx  pointer to 64-bit number = first wanted byte, 0+
2100
;       may be ebx=0 - start from first byte
2101
;  ecx  number of bytes to write, 0+
2102
;  edx  mem location to data
2103
;
2104
;  ret ebx = bytes written (maybe 0)
2105
;      eax = 0 ok write or other = errormsg
2106
;
2107
;--------------------------------------------------------------
2108
fs_HdWrite.access_denied:
2109
        push    ERROR_ACCESS_DENIED
2110
fs_HdWrite.ret0:
2111
        pop     eax
2112
        xor     ebx, ebx
2113
        ret
2114
 
2115
fs_HdWrite.ret11:
2116
        push    11
2117
        jmp     fs_HdWrite.ret0
2118
 
2119
fs_HdWrite:
2120
        cmp     [fs_type], 1
2121
        jz      ntfs_HdWrite
2122
        cmp     [fs_type], 16
2123
        jz      @f
2124
        cmp     [fs_type], 32
2125
        jz      @f
2126
        push    ERROR_UNKNOWN_FS
2127
        jmp     .ret0
2128
@@:
2129
        cmp     byte [esi], 0
2130
        jz      .access_denied
2131
        pushad
2132
        call    hd_find_lfn
2133
        pushfd
2134
        cmp     [hd_error], 0
2135
        jz      @f
2136
        popfd
2137
        popad
2138
        push    11
2139
        jmp     .ret0
2140
@@:
2141
        popfd
2142
        jnc     .found
2143
        popad
2144
        push    ERROR_FILE_NOT_FOUND
2145
        jmp     .ret0
2146
.found:
2147
; FAT does not support files larger than 4GB
2148
        test    ebx, ebx
2149
        jz      .l1
2150
        cmp     dword [ebx+4], 0
2151
        jz      @f
2152
.eof:
2153
        popad
2154
        push    ERROR_END_OF_FILE
2155
        jmp     .ret0
2156
@@:
2157
        mov     ebx, [ebx]
2158
.l1:
2159
; now edi points to direntry, ebx=start byte to write,
2160
; ecx=number of bytes to write, edx=data pointer
2161
 
2162
; extend file if needed
2163
        add     ecx, ebx
2164
        jc      .eof    ; FAT does not support files larger than 4GB
2165
        push    eax     ; save directory sector
2166
        push    0       ; return value=0
2167
 
2168
        call    get_time_for_file
2169
        mov     [edi+22], ax            ; last write time
2170
        call    get_date_for_file
2171
        mov     [edi+24], ax            ; last write date
2172
        mov     [edi+18], ax            ; last access date
2173
 
2174
        push    dword [edi+28]          ; save current file size
2175
        cmp     ecx, [edi+28]
2176
        jbe     .length_ok
2177
        cmp     ecx, ebx
2178
        jz      .length_ok
2179
        call    hd_extend_file
2180
        jnc     .length_ok
2181
        mov     [esp+4], eax
2182
; hd_extend_file can return three error codes: FAT table error, device error or disk full.
2183
; First two cases are fatal errors, in third case we may write some data
2184
        cmp     al, ERROR_DISK_FULL
2185
        jz      .disk_full
2186
        pop     eax
2187
        pop     eax
2188
        mov     [esp+4+28], eax
2189
        pop     eax
2190
        popad
2191
        xor     ebx, ebx
2192
        ret
2193
.disk_full:
2194
; correct number of bytes to write
2195
        mov     ecx, [edi+28]
2196
        cmp     ecx, ebx
2197
        ja      .length_ok
2198
.ret:
2199
        call    update_disk
2200
        cmp     [hd_error], 0
2201
        jz      @f
2202
        mov     byte [esp+4], 11
2203
@@:
2204
        pop     eax
2205
        pop     eax
2206
        mov     [esp+4+28], eax ; eax=return value
2207
        pop     eax
2208
        sub     edx, [esp+20]
2209
        mov     [esp+16], edx   ; ebx=number of written bytes
2210
        popad
2211
        ret
2212
.length_ok:
2213
        mov     esi, [edi+28]
2214
        mov     eax, [edi+20-2]
2215
        mov     ax, [edi+26]
2216
        mov     edi, eax        ; edi=current cluster
2217
        xor     ebp, ebp        ; ebp=current sector in cluster
2218
; save directory
2219
        mov     eax, [esp+8]
2220
        push    ebx
2221
        mov     ebx, buffer
2222
        call    hd_write
2223
        pop     ebx
2224
        cmp     [hd_error], 0
2225
        jz      @f
2226
.device_err:
2227
        mov     byte [esp+4], 11
2228
        jmp     .ret
2229
@@:
2230
 
2231
; now ebx=start pos, ecx=end pos, both lie inside file
2232
        sub     ecx, ebx
2233
        jz      .ret
2234
.write_loop:
2235
; skip unmodified sectors
2236
        cmp     dword [esp], 0x200
2237
        jb      .modify
2238
        sub     ebx, 0x200
2239
        jae     .skip
2240
        add     ebx, 0x200
2241
.modify:
2242
; get length of data in current sector
2243
        push    ecx
2244
        sub     ebx, 0x200
2245
        jb      .hasdata
2246
        neg     ebx
2247
        xor     ecx, ecx
2248
        jmp     @f
2249
.hasdata:
2250
        neg     ebx
2251
        cmp     ecx, ebx
2252
        jbe     @f
2253
        mov     ecx, ebx
2254
@@:
2255
; get current sector number
2256
        mov     eax, edi
2257
        dec     eax
2258
        dec     eax
2259
        imul    eax, [SECTORS_PER_CLUSTER]
2260
        add     eax, [DATA_START]
2261
        add     eax, ebp
2262
; load sector if needed
2263
        cmp     dword [esp+4], 0        ; we don't need to read uninitialized data
2264
        jz      .noread
2265
        cmp     ecx, 0x200      ; we don't need to read sector if it is fully rewritten
2266
        jz      .noread
2267
        cmp     ecx, esi        ; (same for the last sector)
2268
        jz      .noread
2269
        push    ebx
2270
        mov     ebx, buffer
2271
        call    hd_read
2272
        pop     ebx
2273
        cmp     [hd_error], 0
2274
        jz      @f
2275
.device_err2:
2276
        pop     ecx
2277
        jmp     .device_err
2278
@@:
2279
.noread:
2280
; zero uninitialized data if file was extended (because hd_extend_file does not this)
2281
        push    eax ecx edi
2282
        xor     eax, eax
2283
        mov     ecx, 0x200
2284
        sub     ecx, [esp+4+12]
2285
        jbe     @f
2286
        mov     edi, buffer
2287
        add     edi, [esp+4+12]
2288
        rep     stosb
2289
@@:
2290
; zero uninitialized data in the last sector
2291
        mov     ecx, 0x200
2292
        sub     ecx, esi
2293
        jbe     @f
2294
        mov     edi, buffer
2295
        add     edi, esi
2296
        rep     stosb
2297
@@:
2298
        pop     edi ecx
2299
; copy new data
2300
        mov     eax, edx
2301
        neg     ebx
2302
        jecxz   @f
2303
        add     ebx, buffer+0x200
2304
        call    memmove
2305
        xor     ebx, ebx
2306
@@:
2307
        pop     eax
2308
; save sector
2309
        push    ebx
2310
        mov     ebx, buffer
2311
        call    hd_write
2312
        pop     ebx
2313
        cmp     [hd_error], 0
2314
        jnz     .device_err2
2315
        add     edx, ecx
2316
        sub     [esp], ecx
2317
        pop     ecx
2318
        jz      .ret
2319
.skip:
2320
; next sector
2321
        inc     ebp
2322
        cmp     ebp, [SECTORS_PER_CLUSTER]
2323
        jb      @f
2324
        xor     ebp, ebp
2325
        mov     eax, edi
2326
        call    get_FAT
2327
        mov     edi, eax
2328
        cmp     [hd_error], 0
2329
        jnz     .device_err
2330
@@:
2331
        sub     esi, 0x200
2332
        jae     @f
2333
        xor     esi, esi
2334
@@:
2335
        sub     dword [esp], 0x200
2336
        jae     @f
2337
        and     dword [esp], 0
2338
@@:     jmp     .write_loop
2339
 
2340
hd_extend_file.zero_size:
2341
        xor     eax, eax
2342
        jmp     hd_extend_file.start_extend
2343
 
2344
; extends file on hd to given size (new data area is undefined)
2345
; in: edi->direntry, ecx=new size
2346
; out: CF=0 => OK, eax=0
2347
;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL or 11)
2348
hd_extend_file:
2349
        push    ebp
2350
        mov     ebp, [SECTORS_PER_CLUSTER]
2351
        imul    ebp, [BYTES_PER_SECTOR]
2352
        push    ecx
2353
; find the last cluster of file
2354
        mov     eax, [edi+20-2]
2355
        mov     ax, [edi+26]
2356
        mov     ecx, [edi+28]
2357
        jecxz   .zero_size
2358
.last_loop:
2359
        sub     ecx, ebp
2360
        jbe     .last_found
2361
        call    get_FAT
2362
        cmp     [hd_error], 0
2363
        jz      @f
2364
.device_err:
2365
        pop     ecx
2366
.device_err2:
2367
        pop     ebp
2368
        push    11
2369
.ret_err:
2370
        pop     eax
2371
        stc
2372
        ret
2373
@@:
2374
        cmp     eax, 2
2375
        jb      .fat_err
2376
        cmp     eax, [fatRESERVED]
2377
        jb      .last_loop
2378
.fat_err:
2379
        pop     ecx ebp
2380
        push    ERROR_FAT_TABLE
2381
        jmp     .ret_err
2382
.last_found:
2383
        push    eax
2384
        call    get_FAT
2385
        cmp     [hd_error], 0
2386
        jz      @f
2387
        pop     eax
2388
        jmp     .device_err
2389
@@:
2390
        cmp     eax, [fatRESERVED]
2391
        pop     eax
2392
        jb      .fat_err
2393
; set length to full number of clusters
2394
        sub     [edi+28], ecx
2395
.start_extend:
2396
        pop     ecx
2397
; now do extend
2398
        push    edx
2399
        mov     edx, 2          ; start scan from cluster 2
2400
.extend_loop:
2401
        cmp     [edi+28], ecx
2402
        jae     .extend_done
2403
; add new cluster
2404
        push    eax
2405
        call    get_free_FAT
2406
        jc      .disk_full
2407
        mov     edx, [fatEND]
2408
        call    set_FAT
2409
        mov     edx, eax
2410
        pop     eax
2411
        test    eax, eax
2412
        jz      .first_cluster
2413
        push    edx
2414
        call    set_FAT
2415
        pop     edx
2416
        jmp     @f
2417
.first_cluster:
2418
        ror     edx, 16
2419
        mov     [edi+20], dx
2420
        ror     edx, 16
2421
        mov     [edi+26], dx
2422
@@:
2423
        push    ecx
2424
        mov     ecx, -1
2425
        call    add_disk_free_space
2426
        pop     ecx
2427
        mov     eax, edx
2428
        cmp     [hd_error], 0
2429
        jnz     .device_err3
2430
        add     [edi+28], ebp
2431
        jmp     .extend_loop
2432
.extend_done:
2433
        mov     [edi+28], ecx
2434
        pop     edx ebp
2435
        xor     eax, eax        ; CF=0
2436
        ret
2437
.device_err3:
2438
        pop     edx
2439
        jmp     .device_err2
2440
.disk_full:
2441
        pop     eax edx ebp
2442
        push    ERROR_DISK_FULL
2443
        pop     eax
2444
        cmp     [hd_error], 0
2445
        jz      @f
2446
        mov     al, 11
2447
@@:     stc
2448
        ret
2449
 
2450
;----------------------------------------------------------------
2451
;
2452
;  fs_HdSetFileEnd - set end of file on hard disk
2453
;
2454
;  esi  points to filename
2455
;  ebx  points to 64-bit number = new file size
2456
;  ecx  ignored (reserved)
2457
;  edx  ignored (reserved)
2458
;
2459
;  ret eax = 0 ok or other = errormsg
2460
;
2461
;--------------------------------------------------------------
2462
fs_HdSetFileEnd:
2463
        cmp     [fs_type], 1
2464
        jz      ntfs_HdSetFileEnd
2465
        cmp     [fs_type], 16
2466
        jz      @f
2467
        cmp     [fs_type], 32
2468
        jz      @f
2469
        push    ERROR_UNKNOWN_FS
2470
.ret:
2471
        pop     eax
2472
        ret
2473
@@:
2474
        cmp     byte [esi], 0
2475
        jnz     @f
2476
.access_denied:
2477
        push    ERROR_ACCESS_DENIED
2478
        jmp     .ret
2479
@@:
2480
        push    edi
2481
        call    hd_find_lfn
2482
        pushfd
2483
        cmp     [hd_error], 0
2484
        jz      @f
2485
        popfd
2486
        push    11
2487
        jmp     .ret
2488
@@:
2489
        popfd
2490
        jnc     @f
2491
        pop     edi
2492
        push    ERROR_FILE_NOT_FOUND
2493
        jmp     .ret
2494
@@:
2495
; must not be directory
2496
        test    byte [edi+11], 10h
2497
        jz      @f
2498
        pop     edi
2499
        jmp     .access_denied
2500
@@:
2501
; file size must not exceed 4 Gb
2502
        cmp     dword [ebx+4], 0
2503
        jz      @f
2504
        pop     edi
2505
        push    ERROR_END_OF_FILE
2506
        jmp     .ret
2507
@@:
2508
        push    eax     ; save directory sector
2509
; set file modification date/time to current
2510
        call    fat_update_datetime
2511
        mov     eax, [ebx]
2512
        cmp     eax, [edi+28]
2513
        jb      .truncate
2514
        ja      .expand
2515
        pop     eax
2516
        mov     ebx, buffer
2517
        call    hd_write
2518
        pop     edi
2519
        xor     eax, eax
2520
        cmp     [hd_error], 0
2521
        jz      @f
2522
        mov     al, 11
2523
@@:
2524
        ret
2525
.expand:
2526
        push    ebx ebp ecx
2527
        push    dword [edi+28]  ; save old size
2528
        mov     ecx, eax
2529
        call    hd_extend_file
2530
        push    eax             ; return code
2531
        jnc     .expand_ok
2532
        cmp     al, ERROR_DISK_FULL
2533
        jz      .disk_full
2534
.pop_ret:
2535
        call    update_disk
2536
        pop     eax ecx ebp ebx ecx edi edi
2537
        ret
2538
.expand_ok:
2539
.disk_full:
2540
; save directory
2541
        mov     eax, [edi+28]
2542
        xchg    eax, [esp+20]
2543
        mov     ebx, buffer
2544
        call    hd_write
2545
        mov     eax, [edi+20-2]
2546
        mov     ax, [edi+26]
2547
        mov     edi, eax
2548
        cmp     [hd_error], 0
2549
        jz      @f
2550
.pop_ret11:
2551
        mov     byte [esp], 11
2552
        jmp     .pop_ret
2553
@@:
2554
; now zero new data
2555
        xor     ebp, ebp
2556
; edi=current cluster, ebp=sector in cluster
2557
; [esp+20]=new size, [esp+4]=old size, [esp]=return code
2558
.zero_loop:
2559
        sub     dword [esp+4], 0x200
2560
        jae     .next_cluster
2561
        lea     eax, [edi-2]
2562
        imul    eax, [SECTORS_PER_CLUSTER]
2563
        add     eax, [DATA_START]
2564
        add     eax, ebp
2565
        cmp     dword [esp+4], -0x200
2566
        jz      .noread
2567
        mov     ebx, buffer
2568
        call    hd_read
2569
        cmp     [hd_error], 0
2570
        jnz     .err_next
2571
.noread:
2572
        mov     ecx, [esp+4]
2573
        neg     ecx
2574
        push    edi
2575
        mov     edi, buffer+0x200
2576
        add     edi, [esp+8]
2577
        push    eax
2578
        xor     eax, eax
2579
        mov     [esp+12], eax
2580
        rep     stosb
2581
        pop     eax
2582
        pop     edi
2583
        call    hd_write
2584
        cmp     [hd_error], 0
2585
        jz      .next_cluster
2586
.err_next:
2587
        mov     byte [esp], 11
2588
.next_cluster:
2589
        sub     dword [esp+20], 0x200
2590
        jbe     .pop_ret
2591
        inc     ebp
2592
        cmp     ebp, [SECTORS_PER_CLUSTER]
2593
        jb      .zero_loop
2594
        xor     ebp, ebp
2595
        mov     eax, edi
2596
        call    get_FAT
2597
        mov     edi, eax
2598
        cmp     [hd_error], 0
2599
        jnz     .pop_ret11
2600
        jmp     .zero_loop
2601
.truncate:
2602
        mov     [edi+28], eax
2603
        push    ecx
2604
        mov     ecx, [edi+20-2]
2605
        mov     cx, [edi+26]
2606
        push    eax
2607
        test    eax, eax
2608
        jz      .zero_size
2609
; find new last cluster
2610
@@:
2611
        mov     eax, [SECTORS_PER_CLUSTER]
2612
        shl     eax, 9
2613
        sub     [esp], eax
2614
        jbe     @f
2615
        mov     eax, ecx
2616
        call    get_FAT
2617
        mov     ecx, eax
2618
        cmp     [hd_error], 0
2619
        jz      @b
2620
.device_err3:
2621
        pop     eax ecx eax edi
2622
        push    11
2623
        pop     eax
2624
        ret
2625
@@:
2626
; we will zero data at the end of last sector - remember it
2627
        push    ecx
2628
; terminate FAT chain
2629
        push    edx
2630
        mov     eax, ecx
2631
        mov     edx, [fatEND]
2632
        call    set_FAT
2633
        mov     eax, edx
2634
        pop     edx
2635
        cmp     [hd_error], 0
2636
        jz      @f
2637
.device_err4:
2638
        pop     ecx
2639
        jmp     .device_err3
2640
.zero_size:
2641
        and     word [edi+20], 0
2642
        and     word [edi+26], 0
2643
        push    0
2644
        mov     eax, ecx
2645
@@:
2646
; delete FAT chain
2647
        call    clear_cluster_chain
2648
        cmp     [hd_error], 0
2649
        jnz     .device_err4
2650
; save directory
2651
        mov     eax, [esp+12]
2652
        push    ebx
2653
        mov     ebx, buffer
2654
        call    hd_write
2655
        pop     ebx
2656
        cmp     [hd_error], 0
2657
        jnz     .device_err4
2658
; zero last sector, ignore errors
2659
        pop     ecx
2660
        pop     eax
2661
        dec     ecx
2662
        imul    ecx, [SECTORS_PER_CLUSTER]
2663
        add     ecx, [DATA_START]
2664
        push    eax
2665
        sar     eax, 9
2666
        add     ecx, eax
2667
        pop     eax
2668
        and     eax, 0x1FF
2669
        jz      .truncate_done
2670
        push    ebx eax
2671
        mov     eax, ecx
2672
        mov     ebx, buffer
2673
        call    hd_read
2674
        pop     eax
2675
        lea     edi, [buffer+eax]
2676
        push    ecx
2677
        mov     ecx, 0x200
2678
        sub     ecx, eax
2679
        xor     eax, eax
2680
        rep     stosb
2681
        pop     eax
2682
        call    hd_write
2683
        pop     ebx
2684
.truncate_done:
2685
        pop     ecx eax edi
2686
        call    update_disk
2687
        xor     eax, eax
2688
        cmp     [hd_error], 0
2689
        jz      @f
2690
        mov     al, 11
2691
@@:
2692
        ret
2693
 
2694
fs_HdGetFileInfo:
2695
        cmp     [fs_type], 1
2696
        jz      ntfs_HdGetFileInfo
2697
        cmp     [fs_type], 16
2698
        jz      @f
2699
        cmp     [fs_type], 32
2700
        jz      @f
2701
        mov     eax, ERROR_UNKNOWN_FS
2702
        ret
2703
@@:
2704
        cmp     byte [esi], 0
2705
        jnz     @f
2706
        mov     eax, 2
2707
        ret
2708
@@:
2709
        push    edi
2710
        call    hd_find_lfn
2711
        pushfd
2712
        cmp     [hd_error], 0
2713
        jz      @f
2714
        popfd
2715
        pop     edi
2716
        mov     eax, 11
2717
        ret
2718
@@:
2719
        popfd
2720
        jmp     fs_GetFileInfo_finish
2721
 
2722
fs_HdSetFileInfo:
2723
        cmp     [fs_type], 1
2724
        jz      ntfs_HdSetFileInfo
2725
        cmp     [fs_type], 16
2726
        jz      @f
2727
        cmp     [fs_type], 32
2728
        jz      @f
2729
        mov     eax, ERROR_UNKNOWN_FS
2730
        ret
2731
@@:
2732
        cmp     byte [esi], 0
2733
        jnz     @f
2734
        mov     eax, 2
2735
        ret
2736
@@:
2737
        push    edi
2738
        call    hd_find_lfn
2739
        pushfd
2740
        cmp     [hd_error], 0
2741
        jz      @f
2742
        popfd
2743
        pop     edi
2744
        mov     eax, 11
2745
        ret
2746
@@:
2747
        popfd
2748
        jnc     @f
2749
        pop     edi
2750
        mov     eax, ERROR_FILE_NOT_FOUND
2751
        ret
2752
@@:
2753
        push    eax
2754
        call    bdfe_to_fat_entry
2755
        pop     eax
2756
        mov     ebx, buffer
2757
        call    hd_write
2758
        call    update_disk
2759
        pop     edi
2760
        xor     eax, eax
2761
        ret
2762
 
2763
;----------------------------------------------------------------
2764
;
2765
;  fs_HdDelete - delete file or empty folder from hard disk
2766
;
2767
;  esi  points to filename
2768
;
2769
;  ret  eax = 0 ok or other = errormsg
2770
;
2771
;--------------------------------------------------------------
2772
fs_HdDelete:
2773
        cmp     [fs_type], 1
2774
        jz      ntfs_HdDelete
2775
        cmp     [fs_type], 16
2776
        jz      @f
2777
        cmp     [fs_type], 32
2778
        jz      @f
2779
        push    ERROR_UNKNOWN_FS
2780
.pop_ret:
2781
        pop     eax
2782
        ret
2783
@@:
2784
        cmp     byte [esi], 0
2785
        jnz     @f
2786
; cannot delete root!
2787
.access_denied:
2788
        push    ERROR_ACCESS_DENIED
2789
        jmp     .pop_ret
2790
@@:
2791
        and     [longname_sec1], 0
2792
        and     [longname_sec2], 0
2793
        push    edi
2794
        call    hd_find_lfn
2795
        jnc     .found
2796
        pop     edi
2797
        push    ERROR_FILE_NOT_FOUND
2798
        jmp     .pop_ret
2799
.found:
2800
        cmp     dword [edi], '.   '
2801
        jz      .access_denied2
2802
        cmp     dword [edi], '..  '
2803
        jz      .access_denied2
2804
        test    byte [edi+11], 10h
2805
        jz      .dodel
2806
; we can delete only empty folders!
2807
        pushad
2808
        mov     ebp, [edi+20-2]
2809
        mov     bp, [edi+26]
2810
        xor     ecx, ecx
2811
        lea     eax, [ebp-2]
2812
        imul    eax, [SECTORS_PER_CLUSTER]
2813
        add     eax, [DATA_START]
2814
        mov     ebx, buffer
2815
        call    hd_read
2816
        cmp     [hd_error], 0
2817
        jnz     .err1
2818
        add     ebx, 2*0x20
2819
.checkempty:
2820
        cmp     byte [ebx], 0
2821
        jz      .empty
2822
        cmp     byte [ebx], 0xE5
2823
        jnz     .notempty
2824
        add     ebx, 0x20
2825
        cmp     ebx, buffer+0x200
2826
        jb      .checkempty
2827
        inc     ecx
2828
        cmp     ecx, [SECTORS_PER_CLUSTER]
2829
        jb      @f
2830
        mov     eax, ebp
2831
        call    get_FAT
2832
        cmp     [hd_error], 0
2833
        jnz     .err1
2834
        mov     ebp, eax
2835
        xor     ecx, ecx
2836
@@:
2837
        lea     eax, [ebp-2]
2838
        imul    eax, [SECTORS_PER_CLUSTER]
2839
        add     eax, [DATA_START]
2840
        add     eax, ecx
2841
        mov     ebx, buffer
2842
        call    hd_read
2843
        cmp     [hd_error], 0
2844
        jz      .checkempty
2845
.err1:
2846
        popad
2847
.err2:
2848
        pop     edi
2849
        push    11
2850
        pop     eax
2851
        ret
2852
.notempty:
2853
        popad
2854
.access_denied2:
2855
        pop     edi
2856
        push    ERROR_ACCESS_DENIED
2857
        pop     eax
2858
        ret
2859
.empty:
2860
        popad
2861
        push    ebx
2862
        mov     ebx, buffer
2863
        call    hd_read
2864
        pop     ebx
2865
        cmp     [hd_error], 0
2866
        jnz     .err2
2867
.dodel:
2868
        push    eax
2869
        mov     eax, [edi+20-2]
2870
        mov     ax, [edi+26]
2871
        xchg    eax, [esp]
2872
; delete folder entry
2873
        mov     byte [edi], 0xE5
2874
; delete LFN (if present)
2875
.lfndel:
2876
        cmp     edi, buffer
2877
        ja      @f
2878
        cmp     [longname_sec2], 0
2879
        jz      .lfndone
2880
        push    [longname_sec2]
2881
        push    [longname_sec1]
2882
        pop     [longname_sec2]
2883
        and     [longname_sec1], 0
2884
        push    ebx
2885
        mov     ebx, buffer
2886
        call    hd_write
2887
        mov     eax, [esp+4]
2888
        call    hd_read
2889
        pop     ebx
2890
        pop     eax
2891
        mov     edi, buffer+0x200
2892
@@:
2893
        sub     edi, 0x20
2894
        cmp     byte [edi], 0xE5
2895
        jz      .lfndone
2896
        cmp     byte [edi+11], 0xF
2897
        jnz     .lfndone
2898
        mov     byte [edi], 0xE5
2899
        jmp     .lfndel
2900
.lfndone:
2901
        push    ebx
2902
        mov     ebx, buffer
2903
        call    hd_write
2904
        pop     ebx
2905
; delete FAT chain
2906
        pop     eax
2907
        call    clear_cluster_chain
2908
        call    update_disk
2909
        pop     edi
2910
        xor     eax, eax
2911
        cmp     [hd_error], 0
2912
        jz      @f
2913
        mov     al, 11
2914
@@:
2915
        ret
2916
 
2917
; \end{diamond}