-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstandardscan.c
3159 lines (2690 loc) · 69.4 KB
/
standardscan.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
/* $Id: standardscan.c 49096 2014-07-01 18:48:17Z twilen $
* $Log: standardscan.c $
* Revision 2.10 1999/09/11 16:45:50 Michiel
* Versie 1.5 with Unformat and Repair nodos
*
* Revision 2.9 1999/09/10 22:14:49 Michiel
* Bugfixes etc (1.4)
*
* Revision 2.8 1999/08/01 10:48:21 Michiel
* less verbose
*
* Revision 2.7 1999/05/28 05:07:33 Michiel
* Fixed bug occuring on empty directory blocks
* Added rbl.always fix; improved rbl.disksize fix
* Reduced cachesize
*
* Revision 2.6 1999/05/17 10:32:39 Michiel
* long filename support, verbose fixes
*
* Revision 2.5 1999/05/17 09:27:11 Michiel
* fixed logfile bug
* made verbose less verbose
*
* Revision 2.4 1999/05/07 16:49:00 Michiel
* bugfixes etc
*
* Revision 2.3 1999/05/04 17:59:09 Michiel
* check mode, logfile, search rootblock implemented
* bugfixes
*
* Revision 2.1 1999/04/30 12:17:58 Michiel
* Accepts OK disks, bitmapfix and hardlink fix works
*
* Revision 1.2 1999/04/22 15:23:50 Michiel
* compiled
*
* Revision 1.1 1999/04/19 22:16:53 Michiel
* Initial revision
*
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <devices/trackdisk.h>
#include <libraries/asl.h>
#include <dos/dosextens.h>
#include <dos/filehandler.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <proto/asl.h>
#include <proto/exec.h>
#include "pfs3.h"
#include "doctor.h"
long __stack = 30*1024;
extern struct Window *CheckRepairWnd;
/**************************************
* Globals
**************************************/
struct
{
/* state flags */
uint32 flags; /* internal flags */
uint32 opties; /* options */
int pass;
BOOL verbose;
BOOL unformat;
BOOL info;
enum {syntax, resbitmap, mainbitmap, anodebitmap, finished} stage;
struct MinList *doubles;
} ss;
volume_t volume;
char bericht[256];
rootblock_t *rbl= NULL;
c_extensionblock_t rext = { 0 };
bool redosyntax;
bool aborting = 0; // used by break trap
/**************************************
* Protos
**************************************/
static error_t mainStandardScan(uint32 flags);
static error_t exitStandardScan(error_t error);
static error_t ss_CheckDirTree(void);
static error_t vol_CheckBlockNr(ULONG *blocknr);
static error_t GetRootBlock(void);
static error_t CheckRootBlock(void);
static error_t GetRext(void);
static error_t RepairBootBlock(void);
static error_t RepairDeldir(void);
static bool dd_CheckBlock(uint32 bloknr, int seqnr);
static error_t RepairDirTree(void);
static error_t RepairDir(struct direntry *de, c_dirblock_t *parent);
static error_t RepairDirBlock(uint32 bloknr, uint32 anodenr, uint32 parent);
static error_t RepairDirEntry(struct direntry *de, c_dirblock_t *dirblk);
static error_t RepairFile(struct direntry *de, c_dirblock_t *parent);
static error_t RepairHardLink(struct direntry *de, c_dirblock_t *dirblok);
static error_t RepairSoftLink(struct direntry *de, c_dirblock_t *dirblok);
static error_t RepairRollover(struct direntry *de, c_dirblock_t *dirblok);
static error_t RemoveDirEntry(struct direntry *de, c_dirblock_t *dirblk);
static error_t DeleteAnode(uint32 anodechain, uint32 node);
static error_t GetExtraFields(struct extrafields *extrafields, struct direntry *de);
static error_t SetExtraFields(struct extrafields *extrafields, struct direntry *from, c_dirblock_t *dirblk);
static error_t RepairLinkChain(struct direntry *de, c_dirblock_t *dirblk);
static error_t CheckAnode(canode_t *anode, uint32 nr, bool fix);
static error_t RepairAnodeTree(void);
static error_t RepairBitmapTree(void);
static error_t InitReservedBitmap(void);
static error_t InitAnodeBitmap(void);
static error_t InitMainBitmap(void);
static error_t RepairReservedBitmap(void);
static error_t RepairMainBitmap(void);
static error_t RepairAnodeBitmap(void);
static error_t MainBlockUsed(uint32 bloknr);
static error_t AnodeUsed(uint32 bloknr);
static error_t bg_ItemUsed(bitmap_t *bm, uint32 nr);
static BOOL IsAnodeUsed(uint32 nr);
static uint32 vol_SearchFileSystem(void);
static bitmap_t *bg_InitBitmap(uint32 start, uint32 stop, uint32 step);
static void bg_KillBitmap(bitmap_t **bm);
static BOOL bg_IsItemUsed(bitmap_t *bm, uint32 nr);
int SearchInDir(uint32 diranodenr, uint32 target);
static void AddExtraFields(struct direntry *direntry, struct extrafields *extra);
static bool MakeMountFile(char *fname);
static error_t BuildRootBlock_hub(void);
/**************************************
* Break function L1
**************************************/
#ifdef __SASC
int brk(void)
{
aborting = 1;
return 0;
}
void __regargs __chkabort(void)
{
/* Check & clear CTRL_C signal */
if(SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
{
brk();
}
}
#endif
static BOOL FileSizeCheck(ULONG low, ULONG high, ULONG numblocks)
{
ULONG size, oldlow;
oldlow = low;
low += volume.blocksize - 1;
if (low < oldlow)
high++;
size = low >> volume.blockshift;
size |= high << (32 - volume.blockshift);
if (size == numblocks)
return TRUE;
return FALSE;
}
/**************************************
* StandardScan L1
**************************************/
// public interface
error_t StandardScan(uint32 opties)
{
uint32 flags;
aborting = 0;
// onbreak(&brk);
memset(&ss, sizeof(ss), 0);
opties = opties;
flags = opties;
if (opties & (SSF_CHECK|SSF_FIX))
flags |= SSF_GEN_BMMASK;
/* init stats */
memset(&stats, 0, sizeof(stats));
return mainStandardScan(flags);
}
// core standardscan
static error_t mainStandardScan(uint32 flags)
{
error_t error;
volume.showmsg("Initializing\n");
ss.flags = flags;
ss.stage = syntax;
ss.pass = stats.pass = 1;
ss.verbose = (flags & SSF_VERBOSE) != 0;
ss.unformat = (flags & SSF_UNFORMAT) != 0;
ss.info = (flags & SSF_INFO) != 0;
InitFullScan();
/* unformat .. */
if (ss.unformat)
{
mode = repair; // !! niet really correct !! fix this
volume.showmsg("Unformatting...\n");
if (!(rbl = (rootblock_t *)AllocBufMem (MAXRESBLOCKSIZE)))
{
adderror("couldn't allocate memory for rootblock");
return exitStandardScan(e_out_of_memory);
}
if ((error = BuildRootBlock_hub()) || aborting)
return exitStandardScan(error);
}
/* read rootblock */
volume.status(0, "rootblock", 100);
if ((error = GetRootBlock()) || aborting)
return exitStandardScan(error);
volume.progress(0, 40);
if ((error = RepairBootBlock()) || aborting)
return exitStandardScan(error);
/* after the rootblock and bootblock the boundaries of the partition are known
*/
/* read rootblockextension */
volume.progress(0, 30);
rext.data = pfscalloc(1, SIZEOF_RESBLOCK);
rext.mode = check;
if ((error = GetRext()) || aborting)
return exitStandardScan(error);
GetPFS2Revision(bericht);
volume.showmsg("Disk formatted with ");
volume.showmsg(bericht);
volume.showmsg("\n");
if (ss.info) {
exitStandardScan(e_none);
return e_none;
}
/* stage 1 */
volume.progress(0, 30);
while (ss.stage != finished && ss.pass < MAX_PASS)
{
stats.pass = ss.pass;
sprintf(bericht, "Starting pass %d\n", ss.pass);
volume.showmsg(bericht);
volume.updatestats();
if ((mode == check || mode == info) && stats.numerrors > 0)
break;
error = ss_CheckDirTree();
if ((error != e_none) || aborting)
return exitStandardScan(error);
if (mode == check || mode == info)
break;
ss.pass++;
}
if (ss.stage != finished)
return e_max_pass_exceeded;
volume.status(0, "finishing up", 2);
volume.progress(0, 1);
exitStandardScan(e_none);
volume.progress(0, 1);
return e_none;
}
static error_t exitStandardScan(error_t error)
{
UpdateCache();
if (rbl)
{
FreeBufMem(rbl);
rbl = NULL;
}
if (rext.mode == check)
pfsfree (rext.data);
rext.data = NULL;
KillReservedBitmap();
KillAnodeBitmap();
KillMainBitmap();
ExitFullScan();
if ((error != e_none) || aborting)
volume.showmsg("ABORTED\n");
else if (ss.unformat)
{
volume.askuser(
"Disk has been unformatted. Unformatting can\n"
"cause problems later. Therefor we recommend\n"
"to format this partition after backing up \n"
"all data\n", "OK", NULL);
}
return error;
}
/* main loop: check the partition defined by the rootblock and
* rext (rootblock extension)
*/
static error_t ss_CheckDirTree(void)
{
error_t error;
int newpassneeded = 0;
clearstats();
redosyntax = false;
switch (ss.stage)
{
case syntax:
case resbitmap:
/* initialize reserved bitmap generation */
if (ss.flags & SSF_GEN_RESBITMAP)
InitReservedBitmap();
if (rext.mode == check)
if ((error = ResBlockUsed(rext.blocknr)))
return error;
/* anode blok tree */
/* if anodeindexblok faulty --> create (using DiskScan) */
if ((error = RepairAnodeTree()) || aborting)
return error;
/* bitmap */
/* check syntax bitmap tree
* if block faulty --> create, kill bitmap, ss_makebm = false
* and continue */
if ((error = RepairBitmapTree()) || aborting)
return error;
case mainbitmap:
if (ss.flags & SSF_GEN_MAINBITMAP)
InitMainBitmap();
case anodebitmap:
if (ss.flags & SSF_GEN_ANODEBITMAP)
InitAnodeBitmap();
default:
break;
}
/* deldir */
if ((error = RepairDeldir()) || aborting)
return error;
/* directory tree */
if ((error = RepairDirTree()) || aborting)
return error;
/* on hardlink conflict */
if (redosyntax)
{
ss.stage = syntax;
return e_none;
}
switch (ss.stage)
{
case syntax:
case resbitmap:
/* need another pass if blocks were created
* (should not be necessary!!)
*/
if (!IsMinListEmpty(&volume.buildblocks))
newpassneeded = 1;
/* allocating created blocks & reparing reserved bitmap can only
* be done after the reserved bitmap has been generated.
* ss.flags is set to false if generation was aborted.
*/
if (IsBitmapValid(volume.resbitmap))
{
if ((error = AllocBuildBlocks()))
return error;
if ((error = RepairReservedBitmap()) || aborting)
return error;
}
else
{
if (ss.stage < resbitmap)
{
ss.stage = resbitmap;
break;
}
else
return e_res_bitmap_fail; /* fatal error */
}
if (newpassneeded)
break;
case mainbitmap:
case anodebitmap:
/* bitmap generatie has been aborted (disabled during scan)
* generate main and anodebitmap
*/
if (!IsBitmapValid(volume.mainbitmap))
{
if (ss.stage < mainbitmap)
{
ss.stage = mainbitmap;
break;
}
else
return e_main_bitmap_fail; /* fatal error */
}
if (!IsBitmapValid(volume.anodebitmap))
{
if (ss.stage < anodebitmap)
{
ss.stage = anodebitmap;
break;
}
else
return e_anode_bitmap_fail;
}
if ((error = RepairMainBitmap()) || aborting)
return error;
if ((error = RepairAnodeBitmap()) || aborting)
return error;
ss.stage = finished;
break;
default:
// case doubledel:
ss.stage = mainbitmap;
}
/* Need another pass if blocks build (created) during
* scan. Calling allocbuildblocks here is pointless;
* the reserved bitmap is not valid because of invalid blocks,
* possibly during non reserved bitmap phase!
*/
if (!IsMinListEmpty(&volume.buildblocks))
ss.stage = resbitmap;
return e_none;
}
/**************************************
* Volume functions
**************************************/
/* functions for in cache struct
* gets block from reserved area and checks partition
* borders etc.
* status/errors:
* read/write errors
* e_none
* block_outside_reserved
* block_outside_partition
*/
/* check if a block is in partition range */
static error_t vol_CheckBlockNr(ULONG *blocknr)
{
if (*blocknr > volume.lastreserved) {
adderror("block outside reserved area");
return e_block_outside_reserved;
}
*blocknr += volume.firstblock;
if (*blocknr > volume.lastblock || *blocknr < volume.firstblock) {
adderror("block outside partition");
return e_block_outside_partition;
}
return e_none;
}
error_t vol_GetBlock(cachedblock_t *blok, ULONG bloknr)
{
error_t error;
ULONG realbloknr;
realbloknr = bloknr;
if ((error = vol_CheckBlockNr(&realbloknr)))
return error;
if ((error = c_GetBlock((uint8 *)blok->data, realbloknr, SIZEOF_RESBLOCK)))
{
return error;
}
else
{
blok->mode = check;
blok->blocknr = bloknr;
return e_none;
}
}
error_t vol_WriteBlock(cachedblock_t *blok)
{
int status;
uint32 bloknr;
if (blok->mode != build)
{
bloknr = blok->blocknr;
if ((status = vol_CheckBlockNr(&bloknr)))
return status;
return c_WriteBlock((uint8 *)blok->data, bloknr, SIZEOF_RESBLOCK);
}
return e_none;
}
/**************************************
* GetRootBlock
**************************************/
// public functions
static error_t BuildRootBlock_hub(void)
{
error_t error;
if (volume.askuser(
"Press ok to rebuild rootblock or cancel to exit\n"
"WARNING: make sure correct accessmode (direct-scsi/td64/nsd)\n"
"is selected", "ok", "cancel"))
{
if (!(error = BuildRootBlock(rbl)))
{
fixederror("new rootblock build");
c_WriteBlock((uint8 *)rbl, ROOTBLOCK + volume.firstblock, volume.blocksize);
}
}
else
error = e_aborted;
return error;
}
/* read from disk and check
* Returns error (0 = ERROR_NONE = ok).
*/
static error_t GetRootBlock(void)
{
error_t error = e_none;
uint32 bloknr;
int okuser;
struct FileRequester *freq;
char mfname[FNSIZE], *t;
bool ok = false;
enterblock(ROOTBLOCK);
volume.showmsg("Checking rootblock\n");
if (rbl)
FreeBufMem(rbl);
if (!(rbl = (rootblock_t *)AllocBufMem (MAXRESBLOCKSIZE)))
{
adderror("couldn't allocate memory for rootblock");
return e_out_of_memory;
}
// read rootblock
if ((error = c_GetBlock ((uint8 *)rbl, ROOTBLOCK + volume.firstblock, volume.blocksize)))
{
adderror("rootblock could not be loaded");
return error;
}
if (!IsRootBlock(rbl) && volume.blocklogshift)
{
// Check if we have pre-20.0 pfs3 that has ignored >512 block size
InitCache(0, 0, volume.blocksize >> volume.blocklogshift);
volume.blocksize >>= volume.blocklogshift;
volume.blockshift -= volume.blocklogshift;
if (!(c_GetBlock ((uint8 *)rbl, ROOTBLOCK + volume.firstblocknative, volume.blocksize)))
{
if (IsRootBlock(rbl))
{
volume.showmsg("Rootblock found using 512 block size (block size was %lu)\n", volume.blocksize << volume.blocklogshift);
volume.firstblock = volume.firstblocknative;
volume.lastblock = volume.lastblocknative;
volume.disksize = volume.disksizenative;
volume.lastreserved = volume.disksize - 256;
volume.blocklogshift = 0;
goto rootfound;
}
}
volume.blocksize <<= volume.blocklogshift;
volume.blockshift += volume.blocklogshift;
}
rootfound:;
// check rootblock type
if (!IsRootBlock(rbl))
{
adderror("not an AFS, PFS-II or PFS-III disk");
if (ss.info) {
return e_none;
}
okuser = volume.askuser("Rootblock not found.\n"
"Select ok to search for misplaced rootblock.", "ok", "cancel");
if (okuser && (bloknr = vol_SearchFileSystem()))
{
volume.showmsg("\nRootblock found. Repartitioning .. \n");
if ((error = Repartition(bloknr)))
return error;
volume.askuser(
"Partition is misplaced. Probable cause of this problem\n"
"is the use of the 'Get Drive Definition' option in\n"
"HDToolbox after the disk was formatted\n\n"
"Now a mountfile will be needed to mount the partition.\n"
"Press CONTINUE to select location to store the\n"
"mountlist", "CONTINUE", NULL);
freq = AllocAslRequestTags(ASL_FileRequest, ASLFR_SleepWindow, TRUE,
ASLFR_TitleText, (IPTR)"Select mountfile", ASLFR_InitialFile, (IPTR)"mountme",
ASLFR_InitialDrawer, (IPTR)"df0:", ASLFR_DoSaveMode, TRUE, TAG_DONE);
do
{
if (AslRequestTags(freq, ASLFR_Window, (IPTR)CheckRepairWnd, TAG_DONE))
{
t = stpcpy(mfname, freq->fr_Drawer);
t = stpcpy(t, freq->fr_File);
if ((ok = MakeMountFile(mfname)))
volume.showmsg("Mountlist written\n");
}
else
{
volume.showmsg("no mountlist written\n");
}
} while (!ok);
FreeAslRequest(freq);
volume.askuser("Now starting to check/repair\n"
"relocated partition", "CONTINUE", NULL);
return GetRootBlock();
}
else
{
okuser = volume.askuser("Rootblock not found.\n"
"Select ok to build a new one.\n"
"WARNING: Make sure this is a PFS partition.\n"
"Non-PFS disks will be destroyed by this operation\n",
"ok", "cancel");
if (okuser)
return BuildRootBlock_hub();
else
{
aborting = 1;
return e_aborted;
}
}
}
error = CheckRootBlock();
if (ss.info) {
return e_none;
}
switch (error)
{
case e_none:
break;
case e_repartition:
if ((error = Repartition(volume.firstblock+ROOTBLOCK)))
return error;
volume.askuser(
"The partition information stored in the RDB does\n"
"not match the partition information used when the\n"
"disk was formatted. Probable cause of this problem\n"
"is the use of the 'Get Drive Definition' option in\n"
"HDToolbox after the disk was formatted\n\n"
"Now a mountfile will be needed to mount the partition.\n"
"Press CONTINUE to select location to store the\n"
"mountlist", "CONTINUE", NULL);
freq = AllocAslRequestTags(ASL_FileRequest, ASLFR_SleepWindow, TRUE,
ASLFR_TitleText, (IPTR)"Select mountfile", ASLFR_InitialFile, (IPTR)"mountme",
ASLFR_InitialDrawer, (IPTR)"df0:", ASLFR_DoSaveMode, TRUE, TAG_DONE);
do
{
if (AslRequestTags(freq, ASLFR_Window, (IPTR)CheckRepairWnd, TAG_DONE))
{
t = stpcpy(mfname, freq->fr_Drawer);
t = stpcpy(t, freq->fr_File);
if ((ok = MakeMountFile(mfname)))
volume.showmsg("Mountlist written\n");
}
else
{
volume.showmsg("no mountlist written\n");
}
} while (!ok);
FreeAslRequest(freq);
volume.askuser("Now starting to check/repair\n"
"redefined partition", "CONTINUE", NULL);
return GetRootBlock();
case e_options_error:
return error;
default:
return BuildRootBlock_hub();
}
exitblock();
return e_none;
}
static bool MakeMountFile(char *fname)
{
FILE *mf;
if ((mf = fopen(fname, "w")))
{
fprintf(mf, "/****************************************/\n");
fprintf(mf, "/* PFSDoctor generated mountlist */\n");
fprintf(mf, "/****************************************/\n");
fprintf(mf, "REPAIRED:\n");
fprintf(mf, "\tDevice = %s\n", volume.execdevice);
fprintf(mf, "\tUnit = %d\n", volume.execunit);
fprintf(mf, "\tBuffers = 300\n");
fprintf(mf, "\tBufMemType = %lu\n", volume.dosenvec->de_BufMemType);
fprintf(mf, "\tFlags = %lu\n", volume.fssm->fssm_Flags);
fprintf(mf, "\tDosType = %#lx\n", volume.dosenvec->de_DosType);
fprintf(mf, "\tGlobVec = -1\n");
fprintf(mf, "\tInterleave = %ld\n", volume.dosenvec->de_Interleave);
fprintf(mf, "\tReserved = 2\n");
fprintf(mf, "\tLowCyl = %lu\n", volume.firstblock);
fprintf(mf, "\tHighCyl = %lu\n", volume.lastblock);
fprintf(mf, "\tMount = 1\n");
fprintf(mf, "\tSurfaces = 1\n");
fprintf(mf, "\tBlocksPerTrack = 1\n");
fprintf(mf, "\tPriority = 10\n");
fprintf(mf, "\tStackSize = 600\n");
fprintf(mf, "\tMaxTransfer = %lu\n", volume.dosenvec->de_MaxTransfer);
fprintf(mf, "\tMask = %#lx\n", volume.dosenvec->de_Mask);
fclose(mf);
return true;
}
return false;
}
static uint32 vol_SearchFileSystem(void)
{
return SearchFileSystem(
(volume.firstblock > 1024) ? volume.firstblock - 1024 : 0,
volume.firstblock + 1024);
}
bool IsRootBlock(rootblock_t *r)
{
ULONG modemask;
// check rootblock type
if (r->disktype != ID_PFS_DISK && r->disktype != ID_PFS2_DISK) {
if (ss.verbose)
volume.showmsg("Unexpected rootblock id 0x%08lx\n", r->disktype);
return false;
}
// check options
// require non-null options to accept rootblock as such,
// otherwise it could be a bootblock
modemask = MODE_HARDDISK + MODE_SPLITTED_ANODES + MODE_DIR_EXTENSION;
if ((r->options & modemask) != modemask) {
if (ss.verbose)
volume.showmsg("Unexpected rootblock options 0x%08lx\n", r->options);
return false;
}
return true;
}
/* check loaded rootblock
*/
static error_t CheckRootBlock(void)
{
int error = 0;
bool dirty = false;
ULONG modemask, resblocksize;
modemask = MODE_HARDDISK + MODE_SPLITTED_ANODES + MODE_DIR_EXTENSION
+ MODE_DELDIR + MODE_SIZEFIELD + MODE_EXTENSION + MODE_DATESTAMP
+ MODE_SUPERINDEX + MODE_SUPERDELDIR + MODE_EXTROVING + MODE_LONGFN
+ MODE_LARGEFILE + MODE_STORED_GEOM;
if (rbl->options & ~modemask)
{
adderror("Unknown options enabled. Ask for an upgrade.");
return e_options_error;
}
// check the fields
if (!rbl->diskname[0])
{
if (mode == check || mode == info)
adderror("Volume has no name");
else
{
fixederror("Volume had no name");
rbl->diskname[0] = strlen("doctor");
strcpy((char *)&rbl->diskname[1], "doctor");
dirty = true;
}
}
strncpy (volume.diskname, (char *)&rbl->diskname[1], rbl->diskname[0]);
volume.diskname[rbl->diskname[0]+1] = 0;
volume.showmsg("Checking disk ");
volume.showmsg(volume.diskname);
volume.showmsg("\n");
volume.showmsg("Option mask: %08lx\n", rbl->options);
volume.showmsg("Reserved blocksize: %lu\n", rbl->reserved_blksize);
volume.showmsg("Reserved block cluster: %lu\n", rbl->rblkcluster);
resblocksize = 1024;
if (rbl->disktype == ID_PFS2_DISK) {
if (volume.disksize > MAXDISKSIZE4K) {
adderror("Too large (>1.6TB) partition size");
return e_options_error;
}
if (volume.disksize > MAXDISKSIZE1K) {
resblocksize = 2048;
if (volume.disksize > MAXDISKSIZE2K)
resblocksize = 4096;
}
if (volume.blocksize > resblocksize) {
resblocksize = volume.blocksize;
}
} else {
if (volume.disksize > MAXDISKSIZE1K) {
adderror("Too large (>104GB) partition size");
return e_options_error;
}
}
if (rbl->reserved_blksize != resblocksize)
{
if (rbl->reserved_blksize == 1024 || rbl->reserved_blksize == 2048 || rbl->reserved_blksize == 4096) {
adderror("Reserved blocksize is valid but does not match partition size. Driver size limit problem?");
return e_options_error;
}
sprintf(bericht, "Wrong reserved blocksize of %d", rbl->reserved_blksize);
fixederror(bericht);
rbl->reserved_blksize = resblocksize;
dirty = true;
}
volume.rescluster = resblocksize / volume.blocksize;
volume.showmsg("First reserved block: %lu\n", rbl->firstreserved);
volume.showmsg("Last reserved block: %lu\n", rbl->lastreserved);
uint32 blocks = (rbl->lastreserved - rbl->firstreserved + 1) / volume.rescluster;
volume.showmsg("Reserved blocks: %lu\n", blocks);
volume.showmsg("Free reserved blocks: %lu\n", rbl->reserved_free);
volume.showmsg("Free blocks: %lu\n", rbl->blocksfree);
volume.showmsg("Always free blocks: %lu\n", rbl->alwaysfree);
volume.showmsg("Total blocks: %lu\n", rbl->disksize);
// size must match, else repartition
if (rbl->options & MODE_SIZEFIELD)
{
if(rbl->disksize != volume.disksizenative)
{
int32 difference;
/* uses rule: if difference less then 10%, assume mistake
* in RDB, otherwise assume mistake in rootblock
*/
adderror("Wrong disksize");
volume.showmsg("Rootblock size: %lu, driver reported size: %lu\n", rbl->disksize, volume.disksizenative);
difference = abs((int32)rbl->disksize - (int32)volume.disksizenative);
if (difference < volume.disksizenative/10)
{
error = e_repartition;
}
else if (mode != check)
{
rbl->disksize = volume.disksizenative;
dirty = true;
fixederror("set disksize");
}
} else if((rbl->disksize >> volume.blocklogshift) != volume.disksize) {
adderror("Wrong logical block calculated disksize");
volume.showmsg("Rootblock logical block calculated size: %lu, driver reported size: %lu\n", rbl->disksize >> volume.blocklogshift, volume.disksize);
}
}
if (rbl->alwaysfree < volume.disksize/40 ||
rbl->alwaysfree > volume.disksize/15)
{
fixederror("Allocation block buffer out of range");
rbl->alwaysfree = volume.disksize/20;
dirty = true;
}
if (dirty)
return c_WriteBlock((uint8 *)rbl, ROOTBLOCK + volume.firstblock, volume.blocksize);
return error;
}
/**************************************
* GetRootBlockExtension
**************************************/
/* check rootblock extension. Do 'easy fix'. Returns
* false if failure
*/
static error_t GetRext(void)
{
error_t error;
uint32 bloknr, oldbloknr;
bloknr = rbl->extension;
enterblock(bloknr);
volume.showmsg("Checking rext\n");
if (bloknr)
{
if ((error = volume.getblock((cachedblock_t *)&rext, bloknr)))
return error;
}
if (!bloknr || rext.data->id != EXTENSIONID)
{
adderror("Extension block has wrong id");
if (mode == repair)
{
volume.status(1, "Searching rext", 256);
oldbloknr = bloknr;
bloknr = SearchBlock(EXTENSIONID, 0, bloknr, 0, 0, 0);
if (bloknr)
{
sprintf(bericht, "Replaced block %lu with backup %lu", oldbloknr, bloknr);
fixederror(bericht);
rbl->extension = bloknr;
return GetRext();
}
else
{
volume.showmsg("No replacement block found\n");
pfsfree (rext.data);
if ((error = BuildRext(&rext)))
adderror("Building new rootblockextension failed");
else
fixederror("New rootblockextension created");
return error;
}
}
else
{
return e_fatal_error;