aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/sade/disks.c
blob: 513643bb8eb095a5a714018feecd02d8f344e316 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
 * $FreeBSD$
 *
 * Copyright (c) 1995
 *	Jordan Hubbard.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    verbatim and that no modifications are made prior to this
 *    point in the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include "sade.h"
#include <ctype.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libdisk.h>
#include <sys/stat.h>
#include <sys/disklabel.h>

#ifdef WITH_SLICES
enum size_units_t { UNIT_BLOCKS, UNIT_KILO, UNIT_MEG, UNIT_GIG, UNIT_SIZE };

#ifdef PC98
#define	SUBTYPE_FREEBSD		50324
#define	SUBTYPE_FAT		37218
#else
#define	SUBTYPE_FREEBSD		165
#define	SUBTYPE_FAT		6
#endif
#define	SUBTYPE_EFI		239

#ifdef PC98
#define	OTHER_SLICE_VALUES						\
	"Other popular values are 37218 for a\n"			\
	"DOS FAT partition.\n\n"
#else
#define	OTHER_SLICE_VALUES						\
	"Other popular values are 6 for a\n"				\
	"DOS FAT partition, 131 for a Linux ext2fs partition, or\n"	\
	"130 for a Linux swap partition.\n\n"
#endif
#define	NON_FREEBSD_NOTE						\
	"Note:  If you choose a non-FreeBSD partition type, it will not\n" \
	"be formatted or otherwise prepared, it will simply reserve space\n" \
	"for you to use another tool, such as DOS format, to later format\n" \
	"and actually use the partition."

/* Where we start displaying chunk information on the screen */
#define CHUNK_START_ROW		5

/* Where we keep track of MBR chunks */
#define	CHUNK_INFO_ENTRIES	16
static struct chunk *chunk_info[CHUNK_INFO_ENTRIES];
static int current_chunk;

static void	diskPartitionNonInteractive(Device *dev);
#if !defined(__ia64__)
static u_char *	bootalloc(char *name, size_t *size);
#endif

static void
record_chunks(Disk *d)
{
    struct chunk *c1 = NULL;
    int i = 0;
    daddr_t last_free = 0;

    if (!d->chunks)
	msgFatal("No chunk list found for %s!", d->name);

    for (c1 = d->chunks->part; c1; c1 = c1->next) {
	if (c1->type == unused && c1->size > last_free) {
	    last_free = c1->size;
	    current_chunk = i;
	}
	chunk_info[i++] = c1;
    }
    chunk_info[i] = NULL;
    if (current_chunk >= i)
	current_chunk = i - 1;
}

static daddr_t Total;

static void
check_geometry(Disk *d)
{
    int sg;

#ifdef PC98
    if (d->bios_cyl >= 65536 || d->bios_hd > 256 || d->bios_sect >= 256)
#else
    if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64)
#endif
    {
	dlg_clear();
	sg = msgYesNo("WARNING:  It is safe to use a geometry of %lu/%lu/%lu for %s on\n"
		      "computers with modern BIOS versions.  If this disk is to be used\n"
		      "on an old machine it is recommended that it does not have more\n"
		      "than 65535 cylinders, more than 255 heads, or more than\n"
#ifdef PC98
		      "255"
#else
		      "63"
#endif
		      " sectors per track.\n"
		      "\n"
		      "Would you like to keep using the current geometry?\n",
		      d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
	if (sg == 1) {
	    Sanitize_Bios_Geom(d);
	    msgConfirm("A geometry of %lu/%lu/%lu was calculated for %s.\n"
		       "\n"
		       "If you are not sure about this, please consult the Hardware Guide\n"
		       "in the Documentation submenu or use the (G)eometry command to\n"
		       "change it.  Remember: you need to enter whatever your BIOS thinks\n"
		       "the geometry is!  For IDE, it's what you were told in the BIOS\n"
		       "setup.  For SCSI, it's the translation mode your controller is\n"
		       "using.  Do NOT use a ``physical geometry''.\n",
		       d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
	}
    }
}

static void
print_chunks(Disk *d, int u)
{
    int row;
    int i;
    daddr_t sz;
    char *szstr;

    szstr = (u == UNIT_GIG ? "GB" : (u == UNIT_MEG ? "MB" :
	(u == UNIT_KILO ? "KB" : "ST")));

    Total = 0;
    for (i = 0; chunk_info[i]; i++)
	Total += chunk_info[i]->size;
    attrset(A_NORMAL);
    mvaddstr(0, 0, "Disk name:\t");
    clrtobot();
    attrset(A_REVERSE); addstr(d->name); attrset(A_NORMAL);
    attrset(A_REVERSE); mvaddstr(0, 55, "FDISK Partition Editor"); attrset(A_NORMAL);
    mvprintw(1, 0,
	     "DISK Geometry:\t%lu cyls/%lu heads/%lu sectors = %jd sectors (%jdMB)",
	     d->bios_cyl, d->bios_hd, d->bios_sect,
	     (intmax_t)d->bios_cyl * d->bios_hd * d->bios_sect,
	     (intmax_t)d->bios_cyl * d->bios_hd * d->bios_sect / (1024/512) / 1024);
    mvprintw(3, 0, "%6s %10s(%s) %10s %8s %6s %10s %8s %8s",
	     "Offset", "Size", szstr, "End", "Name", "PType", "Desc",
	     "Subtype", "Flags");
    for (i = 0, row = CHUNK_START_ROW; chunk_info[i]; i++, row++) {
	switch(u) {
	default:	/* fall thru */
	case UNIT_BLOCKS:
	    sz = chunk_info[i]->size;
	    break;
	case UNIT_KILO:
	    sz = chunk_info[i]->size / (1024/512);
	    break;
	case UNIT_MEG:
	    sz = chunk_info[i]->size / (1024/512) / 1024;
	    break;
	case UNIT_GIG:
	    sz = chunk_info[i]->size / (1024/512) / 1024 / 1024;
	    break;
	}
	if (i == current_chunk)
	    attrset(ATTR_SELECTED);
	mvprintw(row, 0, "%10jd %10jd %10jd %8s %6d %10s %8d\t%-6s",
		 (intmax_t)chunk_info[i]->offset, (intmax_t)sz,
		 (intmax_t)chunk_info[i]->end, chunk_info[i]->name,
		 chunk_info[i]->type, 
		 slice_type_name(chunk_info[i]->type, chunk_info[i]->subtype),
		 chunk_info[i]->subtype, ShowChunkFlags(chunk_info[i]));
	if (i == current_chunk)
	    attrset(A_NORMAL);
    }
}

static void
print_command_summary(void)
{
    mvprintw(14, 0, "The following commands are supported (in upper or lower case):");
    mvprintw(16, 0, "A = Use Entire Disk   G = set Drive Geometry   C = Create Slice");
    mvprintw(17, 0, "D = Delete Slice      Z = Toggle Size Units    S = Set Bootable   | = Expert m.");
    mvprintw(18, 0, "T = Change Type       U = Undo All Changes     W = Write Changes  Q = Finish");
    mvprintw(21, 0, "Use F1 or ? to get more help, arrow keys to select.");
    move(0, 0);
}

#ifdef PC98
static void
getBootMgr(char *dname, u_char **bootipl, size_t *bootipl_size,
	   u_char **bootmenu, size_t *bootmenu_size)
{
    static u_char *boot0;
    static size_t boot0_size;
    static u_char *boot05;
    static size_t boot05_size;

    char str[80];
    char *cp;
    int i = 0;

    dlg_clr_result();
    cp = variable_get(VAR_BOOTMGR);
    if (!cp) {
	/* Figure out what kind of IPL the user wants */
	sprintf(str, "Install Boot Manager for drive %s?", dname);
	MenuIPLType.title = str;
	i = dmenuOpen(&MenuIPLType);
    } else {
	if (!strncmp(cp, "boot", 4))
            dlg_add_result(MenuIPLType.items[0].prompt);
	else
            dlg_add_result(MenuIPLType.items[1].prompt);
    }
    if (cp || i) {
        if (!strcmp(dialog_vars.input_result, MenuIPLType.items[0].prompt)) {
	    if (!boot0) boot0 = bootalloc("boot0", &boot0_size);
	    *bootipl = boot0;
	    *bootipl_size = boot0_size;
	    if (!boot05) boot05 = bootalloc("boot0.5", &boot05_size);
	    *bootmenu = boot05;
	    *bootmenu_size = boot05_size;
	    return;
        }
    }
    *bootipl = NULL;
    *bootipl_size = 0;
    *bootmenu = NULL;
    *bootmenu_size = 0;
}
#else
static void
getBootMgr(char *dname, u_char **bootCode, size_t *bootCodeSize)
{
#if defined(__i386__) || defined(__amd64__)	/* only meaningful on x86 */
    static u_char *mbr, *boot0;
    static size_t mbr_size, boot0_size;
    char str[80];
    char *cp;
    int i = 0;

    dlg_clr_result();
    cp = variable_get(VAR_BOOTMGR);
    if (!cp) {
	/* Figure out what kind of MBR the user wants */
	sprintf(str, "Install Boot Manager for drive %s?", dname);
	MenuMBRType.title = str;
	i = dmenuOpen(&MenuMBRType);
    }
    else {
	if (!strcmp(cp, "standard"))
            dlg_add_result(MenuMBRType.items[0].prompt);
	if (!strncmp(cp, "boot", 4))
            dlg_add_result(MenuMBRType.items[1].prompt);
	else
            dlg_add_result(MenuMBRType.items[2].prompt);
    }
    if (cp || i) {
        if (!strcmp(dialog_vars.input_result, MenuMBRType.items[0].prompt)) {
	    if (!mbr) mbr = bootalloc("mbr", &mbr_size);
	    *bootCode = mbr;
	    *bootCodeSize = mbr_size;
	    return;
	} else if (!strcmp(dialog_vars.input_result, MenuMBRType.items[1].prompt)) {
	    if (!boot0) boot0 = bootalloc("boot0", &boot0_size);
	    *bootCode = boot0;
	    *bootCodeSize = boot0_size;
	    return;
	}
    }
#endif
    *bootCode = NULL;
    *bootCodeSize = 0;
}
#endif
#endif /* WITH_SLICES */

#ifdef WITH_SLICES
void
diskPartition(Device *dev)
{
    char *cp, *p;
    int rv, key = 0;
    int i;
    Boolean chunking;
    char *msg = NULL;
#ifdef PC98
    u_char *bootipl;
    size_t bootipl_size;
    u_char *bootmenu;
    size_t bootmenu_size;
#else
    u_char *mbrContents;
    size_t mbrSize;
#endif
    WINDOW *w = savescr();
    Disk *d = (Disk *)dev->private;
    int size_unit;

    size_unit = UNIT_BLOCKS;
    chunking = TRUE;
    keypad(stdscr, TRUE);

    /* Flush both the dialog and curses library views of the screen
       since we don't always know who called us */
    dlg_clear(), clear();
    current_chunk = 0;

    /* Set up the chunk array */
    record_chunks(d);

    /* Give the user a chance to sanitize the disk geometry, if necessary */
    check_geometry(d);

    while (chunking) {
	char *val, geometry[80];
	    
	/* Now print our overall state */
	if (d)
	    print_chunks(d, size_unit);
	print_command_summary();
	if (msg) {
	    attrset(title_attr); mvprintw(23, 0, msg); attrset(A_NORMAL);
	    beep();
	    msg = NULL;
	}
	else {
	    move(23, 0);
	    clrtoeol();
	}

	/* Get command character */
	key = getch();
	switch (toupper(key)) {
	case '\014':	/* ^L (redraw) */
	    clear();
	    msg = NULL;
	    break;
	    
	case '\020':	/* ^P */
	case KEY_UP:
	case '-':
	    if (current_chunk != 0)
		--current_chunk;
	    break;
	    
	case '\016':	/* ^N */
	case KEY_DOWN:
	case '+':
	case '\r':
	case '\n':
	    if (chunk_info[current_chunk + 1])
		++current_chunk;
	    break;

	case KEY_HOME:
	    current_chunk = 0;
	    break;

	case KEY_END:
	    while (chunk_info[current_chunk + 1])
		++current_chunk;
	    break;

	case KEY_F(1):
	case '?':
	    systemDisplayHelp("slice");
	    clear();
	    break;

	case 'A':
	case 'F':	/* Undocumented magic Dangerously Dedicated mode */
#if !defined(__i386__) && !defined(__amd64__)
	    rv = 1;
#else	    /* The rest is only relevant on x86 */
	    cp = variable_get(VAR_DEDICATE_DISK);
	    if (cp && !strcasecmp(cp, "always"))
		rv = 1;
	    else if (toupper(key) == 'A')
		rv = 0;
	    else {
		rv = msgYesNo("Do you want to do this with a true partition entry\n"
			      "so as to remain cooperative with any future possible\n"
			      "operating systems on the drive(s)?\n"
			      "(See also the section about ``dangerously dedicated''\n"
			      "disks in the FreeBSD FAQ.)");
		if (rv == -1)
		    rv = 0;
	    }
#endif
	    All_FreeBSD(d, rv);
	    variable_set2(DISK_PARTITIONED, "yes", 0);
	    record_chunks(d);
	    clear();
	    break;
	    
	case 'C':
	    if (chunk_info[current_chunk]->type != unused)
		msg = "Slice in use, delete it first or move to an unused one.";
	    else {
		char *val, tmp[20], name[16], *cp;
		daddr_t size;
		int subtype;
		chunk_e partitiontype;
#ifdef PC98
		snprintf(name, sizeof (name), "%s", "FreeBSD");
		val = msgGetInput(name,
			"Please specify the name for new FreeBSD slice.");
		if (val)
			strncpy(name, val, sizeof (name));
#else
		name[0] = '\0';
#endif
		snprintf(tmp, 20, "%jd", (intmax_t)chunk_info[current_chunk]->size);
		val = msgGetInput(tmp, "Please specify the size for new FreeBSD slice in blocks\n"
				  "or append a trailing `M' for megabytes (e.g. 20M).");
		if (val && (size = strtoimax(val, &cp, 0)) > 0) {
		    if (*cp && toupper(*cp) == 'M')
			size *= ONE_MEG;
		    else if (*cp && toupper(*cp) == 'G')
			size *= ONE_GIG;
		    sprintf(tmp, "%d", SUBTYPE_FREEBSD);
		    val = msgGetInput(tmp, "Enter type of partition to create:\n\n"
			"Pressing Enter will choose the default, a native FreeBSD\n"
			"slice (type %u).  "
			OTHER_SLICE_VALUES
			NON_FREEBSD_NOTE, SUBTYPE_FREEBSD);
		    if (val && (subtype = strtol(val, NULL, 0)) > 0) {
			if (subtype == SUBTYPE_FREEBSD)
			    partitiontype = freebsd;
			else if (subtype == SUBTYPE_FAT)
			    partitiontype = fat;
			else if (subtype == SUBTYPE_EFI)
			    partitiontype = efi;
			else
#ifdef PC98
			    partitiontype = pc98;
#else
			    partitiontype = mbr;
#endif
			Create_Chunk(d, chunk_info[current_chunk]->offset, size, partitiontype, subtype,
				     (chunk_info[current_chunk]->flags & CHUNK_ALIGN), name);
			variable_set2(DISK_PARTITIONED, "yes", 0);
			record_chunks(d);
		    }
		}
		clear();
	    }
	    break;
	    
	case KEY_DC:
	case 'D':
	    if (chunk_info[current_chunk]->type == unused)
		msg = "Slice is already unused!";
	    else {
		Delete_Chunk(d, chunk_info[current_chunk]);
		variable_set2(DISK_PARTITIONED, "yes", 0);
		record_chunks(d);
	    }
	    break;
	    
	case 'T':
	    if (chunk_info[current_chunk]->type == unused)
		msg = "Slice is currently unused (use create instead)";
	    else {
		char *val, tmp[20];
		int subtype;
		chunk_e partitiontype;

		sprintf(tmp, "%d", chunk_info[current_chunk]->subtype);
		val = msgGetInput(tmp, "New partition type:\n\n"
		    "Pressing Enter will use the current type. To choose a native\n"
		    "FreeBSD slice enter %u.  "
		    OTHER_SLICE_VALUES
		    NON_FREEBSD_NOTE, SUBTYPE_FREEBSD);
		if (val && (subtype = strtol(val, NULL, 0)) > 0) {
		    if (subtype == SUBTYPE_FREEBSD)
			partitiontype = freebsd;
		    else if (subtype == SUBTYPE_FAT)
			partitiontype = fat;
		    else if (subtype == SUBTYPE_EFI)
			partitiontype = efi;
		    else
#ifdef PC98
			partitiontype = pc98;
#else
			partitiontype = mbr;
#endif
		    chunk_info[current_chunk]->type = partitiontype;
		    chunk_info[current_chunk]->subtype = subtype;
		}
	    }
	    break;
	    
	case 'G':
	    snprintf(geometry, 80, "%lu/%lu/%lu", d->bios_cyl, d->bios_hd, d->bios_sect);
	    val = msgGetInput(geometry, "Please specify the new geometry in cyl/hd/sect format.\n"
			      "Don't forget to use the two slash (/) separator characters!\n"
			      "It's not possible to parse the field without them.");
	    if (val) {
		long nc, nh, ns;
		nc = strtol(val, &val, 0);
		nh = strtol(val + 1, &val, 0);
		ns = strtol(val + 1, 0, 0);
		Set_Bios_Geom(d, nc, nh, ns);
	    }
	    clear();
	    break;
	
	case 'S':
	    /* Clear active states so we won't have two */
	    for (i = 0; (chunk_info[i] != NULL) && (i < CHUNK_INFO_ENTRIES); i++)
		chunk_info[i]->flags &= !CHUNK_ACTIVE;

	    /* Set Bootable */
	    chunk_info[current_chunk]->flags |= CHUNK_ACTIVE;
	    break;
	
	case 'U':
	    if (!variable_cmp(DISK_LABELLED, "written")) {
		msgConfirm("You've already written this information out - you\n"
			   "can't undo it.");
	    }
	    else if (!msgNoYes("Are you SURE you want to Undo everything?")) {
		char cp[BUFSIZ];

		sstrncpy(cp, d->name, sizeof cp);
		Free_Disk(dev->private);
		d = Open_Disk(cp);
		if (!d)
		    msgConfirm("Can't reopen disk %s! Internal state is probably corrupted", cp);
		dev->private = d;
		variable_unset(DISK_PARTITIONED);
		variable_unset(DISK_LABELLED);
		if (d)
		    record_chunks(d);
	    }
	    clear();
	    break;

	case 'W':
	    if (!msgNoYes("WARNING:  You are about to modify an EXISTING installation.\n"
			       "You should simply type Q when you are finished\n"
			       "here and write to the disk from the label editor.\n\n"
			       "Are you absolutely sure you want to continue?")) {
		variable_set2(DISK_PARTITIONED, "yes", 0);

#ifdef PC98
		/*
		 * Don't trash the IPL if the first (and therefore only) chunk
		 * is marked for a truly dedicated disk (i.e., the disklabel
		 * starts at sector 0), even in cases where the user has
		 * requested a FreeBSD Boot Manager -- both would be fatal in
		 * this case.
		 */
		/*
		 * Don't offer to update the IPL on this disk if the first
		 * "real" chunk looks like a FreeBSD "all disk" partition,
		 * or the disk is entirely FreeBSD.
		 */
		if ((d->chunks->part->type != freebsd) ||
		    (d->chunks->part->offset > 1))
		    getBootMgr(d->name, &bootipl, &bootipl_size,
			       &bootmenu, &bootmenu_size);
		else {
		    bootipl = NULL;
		    bootipl_size = 0;
		    bootmenu = NULL;
		    bootmenu_size = 0;
		}
		Set_Boot_Mgr(d, bootipl, bootipl_size, bootmenu, bootmenu_size);
#else
		/*
		 * Don't trash the MBR if the first (and therefore only) chunk
		 * is marked for a truly dedicated disk (i.e., the disklabel
		 * starts at sector 0), even in cases where the user has
		 * requested booteasy or a "standard" MBR -- both would be
		 * fatal in this case.
		 */
		/*
		 * Don't offer to update the MBR on this disk if the first
		 * "real" chunk looks like a FreeBSD "all disk" partition,
		 * or the disk is entirely FreeBSD.
		 */
		if ((d->chunks->part->type != freebsd) ||
		    (d->chunks->part->offset > 1))
		    getBootMgr(d->name, &mbrContents, &mbrSize);
		else {
		    mbrContents = NULL;
		    mbrSize = 0;
		}
		Set_Boot_Mgr(d, mbrContents, mbrSize);
#endif

		if (DITEM_STATUS(diskPartitionWrite(dev)) != DITEM_SUCCESS)
		    msgConfirm("Disk partition write returned an error status!");
		else
		    msgConfirm("Wrote FDISK partition information out successfully.");
	    }
	    clear();
	    break;

	case '|':
	    if (!msgNoYes("Are you SURE you want to go into Wizard mode?\n"
			  "No seat belts whatsoever are provided!")) {
		clear();
		refresh();
		slice_wizard(d);
		variable_set2(DISK_PARTITIONED, "yes", 0);
		record_chunks(d);
	    }
	    else
		msg = "Wise choice!";
	    clear();
	    break;

	case '\033':	/* ESC */
	case 'Q':
	    chunking = FALSE;
#ifdef PC98
	    /*
	     * Don't trash the IPL if the first (and therefore only) chunk
	     * is marked for a truly dedicated disk (i.e., the disklabel
	     * starts at sector 0), even in cases where the user has requested
	     * a FreeBSD Boot Manager -- both would be fatal in this case.
	     */
	    /*
	     * Don't offer to update the IPL on this disk if the first "real"
	     * chunk looks like a FreeBSD "all disk" partition, or the disk is
	     * entirely FreeBSD. 
	     */
	    if ((d->chunks->part->type != freebsd) ||
		(d->chunks->part->offset > 1)) {
		if (variable_cmp(DISK_PARTITIONED, "written")) {
		    getBootMgr(d->name, &bootipl, &bootipl_size,
			&bootmenu, &bootmenu_size);
		    if (bootipl != NULL && bootmenu != NULL)
			Set_Boot_Mgr(d, bootipl, bootipl_size,
			    bootmenu, bootmenu_size);
		}
	    }
#else
	    /*
	     * Don't trash the MBR if the first (and therefore only) chunk
	     * is marked for a truly dedicated disk (i.e., the disklabel
	     * starts at sector 0), even in cases where the user has requested
	     * booteasy or a "standard" MBR -- both would be fatal in this case.
	     */
	    /*
	     * Don't offer to update the MBR on this disk if the first "real"
	     * chunk looks like a FreeBSD "all disk" partition, or the disk is
	     * entirely FreeBSD. 
	     */
	    if ((d->chunks->part->type != freebsd) ||
		(d->chunks->part->offset > 1)) {
		if (variable_cmp(DISK_PARTITIONED, "written")) {
		    getBootMgr(d->name, &mbrContents, &mbrSize);
		    if (mbrContents != NULL)
			Set_Boot_Mgr(d, mbrContents, mbrSize);
		}
	    }
#endif
	    break;

	case 'Z':
	    size_unit = (size_unit + 1) % UNIT_SIZE;
	    break;
	    
	default:
	    beep();
	    msg = "Type F1 or ? for help";
	    break;
	}
    }
    p = CheckRules(d);
    if (p) {
	char buf[FILENAME_MAX];
	DIALOG_VARS save_vars;

	dlg_save_vars(&save_vars);
        dialog_vars.help_line = "Press F1 to read more about disk slices.";
	dialog_vars.help_file = systemHelpFile("partition", buf);
	if (!variable_get(VAR_NO_WARN))
	    xdialog_msgbox("Disk slicing warning:", p, -1, -1, 1);
	free(p);
	dlg_restore_vars(&save_vars);
    }
    restorescr(w);
}
#endif /* WITH_SLICES */

#if !defined(__ia64__)
static u_char *
bootalloc(char *name, size_t *size)
{
    char buf[FILENAME_MAX];
    struct stat sb;

    snprintf(buf, sizeof buf, "/boot/%s", name);
    if (stat(buf, &sb) != -1) {
	int fd;

	fd = open(buf, O_RDONLY);
	if (fd != -1) {
	    u_char *cp;

	    cp = malloc(sb.st_size);
	    if (read(fd, cp, sb.st_size) != sb.st_size) {
		free(cp);
		close(fd);
		msgDebug("bootalloc: couldn't read %ld bytes from %s\n", (long)sb.st_size, buf);
		return NULL;
	    }
	    close(fd);
	    if (size != NULL)
		*size = sb.st_size;
	    return cp;
	}
	msgDebug("bootalloc: couldn't open %s\n", buf);
    }
    else
	msgDebug("bootalloc: can't stat %s\n", buf);
    return NULL;
}
#endif /* !__ia64__ */

#ifdef WITH_SLICES 
static int
partitionHook(dialogMenuItem *selected)
{
    Device **devs = NULL;

    devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
    if (!devs) {
	msgConfirm("Unable to find disk %s!", selected->prompt);
	return DITEM_FAILURE;
    }
    diskPartition(devs[0]);
    return DITEM_SUCCESS;
}

int
diskPartitionEditor(dialogMenuItem *self)
{
    DMenu *menu;
    Device **devs;

    devs = deviceFind(variable_get(VAR_DISK), DEVICE_TYPE_DISK);
    if (devs == NULL) {
	msgConfirm("No disks found!  Please verify that your disk controller is being\n"
		   "properly probed at boot time.  See the Hardware Guide on the\n"
		   "Documentation menu for clues on diagnosing this type of problem.");
	return DITEM_FAILURE;
    }
    else {
	/* No disks are selected, fall-back case now */
        int cnt = deviceCount(devs);

	if (cnt == 1) {
	    if (variable_get(VAR_NONINTERACTIVE) &&
	      !variable_get(VAR_DISKINTERACTIVE))
		diskPartitionNonInteractive(devs[0]);
	    else
		diskPartition(devs[0]);
	    return DITEM_SUCCESS;
	}
	else {
            int result;

	    menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, partitionHook);
	    if (!menu) {
		msgConfirm("No devices suitable for installation found!\n\n"
			   "Please verify that your disk controller (and attached drives)\n"
			   "were detected properly.  This can be done by pressing the\n"
			   "[Scroll Lock] key and using the Arrow keys to move back to\n"
			   "the boot messages.  Press [Scroll Lock] again to return.");
		return DITEM_FAILURE;
	    }

	    result = dmenuOpen(menu) ? DITEM_SUCCESS : DITEM_FAILURE;
	    free(menu);
	    return result;
	}
    }
    return DITEM_SUCCESS;
}
#endif /* WITH_SLICES */

int
diskPartitionWrite(Device *dev)
{
    Disk *d = (Disk *)dev->private;
#if !defined(__ia64__)
    static u_char *boot1;
#endif
#if defined(__i386__) || defined(__amd64__)
    static u_char *boot2;
#endif

    if (!variable_cmp(DISK_PARTITIONED, "written"))
	return DITEM_SUCCESS;

#if defined(__i386__) || defined(__amd64__)
    if (!boot1) boot1 = bootalloc("boot1", NULL);
    if (!boot2) boot2 = bootalloc("boot2", NULL);
    Set_Boot_Blocks(d, boot1, boot2);
#elif !defined(__ia64__)
    if (!boot1) boot1 = bootalloc("boot1", NULL);
    Set_Boot_Blocks(d, boot1, NULL);
#endif

    msgNotify("Writing partition information to drive %s", d->name);
    if (!Fake && Write_Disk(d)) {
        msgConfirm("ERROR: Unable to write data to disk %s!", d->name);
        return DITEM_FAILURE;
    }

    /* Now it's not "yes", but "written" */
    variable_set2(DISK_PARTITIONED, "written", 0);
    return DITEM_SUCCESS | DITEM_RESTORE;
}

#ifdef WITH_SLICES
/* Partition a disk based wholly on which variables are set */
static void
diskPartitionNonInteractive(Device *dev)
{
    char *cp;
    int i, all_disk = 0;
    daddr_t sz;
#ifdef PC98
    u_char *bootipl;
    size_t bootipl_size;
    u_char *bootmenu;
    size_t bootmenu_size;
#else
    u_char *mbrContents;
    size_t mbrSize;
#endif
    Disk *d = (Disk *)dev->private;

    record_chunks(d);
    cp = variable_get(VAR_GEOMETRY);
    if (cp) {
	if (!strcasecmp(cp, "sane")) {
#ifdef PC98
	    if (d->bios_cyl >= 65536 || d->bios_hd > 256 || d->bios_sect >= 256)
#else
	    if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64)
#endif
	    {
		msgDebug("Warning:  A geometry of %lu/%lu/%lu for %s is incorrect.\n",
		    d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
		Sanitize_Bios_Geom(d);
		msgDebug("Sanitized geometry for %s is %lu/%lu/%lu.\n",
		    d->name, d->bios_cyl, d->bios_hd, d->bios_sect);
	    }
	} else {
	    msgDebug("Setting geometry from script to: %s\n", cp);
	    d->bios_cyl = strtol(cp, &cp, 0);
	    d->bios_hd = strtol(cp + 1, &cp, 0);
	    d->bios_sect = strtol(cp + 1, 0, 0);
	}
    }

    cp = variable_get(VAR_PARTITION);
    if (cp) {
	if (!strcmp(cp, "free")) {
	    /* Do free disk space case */
	    for (i = 0; chunk_info[i]; i++) {
		/* If a chunk is at least 10MB in size, use it. */
		if (chunk_info[i]->type == unused && chunk_info[i]->size > (10 * ONE_MEG)) {
		    Create_Chunk(d, chunk_info[i]->offset, chunk_info[i]->size,
				 freebsd, 3,
				 (chunk_info[i]->flags & CHUNK_ALIGN),
				 "FreeBSD");
		    variable_set2(DISK_PARTITIONED, "yes", 0);
		    break;
		}
	    }
	    if (!chunk_info[i]) {
		msgConfirm("Unable to find any free space on this disk!");
		return;
	    }
	}
	else if (!strcmp(cp, "all")) {
	    /* Do all disk space case */
	    msgDebug("Warning:  Devoting all of disk %s to FreeBSD.\n", d->name);

	    All_FreeBSD(d, FALSE);
	}
	else if (!strcmp(cp, "exclusive")) {
	    /* Do really-all-the-disk-space case */
	    msgDebug("Warning:  Devoting all of disk %s to FreeBSD.\n", d->name);

	    All_FreeBSD(d, all_disk = TRUE);
	}
	else if ((sz = strtoimax(cp, &cp, 0))) {
	    /* Look for sz bytes free */
	    if (*cp && toupper(*cp) == 'M')
		sz *= ONE_MEG;
	    else if (*cp && toupper(*cp) == 'G')
		sz *= ONE_GIG;
	    for (i = 0; chunk_info[i]; i++) {
		/* If a chunk is at least sz MB, use it. */
		if (chunk_info[i]->type == unused && chunk_info[i]->size >= sz) {
		    Create_Chunk(d, chunk_info[i]->offset, sz, freebsd, 3,
				 (chunk_info[i]->flags & CHUNK_ALIGN),
				 "FreeBSD");
		    variable_set2(DISK_PARTITIONED, "yes", 0);
		    break;
		}
	    }
	    if (!chunk_info[i]) {
		    msgConfirm("Unable to find %jd free blocks on this disk!",
			(intmax_t)sz);
		return;
	    }
	}
	else if (!strcmp(cp, "existing")) {
	    /* Do existing FreeBSD case */
	    for (i = 0; chunk_info[i]; i++) {
		if (chunk_info[i]->type == freebsd)
		    break;
	    }
	    if (!chunk_info[i]) {
		msgConfirm("Unable to find any existing FreeBSD partitions on this disk!");
		return;
	    }
	}
	else {
	    msgConfirm("`%s' is an invalid value for %s - is config file valid?", cp, VAR_PARTITION);
	    return;
	}
	if (!all_disk) {
#ifdef PC98
	    getBootMgr(d->name, &bootipl, &bootipl_size,
		       &bootmenu, &bootmenu_size);
	    Set_Boot_Mgr(d, bootipl, bootipl_size, bootmenu, bootmenu_size);
#else
	    getBootMgr(d->name, &mbrContents, &mbrSize);
	    Set_Boot_Mgr(d, mbrContents, mbrSize);
#endif
	}
	variable_set2(DISK_PARTITIONED, "yes", 0);
    }
}
#endif /* WITH_SLICES */