xref: /openbsd-src/sbin/fdisk/user.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: user.c,v 1.23 2006/07/27 04:06:13 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 <errno.h>
30 #include <util.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/fcntl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/disklabel.h>
38 #include <machine/param.h>
39 #include "user.h"
40 #include "disk.h"
41 #include "misc.h"
42 #include "mbr.h"
43 #include "cmd.h"
44 
45 
46 /* Our command table */
47 static cmd_table_t cmd_table[] = {
48 	{"help",   Xhelp,	"Command help list"},
49 	{"manual", Xmanual,	"Show entire OpenBSD man page for fdisk"},
50 	{"reinit", Xreinit,	"Re-initialize loaded MBR (to defaults)"},
51 	{"setpid", Xsetpid,	"Set the identifier of a given table entry"},
52 	{"disk",   Xdisk,	"Edit current drive stats"},
53 	{"edit",   Xedit,	"Edit given table entry"},
54 	{"flag",   Xflag,	"Flag given table entry as bootable"},
55 	{"update", Xupdate,	"Update machine code in loaded MBR"},
56 	{"select", Xselect,	"Select extended partition table entry MBR"},
57 	{"swap",   Xswap,	"Swap two partition entries"},
58 	{"print",  Xprint,	"Print loaded MBR partition table"},
59 	{"write",  Xwrite,	"Write loaded MBR to disk"},
60 	{"exit",   Xexit,	"Exit edit of current MBR, without saving changes"},
61 	{"quit",   Xquit,	"Quit edit of current MBR, saving current changes"},
62 	{"abort",  Xabort,	"Abort program without saving current changes"},
63 	{NULL,     NULL,	NULL}
64 };
65 
66 
67 int
68 USER_init(disk_t *disk, mbr_t *tt, int preserve)
69 {
70 	int fd, yn;
71 	char mbr_buf[DEV_BSIZE];
72 	char *msgp = "\nDo you wish to write new MBR?";
73 	char *msgk = "\nDo you wish to write new MBR and partition table?";
74 
75 	if (preserve)
76 		MBR_pcopy(disk, tt);
77 	else
78 		MBR_init(disk, tt);
79 
80 	/* Write sector 0 */
81 	printf("\a\n"
82 	   "\t-----------------------------------------------------\n"
83 	   "\t------ ATTENTION - UPDATING MASTER BOOT RECORD ------\n"
84 	   "\t-----------------------------------------------------\n");
85 	if (preserve)
86 		yn = ask_yn(msgp);
87 	else
88 		yn = ask_yn(msgk);
89 
90 	if (yn) {
91 		fd = DISK_open(disk->name, O_RDWR);
92 		MBR_make(tt, mbr_buf);
93 		if (MBR_write(fd, 0, mbr_buf) == -1) {
94 			int saved_errno = errno;
95 			DISK_close(fd);
96 			errno = saved_errno;
97 			return (-1);
98 		}
99 		DISK_close(fd);
100 	} else
101 		printf("MBR is unchanged\n");
102 
103 	return (0);
104 }
105 
106 int modified;
107 
108 int
109 USER_modify(disk_t *disk, mbr_t *tt, off_t offset, off_t reloff)
110 {
111 	static int editlevel;
112 	char mbr_buf[DEV_BSIZE];
113 	mbr_t mbr;
114 	cmd_t cmd;
115 	int i, st, fd;
116 
117 	/* One level deeper */
118 	editlevel += 1;
119 
120 	/* Set up command table pointer */
121 	cmd.table = cmd_table;
122 
123 	/* Read MBR & partition */
124 	fd = DISK_open(disk->name, O_RDONLY);
125 	MBR_read(fd, offset, mbr_buf);
126 	DISK_close(fd);
127 
128 	/* Parse the sucker */
129 	MBR_parse(disk, mbr_buf, offset, reloff, &mbr);
130 
131 	printf("Enter 'help' for information\n");
132 
133 	/* Edit cycle */
134 	do {
135 again:
136 		printf("fdisk:%c%d> ", (modified)?'*':' ', editlevel);
137 		fflush(stdout);
138 		ask_cmd(&cmd);
139 
140 		if (cmd.cmd[0] == '\0')
141 			goto again;
142 		for (i = 0; cmd_table[i].cmd != NULL; i++)
143 			if (strstr(cmd_table[i].cmd, cmd.cmd)==cmd_table[i].cmd)
144 				break;
145 
146 		/* Quick hack to put in '?' == 'help' */
147 		if (!strcmp(cmd.cmd, "?"))
148 			i = 0;
149 
150 		/* Check for valid command */
151 		if (cmd_table[i].cmd == NULL) {
152 			printf("Invalid command '%s'.  Try 'help'.\n", cmd.cmd);
153 			continue;
154 		} else
155 			strlcpy(cmd.cmd, cmd_table[i].cmd, sizeof cmd.cmd);
156 
157 		/* Call function */
158 		st = cmd_table[i].fcn(&cmd, disk, &mbr, tt, offset);
159 
160 		/* Update status */
161 		if (st == CMD_EXIT)
162 			break;
163 		if (st == CMD_SAVE)
164 			break;
165 		if (st == CMD_CLEAN)
166 			modified = 0;
167 		if (st == CMD_DIRTY)
168 			modified = 1;
169 	} while (1);
170 
171 	/* Write out MBR */
172 	if (modified) {
173 		if (st == CMD_SAVE) {
174 			printf("Writing current MBR to disk.\n");
175 			fd = DISK_open(disk->name, O_RDWR);
176 			MBR_make(&mbr, mbr_buf);
177 			if (MBR_write(fd, offset, mbr_buf) == -1) {
178 				warn("error writing MBR");
179 				close(fd);
180 				goto again;
181 			}
182 			close(fd);
183 		} else
184 			printf("Aborting changes to current MBR.\n");
185 	}
186 
187 	/* One level less */
188 	editlevel -= 1;
189 
190 	return (0);
191 }
192 
193 int
194 USER_print_disk(disk_t *disk)
195 {
196 	int fd, offset, firstoff, i;
197 	char mbr_buf[DEV_BSIZE];
198 	mbr_t mbr;
199 
200 	fd = DISK_open(disk->name, O_RDONLY);
201 	offset = firstoff = 0;
202 
203 	DISK_printmetrics(disk, NULL);
204 
205 	do {
206 		MBR_read(fd, (off_t)offset, mbr_buf);
207 		MBR_parse(disk, mbr_buf, offset, firstoff, &mbr);
208 
209 		printf("Offset: %d\t", (int)offset);
210 		MBR_print(&mbr, NULL);
211 
212 		/* Print out extended partitions too */
213 		for (offset = i = 0; i < 4; i++)
214 			if (mbr.part[i].id == DOSPTYP_EXTEND ||
215 			    mbr.part[i].id == DOSPTYP_EXTENDL) {
216 				offset = mbr.part[i].bs;
217 				if (firstoff == 0)
218 					firstoff = offset;
219 			}
220 	} while (offset);
221 
222 	return (DISK_close(fd));
223 }
224 
225