2 * Mach Operating System
3 * Copyright (c) 1992 Carnegie Mellon University
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>
46 static char lbuf
[LBUF
];
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
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 */
72 "/dev/ad0", "/dev/da0", "/dev/vkd0", 0
75 int cyls
, sectors
, heads
, cylsecs
, disksecs
;
79 unsigned char padding
[2]; /* force the longs to be long aligned */
80 unsigned char *bootinst
; /* boot code */
82 struct dos_partition parts
[4];
84 struct mboot mboot
= {{0}, NULL
, 0};
87 #define BOOT_MAGIC 0xAA55
94 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
95 #define DOSCYL(c) (c & 0xff)
97 static int partition
= -1;
102 static int current_line_number
;
104 static int geom_processed
= 0;
105 static int part_processed
= 0;
106 static int active_processed
= 0;
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 */
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"}
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"}
162 ,{0x3C, "PartitionMagic recovery"}
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"}
172 ,{0x52, "CP/M or Microport SysV/AT"}
175 ,{0x55, "EZ-Drive (disk manager)"}
177 ,{0x5C, "Priam Edisk (disk manager)"} /* according to S. Widlake */
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"}
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"}
198 ,{0xA0, "Suspend to Disk"}
199 ,{0xA5, "DragonFly/FreeBSD/NetBSD/386BSD"}
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)"}
211 ,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
215 ,{0xEB, "BeOS file system"}
217 ,{0xEF, "EFI System Partition"}
219 ,{0xF2, "DOS 3.3+ Secondary"}
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);
249 static int hex(char *str
, int *num
, int deflt
);
250 static int string(char *str
, char **ans
);
255 main(int argc
, char *argv
[])
259 while ((c
= getopt(argc
, argv
, "BCIab:f:p:istuv1234")) != -1)
307 if (f_flag
|| i_flag
)
315 static char realname
[12];
317 if(strncmp(argv
[0], "/dev", 4) == 0) {
320 snprintf(realname
, 12, "/dev/%s", argv
[0]);
324 if (open_disk(u_flag
) < 0)
325 err(1, "cannot open disk %s", disk
);
326 } else if (disk
== NULL
) {
329 for(i
= 0; disks
[i
]; i
++)
332 rv
= open_disk(u_flag
);
336 err(1, "cannot open any disk");
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
;
352 struct dos_partition
*partp
;
356 printf("%s: %d cyl %d hd %d sec\n", disk
, dos_cyls
, dos_heads
,
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)
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
,
371 printf("******* Working on device %s *******\n",disk
);
375 struct dos_partition
*partp
;
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
-
394 if (read_s0() || i_flag
)
399 if (!read_config(f_flag
))
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");
430 for (i
= 1; i
<= NDOSPART
; i
++)
433 change_part(partition
);
435 if (u_flag
|| a_flag
)
436 change_active(partition
);
441 if (u_flag
|| a_flag
|| B_flag
) {
443 printf("\nWe haven't changed the partition table yet. ");
444 printf("This is your last chance.\n");
448 if (ok("Should we write new partition table?"))
453 printf("\n-t flag specified -- partition table not written.\n");
464 fprintf(stderr
, "%s%s",
465 "usage: fdisk [-BCIaistu] [-b bootcode] [-p diskimage] [-1234] [disk]\n",
466 " fdisk -f configfile [-itv] [disk]\n");
476 printf("Information from DOS bootblock is:\n");
478 for (i
= 1; i
<= NDOSPART
; i
++)
479 printf("%d: ", i
), print_part(i
);
484 static struct dos_partition mtpart
= { 0 };
489 struct dos_partition
*partp
;
492 partp
= ((struct dos_partition
*) &mboot
.parts
) + i
- 1;
494 if (!bcmp(partp
, &mtpart
, sizeof (struct dos_partition
))) {
495 printf("<UNUSED>\n");
499 * Be careful not to overflow.
501 part_mb
= partp
->dp_size
;
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
,
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
)
514 ,DPSECT(partp
->dp_ssect
)
515 ,DPCYL(partp
->dp_ecyl
, partp
->dp_esect
)
517 ,DPSECT(partp
->dp_esect
));
528 fname
= b_flag
? b_flag
: "/boot/mbr";
529 if ((fd
= open(fname
, O_RDONLY
)) == -1 ||
530 fstat(fd
, &sb
) == -1)
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 ||
541 if (n
!= mboot
.bootinst_size
)
542 errx(1, "%s: short read", fname
);
547 init_sector0(unsigned long start
)
549 struct dos_partition
*partp
= (struct dos_partition
*) (&mboot
.parts
[3]);
553 partp
->dp_typ
= DOSPTYP_386BSD
;
554 partp
->dp_flag
= ACTIVE
;
555 start
= ((start
+ dos_sectors
- 1) / dos_sectors
) * dos_sectors
;
558 partp
->dp_start
= start
;
559 partp
->dp_size
= (disksecs
/ dos_cylsecs
) * dos_cylsecs
- start
;
567 struct dos_partition
*partp
= ((struct dos_partition
*) &mboot
.parts
) + i
- 1;
569 printf("The data for partition %d is:\n", i
);
572 if (u_flag
&& ok("Do you want to change it?")) {
576 bzero((char *)partp
, sizeof (struct dos_partition
));
579 printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
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");
593 if (ok("Explicitly specify beg/end address ?"))
596 tcyl
= DPCYL(partp
->dp_scyl
,partp
->dp_ssect
);
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");
605 partp
->dp_ssect
= -1;
608 partp
->dp_scyl
= DOSCYL(tcyl
);
609 partp
->dp_ssect
= DOSSECT(tsec
,tcyl
);
613 tcyl
= DPCYL(partp
->dp_ecyl
,partp
->dp_esect
);
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");
622 partp
->dp_esect
= -1;
625 partp
->dp_ecyl
= DOSCYL(tcyl
);
626 partp
->dp_esect
= DOSSECT(tsec
,tcyl
);
633 } while (!ok("Are we happy with this entry?"));
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
);
651 change_active(int which
)
653 struct dos_partition
*partp
= &mboot
.parts
[0];
654 int active
, i
, new, tmp
;
657 for (i
= 0; i
< NDOSPART
; i
++) {
658 if ((partp
[i
].dp_flag
& ACTIVE
) == 0)
660 printf("Partition %d is marked active\n", i
+ 1);
664 if (a_flag
&& which
!= -1)
666 else if (active
== -1)
669 if (!ok("Do you want to change the active partition?"))
674 Decimal("active partition", new, tmp
);
675 if (new < 1 || new > 4) {
676 printf("Active partition number must be in range 1-4."
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
;
691 if (ok("Do you want to change the boot code?"))
696 get_params_to_use(void)
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
;
710 while(!ok("Are you happy with this choice"));
715 /***********************************************\
716 * Change real numbers into strange dos numbers *
717 \***********************************************/
719 dos(struct dos_partition
*partp
)
724 if (partp
->dp_typ
== 0 && partp
->dp_start
== 0 && partp
->dp_size
== 0) {
725 memcpy(partp
, &mtpart
, sizeof(*partp
));
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");
736 partp
->dp_ssect
= -1;
738 partp
->dp_shd
= partp
->dp_start
% dos_cylsecs
/ dos_sectors
;
739 partp
->dp_scyl
= DOSCYL(cy
);
740 partp
->dp_ssect
= DOSSECT(sec
, cy
);
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");
751 partp
->dp_esect
= -1;
753 partp
->dp_ehd
= end
% dos_cylsecs
/ dos_sectors
;
754 partp
->dp_ecyl
= DOSCYL(cy
);
755 partp
->dp_esect
= DOSSECT(sec
, cy
);
761 /* Getting device status */
764 open_disk(int u_flag
)
768 if (stat(disk
, &st
) == -1) {
771 warnx("can't get file status of %s", disk
);
774 if ( !(st
.st_mode
& S_IFCHR
) && p_flag
== 0 )
775 warnx("device %s is not character special", disk
);
777 a_flag
|| I_flag
|| B_flag
|| u_flag
? O_RDWR
: O_RDONLY
)) == -1) {
780 warnx("can't open device %s", disk
);
783 if (get_params() == -1) {
784 warnx("can't get disk parameters on %s", disk
);
791 read_disk(off_t sector
, void *buf
)
793 lseek(fd
,(sector
* 512), 0);
795 for( secsize
= MIN_SEC_SIZE
; secsize
<= MAX_SEC_SIZE
; secsize
*= 2 )
798 int size
= read(fd
, buf
, secsize
);
799 if( size
== secsize
)
800 /* it worked so return */
804 return read( fd
, buf
, secsize
);
806 /* we failed to read at any of the sizes */
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
);
821 struct partinfo partinfo
; /* disk parameters */
824 if (ioctl(fd
, DIOCGPART
, &partinfo
) == -1) {
825 if (p_flag
&& fstat(fd
, &st
) == 0 && st
.st_size
) {
828 cylsecs
= heads
* sectors
;
829 cyls
= st
.st_size
/ 512 / cylsecs
;
831 warnx("can't get disk parameters on %s; supplying dummy ones",
834 cylsecs
= heads
* sectors
;
837 cyls
= partinfo
.d_ncylinders
;
838 heads
= partinfo
.d_nheads
;
839 sectors
= partinfo
.d_secpertrack
;
840 cylsecs
= heads
* sectors
;
841 secsize
= partinfo
.media_blksize
;
845 dos_sectors
= sectors
;
846 dos_cylsecs
= cylsecs
;
847 disksecs
= cyls
* heads
* sectors
;
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 "
863 if (read_disk(0, mboot
.bootinst
) == -1) {
864 warnx("can't read fdisk partition table");
867 if (*(uint16_t *)&mboot
.bootinst
[MBRSIGOFF
] != BOOT_MAGIC
) {
868 warnx("invalid fdisk partition table found");
869 /* So should we initialize things */
872 memcpy(mboot
.parts
, &mboot
.bootinst
[DOSPARTOFF
], sizeof(mboot
.parts
));
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)
897 if (ioctl(fd
, DIOCWLABEL
, &flag
) < 0)
898 warn("ioctl DIOCWLABEL");
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");
907 ioctl(fd
, DIOCWLABEL
, &flag
);
912 ioctl(fd
, DIOCWLABEL
, &flag
);
921 printf("%s [n] ", str
);
923 if (fgets(lbuf
, LBUF
, stdin
) == NULL
)
925 lbuf
[strlen(lbuf
)-1] = 0;
928 (!strcmp(lbuf
, "yes") || !strcmp(lbuf
, "YES") ||
929 !strcmp(lbuf
, "y") || !strcmp(lbuf
, "Y")))
936 decimal(char *str
, int *num
, int deflt
)
942 printf("Supply a decimal value for \"%s\" [%d] ", str
, deflt
);
944 if (fgets(lbuf
, LBUF
, stdin
) == NULL
)
946 lbuf
[strlen(lbuf
)-1] = 0;
952 while ((c
= *cp
) && (c
== ' ' || c
== '\t')) cp
++;
955 while ((c
= *cp
++)) {
956 if (c
<= '9' && c
>= '0')
957 acc
= acc
* 10 + c
- '0';
961 if (c
== ' ' || c
== '\t')
962 while ((c
= *cp
) && (c
== ' ' || c
== '\t')) cp
++;
967 printf("%s is an invalid decimal number. Try again.\n",
975 hex(char *str
, int *num
, int deflt
)
981 printf("Supply a hex value for \"%s\" [%x] ", str
, deflt
);
982 fgets(lbuf
, LBUF
, stdin
);
983 lbuf
[strlen(lbuf
)-1] = 0;
989 while ((c
= *cp
) && (c
== ' ' || c
== '\t')) cp
++;
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;
1002 if (c
== ' ' || c
== '\t')
1003 while ((c
= *cp
) && (c
== ' ' || c
== '\t')) cp
++;
1008 printf("%s is an invalid hex number. Try again.\n",
1015 string(char *str
, char **ans
)
1021 printf("Supply a string value for \"%s\" [%s] ", str
, *ans
);
1022 fgets(lbuf
, LBUF
, stdin
);
1023 lbuf
[strlen(lbuf
)-1] = 0;
1028 while ((c
= *cp
) && (c
== ' ' || c
== '\t')) cp
++;
1032 while ((c
= *cp
) && c
!= '"') cp
++;
1035 while ((c
= *cp
) && c
!= ' ' && c
!= '\t') cp
++;
1048 int numentries
= (sizeof(part_types
)/sizeof(struct part_type
));
1050 struct part_type
*ptr
= part_types
;
1053 while(counter
< numentries
)
1055 if(ptr
->type
== type
)
1067 parse_config_line(char *line
, CMD
*command
)
1072 while (1) /* dirty trick used to insure one exit point for this
1075 memset(command
, 0, sizeof(*command
));
1077 while (isspace(*cp
)) ++cp
;
1078 if (*cp
== '\0' || *cp
== '#')
1082 command
->cmd
= *cp
++;
1089 while (isspace(*cp
)) ++cp
;
1092 break; /* found comment */
1096 command
->args
[command
->n_args
].argtype
= *cp
++;
1100 break; /* assume end of line */
1103 command
->args
[command
->n_args
].arg_val
= strtol(cp
, &end
, 0);
1106 break; /* couldn't parse number */
1117 process_geometry(CMD
*command
)
1127 "ERROR line %d: the geometry specification line must occur before\n\
1128 all partition specifications",
1129 current_line_number
);
1133 if (command
->n_args
!= 3)
1135 warnx("ERROR line %d: incorrect number of geometry args",
1136 current_line_number
);
1143 for (i
= 0; i
< 3; ++i
)
1145 switch (command
->args
[i
].argtype
)
1148 dos_cyls
= command
->args
[i
].arg_val
;
1151 dos_heads
= command
->args
[i
].arg_val
;
1154 dos_sectors
= command
->args
[i
].arg_val
;
1158 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1159 current_line_number
, command
->args
[i
].argtype
,
1160 command
->args
[i
].argtype
);
1170 dos_cylsecs
= dos_heads
* dos_sectors
;
1173 * Do sanity checks on parameter values
1177 warnx("ERROR line %d: number of cylinders not specified",
1178 current_line_number
);
1181 if (dos_cyls
== 0 || dos_cyls
> 1024)
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
);
1192 warnx("ERROR line %d: number of heads not specified",
1193 current_line_number
);
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
);
1203 if (dos_sectors
< 0)
1205 warnx("ERROR line %d: number of sectors not specified",
1206 current_line_number
);
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
);
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
;
1233 if (command
->n_args
!= 4)
1235 warnx("ERROR line %d: incorrect number of partition args",
1236 current_line_number
);
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
);
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
));
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
1279 "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1281 current_line_number
, partition
);
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
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
;
1303 "ERROR: could not adjust partition to start on a head boundary\n\
1304 and end on a cylinder boundary.");
1307 if (adj_size
!= partp
->dp_size
)
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
);
1331 process_active(CMD
*command
)
1333 int status
= 0, partition
, i
;
1334 struct dos_partition
*partp
;
1338 active_processed
= 1;
1339 if (command
->n_args
!= 1)
1341 warnx("ERROR line %d: incorrect number of active args",
1342 current_line_number
);
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
);
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
;
1369 process_line(char *line
)
1376 parse_config_line(line
, &command
);
1377 switch (command
.cmd
)
1381 * Comment or blank line
1388 status
= process_geometry(&command
);
1391 status
= process_partition(&command
);
1394 status
= process_active(&command
);
1407 read_config(char *config_file
)
1413 while (1) /* dirty trick used to insure one exit point for this
1416 if (strcmp(config_file
, "-") != 0)
1419 * We're not reading from stdin
1421 if ((fp
= fopen(config_file
, "r")) == NULL
)
1431 current_line_number
= 0;
1434 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
1438 ++current_line_number
;
1439 status
= process_line(buf
);
1450 * It doesn't matter if we're reading from stdin, as we've reached EOF
1462 struct dos_partition
*partp
;
1465 for (i
= 0; i
< 4; ++i
)
1467 partp
= ((struct dos_partition
*) &mboot
.parts
) + i
;
1468 bzero((char *)partp
, sizeof (struct dos_partition
));
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. */
1483 if (start
== 0 && partp
->dp_typ
== 0)
1485 warnx("ERROR: size of partition is zero");
1488 /* Return if no adjustment is necessary. */
1489 if (start
% dos_sectors
== 0 && (start
+ size
) % dos_sectors
== 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?"))
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
1516 "ERROR: unable to adjust start of partition to fall on a head boundary");
1519 start
= prev_head_boundary
+ dos_sectors
;
1523 * Adjust size downwards, if necessary, to fall on a cylinder
1526 prev_cyl_boundary
= ((start
+ size
) / dos_cylsecs
) * dos_cylsecs
;
1527 if (prev_cyl_boundary
> start
)
1528 size
= prev_cyl_boundary
- start
;
1530 warnx("ERROR: could not adjust partition to start on a head boundary\n\
1531 and end on a cylinder boundary.");
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",
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
;