-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.asm
1468 lines (1092 loc) · 34.1 KB
/
fs.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; CONSTANTS SECTION:
FirstFATSector equ 1
FATSectors equ 9
FirstRootSector equ 19
RootSectors equ 14
SecondFATSector equ FirstFATSector + FATSectors
BytesPerSector equ 512
Sectors equ 2880
DataSectors equ Sectors - 33 ; total - predefined
FirstDataSector equ 33
MaxRootEntries dw 224
SectorsPerTrack dw 18
Heads dw 2
MaxOpenFiles equ 5
ModeNone equ 0
ModeRead equ 1
ModeWrite equ 2
; STRUCTURES SECTION:
fs_dir_entry:
.size equ 32
.basesize equ 8
.extsize equ 3
.namesize equ .basesize + .extsize
.name equ 0
.ext equ 8
.attribs equ 11
.reserved equ 12
.cr_time equ 14
.cr_date equ 16
.rd_date equ 18
.wr_time equ 20
.wr_date equ 22
.cluster equ 26
.length equ 28
fs_buffer:
.size equ BytesPerSector + 2 + 2 + 2 + 2 + 1
.buffer equ 0
.sector equ BytesPerSector
.offset equ BytesPerSector + 2
.pos equ BytesPerSector + 4
.dirent equ BytesPerSector + 6
.mode equ BytesPerSector + 8
; CODE SECTION:
fs_init_buffers:
push ax
push cx
push si
mov ax, 0
mov cx, MaxOpenFiles*fs_buffer.size
mov di, fs_buffer_index
rep stosb
pop si
pop cx
pop ax
ret
fs_read_root:
; OUT: root buffer loaded from the disk and ready
; to work with
push ax
push bx
push cx
mov ax, FirstRootSector ; Starting with the FirstRootSector
mov bx, root_buffer ; read the whole series of blocks
mov cl, RootSectors ; and save them in the root_buffer
call fs_read_sectors
pop cx
pop bx
pop ax
ret
fs_write_root:
; OUT: changes in the root buffer saved on the disk
push ax
push bx
push cx
mov ax, FirstRootSector ; Starting with the FirstRootSector
mov bx, root_buffer ; write the whole series of blocks
mov cl, RootSectors ; taking them from the root_buffer
call fs_write_sectors
pop cx
pop bx
pop ax
ret
fs_read_fat:
; OUT: FAT buffer loaded from the disk and ready
; to work with
push ax
push bx
push cx
mov ax, FirstFATSector ; Starting with the FirstFATSector
mov bx, fat_buffer ; read the whole series of blocks
mov cl, FATSectors ; and save them in the fat_buffer
call fs_read_sectors
pop cx
pop bx
pop ax
ret
fs_write_fat:
; OUT: changes in the FAT buffer saved on the disk
push ax
push bx
push cx
mov ax, FirstFATSector ; Starting with the FirstFATSector
mov bx, fat_buffer ; write the whole series of blocks
mov cl, FATSectors ; taking them from the fat_buffer
call fs_write_sectors
mov ax, SecondFATSector ; Repeat the same, but start saving
call fs_write_sectors ; with the first SecondFATSector instead
pop cx
pop bx
pop ax
ret
fs_open_read:
; IN: si: filename pointer
;
; OUT: ax: file descriptor
; OUT: cf: set on file not found
; OUT: the file is ready to be read
push di
push cx
push bx
call fs_find_file ; Does the file exist?
jc short .error ; If it doesn't, return with error
; If everything is fine, look for a free buffer
mov bx, fs_buffer_index ; Use bx as current buffer base pointer
mov cx, 0 ; Use cx as current buffer number
.loop:
cmp byte [bx+fs_buffer.mode], ModeNone
je short .success ; Finish if we found a free buffer
add bx, fs_buffer.size ; Make bx point to the next buffer
inc cx ; Update cx
cmp cx, MaxOpenFiles ; If we've reached the last buffer
jae short .error ; return with error
jmp short .loop ; Otherwise repeat the loop
.success:
; Now we are goint to initialize the buffer for the file.
; Notice that fs_find_file left its first sector number in ax,
; and it's directory entry pointer in di.
mov word [bx+fs_buffer.sector], ax
mov word [bx+fs_buffer.offset], 0
mov word [bx+fs_buffer.pos], 0
mov word [bx+fs_buffer.dirent], di
mov byte [bx+fs_buffer.mode], ModeRead ; Mark the buffer as used
clc
jmp short .return
.error:
stc
.return:
mov ax, cx ; Return the buffer number using ax
pop bx
pop cx
pop di
ret
fs_open_write:
; IN: si: filename pointer
;
; OUT: ax: file descriptor
; OUT: cf: set on file not found
; OUT: the file is ready to receive data
;
; VARS:
.size equ 4
.base equ 0
.fd equ 2
push bp
push di
push cx
push bx
push dx
sub sp, .size ; Allocate variables
mov bp, sp ;
; First we're going to check whether the file already exists,
; if it doesn't, an empty file with the requested name is created.
; Then we try to detect if the requested file is already open,
; if it is, the same fd will be used. In the case the file was
; not open, we look for a free buffer and return its number as fd.
call fs_find_file ; Does the file already exist?
jnc short .check_if_open ; If it does, check wheter its alraedy open
mov di, 0 ; If it doesn't, make sure di is zeroed
jmp short .get_fd ; Then try to find a free file buffer
.check_if_open:
mov bx, fs_buffer_index ; Use bx as current buffer base pointer
mov cx, 0 ; Use cx as current buffer number
.loop1:
cmp byte [bx+fs_buffer.mode], ModeWrite
jne .skip ; If the buffer is not in write mode, skip
cmp word [bx+fs_buffer.dirent], di
jne .skip ; If it doesn't refer to our file, skip
mov [bp+.fd], cx ; Otherwise remember cx,
jmp .success ; as it will be returned
.skip:
add bx, fs_buffer.size ; Try with the next buffer
inc cx
cmp cx, MaxOpenFiles ; If we haven't reached the last buffer
jnae short .loop1 ; continue the loop
; If the above loop ended and we're here, it means the file
; isn't open. Now we're going to find a free buffer to use for
; our file.
.get_fd:
mov bx, fs_buffer_index ; Use bx as current buffer base pointer
mov cx, 0 ; Use cx as current buffer number
.loop2:
cmp byte [bx+fs_buffer.mode], ModeNone
je short .found_fd ; If we found the buffer, break the loop
add bx, fs_buffer.size ; Otherwise try with the next buffer
inc cx
cmp cx, MaxOpenFiles ; If we haven't reached the last buffer
jnae short .loop2 ; continue the loop
; If we got here, it means all buffers are busy
; so we are going to communicate failure
jmp .error
.found_fd:
mov [bp+.fd], cx ; Save buffer number in .fd
mov [bp+.base], bx ; Save buffer base pointer in .base
cmp di, 0 ; di is zeroed if the file doesn't exist
jne short .get_last_sector ; If it does, get its last sector
; If it doesn't:
call fs_create_file ; Create the file
jc short .error
; Make the required registers ready for buffer initialization:
; di - is a directory entry pointer
mov ax, 0
mov dx, 0
mov cx, 0
jmp short .init ; Go to initialization
.get_last_sector:
.loop3:
mov dx, ax ; Save the current sector
call fs_get_next_sector
jnc short .loop3 ; If the next sector exists, repeat loop
; Load the last sector into the buffer:
mov ax, dx
; bx - fine
mov cl, 1
call fs_read_sectors
; Make the required registers ready for buffer initialization:
; First we are going to calculate the sector offset, and save it in dx
mov cx, ax ; Save ax, we'll need it later
mov dx, 0
mov bx, 512
mov ax, [di+fs_dir_entry.length]
div bx
mov ax, cx ; Restore ax
; Then we're going to obtain the length of the file, and save it in cx
mov cx, [di+fs_dir_entry.length]
cmp cx, 0 ; If the file is empty, we're done
je .init
; Otherwise check if the offset equals 0
cmp dx, 0
ja .init
; If it does, make it 512, as required by fs_read
mov dx, 512
.init:
; Now were going to initialize the buffer with the
; values we've prepared
mov bx, [bp+.base]
mov [bx+fs_buffer.sector], ax
mov [bx+fs_buffer.offset], dx
mov [bx+fs_buffer.pos], cx
mov [bx+fs_buffer.dirent], di
mov [bx+fs_buffer.mode], byte ModeWrite
.success:
mov ax, [bp+.fd] ; Save the descriptor in ax
add sp, .size ; Dealloc the variables
clc
jmp short .return
.error:
add sp, .size ; Dealloc the variables
stc
.return:
pop dx
pop bx
pop cx
pop di
pop bp
ret
fs_read:
; IN: ax: file descriptor
; IN: di: buffer to load the data to
; IN: cx: number of bytes to be read
;
; OUT: di: contains at least cx bytes of read data
; OUT: cx: number of bytes actually read
; OUT: cf: set on error or end of file
;
; VARS:
.size equ 4
.base equ 0
.max equ 2
push bp
push di
push si
push ax
push bx
push dx
sub sp, .size ; Allocate variables
mov bp, sp ;
; Now convert the file descriptor to buffer pointer
mov dx, 0
mov bx, fs_buffer.size
mul bx
add ax, fs_buffer_index
; Save the values in variables, and appropriate registers
mov [bp+.base], ax ; .base points to the buffer we use
mov [bp+.max], cx ; .max is the number of bytes to write
mov bx, ax ; bx points to the buffer we use
mov dx, 0 ; dx are the actual bytes written
cmp byte [bx+fs_buffer.mode], ModeRead
jne short .error ; Return error if it's not in read mode
cmp word [bp+.max], 0 ; Are there any bytes to be read?
je short .return ; If none, return immediately
mov si, bx ; Make si point to the start of
add si,[bx+fs_buffer.offset]; the buffer and increase it by offset
.loop:
mov bx, [bx+fs_buffer.dirent] ; Save dirent pointer in bx
mov ax, [bx+fs_dir_entry.length] ; Save file's length in ax
mov bx, [bp+.base] ; Restore bx
cmp ax, [bx+fs_buffer.pos] ; Does ax equal our pos in the file?
je short .error ; If so, we're asked to read too much
; If we reached the end of the sector, calculate the next one:
cmp word [bx+fs_buffer.offset], BytesPerSector
jae short .next_sector
.nexted:
; If we start reading a new sector, load it into memory:
cmp word [bx+fs_buffer.offset], 0
je short .load_sector
.loaded:
movsb ; Move a byte from buffer to user's di
inc dx ; Increment dx (read-bytes count)
inc word [bx+fs_buffer.offset] ; Increment the offset
inc word [bx+fs_buffer.pos] ; Increment the pos
cmp dx, [bp+.max] ; Have we read everything we need?
je short .success ; If so, success
jmp .loop ; Otherwise continue the loop
.next_sector:
mov ax, [bx+fs_buffer.sector] ; Get the next sector number
call fs_get_next_sector
jc short .error
mov [bx+fs_buffer.sector], ax ; Save the number in the buffer
jc short .error
mov word[bx+fs_buffer.offset], 0 ; Set buffer's offset to 0
jmp short .nexted ; Return to the main loop
.load_sector:
mov ax, [bx+fs_buffer.sector]
; bx - fine
mov cl, 1
call fs_read_sectors ; Load the next sector
mov word[bx+fs_buffer.offset], 0 ; Zero the offset
mov si, bx ; Move si to the base of the buffer
jmp short .loaded ; Return to the main loop
.error:
add sp, .size ; Dealloc variables
stc
jmp short .return
.success:
add sp, .size ; Dealloc variables
clc
.return:
mov cx, dx ; Save dx in cx
pop dx
pop bx
pop ax
pop si
pop di
pop bp
ret
fs_write:
; IN: ax: file descriptor
; IN: si: buffer to load the data from
; IN: cx: number of bytes to be written
;
; OUT: cx: number of bytes actually written
; OUT: cf: set on error
;
; VARS:
.size equ 4
.base equ 0
.max equ 2
push bp
push di
push si
push ax
push bx
push dx
sub sp, .size ; Allocate variables
mov bp, sp ;
; Now convert the file descriptor to buffer pointer
mov dx, 0
mov bx, fs_buffer.size
mul bx
add ax, fs_buffer_index
; Save the values in variables, and appropriate registers
mov [bp+.base], ax ; .base points to the buffer we use
mov [bp+.max], cx ; .max is the number of bytes to write
mov bx, ax ; bx points to the buffer we use
mov dx, 0 ; dx are the actual bytes written
cmp byte [bx+fs_buffer.mode], ModeWrite
jne .error ; Return error if it's not in write mode
cmp cx, 0 ; If we're asked to write 0 bytes, return
je .return
mov di, bx ; Set di to the start of the buffer
add di,[bx+fs_buffer.offset]; Increment di by the offset
.loop:
; If we've filled the buffer, write the contents to the disk:
cmp word [bx+fs_buffer.offset], BytesPerSector
jae short .store_sector
.stored:
; If we start writing a new sector, get it's number:
cmp word [bx+fs_buffer.offset], 0
je short .next_sector
.nexted:
movsb ; Move a byte from user's si to buffer
inc dx ; Increment dx (written-bytes count)
inc word [bx+fs_buffer.offset] ; Increment the offset
inc word [bx+fs_buffer.pos] ; Increment the pos
cmp dx, [bp+.max] ; Have we written everything we need?
je short .success ; If so, success
jmp short .loop ; Otherwise continue the loop
.next_sector:
mov bx, [bx+fs_buffer.dirent] ; Move dirent pointer to bx
mov bx, [bx+fs_dir_entry.length] ; Move file length to bx
cmp bx, 0 ; Is the file empty?
mov bx, [bp+.base] ; Restore the bx, but...
je .first_sector ; if the file is empty yet,
; it's a special case
mov ax, [bx+fs_buffer.sector] ; Move the current sector number to ax
mov bx, ax ; Save ax in bx for later
call fs_find_free_sector ; Find a free sector after ax
; TODO: handle it more gently:
jc os_fatal_error ; If there are no sectors left, panic
xchg bx, ax ; Move the new number to bx
; and the old to ax
call fs_set_next_sector ; Then make bx the next sector of ax
mov ax, bx
mov bx, 0FFFh
call fs_set_fat_entry ; Make ax the last sector
mov bx, [bp+.base] ; Restore bx as the buffer pointer
mov [bx+fs_buffer.sector], ax ; Set ax as the current sector
jmp short .nexted ; Return to the main loop
.first_sector:
; Here we're going to attach first sector to an empty file
mov ax, FirstDataSector
call fs_find_free_sector ; Search for the first free sector
mov bx, 0FFFh
call fs_set_fat_entry
mov bx, [bp+.base] ; Mark the sector as the last one
mov [bx+fs_buffer.sector], ax ; Set ax as the current sector
sub ax, 31 ; Translate ax to FAT entry number
mov bx,[bx+fs_buffer.dirent] ; Save dirent pointer to bx
mov [bx+fs_dir_entry.cluster],ax ; Save the entry as first
; one of our file
mov bx, [bp+.base] ; Restore bx as the buffer pointer
jmp short .nexted ; Return to the main loop
.store_sector:
mov ax, [bx+fs_buffer.sector]
; bx - fine
mov cl, 1
call fs_write_sectors ; Store the buffer on the disk
mov word[bx+fs_buffer.offset], 0 ; Zero the offset
mov di, bx ; Move di to the base of the buffer
; Now increment the file's length by the number bytes we've written
mov bx, [bx+fs_buffer.dirent]
add word [bx+fs_dir_entry.length], BytesPerSector
mov bx, [bp+.base] ; Restore bx as the buffer pointer
jmp .stored ; Return to the main loop
.error:
add sp, .size ; Dealloc variables
stc
jmp short .return
.success:
add sp, .size ; Dealloc variables
clc
.return:
mov cx, dx ; Save dx in cx
pop dx
pop bx
pop ax
pop si
pop di
pop bp
ret
fs_close:
; IN: ax: file descriptor
;
; OUT: the file is closed
; OUT: all buffered changes written to the disk
pusha
; Convert the file descriptor to buffer pointer
mov dx, 0
mov bx, fs_buffer.size
mul bx
add ax, fs_buffer_index
mov bx, ax
cmp byte [bx+fs_buffer.mode], ModeNone
je short .return ; Return if it's already closed
cmp byte [bx+fs_buffer.mode], ModeRead
je short .return ; Return if it was in read mode
cmp word [bx+fs_buffer.offset], 0 ; Are there bytes in the buffer?
je short .stored ; If so, skip storing them
; Now we're going to store the bytes left
; First save our position in the file in ax
mov ax, [bx+fs_buffer.pos]
mov cx, bx ; Save bx for later
mov bx, [bx+fs_buffer.dirent] ; Move dirent pointer to dx
mov [bx+fs_dir_entry.length], ax ; Save ax as the new file length
mov bx, cx ; Restore bx
mov ax, [bx+fs_buffer.sector]
; bx - fine
mov cl, 1 ; Write the buffered bytes
call fs_write_sectors ; to the disk
.stored:
call fs_write_fat ; Store the updated FAT buffer
call fs_write_root ; Store the updated root buffer
.return:
mov byte [bx+fs_buffer.mode], ModeNone ; Mark the buffer as unused
popa
ret
fs_filename_to_tag:
; IN: si: pointer to the filename string
; IN: di: pointer where the tag will be stored
;
; OUT: di: contains the tag
; OUT: cf: set on error (like an unproper filename)
pusha
mov bx, di
mov dx, di
mov al, '.'
call string_find_char
jnc short .error ; If there's no dot in the name, error
; di points to the dot in the name now
mov cx, di
sub cx, si ; Make cx the num of chars before the dot
mov di, dx ; Make di point to the given filename
mov dx,fs_dir_entry.basesize; Calculate the number of unused chars
sub dx, cx ; before the dot and save in dx
cmp cx,fs_dir_entry.basesize; If the base name is longer than
ja short .error ; the maximal allowed length, error
rep movsb ; Now rewrite the cx chars from si to di
mov al, ' '
mov cx, dx ; Now place a space in di for every
rep stosb ; unused character in the filename
; We have done everything we had to do with the base name, so
; now we will deal with the extension, but first - skip the dot:
inc si
call string_length ; How long the extension is?
mov cx, ax ; Store the result in cx
mov dx,fs_dir_entry.extsize ; Calc the difference between max ext. len
sub dx, cx ; and the actual one, save it in dx
cmp cx,fs_dir_entry.extsize
ja short .error ; If the extension is too long, error
cmp cx, 0
jna short .error ; If the extension is empty, error
rep movsb ; Rewrite the extension chars
mov al, ' '
mov cx, dx
rep stosb ; Now fill the rest with spaces
mov di, bx ; Move di back to the beggining of the tag
; Now we're going to make all the tag chars uppercase
mov cx,fs_dir_entry.namesize
.toupper:
mov al, byte [di] ; Read a di char to al
call string_char_islower
jnc short .skip ; If it's not lowercase, don't bother
add byte [di], `A`-`a` ; Otherwise make it uppercase
.skip:
inc di ; Go for the next character
loop .toupper ; Continue the loop if there are chars left
.success:
clc
jmp short .return
.error:
stc
.return:
popa
ret
fs_tag_to_filename:
; It is assumed that the tag is correct.
;
; IN: si: pointer to the tag string
; IN: di: pointer where the filename will be stored
;
; OUT: di: contains the tag
push si
push di
push ax
push cx
; We'll just take evey char in the tag, and skip it if it's a space,
; we'll also add a dot after having moved over 8 tag chars.
mov cx,fs_dir_entry.namesize
.loop:
cmp cx,fs_dir_entry.extsize ; Is only the extension left?
jne short .skipdot ; If no, don't bother
mov al, '.'
stosb ; But otherwise add a dot to the name
.skipdot:
lodsb ; Load the tag char to ax
cmp al, ` ` ; Is it a space?
je short .continue ; If so, skip it
call string_char_isupper ; Is it uppercase?
jnc short .islower ; If not, skip it
add al, `a`-`A` ; Otherwise make it lowecase
.islower:
stosb ; Add the char to the name
.continue:
loop .loop ; Repeat the loop if there are chars left
mov byte [di], 0 ; Mark the end of the name
pop cx
pop ax
pop di
pop si
ret
fs_create_file:
; IN: si: pointer to the filename
; IN: root directory in buffer
;
; OUT: di: entry pointer
; OUT: cf: set if could not create
; OUT: changes written to the disk
.size equ fs_dir_entry.namesize+2
.tag equ 0
push ax
push bx
push cx
push si
push bp
sub sp, .size ; Allocate variables
mov bp, sp ; bp points to .tag string
call fs_find_file
jnc short .error ; If the file exists, error
mov di, bp
call fs_filename_to_tag ; Save the tag in .tag
jc short .error ; If the filename wasn't correct, error
; Now we're going to look for a free directory entry
mov cx, [MaxRootEntries]
mov bx, root_buffer
.loop:
cmp byte [bx], 0
je short .create ; We've found the entry
cmp byte [bx], 0E5h
je short .create ; We've found the entry
add bx, fs_dir_entry.size ; Go for the next entry
loop .loop ; Repeat the loop if there are entires left
; If we've come to this point, it means no free entry was found
.error:
add sp, .size ; Dealloc vars
stc
jmp short .return
.create:
; Set the file's name in the dir entry to .tag
mov cx, fs_dir_entry.namesize
mov si, di
mov di, bx
add di, fs_dir_entry.name
rep movsb
; Now zero all the attributes in the entry
mov byte [bx+fs_dir_entry.attribs], 0
mov word [bx+fs_dir_entry.reserved], 0
mov word [bx+fs_dir_entry.cr_time], 0
mov word [bx+fs_dir_entry.cr_date], 0
mov word [bx+fs_dir_entry.rd_date], 0
mov word [bx+fs_dir_entry.wr_time], 0
mov word [bx+fs_dir_entry.wr_date], 0
mov word [bx+fs_dir_entry.cluster], 0
mov word [bx+fs_dir_entry.length], 0
mov di, bx ; Save the entry pointer in di
call fs_write_root ; Write the changes to the disk
add sp, .size ; Dealloc vars
clc
.return:
pop bp
pop si
pop cx
pop bx
pop ax
ret
fs_remove_file:
; IN: si: pointer to the filename
;
; OUT: cf: set if could not remove
; OUT: changes written to the disk
push ax
push bx
push di
; We're going to mark the file's dir entry as unused, then go through
; all of its FAT entries and mark them as unused
call fs_find_file ; But if the file doesn't exist,
jc short .error ; don't even try
mov byte [di], 0E5h ; Okay, mark the file as unused
; Thanks to fs_find file ax already contains the file's firt sector,
; now notice bx is the register used by fs_set_fat_entry, and we'll
; set the register to 0 only once
mov bx, 0
.loop:
call fs_set_fat_entry ; Set entry to 0
call fs_get_next_sector ; Get the next sector
jnc short .loop ; If sectors are left, repeat
.success:
call fs_write_fat ; Save FAT buffer to the disk
call fs_write_root ; Save root buffer to the disk
clc
jmp short .return
.error:
stc
.return:
pop di
pop bx
pop ax
ret
fs_rename_file:
; IN: si: original filename pointer
; IN: di: new filename pointer
;
; OUT: the desired file is renamed
; OUT: changes are written to the disk
;
; VARS:
.size equ fs_dir_entry.namesize
.tag equ 0
pusha
sub sp, .size
mov bp, sp
mov bx, si ; Save original si
mov cx, di ; Save original di
mov si, cx
call fs_find_file ; If the new filename belongs to an
jnc short .error ; existing file, return with error
mov di, bp
call fs_filename_to_tag ; Translate the filename to tag
jc short .error ; Is the filename correct?
mov si, bx
call fs_find_file
jc short .error ; Does the file we want to rename exist?
; Now di contains the file's directory entry pointer
mov cx, fs_dir_entry.namesize
mov si, bp
rep movsb ; Insert the new tag to the file data