Clean (void) casts from sbin
[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.4 2004/12/18 21:43:39 swildner 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(void)
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(void)
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(void)
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(void)
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 #ifdef PC98
789 dos(int sec, int size, unsigned short *c, unsigned char *s, unsigned char *h)
790 #else
791 dos(int sec, int size, unsigned char *c, unsigned char *s, unsigned char *h)
792 #endif
793 {
794 int cy;
795 int hd;
796
797         if (sec == 0 && size == 0) {
798                 *s = *c = *h = 0;
799                 return;
800         }
801
802         cy = sec / ( dos_cylsecs );
803         sec = sec - cy * ( dos_cylsecs );
804
805         hd = sec / dos_sectors;
806 #ifdef PC98
807         sec = (sec - hd * dos_sectors);
808
809         *h = hd;
810         *c = cy;
811         *s = sec;
812 #else
813         sec = (sec - hd * dos_sectors) + 1;
814
815         *h = hd;
816         *c = cy & 0xff;
817         *s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
818 #endif
819 }
820
821 int fd;
822
823         /* Getting device status */
824
825 static int
826 open_disk(int u_flag)
827 {
828 struct stat     st;
829
830         if (stat(disk, &st) == -1) {
831                 warnx("can't get file status of %s", disk);
832                 return -1;
833         }
834         if ( !(st.st_mode & S_IFCHR) )
835                 warnx("device %s is not character special", disk);
836         if ((fd = open(disk,
837             a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
838                 if(errno == ENXIO)
839                         return -2;
840                 warnx("can't open device %s", disk);
841                 return -1;
842         }
843         if (get_params(0) == -1) {
844                 warnx("can't get disk parameters on %s", disk);
845                 return -1;
846         }
847         return fd;
848 }
849
850 static ssize_t
851 read_disk(off_t sector, void *buf)
852 {
853         lseek(fd,(sector * 512), 0);
854 #ifdef PC98
855         return read(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
856 #else
857         if( secsize == 0 )
858                 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
859                         {
860                         /* try the read */
861                         int size = read(fd, buf, secsize);
862                         if( size == secsize )
863                                 /* it worked so return */
864                                 return secsize;
865                         }
866         else
867                 return read( fd, buf, secsize );
868
869         /* we failed to read at any of the sizes */
870         return -1;
871 #endif
872 }
873
874 static ssize_t
875 write_disk(off_t sector, void *buf)
876 {
877         lseek(fd,(sector * 512), 0);
878         /* write out in the size that the read_disk found worked */
879 #ifdef PC98
880         return write(fd, buf, secsize > MIN_SEC_SIZE ? secsize : MIN_SEC_SIZE * 2);
881 #else
882         return write(fd, buf, secsize);
883 #endif
884 }
885
886 static int
887 get_params(void)
888 {
889
890     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
891         warnx("can't get disk parameters on %s; supplying dummy ones", disk);
892         dos_cyls = cyls = 1;
893         dos_heads = heads = 1;
894         dos_sectors = sectors = 1;
895         dos_cylsecs = cylsecs = heads * sectors;
896         disksecs = cyls * heads * sectors;
897 #ifdef PC98
898         secsize = disklabel.d_secsize;
899 #endif
900         return disksecs;
901     }
902
903     dos_cyls = cyls = disklabel.d_ncylinders;
904     dos_heads = heads = disklabel.d_ntracks;
905     dos_sectors = sectors = disklabel.d_nsectors;
906     dos_cylsecs = cylsecs = heads * sectors;
907     disksecs = cyls * heads * sectors;
908 #ifdef PC98
909     secsize = disklabel.d_secsize;
910 #endif
911     return (disksecs);
912 }
913 \f
914
915 static int
916 read_s0(void)
917 {
918         if (read_disk(0, (char *) mboot.bootinst) == -1) {
919                 warnx("can't read fdisk partition table");
920                 return -1;
921         }
922         if (mboot.signature != BOOT_MAGIC) {
923                 warnx("invalid fdisk partition table found");
924                 /* So should we initialize things */
925                 return -1;
926         }
927         return 0;
928 }
929
930 static int
931 write_s0(void)
932 {
933 #ifdef NOT_NOW
934         int     flag;
935 #endif
936         if (iotest) {
937                 print_s0(-1);
938                 return 0;
939         }
940         /*
941          * write enable label sector before write (if necessary),
942          * disable after writing.
943          * needed if the disklabel protected area also protects
944          * sector 0. (e.g. empty disk)
945          */
946 #ifdef NOT_NOW
947         flag = 1;
948         if (ioctl(fd, DIOCWLABEL, &flag) < 0)
949                 warn("ioctl DIOCWLABEL");
950 #endif
951         if (write_disk(0, (char *) mboot.bootinst) == -1) {
952                 warn("can't write fdisk partition table");
953                 return -1;
954 #ifdef NOT_NOW
955         flag = 0;
956         ioctl(fd, DIOCWLABEL, &flag);
957 #endif
958         }
959         return(0);
960 }
961
962
963 static int
964 ok(char *str)
965 {
966         printf("%s [n] ", str);
967         fgets(lbuf, LBUF, stdin);
968         lbuf[strlen(lbuf)-1] = 0;
969
970         if (*lbuf &&
971                 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
972                  !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
973                 return 1;
974         else
975                 return 0;
976 }
977
978 static int
979 decimal(char *str, int *num, int deflt)
980 {
981 int acc = 0, c;
982 char *cp;
983
984         while (1) {
985                 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
986                 fgets(lbuf, LBUF, stdin);
987                 lbuf[strlen(lbuf)-1] = 0;
988
989                 if (!*lbuf)
990                         return 0;
991
992                 cp = lbuf;
993                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
994                 if (!c)
995                         return 0;
996                 while ((c = *cp++)) {
997                         if (c <= '9' && c >= '0')
998                                 acc = acc * 10 + c - '0';
999                         else
1000                                 break;
1001                 }
1002                 if (c == ' ' || c == '\t')
1003                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1004                 if (!c) {
1005                         *num = acc;
1006                         return 1;
1007                 } else
1008                         printf("%s is an invalid decimal number.  Try again.\n",
1009                                 lbuf);
1010         }
1011
1012 }
1013
1014 #if 0
1015 static int
1016 hex(char *str, int *num, int deflt)
1017 {
1018 int acc = 0, c;
1019 char *cp;
1020
1021         while (1) {
1022                 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
1023                 fgets(lbuf, LBUF, stdin);
1024                 lbuf[strlen(lbuf)-1] = 0;
1025
1026                 if (!*lbuf)
1027                         return 0;
1028
1029                 cp = lbuf;
1030                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1031                 if (!c)
1032                         return 0;
1033                 while ((c = *cp++)) {
1034                         if (c <= '9' && c >= '0')
1035                                 acc = (acc << 4) + c - '0';
1036                         else if (c <= 'f' && c >= 'a')
1037                                 acc = (acc << 4) + c - 'a' + 10;
1038                         else if (c <= 'F' && c >= 'A')
1039                                 acc = (acc << 4) + c - 'A' + 10;
1040                         else
1041                                 break;
1042                 }
1043                 if (c == ' ' || c == '\t')
1044                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1045                 if (!c) {
1046                         *num = acc;
1047                         return 1;
1048                 } else
1049                         printf("%s is an invalid hex number.  Try again.\n",
1050                                 lbuf);
1051         }
1052
1053 }
1054 #endif
1055
1056 #ifdef PC98
1057 static int
1058 string(char *str, char **ans)
1059 {
1060 #ifdef PC98
1061 int i;
1062 #endif
1063 int c;
1064 char *cp = lbuf;
1065
1066         while (1) {
1067                 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1068                 fgets(lbuf, LBUF, stdin);
1069                 lbuf[strlen(lbuf)-1] = 0;
1070
1071                 if (!*lbuf)
1072                         return 0;
1073
1074                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1075                 if (c == '"') {
1076                         c = *++cp;
1077                         *ans = cp;
1078                         while ((c = *cp) && c != '"') cp++;
1079                 } else {
1080                         *ans = cp;
1081                         while ((c = *cp) && c != ' ' && c != '\t') cp++;
1082                 }
1083
1084 #ifdef PC98
1085                 for (i = strlen(*ans); i < 16; i++)
1086                         (*ans)[i] = ' ';
1087                 (*ans)[16] = 0;
1088 #else
1089                 if (c)
1090                         *cp = 0;
1091 #endif
1092                 return 1;
1093         }
1094 }
1095 #endif
1096
1097 static char *
1098 get_type(int type)
1099 {
1100         int     numentries = (sizeof(part_types)/sizeof(struct part_type));
1101         int     counter = 0;
1102         struct  part_type *ptr = part_types;
1103
1104
1105         while(counter < numentries)
1106         {
1107 #ifdef PC98
1108                 if(ptr->type == (type & 0x7f))
1109 #else
1110                 if(ptr->type == type)
1111 #endif
1112                 {
1113                         return(ptr->name);
1114                 }
1115                 ptr++;
1116                 counter++;
1117         }
1118         return("unknown");
1119 }
1120
1121 #ifndef PC98
1122 static void
1123 parse_config_line(char *line, CMD *command)
1124 {
1125     char        *cp, *end;
1126
1127     cp = line;
1128     while (1)   /* dirty trick used to insure one exit point for this
1129                    function */
1130     {
1131         memset(command, 0, sizeof(*command));
1132
1133         while (isspace(*cp)) ++cp;
1134         if (*cp == '\0' || *cp == '#')
1135         {
1136             break;
1137         }
1138         command->cmd = *cp++;
1139
1140         /*
1141          * Parse args
1142          */
1143         while (1)
1144         {
1145             while (isspace(*cp)) ++cp;
1146             if (*cp == '#')
1147             {
1148                 break;          /* found comment */
1149             }
1150             if (isalpha(*cp))
1151             {
1152                 command->args[command->n_args].argtype = *cp++;
1153             }
1154             if (!isdigit(*cp))
1155             {
1156                 break;          /* assume end of line */
1157             }
1158             end = NULL;
1159             command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1160             if (cp == end)
1161             {
1162                 break;          /* couldn't parse number */
1163             }
1164             cp = end;
1165             command->n_args++;
1166         }
1167         break;
1168     }
1169 }
1170
1171
1172 static int
1173 process_geometry(CMD *command)
1174 {
1175     int         status = 1, i;
1176
1177     while (1)
1178     {
1179         geom_processed = 1;
1180         if (part_processed)
1181         {
1182             warnx(
1183         "ERROR line %d: the geometry specification line must occur before\n\
1184     all partition specifications",
1185                     current_line_number);
1186             status = 0;
1187             break;
1188         }
1189         if (command->n_args != 3)
1190         {
1191             warnx("ERROR line %d: incorrect number of geometry args",
1192                     current_line_number);
1193             status = 0;
1194             break;
1195         }
1196         dos_cyls = -1;
1197         dos_heads = -1;
1198         dos_sectors = -1;
1199         for (i = 0; i < 3; ++i)
1200         {
1201             switch (command->args[i].argtype)
1202             {
1203             case 'c':
1204                 dos_cyls = command->args[i].arg_val;
1205                 break;
1206             case 'h':
1207                 dos_heads = command->args[i].arg_val;
1208                 break;
1209             case 's':
1210                 dos_sectors = command->args[i].arg_val;
1211                 break;
1212             default:
1213                 warnx(
1214                 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1215                         current_line_number, command->args[i].argtype,
1216                         command->args[i].argtype);
1217                 status = 0;
1218                 break;
1219             }
1220         }
1221         if (status == 0)
1222         {
1223             break;
1224         }
1225
1226         dos_cylsecs = dos_heads * dos_sectors;
1227
1228         /*
1229          * Do sanity checks on parameter values
1230          */
1231         if (dos_cyls < 0)
1232         {
1233             warnx("ERROR line %d: number of cylinders not specified",
1234                     current_line_number);
1235             status = 0;
1236         }
1237         if (dos_cyls == 0 || dos_cyls > 1024)
1238         {
1239             warnx(
1240         "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1241     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1242     is dedicated to FreeBSD)",
1243                     current_line_number, dos_cyls);
1244         }
1245
1246         if (dos_heads < 0)
1247         {
1248             warnx("ERROR line %d: number of heads not specified",
1249                     current_line_number);
1250             status = 0;
1251         }
1252         else if (dos_heads < 1 || dos_heads > 256)
1253         {
1254             warnx("ERROR line %d: number of heads must be within (1-256)",
1255                     current_line_number);
1256             status = 0;
1257         }
1258
1259         if (dos_sectors < 0)
1260         {
1261             warnx("ERROR line %d: number of sectors not specified",
1262                     current_line_number);
1263             status = 0;
1264         }
1265         else if (dos_sectors < 1 || dos_sectors > 63)
1266         {
1267             warnx("ERROR line %d: number of sectors must be within (1-63)",
1268                     current_line_number);
1269             status = 0;
1270         }
1271
1272         break;
1273     }
1274     return (status);
1275 }
1276
1277
1278 static int
1279 process_partition(CMD *command)
1280 {
1281     int                         status = 0, partition;
1282     unsigned long               chunks, adj_size, max_end;
1283     struct dos_partition        *partp;
1284
1285     while (1)
1286     {
1287         part_processed = 1;
1288         if (command->n_args != 4)
1289         {
1290             warnx("ERROR line %d: incorrect number of partition args",
1291                     current_line_number);
1292             break;
1293         }
1294         partition = command->args[0].arg_val;
1295         if (partition < 1 || partition > 4)
1296         {
1297             warnx("ERROR line %d: invalid partition number %d",
1298                     current_line_number, partition);
1299             break;
1300         }
1301         partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1302         bzero((char *)partp, sizeof (struct dos_partition));
1303         partp->dp_typ = command->args[1].arg_val;
1304         partp->dp_start = command->args[2].arg_val;
1305         partp->dp_size = command->args[3].arg_val;
1306         max_end = partp->dp_start + partp->dp_size;
1307
1308         if (partp->dp_typ == 0)
1309         {
1310             /*
1311              * Get out, the partition is marked as unused.
1312              */
1313             /*
1314              * Insure that it's unused.
1315              */
1316             bzero((char *)partp, sizeof (struct dos_partition));
1317             status = 1;
1318             break;
1319         }
1320
1321         /*
1322          * Adjust start upwards, if necessary, to fall on an head boundary.
1323          */
1324         if (partp->dp_start % dos_sectors != 0)
1325         {
1326             adj_size =
1327                 (partp->dp_start / dos_sectors + 1) * dos_sectors;
1328             if (adj_size > max_end)
1329             {
1330                 /*
1331                  * Can't go past end of partition
1332                  */
1333                 warnx(
1334         "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1335     a cylinder boundary",
1336                         current_line_number, partition);
1337                 break;
1338             }
1339             warnx(
1340         "WARNING: adjusting start offset of partition '%d' from %lu\n\
1341     to %lu, to round to an head boundary",
1342                     partition, (u_long)partp->dp_start, adj_size);
1343             partp->dp_start = adj_size;
1344         }
1345
1346         /*
1347          * Adjust size downwards, if necessary, to fall on a cylinder
1348          * boundary.
1349          */
1350         chunks =
1351             ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1352         adj_size = chunks - partp->dp_start;
1353         if (adj_size != partp->dp_size)
1354         {
1355             warnx(
1356         "WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1357     to round to a cylinder boundary",
1358                     partition, (u_long)partp->dp_size, adj_size);
1359             if (chunks > 0)
1360             {
1361                 partp->dp_size = adj_size;
1362             }
1363             else
1364             {
1365                 partp->dp_size = 0;
1366             }
1367         }
1368         if (partp->dp_size < 1)
1369         {
1370             warnx("ERROR line %d: size for partition '%d' is zero",
1371                     current_line_number, partition);
1372             break;
1373         }
1374
1375         dos(partp->dp_start, partp->dp_size,
1376             &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1377         dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1378             &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1379         status = 1;
1380         break;
1381     }
1382     return (status);
1383 }
1384
1385
1386 static int
1387 process_active(CMD *command)
1388 {
1389     int                         status = 0, partition, i;
1390     struct dos_partition        *partp;
1391
1392     while (1)
1393     {
1394         active_processed = 1;
1395         if (command->n_args != 1)
1396         {
1397             warnx("ERROR line %d: incorrect number of active args",
1398                     current_line_number);
1399             status = 0;
1400             break;
1401         }
1402         partition = command->args[0].arg_val;
1403         if (partition < 1 || partition > 4)
1404         {
1405             warnx("ERROR line %d: invalid partition number %d",
1406                     current_line_number, partition);
1407             break;
1408         }
1409         /*
1410          * Reset active partition
1411          */
1412         partp = ((struct dos_partition *) &mboot.parts);
1413         for (i = 0; i < NDOSPART; i++)
1414             partp[i].dp_flag = 0;
1415         partp[partition-1].dp_flag = ACTIVE;
1416
1417         status = 1;
1418         break;
1419     }
1420     return (status);
1421 }
1422
1423
1424 static int
1425 process_line(char *line)
1426 {
1427     CMD         command;
1428     int         status = 1;
1429
1430     while (1)
1431     {
1432         parse_config_line(line, &command);
1433         switch (command.cmd)
1434         {
1435         case 0:
1436             /*
1437              * Comment or blank line
1438              */
1439             break;
1440         case 'g':
1441             /*
1442              * Set geometry
1443              */
1444             status = process_geometry(&command);
1445             break;
1446         case 'p':
1447             status = process_partition(&command);
1448             break;
1449         case 'a':
1450             status = process_active(&command);
1451             break;
1452         default:
1453             status = 0;
1454             break;
1455         }
1456         break;
1457     }
1458     return (status);
1459 }
1460
1461
1462 static int
1463 read_config(char *config_file)
1464 {
1465     FILE        *fp = NULL;
1466     int         status = 1;
1467     char        buf[1010];
1468
1469     while (1)   /* dirty trick used to insure one exit point for this
1470                    function */
1471     {
1472         if (strcmp(config_file, "-") != 0)
1473         {
1474             /*
1475              * We're not reading from stdin
1476              */
1477             if ((fp = fopen(config_file, "r")) == NULL)
1478             {
1479                 status = 0;
1480                 break;
1481             }
1482         }
1483         else
1484         {
1485             fp = stdin;
1486         }
1487         current_line_number = 0;
1488         while (!feof(fp))
1489         {
1490             if (fgets(buf, sizeof(buf), fp) == NULL)
1491             {
1492                 break;
1493             }
1494             ++current_line_number;
1495             status = process_line(buf);
1496             if (status == 0)
1497             {
1498                 break;
1499             }
1500         }
1501         break;
1502     }
1503     if (fp)
1504     {
1505         /*
1506          * It doesn't matter if we're reading from stdin, as we've reached EOF
1507          */
1508         fclose(fp);
1509     }
1510     return (status);
1511 }
1512
1513
1514 static void
1515 reset_boot(void)
1516 {
1517     int                         i;
1518     struct dos_partition        *partp;
1519
1520     init_boot();
1521     for (i = 0; i < 4; ++i)
1522     {
1523         partp = ((struct dos_partition *) &mboot.parts) + i;
1524         bzero((char *)partp, sizeof (struct dos_partition));
1525     }
1526 }
1527 #endif