forked from geraldholdsworth/DiscImageManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscImage_DOSPlus.pas
executable file
·2192 lines (2150 loc) · 72.9 KB
/
DiscImage_DOSPlus.pas
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
//++++++++++++++++++ Acorn DOS Plus ++++++++++++++++++++++++++++++++++++++++++++
{-------------------------------------------------------------------------------
Identifies a DOS Plus disc and which type
-------------------------------------------------------------------------------}
function TDiscImage.ID_DOSPlus: Boolean;
var
idbyte: Byte;
begin
Result:=False;
if FFormat=diInvalidImg then
begin
ResetVariables;
//This will only identify a plain DOS Plus disc, not an ADFS Hybrid
if Read16b($0001)=$FFFF then
begin
//Disc ID Byte
idbyte:=ReadByte($0000);
//Set the format based on this ID
if idbyte>$FB then
begin
FFormat:=diDOSPlus<<4;
//Sectors per track
secspertrack:=9;
if idbyte>$FD then secspertrack:=8;
//Sector size
secsize:=256;
doshead:=0;
//Set root address and root size
root:=$800;
root_size:=$C0*$20;
Fdosroot:=root;
dosroot_size:=root_size;
//Set the disc size
disc_size[0]:=GetDataLength;
//And cluster size
cluster_size:=$400;
dosalloc:=1;
DOSBlocks:=disc_size[0]div cluster_size;
//FAT Size and type
DOSFATSize:=1; //This is one less than the actual size
NumFATs:=1;
FATType:=diFAT12;
//Where is the FAT?
dosmap:=doshead;
dosmap2:=doshead;
//Attribute of the first entry of the root will indicate the volume name (bit 3)
if ReadByte(root+$B)<>$8 then FFormat:=diInvalidImg;
end;
end;
//Normal DOS disc (i.e. has a header)?
if FFormat=diInvalidImg then //Only check if nothing found
if IDDOSPartition($0000) then
begin
//ReadDOSHeader;
disc_size[0]:=DOSBlocks*cluster_size;
//Set the format
FFormat:=diDOSPlus<<4;
//And update it with the appropriate FAT
if FATType=diFAT12 then inc(FFormat,1);
if FATType=diFAT16 then inc(FFormat,2);
if FATType=diFAT32 then inc(FFormat,3);
end;
Result:=GetMajorFormatNumber=diDOSPlus;
end;
end;
{-------------------------------------------------------------------------------
ID a DOS Partition, with header
-------------------------------------------------------------------------------}
function TDiscImage.IDDOSPartition(ctr: Cardinal): Boolean;
var
ds,
rs : Word;
md : Byte;
begin
Result:=False;
//Is there E9 or EB stored here
if(ReadByte(ctr)=$E9)or(ReadByte(ctr)=$EB)then
begin
//Read in the block size
ds:=Read16b(ctr+$B);
//And the reserved sectors size
rs:=Read16b(ctr+$E);
//Media Descriptor Byte - should be repeated at the start of the FAT
md:=ReadByte(ctr+$15);
//Are they within range?
if ds*rs<GetDataLength then
//Is there a FAT partition at the location pointed to by above?
if(Read16b(ctr+(ds*rs)+1)=$FFFF)and(ReadByte(ctr+(ds*rs))=md)then
begin
//DOS Header location
doshead:=ctr;
Result:=ReadDOSHeader;
//Mark as DOS Partition present, if the checks return OK
FDOSPresent:=Result;
if not Result then doshead:=0; //Reset the header location on failure
end;
end;
end;
{-------------------------------------------------------------------------------
Reads a DOS Plus partition
-------------------------------------------------------------------------------}
procedure TDiscImage.ReadDOSPartition;
var
i,d,e : Integer;
lenctr : Cardinal;
part : Byte;
begin
//Is this an ADFS disc with DOS Plus partition?
if((GetMajorFormatNumber=diAcornADFS)and(FDOSPresent))
or(GetMajorFormatNumber=diDOSPlus)then //Or a straight DOS Plus?
begin
i:=0;
part:=0;
if GetMajorFormatNumber=diDOSPlus then dir_sep:='\';
//ADFS Hybrid?
if GetMajorFormatNumber=diAcornADFS then
begin
part:=1;
//Set up the second partition
SetLength(disc_size,2);
SetLength(free_space,2);
SetLength(disc_name,2);
if FFormat AND $F<>$F then //Not for hard disc partitions
begin
doshead :=disc_size[0];
//Set the DOS root parameters
dosmap:=doshead;
dosmap2:=doshead;
dosroot_size:=$70*$20;
//And cluster size
cluster_size:=$100;
dosalloc:=8;
//FAT Size and type
DOSFATSize:=1; //This is one less than the actual size
NumFATs:=1; //Number of FATs present
FATType:=diFAT12;//FAT type
disc_size[1]:=GetDataLength-disc_size[0]; //Partition size
DOSBlocks:=disc_size[1]div cluster_size; //Number of blocks on disc
Fdosroot:=doshead+((NumFATs*DOSFATSize)+1)*cluster_size; //Where the root is
end;
if FFormat AND $F=$F then //Hard disc partition - will have a DOS Header
begin
//doshead:=disc_size[0]; //Where the DOS header is
//ReadDOSHeader; //Read the DOS Header
disc_size[1]:=DOSBlocks*cluster_size;//Disc size in bytes
end;
i:=1;
end;
//Update the progress indicator
UpdateProgress('Reading DOS Plus partition');
//Read the volume title
disc_name[i]:='';
//Volume name in the header for DOS Version 29
if DOSVersion=$29 then
if FATType=diFAT32 then disc_name[i]:=ReadString(doshead+$47,-11)
else disc_name[i]:=ReadString(doshead+$2B,-11);
//Remove the spaces
RemoveSpaces(disc_name[i]);
//Add a new entry
d:=Length(FDisc);
SetLength(FDisc,d+1);
//Start the chain by reading the root
if FFormat=diAcornADFS<<4+$F then dosrootname:='C:' else dosrootname:='A:';
//Read the root
FDisc[d]:=ReadDOSDirectory(dosrootname,Fdosroot,lenctr);
//Now go through the root's entries and read them in
repeat
if Length(FDisc[d].Entries)>0 then
for e:=0 to Length(FDisc[d].Entries)-1 do
if FDisc[d].Entries[e].Filename<>'' then //Needs to have an actual name
//If it is a directory, read that in
if Pos('D',FDisc[d].Entries[e].Attributes)>0 then
begin
//Making room for it
SetLength(FDisc,Length(FDisc)+1);
//And now read it in
if FScanSubDirs then
FDisc[Length(FDisc)-1]:=ReadDOSDirectory(GetParent(d)
+GetDirSep(part)
+FDisc[d].Entries[e].Filename,
FDisc[d].Entries[e].Sector,
lenctr);
FDisc[Length(FDisc)-1].Parent:=d;
//Set the length of the directory
FDisc[d].Entries[e].Length:=lenctr;
//Reference to it
FDisc[d].Entries[e].DirRef:=Length(FDisc)-1;
end;
inc(d);
until d>=Length(FDisc);
ReadDOSFSM;
end;
end;
{-------------------------------------------------------------------------------
Reads a DOS Header. doshead must be set prior to calling this
-------------------------------------------------------------------------------}
function TDiscImage.ReadDOSHeader: Boolean;
var
RootFrags : TFragmentArray;
index : Integer;
RootDirSectors,
DataSec,
ClusterCount : Cardinal;
begin
Result:=False;
cluster_size:=Read16b(doshead+$B); //Block (cluster) size
if (cluster_size<>$0200)
and(cluster_size<>$0400)
and(cluster_size<>$0800)
and(cluster_size<>$1000)then exit; //Must be one of these values
dosalloc :=ReadByte(doshead+$D); //Allocation unit in blocks
if (dosalloc<> 1)
and(dosalloc<> 2)
and(dosalloc<> 4)
and(dosalloc<> 8)
and(dosalloc<> 16)
and(dosalloc<> 32)
and(dosalloc<> 64)
and(dosalloc<>128)then exit; //Must be one of these values
if dosalloc*cluster_size>32*1024 then exit; //Cannot be greater than 32K
DOSResSecs :=Read16b(doshead+$E); //Reserved sectors
if DOSResSecs=0 then exit; //Cannot be zero
NumFATs :=ReadByte(doshead+$10); //Number of FATs
if NumFATs=0 then exit; //Can't have zero FATs
dosroot_size:=Read16b(doshead+$11)*$20; //Size of the root
DOSBlocks :=Read16b(doshead+$13); //Total number of blocks
if DOSBlocks=0 then DOSBlocks:=Read32b(doshead+$20);//if >65535 blocks
if DOSBlocks=0 then exit; //Still zero? then fail
DOSFATSize :=Read16b(doshead+$16); //FAT size in blocks
if DOSFATSize=0 then DOSFATSize:=Read32b(doshead+$24);//FAT size in blocks
if DOSFATSize=0 then exit; //Still zero? then fail
//Determine the FAT type, as per Microsoft spec
RootDirSectors:=(dosroot_size+(cluster_size-1))div cluster_size;
DataSec:=DOSBlocks-(DOSResSecs+(NumFATs*DOSFATSize)+RootDirSectors);
ClusterCount:=Floor(DataSec/dosalloc);
if ClusterCount<4085 then FATType:=diFAT12
else if ClusterCount<65525 then FATType:=diFAT16
else FATType:=diFAT32;
//Sanity checks now we know the FAT
if(dosroot_size=0)and(FATType<>diFAT32)then exit; //Can't be zero, unless FAT32
if(dosroot_size<>0)and(FATType=diFAT32)then exit; //But, must be zero if FAT32
if FATType<>diFAT32 then
if dosroot_size mod cluster_size<>0 then exit;//Must be a multiple
if FATType=diFAT32 then
begin
if(Read16b(doshead+$13)<>0) //This must be zero for FAT32
or(Read32b(doshead+$20)=0)then exit; //This must be non-zero for FAT32
if(Read16b(doshead+$16)<>0) //This must be zero for FAT32
or(Read32b(doshead+$24)=0)then exit; //This must be non-zero for FAT32
end;
//Read in the specific parts, dependant on FATType
if(FATType=diFAT12)or(FATType=diFAT16)then
DOSVersion :=ReadByte(doshead+$26); //DOS Version - $28 or $29 (or 0)
if FATType=diFAT32 then
begin
doshead2 :=Read16b(doshead+$32)*cluster_size;//Location of the second DOS header
DOSVersion :=ReadByte(doshead+$42); //DOS Version - $28 or $29 (or 0)
end;
//For version less than $29, the volume name will be in the root
if DOSVersion<$29 then FDOSVolInRoot:=True; //But $29 can be both root and header
//Where the FAT(s) is(are)
dosmap:=doshead+(DOSResSecs*cluster_size);
if NumFATs>1 then dosmap2:=dosmap+DOSFATSize*cluster_size else dosmap2:=dosmap;
//Where the root is
if(FATType=diFAT12)or(FATType=diFAT16)then
Fdosroot:=doshead+((NumFATs*DOSFATSize)+DOSResSecs)*cluster_size;
if FATType=diFAT32 then
begin
//Root is pointed to by location $2C (cluster number)
Fdosroot:=Read32b(doshead+$2C);
//Root size should then be worked out from the number of clusters used.
RootFrags:=GetClusterChain(Fdosroot);
dosroot_size:=00;
if Length(RootFrags)>0 then
for index:=0 to Length(RootFrags)-1 do
inc(dosroot_size,RootFrags[index].Length);
end;
//Got this far? Then it must be a success
Result:=True;
end;
{-------------------------------------------------------------------------------
Reads a DOS Plus Directory
-------------------------------------------------------------------------------}
function TDiscImage.ReadDOSDirectory(dirname: String;addr: Cardinal;
var len: Cardinal): TDir;
var
index,
entry,
side : Integer;
attr,
status,
LFNInd,
LFNEnd : Byte;
C : Char;
ext,
volname: String;
buffer : TDIByteArray;
created: Boolean;
begin
Result.Directory:='';
//Setup the container
ResetDir(Result);
//Don't need the parent or self referrals
if(RightStr(dirname,1)='.')or(RightStr(dirname,2)='..')then exit;
//ADFS hybrid?
if GetMajorFormatNumber=diAcornADFS then side:=1 else side:=0;
//Directory name
index:=Pos(GetDirSep(side),dirname);
while Pos(GetDirSep(side),dirname,index+1)>index do
index:=Pos(GetDirSep(side),dirname,index+1);
if index>0 then
Result.Directory:=Copy(dirname,index+1)
else
Result.Directory:=dirname;
Result.Sector:=addr;
//And set the partition flag to true
Result.DOSPartition:=True;
Result.Partition:=side;
//Update the progress indicator
UpdateProgress('Reading '+dirname);
//First, read the data into a buffer
if(dirname=dosrootname)and((FATType=diFAT12)or(FATType=diFAT16))then
begin //This is the root, so the address will be a physical address
SetLength(buffer,dosroot_size);
ReadDiscData(addr,dosroot_size,side,0,buffer);
end
else //Otherwise it is the starting cluster
buffer:=ReadDOSObject(addr);
//Set the directory length
len:=Length(buffer);
//Start at the beginning
index:=0;
entry:=0;
created:=False;
//Get the status byte (first byte of the filename)
status:=ReadByte(index*32,buffer);
while status<>0 do
begin
//Read the attribute byte
attr:=ReadByte($B+index*32,buffer);
//Is it the volume name?
if(status<>$E5)and(attr AND$8=$8)then
begin
volname:=ReadString(Fdosroot,-11,buffer);
FDOSVolInRoot:=True;//Volume name is in the root flag
//Has it already been set?
if disc_name[Length(disc_name)-1]='' then
disc_name[Length(disc_name)-1]:=volname;//If no, then set it
end;
//Long filename will have attr as $F.
if(status<>$E5)and(status<>$00)and(attr=$F)then
begin
//Long entries always precede the short entry, and in reverse order
if not created then SetLength(Result.Entries,Length(Result.Entries)+1);
entry:=Length(Result.Entries)-1;
if not created then ResetDirEntry(Result.Entries[entry]);
//So we need a flag to indicate whether we have created the entry for it
created:=True;
//Extract each part of the filename
LFNInd:=1;
LFNEnd:=10;
volname:='';
//Part 1 is characters 1, 3, 5, 7, 9
//Part 2 is characters 14,16,18,20,22,24
//Part 3 is characters 28,30
while LFNInd<31 do
begin
while LFNInd<LFNEnd do
begin
C:=' ';
if Read16b(LFNInd+index*32,buffer)<>$FFFF then // $FFFF is end of filename
C:=Chr(Read16b(LFNInd+index*32,buffer)); //Read the character in
volname:=volname+C; //And add it to this part
inc(LFNInd,2);
end;
//Next part
if LFNEnd=25 then begin LFNEnd:=31;LFNInd:=28;end;
if LFNEnd=10 then begin LFNEnd:=25;LFNInd:=14;end;
end;
//Add to the entry
Result.Entries[entry].Filename:=volname+Result.Entries[entry].Filename;
end;
//Continue if valid filename, and not volume name
if(status<>$E5)and(status<>$00)and(attr AND$8=0)then
begin
//Add a new entry
if not created then SetLength(Result.Entries,Length(Result.Entries)+1);
entry:=Length(Result.Entries)-1;
//Reset it to defaults
if not created then ResetDirEntry(Result.Entries[entry]);
//Read in the details
volname:=ReadString($00+index*32,-8,buffer); //Filename
//Replace 0x05 with 0xE5
if volname<>'' then
if Ord(volname[1])=$05 then
volname[1]:=Chr($E5);
//Remove the trailing spaces
RemoveSpaces(volname);
//Read in the extension
ext:=ReadString($08+index*32,-3,buffer); //Extension
RemoveSpaces(ext);
//And assign to the record
Result.Entries[entry].ShortFilename:=BuildDOSFilename(volname,ext);
//If we don't already have a long filename, then use the short one
if Result.Entries[entry].Filename='' then
Result.Entries[entry].Filename:=Result.Entries[entry].ShortFilename;
//Read the rest of the attributes
Result.Entries[entry].Timestamp:=ConvertDOSTimeDate(Read16b($16+index*32,buffer),
Read16b($18+index*32,buffer));//Time/date
Result.Entries[entry].Attributes:=ConvertDOSAttributes(attr);//Attributes
Result.Entries[entry].Sector :=Read16b($1A+index*32,buffer); //Starting cluster
Result.Entries[entry].Length :=Read32b($1C+index*32,buffer); //Filelength
Result.Entries[entry].DirRef :=-1; //Directory Reference
Result.Entries[entry].Parent :=dirname; //Parent
Result.Entries[entry].ShortFileType:=ext;
Result.Entries[entry].FileType:=DOSExtToFileType(ext);
Result.Entries[entry].Side :=side;
//If the filename is '.' or '..' then remove it
if(Result.Entries[entry].Filename='.')
or(Result.Entries[entry].Filename='..')then
SetLength(Result.Entries,Length(Result.Entries)-1);
//Reset the flag as we have finished with this entry
created:=False;
end;
//Move onto the next entry
inc(index);
status:=ReadByte(index*32,buffer);
end;
Result.BeenRead:=True;
end;
{-------------------------------------------------------------------------------
Convert a file extension to a filetype string
-------------------------------------------------------------------------------}
function TDiscImage.DOSExtToFileType(ext: String): String;
var
index : Integer;
begin
index:=Low(DOSFileTypes);
while(ext<>UpperCase(LeftStr(DOSFileTypes[index],3)))
and(index<=High(DOSFileTypes))do inc(index);
if ext=UpperCase(LeftStr(DOSFileTypes[index],3)) then
Result:=Copy(DOSFileTypes[index],4)
else
Result:='';
end;
{-------------------------------------------------------------------------------
Convert time and date words to a time date value
-------------------------------------------------------------------------------}
function TDiscImage.ConvertDOSTimeDate(time,date: Word): TDateTime;
var
year : Word;
month,
day,
hour,
minute,
second : Byte;
begin
//Decode into the separate components
year :=1980+(date>>9);
month :=(date AND$1E0)>>5;
day :=date AND$1F;
hour :=(time>>$B)mod 24;
minute:=((time AND$7E0)>>5)mod 60;
second:=((time AND$1F)*2)mod 60;
//Range check
if(month=0)or(month>12)then month :=1;
if(day=0)or(day>31) then day :=1;
//Encode
Result:=EncodeDateTime(year,month,day,hour,minute,second,0);
end;
{-------------------------------------------------------------------------------
Convert time and date stamp to a time DOS word value
-------------------------------------------------------------------------------}
function TDiscImage.DOSTime(time: TDateTime): Word;
var
year,
month,
day,
hour,
minute,
second,
ms : Word;
begin
DecodeDateTime(time,year,month,day,hour,minute,second,ms);
Result:=(hour<<$B)OR(minute<<5)OR(second div 2);
end;
{-------------------------------------------------------------------------------
Convert time and date stamp to a date DOS word value
-------------------------------------------------------------------------------}
function TDiscImage.DOSDate(date: TDateTime): Word;
var
year,
month,
day,
hour,
minute,
second,
ms : Word;
begin
DecodeDateTime(date,year,month,day,hour,minute,second,ms);
Result:=((year-1980)<<9)OR(month<<5)OR day;
end;
{-------------------------------------------------------------------------------
Convert attribute byte to a string
-------------------------------------------------------------------------------}
function TDiscImage.ConvertDOSAttributes(attr: Byte): String;
begin
Result:='';
if attr AND $01=$01 then Result:=Result+'R'; //Read only
if attr AND $02=$02 then Result:=Result+'H'; //Hidden
if attr AND $04=$04 then Result:=Result+'S'; //System
if attr AND $08=$08 then Result:=Result+'V'; //Volume label
if attr AND $10=$10 then Result:=Result+'D'; //Directory
if attr AND $20=$20 then Result:=Result+'A'; //Archive
end;
{-------------------------------------------------------------------------------
Convert attribute string to a byte
-------------------------------------------------------------------------------}
function TDiscImage.ConvertDOSAttributes(attr: String): Byte;
begin
Result:=0;
attr:=UpperCase(attr);
if Pos('R',attr)>0 then Result:=Result OR $01; //Read only
if Pos('H',attr)>0 then Result:=Result OR $02; //Hidden
if Pos('S',attr)>0 then Result:=Result OR $04; //System
if Pos('V',attr)>0 then Result:=Result OR $08; //Volume label
if Pos('D',attr)>0 then Result:=Result OR $10; //Directory
if Pos('A',attr)>0 then Result:=Result OR $20; //Archive
end;
{-------------------------------------------------------------------------------
Convert a cluster number to a disc offset
-------------------------------------------------------------------------------}
function TDiscImage.DOSClusterToOffset(cluster: Cardinal): Cardinal;
begin
if(FATType=diFAT12)or(FATType=diFAT16)then
Result:=((cluster-2)*cluster_size*dosalloc)+Fdosroot+dosroot_size;
if FATType=diFAT32 then
Result:=((cluster-2)*cluster_size*dosalloc)+dosmap2+DOSFATSize*dosalloc*cluster_size;
end;
{-------------------------------------------------------------------------------
Get the entry from the FAT for a given cluster number
-------------------------------------------------------------------------------}
function TDiscImage.GetClusterEntry(cluster: Cardinal): Cardinal;
var
FAToffset: Cardinal;
begin
Result:=$FFFFFFF7; //Bad cluster, by default
if FATType=diFAT12 then
begin
//Get the offset into the FAT
FAToffset:=(cluster*3)div 2;
//Get the next cluster number
if (cluster*3)mod 2=0 then
Result:=Read16b(FAToffset+dosmap)AND$FFF
else
Result:=Read16b(FAToffset+dosmap)>>4;
end;
if FATType=diFAT16 then
begin
//Get the offset into the FAT
FAToffset:=cluster*2;
//Get the next cluster number
Result:=Read16b(FAToffset+dosmap);
end;
if FATType=diFAT32 then
begin
//Get the offset into the FAT
FAToffset:=cluster*4;
//Get the next cluster number
Result:=Read32b(FAToffset+dosmap);
end;
end;
{-------------------------------------------------------------------------------
Set the entry from the FAT for a given cluster number
-------------------------------------------------------------------------------}
procedure TDiscImage.SetClusterEntry(cluster,entry: Cardinal);
var
FAToffset,
FATentry : Cardinal;
begin
if FATType=diFAT12 then
begin
//Make sure the entry is 12 bits
entry:=entry AND $FFF;
//Get the offset into the FAT
FAToffset:=(cluster*3)div 2;
//Get the current entry
FATentry:=Read16b(FAToffset+dosmap);
//Adjust to add our entry
if (cluster*3)mod 2=0 then
FATentry:=(FATentry AND$F000)OR entry
else
FATentry:=(FATentry AND$000F)OR(entry<<4);
//Write it back
Write16b(FATentry,FAToffset+dosmap);
if dosmap<>dosmap2 then Write16b(FATentry,FAToffset+dosmap2);
end;
if FATType=diFAT16 then
begin
//Make sure the entry is 16 bits
entry:=entry AND $FFFF;
//Get the offset into the FAT
FAToffset:=cluster*2;
//Write the entry
Write16b(entry,FAToffset+dosmap);
if dosmap<>dosmap2 then Write16b(FATentry,FAToffset+dosmap2);
end;
if FATType=diFAT32 then
begin
//Make sure the entry is 32 bits
entry:=entry AND $FFFFFFFF;
//Get the offset into the FAT
FAToffset:=cluster*4;
//Get the next cluster number
Write32b(entry,FAToffset+dosmap);
if numFATs>1 then Write32b(FATentry,FAToffset+dosmap2);
end;
end;
{-------------------------------------------------------------------------------
Determines if the given cluster number is valid
-------------------------------------------------------------------------------}
function TDiscImage.IsClusterValid(cluster: Cardinal): Boolean;
begin
Result:=cluster>1;
if FATType=diFAT12 then Result:=(Result)AND(cluster< $FF0);
if FATType=diFAT16 then Result:=(Result)AND(cluster< $FFF0);
if FATType=diFAT32 then Result:=(Result)AND(cluster<$FFFFFFF0);
end;
{-------------------------------------------------------------------------------
Get the cluster chain as an array of fragments
-------------------------------------------------------------------------------}
function TDiscImage.GetClusterChain(cluster:Cardinal;len:Cardinal=0):TFragmentArray;
var
nolengiven: Boolean;
lenctr : Cardinal;
begin
Result:=nil;
SetLength(Result,0);
if len=0 then nolengiven:=True else nolengiven:=False;
//Go through the FAT, start at the starting cluster, and build the fragment array
if IsClusterValid(cluster)then //Add the first sector, if valid
begin
SetLength(Result,1);
Result[0].Offset:=cluster;
if(cluster_size*dosalloc<len)or(nolengiven)then
Result[0].Length:=cluster_size*dosalloc
else
Result[0].Length:=len;
end;
//If no length given, set the length counter to a dummy value, other than zero
if nolengiven then lenctr:=1
else lenctr:=len-Result[0].Length; //Otherwise set it to the total length
while(IsClusterValid(cluster))and(lenctr>0)do //Continue while valid
begin
//Get the next entry
cluster:=GetClusterEntry(cluster);
//If valid, add a fragment
if IsClusterValid(cluster) then
begin
SetLength(Result,Length(Result)+1);
Result[Length(Result)-1].Offset:=cluster;
if(cluster_size*dosalloc<lenctr)or(nolengiven)then
Result[Length(Result)-1].Length:=cluster_size*dosalloc
else
Result[Length(Result)-1].Length:=lenctr;
if not nolengiven then dec(lenctr,Result[Length(Result)-1].Length);
end;
end;
end;
{-------------------------------------------------------------------------------
Set the cluster chain given in the array of fragments
-------------------------------------------------------------------------------}
procedure TDiscImage.SetClusterChain(fragments: TFragmentArray);
var
index: Integer;
begin
if Length(fragments)>0 then
begin
//Update all but the last entry
if Length(fragments)>1 then
for index:=0 to Length(fragments)-2 do
begin
SetClusterEntry(fragments[index].Offset,fragments[index+1].Offset);
end;
//Now we can put the end of cluster chain marker in the last position
SetClusterEntry(fragments[Length(fragments)-1].Offset,$FFFFFFFF);
end;
end;
{-------------------------------------------------------------------------------
Extracts a file, filename contains complete path
-------------------------------------------------------------------------------}
function TDiscImage.ExtractDOSFile(filename: String;
var buffer: TDIByteArray): Boolean;
var
dir,
entry : Cardinal;
begin
Result:=False;
//if filename[Length(dosrootname)+1]='.' then filename[Length(dosrootname)+1]:='\';
if FileExists(filename,dir,entry) then //Does the file actually exist?
begin
//Reset the buffer
SetLength(buffer,0);
//Read the file data
buffer:=ReadDOSObject(FDisc[dir].Entries[entry].Sector,
FDisc[dir].Entries[entry].Length);
//Return a result, +ve or -ve
Result:=Length(buffer)>0;
end;
end;
{-------------------------------------------------------------------------------
Extracts an object, given starting cluster
-------------------------------------------------------------------------------}
function TDiscImage.ReadDOSObject(cluster:Cardinal;len:Cardinal=0):TDIByteArray;
var
frag,
index,
ptr : Cardinal;
fragments : TFragmentArray;
begin
Result:=nil;
//Reset the buffer
SetLength(Result,0);
//Read the chain of clusters into a fragment array
fragments:=GetClusterChain(cluster,len);
//Any fragments?
if Length(fragments)>0 then
begin
//Go through each fragment
for frag:=0 to Length(fragments)-1 do
begin
//Make a note of the current position in the buffer
ptr:=Length(Result);
//Then extend it
SetLength(Result,Length(Result)+fragments[frag].Length);
//Copy the data for this cluster across
if fragments[frag].Length>0 then //Ensure that there is data there
for index:=0 to fragments[frag].Length-1 do
Result[ptr+index]:=ReadByte(DOSClusterToOffset(fragments[frag].Offset)+index);
end;
end;
end;
{-------------------------------------------------------------------------------
Reads the free space map for a DOS Plus image
-------------------------------------------------------------------------------}
procedure TDiscImage.ReadDOSFSM;
var
fragments: TFragmentArray;
part : Byte;
tracks,
entry,
index,
t,s : Cardinal;
const spt = 9;
begin
//Get all the used sectors
fragments:=DOSGetFreeSectors(True);
//Initialise the variables
if GetMajorFormatNumber=diDOSPlus then
begin
SetLength(free_space_map,1);
part:=0;
end;
if GetMajorFormatNumber=diAcornADFS then //Hybrids - DOS will take up the second 'side'
begin
SetLength(free_space_map,2);
part:=1;
end;
//Initialise the free space
free_space[part]:=disc_size[part];
//Set up the array
tracks:=Ceil((disc_size[part]div cluster_size)/spt);
SetLength(free_space_map[part],tracks);
for entry:=0 to Length(free_space_map[part])-1 do //Sectors per track
begin
SetLength(free_space_map[part,entry],spt);
for index:=0 to spt-1 do free_space_map[part,entry,index]:=$00;
end;
//If there are any used spaces, add them to the FSM
if Length(fragments)>0 then
for index:=0 to Length(fragments)-1 do
begin
for entry:=0 to (fragments[index].Length div cluster_size)-1 do
begin
//Get the track and sector
t:=(((fragments[index].Offset-doshead)div cluster_size)+entry)div spt;
s:=(((fragments[index].Offset-doshead)div cluster_size)+entry)mod spt;
if t<Length(free_space_map[part]) then //Make sure it is within range
if s<Length(free_space_map[part,t]) then
begin
free_space_map[part,t,s]:=$FF-fragments[index].Zone;
dec(free_space[part],cluster_size); //Decrease the free space
end;
end;
end;
end;
{-------------------------------------------------------------------------------
Get an array of all the free sectors (or used, if set to True)
-------------------------------------------------------------------------------}
function TDiscImage.DOSGetFreeSectors(used: Boolean=False): TFragmentArray;
var
LFATSize,
cluster,
entry,
index : Cardinal;
begin
Result:=nil;
//Actual FAT Size
LFATSize:=DOSBlocks div dosalloc;
if FATType=diFAT12 then LFATSize:=((LFATSize*3)+1)div 2;//12 bits per cluster
if FATType=diFAT16 then LFATSize:=(LFATSize+1)*2; //16 bits per cluster
//Add the system areas, if used is requested
if used then
begin
SetLength(Result,Length(Result)+1);
Result[Length(Result)-1].Offset:=doshead;
Result[Length(Result)-1].Length:=(Fdosroot+dosroot_size)-doshead;
Result[Length(Result)-1].Zone :=$01; //Marker for system area
end;
//Add the rest of the FAT
for cluster:=2 to DOSBlocks div dosalloc do
begin
entry:=GetClusterEntry(cluster); //Get the entry for this cluster number
//Add a used fragment
if(used)and(entry<>0)then
begin
SetLength(Result,Length(Result)+1);
Result[Length(Result)-1].Offset:=DOSClusterToOffset(cluster);
Result[Length(Result)-1].Length:=cluster_size*dosalloc;
Result[Length(Result)-1].Zone :=$00;
end;
//Add a free fragment
if(not used)and(entry=0)then
begin
SetLength(Result,Length(Result)+1);
Result[Length(Result)-1].Offset:=DOSClusterToOffset(cluster);
Result[Length(Result)-1].Length:=cluster_size*dosalloc;
Result[Length(Result)-1].Zone :=$00;
end;
end;
//Now compact the array by joining adjacent sectors
if Length(Result)>1 then
begin
index:=Length(Result);
while index>0 do
begin
dec(index);
//Is the one below it adjacent?
if Length(Result)>2 then
if(Result[index].Offset=Result[index-1].Offset+Result[index-1].Length)
and(Result[index].Zone=Result[index-1].Zone)then
begin
//Then join them
inc(Result[index-1].Length,Result[index].Length);
//Move the ones above down
if index<Length(Result)-1 then
for entry:=index to Length(Result)-2 do
Result[entry]:=Result[entry+1];
//And let the last one drop off
SetLength(Result,Length(Result)-1);
end;
end;
end;
end;
{-------------------------------------------------------------------------------
Rename a DOS file
-------------------------------------------------------------------------------}
function TDiscImage.RenameDOSFile(oldname:String;var newname: String): Integer;
var
dir,
entry,
ptr : Cardinal;
ext : String;
side : Byte;
begin
//ADFS hybrid?
if GetMajorFormatNumber=diAcornADFS then side:=1 else side:=0;
Result:=-2; //Original file does not exist
//Validate the filename
newname:=ValidateDOSFilename(newname);
if FileExists(oldname,dir,entry) then
begin
Result:=-3; //New filename already exists
//Check that the proposed new name does not exist
if(not FileExists(GetParent(dir)+GetDirSep(side)+newname,ptr))
// or the user is just changing case
or(LowerCase(GetParent(dir)+GetDirSep(side)+newname)=LowerCase(oldname))then
begin
Result:=-1; //Unknown error
//Change the name
FDisc[dir].Entries[entry].Filename:=newname;
//And extension
if Pos('.',newname)>0 then
begin
ext:=newname;
while Pos('.',ext)>0 do ext:=Copy(ext,Pos('.',ext)+1);
end
else ext:='';
FDisc[dir].Entries[entry].ShortFiletype:=ext;
FDisc[dir].Entries[entry].FileType:=DOSExtToFileType(ext);
//Update the directory
UpdateDOSDirectory(GetParent(dir));
//Return the entry
Result:=entry;
end;
end;
end;
{-------------------------------------------------------------------------------
Validate a DOS filename
-------------------------------------------------------------------------------}
function TDiscImage.ValidateDOSFilename(filename: String;long: Boolean=False): String;
var
fn,
fn2,
ext,
ext2 : String;
index : Integer;
const
illegal = ' "*+,./:;<=>?[\]|';//As per MS FAT32 Specification
longillegal = ' "*./:<>?\|'; //As per MS FAT32 Specification
begin
if FFormat=diDOSPlus then long:=False;//Override this for Master 512
if FDOSUseSFN then long:=False;//Use Short FileNames is set
fn:=filename;
Result:='';
//Extract the extension
if Pos('.',fn)>0 then
begin
ext:=fn;
while Pos('.',ext)>0 do ext:=Copy(ext,Pos('.',ext)+1);
end
else ext:='';
//Remove the extension from the filename
if ext<>'' then fn:=LeftStr(fn,Length(fn)-(Length(ext)+1));
//Validate the length (or, rather, truncate them)
if long then //Long filename
begin
fn:=LeftStr(fn,255);
ext:=LeftStr(ext,255);
if Length(BuildDOSFilename(fn,ext))>255 then fn:=LeftStr(fn,254-Length(ext));
end
else
begin
fn:=LeftStr(fn,8);
ext:=LeftStr(ext,3);
end;
//Remove any trailing spaces
RemoveSpaces(fn);
RemoveSpaces(ext);
//Now we can validate both
fn2:='';
if Length(fn)>0 then
for index:=1 to Length(fn) do
begin
if(Pos(fn[index],illegal)=0)and(not long)then fn2:=fn2+fn[index];
if(Pos(fn[index],longillegal)=0)and(long)then fn2:=fn2+fn[index];
end;
//Convert to uppercase
if not long then fn2:=UpperCase(fn2);
//And the extension
ext2:='';
if Length(ext)>0 then
for index:=1 to Length(ext) do
begin
if(Pos(ext[index],illegal)=0)and(not long)then ext2:=ext2+ext[index];
if(Pos(ext[index],longillegal)=0)and(long)then ext2:=ext2+ext[index];
end;
if not long then ext2:=UpperCase(ext2);
//Return a validated filename
Result:=BuildDOSFilename(fn2,ext2);
end;
{-------------------------------------------------------------------------------
Update the contents of a directory
-------------------------------------------------------------------------------}
procedure TDiscImage.UpdateDOSDirectory(dirname: String);
var