xref: /openbsd-src/sbin/fdisk/user.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: user.c,v 1.24 2009/02/08 18:03:18 krw 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 	char *query;
71 
72 	if (preserve) {
73 		MBR_pcopy(disk, tt);
74 		query = "Do you wish to write new MBR?";
75 	} else {
76 		MBR_init(disk, tt);
77 		query = "Do you wish to write new MBR and partition table?";
78 	}
79 
80 	if (ask_yn(query))
81 		Xwrite(NULL, disk, tt, NULL, 0);
82 
83 	return (0);
84 }
85 
86 int modified;
87 
88 int
89 USER_modify(disk_t *disk, mbr_t *tt, off_t offset, off_t reloff)
90 {
91 	static int editlevel;
92 	char mbr_buf[DEV_BSIZE];
93 	mbr_t mbr;
94 	cmd_t cmd;
95 	int i, st, fd;
96 
97 	/* One level deeper */
98 	editlevel += 1;
99 
100 	/* Set up command table pointer */
101 	cmd.table = cmd_table;
102 
103 	/* Read MBR & partition */
104 	fd = DISK_open(disk->name, O_RDONLY);
105 	MBR_read(fd, offset, mbr_buf);
106 	close(fd);
107 
108 	/* Parse the sucker */
109 	MBR_parse(disk, mbr_buf, offset, reloff, &mbr);
110 
111 	printf("Enter 'help' for information\n");
112 
113 	/* Edit cycle */
114 	do {
115 again:
116 		printf("fdisk:%c%d> ", (modified)?'*':' ', editlevel);
117 		fflush(stdout);
118 		ask_cmd(&cmd);
119 
120 		if (cmd.cmd[0] == '\0')
121 			goto again;
122 		for (i = 0; cmd_table[i].cmd != NULL; i++)
123 			if (strstr(cmd_table[i].cmd, cmd.cmd)==cmd_table[i].cmd)
124 				break;
125 
126 		/* Quick hack to put in '?' == 'help' */
127 		if (!strcmp(cmd.cmd, "?"))
128 			i = 0;
129 
130 		/* Check for valid command */
131 		if (cmd_table[i].cmd == NULL) {
132 			printf("Invalid command '%s'.  Try 'help'.\n", cmd.cmd);
133 			continue;
134 		} else
135 			strlcpy(cmd.cmd, cmd_table[i].cmd, sizeof cmd.cmd);
136 
137 		/* Call function */
138 		st = cmd_table[i].fcn(&cmd, disk, &mbr, tt, offset);
139 
140 		/* Update status */
141 		if (st == CMD_EXIT)
142 			break;
143 		if (st == CMD_SAVE)
144 			break;
145 		if (st == CMD_CLEAN)
146 			modified = 0;
147 		if (st == CMD_DIRTY)
148 			modified = 1;
149 	} while (1);
150 
151 	/* Write out MBR */
152 	if (modified) {
153 		if (st == CMD_SAVE) {
154 			if (Xwrite(NULL, disk, &mbr, NULL, offset) == CMD_CONT)
155 				goto again;
156 			close(fd);
157 		} else
158 			printf("Aborting changes to current MBR.\n");
159 	}
160 
161 	/* One level less */
162 	editlevel -= 1;
163 
164 	return (0);
165 }
166 
167 int
168 USER_print_disk(disk_t *disk)
169 {
170 	int fd, offset, firstoff, i;
171 	char mbr_buf[DEV_BSIZE];
172 	mbr_t mbr;
173 
174 	fd = DISK_open(disk->name, O_RDONLY);
175 	offset = firstoff = 0;
176 
177 	DISK_printmetrics(disk, NULL);
178 
179 	do {
180 		MBR_read(fd, (off_t)offset, mbr_buf);
181 		MBR_parse(disk, mbr_buf, offset, firstoff, &mbr);
182 
183 		printf("Offset: %d\t", (int)offset);
184 		MBR_print(&mbr, NULL);
185 
186 		/* Print out extended partitions too */
187 		for (offset = i = 0; i < 4; i++)
188 			if (mbr.part[i].id == DOSPTYP_EXTEND ||
189 			    mbr.part[i].id == DOSPTYP_EXTENDL) {
190 				offset = mbr.part[i].bs;
191 				if (firstoff == 0)
192 					firstoff = offset;
193 			}
194 	} while (offset);
195 
196 	return (close(fd));
197 }
198 
199