xref: /openbsd-src/sbin/fdisk/fdisk.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: fdisk.c,v 1.33 2001/07/02 13:51:17 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Tobias Weingartner
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *    This product includes software developed by Tobias Weingartner.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <paths.h>
39 #include <sys/types.h>
40 #include <sys/fcntl.h>
41 #include <sys/disklabel.h>
42 #include "disk.h"
43 #include "user.h"
44 
45 #define _PATH_MBR _PATH_BOOTDIR "mbr"
46 static unsigned char builtin_mbr[] = {
47 #include "mbrcode.h"
48 };
49 
50 
51 void
52 usage()
53 {
54 	extern char * __progname;
55 	fprintf(stderr, "usage: %s "
56 	    "[-ie] [-f mbrboot] [-c cyl -h head -s sect] disk\n"
57 	    "\t-i: initialize disk with virgin MBR\n"
58 	    "\t-e: edit MBRs on disk interactively\n"
59 	    "\t-f: specify non-standard MBR template\n"
60 	    "\t-chs: specify disk geometry\n"
61 	    "`disk' may be of the forms: sd0 or /dev/rsd0c.\n",
62 	    __progname);
63 	exit(1);
64 }
65 
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char **argv;
71 {
72 	int ch, fd;
73 	int i_flag = 0, m_flag = 0;
74 	int c_arg = 0, h_arg = 0, s_arg = 0;
75 	disk_t disk;
76 	DISK_metrics *usermetrics;
77 	char *mbrfile = _PATH_MBR;
78 	mbr_t mbr;
79 	char mbr_buf[DEV_BSIZE];
80 
81 	while ((ch = getopt(argc, argv, "ief:c:h:s:")) != -1) {
82 		switch(ch) {
83 		case 'i':
84 			i_flag = 1;
85 			break;
86 		case 'e':
87 			m_flag = 1;
88 			break;
89 		case 'f':
90 			mbrfile = optarg;
91 			break;
92 		case 'c':
93 			c_arg = atoi(optarg);
94 			if (c_arg < 1 || c_arg > 262144)
95 				errx(1, "Cylinder argument out of range.");
96 			break;
97 		case 'h':
98 			h_arg = atoi(optarg);
99 			if (h_arg < 1 || h_arg > 256)
100 				errx(1, "Head argument out of range.");
101 			break;
102 		case 's':
103 			s_arg = atoi(optarg);
104 			if (s_arg < 1 || s_arg > 63)
105 				errx(1, "Sector argument out of range.");
106 			break;
107 		default:
108 			usage();
109 		}
110 	}
111 	argc -= optind;
112 	argv += optind;
113 
114 	/* Argument checking */
115 	if (argc != 1)
116 		usage();
117 	else
118 		disk.name = argv[0];
119 
120 	/* Put in supplied geometry if there */
121 	if (c_arg | h_arg | s_arg) {
122 		usermetrics = malloc(sizeof(DISK_metrics));
123 		if (usermetrics != NULL) {
124 			if (c_arg && h_arg && s_arg) {
125 				usermetrics->cylinders = c_arg;
126 				usermetrics->heads = h_arg;
127 				usermetrics->sectors = s_arg;
128 				usermetrics->size = c_arg * h_arg * s_arg;
129 			} else
130 				errx(1, "Please specify a full geometry with [-chs].");
131 		}
132 	} else
133 		usermetrics = NULL;
134 
135 	/* Get the geometry */
136 	disk.real = NULL;
137 	if (DISK_getmetrics(&disk, usermetrics))
138 		errx(1, "Can't get disk geometry, please use [-chs] to specify.");
139 
140 
141 	/* Print out current MBRs on disk */
142 	if ((i_flag + m_flag) == 0)
143 		exit(USER_print_disk(&disk));
144 
145 	/* Parse mbr template, to pass on later */
146 	if ((fd = open(mbrfile, O_RDONLY)) == -1) {
147 		warn("Can not open MBR file");
148 		warnx("using builtin MBR");
149 		memcpy(mbr_buf, builtin_mbr, sizeof(mbr_buf));
150 	} else {
151 		MBR_read(fd, 0, mbr_buf);
152 		close(fd);
153 	}
154 	MBR_parse(&disk, mbr_buf, 0, 0, &mbr);
155 
156 	/* Punt if no i or m */
157 	if ((i_flag + m_flag) != 1)
158 		usage();
159 
160 	/* Now do what we are supposed to */
161 	if (i_flag)
162 		USER_init(&disk, &mbr);
163 
164 	if (m_flag)
165 		USER_modify(&disk, &mbr, 0, 0);
166 
167 	return (0);
168 }
169 
170