Subversion Repositories Kolibri OS

Rev

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

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