Subversion Repositories Kolibri OS

Rev

Rev 77 | 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
36
    mov   eax,[eax+0x3000+4]
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
52
    mov    eax,0x282000
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
 
68
 
69
 
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
118
    mov    eax,10
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
 
234
read_chs_sector:
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      ; Сторона
276
    mov    [FDD_Sector],2      ; Сектор
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      ; Сторона
305
    mov    [FDD_Sector],2      ; Сектор
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
361
   add  edi,4
362
   add  esi,12
363
 
364
   cmp  edi,0x282000+2856*2   ;2849 clusters
365
   jnz  fcnew_1
366
 
367
   popad
368
   ret
369
 
370
check_label:
371
    pushad
372
    mov    [FDD_Track],0      ; Цилиндр
373
    mov    [FDD_Head],0      ; Сторона
374
    mov    [FDD_Sector],1      ; Сектор
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
385
    jne    fdc_status_error
386
    mov    esi,flp_label
387
    mov    edi,0xD000+39
388
    mov    ecx,15
389
    cld
390
    rep    cmpsb
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      ; Сторона
415
    mov    [FDD_Sector],2      ; Сектор
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
433
 
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      ; Сторона
444
    mov    [FDD_Sector],2      ; Сектор
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
 
470
 
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
    cmp    dl,0
549
    jne    l.21_2
550
    dec    dh
551
    cmp    dh,0
552
    jne    l.20_2
553
    jmp    frnoreadd_1
554
 
555
fdc_status_error_4:
556
    pop    edi esi edx ecx ebx eax
557
    add    esp,32
558
    jmp    fdc_status_error_1
559
 
560
fifoundd_1:
561
    mov    eax,[path_pointer_flp]
562
    cmp    [eax+36],byte 0
563
    je    fifoundd_2
564
    add    edi,0xf
565
    mov    eax,[edi]
566
    and    eax,65535
567
    mov    ebx,[path_pointer_flp]
568
    add    ebx,36
569
    call   get_cluster_of_a_path_flp
570
    jc     frnoreadd_1_1
571
    mov    edi,ebx
572
    add    edi,11
573
    jmp    fifoundd_2_1
574
fifoundd_2:
575
    dec    [FDD_Sector]
576
fifoundd_2_1:
577
    mov    [edi-11],byte 0xE5    ;mark filename deleted
578
    add    edi,0xf
579
    mov    eax,[edi]
580
    and    eax,65535
581
    mov    edi,eax            ;edi = cluster
582
frnewd_1:
583
    shl    edi,1            ;find next cluster from FAT
584
    add    edi,0x282000
585
    mov    eax,[edi]
586
    mov    [edi],word 0x0        ;clear fat chain cluster
587
    and    eax,4095
588
    mov    edi,eax
589
    cmp    edi,dword 4095        ;last cluster ?
590
    jz     frnoreadd2_1
591
    jmp    frnewd_1
592
 
593
frnoreadd2_1:
594
    call   WriteSectWithRetr
595
    cmp    [FDC_Status],0
596
    jne    fdc_status_error_4
597
    call   save_flp_fat
598
    cmp    [FDC_Status],0
599
    jne    fdc_status_error_4
600
;    pop    edi esi edx ecx ebx eax
601
;    add    esp,32
602
    mov    ebp,0       ; file found
603
;    jmp    filedelete_newtry_1
604
    jmp    frnoreadd_1
605
 
606
frnoreadd_1_1:
607
    cmp    [FDC_Status],0
608
    jne    fdc_status_error_4
609
frnoreadd_1:
610
    pop    edi esi edx ecx ebx eax
611
    add    esp,32
612
    mov    eax,ebp
613
    ret
614
 
615
floppy_filesave:
616
;----------------------------------------------------------
617
;
618
; filesave - sys floppy
619
;
620
; eax      ; pointer to file name 11 chars - for root directory
621
; ebx      ; buffer
622
; ecx      ; count to write in bytes
623
; edx      ; 0 create new , 1 append
624
; edi  pointer to path   /fd/1/......  - for all files in nested directories
625
;
626
; output : eax = 0 - ok
627
;                5 - file not found / directory not found
628
;                8 - disk full
629
;               10 - access denied
630
;-----------------------------------------------------------
631
    mov    [path_pointer_flp],edi
632
    sub  esp,32
633
    call expand_filename
634
    cmp  edx,0
635
    jnz  fsdel_1
636
    pusha
637
    call floppy_filedelete
638
    cmp    [FDC_Status],0
639
    jne    fdc_status_error_6
640
    popa
641
    mov    [save_flag],1
642
fsdel_1:
643
    call   floppy_free_space
644
    cmp    [FDC_Status],0
645
    jne    fdc_status_error_6
646
    cmp    ecx,edi
647
    jb     rd_do_save_1
648
    add    esp,32
649
    mov    eax,8    ; not enough free space
650
    mov   [flp_status],0
651
    ret
652
 
653
fdc_status_error_6:
654
    popa
655
    add    esp,32
656
    jmp    fdc_status_error_1
657
 
658
rd_do_save_1:
659
    push   eax ebx ecx edx esi edi
660
    call   read_flp_fat
661
    cmp    [FDC_Status],0
662
    jne    fdc_status_error_7
663
    push   eax
664
    mov    eax,[path_pointer_flp]
665
    cmp    [eax+36],byte 0
666
    jne    fifoundds_2
667
    pop    eax
668
    mov    [save_root_flag],1
669
    call   read_flp_root
670
    cmp    [FDC_Status],0
671
    jne    fdc_status_error_7
672
    mov    edi,0x8000   ;Point at directory
673
    mov    edx,224 +1
674
    ; find an empty spot for filename in the root dir
675
l20ds_1:
676
    sub    edx,1
677
    cmp    edx,0
678
    jnz    l21ds_1
679
    jmp    frnoreadds_1
680
l21ds_1:
681
    cmp    [edi],byte 0xE5
682
    jz     fifoundds_1
683
    cmp    [edi],byte 0x0
684
    jz     fifoundds_1
685
    add    edi,32            ; Advance to next entry
686
    jmp    l20ds_1
687
 
688
fifoundds_2:
689
    pop    eax
690
    mov    [save_root_flag],0
691
    mov    [FDD_Track],0      ; Цилиндр
692
    mov    [FDD_Head],1       ; Сторона
693
    mov    [FDD_Sector],2      ; Сектор
694
    call   SeekTrack
695
    mov    dh,14
696
l.20_3:
697
    call    ReadSectWithRetr
698
    cmp    [FDC_Status],0
699
    jne    fdc_status_error_7
700
    mov    dl,16
701
    mov    edi,0xD000
702
    inc     [FDD_Sector]
703
l.21_3:
704
    mov    esi,eax            ;Name of file we want
705
    mov    ecx,11
706
    cld
707
    rep    cmpsb            ;Found the file?
708
    je     fifoundds_3          ;Yes
709
    add    ecx,21
710
    add    edi, ecx         ;Advance to next entry
711
    dec    dl
712
    cmp    dl,0
713
    jne    l.21_3
714
    dec    dh
715
    cmp    dh,0
716
    jne    l.20_3
717
fdc_status_error_8:
718
    pop     edi esi edx ecx ebx eax
719
    mov    eax,5            ; file not found ?
720
    mov    ebx,-1
721
    add    esp,32
722
    mov    [flp_status],0
723
    ret
724
 
725
fifoundds_3:
726
    add    edi,0xf
727
    mov    eax,[edi]
728
    and    eax,65535
729
    mov    ebx,[path_pointer_flp]
730
    add    ebx,36
731
    call   get_cluster_of_a_path_flp
732
    jc     fdc_status_error_7_1
733
found_directory_for_writing_flp:
734
    call   analyze_directory_to_write_flp
735
    jc     fdc_status_error_7_1
736
    mov    edi,ebx
737
fifoundds_1:
738
    push   edi            ; move the filename to root dir
739
    mov    esi,[esp+4+20]
740
    cmp    [save_root_flag],0
741
    jne    fifoundds_4
742
    mov    esi,[pointer_file_name_flp]
743
fifoundds_4:
744
    mov    ecx,11
745
    cld
746
    rep    movsb
747
    pop    edi
748
    mov    edx,edi
749
    add    edx,11+0xf        ; edx <- cluster save position
750
    mov    ebx,[esp+12]        ; save file size
751
    mov    [edi+28],ebx
752
    mov    [edi+11],byte 0x20    ; attribute
753
    call   get_date_for_file     ; from FAT32.INC
754
    mov    [edi+24],ax      ; date
755
    mov    [edi+18],ax      ; date
756
    call   get_time_for_file     ; from FAT32.INC
757
    mov    [edi+22],ax      ; time
758
    xor    ax,ax
759
    mov    [edi+20],ax
760
    mov    ebx,1            ; first cluster
761
    cmp    [save_root_flag],0
762
    jne    frnewds_1
763
    call   frnewds_2
764
    pusha
765
    call   WriteSectWithRetr
766
    popa
767
    cmp    [FDC_Status],0
768
    jne    fdc_status_error_7
769
    jmp    frnewds_3
770
 
771
frnewds_1:
772
    call   frnewds_2
773
frnewds_3:
774
    pusha                ; move save to floppy cluster
775
    add    ebx,31
776
    mov    eax,ebx
777
    mov    esi,[esp+32+16]
778
    call   take_data_from_application_1
779
    call   save_chs_sector
780
    cmp    [FDC_Status],0
781
    jne    fdc_status_error_7
782
    popa
783
    mov    eax,[esp+12]
784
    cmp    eax,512
785
    jb     flnsa_1
786
    sub    eax,512
787
    mov    [esp+12],eax
788
    mov    eax,[esp+16]
789
    add    eax,512
790
    mov    [esp+16],eax
791
    jmp    frnewds_1
792
 
793
frnewds_2:
794
    add    ebx,1
795
    mov    edi,ebx            ; find free cluster in FAT
796
    shl    edi,1
797
    add    edi,0x282000
798
    mov    eax,[edi]
799
    and    eax,4095
800
    cmp    eax,0x0
801
    jnz    frnewds_2
802
    mov    [edx],bx         ; save next cluster pos. to prev cl.
803
    mov    edx,edi            ; next save pos abs mem add
804
    ret
805
 
806
flnsa_1:
807
    mov    [edi],word 4095        ; mark end of file - last cluster
808
    cmp    [save_root_flag],1
809
    jne    flnsa_2
810
    call   save_flp_root
811
    cmp    [FDC_Status],0
812
    jne    fdc_status_error_7
813
flnsa_2:
814
    call   save_flp_fat
815
    cmp    [FDC_Status],0
816
    jne    fdc_status_error_7
817
frnoreadds_1:
818
    pop    edi esi edx ecx ebx eax
819
    add    esp,32
820
    mov    eax,0
821
    mov   [flp_status],0
822
    ret
823
 
824
fdc_status_error_7_1:
825
    cmp    [FDC_Status],0
826
    je    fdc_status_error_8
827
fdc_status_error_7:
828
    pop    edi esi edx ecx ebx eax
829
    add    esp,32
830
    jmp    fdc_status_error_1
831
 
832
save_chs_sector:
833
    call    calculate_chs
834
    call    WriteSectWithRetr
835
    ret
836
 
837
calculate_chs:
838
    mov    bl,[FDD_Track]
839
    mov    [old_track],bl
840
    mov    ebx,18
841
    xor    edx,edx
842
    div    ebx
843
    inc    edx
844
    mov    [FDD_Sector],dl
845
    xor    edx,edx
846
    mov    ebx,2
847
    div    ebx
848
    mov    [FDD_Track],al
849
    mov    [FDD_Head],0
850
    cmp    edx,0
851
    je     no_head_2
852
    inc    [FDD_Head]
853
no_head_2:
854
    mov     dl,[old_track]
855
    cmp     dl,[FDD_Track]
856
    je      no_seek_track_1
857
    call    SeekTrack
858
no_seek_track_1:
859
    ret
860
 
861
 
862
get_cluster_of_a_path_flp:
863
;---------------------------------------------------------
864
; input  : EBX = pointer to a path string
865
;          (example: the path "/files/data/document" become
866
;                             "files......data.......document...0"
867
;          '.' = space char
868
;          '0' = char(0) (ASCII=0) !!! )
869
; output : if (CARRY=1) -> ERROR in the PATH
870
;          if (CARRY=0) -> EAX=cluster
871
;---------------------------------------------------------
872
 
873
    push  edx
874
    mov   edx,ebx
875
 
876
search_end_of_path_flp:
877
    cmp   [save_flag],0
878
    jne   search_end_of_path_flp_1
879
    cmp   byte [edx],0
880
    je    found_end_of_path_flp
881
    jmp   search_end_of_path_flp_2
882
search_end_of_path_flp_1:
883
    cmp   byte [edx+12],0
884
    je    found_end_of_path_flp
885
search_end_of_path_flp_2:
886
    inc   edx ; '/'
887
    call  analyze_directory_flp
888
    jc    directory_not_found_flp
889
 
890
    mov   eax,[ebx+20-2]        ; read the HIGH 16bit cluster field
891
    mov   ax,[ebx+26]           ; read the LOW 16bit cluster field
892
    and   eax,0xfff           ;[fatMASK]
893
    add   edx,11                ; 8+3 (name+extension)
894
    jmp   search_end_of_path_flp
895
 
896
found_end_of_path_flp:
897
    inc   edx
898
    mov   [pointer_file_name_flp],edx
899
    pop   edx
900
    clc                         ; no errors
901
    ret
902
 
903
directory_not_found_flp:
904
    pop   edx
905
    stc                         ; errors occour
906
    ret
907
 
908
analyze_directory_flp:
909
;--------------------------------
910
; input  : EAX = first cluster of the directory
911
;          EBX = pointer to filename
912
; output : IF CARRY=0 EAX = sector where th file is found
913
;                     EBX = pointer in buffer
914
;                     [buffer .. buffer+511]
915
;                     ECX,EDX,EDI,EDI not changed
916
;          IF CARRY=1
917
;--------------------------------
918
   push ebx ;[esp+16]
919
   push ecx
920
   push edx
921
   push esi
922
   push edi
923
 
924
 
925
adr56_flp:
926
   mov [clust_tmp_flp],eax
927
   add    eax,31
928
   pusha
929
   call   read_chs_sector
930
   popa
931
   cmp    [FDC_Status],0
932
   jne    not_found_file_analyze_flp
933
 
934
   mov ecx,512/32
935
   mov ebx,0xD000
936
 
937
adr1_analyze_flp:
938
   mov esi,edx   ;[esp+16]
939
   mov edi,ebx
940
   cld
941
   push ecx
942
   mov ecx,11
943
   rep cmpsb
944
   pop ecx
945
   je found_file_analyze_flp
946
 
947
   add ebx,32
948
   loop adr1_analyze_flp
949
 
950
    mov eax,[clust_tmp_flp]
951
    shl    eax,1            ;find next cluster from FAT
952
    add    eax,0x282000
953
    mov    eax,[eax]
954
    and    eax,4095
955
    cmp eax,0x0ff8
956
    jb  adr56_flp
957
not_found_file_analyze_flp:
958
   pop edi
959
   pop esi
960
   pop edx
961
   pop ecx
962
   add esp,4
963
   stc        ;file not found
964
   ret
965
 
966
found_file_analyze_flp:
967
   pop edi
968
   pop esi
969
   pop edx
970
   pop ecx
971
   add esp,4
972
   clc        ;file found
973
   ret
974
 
975
 
976
analyze_directory_to_write_flp:
977
;--------------------------------
978
; input  : EAX = first cluster of the directory
979
; output : IF CARRY=0 EAX = sector where the file is found
980
;                     EBX = pointer in buffer
981
;                     [buffer .. buffer+511]
982
;                     ECX,EDX,EDI,EDI not changed
983
;          IF CARRY=1
984
;--------------------------------
985
 
986
   push ecx
987
   push edx
988
   push esi
989
 
990
adr561:
991
   mov [clust_tmp_flp],eax
992
   add    eax,31
993
   pusha
994
   call   read_chs_sector
995
   popa
996
   cmp    [FDC_Status],0
997
   jne    error_found_file_analyze1
998
 
999
   mov ecx,512/32
1000
   mov ebx,0xD000
1001
 
1002
adr1_analyze1:
1003
   cmp byte [ebx],0x00
1004
   je  found_file_analyze1
1005
   cmp byte [ebx],0xe5
1006
   je  found_file_analyze1
1007
 
1008
avanti:
1009
   add ebx,32
1010
   loop adr1_analyze1
1011
 
1012
   mov eax,[clust_tmp_flp]
1013
   shl    eax,1            ;find next cluster from FAT
1014
   add    eax,0x282000
1015
   mov    eax,[eax]
1016
   and    eax,4095
1017
   cmp eax,0x0ff8
1018
   jb  adr561
1019
 
1020
   call get_free_FAT               ;this block of code add a new cluster
1021
                                   ;for the directory because the directory
1022
                                   ;is full
1023
 
1024
   mov [edi],word 0x0fff
1025
 
1026
   mov eax,[clust_tmp_flp]
1027
   shl    eax,1            ;find next cluster from FAT
1028
   add    eax,0x282000
1029
   sub    edi,0x282000
1030
   mov    [eax],di
1031
 
1032
   pusha
1033
   mov ecx,512/4
1034
   xor eax,eax
1035
   mov edi,0xD000
1036
   cld
1037
   rep stosd
1038
   popa
1039
 
1040
   mov    eax,edi
1041
   add    eax,31
1042
   pusha
1043
   call   save_chs_sector
1044
   popa
1045
   cmp    [FDC_Status],0
1046
   jne    error_found_file_analyze1
1047
   mov    ebx,0xD000
1048
 
1049
found_file_analyze1:
1050
 
1051
   pop esi
1052
   pop edx
1053
   pop ecx
1054
   clc        ;file found
1055
   ret
1056
 
1057
error_found_file_analyze1:
1058
   pop esi
1059
   pop edx
1060
   pop ecx
1061
   stc
1062
   ret
1063
 
1064
get_free_FAT_flp:
1065
;------------------------------------------
1066
; input  :  EAX = # cluster for start the searching
1067
; output :  EAX = # first cluster found free
1068
;-------------------------------------------
1069
   push ebx
1070
 
1071
    mov    ebx,1
1072
check_new_flp:
1073
    add    ebx,1
1074
    mov    edi,ebx            ; find free cluster in FAT
1075
    shl    edi,1
1076
    add    edi,0x282000
1077
    mov    eax,[edi]
1078
    and    eax,4095
1079
    cmp    eax,0x0
1080
    jnz    check_new_flp
1081
 
1082
   pop ebx
1083
   ret
71 diamond 1084
 
1085
; \begin{diamond}
1086
fd_find_lfn:
1087
; in: esi->name
1088
; out: CF=1 - file not found
1089
;      else CF=0 and edi->direntry
75 diamond 1090
        pusha
1091
        sub     esp, 262*2      ; reserve place for LFN
1092
        mov     ebp, esp
1093
        push    0               ; for fat_get_name: read ASCII name
1094
        call    read_flp_fat
1095
        cmp     [FDC_Status], 0
1096
        jnz     .error
1097
        mov     eax, 19
1098
        mov     dh, 14
71 diamond 1099
.main_loop:
1100
.20_1:
75 diamond 1101
        pusha
1102
        call    read_chs_sector
1103
        popa
1104
        cmp     [FDC_Status], 0
1105
        jnz     .error
1106
        mov     edi, 0xD000
1107
        inc     [FDD_Sector]
1108
        push    eax
71 diamond 1109
.21_1:
75 diamond 1110
        call    fat_get_name
1111
        jc      @f
1112
        call    fat_compare_name
1113
        jz      .found
71 diamond 1114
@@:
75 diamond 1115
        add     edi, 0x20
1116
        cmp     edi, 0xD200
1117
        jb      .21_1
1118
        pop     eax
1119
        inc     eax
1120
        dec     dh
1121
        js      @f
1122
        jnz     .20_1
71 diamond 1123
.error:
75 diamond 1124
        add     esp, 262*2+4
1125
        popa
1126
        stc
1127
        ret
71 diamond 1128
@@:
1129
; read next sector from FAT
75 diamond 1130
        mov     eax, [(eax-31-1)*2+0x282000]
1131
        and     eax, 0xFFF
1132
        cmp     eax, 0xFF8
1133
        jae     .error
1134
        add     eax, 31
1135
        jmp     .main_loop
71 diamond 1136
.found:
75 diamond 1137
        pop     eax
71 diamond 1138
; if LFN entry, advance to corresponding short entry
75 diamond 1139
        cmp     byte [edi+11], 0xF
1140
        jnz     .entryfound
1141
        add     edi, 0x20
1142
        cmp     edi, 0xD200
1143
        jb      .entryfound
1144
        dec     dh
1145
        jz      .error
1146
        inc     eax
1147
        call    read_chs_sector
1148
        cmp     [FDC_Status], 0
1149
        jnz     .error
1150
        mov     edi, 0xD000
71 diamond 1151
.entryfound:
75 diamond 1152
        cmp     byte [esi], 0
1153
        jz      .done
1154
        test    byte [edi+11], 10h	; is a directory?
1155
        jz      .error
1156
        movzx   eax, word [edi+26]
1157
        add     eax, 31
1158
        mov     dh, 0
1159
        jmp     .main_loop
71 diamond 1160
.done:
75 diamond 1161
        add     esp, 262*2+4+4
1162
        push    edi
1163
        popad
1164
        ret
71 diamond 1165
 
1166
;----------------------------------------------------------------
1167
;
1168
;  fs_FloppyRead - LFN variant for reading floppy
1169
;
1170
;  esi  points to filename
1171
;  ebx  pointer to 64-bit number = first wanted byte, 0+
1172
;       may be ebx=0 - start from first byte
1173
;  ecx  number of bytes to read, 0+
1174
;  edx  mem location to return data
1175
;
77 diamond 1176
;  ret ebx = bytes read or 0xffffffff file not found
71 diamond 1177
;      eax = 0 ok read or other = errormsg
1178
;
1179
;--------------------------------------------------------------
1180
fs_FloppyRead:
75 diamond 1181
        call    read_flp_fat
1182
        cmp     byte [esi], 0
1183
        jnz     @f
1184
        or      ebx, -1
1185
        mov     eax, 10         ; access denied
1186
        ret
71 diamond 1187
@@:
75 diamond 1188
        push    edi
1189
        call    fd_find_lfn
1190
        jnc     .found
1191
        pop     edi
1192
        or      ebx, -1
1193
        mov     eax, 5          ; file not found
1194
        ret
71 diamond 1195
.found:
75 diamond 1196
        test    ebx, ebx
1197
        jz      .l1
1198
        cmp     dword [ebx+4], 0
1199
        jz      @f
77 diamond 1200
        xor     ebx, ebx
71 diamond 1201
.reteof:
75 diamond 1202
        mov     eax, 6          ; EOF
1203
        pop     edi
1204
        ret
71 diamond 1205
@@:
75 diamond 1206
        mov     ebx, [ebx]
71 diamond 1207
.l1:
77 diamond 1208
        push    ecx edx
1209
        push    0
1210
        mov     eax, [edi+28]
1211
        sub     eax, ebx
1212
        jb      .eof
1213
        cmp     eax, ecx
1214
        jae     @f
1215
        mov     ecx, eax
1216
        mov     byte [esp], 6           ; EOF
1217
@@:
75 diamond 1218
        movzx   edi, word [edi+26]
71 diamond 1219
.new:
75 diamond 1220
        jecxz   .done
1221
        test    edi, edi
1222
        jz      .eof
1223
        cmp     edi, 0xFF8
1224
        jae     .eof
77 diamond 1225
        lea     eax, [edi+31]
75 diamond 1226
        pusha
1227
        call    read_chs_sector
1228
        popa
1229
        cmp     [FDC_Status], 0
1230
        jnz     .err
1231
        sub     ebx, 512
1232
        jae     .skip
77 diamond 1233
        lea     eax, [0xD000+ebx+512]
75 diamond 1234
        neg     ebx
1235
        push    ecx
1236
        cmp     ecx, ebx
1237
        jbe     @f
1238
        mov     ecx, ebx
71 diamond 1239
@@:
75 diamond 1240
        mov     ebx, edx
1241
        call    memmove
1242
        add     edx, ecx
1243
        sub     [esp], ecx
1244
        pop     ecx
1245
        xor     ebx, ebx
71 diamond 1246
.skip:
75 diamond 1247
        movzx   edi, word [edi*2+0x282000]
1248
        jmp     .new
71 diamond 1249
.done:
77 diamond 1250
        mov     ebx, edx
1251
        pop     eax edx ecx edi
1252
        sub     ebx, edx
75 diamond 1253
        ret
71 diamond 1254
.eof:
77 diamond 1255
        mov     ebx, edx
1256
        pop     eax edx ecx
75 diamond 1257
        jmp     .reteof
71 diamond 1258
.err:
77 diamond 1259
        mov     ebx, edx
1260
        pop     eax edx ecx edi
1261
        sub     ebx, edx
1262
        mov     al, 5  ; may be other error code?
75 diamond 1263
        ret
1264
 
1265
;----------------------------------------------------------------
1266
;
1267
;  fs_FloppyReadFolder - LFN variant for reading floppy folders
1268
;
1269
;  esi  points to filename
78 diamond 1270
;  ebx  pointer to structure: 32-bit number = first wanted block, 0+
1271
;                           & flags (bitfields)
1272
; flags: bit 0: 0=ANSI names, 1=UNICODE names
75 diamond 1273
;  ecx  number of blocks to read, 0+
1274
;  edx  mem location to return data
1275
;
77 diamond 1276
;  ret ebx = blocks read or 0xffffffff folder not found
75 diamond 1277
;      eax = 0 ok read or other = errormsg
1278
;
1279
;--------------------------------------------------------------
1280
fs_FloppyReadFolder:
1281
        call    read_flp_fat
1282
        push    edi
1283
        cmp     byte [esi], 0
1284
        jz      .root
1285
        call    fd_find_lfn
1286
        jnc     .found
1287
        pop     edi
1288
        or      ebx, -1
1289
        mov     eax, ERROR_FILE_NOT_FOUND
1290
        ret
1291
.found:
1292
        test    byte [edi+11], 0x10     ; do not allow read files
1293
        jnz     .found_dir
1294
        pop     edi
1295
        or      ebx, -1
1296
        mov     eax, ERROR_ACCESS_DENIED
1297
        ret
1298
.found_dir:
1299
        movzx   eax, word [edi+26]
1300
        add     eax, 31
1301
        push    0
1302
        jmp     .doit
1303
.root:
1304
        mov     eax, 19
1305
        push    14
1306
.doit:
1307
        push    ecx ebp
1308
        sub     esp, 262*2      ; reserve space for LFN
1309
        mov     ebp, esp
78 diamond 1310
        push    dword [ebx+4]   ; for fat_get_name: read ANSI/UNICODE names
1311
        mov     ebx, [ebx]
75 diamond 1312
; init header
1313
        push    eax ecx
1314
        mov     edi, edx
1315
        mov     ecx, 32/4
1316
        xor     eax, eax
1317
        rep     stosd
1318
        pop     ecx eax
1319
        mov     byte [edx], 1   ; version
1320
        mov     esi, edi        ; esi points to BDFE
1321
.main_loop:
1322
        pusha
1323
        call    read_chs_sector
1324
        popa
1325
        cmp     [FDC_Status], 0
1326
        jnz     .error
1327
        mov     edi, 0xD000
1328
        push    eax
1329
.l1:
1330
        call    fat_get_name
1331
        jc      .l2
1332
        cmp     byte [edi+11], 0xF
1333
        jnz     .do_bdfe
1334
        add     edi, 0x20
1335
        cmp     edi, 0xD200
1336
        jb      .do_bdfe
1337
        pop     eax
1338
        inc     eax
1339
        dec     byte [esp+262*2+12]
1340
        jz      .done
1341
        jns     @f
1342
; read next sector from FAT
1343
        mov     eax, [(eax-31-1)*2+0x282000]
1344
        and     eax, 0xFFF
1345
        cmp     eax, 0xFF8
1346
        jae     .done
1347
        add     eax, 31
1348
        mov     byte [esp+262*2+12], 0
1349
@@:
1350
        pusha
1351
        call    read_chs_sector
1352
        popa
1353
        cmp     [FDC_Status], 0
1354
        jnz     .error
1355
        mov     edi, 0xD000
1356
        push    eax
1357
.do_bdfe:
1358
        inc     dword [edx+8]   ; new file found
1359
        dec     ebx
1360
        jns     .l2
1361
        dec     ecx
1362
        js      .l2
1363
        inc     dword [edx+4]   ; new file block copied
1364
        call    fat_entry_to_bdfe
1365
.l2:
1366
        add     edi, 0x20
1367
        cmp     edi, 0xD200
1368
        jb      .l1
1369
        pop     eax
1370
        inc     eax
1371
        dec     byte [esp+262*2+12]
1372
        jz      .done
1373
        jns     @f
1374
; read next sector from FAT
1375
        mov     eax, [(eax-31-1)*2+0x282000]
1376
        and     eax, 0xFFF
1377
        cmp     eax, 0xFF8
1378
        jae     .done
1379
        add     eax, 31
1380
        mov     byte [esp+262*2+12], 0
1381
@@:
1382
        jmp     .main_loop
1383
.error:
1384
        add     esp, 262*2+4
1385
        pop     ebp ecx edi edi
1386
        or      ebx, -1
1387
        mov     eax, ERROR_FILE_NOT_FOUND
1388
        ret
1389
.done:
1390
        add     esp, 262*2+4
1391
        pop     ebp
77 diamond 1392
        mov     ebx, [edx+4]
75 diamond 1393
        xor     eax, eax
1394
        dec     ecx
1395
        js      @f
1396
        mov     al, ERROR_END_OF_FILE
1397
@@:
1398
        pop     ecx edi edi
1399
        ret
1400
 
78 diamond 1401
; \end{diamond}