Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1 ha 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                      ;;
3
;; FAT12.INC                                                            ;;
4
;; (C) 2005 Mario79, License: GPL                                       ;;
5
;;                                                                      ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
n_sector    dd 0  ; temporary save for sector value
9
flp_status  dd 0
10
clust_tmp_flp dd 0  ; used by analyze_directory and analyze_directory_to_write
11
path_pointer_flp dd 0
12
pointer_file_name_flp dd 0
13
save_root_flag db 0
14
save_flag   db 0
15
root_read   db 0  ; 0-necessary to load root, 1-not to load root
16
flp_fat     db 0  ; 0-necessary to load fat, 1-not to load fat
17
flp_number  db 0  ; 1- Floppy A, 2-Floppy B
18
old_track   db 0  ; old value track
19
flp_label   rb 15 ; Label and ID of inserted floppy disk
20
 
21
reserve_flp:
22
 
23
    cli
24
    cmp   [flp_status],0
25
    je    reserve_flp_ok
26
 
27
    sti
28
    call  change_task
29
    jmp   reserve_flp
30
 
31
  reserve_flp_ok:
32
 
33
    push  eax
34
    mov   eax,[0x3000]
35
    shl   eax,5
115 poddubny 36
    mov   eax,[eax+0x3000+TASKDATA.pid]
1 ha 37
    mov   [flp_status],eax
38
    pop   eax
39
    sti
40
    ret
41
 
42
floppy_free_space:
43
;---------------------------------------------
44
;
45
; returns free space in edi
46
;
47
;---------------------------------------------
48
    push   eax ebx ecx
49
    call   read_flp_fat
50
    cmp    [FDC_Status],0
51
    jne    fdc_status_error_2
133 diamond 52
    mov    eax,0x282004
1 ha 53
    xor    edi,edi
54
    mov    ecx,2847   ;1448000/512
55
rdfs1_1:
56
    mov    ebx,[eax]
57
    and    ebx,4095
58
    jne    rdfs2_1
59
    add    edi,512
60
rdfs2_1:
61
    add    eax,2
62
    loop   rdfs1_1
63
fdc_status_error_2:
64
    pop    ecx ebx eax
65
    ret
66
 
67
 
131 diamond 68
 
69
 
1 ha 70
floppy_fileread:
71
;----------------------------------------------------------------
72
;
73
;  fileread - sys floppy
74
;
75
;  eax  points to filename 11 chars  - for root directory
76
;  ebx  first wanted block       ; 1+ ; if 0 then set to 1
77
;  ecx  number of blocks to read ; 1+ ; if 0 then set to 1
78
;  edx  mem location to return data
79
;  esi  length of filename 12*X
80
;  edi  pointer to path   /fd/1/......  - for all files in nested directories
81
;
82
;  ret ebx = size or 0xffffffff file not found
83
;      eax = 0 ok read or other = errormsg
84
;            10 = access denied
85
;--------------------------------------------------------------
86
 
87
    mov    [save_flag],0
88
    mov    [path_pointer_flp],edi
89
    cmp    esi,0           ; return ramdisk root
90
    jne    fr_noroot_1
91
    cmp    ebx,224/16
92
    jbe    fr_do_1
93
    mov    eax,5
94
    mov    ebx,0
95
    mov   [flp_status],0
96
    ret
97
 
98
fr_do_1:
99
    push ebx ecx edx
100
    call  read_flp_root
101
    pop edx ecx ebx
102
    cmp    [FDC_Status],0
103
    jne    fdc_status_error_1
104
    mov    edi,edx
105
    dec    ebx
106
    shl    ebx,9
107
    mov    esi,0x8000
108
    add    esi,ebx
109
    shl    ecx,9
110
    cld
111
    rep    movsb
112
    mov    eax,0 ; ok read
113
    mov    ebx,0
114
    mov   [flp_status],0
115
    ret
116
fdc_status_error_1:
117
    mov   [flp_status],0
131 diamond 118
    mov    eax,10
1 ha 119
    mov    ebx,-1
120
    ret
121
 
122
fr_noroot_1:
123
    sub    esp,32
124
    call   expand_filename
125
frfloppy_1:
126
    cmp    ebx,0
127
    jne    frfl5_1
128
    mov    ebx,1
129
frfl5_1:
130
    cmp    ecx,0
131
    jne    frfl6_1
132
    mov    ecx,1
133
frfl6_1:
134
    dec    ebx
135
    push   eax
136
    push   eax ebx ecx edx esi edi
137
    call   read_flp_fat
138
    cmp    [FDC_Status],0
139
    jne    fdc_status_error_3_1
140
    mov    [FDD_Track],0      ; Цилиндр
141
    mov    [FDD_Head],1      ; Сторона
142
    mov    [FDD_Sector],2      ; Сектор
143
    call    SeekTrack
144
    mov     dh,14
145
l.20_1:
146
    call    ReadSectWithRetr
147
    cmp    [FDC_Status],0
148
    jne    fdc_status_error_3_1
149
    mov     dl,16
150
    mov     edi,0xD000
151
    inc     [FDD_Sector]
152
l.21_1:
153
    mov    esi,eax            ;Name of file we want
154
    mov    ecx,11
155
    cld
156
    rep    cmpsb            ;Found the file?
157
    je     fifound_1          ;Yes
158
    add    ecx,21
159
    add    edi, ecx         ;Advance to next entry
160
    dec    dl
161
    cmp    dl,0
162
    jne    l.21_1
163
    dec    dh
164
    cmp    dh,0
165
    jne    l.20_1
166
fdc_status_error_3:
167
    mov    eax,5            ; file not found ?
168
    mov    ebx,-1
169
    add    esp,32+28
170
    mov   [flp_status],0
171
    ret
172
fdc_status_error_3_2:
173
    cmp    [FDC_Status],0
174
    je    fdc_status_error_3
175
fdc_status_error_3_1:
176
    add    esp,32+28
177
    jmp    fdc_status_error_1
178
 
179
fifound_1:
180
    mov    eax,[path_pointer_flp]
181
    cmp    [eax+36],byte 0
182
    je     fifound_2
183
    add    edi,0xf
184
    mov    eax,[edi]
185
    and    eax,65535
186
    mov    ebx,[path_pointer_flp]
187
    add    ebx,36
188
    call   get_cluster_of_a_path_flp
189
    jc     fdc_status_error_3_2
190
    mov    ebx,[ebx-11+28]        ;file size
191
    mov    [esp+20],ebx
192
    mov    [esp+24],ebx
193
    jmp     fifound_3
194
fifound_2:
195
    mov    ebx,[edi-11+28]        ;file size
196
    mov    [esp+20],ebx
197
    mov    [esp+24],ebx
198
    add    edi,0xf
199
    mov    eax,[edi]
200
fifound_3:
201
    and    eax,65535
202
    mov    [n_sector],eax            ;eax=cluster
203
frnew_1:
204
    add    eax,31            ;bootsector+2*fat+filenames
205
    cmp    [esp+16],dword 0     ; wanted cluster ?
206
    jne    frfl7_1
207
    call   read_chs_sector
208
    cmp    [FDC_Status],0
209
    jne    fdc_status_error_5
210
    mov    edi,[esp+8]
211
    call    give_back_application_data_1
212
    add    [esp+8],dword 512
213
    dec    dword [esp+12]        ; last wanted cluster ?
214
    cmp    [esp+12],dword 0
215
    je     frnoread_1
216
    jmp    frfl8_1
217
frfl7_1:
218
    dec    dword [esp+16]
219
frfl8_1:
220
    mov    edi,[n_sector]
221
    shl    edi,1            ;find next cluster from FAT
222
    add    edi,0x282000
223
    mov    eax,[edi]
224
    and    eax,4095
225
    mov    edi,eax
226
    mov    [n_sector],edi
227
    cmp    edi,4095         ;eof  - cluster
228
    jz     frnoread2_1
229
    cmp    [esp+24],dword 512    ;eof  - size
230
    jb     frnoread_1
231
    sub    [esp+24],dword 512
232
    jmp    frnew_1
233
 
131 diamond 234
read_chs_sector:
1 ha 235
    call    calculate_chs
236
    call    ReadSectWithRetr
237
    ret
238
 
239
frnoread2_1:
240
    cmp    [esp+16],dword 0     ; eof without read ?
241
    je     frnoread_1
242
    mov    [fdc_irq_func],fdc_null
243
    pop    edi esi edx ecx
244
    add    esp,4
245
    pop    ebx     ; ebx <- eax : size of file
246
    add    esp,36
247
    mov    eax,6   ; end of file
248
    mov    [flp_status],0
249
    ret
250
 
251
frnoread_1:
252
    pop    edi esi edx ecx
253
    add    esp,4
254
    pop    ebx     ; ebx <- eax : size of file
255
    add    esp,36
256
    mov    eax,0
257
    mov    [flp_status],0
258
    ret
259
 
260
fdc_status_error_5:
261
    pop    edi esi edx ecx
262
    add    esp,4
263
    pop    ebx     ; ebx <- eax : size of file
264
    add    esp,36
265
    jmp    fdc_status_error_1
266
 
267
read_flp_root:
268
    pusha
269
    call  check_label
270
    cmp    [FDC_Status],0
271
    jne    unnecessary_root_read
272
    cmp   [root_read],1
273
    je    unnecessary_root_read
274
    mov    [FDD_Track],0      ; Цилиндр
275
    mov    [FDD_Head],1      ; Сторона
131 diamond 276
    mov    [FDD_Sector],2      ; Сектор
1 ha 277
    mov    edi,0x8000
278
    call   SeekTrack
279
read_flp_root_1:
280
    call   ReadSectWithRetr
281
    cmp    [FDC_Status],0
282
    jne    unnecessary_root_read
283
    push   edi
284
    call   give_back_application_data_1
285
    pop    edi
286
    add    edi,512
287
    inc    [FDD_Sector]
288
    cmp    [FDD_Sector],16
289
    jne    read_flp_root_1
290
    mov    [root_read],1
291
unnecessary_root_read:
292
    popa
293
    ret
294
 
295
 
296
read_flp_fat:
297
    pusha
298
    call  check_label
299
    cmp    [FDC_Status],0
300
    jne    unnecessary_flp_fat
301
    cmp   [flp_fat],1
302
    je    unnecessary_flp_fat
303
    mov    [FDD_Track],0      ; Цилиндр
304
    mov    [FDD_Head],0      ; Сторона
131 diamond 305
    mov    [FDD_Sector],2      ; Сектор
1 ha 306
    mov    edi,0x8000
307
    call   SeekTrack
308
read_flp_fat_1:
309
    call   ReadSectWithRetr
310
    cmp    [FDC_Status],0
311
    jne    unnecessary_flp_fat
312
    push   edi
313
    call   give_back_application_data_1
314
    pop    edi
315
    add    edi,512
316
    inc    [FDD_Sector]
317
    cmp    [FDD_Sector],19
318
    jne    read_flp_fat_1
319
    mov    [FDD_Sector],1
320
    mov    [FDD_Head],1
321
    call   ReadSectWithRetr
322
    cmp    [FDC_Status],0
323
    jne    unnecessary_flp_fat
324
    call   give_back_application_data_1
325
    call   calculatefatchain_flp
326
    mov    [root_read],0
327
    mov    [flp_fat],1
328
unnecessary_flp_fat:
329
    popa
330
    ret
331
 
332
calculatefatchain_flp:
333
   pushad
334
 
335
   mov  esi,0x8000
336
   mov  edi,0x282000
337
 
338
 fcnew_1:
339
   mov  eax,dword [esi]
340
   mov  ebx,dword [esi+4]
341
   mov  ecx,dword [esi+8]
342
   mov  edx,ecx
343
   shr  edx,4   ;8 ok
344
   shr  dx,4    ;7 ok
345
   xor  ch,ch
346
   shld ecx,ebx,20 ;6 ok
347
   shr  cx,4     ;5 ok
348
   shld ebx,eax,12
349
   and  ebx,0x0fffffff  ;4 ok
350
   shr  bx,4    ;3 ok
351
   shl  eax,4
352
   and  eax,0x0fffffff  ;2 ok
353
   shr  ax,4  ;1 ok
354
   mov dword [edi],eax
355
   add  edi,4
356
   mov dword [edi],ebx
357
   add  edi,4
358
   mov dword [edi],ecx
359
   add  edi,4
360
   mov dword [edi],edx
131 diamond 361
   add  edi,4
1 ha 362
   add  esi,12
363
 
364
   cmp  edi,0x282000+2856*2   ;2849 clusters
365
   jnz  fcnew_1
366
 
367
   popad
368
   ret
131 diamond 369
 
1 ha 370
check_label:
371
    pushad
372
    mov    [FDD_Track],0      ; Цилиндр
373
    mov    [FDD_Head],0      ; Сторона
131 diamond 374
    mov    [FDD_Sector],1      ; Сектор
1 ha 375
    call   SetUserInterrupts
376
    call   FDDMotorON
377
    call   RecalibrateFDD
378
    cmp    [FDC_Status],0
379
    jne    fdc_status_error
380
    call   SeekTrack
381
    cmp    [FDC_Status],0
382
    jne    fdc_status_error
383
    call   ReadSectWithRetr
384
    cmp    [FDC_Status],0
131 diamond 385
    jne    fdc_status_error
1 ha 386
    mov    esi,flp_label
387
    mov    edi,0xD000+39
388
    mov    ecx,15
389
    cld
131 diamond 390
    rep    cmpsb
1 ha 391
    je     same_label
392
    mov    [root_read],0
393
    mov    [flp_fat],0
394
same_label:
395
    mov    esi,0xD000+39
396
    mov    edi,flp_label
397
    mov    ecx,15
398
    cld
399
    rep    movsb
400
    popad
401
    ret
402
fdc_status_error:
403
    popad
404
    ret
405
 
406
save_flp_root:
407
    pusha
408
    call  check_label
409
    cmp    [FDC_Status],0
410
    jne    unnecessary_root_save
411
    cmp   [root_read],0
412
    je    unnecessary_root_save
413
    mov    [FDD_Track],0      ; Цилиндр
414
    mov    [FDD_Head],1      ; Сторона
131 diamond 415
    mov    [FDD_Sector],2      ; Сектор
1 ha 416
    mov    esi,0x8000
417
    call   SeekTrack
418
save_flp_root_1:
419
    push   esi
420
    call   take_data_from_application_1
421
    pop    esi
422
    add    esi,512
423
    call   WriteSectWithRetr
424
    cmp    [FDC_Status],0
425
    jne    unnecessary_root_save
426
    inc    [FDD_Sector]
427
    cmp    [FDD_Sector],16
428
    jne    save_flp_root_1
429
unnecessary_root_save:
430
    mov    [fdc_irq_func],fdc_null
431
    popa
432
    ret
131 diamond 433
 
1 ha 434
save_flp_fat:
435
    pusha
436
    call  check_label
437
    cmp    [FDC_Status],0
438
    jne    unnecessary_flp_fat_save
439
    cmp   [flp_fat],0
440
    je    unnecessary_flp_fat_save
441
    call   restorefatchain_flp
442
    mov    [FDD_Track],0      ; Цилиндр
443
    mov    [FDD_Head],0      ; Сторона
131 diamond 444
    mov    [FDD_Sector],2      ; Сектор
1 ha 445
    mov    esi,0x8000
446
    call   SeekTrack
447
save_flp_fat_1:
448
    push   esi
449
    call   take_data_from_application_1
450
    pop    esi
451
    add    esi,512
452
    call   WriteSectWithRetr
453
    cmp    [FDC_Status],0
454
    jne    unnecessary_flp_fat_save
455
    inc    [FDD_Sector]
456
    cmp    [FDD_Sector],19
457
    jne    save_flp_fat_1
458
    mov    [FDD_Sector],1
459
    mov    [FDD_Head],1
460
    call   take_data_from_application_1
461
    call   WriteSectWithRetr
462
    cmp    [FDC_Status],0
463
    jne    unnecessary_flp_fat_save
464
    mov    [root_read],0
465
unnecessary_flp_fat_save:
466
    mov    [fdc_irq_func],fdc_null
467
    popa
468
    ret
469
 
131 diamond 470
 
1 ha 471
restorefatchain_flp:   ; restore fat chain
472
   pushad
473
 
474
   mov  esi,0x282000
475
   mov  edi,0x8000
476
 
477
  fcnew2_1:
478
   mov  eax,dword [esi]
479
   mov  ebx,dword [esi+4]
480
   shl  ax,4
481
   shl  eax,4
482
   shl  bx,4
483
   shr  ebx,4
484
   shrd eax,ebx,8
485
   shr  ebx,8
486
   mov dword [edi],eax
487
   add  edi,4
488
   mov word [edi],bx
489
   add  edi,2
490
   add  esi,8
491
 
492
   cmp  edi,0x8000+0x1200     ;4274 bytes - all used FAT
493
   jb   fcnew2_1
494
 
495
   mov  esi,0x8000           ; duplicate fat chain
496
   mov  edi,0x8000+0x1200
497
   mov  ecx,0x1200/4
498
   cld
499
   rep  movsd
500
 
501
   popad
502
   ret
503
 
504
 
505
floppy_filedelete:
506
;--------------------------------------------
507
;
508
; filedelete - sys floppy
509
; in:
510
; eax - filename 11 chars - for root directory
511
; edi  pointer to path   /fd/1/...... - for all files in nested directories
512
;
513
; out:
514
; eax - 0 = successful, 1 = file not found, 10 = access denied
515
;
516
;--------------------------------------------
517
    mov    [path_pointer_flp],edi
518
    mov    [save_flag],0
519
    mov    ebp,1  ; file not found as default
520
filedelete_newtry_1:
521
    sub    esp,32
522
    call   expand_filename
523
    push   eax ebx ecx edx esi edi
524
    call   read_flp_fat
525
    cmp    [FDC_Status],0
526
    jne    frnoreadd_1
527
    mov    [FDD_Track],0      ; Цилиндр
528
    mov    [FDD_Head],1      ; Сторона
529
    mov    [FDD_Sector],2      ; Сектор
530
    call    SeekTrack
531
    mov     dh,14
532
l.20_2:
533
    call    ReadSectWithRetr
534
    cmp    [FDC_Status],0
535
    jne    fdc_status_error_4
536
    mov     dl,16
537
    mov     edi,0xD000
538
    inc     [FDD_Sector]
539
l.21_2:
540
    mov    esi,eax            ;Name of file we want
541
    mov    ecx,11
542
    cld
543
    rep    cmpsb            ;Found the file?
544
    je     fifoundd_1          ;Yes
545
    add    ecx,21
546
    add    edi, ecx         ;Advance to next entry
547
    dec    dl
548
    jne    l.21_2
549
    dec    dh
550
    jne    l.20_2
551
    jmp    frnoreadd_1
552
 
553
fdc_status_error_4:
554
    pop    edi esi edx ecx ebx eax
555
    add    esp,32
556
    jmp    fdc_status_error_1
557
 
558
fifoundd_1:
559
    mov    eax,[path_pointer_flp]
560
    cmp    [eax+36],byte 0
561
    je    fifoundd_2
83 diamond 562
    movzx  eax, word [edi+0xf]
1 ha 563
    mov    ebx,[path_pointer_flp]
564
    add    ebx,36
565
    call   get_cluster_of_a_path_flp
131 diamond 566
    jc     frnoreadd_1_1
1 ha 567
    mov    edi,ebx
568
    add    edi,11
569
    jmp    fifoundd_2_1
570
fifoundd_2:
571
    dec    [FDD_Sector]
572
fifoundd_2_1:
573
    mov    [edi-11],byte 0xE5    ;mark filename deleted
83 diamond 574
        movzx   edi, word [edi+0xf] ;edi = cluster
1 ha 575
frnewd_1:
576
    shl    edi,1            ;find next cluster from FAT
577
    add    edi,0x282000
578
    mov    eax,[edi]
579
    mov    [edi],word 0x0        ;clear fat chain cluster
580
    and    eax,4095
581
    mov    edi,eax
582
    cmp    edi,dword 4095        ;last cluster ?
583
    jz     frnoreadd2_1
584
    jmp    frnewd_1
585
 
586
frnoreadd2_1:
587
    call   WriteSectWithRetr
588
    cmp    [FDC_Status],0
589
    jne    fdc_status_error_4
590
    call   save_flp_fat
591
    cmp    [FDC_Status],0
592
    jne    fdc_status_error_4
593
;    pop    edi esi edx ecx ebx eax
594
;    add    esp,32
595
    mov    ebp,0       ; file found
596
;    jmp    filedelete_newtry_1
597
    jmp    frnoreadd_1
598
 
599
frnoreadd_1_1:
600
    cmp    [FDC_Status],0
601
    jne    fdc_status_error_4
602
frnoreadd_1:
603
    pop    edi esi edx ecx ebx eax
604
    add    esp,32
605
    mov    eax,ebp
606
    ret
607
 
608
floppy_filesave:
609
;----------------------------------------------------------
610
;
611
; filesave - sys floppy
612
;
613
; eax      ; pointer to file name 11 chars - for root directory
614
; ebx      ; buffer
615
; ecx      ; count to write in bytes
616
; edx      ; 0 create new , 1 append
617
; edi  pointer to path   /fd/1/......  - for all files in nested directories
618
;
619
; output : eax = 0 - ok
131 diamond 620
;                5 - file not found / directory not found
1 ha 621
;                8 - disk full
622
;               10 - access denied
623
;-----------------------------------------------------------
624
    mov    [path_pointer_flp],edi
625
    sub  esp,32
626
    call expand_filename
627
    cmp  edx,0
628
    jnz  fsdel_1
629
    pusha
630
    call floppy_filedelete
631
    cmp    [FDC_Status],0
632
    jne    fdc_status_error_6
633
    popa
634
    mov    [save_flag],1
635
fsdel_1:
636
    call   floppy_free_space
637
    cmp    [FDC_Status],0
638
    jne    fdc_status_error_6
639
    cmp    ecx,edi
640
    jb     rd_do_save_1
641
    add    esp,32
642
    mov    eax,8    ; not enough free space
643
    mov   [flp_status],0
644
    ret
645
 
646
fdc_status_error_6:
131 diamond 647
    popa
1 ha 648
    add    esp,32
649
    jmp    fdc_status_error_1
131 diamond 650
 
1 ha 651
rd_do_save_1:
652
    push   eax ebx ecx edx esi edi
653
    call   read_flp_fat
654
    cmp    [FDC_Status],0
655
    jne    fdc_status_error_7
656
    push   eax
657
    mov    eax,[path_pointer_flp]
658
    cmp    [eax+36],byte 0
659
    jne    fifoundds_2
660
    pop    eax
661
    mov    [save_root_flag],1
662
    call   read_flp_root
663
    cmp    [FDC_Status],0
664
    jne    fdc_status_error_7
665
    mov    edi,0x8000   ;Point at directory
666
    mov    edx,224 +1
667
    ; find an empty spot for filename in the root dir
668
l20ds_1:
669
    sub    edx,1
83 diamond 670
    jz     frnoreadds_1
1 ha 671
l21ds_1:
672
    cmp    [edi],byte 0xE5
673
    jz     fifoundds_1
674
    cmp    [edi],byte 0x0
675
    jz     fifoundds_1
676
    add    edi,32            ; Advance to next entry
677
    jmp    l20ds_1
678
 
679
fifoundds_2:
680
    pop    eax
681
    mov    [save_root_flag],0
682
    mov    [FDD_Track],0      ; Цилиндр
683
    mov    [FDD_Head],1       ; Сторона
684
    mov    [FDD_Sector],2      ; Сектор
685
    call   SeekTrack
686
    mov    dh,14
687
l.20_3:
688
    call    ReadSectWithRetr
689
    cmp    [FDC_Status],0
690
    jne    fdc_status_error_7
691
    mov    dl,16
692
    mov    edi,0xD000
693
    inc     [FDD_Sector]
694
l.21_3:
695
    mov    esi,eax            ;Name of file we want
696
    mov    ecx,11
697
    cld
698
    rep    cmpsb            ;Found the file?
699
    je     fifoundds_3          ;Yes
700
    add    ecx,21
701
    add    edi, ecx         ;Advance to next entry
702
    dec    dl
703
    jne    l.21_3
704
    dec    dh
705
    jne    l.20_3
706
fdc_status_error_8:
707
    pop     edi esi edx ecx ebx eax
708
    mov    eax,5            ; file not found ?
709
    mov    ebx,-1
710
    add    esp,32
711
    mov    [flp_status],0
712
    ret
713
 
714
fifoundds_3:
715
    add    edi,0xf
716
    mov    eax,[edi]
717
    and    eax,65535
718
    mov    ebx,[path_pointer_flp]
719
    add    ebx,36
720
    call   get_cluster_of_a_path_flp
721
    jc     fdc_status_error_7_1
722
found_directory_for_writing_flp:
723
    call   analyze_directory_to_write_flp
724
    jc     fdc_status_error_7_1
725
    mov    edi,ebx
726
fifoundds_1:
727
    push   edi            ; move the filename to root dir
728
    mov    esi,[esp+4+20]
729
    cmp    [save_root_flag],0
730
    jne    fifoundds_4
731
    mov    esi,[pointer_file_name_flp]
732
fifoundds_4:
733
    mov    ecx,11
734
    cld
735
    rep    movsb
736
    pop    edi
737
    mov    edx,edi
738
    add    edx,11+0xf        ; edx <- cluster save position
739
    mov    ebx,[esp+12]        ; save file size
740
    mov    [edi+28],ebx
741
    mov    [edi+11],byte 0x20    ; attribute
742
    call   get_date_for_file     ; from FAT32.INC
743
    mov    [edi+24],ax      ; date
744
    mov    [edi+18],ax      ; date
745
    call   get_time_for_file     ; from FAT32.INC
746
    mov    [edi+22],ax      ; time
747
    xor    ax,ax
748
    mov    [edi+20],ax
749
    mov    ebx,1            ; first cluster
750
    cmp    [save_root_flag],0
751
    jne    frnewds_1
131 diamond 752
    call   frnewds_2
1 ha 753
    pusha
754
    call   WriteSectWithRetr
755
    popa
756
    cmp    [FDC_Status],0
757
    jne    fdc_status_error_7
758
    jmp    frnewds_3
759
 
760
frnewds_1:
761
    call   frnewds_2
762
frnewds_3:
763
    pusha                ; move save to floppy cluster
764
    add    ebx,31
765
    mov    eax,ebx
766
    mov    esi,[esp+32+16]
767
    call   take_data_from_application_1
768
    call   save_chs_sector
769
    cmp    [FDC_Status],0
770
    jne    fdc_status_error_7
771
    popa
772
    mov    eax,[esp+12]
773
    cmp    eax,512
774
    jb     flnsa_1
775
    sub    eax,512
776
    mov    [esp+12],eax
777
    mov    eax,[esp+16]
778
    add    eax,512
779
    mov    [esp+16],eax
780
    jmp    frnewds_1
781
 
782
frnewds_2:
783
    add    ebx,1
784
    mov    edi,ebx            ; find free cluster in FAT
785
    shl    edi,1
786
    add    edi,0x282000
787
    mov    eax,[edi]
788
    and    eax,4095
789
    jnz    frnewds_2
790
    mov    [edx],bx         ; save next cluster pos. to prev cl.
791
    mov    edx,edi            ; next save pos abs mem add
792
    ret
793
 
794
flnsa_1:
795
    mov    [edi],word 4095        ; mark end of file - last cluster
796
    cmp    [save_root_flag],1
797
    jne    flnsa_2
798
    call   save_flp_root
799
    cmp    [FDC_Status],0
800
    jne    fdc_status_error_7
801
flnsa_2:
802
    call   save_flp_fat
803
    cmp    [FDC_Status],0
804
    jne    fdc_status_error_7
805
frnoreadds_1:
806
    pop    edi esi edx ecx ebx eax
807
    add    esp,32
808
    mov    eax,0
809
    mov   [flp_status],0
810
    ret
811
 
812
fdc_status_error_7_1:
813
    cmp    [FDC_Status],0
131 diamond 814
    je    fdc_status_error_8
1 ha 815
fdc_status_error_7:
816
    pop    edi esi edx ecx ebx eax
817
    add    esp,32
818
    jmp    fdc_status_error_1
819
 
820
save_chs_sector:
821
    call    calculate_chs
822
    call    WriteSectWithRetr
823
    ret
131 diamond 824
 
1 ha 825
calculate_chs:
826
    mov    bl,[FDD_Track]
131 diamond 827
    mov    [old_track],bl
1 ha 828
    mov    ebx,18
829
    xor    edx,edx
830
    div    ebx
831
    inc    edx
832
    mov    [FDD_Sector],dl
833
    xor    edx,edx
834
    mov    ebx,2
835
    div    ebx
836
    mov    [FDD_Track],al
837
    mov    [FDD_Head],0
838
    cmp    edx,0
839
    je     no_head_2
840
    inc    [FDD_Head]
841
no_head_2:
842
    mov     dl,[old_track]
843
    cmp     dl,[FDD_Track]
844
    je      no_seek_track_1
845
    call    SeekTrack
846
no_seek_track_1:
847
    ret
848
 
131 diamond 849
 
1 ha 850
get_cluster_of_a_path_flp:
851
;---------------------------------------------------------
852
; input  : EBX = pointer to a path string
853
;          (example: the path "/files/data/document" become
854
;                             "files......data.......document...0"
855
;          '.' = space char
856
;          '0' = char(0) (ASCII=0) !!! )
857
; output : if (CARRY=1) -> ERROR in the PATH
858
;          if (CARRY=0) -> EAX=cluster
859
;---------------------------------------------------------
860
 
861
    push  edx
862
    mov   edx,ebx
863
 
864
search_end_of_path_flp:
865
    cmp   [save_flag],0
866
    jne   search_end_of_path_flp_1
867
    cmp   byte [edx],0
868
    je    found_end_of_path_flp
869
    jmp   search_end_of_path_flp_2
870
search_end_of_path_flp_1:
871
    cmp   byte [edx+12],0
872
    je    found_end_of_path_flp
873
search_end_of_path_flp_2:
874
    inc   edx ; '/'
875
    call  analyze_directory_flp
876
    jc    directory_not_found_flp
877
 
878
    mov   eax,[ebx+20-2]        ; read the HIGH 16bit cluster field
879
    mov   ax,[ebx+26]           ; read the LOW 16bit cluster field
880
    and   eax,0xfff           ;[fatMASK]
881
    add   edx,11                ; 8+3 (name+extension)
882
    jmp   search_end_of_path_flp
883
 
884
found_end_of_path_flp:
885
    inc   edx
886
    mov   [pointer_file_name_flp],edx
887
    pop   edx
888
    clc                         ; no errors
889
    ret
890
 
891
directory_not_found_flp:
892
    pop   edx
893
    stc                         ; errors occour
894
    ret
131 diamond 895
 
1 ha 896
analyze_directory_flp:
897
;--------------------------------
898
; input  : EAX = first cluster of the directory
899
;          EBX = pointer to filename
900
; output : IF CARRY=0 EAX = sector where th file is found
901
;                     EBX = pointer in buffer
902
;                     [buffer .. buffer+511]
903
;                     ECX,EDX,EDI,EDI not changed
904
;          IF CARRY=1
905
;--------------------------------
906
   push ebx ;[esp+16]
907
   push ecx
908
   push edx
909
   push esi
910
   push edi
131 diamond 911
 
912
 
1 ha 913
adr56_flp:
914
   mov [clust_tmp_flp],eax
915
   add    eax,31
916
   pusha
917
   call   read_chs_sector
918
   popa
919
   cmp    [FDC_Status],0
920
   jne    not_found_file_analyze_flp
921
 
922
   mov ecx,512/32
923
   mov ebx,0xD000
131 diamond 924
 
1 ha 925
adr1_analyze_flp:
926
   mov esi,edx   ;[esp+16]
927
   mov edi,ebx
928
   cld
929
   push ecx
930
   mov ecx,11
931
   rep cmpsb
932
   pop ecx
933
   je found_file_analyze_flp
131 diamond 934
 
1 ha 935
   add ebx,32
936
   loop adr1_analyze_flp
131 diamond 937
 
1 ha 938
    mov eax,[clust_tmp_flp]
939
    shl    eax,1            ;find next cluster from FAT
940
    add    eax,0x282000
941
    mov    eax,[eax]
942
    and    eax,4095
943
    cmp eax,0x0ff8
944
    jb  adr56_flp
131 diamond 945
not_found_file_analyze_flp:
1 ha 946
   pop edi
947
   pop esi
948
   pop edx
949
   pop ecx
950
   add esp,4
951
   stc        ;file not found
952
   ret
131 diamond 953
 
1 ha 954
found_file_analyze_flp:
955
   pop edi
956
   pop esi
957
   pop edx
958
   pop ecx
959
   add esp,4
960
   clc        ;file found
961
   ret
131 diamond 962
 
963
 
1 ha 964
analyze_directory_to_write_flp:
965
;--------------------------------
966
; input  : EAX = first cluster of the directory
967
; output : IF CARRY=0 EAX = sector where the file is found
968
;                     EBX = pointer in buffer
969
;                     [buffer .. buffer+511]
970
;                     ECX,EDX,EDI,EDI not changed
971
;          IF CARRY=1
972
;--------------------------------
131 diamond 973
 
1 ha 974
   push ecx
975
   push edx
976
   push esi
131 diamond 977
 
1 ha 978
adr561:
979
   mov [clust_tmp_flp],eax
980
   add    eax,31
981
   pusha
131 diamond 982
   call   read_chs_sector
1 ha 983
   popa
984
   cmp    [FDC_Status],0
985
   jne    error_found_file_analyze1
986
 
987
   mov ecx,512/32
988
   mov ebx,0xD000
131 diamond 989
 
1 ha 990
adr1_analyze1:
991
   cmp byte [ebx],0x00
992
   je  found_file_analyze1
993
   cmp byte [ebx],0xe5
994
   je  found_file_analyze1
131 diamond 995
 
1 ha 996
avanti:
997
   add ebx,32
998
   loop adr1_analyze1
131 diamond 999
 
1 ha 1000
   mov eax,[clust_tmp_flp]
1001
   shl    eax,1            ;find next cluster from FAT
1002
   add    eax,0x282000
1003
   mov    eax,[eax]
1004
   and    eax,4095
1005
   cmp eax,0x0ff8
1006
   jb  adr561
131 diamond 1007
 
1 ha 1008
   call get_free_FAT               ;this block of code add a new cluster
1009
                                   ;for the directory because the directory
1010
                                   ;is full
1011
 
1012
   mov [edi],word 0x0fff
131 diamond 1013
 
1 ha 1014
   mov eax,[clust_tmp_flp]
1015
   shl    eax,1            ;find next cluster from FAT
1016
   add    eax,0x282000
1017
   sub    edi,0x282000
1018
   mov    [eax],di
1019
 
1020
   pusha
1021
   mov ecx,512/4
1022
   xor eax,eax
1023
   mov edi,0xD000
1024
   cld
1025
   rep stosd
1026
   popa
1027
 
1028
   mov    eax,edi
1029
   add    eax,31
1030
   pusha
131 diamond 1031
   call   save_chs_sector
1 ha 1032
   popa
1033
   cmp    [FDC_Status],0
1034
   jne    error_found_file_analyze1
1035
   mov    ebx,0xD000
1036
 
1037
found_file_analyze1:
131 diamond 1038
 
1 ha 1039
   pop esi
1040
   pop edx
1041
   pop ecx
1042
   clc        ;file found
1043
   ret
1044
 
1045
error_found_file_analyze1:
1046
   pop esi
1047
   pop edx
1048
   pop ecx
1049
   stc
131 diamond 1050
   ret
1051
 
1 ha 1052
get_free_FAT_flp:
1053
;------------------------------------------
1054
; input  :  EAX = # cluster for start the searching
1055
; output :  EAX = # first cluster found free
1056
;-------------------------------------------
1057
   push ebx
131 diamond 1058
 
1 ha 1059
    mov    ebx,1
1060
check_new_flp:
1061
    add    ebx,1
1062
    mov    edi,ebx            ; find free cluster in FAT
1063
    shl    edi,1
1064
    add    edi,0x282000
1065
    mov    eax,[edi]
1066
    and    eax,4095
1067
    cmp    eax,0x0
1068
    jnz    check_new_flp
1069
 
1070
   pop ebx
1071
   ret
71 diamond 1072
 
1073
; \begin{diamond}
83 diamond 1074
fat_find_lfn:
71 diamond 1075
; in: esi->name
83 diamond 1076
;     [esp+4] = next
1077
;     [esp+8] = first
1078
;     [esp+C]... - possibly parameters for first and next
71 diamond 1079
; out: CF=1 - file not found
83 diamond 1080
;      else CF=0, esi->next name component, edi->direntry
75 diamond 1081
        pusha
83 diamond 1082
        lea     eax, [esp+0Ch+20h]
1083
        call    dword [eax-4]
1084
        jc      .reterr
75 diamond 1085
        sub     esp, 262*2      ; reserve place for LFN
1086
        mov     ebp, esp
1087
        push    0               ; for fat_get_name: read ASCII name
83 diamond 1088
.l1:
75 diamond 1089
        call    fat_get_name
83 diamond 1090
        jc      .l2
75 diamond 1091
        call    fat_compare_name
1092
        jz      .found
83 diamond 1093
.l2:
1094
        lea     eax, [esp+0Ch+20h+262*2+4]
1095
        call    dword [eax-8]
1096
        jnc     .l1
75 diamond 1097
        add     esp, 262*2+4
83 diamond 1098
.reterr:
1099
        stc
75 diamond 1100
        popa
1101
        ret
71 diamond 1102
.found:
83 diamond 1103
        add     esp, 262*2+4
1104
; if this is LFN entry, advance to true entry
75 diamond 1105
        cmp     byte [edi+11], 0xF
83 diamond 1106
        jnz     @f
1107
        lea     eax, [esp+0Ch+20h]
1108
        call    dword [eax-8]
1109
        jc      .reterr
1110
@@:
1111
        add     esp, 8          ; CF=0
1112
        push    esi
1113
        push    edi
1114
        popa
1115
        ret
1116
 
171 diamond 1117
uglobal
1118
; this is for delete support
1119
fd_prev_sector          dd      ?
1120
fd_prev_prev_sector     dd      ?
1121
endg
1122
 
83 diamond 1123
flp_root_next:
1124
        cmp     edi, 0xD200-0x20
1125
        jae     @f
75 diamond 1126
        add     edi, 0x20
83 diamond 1127
        ret     ; CF=0
1128
@@:
1129
; read next sector
1130
        inc     dword [eax]
1131
        cmp     dword [eax], 14
1132
        jae     flp_root_first.readerr
171 diamond 1133
        push    [fd_prev_sector]
1134
        pop     [fd_prev_prev_sector]
1135
        push    eax
1136
        mov     eax, [eax]
1137
        add     eax, 19-1
1138
        mov     [fd_prev_sector], eax
1139
        pop     eax
83 diamond 1140
flp_root_first:
1141
        mov     eax, [eax]
1142
        pusha
1143
        add     eax, 19
75 diamond 1144
        call    read_chs_sector
83 diamond 1145
        popa
75 diamond 1146
        cmp     [FDC_Status], 0
83 diamond 1147
        jnz     .readerr
75 diamond 1148
        mov     edi, 0xD000
83 diamond 1149
        ret     ; CF=0
1150
.readerr:
1151
        stc
1152
        ret
1153
 
1154
flp_rootmem_first:
1155
        mov     edi, 0x8000
1156
        clc
1157
        ret
1158
flp_rootmem_next:
1159
        add     edi, 0x20
1160
        cmp     edi, 0x8000+14*0x200
1161
        cmc
1162
flp_rootmem_next_write:
1163
flp_rootmem_begin_write:
1164
flp_rootmem_end_write:
1165
        ret
1166
flp_rootmem_extend_dir:
1167
        stc
1168
        ret
1169
 
1170
flp_notroot_next:
1171
        cmp     edi, 0xD200-0x20
1172
        jae     flp_notroot_next_sector
1173
        add     edi, 0x20
1174
        ret     ; CF=0
1175
flp_notroot_next_sector:
1176
        push    ecx
1177
        mov     ecx, [eax]
171 diamond 1178
        push    [fd_prev_sector]
1179
        pop     [fd_prev_prev_sector]
1180
        add     ecx, 31
1181
        mov     [fd_prev_sector], ecx
1182
        mov     ecx, [(ecx-31)*2+0x282000]
83 diamond 1183
        and     ecx, 0xFFF
1184
        cmp     ecx, 2849
1185
        jae     flp_notroot_first.err2
1186
        mov     [eax], ecx
1187
        pop     ecx
1188
flp_notroot_first:
1189
        mov     eax, [eax]
1190
        cmp     eax, 2
1191
        jb      .err
1192
        cmp     eax, 2849
1193
        jae     .err
1194
        pusha
75 diamond 1195
        add     eax, 31
83 diamond 1196
        call    read_chs_sector
1197
        popa
1198
        mov     edi, 0xD000
1199
        cmp     [FDC_Status], 0
1200
        jnz     .err
1201
        ret     ; CF=0
1202
.err2:
1203
        pop     ecx
1204
.err:
1205
        stc
75 diamond 1206
        ret
83 diamond 1207
flp_notroot_begin_write:
1208
        pusha
1209
        mov     eax, [eax]
1210
        add     eax, 31
1211
        call    read_chs_sector
1212
        popa
1213
        ret
1214
flp_notroot_end_write:
1215
        pusha
1216
        mov     eax, [eax]
1217
        add     eax, 31
1218
        call    save_chs_sector
1219
        popa
1220
        ret
1221
flp_notroot_next_write:
1222
        cmp     edi, 0xD200
1223
        jae     @f
1224
        ret
1225
@@:
1226
        call    flp_notroot_end_write
1227
        jmp     flp_notroot_next_sector
1228
flp_notroot_extend_dir:
1229
; find free cluster in FAT
1230
        pusha
1231
        xor     eax, eax
1232
        mov     edi, 0x282000
1233
        mov     ecx, 2849
1234
        repnz   scasw
1235
        jnz     .notfound
1236
        mov     word [edi-2], 0xFFF     ; mark as last cluster
1237
        sub     edi, 0x282000
1238
        shr     edi, 1
1239
        dec     edi
1240
        mov     eax, [esp+28]
1241
        mov     ecx, [eax]
1242
        mov     [0x282000+ecx*2], di
1243
        mov     [eax], edi
1244
        xor     eax, eax
1245
        mov     edi, 0xD000
1246
        mov     ecx, 128
1247
        rep     stosd
1248
        popa
1249
        call    flp_notroot_end_write
1250
        mov     edi, 0xD000
1251
        clc
1252
        ret
1253
.notfound:
1254
        popa
1255
        stc
1256
        ret
71 diamond 1257
 
83 diamond 1258
fd_find_lfn:
1259
; in: esi->name
1260
; out: CF=1 - file not found
86 diamond 1261
;      else CF=0 and edi->direntry, eax=directory cluster (0 for root)
83 diamond 1262
        push    esi edi
1263
        push    0
1264
        push    flp_root_first
1265
        push    flp_root_next
1266
.loop:
1267
        call    fat_find_lfn
1268
        jc      .notfound
1269
        cmp     byte [esi], 0
1270
        jz      .found
1271
        test    byte [edi+11], 10h
1272
        jz      .notfound
1273
        movzx   eax, word [edi+26]      ; cluster
1274
        mov     [esp+8], eax
1275
        mov     dword [esp+4], flp_notroot_first
1276
        mov     dword [esp], flp_notroot_next
1277
        jmp     .loop
1278
.notfound:
1279
        add     esp, 12
1280
        pop     edi esi
1281
        stc
1282
        ret
1283
.found:
86 diamond 1284
        mov     eax, [esp+8]
131 diamond 1285
        add     eax, 31
1286
        cmp     dword [esp], flp_root_next
1287
        jnz     @f
1288
        add     eax, -31+19
1289
@@:
83 diamond 1290
        add     esp, 16         ; CF=0
1291
        pop     esi
1292
        ret
1293
 
71 diamond 1294
;----------------------------------------------------------------
1295
;
1296
;  fs_FloppyRead - LFN variant for reading floppy
1297
;
1298
;  esi  points to filename
1299
;  ebx  pointer to 64-bit number = first wanted byte, 0+
1300
;       may be ebx=0 - start from first byte
1301
;  ecx  number of bytes to read, 0+
1302
;  edx  mem location to return data
1303
;
77 diamond 1304
;  ret ebx = bytes read or 0xffffffff file not found
71 diamond 1305
;      eax = 0 ok read or other = errormsg
1306
;
1307
;--------------------------------------------------------------
1308
fs_FloppyRead:
75 diamond 1309
        call    read_flp_fat
1310
        cmp     byte [esi], 0
1311
        jnz     @f
1312
        or      ebx, -1
1313
        mov     eax, 10         ; access denied
1314
        ret
71 diamond 1315
@@:
75 diamond 1316
        push    edi
1317
        call    fd_find_lfn
1318
        jnc     .found
1319
        pop     edi
1320
        or      ebx, -1
1321
        mov     eax, 5          ; file not found
1322
        ret
71 diamond 1323
.found:
75 diamond 1324
        test    ebx, ebx
1325
        jz      .l1
1326
        cmp     dword [ebx+4], 0
1327
        jz      @f
77 diamond 1328
        xor     ebx, ebx
71 diamond 1329
.reteof:
75 diamond 1330
        mov     eax, 6          ; EOF
1331
        pop     edi
1332
        ret
71 diamond 1333
@@:
75 diamond 1334
        mov     ebx, [ebx]
71 diamond 1335
.l1:
77 diamond 1336
        push    ecx edx
1337
        push    0
1338
        mov     eax, [edi+28]
1339
        sub     eax, ebx
1340
        jb      .eof
1341
        cmp     eax, ecx
1342
        jae     @f
1343
        mov     ecx, eax
1344
        mov     byte [esp], 6           ; EOF
1345
@@:
75 diamond 1346
        movzx   edi, word [edi+26]
71 diamond 1347
.new:
75 diamond 1348
        jecxz   .done
1349
        test    edi, edi
1350
        jz      .eof
1351
        cmp     edi, 0xFF8
1352
        jae     .eof
113 diamond 1353
        sub     ebx, 512
1354
        jae     .skip
77 diamond 1355
        lea     eax, [edi+31]
75 diamond 1356
        pusha
1357
        call    read_chs_sector
1358
        popa
1359
        cmp     [FDC_Status], 0
1360
        jnz     .err
77 diamond 1361
        lea     eax, [0xD000+ebx+512]
75 diamond 1362
        neg     ebx
1363
        push    ecx
1364
        cmp     ecx, ebx
1365
        jbe     @f
1366
        mov     ecx, ebx
71 diamond 1367
@@:
75 diamond 1368
        mov     ebx, edx
1369
        call    memmove
1370
        add     edx, ecx
1371
        sub     [esp], ecx
1372
        pop     ecx
1373
        xor     ebx, ebx
71 diamond 1374
.skip:
75 diamond 1375
        movzx   edi, word [edi*2+0x282000]
1376
        jmp     .new
71 diamond 1377
.done:
77 diamond 1378
        mov     ebx, edx
1379
        pop     eax edx ecx edi
1380
        sub     ebx, edx
75 diamond 1381
        ret
71 diamond 1382
.eof:
77 diamond 1383
        mov     ebx, edx
1384
        pop     eax edx ecx
75 diamond 1385
        jmp     .reteof
71 diamond 1386
.err:
77 diamond 1387
        mov     ebx, edx
1388
        pop     eax edx ecx edi
1389
        sub     ebx, edx
83 diamond 1390
        mov     al, 11
75 diamond 1391
        ret
1392
 
1393
;----------------------------------------------------------------
1394
;
1395
;  fs_FloppyReadFolder - LFN variant for reading floppy folders
1396
;
1397
;  esi  points to filename
78 diamond 1398
;  ebx  pointer to structure: 32-bit number = first wanted block, 0+
1399
;                           & flags (bitfields)
1400
; flags: bit 0: 0=ANSI names, 1=UNICODE names
75 diamond 1401
;  ecx  number of blocks to read, 0+
1402
;  edx  mem location to return data
1403
;
77 diamond 1404
;  ret ebx = blocks read or 0xffffffff folder not found
75 diamond 1405
;      eax = 0 ok read or other = errormsg
1406
;
1407
;--------------------------------------------------------------
1408
fs_FloppyReadFolder:
1409
        call    read_flp_fat
1410
        push    edi
1411
        cmp     byte [esi], 0
1412
        jz      .root
1413
        call    fd_find_lfn
1414
        jnc     .found
1415
        pop     edi
1416
        or      ebx, -1
1417
        mov     eax, ERROR_FILE_NOT_FOUND
1418
        ret
1419
.found:
1420
        test    byte [edi+11], 0x10     ; do not allow read files
1421
        jnz     .found_dir
1422
        pop     edi
1423
        or      ebx, -1
1424
        mov     eax, ERROR_ACCESS_DENIED
1425
        ret
1426
.found_dir:
1427
        movzx   eax, word [edi+26]
1428
        add     eax, 31
1429
        push    0
1430
        jmp     .doit
1431
.root:
1432
        mov     eax, 19
1433
        push    14
1434
.doit:
1435
        push    ecx ebp
1436
        sub     esp, 262*2      ; reserve space for LFN
1437
        mov     ebp, esp
78 diamond 1438
        push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE names
1439
        mov     ebx, [ebx]
75 diamond 1440
; init header
1441
        push    eax ecx
1442
        mov     edi, edx
1443
        mov     ecx, 32/4
1444
        xor     eax, eax
1445
        rep     stosd
1446
        pop     ecx eax
1447
        mov     byte [edx], 1   ; version
1448
        mov     esi, edi        ; esi points to BDFE
1449
.main_loop:
1450
        pusha
1451
        call    read_chs_sector
1452
        popa
1453
        cmp     [FDC_Status], 0
1454
        jnz     .error
1455
        mov     edi, 0xD000
1456
        push    eax
1457
.l1:
1458
        call    fat_get_name
1459
        jc      .l2
1460
        cmp     byte [edi+11], 0xF
1461
        jnz     .do_bdfe
1462
        add     edi, 0x20
1463
        cmp     edi, 0xD200
1464
        jb      .do_bdfe
1465
        pop     eax
1466
        inc     eax
1467
        dec     byte [esp+262*2+12]
1468
        jz      .done
1469
        jns     @f
1470
; read next sector from FAT
1471
        mov     eax, [(eax-31-1)*2+0x282000]
1472
        and     eax, 0xFFF
1473
        cmp     eax, 0xFF8
1474
        jae     .done
1475
        add     eax, 31
1476
        mov     byte [esp+262*2+12], 0
1477
@@:
1478
        pusha
1479
        call    read_chs_sector
1480
        popa
1481
        cmp     [FDC_Status], 0
1482
        jnz     .error
1483
        mov     edi, 0xD000
1484
        push    eax
1485
.do_bdfe:
1486
        inc     dword [edx+8]   ; new file found
1487
        dec     ebx
1488
        jns     .l2
1489
        dec     ecx
1490
        js      .l2
1491
        inc     dword [edx+4]   ; new file block copied
1492
        call    fat_entry_to_bdfe
1493
.l2:
1494
        add     edi, 0x20
1495
        cmp     edi, 0xD200
1496
        jb      .l1
1497
        pop     eax
1498
        inc     eax
1499
        dec     byte [esp+262*2+12]
1500
        jz      .done
1501
        jns     @f
1502
; read next sector from FAT
1503
        mov     eax, [(eax-31-1)*2+0x282000]
1504
        and     eax, 0xFFF
1505
        cmp     eax, 0xFF8
1506
        jae     .done
1507
        add     eax, 31
1508
        mov     byte [esp+262*2+12], 0
1509
@@:
1510
        jmp     .main_loop
1511
.error:
1512
        add     esp, 262*2+4
1513
        pop     ebp ecx edi edi
1514
        or      ebx, -1
1515
        mov     eax, ERROR_FILE_NOT_FOUND
1516
        ret
1517
.done:
1518
        add     esp, 262*2+4
1519
        pop     ebp
77 diamond 1520
        mov     ebx, [edx+4]
75 diamond 1521
        xor     eax, eax
1522
        dec     ecx
1523
        js      @f
1524
        mov     al, ERROR_END_OF_FILE
1525
@@:
1526
        pop     ecx edi edi
1527
        ret
1528
 
83 diamond 1529
;----------------------------------------------------------------
1530
;
1531
;  fs_FloppyRewrite - LFN variant for writing sys floppy
1532
;
1533
;  esi  points to filename
1534
;  ebx  ignored (reserved)
1535
;  ecx  number of bytes to write, 0+
1536
;  edx  mem location to data
1537
;
1538
;  ret ebx = number of written bytes
1539
;      eax = 0 ok read or other = errormsg
1540
;
1541
;--------------------------------------------------------------
1542
@@:
1543
        mov     eax, ERROR_ACCESS_DENIED
1544
        xor     ebx, ebx
1545
        ret
1546
fsfrfe2:
1547
        popad
1548
fsfrfe:
1549
        mov     eax, 11
1550
        xor     ebx, ebx
1551
        ret
1552
 
321 diamond 1553
fs_FloppyCreateFolder:
1554
        mov     al, 1
1555
        jmp     fs_FloppyRewrite.common
1556
 
83 diamond 1557
fs_FloppyRewrite:
321 diamond 1558
        xor     eax, eax
1559
.common:
83 diamond 1560
        cmp     byte [esi], 0
1561
        jz      @b
1562
        call    read_flp_fat
1563
        cmp     [FDC_Status], 0
1564
        jnz     fsfrfe
1565
        pushad
1566
        xor     ebp, ebp
1567
        push    esi
1568
@@:
1569
        lodsb
1570
        test    al, al
1571
        jz      @f
1572
        cmp     al, '/'
1573
        jnz     @b
1574
        lea     ebp, [esi-1]
1575
        jmp     @b
1576
@@:
1577
        pop     esi
1578
        test    ebp, ebp
1579
        jnz     .noroot
1580
        call    read_flp_root
1581
        cmp     [FDC_Status], 0
1582
        jnz     fsfrfe2
1583
        push    flp_rootmem_extend_dir
1584
        push    flp_rootmem_end_write
1585
        push    flp_rootmem_next_write
1586
        push    flp_rootmem_begin_write
1587
        xor     ebp, ebp
1588
        push    ebp
1589
        push    flp_rootmem_first
1590
        push    flp_rootmem_next
1591
        jmp     .common1
1592
.noroot:
1593
; check existence
1594
        mov     byte [ebp], 0
1595
        call    fd_find_lfn
1596
        mov     byte [ebp], '/'
1597
        lea     esi, [ebp+1]
1598
        jnc     @f
1599
        mov     eax, ERROR_FILE_NOT_FOUND
1600
.ret1:
1601
        mov     [esp+28], eax
1602
        popad
1603
        xor     ebx, ebx
1604
        ret
1605
@@:
1606
        test    byte [edi+11], 0x10     ; must be directory
1607
        mov     eax, ERROR_ACCESS_DENIED
1608
        jz      .ret1
1609
        movzx   ebp, word [edi+26]      ; ebp=cluster
1610
        mov     eax, ERROR_FAT_TABLE
1611
        cmp     ebp, 2
1612
        jb      .ret1
1613
        cmp     ebp, 2849
1614
        jae     .ret1
1615
        push    flp_notroot_extend_dir
1616
        push    flp_notroot_end_write
1617
        push    flp_notroot_next_write
1618
        push    flp_notroot_begin_write
1619
        push    ebp
1620
        push    flp_notroot_first
1621
        push    flp_notroot_next
1622
.common1:
1623
        call    fat_find_lfn
1624
        jc      .notfound
321 diamond 1625
; found
91 diamond 1626
        test    byte [edi+11], 10h
321 diamond 1627
        jz      .exists_file
1628
; found directory; if we are creating directory, return OK,
1629
;                  if we are creating file, say "access denied"
1630
        add     esp, 28
1631
        popad
1632
        test    al, al
1633
        mov     eax, ERROR_ACCESS_DENIED
91 diamond 1634
        jz      @f
321 diamond 1635
        mov     al, 0
1636
@@:
1637
        xor     ebx, ebx
1638
        ret
1639
.exists_file:
1640
; found file; if we are creating directory, return "access denied",
1641
;             if we are creating file, delete existing file and continue
1642
        cmp     byte [esp+28+28], 0
1643
        jz      @f
91 diamond 1644
        add     esp, 28
1645
        popad
1646
        mov     eax, ERROR_ACCESS_DENIED
1647
        xor     ebx, ebx
1648
        ret
1649
@@:
1650
; delete FAT chain
83 diamond 1651
        push    edi
1652
        xor     eax, eax
1653
        mov     dword [edi+28], eax     ; zero size
1654
        xchg    ax, word [edi+26]       ; start cluster
1655
        test    eax, eax
1656
        jz      .done1
1657
@@:
1658
        cmp     eax, 0xFF8
1659
        jae     .done1
321 diamond 1660
        lea     edi, [FLOPPY_FAT + eax*2] ; position in FAT
83 diamond 1661
        xor     eax, eax
1662
        xchg    ax, [edi]
1663
        jmp     @b
1664
.done1:
1665
        pop     edi
1666
        call    get_time_for_file
1667
        mov     [edi+22], ax
1668
        call    get_date_for_file
1669
        mov     [edi+24], ax
1670
        mov     [edi+18], ax
1671
        or      byte [edi+11], 20h      ; set 'archive' attribute
1672
        jmp     .doit
1673
.notfound:
1674
; file is not found; generate short name
1675
        call    fat_name_is_legal
1676
        jc      @f
1677
        add     esp, 28
1678
        popad
1679
        mov     eax, ERROR_FILE_NOT_FOUND
1680
        xor     ebx, ebx
1681
        ret
1682
@@:
1683
        sub     esp, 12
1684
        mov     edi, esp
1685
        call    fat_gen_short_name
1686
.test_short_name_loop:
1687
        push    esi edi ecx
1688
        mov     esi, edi
1689
        lea     eax, [esp+12+12+8]
1690
        mov     [eax], ebp
1691
        call    dword [eax-4]
1692
        jc      .found
1693
.test_short_name_entry:
1694
        cmp     byte [edi+11], 0xF
1695
        jz      .test_short_name_cont
1696
        mov     ecx, 11
1697
        push    esi edi
1698
        repz    cmpsb
1699
        pop     edi esi
1700
        jz      .short_name_found
1701
.test_short_name_cont:
1702
        lea     eax, [esp+12+12+8]
1703
        call    dword [eax-8]
1704
        jnc     .test_short_name_entry
1705
        jmp     .found
1706
.short_name_found:
1707
        pop     ecx edi esi
1708
        call    fat_next_short_name
1709
        jnc     .test_short_name_loop
1710
.disk_full:
1711
        add     esp, 12+28
1712
        popa
1713
        mov     eax, ERROR_DISK_FULL
1714
        xor     ebx, ebx
1715
        ret
1716
.found:
1717
        pop     ecx edi esi
1718
; now find space in directory
1719
; we need to save LFN <=> LFN is not equal to short name <=> generated name contains '~'
1720
        mov     al, '~'
1721
        push    ecx edi
1722
        mov     ecx, 8
1723
        repnz   scasb
1724
        push    1
1725
        pop     eax     ; 1 entry
1726
        jnz     .notilde
1727
; we need ceil(strlen(esi)/13) additional entries = floor((strlen(esi)+12+13)/13) total
1728
        xor     eax, eax
1729
@@:
1730
        cmp     byte [esi], 0
1731
        jz      @f
1732
        inc     esi
1733
        inc     eax
1734
        jmp     @b
1735
@@:
1736
        sub     esi, eax
1737
        add     eax, 12+13
1738
        mov     ecx, 13
1739
        push    edx
1740
        cdq
1741
        div     ecx
1742
        pop     edx
1743
.notilde:
1744
        push    -1
1745
        push    -1
1746
; find  successive entries in directory
1747
        xor     ecx, ecx
1748
        push    eax
1749
        lea     eax, [esp+12+8+12+8]
1750
        mov     [eax], ebp
1751
        call    dword [eax-4]
1752
        pop     eax
1753
        jnc     .scan_dir
1754
.fsfrfe3:
1755
        add     esp, 8+8+12+28
1756
        popad
1757
        mov     eax, 11
1758
        xor     ebx, ebx
1759
        ret
1760
.scan_dir:
1761
        cmp     byte [edi], 0
1762
        jz      .free
1763
        cmp     byte [edi], 0xE5
1764
        jz      .free
1765
        xor     ecx, ecx
1766
.scan_cont:
1767
        push    eax
1768
        lea     eax, [esp+12+8+12+8]
1769
        call    dword [eax-8]
1770
        pop     eax
1771
        jnc     .scan_dir
1772
        cmp     [FDC_Status], 0
1773
        jnz     .fsfrfe3
1774
        push    eax
1775
        lea     eax, [esp+12+8+12+8]
1776
        call    dword [eax+16]          ; extend directory
1777
        pop     eax
1778
        jnc     .scan_dir
1779
        add     esp, 8+8+12+28
1780
        popad
1781
        mov     eax, ERROR_DISK_FULL
1782
        xor     ebx, ebx
1783
        ret
1784
.free:
1785
        test    ecx, ecx
1786
        jnz     @f
1787
        mov     [esp], edi
1788
        mov     ecx, [esp+8+8+12+8]
1789
        mov     [esp+4], ecx
1790
        xor     ecx, ecx
1791
@@:
1792
        inc     ecx
1793
        cmp     ecx, eax
1794
        jb      .scan_cont
1795
; found!
1796
; calculate name checksum
1797
        push    esi ecx
1798
        mov     esi, [esp+8+8]
1799
        mov     ecx, 11
1800
        xor     eax, eax
1801
@@:
1802
        ror     al, 1
1803
        add     al, [esi]
1804
        inc     esi
1805
        loop    @b
1806
        pop     ecx esi
1807
        pop     edi
1808
        pop     dword [esp+8+12+8]
1809
; edi points to first entry in free chunk
1810
        dec     ecx
1811
        jz      .nolfn
1812
        push    esi
1813
        push    eax
1814
        lea     eax, [esp+8+8+12+8]
1815
        call    dword [eax+4]         ; begin write
1816
        mov     al, 40h
1817
.writelfn:
1818
        or      al, cl
1819
        mov     esi, [esp+4]
1820
        push    ecx
1821
        dec     ecx
1822
        imul    ecx, 13
1823
        add     esi, ecx
1824
        stosb
1825
        mov     cl, 5
1826
        call    fs_RamdiskRewrite.read_symbols
1827
        mov     ax, 0xF
1828
        stosw
1829
        mov     al, [esp+4]
1830
        stosb
1831
        mov     cl, 6
1832
        call    fs_RamdiskRewrite.read_symbols
1833
        xor     eax, eax
1834
        stosw
1835
        mov     cl, 2
1836
        call    fs_RamdiskRewrite.read_symbols
1837
        pop     ecx
1838
        lea     eax, [esp+8+8+12+8]
1839
        call    dword [eax+8]         ; next write
1840
        xor     eax, eax
1841
        loop    .writelfn
1842
        pop     eax
1843
        pop     esi
1844
;        lea     eax, [esp+8+12+8]
1845
;        call    dword [eax+12]          ; end write
1846
.nolfn:
1847
        xchg    esi, [esp]
1848
        mov     ecx, 11
1849
        rep     movsb
1850
        mov     word [edi], 20h         ; attributes
1851
        sub     edi, 11
1852
        pop     esi ecx
1853
        add     esp, 12
1854
        mov     byte [edi+13], 0        ; tenths of a second at file creation time
1855
        call    get_time_for_file
1856
        mov     [edi+14], ax            ; creation time
1857
        mov     [edi+22], ax            ; last write time
1858
        call    get_date_for_file
1859
        mov     [edi+16], ax            ; creation date
1860
        mov     [edi+24], ax            ; last write date
1861
        mov     [edi+18], ax            ; last access date
1862
        and     word [edi+20], 0        ; high word of cluster
1863
        and     word [edi+26], 0        ; low word of cluster - to be filled
1864
        and     dword [edi+28], 0       ; file size - to be filled
321 diamond 1865
        cmp     byte [esp+28+28], 0
1866
        jz      .doit
1867
; create directory
1868
        mov     byte [edi+11], 10h      ; attributes: folder
1869
        mov     ecx, 32*2
1870
        mov     edx, edi
83 diamond 1871
.doit:
1872
        lea     eax, [esp+8]
1873
        call    dword [eax+12]  ; flush directory
1874
        push    ecx
1875
        push    edi
133 diamond 1876
        push    0
83 diamond 1877
        mov     esi, edx
321 diamond 1878
        test    ecx, ecx
1879
        jz      .done
83 diamond 1880
        mov     ecx, 2849
321 diamond 1881
        mov     edi, FLOPPY_FAT
83 diamond 1882
        push    0       ; first cluster
1883
.write_loop:
1884
; allocate new cluster
1885
        xor     eax, eax
1886
        repnz   scasw
1887
        mov     al, ERROR_DISK_FULL
1888
        jnz     .ret
1889
        dec     edi
1890
        dec     edi
321 diamond 1891
        lea     eax, [edi-(FLOPPY_FAT)]
83 diamond 1892
        shr     eax, 1                  ; eax = cluster
1893
        mov     word [edi], 0xFFF       ; mark as last cluster
133 diamond 1894
        xchg    edi, [esp+4]
83 diamond 1895
        cmp     dword [esp], 0
1896
        jz      .first
1897
        stosw
1898
        jmp     @f
1899
.first:
1900
        mov     [esp], eax
1901
@@:
133 diamond 1902
        mov     edi, [esp+4]
83 diamond 1903
        inc     ecx
1904
; write data
1905
        push    ecx edi
1906
        mov     ecx, 512
1907
        cmp     dword [esp+20], ecx
1908
        jae     @f
1909
        mov     ecx, [esp+20]
1910
@@:
321 diamond 1911
        mov     edi, 0xD000
1912
        cmp     byte [esp+24+28+28], 0
1913
        jnz     .writedir
83 diamond 1914
        push    ecx
1915
        rep     movsb
1916
        pop     ecx
321 diamond 1917
.writedircont:
83 diamond 1918
        push    ecx
1919
        sub     ecx, 512
1920
        neg     ecx
1921
        push    eax
1922
        xor     eax, eax
1923
        rep     stosb
1924
        pop     eax
1925
        add     eax, 31
1926
        pusha
1927
        call    save_chs_sector
1928
        popa
1929
        pop     ecx
1930
        cmp     [FDC_Status], 0
1931
        jnz     .diskerr
1932
        sub     [esp+20], ecx
1933
        pop     edi ecx
1934
        jnz     .write_loop
1935
.done:
1936
        xor     eax, eax
1937
.ret:
1938
        pop     ebx edi edi ecx
1939
        mov     [esp+28+28], eax
1940
        lea     eax, [esp+8]
1941
        call    dword [eax+4]
1942
        mov     [edi+26], bx
1943
        mov     ebx, esi
1944
        sub     ebx, edx
1945
        mov     [edi+28], ebx
1946
        call    dword [eax+12]
1947
        mov     [esp+28+16], ebx
1948
        test    ebp, ebp
1949
        jnz     @f
1950
        call    save_flp_root
1951
@@:
1952
        add     esp, 28
1953
        cmp     [FDC_Status], 0
1954
        jnz     .err3
1955
        call    save_flp_fat
1956
        cmp     [FDC_Status], 0
1957
        jnz     .err3
1958
        popa
1959
        ret
1960
.err3:
1961
        popa
1962
        mov     al, 11
1963
        xor     ebx, ebx
1964
        ret
1965
.diskerr:
1966
        sub     esi, ecx
1967
        mov     eax, 11
1968
        pop     edi ecx
1969
        jmp     .ret
321 diamond 1970
.writedir:
1971
        push    ecx
1972
        mov     ecx, 32/4
1973
        push    ecx esi
1974
        rep     movsd
1975
        pop     esi ecx
1976
        mov     dword [edi-32], '.   '
1977
        mov     dword [edi-32+4], '    '
1978
        mov     dword [edi-32+8], '    '
1979
        mov     byte [edi-32+11], 10h
1980
        mov     word [edi-32+26], ax
1981
        push    esi
1982
        rep     movsd
1983
        pop     esi
1984
        mov     dword [edi-32], '..  '
1985
        mov     dword [edi-32+4], '    '
1986
        mov     dword [edi-32+8], '    '
1987
        mov     byte [edi-32+11], 10h
1988
        mov     ecx, [esp+28+8]
1989
        mov     word [edi-32+26], cx
1990
        pop     ecx
1991
        jmp     .writedircont
83 diamond 1992
 
131 diamond 1993
;----------------------------------------------------------------
1994
;
1995
;  fs_FloppyWrite - LFN variant for writing to floppy
1996
;
1997
;  esi  points to filename
1998
;  ebx  pointer to 64-bit number = first wanted byte, 0+
1999
;       may be ebx=0 - start from first byte
2000
;  ecx  number of bytes to write, 0+
2001
;  edx  mem location to data
2002
;
2003
;  ret ebx = bytes written (maybe 0)
2004
;      eax = 0 ok write or other = errormsg
2005
;
2006
;--------------------------------------------------------------
2007
 
2008
@@:
2009
        push    ERROR_ACCESS_DENIED
2010
fs_FloppyWrite.ret0:
2011
        pop     eax
2012
        xor     ebx, ebx
2013
        ret
2014
 
2015
fs_FloppyWrite.ret11:
2016
        push    11
2017
        jmp     fs_FloppyWrite.ret0
2018
 
2019
fs_FloppyWrite:
2020
        cmp     byte [esi], 0
2021
        jz      @b
2022
        call    read_flp_fat
2023
        cmp     [FDC_Status], 0
2024
        jnz     .ret11
2025
        pushad
2026
        call    fd_find_lfn
2027
        jnc     .found
2028
        popad
2029
        push    ERROR_FILE_NOT_FOUND
2030
        jmp     .ret0
2031
.found:
2032
; FAT does not support files larger than 4GB
2033
        test    ebx, ebx
2034
        jz      .l1
2035
        cmp     dword [ebx+4], 0
2036
        jz      @f
2037
.eof:
2038
        popad
2039
        push    ERROR_END_OF_FILE
2040
        jmp     .ret0
2041
@@:
2042
        mov     ebx, [ebx]
2043
.l1:
2044
; now edi points to direntry, ebx=start byte to write,
2045
; ecx=number of bytes to write, edx=data pointer
2046
 
2047
; extend file if needed
2048
        add     ecx, ebx
2049
        jc      .eof    ; FAT does not support files larger than 4GB
2050
        push    eax     ; save directory cluster
2051
        push    0       ; return value=0
2052
 
2053
        call    get_time_for_file
2054
        mov     [edi+22], ax            ; last write time
2055
        call    get_date_for_file
2056
        mov     [edi+24], ax            ; last write date
2057
        mov     [edi+18], ax            ; last access date
2058
 
2059
        push    dword [edi+28]          ; save current file size
2060
        cmp     ecx, [edi+28]
2061
        jbe     .length_ok
2062
        cmp     ecx, ebx
2063
        jz      .length_ok
2064
        call    floppy_extend_file
2065
        jnc     .length_ok
2066
        mov     [esp+4], eax
2067
; floppy_extend_file can return two error codes: FAT table error or disk full.
2068
; First case is fatal error, in second case we may write some data
2069
        cmp     al, ERROR_DISK_FULL
2070
        jz      .disk_full
2071
        pop     eax
2072
        pop     eax
2073
        mov     [esp+4+28], eax
2074
        pop     eax
2075
        popad
2076
        xor     ebx, ebx
2077
        ret
2078
.disk_full:
2079
; correct number of bytes to write
2080
        mov     ecx, [edi+28]
2081
        cmp     ecx, ebx
2082
        ja      .length_ok
2083
.ret:
2084
        pop     eax
2085
        pop     eax
2086
        mov     [esp+4+28], eax ; eax=return value
2087
        pop     eax
2088
        sub     edx, [esp+20]
2089
        mov     [esp+16], edx   ; ebx=number of written bytes
2090
        popad
2091
        ret
2092
.length_ok:
2093
; save FAT & directory
2094
; note that directory must be saved first because save_flp_fat uses buffer at 0xD000
2095
        mov     esi, [edi+28]
2096
        movzx   edi, word [edi+26]      ; starting cluster
2097
        mov     eax, [esp+8]
2098
        pusha
2099
        call    save_chs_sector
2100
        popa
2101
        cmp     [FDC_Status], 0
2102
        jnz     .device_err
2103
        call    save_flp_fat
2104
        cmp     [FDC_Status], 0
2105
        jz      @f
2106
.device_err:
2107
        mov     byte [esp+4], 11
2108
        jmp     .ret
2109
@@:
2110
 
2111
; now ebx=start pos, ecx=end pos, both lie inside file
2112
        sub     ecx, ebx
2113
        jz      .ret
2114
        call    SetUserInterrupts
2115
.write_loop:
321 diamond 2116
; skip unmodified sectors
2117
        cmp     dword [esp], 0x200
2118
        jb      .modify
2119
        sub     ebx, 0x200
2120
        jae     .skip
2121
        add     ebx, 0x200
2122
.modify:
131 diamond 2123
        lea     eax, [edi+31]   ; current sector
2124
; get length of data in current sector
2125
        push    ecx
2126
        sub     ebx, 0x200
2127
        jb      .hasdata
2128
        neg     ebx
2129
        xor     ecx, ecx
2130
        jmp     @f
2131
.hasdata:
2132
        neg     ebx
2133
        cmp     ecx, ebx
2134
        jbe     @f
2135
        mov     ecx, ebx
2136
@@:
2137
; load sector if needed
2138
        cmp     dword [esp+4], 0        ; we don't need to read uninitialized data
2139
        jz      .noread
2140
        cmp     ecx, 0x200      ; we don't need to read sector if it is fully rewritten
2141
        jz      .noread
2142
        cmp     ecx, esi        ; (same for the last sector)
2143
        jz      .noread
2144
        pusha
2145
        call    read_chs_sector
2146
        popa
2147
        cmp     [FDC_Status], 0
2148
        jz      @f
2149
.device_err2:
2150
        pop     ecx
2151
        jmp     .device_err
2152
@@:
2153
.noread:
2154
; zero uninitialized data if file was extended (because floppy_extend_file does not this)
2155
        push    eax ecx edi
2156
        xor     eax, eax
2157
        mov     ecx, 0x200
2158
        sub     ecx, [esp+4+12]
2159
        jbe     @f
2160
        mov     edi, 0xD000
2161
        add     edi, [esp+4+12]
2162
        rep     stosb
2163
@@:
2164
; zero uninitialized data in the last sector
2165
        mov     ecx, 0x200
2166
        sub     ecx, esi
2167
        jbe     @f
2168
        mov     edi, 0xD000
2169
        add     edi, esi
2170
        rep     stosb
2171
@@:
2172
        pop     edi ecx eax
2173
; copy new data
2174
        push    eax
2175
        mov     eax, edx
2176
        neg     ebx
2177
        jecxz   @f
2178
        add     ebx, 0xD000+0x200
2179
        call    memmove
2180
        xor     ebx, ebx
2181
@@:
2182
        pop     eax
2183
; save sector
2184
        pusha
2185
        call    save_chs_sector
2186
        popa
2187
        cmp     [FDC_Status], 0
2188
        jnz     .device_err2
2189
        add     edx, ecx
2190
        sub     [esp], ecx
2191
        pop     ecx
2192
        jz      .done
321 diamond 2193
.skip:
131 diamond 2194
.next_cluster:
2195
        movzx   edi, word [edi*2+0x282000]
2196
        sub     esi, 0x200
2197
        jae     @f
2198
        xor     esi, esi
2199
@@:
2200
        sub     dword [esp], 0x200
2201
        jae     .write_loop
2202
        and     dword [esp], 0
2203
        jmp     .write_loop
2204
.done:
2205
        mov     [fdc_irq_func], fdc_null
2206
        jmp     .ret
2207
 
2208
floppy_extend_file.zero_size:
2209
        xor     eax, eax
2210
        jmp     floppy_extend_file.start_extend
2211
 
2212
; extends file on floppy to given size (new data area is undefined)
2213
; in: edi->direntry, ecx=new size
133 diamond 2214
; out: CF=0 => OK, eax=0
131 diamond 2215
;      CF=1 => error, eax=code (ERROR_FAT_TABLE or ERROR_DISK_FULL)
2216
floppy_extend_file:
2217
        push    ecx
2218
; find the last cluster of file
2219
        movzx   eax, word [edi+26]      ; first cluster
2220
        mov     ecx, [edi+28]
2221
        jecxz   .zero_size
2222
@@:
2223
        sub     ecx, 0x200
2224
        jbe     @f
2225
        mov     eax, [eax*2+0x282000]
2226
        and     eax, 0xFFF
2227
        jz      .fat_err
2228
        cmp     eax, 0xFF8
2229
        jb      @b
2230
.fat_err:
2231
        pop     ecx
2232
        push    ERROR_FAT_TABLE
2233
        pop     eax
2234
        stc
2235
        ret
2236
@@:
2237
        push    eax
2238
        mov     eax, [eax*2+0x282000]
2239
        and     eax, 0xFFF
2240
        cmp     eax, 0xFF8
2241
        pop     eax
2242
        jb      .fat_err
2243
; set length to full number of sectors
2244
        sub     [edi+28], ecx
2245
.start_extend:
2246
        pop     ecx
2247
; now do extend
2248
        push    edx esi
2249
        mov     esi, 0x282000+2*2       ; start scan from cluster 2
2250
        mov     edx, 2847               ; number of clusters to scan
2251
.extend_loop:
2252
        cmp     [edi+28], ecx
2253
        jae     .extend_done
2254
; add new sector
2255
        push    ecx
2256
        push    edi
2257
.scan:
2258
        mov     ecx, edx
2259
        mov     edi, esi
2260
        jecxz   .disk_full
2261
        push    eax
2262
        xor     eax, eax
2263
        repnz   scasw
2264
        pop     eax
2265
        jnz     .disk_full
2266
        mov     word [edi-2], 0xFFF
2267
        mov     esi, edi
2268
        mov     edx, ecx
2269
        sub     edi, 0x282000
2270
        shr     edi, 1
2271
        dec     edi     ; now edi=new cluster
2272
        test    eax, eax
2273
        jz      .first_cluster
2274
        mov     [0x282000+eax*2], di
2275
        jmp     @f
2276
.first_cluster:
2277
        pop     eax             ; eax->direntry
2278
        push    eax
2279
        mov     [eax+26], di
2280
@@:
2281
        mov     eax, edi        ; eax=new cluster
2282
        pop     edi             ; edi->direntry
2283
        pop     ecx             ; ecx=required size
2284
        add     dword [edi+28], 0x200
2285
        jmp     .extend_loop
2286
.extend_done:
2287
        mov     [edi+28], ecx
2288
        pop     esi edx
133 diamond 2289
        xor     eax, eax        ; CF=0
131 diamond 2290
        ret
2291
.disk_full:
2292
        pop     edi ecx
2293
        pop     esi edx
2294
        stc
2295
        push    ERROR_DISK_FULL
2296
        pop     eax
2297
        ret
2298
 
133 diamond 2299
;----------------------------------------------------------------
2300
;
2301
;  fs_FloppySetFileEnd - set end of file on floppy
2302
;
2303
;  esi  points to filename
2304
;  ebx  points to 64-bit number = new file size
2305
;  ecx  ignored (reserved)
2306
;  edx  ignored (reserved)
2307
;
2308
;  ret eax = 0 ok or other = errormsg
2309
;
2310
;--------------------------------------------------------------
2311
fs_FloppySetFileEnd:
2312
        call    read_flp_fat
2313
        cmp     [FDC_Status], 0
2314
        jnz     ret11
2315
        cmp     byte [esi], 0
2316
        jnz     @f
2317
.access_denied:
2318
        push    ERROR_ACCESS_DENIED
2319
        jmp     .ret
2320
@@:
2321
        push    edi
2322
        call    fd_find_lfn
2323
        jnc     @f
2324
        pop     edi
2325
        push    ERROR_FILE_NOT_FOUND
2326
.ret:
2327
        pop     eax
2328
        jmp     .doret
2329
@@:
2330
; must not be directory
2331
        test    byte [edi+11], 10h
2332
        jz      @f
2333
        pop     edi
2334
        jmp     .access_denied
2335
@@:
2336
; file size must not exceed 4 Gb
2337
        cmp     dword [ebx+4], 0
2338
        jz      @f
2339
        pop     edi
2340
        push    ERROR_END_OF_FILE
2341
        jmp     .ret
2342
@@:
2343
        push    eax
2344
; set file modification date/time to current
2345
        call    fat_update_datetime
2346
        mov     eax, [ebx]
2347
        cmp     eax, [edi+28]
2348
        jb      .truncate
2349
        ja      .expand
2350
        pop     eax
2351
        pushad
2352
        call    save_chs_sector
2353
        popad
2354
        pop     edi
2355
        xor     eax, eax
2356
        cmp     [FDC_Status], 0
2357
        jz      @f
2358
        mov     al, 11
2359
@@:
2360
.doret:
2361
        mov     [fdc_irq_func], fdc_null
2362
        ret
2363
.expand:
2364
        push    ecx
2365
        push    dword [edi+28]  ; save old size
2366
        mov     ecx, eax
2367
        call    floppy_extend_file
2368
        push    eax     ; return code
2369
        jnc     .expand_ok
2370
        cmp     al, ERROR_DISK_FULL
2371
        jz      .disk_full
2372
        pop     eax ecx ecx edi edi
2373
        jmp     .doret
2374
.device_err:
2375
        pop     eax
2376
.device_err2:
2377
        pop     ecx ecx eax edi
2378
        push    11
2379
        jmp     .ret
2380
.disk_full:
2381
.expand_ok:
2382
; save directory & FAT
2383
        mov     eax, [edi+28]
2384
        xchg    eax, [esp+12]
2385
        movzx   edi, word [edi+26]
2386
        pusha
2387
        call    save_chs_sector
2388
        popa
2389
        cmp     [FDC_Status], 0
2390
        jnz     .device_err
2391
        call    save_flp_fat
2392
        cmp     [FDC_Status], 0
2393
        jnz     .device_err
2394
        call    SetUserInterrupts
2395
; now zero new data
2396
; edi = current cluster, [esp+12]=new size, [esp+4]=old size, [esp]=return code
2397
.zero_loop:
2398
        sub     dword [esp+4], 0x200
2399
        jae     .next_cluster
2400
        cmp     dword [esp+4], -0x200
2401
        jz      .noread
2402
        lea     eax, [edi+31]
2403
        pusha
2404
        call    read_chs_sector
2405
        popa
2406
        cmp     [FDC_Status], 0
2407
        jnz     .err_next
2408
.noread:
2409
        mov     ecx, [esp+4]
2410
        neg     ecx
2411
        push    edi
2412
        mov     edi, 0xD000+0x200
2413
        add     edi, [esp+8]
2414
        xor     eax, eax
2415
        mov     [esp+8], eax
2416
        rep     stosb
2417
        pop     edi
2418
        lea     eax, [edi+31]
2419
        pusha
2420
        call    save_chs_sector
2421
        popa
2422
        cmp     [FDC_Status], 0
2423
        jz      .next_cluster
2424
.err_next:
2425
        mov     byte [esp], 11
2426
.next_cluster:
2427
        sub     dword [esp+12], 0x200
2428
        jbe     .expand_done
2429
        movzx   edi, word [0x282000+edi*2]
2430
        jmp     .zero_loop
2431
.expand_done:
2432
        pop     eax ecx ecx edi edi
2433
        jmp     .doret
2434
.truncate:
2435
        mov     [edi+28], eax
2436
        push    ecx
2437
        movzx   ecx, word [edi+26]
2438
        test    eax, eax
2439
        jz      .zero_size
2440
; find new last sector
2441
@@:
2442
        sub     eax, 0x200
2443
        jbe     @f
2444
        movzx   ecx, word [0x282000+ecx*2]
2445
        jmp     @b
2446
@@:
2447
; we will zero data at the end of last sector - remember it
2448
        push    ecx
2449
; terminate FAT chain
2450
        lea     ecx, [0x282000+ecx+ecx]
2451
        push    dword [ecx]
2452
        mov     word [ecx], 0xFFF
2453
        pop     ecx
2454
        and     ecx, 0xFFF
2455
        jmp     .delete
2456
.zero_size:
2457
        and     word [edi+26], 0
2458
        push    0
2459
.delete:
2460
; delete FAT chain starting with ecx
2461
; mark all clusters as free
2462
        cmp     ecx, 0xFF8
2463
        jae     .deleted
2464
        lea     ecx, [0x282000+ecx+ecx]
2465
        push    dword [ecx]
2466
        and     word [ecx], 0
2467
        pop     ecx
2468
        and     ecx, 0xFFF
2469
        jmp     .delete
2470
.deleted:
2471
        mov     edi, [edi+28]
2472
; save directory & FAT
2473
        mov     eax, [esp+8]
2474
        pusha
2475
        call    save_chs_sector
2476
        popa
2477
        cmp     [FDC_Status], 0
2478
        jnz     .device_err2
2479
        call    save_flp_fat
2480
        cmp     [FDC_Status], 0
2481
        jnz     .device_err2
2482
; zero last sector, ignore errors
2483
        pop     eax
2484
        add     eax, 31
2485
        and     edi, 0x1FF
2486
        jz      .truncate_done
2487
        call    SetUserInterrupts
2488
        pusha
2489
        call    read_chs_sector
2490
        popa
2491
        add     edi, 0xD000
2492
        mov     ecx, 0xD000+0x200
2493
        sub     ecx, edi
2494
        push    eax
2495
        xor     eax, eax
2496
        rep     stosb
2497
        pop     eax
2498
        pusha
2499
        call    save_chs_sector
2500
        popa
2501
.truncate_done:
2502
        pop     ecx eax edi
2503
        xor     eax, eax
2504
        jmp     .doret
2505
 
86 diamond 2506
fs_FloppyGetFileInfo:
2507
        call    read_flp_fat
2508
        cmp     [FDC_Status], 0
2509
        jnz     ret11
2510
        cmp     byte [esi], 0
2511
        jnz     @f
2512
        mov     eax, 2  ; unsupported
2513
        ret
2514
@@:
2515
        push    edi
2516
        call    fd_find_lfn
2517
        jmp     fs_GetFileInfo_finish
2518
 
2519
ret11:
2520
        mov     eax, 11
2521
        ret
2522
 
2523
fs_FloppySetFileInfo:
2524
        call    read_flp_fat
2525
        cmp     [FDC_Status], 0
2526
        jnz     ret11
2527
        cmp     byte [esi], 0
2528
        jnz     @f
2529
        mov     eax, 2  ; unsupported
2530
        ret
2531
@@:
2532
        push    edi
2533
        call    fd_find_lfn
2534
        jnc     @f
2535
        pop     edi
2536
        mov     eax, ERROR_FILE_NOT_FOUND
2537
        ret
2538
@@:
2539
        push    eax
2540
        call    bdfe_to_fat_entry
2541
        pop     eax
2542
        pusha
2543
        call    save_chs_sector
2544
        popa
2545
        pop     edi
2546
        xor     eax, eax
2547
        cmp     [FDC_Status], 0
2548
        jz      @f
2549
        mov     al, 11
2550
@@:
2551
        ret
2552
 
321 diamond 2553
if 0
91 diamond 2554
;----------------------------------------------------------------
2555
;
2556
;  fs_FloppyExecute - LFN variant for executing from floppy
2557
;
2558
;  esi  points to floppy filename (e.g. 'dir1/name')
2559
;  ebp  points to full filename (e.g. '/fd/1/dir1/name')
2560
;  dword [ebx] = flags
2561
;  dword [ebx+4] = cmdline
2562
;
2563
;  ret ebx,edx destroyed
2564
;      eax > 0 - PID, < 0 - error
2565
;
2566
;--------------------------------------------------------------
2567
fs_FloppyExecute:
2568
        mov     edx, [ebx]
2569
        mov     ebx, [ebx+4]
2570
        test    ebx, ebx
2571
        jz      @f
2572
        add     ebx, std_application_base_address
2573
@@:
2574
 
2575
;----------------------------------------------------------------
2576
;
2577
; fs_FloppyExecute.flags - second entry
2578
;
2579
;  esi  points to floppy filename (kernel address)
2580
;  ebp  points to full filename
2581
;  edx  flags
2582
;  ebx  cmdline (kernel address)
2583
;
2584
;  ret  eax > 0 - PID, < 0 - error
2585
;
2586
;--------------------------------------------------------------
2587
 
2588
.flags:
2589
        call    read_flp_fat
2590
        cmp     byte [esi], 0
2591
        jnz     @f
2592
; cannot execute root!
2593
        mov     eax, -ERROR_ACCESS_DENIED
2594
        ret
2595
@@:
2596
        push    edi
2597
        call    fd_find_lfn
2598
        jnc     .found
2599
        pop     edi
2600
        mov     eax, -ERROR_FILE_NOT_FOUND
2601
        ret
2602
.found:
2603
        movzx   eax, word [edi+26]      ; cluster
2604
        push    eax
2605
        push    dword [edi+28]          ; size
2606
        push    .DoRead
2607
        call    fs_execute
2608
        add     esp, 12
2609
        pop     edi
2610
        ret
2611
 
2612
.DoRead:
2613
; read next block
2614
; in: eax->parameters, edi->buffer
2615
; out: eax = error code
2616
        pushad
2617
        cmp     dword [eax], 0  ; file size
2618
        jz      .eof
2619
        mov     eax, [eax+4]    ; cluster
2620
        add     eax, 31
2621
        call    read_chs_sector
2622
        cmp     [FDC_Status], 0
2623
        jnz     .err
2624
        pop     edi
2625
        mov     esi, 0xD000
2626
        push    edi
2627
        mov     ecx, 512/4
2628
        rep     movsd
2629
        mov     eax, [esp+28]
2630
        mov     ecx, [eax]
2631
        sub     ecx, 512
2632
        jae     @f
2633
        add     edi, ecx
2634
        neg     ecx
2635
        push    eax
2636
        xor     eax, eax
2637
        rep     stosb
2638
        pop     eax
2639
@@:
2640
        mov     [eax], ecx
2641
        mov     edx, [eax+4]
2642
        mov     dx, [edx*2+0x282000]
2643
        mov     [eax+4], dx     ; high word is already zero
2644
        popad
2645
        xor     eax, eax
2646
        ret
2647
.eof:
2648
        popad
2649
        mov     eax, 6
2650
        ret
2651
.err:
2652
        popad
2653
        mov     eax, 11
2654
        ret
321 diamond 2655
end if
91 diamond 2656
 
171 diamond 2657
;----------------------------------------------------------------
2658
;
2659
;  fs_FloppyDelete - delete file or empty folder from floppy
2660
;
2661
;  esi  points to filename
2662
;
2663
;  ret  eax = 0 ok or other = errormsg
2664
;
2665
;--------------------------------------------------------------
2666
fs_FloppyDelete:
2667
        call    read_flp_fat
2668
        cmp     [FDC_Status], 0
2669
        jz      @f
2670
        push    11
2671
        jmp     .pop_ret
2672
@@:
2673
        cmp     byte [esi], 0
2674
        jnz     @f
2675
; cannot delete root!
2676
.access_denied:
2677
        push    ERROR_ACCESS_DENIED
2678
.pop_ret:
2679
        pop     eax
2680
        ret
2681
@@:
2682
        and     [fd_prev_sector], 0
2683
        and     [fd_prev_prev_sector], 0
2684
        push    edi
2685
        call    fd_find_lfn
2686
        jnc     .found
2687
        pop     edi
2688
        push    ERROR_FILE_NOT_FOUND
2689
        jmp     .pop_ret
2690
.found:
2691
        cmp     dword [edi], '.   '
2692
        jz      .access_denied2
2693
        cmp     dword [edi], '..  '
2694
        jz      .access_denied2
2695
        test    byte [edi+11], 10h
2696
        jz      .dodel
2697
; we can delete only empty folders!
2698
        push    eax
2699
        movzx   eax, word [edi+26]
2700
        push    ebx
2701
        pusha
2702
        add     eax, 31
2703
        call    read_chs_sector
2704
        popa
2705
        mov     ebx, FDD_DATA + 2*0x20
2706
.checkempty:
2707
        cmp     byte [ebx], 0
2708
        jz      .empty
2709
        cmp     byte [ebx], 0xE5
2710
        jnz     .notempty
2711
        add     ebx, 0x20
2712
        cmp     ebx, FDD_DATA + 0x200
2713
        jb      .checkempty
2714
        movzx   eax, word [FLOPPY_FAT + eax*2]
2715
        pusha
2716
        add     eax, 31
2717
        call    read_chs_sector
2718
        popa
2719
        mov     ebx, FDD_DATA
2720
        jmp     .checkempty
2721
.notempty:
2722
        pop     ebx
2723
        pop     eax
2724
.access_denied2:
2725
        pop     edi
2726
        jmp     .access_denied
2727
.empty:
2728
        pop     ebx
2729
        pop     eax
2730
        pusha
2731
        call    read_chs_sector
2732
        popa
2733
.dodel:
2734
        push    eax
2735
        movzx   eax, word [edi+26]
2736
        xchg    eax, [esp]
2737
; delete folder entry
2738
        mov     byte [edi], 0xE5
2739
; delete LFN (if present)
2740
.lfndel:
2741
        cmp     edi, FDD_DATA
2742
        ja      @f
2743
        cmp     [fd_prev_sector], 0
2744
        jz      .lfndone
2745
        push    [fd_prev_sector]
2746
        push    [fd_prev_prev_sector]
2747
        pop     [fd_prev_sector]
2748
        and     [fd_prev_prev_sector], 0
2749
        pusha
2750
        call    save_chs_sector
2751
        popa
2752
        pop     eax
2753
        pusha
2754
        call    read_chs_sector
2755
        popa
2756
        mov     edi, FDD_DATA+0x200
2757
@@:
2758
        sub     edi, 0x20
2759
        cmp     byte [edi], 0xE5
2760
        jz      .lfndone
2761
        cmp     byte [edi+11], 0xF
2762
        jnz     .lfndone
2763
        mov     byte [edi], 0xE5
2764
        jmp     .lfndel
2765
.lfndone:
2766
        pusha
2767
        call    save_chs_sector
2768
        popa
2769
; delete FAT chain
2770
        pop     eax
2771
        test    eax, eax
2772
        jz      .done
2773
@@:
2774
        lea     eax, [FLOPPY_FAT + eax*2]
2775
        push    dword [eax]
2776
        and     word [eax], 0
2777
        pop     eax
2778
        and     eax, 0xFFF
2779
        jnz     @b
2780
.done:
2781
        call    save_flp_fat
2782
        pop     edi
2783
        xor     eax, eax
2784
        ret
2785
 
78 diamond 2786
; \end{diamond}