forked from jhallen/atari-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atr.c
1845 lines (1676 loc) · 64.6 KB
/
atr.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
/* Atari diskette access
* Copyright
* (C) 2011 Joseph H. Allen
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 1, or (at your option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Atari disk access */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Disks: .ATR file has a 16 byte header, then data:
*
* DOS 2.0S single density (40 tracks, 18 sectors, 128 byte sectors): 92160 bytes
*
* Drive numbers sectors 1..720 but allocation map numbers sectors
* 0..719. Since there is no sector 0, it's always marked in-use in the
* allocation map. Sector 720 can not be allocated since there is no
* allocation map bit for it.
*
* Boot sectors = 1..3. These are allocated and written on newly
* formatted disks even if there is no dos.sys.
*
* VTOC sector = 360 (0x168)
*
* Directory sectors = 361..368 (0x169..0x170)
*
* Out of reach sector = 720 (no bitmap bit for it)
*
* VTOC:
* 0: DOS version code: 2 for Atari DOS 2.0
* 1..2: Initial number of free sectors in allocation map. Excludes pre-allocated
* sectors including sector 0, boot, VTOC, and directory. Should be 707.
*
* 3..4: Current number of free sectors 0..707
* 5..9: unused
* 10..99: allocation bitmap for sectors 0..719. 0 means in use.
* 100-127: unused
*
* Directory entry: (8 entries per 128 byte sector)
* 0: flag byte (0 means unused, 0x42 means in use)
* bit 0: opened for output
* bit 1: created by DOS 2
* bit 5: file locked
* bit 6: this entry in use
* bit 7: this entry has been deleted
* 1..2: number of sectors in file
* 3..4: starting sector number
* 5..12: 8 byte file name
* 13..15: 3 byte extension
*
* Data sectors:
* 0..124 Contain data
* 125 File number in upper 6 bits. Upper 2 bits of next
* sector number in lower two bits.
* 126 Lower 8 bits of next sector number.
* 127 Number of data bytes in sector: Usually 125 except for last sector
*
* DOS 2.5 Enhanced density (40 tracks, 26 sectors, 128 byte sectors): 133120 bytes
*
* Drive numbers sectors 1..1040 but allocation map numbers sectors
* 0..1023. Since there is no sector 0, it's always marked in-use in the
* allocation map. Out of reach sector 1024 is used for VTOC2. Sectors
* 1025..1040 not used because next sector number is only 10 bits.
*
* Note: on new disks, DOS 2.5 allocates sector 720 even though it is not used
* for anything. I think this is to enhance backward compatibility with DOS 2.0s
* (where some programs might use sector 720, knowing that the OS will not normally use
* use it).
*
* Boot sectors = 1..3. These are allocated and written on newly formatted disks
* even if there is no dos.sys written.
*
* VTOC sector = 360 (0x168)
*
* Directory sectors = 361..368
*
* VTOC2 = 1024 (has more bitmap bits)
*
* Out of reach sectors = 1024..1040 (because next sector number is 10 bits).
*
* VTOC: Same as 2.0s, except:
*
* Initial number of free sectors in allocation map. Excludes pre-allocated
* sector 0, boot, VTOC, directory and sector 720. Should be 1010 but 1011
* is probably OK as well (for a format without pre-allocating 720).
*
* Current number of free sectors below 720. Should be 707 on a new disk since
* sector 0, boot sectors, VTOC and directory sectors are pre-allocated.
*
* VTOC2:
* 0..83: Repeat VTOC bitmap for sectors 48..719 (write these, do not read them)
* 84..121: Bitmap for sectors 720..1023
* 122..123: Current number of free sectors above sector 719. Should be 303 on a new
* disk because sector 720 is pre-allocated.
* 124..127: Unused.
*
* Directory: same as DOS 2.0s
*
* Data sectors: same as DOS 2.0s
*
* DOS 2.0d Double density (40 tracks, 18 sectors, 256 byte sectors):
* 184320 - 384 = 183936 bytes (subtract 384 because first three sectors have 128 bytes).
*
* Sector numbering: Same as DOS 2.0s
*
* Boot sectors = 1..3 Only first 128 bytes of each used even though on the disk they
* are 256 bytes. Usually these sectors use 128 bytes in the .ATR file, but not
* always.
*
* VTOC sector = 360 (0x168)
*
* Directory sectors = 361..368 (0x169..0x170)
*
* Out of reach sector = 720 (out of reach because no bitmap bit for it)
*
* VTOC: Same as DOS 2.0s, except balance of 256 byte sector is left unused.
*
* Directory: Same as DOS 2.0s. Note that each directory sector has 8
* entries even though 16 would fit. Bytes 128 - 255 of each directory sector
* are left unused.
*
* Data sectors:
* 0..252 Contain data
* 253 File number in upper 6 bits. Upper 2 bits of next
* sector number in lower two bits.
* 254 Lower 8 bits of next sector number.
* 255 Number of data bytes in sector: Usually 253 except for last sector
*/
/* Error status */
int status = 0;
/* Set if fixes were made */
int fixes = 0;
/* Sector size in bytes */
#define SECTOR_SIZE 128
#define DD_SECTOR_SIZE 256
/* True if disk is double-density */
int disk_dd = 0;
int sector_size = SECTOR_SIZE;
/* Largest reachable sector + 1 */
int disk_size = 720;
#define SD_DISK_SIZE 720
#define ED_DISK_SIZE 1024
#define DD_DISK_SIZE 720
/* Specific sectors */
#define SECTOR_VTOC 0x168 /* VTOC / free space bitmap */
#define SECTOR_VTOC2 0x400 /* VTOC2 */
#define SECTOR_DIR 0x169 /* First directory sector */
/* Number of directory sectors */
#define SECTOR_DIR_SIZE 8
/* Directory entry */
#define FLAG_NEVER_USED 0x00
#define FLAG_DELETED 0x80
#define FLAG_IN_USE 0x40
#define FLAG_LOCKED 0x20
#define FLAG_DOS2 0x02
#define FLAG_OPENED 0x01
/* Directory entry */
struct dirent {
unsigned char flag;
unsigned char count_lo;
unsigned char count_hi;
unsigned char start_lo;
unsigned char start_hi;
unsigned char name[8];
unsigned char suffix[3];
};
/* Size of a directory entry */
#define ENTRY_SIZE 16
/* Bytes within data sectors */
/* First 125 bytes are used for data */
#define DATA_SIZE 125
#define DD_DATA_SIZE 253
int data_size = DATA_SIZE;
/* Byte 125 has file number in upper 6 bits */
#define DATA_FILE_NUM 125 /* Upper 6 bits (0..63) */
#define DD_DATA_FILE_NUM 253
int data_file_num = DATA_FILE_NUM;
/* Byte 125 and 126 have next sector number: valid values (1..719) or (1..1023) */
#define DATA_NEXT_HIGH 125 /* Lower 2 bits */
#define DD_DATA_NEXT_HIGH 253
int data_next_high = DATA_NEXT_HIGH;
#define DATA_NEXT_LOW 126 /* All 8 bits */
#define DD_DATA_NEXT_LOW 254 /* All 8 bits */
int data_next_low = DATA_NEXT_LOW;
/* Byte 127 has number of bytes used */
#define DATA_BYTES 127
#define DD_DATA_BYTES 255
int data_bytes = DATA_BYTES;
void set_density(int dd)
{
if (dd) {
sector_size = DD_SECTOR_SIZE;
data_size = DD_DATA_SIZE;
data_file_num = DD_DATA_FILE_NUM;
data_next_high = DD_DATA_NEXT_HIGH;
data_next_low = DD_DATA_NEXT_LOW;
data_bytes = DD_DATA_BYTES;
disk_dd = 1;
} else {
sector_size = SECTOR_SIZE;
data_size = DATA_SIZE;
data_file_num = DATA_FILE_NUM;
data_next_high = DATA_NEXT_HIGH;
data_next_low = DATA_NEXT_LOW;
data_bytes = DATA_BYTES;
disk_dd = 0;
}
}
/* Bytes within VTOC */
/* Bytes 0 - 9 is a header */
#define VTOC_TYPE 0
#define VTOC_NUM_SECTS 1
#define VTOC_NUM_UNUSED 3
#define VTOC_RESERVED 5
#define VTOC_UNUSED 6
/* Location of bitmap in VTOC */
#define VTOC_BITMAP 10
/* Bytes 10 - 99 used
If the bit is a 1, the sector is free.
If the bit is a 0, the sector is in use.
Left most bit of byte 10 is sector 0 (does not exist)
Right most bit of byte 99 is sector 719
Sector 720 can not be used by DOS.
First real sector is sector 1.
*/
/* Bytes 100 - 127 unused */
/* VTOC2 format */
/* Size of bitmap */
#define SD_BITMAP_SIZE 90
#define ED_BITMAP_SIZE 128
/* Offset to first bitmap byte copied to VTOC2 */
#define ED_BITMAP_START 6
/* ED disks have 128 bytes for their bitmap:
* VTOC has bytes 0 - 99 and is located at offset 10 within the VTOC sector
* VTOC2 has bytes 6 - 127 and is located at offset 0 within the VTOC2 sector
*/
#define VTOC2_NUM_UNUSED 122
FILE *disk;
int getsect(unsigned char *buf, int sect)
{
int offset;
int size;
if (!sect) {
fprintf(stderr,"Oops, tried to read sector 0\n");
return -1;
}
sect -= 1;
if (disk_dd) {
if (sect < 3) {
size = SECTOR_SIZE;
offset = SECTOR_SIZE * sect;
} else {
size = DD_SECTOR_SIZE;
offset = SECTOR_SIZE * 3 + DD_SECTOR_SIZE * (sect - 3);
}
} else {
size = SECTOR_SIZE;
offset = SECTOR_SIZE * sect;
}
if (fseek(disk, offset + 16, SEEK_SET)) {
fprintf(stderr,"Oops, tried to seek past end (sector %d)\n", sect + 1);
status = 1;
return -1;
}
if (size != fread((char *)buf, 1, size, disk)) {
fprintf(stderr,"Oops, read error (sector %d)\n", sect + 1);
status = 1;
return -1;
}
return 0;
}
void putsect(unsigned char *buf, int sect)
{
int offset;
int size;
if (!sect) {
fprintf(stderr,"Oops, requested sector 0\n");
exit(-1);
}
sect -= 1;
if (disk_dd) {
if (sect < 3) {
size = SECTOR_SIZE;
offset = SECTOR_SIZE * sect;
} else {
size = DD_SECTOR_SIZE;
offset = SECTOR_SIZE * 3 + DD_SECTOR_SIZE * (sect - 3);
}
} else {
size = SECTOR_SIZE;
offset = SECTOR_SIZE * sect;
}
if (fseek(disk, offset + 16, SEEK_SET)) {
fprintf(stderr,"Oops, seek error during write (sector %d)\n", sect + 1);
exit(-1);
}
if (size != fwrite((char *)buf, 1, size, disk)) {
fprintf(stderr,"Oops, write error (sector %d)\n", sect + 1);
exit(-1);
}
}
/* Count number of free sectors in a bitmap */
int count_free(unsigned char *bitmap, int len)
{
int count = 0;
int x;
while (len--) {
for (x = 1; x != 256; x *= 2) {
if (*bitmap & x)
++count;
}
++bitmap;
}
return count;
}
/* Fix it? */
int fix;
int fixit()
{
if (!fix)
return 0;
for (;;) {
char buf[80];
printf("Fix it (y,n)? ");
fflush(stdout);
if (fgets(buf,sizeof(buf),stdin)) {
if (buf[0] == 'y' || buf[0] == 'Y')
return 1;
else if (buf[0] == 'n' || buf[0] == 'N')
return 0;
}
}
}
/* Get allocation bitmap */
void getmap(unsigned char *bitmap, int check)
{
unsigned char vtoc[DD_SECTOR_SIZE];
unsigned char vtoc2[DD_SECTOR_SIZE];
int upd = 0;
if (getsect(vtoc, SECTOR_VTOC)) {
fprintf(stderr," (trying to read VTOC)\n");
exit(-1);
}
memcpy(bitmap, vtoc + VTOC_BITMAP, SD_BITMAP_SIZE);
if (check) {
int count = count_free(bitmap, SD_BITMAP_SIZE);
int vtoc_count = vtoc[VTOC_NUM_UNUSED] + (256 * vtoc[VTOC_NUM_UNUSED + 1]);
int vtoc_total = vtoc[VTOC_NUM_SECTS] + (256 * vtoc[VTOC_NUM_SECTS + 1]);
int expected_size;
printf(" Checking that VTOC current free sector count matches bitmap...\n");
if (count != vtoc_count) {
fprintf(stderr," ** It doesn't match: bitmap has %d free, but VTOC count is %d\n", count, vtoc_count);
status = 1;
if (fixit()) {
vtoc[VTOC_NUM_UNUSED] = (0xFF & count);
vtoc[VTOC_NUM_UNUSED + 1] = (0xFF & (count >> 8));
upd = 1;
}
} else {
printf(" It's OK (count is %d)\n", count);
}
if (disk_size == ED_DISK_SIZE)
expected_size = 1010; /* 1011 if we don't pre-allocate 720 */
else
expected_size = 707;
printf(" Checking that VTOC initial free sector count is %d...\n", expected_size);
if (vtoc_total != expected_size) {
fprintf(stderr," ** It's wrong, we found: %d\n", vtoc_total);
status = 1;
if (fixit()) {
vtoc[VTOC_NUM_SECTS] = (0xFF & expected_size);
vtoc[VTOC_NUM_SECTS + 1] = (0xFF & (expected_size >> 8));
upd = 1;
}
} else
printf(" It's OK\n");
printf(" Checking that VTOC type code is 2...\n");
if (vtoc[VTOC_TYPE] == 2)
printf(" It's OK\n");
else {
fprintf(stderr, " ** It's wrong, we found: %d\n", vtoc[VTOC_TYPE]);
status = 1;
if (fixit()) {
vtoc[VTOC_TYPE] = 2;
upd = 1;
}
}
if (upd) {
printf("Saving VTOC1 fixes...\n");
putsect(vtoc, SECTOR_VTOC);
printf(" done.\n");
upd = 0;
fixes = 1;
}
}
if (disk_size == ED_DISK_SIZE) {
if (getsect(vtoc2, SECTOR_VTOC2)) {
printf(" (trying to read VTOC2)\n");
exit(-1);
}
memcpy(
bitmap + SD_BITMAP_SIZE,
vtoc2 + (SD_BITMAP_SIZE - ED_BITMAP_START),
ED_BITMAP_SIZE - SD_BITMAP_SIZE
);
if (check) {
int count = count_free(bitmap + SD_BITMAP_SIZE, ED_BITMAP_SIZE - SD_BITMAP_SIZE);
int vtoc2_count = vtoc2[VTOC2_NUM_UNUSED] + 256 * vtoc2[VTOC2_NUM_UNUSED + 1];
printf(" Checking that VTOC2 current free sector count matches bitmap...\n");
if (count != vtoc2_count) {
fprintf(stderr," ** It doesn't match: bitmap has %d free, but VTOC2 count is %d\n", count, vtoc2_count);
status = 1;
if (fixit()) {
vtoc2[VTOC2_NUM_UNUSED] = (count & 0xFF);
vtoc2[VTOC2_NUM_UNUSED + 1] = (0xFF & (count >> 8));
upd = 1;
}
} else {
printf(" It's OK (count is %d)\n", count);
}
if (upd) {
printf("Saving VTOC2 fixes...\n");
putsect(vtoc2, SECTOR_VTOC2);
printf(" done.\n");
upd = 0;
fixes = 1;
}
}
}
}
/* Write back allocation bitmap */
void putmap(unsigned char *bitmap)
{
unsigned char vtoc[DD_SECTOR_SIZE];
int count;
unsigned char vtoc2[DD_SECTOR_SIZE];
if (getsect(vtoc, SECTOR_VTOC)) {
fprintf(stderr," (trying to read VTOC)\n");
exit(-1);
}
memcpy(vtoc + VTOC_BITMAP, bitmap, SD_BITMAP_SIZE);
/* Update free count */
count = count_free(bitmap, SD_BITMAP_SIZE);
vtoc[VTOC_NUM_UNUSED] = count;
vtoc[VTOC_NUM_UNUSED + 1] = (count >> 8);
putsect(vtoc, SECTOR_VTOC);
if (disk_size == ED_DISK_SIZE) {
if (getsect(vtoc2, SECTOR_VTOC2)) {
fprintf(stderr," (trying to read VTOC2)\n");
exit(-1);
}
memcpy(vtoc2, bitmap + ED_BITMAP_START, ED_BITMAP_SIZE - ED_BITMAP_START);
/* Update free count */
count = count_free(bitmap + SD_BITMAP_SIZE, ED_BITMAP_SIZE - SD_BITMAP_SIZE);
vtoc2[VTOC2_NUM_UNUSED] = count;
vtoc2[VTOC2_NUM_UNUSED + 1] = (count >> 8);
putsect(vtoc2, SECTOR_VTOC2);
}
}
/* Segment list */
struct segment
{
struct segment *next;
int start; /* 2E0=RUN, 2E2=INIT */
int size;
int init;
int run;
};
/* File stored internally for nice formatting */
struct name
{
char *name;
/* From directory entry */
int locked; /* Set if write-protected */
int sector; /* Starting sector of file */
int sects; /* Sector count */
int is_sys; /* Set if it's a .SYS file */
int is_cm; /* Set if it's a .COM file */
/* From file itself */
struct segment *segments;
int size;
};
/* Array of internal file names for formatting */
struct name *names[(SECTOR_DIR_SIZE * SECTOR_SIZE) / ENTRY_SIZE];
int name_n;
/* For qsort */
int comp(struct name **l, struct name **r)
{
return strcmp((*l)->name, (*r)->name);
}
/* Fine an empty directory entry to use for a new file */
int find_empty_entry()
{
unsigned char buf[DD_SECTOR_SIZE];
int x, y;
for (x = SECTOR_DIR; x != SECTOR_DIR + SECTOR_DIR_SIZE; ++x) {
int y;
if (getsect(buf, x)) {
fprintf(stderr," (trying to read directory sector)\n");
exit(-1);
}
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
if (!(d->flag & FLAG_IN_USE)) {
return (x - SECTOR_DIR) * SECTOR_SIZE / ENTRY_SIZE + (y / ENTRY_SIZE);
}
}
}
return -1;
}
int lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else
return c;
}
/* Convert file name from directory into UNIX zero-terminated C string name */
char *getname(struct dirent *d)
{
static char s[50];
int p = 0;
int r;
int i;
/* Get name */
for (i = 0; i != sizeof(d->name); i++) {
s[p++] = lower(d->name[i]);
}
/* Zap trailing spaces */
while (p && s[p - 1] == ' ') --p;
/* Append '.' */
s[p++] = '.';
r = p;
/* Get extension */
for (i = 0; i != sizeof(d->suffix); i++) {
s[p++] = lower(d->suffix[i]);
}
/* Zap tailing spaces */
while (p && s[p - 1] == ' ') --p;
/* Zap '.' if no extension */
if (p == r) --p;
/* Terminate */
s[p] = 0;
return s;
}
/* Write UNIX name into Atari directory entry */
void putname(struct dirent *d, char *name)
{
int x;
/* Copy file name into directory entry */
x = 0;
while (*name && *name != '.' && x < 8) {
if (*name >= 'a' && *name <= 'z')
d->name[x++] = *name++ - 'a' + 'A';
else
d->name[x++] = *name++;
}
while (x < 8) {
d->name[x++] = ' ';
}
x = 0;
while (*name && *name != '.')
++name;
if (*name == '.') {
++name;
while (*name && x < 3) {
if (*name >= 'a' && *name <= 'z')
d->suffix[x++] = *name++ - 'a' + 'A';
else
d->suffix[x++] = *name++;
}
}
while (x < 3) {
d->suffix[x++] = ' ';
}
}
/* Find a file, return number of its first sector */
/* If del is set, mark directory for deletion */
int find_file(char *filename, int del, char *new_name)
{
unsigned char buf[DD_SECTOR_SIZE];
int x, y;
for (x = SECTOR_DIR; x != SECTOR_DIR + SECTOR_DIR_SIZE; ++x) {
int y;
if (getsect(buf, x)) {
fprintf(stderr," (trying to read directory)\n");
goto done;
}
for (y = 0; y != SECTOR_SIZE; y += ENTRY_SIZE) {
struct dirent *d = (struct dirent *)(buf + y);
/* OSS OS/A+ disks put junk after first never used directory entry */
if (!(d->flag & (FLAG_IN_USE | FLAG_DELETED)))
goto done;
if (d->flag & FLAG_IN_USE) {
char *s = getname(d);
if (!strcmp(s, filename)) {
if (del) {
d->flag = 0x80;
putsect(buf, x);
}
if (new_name) {
putname(d, new_name);
putsect(buf, x);
}
return (d->start_hi << 8) + d->start_lo;
}
}
}
}
done:
return -1;
}
/* Read a file */
int cvt_ending = 0;
void read_file(int sector, FILE *f)
{
int count = 0;
do {
unsigned char buf[DD_SECTOR_SIZE];
int next;
int file_no;
int bytes;
if (count == 2048) {
fprintf(stderr," (file too long)\n");
status = 1;
break;
}
if (getsect(buf, sector)) {
fprintf(stderr," (trying to read from file)\n");
status = 1;
return;
}
++count;
next = (int)buf[data_next_low] + ((int)(0x3 & buf[data_next_high]) << 8);
file_no = ((buf[data_file_num] >> 2) & 0x3F);
bytes = buf[data_bytes];
// printf("Sector %d: next=%d, bytes=%d, file_no=%d, short=%d\n",
// sector, next, bytes, file_no, short_sect);
if (cvt_ending) {
int x;
for (x = 0; x != bytes; ++x)
if (buf[x] == 0x9b) {
buf[x] = '\n';
}
}
fwrite(buf, bytes, 1, f);
sector = next;
} while(sector);
}
/* cat a file */
void cat(char *name)
{
int sector = find_file(name, 0, NULL);
if (sector == -1) {
fprintf(stderr,"File '%s' not found\n", name);
exit(-1);
} else {
/* printf("Found file. First sector of file is %d\n", sector); */
read_file(sector, stdout);
}
}
/* get a file from the disk */
int get_file(char *atari_name, char *local_name)
{
int sector = find_file(atari_name, 0, NULL);
if (sector == -1) {
fprintf(stderr,"File '%s' not found\n", atari_name);
return -1;
} else {
FILE *f = fopen(local_name, "w");
if (!f) {
fprintf(stderr,"Couldn't open local file '%s'\n", local_name);
return -1;
}
/* printf("Found file. First sector of file is %d\n", sector); */
read_file(sector, f);
if (fclose(f)) {
fprintf(stderr,"Couldn't close local file '%s'\n", local_name);
return -1;
}
return status;
}
}
/* Mark a sector as allocated or free */
void mark_space(unsigned char *bitmap, int start, int alloc)
{
if (alloc) {
bitmap[start >> 3] &= ~(1 << (7 - (start & 7)));
} else {
bitmap[start >> 3] |= (1 << (7 - (start & 7)));
}
}
/* Delete file */
int del_file(int sector)
{
unsigned char bitmap[ED_BITMAP_SIZE];
int count = 0;
getmap(bitmap, 0);
do {
unsigned char buf[DD_SECTOR_SIZE];
int next;
int file_no;
int bytes;
if (count == 2048) {
fprintf(stderr," (file too long)\n");
break;
}
if (getsect(buf, sector)) {
fprintf(stderr," (while deleting a file)\n");
break;
}
++count;
next = (int)buf[data_next_low] + ((int)(0x3 & buf[data_next_high]) << 8);
file_no = ((buf[data_file_num] >> 2) & 0x3F);
bytes = buf[data_bytes];
// printf("Sector %d: next=%d, bytes=%d, file_no=%d, short=%d\n", sector, next, bytes, file_no, short_sect);
mark_space(bitmap, sector, 0);
sector = next;
} while(sector);
putmap(bitmap);
return 0;
}
/* Delete file name */
int rm(char *name, int ignore)
{
int first_sect = find_file(name, 1, NULL);
if (first_sect != -1) {
if (del_file(first_sect)) {
fprintf(stderr,"Error deleting file '%s'\n", name);
return -1;
} else {
return status;
}
} else {
if (!ignore) {
fprintf(stderr,"File '%s' not found\n", name);
status = 1;
}
return -1;
}
}
/* Count free sectors */
int amount_free(unsigned char *bitmap)
{
int total = 0;
int x;
for (x = 0; x != disk_size; ++x) {
if (bitmap[(x >> 3)] & (1 << (7 - (x & 7))))
++total;
}
return total;
}
/* Free command */
int do_free(void)
{
int amount;
unsigned char bitmap[ED_BITMAP_SIZE];
getmap(bitmap, 0);
amount = amount_free(bitmap);
printf("%d free sectors, %d free bytes\n", amount, amount * sector_size);
return 0;
}
/* Check a single file */
int check_file(struct dirent *d, int y, int x, char *map, char *name[])
{
unsigned char fbuf[DD_SECTOR_SIZE];
char *filename = strdup(getname(d));
int upddir = 0;
int sector;
int sects;
int count = 0;
int upd = 0;
int file_no = (y / ENTRY_SIZE) + ((x - SECTOR_DIR) * SECTOR_SIZE / ENTRY_SIZE);
sector = (d->start_hi << 8) + d->start_lo;
sects = (d->count_hi << 8) + d->count_lo;
printf("Checking %s (file_no %d)\n", filename, file_no);
if (d->flag & FLAG_OPENED) {
printf(" ** Warning: file is marked as opened\n");
if (fixit()) {
d->flag &= ~FLAG_OPENED;
upddir = 1;
}
}
do {
int next;
int sector_file_no;
++count;
if (count == 2048) {
fprintf(stderr," (file too long)\n");
status = 1;
break;
}
if (getsect(fbuf, sector)) {
fprintf(stderr," (reading file)\n");
exit(-1);
}
if (map[sector] != -1) {
fprintf(stderr," ** Uh oh.. sector %d already in use by %s (%d)\n", sector, name[sector] ? name[sector] : "reserved", map[sector]);
status = 1;
}
if (map[sector] == file_no) {
fprintf(stderr," ** Warning: Infinite linked list detected\n");
status = 1;
break;
} else {
int dsize;
map[sector] = file_no;
name[sector] = filename;
next = (int)fbuf[data_next_low] + ((int)(0x3 & fbuf[data_next_high]) << 8);
sector_file_no = ((int)fbuf[data_file_num] >> 2);
if (sector_file_no != file_no) {
fprintf(stderr," ** Warning: Sector %d claims to belong to file %d\n", sector, sector_file_no);
status = 1;
if (fixit()) {
fbuf[data_file_num] = (fbuf[data_file_num] & 0x3) | (file_no << 2);
upd = 1;
}
}
dsize = (int)fbuf[data_bytes];
if (next) {
if (dsize != data_size) {
fprintf(stderr, " ** Warning: Sector %d is short\n", sector);
}
} else {
if (dsize == 0) {
fprintf(stderr, " ** Warning: Sector %d (last sector of file) is empty\n", sector);
}
}
}
if (upd) {
putsect(fbuf, sector);
fixes = 1;
upd = 0;
}
sector = next;
} while (sector);
if (count != sects) {
fprintf(stderr," ** Warning: size in directory (%d) does not match size on disk (%d) for file %s\n",
sects, count, filename);
status = 1;
if (fixit()) {
d->count_hi = (0xFF & (count >> 8));
d->count_lo = (0xFF & count);
upddir = 1;
}
}
printf(" Found %d sectors\n", count);
return upddir;
}
/* Check disk: regen bit map */
int do_check()
{
unsigned char bitmap[ED_BITMAP_SIZE];
unsigned char buf[DD_SECTOR_SIZE];
int x, y;
int total;
int ok;
int found_eod = 0;
char map[ED_DISK_SIZE];
char *name[ED_DISK_SIZE];
if (disk_size == ED_DISK_SIZE)
printf("Checking DOS 2.5 enhanced density disk...\n");
else if (disk_dd)
printf("Checking DOS 2.0d double density disk...\n");
else
printf("Checking DOS 2.0s single density disk...\n");
/* Mark all as free */
for (x = 0; x != ED_DISK_SIZE; ++x) {
map[x] = -1;
name[x] = 0;
}
/* Mark non-existent sector 0 as allocated */
map[0] = 64;
/* Mark VTOC and DIR */
map[SECTOR_VTOC] = 64;
for (x = SECTOR_DIR; x != SECTOR_DIR + SECTOR_DIR_SIZE; ++x)
map[x] = 64;