forked from dad4x/s4-3b1-pc7300
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths4lib.c
1710 lines (1357 loc) · 39.5 KB
/
s4lib.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
/*
* Library for safari4 for access, see s4lib.h
*/
/*
From SV fsck, we see that there are the following block layouts in the FS.
union {
char b_buf[BSIZE]; // buffer space
short b_lnks[SPERB]; // link counts
daddr32_t b_indir[NINDIR]; // indirect block
struct filsys32 b_fs; // super block
struct fblk32 b_fb; // free block
struct s4_dinode b_dinode[INOPB]; // inode block
struct direct32 b_dir[NDIRECT]; // directory
}
The SYSV filesystem is layed out differntly on different machines.
On some, block 0 is unused, or used as the bad-block table. When
so, the superblock is at block SUPERB(1), which is SUPERBOFF bytes
from the start.
The CTIX filesystem is at block 0 of the partition, with no offset?
We fudge this by considering the first 2 blocks presented when we
open a filesystem.
The inode table is continuous blocks starting 2 after the superblock;
If the superblock is at LBA 0, then the first inode is at LBA 3;.
If the superblock is at LBA 1, then the first inode is at LBA 4;
The file system blocks begin at FIXME.
From there, blocks are considered in FS block sized unita, and the
inode table begins at ... fixme.
Following the superblock are s_isize blocks for inodes.
The root inode of the filesystem is at ROOTINO, which is inode 2.
PROBLEMS:
There are bad block mappings that on the 3b1 are handled by the
operating system, transparently to the user. It does this
by reserving a space sector on each track, so if you have 17
physical sectors, you only get to use 16.
Something has to keep track of any bad blocks -- preferably
none (which we can arrange), but we do have to skip one block
every track.
If we're not properly mapping, then the LBA to PBA translation
is:
pba = lba + (lba % (sectors-per-track - 1)) - 1;
which adds an extra skipped block for every preceding track.
This is why it's good to have partitions be on track boundaries,
even if not on cylinder boundaries.
To have this in a file-system image file, we'll use block 0
as the BBT, and in the superblock, use dinfo[3] as
sectors per track. We'll have to construct this
when we rip a filesystem from a disk image.
We'll use the CTIX disk data structure for the BBT, but
we won't use their memory structures.
Wonder if we can have one track with 400k sectors?
Maybe have utility to add/delete the spare sectors
when going to/from native to emulated?
*/
#include <s4lib.h>
#include <time.h>
#include <errno.h>
#include <string.h> /* strerror */
#include <ctype.h>
/* ------------------------------------------- */
/* Types used here, but not visible to callers */
typedef struct {
int mask; /* mask to these bits */
int eqbits; /* require == these set */
const char *name;
} s4_maskname;
/* --------------------- */
/* Private constant data */
static const s4_maskname s4_typebits[] =
{
{ IFMT, IFDIR, "d" },
{ IFMT, IFCHR, "c" },
{ IFMT, IFBLK, "b" },
{ IFMT, IFREG, "-" },
{ IFMT, IFIFO, "f" },
{ 0, 0, NULL }, /* no match value */
};
static const s4_maskname s4_modebits[] =
{
{ 0, 0, "-" }, /* no match value */
{ 00400, 000400, "r" },
{ 00200, 000200, "w" },
{ 04100, 000100, "x" },
{ 04100, 004100, "S" },
{ 00040, 000040, "r" },
{ 00020, 000020, "w" },
{ 02010, 000010, "x" },
{ 02010, 020010, "S" },
{ 00004, 000004, "r" },
{ 00002, 000002, "w" },
{ 01001, 000001, "x" },
{ 01001, 010001, "t" },
{ 0, 0, NULL }
};
/* ----------------------------------------- */
/* Forward declarations of private functions */
static void s4_resdes_show( struct s4_resdes *map );
static void s4_vhbd_glorp_show( struct s4_vhbd *vhbd );
static void s4_vhbd_show( struct s4_vhbd *vhbd );
static void s4_disk_decode_partitions( s4_diskinfo *dinfo,
struct s4_vhbd *vhbd );
static void s4_disk_get_bbt( s4_diskinfo *dinfo );
static void s4_disk_bbt_show( s4_diskinfo *dinfo );
static int s4_checksum32( unsigned char *p, int len, int expect );
static void s4_maskname_show( const s4_maskname *bits, int mode );
static void s4_modes_show( int mode );
static s4err s4_filsys_good_super( s4_filsys *xfs );
/* -------------- */
/* Private macros */
// static s4err s4_disk_good_lba( s4_diskinfo *dinfo, int lba );
#define s4_disk_good_lba( d, lba ) \
( ((lba)>=0 && (lba) < (d)->nblocks) ? s4_ok : s4_range )
// static s4err s4_filsys_good_inode( s4_filsys *fs, int ino );
#define s4_filsys_good_inode( fs, ino ) \
( (((ino)>=0) && (ino) < (fs)->dfs->s_ninode) ? s4_ok : s4_range )
// static s4err s4_filsys_good_fsblk( s4_filsys *fs, int fsblk );
#define s4_filsys_good_fsblk( fs, fsblk ) \
( (((fsblk)>=0) && ((fsblk) < (fs)->dfs->s_fsize)) ? s4_ok : s4_range )
/* ================================================================ */
/* PUBLIC FUNCTIONS */
const char *s4errstr( s4err err )
{
const char *s = "bad s4err";
switch( err )
{
case s4_ok: s = "OK"; break;
case s4_error: s = "error"; break;
case s4_badmagic: s = "badmagic"; break;
case s4_open: s = "open"; break;
case s4_close: s = "close"; break;
case s4_seek: s = "seek"; break;
case s4_read: s = "read"; break;
case s4_write: s = "write"; break;
case s4_range: s = "range"; break;
default: break;
}
return s;
}
const char *s4btypestr( s4btype btype )
{
const char *s = "bad s4btype";
switch( btype )
{
case s4b_unk: s = "unknown"; break;
case s4b_vhbd: s = "vhbd"; break;
case s4b_bbt: s = "bbt"; break;
case s4b_super: s = "super"; break;
case s4b_ino: s = "ino"; break;
case s4b_idx: s = "indirect"; break;
case s4b_dir: s = "dir"; break;
case s4b_linkcnt: s = "linkcnt"; break;
case s4b_free: s = "free"; break;
case s4b_raw: s = "raw"; break;
case s4b_last_fs: s = "last_fs"; break;
default:
break;
}
return s;
}
int s4swapi( int i )
{
int rv = i;
#ifdef S4_LITTLE_ENDIAN
unsigned char *src = (unsigned char*)&i;
unsigned char *dst = (unsigned char*)&rv;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
#endif
return rv;
};
int s4swaph( int i )
{
int rv = i;
#ifdef S4_LITTLE_ENDIAN
unsigned char *src = (unsigned char*)&i;
unsigned char *dst = (unsigned char*)&rv;
dst[2] = 0;
dst[3] = 0;
dst[0] = src[1];
dst[1] = src[0];
#endif
return rv;
};
/* does work on LE machine */
int s4bei( int i )
{
#ifdef S4_LITTLE_ENDIAN
return s4swapi( i );
#else
return i;
#endif
}
int s4beh( int i )
{
#ifdef S4_LITTLE_ENDIAN
return s4swaph( i );
#else
return i;
#endif
}
void s4dump( char *b, size_t len, int absolute )
{
int i, j;
int left = len;
char *base = absolute ? b : 0;
int width = 32;
/* print header line */
if( absolute )
printf("Address ");
else
printf( len > 1024 ? " Dec Hex" : " Dec Hex" );
for( j = 0; j < width; j++ )
{
if( !(j % 8 ) )
printf(" ");
printf("%2x", j & 0xf );
}
printf(" ");
for( j = 0; j < width; j++ )
if( j & 0xf )
printf( "%x", j & 0xf );
else
printf( " " );
printf("\n");
/* print one line at a time until nothing left */
for( i = 0; left > 0 ; )
{
/* prefix */
if( absolute )
printf("%8p:", base + i );
else
printf(len > 1024 ? "%5d %04x" : "%4d %03x", i, i );
/* hex parts */
for( j = 0; j < width && j < left; j++ )
{
if( !(j % 8) )
printf(" ");
printf("%02x", (b[i + j]) & 0xff );
}
/* rest of hex part if short */
for( ; j < width; j++ )
{
if( !(j % 8) )
printf(" ");
printf(" ");
}
/* char part of line */
printf(" ");
for( j = 0; j < width && j < left; j++ )
{
printf("%c", s4showc( b[i + j] & 0xff) );
}
printf("\n");
left -= j;
i += j;
}
}
int s4_lba2pba( int lba, s4_bbt *bbt, int nbb, int lstrk )
{
int pba = lba + (lba/(lstrk)); /* one spare sector per track */
int npba;
if( nbb )
{
int track = lba / lstrk;
int hdsec = lba % lstrk;
int i, cylsec;
for( i = 0 ; i < nbb ; i++ )
{
cylsec = bbt[i].cyl;
if( !cylsec )
break;
if( track == (cylsec / (lstrk+1)) && hdsec == (cylsec % (lstrk+1)) )
{
npba = (track * (lstrk+1)) + 16;
printf("Mapping lba %d from pba %d to %d. BB cyl %d trk %d hdsec %d\n",
lba, pba, npba, cylsec, track, hdsec, pba, npba );
break;
}
}
}
return pba;
}
int s4_pba2lba( int pba, struct s4_bbe *bbt, int nbb, int strk, int heads )
{
int lba = pba - (pba/strk);
int hdsec = pba % strk;
/* if physical sector is the reserved one, we remapped it. */
if( hdsec == strk - 1 )
{
if( nbb )
{
int track = pba / strk;
int i, cylsec;
for( i = 0; i < nbb ; i++ )
{
cylsec = bbt[i].cyl;
if( track == (cylsec/strk) )
{
/* this is almost certainly wrong: test & FIXME */
lba = (track * (strk-1)) + ((cylsec % strk) % heads);
}
}
}
else
{
printf("Remapped PBA %d, but no bad block table!?\n", pba );
}
}
return lba;
}
/* initialize everything to "s4_disk_show"-able state.
Do not clear, because fd may be open, vhbd may already be read */
void s4_init_disk( const char *name, int fd,
int cyls, int heads, int secsz, int pscyl,
struct s4_vhbd *opt_vhbd,
s4_diskinfo *d )
{
d->fname = strdup( name );
d->fd = fd;
d->nbb = 0;
d->bbt = &d->bbt_fsu.bbt[0];
d->cyls = cyls;
d->heads = heads;
d->secsz = secsz;
d->pstrk = pscyl / heads;
d->lstrk = d->pstrk - 1;
d->pscyl = pscyl;
d->lscyl = d->pscyl - heads; /* less one per track */
d->pblks = d->cyls * d->pscyl;
d->lblks = d->cyls * d->lscyl;
d->ptrksz = d->pstrk * d->secsz;
d->ltrksz = d->lstrk * d->secsz;
d->pcylsz = d->pscyl * d->secsz;
d->lcylsz = d->lscyl * d->secsz;
d->loader_lba = 0;
d->loader_nblks = 0;
d->bbt_lba = 0;
d->bbt_nblks = 0;
d->bbt = &d->bbt_fsu.bbt[0];
d->nbb = 0;
/* setup fake partition at the start */
d->nparts = 1;
d->parts[0].strk = 0;
d->parts[0].ntrk = 1;
d->parts[0].pblks = d->pstrk;
d->parts[0].lblks = d->lstrk;
d->parts[0].partoff = 0;
d->parts[0].partlba = 0;
if( opt_vhbd )
{
/* vhbd.resmap units are in FS 1k blocks, convert to PBA */
d->loader_lba = opt_vhbd->resmap[0].blkstart * 2;
d->loader_nblks = opt_vhbd->resmap[0].nblocks * 2;
d->bbt_lba = opt_vhbd->resmap[1].blkstart * 2;
d->bbt_nblks = opt_vhbd->resmap[1].nblocks * 2;
/* take real partitions instead */
s4_disk_decode_partitions( d, opt_vhbd );
}
}
s4err s4_open_disk( const char *dfile, int mode, s4_diskinfo *dinfo )
{
s4err rv = s4_ok;
int fd;
memset( dinfo, 0, sizeof(*dinfo) );
fd = open( dfile, mode, 0 );
if( fd < 0 )
{
printf("Unable to open '%s': %s\n", dfile, strerror( errno ));
rv = s4_open;
goto done;
}
rv = s4_seek_read( fd, 0, (char*)&dinfo->vhbd, 512 );
if( s4_ok != rv )
goto done;
if( S4_VHBMAGIC == dinfo->vhbd.magic )
{
dinfo->doswap = 0;
}
else if( S4_VHBMAGIC == s4swapi( dinfo->vhbd.magic ) )
{
dinfo->doswap = 1;
s4_fsu_swap( (s4_fsu*)&dinfo->vhbd, s4b_vhbd );
}
else
{
s4_fsu_show( (s4_fsu*)&dinfo->vhbd, s4b_vhbd );
rv = s4_badmagic;
goto done;
}
s4_init_disk( dfile, fd,
dinfo->vhbd.dsk.cyls,
dinfo->vhbd.dsk.heads,
dinfo->vhbd.dsk.sectorsz,
dinfo->vhbd.dsk.pseccyl,
&dinfo->vhbd,
dinfo );
if( dinfo->pscyl != dinfo->vhbd.dsk.pseccyl )
{
printf("WARNING: computed sectors/cyl %d not reported %d\n",
dinfo->pscyl, dinfo->vhbd.dsk.pseccyl );
}
s4_disk_get_bbt( dinfo );
done:
return rv;
}
s4err s4_disk_import( s4_diskinfo *odinfo, int opnum, int ooffblks, int ifd )
{
int rv = 0;
s4err err = s4_ok;
s4_diskinfo *d = odinfo;
long offset;
int lbar, lbaa; /* relative and absolute blocks */
int partlba = d->parts[opnum].partlba;
int len;
char buf[ 512 ];
/* copy everything from the input fd to successive LBA's */
for( lbar = 0; lbar < d->parts[opnum].lblks && s4_ok == err; lbar++ )
{
len = read( ifd, buf, sizeof(buf));
if( len <= 0 )
{
if( len < 0 )
{
printf("read error %s\n", strerror(errno));
err = s4_read;
}
break;
}
lbaa = partlba + ooffblks + lbar;
offset = LBA_TO_DISK_OFFSET(d, lbaa);
err = s4_seek_write( odinfo->fd, offset, buf, len );
if( s4_ok != err )
{
printf("seek write error, offset %ld\n", offset );
break;
}
}
printf("Imported %d blocks into partition %d\n", lbar, opnum );
return err;
}
/* export cnt lba's from pnum starting at ioffblks to fd. */
s4err s4_disk_export( s4_diskinfo *idinfo, int ipnum, int ioffblks,
int icnt, int ofd )
{
s4_diskinfo *d = idinfo;
s4err err = s4_ok;
long offset;
int lbar, lbaa, rv;
long partlba = d->parts[ipnum].partlba;
char buf[ 512 ];
for( lbar = 0; lbar < icnt && s4_ok == err; lbar++ )
{
lbaa = partlba + ioffblks + lbar;
offset = LBA_TO_DISK_OFFSET(d, lbaa);
err = s4_seek_read( d->fd, offset, buf, sizeof(buf) );
if( s4_ok != err )
{
printf("seek read error, offset %d\n", offset );
break;
}
rv = write( ofd, buf, sizeof(buf) );
if( rv != sizeof(buf) )
{
printf("%s writing output\n", strerror(errno));
err = s4_write;
}
}
if( s4_ok == err )
printf("Exported %d blocks from partition %d\n", lbar, ipnum );
return err;
}
/* export icnt lba's from idinfo partion at ioffblks
to odinfo partition at ooffblks */
s4err s4_disk_transfer( s4_diskinfo *odinfo, int opnum, int ooffblks,
s4_diskinfo *idinfo, int ipnum, int ioffblks,
int icnt )
{
s4err err = s4_ok;
int lbar;
int ilbaa, ipartlba;
int olbaa, opartlba;
long ioffset, ooffset;
char buf[ 512 ];
ipartlba = idinfo->parts[ipnum].partlba;
opartlba = odinfo->parts[opnum].partlba;
for( lbar = 0; lbar < icnt && s4_ok == err; lbar++ )
{
ilbaa = ipartlba + ioffblks + lbar;
ioffset = LBA_TO_DISK_OFFSET(idinfo, ilbaa);
err = s4_seek_read( idinfo->fd, ioffset, buf, sizeof(buf) );
if( s4_ok != err )
{
printf("seek read error, offset %ld\n", ioffset );
break;
}
olbaa = opartlba + ooffblks + lbar;
ooffset = LBA_TO_DISK_OFFSET(odinfo, olbaa);
err = s4_seek_write( odinfo->fd, ooffset, buf, sizeof(buf) );
if( s4_ok != err )
{
printf("seek write error, offset %ld\n", ooffset );
break;
}
}
if( s4_ok == err )
printf("Transferred %d blocks\n", lbar );
return err;
}
void s4_disk_set_part( s4_diskinfo *dinfo, int pnum, int strk, int numtrk )
{
s4_part *part = &dinfo->parts[ pnum ];
part->strk = strk;
part->ntrk = numtrk;
part->pblks = numtrk * dinfo->pstrk;
part->lblks = numtrk * dinfo->lstrk;
part->partoff = TRK_TO_OFFSET( dinfo, strk );
part->partlba = TRK_TO_LBA( dinfo, strk );
}
static void s4_disk_decode_partitions( s4_diskinfo *dinfo,
struct s4_vhbd *vhbd )
{
int i;
int strk, ntrk, ltrk;
dinfo->nparts = 0;
for( i = 0; i < S4_MAXSLICE ; i++ )
{
strk = vhbd->partab[ i ].sz.strk;
if( i && !strk )
break;
/* if there is a next partition... */
if( i + 1 < S4_MAXSLICE )
ntrk = vhbd->partab[ i + 1 ].sz.strk;
else
ntrk = 0;
/* either have next track, or take to the end of the disk */
ltrk = ntrk ? ntrk - 1 : (dinfo->cyls * dinfo->heads) - 1;
/* ltrk == strk is a one track partition */
if( ltrk < strk )
{
printf("last track %d start %d ntrk %d in partition %d\n",
ltrk, strk, ntrk, i );
abort();
}
s4_disk_set_part( dinfo, i, strk, ltrk - strk + 1 );
}
dinfo->nparts = i;
}
static void s4_disk_get_bbt( s4_diskinfo *dinfo )
{
int rv, i;
dinfo->nbb = 0;
dinfo->bbt = &dinfo->bbt_fsu.bbt[0];
rv = s4_seek_read( dinfo->fd,
LBA_TO_DISK_OFFSET(dinfo, dinfo->bbt_lba ),
dinfo->bbt_fsu.buf,
sizeof(dinfo->bbt_fsu.buf));
if( s4_ok == rv )
{
if( dinfo->doswap )
s4_fsu_swap( &dinfo->bbt_fsu, s4b_bbt );
for( i = 0; i < S4_NBB && dinfo->bbt[i].cyl; i++ )
dinfo->nbb++;
}
}
void s4_vhbd_show( struct s4_vhbd *vhbd )
{
int i;
printf(" Magic %x %s %x, checksum %x (unchecked)\n",
vhbd->magic,
vhbd->magic == S4_VHBMAGIC ? "is" : "IS NOT",
S4_VHBMAGIC,
vhbd->chksum );
printf(" '%-6s' %d cyls, %d heads, %d sectors/track %d sec/cyl\n",
vhbd->dsk.name,
vhbd->dsk.cyls,
vhbd->dsk.heads,
vhbd->dsk.psectrk,
vhbd->dsk.pseccyl );
printf(" Partitions:\n");
for( i = 0; i < S4_MAXSLICE; i++ )
{
if( i && !vhbd->partab[i].sz.strk )
break;
else
printf(" [%d] start track %d\n", i, vhbd->partab[i].sz.strk );
}
printf("\n");
s4_resdes_show( vhbd->resmap );
}
void s4_disk_show( s4_diskinfo *dinfo )
{
int i;
s4_part *part;
printf("Showing disk header from '%s'\n"
"Magic %x %s %x, checksum %x (unchecked) %s\n",
dinfo->fname,
dinfo->vhbd.magic,
dinfo->vhbd.magic == S4_VHBMAGIC ? "is" : "IS NOT",
S4_VHBMAGIC,
dinfo->vhbd.chksum,
dinfo->doswap ? "SWAPPED" : "no swap needed" );
printf(" %d cyls, %d heads; %d s/trk, %d s/cyl; step %d, %d byte sectors\n",
dinfo->cyls,
dinfo->heads,
dinfo->pstrk,
dinfo->pscyl,
dinfo->vhbd.dsk.step,
dinfo->secsz );
printf(" %d blocks, %dk bytes %dM bytes\n",
dinfo->cyls * dinfo->pscyl,
dinfo->cyls * dinfo->pscyl * dinfo->secsz / 1024,
dinfo->cyls * dinfo->pscyl * dinfo->secsz / 1024 / 1024 );
s4_vhbd_glorp_show( &dinfo->vhbd );
printf("Loader is %d blocks at PBA %d\n",
dinfo->loader_nblks, dinfo->loader_lba );
printf("Bad Block Table is %d blocks at PBA %d\n",
dinfo->bbt_nblks, dinfo->bbt_lba );
printf("\nPartitions: (%d)\n", dinfo->nparts );
printf("Num STrk Ntrk PBA LBA Cyl Offset ");
printf("PSects PSize LSects LSize\n");
printf("--- ---- ---- ----- ----- ---- --------- ");
printf("------- --------------- ------- ---------------\n");
for( i = 0; i < dinfo->nparts; i++ )
{
part = &dinfo->parts[i];
printf("%3d %5d %5u %5u %5u %4u %10u %8d %6uk %6.2fM %8d %6uk %6.2fM\n",
i,
part->strk,
part->ntrk,
TRK_TO_PBA( dinfo, part->strk ),
TRK_TO_LBA( dinfo, part->strk ),
TRK_TO_CYL( dinfo, part->strk ),
PNUM_TO_OFFSET( dinfo, i ),
part->pblks,
part->pblks * dinfo->secsz / 1024,
((double)part->pblks) * dinfo->secsz / 1024 / 1024,
part->lblks,
part->lblks * dinfo->secsz / 1024,
((double)part->lblks) * dinfo->secsz / 1024 / 1024 );
}
printf("\nSpecial sections in part 0\n");
s4_resdes_show( dinfo->vhbd.resmap );
s4_disk_bbt_show( dinfo );
}
static void s4_resdes_show( struct s4_resdes *map )
{
int i;
printf(" Resmap:\n");
for( i = 0; i < 8 ; i++ )
{
if( map[i].blkstart )
{
printf(" [%d] at FSBLK %d, blocks %d\n",
i,
map[i].blkstart,
map[i].nblocks );
}
}
printf("\n");
}
s4err s4_disk_close( s4_diskinfo *dinfo )
{
s4err rv = s4_ok;
if( dinfo->fname )
free( dinfo->fname );
if( dinfo->fd > 0 )
{
if( close( dinfo->fd ) < 0 )
{
printf("%s closing fd %d\n", strerror(errno), dinfo->fd );
rv = s4_close;
}
dinfo->fd = -1;
}
memset( dinfo, 0, sizeof(*dinfo) );
return rv;
}
s4err s4_seek_read( int fd, int offset, char *buf, int blen )
{
s4err rv = s4_ok;
long off = lseek( fd, (long)offset, 0 );
if( off < 0 )
{
printf("%s seeking to offset %u in fs %d\n",
strerror( errno ), offset, fd );
rv = s4_seek;
}
else
{
off = read( fd, buf, blen );
if( off < 0 )
{
printf("%s at offset %ld wanted %d got %d\n",
strerror( errno ), offset, blen, off );
rv = s4_read;
}
}
#if 0
printf("seek_read LBA %d offset %d for %d returns %d %s\n",
offset / 512,
offset,
blen, rv, s4errstr(rv) );
#endif
return rv;
}
s4err s4_seek_write( int fd, int offset, char *buf, int blen )
{
s4err rv = s4_ok;
long off = lseek( fd, (long)offset, 0 );
if( off < 0 || off != offset )
{
printf("%s seeking to offset %u in fs %d\n",
strerror( errno ), offset, fd );
rv = s4_seek;
}
else
{
off = write( fd, buf, blen );
if( off < 0 || off != blen )
{
printf("%s writing at offset %d wanted %d got %ld\n",
strerror( errno ), offset, blen, off );
rv = s4_read;
}
}
#if 0
printf("seek_write LBA %d offset %d for %d returns %d %s\n",
offset / 512,
offset,
blen, rv, s4errstr(rv) );
#endif
return rv;
}
s4err s4_disk_open_filsys( s4_diskinfo *dinfo, int volnum, s4_filsys *fs )
{
s4err rv;
int offset;
int block;
int fd;
fs->dinfo = dinfo;
fs->part = &dinfo->parts[ volnum ];
fd = dinfo->fd;
/* FS should be at 512 bytes from 0 */
for( block = 1; block < 3; block++ )
{
offset = fs->part->partoff + PBA_TO_OFFSET( dinfo, block );
rv = s4_seek_read( fd, offset, fs->super.buf, 512 );
if( s4_ok != rv )
goto done;
fs->doswap = 0;
rv = s4_filsys_good_super( fs );
if( s4_ok == rv )
{
fs->bksz = fs->super.super.s_type == 2 ? 1024 : 512;
#if 0
printf("FS blocks start at LBAR %d\n", fs->super.super.s_isize);
#endif
break;
}
else
{
printf("FS Superblock at lbar %d offset %d is %s\n",
block, offset, s4errstr(rv) );
}
}
done:
return rv;
}
s4err s4_open_filsys( const char *path, s4_filsys *fs )
{
s4err rv;
s4_diskinfo *d = &fs->fakedisk;
struct stat sb;
/* open the file and get it's size */
d->fname = strdup( path );
d->fd = open( path, 002, 0 );
if( d->fd < 0 )
{
printf("%s opening file '%s'\n", strerror(errno), path );
rv = s4_open;
goto done;
}
if( fstat( d->fd, &sb ) < 0 )
{