]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fdisk_pc98/fdisk.c
This commit was generated by cvs2svn to compensate for changes in r168463,
[FreeBSD/FreeBSD.git] / sbin / fdisk_pc98 / 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/diskpc98.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 <libgeom.h>
41 #include <paths.h>
42 #include <regex.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 int iotest;
50
51 #define LBUF 100
52 static char lbuf[LBUF];
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 String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
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[510];
79         unsigned short int      signature;
80         struct  pc98_partition parts[8];
81         unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
82 };
83
84 static struct mboot mboot;
85 static int fd;
86
87 #define ACTIVE 0x80
88
89 static uint dos_cyls;
90 static uint dos_heads;
91 static uint dos_sectors;
92 static uint dos_cylsecs;
93
94 #define MAX_ARGS        10
95
96 typedef struct cmd {
97     char                cmd;
98     int                 n_args;
99     struct arg {
100         char    argtype;
101         int     arg_val;
102     }                   args[MAX_ARGS];
103 } CMD;
104
105 static int B_flag  = 0;         /* replace boot code */
106 static int I_flag  = 0;         /* Inizialize disk to defaults */
107 static int a_flag  = 0;         /* set active partition */
108 static int i_flag  = 0;         /* replace partition data */
109 static int u_flag  = 0;         /* update partition data */
110 static int s_flag  = 0;         /* Print a summary and exit */
111 static int t_flag  = 0;         /* test only */
112 static char *f_flag = NULL;     /* Read config info from file */
113 static int v_flag  = 0;         /* Be verbose */
114
115 static struct part_type
116 {
117         unsigned char type;
118         const char *name;
119 } part_types[] = {
120          {0x00, "unused"}
121         ,{0x01, "Primary DOS with 12 bit FAT"}
122         ,{0x11, "MSDOS"}
123         ,{0x20, "MSDOS"}
124         ,{0x21, "MSDOS"}
125         ,{0x22, "MSDOS"}
126         ,{0x23, "MSDOS"}
127         ,{0x02, "XENIX / file system"}  
128         ,{0x03, "XENIX /usr file system"}  
129         ,{0x04, "PC-UX"}   
130         ,{0x05, "Extended DOS"}   
131         ,{0x06, "Primary 'big' DOS (> 32MB)"}   
132         ,{0x07, "OS/2 HPFS, QNX or Advanced UNIX"}  
133         ,{0x08, "AIX file system"}   
134         ,{0x09, "AIX boot partition or Coherent"}  
135         ,{0x0A, "OS/2 Boot Manager or OPUS"}  
136         ,{0x10, "OPUS"} 
137         ,{0x14, "FreeBSD/NetBSD/386BSD"}  
138         ,{0x94, "FreeBSD/NetBSD/386BSD"}
139         ,{0x40, "VENIX 286"}  
140         ,{0x50, "DM"}   
141         ,{0x51, "DM"}   
142         ,{0x52, "CP/M or Microport SysV/AT"}
143         ,{0x56, "GB"}
144         ,{0x61, "Speed"}
145         ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
146         ,{0x64, "Novell Netware 2.xx"}
147         ,{0x65, "Novell Netware 3.xx"}
148         ,{0x75, "PCIX"}
149         ,{0x40, "Minix"} 
150 };
151
152 static void print_s0(int which);
153 static void print_part(int i);
154 static void init_sector0(unsigned long start);
155 static void init_boot(void);
156 static void change_part(int i);
157 static void print_params(void);
158 static void change_active(int which);
159 static void change_code(void);
160 static void get_params_to_use(void);
161 static char *get_rootdisk(void);
162 static void dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp);
163 static int open_disk(int flag);
164 static ssize_t read_disk(off_t sector, void *buf);
165 static int write_disk(off_t sector, void *buf);
166 static int get_params(void);
167 static int read_s0(void);
168 static int write_s0(void);
169 static int ok(const char *str);
170 static int decimal(const char *str, int *num, int deflt);
171 static const char *get_type(int type);
172 static void usage(void);
173 static int string(const char *str, char **ans);
174 static void reset_boot(void);
175
176 int
177 main(int argc, char *argv[])
178 {
179         struct  stat sb;
180         int     c, i;
181         int     partition = -1;
182         struct  pc98_partition *partp;
183
184         while ((c = getopt(argc, argv, "BIa:f:istuv12345678")) != -1)
185                 switch (c) {
186                 case 'B':
187                         B_flag = 1;
188                         break;
189                 case 'I':
190                         I_flag = 1;
191                         break;
192                 case 'a':
193                         a_flag = 1;
194                         break;
195                 case 'f':
196                         f_flag = optarg;
197                         break;
198                 case 'i':
199                         i_flag = 1;
200                         break;
201                 case 's':
202                         s_flag = 1;
203                         break;
204                 case 't':
205                         t_flag = 1;
206                         break;
207                 case 'u':
208                         u_flag = 1;
209                         break;
210                 case 'v':
211                         v_flag = 1;
212                         break;
213                 case '1':
214                 case '2':
215                 case '3':
216                 case '4':
217                 case '5':
218                 case '6':
219                 case '7':
220                 case '8':
221                         partition = c - '0';
222                         break;
223                 default:
224                         usage();
225                 }
226         if (f_flag || i_flag)
227                 u_flag = 1;
228         if (t_flag)
229                 v_flag = 1;
230         argc -= optind;
231         argv += optind;
232
233         if (argc == 0) {
234                 disk = get_rootdisk();
235         } else {
236                 if (stat(argv[0], &sb) == 0) {
237                         /* OK, full pathname given */
238                         disk = argv[0];
239                 } else if (errno == ENOENT && argv[0][0] != '/') {
240                         /* Try prepending "/dev" */
241                         asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
242                         if (disk == NULL)
243                                 errx(1, "out of memory");
244                 } else {
245                         /* other stat error, let it fail below */
246                         disk = argv[0];
247                 }
248         }
249         if (open_disk(u_flag) < 0)
250                 err(1, "cannot open disk %s", disk);
251
252         if (s_flag) {
253                 if (read_s0())
254                         err(1, "read_s0");
255                 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
256                     dos_sectors);
257                 printf("Part  %11s %11s %4s %4s\n", "Start", "Size", "MID",
258                     "SID");
259                 for (i = 0; i < NDOSPART; i++) {
260                         partp = ((struct pc98_partition *) &mboot.parts) + i;
261                         if (partp->dp_sid == 0)
262                                 continue;
263                         printf("%4d: %11u %11u 0x%02x 0x%02x\n", i + 1,
264                             partp->dp_scyl * cylsecs,
265                             (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs,
266                             partp->dp_mid, partp->dp_sid);
267                 }
268                 exit(0);
269         }
270
271         printf("******* Working on device %s *******\n",disk);
272
273         if (I_flag) {
274                 read_s0();
275                 reset_boot();
276                 partp = (struct pc98_partition *) (&mboot.parts[0]);
277                 partp->dp_mid = DOSMID_386BSD;
278                 partp->dp_sid = DOSSID_386BSD;
279                 /* Start c/h/s. */
280                 partp->dp_scyl = partp->dp_ipl_cyl = 1;
281                 partp->dp_shd = partp->dp_ipl_head = 1;
282                 partp->dp_ssect = partp->dp_ipl_sct = 0;
283
284                 /* End c/h/s. */
285                 partp->dp_ecyl = dos_cyls - 1;
286                 partp->dp_ehd = dos_cylsecs / dos_sectors;
287                 partp->dp_esect = dos_sectors;
288
289                 if (v_flag)
290                         print_s0(-1);
291                 if (!t_flag)
292                         write_s0();
293                 exit(0);
294         }
295
296         if (f_flag) {
297             if (v_flag)
298                 print_s0(-1);
299             if (!t_flag)
300                 write_s0();
301         } else {
302             if(u_flag)
303                 get_params_to_use();
304             else
305                 print_params();
306
307             if (read_s0())
308                 init_sector0(dos_sectors);
309
310             printf("Media sector size is %d\n", secsize);
311             printf("Warning: BIOS sector numbering starts with sector 1\n");
312             printf("Information from DOS bootblock is:\n");
313             if (partition == -1)
314                 for (i = 1; i <= NDOSPART; i++)
315                     change_part(i);
316             else
317                 change_part(partition);
318
319             if (u_flag || a_flag)
320                 change_active(partition);
321
322             if (B_flag)
323                 change_code();
324
325             if (u_flag || a_flag || B_flag) {
326                 if (!t_flag) {
327                     printf("\nWe haven't changed the partition table yet.  ");
328                     printf("This is your last chance.\n");
329                 }
330                 print_s0(-1);
331                 if (!t_flag) {
332                     if (ok("Should we write new partition table?"))
333                         write_s0();
334                 } else {
335                     printf("\n-t flag specified -- partition table not written.\n");
336                 }
337             }
338         }
339
340         exit(0);
341 }
342
343 static void
344 usage()
345 {
346         fprintf(stderr, "%s%s",
347                 "usage: fdisk [-BIaistu] [-12345678] [disk]\n",
348                 "       fdisk -f configfile [-itv] [disk]\n");
349         exit(1);
350 }
351
352 static void
353 print_s0(int which)
354 {
355         int     i;
356
357         print_params();
358         printf("Information from DOS bootblock is:\n");
359         if (which == -1)
360                 for (i = 1; i <= NDOSPART; i++)
361                         printf("%d: ", i), print_part(i);
362         else
363                 print_part(which);
364 }
365
366 static struct pc98_partition mtpart;
367
368 static void
369 print_part(int i)
370 {
371         struct    pc98_partition *partp;
372         u_int64_t part_sz, part_mb;
373
374         partp = ((struct pc98_partition *) &mboot.parts) + i - 1;
375
376         if (!bcmp(partp, &mtpart, sizeof (struct pc98_partition))) {
377                 printf("<UNUSED>\n");
378                 return;
379         }
380         /*
381          * Be careful not to overflow.
382          */
383         part_sz = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
384         part_mb = part_sz * secsize;
385         part_mb /= (1024 * 1024);
386         printf("sysmid %d (%#04x),(%s)\n", partp->dp_mid, partp->dp_mid,
387             get_type(partp->dp_mid));
388         printf("    start %lu, size %lu (%ju Meg), sid %d\n",
389                 (u_long)(partp->dp_scyl * cylsecs), (u_long)part_sz,
390                 (uintmax_t)part_mb, partp->dp_sid);
391         printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
392                 ,partp->dp_scyl
393                 ,partp->dp_shd
394                 ,partp->dp_ssect
395                 ,partp->dp_ecyl
396                 ,partp->dp_ehd
397                 ,partp->dp_esect);
398         printf ("\tsystem Name %.16s\n", partp->dp_name);
399 }
400
401
402 static void
403 init_boot(void)
404 {
405
406         mboot.signature = DOSMAGIC;
407 }
408
409
410 static void
411 init_sector0(unsigned long start)
412 {
413         struct pc98_partition *partp =
414                 (struct pc98_partition *)(&mboot.parts[0]);
415
416         init_boot();
417
418         partp->dp_mid = DOSMID_386BSD;
419         partp->dp_sid = DOSSID_386BSD;
420
421         dos(start, disksecs - start, partp);
422 }
423
424 static void
425 change_part(int i)
426 {
427         struct pc98_partition *partp =
428                 ((struct pc98_partition *) &mboot.parts) + i - 1;
429
430     printf("The data for partition %d is:\n", i);
431     print_part(i);
432
433     if (u_flag && ok("Do you want to change it?")) {
434         int tmp;
435
436         if (i_flag) {
437                 bzero((char *)partp, sizeof (struct pc98_partition));
438                 if (i == 1) {
439                         init_sector0(1);
440                         printf("\nThe static data for the slice 1 has been reinitialized to:\n");
441                         print_part(i);
442                 }
443         }
444
445         do {
446                 int x_start = partp->dp_scyl * cylsecs ;
447                 int x_size  = (partp->dp_ecyl - partp->dp_scyl + 1) * cylsecs;
448                 Decimal("sysmid", partp->dp_mid, tmp);
449                 Decimal("syssid", partp->dp_sid, tmp);
450                 String ("system name", partp->dp_name, 16);
451                 Decimal("start", x_start, tmp);
452                 Decimal("size", x_size, tmp);
453
454                 if (ok("Explicitly specify beg/end address ?"))
455                 {
456                         int     tsec,tcyl,thd;
457                         tcyl = partp->dp_scyl;
458                         thd = partp->dp_shd;
459                         tsec = partp->dp_ssect;
460                         Decimal("beginning cylinder", tcyl, tmp);
461                         Decimal("beginning head", thd, tmp);
462                         Decimal("beginning sector", tsec, tmp);
463                         partp->dp_scyl = tcyl;
464                         partp->dp_ssect = tsec;
465                         partp->dp_shd = thd;
466                         partp->dp_ipl_cyl = partp->dp_scyl;
467                         partp->dp_ipl_sct = partp->dp_ssect;
468                         partp->dp_ipl_head = partp->dp_shd;
469
470                         tcyl = partp->dp_ecyl;
471                         thd = partp->dp_ehd;
472                         tsec = partp->dp_esect;
473                         Decimal("ending cylinder", tcyl, tmp);
474                         Decimal("ending head", thd, tmp);
475                         Decimal("ending sector", tsec, tmp);
476                         partp->dp_ecyl = tcyl;
477                         partp->dp_esect = tsec;
478                         partp->dp_ehd = thd;
479                 } else
480                         dos(x_start, x_size, partp);
481
482                 print_part(i);
483         } while (!ok("Are we happy with this entry?"));
484     }
485 }
486
487 static void
488 print_params()
489 {
490         printf("parameters extracted from in-core disklabel are:\n");
491         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
492                         ,cyls,heads,sectors,cylsecs);
493         if (dos_cyls > 65535 || dos_heads > 255 || dos_sectors > 255)
494                 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
495         printf("parameters to be used for BIOS calculations are:\n");
496         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
497                 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
498 }
499
500 static void
501 change_active(int which)
502 {
503         struct pc98_partition *partp = &mboot.parts[0];
504         int active, i, new, tmp;
505
506         active = -1;
507         for (i = 0; i < NDOSPART; i++) {
508                 if ((partp[i].dp_sid & ACTIVE) == 0)
509                         continue;
510                 printf("Partition %d is marked active\n", i + 1);
511                 if (active == -1)
512                         active = i + 1;
513         }
514         if (a_flag && which != -1)
515                 active = which;
516         else if (active == -1)
517                 active = 1;
518
519         if (!ok("Do you want to change the active partition?"))
520                 return;
521 setactive:
522         do {
523                 new = active;
524                 Decimal("active partition", new, tmp);
525                 if (new < 1 || new > 8) {
526                         printf("Active partition number must be in range 1-8."
527                                         "  Try again.\n");
528                         goto setactive;
529                 }
530                 active = new;
531         } while (!ok("Are you happy with this choice"));
532         if (active > 0 && active <= 8)
533                 partp[active-1].dp_sid |= ACTIVE;
534 }
535
536 static void
537 change_code()
538 {
539         if (ok("Do you want to change the boot code?"))
540                 init_boot();
541 }
542
543 void
544 get_params_to_use()
545 {
546         int     tmp;
547         print_params();
548         if (ok("Do you want to change our idea of what BIOS thinks ?"))
549         {
550                 do
551                 {
552                         Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
553                         Decimal("BIOS's idea of #heads", dos_heads, tmp);
554                         Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
555                         dos_cylsecs = dos_heads * dos_sectors;
556                         print_params();
557                 }
558                 while(!ok("Are you happy with this choice"));
559         }
560 }
561
562
563 /***********************************************\
564 * Change real numbers into strange dos numbers  *
565 \***********************************************/
566 static void
567 dos(u_int32_t start, u_int32_t size, struct pc98_partition *partp)
568 {
569         u_int32_t end;
570
571         if (partp->dp_mid == 0 && partp->dp_sid == 0 &&
572             start == 0 && size == 0) {
573                 memcpy(partp, &mtpart, sizeof(*partp));
574                 return;
575         }
576
577         /* Start c/h/s. */
578         partp->dp_scyl = partp->dp_ipl_cyl = start / dos_cylsecs;
579         partp->dp_shd = partp->dp_ipl_head = start % dos_cylsecs / dos_sectors;
580         partp->dp_ssect = partp->dp_ipl_sct = start % dos_sectors;
581
582         /* End c/h/s. */
583         end = start + size - cylsecs;
584         partp->dp_ecyl = end / dos_cylsecs;
585         partp->dp_ehd = end % dos_cylsecs / dos_sectors;
586         partp->dp_esect = end % dos_sectors;
587 }
588
589 static int
590 open_disk(int flag)
591 {
592         struct stat     st;
593         int rwmode;
594
595         if (stat(disk, &st) == -1) {
596                 if (errno == ENOENT)
597                         return -2;
598                 warnx("can't get file status of %s", disk);
599                 return -1;
600         }
601         if ( !(st.st_mode & S_IFCHR) )
602                 warnx("device %s is not character special", disk);
603         rwmode = I_flag || a_flag || B_flag || flag ? O_RDWR : O_RDONLY;
604         fd = open(disk, rwmode);
605         if (fd == -1 && errno == EPERM && rwmode == O_RDWR)
606                 fd = open(disk, O_RDONLY);
607         if (fd == -1 && errno == ENXIO)
608                 return -2;
609         if (fd == -1) {
610                 warnx("can't open device %s", disk);
611                 return -1;
612         }
613         if (get_params() == -1) {
614                 warnx("can't get disk parameters on %s", disk);
615                 return -1;
616         }
617         return fd;
618 }
619
620 static ssize_t
621 read_disk(off_t sector, void *buf)
622 {
623
624         lseek(fd, (sector * 512), 0);
625         return read(fd, buf,
626                     secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
627 }
628
629 static int
630 write_disk(off_t sector, void *buf)
631 {
632         int error;
633         struct gctl_req *grq;
634         const char *q;
635         char fbuf[BUFSIZ];
636         int i, fdw, sz;
637
638         sz = secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2;
639         grq = gctl_get_handle();
640         gctl_ro_param(grq, "verb", -1, "write PC98");
641         gctl_ro_param(grq, "class", -1, "PC98");
642         q = strrchr(disk, '/');
643         if (q == NULL)
644                 q = disk;
645         else
646                 q++;
647         gctl_ro_param(grq, "geom", -1, q);
648         gctl_ro_param(grq, "data", sz, buf);
649         q = gctl_issue(grq);
650         if (q == NULL) {
651                 gctl_free(grq);
652                 return(0);
653         }
654         warnx("Geom problem: %s", q);
655         gctl_free(grq);
656
657         warnx("Warning: Partitioning via geom failed, trying raw write");
658         error = pwrite(fd, buf, sz, sector * 512);
659         if (error == sz)
660                 return (0);
661
662         for (i = 0; i < NDOSPART; i++) {
663                 sprintf(fbuf, "%ss%d", disk, i + 1);
664                 fdw = open(fbuf, O_RDWR, 0);
665                 if (fdw < 0)
666                         continue;
667                 error = ioctl(fdw, DIOCSPC98, buf);
668                 close(fdw);
669                 if (error == 0)
670                         return (0);
671         }
672         warnx("Failed to write sector zero");
673         return(EINVAL);
674 }
675
676 static int
677 get_params()
678 {
679         int error;
680         u_int u;
681         off_t o;
682
683         error = ioctl(fd, DIOCGFWSECTORS, &u);
684         if (error == 0)
685                 sectors = dos_sectors = u;
686         else
687                 sectors = dos_sectors = 17;
688
689         error = ioctl(fd, DIOCGFWHEADS, &u);
690         if (error == 0)
691                 heads = dos_heads = u;
692         else
693                 heads = dos_heads = 8;
694
695         dos_cylsecs = cylsecs = heads * sectors;
696         disksecs = cyls * heads * sectors;
697
698         error = ioctl(fd, DIOCGSECTORSIZE, &u);
699         if (error != 0 || u == 0)
700                 u = 512;
701         secsize = u;
702
703         error = ioctl(fd, DIOCGMEDIASIZE, &o);
704         if (error == 0) {
705                 disksecs = o / u;
706                 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
707         }
708
709         return (disksecs);
710 }
711 \f
712
713 static int
714 read_s0()
715 {
716
717         if (read_disk(0, (char *) mboot.bootinst) == -1) {
718                 warnx("can't read fdisk partition table");
719                 return -1;
720         }
721         if (mboot.signature != DOSMAGIC) {
722                 warnx("invalid fdisk partition table found");
723                 /* So should we initialize things */
724                 return -1;
725         }
726
727         return 0;
728 }
729
730 static int
731 write_s0()
732 {
733
734         if (iotest) {
735                 print_s0(-1);
736                 return 0;
737         }
738
739         /*
740          * write enable label sector before write (if necessary),
741          * disable after writing.
742          * needed if the disklabel protected area also protects
743          * sector 0. (e.g. empty disk)
744          */
745         if (write_disk(0, (char *) mboot.bootinst) == -1) {
746                 warn("can't write fdisk partition table");
747                 return -1;
748         }
749
750         return(0);
751 }
752
753
754 static int
755 ok(const char *str)
756 {
757         printf("%s [n] ", str);
758         fflush(stdout);
759         if (fgets(lbuf, LBUF, stdin) == NULL)
760                 exit(1);
761         lbuf[strlen(lbuf)-1] = 0;
762
763         if (*lbuf &&
764                 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
765                  !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
766                 return 1;
767         else
768                 return 0;
769 }
770
771 static int
772 decimal(const char *str, int *num, int deflt)
773 {
774         int acc = 0, c;
775         char *cp;
776
777         while (1) {
778                 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
779                 fflush(stdout);
780                 if (fgets(lbuf, LBUF, stdin) == NULL)
781                         exit(1);
782                 lbuf[strlen(lbuf)-1] = 0;
783
784                 if (!*lbuf)
785                         return 0;
786
787                 cp = lbuf;
788                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
789                 if (!c)
790                         return 0;
791                 while ((c = *cp++)) {
792                         if (c <= '9' && c >= '0')
793                                 acc = acc * 10 + c - '0';
794                         else
795                                 break;
796                 }
797                 if (c == ' ' || c == '\t')
798                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
799                 if (!c) {
800                         *num = acc;
801                         return 1;
802                 } else
803                         printf("%s is an invalid decimal number.  Try again.\n",
804                                 lbuf);
805         }
806
807 }
808
809 static int
810 string(const char *str, char **ans)
811 {
812         int i, c;
813         char *cp = lbuf;
814
815         while (1) {
816                 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
817                 fgets(lbuf, LBUF, stdin);
818                 lbuf[strlen(lbuf)-1] = 0;
819
820                 if (!*lbuf)
821                         return 0;
822
823                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
824                 if (c == '"') {
825                         c = *++cp;
826                         *ans = cp;
827                         while ((c = *cp) && c != '"') cp++;
828                 } else {
829                         *ans = cp;
830                         while ((c = *cp) && c != ' ' && c != '\t') cp++;
831                 }
832
833                 for (i = strlen(*ans); i < 16; i++)
834                         (*ans)[i] = ' ';
835                 (*ans)[16] = 0;
836
837                 return 1;
838         }
839 }
840
841 static const char *
842 get_type(int type)
843 {
844         int     numentries = (sizeof(part_types)/sizeof(struct part_type));
845         int     counter = 0;
846         struct  part_type *ptr = part_types;
847
848
849         while(counter < numentries) {
850                 if(ptr->type == (type & 0x7f))
851                         return(ptr->name);
852                 ptr++;
853                 counter++;
854         }
855         return("unknown");
856 }
857
858 /*
859  * Try figuring out the root device's canonical disk name.
860  * The following choices are considered:
861  *   /dev/ad0s1a     => /dev/ad0
862  *   /dev/da0a       => /dev/da0
863  *   /dev/vinum/root => /dev/vinum/root
864  */
865 static char *
866 get_rootdisk(void)
867 {
868         struct statfs rootfs;
869         regex_t re;
870 #define NMATCHES 2
871         regmatch_t rm[NMATCHES];
872         char *s;
873         int rv;
874
875         if (statfs("/", &rootfs) == -1)
876                 err(1, "statfs(\"/\")");
877
878         if ((rv = regcomp(&re, "^(/dev/[a-z]+[0-9]+)([sp][0-9]+)?[a-h]?$",
879                     REG_EXTENDED)) != 0)
880                 errx(1, "regcomp() failed (%d)", rv);
881         if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
882                 errx(1,
883 "mounted root fs resource doesn't match expectations (regexec returned %d)",
884                     rv);
885         if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
886                 errx(1, "out of memory");
887         memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
888             rm[1].rm_eo - rm[1].rm_so);
889         s[rm[1].rm_eo - rm[1].rm_so] = 0;
890
891         return s;
892 }
893
894 static void
895 reset_boot(void)
896 {
897         int i;
898         struct pc98_partition *partp;
899
900         init_boot();
901         for (i = 1; i <= NDOSPART; i++) {
902                 partp = ((struct pc98_partition *) &mboot.parts) + i - 1;
903                 bzero((char *)partp, sizeof (struct pc98_partition));
904         }
905 }