]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/fdisk.tproj/user.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.
60 #include <sys/fcntl.h>
61 #include <sys/types.h>
63 #include <machine/param.h>
71 /* Our command table */
72 static cmd_table_t cmd_table
[] = {
73 {"help", Xhelp
, "Command help list"},
74 {"manual", Xmanual
, "Show entire man page for fdisk"},
75 {"reinit", Xreinit
, "Re-initialize loaded MBR (to defaults)"},
76 {"auto", Xauto
, "Auto-partition the disk with a partition style"},
77 {"setpid", Xsetpid
, "Set the identifier of a given table entry"},
78 {"disk", Xdisk
, "Edit current drive stats"},
79 {"edit", Xedit
, "Edit given table entry"},
80 {"erase", Xerase
, "Erase current MBR"},
81 {"flag", Xflag
, "Flag given table entry as bootable"},
82 {"update", Xupdate
, "Update machine code in loaded MBR"},
83 {"select", Xselect
, "Select extended partition table entry MBR"},
84 {"print", Xprint
, "Print loaded MBR partition table"},
85 {"write", Xwrite
, "Write loaded MBR to disk"},
86 {"exit", Xexit
, "Exit edit of current MBR, without saving changes"},
87 {"quit", Xquit
, "Quit edit of current MBR, saving current changes"},
88 {"abort", Xabort
, "Abort program without saving current changes"},
94 USER_write(disk
, tt
, preserve
, force
)
96 mbr_t
*tt
; /* Template MBR to write */
97 int preserve
; /* Preserve partition table and just write boot code */
98 int force
; /* Don't ask any questions */
101 char *msgp
= "\nDo you wish to write new MBR?";
102 char *msgk
= "\nDo you wish to write new MBR and partition table?";
109 "\t-----------------------------------------------------\n"
110 "\t------ ATTENTION - UPDATING MASTER BOOT RECORD ------\n"
111 "\t-----------------------------------------------------\n");
113 yn
= ask_yn(msgp
, 0);
115 yn
= ask_yn(msgk
, 0);
121 /* Only write the first one, if there's more than one in an extended partition chain */
122 /* Since we're updating boot code, we don't require exclusive access */
123 fd
= DISK_openshared(disk
->name
, O_RDWR
, &shared
);
125 MBR_write(disk
, fd
, tt
);
128 MBR_write_all(disk
, tt
);
131 printf("MBR is unchanged\n");
139 USER_modify(disk
, tt
, offset
, reloff
)
145 static int editlevel
;
151 /* One level deeper */
154 /* Set up command table pointer */
155 cmd
.table
= cmd_table
;
157 /* Read MBR & partition */
158 mbr
= MBR_alloc(NULL
);
159 fd
= DISK_open(disk
->name
, O_RDONLY
);
160 MBR_read(disk
, fd
, offset
, mbr
);
163 /* Parse the sucker */
164 MBR_parse(disk
, offset
, reloff
, mbr
);
166 if (mbr
->signature
!= MBR_SIGNATURE
) {
167 int yn
= ask_yn("The signature for this MBR is invalid.\nWould you like to initialize the partition table?", 1);
169 strlcpy(cmd
.cmd
, "erase", sizeof(cmd
.cmd
));
171 st
= Xerase(&cmd
, disk
, mbr
, tt
, offset
);
176 printf("Enter 'help' for information\n");
181 printf("fdisk:%c%d> ", (modified
)?'*':' ', editlevel
);
185 if (cmd
.cmd
[0] == '\0')
187 for (i
= 0; cmd_table
[i
].cmd
!= NULL
; i
++)
188 if (strstr(cmd_table
[i
].cmd
, cmd
.cmd
)==cmd_table
[i
].cmd
)
191 /* Quick hack to put in '?' == 'help' */
192 if (!strcmp(cmd
.cmd
, "?"))
195 /* Check for valid command */
196 if (cmd_table
[i
].cmd
== NULL
) {
197 printf("Invalid command '%s'. Try 'help'.\n", cmd
.cmd
);
200 strlcpy(cmd
.cmd
, cmd_table
[i
].cmd
, sizeof(cmd
.cmd
));
203 st
= cmd_table
[i
].fcn(&cmd
, disk
, mbr
, tt
, offset
);
218 if (st
== CMD_SAVE
) {
220 printf("Writing current MBR to disk.\n");
221 fd
= DISK_openshared(disk
->name
, O_RDWR
, &shared
);
223 if(!ask_yn("Device could not be accessed exclusively.\nA reboot will be needed for changes to take effect. OK?", 0)) {
230 MBR_write(disk
, fd
, mbr
);
233 int yn
= ask_yn("MBR was modified; really quit without saving?", 0);
235 printf("Aborting changes to current MBR.\n");
251 USER_print_disk(disk
, do_dump
)
255 int fd
, offset
, firstoff
;
258 fd
= DISK_open(disk
->name
, O_RDONLY
);
259 offset
= firstoff
= 0;
262 DISK_printmetrics(disk
);
264 mbr
= MBR_read_all(disk
);
271 return (DISK_close(fd
));