-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_dir_entry.aii
1009 lines (759 loc) · 12.5 KB
/
get_dir_entry.aii
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
;
; This file contains the implementation of get_dir_entry.
; It also contains other functions for finding an entry within
; a directory.
;
string asis
include 'gsos.equ'
include 'minix.equ'
include 'records.equ'
include 'fst.equ'
include 'fst.macros'
include 'M16.Debug'
include 'p.equ'
import disk_inode:v1_inode
import disk_super:v1_super
;
; dirent_entry 0 is a special case.
; (assuming base = 1, displacement = 1)
; first call:
; before
; dirent_entry = 0, dirent_zone = 0, dirent_offset = 0
; after:
; dirent_entry = 1, dirent_zone = 0, dirent_offset = 0
;
; subsequent calls:
; before
; dirent_entry = 1, dirent_zone = ..., dirent_offset = ...
; after
; dirent_entry = 2, dirent_zone = ..., dirent_offset = ...
;
; don't think this needs to be dp... move to a normal record.
data record
; size of the directory, divided by dirent_size.
size ds.l 1
displacement ds.w 1
dirent_size ds.w 1
dirent_zone ds.w 1
dirent_offset ds.w 1
dirent_entry ds.w 1
dirent ds v1L_dirent
endr
import init_fcr
import init_vcr
import load_inode
import read_data_block
import do_ignore
import do_eof
import do_blocks
import do_r_eof
import do_r_blocks
import do_access
import do_file_sys_id
import do_option_list
import do_create_date_time
import do_mod_date_time
import do_file_type
import do_aux_type
entry init
entry base_displace
entry find_next_dirent
entry find_absolute_dirent
entry find_dirent_block
entry count_entries
entry count_dirent_block
entry do_flags
entry do_name_buffer_0
entry do_name_buffer_1
entry do_entry_num
entry sparse_dirent_block
get_dir_entry procname export
with fst_parms
with dp
with data
;~DebugSetTrace #1
jsr init_vcr
jsr init_fcr
;brk $ea
; check for read-access
ldy #fcr.access
lda [<my_fcr],y
and #read_access
bne access_ok
lda #invalid_access
sec
rtl
access_ok
; is it a dir?
ldy #fcr.disk_inode.mode
lda [my_fcr],y
and #S_IFMT
cmp #S_IFDIR
beq dir
lda #bad_store_type
sec
rtl
dir
jsr init
jsr base_displace
bcc @ok
rtl
@ok
; if data.dirent.inode is valid, load the inode
; (if not valid, this was a base/displace of 0 to return
; the dirent count. field info will be returned from the
; directory inode)
lda data.dirent.inode
beq dispatch
; if inode == the directory inode (.) , no need to re-load it.
cmp inode
beq dispatch
sta inode
jsr load_inode
bcc dispatch
exit
rtl
dispatch
lda <call_class
beq class0
class1
lda [param_blk_ptr] ; pcount
dec a
asl a ; x 2
asl a ; x 4
tax
dispatch get_dir_entry_dcb_1
;~DebugHexDump param_blk_ptr, #DirEntryRecGS.__sizeof
lda tool_error
cmp #1
rtl
class0
ldx #get_dir_entry_dcb_0_size-4
dispatch get_dir_entry_dcb_0
lda tool_error
cmp #1
rtl
get_dir_entry_dcb_0
dc.w $00, do_ignore ; refnum
dc.w $02, do_flags
dc.w $04, do_ignore ; base
dc.w $06, do_ignore ; displacement
dc.w $08, do_name_buffer_1 ; uses gs/os output string!
dc.w $0c, do_entry_num
dc.w $0e, do_file_type
dc.w $10, do_eof
dc.w $14, do_blocks
dc.w $18, do_create_date_time
dc.w $20, do_mod_date_time
dc.w $28, do_access
dc.w $2a, do_aux_type
dc.w $2e, do_file_sys_id
get_dir_entry_dcb_0_size equ *-get_dir_entry_dcb_0
get_dir_entry_dcb_1
;dc.w $00, do_ignore ; pCount
dc.w $02, do_ignore ; refnum
dc.w $04, do_flags
dc.w $06, do_ignore ; base
dc.w $08, do_ignore ; displacement
dc.w $0a, do_name_buffer_1 ;
dc.w $0e, do_entry_num
dc.w $10, do_file_type
dc.w $12, do_eof
dc.w $16, do_blocks
dc.w $1a, do_create_date_time
dc.w $22, do_mod_date_time
dc.w $2a, do_access
dc.w $2c, do_aux_type
dc.w $30, do_file_sys_id
dc.w $32, do_option_list
dc.w $36, do_r_eof
dc.w $3a, do_r_blocks
endp
init procname
with fst_parms,dp,data
; must be set up before hand.
;lda device
;sta dev_parms.dev_num
; directory entry (and inode lookup) should be cached.
lda #in_cache
sta dev_parms.dev_cache_pr
; should probably read half-blocks...
lda #1024
;sta dev_parms.dev_blk_size
sta dev_parms.dev_req_cnt
;stz dirent_offset
;stz dirent_entry
;stz dirent_zone
ldy #fcr.dirent_zone
lda [my_fcr],y
sta dirent_zone
ldy #fcr.dirent_offset
lda [my_fcr],y
sta dirent_offset
ldy #fcr.dirent_entry
lda [my_fcr],y
sta dirent_entry
stz data.dirent.inode
stz data.dirent.name
lda #v1_dirent.__sizeof
sta dirent_size
lda disk_inode.size
sta size
lda disk_inode.size+2
sta size+2
; / 2
lsr size+2
ror size
; / 4
lsr size+2
ror size
; / 8
lsr size+2
ror size
; / 16
lsr size+2
ror size
;~DebugHexDump <my_vcr, #vcr.__sizeof
ldy #vcr.super.magic
lda [my_vcr],y
cmp #v1L.MAGIC
bne minix
; this is
linux
; linux dirents are twice as big.
asl dirent_size
; / 32
lsr size+2
ror size
minix
rts
endp
base_displace procname
with fst_parms, dp
with data
; offset is $04 for class 0, $06 for class 1
lda #$04
clc
adc <call_class
tay
lda [param_blk_ptr],y ; base
cmp #3
bcs perr
asl a
tax
iny
iny
lda [param_blk_ptr],y ; displacement
sta displacement
jmp (base_table,x)
perr
lda #parm_range_err
sec
rts
base_table
dc.w absolute, forward, backward
absolute
; absolute with a displacement of 0 -> return total count.
;
cmp #0
beq @count
jmp find_absolute_dirent
@count
jmp count_entries
forward
jmp find_next_dirent
;
backward
; backward 0 ? == forward 0
cmp #0
beq forward
eor #$ffff
sec ; inc / clc
adc dirent_entry
sta displacement
jmp find_absolute_dirent
endp
count_entries procname
; count the number of entries.
with fst_parms
with dp, data
; read each block and count the entries.
; only handles direct blocks. so there.
; disk inode has been copied to the dp.
stz dirent_entry
ldx #0
zone_loop
stx dirent_zone
lda disk_inode.zone,x
bne @ok ; directory should not be sparse!
; sparse!
jsr sparse_dirent_block
bcs done
bra next
@ok
jsr read_data_block
bcs exit
; ~DebugHexDump <io_buffer, #1024
jsr count_dirent_block
lda size
beq done
next
ldx dirent_zone
inx
inx
cpx #v1.NR_DZONES*2
bcs done
bra zone_loop
;
; minix has 16-byte dirents.
; linux has 32-byte dirents.
done
; - 2 for . and ..
dec dirent_entry
dec dirent_entry
; also reset the displacement to 0.
lda #0
ldy #fcr.dirent_entry
sta [my_fcr],y
ldy #fcr.dirent_zone
sta [my_fcr],y
ldy #fcr.dirent_offset
sta [my_fcr],y
clc
exit
rts
endp
count_dirent_block procname
; 16-byte dirent entries -- 64 per block.
with dp, data
ldy #0
loop
lda [io_buffer],y
beq next
inc dirent_entry
next
; not 32-bit safe.
dec size
beq done
tya
clc
adc dirent_size
tay
cmp #1024
bcc loop
done
rts
endp
strlen procname
; strlen the dirent.
; will be 0-terminated unless if < dirent size.
lda data.dirent.inode
beq exit
ldx #2
short m
loop
lda data.dirent,x
beq zero
inx
cpx data.dirent_size
bcc loop
zero
long m
txa
dec a ; -2 for dirent.inode
dec a
exit
rts
endp
do_flags procname
with fst_parms
; $8000 for extended file, 0 for everything else.
lda #0
sta [param_blk_ptr],y
rts
endp
do_entry_num procname
with fst_parms
with data
lda dirent_entry
sta [param_blk_ptr],y
rts
endp
do_name_buffer_0 procname
with fst_parms
with dp
lda [param_blk_ptr],y
sta ptr
iny
iny
lda [param_blk_ptr],y
sta ptr+2
ora ptr
beq exit
copy_it
phx
jsr strlen
; strlen handles inode = 0
tax
tay
short m
sta [ptr]
; no need to dey since there is a length byte.
dex
bmi @done
@loop
lda data.dirent.name,x
sta [ptr],y
dey
dex
bpl @loop
@done
long m
plx
exit
rts
endp
do_name_buffer_1 procname
with fst_parms
with dp
lda [param_blk_ptr],y
sta ptr
iny
iny
lda [param_blk_ptr],y
sta ptr+2
ora ptr
beq exit
phx
jsr strlen
pha ; save
lda [ptr] ; total output buffer size
sec
sbc #4
bmi error
cmp 1,s
bcs ok
error_store_size
ldy #2
sta [ptr],y
error
pla
lda #buff_too_small
sta tool_error
plx
rts
ok
pla ; strlen
ldy #2
sta [ptr],y
tax
tay
iny ; skip gs/os string overhead
iny
iny
short m
dex
bmi @done
@loop
lda data.dirent.name,x
sta [ptr],y
dey
dex
bpl @loop
@done
long m
plx
exit
rts
endp
;
;
; new code....
; here's our big insight
; 1. base 1, displacement xxx is equivalent to stepping forward xxx entries.
; 2. base 2, displacement xxx is equivalent to base 0, displacement fcr.dirent_entry - displacement
; 2a. if displacement is 0, is equivalent to stepping forward 0 entries.
; 3. base 0, displacement xxx is equivalent to base 1 displacement xxx when fcr.dirent_entry = 0
;
; therefore the only special case is dirent_entry == 0 (since displacement 1 -> read entry 0)
;
;
; 9/4/2015 -- The first two entries (. and ..) should be skipped.
;
;
find_absolute_dirent procname
with data
; if displacement > dirent_entry, we can re-use that info.
lda displacement
cmp dirent_entry
bcc no
sec
sbc dirent_entry
sta displacement
bra find_next_dirent
no
stz dirent_entry
stz dirent_zone
stz dirent_offset
bra find_next_dirent
endp
find_next_dirent proc
with data, dp
lda dirent_entry
beq @first
dec dirent_entry
bra @ok
@first
dec displacement
bmi eod
; set dirent_offset = dirent_size * 2 to skip over . and ..
lda dirent_size
asl a ; x 2
sta dirent_offset
@ok
; make sure dir not empty (should never happen...)
lda size
beq eod
ldx dirent_zone
loop
lda disk_inode.zone,x
beq sparse ; should never happen...
phx ; save
jsr read_data_block
plx
bcs exit
; phx
; phy
; pha
; ~DebugHexDump <io_buffer, #1024
; pla
; ply
; plx
jsr find_dirent_block
bcc next
; a = 0 if it was found!
cmp #1
rts
next
inx
inx
stz dirent_offset
cpx #v1.NR_DZONES*2
bcs exit
bra loop
sparse
jsr sparse_dirent_block
bcs eod
bra next
found_it
;stx dirent_zone
;sty dirent_offset
clc
exit
rts
eod
lda #end_of_dir
sec
rts
endp
;
; returns:
; carry set on error or if found.
; a = 0 if found, or error.
;
; inputs y = dirent offset
find_dirent_block procname
with dp, data
ldy dirent_offset
loop
lda [io_buffer],y
beq next
inc dirent_entry
dec displacement
bmi found_it
next
dec size
beq eod
tya
clc
adc dirent_size
tay
cmp #1024
bcc loop
; not found ...
lda #0
clc
rts
eod
lda #end_of_dir
sec
rts
found_it
; we found our entry!!!
; copy it over.
stx dirent_zone
sty dirent_offset
; dirent_entry updated above.
ldx #0
@loop
lda [io_buffer],y
sta data.dirent,x
iny
iny
inx
inx
cpx dirent_size
bcc @loop
; update the fcr.
lda dirent_entry
ldy #fcr.dirent_entry
sta [my_fcr],y
lda dirent_offset
ldy #fcr.dirent_offset
sta [my_fcr],y
lda dirent_zone
ldy #fcr.dirent_zone
sta [my_fcr],y
lda #0
sec ; found!
rts
endp
strcmp procname
; clobbers ptr
; check if the name is even possible...
with dp, data
import target:GSString32
;~DebugSetTrace #1
iny ; skip inode
iny
tya
clc
adc io_buffer
sta ptr
lda #0
adc io_buffer+2
sta ptr+2
ldy #0
ldx target.length
short m
; y = offset
; x = length of target string.
loop
lda [ptr],y
beq eos
cmp target.text,y
bne no8
iny
dex
bpl loop
eos
long m
; if y == target.length, this is a match.
cpy target.length
bne no
yes
;~DebugSetTrace #0
clc
rts
no8
long m
no
;~DebugSetTrace #0
sec
rts
endp
sparse_dirent_block procname
; (not 32-bit safe)
; decrease size by the appropriate amount
; for a sparse block.
; (should probably never happen!)
; sets c if size <= 0
with data
lda dirent_size
cmp #32 ; linux? 32 dirents per block
beq @ok
lda #64 ; minix? 64 dirents per block
@ok
eor #$ffff
sec
adc size
sta size
bmi eod
beq eod
clc
rts
eod
sec
rts
endp
find_entry_by_name procname export
; scan the directory and search by name...
with dp
with data
import target:GSString32
jsr init
lda size
beq fnf
bmi fnf
; make sure the name is valid.
ldx target.length
beq fnf
; if dirent_size < target.length, it can't be found.
; should do this sooner.
lda data.dirent_size
dec a
dec a
cmp target.length
bcc fnf
ldx #0
zone_loop
; x = dirent_zone
stx dirent_zone
lda disk_inode.zone,x
beq sparse
jsr read_data_block
bcc @ok
rts
@ok
ldy #0
dirent_loop
; y = dirent offset
sty dirent_offset
lda [io_buffer],y
beq next_dirent
sta inode ; optomistic...
jsr strcmp
bcs next_dirent
; found it!
lda #0
clc
rts
next_dirent
dec size
beq fnf
lda dirent_offset
clc
adc dirent_size
tay
cmp #1024
bcs next_zone
bra dirent_loop
next_zone
ldx dirent_zone
inx
inx
cpx #v1.NR_DZONES*2
bcc zone_loop
; all out of zones -> not found!
fnf
stz inode
lda #file_not_found
sec
rts
sparse