1 /* $OpenBSD: fdisk.c,v 1.43 2006/07/27 04:53:27 ray 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <err.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <paths.h>
34 #include <sys/types.h>
35 #include <sys/fcntl.h>
36 #include <sys/disklabel.h>
37 #include "disk.h"
38 #include "user.h"
39
40 #define _PATH_MBR _PATH_BOOTDIR "mbrmgr"
41 static unsigned char builtin_mbr[] = {
42 #include "mbrcode.h"
43 };
44
45 __RCSID("$MirOS: src/sbin/fdisk/fdisk.c,v 1.5 2012/10/17 16:44:47 tg Exp $");
46
47 static __dead void
usage(void)48 usage(void)
49 {
50 extern char * __progname;
51
52 fprintf(stderr, "usage: %s "
53 "[-ieu] [-c cylinders -h heads -s sectors] [-f mbrfile] device\n"
54 "\t-i: initialise disk with virgin MBR\n"
55 "\t-u: update MBR code, preserve partition table\n"
56 "\t-e: edit MBRs on disk interactively\n"
57 "\t-f: specify non-standard MBR template\n"
58 "\t-chs: specify disk geometry\n"
59 "`disk' may be of the forms: sd0 or /dev/rsd0c.\n",
60 __progname);
61 exit(1);
62 }
63
64
65 int
main(int argc,char * argv[])66 main(int argc, char *argv[])
67 {
68 int ch, fd;
69 int i_flag = 0, m_flag = 0, u_flag = 0;
70 int c_arg = 0, h_arg = 0, s_arg = 0;
71 disk_t disk;
72 DISK_metrics *usermetrics;
73 #if defined(__amd64__) || defined(__i386__) || defined (__powerpc__)
74 const char *mbrfile = _PATH_MBR;
75 #else
76 const char *mbrfile = NULL;
77 #endif
78 mbr_t mbr;
79 char mbr_buf[DEV_BSIZE];
80
81 while ((ch = getopt(argc, argv, "ieuf:c:h:s:")) != -1) {
82 const char *errstr;
83
84 switch(ch) {
85 case 'i':
86 i_flag = 1;
87 break;
88 case 'u':
89 u_flag = 1;
90 break;
91 case 'e':
92 m_flag = 1;
93 break;
94 case 'f':
95 mbrfile = optarg;
96 break;
97 case 'c':
98 c_arg = strtonum(optarg, 1, 2097151, &errstr);
99 if (errstr)
100 errx(1, "Cylinder argument %s [1..2097151].",
101 errstr);
102 break;
103 case 'h':
104 h_arg = strtonum(optarg, 1, 256, &errstr);
105 if (errstr)
106 errx(1, "Head argument %s [1..256].", errstr);
107 break;
108 case 's':
109 s_arg = strtonum(optarg, 1, 63, &errstr);
110 if (errstr)
111 errx(1, "Sector argument %s [1..63].", errstr);
112 break;
113 default:
114 usage();
115 }
116 }
117 argc -= optind;
118 argv += optind;
119
120 /* Argument checking */
121 if (argc != 1)
122 usage();
123 else
124 disk.name = argv[0];
125
126 /* Put in supplied geometry if there */
127 if (c_arg | h_arg | s_arg) {
128 usermetrics = malloc(sizeof(DISK_metrics));
129 if (usermetrics != NULL) {
130 if (c_arg && h_arg && s_arg) {
131 usermetrics->cylinders = c_arg;
132 usermetrics->heads = h_arg;
133 usermetrics->sectors = s_arg;
134 usermetrics->size = c_arg * h_arg * s_arg;
135 } else
136 errx(1, "Please specify a full geometry with [-chs].");
137 }
138 } else
139 usermetrics = NULL;
140
141 /* Get the geometry */
142 disk.real = NULL;
143 if (DISK_getmetrics(&disk, usermetrics))
144 errx(1, "Can't get disk geometry, please use [-chs] to specify.");
145
146
147 /* Print out current MBRs on disk */
148 if ((i_flag + u_flag + m_flag) == 0)
149 exit(USER_print_disk(&disk));
150
151 /* Parse mbr template, to pass on later */
152 if (mbrfile != NULL && (fd = open(mbrfile, O_RDONLY)) == -1) {
153 warn("%s", mbrfile);
154 warnx("using builtin MBR");
155 mbrfile = NULL;
156 }
157 if (mbrfile == NULL) {
158 memcpy(mbr_buf, builtin_mbr, sizeof(mbr_buf));
159 } else {
160 MBR_read(fd, 0, mbr_buf);
161 close(fd);
162 }
163 MBR_parse(&disk, mbr_buf, 0, 0, &mbr);
164
165 /* Now do what we are supposed to */
166 if (i_flag || u_flag)
167 if (USER_init(&disk, &mbr, u_flag) == -1)
168 err(1, "error initializing MBR");
169
170 if (m_flag)
171 USER_modify(&disk, &mbr, 0, 0);
172
173 return (0);
174 }
175