Subversion Repositories Kolibri OS

Rev

Rev 846 | 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: 848 $
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:
848 serge 894
 
766 Rus 895
	cmp	byte [esi], 0
896
	jnz	@f
897
	or	ebx, -1
898
	mov	eax, 10 	; access denied
899
	ret
71 diamond 900
@@:
766 Rus 901
	push	edi
902
	call	rd_find_lfn
903
	jnc	.found
904
	pop	edi
905
	or	ebx, -1
906
	mov	eax, 5		; file not found
907
	ret
71 diamond 908
.found:
766 Rus 909
	test	ebx, ebx
910
	jz	.l1
911
	cmp	dword [ebx+4], 0
912
	jz	@f
913
	xor	ebx, ebx
71 diamond 914
.reteof:
766 Rus 915
	mov	eax, 6		; EOF
916
	pop	edi
917
	ret
71 diamond 918
@@:
766 Rus 919
	mov	ebx, [ebx]
71 diamond 920
.l1:
766 Rus 921
	push	ecx edx
922
	push	0
923
	mov	eax, [edi+28]
924
	sub	eax, ebx
925
	jb	.eof
926
	cmp	eax, ecx
927
	jae	@f
928
	mov	ecx, eax
929
	mov	byte [esp], 6		; EOF
77 diamond 930
@@:
766 Rus 931
	movzx	edi, word [edi+26]	; cluster
71 diamond 932
.new:
766 Rus 933
	jecxz	.done
934
	test	edi, edi
935
	jz	.eof
936
	cmp	edi, 0xFF8
937
	jae	.eof
938
	lea	eax, [edi+31]		; bootsector+2*fat+filenames
939
	shl	eax, 9			; *512
846 serge 940
        add     eax, [_rd_base]         ; image base
71 diamond 941
; now eax points to data of cluster
766 Rus 942
	sub	ebx, 512
943
	jae	.skip
944
	lea	eax, [eax+ebx+512]
945
	neg	ebx
946
	push	ecx
947
	cmp	ecx, ebx
948
	jbe	@f
949
	mov	ecx, ebx
71 diamond 950
@@:
766 Rus 951
	mov	ebx, edx
952
	call	memmove
953
	add	edx, ecx
954
	sub	[esp], ecx
955
	pop	ecx
956
	xor	ebx, ebx
71 diamond 957
.skip:
766 Rus 958
	movzx	edi, word [edi*2+RAMDISK_FAT]	   ; find next cluster from FAT
959
	jmp	.new
71 diamond 960
.eof:
766 Rus 961
	mov	ebx, edx
962
	pop	eax edx ecx
963
	sub	ebx, edx
964
	jmp	.reteof
71 diamond 965
.done:
766 Rus 966
	mov	ebx, edx
967
	pop	eax edx ecx edi
968
	sub	ebx, edx
969
	ret
71 diamond 970
 
75 diamond 971
;----------------------------------------------------------------
972
;
973
;  fs_RamdiskReadFolder - LFN variant for reading sys floppy folder
974
;
975
;  esi  points to filename; only root is folder on ramdisk
78 diamond 976
;  ebx  pointer to structure 32-bit number = first wanted block
977
;                          & flags (bitfields)
978
; flags: bit 0: 0=ANSI names, 1=UNICODE names
75 diamond 979
;  ecx  number of blocks to read, 0+
980
;  edx  mem location to return data
981
;
982
;  ret ebx = size or 0xffffffff file not found
983
;      eax = 0 ok read or other = errormsg
984
;
985
;--------------------------------------------------------------
986
fs_RamdiskReadFolder:
766 Rus 987
	push	edi
988
	cmp	byte [esi], 0
989
	jz	.root
990
	call	rd_find_lfn
991
	jnc	.found
992
	pop	edi
993
	or	ebx, -1
994
	mov	eax, ERROR_FILE_NOT_FOUND
995
	ret
91 diamond 996
.found:
766 Rus 997
	test	byte [edi+11], 0x10
998
	jnz	.found_dir
999
	pop	edi
1000
	or	ebx, -1
1001
	mov	eax, ERROR_ACCESS_DENIED
1002
	ret
91 diamond 1003
.found_dir:
766 Rus 1004
	movzx	eax, word [edi+26]
1005
	add	eax, 31
1006
	push	0
1007
	jmp	.doit
91 diamond 1008
.root:
766 Rus 1009
	mov	eax, 19
1010
	push	14
91 diamond 1011
.doit:
766 Rus 1012
	push	esi ecx ebp
1013
	sub	esp, 262*2	; reserve space for LFN
1014
	mov	ebp, esp
1015
	push	dword [ebx+4]	; for fat_get_name: read ANSI/UNICODE names
1016
	mov	ebx, [ebx]
75 diamond 1017
; init header
766 Rus 1018
	push	eax ecx
1019
	mov	edi, edx
1020
	mov	ecx, 32/4
1021
	xor	eax, eax
1022
	rep	stosd
1023
	mov	byte [edx], 1	; version
1024
	pop	ecx eax
1025
	mov	esi, edi	; esi points to block of data of folder entry (BDFE)
91 diamond 1026
.main_loop:
766 Rus 1027
	mov	edi, eax
1028
	shl	edi, 9
846 serge 1029
        add     edi, [_rd_base]
766 Rus 1030
	push	eax
75 diamond 1031
.l1:
766 Rus 1032
	call	fat_get_name
1033
	jc	.l2
1034
	cmp	byte [edi+11], 0xF
1035
	jnz	.do_bdfe
1036
	add	edi, 0x20
1037
	test	edi, 0x1FF
1038
	jnz	.do_bdfe
1039
	pop	eax
1040
	inc	eax
1041
	dec	byte [esp+262*2+16]
1042
	jz	.done
1043
	jns	@f
91 diamond 1044
; read next sector from FAT
766 Rus 1045
	mov	eax, [(eax-31-1)*2+RAMDISK_FAT]
1046
	and	eax, 0xFFF
1047
	cmp	eax, 0xFF8
1048
	jae	.done
1049
	add	eax, 31
1050
	mov	byte [esp+262*2+16], 0
75 diamond 1051
@@:
766 Rus 1052
	mov	edi, eax
1053
	shl	edi, 9
846 serge 1054
        add     edi, [_rd_base]
766 Rus 1055
	push	eax
91 diamond 1056
.do_bdfe:
766 Rus 1057
	inc	dword [edx+8]	; new file found
1058
	dec	ebx
1059
	jns	.l2
1060
	dec	ecx
1061
	js	.l2
1062
	inc	dword [edx+4]  ; new file block copied
1063
	call	fat_entry_to_bdfe
75 diamond 1064
.l2:
766 Rus 1065
	add	edi, 0x20
1066
	test	edi, 0x1FF
1067
	jnz	.l1
1068
	pop	eax
1069
	inc	eax
1070
	dec	byte [esp+262*2+16]
1071
	jz	.done
1072
	jns	@f
91 diamond 1073
; read next sector from FAT
766 Rus 1074
	mov	eax, [(eax-31-1)*2+RAMDISK_FAT]
1075
	and	eax, 0xFFF
1076
	cmp	eax, 0xFF8
1077
	jae	.done
1078
	add	eax, 31
1079
	mov	byte [esp+262*2+16], 0
91 diamond 1080
@@:
766 Rus 1081
	jmp	.main_loop
91 diamond 1082
.done:
766 Rus 1083
	add	esp, 262*2+4
1084
	pop	ebp
1085
	mov	ebx, [edx+4]
1086
	xor	eax, eax
1087
	dec	ecx
1088
	js	@f
1089
	mov	al, ERROR_END_OF_FILE
75 diamond 1090
@@:
766 Rus 1091
	pop	ecx esi edi edi
1092
	ret
75 diamond 1093
 
83 diamond 1094
iglobal
1095
label fat_legal_chars byte
1096
; 0 = not allowed
1097
; 1 = allowed only in long names
1098
; 3 = allowed
766 Rus 1099
	times 32 db 0
83 diamond 1100
;                 ! " # $ % & ' ( ) * + , - . /
766 Rus 1101
	db	1,3,0,3,3,3,3,3,3,3,0,1,1,3,3,0
83 diamond 1102
;               0 1 2 3 4 5 6 7 8 9 : ; < = > ?
766 Rus 1103
	db	3,3,3,3,3,3,3,3,3,3,0,1,0,1,0,0
83 diamond 1104
;               @ A B C D E F G H I J K L M N O
766 Rus 1105
	db	3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
83 diamond 1106
;               P Q R S T U V W X Y Z [ \ ] ^ _
766 Rus 1107
	db	3,3,3,3,3,3,3,3,3,3,3,1,0,1,3,3
83 diamond 1108
;               ` a b c d e f g h i j k l m n o
766 Rus 1109
	db	3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
83 diamond 1110
;               p q r s t u v w x y z { | } ~
766 Rus 1111
	db	3,3,3,3,3,3,3,3,3,3,3,3,0,3,3,0
83 diamond 1112
endg
1113
 
1114
fat_name_is_legal:
1115
; in: esi->(long) name
1116
; out: CF set <=> legal
1117
; destroys eax
766 Rus 1118
	push	esi
1119
	xor	eax, eax
83 diamond 1120
@@:
766 Rus 1121
	lodsb
1122
	test	al, al
1123
	jz	.done
1124
	cmp	al, 80h
1125
	jae	.big
1126
	test	[fat_legal_chars+eax], 1
1127
	jnz	@b
83 diamond 1128
.err:
766 Rus 1129
	pop	esi
1130
	clc
1131
	ret
83 diamond 1132
.big:
1133
; 0x80-0xAF, 0xE0-0xEF
766 Rus 1134
	cmp	al, 0xB0
1135
	jb	@b
1136
	cmp	al, 0xE0
1137
	jb	.err
1138
	cmp	al, 0xF0
1139
	jb	@b
1140
	jmp	.err
83 diamond 1141
.done:
766 Rus 1142
	sub	esi, [esp]
1143
	cmp	esi, 257
1144
	pop	esi
1145
	ret
83 diamond 1146
 
1147
fat_next_short_name:
1148
; in: edi->8+3 name
1149
; out: name corrected
1150
;      CF=1 <=> error
766 Rus 1151
	pushad
1152
	mov	ecx, 8
1153
	mov	al, '~'
1154
	std
1155
	push	edi
1156
	add	edi, 7
1157
	repnz	scasb
1158
	pop	edi
1159
	cld
1160
	jz	.tilde
83 diamond 1161
; tilde is not found, insert "~1" at end
766 Rus 1162
	add	edi, 6
1163
	cmp	word [edi], '  '
1164
	jnz	.insert_tilde
1165
@@:	dec	edi
1166
	cmp	byte [edi], ' '
1167
	jz	@b
1168
	inc	edi
83 diamond 1169
.insert_tilde:
766 Rus 1170
	mov	word [edi], '~1'
1171
	popad
1172
	clc
1173
	ret
83 diamond 1174
.tilde:
766 Rus 1175
	push	edi
1176
	add	edi, 7
1177
	xor	ecx, ecx
83 diamond 1178
@@:
1179
; after tilde may be only digits and trailing spaces
766 Rus 1180
	cmp	byte [edi], '~'
1181
	jz	.break
1182
	cmp	byte [edi], ' '
1183
	jz	.space
1184
	cmp	byte [edi], '9'
1185
	jnz	.found
1186
	dec	edi
1187
	jmp	@b
83 diamond 1188
.space:
766 Rus 1189
	dec	edi
1190
	inc	ecx
1191
	jmp	@b
83 diamond 1192
.found:
766 Rus 1193
	inc	byte [edi]
1194
	add	dword [esp], 8
1195
	jmp	.zerorest
83 diamond 1196
.break:
766 Rus 1197
	jecxz	.noplace
1198
	inc	edi
1199
	mov	al, '1'
83 diamond 1200
@@:
766 Rus 1201
	xchg	al, [edi]
1202
	inc	edi
1203
	cmp	al, ' '
1204
	mov	al, '0'
1205
	jnz	@b
466 diamond 1206
.succ:
766 Rus 1207
	pop	edi
1208
	popad
1209
	clc
1210
	ret
83 diamond 1211
.noplace:
766 Rus 1212
	dec	edi
1213
	cmp	edi, [esp]
1214
	jz	.err
1215
	add	dword [esp], 8
1216
	mov	word [edi], '~1'
1217
	inc	edi
1218
	inc	edi
83 diamond 1219
@@:
766 Rus 1220
	mov	byte [edi], '0'
466 diamond 1221
.zerorest:
766 Rus 1222
	inc	edi
1223
	cmp	edi, [esp]
1224
	jb	@b
1225
	pop	edi
1226
	popad
1227
	;clc    ; automatically
1228
	ret
83 diamond 1229
.err:
766 Rus 1230
	pop	edi
1231
	popad
1232
	stc
1233
	ret
83 diamond 1234
 
1235
fat_gen_short_name:
1236
; in: esi->long name
1237
;     edi->buffer (8+3=11 chars)
1238
; out: buffer filled
766 Rus 1239
	pushad
1240
	mov	eax, '    '
1241
	push	edi
1242
	stosd
1243
	stosd
1244
	stosd
1245
	pop	edi
1246
	xor	eax, eax
1247
	push	8
1248
	pop	ebx
1249
	lea	ecx, [edi+8]
83 diamond 1250
.loop:
766 Rus 1251
	lodsb
1252
	test	al, al
1253
	jz	.done
1254
	call	char_toupper
1255
	cmp	al, ' '
1256
	jz	.space
1257
	cmp	al, 80h
1258
	ja	.big
1259
	test	[fat_legal_chars+eax], 2
1260
	jnz	.symbol
83 diamond 1261
.inv_symbol:
766 Rus 1262
	mov	al, '_'
1263
	or	bh, 1
83 diamond 1264
.symbol:
766 Rus 1265
	cmp	al, '.'
1266
	jz	.dot
83 diamond 1267
.normal_symbol:
766 Rus 1268
	dec	bl
1269
	jns	.store
1270
	mov	bl, 0
83 diamond 1271
.space:
766 Rus 1272
	or	bh, 1
1273
	jmp	.loop
83 diamond 1274
.store:
766 Rus 1275
	stosb
1276
	jmp	.loop
83 diamond 1277
.big:
766 Rus 1278
	cmp	al, 0xB0
1279
	jb	.normal_symbol
1280
	cmp	al, 0xE0
1281
	jb	.inv_symbol
1282
	cmp	al, 0xF0
1283
	jb	.normal_symbol
1284
	jmp	.inv_symbol
83 diamond 1285
.dot:
766 Rus 1286
	test	bh, 2
1287
	jz	.firstdot
1288
	pop	ebx
1289
	add	ebx, edi
1290
	sub	ebx, ecx
1291
	push	ebx
1292
	cmp	ebx, ecx
1293
	jb	@f
1294
	pop	ebx
1295
	push	ecx
527 diamond 1296
@@:
766 Rus 1297
	cmp	edi, ecx
1298
	jbe	.skip
83 diamond 1299
@@:
766 Rus 1300
	dec	edi
1301
	mov	al, [edi]
1302
	dec	ebx
1303
	mov	[ebx], al
1304
	mov	byte [edi], ' '
1305
	cmp	edi, ecx
1306
	ja	@b
83 diamond 1307
.skip:
766 Rus 1308
	mov	bh, 3
1309
	jmp	@f
83 diamond 1310
.firstdot:
766 Rus 1311
	cmp	bl, 8
1312
	jz	.space
1313
	push	edi
1314
	or	bh, 2
83 diamond 1315
@@:
766 Rus 1316
	mov	edi, ecx
1317
	mov	bl, 3
1318
	jmp	.loop
83 diamond 1319
.done:
766 Rus 1320
	test	bh, 2
1321
	jz	@f
1322
	pop	edi
83 diamond 1323
@@:
766 Rus 1324
	lea	edi, [ecx-8]
1325
	test	bh, 1
1326
	jz	@f
1327
	call	fat_next_short_name
83 diamond 1328
@@:
766 Rus 1329
	popad
1330
	ret
83 diamond 1331
 
1332
;----------------------------------------------------------------
1333
;
321 diamond 1334
;  fs_RamdiskRewrite - LFN variant for writing ramdisk
1335
;  fs_RamdiskCreateFolder - create folder on ramdisk
83 diamond 1336
;
321 diamond 1337
;  esi  points to file/folder name
83 diamond 1338
;  ebx  ignored (reserved)
321 diamond 1339
;  ecx  number of bytes to write, 0+ (ignored for folders)
1340
;  edx  mem location to data (ignored for folders)
83 diamond 1341
;
1342
;  ret ebx = number of written bytes
1343
;      eax = 0 ok read or other = errormsg
1344
;
1345
;--------------------------------------------------------------
1346
@@:
766 Rus 1347
	mov	eax, ERROR_ACCESS_DENIED
1348
	xor	ebx, ebx
1349
	ret
83 diamond 1350
 
321 diamond 1351
fs_RamdiskCreateFolder:
766 Rus 1352
	mov	al, 1		; create folder
1353
	jmp	fs_RamdiskRewrite.common
321 diamond 1354
 
83 diamond 1355
fs_RamdiskRewrite:
766 Rus 1356
	xor	eax, eax	; create file
321 diamond 1357
.common:
766 Rus 1358
	cmp	byte [esi], 0
1359
	jz	@b
1360
	pushad
1361
	xor	edi, edi
1362
	push	esi
1363
	test	ebp, ebp
1364
	jz	@f
1365
	mov	esi, ebp
83 diamond 1366
@@:
766 Rus 1367
	lodsb
1368
	test	al, al
1369
	jz	@f
1370
	cmp	al, '/'
1371
	jnz	@b
1372
	lea	edi, [esi-1]
1373
	jmp	@b
91 diamond 1374
@@:
766 Rus 1375
	pop	esi
1376
	test	edi, edi
1377
	jnz	.noroot
1378
	test	ebp, ebp
1379
	jnz	.hasebp
1380
	push	ramdisk_root_extend_dir
1381
	push	ramdisk_root_next_write
1382
	push	edi
1383
	push	ramdisk_root_first
1384
	push	ramdisk_root_next
1385
	jmp	.common1
521 diamond 1386
.hasebp:
766 Rus 1387
	mov	eax, ERROR_ACCESS_DENIED
1388
	cmp	byte [ebp], 0
1389
	jz	.ret1
1390
	push	ebp
1391
	xor	ebp, ebp
1392
	call	rd_find_lfn
1393
	pop	esi
1394
	jc	.notfound0
1395
	jmp	.common0
91 diamond 1396
.noroot:
766 Rus 1397
	mov	eax, ERROR_ACCESS_DENIED
1398
	cmp	byte [edi+1], 0
1399
	jz	.ret1
91 diamond 1400
; check existence
766 Rus 1401
	mov	byte [edi], 0
1402
	push	edi
1403
	call	rd_find_lfn
1404
	pop	esi
1405
	mov	byte [esi], '/'
1406
	jnc	@f
521 diamond 1407
.notfound0:
766 Rus 1408
	mov	eax, ERROR_FILE_NOT_FOUND
91 diamond 1409
.ret1:
766 Rus 1410
	mov	[esp+28], eax
1411
	popad
1412
	xor	ebx, ebx
1413
	ret
83 diamond 1414
@@:
766 Rus 1415
	inc	esi
521 diamond 1416
.common0:
766 Rus 1417
	test	byte [edi+11], 0x10	; must be directory
1418
	mov	eax, ERROR_ACCESS_DENIED
1419
	jz	.ret1
1420
	movzx	ebp, word [edi+26]	; ebp=cluster
1421
	mov	eax, ERROR_FAT_TABLE
1422
	cmp	ebp, 2
1423
	jb	.ret1
1424
	cmp	ebp, 2849
1425
	jae	.ret1
1426
	push	ramdisk_notroot_extend_dir
1427
	push	ramdisk_notroot_next_write
1428
	push	ebp
1429
	push	ramdisk_notroot_first
1430
	push	ramdisk_notroot_next
91 diamond 1431
.common1:
766 Rus 1432
	call	fat_find_lfn
1433
	jc	.notfound
321 diamond 1434
; found
766 Rus 1435
	test	byte [edi+11], 10h
1436
	jz	.exists_file
321 diamond 1437
; found directory; if we are creating directory, return OK,
1438
;                  if we are creating file, say "access denied"
766 Rus 1439
	add	esp, 20
1440
	popad
1441
	test	al, al
1442
	mov	eax, ERROR_ACCESS_DENIED
1443
	jz	@f
1444
	mov	al, 0
321 diamond 1445
@@:
766 Rus 1446
	xor	ebx, ebx
1447
	ret
321 diamond 1448
.exists_file:
1449
; found file; if we are creating directory, return "access denied",
1450
;             if we are creating file, delete existing file and continue
766 Rus 1451
	cmp	byte [esp+20+28], 0
1452
	jz	@f
1453
	add	esp, 20
1454
	popad
1455
	mov	eax, ERROR_ACCESS_DENIED
1456
	xor	ebx, ebx
1457
	ret
91 diamond 1458
@@:
1459
; delete FAT chain
766 Rus 1460
	push	edi
1461
	xor	eax, eax
1462
	mov	dword [edi+28], eax	; zero size
1463
	xchg	ax, word [edi+26]	; start cluster
1464
	test	eax, eax
1465
	jz	.done1
83 diamond 1466
@@:
766 Rus 1467
	cmp	eax, 0xFF8
1468
	jae	.done1
1469
	lea	edi, [RAMDISK_FAT + eax*2] ; position in FAT
1470
	xor	eax, eax
1471
	xchg	ax, [edi]
1472
	jmp	@b
83 diamond 1473
.done1:
766 Rus 1474
	pop	edi
1475
	call	get_time_for_file
1476
	mov	[edi+22], ax
1477
	call	get_date_for_file
1478
	mov	[edi+24], ax
1479
	mov	[edi+18], ax
1480
	or	byte [edi+11], 20h	; set 'archive' attribute
1481
	jmp	.doit
83 diamond 1482
.notfound:
1483
; file is not found; generate short name
766 Rus 1484
	call	fat_name_is_legal
1485
	jc	@f
1486
	add	esp, 20
1487
	popad
1488
	mov	eax, ERROR_FILE_NOT_FOUND
1489
	xor	ebx, ebx
1490
	ret
83 diamond 1491
@@:
766 Rus 1492
	sub	esp, 12
1493
	mov	edi, esp
1494
	call	fat_gen_short_name
83 diamond 1495
.test_short_name_loop:
766 Rus 1496
	push	esi edi ecx
1497
	mov	esi, edi
1498
	lea	eax, [esp+12+12+8]
1499
	mov	[eax], ebp
1500
	call	dword [eax-4]
1501
	jc	.found
83 diamond 1502
.test_short_name_entry:
766 Rus 1503
	cmp	byte [edi+11], 0xF
1504
	jz	.test_short_name_cont
1505
	mov	ecx, 11
1506
	push	esi edi
1507
	repz	cmpsb
1508
	pop	edi esi
1509
	jz	.short_name_found
83 diamond 1510
.test_short_name_cont:
766 Rus 1511
	lea	eax, [esp+12+12+8]
1512
	call	dword [eax-8]
1513
	jnc	.test_short_name_entry
1514
	jmp	.found
83 diamond 1515
.short_name_found:
766 Rus 1516
	pop	ecx edi esi
1517
	call	fat_next_short_name
1518
	jnc	.test_short_name_loop
83 diamond 1519
.disk_full:
766 Rus 1520
	add	esp, 12+20
1521
	popad
1522
	mov	eax, ERROR_DISK_FULL
1523
	xor	ebx, ebx
1524
	ret
83 diamond 1525
.found:
766 Rus 1526
	pop	ecx edi esi
83 diamond 1527
; now find space in directory
1528
; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
766 Rus 1529
	mov	al, '~'
1530
	push	ecx edi
1531
	mov	ecx, 8
1532
	repnz	scasb
1533
	push	1
1534
	pop	eax	; 1 entry
1535
	jnz	.notilde
83 diamond 1536
; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
766 Rus 1537
	xor	eax, eax
83 diamond 1538
@@:
766 Rus 1539
	cmp	byte [esi], 0
1540
	jz	@f
1541
	inc	esi
1542
	inc	eax
1543
	jmp	@b
83 diamond 1544
@@:
766 Rus 1545
	sub	esi, eax
1546
	add	eax, 12+13
1547
	mov	ecx, 13
1548
	push	edx
1549
	cdq
1550
	div	ecx
1551
	pop	edx
83 diamond 1552
.notilde:
766 Rus 1553
	push	-1
1554
	push	-1
83 diamond 1555
; find  successive entries in directory
766 Rus 1556
	xor	ecx, ecx
1557
	push	eax
1558
	lea	eax, [esp+12+8+12+8]
1559
	mov	[eax], ebp
1560
	call	dword [eax-4]
1561
	pop	eax
83 diamond 1562
.scan_dir:
766 Rus 1563
	cmp	byte [edi], 0
1564
	jz	.free
1565
	cmp	byte [edi], 0xE5
1566
	jz	.free
1567
	xor	ecx, ecx
83 diamond 1568
.scan_cont:
766 Rus 1569
	push	eax
1570
	lea	eax, [esp+12+8+12+8]
1571
	call	dword [eax-8]
1572
	pop	eax
1573
	jnc	.scan_dir
1574
	push	eax
1575
	lea	eax, [esp+12+8+12+8]
1576
	call	dword [eax+8]		; extend directory
1577
	pop	eax
1578
	jnc	.scan_dir
1579
	add	esp, 8+8+12+20
1580
	popad
1581
	mov	eax, ERROR_DISK_FULL
1582
	xor	ebx, ebx
1583
	ret
83 diamond 1584
.free:
766 Rus 1585
	test	ecx, ecx
1586
	jnz	@f
1587
	mov	[esp], edi
1588
	mov	ecx, [esp+8+8+12+8]
1589
	mov	[esp+4], ecx
1590
	xor	ecx, ecx
91 diamond 1591
@@:
766 Rus 1592
	inc	ecx
1593
	cmp	ecx, eax
1594
	jb	.scan_cont
83 diamond 1595
; found!
1596
; calculate name checksum
766 Rus 1597
	push	esi ecx
1598
	mov	esi, [esp+8+8]
1599
	mov	ecx, 11
1600
	xor	eax, eax
83 diamond 1601
@@:
766 Rus 1602
	ror	al, 1
1603
	add	al, [esi]
1604
	inc	esi
1605
	loop	@b
1606
	pop	ecx esi
1607
	pop	edi
1608
	pop	dword [esp+8+12+8]
83 diamond 1609
; edi points to last entry in free chunk
766 Rus 1610
	dec	ecx
1611
	jz	.nolfn
1612
	push	esi
1613
	push	eax
1614
	mov	al, 40h
83 diamond 1615
.writelfn:
766 Rus 1616
	or	al, cl
1617
	mov	esi, [esp+4]
1618
	push	ecx
1619
	dec	ecx
1620
	imul	ecx, 13
1621
	add	esi, ecx
1622
	stosb
1623
	mov	cl, 5
1624
	call	.read_symbols
1625
	mov	ax, 0xF
1626
	stosw
1627
	mov	al, [esp+4]
1628
	stosb
1629
	mov	cl, 6
1630
	call	.read_symbols
1631
	xor	eax, eax
1632
	stosw
1633
	mov	cl, 2
1634
	call	.read_symbols
1635
	pop	ecx
1636
	lea	eax, [esp+8+8+12+8]
1637
	call	dword [eax+4]	; next write
1638
	xor	eax, eax
1639
	loop	.writelfn
1640
	pop	eax
1641
	pop	esi
83 diamond 1642
.nolfn:
766 Rus 1643
	xchg	esi, [esp]
1644
	mov	ecx, 11
1645
	rep	movsb
1646
	mov	word [edi], 20h 	; attributes
1647
	sub	edi, 11
1648
	pop	esi ecx
1649
	add	esp, 12
1650
	mov	byte [edi+13], 0	; tenths of a second at file creation time
1651
	call	get_time_for_file
1652
	mov	[edi+14], ax		; creation time
1653
	mov	[edi+22], ax		; last write time
1654
	call	get_date_for_file
1655
	mov	[edi+16], ax		; creation date
1656
	mov	[edi+24], ax		; last write date
1657
	mov	[edi+18], ax		; last access date
1658
	and	word [edi+20], 0	; high word of cluster
1659
	and	word [edi+26], 0	; low word of cluster - to be filled
1660
	and	dword [edi+28], 0	; file size - to be filled
1661
	cmp	byte [esp+20+28], 0
1662
	jz	.doit
321 diamond 1663
; create directory
766 Rus 1664
	mov	byte [edi+11], 10h	   ; attributes: folder
1665
	mov	ecx, 32*2
1666
	mov	edx, edi
83 diamond 1667
.doit:
766 Rus 1668
	push	edx
1669
	push	ecx
1670
	push	edi
1671
	add	edi, 26 	; edi points to low word of cluster
1672
	push	edi
1673
	jecxz	.done
1674
	mov	ecx, 2849
1675
	mov	edi, RAMDISK_FAT
83 diamond 1676
.write_loop:
1677
; allocate new cluster
766 Rus 1678
	xor	eax, eax
1679
	repnz	scasw
1680
	jnz	.disk_full2
1681
	dec	edi
1682
	dec	edi
465 serge 1683
 
1684
    ;    lea     eax, [edi-(RAMDISK_FAT)]
1685
 
766 Rus 1686
	mov eax, edi
1687
	sub eax, RAMDISK_FAT
465 serge 1688
 
766 Rus 1689
	shr	eax, 1			; eax = cluster
1690
	mov	word [edi], 0xFFF	; mark as last cluster
1691
	xchg	edi, [esp]
1692
	stosw
1693
	pop	edi
1694
	push	edi
1695
	inc	ecx
83 diamond 1696
; write data
766 Rus 1697
	cmp	byte [esp+16+20+28], 0
1698
	jnz	.writedir
1699
	shl	eax, 9
846 serge 1700
        add     eax, 31*512
1701
        add     eax, [_rd_base]
321 diamond 1702
.writefile:
766 Rus 1703
	mov	ebx, edx
1704
	xchg	eax, ebx
1705
	push	ecx
1706
	mov	ecx, 512
1707
	cmp	dword [esp+12], ecx
1708
	jae	@f
1709
	mov	ecx, [esp+12]
83 diamond 1710
@@:
766 Rus 1711
	call	memmove
1712
	add	edx, ecx
1713
	sub	[esp+12], ecx
1714
	pop	ecx
1715
	jnz	.write_loop
83 diamond 1716
.done:
766 Rus 1717
	mov	ebx, edx
1718
	pop	edi edi ecx edx
1719
	sub	ebx, edx
1720
	mov	[edi+28], ebx
1721
	add	esp, 20
1722
	mov	[esp+16], ebx
1723
	popad
1724
	xor	eax, eax
1725
	ret
83 diamond 1726
.disk_full2:
766 Rus 1727
	mov	ebx, edx
1728
	pop	edi edi ecx edx
1729
	sub	ebx, edx
1730
	mov	[edi+28], ebx
1731
	add	esp, 20
1732
	mov	[esp+16], ebx
1733
	popad
1734
	push	ERROR_DISK_FULL
1735
	pop	eax
1736
	ret
321 diamond 1737
.writedir:
766 Rus 1738
	mov	edi, eax
1739
	shl	edi, 9
846 serge 1740
        add     edi, [_rd_base]
1741
        add     edi, 31*512
766 Rus 1742
	mov	esi, edx
1743
	mov	ecx, 32/4
1744
	push	ecx
1745
	rep	movsd
1746
	mov	dword [edi-32], '.   '
1747
	mov	dword [edi-32+4], '    '
1748
	mov	dword [edi-32+8], '    '
1749
	mov	byte [edi-32+11], 10h
1750
	mov	word [edi-32+26], ax
1751
	mov	esi, edx
1752
	pop	ecx
1753
	rep	movsd
1754
	mov	dword [edi-32], '..  '
1755
	mov	dword [edi-32+4], '    '
1756
	mov	dword [edi-32+8], '    '
1757
	mov	byte [edi-32+11], 10h
1758
	mov	eax, [esp+16+8]
1759
	mov	word [edi-32+26], ax
1760
	xor	eax, eax
1761
	mov	ecx, (512-32*2)/4
1762
	rep	stosd
1763
	pop	edi edi ecx edx
1764
	add	esp, 20
1765
	popad
1766
	xor	eax, eax
1767
	xor	ebx, ebx
1768
	ret
83 diamond 1769
 
1770
.read_symbol:
766 Rus 1771
	or	ax, -1
1772
	test	esi, esi
1773
	jz	.retFFFF
1774
	lodsb
1775
	test	al, al
1776
	jnz	ansi2uni_char
1777
	xor	eax, eax
1778
	xor	esi, esi
83 diamond 1779
.retFFFF:
766 Rus 1780
	ret
83 diamond 1781
 
1782
.read_symbols:
766 Rus 1783
	call	.read_symbol
1784
	stosw
1785
	loop	.read_symbols
1786
	ret
83 diamond 1787
 
131 diamond 1788
;----------------------------------------------------------------
1789
;
1790
;  fs_RamdiskWrite - LFN variant for writing to sys floppy
1791
;
1792
;  esi  points to filename
1793
;  ebx  pointer to 64-bit number = first wanted byte, 0+
1794
;       may be ebx=0 - start from first byte
1795
;  ecx  number of bytes to write, 0+
1796
;  edx  mem location to data
1797
;
1798
;  ret ebx = bytes written (maybe 0)
1799
;      eax = 0 ok write or other = errormsg
1800
;
1801
;--------------------------------------------------------------
1802
@@:
766 Rus 1803
	push	ERROR_ACCESS_DENIED
131 diamond 1804
fs_RamdiskWrite.ret0:
766 Rus 1805
	pop	eax
1806
	xor	ebx, ebx
1807
	ret
131 diamond 1808
 
1809
fs_RamdiskWrite:
766 Rus 1810
	cmp	byte [esi], 0
1811
	jz	@b
1812
	pushad
1813
	call	rd_find_lfn
1814
	jnc	.found
1815
	popad
1816
	push	ERROR_FILE_NOT_FOUND
1817
	jmp	.ret0
131 diamond 1818
.found:
1819
; must not be directory
766 Rus 1820
	test	byte [edi+11], 10h
1821
	jz	@f
1822
	popad
1823
	push	ERROR_ACCESS_DENIED
1824
	jmp	.ret0
131 diamond 1825
@@:
1826
; FAT does not support files larger than 4GB
766 Rus 1827
	test	ebx, ebx
1828
	jz	.l1
1829
	cmp	dword [ebx+4], 0
1830
	jz	@f
131 diamond 1831
.eof:
766 Rus 1832
	popad
1833
	push	ERROR_END_OF_FILE
1834
	jmp	.ret0
131 diamond 1835
@@:
766 Rus 1836
	mov	ebx, [ebx]
131 diamond 1837
.l1:
1838
; now edi points to direntry, ebx=start byte to write,
1839
; ecx=number of bytes to write, edx=data pointer
766 Rus 1840
	call	fat_update_datetime
131 diamond 1841
 
1842
; extend file if needed
766 Rus 1843
	add	ecx, ebx
1844
	jc	.eof	; FAT does not support files larger than 4GB
1845
	push	0	; return value=0
1846
	cmp	ecx, [edi+28]
1847
	jbe	.length_ok
1848
	cmp	ecx, ebx
1849
	jz	.length_ok
1850
	call	ramdisk_extend_file
1851
	jnc	.length_ok
131 diamond 1852
; ramdisk_extend_file can return two error codes: FAT table error or disk full.
1853
; First case is fatal error, in second case we may write some data
766 Rus 1854
	mov	[esp], eax
1855
	cmp	al, ERROR_DISK_FULL
1856
	jz	.disk_full
1857
	pop	eax
1858
	mov	[esp+28], eax
1859
	popad
1860
	xor	ebx, ebx
1861
	ret
131 diamond 1862
.disk_full:
1863
; correct number of bytes to write
766 Rus 1864
	mov	ecx, [edi+28]
1865
	cmp	ecx, ebx
1866
	ja	.length_ok
131 diamond 1867
.ret:
766 Rus 1868
	pop	eax
1869
	mov	[esp+28], eax	; eax=return value
1870
	sub	edx, [esp+20]
1871
	mov	[esp+16], edx	; ebx=number of written bytes
1872
	popad
1873
	ret
131 diamond 1874
.length_ok:
1875
; now ebx=start pos, ecx=end pos, both lie inside file
766 Rus 1876
	sub	ecx, ebx
1877
	jz	.ret
1878
	movzx	edi, word [edi+26]	; starting cluster
131 diamond 1879
.write_loop:
766 Rus 1880
	sub	ebx, 0x200
1881
	jae	.next_cluster
1882
	push	ecx
1883
	neg	ebx
1884
	cmp	ecx, ebx
1885
	jbe	@f
1886
	mov	ecx, ebx
131 diamond 1887
@@:
766 Rus 1888
	mov	eax, edi
1889
	shl	eax, 9
846 serge 1890
        add     eax, [_rd_base]
1891
        add     eax, 31*512+0x200
766 Rus 1892
	sub	eax, ebx
1893
	mov	ebx, eax
1894
	mov	eax, edx
1895
	call	memmove
1896
	xor	ebx, ebx
1897
	add	edx, ecx
1898
	sub	[esp], ecx
1899
	pop	ecx
1900
	jz	.ret
131 diamond 1901
.next_cluster:
766 Rus 1902
	movzx	edi, word [edi*2+RAMDISK_FAT]
1903
	jmp	.write_loop
131 diamond 1904
 
1905
ramdisk_extend_file.zero_size:
766 Rus 1906
	xor	eax, eax
1907
	jmp	ramdisk_extend_file.start_extend
131 diamond 1908
 
1909
; extends file on ramdisk to given size, new data area is filled by 0
1910
; in: edi->direntry, ecx=new size
133 diamond 1911
; out: CF=0 => OK, eax=0
131 diamond 1912
;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL)
1913
ramdisk_extend_file:
766 Rus 1914
	push	ecx
131 diamond 1915
; find the last cluster of file
766 Rus 1916
	movzx	eax, word [edi+26]	; first cluster
1917
	mov	ecx, [edi+28]
1918
	jecxz	.zero_size
131 diamond 1919
@@:
766 Rus 1920
	sub	ecx, 0x200
1921
	jbe	@f
1922
	mov	eax, [eax*2+RAMDISK_FAT]
1923
	and	eax, 0xFFF
1924
	jz	.fat_err
1925
	cmp	eax, 0xFF8
1926
	jb	@b
131 diamond 1927
.fat_err:
766 Rus 1928
	pop	ecx
1929
	push	ERROR_FAT_TABLE
1930
	pop	eax
1931
	stc
1932
	ret
131 diamond 1933
@@:
766 Rus 1934
	push	eax
1935
	mov	eax, [eax*2+RAMDISK_FAT]
1936
	and	eax, 0xFFF
1937
	cmp	eax, 0xFF8
1938
	pop	eax
1939
	jb	.fat_err
131 diamond 1940
; set length to full number of sectors and make sure that last sector is zero-padded
766 Rus 1941
	sub	[edi+28], ecx
1942
	push	eax edi
1943
	mov	edi, eax
1944
	shl	edi, 9
846 serge 1945
        add edi, [_rd_base]
1946
        lea     edi, [edi+31*512+0x200+ecx]
766 Rus 1947
	neg	ecx
1948
	xor	eax, eax
1949
	rep	stosb
1950
	pop	edi eax
131 diamond 1951
.start_extend:
766 Rus 1952
	pop	ecx
131 diamond 1953
; now do extend
766 Rus 1954
	push	edx esi
1955
	mov	esi, RAMDISK_FAT+2*2	   ; start scan from cluster 2
1956
	mov	edx, 2847		; number of clusters to scan
131 diamond 1957
.extend_loop:
766 Rus 1958
	cmp	[edi+28], ecx
1959
	jae	.extend_done
131 diamond 1960
; add new sector
766 Rus 1961
	push	ecx
1962
	mov	ecx, edx
1963
	push	edi
1964
	mov	edi, esi
1965
	jecxz	.disk_full
1966
	push	eax
1967
	xor	eax, eax
1968
	repnz	scasw
1969
	pop	eax
1970
	jnz	.disk_full
1971
	mov	word [edi-2], 0xFFF
1972
	mov	esi, edi
1973
	mov	edx, ecx
1974
	sub	edi, RAMDISK_FAT
1975
	shr	edi, 1
1976
	dec	edi	; now edi=new cluster
1977
	test	eax, eax
1978
	jz	.first_cluster
1979
	mov	[RAMDISK_FAT+eax*2], di
1980
	jmp	@f
131 diamond 1981
.first_cluster:
766 Rus 1982
	pop	eax	; eax->direntry
1983
	push	eax
1984
	mov	[eax+26], di
131 diamond 1985
@@:
766 Rus 1986
	push	edi
1987
	shl	edi, 9
846 serge 1988
        add edi, [_rd_base]
1989
        add     edi, 31*512
766 Rus 1990
	xor	eax, eax
1991
	mov	ecx, 512/4
1992
	rep	stosd
1993
	pop	eax	; eax=new cluster
1994
	pop	edi	; edi->direntry
1995
	pop	ecx	; ecx=required size
1996
	add	dword [edi+28], 0x200
1997
	jmp	.extend_loop
131 diamond 1998
.extend_done:
766 Rus 1999
	mov	[edi+28], ecx
2000
	pop	esi edx
2001
	xor	eax, eax	; CF=0
2002
	ret
131 diamond 2003
.disk_full:
766 Rus 2004
	pop	edi ecx
2005
	pop	esi edx
2006
	stc
2007
	push	ERROR_DISK_FULL
2008
	pop	eax
2009
	ret
131 diamond 2010
 
133 diamond 2011
fat_update_datetime:
766 Rus 2012
	call	get_time_for_file
2013
	mov	[edi+22], ax		; last write time
2014
	call	get_date_for_file
2015
	mov	[edi+24], ax		; last write date
2016
	mov	[edi+18], ax		; last access date
2017
	ret
133 diamond 2018
 
2019
;----------------------------------------------------------------
2020
;
2021
;  fs_RamdiskSetFileEnd - set end of file on ramdisk
2022
;
2023
;  esi  points to filename
2024
;  ebx  points to 64-bit number = new file size
2025
;  ecx  ignored (reserved)
2026
;  edx  ignored (reserved)
2027
;
2028
;  ret eax = 0 ok or other = errormsg
2029
;
2030
;--------------------------------------------------------------
2031
fs_RamdiskSetFileEnd:
766 Rus 2032
	cmp	byte [esi], 0
2033
	jnz	@f
133 diamond 2034
.access_denied:
766 Rus 2035
	push	ERROR_ACCESS_DENIED
2036
	jmp	.ret
133 diamond 2037
@@:
766 Rus 2038
	push	edi
2039
	call	rd_find_lfn
2040
	jnc	@f
2041
	pop	edi
2042
	push	ERROR_FILE_NOT_FOUND
133 diamond 2043
.ret:
766 Rus 2044
	pop	eax
2045
	ret
133 diamond 2046
@@:
2047
; must not be directory
766 Rus 2048
	test	byte [edi+11], 10h
2049
	jz	@f
2050
	pop	edi
2051
	jmp	.access_denied
133 diamond 2052
@@:
2053
; file size must not exceed 4Gb
766 Rus 2054
	cmp	dword [ebx+4], 0
2055
	jz	@f
2056
	pop	edi
2057
	push	ERROR_END_OF_FILE
2058
	jmp	.ret
133 diamond 2059
@@:
2060
; set file modification date/time to current
766 Rus 2061
	call	fat_update_datetime
2062
	mov	eax, [ebx]
2063
	cmp	eax, [edi+28]
2064
	jb	.truncate
2065
	ja	.expand
2066
	pop	edi
2067
	xor	eax, eax
2068
	ret
133 diamond 2069
.expand:
766 Rus 2070
	push	ecx
2071
	mov	ecx, eax
2072
	call	ramdisk_extend_file
2073
	pop	ecx
2074
	pop	edi
2075
	ret
133 diamond 2076
.truncate:
766 Rus 2077
	mov	[edi+28], eax
2078
	push	ecx
2079
	movzx	ecx, word [edi+26]
2080
	test	eax, eax
2081
	jz	.zero_size
133 diamond 2082
; find new last sector
2083
@@:
766 Rus 2084
	sub	eax, 0x200
2085
	jbe	@f
2086
	movzx	ecx, word [RAMDISK_FAT+ecx*2]
2087
	jmp	@b
133 diamond 2088
@@:
2089
; zero data at the end of last sector
766 Rus 2090
	push	ecx
2091
	mov	edi, ecx
2092
	shl	edi, 9
846 serge 2093
        add edi, [_rd_base]
2094
        lea     edi, [edi+31*512+eax+0x200]
766 Rus 2095
	mov	ecx, eax
2096
	neg	ecx
2097
	xor	eax, eax
2098
	rep	stosb
2099
	pop	ecx
133 diamond 2100
; terminate FAT chain
766 Rus 2101
	lea	ecx, [RAMDISK_FAT+ecx+ecx]
2102
	push	dword [ecx]
2103
	mov	word [ecx], 0xFFF
2104
	pop	ecx
2105
	and	ecx, 0xFFF
2106
	jmp	.delete
133 diamond 2107
.zero_size:
766 Rus 2108
	and	word [edi+26], 0
133 diamond 2109
.delete:
2110
; delete FAT chain starting with ecx
2111
; mark all clusters as free
766 Rus 2112
	cmp	ecx, 0xFF8
2113
	jae	.deleted
2114
	lea	ecx, [RAMDISK_FAT+ecx+ecx]
2115
	push	dword [ecx]
2116
	and	word [ecx], 0
2117
	pop	ecx
2118
	and	ecx, 0xFFF
2119
	jmp	.delete
133 diamond 2120
.deleted:
766 Rus 2121
	pop	ecx
2122
	pop	edi
2123
	xor	eax, eax
2124
	ret
133 diamond 2125
 
86 diamond 2126
fs_RamdiskGetFileInfo:
766 Rus 2127
	cmp	byte [esi], 0
2128
	jnz	@f
2129
	mov	eax, 2	; unsupported
2130
	ret
86 diamond 2131
@@:
766 Rus 2132
	push	edi
2133
	call	rd_find_lfn
86 diamond 2134
fs_GetFileInfo_finish:
766 Rus 2135
	jnc	@f
2136
	pop	edi
2137
	mov	eax, ERROR_FILE_NOT_FOUND
2138
	ret
86 diamond 2139
@@:
766 Rus 2140
	push	esi ebp
2141
	xor	ebp, ebp
2142
	mov	esi, edx
2143
	and	dword [esi+4], 0
2144
	call	fat_entry_to_bdfe2
2145
	pop	ebp esi
2146
	pop	edi
2147
	xor	eax, eax
2148
	ret
86 diamond 2149
 
2150
fs_RamdiskSetFileInfo:
766 Rus 2151
	cmp	byte [esi], 0
2152
	jnz	@f
2153
	mov	eax, 2	; unsupported
2154
	ret
86 diamond 2155
@@:
766 Rus 2156
	push	edi
2157
	call	rd_find_lfn
2158
	jnc	@f
2159
	pop	edi
2160
	mov	eax, ERROR_FILE_NOT_FOUND
2161
	ret
86 diamond 2162
@@:
766 Rus 2163
	call	bdfe_to_fat_entry
2164
	pop	edi
2165
	xor	eax, eax
2166
	ret
86 diamond 2167
 
91 diamond 2168
;----------------------------------------------------------------
2169
;
171 diamond 2170
;  fs_RamdiskDelete - delete file or empty folder from ramdisk
2171
;
2172
;  esi  points to filename
2173
;
2174
;  ret  eax = 0 ok or other = errormsg
2175
;
2176
;--------------------------------------------------------------
2177
fs_RamdiskDelete:
766 Rus 2178
	cmp	byte [esi], 0
2179
	jnz	@f
171 diamond 2180
; cannot delete root!
2181
.access_denied:
766 Rus 2182
	push	ERROR_ACCESS_DENIED
171 diamond 2183
.pop_ret:
766 Rus 2184
	pop	eax
2185
	ret
171 diamond 2186
@@:
766 Rus 2187
	and	[rd_prev_sector], 0
2188
	and	[rd_prev_prev_sector], 0
2189
	push	edi
2190
	call	rd_find_lfn
2191
	jnc	.found
2192
	pop	edi
2193
	push	ERROR_FILE_NOT_FOUND
2194
	jmp	.pop_ret
171 diamond 2195
.found:
766 Rus 2196
	cmp	dword [edi], '.   '
2197
	jz	.access_denied2
2198
	cmp	dword [edi], '..  '
2199
	jz	.access_denied2
2200
	test	byte [edi+11], 10h
2201
	jz	.dodel
171 diamond 2202
; we can delete only empty folders!
766 Rus 2203
	movzx	eax, word [edi+26]
2204
	push	ebx
2205
	mov	ebx, eax
2206
	shl	ebx, 9
846 serge 2207
        add ebx, [_rd_base]
2208
        add     ebx, 31*0x200 + 2*0x20
171 diamond 2209
.checkempty:
766 Rus 2210
	cmp	byte [ebx], 0
2211
	jz	.empty
2212
	cmp	byte [ebx], 0xE5
2213
	jnz	.notempty
2214
	add	ebx, 0x20
2215
	test	ebx, 0x1FF
2216
	jnz	.checkempty
2217
	movzx	eax, word [RAMDISK_FAT + eax*2]
2218
	test	eax, eax
2219
	jz	.empty
2220
	mov	ebx, eax
2221
	shl	ebx, 9
846 serge 2222
        add ebx, [_rd_base]
2223
        add     ebx, 31*0x200
766 Rus 2224
	jmp	.checkempty
171 diamond 2225
.notempty:
766 Rus 2226
	pop	ebx
171 diamond 2227
.access_denied2:
766 Rus 2228
	pop	edi
2229
	jmp	.access_denied
171 diamond 2230
.empty:
766 Rus 2231
	pop	ebx
171 diamond 2232
.dodel:
766 Rus 2233
	movzx	eax, word [edi+26]
171 diamond 2234
; delete folder entry
766 Rus 2235
	mov	byte [edi], 0xE5
171 diamond 2236
; delete LFN (if present)
2237
.lfndel:
766 Rus 2238
	test	edi, 0x1FF
2239
	jnz	@f
2240
	cmp	[rd_prev_sector], 0
2241
	jz	@f
2242
	cmp	[rd_prev_sector], -1
2243
	jz	.lfndone
2244
	mov	edi, [rd_prev_sector]
2245
	push	[rd_prev_prev_sector]
2246
	pop	[rd_prev_sector]
2247
	or	[rd_prev_prev_sector], -1
2248
	shl	edi, 9
846 serge 2249
        add edi, [_rd_base]
2250
        add     edi, 31*0x200 + 0x200
171 diamond 2251
@@:
766 Rus 2252
	sub	edi, 0x20
2253
	cmp	byte [edi], 0xE5
2254
	jz	.lfndone
2255
	cmp	byte [edi+11], 0xF
2256
	jnz	.lfndone
2257
	mov	byte [edi], 0xE5
2258
	jmp	.lfndel
171 diamond 2259
.lfndone:
2260
; delete FAT chain
766 Rus 2261
	test	eax, eax
2262
	jz	.done
2263
	lea	eax, [RAMDISK_FAT + eax*2]
2264
	push	dword [eax]
2265
	and	word [eax], 0
2266
	pop	eax
2267
	and	eax, 0xFFF
2268
	jmp	.lfndone
171 diamond 2269
.done:
766 Rus 2270
	pop	edi
2271
	xor	eax, eax
2272
	ret
171 diamond 2273
 
78 diamond 2274
; \end{diamond}