-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathdisklabel.c
More file actions
1897 lines (1778 loc) · 45.1 KB
/
disklabel.c
File metadata and controls
1897 lines (1778 loc) · 45.1 KB
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
/* $NetBSD: disklabel.c,v 1.73 1999/06/04 19:02:34 is Exp $ */
/*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Symmetric Computer Systems.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95";
/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
#else
__RCSID("$NetBSD: disklabel.c,v 1.73 1999/06/04 19:02:34 is Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define DKTYPENAMES
#define FSTYPENAMES
#include <sys/disklabel.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <util.h>
#include <disktab.h>
#include "pathnames.h"
#include "extern.h"
#include "dkcksum.h"
/*
* Disklabel: read and write disklabels.
* The label is usually placed on one of the first sectors of the disk.
* Many machines also place a bootstrap in the same area,
* in which case the label is embedded in the bootstrap.
* The bootstrap source must leave space at the proper offset
* for the label on such machines.
*/
#ifndef BBSIZE
#define BBSIZE 8192 /* size of boot area, with label */
#endif
#ifndef NUMBOOT
#define NUMBOOT 0
#endif
extern char *__progname;
#define DEFEDITOR _PATH_VI
static char *dkname;
static char *specname;
static char tmpfil[] = _PATH_TMP;
static char namebuf[BBSIZE], *np = namebuf;
static struct disklabel lab;
char bootarea[BBSIZE];
#if NUMBOOT > 0
static int installboot; /* non-zero if we should install a boot program */
static char *bootbuf; /* pointer to buffer with remainder of boot prog */
static int bootsize; /* size of remaining boot program */
static char *xxboot; /* primary boot */
static char *bootxx; /* secondary boot */
static char boot0[MAXPATHLEN];
#if NUMBOOT > 1
static char boot1[MAXPATHLEN];
#endif
#endif
static enum {
UNSPEC, EDIT, READ, RESTORE, SETWRITEABLE, WRITE, WRITEBOOT, INTERACT
} op = UNSPEC;
static int rflag;
static int tflag;
static int Cflag;
#ifdef DEBUG
static int debug;
#define OPTIONS "BCNRWb:def:irs:tw"
#else
#define OPTIONS "BCNRWb:ef:irs:tw"
#endif
#ifdef __i386__
static struct mbr_partition *dosdp; /* i386 DOS partition, if found */
static int mbrpt_nobsd; /* MBR partition table exists, but no BSD partition */
static struct mbr_partition *readmbr __P((int));
#endif
#ifdef __arm32__
static u_int filecore_partition_offset;
static u_int get_filecore_partition __P((int));
static int filecore_checksum __P((u_char *));
#endif /* __arm32__ */
#if defined(__i386__) || (defined(__arm32__) && defined(notyet))
static void confirm __P((const char *));
#endif
int main __P((int, char *[]));
static void makedisktab __P((FILE *, struct disklabel *));
static void makelabel __P((char *, char *, struct disklabel *));
static void l_perror __P((char *));
static struct disklabel *readlabel __P((int));
static struct disklabel *makebootarea __P((char *, struct disklabel *, int));
static void showinfo __P((FILE *, struct disklabel *));
static int edit __P((struct disklabel *, int));
static int editit __P((void));
static char *skip __P((char *));
static char *word __P((char *));
static int getasciilabel __P((FILE *, struct disklabel *));
#if NUMBOOT > 0
static void setbootflag __P((struct disklabel *));
#endif
static void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
struct disklabel *lp;
FILE *t;
int ch, f, writeable, error = 0;
while ((ch = getopt(argc, argv, OPTIONS)) != -1)
switch (ch) {
#if NUMBOOT > 0
case 'B':
++installboot;
break;
case 'b':
xxboot = optarg;
break;
#if NUMBOOT > 1
case 's':
bootxx = optarg;
break;
#endif
#endif
case 'C':
++Cflag;
break;
case 'N':
if (op != UNSPEC)
usage();
writeable = 0;
op = SETWRITEABLE;
break;
case 'R':
if (op != UNSPEC)
usage();
op = RESTORE;
break;
case 'W':
if (op != UNSPEC)
usage();
writeable = 1;
op = SETWRITEABLE;
break;
case 'e':
if (op != UNSPEC)
usage();
op = EDIT;
break;
case 'f':
if (setdisktab(optarg) == -1)
usage();
break;
case 'i':
if (op != UNSPEC)
usage();
op = INTERACT;
break;
case 't':
++tflag;
break;
case 'r':
++rflag;
break;
case 'w':
if (op != UNSPEC)
usage();
op = WRITE;
break;
#ifdef DEBUG
case 'd':
debug++;
break;
#endif
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
#if NUMBOOT > 0
if (installboot) {
rflag++;
if (op == UNSPEC)
op = WRITEBOOT;
} else {
if (op == UNSPEC)
op = READ;
}
#else
if (op == UNSPEC)
op = READ;
#endif
if (argc < 1)
usage();
dkname = argv[0];
f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, np, MAXPATHLEN, 0);
specname = np;
np += strlen(specname) + 1;
if (f < 0)
err(4, "%s", specname);
#ifdef __i386__
/*
* Check for presence of DOS partition table in
* master boot record. Return pointer to NetBSD/i386
* partition, if present.
*/
dosdp = readmbr(f);
#endif
#ifdef __arm32__
/*
* Check for the presence of a RiscOS filecore boot block
* indicating an ADFS file system on the disc.
* Return the offset to the NetBSD part of the disc if
* this can be determined.
* This routine will terminate disklabel if the disc
* is found to be ADFS only.
*/
filecore_partition_offset = get_filecore_partition(f);
#endif /* __arm32__ */
switch (op) {
case EDIT:
if (argc != 1)
usage();
lp = readlabel(f);
error = edit(lp, f);
break;
case INTERACT:
if (argc != 1)
usage();
lp = readlabel(f);
/*
* XXX: Fill some default values so checklabel does not fail
*/
if (lp->d_bbsize == 0)
lp->d_bbsize = BBSIZE;
if (lp->d_sbsize == 0)
lp->d_sbsize = SBSIZE;
interact(lp, f);
break;
case READ:
if (argc != 1)
usage();
lp = readlabel(f);
if (tflag)
makedisktab(stdout, lp);
else {
showinfo(stdout, lp);
showpartitions(stdout, lp);
}
error = checklabel(lp);
break;
case RESTORE:
if (argc < 2 || argc > 3)
usage();
#if NUMBOOT > 0
if (installboot && argc == 3)
makelabel(argv[2], (char *)0, &lab);
#endif
lp = makebootarea(bootarea, &lab, f);
if (!(t = fopen(argv[1], "r")))
err(4, "%s", argv[1]);
if (getasciilabel(t, lp))
error = writelabel(f, bootarea, lp);
break;
case SETWRITEABLE:
if (ioctl(f, DIOCWLABEL, (char *)&writeable) < 0)
err(4, "ioctl DIOCWLABEL");
break;
case WRITE:
if (argc < 2 || argc > 3)
usage();
makelabel(argv[1], argc == 3 ? argv[2] : (char *)0, &lab);
lp = makebootarea(bootarea, &lab, f);
*lp = lab;
if (checklabel(lp) == 0)
error = writelabel(f, bootarea, lp);
else
error = 1;
break;
case WRITEBOOT:
#if NUMBOOT > 0
{
struct disklabel tlab;
lp = readlabel(f);
tlab = *lp;
if (argc == 2)
makelabel(argv[1], (char *)0, &lab);
lp = makebootarea(bootarea, &lab, f);
*lp = tlab;
if (checklabel(lp) == 0)
error = writelabel(f, bootarea, lp);
else
error = 1;
break;
}
#endif
case UNSPEC:
usage();
}
exit(error);
}
/*
* Construct a prototype disklabel from /etc/disktab. As a side
* effect, set the names of the primary and secondary boot files
* if specified.
*/
static void
makelabel(type, name, lp)
char *type, *name;
struct disklabel *lp;
{
struct disklabel *dp;
dp = getdiskbyname(type);
if (dp == NULL)
errx(1, "unknown disk type: %s", type);
*lp = *dp;
#if NUMBOOT > 0
/*
* Set bootstrap name(s).
* 1. If set from command line, use those,
* 2. otherwise, check if disktab specifies them (b0 or b1),
* 3. otherwise, makebootarea() will choose ones based on the name
* of the disk special file. E.g. /dev/ra0 -> raboot, bootra
*/
if (!xxboot && lp->d_boot0) {
if (*lp->d_boot0 != '/')
(void)sprintf(boot0, "%s/%s",
_PATH_BOOTDIR, lp->d_boot0);
else
(void)strcpy(boot0, lp->d_boot0);
xxboot = boot0;
}
#if NUMBOOT > 1
if (!bootxx && lp->d_boot1) {
if (*lp->d_boot1 != '/')
(void)sprintf(boot1, "%s/%s",
_PATH_BOOTDIR, lp->d_boot1);
else
(void)strcpy(boot1, lp->d_boot1);
bootxx = boot1;
}
#endif
#endif
/* d_packname is union d_boot[01], so zero */
(void) memset(lp->d_packname, 0, sizeof(lp->d_packname));
if (name)
(void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
}
#if defined(__i386__) || (defined(__arm32__) && defined(notyet))
static void
confirm(txt)
const char *txt;
{
int first, ch;
(void) printf("%s? [n]: ", txt);
(void) fflush(stdout);
first = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (first != 'y' && first != 'Y')
exit(0);
}
#endif
int
writelabel(f, boot, lp)
int f;
char *boot;
struct disklabel *lp;
{
int writeable;
off_t sectoffset = 0;
#if NUMBOOT > 0
setbootflag(lp);
#endif
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
#ifdef __sparc__
/* Let the kernel deal with SunOS disklabel compatibility */
if (0) {
#else
if (rflag) {
#endif
#ifdef __i386__
struct partition *pp = &lp->d_partitions[2];
/*
* If NetBSD/i386 DOS partition is missing, or if
* the label to be written is not within partition,
* prompt first. Need to allow this in case operator
* wants to convert the drive for dedicated use.
*/
if (dosdp) {
if (dosdp->mbrp_start != pp->p_offset)
confirm("Write outside MBR partition");
sectoffset = (off_t)pp->p_offset * lp->d_secsize;
} else {
if (mbrpt_nobsd)
confirm("Erase the previous contents"
" of the disk");
sectoffset = 0;
}
#endif
#ifdef __arm32__
/* XXX */
sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
#endif /* __arm32__ */
/*
* First set the kernel disk label,
* then write a label to the raw disk.
* If the SDINFO ioctl fails because it is unimplemented,
* keep going; otherwise, the kernel consistency checks
* may prevent us from changing the current (in-core)
* label.
*/
if (ioctl(f, DIOCSDINFO, lp) < 0 &&
errno != ENODEV && errno != ENOTTY) {
l_perror("ioctl DIOCSDINFO");
return (1);
}
if (lseek(f, sectoffset, SEEK_SET) < 0) {
perror("lseek");
return (1);
}
/*
* write enable label sector before write (if necessary),
* disable after writing.
*/
writeable = 1;
if (ioctl(f, DIOCWLABEL, &writeable) < 0)
perror("ioctl DIOCWLABEL");
#ifdef __alpha__
/*
* The Alpha requires that the boot block be checksummed.
* The NetBSD/alpha disklabel.h provides a macro to do it.
*/
{
struct boot_block *bb = (struct boot_block *)boot;
CHECKSUM_BOOT_BLOCK(bb, &bb->bb_cksum);
}
#endif
if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
perror("write");
return (1);
}
#if NUMBOOT > 0
/*
* Output the remainder of the disklabel
*/
if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
perror("write");
return(1);
}
#endif
writeable = 0;
if (ioctl(f, DIOCWLABEL, &writeable) < 0)
perror("ioctl DIOCWLABEL");
} else {
if (ioctl(f, DIOCWDINFO, lp) < 0) {
l_perror("ioctl DIOCWDINFO");
return (1);
}
}
#ifdef __vax__
if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
daddr_t alt;
int i;
alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
(void)lseek(f, (off_t)(alt + i) * lp->d_secsize,
SEEK_SET);
if (write(f, boot, lp->d_secsize) < lp->d_secsize)
warn("alternate label %d write", i/2);
}
}
#endif
return (0);
}
static void
l_perror(s)
char *s;
{
switch (errno) {
case ESRCH:
warnx("%s: No disk label on disk;\n"
"use \"disklabel -r\" to install initial label", s);
break;
case EINVAL:
warnx("%s: Label magic number or checksum is wrong!\n"
"(disklabel or kernel is out of date?)", s);
break;
case EBUSY:
warnx("%s: Open partition would move or shrink", s);
break;
case EXDEV:
warnx("%s: Labeled partition or 'a' partition must start"
" at beginning of disk", s);
break;
default:
warn("%s", s);
break;
}
}
#ifdef __i386__
/*
* Fetch DOS partition table from disk.
*/
static struct mbr_partition *
readmbr(f)
int f;
{
static char mbr[DEV_BSIZE];
struct mbr_partition *dp = (struct mbr_partition *)&mbr[MBR_PARTOFF];
u_int16_t *mbrmagicp;
int part;
if (lseek(f, (off_t)MBR_BBSECTOR * DEV_BSIZE, SEEK_SET) < 0 ||
read(f, mbr, sizeof(mbr)) < sizeof(mbr))
err(4, "can't read master boot record");
/*
* Don't (yet) know disk geometry (BIOS), use
* partition table to find NetBSD/i386 partition, and obtain
* disklabel from there.
*/
/* Check if table is valid. */
mbrmagicp = (u_int16_t *)(&mbr[MBR_MAGICOFF]);
if (*mbrmagicp != MBR_MAGIC)
return (0);
/* Find NetBSD partition. */
for (part = 0; part < NMBRPART; part++) {
if (dp[part].mbrp_typ == MBR_PTYPE_NETBSD)
return (&dp[part]);
}
#ifdef COMPAT_386BSD_MBRPART
/* didn't find it -- look for 386BSD partition */
for (part = 0; part < NMBRPART; part++) {
if (dp[part].mbrp_typ == MBR_PTYPE_386BSD) {
warnx("old BSD partition ID!");
return (&dp[part]);
}
}
#endif
/*
* Table doesn't contain a partition for us. Keep a flag
* remembering us to warn before it is destroyed.
*/
mbrpt_nobsd = 1;
return (0);
}
#endif
#ifdef __arm32__
/*
* static int filecore_checksum(u_char *bootblock)
*
* Calculates the filecore boot block checksum. This is used to validate
* a filecore boot block on the disk. If a boot block is validated then
* it is used to locate the partition table. If the boot block is not
* validated, it is assumed that the whole disk is NetBSD.
*
* The basic algorithm is:
*
* for (each byte in block, excluding checksum) {
* sum += byte;
* if (sum > 255)
* sum -= 255;
* }
*
* That's equivalent to summing all of the bytes in the block
* (excluding the checksum byte, of course), then calculating the
* checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
* expression may or may not yield a faster checksum function,
* but it's easier to reason about.
*
* Note that if you have a block filled with bytes of a single
* value "X" (regardless of that value!) and calculate the cksum
* of the block (excluding the checksum byte), you will _always_
* end up with a checksum of X. (Do the math; that can be derived
* from the checksum calculation function!) That means that
* blocks which contain bytes which all have the same value will
* always checksum properly. That's a _very_ unlikely occurence
* (probably impossible, actually) for a valid filecore boot block,
* so we treat such blocks as invalid.
*/
static int
filecore_checksum(bootblock)
u_char *bootblock;
{
u_char byte0, accum_diff;
u_int sum;
int i;
sum = 0;
accum_diff = 0;
byte0 = bootblock[0];
/*
* Sum the contents of the block, keeping track of whether
* or not all bytes are the same. If 'accum_diff' ends up
* being zero, all of the bytes are, in fact, the same.
*/
for (i = 0; i < 511; ++i) {
sum += bootblock[i];
accum_diff |= bootblock[i] ^ byte0;
}
/*
* Check to see if the checksum byte is the same as the
* rest of the bytes, too. (Note that if all of the bytes
* are the same except the checksum, a checksum compare
* won't succeed, but that's not our problem.)
*/
accum_diff |= bootblock[i] ^ byte0;
/* All bytes in block are the same; call it invalid. */
if (accum_diff == 0)
return (-1);
return (sum - ((sum - 1) / 255) * 255);
}
/*
* Fetch filecore bootblock from disk and analyse it
*/
static u_int
get_filecore_partition(f)
int f;
{
static char bb[DEV_BSIZE];
struct filecore_bootblock *fcbb;
u_int offset;
if (lseek(f, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0 ||
read(f, bb, sizeof(bb)) < sizeof(bb))
err(4, "can't read filecore boot block");
fcbb = (struct filecore_bootblock *)bb;
/* Check if table is valid. */
if (filecore_checksum(bb) != fcbb->checksum)
return (0);
/*
* Check for NetBSD/arm32 (RiscBSD) partition marker.
* If found the NetBSD disklabel location is easy.
*/
offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8))
* fcbb->heads * fcbb->secspertrack;
if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
return (offset);
else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
struct riscix_partition_table *riscix_part;
int loop;
/*
* Read the RISCiX partition table and search for the
* first partition named "RiscBSD", "NetBSD", or "Empty:"
*
* XXX is use of 'Empty:' really desirable?! -- cgd
*/
if (lseek(f, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0 ||
read(f, bb, sizeof(bb)) < sizeof(bb))
err(4, "can't read riscix partition table");
riscix_part = (struct riscix_partition_table *)bb;
for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
if (strcmp(riscix_part->partitions[loop].rp_name,
"RiscBSD") == 0 ||
strcmp(riscix_part->partitions[loop].rp_name,
"NetBSD") == 0 ||
strcmp(riscix_part->partitions[loop].rp_name,
"Empty:") == 0) {
offset = riscix_part->partitions[loop].rp_start;
break;
}
}
if (loop == NRISCIX_PARTITIONS) {
/*
* Valid filecore boot block, RISCiX partition table
* but no NetBSD partition. We should leave this
* disc alone.
*/
errx(4, "cannot label: no NetBSD partition found"
" in RISCiX partition table");
}
return (offset);
} else {
/*
* Valid filecore boot block and no non-ADFS partition.
* This means that the whole disc is allocated for ADFS
* so do not trash ! If the user really wants to put a
* NetBSD disklabel on the disc then they should remove
* the filecore boot block first with dd.
*/
errx(4, "cannot label: filecore-only disk"
" (no non-ADFS partition)");
}
return (0);
}
#endif /* __arm32__ */
/*
* Fetch disklabel for disk.
* Use ioctl to get label unless -r flag is given.
*/
static struct disklabel *
readlabel(f)
int f;
{
struct disklabel *lp;
if (rflag) {
char *msg;
off_t sectoffset = 0;
#ifdef __i386__
if (dosdp)
sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
#endif
#ifdef __arm32__
/* XXX */
sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
#endif /* __arm32__ */
if (lseek(f, sectoffset, SEEK_SET) < 0 ||
read(f, bootarea, BBSIZE) < BBSIZE)
err(4, "%s", specname);
msg = "no disk label";
for (lp = (struct disklabel *)bootarea;
lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
lp = (struct disklabel *)((char *)lp + sizeof(long))) {
if (lp->d_magic == DISKMAGIC &&
lp->d_magic2 == DISKMAGIC) {
if (lp->d_npartitions <= MAXPARTITIONS &&
dkcksum(lp) == 0)
return (lp);
msg = "disk label corrupted";
}
}
/* lp = (struct disklabel *)(bootarea + LABELOFFSET); */
errx(1, msg);
} else {
lp = &lab;
if (ioctl(f, DIOCGDINFO, lp) < 0)
err(4, "ioctl DIOCGDINFO");
}
return (lp);
}
/*
* Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
* Returns a pointer to the disklabel portion of the bootarea.
*/
static struct disklabel *
makebootarea(boot, dp, f)
char *boot;
struct disklabel *dp;
int f;
{
struct disklabel *lp;
char *p;
#if NUMBOOT > 0
int b;
char *dkbasename;
# if NUMBOOT <= 1
struct stat sb;
# endif
#endif
/* XXX */
if (dp->d_secsize == 0) {
dp->d_secsize = DEV_BSIZE;
dp->d_bbsize = BBSIZE;
}
lp = (struct disklabel *)
(boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
(void) memset(lp, 0, sizeof *lp);
#ifdef SAVEBOOTAREA
/*
* We must read the current bootarea so we don't clobber the
* existing boot block, if any.
*/
if (rflag) {
off_t sectoffset = 0;
if (lseek(f, sectoffset, SEEK_SET) < 0 ||
read(f, boot, BBSIZE) < BBSIZE)
err(4, "%s", specname);
(void) memset(lp, 0, sizeof *lp);
}
#endif
#if NUMBOOT > 0
/*
* If we are not installing a boot program but we are installing a
* label on disk then we must read the current bootarea so we don't
* clobber the existing boot.
*/
if (!installboot) {
if (rflag) {
off_t sectoffset = 0;
#ifdef __i386__
if (dosdp)
sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
#endif
#ifdef __arm32__
/* XXX */
sectoffset = (off_t)filecore_partition_offset
* DEV_BSIZE;
#endif /* __arm32__ */
if (lseek(f, sectoffset, SEEK_SET) < 0 ||
read(f, boot, BBSIZE) < BBSIZE)
err(4, "%s", specname);
(void) memset(lp, 0, sizeof *lp);
}
return (lp);
}
/*
* We are installing a boot program. Determine the name(s) and
* read them into the appropriate places in the boot area.
*/
if (!xxboot || !bootxx) {
dkbasename = np;
if ((p = strrchr(dkname, '/')) == NULL)
p = dkname;
else
p++;
while (*p && !isdigit(*p))
*np++ = *p++;
*np++ = '\0';
if (!xxboot) {
(void)sprintf(np, "%s/%sboot",
_PATH_BOOTDIR, dkbasename);
if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
dkbasename++;
xxboot = np;
(void)sprintf(xxboot, "%s/%sboot",
_PATH_BOOTDIR, dkbasename);
np += strlen(xxboot) + 1;
}
#if NUMBOOT > 1
if (!bootxx) {
(void)sprintf(np, "%s/boot%s",
_PATH_BOOTDIR, dkbasename);
if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
dkbasename++;
bootxx = np;
(void)sprintf(bootxx, "%s/boot%s",
_PATH_BOOTDIR, dkbasename);
np += strlen(bootxx) + 1;
}
#endif
}
#ifdef DEBUG
if (debug)
warnx("bootstraps: xxboot = %s, bootxx = %s", xxboot,
bootxx ? bootxx : "NONE");
#endif
/*
* Strange rules:
* 1. One-piece bootstrap (hp300/hp800)
* up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
* is remembered and written later following the bootarea.
* 2. Two-piece bootstraps (vax/i386?/mips?)
* up to d_secsize bytes of ``xxboot'' go in first d_secsize
* bytes of bootarea, remaining d_bbsize-d_secsize filled
* from ``bootxx''.
*/
b = open(xxboot, O_RDONLY);
if (b < 0)
err(4, "%s", xxboot);
#if NUMBOOT > 1
if (read(b, boot, (int)dp->d_secsize) < 0)
err(4, "%s", xxboot);
(void)close(b);
b = open(bootxx, O_RDONLY);
if (b < 0)
err(4, "%s", bootxx);
if (read(b, &boot[dp->d_secsize],
(int)(dp->d_bbsize-dp->d_secsize)) < 0)
err(4, "%s", bootxx);
#else
if (read(b, boot, (int)dp->d_bbsize) < 0)
err(4, "%s", xxboot);
(void)fstat(b, &sb);
bootsize = (int)sb.st_size - dp->d_bbsize;
if (bootsize > 0) {
/* XXX assume d_secsize is a power of two */
bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
bootbuf = (char *)malloc((size_t)bootsize);
if (bootbuf == 0)
err(4, "%s", xxboot);