]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/fdisk.tproj/cmd.c
2 * Copyright (c) 2002-2005 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/fcntl.h>
68 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
71 Xerase(cmd
, disk
, mbr
, tt
, offset
)
78 bzero(mbr
->part
, sizeof(mbr
->part
));
79 mbr
->signature
= MBR_SIGNATURE
;
84 Xreinit(cmd
, disk
, mbr
, tt
, offset
)
91 /* Copy template MBR */
93 MBR_parse(disk
, offset
, 0, mbr
);
97 /* Tell em we did something */
98 printf("In memory copy is initialized to:\n");
99 printf("Offset: %d\t", offset
);
101 printf("Use 'write' to update disk.\n");
107 Xauto(cmd
, disk
, mbr
, tt
, offset
)
114 if (cmd
->args
[0] == '\0') {
115 printf("usage: auto <style>\n");
116 printf(" where style is one of:\n");
117 AUTO_print_styles(stdout
);
121 if (AUTO_init(disk
, cmd
->args
, mbr
) != AUTO_OK
) {
129 Xdisk(cmd
, disk
, mbr
, tt
, offset
)
140 /* Print out disk info */
141 DISK_printmetrics(disk
);
143 #if defined (__powerpc__) || defined (__mips__)
149 /* Ask for new info */
150 if (ask_yn("Change disk geometry?", 0)) {
151 disk
->real
->cylinders
= ask_num("BIOS Cylinders", ASK_DEC
,
152 disk
->real
->cylinders
, 1, maxcyl
, NULL
);
153 disk
->real
->heads
= ask_num("BIOS Heads", ASK_DEC
,
154 disk
->real
->heads
, 1, maxhead
, NULL
);
155 disk
->real
->sectors
= ask_num("BIOS Sectors", ASK_DEC
,
156 disk
->real
->sectors
, 1, maxsec
, NULL
);
158 disk
->real
->size
= disk
->real
->cylinders
* disk
->real
->heads
159 * disk
->real
->sectors
;
166 Xedit(cmd
, disk
, mbr
, tt
, offset
)
178 if (!isdigit(cmd
->args
[0])) {
179 printf("Invalid argument: %s <partition number>\n", cmd
->cmd
);
182 pn
= atoi(cmd
->args
) - 1;
184 if (pn
< 0 || pn
> 3) {
185 printf("Invalid partition number.\n");
189 /* Print out current table entry */
194 #define EDIT(p, f, v, n, m, h) \
195 if ((num = ask_num(p, f, v, n, m, h)) != v) \
199 /* Ask for partition type */
200 EDIT("Partition id ('0' to disable) ", ASK_HEX
, pp
->id
, 0, 0xFF, PRT_printall
);
202 /* Unused, so just zero out */
203 if (pp
->id
== DOSPTYP_UNUSED
) {
204 memset(pp
, 0, sizeof(*pp
));
205 printf("Partition %d is disabled.\n", pn
+ 1);
209 /* Change table entry */
210 if (ask_yn("Do you wish to edit in CHS mode?", 0)) {
211 int maxcyl
, maxhead
, maxsect
;
214 maxcyl
= disk
->real
->cylinders
- 1;
215 maxhead
= disk
->real
->heads
- 1;
216 maxsect
= disk
->real
->sectors
;
219 EDIT("BIOS Starting cylinder", ASK_DEC
, pp
->scyl
, 0, maxcyl
, NULL
);
220 EDIT("BIOS Starting head", ASK_DEC
, pp
->shead
, 0, maxhead
, NULL
);
221 EDIT("BIOS Starting sector", ASK_DEC
, pp
->ssect
, 1, maxsect
, NULL
);
222 EDIT("BIOS Ending cylinder", ASK_DEC
, pp
->ecyl
, 0, maxcyl
, NULL
);
223 EDIT("BIOS Ending head", ASK_DEC
, pp
->ehead
, 0, maxhead
, NULL
);
224 EDIT("BIOS Ending sector", ASK_DEC
, pp
->esect
, 1, maxsect
, NULL
);
225 /* Fix up off/size values */
226 PRT_fix_BN(disk
, pp
, pn
);
227 /* Fix up CHS values for LBA */
228 PRT_fix_CHS(disk
, pp
, pn
);
233 pp
->bs
= 63 + offset
;
235 if (mbr
->part
[pn
-1].id
!= 0) {
236 pp
->bs
= mbr
->part
[pn
-1].bs
+ mbr
->part
[pn
-1].ns
;
240 EDIT("Partition offset", ASK_DEC
, pp
->bs
, 0,
241 disk
->real
->size
, NULL
);
242 m
= MAX(pp
->ns
, disk
->real
->size
- pp
->bs
);
243 if ( m
> disk
->real
->size
- pp
->bs
) {
244 /* dont have default value extend beyond end of disk */
245 m
= disk
->real
->size
- pp
->bs
;
248 EDIT("Partition size", ASK_DEC
, pp
->ns
, 1,
251 /* Fix up CHS values */
252 PRT_fix_CHS(disk
, pp
, pn
);
259 Xsetpid(cmd
, disk
, mbr
, tt
, offset
)
271 if (!isdigit(cmd
->args
[0])) {
272 printf("Invalid argument: %s <partition number>\n", cmd
->cmd
);
275 pn
= atoi(cmd
->args
) - 1;
277 if (pn
< 0 || pn
> 3) {
278 printf("Invalid partition number.\n");
282 /* Print out current table entry */
287 #define EDIT(p, f, v, n, m, h) \
288 if ((num = ask_num(p, f, v, n, m, h)) != v) \
292 /* Ask for partition type */
293 EDIT("Partition id ('0' to disable) ", ASK_HEX
, pp
->id
, 0, 0xFF, PRT_printall
);
299 Xselect(cmd
, disk
, mbr
, tt
, offset
)
306 static int firstoff
= 0;
310 if (!isdigit(cmd
->args
[0])) {
311 printf("Invalid argument: %s <partition number>\n", cmd
->cmd
);
315 pn
= atoi(cmd
->args
) - 1;
316 if (pn
< 0 || pn
> 3) {
317 printf("Invalid partition number.\n");
321 off
= mbr
->part
[pn
].bs
;
324 if ((mbr
->part
[pn
].id
!= DOSPTYP_EXTEND
) &&
325 (mbr
->part
[pn
].id
!= DOSPTYP_EXTENDL
)) {
326 printf("Partition %d is not an extended partition.\n", pn
+ 1);
334 printf("Loop to offset 0! Not selected.\n");
337 printf("Selected extended partition %d\n", pn
+ 1);
338 printf("New MBR at offset %d.\n", off
);
341 /* Recursion is beautifull! */
342 USER_modify(disk
, tt
, off
, firstoff
);
347 Xprint(cmd
, disk
, mbr
, tt
, offset
)
355 DISK_printmetrics(disk
);
356 printf("Offset: %d\t", offset
);
363 Xwrite(cmd
, disk
, mbr
, tt
, offset
)
373 fd
= DISK_openshared(disk
->name
, O_RDWR
, &shared
);
375 if(!ask_yn("Device could not be accessed exclusively.\nA reboot will be needed for changes to take effect. OK?", 0)) {
377 printf("MBR unchanged\n");
382 printf("Writing MBR at offset %d.\n", offset
);
385 MBR_write(disk
, fd
, mbr
);
391 Xquit(cmd
, disk
, r
, tt
, offset
)
399 /* Nothing to do here */
404 Xabort(cmd
, disk
, mbr
, tt
, offset
)
419 Xexit(cmd
, disk
, mbr
, tt
, offset
)
427 /* Nothing to do here */
432 Xhelp(cmd
, disk
, mbr
, tt
, offset
)
439 cmd_table_t
*cmd_table
= cmd
->table
;
442 /* Hmm, print out cmd_table here... */
443 for (i
= 0; cmd_table
[i
].cmd
!= NULL
; i
++)
444 printf("\t%s\t\t%s\n", cmd_table
[i
].cmd
, cmd_table
[i
].help
);
449 Xupdate(cmd
, disk
, mbr
, tt
, offset
)
456 extern char *mbr_binary
;
458 memcpy(mbr
->code
, mbr_binary
, MBR_CODE_SIZE
);
459 printf("Machine code updated.\n");
464 Xflag(cmd
, disk
, mbr
, tt
, offset
)
473 /* Parse partition table entry number */
474 if (!isdigit(cmd
->args
[0])) {
475 printf("Invalid argument: %s <partition number>\n", cmd
->cmd
);
478 pn
= atoi(cmd
->args
) - 1;
480 if (pn
< 0 || pn
> 3) {
481 printf("Invalid partition number.\n");
485 /* Set active flag */
486 for (i
= 0; i
< 4; i
++) {
488 mbr
->part
[i
].flag
= DOSACTIVE
;
490 mbr
->part
[i
].flag
= 0x00;
493 printf("Partition %d marked active.\n", pn
+ 1);
498 Xmanual(cmd
, disk
, mbr
, tt
, offset
)
505 system("man 8 fdisk");