]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/fdisk.tproj/fdisk.c
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1997 Tobias Weingartner
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Tobias Weingartner.
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 #include <sys/types.h>
62 #include <sys/fcntl.h>
67 #define _PATH_MBR "/usr/standalone/i386/boot0"
72 extern char * __progname
;
73 fprintf(stderr
, "usage: %s "
74 "[-ieu] [-f mbrboot] [-c cyl -h head -s sect] [-S size] [-r] [-a style] disk\n"
75 "\t-i: initialize disk with new MBR\n"
76 "\t-u: update MBR code, preserve partition table\n"
77 "\t-e: edit MBRs on disk interactively\n"
78 "\t-f: specify non-standard MBR template\n"
79 "\t-chs: specify disk geometry\n"
80 "\t-S: specify disk size\n"
81 "\t-r: read partition specs from stdin (implies -i)\n"
82 "\t-a: auto-partition with the given style\n"
83 "\t-d: dump partition table\n"
84 "\t-y: don't ask any questions\n"
85 "\t-t: test if disk is partitioned\n"
86 "`disk' is of the form /dev/rdisk0.\n",
88 fprintf(stderr
, "auto-partition styles:\n");
89 AUTO_print_styles(stderr
);
93 char *mbr_binary
= NULL
;
100 int i_flag
= 0, m_flag
= 0, u_flag
= 0, r_flag
= 0, d_flag
= 0, y_flag
= 0, t_flag
= 0;
101 int c_arg
= 0, h_arg
= 0, s_arg
= 0;
103 int block_size_arg
= 0;
105 DISK_metrics
*usermetrics
;
106 char *mbrfile
= _PATH_MBR
;
108 char *auto_style
= NULL
;
110 while ((ch
= getopt(argc
, argv
, "ieuf:c:h:s:b:S:ra:dyt")) != -1) {
125 c_arg
= atoi(optarg
);
126 if (c_arg
< 1 || c_arg
> 262144)
127 errx(1, "Cylinder argument out of range.");
130 h_arg
= atoi(optarg
);
131 if (h_arg
< 1 || h_arg
> 256)
132 errx(1, "Head argument out of range.");
135 s_arg
= atoi(optarg
);
136 if (s_arg
< 1 || s_arg
> 63)
137 errx(1, "Sector argument out of range.");
140 block_size_arg
= atoi(optarg
);
141 if (block_size_arg
& (block_size_arg
- 1))
142 errx(1, "Block size argument not a power of two.");
143 if (block_size_arg
< 512 || block_size_arg
> 4096)
144 errx(1, "Block size argument out of range 512..4096.");
147 size_arg
= atoi(optarg
);
171 /* Argument checking */
177 if (i_flag
&& u_flag
) errx(1, "-i and -u cannot be specified simultaneously");
179 /* Put in supplied geometry if there */
180 if (c_arg
| h_arg
| s_arg
| size_arg
| block_size_arg
) {
181 usermetrics
= malloc(sizeof(DISK_metrics
));
182 if (usermetrics
!= NULL
) {
183 if (c_arg
&& h_arg
&& s_arg
) {
184 usermetrics
->cylinders
= c_arg
;
185 usermetrics
->heads
= h_arg
;
186 usermetrics
->sectors
= s_arg
;
188 usermetrics
->size
= size_arg
;
190 usermetrics
->size
= c_arg
* h_arg
* s_arg
;
194 usermetrics
->size
= size_arg
;
195 DISK_fake_CHS(usermetrics
);
197 errx(1, "Please specify a full geometry with [-chs].");
200 if (block_size_arg
) {
201 usermetrics
->sector_size
= block_size_arg
;
203 DISK_get_sector_size(&disk
, usermetrics
);
210 /* Get the geometry */
212 if (DISK_getmetrics(&disk
, usermetrics
))
213 errx(1, "Can't get disk geometry, please use [-chs] to specify.");
215 /* If only testing, read MBR and silently exit */
219 mp
= mbr
= MBR_read_all(&disk
);
221 if (mp
->signature
!= MBR_SIGNATURE
) {
231 /* If not editing the disk, print out current MBRs on disk */
232 if ((i_flag
+ r_flag
+ u_flag
+ m_flag
) == 0) {
233 exit(USER_print_disk(&disk
, d_flag
));
236 /* Parse mbr template or read partition specs, to pass on later */
237 if (auto_style
&& r_flag
) {
238 errx(1, "Can't specify both -r and -a");
241 mbr_binary
= (char *)malloc(MBR_CODE_SIZE
);
242 if ((fd
= open(mbrfile
, O_RDONLY
)) == -1) {
243 warn("could not open MBR file %s", mbrfile
);
244 bzero(mbr_binary
, MBR_CODE_SIZE
);
247 cc
= read(fd
, mbr_binary
, MBR_CODE_SIZE
);
248 if (cc
< MBR_CODE_SIZE
) {
249 err(1, "could not read MBR code");
255 /* Don't hose the partition table; just write the boot code */
256 mp
= MBR_read_all(&disk
);
257 bcopy(mbr_binary
, mp
->code
, MBR_CODE_SIZE
);
260 /* If they didn't specify -a, they'll get the default auto style */
261 mp
= MBR_alloc(NULL
);
262 if (AUTO_init(&disk
, auto_style
, mp
) != AUTO_OK
) {
263 errx(1, "error initializing disk");
265 bcopy(mbr_binary
, mp
->code
, MBR_CODE_SIZE
);
268 mp
= MBR_parse_spec(stdin
, &disk
);
269 bcopy(mbr_binary
, mp
->code
, MBR_CODE_SIZE
);
272 /* Use what's on the disk. */
273 mp
= MBR_read_all(&disk
);
276 /* Now do what we are supposed to */
277 if (i_flag
|| r_flag
|| u_flag
) {
278 USER_write(&disk
, mp
, u_flag
, y_flag
);
282 USER_modify(&disk
, mp
, 0, 0);