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