xref: /freebsd/sbin/fdisk/fdisk.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 /*
2  * Mach Operating System
3  * Copyright (c) 1992 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  */
26 
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD$";
30 #endif /* not lint */
31 
32 #include <sys/disklabel.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/mount.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <paths.h>
41 #include <regex.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 int iotest;
48 
49 #define LBUF 100
50 static char lbuf[LBUF];
51 
52 #define MBRSIGOFF	510
53 
54 /*
55  *
56  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
57  *
58  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
59  *	Copyright (c) 1989	Robert. V. Baron
60  *	Created.
61  */
62 
63 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
64 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
65 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
66 
67 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
68 
69 #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
70 #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
71 int secsize = 0;		/* the sensed sector size */
72 
73 char *disk;
74 
75 struct disklabel disklabel;		/* disk parameters */
76 
77 int cyls, sectors, heads, cylsecs, disksecs;
78 
79 struct mboot
80 {
81 	unsigned char padding[2]; /* force the longs to be long aligned */
82   	unsigned char *bootinst;  /* boot code */
83   	off_t bootinst_size;
84 	struct	dos_partition parts[4];
85 };
86 struct mboot mboot = {{0}, NULL, 0};
87 
88 #define ACTIVE 0x80
89 #define BOOT_MAGIC 0xAA55
90 
91 int dos_cyls;
92 int dos_heads;
93 int dos_sectors;
94 int dos_cylsecs;
95 
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c)	(c & 0xff)
98 static int partition = -1;
99 
100 
101 #define MAX_ARGS	10
102 
103 static int	current_line_number;
104 
105 static int	geom_processed = 0;
106 static int	part_processed = 0;
107 static int	active_processed = 0;
108 
109 
110 typedef struct cmd {
111     char		cmd;
112     int			n_args;
113     struct arg {
114 	char	argtype;
115 	int	arg_val;
116     }			args[MAX_ARGS];
117 } CMD;
118 
119 
120 static int B_flag  = 0;		/* replace boot code */
121 static int I_flag  = 0;		/* use entire disk for FreeBSD */
122 static int a_flag  = 0;		/* set active partition */
123 static char *b_flag = NULL;	/* path to boot code */
124 static int i_flag  = 0;		/* replace partition data */
125 static int u_flag  = 0;		/* update partition data */
126 static int s_flag  = 0;		/* Print a summary and exit */
127 static int t_flag  = 0;		/* test only, if f_flag is given */
128 static char *f_flag = NULL;	/* Read config info from file */
129 static int v_flag  = 0;		/* Be verbose */
130 
131 struct part_type
132 {
133  unsigned char type;
134  char *name;
135 }part_types[] =
136 {
137 	 {0x00, "unused"}
138 	,{0x01, "Primary DOS with 12 bit FAT"}
139 	,{0x02, "XENIX / filesystem"}
140 	,{0x03, "XENIX /usr filesystem"}
141 	,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
142 	,{0x05, "Extended DOS"}
143 	,{0x06, "Primary 'big' DOS (> 32MB)"}
144 	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
145 	,{0x08, "AIX filesystem"}
146 	,{0x09, "AIX boot partition or Coherent"}
147 	,{0x0A, "OS/2 Boot Manager or OPUS"}
148 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
149 	,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
150 	,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
151 	,{0x0F, "Extended DOS, LBA"}
152 	,{0x10, "OPUS"}
153 	,{0x39, "plan9"}
154 	,{0x40, "VENIX 286"}
155 	,{0x4D, "QNX 4.2 Primary"}
156 	,{0x4E, "QNX 4.2 Secondary"}
157 	,{0x4F, "QNX 4.2 Tertiary"}
158 	,{0x50, "DM"}
159 	,{0x51, "DM"}
160 	,{0x52, "CP/M or Microport SysV/AT"}
161 	,{0x56, "GB"}
162 	,{0x61, "Speed"}
163 	,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
164 	,{0x64, "Novell Netware 2.xx"}
165 	,{0x65, "Novell Netware 3.xx"}
166 	,{0x75, "PCIX"}
167 	,{0x80, "Minix 1.1 ... 1.4a"}
168 	,{0x81, "Minix 1.4b ... 1.5.10"}
169 	,{0x82, "Linux swap or Solaris x86"}
170 	,{0x83, "Linux filesystem"}
171 	,{0x93, "Amoeba filesystem"}
172 	,{0x94, "Amoeba bad block table"}
173 	,{0x9F, "BSD/OS"}
174 	,{0xA0, "Suspend to Disk"}
175 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
176 	,{0xA6, "OpenBSD"}
177 	,{0xA7, "NEXTSTEP"}
178 	,{0xA9, "NetBSD"}
179 	,{0xB7, "BSDI BSD/386 filesystem"}
180 	,{0xB8, "BSDI BSD/386 swap"}
181 	,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
182 	,{0xE1, "Speed"}
183 	,{0xE3, "Speed"}
184 	,{0xE4, "Speed"}
185 	,{0xF1, "Speed"}
186 	,{0xF2, "DOS 3.3+ Secondary"}
187 	,{0xF4, "Speed"}
188 	,{0xFF, "BBT (Bad Blocks Table)"}
189 };
190 
191 static void print_s0(int which);
192 static void print_part(int i);
193 static void init_sector0(unsigned long start);
194 static void init_boot(void);
195 static void change_part(int i);
196 static void print_params();
197 static void change_active(int which);
198 static void change_code();
199 static void get_params_to_use();
200 static char *get_rootdisk(void);
201 static void dos(struct dos_partition *partp);
202 static int open_disk(int u_flag);
203 static ssize_t read_disk(off_t sector, void *buf);
204 static ssize_t write_disk(off_t sector, void *buf);
205 static int get_params();
206 static int read_s0();
207 static int write_s0();
208 static int ok(char *str);
209 static int decimal(char *str, int *num, int deflt);
210 static char *get_type(int type);
211 static int read_config(char *config_file);
212 static void reset_boot(void);
213 static int sanitize_partition(struct dos_partition *);
214 static void usage(void);
215 #if 0
216 static int hex(char *str, int *num, int deflt);
217 static int string(char *str, char **ans);
218 #endif
219 
220 
221 int
222 main(int argc, char *argv[])
223 {
224 	struct	stat sb;
225 	int	c, i;
226 
227 	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
228 		switch (c) {
229 		case 'B':
230 			B_flag = 1;
231 			break;
232 		case 'I':
233 			I_flag = 1;
234 			break;
235 		case 'a':
236 			a_flag = 1;
237 			break;
238 		case 'b':
239 			b_flag = optarg;
240 			break;
241 		case 'f':
242 			f_flag = optarg;
243 			break;
244 		case 'i':
245 			i_flag = 1;
246 			break;
247 		case 's':
248 			s_flag = 1;
249 			break;
250 		case 't':
251 			t_flag = 1;
252 			break;
253 		case 'u':
254 			u_flag = 1;
255 			break;
256 		case 'v':
257 			v_flag = 1;
258 			break;
259 		case '1':
260 		case '2':
261 		case '3':
262 		case '4':
263 			partition = c - '0';
264 			break;
265 		default:
266 			usage();
267 		}
268 	if (f_flag || i_flag)
269 		u_flag = 1;
270 	if (t_flag)
271 		v_flag = 1;
272 	argc -= optind;
273 	argv += optind;
274 
275 	if (argc == 0) {
276 		disk = get_rootdisk();
277 	} else {
278 		if (stat(argv[0], &sb) == 0) {
279 			/* OK, full pathname given */
280 			disk = argv[0];
281 		} else if (errno == ENOENT) {
282 			/* Try prepending "/dev" */
283 			if ((disk = malloc(strlen(argv[0]) + strlen(_PATH_DEV) +
284 			     1)) == NULL)
285 				errx(1, "out of memory");
286 			strcpy(disk, _PATH_DEV);
287 			strcat(disk, argv[0]);
288 		} else {
289 			/* other stat error, let it fail below */
290 			disk = argv[0];
291 		}
292 	}
293 	if (open_disk(u_flag) < 0)
294 		err(1, "cannot open disk %s", disk);
295 
296 	/* (abu)use mboot.bootinst to probe for the sector size */
297 	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
298 		err(1, "cannot allocate buffer to determine disk sector size");
299 	read_disk(0, mboot.bootinst);
300 	free(mboot.bootinst);
301 	mboot.bootinst = NULL;
302 
303 	if (s_flag)
304 	{
305 		int i;
306 		struct dos_partition *partp;
307 
308 		if (read_s0())
309 			err(1, "read_s0");
310 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
311 		    dos_sectors);
312 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
313 		for (i = 0; i < NDOSPART; i++) {
314 			partp = ((struct dos_partition *) &mboot.parts) + i;
315 			if (partp->dp_start == 0 && partp->dp_size == 0)
316 				continue;
317 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
318 			    (u_long) partp->dp_start,
319 			    (u_long) partp->dp_size, partp->dp_typ,
320 			    partp->dp_flag);
321 		}
322 		exit(0);
323 	}
324 
325 	printf("******* Working on device %s *******\n",disk);
326 
327 	if (I_flag)
328 	{
329 		struct dos_partition *partp;
330 
331 		read_s0();
332 		reset_boot();
333 		partp = (struct dos_partition *) (&mboot.parts[0]);
334 		partp->dp_typ = DOSPTYP_386BSD;
335 		partp->dp_flag = ACTIVE;
336 		partp->dp_start = dos_sectors;
337 		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
338 		    dos_sectors;
339 
340 		dos(partp);
341 		if (v_flag)
342 			print_s0(-1);
343 		write_s0();
344 		exit(0);
345 	}
346 	if (f_flag)
347 	{
348 	    if (read_s0() || i_flag)
349 	    {
350 		reset_boot();
351 	    }
352 
353 	    if (!read_config(f_flag))
354 	    {
355 		exit(1);
356 	    }
357 	    if (v_flag)
358 	    {
359 		print_s0(-1);
360 	    }
361 	    if (!t_flag)
362 	    {
363 		write_s0();
364 	    }
365 	}
366 	else
367 	{
368 	    if(u_flag)
369 	    {
370 		get_params_to_use();
371 	    }
372 	    else
373 	    {
374 		print_params();
375 	    }
376 
377 	    if (read_s0())
378 		init_sector0(dos_sectors);
379 
380 	    printf("Media sector size is %d\n", secsize);
381 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
382 	    printf("Information from DOS bootblock is:\n");
383 	    if (partition == -1)
384 		for (i = 1; i <= NDOSPART; i++)
385 		    change_part(i);
386 	    else
387 		change_part(partition);
388 
389 	    if (u_flag || a_flag)
390 		change_active(partition);
391 
392 	    if (B_flag)
393 		change_code();
394 
395 	    if (u_flag || a_flag || B_flag) {
396 		if (!t_flag)
397 		{
398 		    printf("\nWe haven't changed the partition table yet.  ");
399 		    printf("This is your last chance.\n");
400 		}
401 		print_s0(-1);
402 		if (!t_flag)
403 		{
404 		    if (ok("Should we write new partition table?"))
405 			write_s0();
406 		}
407 		else
408 		{
409 		    printf("\n-t flag specified -- partition table not written.\n");
410 		}
411 	    }
412 	}
413 
414 	exit(0);
415 }
416 
417 static void
418 usage()
419 {
420 	fprintf(stderr, "%s%s",
421 		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
422  		"       fdisk -f configfile [-itv] [disk]\n");
423         exit(1);
424 }
425 
426 static void
427 print_s0(int which)
428 {
429 int	i;
430 
431 	print_params();
432 	printf("Information from DOS bootblock is:\n");
433 	if (which == -1)
434 		for (i = 1; i <= NDOSPART; i++)
435 			printf("%d: ", i), print_part(i);
436 	else
437 		print_part(which);
438 }
439 
440 static struct dos_partition mtpart = { 0 };
441 
442 static void
443 print_part(int i)
444 {
445 	struct	  dos_partition *partp;
446 	u_int64_t part_mb;
447 
448 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
449 
450 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
451 		printf("<UNUSED>\n");
452 		return;
453 	}
454 	/*
455 	 * Be careful not to overflow.
456 	 */
457 	part_mb = partp->dp_size;
458 	part_mb *= secsize;
459 	part_mb /= (1024 * 1024);
460 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
461 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
462 		(u_long)partp->dp_start,
463 		(u_long)partp->dp_size,
464 		part_mb,
465 		partp->dp_flag,
466 		partp->dp_flag == ACTIVE ? " (active)" : "");
467 	printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
468 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
469 		,partp->dp_shd
470 		,DPSECT(partp->dp_ssect)
471 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
472 		,partp->dp_ehd
473 		,DPSECT(partp->dp_esect));
474 }
475 
476 
477 static void
478 init_boot(void)
479 {
480 	const char *fname;
481 	int fd, n;
482 	struct stat sb;
483 
484 	fname = b_flag ? b_flag : "/boot/mbr";
485 	if ((fd = open(fname, O_RDONLY)) == -1 ||
486 	    fstat(fd, &sb) == -1)
487 		err(1, "%s", fname);
488 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
489 		errx(1, "%s: length must be a multiple of sector size", fname);
490 	if (mboot.bootinst != NULL)
491 		free(mboot.bootinst);
492 	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
493 		errx(1, "%s: unable to allocate read buffer", fname);
494 	if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
495 	    close(fd))
496 		err(1, "%s", fname);
497 	if (n != mboot.bootinst_size)
498 		errx(1, "%s: short read", fname);
499 }
500 
501 
502 static void
503 init_sector0(unsigned long start)
504 {
505 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
506 
507 	init_boot();
508 
509 	partp->dp_typ = DOSPTYP_386BSD;
510 	partp->dp_flag = ACTIVE;
511 	start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
512 	if(start == 0)
513 		start = dos_sectors;
514 	partp->dp_start = start;
515 	partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
516 
517 	dos(partp);
518 }
519 
520 static void
521 change_part(int i)
522 {
523 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
524 
525     printf("The data for partition %d is:\n", i);
526     print_part(i);
527 
528     if (u_flag && ok("Do you want to change it?")) {
529 	int tmp;
530 
531 	if (i_flag) {
532 		bzero((char *)partp, sizeof (struct dos_partition));
533 		if (i == 4) {
534 			init_sector0(1);
535 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
536 			print_part(i);
537 		}
538 	}
539 
540 	do {
541 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
542 		Decimal("start", partp->dp_start, tmp);
543 		Decimal("size", partp->dp_size, tmp);
544 		if (!sanitize_partition(partp)) {
545 			warnx("ERROR: failed to adjust; setting sysid to 0");
546 			partp->dp_typ = 0;
547 		}
548 
549 		if (ok("Explicitly specify beg/end address ?"))
550 		{
551 			int	tsec,tcyl,thd;
552 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
553 			thd = partp->dp_shd;
554 			tsec = DPSECT(partp->dp_ssect);
555 			Decimal("beginning cylinder", tcyl, tmp);
556 			Decimal("beginning head", thd, tmp);
557 			Decimal("beginning sector", tsec, tmp);
558 			partp->dp_scyl = DOSCYL(tcyl);
559 			partp->dp_ssect = DOSSECT(tsec,tcyl);
560 			partp->dp_shd = thd;
561 
562 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
563 			thd = partp->dp_ehd;
564 			tsec = DPSECT(partp->dp_esect);
565 			Decimal("ending cylinder", tcyl, tmp);
566 			Decimal("ending head", thd, tmp);
567 			Decimal("ending sector", tsec, tmp);
568 			partp->dp_ecyl = DOSCYL(tcyl);
569 			partp->dp_esect = DOSSECT(tsec,tcyl);
570 			partp->dp_ehd = thd;
571 		} else
572 			dos(partp);
573 
574 		print_part(i);
575 	} while (!ok("Are we happy with this entry?"));
576     }
577 }
578 
579 static void
580 print_params()
581 {
582 	printf("parameters extracted from in-core disklabel are:\n");
583 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
584 			,cyls,heads,sectors,cylsecs);
585 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
586 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
587 	printf("parameters to be used for BIOS calculations are:\n");
588 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
589 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
590 }
591 
592 static void
593 change_active(int which)
594 {
595 	struct dos_partition *partp = &mboot.parts[0];
596 	int active, i, new, tmp;
597 
598 	active = -1;
599 	for (i = 0; i < NDOSPART; i++) {
600 		if ((partp[i].dp_flag & ACTIVE) == 0)
601 			continue;
602 		printf("Partition %d is marked active\n", i + 1);
603 		if (active == -1)
604 			active = i + 1;
605 	}
606 	if (a_flag && which != -1)
607 		active = which;
608 	else if (active == -1)
609 		active = 1;
610 
611 	if (!ok("Do you want to change the active partition?"))
612 		return;
613 setactive:
614 	do {
615 		new = active;
616 		Decimal("active partition", new, tmp);
617 		if (new < 1 || new > 4) {
618 			printf("Active partition number must be in range 1-4."
619 					"  Try again.\n");
620 			goto setactive;
621 		}
622 		active = new;
623 	} while (!ok("Are you happy with this choice"));
624 	for (i = 0; i < NDOSPART; i++)
625 		partp[i].dp_flag = 0;
626 	if (active > 0 && active <= NDOSPART)
627 		partp[active-1].dp_flag = ACTIVE;
628 }
629 
630 static void
631 change_code()
632 {
633 	if (ok("Do you want to change the boot code?"))
634 		init_boot();
635 }
636 
637 void
638 get_params_to_use()
639 {
640 	int	tmp;
641 	print_params();
642 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
643 	{
644 		do
645 		{
646 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
647 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
648 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
649 			dos_cylsecs = dos_heads * dos_sectors;
650 			print_params();
651 		}
652 		while(!ok("Are you happy with this choice"));
653 	}
654 }
655 
656 
657 /***********************************************\
658 * Change real numbers into strange dos numbers	*
659 \***********************************************/
660 static void
661 dos(partp)
662 	struct dos_partition *partp;
663 {
664 	int cy, sec;
665 	u_int32_t end;
666 
667 	if (partp->dp_typ == 0 && partp->dp_start == 0 &&
668 	    partp->dp_size == 0) {
669 		bcopy(partp, &mtpart, sizeof(*partp));
670 		return;
671 	}
672 
673 	/* Start c/h/s. */
674 	partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
675 	cy = partp->dp_start / dos_cylsecs;
676 	sec = partp->dp_start % dos_sectors + 1;
677 	partp->dp_scyl = DOSCYL(cy);
678 	partp->dp_ssect = DOSSECT(sec, cy);
679 
680 	/* End c/h/s. */
681 	end = partp->dp_start + partp->dp_size - 1;
682 	partp->dp_ehd = end % dos_cylsecs / dos_sectors;
683 	cy = end / dos_cylsecs;
684 	sec = end % dos_sectors + 1;
685 	partp->dp_ecyl = DOSCYL(cy);
686 	partp->dp_esect = DOSSECT(sec, cy);
687 }
688 
689 int fd;
690 
691 	/* Getting device status */
692 
693 static int
694 open_disk(int u_flag)
695 {
696 	struct stat 	st;
697 
698 	if (stat(disk, &st) == -1) {
699 		if (errno == ENOENT)
700 			return -2;
701 		warnx("can't get file status of %s", disk);
702 		return -1;
703 	}
704 	if ( !(st.st_mode & S_IFCHR) )
705 		warnx("device %s is not character special", disk);
706 	if ((fd = open(disk,
707 	    a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
708 		if(errno == ENXIO)
709 			return -2;
710 		warnx("can't open device %s", disk);
711 		return -1;
712 	}
713 	if (get_params(0) == -1) {
714 		warnx("can't get disk parameters on %s", disk);
715 		return -1;
716 	}
717 	return fd;
718 }
719 
720 static ssize_t
721 read_disk(off_t sector, void *buf)
722 {
723 	lseek(fd,(sector * 512), 0);
724 	if( secsize == 0 )
725 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
726 			{
727 			/* try the read */
728 			int size = read(fd, buf, secsize);
729 			if( size == secsize )
730 				/* it worked so return */
731 				return secsize;
732 			}
733 	else
734 		return read( fd, buf, secsize );
735 
736 	/* we failed to read at any of the sizes */
737 	return -1;
738 }
739 
740 static ssize_t
741 write_disk(off_t sector, void *buf)
742 {
743 	lseek(fd,(sector * 512), 0);
744 	/* write out in the size that the read_disk found worked */
745 	return write(fd, buf, secsize);
746 }
747 
748 static int
749 get_params()
750 {
751 
752     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
753 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
754 	dos_cyls = cyls = 1;
755 	dos_heads = heads = 1;
756 	dos_sectors = sectors = 1;
757 	dos_cylsecs = cylsecs = heads * sectors;
758 	disksecs = cyls * heads * sectors;
759 	return disksecs;
760     }
761 
762     dos_cyls = cyls = disklabel.d_ncylinders;
763     dos_heads = heads = disklabel.d_ntracks;
764     dos_sectors = sectors = disklabel.d_nsectors;
765     dos_cylsecs = cylsecs = heads * sectors;
766     disksecs = cyls * heads * sectors;
767 
768     return (disksecs);
769 }
770 
771 
772 static int
773 read_s0()
774 {
775 	mboot.bootinst_size = secsize;
776 	if (mboot.bootinst != NULL)
777 		free(mboot.bootinst);
778 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
779 		warnx("unable to allocate buffer to read fdisk "
780 		      "partition table");
781 		return -1;
782 	}
783 	if (read_disk(0, mboot.bootinst) == -1) {
784 		warnx("can't read fdisk partition table");
785 		return -1;
786 	}
787 	if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
788 		warnx("invalid fdisk partition table found");
789 		/* So should we initialize things */
790 		return -1;
791 	}
792 	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
793 	return 0;
794 }
795 
796 static int
797 write_s0()
798 {
799 #ifdef NOT_NOW
800 	int	flag;
801 #endif
802 	int	sector;
803 
804 	if (iotest) {
805 		print_s0(-1);
806 		return 0;
807 	}
808 	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
809 	/*
810 	 * write enable label sector before write (if necessary),
811 	 * disable after writing.
812 	 * needed if the disklabel protected area also protects
813 	 * sector 0. (e.g. empty disk)
814 	 */
815 #ifdef NOT_NOW
816 	flag = 1;
817 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
818 		warn("ioctl DIOCWLABEL");
819 #endif
820 	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
821 		if (write_disk(sector,
822 			       &mboot.bootinst[sector * secsize]) == -1) {
823 			warn("can't write fdisk partition table");
824 			return -1;
825 #ifdef NOT_NOW
826 			flag = 0;
827 			(void) ioctl(fd, DIOCWLABEL, &flag);
828 #endif
829 		}
830 #ifdef NOT_NOW
831 	flag = 0;
832 	(void) ioctl(fd, DIOCWLABEL, &flag);
833 #endif
834 	return(0);
835 }
836 
837 
838 static int
839 ok(str)
840 char *str;
841 {
842 	printf("%s [n] ", str);
843 	fflush(stdout);
844 	if (fgets(lbuf, LBUF, stdin) == NULL)
845 		exit(1);
846 	lbuf[strlen(lbuf)-1] = 0;
847 
848 	if (*lbuf &&
849 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
850 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
851 		return 1;
852 	else
853 		return 0;
854 }
855 
856 static int
857 decimal(char *str, int *num, int deflt)
858 {
859 int acc = 0, c;
860 char *cp;
861 
862 	while (1) {
863 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
864 		fflush(stdout);
865 		if (fgets(lbuf, LBUF, stdin) == NULL)
866 			exit(1);
867 		lbuf[strlen(lbuf)-1] = 0;
868 
869 		if (!*lbuf)
870 			return 0;
871 
872 		cp = lbuf;
873 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
874 		if (!c)
875 			return 0;
876 		while ((c = *cp++)) {
877 			if (c <= '9' && c >= '0')
878 				acc = acc * 10 + c - '0';
879 			else
880 				break;
881 		}
882 		if (c == ' ' || c == '\t')
883 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
884 		if (!c) {
885 			*num = acc;
886 			return 1;
887 		} else
888 			printf("%s is an invalid decimal number.  Try again.\n",
889 				lbuf);
890 	}
891 
892 }
893 
894 #if 0
895 static int
896 hex(char *str, int *num, int deflt)
897 {
898 int acc = 0, c;
899 char *cp;
900 
901 	while (1) {
902 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
903 		fgets(lbuf, LBUF, stdin);
904 		lbuf[strlen(lbuf)-1] = 0;
905 
906 		if (!*lbuf)
907 			return 0;
908 
909 		cp = lbuf;
910 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
911 		if (!c)
912 			return 0;
913 		while ((c = *cp++)) {
914 			if (c <= '9' && c >= '0')
915 				acc = (acc << 4) + c - '0';
916 			else if (c <= 'f' && c >= 'a')
917 				acc = (acc << 4) + c - 'a' + 10;
918 			else if (c <= 'F' && c >= 'A')
919 				acc = (acc << 4) + c - 'A' + 10;
920 			else
921 				break;
922 		}
923 		if (c == ' ' || c == '\t')
924 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
925 		if (!c) {
926 			*num = acc;
927 			return 1;
928 		} else
929 			printf("%s is an invalid hex number.  Try again.\n",
930 				lbuf);
931 	}
932 
933 }
934 
935 static int
936 string(char *str, char **ans)
937 {
938 int c;
939 char *cp = lbuf;
940 
941 	while (1) {
942 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
943 		fgets(lbuf, LBUF, stdin);
944 		lbuf[strlen(lbuf)-1] = 0;
945 
946 		if (!*lbuf)
947 			return 0;
948 
949 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
950 		if (c == '"') {
951 			c = *++cp;
952 			*ans = cp;
953 			while ((c = *cp) && c != '"') cp++;
954 		} else {
955 			*ans = cp;
956 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
957 		}
958 
959 		if (c)
960 			*cp = 0;
961 		return 1;
962 	}
963 }
964 #endif
965 
966 static char *
967 get_type(int type)
968 {
969 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
970 	int	counter = 0;
971 	struct	part_type *ptr = part_types;
972 
973 
974 	while(counter < numentries)
975 	{
976 		if(ptr->type == type)
977 		{
978 			return(ptr->name);
979 		}
980 		ptr++;
981 		counter++;
982 	}
983 	return("unknown");
984 }
985 
986 
987 static void
988 parse_config_line(line, command)
989     char	*line;
990     CMD		*command;
991 {
992     char	*cp, *end;
993 
994     cp = line;
995     while (1)	/* dirty trick used to insure one exit point for this
996 		   function */
997     {
998 	memset(command, 0, sizeof(*command));
999 
1000 	while (isspace(*cp)) ++cp;
1001 	if (*cp == '\0' || *cp == '#')
1002 	{
1003 	    break;
1004 	}
1005 	command->cmd = *cp++;
1006 
1007 	/*
1008 	 * Parse args
1009 	 */
1010 	while (1)
1011 	{
1012 	    while (isspace(*cp)) ++cp;
1013 	    if (*cp == '#')
1014 	    {
1015 		break;		/* found comment */
1016 	    }
1017 	    if (isalpha(*cp))
1018 	    {
1019 		command->args[command->n_args].argtype = *cp++;
1020 	    }
1021 	    if (!isdigit(*cp))
1022 	    {
1023 		break;		/* assume end of line */
1024 	    }
1025 	    end = NULL;
1026 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1027 	    if (cp == end)
1028 	    {
1029 		break;		/* couldn't parse number */
1030 	    }
1031 	    cp = end;
1032 	    command->n_args++;
1033 	}
1034 	break;
1035     }
1036 }
1037 
1038 
1039 static int
1040 process_geometry(command)
1041     CMD		*command;
1042 {
1043     int		status = 1, i;
1044 
1045     while (1)
1046     {
1047 	geom_processed = 1;
1048 	if (part_processed)
1049 	{
1050 	    warnx(
1051 	"ERROR line %d: the geometry specification line must occur before\n\
1052     all partition specifications",
1053 		    current_line_number);
1054 	    status = 0;
1055 	    break;
1056 	}
1057 	if (command->n_args != 3)
1058 	{
1059 	    warnx("ERROR line %d: incorrect number of geometry args",
1060 		    current_line_number);
1061 	    status = 0;
1062 	    break;
1063 	}
1064 	dos_cyls = -1;
1065 	dos_heads = -1;
1066 	dos_sectors = -1;
1067 	for (i = 0; i < 3; ++i)
1068 	{
1069 	    switch (command->args[i].argtype)
1070 	    {
1071 	    case 'c':
1072 		dos_cyls = command->args[i].arg_val;
1073 		break;
1074 	    case 'h':
1075 		dos_heads = command->args[i].arg_val;
1076 		break;
1077 	    case 's':
1078 		dos_sectors = command->args[i].arg_val;
1079 		break;
1080 	    default:
1081 		warnx(
1082 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1083 			current_line_number, command->args[i].argtype,
1084 			command->args[i].argtype);
1085 		status = 0;
1086 		break;
1087 	    }
1088 	}
1089 	if (status == 0)
1090 	{
1091 	    break;
1092 	}
1093 
1094 	dos_cylsecs = dos_heads * dos_sectors;
1095 
1096 	/*
1097 	 * Do sanity checks on parameter values
1098 	 */
1099 	if (dos_cyls < 0)
1100 	{
1101 	    warnx("ERROR line %d: number of cylinders not specified",
1102 		    current_line_number);
1103 	    status = 0;
1104 	}
1105 	if (dos_cyls == 0 || dos_cyls > 1024)
1106 	{
1107 	    warnx(
1108 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1109     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1110     is dedicated to FreeBSD)",
1111 		    current_line_number, dos_cyls);
1112 	}
1113 
1114 	if (dos_heads < 0)
1115 	{
1116 	    warnx("ERROR line %d: number of heads not specified",
1117 		    current_line_number);
1118 	    status = 0;
1119 	}
1120 	else if (dos_heads < 1 || dos_heads > 256)
1121 	{
1122 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1123 		    current_line_number);
1124 	    status = 0;
1125 	}
1126 
1127 	if (dos_sectors < 0)
1128 	{
1129 	    warnx("ERROR line %d: number of sectors not specified",
1130 		    current_line_number);
1131 	    status = 0;
1132 	}
1133 	else if (dos_sectors < 1 || dos_sectors > 63)
1134 	{
1135 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1136 		    current_line_number);
1137 	    status = 0;
1138 	}
1139 
1140 	break;
1141     }
1142     return (status);
1143 }
1144 
1145 
1146 static int
1147 process_partition(command)
1148     CMD		*command;
1149 {
1150     int				status = 0, partition;
1151     u_int32_t			prev_head_boundary, prev_cyl_boundary;
1152     u_int32_t			adj_size, max_end;
1153     struct dos_partition	*partp;
1154 
1155     while (1)
1156     {
1157 	part_processed = 1;
1158 	if (command->n_args != 4)
1159 	{
1160 	    warnx("ERROR line %d: incorrect number of partition args",
1161 		    current_line_number);
1162 	    break;
1163 	}
1164 	partition = command->args[0].arg_val;
1165 	if (partition < 1 || partition > 4)
1166 	{
1167 	    warnx("ERROR line %d: invalid partition number %d",
1168 		    current_line_number, partition);
1169 	    break;
1170 	}
1171 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1172 	bzero((char *)partp, sizeof (struct dos_partition));
1173 	partp->dp_typ = command->args[1].arg_val;
1174 	partp->dp_start = command->args[2].arg_val;
1175 	partp->dp_size = command->args[3].arg_val;
1176 	max_end = partp->dp_start + partp->dp_size;
1177 
1178 	if (partp->dp_typ == 0)
1179 	{
1180 	    /*
1181 	     * Get out, the partition is marked as unused.
1182 	     */
1183 	    /*
1184 	     * Insure that it's unused.
1185 	     */
1186 	    bzero((char *)partp, sizeof (struct dos_partition));
1187 	    status = 1;
1188 	    break;
1189 	}
1190 
1191 	/*
1192 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1193 	 */
1194 	if (partp->dp_start % dos_sectors != 0)
1195 	{
1196 	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1197 	    if (max_end < dos_sectors ||
1198 		prev_head_boundary > max_end - dos_sectors)
1199 	    {
1200 		/*
1201 		 * Can't go past end of partition
1202 		 */
1203 		warnx(
1204 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1205     a head boundary",
1206 			current_line_number, partition);
1207 		break;
1208 	    }
1209 	    warnx(
1210 	"WARNING: adjusting start offset of partition %d\n\
1211     from %u to %u, to fall on a head boundary",
1212 		    partition, (u_int)partp->dp_start,
1213 		    (u_int)(prev_head_boundary + dos_sectors));
1214 	    partp->dp_start = prev_head_boundary + dos_sectors;
1215 	}
1216 
1217 	/*
1218 	 * Adjust size downwards, if necessary, to fall on a cylinder
1219 	 * boundary.
1220 	 */
1221 	prev_cyl_boundary =
1222 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1223 	if (prev_cyl_boundary > partp->dp_start)
1224 	    adj_size = prev_cyl_boundary - partp->dp_start;
1225 	else
1226 	{
1227 	    warnx(
1228 	"ERROR: could not adjust partition to start on a head boundary\n\
1229     and end on a cylinder boundary.");
1230 	    return (0);
1231 	}
1232 	if (adj_size != partp->dp_size)
1233 	{
1234 	    warnx(
1235 	"WARNING: adjusting size of partition %d from %u to %u\n\
1236     to end on a cylinder boundary",
1237 		    partition, (u_int)partp->dp_size, (u_int)adj_size);
1238 	    partp->dp_size = adj_size;
1239 	}
1240 	if (partp->dp_size == 0)
1241 	{
1242 	    warnx("ERROR line %d: size of partition %d is zero",
1243 		    current_line_number, partition);
1244 	    break;
1245 	}
1246 
1247 	dos(partp);
1248 	status = 1;
1249 	break;
1250     }
1251     return (status);
1252 }
1253 
1254 
1255 static int
1256 process_active(command)
1257     CMD		*command;
1258 {
1259     int				status = 0, partition, i;
1260     struct dos_partition	*partp;
1261 
1262     while (1)
1263     {
1264 	active_processed = 1;
1265 	if (command->n_args != 1)
1266 	{
1267 	    warnx("ERROR line %d: incorrect number of active args",
1268 		    current_line_number);
1269 	    status = 0;
1270 	    break;
1271 	}
1272 	partition = command->args[0].arg_val;
1273 	if (partition < 1 || partition > 4)
1274 	{
1275 	    warnx("ERROR line %d: invalid partition number %d",
1276 		    current_line_number, partition);
1277 	    break;
1278 	}
1279 	/*
1280 	 * Reset active partition
1281 	 */
1282 	partp = ((struct dos_partition *) &mboot.parts);
1283 	for (i = 0; i < NDOSPART; i++)
1284 	    partp[i].dp_flag = 0;
1285 	partp[partition-1].dp_flag = ACTIVE;
1286 
1287 	status = 1;
1288 	break;
1289     }
1290     return (status);
1291 }
1292 
1293 
1294 static int
1295 process_line(line)
1296     char	*line;
1297 {
1298     CMD		command;
1299     int		status = 1;
1300 
1301     while (1)
1302     {
1303 	parse_config_line(line, &command);
1304 	switch (command.cmd)
1305 	{
1306 	case 0:
1307 	    /*
1308 	     * Comment or blank line
1309 	     */
1310 	    break;
1311 	case 'g':
1312 	    /*
1313 	     * Set geometry
1314 	     */
1315 	    status = process_geometry(&command);
1316 	    break;
1317 	case 'p':
1318 	    status = process_partition(&command);
1319 	    break;
1320 	case 'a':
1321 	    status = process_active(&command);
1322 	    break;
1323 	default:
1324 	    status = 0;
1325 	    break;
1326 	}
1327 	break;
1328     }
1329     return (status);
1330 }
1331 
1332 
1333 static int
1334 read_config(config_file)
1335     char *config_file;
1336 {
1337     FILE	*fp = NULL;
1338     int		status = 1;
1339     char	buf[1010];
1340 
1341     while (1)	/* dirty trick used to insure one exit point for this
1342 		   function */
1343     {
1344 	if (strcmp(config_file, "-") != 0)
1345 	{
1346 	    /*
1347 	     * We're not reading from stdin
1348 	     */
1349 	    if ((fp = fopen(config_file, "r")) == NULL)
1350 	    {
1351 		status = 0;
1352 		break;
1353 	    }
1354 	}
1355 	else
1356 	{
1357 	    fp = stdin;
1358 	}
1359 	current_line_number = 0;
1360 	while (!feof(fp))
1361 	{
1362 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1363 	    {
1364 		break;
1365 	    }
1366 	    ++current_line_number;
1367 	    status = process_line(buf);
1368 	    if (status == 0)
1369 	    {
1370 		break;
1371 	    }
1372 	}
1373 	break;
1374     }
1375     if (fp)
1376     {
1377 	/*
1378 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1379 	 */
1380 	fclose(fp);
1381     }
1382     return (status);
1383 }
1384 
1385 
1386 static void
1387 reset_boot(void)
1388 {
1389     int				i;
1390     struct dos_partition	*partp;
1391 
1392     init_boot();
1393     for (i = 0; i < 4; ++i)
1394     {
1395 	partp = ((struct dos_partition *) &mboot.parts) + i;
1396 	bzero((char *)partp, sizeof (struct dos_partition));
1397     }
1398 }
1399 
1400 static int
1401 sanitize_partition(partp)
1402     struct dos_partition	*partp;
1403 {
1404     u_int32_t			prev_head_boundary, prev_cyl_boundary;
1405     u_int32_t			max_end, size, start;
1406 
1407     start = partp->dp_start;
1408     size = partp->dp_size;
1409     max_end = start + size;
1410     /* Only allow a zero size if the partition is being marked unused. */
1411     if (size == 0) {
1412 	if (start == 0 && partp->dp_typ == 0)
1413 	    return (1);
1414 	warnx("ERROR: size of partition is zero");
1415 	return (0);
1416     }
1417     /* Return if no adjustment is necessary. */
1418     if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1419 	return (1);
1420 
1421     if (start % dos_sectors != 0)
1422 	warnx("WARNING: partition does not start on a head boundary");
1423     if ((start  +size) % dos_sectors != 0)
1424 	warnx("WARNING: partition does not end on a cylinder boundary");
1425     warnx("WARNING: this may confuse the BIOS or some operating systems");
1426     if (!ok("Correct this automatically?"))
1427 	return (1);
1428 
1429     /*
1430      * Adjust start upwards, if necessary, to fall on an head boundary.
1431      */
1432     if (start % dos_sectors != 0) {
1433 	prev_head_boundary = start / dos_sectors * dos_sectors;
1434 	if (max_end < dos_sectors ||
1435 	    prev_head_boundary >= max_end - dos_sectors) {
1436 	    /*
1437 	     * Can't go past end of partition
1438 	     */
1439 	    warnx(
1440     "ERROR: unable to adjust start of partition to fall on a head boundary");
1441 	    return (0);
1442         }
1443 	start = prev_head_boundary + dos_sectors;
1444     }
1445 
1446     /*
1447      * Adjust size downwards, if necessary, to fall on a cylinder
1448      * boundary.
1449      */
1450     prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1451     if (prev_cyl_boundary > start)
1452 	size = prev_cyl_boundary - start;
1453     else {
1454 	warnx("ERROR: could not adjust partition to start on a head boundary\n\
1455     and end on a cylinder boundary.");
1456 	return (0);
1457     }
1458 
1459     /* Finally, commit any changes to partp and return. */
1460     if (start != partp->dp_start) {
1461 	warnx("WARNING: adjusting start offset of partition to %u",
1462 	    (u_int)start);
1463 	partp->dp_start = start;
1464     }
1465     if (size != partp->dp_size) {
1466 	warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1467 	partp->dp_size = size;
1468     }
1469 
1470     return (1);
1471 }
1472 
1473 /*
1474  * Try figuring out the root device's canonical disk name.
1475  * The following choices are considered:
1476  *   /dev/ad0s1a     => /dev/ad0
1477  *   /dev/da0a       => /dev/da0
1478  *   /dev/vinum/root => /dev/vinum/root
1479  */
1480 static char *
1481 get_rootdisk(void)
1482 {
1483 	struct statfs rootfs;
1484 	regex_t re;
1485 #define NMATCHES 2
1486 	regmatch_t rm[NMATCHES];
1487 	char *s;
1488 	int rv;
1489 
1490 	if (statfs("/", &rootfs) == -1)
1491 		err(1, "statfs(\"/\")");
1492 
1493 	if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1494 		    REG_EXTENDED)) != 0)
1495 		errx(1, "regcomp() failed (%d)", rv);
1496 	if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1497 		errx(1,
1498 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1499 		    rv);
1500 	if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1501 		errx(1, "out of memory");
1502 	memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1503 	    rm[1].rm_eo - rm[1].rm_so);
1504 	s[rm[1].rm_eo - rm[1].rm_so] = 0;
1505 
1506 	return s;
1507 }
1508