-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixtool.c
815 lines (723 loc) · 20.3 KB
/
unixtool.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
/*
* TI/LMI SYSV disk tool
*
* Copyright 2019
* Daniel Seagraves <[email protected]>
*
* Copyright 2022
* Modified for TI S1500 by Jeffrey H. Johnson <[email protected]>
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
int disk_fd = -1; /* FD for band image */
int file_fd = -1; /* FD for source/target file */
char *disk_fname = NULL; /* Band image filename */
uint8_t DISK_BLOCK[1024]; /* One disk block */
/* SYSV (68K) SUPERBLOCK - ON-DISK STRUCTURE */
/* ALL WORDS NEED BYTE-SWAPPED */
typedef struct rSuperBlock
{
uint16_t isize; /* SIZE OF BLOCKS IN I-LIST */
uint32_t fsize; /* SIZE OF BLOCKS IN THE VOLUME */
uint16_t nfree; /* NUMBER OF ADDRESSES IN FREE LIST */
uint32_t free[50]; /* FREE BLOCK LIST */
uint16_t ninode; /* INODES IN FREE INODE LIST */
uint16_t inode[100]; /* FREE INODE LIST */
uint8_t flock; /* FREE BLOCK LIST UPDATE LOCK */
uint8_t ilock; /* FREE INODE LIST UPDATE LOCK */
uint8_t fmod; /* SUPERBLOCK MODIFIED FLAG */
uint8_t readonly; /* READ-ONLY MOUNT FLAG */
uint32_t time; /* TIMESTAMP OF LAST SUPERBLOCK UPDATE */
uint16_t dinfo[4]; /* DEVICE INFORMATION (?) */
uint32_t tfree; /* TOTAL FREE BLOCKS */
uint16_t tinode; /* TOTAL FREE INODES */
char fname[6]; /* FILESYSTEM NAME */
char fpack[6]; /* DISK PACK NAME */
uint8_t fill[572]; /* FILL BYTES */
uint32_t magic; /* MAGIC NUMBER */
uint32_t type; /* FILESYSTEM TYPE */
} __attribute__ (( packed )) SuperBlock;
uint8_t superblock_buffer[1024]; /* BUFFER FOR HOLDING SUPERBLOCK */
SuperBlock *superblock = (SuperBlock *)&superblock_buffer;
/* INODE (on-disk representation) */
/* ALL WORDS NEED BYTE-SWAPPED */
typedef struct rInodeODR
{
uint16_t mode; /* MODE AND TYPE OF FILE */
uint16_t nlink; /* NUMBER OF LINKS TO HERE */
uint16_t uid;
uint16_t gid;
uint32_t size;
uint8_t addr[40]; /* DISK ADDRESS BYTES */
uint32_t atime; /* LAST ACCESS TIMESTAMP */
uint32_t mtime; /* LAST MODIFICATION TIMESTAMP */
uint32_t ctime; /* CREATION TIMESTAMP */
} __attribute__ (( packed )) InodeODR;
/* Inode (in-memory representation) */
typedef struct rInode
{
uint16_t mode; /* MODE BITS */
uint16_t type; /* TYPE BITS */
uint16_t nlink; /* NUMBER OF LINKS TO HERE */
uint16_t uid;
uint16_t gid;
uint32_t size;
uint32_t addr[13]; /* DISK ADDRESSES */
time_t atime; /* LAST ACCESS TIMESTAMP */
time_t mtime; /* LAST MODIFICATION TIMESTAMP */
time_t ctime; /* CREATION TIMESTAMP */
} Inode;
/* Inode file types */
#define INODE_FT_FIFO 1
#define INODE_FT_CHAR 2
#define INODE_FT_DIR 4
#define INODE_FT_BLK 6
#define INODE_FT_FILE 8
/* DIRECTORY ENTRY */
typedef struct rDirent
{
uint16_t inode;
unsigned char name[14];
} __attribute__ (( packed )) Dirent;
/* SWAP BYTES OF 32-BIT WORD */
uint32_t
swap_word(uint32_t in)
{
uint32_t out = (( in & 0xFF000000 ) >> 24 );
out |= (( in & 0x00FF0000 ) >> 8 );
out |= (( in & 0x0000FF00 ) << 8 );
out |= (( in & 0x000000FF ) << 24 );
return out;
}
uint16_t
swap_hword(uint16_t in)
{
uint32_t out = (( in & 0x00FF ) << 8 );
out |= (( in & 0xFF00 ) >> 8 );
return out;
}
/* READ INODE */
int
read_inode(int number, Inode *inode)
{
/* Read the given inode */
ssize_t io_res;
off_t seek_res;
off_t inode_disk_offset = 0x7C0;
int x = 0;
InodeODR raw_inode;
inode_disk_offset += ( number * 0x40 );
/* printf("read_inode(%d): diskaddr 0x%.8llx\n",number,inode_disk_offset); */
seek_res = lseek(disk_fd, inode_disk_offset, SEEK_SET);
if (seek_res < 0)
{
/* Seek error! */
perror("read_inode():lseek()");
return -1;
}
/* Read raw inode */
io_res = read(disk_fd, (uint8_t *)&raw_inode, 0x40);
if (io_res < 0)
{
/* Read error! */
perror("read_inode():read()");
return -1;
}
/* Read in inode particulars */
inode->mode = ( raw_inode.mode & 0xFF00 ) >> 8;
inode->mode |= (( raw_inode.mode & 0x000F ) << 8 );
inode->type = ( raw_inode.mode & 0x00F0 ) >> 4;
inode->nlink = swap_hword(raw_inode.nlink);
inode->uid = swap_hword(raw_inode.uid);
inode->gid = swap_hword(raw_inode.gid);
inode->size = swap_word(raw_inode.size);
inode->atime = swap_word(raw_inode.atime);
inode->mtime = swap_word(raw_inode.mtime);
inode->ctime = swap_word(raw_inode.ctime);
/*
* printf("inode %d: type %o mode %.5o owner %.6o:%.6o size %d\n",
* number,inode->type,inode->mode,inode->uid,inode->gid,inode->size);
*/
/* Read in block addresses */
while (x < 13)
{
inode->addr[x] = ( raw_inode.addr[x * 3] << 16 );
inode->addr[x] |= ( raw_inode.addr[x * 3 + 1] << 8 );
inode->addr[x] |= raw_inode.addr[x * 3 + 2];
/*
* if (inode->addr[x] > 0) {
* printf("block %d: %.6x\n",x,inode->addr[x]); }
*/
x++;
}
/* Done */
return 0;
}
int
disk_block_read(int adr, uint8_t *buf)
{
ssize_t io_res; /* Result of read/write operations */
/* Reposition the file pointer. */
off_t seek_res = lseek(disk_fd, ( adr * 0x400 ), SEEK_SET);
if (seek_res < 0)
{
/* Seek error! */
perror("unixtool: disk lseek()");
return -1;
}
/* Read in a sector. */
io_res = read(disk_fd, buf, 1024);
if (io_res < 0)
{
perror("unixtool: disk read()");
return -1;
}
return io_res;
}
int
inode_block_read(int adr, Inode *inode, uint8_t *buf)
{
int block = 0;
int rv = 0;
if (adr < 10)
{
/* Direct */
block = inode->addr[adr];
if (block > 0)
{
printf("inode_block_read(%d) => disk_block_read(%d)\n", adr, block);
return disk_block_read(block, buf);
}
else
{
return 0; /* EOF */
}
}
else
{
if (adr < 266)
{
/* One-level indirection */
uint32_t indirect_block[256];
int indirect_offset = ( adr - 10 );
rv = disk_block_read(inode->addr[10], (uint8_t *)indirect_block);
if (rv < 0)
{
return rv;
}
block = swap_word(indirect_block[indirect_offset]);
printf(
"inode_block_read(%d) => %d(%d) => disk_block_read(%d)\n",
adr,
inode->addr[10],
indirect_offset,
block);
return disk_block_read(block, buf);
}
if (adr < 65802)
{
/* Two-level indirection */
uint32_t first_indirect_block[256];
int first_indirect_offset = ( adr - 266 ) / 256;
uint32_t second_indirect_block[256];
int second_indirect_offset = ( adr - 266 ) - ( first_indirect_offset * 256 );
rv = disk_block_read(inode->addr[11], (uint8_t *)first_indirect_block);
if (rv < 0)
{
return rv;
}
rv = disk_block_read(
swap_word(first_indirect_block[first_indirect_offset]),
(uint8_t *)second_indirect_block);
if (rv < 0)
{
return rv;
}
block = swap_word(second_indirect_block[second_indirect_offset]);
printf(
"inode_block_read(%d) => %d(%d) => %d(%d) => disk_block_read(%d)\n",
adr,
inode->addr[11],
first_indirect_offset,
swap_word(first_indirect_block[first_indirect_offset]),
second_indirect_offset,
block);
return disk_block_read(block, buf);
}
printf("Further indirection required\n");
return -1;
}
/* Shouldn't get here! */
printf("inode_block_read(): Fell off end!\n");
return -1;
}
int
unix_ls(char *path)
{
/* List the given path */
Inode dir_inode;
Inode next_dir_inode;
char *pathpart;
uint8_t dir_buffer[1024];
int x = 0; /* index into directory */
int dirblock = 0; /* Block index into directory inode */
int rv = 0;
int done = 0;
if (path[0] != '/')
{
printf("unixtool: ls: Invalid path\n");
return -1;
}
/* Load the root directory */
rv = read_inode(2, &dir_inode);
if (rv < 0)
{
return rv;
}
/* Walk path */
pathpart = strtok(path, "/");
while (pathpart != NULL)
{
uint16_t dir_inode_number = 0;
printf("pathpart: %s\n", pathpart);
done = 0;
while (!done)
{
Dirent *dir_entry = (Dirent *)&dir_buffer;
rv = inode_block_read(dirblock, &dir_inode, dir_buffer);
if (rv < 0)
{
return rv;
}
if (rv == 0)
{
return 0;
} /* EOF, we are done */
x = 0;
dir_inode_number = 0;
while (dir_entry->inode != 0 && x < 64)
{
/* Stuff */
if (strlen(pathpart) > 14)
{
/* Very funny. */
printf("unixtool: No such file or directory (in image).\n");
return 0;
}
/* printf("ent %d: %s\n",x,dir_entry->name); */
if (strncmp(pathpart, (const char *)dir_entry->name, 14) == 0)
{
/* FOUND! */
rv = read_inode(swap_hword(dir_entry->inode), &next_dir_inode);
if (rv < 0)
{
return rv;
}
if (next_dir_inode.type == INODE_FT_DIR)
{
/* Subdirectory */
dir_inode_number = swap_hword(dir_entry->inode);
/* printf("SUBDIRECTORY at %.4X\n",dir_inode_number); */
rv = read_inode(dir_inode_number, &dir_inode);
if (rv < 0)
{
printf("subdirectory read_inode() blew it!\n");
return rv;
}
dirblock = 0;
}
done = 1;
break;
}
dir_entry++;
x++;
}
if (x < 64)
{
/* We are done! */
done = 1;
}
else
{
/* Read next block */
dirblock++;
}
}
if (dir_inode_number == 0)
{
printf(
"unixtool: No such file or directory (in image, path search).\n");
return -1;
}
pathpart = strtok(NULL, "/");
}
/* Print loaded directory */
done = 0;
printf("%s:\n", path);
while (!done)
{
Inode file_inode;
Dirent *dir_entry = (Dirent *)&dir_buffer;
rv = inode_block_read(dirblock, &dir_inode, dir_buffer);
if (rv < 0)
{
return rv;
}
if (rv == 0)
{
return 0;
} /* EOF, we are done */
x = 0;
while (dir_entry->inode != 0 && x < 64)
{
char permbuf[10] = "----------";
char modtimebuf[32] = "";
struct tm *modtime;
rv = read_inode(swap_hword(dir_entry->inode), &file_inode);
if (rv < 0)
{
return rv;
}
if (file_inode.type == INODE_FT_DIR)
{
permbuf[0] = 'd';
} /* Directory */
if (file_inode.type == INODE_FT_CHAR)
{
permbuf[0] = 'c';
} /* Char special */
if (file_inode.type == INODE_FT_BLK)
{
permbuf[0] = 'b';
} /* Block special */
if (file_inode.type == INODE_FT_FIFO)
{
permbuf[0] = 'p';
} /* FIFO */
if (file_inode.mode & 0400)
{
permbuf[1] = 'r';
}
if (file_inode.mode & 0200)
{
permbuf[2] = 'w';
}
if (file_inode.mode & 0100)
{
permbuf[3] = 'x';
}
if (file_inode.mode & 0040)
{
permbuf[4] = 'r';
}
if (file_inode.mode & 0020)
{
permbuf[5] = 'w';
}
if (file_inode.mode & 0010)
{
permbuf[6] = 'x';
}
if (file_inode.mode & 0004)
{
permbuf[7] = 'r';
}
if (file_inode.mode & 0002)
{
permbuf[8] = 'w';
}
if (file_inode.mode & 0001)
{
permbuf[9] = 'x';
}
modtime = localtime(&file_inode.mtime);
if (modtime == NULL)
{
printf("localtime() blew it!\n");
return -1;
}
if (strftime(modtimebuf, 31, "%b %e %Y", modtime) == 0)
{
printf("strftime() blew it!\n");
return -1;
}
printf(
"%s %2d %.6o %.6o %7d %s %s\n",
permbuf,
file_inode.nlink,
file_inode.uid,
file_inode.gid,
file_inode.size,
modtimebuf,
dir_entry->name);
dir_entry++;
x++;
}
if (x < 64)
{
/* We are done! */
done = 1;
}
else
{
/* Read next block */
dirblock++;
}
}
return 0;
}
int
unix_read(char *path, char *filename)
{
/* Read path (from image) into filename (on host) */
Inode dir_inode;
Inode file_inode;
char *pathpart;
uint8_t dir_buffer[1024];
uint8_t file_buffer[1024];
unsigned int x = 0; /* index into directory */
int dirblock = 0; /* Block index into directory inode */
int fileblock = 0; /* Block index into source file */
int rv = 0;
int done = 0;
uint16_t dir_inode_number = 0;
uint16_t file_inode_number = 0;
/* Fast checks */
if (path[0] != '/')
{
printf("unixtool: read: Invalid path\n");
return -1;
}
/* Open target */
file_fd = open(filename, O_RDWR | O_CREAT, 0660);
if (file_fd < 0)
{
perror("unixtool:open()");
return -1;
}
/* Load the root directory */
rv = read_inode(2, &dir_inode);
if (rv < 0)
{
return rv;
}
/* Walk path */
pathpart = strtok(path, "/");
while (pathpart != NULL)
{
if (file_inode_number != 0)
{
/* We already found a regular file? Something went wrong. */
printf(
"unixtool: No such file or directory (found regular file early "
"in image).\n");
return 0;
}
printf("pathpart: %s\n", pathpart);
done = 0;
while (!done)
{
Dirent *dir_entry = (Dirent *)&dir_buffer;
rv = inode_block_read(dirblock, &dir_inode, dir_buffer);
if (rv < 0)
{
return rv;
}
if (rv == 0)
{
return 0;
} /* EOF, we are done */
x = 0;
dir_inode_number = 0;
while (dir_entry->inode != 0 && x < 64)
{
/* Stuff */
if (strlen(pathpart) > 14)
{
/* Very funny. */
printf("unixtool: No such file or directory (in image).\n");
return 0;
}
/* printf("ent %d: %s\n",x,dir_entry->name); */
if (strncmp(pathpart, (const char *)dir_entry->name, 14) == 0)
{
/* FOUND! */
rv = read_inode(swap_hword(dir_entry->inode), &file_inode);
if (rv < 0)
{
return rv;
}
if (file_inode.type == INODE_FT_DIR)
{
/* Subdirectory */
dir_inode_number = swap_hword(dir_entry->inode);
/* printf("SUBDIRECTORY at %.4X\n",dir_inode_number); */
rv = read_inode(dir_inode_number, &dir_inode);
if (rv < 0)
{
return rv;
}
dirblock = 0;
}
if (file_inode.type == INODE_FT_FILE)
{
/* Regular file */
file_inode_number = swap_hword(dir_entry->inode);
}
done = 1;
break;
}
dir_entry++;
x++;
}
if (x < 64)
{
/* We are done! */
done = 1;
}
else
{
/* Read next block */
dirblock++;
}
}
if (dir_inode_number == 0 && file_inode_number == 0)
{
printf(
"unixtool: No such file or directory (in image, path search).\n");
return -1;
}
/* Otherwise obtain next part and carry on */
pathpart = strtok(NULL, "/");
}
if (file_inode_number == 0)
{
printf("unixtool: No such file or directory (in image, file read).\n");
return 0;
}
/* Do the deed! */
printf("Copying %d bytes\n", file_inode.size);
x = 0;
while (x < file_inode.size)
{
int osize = 1024;
if (( file_inode.size - x ) < 1024)
{
osize = file_inode.size - x;
}
rv = inode_block_read(fileblock, &file_inode, file_buffer);
if (rv < 0)
{
return rv;
}
if (rv == 0)
{
/* EOF, we are done early? */
printf("unixtool: Unexpected end-of-file\n");
return -1;
}
rv = write(file_fd, file_buffer, osize);
if (rv < 0)
{
perror("unixtool:write()");
return -1;
}
x += osize;
fileblock++;
}
printf("Wrote %d of %d bytes\n", x, file_inode.size);
close(file_fd);
return 0;
}
int
main(int argc, char *argv[])
{
int rv = 0;
/* Sigh. */
if (sizeof ( SuperBlock ) != 1024)
{
printf("SOMETHING IS BROKEN: SUPERBLOCK SIZE %ld\n", sizeof ( SuperBlock ));
return -1;
}
if (argc < 2 || strncmp(argv[1], "help", 4) == 0
|| strncmp(argv[1], "-?", 2) == 0)
{
/* Handle command-line options */
printf("TI/LMI unixtool v0.0.1\n\n");
printf(
"Usage: unixtool <command> <image file> [parameters]...\n\n");
printf(" Commands:\n\n");
printf(" help Prints this information\n\n");
printf(" ls Lists the given directory\n");
printf(" Parameters: <directory>\n\n");
printf(" read Copy path from image file to destination\n");
printf(" Parameters: <source path> <destination>\n\n");
return 0;
}
if (argc < 3)
{
printf(
"unixtool: Band image file name is required; See \"unixtool "
"help\" for usage information.\n");
return -1;
}
/* We have a disk filename, so open it. */
disk_fname = argv[2];
disk_fd = open(disk_fname, O_RDWR);
if (disk_fd < 0)
{
perror("unixtool: disk open()");
return -1;
}
/* Read in and check superblock */
rv = disk_block_read(1, superblock_buffer);
if (rv < 0)
{
return rv;
}
if (superblock->magic != 0x207E18FD)
{
printf(
"unixtool: Bad superblock magic: Expected 0x207E18FD, got 0x%.8X\n",
superblock->magic);
return -1;
}
/* Select option (or bail) */
if (argc >= 3)
{
if (strncmp(argv[1], "ls", 2) == 0)
{
if (argc < 4)
{
printf("unixtool: ls: directory path is required\n");
return -1;
}
return unix_ls(argv[3]);
}
if (strncmp(argv[1], "read", 4) == 0)
{
if (argc < 4)
{
printf("unixtool: read: source path is required\n");
return -1;
}
if (argc < 5)
{
printf("unixtool: read: destination path is required\n");
return -1;
}
return unix_read(argv[3], argv[4]);
}
printf(
"unixtool: Unknown parameters; See \"unixtool help\" for usage "
"information.\n");
return -1;
}
}