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