main.c revision 1.1 1 1.1 dyoung /* $NetBSD: main.c,v 1.1 2005/06/12 19:18:34 dyoung Exp $ */
2 1.1 dyoung
3 1.1 dyoung /*
4 1.1 dyoung * Copyright (c) 1987, 1993
5 1.1 dyoung * The Regents of the University of California. All rights reserved.
6 1.1 dyoung *
7 1.1 dyoung * This code is derived from software contributed to Berkeley by
8 1.1 dyoung * Symmetric Computer Systems.
9 1.1 dyoung *
10 1.1 dyoung * Redistribution and use in source and binary forms, with or without
11 1.1 dyoung * modification, are permitted provided that the following conditions
12 1.1 dyoung * are met:
13 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
14 1.1 dyoung * notice, this list of conditions and the following disclaimer.
15 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
17 1.1 dyoung * documentation and/or other materials provided with the distribution.
18 1.1 dyoung * 3. Neither the name of the University nor the names of its contributors
19 1.1 dyoung * may be used to endorse or promote products derived from this software
20 1.1 dyoung * without specific prior written permission.
21 1.1 dyoung *
22 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 dyoung * SUCH DAMAGE.
33 1.1 dyoung */
34 1.1 dyoung
35 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H
36 1.1 dyoung #include "nbtool_config.h"
37 1.1 dyoung #endif
38 1.1 dyoung
39 1.1 dyoung #include <sys/cdefs.h>
40 1.1 dyoung #ifndef lint
41 1.1 dyoung __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
42 1.1 dyoung The Regents of the University of California. All rights reserved.\n");
43 1.1 dyoung #endif /* not lint */
44 1.1 dyoung
45 1.1 dyoung #ifndef lint
46 1.1 dyoung #if 0
47 1.1 dyoung static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95";
48 1.1 dyoung /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
49 1.1 dyoung #else
50 1.1 dyoung __RCSID("$NetBSD: main.c,v 1.1 2005/06/12 19:18:34 dyoung Exp $");
51 1.1 dyoung #endif
52 1.1 dyoung #endif /* not lint */
53 1.1 dyoung
54 1.1 dyoung #include <sys/param.h>
55 1.1 dyoung #include <sys/file.h>
56 1.1 dyoung #include <sys/stat.h>
57 1.1 dyoung #include <sys/wait.h>
58 1.1 dyoung #define DKTYPENAMES
59 1.1 dyoung #define FSTYPENAMES
60 1.1 dyoung
61 1.1 dyoung #include <ctype.h>
62 1.1 dyoung #include <err.h>
63 1.1 dyoung #include <errno.h>
64 1.1 dyoung #include <unistd.h>
65 1.1 dyoung #include <signal.h>
66 1.1 dyoung #include <string.h>
67 1.1 dyoung #include <stdio.h>
68 1.1 dyoung #include <stdlib.h>
69 1.1 dyoung #include <unistd.h>
70 1.1 dyoung
71 1.1 dyoung #include <ufs/ufs/dinode.h>
72 1.1 dyoung #include <ufs/ffs/fs.h>
73 1.1 dyoung
74 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H
75 1.1 dyoung #include <nbinclude/sys/disklabel.h>
76 1.1 dyoung #include <nbinclude/sys/bootblock.h>
77 1.1 dyoung #include "../../include/util.h"
78 1.1 dyoung #include "../../include/disktab.h"
79 1.1 dyoung #else
80 1.1 dyoung #include <sys/ioctl.h>
81 1.1 dyoung #include <sys/disklabel.h>
82 1.1 dyoung #include <sys/bootblock.h>
83 1.1 dyoung #include <util.h>
84 1.1 dyoung #include <disktab.h>
85 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
86 1.1 dyoung
87 1.1 dyoung #include "pathnames.h"
88 1.1 dyoung #include "extern.h"
89 1.1 dyoung #include "dkcksum.h"
90 1.1 dyoung
91 1.1 dyoung /*
92 1.1 dyoung * Disklabel: read and write disklabels.
93 1.1 dyoung * The label is usually placed on one of the first sectors of the disk.
94 1.1 dyoung * Many machines also place a bootstrap in the same area,
95 1.1 dyoung * in which case the label is embedded in the bootstrap.
96 1.1 dyoung * The bootstrap source must leave space at the proper offset
97 1.1 dyoung * for the label on such machines.
98 1.1 dyoung */
99 1.1 dyoung
100 1.1 dyoung #ifndef BBSIZE
101 1.1 dyoung #define BBSIZE 8192 /* size of boot area, with label */
102 1.1 dyoung #endif
103 1.1 dyoung
104 1.1 dyoung #ifndef NUMBOOT
105 1.1 dyoung #define NUMBOOT 0
106 1.1 dyoung #endif
107 1.1 dyoung
108 1.1 dyoung #define DEFEDITOR _PATH_VI
109 1.1 dyoung
110 1.1 dyoung static char *dkname;
111 1.1 dyoung static char tmpfil[MAXPATHLEN];
112 1.1 dyoung
113 1.1 dyoung static char namebuf[BBSIZE], *np = namebuf;
114 1.1 dyoung static struct disklabel lab;
115 1.1 dyoung
116 1.1 dyoung char bootarea[BBSIZE];
117 1.1 dyoung char *specname;
118 1.1 dyoung
119 1.1 dyoung
120 1.1 dyoung #if NUMBOOT > 0
121 1.1 dyoung static int installboot; /* non-zero if we should install a boot program */
122 1.1 dyoung static char *bootbuf; /* pointer to buffer with remainder of boot prog */
123 1.1 dyoung static int bootsize; /* size of remaining boot program */
124 1.1 dyoung static char *xxboot; /* primary boot */
125 1.1 dyoung static char *bootxx; /* secondary boot */
126 1.1 dyoung static char boot0[MAXPATHLEN];
127 1.1 dyoung #if NUMBOOT > 1
128 1.1 dyoung static char boot1[MAXPATHLEN];
129 1.1 dyoung #endif /* NUMBOOT > 1 */
130 1.1 dyoung #endif /* NUMBOOT > 0 */
131 1.1 dyoung
132 1.1 dyoung static enum {
133 1.1 dyoung UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, WRITE, WRITEBOOT, INTERACT
134 1.1 dyoung } op = UNSPEC;
135 1.1 dyoung
136 1.1 dyoung static int Fflag;
137 1.1 dyoung static int rflag;
138 1.1 dyoung static int tflag;
139 1.1 dyoung int Cflag;
140 1.1 dyoung static int Iflag;
141 1.1 dyoung
142 1.1 dyoung #define COMMON_OPTIONS "BCFINRWb:ef:irs:tw"
143 1.1 dyoung
144 1.1 dyoung #ifdef DEBUG
145 1.1 dyoung static int debug;
146 1.1 dyoung #define OPTIONS COMMON_OPTIONS "d"
147 1.1 dyoung #else /* ! DEBUG */
148 1.1 dyoung #define OPTIONS COMMON_OPTIONS
149 1.1 dyoung #endif /* ! DEBUG */
150 1.1 dyoung
151 1.1 dyoung #ifdef USE_MBR
152 1.1 dyoung static struct mbr_partition *dosdp; /* i386 DOS partition, if found */
153 1.1 dyoung static struct mbr_partition *readmbr(int);
154 1.1 dyoung #endif /* USE_MBR */
155 1.1 dyoung
156 1.1 dyoung #ifdef USE_ACORN
157 1.1 dyoung static u_int filecore_partition_offset;
158 1.1 dyoung static u_int get_filecore_partition(int);
159 1.1 dyoung static int filecore_checksum(u_char *);
160 1.1 dyoung #endif /* USE_ACORN */
161 1.1 dyoung
162 1.1 dyoung #if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
163 1.1 dyoung static void confirm(const char *);
164 1.1 dyoung #endif
165 1.1 dyoung
166 1.1 dyoung int main(int, char *[]);
167 1.1 dyoung
168 1.1 dyoung static void makedisktab(FILE *, struct disklabel *);
169 1.1 dyoung static void makelabel(const char *, const char *,
170 1.1 dyoung struct disklabel *);
171 1.1 dyoung static void l_perror(const char *);
172 1.1 dyoung static struct disklabel *readlabel(int);
173 1.1 dyoung static struct disklabel *makebootarea(char *, struct disklabel *, int);
174 1.1 dyoung static int edit(struct disklabel *, int);
175 1.1 dyoung static int editit(void);
176 1.1 dyoung static char *skip(char *);
177 1.1 dyoung static char *word(char *);
178 1.1 dyoung static int getasciilabel(FILE *, struct disklabel *);
179 1.1 dyoung #if NUMBOOT > 0
180 1.1 dyoung static void setbootflag(struct disklabel *);
181 1.1 dyoung #endif
182 1.1 dyoung static void usage(void);
183 1.1 dyoung static int getulong(const char *, char, char **,
184 1.1 dyoung unsigned long *, unsigned long);
185 1.1 dyoung #define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX)
186 1.1 dyoung #define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX)
187 1.1 dyoung #define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX)
188 1.1 dyoung
189 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H
190 1.1 dyoung #define GETLABELOFFSET() LABELOFFSET
191 1.1 dyoung #define GETLABELSECTOR() LABELSECTOR
192 1.1 dyoung #else /* HAVE_NBTOOL_CONFIG_H */
193 1.1 dyoung #define GETLABELOFFSET() getlabeloffset()
194 1.1 dyoung #define GETLABELSECTOR() getlabelsector()
195 1.1 dyoung #endif
196 1.1 dyoung
197 1.1 dyoung int
198 1.1 dyoung main(int argc, char *argv[])
199 1.1 dyoung {
200 1.1 dyoung struct disklabel *lp;
201 1.1 dyoung FILE *t;
202 1.1 dyoung int ch, f, writable, error;
203 1.1 dyoung
204 1.1 dyoung error = 0;
205 1.1 dyoung while ((ch = getopt(argc, argv, OPTIONS)) != -1)
206 1.1 dyoung switch (ch) {
207 1.1 dyoung #if NUMBOOT > 0
208 1.1 dyoung case 'B':
209 1.1 dyoung ++installboot;
210 1.1 dyoung break;
211 1.1 dyoung case 'b':
212 1.1 dyoung xxboot = optarg;
213 1.1 dyoung break;
214 1.1 dyoung #if NUMBOOT > 1
215 1.1 dyoung case 's':
216 1.1 dyoung bootxx = optarg;
217 1.1 dyoung break;
218 1.1 dyoung #endif /* NUMBOOT > 1 */
219 1.1 dyoung #endif /* NUMBOOT > 0 */
220 1.1 dyoung case 'C':
221 1.1 dyoung ++Cflag;
222 1.1 dyoung break;
223 1.1 dyoung case 'F':
224 1.1 dyoung ++Fflag;
225 1.1 dyoung break;
226 1.1 dyoung case 'I':
227 1.1 dyoung ++Iflag;
228 1.1 dyoung break;
229 1.1 dyoung case 'R':
230 1.1 dyoung if (op != UNSPEC)
231 1.1 dyoung usage();
232 1.1 dyoung op = RESTORE;
233 1.1 dyoung break;
234 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
235 1.1 dyoung case 'N':
236 1.1 dyoung if (op != UNSPEC)
237 1.1 dyoung usage();
238 1.1 dyoung writable = 0;
239 1.1 dyoung op = SETWRITABLE;
240 1.1 dyoung break;
241 1.1 dyoung case 'W':
242 1.1 dyoung if (op != UNSPEC)
243 1.1 dyoung usage();
244 1.1 dyoung writable = 1;
245 1.1 dyoung op = SETWRITABLE;
246 1.1 dyoung break;
247 1.1 dyoung #endif /* !HAVE_NBTOOL_CONFIG_H */
248 1.1 dyoung case 'e':
249 1.1 dyoung if (op != UNSPEC)
250 1.1 dyoung usage();
251 1.1 dyoung op = EDIT;
252 1.1 dyoung break;
253 1.1 dyoung case 'f':
254 1.1 dyoung if (setdisktab(optarg) == -1)
255 1.1 dyoung usage();
256 1.1 dyoung break;
257 1.1 dyoung case 'i':
258 1.1 dyoung if (op != UNSPEC)
259 1.1 dyoung usage();
260 1.1 dyoung op = INTERACT;
261 1.1 dyoung break;
262 1.1 dyoung case 't':
263 1.1 dyoung ++tflag;
264 1.1 dyoung break;
265 1.1 dyoung case 'r':
266 1.1 dyoung ++rflag;
267 1.1 dyoung break;
268 1.1 dyoung case 'w':
269 1.1 dyoung if (op != UNSPEC)
270 1.1 dyoung usage();
271 1.1 dyoung op = WRITE;
272 1.1 dyoung break;
273 1.1 dyoung #ifdef DEBUG
274 1.1 dyoung case 'd':
275 1.1 dyoung debug++;
276 1.1 dyoung break;
277 1.1 dyoung #endif
278 1.1 dyoung case '?':
279 1.1 dyoung default:
280 1.1 dyoung usage();
281 1.1 dyoung }
282 1.1 dyoung argc -= optind;
283 1.1 dyoung argv += optind;
284 1.1 dyoung
285 1.1 dyoung #if NUMBOOT > 0
286 1.1 dyoung if (installboot) {
287 1.1 dyoung rflag++;
288 1.1 dyoung if (op == UNSPEC)
289 1.1 dyoung op = WRITEBOOT;
290 1.1 dyoung } else {
291 1.1 dyoung if (op == UNSPEC)
292 1.1 dyoung op = READ;
293 1.1 dyoung }
294 1.1 dyoung #else /* NUMBOOT <= 0 */
295 1.1 dyoung if (op == UNSPEC)
296 1.1 dyoung op = READ;
297 1.1 dyoung #endif /* NUMBOOT <= 0 */
298 1.1 dyoung
299 1.1 dyoung if (argc < 1)
300 1.1 dyoung usage();
301 1.1 dyoung
302 1.1 dyoung if (Iflag && op != EDIT && op != INTERACT)
303 1.1 dyoung usage();
304 1.1 dyoung
305 1.1 dyoung dkname = argv[0];
306 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H
307 1.1 dyoung f = open(dkname, op == READ ? O_RDONLY : O_RDWR, 0);
308 1.1 dyoung strlcpy(np, dkname, MAXPATHLEN);
309 1.1 dyoung #else
310 1.1 dyoung f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, np, MAXPATHLEN, 0);
311 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
312 1.1 dyoung specname = np;
313 1.1 dyoung np += strlen(specname) + 1;
314 1.1 dyoung if (f < 0)
315 1.1 dyoung err(4, "%s", specname);
316 1.1 dyoung
317 1.1 dyoung #ifdef USE_MBR
318 1.1 dyoung /*
319 1.1 dyoung * Check for presence of DOS partition table in
320 1.1 dyoung * master boot record. Return pointer to NetBSD/i386
321 1.1 dyoung * partition, if present.
322 1.1 dyoung */
323 1.1 dyoung dosdp = readmbr(f);
324 1.1 dyoung #endif /* USE_MBR */
325 1.1 dyoung
326 1.1 dyoung #ifdef USE_ACORN
327 1.1 dyoung /*
328 1.1 dyoung * Check for the presence of a RiscOS filecore boot block
329 1.1 dyoung * indicating an ADFS file system on the disc.
330 1.1 dyoung * Return the offset to the NetBSD part of the disc if
331 1.1 dyoung * this can be determined.
332 1.1 dyoung * This routine will terminate disklabel if the disc
333 1.1 dyoung * is found to be ADFS only.
334 1.1 dyoung */
335 1.1 dyoung filecore_partition_offset = get_filecore_partition(f);
336 1.1 dyoung #endif /* USE_ACORN */
337 1.1 dyoung
338 1.1 dyoung switch (op) {
339 1.1 dyoung
340 1.1 dyoung case EDIT:
341 1.1 dyoung if (argc != 1)
342 1.1 dyoung usage();
343 1.1 dyoung lp = readlabel(f);
344 1.1 dyoung error = edit(lp, f);
345 1.1 dyoung break;
346 1.1 dyoung
347 1.1 dyoung case INTERACT:
348 1.1 dyoung if (argc != 1)
349 1.1 dyoung usage();
350 1.1 dyoung lp = readlabel(f);
351 1.1 dyoung /*
352 1.1 dyoung * XXX: Fill some default values so checklabel does not fail
353 1.1 dyoung */
354 1.1 dyoung if (lp->d_bbsize == 0)
355 1.1 dyoung lp->d_bbsize = BBSIZE;
356 1.1 dyoung if (lp->d_sbsize == 0)
357 1.1 dyoung lp->d_sbsize = SBLOCKSIZE;
358 1.1 dyoung interact(lp, f);
359 1.1 dyoung break;
360 1.1 dyoung
361 1.1 dyoung case READ:
362 1.1 dyoung if (argc != 1)
363 1.1 dyoung usage();
364 1.1 dyoung lp = readlabel(f);
365 1.1 dyoung if (tflag)
366 1.1 dyoung makedisktab(stdout, lp);
367 1.1 dyoung else {
368 1.1 dyoung showinfo(stdout, lp, specname);
369 1.1 dyoung showpartitions(stdout, lp, Cflag);
370 1.1 dyoung }
371 1.1 dyoung error = checklabel(lp);
372 1.1 dyoung if (error)
373 1.1 dyoung error += 100;
374 1.1 dyoung break;
375 1.1 dyoung
376 1.1 dyoung case RESTORE:
377 1.1 dyoung if (argc < 2 || argc > 3)
378 1.1 dyoung usage();
379 1.1 dyoung #if NUMBOOT > 0
380 1.1 dyoung if (installboot && argc == 3)
381 1.1 dyoung makelabel(argv[2], (char *)0, &lab);
382 1.1 dyoung #endif
383 1.1 dyoung lp = makebootarea(bootarea, &lab, f);
384 1.1 dyoung if (!(t = fopen(argv[1], "r")))
385 1.1 dyoung err(4, "%s", argv[1]);
386 1.1 dyoung if (getasciilabel(t, lp))
387 1.1 dyoung error = writelabel(f, bootarea, lp);
388 1.1 dyoung else
389 1.1 dyoung error = 1;
390 1.1 dyoung break;
391 1.1 dyoung
392 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
393 1.1 dyoung case SETWRITABLE:
394 1.1 dyoung if (ioctl(f, DIOCWLABEL, (char *)&writable) < 0)
395 1.1 dyoung err(4, "ioctl DIOCWLABEL");
396 1.1 dyoung break;
397 1.1 dyoung #endif /* !HAVE_NBTOOL_CONFIG_H */
398 1.1 dyoung
399 1.1 dyoung case WRITE:
400 1.1 dyoung if (argc < 2 || argc > 3)
401 1.1 dyoung usage();
402 1.1 dyoung makelabel(argv[1], argc == 3 ? argv[2] : (char *)0, &lab);
403 1.1 dyoung lp = makebootarea(bootarea, &lab, f);
404 1.1 dyoung *lp = lab;
405 1.1 dyoung if (checklabel(lp) == 0)
406 1.1 dyoung error = writelabel(f, bootarea, lp);
407 1.1 dyoung else
408 1.1 dyoung error = 1;
409 1.1 dyoung break;
410 1.1 dyoung
411 1.1 dyoung case WRITEBOOT:
412 1.1 dyoung #if NUMBOOT > 0
413 1.1 dyoung {
414 1.1 dyoung struct disklabel tlab;
415 1.1 dyoung
416 1.1 dyoung lp = readlabel(f);
417 1.1 dyoung tlab = *lp;
418 1.1 dyoung if (argc == 2)
419 1.1 dyoung makelabel(argv[1], (char *)0, &lab);
420 1.1 dyoung lp = makebootarea(bootarea, &lab, f);
421 1.1 dyoung *lp = tlab;
422 1.1 dyoung if (checklabel(lp) == 0)
423 1.1 dyoung error = writelabel(f, bootarea, lp);
424 1.1 dyoung else
425 1.1 dyoung error = 1;
426 1.1 dyoung break;
427 1.1 dyoung }
428 1.1 dyoung #endif /* NUMBOOT > 0 */
429 1.1 dyoung
430 1.1 dyoung case UNSPEC:
431 1.1 dyoung usage();
432 1.1 dyoung
433 1.1 dyoung }
434 1.1 dyoung exit(error);
435 1.1 dyoung }
436 1.1 dyoung
437 1.1 dyoung /*
438 1.1 dyoung * Construct a prototype disklabel from /etc/disktab. As a side
439 1.1 dyoung * effect, set the names of the primary and secondary boot files
440 1.1 dyoung * if specified.
441 1.1 dyoung */
442 1.1 dyoung static void
443 1.1 dyoung makelabel(const char *type, const char *name, struct disklabel *lp)
444 1.1 dyoung {
445 1.1 dyoung struct disklabel *dp;
446 1.1 dyoung
447 1.1 dyoung dp = getdiskbyname(type);
448 1.1 dyoung if (dp == NULL)
449 1.1 dyoung errx(1, "unknown disk type: %s", type);
450 1.1 dyoung *lp = *dp;
451 1.1 dyoung
452 1.1 dyoung #if NUMBOOT > 0
453 1.1 dyoung /*
454 1.1 dyoung * Set bootstrap name(s).
455 1.1 dyoung * 1. If set from command line, use those,
456 1.1 dyoung * 2. otherwise, check if disktab specifies them (b0 or b1),
457 1.1 dyoung * 3. otherwise, makebootarea() will choose ones based on the name
458 1.1 dyoung * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
459 1.1 dyoung */
460 1.1 dyoung if (!xxboot && lp->d_boot0) {
461 1.1 dyoung if (*lp->d_boot0 != '/')
462 1.1 dyoung (void)snprintf(boot0, sizeof(boot0), "%s/%s",
463 1.1 dyoung _PATH_BOOTDIR, lp->d_boot0);
464 1.1 dyoung else
465 1.1 dyoung (void)strlcpy(boot0, lp->d_boot0, sizeof(boot0));
466 1.1 dyoung xxboot = boot0;
467 1.1 dyoung }
468 1.1 dyoung
469 1.1 dyoung #if NUMBOOT > 1
470 1.1 dyoung if (!bootxx && lp->d_boot1) {
471 1.1 dyoung if (*lp->d_boot1 != '/')
472 1.1 dyoung (void)snprintf(boot1, sizeof(boot1), "%s/%s",
473 1.1 dyoung _PATH_BOOTDIR, lp->d_boot1);
474 1.1 dyoung else
475 1.1 dyoung (void)strlcpy(boot1, lp->d_boot1, sizeof(boot1));
476 1.1 dyoung bootxx = boot1;
477 1.1 dyoung }
478 1.1 dyoung #endif /* NUMBOOT > 1 */
479 1.1 dyoung #endif /* NUMBOOT > 0 */
480 1.1 dyoung
481 1.1 dyoung /* d_packname is union d_boot[01], so zero */
482 1.1 dyoung (void) memset(lp->d_packname, 0, sizeof(lp->d_packname));
483 1.1 dyoung if (name)
484 1.1 dyoung (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
485 1.1 dyoung }
486 1.1 dyoung
487 1.1 dyoung #if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
488 1.1 dyoung static void
489 1.1 dyoung confirm(const char *txt)
490 1.1 dyoung {
491 1.1 dyoung int first, ch;
492 1.1 dyoung
493 1.1 dyoung (void) printf("%s? [n]: ", txt);
494 1.1 dyoung (void) fflush(stdout);
495 1.1 dyoung first = ch = getchar();
496 1.1 dyoung while (ch != '\n' && ch != EOF)
497 1.1 dyoung ch = getchar();
498 1.1 dyoung if (first != 'y' && first != 'Y')
499 1.1 dyoung exit(0);
500 1.1 dyoung }
501 1.1 dyoung #endif /* USE_MBR || USE_ACORN && notyet */
502 1.1 dyoung
503 1.1 dyoung int
504 1.1 dyoung writelabel(int f, char *boot, struct disklabel *lp)
505 1.1 dyoung {
506 1.1 dyoung int writable;
507 1.1 dyoung off_t sectoffset;
508 1.1 dyoung
509 1.1 dyoung sectoffset = 0;
510 1.1 dyoung #if NUMBOOT > 0
511 1.1 dyoung setbootflag(lp);
512 1.1 dyoung #endif
513 1.1 dyoung lp->d_magic = DISKMAGIC;
514 1.1 dyoung lp->d_magic2 = DISKMAGIC;
515 1.1 dyoung lp->d_checksum = 0;
516 1.1 dyoung lp->d_checksum = dkcksum(lp);
517 1.1 dyoung
518 1.1 dyoung if (Fflag || rflag || Iflag)
519 1.1 dyoung {
520 1.1 dyoung #ifdef USE_MBR
521 1.1 dyoung struct partition *pp = &lp->d_partitions[2];
522 1.1 dyoung
523 1.1 dyoung /*
524 1.1 dyoung * If NetBSD/i386 DOS partition is missing, or if
525 1.1 dyoung * the label to be written is not within partition,
526 1.1 dyoung * prompt first. Need to allow this in case operator
527 1.1 dyoung * wants to convert the drive for dedicated use.
528 1.1 dyoung */
529 1.1 dyoung if (dosdp) {
530 1.1 dyoung if (dosdp->mbrp_start != pp->p_offset) {
531 1.1 dyoung printf("NetBSD slice at %u, "
532 1.1 dyoung "partition C at %u\n", dosdp->mbrp_start,
533 1.1 dyoung pp->p_offset);
534 1.1 dyoung confirm("Write outside MBR partition");
535 1.1 dyoung }
536 1.1 dyoung sectoffset = (off_t)pp->p_offset * lp->d_secsize;
537 1.1 dyoung } else {
538 1.1 dyoung sectoffset = 0;
539 1.1 dyoung }
540 1.1 dyoung #endif /* USE_MBR */
541 1.1 dyoung
542 1.1 dyoung #ifdef USE_ACORN
543 1.1 dyoung /* XXX */
544 1.1 dyoung sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
545 1.1 dyoung #endif /* USE_ACORN */
546 1.1 dyoung
547 1.1 dyoung /*
548 1.1 dyoung * First set the kernel disk label,
549 1.1 dyoung * then write a label to the raw disk.
550 1.1 dyoung * If the SDINFO ioctl fails because it is unimplemented,
551 1.1 dyoung * keep going; otherwise, the kernel consistency checks
552 1.1 dyoung * may prevent us from changing the current (in-core)
553 1.1 dyoung * label.
554 1.1 dyoung */
555 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
556 1.1 dyoung if (!Fflag && ioctl(f, DIOCSDINFO, lp) < 0 &&
557 1.1 dyoung errno != ENODEV && errno != ENOTTY) {
558 1.1 dyoung l_perror("ioctl DIOCSDINFO");
559 1.1 dyoung return (1);
560 1.1 dyoung }
561 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
562 1.1 dyoung if (lseek(f, sectoffset, SEEK_SET) < 0) {
563 1.1 dyoung perror("lseek");
564 1.1 dyoung return (1);
565 1.1 dyoung }
566 1.1 dyoung /*
567 1.1 dyoung * write enable label sector before write (if necessary),
568 1.1 dyoung * disable after writing.
569 1.1 dyoung */
570 1.1 dyoung writable = 1;
571 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
572 1.1 dyoung if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
573 1.1 dyoung perror("ioctl DIOCWLABEL");
574 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
575 1.1 dyoung
576 1.1 dyoung #ifdef __alpha__
577 1.1 dyoung /*
578 1.1 dyoung * The Alpha requires that the boot block be checksummed.
579 1.1 dyoung * The NetBSD/alpha disklabel.h provides a macro to do it.
580 1.1 dyoung */
581 1.1 dyoung {
582 1.1 dyoung struct alpha_boot_block *bb;
583 1.1 dyoung
584 1.1 dyoung bb = (struct alpha_boot_block *)boot;
585 1.1 dyoung ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
586 1.1 dyoung }
587 1.1 dyoung #endif /* __alpha__ */
588 1.1 dyoung
589 1.1 dyoung if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
590 1.1 dyoung perror("write");
591 1.1 dyoung return (1);
592 1.1 dyoung }
593 1.1 dyoung
594 1.1 dyoung #if NUMBOOT > 0
595 1.1 dyoung /*
596 1.1 dyoung * Output the remainder of the disklabel
597 1.1 dyoung */
598 1.1 dyoung if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
599 1.1 dyoung perror("write");
600 1.1 dyoung return (1);
601 1.1 dyoung }
602 1.1 dyoung #endif /* NUMBOOT > 0 */
603 1.1 dyoung
604 1.1 dyoung writable = 0;
605 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
606 1.1 dyoung if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
607 1.1 dyoung perror("ioctl DIOCWLABEL");
608 1.1 dyoung /*
609 1.1 dyoung * Now issue a DIOCWDINFO. This will let the kernel convert the
610 1.1 dyoung * disklabel to some machdep format if needed.
611 1.1 dyoung */
612 1.1 dyoung if (!Fflag && ioctl(f, DIOCWDINFO, lp) < 0) {
613 1.1 dyoung l_perror("ioctl DIOCWDINFO");
614 1.1 dyoung return (1);
615 1.1 dyoung }
616 1.1 dyoung #endif /* !HAVE_NBTOOL_CONFIG_H */
617 1.1 dyoung } else {
618 1.1 dyoung #if !HAVE_NBTOOL_CONFIG_H
619 1.1 dyoung if (ioctl(f, DIOCWDINFO, lp) < 0) {
620 1.1 dyoung l_perror("ioctl DIOCWDINFO");
621 1.1 dyoung return (1);
622 1.1 dyoung }
623 1.1 dyoung #else
624 1.1 dyoung errx(1, "use -F, -r, or -I");
625 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
626 1.1 dyoung }
627 1.1 dyoung
628 1.1 dyoung #ifdef __vax__
629 1.1 dyoung if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
630 1.1 dyoung daddr_t alt;
631 1.1 dyoung int i;
632 1.1 dyoung
633 1.1 dyoung alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
634 1.1 dyoung for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
635 1.1 dyoung (void)lseek(f, (off_t)(alt + i) * lp->d_secsize,
636 1.1 dyoung SEEK_SET);
637 1.1 dyoung if (write(f, boot, lp->d_secsize) < lp->d_secsize)
638 1.1 dyoung warn("alternate label %d write", i/2);
639 1.1 dyoung }
640 1.1 dyoung }
641 1.1 dyoung #endif /* __vax__ */
642 1.1 dyoung
643 1.1 dyoung return (0);
644 1.1 dyoung }
645 1.1 dyoung
646 1.1 dyoung static void
647 1.1 dyoung l_perror(const char *s)
648 1.1 dyoung {
649 1.1 dyoung
650 1.1 dyoung switch (errno) {
651 1.1 dyoung
652 1.1 dyoung case ESRCH:
653 1.1 dyoung warnx("%s: No disk label on disk;\n"
654 1.1 dyoung "use \"disklabel -I\" to install initial label", s);
655 1.1 dyoung break;
656 1.1 dyoung
657 1.1 dyoung case EINVAL:
658 1.1 dyoung warnx("%s: Label magic number or checksum is wrong!\n"
659 1.1 dyoung "(disklabel or kernel is out of date?)", s);
660 1.1 dyoung break;
661 1.1 dyoung
662 1.1 dyoung case EBUSY:
663 1.1 dyoung warnx("%s: Open partition would move or shrink", s);
664 1.1 dyoung break;
665 1.1 dyoung
666 1.1 dyoung case EXDEV:
667 1.1 dyoung warnx("%s: Labeled partition or 'a' partition must start"
668 1.1 dyoung " at beginning of disk", s);
669 1.1 dyoung break;
670 1.1 dyoung
671 1.1 dyoung default:
672 1.1 dyoung warn("%s", s);
673 1.1 dyoung break;
674 1.1 dyoung }
675 1.1 dyoung }
676 1.1 dyoung
677 1.1 dyoung #ifdef USE_MBR
678 1.1 dyoung /*
679 1.1 dyoung * Fetch DOS partition table from disk.
680 1.1 dyoung */
681 1.1 dyoung static struct mbr_partition *
682 1.1 dyoung readmbr(int f)
683 1.1 dyoung {
684 1.1 dyoung struct mbr_partition *dp;
685 1.1 dyoung struct mbr_sector mbr;
686 1.1 dyoung int part;
687 1.1 dyoung u_int ext_base, next_ext, this_ext;
688 1.1 dyoung static struct mbr_partition netbsd_part;
689 1.1 dyoung
690 1.1 dyoung /*
691 1.1 dyoung * Don't (yet) know disk geometry (BIOS), use
692 1.1 dyoung * partition table to find NetBSD/i386 partition, and obtain
693 1.1 dyoung * disklabel from there.
694 1.1 dyoung */
695 1.1 dyoung
696 1.1 dyoung ext_base = 0;
697 1.1 dyoung next_ext = 0;
698 1.1 dyoung for (;;) {
699 1.1 dyoung this_ext = next_ext;
700 1.1 dyoung next_ext = 0;
701 1.1 dyoung if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE)
702 1.1 dyoung != sizeof(mbr)) {
703 1.1 dyoung warn("Can't read master boot record %d", this_ext);
704 1.1 dyoung return 0;
705 1.1 dyoung }
706 1.1 dyoung
707 1.1 dyoung /* Check if table is valid. */
708 1.1 dyoung if (mbr.mbr_magic != htole16(MBR_MAGIC)) {
709 1.1 dyoung warnx("Invalid signature in mbr record %d", this_ext);
710 1.1 dyoung return 0;
711 1.1 dyoung }
712 1.1 dyoung
713 1.1 dyoung dp = &mbr.mbr_parts[0];
714 1.1 dyoung #if defined(_no_longer_needed) && !defined(__i386__) && !defined(__x86_64__)
715 1.1 dyoung /* avoid alignment error */
716 1.1 dyoung memcpy(mbr, dp, MBR_PART_COUNT * sizeof(*dp));
717 1.1 dyoung dp = (struct mbr_partition *)mbr;
718 1.1 dyoung #endif /* ! __i386__ */
719 1.1 dyoung
720 1.1 dyoung /* Find NetBSD partition. */
721 1.1 dyoung for (part = 0; part < MBR_PART_COUNT; dp++, part++) {
722 1.1 dyoung dp->mbrp_start = le32toh(dp->mbrp_start);
723 1.1 dyoung dp->mbrp_size = le32toh(dp->mbrp_size);
724 1.1 dyoung switch (dp->mbrp_type) {
725 1.1 dyoung case MBR_PTYPE_NETBSD:
726 1.1 dyoung netbsd_part = *dp;
727 1.1 dyoung break;
728 1.1 dyoung case MBR_PTYPE_EXT:
729 1.1 dyoung case MBR_PTYPE_EXT_LBA:
730 1.1 dyoung case MBR_PTYPE_EXT_LNX:
731 1.1 dyoung next_ext = dp->mbrp_start;
732 1.1 dyoung continue;
733 1.1 dyoung #ifdef COMPAT_386BSD_MBRPART
734 1.1 dyoung case MBR_PTYPE_386BSD:
735 1.1 dyoung if (ext_base == 0)
736 1.1 dyoung netbsd_part = *dp;
737 1.1 dyoung continue;
738 1.1 dyoung #endif /* COMPAT_386BSD_MBRPART */
739 1.1 dyoung default:
740 1.1 dyoung continue;
741 1.1 dyoung }
742 1.1 dyoung break;
743 1.1 dyoung }
744 1.1 dyoung if (part < MBR_PART_COUNT)
745 1.1 dyoung /* We found a netbsd partition */
746 1.1 dyoung break;
747 1.1 dyoung if (next_ext == 0)
748 1.1 dyoung /* No more extended partitions */
749 1.1 dyoung break;
750 1.1 dyoung next_ext += ext_base;
751 1.1 dyoung if (ext_base == 0)
752 1.1 dyoung ext_base = next_ext;
753 1.1 dyoung
754 1.1 dyoung if (next_ext <= this_ext) {
755 1.1 dyoung warnx("Invalid extended chain %x <= %x",
756 1.1 dyoung next_ext, this_ext);
757 1.1 dyoung break;
758 1.1 dyoung }
759 1.1 dyoung }
760 1.1 dyoung
761 1.1 dyoung if (netbsd_part.mbrp_type == 0)
762 1.1 dyoung return 0;
763 1.1 dyoung
764 1.1 dyoung netbsd_part.mbrp_start += this_ext;
765 1.1 dyoung return &netbsd_part;
766 1.1 dyoung }
767 1.1 dyoung #endif /* USE_MBR */
768 1.1 dyoung
769 1.1 dyoung #ifdef USE_ACORN
770 1.1 dyoung /*
771 1.1 dyoung * static int filecore_checksum(u_char *bootblock)
772 1.1 dyoung *
773 1.1 dyoung * Calculates the filecore boot block checksum. This is used to validate
774 1.1 dyoung * a filecore boot block on the disk. If a boot block is validated then
775 1.1 dyoung * it is used to locate the partition table. If the boot block is not
776 1.1 dyoung * validated, it is assumed that the whole disk is NetBSD.
777 1.1 dyoung *
778 1.1 dyoung * The basic algorithm is:
779 1.1 dyoung *
780 1.1 dyoung * for (each byte in block, excluding checksum) {
781 1.1 dyoung * sum += byte;
782 1.1 dyoung * if (sum > 255)
783 1.1 dyoung * sum -= 255;
784 1.1 dyoung * }
785 1.1 dyoung *
786 1.1 dyoung * That's equivalent to summing all of the bytes in the block
787 1.1 dyoung * (excluding the checksum byte, of course), then calculating the
788 1.1 dyoung * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
789 1.1 dyoung * expression may or may not yield a faster checksum function,
790 1.1 dyoung * but it's easier to reason about.
791 1.1 dyoung *
792 1.1 dyoung * Note that if you have a block filled with bytes of a single
793 1.1 dyoung * value "X" (regardless of that value!) and calculate the cksum
794 1.1 dyoung * of the block (excluding the checksum byte), you will _always_
795 1.1 dyoung * end up with a checksum of X. (Do the math; that can be derived
796 1.1 dyoung * from the checksum calculation function!) That means that
797 1.1 dyoung * blocks which contain bytes which all have the same value will
798 1.1 dyoung * always checksum properly. That's a _very_ unlikely occurence
799 1.1 dyoung * (probably impossible, actually) for a valid filecore boot block,
800 1.1 dyoung * so we treat such blocks as invalid.
801 1.1 dyoung */
802 1.1 dyoung static int
803 1.1 dyoung filecore_checksum(u_char *bootblock)
804 1.1 dyoung {
805 1.1 dyoung u_char byte0, accum_diff;
806 1.1 dyoung u_int sum;
807 1.1 dyoung int i;
808 1.1 dyoung
809 1.1 dyoung sum = 0;
810 1.1 dyoung accum_diff = 0;
811 1.1 dyoung byte0 = bootblock[0];
812 1.1 dyoung
813 1.1 dyoung /*
814 1.1 dyoung * Sum the contents of the block, keeping track of whether
815 1.1 dyoung * or not all bytes are the same. If 'accum_diff' ends up
816 1.1 dyoung * being zero, all of the bytes are, in fact, the same.
817 1.1 dyoung */
818 1.1 dyoung for (i = 0; i < 511; ++i) {
819 1.1 dyoung sum += bootblock[i];
820 1.1 dyoung accum_diff |= bootblock[i] ^ byte0;
821 1.1 dyoung }
822 1.1 dyoung
823 1.1 dyoung /*
824 1.1 dyoung * Check to see if the checksum byte is the same as the
825 1.1 dyoung * rest of the bytes, too. (Note that if all of the bytes
826 1.1 dyoung * are the same except the checksum, a checksum compare
827 1.1 dyoung * won't succeed, but that's not our problem.)
828 1.1 dyoung */
829 1.1 dyoung accum_diff |= bootblock[i] ^ byte0;
830 1.1 dyoung
831 1.1 dyoung /* All bytes in block are the same; call it invalid. */
832 1.1 dyoung if (accum_diff == 0)
833 1.1 dyoung return (-1);
834 1.1 dyoung
835 1.1 dyoung return (sum - ((sum - 1) / 255) * 255);
836 1.1 dyoung }
837 1.1 dyoung
838 1.1 dyoung /*
839 1.1 dyoung * Fetch filecore bootblock from disk and analyse it
840 1.1 dyoung */
841 1.1 dyoung static u_int
842 1.1 dyoung get_filecore_partition(int f)
843 1.1 dyoung {
844 1.1 dyoung struct filecore_bootblock *fcbb;
845 1.1 dyoung static char bb[DEV_BSIZE];
846 1.1 dyoung u_int offset;
847 1.1 dyoung
848 1.1 dyoung if (lseek(f, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0 ||
849 1.1 dyoung read(f, bb, sizeof(bb)) != sizeof(bb))
850 1.1 dyoung err(4, "can't read filecore boot block");
851 1.1 dyoung fcbb = (struct filecore_bootblock *)bb;
852 1.1 dyoung
853 1.1 dyoung /* Check if table is valid. */
854 1.1 dyoung if (filecore_checksum(bb) != fcbb->checksum)
855 1.1 dyoung return (0);
856 1.1 dyoung
857 1.1 dyoung /*
858 1.1 dyoung * Check for NetBSD/arm32 (RiscBSD) partition marker.
859 1.1 dyoung * If found the NetBSD disklabel location is easy.
860 1.1 dyoung */
861 1.1 dyoung offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8))
862 1.1 dyoung * fcbb->heads * fcbb->secspertrack;
863 1.1 dyoung if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
864 1.1 dyoung return (offset);
865 1.1 dyoung else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
866 1.1 dyoung struct riscix_partition_table *riscix_part;
867 1.1 dyoung int loop;
868 1.1 dyoung
869 1.1 dyoung /*
870 1.1 dyoung * Read the RISCiX partition table and search for the
871 1.1 dyoung * first partition named "RiscBSD", "NetBSD", or "Empty:"
872 1.1 dyoung *
873 1.1 dyoung * XXX is use of 'Empty:' really desirable?! -- cgd
874 1.1 dyoung */
875 1.1 dyoung
876 1.1 dyoung if (lseek(f, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0 ||
877 1.1 dyoung read(f, bb, sizeof(bb)) != sizeof(bb))
878 1.1 dyoung err(4, "can't read riscix partition table");
879 1.1 dyoung riscix_part = (struct riscix_partition_table *)bb;
880 1.1 dyoung
881 1.1 dyoung for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
882 1.1 dyoung if (strcmp(riscix_part->partitions[loop].rp_name,
883 1.1 dyoung "RiscBSD") == 0 ||
884 1.1 dyoung strcmp(riscix_part->partitions[loop].rp_name,
885 1.1 dyoung "NetBSD") == 0 ||
886 1.1 dyoung strcmp(riscix_part->partitions[loop].rp_name,
887 1.1 dyoung "Empty:") == 0) {
888 1.1 dyoung offset = riscix_part->partitions[loop].rp_start;
889 1.1 dyoung break;
890 1.1 dyoung }
891 1.1 dyoung }
892 1.1 dyoung if (loop == NRISCIX_PARTITIONS) {
893 1.1 dyoung /*
894 1.1 dyoung * Valid filecore boot block, RISCiX partition table
895 1.1 dyoung * but no NetBSD partition. We should leave this
896 1.1 dyoung * disc alone.
897 1.1 dyoung */
898 1.1 dyoung errx(4, "cannot label: no NetBSD partition found"
899 1.1 dyoung " in RISCiX partition table");
900 1.1 dyoung }
901 1.1 dyoung return (offset);
902 1.1 dyoung } else {
903 1.1 dyoung /*
904 1.1 dyoung * Valid filecore boot block and no non-ADFS partition.
905 1.1 dyoung * This means that the whole disc is allocated for ADFS
906 1.1 dyoung * so do not trash ! If the user really wants to put a
907 1.1 dyoung * NetBSD disklabel on the disc then they should remove
908 1.1 dyoung * the filecore boot block first with dd.
909 1.1 dyoung */
910 1.1 dyoung errx(4, "cannot label: filecore-only disk"
911 1.1 dyoung " (no non-ADFS partition)");
912 1.1 dyoung }
913 1.1 dyoung return (0);
914 1.1 dyoung }
915 1.1 dyoung #endif /* USE_ACORN */
916 1.1 dyoung
917 1.1 dyoung /*
918 1.1 dyoung * Fetch disklabel for disk.
919 1.1 dyoung * Use ioctl to get label unless -r flag is given.
920 1.1 dyoung */
921 1.1 dyoung static struct disklabel *
922 1.1 dyoung readlabel(int f)
923 1.1 dyoung {
924 1.1 dyoung struct disklabel *lp;
925 1.1 dyoung
926 1.1 dyoung if (Fflag || rflag || Iflag) {
927 1.1 dyoung const char *msg;
928 1.1 dyoung off_t sectoffset;
929 1.1 dyoung
930 1.1 dyoung msg = NULL;
931 1.1 dyoung sectoffset = 0;
932 1.1 dyoung
933 1.1 dyoung #ifdef USE_MBR
934 1.1 dyoung if (dosdp)
935 1.1 dyoung sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
936 1.1 dyoung #endif /* USE_MBR */
937 1.1 dyoung
938 1.1 dyoung #ifdef USE_ACORN
939 1.1 dyoung /* XXX */
940 1.1 dyoung sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
941 1.1 dyoung #endif /* USE_ACORN */
942 1.1 dyoung
943 1.1 dyoung if (lseek(f, sectoffset, SEEK_SET) < 0 ||
944 1.1 dyoung read(f, bootarea, BBSIZE) != BBSIZE)
945 1.1 dyoung err(4, "%s", specname);
946 1.1 dyoung
947 1.1 dyoung msg = "no disklabel";
948 1.1 dyoung for (lp = (struct disklabel *)bootarea;
949 1.1 dyoung lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
950 1.1 dyoung lp = (struct disklabel *)((char *)lp + sizeof(long))) {
951 1.1 dyoung if (lp->d_magic == DISKMAGIC &&
952 1.1 dyoung lp->d_magic2 == DISKMAGIC) {
953 1.1 dyoung if (lp->d_npartitions <= MAXPARTITIONS &&
954 1.1 dyoung dkcksum(lp) == 0)
955 1.1 dyoung return (lp);
956 1.1 dyoung msg = "disk label corrupted";
957 1.1 dyoung }
958 1.1 dyoung }
959 1.1 dyoung if (msg != NULL && !Iflag)
960 1.1 dyoung errx(1, "%s", msg);
961 1.1 dyoung #if HAVE_NBTOOL_CONFIG_H
962 1.1 dyoung goto err;
963 1.1 dyoung #else
964 1.1 dyoung /*
965 1.1 dyoung * There was no label on the disk. Get the fictious one
966 1.1 dyoung * as a basis for initialisation.
967 1.1 dyoung */
968 1.1 dyoung lp = makebootarea(bootarea, &lab, f);
969 1.1 dyoung if (ioctl(f, DIOCGDINFO, lp) < 0 &&
970 1.1 dyoung ioctl(f, DIOCGDEFLABEL, lp) < 0)
971 1.1 dyoung goto err;
972 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
973 1.1 dyoung } else {
974 1.1 dyoung #ifdef HAVE_NBTOOL_CONFIG_H
975 1.1 dyoung goto err;
976 1.1 dyoung #else
977 1.1 dyoung lp = &lab;
978 1.1 dyoung if (ioctl(f, DIOCGDINFO, lp) < 0)
979 1.1 dyoung err(4, "ioctl DIOCGDINFO");
980 1.1 dyoung #endif /* HAVE_NBTOOL_CONFIG_H */
981 1.1 dyoung }
982 1.1 dyoung return (lp);
983 1.1 dyoung err:
984 1.1 dyoung errx(1, "could not get initial label");
985 1.1 dyoung }
986 1.1 dyoung
987 1.1 dyoung /*
988 1.1 dyoung * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
989 1.1 dyoung * Returns a pointer to the disklabel portion of the bootarea.
990 1.1 dyoung */
991 1.1 dyoung static struct disklabel *
992 1.1 dyoung makebootarea(char *boot, struct disklabel *dp, int f)
993 1.1 dyoung {
994 1.1 dyoung struct disklabel *lp;
995 1.1 dyoung char *p;
996 1.1 dyoung daddr_t lsec;
997 1.1 dyoung off_t loff;
998 1.1 dyoung #if NUMBOOT > 0
999 1.1 dyoung int b;
1000 1.1 dyoung char *dkbasename;
1001 1.1 dyoung # if NUMBOOT <= 1
1002 1.1 dyoung struct stat sb;
1003 1.1 dyoung # endif
1004 1.1 dyoung #endif /* NUMBOOT > 0 */
1005 1.1 dyoung
1006 1.1 dyoung if ((lsec = GETLABELSECTOR()) < 0)
1007 1.1 dyoung err(4, "getlabelsector()");
1008 1.1 dyoung if ((loff = GETLABELOFFSET()) < 0)
1009 1.1 dyoung err(4, "getlabeloffset()");
1010 1.1 dyoung
1011 1.1 dyoung /* XXX */
1012 1.1 dyoung if (dp->d_secsize == 0) {
1013 1.1 dyoung dp->d_secsize = DEV_BSIZE;
1014 1.1 dyoung dp->d_bbsize = BBSIZE;
1015 1.1 dyoung }
1016 1.1 dyoung lp = (struct disklabel *) (boot + (lsec * dp->d_secsize) + loff);
1017 1.1 dyoung (void) memset(lp, 0, sizeof(*lp));
1018 1.1 dyoung
1019 1.1 dyoung #ifdef SAVEBOOTAREA
1020 1.1 dyoung /*
1021 1.1 dyoung * We must read the current bootarea so we don't clobber the
1022 1.1 dyoung * existing boot block, if any.
1023 1.1 dyoung */
1024 1.1 dyoung if (rflag || Iflag) {
1025 1.1 dyoung off_t sectoffset;
1026 1.1 dyoung
1027 1.1 dyoung sectoffset = 0;
1028 1.1 dyoung if (lseek(f, sectoffset, SEEK_SET) < 0 ||
1029 1.1 dyoung read(f, boot, BBSIZE) != BBSIZE)
1030 1.1 dyoung err(4, "%s", specname);
1031 1.1 dyoung (void) memset(lp, 0, sizeof(*lp));
1032 1.1 dyoung }
1033 1.1 dyoung #endif /* SAVEBOOTAREA */
1034 1.1 dyoung
1035 1.1 dyoung #if NUMBOOT > 0
1036 1.1 dyoung /*
1037 1.1 dyoung * If we are not installing a boot program but we are installing a
1038 1.1 dyoung * label on disk then we must read the current bootarea so we don't
1039 1.1 dyoung * clobber the existing boot.
1040 1.1 dyoung */
1041 1.1 dyoung if (!installboot) {
1042 1.1 dyoung if (rflag || Iflag) {
1043 1.1 dyoung off_t sectoffset;
1044 1.1 dyoung
1045 1.1 dyoung sectoffset = 0;
1046 1.1 dyoung #ifdef USE_MBR
1047 1.1 dyoung if (dosdp)
1048 1.1 dyoung sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
1049 1.1 dyoung #endif /* USE_MBR */
1050 1.1 dyoung
1051 1.1 dyoung #ifdef USE_ACORN
1052 1.1 dyoung /* XXX */
1053 1.1 dyoung sectoffset = (off_t)filecore_partition_offset
1054 1.1 dyoung * DEV_BSIZE;
1055 1.1 dyoung #endif /* USE_ACORN */
1056 1.1 dyoung
1057 1.1 dyoung if (lseek(f, sectoffset, SEEK_SET) < 0 ||
1058 1.1 dyoung read(f, boot, BBSIZE) != BBSIZE)
1059 1.1 dyoung err(4, "%s", specname);
1060 1.1 dyoung (void) memset(lp, 0, sizeof(*lp));
1061 1.1 dyoung }
1062 1.1 dyoung return (lp);
1063 1.1 dyoung }
1064 1.1 dyoung /*
1065 1.1 dyoung * We are installing a boot program. Determine the name(s) and
1066 1.1 dyoung * read them into the appropriate places in the boot area.
1067 1.1 dyoung */
1068 1.1 dyoung if (!xxboot || !bootxx) {
1069 1.1 dyoung dkbasename = np;
1070 1.1 dyoung if ((p = strrchr(dkname, '/')) == NULL)
1071 1.1 dyoung p = dkname;
1072 1.1 dyoung else
1073 1.1 dyoung p++;
1074 1.1 dyoung while (*p && !isdigit(*p & 0xff))
1075 1.1 dyoung *np++ = *p++;
1076 1.1 dyoung *np++ = '\0';
1077 1.1 dyoung
1078 1.1 dyoung if (!xxboot) {
1079 1.1 dyoung (void)sprintf(np, "%s/%sboot",
1080 1.1 dyoung _PATH_BOOTDIR, dkbasename);
1081 1.1 dyoung if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
1082 1.1 dyoung dkbasename++;
1083 1.1 dyoung xxboot = np;
1084 1.1 dyoung (void)sprintf(xxboot, "%s/%sboot",
1085 1.1 dyoung _PATH_BOOTDIR, dkbasename);
1086 1.1 dyoung np += strlen(xxboot) + 1;
1087 1.1 dyoung }
1088 1.1 dyoung #if NUMBOOT > 1
1089 1.1 dyoung if (!bootxx) {
1090 1.1 dyoung (void)sprintf(np, "%s/boot%s",
1091 1.1 dyoung _PATH_BOOTDIR, dkbasename);
1092 1.1 dyoung if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
1093 1.1 dyoung dkbasename++;
1094 1.1 dyoung bootxx = np;
1095 1.1 dyoung (void)sprintf(bootxx, "%s/boot%s",
1096 1.1 dyoung _PATH_BOOTDIR, dkbasename);
1097 1.1 dyoung np += strlen(bootxx) + 1;
1098 1.1 dyoung }
1099 1.1 dyoung #endif /* NUMBOOT > 1 */
1100 1.1 dyoung }
1101 1.1 dyoung
1102 1.1 dyoung #ifdef DEBUG
1103 1.1 dyoung if (debug)
1104 1.1 dyoung warnx("bootstraps: xxboot = %s, bootxx = %s", xxboot,
1105 1.1 dyoung bootxx ? bootxx : "NONE");
1106 1.1 dyoung #endif
1107 1.1 dyoung
1108 1.1 dyoung /*
1109 1.1 dyoung * Strange rules:
1110 1.1 dyoung * 1. One-piece bootstrap (hp300/hp800)
1111 1.1 dyoung * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
1112 1.1 dyoung * is remembered and written later following the bootarea.
1113 1.1 dyoung * 2. Two-piece bootstraps (vax/i386?/mips?)
1114 1.1 dyoung * up to d_secsize bytes of ``xxboot'' go in first d_secsize
1115 1.1 dyoung * bytes of bootarea, remaining d_bbsize-d_secsize filled
1116 1.1 dyoung * from ``bootxx''.
1117 1.1 dyoung */
1118 1.1 dyoung b = open(xxboot, O_RDONLY);
1119 1.1 dyoung if (b < 0)
1120 1.1 dyoung err(4, "%s", xxboot);
1121 1.1 dyoung #if NUMBOOT > 1
1122 1.1 dyoung if (read(b, boot, (int)dp->d_secsize) < 0)
1123 1.1 dyoung err(4, "%s", xxboot);
1124 1.1 dyoung (void)close(b);
1125 1.1 dyoung b = open(bootxx, O_RDONLY);
1126 1.1 dyoung if (b < 0)
1127 1.1 dyoung err(4, "%s", bootxx);
1128 1.1 dyoung if (read(b, &boot[dp->d_secsize],
1129 1.1 dyoung (int)(dp->d_bbsize-dp->d_secsize)) < 0)
1130 1.1 dyoung err(4, "%s", bootxx);
1131 1.1 dyoung #else /* NUMBOOT <= 1 */
1132 1.1 dyoung if (read(b, boot, (int)dp->d_bbsize) < 0)
1133 1.1 dyoung err(4, "%s", xxboot);
1134 1.1 dyoung (void)fstat(b, &sb);
1135 1.1 dyoung bootsize = (int)sb.st_size - dp->d_bbsize;
1136 1.1 dyoung if (bootsize > 0) {
1137 1.1 dyoung /* XXX assume d_secsize is a power of two */
1138 1.1 dyoung bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
1139 1.1 dyoung bootbuf = (char *)malloc((size_t)bootsize);
1140 1.1 dyoung if (bootbuf == 0)
1141 1.1 dyoung err(4, "%s", xxboot);
1142 1.1 dyoung if (read(b, bootbuf, bootsize) < 0) {
1143 1.1 dyoung free(bootbuf);
1144 1.1 dyoung err(4, "%s", xxboot);
1145 1.1 dyoung }
1146 1.1 dyoung }
1147 1.1 dyoung #endif /* NUMBOOT <= 1 */
1148 1.1 dyoung (void)close(b);
1149 1.1 dyoung #endif /* NUMBOOT > 0 */
1150 1.1 dyoung
1151 1.1 dyoung /*
1152 1.1 dyoung * Make sure no part of the bootstrap is written in the area
1153 1.1 dyoung * reserved for the label.
1154 1.1 dyoung */
1155 1.1 dyoung for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
1156 1.1 dyoung if (*p)
1157 1.1 dyoung errx(2, "Bootstrap doesn't leave room for disk label");
1158 1.1 dyoung return (lp);
1159 1.1 dyoung }
1160 1.1 dyoung
1161 1.1 dyoung static void
1162 1.1 dyoung makedisktab(FILE *f, struct disklabel *lp)
1163 1.1 dyoung {
1164 1.1 dyoung int i;
1165 1.1 dyoung const char *did;
1166 1.1 dyoung struct partition *pp;
1167 1.1 dyoung
1168 1.1 dyoung did = "\\\n\t:";
1169 1.1 dyoung (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=",
1170 1.1 dyoung (int) sizeof(lp->d_typename), lp->d_typename);
1171 1.1 dyoung if ((unsigned) lp->d_type < DKMAXTYPES)
1172 1.1 dyoung (void) fprintf(f, "%s:", dktypenames[lp->d_type]);
1173 1.1 dyoung else
1174 1.1 dyoung (void) fprintf(f, "unknown%d:", lp->d_type);
1175 1.1 dyoung
1176 1.1 dyoung (void) fprintf(f, "se#%d:", lp->d_secsize);
1177 1.1 dyoung (void) fprintf(f, "ns#%d:", lp->d_nsectors);
1178 1.1 dyoung (void) fprintf(f, "nt#%d:", lp->d_ntracks);
1179 1.1 dyoung (void) fprintf(f, "sc#%d:", lp->d_secpercyl);
1180 1.1 dyoung (void) fprintf(f, "nc#%d:", lp->d_ncylinders);
1181 1.1 dyoung
1182 1.1 dyoung if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) {
1183 1.1 dyoung (void) fprintf(f, "%ssu#%d:", did, lp->d_secperunit);
1184 1.1 dyoung did = "";
1185 1.1 dyoung }
1186 1.1 dyoung if (lp->d_rpm != 3600) {
1187 1.1 dyoung (void) fprintf(f, "%srm#%d:", did, lp->d_rpm);
1188 1.1 dyoung did = "";
1189 1.1 dyoung }
1190 1.1 dyoung if (lp->d_interleave != 1) {
1191 1.1 dyoung (void) fprintf(f, "%sil#%d:", did, lp->d_interleave);
1192 1.1 dyoung did = "";
1193 1.1 dyoung }
1194 1.1 dyoung if (lp->d_trackskew != 0) {
1195 1.1 dyoung (void) fprintf(f, "%ssk#%d:", did, lp->d_trackskew);
1196 1.1 dyoung did = "";
1197 1.1 dyoung }
1198 1.1 dyoung if (lp->d_cylskew != 0) {
1199 1.1 dyoung (void) fprintf(f, "%scs#%d:", did, lp->d_cylskew);
1200 1.1 dyoung did = "";
1201 1.1 dyoung }
1202 1.1 dyoung if (lp->d_headswitch != 0) {
1203 1.1 dyoung (void) fprintf(f, "%shs#%d:", did, lp->d_headswitch);
1204 1.1 dyoung did = "";
1205 1.1 dyoung }
1206 1.1 dyoung if (lp->d_trkseek != 0) {
1207 1.1 dyoung (void) fprintf(f, "%sts#%d:", did, lp->d_trkseek);
1208 1.1 dyoung did = "";
1209 1.1 dyoung }
1210 1.1 dyoung #ifdef notyet
1211 1.1 dyoung (void) fprintf(f, "drivedata: ");
1212 1.1 dyoung for (i = NDDATA - 1; i >= 0; i--)
1213 1.1 dyoung if (lp->d_drivedata[i])
1214 1.1 dyoung break;
1215 1.1 dyoung if (i < 0)
1216 1.1 dyoung i = 0;
1217 1.1 dyoung for (j = 0; j <= i; j++)
1218 1.1 dyoung (void) fprintf(f, "%d ", lp->d_drivedata[j]);
1219 1.1 dyoung #endif /* notyet */
1220 1.1 dyoung pp = lp->d_partitions;
1221 1.1 dyoung for (i = 0; i < lp->d_npartitions; i++, pp++) {
1222 1.1 dyoung if (pp->p_size) {
1223 1.1 dyoung char c = 'a' + i;
1224 1.1 dyoung (void) fprintf(f, "\\\n\t:");
1225 1.1 dyoung (void) fprintf(f, "p%c#%d:", c, pp->p_size);
1226 1.1 dyoung (void) fprintf(f, "o%c#%d:", c, pp->p_offset);
1227 1.1 dyoung if (pp->p_fstype != FS_UNUSED) {
1228 1.1 dyoung if ((unsigned) pp->p_fstype < FSMAXTYPES)
1229 1.1 dyoung (void) fprintf(f, "t%c=%s:", c,
1230 1.1 dyoung fstypenames[pp->p_fstype]);
1231 1.1 dyoung else
1232 1.1 dyoung (void) fprintf(f, "t%c=unknown%d:",
1233 1.1 dyoung c, pp->p_fstype);
1234 1.1 dyoung }
1235 1.1 dyoung switch (pp->p_fstype) {
1236 1.1 dyoung
1237 1.1 dyoung case FS_UNUSED:
1238 1.1 dyoung break;
1239 1.1 dyoung
1240 1.1 dyoung case FS_BSDFFS:
1241 1.1 dyoung case FS_BSDLFS:
1242 1.1 dyoung case FS_EX2FS:
1243 1.1 dyoung case FS_ADOS:
1244 1.1 dyoung case FS_APPLEUFS:
1245 1.1 dyoung (void) fprintf(f, "b%c#%d:", c,
1246 1.1 dyoung pp->p_fsize * pp->p_frag);
1247 1.1 dyoung (void) fprintf(f, "f%c#%d:", c, pp->p_fsize);
1248 1.1 dyoung break;
1249 1.1 dyoung default:
1250 1.1 dyoung break;
1251 1.1 dyoung }
1252 1.1 dyoung }
1253 1.1 dyoung }
1254 1.1 dyoung (void) fprintf(f, "\n");
1255 1.1 dyoung (void) fflush(f);
1256 1.1 dyoung }
1257 1.1 dyoung
1258 1.1 dyoung static int
1259 1.1 dyoung edit(struct disklabel *lp, int f)
1260 1.1 dyoung {
1261 1.1 dyoung struct disklabel label;
1262 1.1 dyoung const char *tmpdir;
1263 1.1 dyoung int first, ch, fd;
1264 1.1 dyoung FILE *fp;
1265 1.1 dyoung
1266 1.1 dyoung if ((tmpdir = getenv("TMPDIR")) == NULL)
1267 1.1 dyoung tmpdir = _PATH_TMP;
1268 1.1 dyoung (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE);
1269 1.1 dyoung if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) {
1270 1.1 dyoung warn("%s", tmpfil);
1271 1.1 dyoung return (1);
1272 1.1 dyoung }
1273 1.1 dyoung (void)fchmod(fd, 0600);
1274 1.1 dyoung showinfo(fp, lp, specname);
1275 1.1 dyoung showpartitions(fp, lp, Cflag);
1276 1.1 dyoung (void) fclose(fp);
1277 1.1 dyoung for (;;) {
1278 1.1 dyoung if (!editit())
1279 1.1 dyoung break;
1280 1.1 dyoung fp = fopen(tmpfil, "r");
1281 1.1 dyoung if (fp == NULL) {
1282 1.1 dyoung warn("%s", tmpfil);
1283 1.1 dyoung break;
1284 1.1 dyoung }
1285 1.1 dyoung (void) memset(&label, 0, sizeof(label));
1286 1.1 dyoung if (getasciilabel(fp, &label)) {
1287 1.1 dyoung *lp = label;
1288 1.1 dyoung if (writelabel(f, bootarea, lp) == 0) {
1289 1.1 dyoung (void) unlink(tmpfil);
1290 1.1 dyoung return (0);
1291 1.1 dyoung }
1292 1.1 dyoung }
1293 1.1 dyoung (void) printf("re-edit the label? [y]: ");
1294 1.1 dyoung (void) fflush(stdout);
1295 1.1 dyoung first = ch = getchar();
1296 1.1 dyoung while (ch != '\n' && ch != EOF)
1297 1.1 dyoung ch = getchar();
1298 1.1 dyoung if (first == 'n' || first == 'N')
1299 1.1 dyoung break;
1300 1.1 dyoung }
1301 1.1 dyoung (void)unlink(tmpfil);
1302 1.1 dyoung return (1);
1303 1.1 dyoung }
1304 1.1 dyoung
1305 1.1 dyoung static int
1306 1.1 dyoung editit(void)
1307 1.1 dyoung {
1308 1.1 dyoung int pid, xpid;
1309 1.1 dyoung int status;
1310 1.1 dyoung sigset_t nsigset, osigset;
1311 1.1 dyoung
1312 1.1 dyoung sigemptyset(&nsigset);
1313 1.1 dyoung sigaddset(&nsigset, SIGINT);
1314 1.1 dyoung sigaddset(&nsigset, SIGQUIT);
1315 1.1 dyoung sigaddset(&nsigset, SIGHUP);
1316 1.1 dyoung sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1317 1.1 dyoung while ((pid = fork()) < 0) {
1318 1.1 dyoung if (errno != EAGAIN) {
1319 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1320 1.1 dyoung warn("fork");
1321 1.1 dyoung return (0);
1322 1.1 dyoung }
1323 1.1 dyoung sleep(1);
1324 1.1 dyoung }
1325 1.1 dyoung if (pid == 0) {
1326 1.1 dyoung const char *ed;
1327 1.1 dyoung char *buf;
1328 1.1 dyoung int retval;
1329 1.1 dyoung
1330 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1331 1.1 dyoung setgid(getgid());
1332 1.1 dyoung setuid(getuid());
1333 1.1 dyoung if ((ed = getenv("EDITOR")) == (char *)0)
1334 1.1 dyoung ed = DEFEDITOR;
1335 1.1 dyoung /*
1336 1.1 dyoung * Jump through a few extra hoops in case someone's editor
1337 1.1 dyoung * is "editor arg1 arg2".
1338 1.1 dyoung */
1339 1.1 dyoung asprintf(&buf, "%s %s", ed, tmpfil);
1340 1.1 dyoung if (!buf)
1341 1.1 dyoung err(1, "malloc");
1342 1.1 dyoung retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL);
1343 1.1 dyoung if (retval == -1)
1344 1.1 dyoung perror(ed);
1345 1.1 dyoung exit(retval);
1346 1.1 dyoung }
1347 1.1 dyoung while ((xpid = wait(&status)) >= 0)
1348 1.1 dyoung if (xpid == pid)
1349 1.1 dyoung break;
1350 1.1 dyoung sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1351 1.1 dyoung return (!status);
1352 1.1 dyoung }
1353 1.1 dyoung
1354 1.1 dyoung static char *
1355 1.1 dyoung skip(char *cp)
1356 1.1 dyoung {
1357 1.1 dyoung
1358 1.1 dyoung cp += strspn(cp, " \t");
1359 1.1 dyoung if (*cp == '\0')
1360 1.1 dyoung return (NULL);
1361 1.1 dyoung return (cp);
1362 1.1 dyoung }
1363 1.1 dyoung
1364 1.1 dyoung static char *
1365 1.1 dyoung word(char *cp)
1366 1.1 dyoung {
1367 1.1 dyoung
1368 1.1 dyoung if (cp == NULL || *cp == '\0')
1369 1.1 dyoung return (NULL);
1370 1.1 dyoung
1371 1.1 dyoung cp += strcspn(cp, " \t");
1372 1.1 dyoung if (*cp == '\0')
1373 1.1 dyoung return (NULL);
1374 1.1 dyoung *cp++ = '\0';
1375 1.1 dyoung cp += strspn(cp, " \t");
1376 1.1 dyoung if (*cp == '\0')
1377 1.1 dyoung return (NULL);
1378 1.1 dyoung return (cp);
1379 1.1 dyoung }
1380 1.1 dyoung
1381 1.1 dyoung #define _CHECKLINE \
1382 1.1 dyoung if (tp == NULL || *tp == '\0') { \
1383 1.1 dyoung warnx("line %d: too few fields", lineno); \
1384 1.1 dyoung errors++; \
1385 1.1 dyoung break; \
1386 1.1 dyoung }
1387 1.1 dyoung
1388 1.1 dyoung #define __CHECKLINE \
1389 1.1 dyoung if (*tp == NULL || **tp == '\0') { \
1390 1.1 dyoung warnx("line %d: too few fields", lineno); \
1391 1.1 dyoung *tp = _error_; \
1392 1.1 dyoung return 0; \
1393 1.1 dyoung }
1394 1.1 dyoung
1395 1.1 dyoung static char _error_[] = "";
1396 1.1 dyoung #define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \
1397 1.1 dyoung ; else goto error
1398 1.1 dyoung #define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \
1399 1.1 dyoung ; else goto error
1400 1.1 dyoung
1401 1.1 dyoung static unsigned long
1402 1.1 dyoung nxtnum(char **tp, int lineno)
1403 1.1 dyoung {
1404 1.1 dyoung char *cp;
1405 1.1 dyoung unsigned long v;
1406 1.1 dyoung
1407 1.1 dyoung __CHECKLINE
1408 1.1 dyoung if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) {
1409 1.1 dyoung warnx("line %d: syntax error", lineno);
1410 1.1 dyoung *tp = _error_;
1411 1.1 dyoung return 0;
1412 1.1 dyoung }
1413 1.1 dyoung *tp = cp;
1414 1.1 dyoung return v;
1415 1.1 dyoung }
1416 1.1 dyoung
1417 1.1 dyoung static unsigned long
1418 1.1 dyoung nxtxnum(char **tp, struct disklabel *lp, int lineno)
1419 1.1 dyoung {
1420 1.1 dyoung char *cp, *ncp;
1421 1.1 dyoung unsigned long n, v;
1422 1.1 dyoung
1423 1.1 dyoung __CHECKLINE
1424 1.1 dyoung cp = *tp;
1425 1.1 dyoung if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0)
1426 1.1 dyoung goto bad;
1427 1.1 dyoung
1428 1.1 dyoung if (*ncp == '/') {
1429 1.1 dyoung n *= lp->d_secpercyl;
1430 1.1 dyoung cp = ncp + 1;
1431 1.1 dyoung if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0)
1432 1.1 dyoung goto bad;
1433 1.1 dyoung n += v * lp->d_nsectors;
1434 1.1 dyoung cp = ncp + 1;
1435 1.1 dyoung if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0)
1436 1.1 dyoung goto bad;
1437 1.1 dyoung n += v;
1438 1.1 dyoung }
1439 1.1 dyoung *tp = ncp;
1440 1.1 dyoung return n;
1441 1.1 dyoung bad:
1442 1.1 dyoung warnx("line %d: invalid format", lineno);
1443 1.1 dyoung *tp = _error_;
1444 1.1 dyoung return 0;
1445 1.1 dyoung }
1446 1.1 dyoung
1447 1.1 dyoung /*
1448 1.1 dyoung * Read an ascii label in from fd f,
1449 1.1 dyoung * in the same format as that put out by showinfo() and showpartitions(),
1450 1.1 dyoung * and fill in lp.
1451 1.1 dyoung */
1452 1.1 dyoung static int
1453 1.1 dyoung getasciilabel(FILE *f, struct disklabel *lp)
1454 1.1 dyoung {
1455 1.1 dyoung const char *const *cpp, *s;
1456 1.1 dyoung struct partition *pp;
1457 1.1 dyoung char *cp, *tp, line[BUFSIZ], tbuf[15];
1458 1.1 dyoung int lineno, errors;
1459 1.1 dyoung unsigned long v;
1460 1.1 dyoung unsigned int part;
1461 1.1 dyoung
1462 1.1 dyoung lineno = 0;
1463 1.1 dyoung errors = 0;
1464 1.1 dyoung lp->d_bbsize = BBSIZE; /* XXX */
1465 1.1 dyoung lp->d_sbsize = SBLOCKSIZE; /* XXX */
1466 1.1 dyoung while (fgets(line, sizeof(line) - 1, f)) {
1467 1.1 dyoung lineno++;
1468 1.1 dyoung if ((cp = strpbrk(line, "#\r\n")) != NULL)
1469 1.1 dyoung *cp = '\0';
1470 1.1 dyoung cp = skip(line);
1471 1.1 dyoung if (cp == NULL) /* blank line or comment line */
1472 1.1 dyoung continue;
1473 1.1 dyoung tp = strchr(cp, ':'); /* everything has a colon in it */
1474 1.1 dyoung if (tp == NULL) {
1475 1.1 dyoung warnx("line %d: syntax error", lineno);
1476 1.1 dyoung errors++;
1477 1.1 dyoung continue;
1478 1.1 dyoung }
1479 1.1 dyoung *tp++ = '\0', tp = skip(tp);
1480 1.1 dyoung if (!strcmp(cp, "type")) {
1481 1.1 dyoung if (tp == NULL) {
1482 1.1 dyoung strlcpy(tbuf, "unknown", sizeof(tbuf));
1483 1.1 dyoung tp = tbuf;
1484 1.1 dyoung }
1485 1.1 dyoung cpp = dktypenames;
1486 1.1 dyoung for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
1487 1.1 dyoung if ((s = *cpp) && !strcasecmp(s, tp)) {
1488 1.1 dyoung lp->d_type = cpp - dktypenames;
1489 1.1 dyoung goto next;
1490 1.1 dyoung }
1491 1.1 dyoung if (GETNUM16(tp, &v) != 0) {
1492 1.1 dyoung warnx("line %d: syntax error", lineno);
1493 1.1 dyoung errors++;
1494 1.1 dyoung continue;
1495 1.1 dyoung }
1496 1.1 dyoung if (v >= DKMAXTYPES)
1497 1.1 dyoung warnx("line %d: warning, unknown disk type: %s",
1498 1.1 dyoung lineno, tp);
1499 1.1 dyoung lp->d_type = v;
1500 1.1 dyoung continue;
1501 1.1 dyoung }
1502 1.1 dyoung if (!strcmp(cp, "flags")) {
1503 1.1 dyoung for (v = 0; (cp = tp) && *cp != '\0';) {
1504 1.1 dyoung tp = word(cp);
1505 1.1 dyoung if (!strcasecmp(cp, "removable"))
1506 1.1 dyoung v |= D_REMOVABLE;
1507 1.1 dyoung else if (!strcasecmp(cp, "ecc"))
1508 1.1 dyoung v |= D_ECC;
1509 1.1 dyoung else if (!strcasecmp(cp, "badsect"))
1510 1.1 dyoung v |= D_BADSECT;
1511 1.1 dyoung else {
1512 1.1 dyoung warnx("line %d: bad flag: %s",
1513 1.1 dyoung lineno, cp);
1514 1.1 dyoung errors++;
1515 1.1 dyoung }
1516 1.1 dyoung }
1517 1.1 dyoung lp->d_flags = v;
1518 1.1 dyoung continue;
1519 1.1 dyoung }
1520 1.1 dyoung if (!strcmp(cp, "drivedata")) {
1521 1.1 dyoung int i;
1522 1.1 dyoung
1523 1.1 dyoung for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1524 1.1 dyoung if (GETNUM32(cp, &v) != 0) {
1525 1.1 dyoung warnx("line %d: bad drive data",
1526 1.1 dyoung lineno);
1527 1.1 dyoung errors++;
1528 1.1 dyoung } else
1529 1.1 dyoung lp->d_drivedata[i] = v;
1530 1.1 dyoung i++;
1531 1.1 dyoung tp = word(cp);
1532 1.1 dyoung }
1533 1.1 dyoung continue;
1534 1.1 dyoung }
1535 1.1 dyoung if (sscanf(cp, "%lu partitions", &v) == 1) {
1536 1.1 dyoung if (v == 0 || v > MAXPARTITIONS) {
1537 1.1 dyoung warnx("line %d: bad # of partitions", lineno);
1538 1.1 dyoung lp->d_npartitions = MAXPARTITIONS;
1539 1.1 dyoung errors++;
1540 1.1 dyoung } else
1541 1.1 dyoung lp->d_npartitions = v;
1542 1.1 dyoung continue;
1543 1.1 dyoung }
1544 1.1 dyoung if (tp == NULL) {
1545 1.1 dyoung tbuf[0] = '\0';
1546 1.1 dyoung tp = tbuf;
1547 1.1 dyoung }
1548 1.1 dyoung if (!strcmp(cp, "disk")) {
1549 1.1 dyoung strncpy(lp->d_typename, tp, sizeof(lp->d_typename));
1550 1.1 dyoung continue;
1551 1.1 dyoung }
1552 1.1 dyoung if (!strcmp(cp, "label")) {
1553 1.1 dyoung strncpy(lp->d_packname, tp, sizeof(lp->d_packname));
1554 1.1 dyoung continue;
1555 1.1 dyoung }
1556 1.1 dyoung if (!strcmp(cp, "bytes/sector")) {
1557 1.1 dyoung if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) {
1558 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1559 1.1 dyoung errors++;
1560 1.1 dyoung } else
1561 1.1 dyoung lp->d_secsize = v;
1562 1.1 dyoung continue;
1563 1.1 dyoung }
1564 1.1 dyoung if (!strcmp(cp, "sectors/track")) {
1565 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1566 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1567 1.1 dyoung errors++;
1568 1.1 dyoung } else
1569 1.1 dyoung lp->d_nsectors = v;
1570 1.1 dyoung continue;
1571 1.1 dyoung }
1572 1.1 dyoung if (!strcmp(cp, "sectors/cylinder")) {
1573 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1574 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1575 1.1 dyoung errors++;
1576 1.1 dyoung } else
1577 1.1 dyoung lp->d_secpercyl = v;
1578 1.1 dyoung continue;
1579 1.1 dyoung }
1580 1.1 dyoung if (!strcmp(cp, "tracks/cylinder")) {
1581 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1582 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1583 1.1 dyoung errors++;
1584 1.1 dyoung } else
1585 1.1 dyoung lp->d_ntracks = v;
1586 1.1 dyoung continue;
1587 1.1 dyoung }
1588 1.1 dyoung if (!strcmp(cp, "cylinders")) {
1589 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1590 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1591 1.1 dyoung errors++;
1592 1.1 dyoung } else
1593 1.1 dyoung lp->d_ncylinders = v;
1594 1.1 dyoung continue;
1595 1.1 dyoung }
1596 1.1 dyoung if (!strcmp(cp, "total sectors")) {
1597 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1598 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1599 1.1 dyoung errors++;
1600 1.1 dyoung } else
1601 1.1 dyoung lp->d_secperunit = v;
1602 1.1 dyoung continue;
1603 1.1 dyoung }
1604 1.1 dyoung if (!strcmp(cp, "rpm")) {
1605 1.1 dyoung if (GETNUM16(tp, &v) != 0) {
1606 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1607 1.1 dyoung errors++;
1608 1.1 dyoung } else
1609 1.1 dyoung lp->d_rpm = v;
1610 1.1 dyoung continue;
1611 1.1 dyoung }
1612 1.1 dyoung if (!strcmp(cp, "interleave")) {
1613 1.1 dyoung if (GETNUM16(tp, &v) != 0) {
1614 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1615 1.1 dyoung errors++;
1616 1.1 dyoung } else
1617 1.1 dyoung lp->d_interleave = v;
1618 1.1 dyoung continue;
1619 1.1 dyoung }
1620 1.1 dyoung if (!strcmp(cp, "trackskew")) {
1621 1.1 dyoung if (GETNUM16(tp, &v) != 0) {
1622 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1623 1.1 dyoung errors++;
1624 1.1 dyoung } else
1625 1.1 dyoung lp->d_trackskew = v;
1626 1.1 dyoung continue;
1627 1.1 dyoung }
1628 1.1 dyoung if (!strcmp(cp, "cylinderskew")) {
1629 1.1 dyoung if (GETNUM16(tp, &v) != 0) {
1630 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1631 1.1 dyoung errors++;
1632 1.1 dyoung } else
1633 1.1 dyoung lp->d_cylskew = v;
1634 1.1 dyoung continue;
1635 1.1 dyoung }
1636 1.1 dyoung if (!strcmp(cp, "headswitch")) {
1637 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1638 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1639 1.1 dyoung errors++;
1640 1.1 dyoung } else
1641 1.1 dyoung lp->d_headswitch = v;
1642 1.1 dyoung continue;
1643 1.1 dyoung }
1644 1.1 dyoung if (!strcmp(cp, "track-to-track seek")) {
1645 1.1 dyoung if (GETNUM32(tp, &v) != 0) {
1646 1.1 dyoung warnx("line %d: bad %s: %s", lineno, cp, tp);
1647 1.1 dyoung errors++;
1648 1.1 dyoung } else
1649 1.1 dyoung lp->d_trkseek = v;
1650 1.1 dyoung continue;
1651 1.1 dyoung }
1652 1.1 dyoung if ('a' > *cp || *cp > 'z' || cp[1] != '\0') {
1653 1.1 dyoung warnx("line %d: unknown field: %s", lineno, cp);
1654 1.1 dyoung errors++;
1655 1.1 dyoung continue;
1656 1.1 dyoung }
1657 1.1 dyoung
1658 1.1 dyoung /* We have a partition entry */
1659 1.1 dyoung part = *cp - 'a';
1660 1.1 dyoung
1661 1.1 dyoung if (part >= MAXPARTITIONS) {
1662 1.1 dyoung warnx("line %d: bad partition name: %s", lineno, cp);
1663 1.1 dyoung errors++;
1664 1.1 dyoung continue;
1665 1.1 dyoung }
1666 1.1 dyoung pp = &lp->d_partitions[part];
1667 1.1 dyoung
1668 1.1 dyoung NXTXNUM(pp->p_size);
1669 1.1 dyoung NXTXNUM(pp->p_offset);
1670 1.1 dyoung /* can't use word() here because of blanks in fstypenames[] */
1671 1.1 dyoung tp += strspn(tp, " \t");
1672 1.1 dyoung _CHECKLINE
1673 1.1 dyoung cp = tp;
1674 1.1 dyoung cpp = fstypenames;
1675 1.1 dyoung for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
1676 1.1 dyoung s = *cpp;
1677 1.1 dyoung if (s == NULL ||
1678 1.1 dyoung (cp[strlen(s)] != ' ' &&
1679 1.1 dyoung cp[strlen(s)] != '\t' &&
1680 1.1 dyoung cp[strlen(s)] != '\0'))
1681 1.1 dyoung continue;
1682 1.1 dyoung if (!memcmp(s, cp, strlen(s))) {
1683 1.1 dyoung pp->p_fstype = cpp - fstypenames;
1684 1.1 dyoung tp += strlen(s);
1685 1.1 dyoung if (*tp == '\0')
1686 1.1 dyoung tp = NULL;
1687 1.1 dyoung else {
1688 1.1 dyoung tp += strspn(tp, " \t");
1689 1.1 dyoung if (*tp == '\0')
1690 1.1 dyoung tp = NULL;
1691 1.1 dyoung }
1692 1.1 dyoung goto gottype;
1693 1.1 dyoung }
1694 1.1 dyoung }
1695 1.1 dyoung tp = word(cp);
1696 1.1 dyoung if (isdigit(*cp & 0xff)) {
1697 1.1 dyoung if (GETNUM8(cp, &v) != 0) {
1698 1.1 dyoung warnx("line %d: syntax error", lineno);
1699 1.1 dyoung errors++;
1700 1.1 dyoung }
1701 1.1 dyoung } else
1702 1.1 dyoung v = FSMAXTYPES;
1703 1.1 dyoung if ((unsigned)v >= FSMAXTYPES) {
1704 1.1 dyoung warnx("line %d: warning, unknown filesystem type: %s",
1705 1.1 dyoung lineno, cp);
1706 1.1 dyoung v = FS_UNUSED;
1707 1.1 dyoung }
1708 1.1 dyoung pp->p_fstype = v;
1709 1.1 dyoung gottype:
1710 1.1 dyoung switch (pp->p_fstype) {
1711 1.1 dyoung
1712 1.1 dyoung case FS_UNUSED: /* XXX */
1713 1.1 dyoung NXTNUM(pp->p_fsize);
1714 1.1 dyoung if (pp->p_fsize == 0)
1715 1.1 dyoung break;
1716 1.1 dyoung NXTNUM(v);
1717 1.1 dyoung pp->p_frag = v / pp->p_fsize;
1718 1.1 dyoung break;
1719 1.1 dyoung
1720 1.1 dyoung case FS_BSDFFS:
1721 1.1 dyoung case FS_ADOS:
1722 1.1 dyoung case FS_APPLEUFS:
1723 1.1 dyoung NXTNUM(pp->p_fsize);
1724 1.1 dyoung if (pp->p_fsize == 0)
1725 1.1 dyoung break;
1726 1.1 dyoung NXTNUM(v);
1727 1.1 dyoung pp->p_frag = v / pp->p_fsize;
1728 1.1 dyoung NXTNUM(pp->p_cpg);
1729 1.1 dyoung break;
1730 1.1 dyoung case FS_BSDLFS:
1731 1.1 dyoung NXTNUM(pp->p_fsize);
1732 1.1 dyoung if (pp->p_fsize == 0)
1733 1.1 dyoung break;
1734 1.1 dyoung NXTNUM(v);
1735 1.1 dyoung pp->p_frag = v / pp->p_fsize;
1736 1.1 dyoung NXTNUM(pp->p_sgs);
1737 1.1 dyoung break;
1738 1.1 dyoung case FS_EX2FS:
1739 1.1 dyoung NXTNUM(pp->p_fsize);
1740 1.1 dyoung if (pp->p_fsize == 0)
1741 1.1 dyoung break;
1742 1.1 dyoung NXTNUM(v);
1743 1.1 dyoung pp->p_frag = v / pp->p_fsize;
1744 1.1 dyoung break;
1745 1.1 dyoung case FS_ISO9660:
1746 1.1 dyoung NXTNUM(pp->p_cdsession);
1747 1.1 dyoung break;
1748 1.1 dyoung default:
1749 1.1 dyoung break;
1750 1.1 dyoung }
1751 1.1 dyoung continue;
1752 1.1 dyoung error:
1753 1.1 dyoung errors++;
1754 1.1 dyoung next:
1755 1.1 dyoung ;
1756 1.1 dyoung }
1757 1.1 dyoung errors += checklabel(lp);
1758 1.1 dyoung return (errors == 0);
1759 1.1 dyoung }
1760 1.1 dyoung
1761 1.1 dyoung /*
1762 1.1 dyoung * Check disklabel for errors and fill in
1763 1.1 dyoung * derived fields according to supplied values.
1764 1.1 dyoung */
1765 1.1 dyoung int
1766 1.1 dyoung checklabel(struct disklabel *lp)
1767 1.1 dyoung {
1768 1.1 dyoung struct partition *pp, *qp;
1769 1.1 dyoung int i, j, errors;
1770 1.1 dyoung char part;
1771 1.1 dyoung
1772 1.1 dyoung errors = 0;
1773 1.1 dyoung if (lp->d_secsize == 0) {
1774 1.1 dyoung warnx("sector size %d", lp->d_secsize);
1775 1.1 dyoung return (1);
1776 1.1 dyoung }
1777 1.1 dyoung if (lp->d_nsectors == 0) {
1778 1.1 dyoung warnx("sectors/track %d", lp->d_nsectors);
1779 1.1 dyoung return (1);
1780 1.1 dyoung }
1781 1.1 dyoung if (lp->d_ntracks == 0) {
1782 1.1 dyoung warnx("tracks/cylinder %d", lp->d_ntracks);
1783 1.1 dyoung return (1);
1784 1.1 dyoung }
1785 1.1 dyoung if (lp->d_ncylinders == 0) {
1786 1.1 dyoung warnx("cylinders/unit %d", lp->d_ncylinders);
1787 1.1 dyoung errors++;
1788 1.1 dyoung }
1789 1.1 dyoung if (lp->d_rpm == 0)
1790 1.1 dyoung warnx("warning, revolutions/minute %d", lp->d_rpm);
1791 1.1 dyoung if (lp->d_secpercyl == 0)
1792 1.1 dyoung lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1793 1.1 dyoung if (lp->d_secperunit == 0)
1794 1.1 dyoung lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1795 1.1 dyoung #ifdef __i386__notyet__
1796 1.1 dyoung if (dosdp && lp->d_secperunit > dosdp->mbrp_start + dosdp->mbrp_size) {
1797 1.1 dyoung warnx("exceeds DOS partition size");
1798 1.1 dyoung errors++;
1799 1.1 dyoung lp->d_secperunit = dosdp->mbrp_start + dosdp->mbrp_size;
1800 1.1 dyoung }
1801 1.1 dyoung /* XXX should also check geometry against BIOS's idea */
1802 1.1 dyoung #endif /* __i386__notyet__ */
1803 1.1 dyoung #ifdef __arm32__notyet__
1804 1.1 dyoung /* XXX similar code as for i386 */
1805 1.1 dyoung #endif /* __arm32__notyet__ */
1806 1.1 dyoung if (lp->d_bbsize == 0) {
1807 1.1 dyoung warnx("boot block size %d", lp->d_bbsize);
1808 1.1 dyoung errors++;
1809 1.1 dyoung } else if (lp->d_bbsize % lp->d_secsize)
1810 1.1 dyoung warnx("warning, boot block size %% sector-size != 0");
1811 1.1 dyoung if (lp->d_sbsize == 0) {
1812 1.1 dyoung warnx("super block size %d", lp->d_sbsize);
1813 1.1 dyoung errors++;
1814 1.1 dyoung } else if (lp->d_sbsize % lp->d_secsize)
1815 1.1 dyoung warnx("warning, super block size %% sector-size != 0");
1816 1.1 dyoung if (lp->d_npartitions > MAXPARTITIONS)
1817 1.1 dyoung warnx("warning, number of partitions (%d) > MAXPARTITIONS (%d)",
1818 1.1 dyoung lp->d_npartitions, MAXPARTITIONS);
1819 1.1 dyoung else
1820 1.1 dyoung for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) {
1821 1.1 dyoung part = 'a' + i;
1822 1.1 dyoung pp = &lp->d_partitions[i];
1823 1.1 dyoung if (pp->p_size || pp->p_offset) {
1824 1.1 dyoung warnx("warning, partition %c increased "
1825 1.1 dyoung "number of partitions from %d to %d",
1826 1.1 dyoung part, lp->d_npartitions, i + 1);
1827 1.1 dyoung lp->d_npartitions = i + 1;
1828 1.1 dyoung break;
1829 1.1 dyoung }
1830 1.1 dyoung }
1831 1.1 dyoung for (i = 0; i < lp->d_npartitions; i++) {
1832 1.1 dyoung part = 'a' + i;
1833 1.1 dyoung pp = &lp->d_partitions[i];
1834 1.1 dyoung if (pp->p_size == 0 && pp->p_offset != 0)
1835 1.1 dyoung warnx("warning, partition %c: size 0, but offset %d",
1836 1.1 dyoung part, pp->p_offset);
1837 1.1 dyoung #ifdef STRICT_CYLINDER_ALIGNMENT
1838 1.1 dyoung if (pp->p_offset % lp->d_secpercyl) {
1839 1.1 dyoung warnx("warning, partition %c:"
1840 1.1 dyoung " not starting on cylinder boundary",
1841 1.1 dyoung part);
1842 1.1 dyoung errors++;
1843 1.1 dyoung }
1844 1.1 dyoung #endif /* STRICT_CYLINDER_ALIGNMENT */
1845 1.1 dyoung if (pp->p_offset > lp->d_secperunit) {
1846 1.1 dyoung warnx("partition %c: offset past end of unit", part);
1847 1.1 dyoung errors++;
1848 1.1 dyoung }
1849 1.1 dyoung if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1850 1.1 dyoung warnx("partition %c: partition extends"
1851 1.1 dyoung " past end of unit",
1852 1.1 dyoung part);
1853 1.1 dyoung errors++;
1854 1.1 dyoung }
1855 1.1 dyoung if (pp->p_fstype != FS_UNUSED)
1856 1.1 dyoung for (j = i + 1; j < lp->d_npartitions; j++) {
1857 1.1 dyoung qp = &lp->d_partitions[j];
1858 1.1 dyoung if (qp->p_fstype == FS_UNUSED)
1859 1.1 dyoung continue;
1860 1.1 dyoung if (pp->p_offset < qp->p_offset + qp->p_size &&
1861 1.1 dyoung qp->p_offset < pp->p_offset + pp->p_size)
1862 1.1 dyoung warnx("partitions %c and %c overlap",
1863 1.1 dyoung part, 'a' + j);
1864 1.1 dyoung }
1865 1.1 dyoung }
1866 1.1 dyoung return (errors);
1867 1.1 dyoung }
1868 1.1 dyoung
1869 1.1 dyoung #if NUMBOOT > 0
1870 1.1 dyoung /*
1871 1.1 dyoung * If we are installing a boot program that doesn't fit in d_bbsize
1872 1.1 dyoung * we need to mark those partitions that the boot overflows into.
1873 1.1 dyoung * This allows newfs to prevent creation of a filesystem where it might
1874 1.1 dyoung * clobber bootstrap code.
1875 1.1 dyoung */
1876 1.1 dyoung static void
1877 1.1 dyoung setbootflag(struct disklabel *lp)
1878 1.1 dyoung {
1879 1.1 dyoung struct partition *pp;
1880 1.1 dyoung int i, errors;
1881 1.1 dyoung char part;
1882 1.1 dyoung u_long boffset;
1883 1.1 dyoung
1884 1.1 dyoung errors = 0;
1885 1.1 dyoung if (bootbuf == 0)
1886 1.1 dyoung return;
1887 1.1 dyoung boffset = bootsize / lp->d_secsize;
1888 1.1 dyoung for (i = 0; i < lp->d_npartitions; i++) {
1889 1.1 dyoung part = 'a' + i;
1890 1.1 dyoung pp = &lp->d_partitions[i];
1891 1.1 dyoung if (pp->p_size == 0)
1892 1.1 dyoung continue;
1893 1.1 dyoung if (boffset <= pp->p_offset) {
1894 1.1 dyoung if (pp->p_fstype == FS_BOOT)
1895 1.1 dyoung pp->p_fstype = FS_UNUSED;
1896 1.1 dyoung } else if (pp->p_fstype != FS_BOOT) {
1897 1.1 dyoung if (pp->p_fstype != FS_UNUSED) {
1898 1.1 dyoung warnx("boot overlaps used partition %c",
1899 1.1 dyoung part);
1900 1.1 dyoung errors++;
1901 1.1 dyoung } else {
1902 1.1 dyoung pp->p_fstype = FS_BOOT;
1903 1.1 dyoung warnx("warning, boot overlaps partition %c, %s",
1904 1.1 dyoung part, "marked as FS_BOOT");
1905 1.1 dyoung }
1906 1.1 dyoung }
1907 1.1 dyoung }
1908 1.1 dyoung if (errors)
1909 1.1 dyoung errx(4, "cannot install boot program");
1910 1.1 dyoung }
1911 1.1 dyoung #endif /* NUMBOOT > 0 */
1912 1.1 dyoung
1913 1.1 dyoung static void
1914 1.1 dyoung usage(void)
1915 1.1 dyoung {
1916 1.1 dyoung static const struct {
1917 1.1 dyoung const char *name;
1918 1.1 dyoung const char *expn;
1919 1.1 dyoung } usages[] = {
1920 1.1 dyoung { "[-rt] [-C] [-F] disk",
1921 1.1 dyoung "(to read label)" },
1922 1.1 dyoung { "-w [-r] [-F] [-f disktab] disk type [ packid ]",
1923 1.1 dyoung #if NUMBOOT > 0
1924 1.1 dyoung "(to write label with existing boot program)"
1925 1.1 dyoung #else
1926 1.1 dyoung "(to write label)"
1927 1.1 dyoung #endif
1928 1.1 dyoung },
1929 1.1 dyoung { "-e [-r] [-I] [-C] [-F] disk",
1930 1.1 dyoung "(to edit label)" },
1931 1.1 dyoung { "-i [-I] [-r] [-F] disk",
1932 1.1 dyoung "(to create a label interactively)" },
1933 1.1 dyoung { "-R [-r] [-F] disk protofile",
1934 1.1 dyoung #if NUMBOOT > 0
1935 1.1 dyoung "(to restore label with existing boot program)"
1936 1.1 dyoung #else
1937 1.1 dyoung "(to restore label)"
1938 1.1 dyoung #endif
1939 1.1 dyoung },
1940 1.1 dyoung #if NUMBOOT > 0
1941 1.1 dyoung # if NUMBOOT > 1
1942 1.1 dyoung { "-B [-f disktab] [ -b xxboot [ -s bootxx ] ] disk [ type ]",
1943 1.1 dyoung "(to install boot program with existing label)" },
1944 1.1 dyoung { "-w -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk type [ packid ]",
1945 1.1 dyoung "(to write label and boot program)" },
1946 1.1 dyoung { "-R -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk protofile [ type ]",
1947 1.1 dyoung "(to restore label and boot program)" },
1948 1.1 dyoung # else
1949 1.1 dyoung { "-B [-F] [-f disktab] [ -b bootprog ] disk [ type ]",
1950 1.1 dyoung "(to install boot program with existing on-disk label)" },
1951 1.1 dyoung { "-w -B [-F] [-f disktab] [ -b bootprog ] disk type [ packid ]",
1952 1.1 dyoung "(to write label and install boot program)" },
1953 1.1 dyoung { "-R -B [-F] [-f disktab] [ -b bootprog ] disk protofile [ type ]",
1954 1.1 dyoung "(to restore label and install boot program)" },
1955 1.1 dyoung # endif
1956 1.1 dyoung #endif
1957 1.1 dyoung { "[-NW] disk",
1958 1.1 dyoung "(to write disable/enable label)" },
1959 1.1 dyoung { NULL,
1960 1.1 dyoung NULL }
1961 1.1 dyoung };
1962 1.1 dyoung int i;
1963 1.1 dyoung
1964 1.1 dyoung for (i = 0; usages[i].name; i++) {
1965 1.1 dyoung (void) fputs(i ? "or " : "usage: ", stderr);
1966 1.1 dyoung (void) fprintf(stderr, "%s %s", getprogname(), usages[i].name);
1967 1.1 dyoung (void) fputs("\n\t", stderr);
1968 1.1 dyoung (void) fprintf(stderr, "%s %s", getprogname(), usages[i].expn);
1969 1.1 dyoung (void) fputs("\n", stderr);
1970 1.1 dyoung }
1971 1.1 dyoung exit(1);
1972 1.1 dyoung }
1973 1.1 dyoung
1974 1.1 dyoung static int
1975 1.1 dyoung getulong(const char *str, char sep, char **epp, unsigned long *ul,
1976 1.1 dyoung unsigned long max)
1977 1.1 dyoung {
1978 1.1 dyoung char *ep;
1979 1.1 dyoung
1980 1.1 dyoung if (epp == NULL)
1981 1.1 dyoung epp = &ep;
1982 1.1 dyoung
1983 1.1 dyoung *ul = strtoul(str, epp, 10);
1984 1.1 dyoung
1985 1.1 dyoung if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max)
1986 1.1 dyoung return ERANGE;
1987 1.1 dyoung
1988 1.1 dyoung if (*str == '\0' || (**epp != '\0' && **epp != sep &&
1989 1.1 dyoung !isspace((unsigned char)**epp)))
1990 1.1 dyoung return EFTYPE;
1991 1.1 dyoung
1992 1.1 dyoung return 0;
1993 1.1 dyoung }
1994