Correct comments and minor variable naming and sysctl issues.
[dragonfly.git] / sbin / i386 / fdisk / fdisk.c
blob3081a18fc131c2c6fea9aafbf24ecdbe9788bd03
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 <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 int iotest;
45 #define LBUF 100
46 static char lbuf[LBUF];
48 #define MBRSIGOFF 510
52 * Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
54 * 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
55 * Copyright (c) 1989 Robert. V. Baron
56 * Created.
59 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
60 #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
61 #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
63 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
65 #define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
66 #define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
67 int secsize = 0; /* the sensed sector size */
69 const char *disk;
70 const char *disks[] =
72 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
75 int cyls, sectors, heads, cylsecs, disksecs;
77 struct mboot
79 unsigned char padding[2]; /* force the longs to be long aligned */
80 unsigned char *bootinst; /* boot code */
81 off_t bootinst_size;
82 struct dos_partition parts[4];
84 struct mboot mboot = {{0}, NULL, 0};
86 #define ACTIVE 0x80
87 #define BOOT_MAGIC 0xAA55
89 int dos_cyls;
90 int dos_heads;
91 int dos_sectors;
92 int dos_cylsecs;
94 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
95 #define DOSCYL(c) (c & 0xff)
96 #define MAXCYL 1023
97 static int partition = -1;
100 #define MAX_ARGS 10
102 static int current_line_number;
104 static int geom_processed = 0;
105 static int part_processed = 0;
106 static int active_processed = 0;
109 typedef struct cmd {
110 char cmd;
111 int n_args;
112 struct arg {
113 char argtype;
114 int arg_val;
115 } args[MAX_ARGS];
116 } CMD;
119 static int B_flag = 0; /* replace boot code */
120 static int C_flag = 0; /* use wrapped values for CHS */
121 static int I_flag = 0; /* use entire disk for DragonFly */
122 static int a_flag = 0; /* set active partition */
123 static char *b_flag = NULL; /* path to boot code */
124 static int i_flag = 0; /* replace partition data */
125 static int u_flag = 0; /* update partition data */
126 static int p_flag = 0; /* operate on a disk image file */
127 static int s_flag = 0; /* Print a summary and exit */
128 static int t_flag = 0; /* test only */
129 static char *f_flag = NULL; /* Read config info from file */
130 static int v_flag = 0; /* Be verbose */
132 struct part_type
134 unsigned char type;
135 char *name;
136 }part_types[] =
138 {0x00, "unused"}
139 ,{0x01, "Primary DOS with 12 bit FAT"}
140 ,{0x02, "XENIX / filesystem"}
141 ,{0x03, "XENIX /usr filesystem"}
142 ,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
143 ,{0x05, "Extended DOS"}
144 ,{0x06, "Primary 'big' DOS (> 32MB)"}
145 ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
146 ,{0x08, "AIX filesystem"}
147 ,{0x09, "AIX boot partition or Coherent"}
148 ,{0x0A, "OS/2 Boot Manager or OPUS"}
149 ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
150 ,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
151 ,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
152 ,{0x0F, "Extended DOS, LBA"}
153 ,{0x10, "OPUS"}
154 ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
155 ,{0x12, "Compaq diagnostics"}
156 ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
157 ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
158 ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
159 ,{0x18, "AST Windows swapfile"}
160 ,{0x24, "NEC DOS"}
161 ,{0x39, "plan9"}
162 ,{0x3C, "PartitionMagic recovery"}
163 ,{0x40, "VENIX 286"}
164 ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
165 ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
166 ,{0x43, "Linux native (sharing disk with DRDOS)"}
167 ,{0x4D, "QNX 4.2 Primary"}
168 ,{0x4E, "QNX 4.2 Secondary"}
169 ,{0x4F, "QNX 4.2 Tertiary"}
170 ,{0x50, "DM"}
171 ,{0x51, "DM"}
172 ,{0x52, "CP/M or Microport SysV/AT"}
173 ,{0x53, "DM6 Aux3"}
174 ,{0x54, "DM6"}
175 ,{0x55, "EZ-Drive (disk manager)"}
176 ,{0x56, "GB"}
177 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
178 ,{0x61, "Speed"}
179 ,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
180 ,{0x64, "Novell Netware 2.xx"}
181 ,{0x65, "Novell Netware 3.xx"}
182 ,{0x70, "DiskSecure Multi-Boot"}
183 ,{0x75, "PCIX"}
184 ,{0x77, "QNX4.x"}
185 ,{0x78, "QNX4.x 2nd part"}
186 ,{0x79, "QNX4.x 3rd part"}
187 ,{0x80, "Minix 1.1 ... 1.4a"}
188 ,{0x81, "Minix 1.4b ... 1.5.10"}
189 ,{0x82, "Linux swap or Solaris x86"}
190 ,{0x83, "Linux filesystem"}
191 ,{0x84, "OS/2 hidden C: drive"}
192 ,{0x85, "Linux extended"}
193 ,{0x86, "NTFS volume set??"}
194 ,{0x87, "NTFS volume set??"}
195 ,{0x93, "Amoeba filesystem"}
196 ,{0x94, "Amoeba bad block table"}
197 ,{0x9F, "BSD/OS"}
198 ,{0xA0, "Suspend to Disk"}
199 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
200 ,{0xA6, "OpenBSD"}
201 ,{0xA7, "NEXTSTEP"}
202 ,{0xA9, "NetBSD"}
203 ,{0xAC, "IBM JFS"}
204 ,{0xB7, "BSDI BSD/386 filesystem"}
205 ,{0xB8, "BSDI BSD/386 swap"}
206 ,{0xBE, "Solaris x86 boot"}
207 ,{0xC1, "DRDOS/sec with 12-bit FAT"}
208 ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
209 ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
210 ,{0xC7, "Syrinx"}
211 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
212 ,{0xE1, "Speed"}
213 ,{0xE3, "Speed"}
214 ,{0xE4, "Speed"}
215 ,{0xEB, "BeOS file system"}
216 ,{0xEE, "EFI GPT"}
217 ,{0xEF, "EFI System Partition"}
218 ,{0xF1, "Speed"}
219 ,{0xF2, "DOS 3.3+ Secondary"}
220 ,{0xF4, "Speed"}
221 ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
222 ,{0xFF, "BBT (Bad Blocks Table)"}
225 static void print_s0(int which);
226 static void print_part(int i);
227 static void init_sector0(unsigned long start);
228 static void init_boot(void);
229 static void change_part(int i);
230 static void print_params();
231 static void change_active(int which);
232 static void change_code();
233 static void get_params_to_use();
234 static void dos(struct dos_partition *partp);
235 static int open_disk(int u_flag);
236 static ssize_t read_disk(off_t sector, void *buf);
237 static ssize_t write_disk(off_t sector, void *buf);
238 static int get_params();
239 static int read_s0();
240 static int write_s0();
241 static int ok(char *str);
242 static int decimal(char *str, int *num, int deflt);
243 static char *get_type(int type);
244 static int read_config(char *config_file);
245 static void reset_boot(void);
246 static int sanitize_partition(struct dos_partition *);
247 static void usage(void);
248 #if 0
249 static int hex(char *str, int *num, int deflt);
250 static int string(char *str, char **ans);
251 #endif
255 main(int argc, char *argv[])
257 int c, i;
259 while ((c = getopt(argc, argv, "BCIab:f:p:istuv1234")) != -1)
260 switch (c) {
261 case 'B':
262 B_flag = 1;
263 break;
264 case 'C':
265 C_flag = 1;
266 break;
267 case 'I':
268 I_flag = 1;
269 break;
270 case 'a':
271 a_flag = 1;
272 break;
273 case 'b':
274 b_flag = optarg;
275 break;
276 case 'f':
277 f_flag = optarg;
278 break;
279 case 'p':
280 disk = optarg;
281 p_flag = 1;
282 break;
283 case 'i':
284 i_flag = 1;
285 break;
286 case 's':
287 s_flag = 1;
288 break;
289 case 't':
290 t_flag = 1;
291 break;
292 case 'u':
293 u_flag = 1;
294 break;
295 case 'v':
296 v_flag = 1;
297 break;
298 case '1':
299 case '2':
300 case '3':
301 case '4':
302 partition = c - '0';
303 break;
304 default:
305 usage();
307 if (f_flag || i_flag)
308 u_flag = 1;
309 if (t_flag)
310 v_flag = 1;
311 argc -= optind;
312 argv += optind;
314 if (argc > 0) {
315 static char realname[12];
317 if(strncmp(argv[0], "/dev", 4) == 0) {
318 disk = argv[0];
319 } else {
320 snprintf(realname, 12, "/dev/%s", argv[0]);
321 disk = realname;
324 if (open_disk(u_flag) < 0)
325 err(1, "cannot open disk %s", disk);
326 } else if (disk == NULL) {
327 int rv = 0;
329 for(i = 0; disks[i]; i++)
331 disk = disks[i];
332 rv = open_disk(u_flag);
333 if(rv != -2) break;
335 if(rv < 0)
336 err(1, "cannot open any disk");
337 } else {
338 if (open_disk(u_flag) < 0)
339 err(1, "cannot open disk %s", disk);
342 /* (abu)use mboot.bootinst to probe for the sector size */
343 if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
344 err(1, "cannot allocate buffer to determine disk sector size");
345 read_disk(0, mboot.bootinst);
346 free(mboot.bootinst);
347 mboot.bootinst = NULL;
349 if (s_flag)
351 int i;
352 struct dos_partition *partp;
354 if (read_s0())
355 err(1, "read_s0");
356 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
357 dos_sectors);
358 printf("Part %11s %11s Type Flags\n", "Start", "Size");
359 for (i = 0; i < NDOSPART; i++) {
360 partp = ((struct dos_partition *) &mboot.parts) + i;
361 if (partp->dp_start == 0 && partp->dp_size == 0)
362 continue;
363 printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
364 (u_long) partp->dp_start,
365 (u_long) partp->dp_size, partp->dp_typ,
366 partp->dp_flag);
368 exit(0);
371 printf("******* Working on device %s *******\n",disk);
373 if (I_flag)
375 struct dos_partition *partp;
377 read_s0();
378 reset_boot();
379 partp = (struct dos_partition *) (&mboot.parts[0]);
380 partp->dp_typ = DOSPTYP_386BSD;
381 partp->dp_flag = ACTIVE;
382 partp->dp_start = dos_sectors;
383 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
384 dos_sectors;
385 dos(partp);
386 if (v_flag)
387 print_s0(-1);
388 if (!t_flag)
389 write_s0();
390 exit(0);
392 if (f_flag)
394 if (read_s0() || i_flag)
396 reset_boot();
399 if (!read_config(f_flag))
401 exit(1);
403 if (v_flag)
405 print_s0(-1);
407 if (!t_flag)
409 write_s0();
412 else
414 if(u_flag)
416 get_params_to_use();
418 else
420 print_params();
423 if (read_s0())
424 init_sector0(dos_sectors);
426 printf("Media sector size is %d\n", secsize);
427 printf("Warning: BIOS sector numbering starts with sector 1\n");
428 printf("Information from DOS bootblock is:\n");
429 if (partition == -1)
430 for (i = 1; i <= NDOSPART; i++)
431 change_part(i);
432 else
433 change_part(partition);
435 if (u_flag || a_flag)
436 change_active(partition);
438 if (B_flag)
439 change_code();
441 if (u_flag || a_flag || B_flag) {
442 if (!t_flag) {
443 printf("\nWe haven't changed the partition table yet. ");
444 printf("This is your last chance.\n");
446 print_s0(-1);
447 if (!t_flag) {
448 if (ok("Should we write new partition table?"))
449 write_s0();
451 else
453 printf("\n-t flag specified -- partition table not written.\n");
458 exit(0);
461 static void
462 usage(void)
464 fprintf(stderr, "%s%s",
465 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
466 " fdisk -f configfile [-itv] [disk]\n");
467 exit(1);
470 static void
471 print_s0(int which)
473 int i;
475 print_params();
476 printf("Information from DOS bootblock is:\n");
477 if (which == -1)
478 for (i = 1; i <= NDOSPART; i++)
479 printf("%d: ", i), print_part(i);
480 else
481 print_part(which);
484 static struct dos_partition mtpart = { 0 };
486 static void
487 print_part(int i)
489 struct dos_partition *partp;
490 u_int64_t part_mb;
492 partp = ((struct dos_partition *) &mboot.parts) + i - 1;
494 if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
495 printf("<UNUSED>\n");
496 return;
499 * Be careful not to overflow.
501 part_mb = partp->dp_size;
502 part_mb *= secsize;
503 part_mb /= (1024 * 1024);
504 printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
505 printf(" start %lu, size %lu (%qd Meg), flag %x%s\n",
506 (u_long)partp->dp_start,
507 (u_long)partp->dp_size,
508 part_mb,
509 partp->dp_flag,
510 partp->dp_flag == ACTIVE ? " (active)" : "");
511 printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
512 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
513 ,partp->dp_shd
514 ,DPSECT(partp->dp_ssect)
515 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
516 ,partp->dp_ehd
517 ,DPSECT(partp->dp_esect));
521 static void
522 init_boot(void)
524 const char *fname;
525 int fd, n;
526 struct stat sb;
528 fname = b_flag ? b_flag : "/boot/mbr";
529 if ((fd = open(fname, O_RDONLY)) == -1 ||
530 fstat(fd, &sb) == -1)
531 err(1, "%s", fname);
532 if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
533 errx(1, "%s: length must be a multiple of sector size", fname);
534 if (mboot.bootinst != NULL)
535 free(mboot.bootinst);
536 if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
537 errx(1, "%s: unable to allocate read buffer", fname);
538 if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
539 close(fd))
540 err(1, "%s", fname);
541 if (n != mboot.bootinst_size)
542 errx(1, "%s: short read", fname);
546 static void
547 init_sector0(unsigned long start)
549 struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
551 init_boot();
553 partp->dp_typ = DOSPTYP_386BSD;
554 partp->dp_flag = ACTIVE;
555 start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
556 if(start == 0)
557 start = dos_sectors;
558 partp->dp_start = start;
559 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
561 dos(partp);
564 static void
565 change_part(int i)
567 struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
569 printf("The data for partition %d is:\n", i);
570 print_part(i);
572 if (u_flag && ok("Do you want to change it?")) {
573 int tmp;
575 if (i_flag) {
576 bzero((char *)partp, sizeof (struct dos_partition));
577 if (i == 4) {
578 init_sector0(1);
579 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
580 print_part(i);
584 do {
585 Decimal("sysid (165=DragonFly)", partp->dp_typ, tmp);
586 Decimal("start", partp->dp_start, tmp);
587 Decimal("size", partp->dp_size, tmp);
588 if (!sanitize_partition(partp)) {
589 warnx("ERROR: failed to adjust; setting sysid to 0");
590 partp->dp_typ = 0;
593 if (ok("Explicitly specify beg/end address ?"))
595 int tsec,tcyl,thd;
596 tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
597 thd = partp->dp_shd;
598 tsec = DPSECT(partp->dp_ssect);
599 Decimal("beginning cylinder", tcyl, tmp);
600 Decimal("beginning head", thd, tmp);
601 Decimal("beginning sector", tsec, tmp);
602 if (tcyl > MAXCYL && C_flag == 0) {
603 printf("Warning: starting cylinder wraps, using all 1's\n");
604 partp->dp_scyl = -1;
605 partp->dp_ssect = -1;
606 partp->dp_shd = -1;
607 } else {
608 partp->dp_scyl = DOSCYL(tcyl);
609 partp->dp_ssect = DOSSECT(tsec,tcyl);
610 partp->dp_shd = thd;
613 tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
614 thd = partp->dp_ehd;
615 tsec = DPSECT(partp->dp_esect);
616 Decimal("ending cylinder", tcyl, tmp);
617 Decimal("ending head", thd, tmp);
618 Decimal("ending sector", tsec, tmp);
619 if (tcyl > MAXCYL && C_flag == 0) {
620 printf("Warning: ending cylinder wraps, using all 1's\n");
621 partp->dp_ecyl = -1;
622 partp->dp_esect = -1;
623 partp->dp_ehd = -1;
624 } else {
625 partp->dp_ecyl = DOSCYL(tcyl);
626 partp->dp_esect = DOSSECT(tsec,tcyl);
627 partp->dp_ehd = thd;
629 } else
630 dos(partp);
632 print_part(i);
633 } while (!ok("Are we happy with this entry?"));
637 static void
638 print_params(void)
640 printf("parameters extracted from device are:\n");
641 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
642 ,cyls,heads,sectors,cylsecs);
643 if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
644 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
645 printf("parameters to be used for BIOS calculations are:\n");
646 printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
647 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
650 static void
651 change_active(int which)
653 struct dos_partition *partp = &mboot.parts[0];
654 int active, i, new, tmp;
656 active = -1;
657 for (i = 0; i < NDOSPART; i++) {
658 if ((partp[i].dp_flag & ACTIVE) == 0)
659 continue;
660 printf("Partition %d is marked active\n", i + 1);
661 if (active == -1)
662 active = i + 1;
664 if (a_flag && which != -1)
665 active = which;
666 else if (active == -1)
667 active = 1;
669 if (!ok("Do you want to change the active partition?"))
670 return;
671 setactive:
672 do {
673 new = active;
674 Decimal("active partition", new, tmp);
675 if (new < 1 || new > 4) {
676 printf("Active partition number must be in range 1-4."
677 " Try again.\n");
678 goto setactive;
680 active = new;
681 } while (!ok("Are you happy with this choice"));
682 for (i = 0; i < NDOSPART; i++)
683 partp[i].dp_flag = 0;
684 if (active > 0 && active <= NDOSPART)
685 partp[active-1].dp_flag = ACTIVE;
688 static void
689 change_code(void)
691 if (ok("Do you want to change the boot code?"))
692 init_boot();
695 void
696 get_params_to_use(void)
698 int tmp;
699 print_params();
700 if (ok("Do you want to change our idea of what BIOS thinks ?"))
704 Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
705 Decimal("BIOS's idea of #heads", dos_heads, tmp);
706 Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
707 dos_cylsecs = dos_heads * dos_sectors;
708 print_params();
710 while(!ok("Are you happy with this choice"));
715 /***********************************************\
716 * Change real numbers into strange dos numbers *
717 \***********************************************/
718 static void
719 dos(struct dos_partition *partp)
721 int cy, sec;
722 u_int32_t end;
724 if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
725 memcpy(partp, &mtpart, sizeof(*partp));
726 return;
729 /* Start c/h/s. */
730 cy = partp->dp_start / dos_cylsecs;
731 sec = partp->dp_start % dos_sectors + 1;
732 if (cy > MAXCYL && C_flag == 0) {
733 printf("Warning: starting cylinder wraps, using all 1's\n");
734 partp->dp_shd = -1;
735 partp->dp_scyl = -1;
736 partp->dp_ssect = -1;
737 } else {
738 partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
739 partp->dp_scyl = DOSCYL(cy);
740 partp->dp_ssect = DOSSECT(sec, cy);
743 /* End c/h/s. */
744 end = partp->dp_start + partp->dp_size - 1;
745 cy = end / dos_cylsecs;
746 sec = end % dos_sectors + 1;
747 if (cy > MAXCYL && C_flag == 0) {
748 printf("Warning: ending cylinder wraps, using all 1's\n");
749 partp->dp_ehd = -1;
750 partp->dp_ecyl = -1;
751 partp->dp_esect = -1;
752 } else {
753 partp->dp_ehd = end % dos_cylsecs / dos_sectors;
754 partp->dp_ecyl = DOSCYL(cy);
755 partp->dp_esect = DOSSECT(sec, cy);
759 int fd;
761 /* Getting device status */
763 static int
764 open_disk(int u_flag)
766 struct stat st;
768 if (stat(disk, &st) == -1) {
769 if (errno == ENOENT)
770 return -2;
771 warnx("can't get file status of %s", disk);
772 return -1;
774 if ( !(st.st_mode & S_IFCHR) && p_flag == 0 )
775 warnx("device %s is not character special", disk);
776 if ((fd = open(disk,
777 a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
778 if(errno == ENXIO)
779 return -2;
780 warnx("can't open device %s", disk);
781 return -1;
783 if (get_params() == -1) {
784 warnx("can't get disk parameters on %s", disk);
785 return -1;
787 return fd;
790 static ssize_t
791 read_disk(off_t sector, void *buf)
793 lseek(fd,(sector * 512), 0);
794 if( secsize == 0 )
795 for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
797 /* try the read */
798 int size = read(fd, buf, secsize);
799 if( size == secsize )
800 /* it worked so return */
801 return secsize;
803 else
804 return read( fd, buf, secsize );
806 /* we failed to read at any of the sizes */
807 return -1;
810 static ssize_t
811 write_disk(off_t sector, void *buf)
813 lseek(fd,(sector * 512), 0);
814 /* write out in the size that the read_disk found worked */
815 return write(fd, buf, secsize);
818 static int
819 get_params(void)
821 struct partinfo partinfo; /* disk parameters */
822 struct stat st;
824 if (ioctl(fd, DIOCGPART, &partinfo) == -1) {
825 if (p_flag && fstat(fd, &st) == 0 && st.st_size) {
826 sectors = 63;
827 heads = 16;
828 cylsecs = heads * sectors;
829 cyls = st.st_size / 512 / cylsecs;
830 } else {
831 warnx("can't get disk parameters on %s; supplying dummy ones",
832 disk);
833 heads = 1;
834 cylsecs = heads * sectors;
836 } else {
837 cyls = partinfo.d_ncylinders;
838 heads = partinfo.d_nheads;
839 sectors = partinfo.d_secpertrack;
840 cylsecs = heads * sectors;
841 secsize = partinfo.media_blksize;
843 dos_cyls = cyls;
844 dos_heads = heads;
845 dos_sectors = sectors;
846 dos_cylsecs = cylsecs;
847 disksecs = cyls * heads * sectors;
848 return (disksecs);
852 static int
853 read_s0(void)
855 mboot.bootinst_size = secsize;
856 if (mboot.bootinst != NULL)
857 free(mboot.bootinst);
858 if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
859 warnx("unable to allocate buffer to read fdisk "
860 "partition table");
861 return -1;
863 if (read_disk(0, mboot.bootinst) == -1) {
864 warnx("can't read fdisk partition table");
865 return -1;
867 if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
868 warnx("invalid fdisk partition table found");
869 /* So should we initialize things */
870 return -1;
872 memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
873 return 0;
876 static int
877 write_s0(void)
879 #ifdef NOT_NOW
880 int flag;
881 #endif
882 int sector;
884 if (iotest) {
885 print_s0(-1);
886 return 0;
888 memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
890 * write enable label sector before write (if necessary),
891 * disable after writing.
892 * needed if the disklabel protected area also protects
893 * sector 0. (e.g. empty disk)
895 #ifdef NOT_NOW
896 flag = 1;
897 if (ioctl(fd, DIOCWLABEL, &flag) < 0)
898 warn("ioctl DIOCWLABEL");
899 #endif
900 for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
901 if (write_disk(sector,
902 &mboot.bootinst[sector * secsize]) == -1) {
903 warn("can't write fdisk partition table");
904 return -1;
905 #ifdef NOT_NOW
906 flag = 0;
907 ioctl(fd, DIOCWLABEL, &flag);
908 #endif
910 #ifdef NOT_NOW
911 flag = 0;
912 ioctl(fd, DIOCWLABEL, &flag);
913 #endif
914 return(0);
918 static int
919 ok(char *str)
921 printf("%s [n] ", str);
922 fflush(stdout);
923 if (fgets(lbuf, LBUF, stdin) == NULL)
924 exit(1);
925 lbuf[strlen(lbuf)-1] = 0;
927 if (*lbuf &&
928 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
929 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
930 return 1;
931 else
932 return 0;
935 static int
936 decimal(char *str, int *num, int deflt)
938 int acc = 0, c;
939 char *cp;
941 while (1) {
942 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
943 fflush(stdout);
944 if (fgets(lbuf, LBUF, stdin) == NULL)
945 exit(1);
946 lbuf[strlen(lbuf)-1] = 0;
948 if (!*lbuf)
949 return 0;
951 cp = lbuf;
952 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
953 if (!c)
954 return 0;
955 while ((c = *cp++)) {
956 if (c <= '9' && c >= '0')
957 acc = acc * 10 + c - '0';
958 else
959 break;
961 if (c == ' ' || c == '\t')
962 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
963 if (!c) {
964 *num = acc;
965 return 1;
966 } else
967 printf("%s is an invalid decimal number. Try again.\n",
968 lbuf);
973 #if 0
974 static int
975 hex(char *str, int *num, int deflt)
977 int acc = 0, c;
978 char *cp;
980 while (1) {
981 printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
982 fgets(lbuf, LBUF, stdin);
983 lbuf[strlen(lbuf)-1] = 0;
985 if (!*lbuf)
986 return 0;
988 cp = lbuf;
989 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
990 if (!c)
991 return 0;
992 while ((c = *cp++)) {
993 if (c <= '9' && c >= '0')
994 acc = (acc << 4) + c - '0';
995 else if (c <= 'f' && c >= 'a')
996 acc = (acc << 4) + c - 'a' + 10;
997 else if (c <= 'F' && c >= 'A')
998 acc = (acc << 4) + c - 'A' + 10;
999 else
1000 break;
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 hex number. Try again.\n",
1009 lbuf);
1014 static int
1015 string(char *str, char **ans)
1017 int c;
1018 char *cp = lbuf;
1020 while (1) {
1021 printf("Supply a string value for \"%s\" [%s] ", str, *ans);
1022 fgets(lbuf, LBUF, stdin);
1023 lbuf[strlen(lbuf)-1] = 0;
1025 if (!*lbuf)
1026 return 0;
1028 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
1029 if (c == '"') {
1030 c = *++cp;
1031 *ans = cp;
1032 while ((c = *cp) && c != '"') cp++;
1033 } else {
1034 *ans = cp;
1035 while ((c = *cp) && c != ' ' && c != '\t') cp++;
1038 if (c)
1039 *cp = 0;
1040 return 1;
1043 #endif
1045 static char *
1046 get_type(int type)
1048 int numentries = (sizeof(part_types)/sizeof(struct part_type));
1049 int counter = 0;
1050 struct part_type *ptr = part_types;
1053 while(counter < numentries)
1055 if(ptr->type == type)
1057 return(ptr->name);
1059 ptr++;
1060 counter++;
1062 return("unknown");
1066 static void
1067 parse_config_line(char *line, CMD *command)
1069 char *cp, *end;
1071 cp = line;
1072 while (1) /* dirty trick used to insure one exit point for this
1073 function */
1075 memset(command, 0, sizeof(*command));
1077 while (isspace(*cp)) ++cp;
1078 if (*cp == '\0' || *cp == '#')
1080 break;
1082 command->cmd = *cp++;
1085 * Parse args
1087 while (1)
1089 while (isspace(*cp)) ++cp;
1090 if (*cp == '#')
1092 break; /* found comment */
1094 if (isalpha(*cp))
1096 command->args[command->n_args].argtype = *cp++;
1098 if (!isdigit(*cp))
1100 break; /* assume end of line */
1102 end = NULL;
1103 command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1104 if (cp == end)
1106 break; /* couldn't parse number */
1108 cp = end;
1109 command->n_args++;
1111 break;
1116 static int
1117 process_geometry(CMD *command)
1119 int status = 1, i;
1121 while (1)
1123 geom_processed = 1;
1124 if (part_processed)
1126 warnx(
1127 "ERROR line %d: the geometry specification line must occur before\n\
1128 all partition specifications",
1129 current_line_number);
1130 status = 0;
1131 break;
1133 if (command->n_args != 3)
1135 warnx("ERROR line %d: incorrect number of geometry args",
1136 current_line_number);
1137 status = 0;
1138 break;
1140 dos_cyls = -1;
1141 dos_heads = -1;
1142 dos_sectors = -1;
1143 for (i = 0; i < 3; ++i)
1145 switch (command->args[i].argtype)
1147 case 'c':
1148 dos_cyls = command->args[i].arg_val;
1149 break;
1150 case 'h':
1151 dos_heads = command->args[i].arg_val;
1152 break;
1153 case 's':
1154 dos_sectors = command->args[i].arg_val;
1155 break;
1156 default:
1157 warnx(
1158 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1159 current_line_number, command->args[i].argtype,
1160 command->args[i].argtype);
1161 status = 0;
1162 break;
1165 if (status == 0)
1167 break;
1170 dos_cylsecs = dos_heads * dos_sectors;
1173 * Do sanity checks on parameter values
1175 if (dos_cyls < 0)
1177 warnx("ERROR line %d: number of cylinders not specified",
1178 current_line_number);
1179 status = 0;
1181 if (dos_cyls == 0 || dos_cyls > 1024)
1183 warnx(
1184 "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1185 (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1186 is dedicated to DragonFly)",
1187 current_line_number, dos_cyls);
1190 if (dos_heads < 0)
1192 warnx("ERROR line %d: number of heads not specified",
1193 current_line_number);
1194 status = 0;
1196 else if (dos_heads < 1 || dos_heads > 256)
1198 warnx("ERROR line %d: number of heads must be within (1-256)",
1199 current_line_number);
1200 status = 0;
1203 if (dos_sectors < 0)
1205 warnx("ERROR line %d: number of sectors not specified",
1206 current_line_number);
1207 status = 0;
1209 else if (dos_sectors < 1 || dos_sectors > 63)
1211 warnx("ERROR line %d: number of sectors must be within (1-63)",
1212 current_line_number);
1213 status = 0;
1216 break;
1218 return (status);
1222 static int
1223 process_partition(CMD *command)
1225 int status = 0, partition;
1226 u_int32_t prev_head_boundary, prev_cyl_boundary;
1227 u_int32_t adj_size, max_end;
1228 struct dos_partition *partp;
1230 while (1)
1232 part_processed = 1;
1233 if (command->n_args != 4)
1235 warnx("ERROR line %d: incorrect number of partition args",
1236 current_line_number);
1237 break;
1239 partition = command->args[0].arg_val;
1240 if (partition < 1 || partition > 4)
1242 warnx("ERROR line %d: invalid partition number %d",
1243 current_line_number, partition);
1244 break;
1246 partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1247 bzero((char *)partp, sizeof (struct dos_partition));
1248 partp->dp_typ = command->args[1].arg_val;
1249 partp->dp_start = command->args[2].arg_val;
1250 partp->dp_size = command->args[3].arg_val;
1251 max_end = partp->dp_start + partp->dp_size;
1253 if (partp->dp_typ == 0)
1256 * Get out, the partition is marked as unused.
1259 * Insure that it's unused.
1261 bzero((char *)partp, sizeof (struct dos_partition));
1262 status = 1;
1263 break;
1267 * Adjust start upwards, if necessary, to fall on an head boundary.
1269 if (partp->dp_start % dos_sectors != 0)
1271 prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1272 if (max_end < dos_sectors ||
1273 prev_head_boundary > max_end - dos_sectors)
1276 * Can't go past end of partition
1278 warnx(
1279 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1280 a head boundary",
1281 current_line_number, partition);
1282 break;
1284 warnx(
1285 "WARNING: adjusting start offset of partition %d\n\
1286 from %u to %u, to fall on a head boundary",
1287 partition, (u_int)partp->dp_start,
1288 (u_int)(prev_head_boundary + dos_sectors));
1289 partp->dp_start = prev_head_boundary + dos_sectors;
1293 * Adjust size downwards, if necessary, to fall on a cylinder
1294 * boundary.
1296 prev_cyl_boundary =
1297 ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1298 if (prev_cyl_boundary > partp->dp_start)
1299 adj_size = prev_cyl_boundary - partp->dp_start;
1300 else
1302 warnx(
1303 "ERROR: could not adjust partition to start on a head boundary\n\
1304 and end on a cylinder boundary.");
1305 return (0);
1307 if (adj_size != partp->dp_size)
1309 warnx(
1310 "WARNING: adjusting size of partition %d from %u to %u\n\
1311 to end on a cylinder boundary",
1312 partition, (u_int)partp->dp_size, (u_int)adj_size);
1313 partp->dp_size = adj_size;
1315 if (partp->dp_size == 0)
1317 warnx("ERROR line %d: size of partition %d is zero",
1318 current_line_number, partition);
1319 break;
1322 dos(partp);
1323 status = 1;
1324 break;
1326 return (status);
1330 static int
1331 process_active(CMD *command)
1333 int status = 0, partition, i;
1334 struct dos_partition *partp;
1336 while (1)
1338 active_processed = 1;
1339 if (command->n_args != 1)
1341 warnx("ERROR line %d: incorrect number of active args",
1342 current_line_number);
1343 status = 0;
1344 break;
1346 partition = command->args[0].arg_val;
1347 if (partition < 1 || partition > 4)
1349 warnx("ERROR line %d: invalid partition number %d",
1350 current_line_number, partition);
1351 break;
1354 * Reset active partition
1356 partp = ((struct dos_partition *) &mboot.parts);
1357 for (i = 0; i < NDOSPART; i++)
1358 partp[i].dp_flag = 0;
1359 partp[partition-1].dp_flag = ACTIVE;
1361 status = 1;
1362 break;
1364 return (status);
1368 static int
1369 process_line(char *line)
1371 CMD command;
1372 int status = 1;
1374 while (1)
1376 parse_config_line(line, &command);
1377 switch (command.cmd)
1379 case 0:
1381 * Comment or blank line
1383 break;
1384 case 'g':
1386 * Set geometry
1388 status = process_geometry(&command);
1389 break;
1390 case 'p':
1391 status = process_partition(&command);
1392 break;
1393 case 'a':
1394 status = process_active(&command);
1395 break;
1396 default:
1397 status = 0;
1398 break;
1400 break;
1402 return (status);
1406 static int
1407 read_config(char *config_file)
1409 FILE *fp = NULL;
1410 int status = 1;
1411 char buf[1010];
1413 while (1) /* dirty trick used to insure one exit point for this
1414 function */
1416 if (strcmp(config_file, "-") != 0)
1419 * We're not reading from stdin
1421 if ((fp = fopen(config_file, "r")) == NULL)
1423 status = 0;
1424 break;
1427 else
1429 fp = stdin;
1431 current_line_number = 0;
1432 while (!feof(fp))
1434 if (fgets(buf, sizeof(buf), fp) == NULL)
1436 break;
1438 ++current_line_number;
1439 status = process_line(buf);
1440 if (status == 0)
1442 break;
1445 break;
1447 if (fp)
1450 * It doesn't matter if we're reading from stdin, as we've reached EOF
1452 fclose(fp);
1454 return (status);
1458 static void
1459 reset_boot(void)
1461 int i;
1462 struct dos_partition *partp;
1464 init_boot();
1465 for (i = 0; i < 4; ++i)
1467 partp = ((struct dos_partition *) &mboot.parts) + i;
1468 bzero((char *)partp, sizeof (struct dos_partition));
1472 static int
1473 sanitize_partition(struct dos_partition *partp)
1475 u_int32_t prev_head_boundary, prev_cyl_boundary;
1476 u_int32_t max_end, size, start;
1478 start = partp->dp_start;
1479 size = partp->dp_size;
1480 max_end = start + size;
1481 /* Only allow a zero size if the partition is being marked unused. */
1482 if (size == 0) {
1483 if (start == 0 && partp->dp_typ == 0)
1484 return (1);
1485 warnx("ERROR: size of partition is zero");
1486 return (0);
1488 /* Return if no adjustment is necessary. */
1489 if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1490 return (1);
1492 if (start == 0) {
1493 warnx("WARNING: partition overlaps with partition table");
1494 if (ok("Correct this automatically?"))
1495 start = dos_sectors;
1497 if (start % dos_sectors != 0)
1498 warnx("WARNING: partition does not start on a head boundary");
1499 if ((start +size) % dos_sectors != 0)
1500 warnx("WARNING: partition does not end on a cylinder boundary");
1501 warnx("WARNING: this may confuse the BIOS or some operating systems");
1502 if (!ok("Correct this automatically?"))
1503 return (1);
1506 * Adjust start upwards, if necessary, to fall on an head boundary.
1508 if (start % dos_sectors != 0) {
1509 prev_head_boundary = start / dos_sectors * dos_sectors;
1510 if (max_end < dos_sectors ||
1511 prev_head_boundary >= max_end - dos_sectors) {
1513 * Can't go past end of partition
1515 warnx(
1516 "ERROR: unable to adjust start of partition to fall on a head boundary");
1517 return (0);
1519 start = prev_head_boundary + dos_sectors;
1523 * Adjust size downwards, if necessary, to fall on a cylinder
1524 * boundary.
1526 prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1527 if (prev_cyl_boundary > start)
1528 size = prev_cyl_boundary - start;
1529 else {
1530 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1531 and end on a cylinder boundary.");
1532 return (0);
1535 /* Finally, commit any changes to partp and return. */
1536 if (start != partp->dp_start) {
1537 warnx("WARNING: adjusting start offset of partition to %u",
1538 (u_int)start);
1539 partp->dp_start = start;
1541 if (size != partp->dp_size) {
1542 warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1543 partp->dp_size = size;
1546 return (1);