disks.c revision 18650
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.65 1996/10/01 04:56:31 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 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include "sysinstall.h"
38#include <ctype.h>
39#include <sys/disklabel.h>
40
41/* Where we start displaying chunk information on the screen */
42#define CHUNK_START_ROW		5
43
44/* Where we keep track of MBR chunks */
45static struct chunk *chunk_info[16];
46static int current_chunk;
47
48static void
49record_chunks(Disk *d)
50{
51    struct chunk *c1 = NULL;
52    int i = 0;
53    int last_free = 0;
54
55    if (!d->chunks)
56	msgFatal("No chunk list found for %s!", d->name);
57
58    for (c1 = d->chunks->part; c1; c1 = c1->next) {
59	if (c1->type == unused && c1->size > last_free) {
60	    last_free = c1->size;
61	    current_chunk = i;
62	}
63	chunk_info[i++] = c1;
64    }
65    chunk_info[i] = NULL;
66}
67
68static void
69print_chunks(Disk *d)
70{
71    int row;
72    int i, sz;
73
74    for (i = sz = 0; chunk_info[i]; i++)
75	sz += chunk_info[i]->size;
76    if (d->bios_hd <= 1 && d->bios_sect <= 1) {
77	All_FreeBSD(d, TRUE);
78	d->bios_hd = d->bios_sect = d->bios_cyl = 1;
79    }
80    else if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64) {
81	dialog_clear_norefresh();
82	msgConfirm("WARNING:  A geometry of %d/%d/%d for %s is incorrect.  Using\n"
83		   "a default geometry of 64 heads and 32 sectors.  If this geometry\n"
84		   "is incorrect or you are unsure as to whether or not it's correct,\n"
85		   "please consult the Hardware Guide in the Documentation submenu\n"
86		   "or use the (G)eometry command to change it now.", d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
87	d->bios_hd = 64;
88	d->bios_sect = 32;
89	d->bios_cyl = sz / ONE_MEG;
90    }
91    attrset(A_NORMAL);
92    mvaddstr(0, 0, "Disk name:\t");
93    clrtobot();
94    attrset(A_REVERSE); addstr(d->name); attrset(A_NORMAL);
95    attrset(A_REVERSE); mvaddstr(0, 55, "FDISK Partition Editor"); attrset(A_NORMAL);
96    mvprintw(1, 0,
97	     "DISK Geometry:\t%lu cyls/%lu heads/%lu sectors",
98	     d->bios_cyl, d->bios_hd, d->bios_sect);
99    mvprintw(3, 1, "%10s %10s %10s %8s %8s %8s %8s %8s",
100	     "Offset", "Size", "End", "Name", "PType", "Desc",
101	     "Subtype", "Flags");
102    for (i = 0, row = CHUNK_START_ROW; chunk_info[i]; i++, row++) {
103	if (i == current_chunk)
104	    attrset(ATTR_SELECTED);
105	mvprintw(row, 2, "%10ld %10lu %10lu %8s %8d %8s %8d\t%-6s",
106		 chunk_info[i]->offset, chunk_info[i]->size,
107		 chunk_info[i]->end, chunk_info[i]->name,
108		 chunk_info[i]->type, chunk_n[chunk_info[i]->type],
109		 chunk_info[i]->subtype, ShowChunkFlags(chunk_info[i]));
110	if (i == current_chunk)
111	    attrset(A_NORMAL);
112    }
113}
114
115static void
116print_command_summary()
117{
118    mvprintw(14, 0, "The following commands are supported (in upper or lower case):");
119    mvprintw(16, 0, "A = Use Entire Disk    B = Bad Block Scan     C = Create Partition");
120    mvprintw(17, 0, "D = Delete Partition   G = Set Drive Geometry S = Set Bootable");
121    mvprintw(18, 0, "U = Undo All Changes   Q = Finish");
122    if (!RunningAsInit)
123	mvprintw(18, 46, "W = Write Changes");
124    mvprintw(21, 0, "Use F1 or ? to get more help, arrow keys to select.");
125    move(0, 0);
126}
127
128static u_char *
129getBootMgr(char *dname)
130{
131    extern u_char mbr[], bteasy17[];
132    char str[80];
133    char *cp;
134    int i = 0;
135
136    cp = variable_get(VAR_BOOTMGR);
137    if (!cp) {
138	/* Figure out what kind of MBR the user wants */
139	sprintf(str, "Install Boot Manager for drive %s?", dname);
140	MenuMBRType.title = str;
141	i = dmenuOpenSimple(&MenuMBRType, FALSE);
142    }
143    else {
144	if (!strncmp(cp, "boot", 4))
145	    BootMgr = 0;
146	else if (!strcmp(cp, "standard"))
147	    BootMgr = 1;
148	else
149	    BootMgr = 2;
150    }
151    if (cp || i) {
152	switch (BootMgr) {
153	case 0:
154	    return bteasy17;
155
156	case 1:
157	    return mbr;
158
159	case 2:
160	default:
161	    break;
162	}
163    }
164    return NULL;
165}
166
167void
168diskPartition(Device *dev, Disk *d)
169{
170    char *p;
171    int rv, key = 0;
172    Boolean chunking;
173    char *msg = NULL;
174    u_char *mbrContents;
175    WINDOW *w = savescr();
176
177    chunking = TRUE;
178    keypad(stdscr, TRUE);
179
180    /* Flush both the dialog and curses library views of the screen
181       since we don't always know who called us */
182    dialog_clear_norefresh(), clear();
183
184    while (chunking) {
185	/* Set up the chunk array */
186	record_chunks(d);
187
188	/* Now print our overall state */
189	print_chunks(d);
190	print_command_summary();
191	if (msg) {
192	    attrset(title_attr); mvprintw(23, 0, msg); attrset(A_NORMAL);
193	    beep();
194	    msg = NULL;
195	}
196	else {
197	    move(23, 0);
198	    clrtoeol();
199	}
200
201	/* Get command character */
202	key = getch();
203	switch (toupper(key)) {
204
205	    /* redraw */
206	case '\014':	/* ^L */
207	    clear();
208	    msg = NULL;
209	    break;
210
211	case KEY_UP:
212	case '-':
213	    if (current_chunk != 0)
214		--current_chunk;
215	    break;
216
217	case KEY_DOWN:
218	case '+':
219	case '\r':
220	case '\n':
221	    if (chunk_info[current_chunk + 1])
222		++current_chunk;
223	    break;
224
225	case KEY_HOME:
226	    current_chunk = 0;
227	    break;
228
229	case KEY_END:
230	    while (chunk_info[current_chunk + 1])
231		++current_chunk;
232	    break;
233
234	case KEY_F(1):
235	case '?':
236	    systemDisplayHelp("slice");
237	    clear();
238	    break;
239
240	case 'A':
241	    rv = msgYesNo("Do you want to do this with a true partition entry\n"
242			  "so as to remain cooperative with any future possible\n"
243			  "operating systems on the drive(s)?");
244	    if (rv) {
245		rv = !msgYesNo("This is dangerous in that it will make the drive totally\n"
246			       "uncooperative with other potential operating systems on the\n"
247			       "same disk.  It will lead instead to a totally dedicated disk,\n"
248			       "starting at the very first sector, bypassing all BIOS geometry\n"
249			       "considerations.  This precludes the existance of any boot\n"
250			       "manager or other stuff in sector 0, since the BSD bootstrap\n"
251			       "will live there.\n"
252			       "You will run into serious trouble with ST-506 and ESDI drives\n"
253			       "and possibly some IDE drives (e.g. drives running under the\n"
254			       "control of sort of disk manager).  SCSI drives are considerably\n"
255			       "less at risk.\n\n"
256			       "Do you insist on dedicating the entire disk this way?");
257	    }
258	    All_FreeBSD(d, rv);
259	    if (rv)
260		d->bios_hd = d->bios_sect = d->bios_cyl = 1;
261	    variable_set2(DISK_PARTITIONED, "yes");
262	    clear();
263	    break;
264
265	case 'B':
266	    if (chunk_info[current_chunk]->type != freebsd)
267		msg = "Can only scan for bad blocks in FreeBSD partition.";
268	    else if (strncmp(d->name, "sd", 2) ||
269		     !msgYesNo("This typically makes sense only for ESDI, IDE or MFM drives.\n"
270			       "Are you sure you want to do this on a SCSI disk?")) {
271		if (chunk_info[current_chunk]->flags & CHUNK_BAD144)
272		    chunk_info[current_chunk]->flags &= ~CHUNK_BAD144;
273		else
274		    chunk_info[current_chunk]->flags |= CHUNK_BAD144;
275	    }
276	    clear();
277	    break;
278
279	case 'C':
280	    if (chunk_info[current_chunk]->type != unused)
281		msg = "Partition in use, delete it first or move to an unused one.";
282	    else {
283		char *val, tmp[20], *cp;
284		int size, subtype;
285		chunk_e partitiontype;
286
287		snprintf(tmp, 20, "%d", chunk_info[current_chunk]->size);
288		val = msgGetInput(tmp, "Please specify the size for new FreeBSD partition in blocks\n"
289				       "or append a trailing `M' for megabytes (e.g. 20M).");
290		if (val && (size = strtol(val, &cp, 0)) > 0) {
291		    if (*cp && toupper(*cp) == 'M')
292			size *= ONE_MEG;
293		    strcpy(tmp, "165");
294		    val = msgGetInput(tmp, "Enter type of partition to create:\n\n"
295				      "Pressing Enter will choose the default, a native FreeBSD\n"
296				      "partition (type 165).  You can choose other types, 6 for a\n"
297				      "DOS partition or 131 for a Linux partition, for example.\n\n"
298				      "Note:  If you choose a non-FreeBSD partition type, it will not\n"
299				      "be formatted or otherwise prepared, it will simply reserve space\n"
300				      "for you to use another tool, such as DOS FORMAT, to later format\n"
301				      "and use the partition.");
302		    if (val && (subtype = strtol(val, NULL, 0)) > 0) {
303			if (subtype==165)
304				partitiontype=freebsd;
305			else if (subtype==6)
306				partitiontype=fat;
307			else
308				partitiontype=unknown;
309		    Create_Chunk(d, chunk_info[current_chunk]->offset, size, partitiontype, subtype,
310				 (chunk_info[current_chunk]->flags & CHUNK_ALIGN));
311		    variable_set2(DISK_PARTITIONED, "yes");
312		    }
313		}
314		clear();
315	    }
316	    break;
317
318	case KEY_DC:
319	case 'D':
320	    if (chunk_info[current_chunk]->type == unused)
321		msg = "Partition is already unused!";
322	    else {
323		Delete_Chunk(d, chunk_info[current_chunk]);
324		variable_set2(DISK_PARTITIONED, "yes");
325	    }
326	    break;
327
328	case 'G': {
329	    char *val, geometry[80];
330
331	    snprintf(geometry, 80, "%lu/%lu/%lu", d->bios_cyl, d->bios_hd, d->bios_sect);
332	    val = msgGetInput(geometry, "Please specify the new geometry in cyl/hd/sect format.\n"
333			      "Don't forget to use the two slash (/) separator characters!\n"
334			      "It's not possible to parse the field without them.");
335	    if (val) {
336		d->bios_cyl = strtol(val, &val, 0);
337		d->bios_hd = strtol(val + 1, &val, 0);
338		d->bios_sect = strtol(val + 1, 0, 0);
339	    }
340	    clear();
341	}
342	break;
343
344    case 'S':
345	/* Set Bootable */
346	chunk_info[current_chunk]->flags |= CHUNK_ACTIVE;
347	break;
348
349    case 'U':
350	    if (msgYesNo("Are you SURE you want to Undo everything?")) {
351		clear();
352		break;
353	    }
354	    d = Open_Disk(d->name);
355	    if (!d) {
356		msgConfirm("Can't reopen disk %s! Internal state is probably corrupted", d->name);
357	    	clear();
358		break;
359	    }
360	    Free_Disk(dev->private);
361	    dev->private = d;
362	    variable_unset(DISK_PARTITIONED);
363	    variable_unset(DISK_LABELLED);
364	    clear();
365	    break;
366
367	case 'W':
368	    if (!msgYesNo("WARNING:  This should only be used for modifying an\n"
369			  "EXISTING installation - DO NOT USE this option if you\n"
370			  "are installing FreeBSD for the first time!  This is not\n"
371			  "an option for use during the standard install.\n\n"
372			  "Are you absolutely sure you want to do this now?")) {
373
374		variable_set2(DISK_PARTITIONED, "yes");
375
376		/* Don't trash the MBR if the first (and therefore only) chunk is marked for a truly dedicated
377		 * disk (i.e., the disklabel starts at sector 0), even in cases where the user has requested
378		 * booteasy or a "standard" MBR -- both would be fatal in this case.
379		 */
380		if ((d->chunks->part->flags & CHUNK_FORCE_ALL) != CHUNK_FORCE_ALL
381		    && (mbrContents = getBootMgr(d->name)) != NULL)
382		    Set_Boot_Mgr(d, mbrContents);
383
384		if (DITEM_STATUS(diskPartitionWrite(NULL)) != DITEM_SUCCESS)
385		    msgConfirm("Disk partition write returned an error status!");
386		else
387		    msgConfirm("Wrote FDISK partition information out successfully.");
388	    }
389	    clear();
390	    break;
391
392	case '|':
393	    if (!msgYesNo("Are you SURE you want to go into Wizard mode?\n"
394			  "No seat belts whatsoever are provided!")) {
395		clear();
396		refresh();
397		slice_wizard(d);
398		variable_set2(DISK_PARTITIONED, "yes");
399	    }
400	    else
401		msg = "Wise choice!";
402	    clear();
403	    break;
404
405	case 'Q':
406	    chunking = FALSE;
407	    /* Don't trash the MBR if the first (and therefore only) chunk is marked for a truly dedicated
408	     * disk (i.e., the disklabel starts at sector 0), even in cases where the user has requested
409	     * booteasy or a "standard" MBR -- both would be fatal in this case.
410	     */
411	    if ((d->chunks->part->flags & CHUNK_FORCE_ALL) != CHUNK_FORCE_ALL
412		&& (mbrContents = getBootMgr(d->name)) != NULL)
413		Set_Boot_Mgr(d, mbrContents);
414	    break;
415
416	default:
417	    beep();
418	    msg = "Type F1 or ? for help";
419	    break;
420	}
421    }
422    p = CheckRules(d);
423    if (p) {
424	char buf[FILENAME_MAX];
425
426	dialog_clear_norefresh();
427        use_helpline("Press F1 to read more about disk partitioning.");
428	use_helpfile(systemHelpFile("partition", buf));
429	dialog_mesgbox("Disk partitioning warning:", p, -1, -1);
430	free(p);
431    }
432    restorescr(w);
433}
434
435static int
436partitionHook(dialogMenuItem *selected)
437{
438    Device **devs = NULL;
439
440    devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
441    if (!devs) {
442	msgConfirm("Unable to find disk %s!", selected->prompt);
443	return DITEM_FAILURE;
444    }
445    /* Toggle enabled status? */
446    if (!devs[0]->enabled) {
447	devs[0]->enabled = TRUE;
448	diskPartition(devs[0], (Disk *)devs[0]->private);
449    }
450    else
451	devs[0]->enabled = FALSE;
452    return DITEM_SUCCESS | DITEM_REDRAW;
453}
454
455static int
456partitionCheck(dialogMenuItem *selected)
457{
458    Device **devs = NULL;
459
460    devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
461    if (!devs || devs[0]->enabled == FALSE)
462	return FALSE;
463    return TRUE;
464}
465
466int
467diskPartitionEditor(dialogMenuItem *self)
468{
469    DMenu *menu;
470    Device **devs;
471    int i, cnt;
472    char *cp;
473
474    cp = variable_get(VAR_DISK);
475    devs = deviceFind(cp, DEVICE_TYPE_DISK);
476    cnt = deviceCount(devs);
477    if (!cnt) {
478	msgConfirm("No disks found!  Please verify that your disk controller is being\n"
479		   "properly probed at boot time.  See the Hardware Guide on the\n"
480		   "Documentation menu for clues on diagnosing this type of problem.");
481	i = DITEM_FAILURE;
482    }
483    else if (cnt == 1) {
484	devs[0]->enabled = TRUE;
485	diskPartition(devs[0], (Disk *)devs[0]->private);
486	i = DITEM_SUCCESS;
487    }
488    else {
489	menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, partitionHook, partitionCheck);
490	if (!menu) {
491	    msgConfirm("No devices suitable for installation found!\n\n"
492		       "Please verify that your disk controller (and attached drives)\n"
493		       "were detected properly.  This can be done by pressing the\n"
494		       "[Scroll Lock] key and using the Arrow keys to move back to\n"
495		       "the boot messages.  Press [Scroll Lock] again to return.");
496	    i = DITEM_FAILURE;
497	}
498	else {
499	    i = dmenuOpenSimple(menu, FALSE) ? DITEM_SUCCESS : DITEM_FAILURE;
500	    free(menu);
501	}
502	i = i | DITEM_RECREATE;
503    }
504    return i;
505}
506
507int
508diskPartitionWrite(dialogMenuItem *self)
509{
510    Device **devs;
511    char *cp;
512    int i;
513
514    if ((cp = variable_get(DISK_PARTITIONED)) && strcmp(cp, "yes"))
515	return DITEM_SUCCESS;
516    else if (!cp) {
517	msgConfirm("You must partition the disk(s) before this option can be used.");
518	return DITEM_FAILURE;
519    }
520
521    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
522    if (!devs) {
523	msgConfirm("Unable to find any disks to write to??");
524	return DITEM_FAILURE;
525    }
526    if (isDebug())
527	msgDebug("diskPartitionWrite: Examining %d devices\n", deviceCount(devs));
528
529    for (i = 0; devs[i]; i++) {
530	Chunk *c1;
531	Disk *d = (Disk *)devs[i]->private;
532
533	if (!devs[i]->enabled)
534	    continue;
535
536	Set_Boot_Blocks(d, boot1, boot2);
537	msgNotify("Writing partition information to drive %s", d->name);
538	if (!Fake && Write_Disk(d)) {
539	    msgConfirm("ERROR: Unable to write data to disk %s!", d->name);
540	    return DITEM_FAILURE;
541	}
542	/* Now scan for bad blocks, if necessary */
543	for (c1 = d->chunks->part; c1; c1 = c1->next) {
544	    if (c1->flags & CHUNK_BAD144) {
545		int ret;
546
547		msgNotify("Running bad block scan on partition %s", c1->name);
548		if (!Fake) {
549		    ret = vsystem("bad144 -v /dev/r%s 1234", c1->name);
550		    if (ret)
551			msgConfirm("Bad144 init on %s returned status of %d!", c1->name, ret);
552		    ret = vsystem("bad144 -v -s /dev/r%s", c1->name);
553		    if (ret)
554			msgConfirm("Bad144 scan on %s returned status of %d!", c1->name, ret);
555		}
556	    }
557	}
558    }
559    /* Now it's not "yes", but "written" */
560    variable_set2(DISK_PARTITIONED, "written");
561    return DITEM_SUCCESS;
562}
563