INSTALLER - Fix a bug with varargs
[dragonfly.git] / sbin / i386 / fdisk / fdisk.c
blob01a7347fbddf83bd1611268bd5fed1c18aa4c8c2
1 /*
2 * Mach Operating System
3 * Copyright (c) 1992 Carnegie Mellon University
4 * All Rights Reserved.
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
26 * $FreeBSD: /repoman/r/ncvs/src/sbin/i386/fdisk/fdisk.c,v 1.36.2.14 2004/01/30 14:40:47 harti Exp $
27 * $DragonFly: src/sbin/i386/fdisk/fdisk.c,v 1.16 2008/04/23 22:09:07 thomas Exp $
30 #include <sys/types.h>
31 #include <sys/diskslice.h>
32 #include <sys/diskmbr.h>
33 #include <sys/stat.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <err.h>
37 #include <fstab.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 int iotest;
46 #define LBUF 100
47 static char lbuf[LBUF];
49 #define MBRSIGOFF 510
53 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
55 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
56 * Copyright (c) 1989 Robert. V. Baron
57 * Created.
60 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
61 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
62 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
64 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
66 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
67 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
68 int secsize = 0; /* the sensed sector size */
70 const char *disk;
71 const char *disks[] =
73 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
76 int cyls, sectors, heads, cylsecs;
77 int64_t disksecs;
79 struct mboot
81 unsigned char padding[2]; /* force the longs to be long aligned */
82 unsigned char *bootinst; /* boot code */
83 off_t bootinst_size;
84 struct dos_partition parts[4];
86 struct mboot mboot = {{0}, NULL, 0};
88 #define ACTIVE 0x80
89 #define BOOT_MAGIC 0xAA55
91 int dos_cyls;
92 int dos_heads;
93 int dos_sectors;
94 int dos_cylsecs;
96 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
97 #define DOSCYL(c) (c & 0xff)
98 #define MAXCYL 1023
99 static int partition = -1;
102 #define MAX_ARGS 10
104 static int current_line_number;
106 static int geom_processed = 0;
107 static int part_processed = 0;
108 static int active_processed = 0;
111 typedef struct cmd {
112 char cmd;
113 int n_args;
114 struct arg {
115 char argtype;
116 int arg_val;
117 } args[MAX_ARGS];
118 } CMD;
121 static int B_flag = 0; /* replace boot code */
122 static int C_flag = 0; /* use wrapped values for CHS */
123 static int I_flag = 0; /* use entire disk for DragonFly */
124 static int a_flag = 0; /* set active partition */
125 static char *b_flag = NULL; /* path to boot code */
126 static int i_flag = 0; /* replace partition data */
127 static int u_flag = 0; /* update partition data */
128 static int p_flag = 0; /* operate on a disk image file */
129 static int s_flag = 0; /* Print a summary and exit */
130 static int t_flag = 0; /* test only */
131 static char *f_flag = NULL; /* Read config info from file */
132 static int v_flag = 0; /* Be verbose */
134 struct part_type
136 unsigned char type;
137 char *name;
138 }part_types[] =
140 {0x00, "unused"}
141 ,{0x01, "Primary DOS with 12 bit FAT"}
142 ,{0x02, "XENIX / filesystem"}
143 ,{0x03, "XENIX /usr filesystem"}
144 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
145 ,{0x05, "Extended DOS"}
146 ,{0x06, "Primary 'big' DOS (> 32MB)"}
147 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
148 ,{0x08, "AIX filesystem"}
149 ,{0x09, "AIX boot partition or Coherent"}
150 ,{0x0A, "OS/2 Boot Manager or OPUS"}
151 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
152 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
153 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
154 ,{0x0F, "Extended DOS, LBA"}
155 ,{0x10, "OPUS"}
156 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
157 ,{0x12, "Compaq diagnostics"}
158 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
159 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
160 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
161 ,{0x18, "AST Windows swapfile"}
162 ,{0x24, "NEC DOS"}
163 ,{0x39, "plan9"}
164 ,{0x3C, "PartitionMagic recovery"}
165 ,{0x40, "VENIX 286"}
166 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
167 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
168 ,{0x43, "Linux native (sharing disk with DRDOS)"}
169 ,{0x4D, "QNX 4.2 Primary"}
170 ,{0x4E, "QNX 4.2 Secondary"}
171 ,{0x4F, "QNX 4.2 Tertiary"}
172 ,{0x50, "DM"}
173 ,{0x51, "DM"}
174 ,{0x52, "CP/M or Microport SysV/AT"}
175 ,{0x53, "DM6 Aux3"}
176 ,{0x54, "DM6"}
177 ,{0x55, "EZ-Drive (disk manager)"}
178 ,{0x56, "GB"}
179 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
180 ,{0x61, "Speed"}
181 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
182 ,{0x64, "Novell Netware 2.xx"}
183 ,{0x65, "Novell Netware 3.xx"}
184 ,{0x70, "DiskSecure Multi-Boot"}
185 ,{0x75, "PCIX"}
186 ,{0x77, "QNX4.x"}
187 ,{0x78, "QNX4.x 2nd part"}
188 ,{0x79, "QNX4.x 3rd part"}
189 ,{0x80, "Minix 1.1 ... 1.4a"}
190 ,{0x81, "Minix 1.4b ... 1.5.10"}
191 ,{0x82, "Linux swap or Solaris x86"}
192 ,{0x83, "Linux filesystem"}
193 ,{0x84, "OS/2 hidden C: drive"}
194 ,{0x85, "Linux extended"}
195 ,{0x86, "NTFS volume set??"}
196 ,{0x87, "NTFS volume set??"}
197 ,{0x93, "Amoeba filesystem"}
198 ,{0x94, "Amoeba bad block table"}
199 ,{0x9F, "BSD/OS"}
200 ,{0xA0, "Suspend to Disk"}
201 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
202 ,{0xA6, "OpenBSD"}
203 ,{0xA7, "NEXTSTEP"}
204 ,{0xA9, "NetBSD"}
205 ,{0xAC, "IBM JFS"}
206 ,{0xB7, "BSDI BSD/386 filesystem"}
207 ,{0xB8, "BSDI BSD/386 swap"}
208 ,{0xBE, "Solaris x86 boot"}
209 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
210 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
211 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
212 ,{0xC7, "Syrinx"}
213 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
214 ,{0xE1, "Speed"}
215 ,{0xE3, "Speed"}
216 ,{0xE4, "Speed"}
217 ,{0xEB, "BeOS file system"}
218 ,{0xEE, "EFI GPT"}
219 ,{0xEF, "EFI System Partition"}
220 ,{0xF1, "Speed"}
221 ,{0xF2, "DOS 3.3+ Secondary"}
222 ,{0xF4, "Speed"}
223 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
224 ,{0xFF, "BBT (Bad Blocks Table)"}
227 static void print_s0(int which);
228 static void print_part(int i);
229 static void init_sector0(unsigned long start);
230 static void init_boot(void);
231 static void change_part(int i);
232 static void print_params();
233 static void change_active(int which);
234 static void change_code();
235 static void get_params_to_use();
236 static void dos(struct dos_partition *partp);
237 static int open_disk(int u_flag);
238 static ssize_t read_disk(off_t sector, void *buf);
239 static ssize_t write_disk(off_t sector, void *buf);
240 static int get_params();
241 static int read_s0();
242 static int write_s0();
243 static int ok(char *str);
244 static int decimal(char *str, int *num, int deflt);
245 static char *get_type(int type);
246 static int read_config(char *config_file);
247 static void reset_boot(void);
248 static int sanitize_partition(struct dos_partition *);
249 static void usage(void);
250 #if 0
251 static int hex(char *str, int *num, int deflt);
252 static int string(char *str, char **ans);
253 #endif
257 main(int argc, char *argv[])
259 int c, i;
261 while ((c = getopt(argc, argv, "BCIab:f:p:istuv1234")) != -1)
262 switch (c) {
263 case 'B':
264 B_flag = 1;
265 break;
266 case 'C':
267 C_flag = 1;
268 break;
269 case 'I':
270 I_flag = 1;
271 break;
272 case 'a':
273 a_flag = 1;
274 break;
275 case 'b':
276 b_flag = optarg;
277 break;
278 case 'f':
279 f_flag = optarg;
280 break;
281 case 'p':
282 disk = optarg;
283 p_flag = 1;
284 break;
285 case 'i':
286 i_flag = 1;
287 break;
288 case 's':
289 s_flag = 1;
290 break;
291 case 't':
292 t_flag = 1;
293 break;
294 case 'u':
295 u_flag = 1;
296 break;
297 case 'v':
298 v_flag = 1;
299 break;
300 case '1':
301 case '2':
302 case '3':
303 case '4':
304 partition = c - '0';
305 break;
306 default:
307 usage();
309 if (f_flag || i_flag)
310 u_flag = 1;
311 if (t_flag)
312 v_flag = 1;
313 argc -= optind;
314 argv += optind;
316 if (argc > 0) {
317 disk = getdevpath(argv[0], 0);
318 if (open_disk(u_flag) < 0)
319 err(1, "cannot open disk %s", disk);
320 } else if (disk == NULL) {
321 int rv = 0;
323 for(i = 0; disks[i]; i++)
325 disk = disks[i];
326 rv = open_disk(u_flag);
327 if(rv != -2) break;
329 if(rv < 0)
330 err(1, "cannot open any disk");
331 } else {
332 if (open_disk(u_flag) < 0)
333 err(1, "cannot open disk %s", disk);
336 /* (abu)use mboot.bootinst to probe for the sector size */
337 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
338 err(1, "cannot allocate buffer to determine disk sector size");
339 read_disk(0, mboot.bootinst);
340 free(mboot.bootinst);
341 mboot.bootinst = NULL;
343 if (s_flag)
345 int i;
346 struct dos_partition *partp;
348 if (read_s0())
349 err(1, "read_s0");
350 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
351 dos_sectors);
352 printf("Part %11s %11s Type Flags\n", "Start", "Size");
353 for (i = 0; i < NDOSPART; i++) {
354 partp = ((struct dos_partition *) &mboot.parts) + i;
355 if (partp->dp_start == 0 && partp->dp_size == 0)
356 continue;
357 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
358 (u_long) partp->dp_start,
359 (u_long) partp->dp_size, partp->dp_typ,
360 partp->dp_flag);
362 exit(0);
365 printf("******* Working on device %s *******\n",disk);
367 if (I_flag)
369 struct dos_partition *partp;
371 read_s0();
372 reset_boot();
373 partp = (struct dos_partition *) (&mboot.parts[0]);
374 partp->dp_typ = DOSPTYP_386BSD;
375 partp->dp_flag = ACTIVE;
376 partp->dp_start = dos_sectors;
377 if (disksecs - dos_sectors > 0xFFFFFFFFU) {
378 printf("Warning: Ending logical block > 2TB, using max value\n");
379 partp->dp_size = 0xFFFFFFFFU;
380 } else {
381 partp->dp_size = (disksecs / dos_cylsecs) *
382 dos_cylsecs - dos_sectors;
384 dos(partp);
385 if (v_flag)
386 print_s0(-1);
387 if (!t_flag)
388 write_s0();
389 exit(0);
391 if (f_flag)
393 if (read_s0() || i_flag)
395 reset_boot();
398 if (!read_config(f_flag))
400 exit(1);
402 if (v_flag)
404 print_s0(-1);
406 if (!t_flag)
408 write_s0();
411 else
413 if(u_flag)
415 get_params_to_use();
417 else
419 print_params();
422 if (read_s0())
423 init_sector0(dos_sectors);
425 printf("Media sector size is %d\n", secsize);
426 printf("Warning: BIOS sector numbering starts with sector 1\n");
427 printf("Information from DOS bootblock is:\n");
428 if (partition == -1)
429 for (i = 1; i <= NDOSPART; i++)
430 change_part(i);
431 else
432 change_part(partition);
434 if (u_flag || a_flag)
435 change_active(partition);
437 if (B_flag)
438 change_code();
440 if (u_flag || a_flag || B_flag) {
441 if (!t_flag) {
442 printf("\nWe haven't changed the partition table yet. ");
443 printf("This is your last chance.\n");
445 print_s0(-1);
446 if (!t_flag) {
447 if (ok("Should we write new partition table?"))
448 write_s0();
450 else
452 printf("\n-t flag specified -- partition table not written.\n");
457 exit(0);
460 static void
461 usage(void)
463 fprintf(stderr, "%s%s",
464 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
465 " fdisk -f configfile [-itv] [disk]\n");
466 exit(1);
469 static void
470 print_s0(int which)
472 int i;
474 print_params();
475 printf("Information from DOS bootblock is:\n");
476 if (which == -1)
477 for (i = 1; i <= NDOSPART; i++)
478 printf("%d: ", i), print_part(i);
479 else
480 print_part(which);
483 static struct dos_partition mtpart = { 0 };
485 static void
486 print_part(int i)
488 struct dos_partition *partp;
489 u_int64_t part_mb;
491 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
493 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
494 printf("<UNUSED>\n");
495 return;
498 * Be careful not to overflow.
500 part_mb = partp->dp_size;
501 part_mb *= secsize;
502 part_mb /= (1024 * 1024);
503 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
504 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
505 (u_long)partp->dp_start,
506 (u_long)partp->dp_size,
507 part_mb,
508 partp->dp_flag,
509 partp->dp_flag == ACTIVE ? " (active)" : "");
510 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
511 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
512 ,partp->dp_shd
513 ,DPSECT(partp->dp_ssect)
514 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
515 ,partp->dp_ehd
516 ,DPSECT(partp->dp_esect));
520 static void
521 init_boot(void)
523 const char *fname;
524 int fd, n;
525 struct stat sb;
527 fname = b_flag ? b_flag : "/boot/mbr";
528 if ((fd = open(fname, O_RDONLY)) == -1 ||
529 fstat(fd, &sb) == -1)
530 err(1, "%s", fname);
531 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
532 errx(1, "%s: length must be a multiple of sector size", fname);
533 if (mboot.bootinst != NULL)
534 free(mboot.bootinst);
535 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
536 errx(1, "%s: unable to allocate read buffer", fname);
537 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
538 close(fd))
539 err(1, "%s", fname);
540 if (n != mboot.bootinst_size)
541 errx(1, "%s: short read", fname);
545 static void
546 init_sector0(unsigned long start)
548 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
550 init_boot();
552 partp->dp_typ = DOSPTYP_386BSD;
553 partp->dp_flag = ACTIVE;
554 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
555 if(start == 0)
556 start = dos_sectors;
557 partp->dp_start = start;
558 if (disksecs - start > 0xFFFFFFFFU) {
559 printf("Warning: Ending logical block > 2TB, using max value\n");
560 partp->dp_size = 0xFFFFFFFFU;
561 } else {
562 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
565 dos(partp);
568 static void
569 change_part(int i)
571 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
573 printf("The data for partition %d is:\n", i);
574 print_part(i);
576 if (u_flag && ok("Do you want to change it?")) {
577 int tmp;
579 if (i_flag) {
580 bzero((char *)partp, sizeof (struct dos_partition));
581 if (i == 4) {
582 init_sector0(1);
583 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
584 print_part(i);
588 do {
589 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
590 Decimal("start", partp->dp_start, tmp);
591 Decimal("size", partp->dp_size, tmp);
592 if (!sanitize_partition(partp)) {
593 warnx("ERROR: failed to adjust; setting sysid to 0");
594 partp->dp_typ = 0;
597 if (ok("Explicitly specify beg/end address ?"))
599 int tsec,tcyl,thd;
600 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
601 thd = partp->dp_shd;
602 tsec = DPSECT(partp->dp_ssect);
603 Decimal("beginning cylinder", tcyl, tmp);
604 Decimal("beginning head", thd, tmp);
605 Decimal("beginning sector", tsec, tmp);
606 if (tcyl > MAXCYL && C_flag == 0) {
607 printf("Warning: starting cylinder wraps, using all 1's\n");
608 partp->dp_scyl = -1;
609 partp->dp_ssect = -1;
610 partp->dp_shd = -1;
611 } else {
612 partp->dp_scyl = DOSCYL(tcyl);
613 partp->dp_ssect = DOSSECT(tsec,tcyl);
614 partp->dp_shd = thd;
617 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
618 thd = partp->dp_ehd;
619 tsec = DPSECT(partp->dp_esect);
620 Decimal("ending cylinder", tcyl, tmp);
621 Decimal("ending head", thd, tmp);
622 Decimal("ending sector", tsec, tmp);
623 if (tcyl > MAXCYL && C_flag == 0) {
624 printf("Warning: ending cylinder wraps, using all 1's\n");
625 partp->dp_ecyl = -1;
626 partp->dp_esect = -1;
627 partp->dp_ehd = -1;
628 } else {
629 partp->dp_ecyl = DOSCYL(tcyl);
630 partp->dp_esect = DOSSECT(tsec,tcyl);
631 partp->dp_ehd = thd;
633 } else
634 dos(partp);
636 print_part(i);
637 } while (!ok("Are we happy with this entry?"));
641 static void
642 print_params(void)
644 printf("parameters extracted from device are:\n");
645 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
646 ,cyls,heads,sectors,cylsecs);
647 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
648 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
649 printf("parameters to be used for BIOS calculations are:\n");
650 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
651 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
654 static void
655 change_active(int which)
657 struct dos_partition *partp = &mboot.parts[0];
658 int active, i, new, tmp;
660 active = -1;
661 for (i = 0; i < NDOSPART; i++) {
662 if ((partp[i].dp_flag & ACTIVE) == 0)
663 continue;
664 printf("Partition %d is marked active\n", i + 1);
665 if (active == -1)
666 active = i + 1;
668 if (a_flag && which != -1)
669 active = which;
670 else if (active == -1)
671 active = 1;
673 if (!ok("Do you want to change the active partition?"))
674 return;
675 setactive:
676 do {
677 new = active;
678 Decimal("active partition", new, tmp);
679 if (new < 1 || new > 4) {
680 printf("Active partition number must be in range 1-4."
681 " Try again.\n");
682 goto setactive;
684 active = new;
685 } while (!ok("Are you happy with this choice"));
686 for (i = 0; i < NDOSPART; i++)
687 partp[i].dp_flag = 0;
688 if (active > 0 && active <= NDOSPART)
689 partp[active-1].dp_flag = ACTIVE;
692 static void
693 change_code(void)
695 if (ok("Do you want to change the boot code?"))
696 init_boot();
699 void
700 get_params_to_use(void)
702 int tmp;
703 print_params();
704 if (ok("Do you want to change our idea of what BIOS thinks ?"))
708 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
709 Decimal("BIOS's idea of #heads", dos_heads, tmp);
710 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
711 dos_cylsecs = dos_heads * dos_sectors;
712 print_params();
714 while(!ok("Are you happy with this choice"));
719 /***********************************************\
720 * Change real numbers into strange dos numbers *
721 \***********************************************/
722 static void
723 dos(struct dos_partition *partp)
725 int cy, sec;
726 u_int32_t end;
728 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
729 memcpy(partp, &mtpart, sizeof(*partp));
730 return;
733 /* Start c/h/s. */
734 cy = partp->dp_start / dos_cylsecs;
735 sec = partp->dp_start % dos_sectors + 1;
736 if (cy > MAXCYL && C_flag == 0) {
737 printf("Warning: starting cylinder wraps, using all 1's\n");
738 partp->dp_shd = -1;
739 partp->dp_scyl = -1;
740 partp->dp_ssect = -1;
741 } else {
742 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
743 partp->dp_scyl = DOSCYL(cy);
744 partp->dp_ssect = DOSSECT(sec, cy);
747 /* End c/h/s. */
748 end = partp->dp_start + partp->dp_size - 1;
749 cy = end / dos_cylsecs;
750 sec = end % dos_sectors + 1;
751 if (cy > MAXCYL && C_flag == 0) {
752 printf("Warning: ending cylinder wraps, using all 1's\n");
753 partp->dp_ehd = -1;
754 partp->dp_ecyl = -1;
755 partp->dp_esect = -1;
756 } else {
757 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
758 partp->dp_ecyl = DOSCYL(cy);
759 partp->dp_esect = DOSSECT(sec, cy);
763 int fd;
765 /* Getting device status */
767 static int
768 open_disk(int u_flag)
770 struct stat st;
772 if (stat(disk, &st) == -1) {
773 if (errno == ENOENT)
774 return -2;
775 warnx("can't get file status of %s", disk);
776 return -1;
778 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
779 warnx("device %s is not character special", disk);
780 if ((fd = open(disk,
781 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
782 if(errno == ENXIO)
783 return -2;
784 warnx("can't open device %s", disk);
785 return -1;
787 if (get_params() == -1) {
788 warnx("can't get disk parameters on %s", disk);
789 return -1;
791 return fd;
794 static ssize_t
795 read_disk(off_t sector, void *buf)
797 lseek(fd,(sector * 512), 0);
798 if( secsize == 0 )
799 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
801 /* try the read */
802 int size = read(fd, buf, secsize);
803 if( size == secsize )
804 /* it worked so return */
805 return secsize;
807 else
808 return read( fd, buf, secsize );
810 /* we failed to read at any of the sizes */
811 return -1;
814 static ssize_t
815 write_disk(off_t sector, void *buf)
817 lseek(fd,(sector * 512), 0);
818 /* write out in the size that the read_disk found worked */
819 return write(fd, buf, secsize);
822 static int
823 get_params(void)
825 struct partinfo partinfo; /* disk parameters */
826 struct stat st;
828 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
829 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
830 sectors = 63;
831 heads = 16;
832 cylsecs = heads * sectors;
833 cyls = st.st_size / 512 / cylsecs;
834 } else {
835 warnx("can't get disk parameters on %s; supplying dummy ones",
836 disk);
837 heads = 1;
838 cylsecs = heads * sectors;
840 } else {
841 cyls = partinfo.d_ncylinders;
842 heads = partinfo.d_nheads;
843 sectors = partinfo.d_secpertrack;
844 cylsecs = heads * sectors;
845 secsize = partinfo.media_blksize;
847 dos_cyls = cyls;
848 dos_heads = heads;
849 dos_sectors = sectors;
850 dos_cylsecs = cylsecs;
851 disksecs = (int64_t)cyls * heads * sectors;
852 return (disksecs);
856 static int
857 read_s0(void)
859 mboot.bootinst_size = secsize;
860 if (mboot.bootinst != NULL)
861 free(mboot.bootinst);
862 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
863 warnx("unable to allocate buffer to read fdisk "
864 "partition table");
865 return -1;
867 if (read_disk(0, mboot.bootinst) == -1) {
868 warnx("can't read fdisk partition table");
869 return -1;
871 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
872 warnx("invalid fdisk partition table found");
873 /* So should we initialize things */
874 return -1;
876 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
877 return 0;
880 static int
881 write_s0(void)
883 #ifdef NOT_NOW
884 int flag;
885 #endif
886 int sector;
888 if (iotest) {
889 print_s0(-1);
890 return 0;
892 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
894 * write enable label sector before write (if necessary),
895 * disable after writing.
896 * needed if the disklabel protected area also protects
897 * sector 0. (e.g. empty disk)
899 #ifdef NOT_NOW
900 flag = 1;
901 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
902 warn("ioctl DIOCWLABEL");
903 #endif
904 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
905 if (write_disk(sector,
906 &mboot.bootinst[sector * secsize]) == -1) {
907 warn("can't write fdisk partition table");
908 return -1;
909 #ifdef NOT_NOW
910 flag = 0;
911 ioctl(fd, DIOCWLABEL, &flag);
912 #endif
914 #ifdef NOT_NOW
915 flag = 0;
916 ioctl(fd, DIOCWLABEL, &flag);
917 #endif
918 return(0);
922 static int
923 ok(char *str)
925 printf("%s [n] ", str);
926 fflush(stdout);
927 if (fgets(lbuf, LBUF, stdin) == NULL)
928 exit(1);
929 lbuf[strlen(lbuf)-1] = 0;
931 if (*lbuf &&
932 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
933 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
934 return 1;
935 else
936 return 0;
939 static int
940 decimal(char *str, int *num, int deflt)
942 int acc = 0, c;
943 char *cp;
945 while (1) {
946 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
947 fflush(stdout);
948 if (fgets(lbuf, LBUF, stdin) == NULL)
949 exit(1);
950 lbuf[strlen(lbuf)-1] = 0;
952 if (!*lbuf)
953 return 0;
955 cp = lbuf;
956 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
957 if (!c)
958 return 0;
959 while ((c = *cp++)) {
960 if (c <= '9' && c >= '0')
961 acc = acc * 10 + c - '0';
962 else
963 break;
965 if (c == ' ' || c == '\t')
966 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
967 if (!c) {
968 *num = acc;
969 return 1;
970 } else
971 printf("%s is an invalid decimal number. Try again.\n",
972 lbuf);
977 #if 0
978 static int
979 hex(char *str, int *num, int deflt)
981 int acc = 0, c;
982 char *cp;
984 while (1) {
985 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
986 fgets(lbuf, LBUF, stdin);
987 lbuf[strlen(lbuf)-1] = 0;
989 if (!*lbuf)
990 return 0;
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 << 4) + c - '0';
999 else if (c <= 'f' && c >= 'a')
1000 acc = (acc << 4) + c - 'a' + 10;
1001 else if (c <= 'F' && c >= 'A')
1002 acc = (acc << 4) + c - 'A' + 10;
1003 else
1004 break;
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 hex number. Try again.\n",
1013 lbuf);
1018 static int
1019 string(char *str, char **ans)
1021 int c;
1022 char *cp = lbuf;
1024 while (1) {
1025 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1026 fgets(lbuf, LBUF, stdin);
1027 lbuf[strlen(lbuf)-1] = 0;
1029 if (!*lbuf)
1030 return 0;
1032 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1033 if (c == '"') {
1034 c = *++cp;
1035 *ans = cp;
1036 while ((c = *cp) && c != '"') cp++;
1037 } else {
1038 *ans = cp;
1039 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1042 if (c)
1043 *cp = 0;
1044 return 1;
1047 #endif
1049 static char *
1050 get_type(int type)
1052 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1053 int counter = 0;
1054 struct part_type *ptr = part_types;
1057 while(counter < numentries)
1059 if(ptr->type == type)
1061 return(ptr->name);
1063 ptr++;
1064 counter++;
1066 return("unknown");
1070 static void
1071 parse_config_line(char *line, CMD *command)
1073 char *cp, *end;
1075 cp = line;
1076 while (1) /* dirty trick used to insure one exit point for this
1077 function */
1079 memset(command, 0, sizeof(*command));
1081 while (isspace(*cp)) ++cp;
1082 if (*cp == '\0' || *cp == '#')
1084 break;
1086 command->cmd = *cp++;
1089 * Parse args
1091 while (1)
1093 while (isspace(*cp)) ++cp;
1094 if (*cp == '#')
1096 break; /* found comment */
1098 if (isalpha(*cp))
1100 command->args[command->n_args].argtype = *cp++;
1102 if (!isdigit(*cp))
1104 break; /* assume end of line */
1106 end = NULL;
1107 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1108 if (cp == end)
1110 break; /* couldn't parse number */
1112 cp = end;
1113 command->n_args++;
1115 break;
1120 static int
1121 process_geometry(CMD *command)
1123 int status = 1, i;
1125 while (1)
1127 geom_processed = 1;
1128 if (part_processed)
1130 warnx(
1131 "ERROR line %d: the geometry specification line must occur before\n\
1132 all partition specifications",
1133 current_line_number);
1134 status = 0;
1135 break;
1137 if (command->n_args != 3)
1139 warnx("ERROR line %d: incorrect number of geometry args",
1140 current_line_number);
1141 status = 0;
1142 break;
1144 dos_cyls = -1;
1145 dos_heads = -1;
1146 dos_sectors = -1;
1147 for (i = 0; i < 3; ++i)
1149 switch (command->args[i].argtype)
1151 case 'c':
1152 dos_cyls = command->args[i].arg_val;
1153 break;
1154 case 'h':
1155 dos_heads = command->args[i].arg_val;
1156 break;
1157 case 's':
1158 dos_sectors = command->args[i].arg_val;
1159 break;
1160 default:
1161 warnx(
1162 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1163 current_line_number, command->args[i].argtype,
1164 command->args[i].argtype);
1165 status = 0;
1166 break;
1169 if (status == 0)
1171 break;
1174 dos_cylsecs = dos_heads * dos_sectors;
1177 * Do sanity checks on parameter values
1179 if (dos_cyls < 0)
1181 warnx("ERROR line %d: number of cylinders not specified",
1182 current_line_number);
1183 status = 0;
1185 if (dos_cyls == 0 || dos_cyls > 1024)
1187 warnx(
1188 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1189 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1190 is dedicated to DragonFly)",
1191 current_line_number, dos_cyls);
1194 if (dos_heads < 0)
1196 warnx("ERROR line %d: number of heads not specified",
1197 current_line_number);
1198 status = 0;
1200 else if (dos_heads < 1 || dos_heads > 256)
1202 warnx("ERROR line %d: number of heads must be within (1-256)",
1203 current_line_number);
1204 status = 0;
1207 if (dos_sectors < 0)
1209 warnx("ERROR line %d: number of sectors not specified",
1210 current_line_number);
1211 status = 0;
1213 else if (dos_sectors < 1 || dos_sectors > 63)
1215 warnx("ERROR line %d: number of sectors must be within (1-63)",
1216 current_line_number);
1217 status = 0;
1220 break;
1222 return (status);
1226 static int
1227 process_partition(CMD *command)
1229 int status = 0, partition;
1230 u_int32_t prev_head_boundary, prev_cyl_boundary;
1231 u_int32_t adj_size, max_end;
1232 struct dos_partition *partp;
1234 while (1)
1236 part_processed = 1;
1237 if (command->n_args != 4)
1239 warnx("ERROR line %d: incorrect number of partition args",
1240 current_line_number);
1241 break;
1243 partition = command->args[0].arg_val;
1244 if (partition < 1 || partition > 4)
1246 warnx("ERROR line %d: invalid partition number %d",
1247 current_line_number, partition);
1248 break;
1250 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1251 bzero((char *)partp, sizeof (struct dos_partition));
1252 partp->dp_typ = command->args[1].arg_val;
1253 partp->dp_start = command->args[2].arg_val;
1254 partp->dp_size = command->args[3].arg_val;
1255 max_end = partp->dp_start + partp->dp_size;
1257 if (partp->dp_typ == 0)
1260 * Get out, the partition is marked as unused.
1263 * Insure that it's unused.
1265 bzero((char *)partp, sizeof (struct dos_partition));
1266 status = 1;
1267 break;
1271 * Adjust start upwards, if necessary, to fall on an head boundary.
1273 if (partp->dp_start % dos_sectors != 0)
1275 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1276 if (max_end < dos_sectors ||
1277 prev_head_boundary > max_end - dos_sectors)
1280 * Can't go past end of partition
1282 warnx(
1283 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1284 a head boundary",
1285 current_line_number, partition);
1286 break;
1288 warnx(
1289 "WARNING: adjusting start offset of partition %d\n\
1290 from %u to %u, to fall on a head boundary",
1291 partition, (u_int)partp->dp_start,
1292 (u_int)(prev_head_boundary + dos_sectors));
1293 partp->dp_start = prev_head_boundary + dos_sectors;
1297 * Adjust size downwards, if necessary, to fall on a cylinder
1298 * boundary.
1300 prev_cyl_boundary =
1301 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1302 if (prev_cyl_boundary > partp->dp_start)
1303 adj_size = prev_cyl_boundary - partp->dp_start;
1304 else
1306 warnx(
1307 "ERROR: could not adjust partition to start on a head boundary\n\
1308 and end on a cylinder boundary.");
1309 return (0);
1311 if (adj_size != partp->dp_size)
1313 warnx(
1314 "WARNING: adjusting size of partition %d from %u to %u\n\
1315 to end on a cylinder boundary",
1316 partition, (u_int)partp->dp_size, (u_int)adj_size);
1317 partp->dp_size = adj_size;
1319 if (partp->dp_size == 0)
1321 warnx("ERROR line %d: size of partition %d is zero",
1322 current_line_number, partition);
1323 break;
1326 dos(partp);
1327 status = 1;
1328 break;
1330 return (status);
1334 static int
1335 process_active(CMD *command)
1337 int status = 0, partition, i;
1338 struct dos_partition *partp;
1340 while (1)
1342 active_processed = 1;
1343 if (command->n_args != 1)
1345 warnx("ERROR line %d: incorrect number of active args",
1346 current_line_number);
1347 status = 0;
1348 break;
1350 partition = command->args[0].arg_val;
1351 if (partition < 1 || partition > 4)
1353 warnx("ERROR line %d: invalid partition number %d",
1354 current_line_number, partition);
1355 break;
1358 * Reset active partition
1360 partp = ((struct dos_partition *) &mboot.parts);
1361 for (i = 0; i < NDOSPART; i++)
1362 partp[i].dp_flag = 0;
1363 partp[partition-1].dp_flag = ACTIVE;
1365 status = 1;
1366 break;
1368 return (status);
1372 static int
1373 process_line(char *line)
1375 CMD command;
1376 int status = 1;
1378 while (1)
1380 parse_config_line(line, &command);
1381 switch (command.cmd)
1383 case 0:
1385 * Comment or blank line
1387 break;
1388 case 'g':
1390 * Set geometry
1392 status = process_geometry(&command);
1393 break;
1394 case 'p':
1395 status = process_partition(&command);
1396 break;
1397 case 'a':
1398 status = process_active(&command);
1399 break;
1400 default:
1401 status = 0;
1402 break;
1404 break;
1406 return (status);
1410 static int
1411 read_config(char *config_file)
1413 FILE *fp = NULL;
1414 int status = 1;
1415 char buf[1010];
1417 while (1) /* dirty trick used to insure one exit point for this
1418 function */
1420 if (strcmp(config_file, "-") != 0)
1423 * We're not reading from stdin
1425 if ((fp = fopen(config_file, "r")) == NULL)
1427 status = 0;
1428 break;
1431 else
1433 fp = stdin;
1435 current_line_number = 0;
1436 while (!feof(fp))
1438 if (fgets(buf, sizeof(buf), fp) == NULL)
1440 break;
1442 ++current_line_number;
1443 status = process_line(buf);
1444 if (status == 0)
1446 break;
1449 break;
1451 if (fp)
1454 * It doesn't matter if we're reading from stdin, as we've reached EOF
1456 fclose(fp);
1458 return (status);
1462 static void
1463 reset_boot(void)
1465 int i;
1466 struct dos_partition *partp;
1468 init_boot();
1469 for (i = 0; i < 4; ++i)
1471 partp = ((struct dos_partition *) &mboot.parts) + i;
1472 bzero((char *)partp, sizeof (struct dos_partition));
1476 static int
1477 sanitize_partition(struct dos_partition *partp)
1479 u_int32_t prev_head_boundary, prev_cyl_boundary;
1480 u_int32_t max_end, size, start;
1482 start = partp->dp_start;
1483 size = partp->dp_size;
1484 max_end = start + size;
1485 /* Only allow a zero size if the partition is being marked unused. */
1486 if (size == 0) {
1487 if (start == 0 && partp->dp_typ == 0)
1488 return (1);
1489 warnx("ERROR: size of partition is zero");
1490 return (0);
1492 /* Return if no adjustment is necessary. */
1493 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1494 return (1);
1496 if (start == 0) {
1497 warnx("WARNING: partition overlaps with partition table");
1498 if (ok("Correct this automatically?"))
1499 start = dos_sectors;
1501 if (start % dos_sectors != 0)
1502 warnx("WARNING: partition does not start on a head boundary");
1503 if ((start +size) % dos_sectors != 0)
1504 warnx("WARNING: partition does not end on a cylinder boundary");
1505 warnx("WARNING: this may confuse the BIOS or some operating systems");
1506 if (!ok("Correct this automatically?"))
1507 return (1);
1510 * Adjust start upwards, if necessary, to fall on an head boundary.
1512 if (start % dos_sectors != 0) {
1513 prev_head_boundary = start / dos_sectors * dos_sectors;
1514 if (max_end < dos_sectors ||
1515 prev_head_boundary >= max_end - dos_sectors) {
1517 * Can't go past end of partition
1519 warnx(
1520 "ERROR: unable to adjust start of partition to fall on a head boundary");
1521 return (0);
1523 start = prev_head_boundary + dos_sectors;
1527 * Adjust size downwards, if necessary, to fall on a cylinder
1528 * boundary.
1530 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1531 if (prev_cyl_boundary > start)
1532 size = prev_cyl_boundary - start;
1533 else {
1534 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1535 and end on a cylinder boundary.");
1536 return (0);
1539 /* Finally, commit any changes to partp and return. */
1540 if (start != partp->dp_start) {
1541 warnx("WARNING: adjusting start offset of partition to %u",
1542 (u_int)start);
1543 partp->dp_start = start;
1545 if (size != partp->dp_size) {
1546 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1547 partp->dp_size = size;
1550 return (1);