disks.c revision 8668
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $Id: disks.c,v 1.24 1995/05/20 19:22:18 jkh Exp $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *	This product includes software developed by Jordan Hubbard
25 *	for the FreeBSD Project.
26 * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
27 *    endorse or promote products derived from this software without specific
28 *    prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 */
43
44#include "sysinstall.h"
45#include <ctype.h>
46#include <sys/disklabel.h>
47
48/* Where we start displaying chunk information on the screen */
49#define CHUNK_START_ROW		5
50
51/* Where we keep track of MBR chunks */
52static struct chunk *chunk_info[10];
53static int current_chunk;
54
55static void
56record_chunks(Disk *d)
57{
58    struct chunk *c1;
59    int i = 0;
60    int last_free = 0;
61    if (!d->chunks)
62	msgFatal("No chunk list found for %s!", d->name);
63    c1 = d->chunks->part;
64    current_chunk = 0;
65    while (c1) {
66	if (c1->type == unused && c1->size > last_free) {
67	    last_free = c1->size;
68	    current_chunk = i;
69	}
70	chunk_info[i++] = c1;
71	c1 = c1->next;
72    }
73    chunk_info[i] = NULL;
74}
75
76static void
77print_chunks(Disk *d)
78{
79    int row;
80    int i;
81
82    attrset(A_NORMAL);
83    mvaddstr(0, 0, "Disk name:\t");
84    clrtobot();
85    attrset(A_REVERSE); addstr(d->name); attrset(A_NORMAL);
86    attrset(A_REVERSE); mvaddstr(0, 55, "Master Partition Editor"); attrset(A_NORMAL);
87    mvprintw(1, 0,
88	     "BIOS Geometry:\t%lu cyls/%lu heads/%lu sectors",
89	     d->bios_cyl, d->bios_hd, d->bios_sect);
90    mvprintw(3, 1, "%10s %10s %10s %8s %8s %8s %8s %8s",
91	     "Offset", "Size", "End", "Name", "PType", "Desc",
92	     "Subtype", "Flags");
93    for (i = 0, row = CHUNK_START_ROW; chunk_info[i]; i++, row++) {
94	if (i == current_chunk)
95	    attrset(A_REVERSE);
96	mvprintw(row, 2, "%10ld %10lu %10lu %8s %8d %8s %8d %6lx",
97		 chunk_info[i]->offset, chunk_info[i]->size,
98		 chunk_info[i]->end, chunk_info[i]->name,
99		 chunk_info[i]->type, chunk_n[chunk_info[i]->type],
100		 chunk_info[i]->subtype, chunk_info[i]->flags);
101	if (i == current_chunk)
102	    attrset(A_NORMAL);
103    }
104}
105
106static void
107print_command_summary()
108{
109    mvprintw(14, 0, "The following commands are supported (in upper or lower case):");
110    mvprintw(16, 0, "A = Use Entire Disk    B = Bad Block Scan     C = Create Partition");
111    mvprintw(17, 0, "D = Delete Partition   G = Set BIOS Geometry  S = Set Bootable");
112    mvprintw(18, 0, "U = Undo All Changes   W = `Wizard' Mode      ESC = Exit this screen");
113    mvprintw(20, 0, "The currently selected partition is displayed in ");
114    attrset(A_REVERSE); addstr("reverse video."); attrset(A_NORMAL);
115    mvprintw(21, 0, "Use F1 or ? to get more help, arrow keys to move.");
116    move(0, 0);
117}
118
119static Disk *
120diskPartition(Disk *d)
121{
122    char *p;
123    int key = 0;
124    Boolean chunking;
125    char *msg = NULL;
126    char name[40];
127
128    chunking = TRUE;
129    strncpy(name, d->name, 40);
130    keypad(stdscr, TRUE);
131
132    clear();
133    record_chunks(d);
134    while (chunking) {
135	print_chunks(d);
136	print_command_summary();
137	if (msg) {
138	    standout(); mvprintw(23, 0, msg); standend();
139	    beep();
140	    msg = NULL;
141	}
142
143	key = toupper(getch());
144	switch (key) {
145	case KEY_UP:
146	case '-':
147	    if (current_chunk != 0)
148		--current_chunk;
149	    break;
150
151	case KEY_DOWN:
152	case '+':
153	case '\r':
154	case '\n':
155	    if (chunk_info[current_chunk + 1])
156		++current_chunk;
157	    break;
158
159	case KEY_HOME:
160	    current_chunk = 0;
161	    break;
162
163	case KEY_END:
164	    while (chunk_info[current_chunk + 1])
165		++current_chunk;
166	    break;
167
168	case KEY_F(1):
169	case '?':
170	    systemDisplayFile("slice.hlp");
171	    break;
172
173	case 'A':
174	    All_FreeBSD(d);
175	    record_chunks(d);
176	    break;
177
178	case 'B':
179	    if (chunk_info[current_chunk]->type != freebsd)
180		msg = "Can only scan for bad blocks in FreeBSD partition.";
181	    else if (strncmp(name, "sd", 2) ||
182		     !msgYesNo("This typically makes sense only for ESDI, IDE or MFM drives.\nAre you sure you want to do this on a SCSI disk?"))
183		if (chunk_info[current_chunk]->flags & CHUNK_BAD144)
184		    chunk_info[current_chunk]->flags &= ~CHUNK_BAD144;
185		else
186		    chunk_info[current_chunk]->flags |= CHUNK_BAD144;
187	    break;
188
189	case 'C':
190	    if (chunk_info[current_chunk]->type != unused)
191		msg = "Partition in use, delete it first or move to an unused one.";
192	    else {
193		char *val, tmp[20], *cp;
194		int size;
195
196		snprintf(tmp, 20, "%d", chunk_info[current_chunk]->size);
197		val = msgGetInput(tmp, "Please specify the size for new FreeBSD partition in blocks, or append\na trailing `M' for megabytes (e.g. 20M).");
198		if (val && (size = strtol(val, &cp, 0)) > 0) {
199		    if (*cp && toupper(*cp) == 'M')
200			size *= 2048;
201		    Create_Chunk(d, chunk_info[current_chunk]->offset,
202				 size,
203				 freebsd,
204				 3,
205				 (chunk_info[current_chunk]->flags &
206				  CHUNK_ALIGN));
207		    record_chunks(d);
208		}
209	    }
210	    break;
211
212	case 'D':
213	    if (chunk_info[current_chunk]->type == unused)
214		msg = "Partition is already unused!";
215	    else {
216		Delete_Chunk(d, chunk_info[current_chunk]);
217		record_chunks(d);
218	    }
219	    break;
220
221	case 'G': {
222	    char *val, geometry[80];
223
224	    snprintf(geometry, 80, "%lu/%lu/%lu",
225		     d->bios_cyl, d->bios_hd, d->bios_sect);
226	    val = msgGetInput(geometry,
227"Please specify the new geometry in cyl/hd/sect format.\nDon't forget to use the two slash (/) separator characters!\nIt's not possible to parse the field without them.");
228	    if (val) {
229		d->bios_cyl = strtol(val, &val, 0);
230		d->bios_hd = strtol(val + 1, &val, 0);
231		d->bios_sect = strtol(val + 1, 0, 0);
232	    }
233	}
234	    break;
235
236	case 'S':
237	    /* Set Bootable */
238	    chunk_info[current_chunk]->flags |= CHUNK_ACTIVE;
239	    break;
240
241	case 'U':
242	    Free_Disk(d);
243	    d = Open_Disk(name);
244	    if (!d)
245		msgFatal("Can't reopen disk %s!", name);
246	    record_chunks(d);
247	    break;
248
249	case 'W':
250	    if (!msgYesNo("Are you sure you want to go into Wizard mode?\nNo seat belts whatsoever are provided!")) {
251		dialog_clear();
252		end_dialog();
253		DialogActive = FALSE;
254		slice_wizard(d);
255		dialog_clear();
256		DialogActive = TRUE;
257		record_chunks(d);
258	    }
259	    else
260		msg = "Wise choice!";
261	    break;
262
263	case 27:	/* ESC */
264	    chunking = FALSE;
265	    break;
266
267	default:
268	    beep();
269	    msg = "Type F1 or ? for help";
270	    break;
271	}
272    }
273    p = CheckRules(d);
274    if (p) {
275	msgConfirm(p);
276	free(p);
277    }
278    dialog_clear();
279    refresh();
280    variable_set2(DISK_PARTITIONED, "yes");
281    return d;
282}
283
284static int
285partitionHook(char *str)
286{
287    Device **devs = NULL;
288
289    /* Clip garbage off the ends */
290    string_prune(str);
291    str = string_skipwhite(str);
292    /* Try and open all the disks */
293    while (str) {
294	char *cp;
295
296	cp = index(str, '\n');
297	if (cp)
298	   *cp++ = 0;
299	if (!*str) {
300	    beep();
301	    return 0;
302	}
303	devs = deviceFind(str, DEVICE_TYPE_DISK);
304	if (!devs) {
305	    msgConfirm("Unable to find disk %s!", str);
306	    return 0;
307	}
308	else if (devs[1])
309	    msgConfirm("Bizarre multiple match for %s!", str);
310	devs[0]->private = diskPartition((Disk *)devs[0]->private);
311	devs[0]->enabled = TRUE;
312	str = cp;
313    }
314    return devs ? 1 : 0;
315}
316
317int
318diskPartitionEditor(char *str)
319{
320    DMenu *menu;
321
322    menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, partitionHook);
323    if (!menu) {
324	msgConfirm("No devices suitable for installation found!\n\nPlease verify that your disk controller (and attached drives) were detected properly.  This can be done by selecting the ``Bootmsg'' option on the main menu and reviewing the boot messages carefully.");
325    }
326    else {
327	dmenuOpenSimple(menu);
328	free(menu);
329    }
330    return 0;
331}
332