-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3e.c
executable file
·2042 lines (1789 loc) · 53.3 KB
/
3e.c
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
#define PROGRAM_VERSION "0.5alpha"
/*
'3e': Utility for handing files in a memory card formatted for a +3E
enhanced Spectrum.
(C)2009-2012 Miguel Angel Rodriguez Jodar (mcleod_ideafix). GPL Licensed.
For support, please check http://www.zxprojects.com
CHANGELOG and USAGE
See the readme.txt file for details.
COMPILING
See the install.txt file for details.
*/
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#define DIR_SEP '\\'
#else
#define DIR_SEP '/'
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef WIN32
#include <libgen.h>
#include <sys/io.h>
#else
#include <io.h>
#endif
#include <fcntl.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef WIN32
#define stricmp strcasecmp
#endif
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
#ifdef WIN32
typedef unsigned __int64 u64;
#else
typedef unsigned long long u64;
#endif
/*
XDPB structure:
Bytes 0...1 SPT records per track
Byte 2 BSH log2(block size / 128)
Byte 3 BLM block size / 128 - 1
Byte 4 EXM extent mask
Bytes 5...6 DSM last block number
Bytes 7...8 DRM last directory entry number
Byte 9 AL0 directory bit map
Byte 10 AL1 directory bit map
Bytes 11...12 CKS size of checksum vector (bit 15 = permanent)
Bytes 13...14 OFF number of reserved tracks
Byte 15 PSH log2(sector size / 128)
Byte 16 PHM sector size / 128 - 1
Byte 17 Bits 0...1 Sidedness
0 = Single sided
1 = Double sided (alternating sides)
2 = Double sided (successive sides)
Bits 2...6 Reserved (set to 0)
Bit 7 Double track
Byte 18 Number of tracks per side
Byte 19 Number of sectors per track
Byte 20 First sector number
Bytes 21...22 Sector size
Byte 23 Gap length (/write)
Byte 24 Gap length (format)
Byte 25 Bit 7 Multi-track operation
1 = multi-track
0 = single track
Bit 6 Modulation mode
1 = MFM mode
0 = FM mode
Bit 5 Skip deleted data address mark
1 = skip deleted data address mark
0 = don't skip deleted address mark
Bits 0...4 = 0
Byte 26 Freeze flag
00h (0) = auto-detect disk format
FFh (255) = don't auto-detect disk format
*/
typedef struct stXDPB
{
u16 spt;
u8 bsh;
u8 blm;
u8 exm;
u16 dsm;
u16 drm;
u8 al0;
u8 al1;
u16 cks;
u16 off;
u8 psh;
u8 phm;
u8 sideness;
u8 tracks_per_side;
u8 sectors_per_track;
u8 first_sector;
u16 sector_size;
u8 gap_length_rw;
u8 gap_length_fmt;
u8 mlt_track;
u8 freeze_flag;
} tXDPB;
typedef struct stDiskPar
{
u16 cyl;
u8 head;
u8 sect;
u16 sect_per_cyl;
u16 last_pentry;
} tDiskPar;
/*
The 64 bytes partition entry
Offset Length Description
+0 16 Partition name (case-insensitive, space-padded).
+16 16 Partition definition.
+16 1 Partition type (0=free handle)
+17 2 Starting cylinder.
+19 1 Starting head.
+20 2 Ending cylinder.
+22 1 Ending head.
+23 4 Largest logical sector number.
+27 5 Type-specific information.
+32 32 Type-specific information.
Partition types
number Description
#00 Unused partition/free handle.
#01 System partition. The first partition on a disk, starting at phisical sector 1 (cylinder 0, head 0 or 1), is always the system partition and contains a list of 64-byte partition entries that define all the partitions on the disk (including the system one). Only one partition of this type is permitted on a disk, and this is always the first partition. The name is always "PLUSIDEDOS" (followed by 6 spaces).
#02 Swap partition.
#03 +3DOS partition. The maximum theoretical size for a +3DOS partition is just under 32Mb. The XDPB has logical geometry.
#04 CP/M partition with XDPB that reflects phisical disk structure. So if the disk has 17 spt, the LSPT is 68. The partition uses always integer number of cylinders and uses whole cylinder (from head 0). Otherwise (when from not track 0) this is converted to reserved tracks (OFF in XDPB). This is required for my DSKHNDLR low level disk drivers.
#05 Boot partition. This is only one file, stored as a partition. Used to boot a hardware. Eg. Timex FDD 3000 extedend with YABUS.TF, will search the IDEDOS partiton table to find "YABUS.TF" partition. If found, the partition contents is loaded into RAM and started. The partition size is usually 8k to 64kB, what gives 1..2 tracks (or 1..8 track for disks with 17 spt). The number of sectors to load is in partition definition.
#10 MS-DOS (FAT16) partition.
#20 UZI(X) partition.
#30 TR-DOS diskimage partition. Usually 640kB. Sector offset.
#31 SAMDOS diskimage partition (B-DOS record), 800kB. Sector offset.
#32 MB-02 diskimage partition. Usually 1804kB. Sector offset.
#FE Bad disk space.
#FF Free disk space.
*/
typedef struct stParEntry
{
u8 name[17];
u8 type;
u8 drive_letter;
u16 cyl_start;
u8 head_start;
u16 cyl_end;
u8 head_end;
u32 last_sector;
u32 FirstLBA;
u32 LastLBA;
tXDPB xdpb; /* only for +3DOS partitions */
tDiskPar DiskPar; /* only for system partition */
} tParEntry;
/*
Directory entries - The directory is a sequence of directory entries (also called extents), which contain
32 bytes of the following structure:
St F0 F1 F2 F3 F4 F5 F6 F7 E0 E1 E2 Xl Bc Xh Rc
Al Al Al Al Al Al Al Al Al Al Al Al Al Al Al Al
*/
typedef struct stDirEntry
{
char fname[12];
u8 type;
u32 nbytes;
u8 read_only;
u8 system_file;
u8 archived;
u16 extents[256];
u16 extused;
u16 blocks[2048];
u16 blused;
} tDirEntry;
/*
Bytes 0...7 - +3DOS signature - 'PLUS3DOS'
Byte 8 - 1Ah (26) Soft-EOF (end of file)
Byte 9 - Issue number
Byte 10 - Version number
Bytes 11...14 - Length of the file in bytes, 32 bit number,
least significant byte in lowest address
Bytes 15...22 - +3 BASIC header data
Bytes 23...126 - Reserved (set to 0)
Byte 127 - Checksum (sum of bytes 0...126 modulo 256)
*/
typedef struct stP3Header
{
u8 issue;
u8 version;
u32 nbytes;
u8 type;
u16 length;
u16 start;
u16 vars;
} tP3Header;
int HalvedHDF=0; /* flag for halved sector HDF's */
int HalvedDisks=0; /* flag for halved sector physical disks or raw images */
int SectorSize=512; /* normally 512, unless halved disks are in use */
u8 sep=','; /* separator for formatted output */
u8 ptype=0x7f; /* partition type to store an IDEPLUSDOS hard disk inside an IBM partition */
u32 OffsHeader; /* offset to the real start of disk image. */
u32 StartIDEDOS; /* offset to the beginning of the IDEDOS partition table */
tParEntry *ParTable=NULL;
int num_parts=0;
u8 *RawDirectory=NULL; /* the actual directory (raw data) */
u8 *BlockMap=NULL; /* List of blocks currently used. */
int blocks_filesystem;
tDirEntry *Directory=NULL;
int num_dentries=0;
#ifdef WIN32
char *basename (char *s)
{
char *pos;
pos=strrchr(s,'.'); /* strip extension if any */
if (pos)
pos[0]='\0';
pos=strrchr(s, DIR_SEP);
if (!pos)
return s;
else
return pos+1;
}
#endif
/* Removes spaces at the beginning and at the end of a string. BEWARE: modifies the source string. */
char *strtrim (char *s)
{
int i;
for (i=strlen(s)-1;i>=0;i--)
if (s[i]!=' ')
break;
s[i+1]='\0';
for (i=0;s[i];i++)
if (s[i]!=' ')
break;
return (&s[i]);
}
u32 next512mult (u32 n)
{
if (n%512==0)
return n;
else
return ((n+512)/512)*512;
}
int FileExists (u8 *fname)
{
int i;
for (i=0;i<num_dentries;i++)
if (strncmp(fname, Directory[i].fname, 11)==0)
return i;
return -1;
}
int FindPartition (char *part)
{
int i;
for (i=0;i<num_parts;i++)
if (!strncmp (part, ParTable[i].name, strlen(part)))
return i;
return -1;
}
/* Read/Writes LE 16 bit and 32 bit values */
u16 read16 (u8 *p)
{
u16 res;
res=p[0] | (p[1]<<8);
return res;
}
u32 read32 (u8 *p)
{
u32 res;
res=p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
return res;
}
void write16 (u8 *p, u16 v)
{
p[0]=v&0xFF;
p[1]=(v>>8)&0xFF;
}
void write32 (u8 *p, u32 v)
{
p[0] = v&0xFF;
p[1] = (v>>8)&0xFF;
p[2] = (v>>16)&0xFF;
p[3] = (v>>24)&0xFF;
}
void Cleanup (void)
{
if (BlockMap)
free (BlockMap);
if (RawDirectory)
free (RawDirectory);
if (Directory)
free (Directory);
/*if (ParTable)
free (ParTable);*/
BlockMap = NULL;
RawDirectory = NULL;
Directory = NULL;
/*ParTable = NULL;*/
blocks_filesystem=0;
num_dentries=0;
}
#define OFFS_TO_PARTS 0x1be
#define OFFS_TO_PTYPE 0x4
#define OFFS_TO_PSTART 0x8
int search_plusidedos_shared_disk (int fd, u8 *sector)
{
int i;
int bc;
u32 lba_start;
/* is this a valid MBR */
if (sector[510]!=0x55 || sector[511]!=0xAA)
return 0;
/* Search for partition type 7F */
for (i=0;i<4;i++)
if (sector[OFFS_TO_PARTS+i*16+OFFS_TO_PTYPE]==ptype)
break;
if (i==4) /* if not found, it's not considered a shared disk */
return 0;
lba_start = read32 (sector+OFFS_TO_PARTS+i*16+OFFS_TO_PSTART);
StartIDEDOS = lba_start*512;
lseek (fd, OffsHeader+StartIDEDOS, SEEK_SET); /* jump to the start of the IDEDOS partition. */
bc = read (fd, sector, SectorSize);
if (bc<SectorSize)
return 0;
if (strncmp (sector, "PLUSIDEDOS", 10)==0)
return 1;
else
return 0;
}
int get_partition_table (int fd)
{
u8 *buff; /* partition table buffer. */
u8 sector[512];
tParEntry temp;
u8 *pxdbp;
u8 *buffer;
int bc;
int i=0;
int part_max, free_part;
StartIDEDOS = 0;
lseek (fd, OffsHeader+StartIDEDOS, SEEK_SET); /* jump over the HDF header, if any. */
bc = read (fd, sector, SectorSize); /* read first sector, with system partition */
if (bc<SectorSize)
return 0;
/* first partition must be system partition */
if (strncmp (sector, "PLUSIDEDOS", 10))
{
/* TODO: Check if this is shared disk */
if (search_plusidedos_shared_disk (fd, sector)==0) /* this will change StartIDEDOS */
return 0;
}
part_max=1+read16(sector+38); /* Max. number of partitions available. */
buff=(u8 *)malloc(next512mult(part_max*64)); /* disk buffer to read all partitions */
ParTable=(tParEntry *)malloc(part_max * sizeof(tParEntry));
lseek (fd, OffsHeader+StartIDEDOS, SEEK_SET); /* reset file position to first partition. */
bc = read (fd, buff, next512mult(part_max*64));
if (bc<0)
{
free (buff);
free (ParTable);
return 0;
}
buffer=buff;
for (i=0;i<part_max;i++)
{
/* Jump over a unused entry */
if (buffer[16]==0)
{
buffer+=64;
continue;
}
/* Get partition entry parms */
strncpy (ParTable[num_parts].name, buffer, 16);
ParTable[num_parts].name[16]='\0';
ParTable[num_parts].type=buffer[16];
if (ParTable[num_parts].type==255) /* if it's the partition that holds free space, change the name to ----- */
strcpy (ParTable[num_parts].name, "----------------");
ParTable[num_parts].cyl_start=read16(buffer+17);
ParTable[num_parts].head_start=buffer[19];
ParTable[num_parts].cyl_end=read16(buffer+20);
ParTable[num_parts].head_end=buffer[22];
ParTable[num_parts].last_sector=read32(buffer+23);
if (num_parts==0)
ParTable[num_parts].FirstLBA=0;
else
ParTable[num_parts].FirstLBA=ParTable[num_parts].cyl_start*ParTable[0].DiskPar.head*ParTable[0].DiskPar.sect+
ParTable[num_parts].head_start*ParTable[0].DiskPar.sect;
ParTable[num_parts].LastLBA=ParTable[num_parts].FirstLBA+ParTable[i].last_sector;
ParTable[num_parts].drive_letter=buffer[60]; /* drive letter mapped to this partition */
/* Get XDBP table */
if (ParTable[num_parts].type==3)
{
pxdbp=buffer+32;
ParTable[num_parts].xdpb.spt=read16(pxdbp);
ParTable[num_parts].xdpb.bsh=pxdbp[2];
ParTable[num_parts].xdpb.blm=pxdbp[3];
ParTable[num_parts].xdpb.exm=pxdbp[4];
ParTable[num_parts].xdpb.dsm=read16(pxdbp+5);
ParTable[num_parts].xdpb.drm=read16(pxdbp+7);
ParTable[num_parts].xdpb.al0=pxdbp[9];
ParTable[num_parts].xdpb.al1=pxdbp[10];
ParTable[num_parts].xdpb.cks=read16(pxdbp+11);
ParTable[num_parts].xdpb.off=read16(pxdbp+13);
ParTable[num_parts].xdpb.psh=pxdbp[15];
ParTable[num_parts].xdpb.phm=pxdbp[16];
ParTable[num_parts].xdpb.sideness=pxdbp[17];
ParTable[num_parts].xdpb.tracks_per_side=pxdbp[18];
ParTable[num_parts].xdpb.sectors_per_track=pxdbp[19];
ParTable[num_parts].xdpb.first_sector=pxdbp[20];
ParTable[num_parts].xdpb.sector_size=read16(pxdbp+21);
ParTable[num_parts].xdpb.gap_length_rw=pxdbp[23];
ParTable[num_parts].xdpb.gap_length_fmt=pxdbp[24];
ParTable[num_parts].xdpb.mlt_track=pxdbp[25];
ParTable[num_parts].xdpb.freeze_flag=pxdbp[26];
}
else if (ParTable[num_parts].type==1)
{
ParTable[num_parts].DiskPar.cyl=read16(buffer+32);
ParTable[num_parts].DiskPar.head=buffer[34];
ParTable[num_parts].DiskPar.sect=buffer[35];
ParTable[num_parts].DiskPar.sect_per_cyl=read16(buffer+36);
ParTable[num_parts].DiskPar.last_pentry=read16(buffer+38);
}
num_parts++;
buffer+=64;
/* Assume that the free partition type is the last one. */
/* Record the position for the "FREE" partition, to put it the last one
for listing purposes. */
if (ParTable[num_parts-1].type==255)
free_part = num_parts-1;
}
if (free_part!=num_parts-1) /* if it's not the last partition */
{
temp=ParTable[num_parts-1];
ParTable[num_parts-1]=ParTable[free_part];
ParTable[free_part]=temp;
}
free (buff);
return 1;
}
void show_partition_table (void)
{
int i;
char *typ;
char type_drive[20];
printf ("\nDisk geometry (C/H/S): %d/%d/%d\n", ParTable[0].DiskPar.cyl, ParTable[0].DiskPar.head, ParTable[0].DiskPar.sect);
printf ("\nPARTITION TABLE\n");
printf ("\nNum. Name Type CH Begin CH End LBA Begin LBA End Size\n");
printf ("---------------------------------------------------------------------------\n");
for (i=0;i<num_parts;i++)
{
printf ("%4d %16.16s ", i, ParTable[i].name);
switch (ParTable[i].type)
{
case 0: typ="Free"; break;
case 1: typ="System"; break;
case 2: typ="Swap"; break;
case 3: typ="+3DOS"; break;
case 255: typ="FREE"; break;
default: typ="Other"; break;
}
if (ParTable[i].drive_letter>='A' && ParTable[i].drive_letter<='Z')
sprintf (type_drive, "%s %c:", typ, ParTable[i].drive_letter);
else
strcpy (type_drive, typ);
printf ("%-7.7s", type_drive);
printf ("%5d/%-2d %5d/%-2d %-9d %-9d %-4dMB\n",
ParTable[i].cyl_start,
ParTable[i].head_start,
ParTable[i].cyl_end,
ParTable[i].head_end,
ParTable[i].FirstLBA,
ParTable[i].LastLBA,
(ParTable[i].LastLBA-ParTable[i].FirstLBA+1)/2048);
}
printf ("\n%d partition entries remain unassigned.\n", ParTable[0].DiskPar.last_pentry + 1 - num_parts);
}
void show_partition_table_backend (void)
{
int i;
printf ("%d%c%d%c%d\n", ParTable[0].DiskPar.cyl, sep, ParTable[0].DiskPar.head, sep, ParTable[0].DiskPar.sect);
printf ("%d%c%d\n", num_parts, sep, ParTable[0].DiskPar.last_pentry + 1 - num_parts);
for (i=0;i<num_parts;i++)
{
printf ("%s%c%d%c", strtrim(ParTable[i].name), sep, ParTable[i].type, sep);
if (ParTable[i].drive_letter>='A' && ParTable[i].drive_letter<='Z')
printf ("%c%c", ParTable[i].drive_letter, sep);
else
printf ("%c", sep);
#ifdef WIN32
printf ("%d%c%d%c%d%c%d%c%d%c%d%c%I64u\n",
#else
printf ("%d%c%d%c%d%c%d%c%d%c%d%c%llu\n",
#endif
ParTable[i].cyl_start, sep,
ParTable[i].head_start, sep,
ParTable[i].cyl_end, sep,
ParTable[i].head_end, sep,
ParTable[i].FirstLBA, sep,
ParTable[i].LastLBA, sep,
(u64)(ParTable[i].LastLBA-ParTable[i].FirstLBA+1)*SectorSize*1L);
}
}
void show_partition_table_bare (void)
{
int i;
for (i=0;i<num_parts;i++)
if (ParTable[i].type==3)
printf ("%s\n", strtrim(ParTable[i].name));
}
int show_partition_entry (char *part)
{
char *typ;
int np;
np=FindPartition (part);
if (np<0)
np=atoi(part);
if (np<0 || np>=num_parts)
{
fprintf (stderr, "Incorrect partition number.\n");
return 0;
}
if (ParTable[np].type!=3)
{
fprintf (stderr, "Details for this partition are not available.\n");
return 0;
}
printf ("\nDETAILS FOR PARTITION: %s\n", ParTable[np].name);
switch (ParTable[np].type)
{
case 0: typ="Free"; break;
case 1: typ="System"; break;
case 2: typ="Swap"; break;
case 3: typ="+3DOS"; break;
case 255: typ="FREE"; break;
default: typ="Other"; break;
}
printf ("Type: %s", typ);
if (ParTable[np].drive_letter>='A' && ParTable[np].drive_letter<='Z')
printf (" mapped to %c:\n", ParTable[np].drive_letter);
else
printf ("\n");
printf ("First LBA address: %d\n", ParTable[np].FirstLBA);
printf ("Last LBA address: %d\n", ParTable[np].LastLBA);
printf ("Size (MBytes): %d MB\n", (ParTable[np].LastLBA-ParTable[np].FirstLBA+1)/2048);
printf ("Reserved bytes from begining of partition: %d\n", ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128);
printf ("Block size: %d bytes\n", (ParTable[np].xdpb.blm+1)*128);
printf ("Directory entries: %d\n", ParTable[np].xdpb.drm+1);
printf ("Offset to data area (directory size): %Xh\n", (ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128) + (ParTable[np].xdpb.drm+1)*32);
printf ("Data size for this filesystem: %d bytes.\n", (ParTable[np].xdpb.dsm+1) * (ParTable[np].xdpb.blm+1) * 128);
printf ("\nXDBP parms:\n");
printf (" SPT : %d\t\t", ParTable[np].xdpb.spt);
printf (" BSH : %d\n", ParTable[np].xdpb.bsh);
printf (" BLM : %d\t\t", ParTable[np].xdpb.blm);
printf (" EXM : %d\n", ParTable[np].xdpb.exm);
printf (" DSM : %d\t\t", ParTable[np].xdpb.dsm);
printf (" DRM : %d\n", ParTable[np].xdpb.drm);
printf (" AL0 : %d\t\t", ParTable[np].xdpb.al0);
printf (" AL1 : %d\n", ParTable[np].xdpb.al1);
printf (" CKS : %d\t\t", ParTable[np].xdpb.cks);
printf (" OFF : %d\n", ParTable[np].xdpb.off);
printf (" PSH : %d\t\t", ParTable[np].xdpb.psh);
printf (" PHM : %d\n", ParTable[np].xdpb.phm);
printf (" Sideness : %d\t\t\t", ParTable[np].xdpb.sideness);
printf (" Tracks per side : %d\n", ParTable[np].xdpb.tracks_per_side);
printf (" Sectors per track : %d\t", ParTable[np].xdpb.sectors_per_track);
printf (" First sector # : %d\n", ParTable[np].xdpb.first_sector);
printf (" Sector size : %d\t\t", ParTable[np].xdpb.sector_size);
printf (" GAP length R/W : %d\n", ParTable[np].xdpb.gap_length_rw);
printf (" GAP length fmt : %d\t\t", ParTable[np].xdpb.gap_length_fmt);
printf (" Multitrack : %d\n", ParTable[np].xdpb.mlt_track);
printf (" Freeze flag : %d\n", ParTable[np].xdpb.freeze_flag);
return 1;
}
int show_partition_entry_backend (char *part)
{
int np;
np=FindPartition (part);
if (np<0)
np=atoi(part);
if (np<0 || np>=num_parts)
{
fprintf (stderr, "Incorrect partition number.\n");
return 0;
}
if (ParTable[np].type!=3)
{
fprintf (stderr, "Details for this partition are not available.\n");
return 0;
}
printf ("%s%c%d%c", strtrim(ParTable[np].name), sep, ParTable[np].type, sep);
if (ParTable[np].drive_letter>='A' && ParTable[np].drive_letter<='Z')
printf ("%c%c", ParTable[np].drive_letter, sep);
else
printf ("%c", sep);
printf ("%d%c", ParTable[np].FirstLBA, sep);
printf ("%d%c", ParTable[np].LastLBA, sep);
printf ("%d%c", (ParTable[np].LastLBA-ParTable[np].FirstLBA+1)*SectorSize, sep);
printf ("%d%c", ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128, sep);
printf ("%d%c", (ParTable[np].xdpb.blm+1)*128, sep);
printf ("%d%c", ParTable[np].xdpb.drm+1, sep);
printf ("%d%c", (ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128) + (ParTable[np].xdpb.drm+1)*32, sep);
printf ("%d%c", (ParTable[np].xdpb.dsm+1) * (ParTable[np].xdpb.blm+1) * 128, sep);
printf ("%d%c", ParTable[np].xdpb.spt, sep);
printf ("%d%c", ParTable[np].xdpb.bsh, sep);
printf ("%d%c", ParTable[np].xdpb.blm, sep);
printf ("%d%c", ParTable[np].xdpb.exm, sep);
printf ("%d%c", ParTable[np].xdpb.dsm, sep);
printf ("%d%c", ParTable[np].xdpb.drm, sep);
printf ("%d%c", ParTable[np].xdpb.al0, sep);
printf ("%d%c", ParTable[np].xdpb.al1, sep);
printf ("%d%c", ParTable[np].xdpb.cks, sep);
printf ("%d%c", ParTable[np].xdpb.off, sep);
printf ("%d%c", ParTable[np].xdpb.psh, sep);
printf ("%d%c", ParTable[np].xdpb.phm, sep);
printf ("%d%c", ParTable[np].xdpb.sideness, sep);
printf ("%d%c", ParTable[np].xdpb.tracks_per_side, sep);
printf ("%d%c", ParTable[np].xdpb.sectors_per_track, sep);
printf ("%d%c", ParTable[np].xdpb.first_sector, sep);
printf ("%d%c", ParTable[np].xdpb.sector_size, sep);
printf ("%d%c", ParTable[np].xdpb.gap_length_rw, sep);
printf ("%d%c", ParTable[np].xdpb.gap_length_fmt, sep);
printf ("%d%c", ParTable[np].xdpb.mlt_track, sep);
printf ("%d\n", ParTable[np].xdpb.freeze_flag);
return 1;
}
int get_directory (int fd, char *part)
{
int i,j,bc,bl;
u8 *dentry,*dentr2;
u16 blnum;
int np;
np=FindPartition (part);
if (np<0)
np=atoi(part);
if (np<0 || np>=num_parts)
{
fprintf (stderr, "Invalid partition number.\n");
return 0;
}
if (ParTable[np].type!=3)
{
fprintf (stderr, "Unable to obtain directory for this partition.\n");
return 0;
}
/* Seek for the beginning of directory */
lseek (fd, OffsHeader + /* OffsHeader from beginning of disk image. */
ParTable[np].FirstLBA * SectorSize + /* Offset to the beginning of partition */
ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128, /* Offset to beginning of directory. */
SEEK_SET);
/* Read the whole directory. Each entry fills 32 bytes */
RawDirectory=(u8 *)malloc((ParTable[np].xdpb.drm+1)*32);
bc=read (fd, RawDirectory, (ParTable[np].xdpb.drm+1)*32);
if (bc<(ParTable[np].xdpb.drm+1)*32)
{
fprintf (stderr, "Error reading directory table.\n");
free(RawDirectory);
return 0;
}
Directory=(tDirEntry *)malloc((ParTable[np].xdpb.drm+1)*sizeof(tDirEntry));
blocks_filesystem=(ParTable[np].xdpb.dsm+1);
BlockMap=(u8 *)malloc(blocks_filesystem);
memset (BlockMap, 0, blocks_filesystem);
for (i=0;i<(ParTable[np].xdpb.drm+1)*32/((ParTable[np].xdpb.blm+1)*128);i++)
BlockMap[i]=1; /* Mark directory blocks as used. */
num_dentries=0;
for (i=0;i<=ParTable[np].xdpb.drm;i++)
{
dentry=RawDirectory+i*32; /* point to next dentry */
if (dentry[0]==0xe5) /* free dentry */
continue;
if (FileExists (dentry+1)>=0) /* this denotes a file with more than one extent */
continue;
Directory[num_dentries].type=dentry[0];
for (j=0;j<11;j++)
Directory[num_dentries].fname[j]=dentry[1+j] & 0x7f;
Directory[num_dentries].fname[11]='\0';
Directory[num_dentries].read_only=(dentry[9]&0x80)?1:0;
Directory[num_dentries].system_file=(dentry[10]&0x80)?1:0;
Directory[num_dentries].archived=(dentry[11]&0x80)?1:0;
/* Find and add all extents that belong to the same file */
if (Directory[num_dentries].type<=33)
{
Directory[num_dentries].extused=0;
Directory[num_dentries].blused=0;
Directory[num_dentries].nbytes=0;
for (j=0;j<=ParTable[np].xdpb.drm;j++)
{
dentr2=RawDirectory+j*32; /* point to next dentry */
if (dentr2[0]<=33 && strncmp(Directory[num_dentries].fname, dentr2+1, 11)==0) /* found a new extent for this file */
{
Directory[num_dentries].extents[Directory[num_dentries].extused++] = j;
for (bl=0;bl<8;bl++) /* Add all blocks that this extent uses to the list of blocks for this file */
{
blnum=read16(dentr2+16+bl*2);
if (blnum)
{
Directory[num_dentries].blocks[Directory[num_dentries].blused++]=blnum;
Directory[num_dentries].nbytes += (ParTable[np].xdpb.blm+1)*128;
/* Update block map with blocks used by this file */
BlockMap[blnum]=1;
}
else
break; /* no more blocks for this file */
}
if (dentr2[15]!=128) /* I assume that a Rc value of 128 means all the extent is used. */
{
/* Substract number of bytes in the last logical extent. */
if (Directory[num_dentries].nbytes % 16384 == 0)
Directory[num_dentries].nbytes -= 16384;
else
Directory[num_dentries].nbytes = 16384*(Directory[num_dentries].nbytes/16384);
Directory[num_dentries].nbytes += (dentr2[15]-1)*128; /* number of 128 records filled in last logical extent. */
Directory[num_dentries].nbytes += (dentr2[13]==0)?128:dentr2[13]; /* add number of bytes in last record (0 means 128) */
}
}
}
}
num_dentries++;
}
return np; /* will be >0, as partition 0 is system partition and cannot be directory processed */
}
int get_3dos_header (int fd, int np, int nf, tP3Header *header)
{
tP3Header h;
u8 head[512];
int bc,res;
/* Seek for the beginning of directory */
res=lseek (fd, OffsHeader + /* OffsHeader from beginning of disk image. */
ParTable[np].FirstLBA * SectorSize + /* Offset to the beginning of partition */
ParTable[np].xdpb.spt * ParTable[np].xdpb.off * 128 + /* Offset to beginning of blocks (directory starts at block 0). */
(Directory[nf].blocks[0]*(ParTable[np].xdpb.blm+1)*128), /* Offset to block 0 of this file. */
SEEK_SET);
bc=read (fd, head, 512); /* actually, we need only the first 128 bytes. */
if (bc<128)
return 0;
if (strncmp(head,"PLUS3DOS",8))
return 0; /* return if +3DOS header not found. */
h.issue=head[9];
h.version=head[10];
h.nbytes=read32(head+11);
h.type=head[15];
h.length=read16(head+16);
h.start=read16(head+18);
h.vars=read16(head+20);
*header = h;
return 1;
}
void show_directory (int fd, int np)
{
int i;
tP3Header p3h;
char *typ;
printf ("Directory for %s\n\n", ParTable[np].name);
printf ("Name Disksize Att Ver HdSize Type RSize Start Vars \n");
printf ("---------------------------------------------------------------------------\n");
for (i=0;i<num_dentries;i++)
{
if (Directory[i].type>33)
continue;
printf ("%-8.8s.%-3.3s ", Directory[i].fname, Directory[i].fname+8);
printf ("%-8d ", Directory[i].nbytes);
printf ("%c%c%c ", (Directory[i].read_only)?'R':' ', (Directory[i].system_file)?'S':' ', (Directory[i].archived)?'A':' ');
if (get_3dos_header (fd, np, i, &p3h))
{
printf ("%d.%d ", p3h.issue, p3h.version);
printf ("%-8d ", p3h.nbytes);
switch (p3h.type)
{
case 0: typ="Program"; break;
case 1: typ="Num array"; break;
case 2: typ="Char array"; break;
case 3: typ="Bytes"; break;
default: typ="Unknown"; break;
}
printf ("%-10.10s ", typ);
printf ("%-5d %-5d %-5d ", p3h.length, p3h.start, p3h.vars);
}
else
printf ("Headerless file.");
printf ("\n");
}
}
void show_directory_backend (int fd, int np)
{
tP3Header p3h;
int i;
char name[9];
char exts[4];
printf ("%d\n", num_dentries);
for (i=0;i<num_dentries;i++)
{
if (Directory[i].type>33)
continue;
strncpy (name, Directory[i].fname, 8);
name[8]='\0';
strncpy (exts, Directory[i].fname+8, 3);
exts[3]='\0';
if (strlen(strtrim(exts))>0)
printf ("%s.%s%c", strtrim(name), strtrim(exts), sep);
else
printf ("%s%c", strtrim(name), sep);
printf ("%d%c", Directory[i].nbytes, sep);
printf ("%c%c%c%c", (Directory[i].read_only)?'R':' ', (Directory[i].system_file)?'S':' ', (Directory[i].archived)?'A':' ', sep);
if (get_3dos_header (fd, np, i, &p3h))
{
printf ("%d%c%d%c", p3h.issue, sep, p3h.version, sep);
printf ("%d%c", p3h.nbytes, sep);
printf ("%d%c", p3h.type, sep);
printf ("%d%c%d%c%d", p3h.length, sep, p3h.start, sep, p3h.vars);
}
else
printf ("1%c0%c%d%c255%c%d%c0%c32768", sep, sep, Directory[i].nbytes, sep, sep, Directory[i].nbytes, sep, sep);
printf ("\n");
}
}
void show_directory_bare (int fd, int np)
{
int i;
char name[9];
char exts[4];
for (i=0;i<num_dentries;i++)
{
if (Directory[i].type>33)
continue;
strncpy (name, Directory[i].fname, 8);
name[8]='\0';
strncpy (exts, Directory[i].fname+8, 3);
exts[3]='\0';
if (strlen(strtrim(exts))>0)
printf ("%s.%s\n", strtrim(name), strtrim(exts));
else
printf ("%s\n", strtrim(name));
}
}
int PC2CPM (int fd, char *fname, int *npar, int *nfil, char *filename, char *outname)
{
char *name, *ext;
char fnam[100];
int np,i;
strcpy (fnam, fname);
name=strchr(fnam,':');
if (!name)
{
fprintf (stderr, "Bad file name: missing partition. Format is: partition:name[.ext]\n");
return 0;
}
name[0]='\0'; /* writes a NULL instead of : */
np=FindPartition (fnam);
if (np<0)
np=atoi(fnam);
if (np<0 || np>=num_parts || ParTable[np].type!=3)
{
fprintf (stderr, "Invalid partition.\n");
return 0;
}