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