-
Notifications
You must be signed in to change notification settings - Fork 1
/
cocofs.c
1519 lines (1334 loc) · 34.1 KB
/
cocofs.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
/*-
* Copyright (c) 2022 Jason R. Thorpe.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* cocofs -- A tool to interact with TRS-80 CoCo floppy disk images.
*
* cocofs has the following commands:
*
* ==> ls List the contents of a floppy disk.
*
* ==> copyout Copy one or more files from the floppy disk into the
* current working directory.
*
* ==> copyin Copy one or more files to the floppy disk. The type
* and encoding will be guessed for each file, based on
* the file extension; overriding on a per-file basis
* is possible using the following format for the file
* names:
*
* FOO.DAT[binary,data]
* HELLO.C[ascii,data]
*
* The following type qualifiers are allowed:
*
* basic
* data
* code
* text
*
* The following encoding qualifiers are allowed:
*
* binary
* ascii
*
* One or both qualifiers may be specified, and in any order.
* The default if the default type/encoding cannot be guessed,
* or if qualifiers are specified, is binary data.
*
* There is a slight danger that [ and ] are legitimate
* characters in the file name, but it's extremely unlikely
* because those keys don't exist on CoCo keyboard.
*
* ==> rm Remove one or more files from the floppy disk.
*
* ==> format Create a new floppy image.
*
* ==> dump Dump information about the floppy disk. This is
* essentially an enhanced version of the "ls" command
* that also shows information about the layout of the
* files on disk and shows additional information when
* disk format errors are encountered.
*/
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* We need to use O_BINARY on platforms that have it (Windows). */
#ifndef O_BINARY
#define O_BINARY 0
#endif
/*
* Format information is gleaned from:
*
* http://dragon32.info/info/tandydsk.html
*
* The CoCo DOS supports a single disk format:
*
* 1 head
* 35 tracks (0 - 34)
* 18 sectors per track (1 - 18)
* 256 bytes per sector
*
* Like other TRSDOS formats, there are 2 granules per track, meaning
* that a granule is group of 9 sectors. With 35 tracks, there are a
* total of 70 granules. The directory is stored in track 17, which
* leaves 68 granules for file data.
*
* The total size of the file system, including all metadata, is thus:
*
* 35*18*256 == 161280 == 157.5KiB
*
* ...and the size available for files is:
*
* 68*9*256 == 156672 == 153KiB
*/
#define COCOFS_TRACKS 35
#define COCOFS_SEC_PER_TRACK 18
#define COCOFS_SEC_PER_GRANULE 9
#define COCOFS_BYTES_PER_SEC 256
#define COCOFS_BYTES_PER_TRACK (COCOFS_SEC_PER_TRACK * COCOFS_BYTES_PER_SEC)
#define COCOFS_BYTES_PER_GRANULE \
(COCOFS_SEC_PER_GRANULE * COCOFS_BYTES_PER_SEC)
#define COCOFS_TOTALSIZE \
(COCOFS_TRACKS * COCOFS_SEC_PER_TRACK * COCOFS_BYTES_PER_SEC)
#define COCOFS_DIR_TRACK 17
#define COCOFS_GRANULES_PER_TRACK \
(COCOFS_SEC_PER_TRACK / COCOFS_SEC_PER_GRANULE)
#define COCOFS_NGRANULES \
((COCOFS_TRACKS - 1) * COCOFS_GRANULES_PER_TRACK)
static unsigned int
cocofs_track_to_offset(unsigned int track)
{
return track * (COCOFS_SEC_PER_TRACK * COCOFS_BYTES_PER_SEC);
}
static unsigned int
cocofs_sector_to_offset(unsigned int sector)
{
/* sectors are numbered 1 - 18 */
return (sector - 1) * COCOFS_BYTES_PER_SEC;
}
static unsigned int
cocofs_granule_to_track(unsigned int granule)
{
unsigned int track;
track = granule / COCOFS_GRANULES_PER_TRACK;
if (track >= COCOFS_DIR_TRACK) {
track++;
}
return track;
}
static unsigned int
cocofs_granule_to_offset(unsigned int granule)
{
unsigned int track;
unsigned int offset;
track = cocofs_granule_to_track(granule);
offset = cocofs_track_to_offset(track);
if (granule & 1) {
/* There are 2 granules per track. */
offset += COCOFS_BYTES_PER_GRANULE;
}
return offset;
}
/*
* CoCo DOS directory track:
*
* Sector 2 contains the Granule Map.
* Sectors 3 - 11 store the directory entries
*
* The Granule Map works more or less like the FAT in MS-DOS. Each byte
* represents one granule:
*
* 0 - 67 Points to the next granule in the file chain
* 0xc0 - 0xc9 This granule is the last granule in the chain. The lower
* 4 bits indicate how many sectors in the last granule are
* used. NOTE: This implies that 0xc0 should never be
* seen in the wild.
* 0xff This granule is free.
* ... Any other value indicates either a corrupt Granule Map
* entry, or a means of allocating space to "hidden" files.
*
* The diretctory entry format is 32 bytes long and is as follows:
*
* 0 - 7 file name (padded with ' ')
* 8 - 10 extension (padded with ' ')
* 11 file type:
* 0x00 Basic
* 0x01 Data
* 0x02 Machine code program
* 0x03 Text editor
*
* 12 encoding:
* 0x00 binary
* 0xff ASCII
* 13 first granule (0 - 67)
* 14 - 15 number of bytes used in last sector of file (!! big-endian !!)
* [remainder unused]
*/
#define GMAP_SECTOR 2
#define GMAP_FREE 0xff
#define GMAP_ALLOCATED 0xfe /* pseudo; see cocofs_galloc() */
#define GMAP_LAST 0xc0
#define GMAP_IS_LAST(v) (((v) & GMAP_LAST) == GMAP_LAST)
#define GMAP_LAST_NSEC(v) (((v) & 0x0f))
static bool
gmap_entry_is_valid(uint8_t v)
{
return (v <= COCOFS_NGRANULES) ||
(v >= 0xc0 && v <= 0xc9) ||
v == GMAP_FREE;
}
struct cocofs_dirent {
int8_t d_name[8];
int8_t d_ext[3];
uint8_t d_type;
uint8_t d_encoding;
uint8_t d_first_granule;
uint8_t d_last_bytes[2];
uint8_t d_unused[16];
};
#define COCOFS_DIRENT_TYPE_BASIC 0x00
#define COCOFS_DIRENT_TYPE_DATA 0x01
#define COCOFS_DIRENT_TYPE_CODE 0x02
#define COCOFS_DIRENT_TYPE_TEXT 0x03
#define COCOFS_DIRENT_TYPE_FREE 0xff
#define COCOFS_DIRENT_ENC_BINARY 0x00
#define COCOFS_DIRENT_ENC_ASCII 0xff
#define COCOFS_DIR_TRACK_FIRST_SEC 3
#define COCOFS_DIR_TRACK_LAST_SEC 11
#define COCOFS_DIR_TRACK_NSEC 9
#define COCOFS_DIR_TRACK_NENTRIES \
((COCOFS_DIR_TRACK_NSEC * COCOFS_BYTES_PER_SEC) / \
sizeof(struct cocofs_dirent))
/*
* In-memory representation of a CoCo DOS file system.
*/
struct cocofs {
int fd; /* file descriptor backing the image */
uint8_t *image_data; /* full image data */
uint8_t *granule_map; /* pointer to the Granule Map */
struct cocofs_dirent *directory;/* pointer to the directory */
unsigned int free_granules; /* # of free granules */
};
/*
* stat(2)-like information about a CoCo DOS file.
*/
struct cocofs_stat {
uint32_t st_size; /* total size */
char st_name[8+1]; /* name (null-terminated) */
char st_ext[3+1]; /* extension (null-terminated) */
uint8_t st_type; /* file type */
uint8_t st_encoding; /* file encoding */
};
static char
cocofs_mapchar(char c)
{
if (c >= 'a' && c <= 'z') {
return 'A' + (c - 'a');
}
return c;
}
static bool
cocofs_conv_name(const char *full, char name[8], char ext[3])
{
int i;
memset(name, ' ', 8);
memset(ext, ' ', 3);
for (i = 0; *full != '\0'; i++, full++) {
if (*full == '.') {
full++;
break;
}
if (i == 8) {
return false;
}
name[i] = cocofs_mapchar(*full);
}
for (i = 0; *full != '\0'; i++, full++) {
if (i == 3) {
return false;
}
ext[i] = cocofs_mapchar(*full);
}
return true;
}
static const char *
plural(long v)
{
return v == 1 ? "" : "s";
}
struct str2val {
const char *str;
unsigned int val;
};
static const struct str2val *
str2val_lookup_val(const struct str2val *tab, uint8_t val)
{
for (; tab->str != NULL; tab++) {
if (tab->val == val) {
return tab;
}
}
return NULL;
}
static const struct str2val *
str2val_lookup_str(const struct str2val *tab, const char *str)
{
for (; tab->str != NULL; tab++) {
if (strcasecmp(tab->str, str) == 0) {
return tab;
}
}
return NULL;
}
static const struct str2val cocofs_default_types_and_encodings[] = {
{ "ASM", (COCOFS_DIRENT_TYPE_DATA << 8) |
COCOFS_DIRENT_ENC_ASCII },
{ "BAS", (COCOFS_DIRENT_TYPE_BASIC << 8) |
COCOFS_DIRENT_ENC_BINARY },
{ "BIN", (COCOFS_DIRENT_TYPE_CODE << 8) |
COCOFS_DIRENT_ENC_BINARY },
{ "DAT", (COCOFS_DIRENT_TYPE_DATA << 8) |
COCOFS_DIRENT_ENC_BINARY },
{ "TXT", (COCOFS_DIRENT_TYPE_TEXT << 8) |
COCOFS_DIRENT_ENC_ASCII },
{ "C", (COCOFS_DIRENT_TYPE_DATA << 8) |
COCOFS_DIRENT_ENC_ASCII },
{ "H", (COCOFS_DIRENT_TYPE_DATA << 8) |
COCOFS_DIRENT_ENC_ASCII },
{ NULL, 0 },
};
static const struct str2val cocofs_dir_types[] = {
{ "Basic", COCOFS_DIRENT_TYPE_BASIC },
{ "Data", COCOFS_DIRENT_TYPE_DATA },
{ "Code", COCOFS_DIRENT_TYPE_CODE },
{ "Text", COCOFS_DIRENT_TYPE_TEXT },
{ NULL, 0 },
};
static const struct str2val cocofs_dir_encodings[] = {
{ "Binary", COCOFS_DIRENT_ENC_BINARY },
{ "ASCII", COCOFS_DIRENT_ENC_ASCII },
{ NULL, 0 },
};
static const char *
cocofs_dir_type(uint8_t t)
{
static char buf[sizeof("<type 0xff>")];
const struct str2val *tab;
tab = str2val_lookup_val(cocofs_dir_types, t);
if (tab != NULL) {
return tab->str;
}
snprintf(buf, sizeof(buf), "<type 0x%02x>", t);
return buf;
}
static const char *
cocofs_dir_encoding(uint8_t e)
{
static char buf[sizeof("<encoding 0xff>")];
const struct str2val *tab;
tab = str2val_lookup_val(cocofs_dir_encodings, e);
if (tab != NULL) {
return tab->str;
}
snprintf(buf, sizeof(buf), "<encoding 0x%02x>", e);
return buf;
}
static void
cocofs_default_type_and_encoding(const char *ext,
uint8_t *typep, uint8_t *encp)
{
const struct str2val *tab;
tab = str2val_lookup_str(cocofs_default_types_and_encodings, ext);
if (tab != NULL) {
*typep = tab->val >> 8;
*encp = tab->val & 0xff;
} else {
/* default to binary data. */
*typep = COCOFS_DIRENT_TYPE_DATA;
*encp = COCOFS_DIRENT_ENC_BINARY;
}
}
/* N.B. modifies fname. */
static bool
cocofs_parse_fname(char *fname, char name[8], char ext[3],
uint8_t *typep, uint8_t *encp)
{
char *qual1 = NULL, *qual2 = NULL;
char *cp1, *cp2;
size_t fnamelen;
bool have_type = false, have_enc = false;
uint8_t type = COCOFS_DIRENT_TYPE_DATA;
uint8_t enc = COCOFS_DIRENT_ENC_BINARY;
fnamelen = strlen(fname);
/*
* Check for qualifiers, separatre them from the file name
* and separators.
*/
cp2 = &fname[fnamelen - 1];
if (*cp2 == ']') {
for (cp1 = cp2; cp1 > fname; cp1--) {
if (*cp1 == '[') {
break;
}
}
if (cp1 > fname) {
/* We have qualifiers. */
assert(*cp1 == '[');
*cp1++ = '\0';
*cp2 = '\0';
qual1 = cp1;
cp2 = strchr(cp1, ',');
if (cp2 != NULL) {
*cp2++ = '\0';
qual2 = cp2;
}
}
}
/*
* fname is now separated from the qualifiers. Parse the qualifiers,
* if we have them.
*/
for (cp1 = qual1; cp1 != NULL; cp1 = (cp1 == qual1) ? qual2 : NULL) {
const struct str2val *tab;
if ((tab = str2val_lookup_str(cocofs_dir_types,
cp1)) != NULL) {
if (have_type) {
fprintf(stderr,
"multiple types specified for %s\n",
fname);
return false;
}
type = tab->val;
have_type = true;
} else if ((tab = str2val_lookup_str(cocofs_dir_encodings,
cp1)) != NULL) {
if (have_enc) {
fprintf(stderr,
"multiple encodings specified for %s\n",
fname);
return false;
}
enc = tab->val;
have_enc = true;
} else {
fprintf(stderr,
"unknown type/encoding qualifier for %s: %s\n",
fname, cp1);
return false;
}
}
/*
* Now convert the file name into the correct form. Look only
* at the final path component.
*/
cp1 = strrchr(fname, '/');
if (cp1 != NULL) {
cp1++;
} else {
cp1 = fname;
}
if (! cocofs_conv_name(cp1, name, ext)) {
fprintf(stderr,
"invalid file name: %s\n", fname);
return false;
}
/*
* If qualifiers were not specified, then try to guess based
* on the file name extension.
*/
if (!have_type && !have_enc && (cp2 = strchr(cp1, '.')) != NULL) {
cocofs_default_type_and_encoding(++cp2, &type, &enc);
}
*typep = type;
*encp = enc;
return true;
}
static uint16_t
cocofs_dir_lastbytes(const uint8_t *lastbytes)
{
return (lastbytes[0] << 8) | lastbytes[1];
}
static void
cocofs_dir_set_lastbytes(unsigned int cnt, uint8_t *lastbytes)
{
assert(cnt <= COCOFS_BYTES_PER_SEC);
lastbytes[0] = (uint8_t)(cnt >> 8);
lastbytes[1] = (uint8_t)cnt;
}
/*
* We provide our own versions of pread() and pwrite() in order to
* improve code portability.
*/
static ssize_t
cocofs_pread(int d, void *buf, size_t nbyte, off_t offset)
{
if (lseek(d, offset, SEEK_SET) == -1) {
return -1;
}
return read(d, buf, nbyte);
}
static ssize_t
cocofs_pwrite(int d, const void *buf, size_t nbyte, off_t offset)
{
if (lseek(d, offset, SEEK_SET) == -1) {
return -1;
}
return write(d, buf, nbyte);
}
static struct cocofs *
cocofs_alloc(int fd)
{
struct cocofs *fs = calloc(1, sizeof(*fs));
assert(fs != NULL);
/* Allocate a fresh, zero'd image. */
fs->image_data = calloc(1, COCOFS_TOTALSIZE);
assert(fs->image_data != NULL);
/* Cache pointers to Granule Map and directory. */
uint8_t *directory_track =
fs->image_data + cocofs_track_to_offset(COCOFS_DIR_TRACK);
fs->granule_map =
directory_track + cocofs_sector_to_offset(GMAP_SECTOR);
fs->directory = (struct cocofs_dirent *)
(directory_track +
cocofs_sector_to_offset(COCOFS_DIR_TRACK_FIRST_SEC));
fs->fd = fd;
return fs;
}
static void
cocofs_free(struct cocofs *fs)
{
free(fs->image_data);
free(fs);
}
static struct cocofs *
cocofs_format(int fd)
{
struct cocofs *fs = cocofs_alloc(fd);
/*
* Looking at several CoCo disk images, it appears that simply
* initializing the entire disk to 0xff's is sufficient. That
* marks all of the granule map entries as "FREE", and it appears
* to be what a free directory entry looks like, as well.
*
* As far as I can tell, CoCo disks to not have the separate
* granule allocation table in sector 1 of the directory track.
*/
memset(fs->image_data, 0xff, COCOFS_TOTALSIZE);
fs->free_granules = COCOFS_NGRANULES;
return fs;
}
static struct cocofs *
cocofs_load(int fd)
{
struct cocofs *fs = cocofs_alloc(fd);
struct stat sb;
ssize_t rsize, rv;
int i;
/* Get the size of the image. */
if (fstat(fd, &sb) == -1) {
fprintf(stderr, "ERROR: unable to stat image: %s\n",
strerror(errno));
cocofs_free(fs);
return NULL;
}
/*
* Reject images that are too large. Compensate for images
* that are too small (assume that the trailing tracks are
* simply left off).
*/
if (sb.st_size > COCOFS_TOTALSIZE) {
fprintf(stderr, "ERROR: image size %lld exceeds max size %u\n",
(long long)sb.st_size, COCOFS_TOTALSIZE);
cocofs_free(fs);
return NULL;
}
rsize = (ssize_t)sb.st_size;
if (sb.st_size < COCOFS_TOTALSIZE) {
fprintf(stderr,
"WARNING: image size %ld less than expected size %u\n",
(long)rsize, COCOFS_TOTALSIZE);
memset(fs->image_data, 0xff, COCOFS_TOTALSIZE);
}
/* Read in the image. */
rv = cocofs_pread(fd, fs->image_data, rsize, 0);
if (rv == -1) {
fprintf(stderr, "ERROR: unable to read image: %s\n",
strerror(errno));
cocofs_free(fs);
return NULL;
}
if (rv != rsize) {
fprintf(stderr,
"WARNING: read only %ld byte%s of image data, "
"expected %ld\n", (long)rv, plural(rv), (long)rsize);
}
for (i = 0; i < COCOFS_NGRANULES; i++) {
if (fs->granule_map[i] == GMAP_FREE) {
fs->free_granules++;
}
}
return fs;
}
static bool
cocofs_save(const struct cocofs *fs)
{
ssize_t rv;
rv = cocofs_pwrite(fs->fd, fs->image_data, COCOFS_TOTALSIZE, 0);
if (rv != COCOFS_TOTALSIZE) {
fprintf(stderr, "ERROR: unable to write image data: %s\n",
strerror(errno));
return false;
}
return true;
}
static void
cocofs_close(struct cocofs *fs)
{
close(fs->fd);
cocofs_free(fs);
}
static struct cocofs_dirent *
cocofs_lookup_raw(struct cocofs *fs, const char *name, const char *ext)
{
struct cocofs_dirent *dir;
unsigned int i;
for (i = 0; i < COCOFS_DIR_TRACK_NENTRIES; i++) {
dir = &fs->directory[i];
if (dir->d_type == COCOFS_DIRENT_TYPE_FREE) {
continue;
}
if (memcmp(dir->d_name, name, sizeof(dir->d_name)) != 0) {
continue;
}
if (memcmp(dir->d_ext, ext, sizeof(dir->d_ext)) != 0) {
continue;
}
break;
}
if (i < COCOFS_DIR_TRACK_NENTRIES) {
return dir;
}
return NULL;
}
static struct cocofs_dirent *
cocofs_lookup(struct cocofs *fs, const char *lookup)
{
char name[8], ext[3];
if (! cocofs_conv_name(lookup, name, ext)) {
return NULL;
}
return cocofs_lookup_raw(fs, name, ext);
}
static void
cocofs_stat(const struct cocofs *fs, const struct cocofs_dirent *dir,
struct cocofs_stat *st)
{
uint32_t size = 0;
uint32_t last_nsec = 0;
unsigned int loopcnt;
uint8_t g, gn;
for (loopcnt = 0, g = dir->d_first_granule;
loopcnt <= COCOFS_NGRANULES;
loopcnt++, g = gn) {
gn = fs->granule_map[g];
if (! gmap_entry_is_valid(gn) ||
gn == GMAP_FREE) {
break;
}
if (GMAP_IS_LAST(gn)) {
last_nsec = GMAP_LAST_NSEC(gn);
break;
}
size += (COCOFS_SEC_PER_GRANULE * COCOFS_BYTES_PER_SEC);
}
if (last_nsec) {
uint16_t lastsec_bytes;
size += last_nsec * COCOFS_BYTES_PER_SEC;
lastsec_bytes = cocofs_dir_lastbytes(dir->d_last_bytes);
if (lastsec_bytes > COCOFS_BYTES_PER_SEC) {
lastsec_bytes = COCOFS_BYTES_PER_SEC;
}
size -= (COCOFS_BYTES_PER_SEC - lastsec_bytes);
}
st->st_size = size;
int i;
memcpy(st->st_name, dir->d_name, sizeof(st->st_name));
st->st_name[8] = '\0';
for (i = 7; i >= 0 && st->st_name[i] == ' '; i--) {
st->st_name[i] = '\0';
}
memcpy(st->st_ext, dir->d_ext, sizeof(st->st_ext));
st->st_ext[3] = '\0';
for (i = 2; i >= 0 && st->st_ext[i] == ' '; i--) {
st->st_ext[i] = '\0';
}
st->st_type = dir->d_type;
st->st_encoding = dir->d_encoding;
}
static void
cocofs_print_stat(const struct cocofs_stat *st)
{
printf(" %-8s %-3s %6u byte%-1s (%s, %s)\n",
st->st_name, st->st_ext, st->st_size, plural(st->st_size),
cocofs_dir_type(st->st_type),
cocofs_dir_encoding(st->st_encoding));
}
static void
cocofs_enumerate_directory(struct cocofs *fs, bool do_dump)
{
struct cocofs_dirent *dir;
struct cocofs_stat st;
int nfiles = 0, gi;
unsigned int di;
unsigned int free_granules = COCOFS_NGRANULES;
unsigned int loopcnt;
uint16_t lastbytes;
uint8_t g, gn;
uint8_t gmap_shadow[COCOFS_NGRANULES];
memset(gmap_shadow, 0xff, sizeof(gmap_shadow));
printf("\n");
for (di = 0; di < COCOFS_DIR_TRACK_NENTRIES; di++) {
dir = &fs->directory[di];
if (dir->d_type > COCOFS_DIRENT_TYPE_TEXT) {
if (do_dump) {
printf("%2d: entry type 0x%02x, skipping.\n",
di, dir->d_type);
}
continue;
}
nfiles++;
cocofs_stat(fs, dir, &st);
cocofs_print_stat(&st);
if (! do_dump) {
continue;
}
/* Chase the granule list for this file. */
for (gi = 0, g = dir->d_first_granule, loopcnt = 0;
loopcnt <= COCOFS_NGRANULES;
gi++, g = gn) {
if (g >= COCOFS_NGRANULES) {
printf("\tINVALID GRANULE #%d: %d\n",
gi, g);
break;
}
if (gmap_shadow[g] != 0xff) {
printf("\tGRANULE %d ALREADY ALLOCATED "
"TO FILE %d\n", g, di);
} else {
assert(free_granules != 0);
free_granules--;
gmap_shadow[g] = di;
}
gn = fs->granule_map[g];
if (! gmap_entry_is_valid(gn)) {
printf("\tINVALID GRANULE MAP ENTRY "
"%2d: %d -> 0x%02x\n", gi, g, gn);
break;
}
if (GMAP_IS_LAST(gn)) {
printf("\tGranule %2d: %d (last, nsec=%d)\n",
gi, g, GMAP_LAST_NSEC(gn));
break;
} else {
printf("\tGranule %2d: %d\n",
gi, g);
}
g = gn;
}
if (loopcnt > COCOFS_NGRANULES) {
printf("\tGRANULE LIST CYCLE DETECTED\n");
}
lastbytes = cocofs_dir_lastbytes(dir->d_last_bytes);
printf("\tBytes in last sector: %u (0x%02x 0x%02x)\n",
lastbytes,
dir->d_last_bytes[0], dir->d_last_bytes[1]);
}
if (nfiles) {
printf("\n");
}
if (! do_dump) {
free_granules = fs->free_granules;
}
printf("%d file%s, %u granule%s (%u bytes) free\n",
nfiles, plural(nfiles),
free_granules, plural(free_granules),
free_granules * COCOFS_SEC_PER_GRANULE * COCOFS_BYTES_PER_SEC);
if (do_dump && free_granules != fs->free_granules) {
printf("WARNING: FREE GRANULES LOADED %u != COMPUTED %u\n",
fs->free_granules, free_granules);
}
}
static bool
cocofs_rm(struct cocofs *fs, struct cocofs_dirent *dir)
{
unsigned int gi;
uint8_t g, gn;
for (gi = 0, g = dir->d_first_granule;; gi++, g = gn) {
/* No need to detect cycles here, because we'll break them. */
if (g >= COCOFS_NGRANULES) {
fprintf(stderr, "INVALID GRANULE #%d: %d\n",
gi, g);
return false;
}
gn = fs->granule_map[g];
if (! gmap_entry_is_valid(gn) ||
gn == GMAP_FREE) {
printf("INVALID GRANULE MAP ENTRY "
"%2d: %d -> 0x%02x\n", gi, g, gn);
return false;
}
fs->granule_map[g] = GMAP_FREE;
fs->free_granules++;
if (GMAP_IS_LAST(gn)) {
break;
}
}
memset(dir, 0xff, sizeof(*dir));
return true;
}
static bool
cocofs_copyout(const struct cocofs *fs, const struct cocofs_dirent *dir,
const char *outfname)
{
unsigned int loopcnt;
unsigned int last_nsec = 0;
uint16_t last_nbytes;
unsigned int offset;
unsigned int gi;
ssize_t rv;
uint8_t g, gn;
int outfd;
outfd = open(outfname, O_WRONLY | O_CREAT | O_BINARY, 0644);
if (outfd == -1) {
fprintf(stderr, "unable to open output file %s: %s\n",
outfname, strerror(errno));
return false;
}
for (gi = 0, g = dir->d_first_granule, loopcnt = 0;; gi++, g = gn) {
if (loopcnt > COCOFS_NGRANULES) {
fprintf(stderr, "GRANULE MAP CYCLE DETECTED\n");
goto bad;
}
if (g >= COCOFS_NGRANULES) {
fprintf(stderr, "INVALID GRANULE #%d: %d\n",
gi, g);
goto bad;
}
gn = fs->granule_map[g];
if (! gmap_entry_is_valid(gn) ||
gn == GMAP_FREE) {
printf("INVALID GRANULE MAP ENTRY "
"%2d: %d -> 0x%02x\n", gi, g, gn);
goto bad;
}
if (GMAP_IS_LAST(gn)) {
last_nsec = GMAP_LAST_NSEC(gn);
break;
} else {
/* Write out a full granule. */
offset = cocofs_granule_to_offset(g);
rv = write(outfd, fs->image_data + offset,
COCOFS_BYTES_PER_GRANULE);
if (rv != COCOFS_BYTES_PER_GRANULE) {
fprintf(stderr, "error writing %s: %s\n",
outfname, strerror(errno));
goto bad;
}
}
g = gn;
}
if (last_nsec < 1 || last_nsec > COCOFS_SEC_PER_GRANULE) {
fprintf(stderr, "UNEXPECTED LAST_NSEC %u\n", last_nsec);