Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.15
      1 /*	$NetBSD: label.c,v 1.15 2019/12/11 19:23:37 martin Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Jonathan Stone
      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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Jonathan Stone.
     19  * 4. The name of Jonathan Stone may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
     24  * AND 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 PIERMONT INFORMATION SYSTEMS INC. BE
     27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33  * THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 __RCSID("$NetBSD: label.c,v 1.15 2019/12/11 19:23:37 martin Exp $");
     40 #endif
     41 
     42 #include <sys/types.h>
     43 #include <stddef.h>
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <fcntl.h>
     48 #include <util.h>
     49 #include <unistd.h>
     50 #include <sys/dkio.h>
     51 #include <sys/param.h>
     52 #include <sys/bootblock.h>
     53 #include <ufs/ffs/fs.h>
     54 
     55 #include "defs.h"
     56 #include "msg_defs.h"
     57 #include "menu_defs.h"
     58 
     59 /*
     60  * local prototypes
     61  */
     62 static bool boringpart(const struct disk_part_info *info);
     63 static bool checklabel(struct disk_partitions*, char[MENUSTRSIZE],
     64     char[MENUSTRSIZE]);
     65 static void show_partition_adder(menudesc *, struct partition_usage_set*);
     66 
     67 /*
     68  * Return 1 if a partition should be ignored when checking
     69  * for overlapping partitions.
     70  */
     71 static bool
     72 boringpart(const struct disk_part_info *info)
     73 {
     74 
     75 	if (info->size == 0)
     76 		return true;
     77 	if (info->flags &
     78 	     (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
     79 	     PTI_RAW_PART))
     80 		return true;
     81 
     82 	return false;
     83 }
     84 
     85 /*
     86  * We have some partitions in our "wanted" list that we may not edit,
     87  * like the RAW_PART in disklabel, some that just represent external
     88  * mount entries for the final fstab or similar.
     89  * We have previously sorted pset->parts and pset->infos to be in sync,
     90  * but the former "array" may be shorter.
     91  * Here are a few quick predicates to check for them.
     92  */
     93 static bool
     94 real_partition(const struct partition_usage_set *pset, int index)
     95 {
     96 	if (index < 0 || (size_t)index >= pset->num)
     97 		return false;
     98 
     99 	return pset->infos[index].cur_part_id != NO_PART;
    100 }
    101 
    102 /*
    103  * Check partitioning for overlapping partitions.
    104  * Returns 0 if no overlapping partition found, nonzero otherwise.
    105  * Sets reference arguments ovly1 and ovly2 to the indices of
    106  * overlapping partitions if any are found.
    107  */
    108 static bool
    109 checklabel(struct disk_partitions *parts,
    110     char ovl1[MENUSTRSIZE], char ovl2[MENUSTRSIZE])
    111 {
    112 	part_id i, j;
    113 	struct disk_part_info info;
    114 	daddr_t istart, iend, jstart, jend;
    115 	unsigned int fs_type, fs_sub_type;
    116 
    117 	for (i = 0; i < parts->num_part - 1; i ++ ) {
    118 		if (!parts->pscheme->get_part_info(parts, i, &info))
    119 			continue;
    120 
    121 		/* skip unused or reserved partitions */
    122 		if (boringpart(&info))
    123 			continue;
    124 
    125 		/*
    126 		 * check succeeding partitions for overlap.
    127 		 * O(n^2), but n is small (currently <= 16).
    128 		 */
    129 		istart = info.start;
    130 		iend = istart + info.size;
    131 		fs_type = info.fs_type;
    132 		fs_sub_type = info.fs_sub_type;
    133 
    134 		for (j = i+1; j < parts->num_part; j++) {
    135 
    136 			if (!parts->pscheme->get_part_info(parts, j, &info))
    137 				continue;
    138 
    139 			/* skip unused or reserved partitions */
    140 			if (boringpart(&info))
    141 				continue;
    142 
    143 			jstart = info.start;
    144 			jend = jstart + info.size;
    145 
    146 			/* overlap? */
    147 			if ((istart <= jstart && jstart < iend) ||
    148 			    (jstart <= istart && istart < jend)) {
    149 				snprintf(ovl1, sizeof(*ovl1),
    150 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    151 				    istart / sizemult, iend / sizemult,
    152 				    multname,
    153 				    getfslabelname(fs_type, fs_sub_type));
    154 				snprintf(ovl2, sizeof(*ovl2),
    155 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    156 				    jstart / sizemult, jend / sizemult,
    157 				    multname,
    158 				    getfslabelname(info.fs_type,
    159 				        info.fs_sub_type));
    160 				return false;
    161 			}
    162 		}
    163 	}
    164 
    165 	return true;
    166 }
    167 
    168 int
    169 checkoverlap(struct disk_partitions *parts)
    170 {
    171 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
    172 	if (!checklabel(parts, desc1, desc2)) {
    173 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
    174 		return 1;
    175 	}
    176 	return 0;
    177 }
    178 
    179 /*
    180  * return (see post_edit_verify):
    181  *  0 -> abort
    182  *  1 -> re-edit
    183  *  2 -> continue installation
    184  */
    185 static int
    186 verify_parts(struct partition_usage_set *pset)
    187 {
    188 	struct part_usage_info *wanted;
    189 	struct disk_partitions *parts;
    190 	size_t i, num_root;
    191 	daddr_t first_bsdstart, first_bsdsize, inst_start, inst_size;
    192 	int rv;
    193 
    194 	first_bsdstart = first_bsdsize = 0;
    195 	inst_start = inst_size = 0;
    196 	num_root = 0;
    197 	parts = pset->parts;
    198 	for (i = 0; i < pset->num; i++) {
    199 		wanted = &pset->infos[i];
    200 
    201 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
    202 			continue;
    203 		if (wanted->cur_part_id == NO_PART)
    204 			continue;
    205 		if (!(wanted->instflags & PUIINST_MOUNT))
    206 			continue;
    207 		if (strcmp(wanted->mount, "/") != 0)
    208 			continue;
    209 		num_root++;
    210 
    211 		if (first_bsdstart == 0) {
    212 			first_bsdstart = wanted->cur_start;
    213 			first_bsdsize = wanted->size;
    214 		}
    215 		if (inst_start == 0 && wanted->cur_start == pm->ptstart) {
    216 			inst_start = wanted->cur_start;
    217 			inst_size = wanted->size;
    218 		}
    219 	}
    220 
    221 	if (num_root == 0 ||
    222 	    (num_root > 1 && inst_start == 0)) {
    223 		if (num_root == 0)
    224 			msg_display_subst(MSG_must_be_one_root, 2,
    225 			    msg_string(parts->pscheme->name),
    226 			    msg_string(parts->pscheme->short_name));
    227 		else
    228 			msg_display_subst(MSG_multbsdpart, 2,
    229 			    msg_string(parts->pscheme->name),
    230 			    msg_string(parts->pscheme->short_name));
    231 		rv = ask_reedit(parts);
    232 		if (rv != 2)
    233 			return rv;
    234 	}
    235 
    236 	if (pm->ptstart == 0) {
    237 		if (inst_start > 0) {
    238 			pm->ptstart = inst_start;
    239 			pm->ptsize = inst_size;
    240 		} else if (first_bsdstart > 0) {
    241 			pm->ptstart = first_bsdstart;
    242 			pm->ptsize = first_bsdsize;
    243 		} else if (parts->pscheme->guess_install_target &&
    244 			   parts->pscheme->guess_install_target(
    245 			   parts, &inst_start, &inst_size)) {
    246 			pm->ptstart = inst_start;
    247 			pm->ptsize = inst_size;
    248 		}
    249 	}
    250 
    251 	/* Check for overlaps */
    252 	if (checkoverlap(parts) != 0) {
    253 		rv = ask_reedit(parts);
    254 		if (rv != 2)
    255 			return rv;
    256 	}
    257 
    258 	/*
    259 	 * post_edit_verify returns:
    260 	 *  0 -> abort
    261 	 *  1 -> re-edit
    262 	 *  2 -> continue installation
    263 	 */
    264 	if (parts->pscheme->post_edit_verify)
    265 		return parts->pscheme->post_edit_verify(parts, false);
    266 
    267 	return 2;
    268 }
    269 
    270 static int
    271 edit_fs_start(menudesc *m, void *arg)
    272 {
    273 	struct single_part_fs_edit *edit = arg;
    274 	daddr_t start, end;
    275 
    276 	start = getpartoff(edit->pset->parts, edit->info.start);
    277 	if (edit->info.size != 0) {
    278 		/* Try to keep end in the same place */
    279 		end = edit->info.start + edit->info.size;
    280 		if (end < start)
    281 			edit->info.size = edit->pset->parts->pscheme->
    282 			    max_free_space_at(edit->pset->parts,
    283 			    edit->info.start);
    284 		else
    285 			edit->info.size = end - start;
    286 	}
    287 	edit->info.start = start;
    288 	return 0;
    289 }
    290 
    291 static int
    292 edit_fs_size(menudesc *m, void *arg)
    293 {
    294 	struct single_part_fs_edit *edit = arg;
    295 	daddr_t size;
    296 
    297 	size = getpartsize(edit->pset->parts, edit->info.start,
    298 	    edit->info.size);
    299 	if (size < 0)
    300 		return 0;
    301 	if (size > edit->pset->parts->disk_size)
    302 		size = edit->pset->parts->disk_size - edit->info.start;
    303 	edit->info.size = size;
    304 	return 0;
    305 }
    306 
    307 static int
    308 edit_fs_preserve(menudesc *m, void *arg)
    309 {
    310 	struct single_part_fs_edit *edit = arg;
    311 
    312 	edit->wanted->instflags ^= PUIINST_NEWFS;
    313 	return 0;
    314 }
    315 
    316 static int
    317 edit_install(menudesc *m, void *arg)
    318 {
    319 	struct single_part_fs_edit *edit = arg;
    320 
    321 	if (edit->info.start == pm->ptstart)
    322 		pm->ptstart = 0;
    323 	else
    324 		pm->ptstart = edit->info.start;
    325 	return 0;
    326 }
    327 
    328 static int
    329 edit_fs_mount(menudesc *m, void *arg)
    330 {
    331 	struct single_part_fs_edit *edit = arg;
    332 
    333 	edit->wanted->instflags ^= PUIINST_MOUNT;
    334 	return 0;
    335 }
    336 
    337 static int
    338 edit_fs_mountpt(menudesc *m, void *arg)
    339 {
    340 	struct single_part_fs_edit *edit = arg;
    341 	char *p, *first, *last, buf[MOUNTLEN];
    342 
    343 	strlcpy(buf, edit->wanted->mount, sizeof buf);
    344 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
    345 		buf, buf, MOUNTLEN);
    346 
    347 	/*
    348 	 * Trim all leading and trailing whitespace
    349 	 */
    350 	for (first = NULL, last = NULL, p = buf; *p; p++) {
    351 		if (isspace((unsigned char)*p))
    352 			continue;
    353 		if (first == NULL)
    354 			first = p;
    355 		last = p;
    356 	}
    357 	if (last != NULL)
    358 		last[1] = 0;
    359 
    360 	if (*first == 0 || strcmp(first, "none") == 0) {
    361 		edit->wanted->mount[0] = 0;
    362 		edit->wanted->instflags &= ~PUIINST_MOUNT;
    363 		return 0;
    364 	}
    365 
    366 	if (*first != '/') {
    367 		edit->wanted->mount[0] = '/';
    368 		strlcpy(&edit->wanted->mount[1], first,
    369 		    sizeof(edit->wanted->mount)-1);
    370 	} else {
    371 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
    372 	}
    373 
    374 	return 0;
    375 }
    376 
    377 static int
    378 edit_restore(menudesc *m, void *arg)
    379 {
    380 	struct single_part_fs_edit *edit = arg;
    381 
    382 	edit->info = edit->old_info;
    383 	*edit->wanted = edit->old_usage;
    384 	return 0;
    385 }
    386 
    387 static int
    388 edit_cancel(menudesc *m, void *arg)
    389 {
    390 	struct single_part_fs_edit *edit = arg;
    391 
    392 	edit->rv = -1;
    393 	return 1;
    394 }
    395 
    396 static int
    397 edit_delete_ptn(menudesc *m, void *arg)
    398 {
    399 	struct single_part_fs_edit *edit = arg;
    400 
    401 	edit->rv = -2;
    402 	return 1;
    403 }
    404 
    405 /*
    406  * We have added/removed partitions, all cur_part_id values are
    407  * out of sync. Re-fetch and reorder partitions accordingly.
    408  */
    409 static void
    410 renumber_partitions(struct partition_usage_set *pset)
    411 {
    412 	struct part_usage_info *ninfos;
    413 	struct disk_part_info info;
    414 	size_t i;
    415 	part_id pno;
    416 
    417 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
    418 	if (ninfos == NULL) {
    419 		err_msg_win(err_outofmem);
    420 		return;
    421 	}
    422 
    423 	for (pno = 0; pno < pset->parts->num_part; pno++) {
    424 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
    425 		    &info))
    426 			continue;
    427 		for (i = 0; i < pset->parts->num_part; i++) {
    428 			if (pset->infos[i].cur_start != info.start)
    429 				continue;
    430 			memcpy(&ninfos[pno], &pset->infos[i],
    431 			    sizeof(ninfos[pno]));
    432 			ninfos[pno].cur_part_id = pno;
    433 			break;
    434 		}
    435 	}
    436 
    437 	memcpy(pset->infos, ninfos, sizeof(*pset->infos)*pset->parts->num_part);
    438 	free(ninfos);
    439 }
    440 
    441 /*
    442  * Most often used file system types, we offer them in a first level menu.
    443  */
    444 static const uint edit_fs_common_types[] =
    445     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
    446 
    447 /*
    448  * Functions for uncommon file system types - we offer the full list,
    449  * but put FFSv2 and FFSv1 at the front.
    450  */
    451 static void
    452 init_fs_type_ext(menudesc *menu, void *arg)
    453 {
    454 	struct single_part_fs_edit *edit = arg;
    455 	uint t = edit->info.fs_type;
    456 	size_t i, ndx, max = menu->numopts;
    457 
    458 	if (t == FS_BSDFFS) {
    459 		if (edit->info.fs_sub_type == 2)
    460 			menu->cursel = 0;
    461 		else
    462 			menu->cursel = 1;
    463 		return;
    464 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
    465 		menu->cursel = FSMAXTYPES;
    466 		return;
    467 	}
    468 	/* skip the two FFS entries, and do not add FFS later again */
    469 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    470 		if (i == FS_UNUSED)
    471 			continue;
    472 		if (i == FS_BSDFFS)
    473 			continue;
    474 		if (fstypenames[i] == NULL)
    475 			continue;
    476 
    477 		if (i == t) {
    478 			menu->cursel = ndx;
    479 			break;
    480 		}
    481 		ndx++;
    482 	}
    483 }
    484 
    485 static int
    486 set_fstype_ext(menudesc *menu, void *arg)
    487 {
    488 	struct single_part_fs_edit *edit = arg;
    489 	size_t i, ndx, max = menu->numopts;
    490 
    491 	if (menu->cursel == 0 || menu->cursel == 1) {
    492 		edit->info.fs_type = FS_BSDFFS;
    493 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    494 		goto found_type;
    495 	} else if (menu->cursel == FSMAXTYPES) {
    496 		edit->info.fs_type = FS_EX2FS;
    497 		edit->info.fs_sub_type = 1;
    498 		goto found_type;
    499 	}
    500 
    501 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    502 		if (i == FS_UNUSED)
    503 			continue;
    504 		if (i == FS_BSDFFS)
    505 			continue;
    506 		if (fstypenames[i] == NULL)
    507 			continue;
    508 
    509 		if (ndx == (size_t)menu->cursel) {
    510 			edit->info.fs_type = i;
    511 			edit->info.fs_sub_type = 0;
    512 			goto found_type;
    513 		}
    514 		ndx++;
    515 	}
    516 	return 1;
    517 
    518 found_type:
    519 	edit->info.nat_type = edit->pset->parts->pscheme->
    520 	    get_fs_part_type(edit->info.fs_type, edit->info.fs_sub_type);
    521 	if (edit->info.nat_type == NULL)
    522 		edit->info.nat_type = edit->pset->parts->pscheme->
    523 		    get_generic_part_type(PT_root);
    524 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    525 	edit->wanted->fs_type = edit->info.fs_type;
    526 	edit->wanted->fs_version = edit->info.fs_sub_type;
    527 	return 1;
    528 }
    529 
    530 /*
    531  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
    532  * skip later FFS entry in the generic list.
    533  */
    534 static int
    535 edit_fs_type_ext(menudesc *menu, void *arg)
    536 {
    537 	menu_ent *opts;
    538 	int m;
    539 	size_t i, ndx, cnt;
    540 
    541 	cnt = __arraycount(fstypenames);
    542 	opts = calloc(cnt, sizeof(*opts));
    543 	if (opts == NULL)
    544 		return 1;
    545 
    546 	ndx = 0;
    547 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
    548 	opts[ndx].opt_action = set_fstype_ext;
    549 	ndx++;
    550 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
    551 	opts[ndx].opt_action = set_fstype_ext;
    552 	ndx++;
    553 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
    554 		if (i == FS_UNUSED)
    555 			continue;
    556 		if (i == FS_BSDFFS)
    557 			continue;
    558 		if (fstypenames[i] == NULL)
    559 			continue;
    560 		opts[ndx].opt_name = fstypenames[i];
    561 		opts[ndx].opt_action = set_fstype_ext;
    562 		ndx++;
    563 	}
    564 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
    565 	opts[ndx].opt_action = set_fstype_ext;
    566 	ndx++;
    567 	assert(ndx == cnt);
    568 	m = new_menu(MSG_Select_the_type, opts, ndx,
    569 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    570 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
    571 
    572 	if (m < 0)
    573 		return 1;
    574 	process_menu(m, arg);
    575 	free_menu(m);
    576 	free(opts);
    577 
    578 	return 1;
    579 }
    580 
    581 static void
    582 init_fs_type(menudesc *menu, void *arg)
    583 {
    584 	struct single_part_fs_edit *edit = arg;
    585 	size_t i;
    586 
    587 	/* init menu->cursel from fs type in arg */
    588 	if (edit->info.fs_type == FS_BSDFFS) {
    589 		if (edit->info.fs_sub_type == 2)
    590 			menu->cursel = 0;
    591 		else
    592 			menu->cursel = 1;
    593 	}
    594 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
    595 		if (edit->info.fs_type == edit_fs_common_types[i]) {
    596 			menu->cursel = i+1;
    597 			break;
    598 		}
    599 	}
    600 }
    601 
    602 static int
    603 set_fstype(menudesc *menu, void *arg)
    604 {
    605 	struct single_part_fs_edit *edit = arg;
    606 	int ndx;
    607 
    608 	if (menu->cursel < 2) {
    609 		edit->info.fs_type = FS_BSDFFS;
    610 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    611 		edit->info.nat_type = edit->pset->parts->pscheme->
    612 		    get_fs_part_type(FS_BSDFFS, 2);
    613 		if (edit->info.nat_type == NULL)
    614 			edit->info.nat_type = edit->pset->parts->
    615 			    pscheme->get_generic_part_type(PT_root);
    616 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    617 		edit->wanted->fs_type = edit->info.fs_type;
    618 		edit->wanted->fs_version = edit->info.fs_sub_type;
    619 		return 1;
    620 	}
    621 	ndx = menu->cursel-1;
    622 
    623 	if (ndx < 0 ||
    624 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
    625 		return 1;
    626 
    627 	edit->info.fs_type = edit_fs_common_types[ndx];
    628 	edit->info.fs_sub_type = 0;
    629 	edit->info.nat_type = edit->pset->parts->pscheme->
    630 	    get_fs_part_type(edit->info.fs_type, 0);
    631 	if (edit->info.nat_type == NULL)
    632 		edit->info.nat_type = edit->pset->parts->
    633 		    pscheme->get_generic_part_type(PT_root);
    634 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    635 	edit->wanted->fs_type = edit->info.fs_type;
    636 	edit->wanted->fs_version = edit->info.fs_sub_type;
    637 	return 1;
    638 }
    639 
    640 /*
    641  * Offer a menu selecting the common file system types
    642  */
    643 static int
    644 edit_fs_type(menudesc *menu, void *arg)
    645 {
    646 	struct single_part_fs_edit *edit = arg;
    647 	menu_ent *opts;
    648 	int m, cnt;
    649 	size_t i;
    650 
    651 	/*
    652 	 * Shortcut to full menu if we have an exotic value
    653 	 */
    654 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
    655 		edit_fs_type_ext(menu, arg);
    656 		return 0;
    657 	}
    658 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
    659 		if (edit->info.fs_type == edit_fs_common_types[i])
    660 			break;
    661 	if (i >= __arraycount(edit_fs_common_types)) {
    662 		edit_fs_type_ext(menu, arg);
    663 		return 0;
    664 	}
    665 
    666 	/*
    667 	 * Starting with a common type, show short menu first
    668 	 */
    669 	cnt = __arraycount(edit_fs_common_types) + 2;
    670 	opts = calloc(cnt, sizeof(*opts));
    671 	if (opts == NULL)
    672 		return 0;
    673 
    674 	/* special case entry 0: two FFS entries */
    675 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
    676 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
    677 		opts[i+1].opt_action = set_fstype;
    678 	}
    679 	/* duplicate FFS (at offset 1) into first entry */
    680 	opts[0] = opts[1];
    681 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
    682 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
    683 	/* add secondary sub-menu */
    684 	assert(i+1 < (size_t)cnt);
    685 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
    686 	opts[i+1].opt_action = edit_fs_type_ext;
    687 
    688 	m = new_menu(MSG_Select_the_type, opts, cnt,
    689 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
    690 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
    691 
    692 	if (m < 0)
    693 		return 0;
    694 	process_menu(m, arg);
    695 	free_menu(m);
    696 	free(opts);
    697 
    698 	return 0;
    699 }
    700 
    701 
    702 static void update_edit_ptn_menu(menudesc *m, void *arg);
    703 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
    704 static int edit_ptn_custom_type(menudesc *m, void *arg);
    705 
    706 int
    707 edit_ptn(menudesc *menu, void *arg)
    708 {
    709 	struct partition_usage_set *pset = arg;
    710 	struct single_part_fs_edit edit;
    711 	int fspart_menu, num_opts;
    712 	const char *err;
    713 	menu_ent *mopts, *popt;
    714 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
    715 
    716 	static const menu_ent edit_ptn_fields_head[] = {
    717 		{ .opt_action=edit_fs_type },
    718 		{ .opt_action=edit_fs_start },
    719 		{ .opt_action=edit_fs_size },
    720 		{ .opt_flags=OPT_IGNORE },
    721 	};
    722 
    723 	static const menu_ent edit_ptn_fields_head_add[] = {
    724 		{ .opt_action=edit_install },
    725 	};
    726 
    727 	static const menu_ent edit_ptn_fields_head2[] = {
    728 		{ .opt_action=edit_fs_preserve },
    729 		{ .opt_action=edit_fs_mount },
    730 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    731 		{ .opt_action=edit_fs_mountpt },
    732 	};
    733 	static const menu_ent edit_ptn_fields_tail[] = {
    734 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    735 		  .opt_flags=OPT_SUB },
    736 		{ .opt_name=MSG_restore,
    737 		  .opt_action=edit_restore},
    738 		{ .opt_name=MSG_Delete_partition,
    739 		  .opt_action=edit_delete_ptn},
    740 		{ .opt_name=MSG_cancel,
    741 		  .opt_action=edit_cancel},
    742 	};
    743 
    744 	memset(&edit, 0, sizeof edit);
    745 	edit.pset = pset;
    746 	edit.index = menu->cursel;
    747 	edit.wanted = &pset->infos[edit.index];
    748 
    749 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
    750 		return 0;
    751 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
    752 
    753 	num_opts = __arraycount(edit_ptn_fields_head) +
    754 	    __arraycount(edit_ptn_fields_head2) +
    755 	    __arraycount(edit_ptn_fields_tail);
    756 	if (with_inst_opt)
    757 		num_opts += __arraycount(edit_ptn_fields_head_add);
    758 	if (is_new_part)
    759 		num_opts--;
    760 	else
    761 		num_opts += pset->parts->pscheme->custom_attribute_count;
    762 
    763 	mopts = calloc(num_opts, sizeof(*mopts));
    764 	if (mopts == NULL) {
    765 		err_msg_win(err_outofmem);
    766 		return 0;
    767 	}
    768 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
    769 	popt = mopts + __arraycount(edit_ptn_fields_head);
    770 	if (with_inst_opt) {
    771 		memcpy(popt, edit_ptn_fields_head_add,
    772 		    sizeof(edit_ptn_fields_head_add));
    773 		popt +=  __arraycount(edit_ptn_fields_head_add);
    774 	}
    775 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
    776 	popt +=  __arraycount(edit_ptn_fields_head2);
    777 	edit.first_custom_attr = popt - mopts;
    778 	if (!is_new_part) {
    779 		for (size_t i = 0;
    780 		    i < pset->parts->pscheme->custom_attribute_count;
    781 		    i++, popt++) {
    782 			popt->opt_action = edit_ptn_custom_type;
    783 		}
    784 	}
    785 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
    786 	popt += __arraycount(edit_ptn_fields_tail) - 1;
    787 	if (is_new_part)
    788 		memcpy(popt-1, popt, sizeof(*popt));
    789 
    790 	if (is_new_part) {
    791 		struct disk_part_free_space space;
    792 		daddr_t align = pset->parts->pscheme->get_part_alignment(
    793 		    pset->parts);
    794 
    795 		edit.id = NO_PART;
    796 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
    797 		    &space, 1, align, align, -1, -1) == 1) {
    798 			edit.info.start = space.start;
    799 			edit.info.size = space.size;
    800 			edit.info.fs_type = FS_BSDFFS;
    801 			edit.info.fs_sub_type = 2;
    802 			edit.info.nat_type = pset->parts->pscheme->
    803 			    get_fs_part_type(edit.info.fs_type,
    804 			    edit.info.fs_sub_type);
    805 			edit.wanted->instflags = PUIINST_NEWFS;
    806 		}
    807 	} else {
    808 		edit.id = pset->infos[edit.index].cur_part_id;
    809 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
    810 		    &edit.info)) {
    811 			free(mopts);
    812 			return 0;
    813 		}
    814 	}
    815 
    816 	edit.old_usage = *edit.wanted;
    817 	edit.old_info = edit.info;
    818 
    819 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
    820 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
    821 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
    822 		NULL, MSG_OK);
    823 
    824 	process_menu(fspart_menu, &edit);
    825 	free(mopts);
    826 	free_menu(fspart_menu);
    827 
    828 	if (edit.rv == 0) {	/* OK, set new data */
    829 		edit.info.last_mounted = edit.wanted->mount;
    830 		if (is_new_part) {
    831 			edit.wanted->cur_part_id = pset->parts->pscheme->
    832 			    add_partition(pset->parts, &edit.info, &err);
    833 			if (edit.wanted->cur_part_id == NO_PART)
    834 				err_msg_win(err);
    835 			else {
    836 				pset->parts->pscheme->get_part_info(
    837 				    pset->parts, edit.wanted->cur_part_id,
    838 				    &edit.info);
    839 				edit.wanted->cur_start = edit.info.start;
    840 				edit.wanted->size = edit.info.size;
    841 				edit.wanted->type =
    842 				    edit.info.nat_type->generic_ptype;
    843 				edit.wanted->fs_type = edit.info.fs_type;
    844 				edit.wanted->fs_version = edit.info.fs_sub_type;
    845 				/* things have changed, re-sort */
    846 				renumber_partitions(pset);
    847 			}
    848 		} else {
    849 			if (!pset->parts->pscheme->set_part_info(pset->parts,
    850 			    edit.id, &edit.info, &err))
    851 				err_msg_win(err);
    852 		}
    853 
    854 		/*
    855 		 * if size has changed, we may need to add or remove
    856 		 * the option to add partitions
    857 		 */
    858 		show_partition_adder(menu, pset);
    859 	} else if (edit.rv == -1) {	/* cancel edit */
    860 		if (is_new_part) {
    861 			memmove(pset->infos+edit.index,
    862 			    pset->infos+edit.index+1,
    863 			    sizeof(*pset->infos)*(pset->num-edit.index));
    864 			memmove(menu->opts+edit.index,
    865 			    menu->opts+edit.index+1,
    866 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
    867 			menu->numopts--;
    868 			menu->cursel = 0;
    869 			pset->num--;
    870 			return -1;
    871 		}
    872 		pset->infos[edit.index] = edit.old_usage;
    873 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
    874 		if (!pset->parts->pscheme->delete_partition(pset->parts,
    875 		    edit.id, &err)) {
    876 			err_msg_win(err);
    877 			return 0;
    878 		}
    879 		memmove(pset->infos+edit.index,
    880 		    pset->infos+edit.index+1,
    881 		    sizeof(*pset->infos)*(pset->num-edit.index));
    882 		memmove(menu->opts+edit.index,
    883 		    menu->opts+edit.index+1,
    884 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
    885 		menu->numopts--;
    886 		menu->cursel = 0;
    887 
    888 		/* things have changed, re-sort */
    889 		pset->num--;
    890 		renumber_partitions(pset);
    891 
    892 		/* we can likely add new partitions now */
    893 		show_partition_adder(menu, pset);
    894 
    895 		return -1;
    896 	}
    897 
    898 	return 0;
    899 }
    900 
    901 static void
    902 update_edit_ptn_menu(menudesc *m, void *arg)
    903 {
    904 	struct single_part_fs_edit *edit = arg;
    905 	int i;
    906 	uint t = edit->info.fs_type;
    907 	size_t attr_no;
    908 
    909 	/* Determine which of the properties can be changed */
    910 	for (i = 0; i < m->numopts; i++) {
    911 		if (m->opts[i].opt_action == NULL &&
    912 		    m->opts[i].opt_menu != MENU_mountoptions)
    913 			continue;
    914 
    915 		/* Default to disabled... */
    916 		m->opts[i].opt_flags |= OPT_IGNORE;
    917 		if ((t == FS_UNUSED || t == FS_SWAP) &&
    918 		    (m->opts[i].opt_action == edit_fs_preserve ||
    919 		     m->opts[i].opt_action == edit_fs_mount ||
    920 		     m->opts[i].opt_action == edit_fs_mountpt ||
    921 		     m->opts[i].opt_menu == MENU_mountoptions))
    922 			continue;
    923 		if (m->opts[i].opt_action == edit_install &&
    924 		    edit->info.nat_type &&
    925 		    edit->info.nat_type->generic_ptype != PT_root)
    926 			/* can only install onto PT_root partitions */
    927 			continue;
    928 		if (m->opts[i].opt_action == edit_fs_preserve &&
    929 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
    930 		    t != FS_MSDOS && t != FS_EX2FS) {
    931 			/* Can not newfs this filesystem */
    932 			edit->wanted->instflags &= ~PUIINST_NEWFS;
    933 			continue;
    934 		}
    935 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
    936 			attr_no = (size_t)i - edit->first_custom_attr;
    937 			if (!edit->pset->parts->pscheme->
    938 			    custom_attribute_writable(
    939 			    edit->pset->parts, edit->id, attr_no))
    940 				continue;
    941 		}
    942 		/* Ok: we want this one */
    943 		m->opts[i].opt_flags &= ~OPT_IGNORE;
    944 	}
    945 }
    946 
    947 static void
    948 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
    949 {
    950 	struct single_part_fs_edit *edit = arg;
    951 	static int col_width;
    952 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
    953 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
    954 	     *ptn_install;
    955 	const char *c;
    956 	char val[MENUSTRSIZE];
    957 	const char *attrname;
    958 	size_t attr_no;
    959 
    960 	if (col_width == 0) {
    961 		int l;
    962 
    963 #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
    964 			if (l > col_width) col_width = l
    965 
    966 		LOAD(ptn_type);
    967 		LOAD(ptn_start);
    968 		LOAD(ptn_size);
    969 		LOAD(ptn_end);
    970 		LOAD(ptn_install);
    971 		LOAD(ptn_newfs);
    972 		LOAD(ptn_mount);
    973 		LOAD(ptn_mount_options);
    974 		LOAD(ptn_mountpt);
    975 #undef LOAD
    976 
    977 		for (size_t i = 0;
    978 		    i < edit->pset->parts->pscheme->custom_attribute_count;
    979 		    i++) {
    980 			attrname = msg_string(
    981 			    edit->pset->parts->pscheme->custom_attributes[i]
    982 			    .label);
    983 			l = strlen(attrname);
    984 			if (l > col_width) col_width = l;
    985 		}
    986 
    987 		col_width += 3;
    988 	}
    989 
    990 	if (m->opts[opt].opt_flags & OPT_IGNORE
    991 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
    992 	    && m->opts[opt].opt_action != edit_ptn_custom_type) {
    993 		wprintw(m->mw, "%*s -", col_width, "");
    994 		return;
    995 	}
    996 
    997 	if (opt < 4) {
    998 		switch (opt) {
    999 		case 0:
   1000 			if (edit->info.fs_type == FS_BSDFFS)
   1001 				if (edit->info.fs_sub_type == 2)
   1002 					c = msg_string(MSG_fs_type_ffsv2);
   1003 				else
   1004 					c = msg_string(MSG_fs_type_ffs);
   1005 			else
   1006 				c = getfslabelname(edit->info.fs_type,
   1007 				    edit->info.fs_sub_type);
   1008 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
   1009 			return;
   1010 		case 1:
   1011 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1012 			    ptn_start, edit->info.start / sizemult, multname);
   1013 			return;
   1014 		case 2:
   1015 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1016 			    ptn_size, edit->info.size / sizemult, multname);
   1017 			return;
   1018 		case 3:
   1019 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1020 			    ptn_end, (edit->info.start + edit->info.size)
   1021 			    / sizemult, multname);
   1022 			return;
   1023 		 }
   1024 	}
   1025 	if (m->opts[opt].opt_action == edit_install) {
   1026 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
   1027 			msg_string(edit->info.start == pm->ptstart
   1028 			    ? MSG_Yes : MSG_No));
   1029 		return;
   1030 	}
   1031 	if (m->opts[opt].opt_action == edit_fs_preserve) {
   1032 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
   1033 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
   1034 			    ? MSG_Yes : MSG_No));
   1035 		return;
   1036 	}
   1037 	if (m->opts[opt].opt_action == edit_fs_mount) {
   1038 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
   1039 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
   1040 			    ? MSG_Yes : MSG_No));
   1041 		return;
   1042 	}
   1043 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
   1044 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
   1045 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
   1046 			wprintw(m->mw, "async ");
   1047 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
   1048 			wprintw(m->mw, "noatime ");
   1049 		if (edit->wanted->mountflags & PUIMNT_NODEV)
   1050 			wprintw(m->mw, "nodev ");
   1051 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
   1052 			wprintw(m->mw, "nodevmtime ");
   1053 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
   1054 			wprintw(m->mw, "noexec ");
   1055 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
   1056 			wprintw(m->mw, "nosuid ");
   1057 		if (edit->wanted->mountflags & PUIMNT_LOG)
   1058 			wprintw(m->mw, "log ");
   1059 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
   1060 			wprintw(m->mw, "noauto  ");
   1061 		return;
   1062 	}
   1063 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
   1064 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
   1065 		    edit->wanted->mount);
   1066 		return;
   1067 	}
   1068 
   1069 	attr_no = opt - edit->first_custom_attr;
   1070 	edit->pset->parts->pscheme->format_custom_attribute(
   1071 	    edit->pset->parts, edit->id, attr_no, &edit->info,
   1072 	    val, sizeof val);
   1073 	attrname = msg_string(edit->pset->parts->pscheme->
   1074 	    custom_attributes[attr_no].label);
   1075 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
   1076 }
   1077 
   1078 static int
   1079 edit_ptn_custom_type(menudesc *m, void *arg)
   1080 {
   1081 	struct single_part_fs_edit *edit = arg;
   1082 	size_t attr_no = m->cursel - edit->first_custom_attr;
   1083 	char line[STRSIZE];
   1084 
   1085 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
   1086 	case pet_bool:
   1087 		edit->pset->parts->pscheme->custom_attribute_toggle(
   1088 		    edit->pset->parts, edit->id, attr_no);
   1089 		break;
   1090 	case pet_cardinal:
   1091 	case pet_str:
   1092 		edit->pset->parts->pscheme->format_custom_attribute(
   1093 		    edit->pset->parts, edit->id, attr_no, &edit->info,
   1094 		    line, sizeof(line));
   1095 		msg_prompt_win(
   1096 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
   1097 		    label, -1, 18, 0, 0, line, line, sizeof(line));
   1098 		edit->pset->parts->pscheme->custom_attribute_set_str(
   1099 		    edit->pset->parts, edit->id, attr_no, line);
   1100 		break;
   1101 	}
   1102 
   1103 	return 0;
   1104 }
   1105 
   1106 
   1107 /*
   1108  * Some column width depend on translation, we will set these in
   1109  * fmt_fspart_header and later use it when formatting single entries
   1110  * in fmt_fspart_row.
   1111  * The table consist of 3 "size like" columns, all fixed width, then
   1112  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
   1113  * (which is variable width).
   1114  */
   1115 static int fstype_width, flags_width;
   1116 static char fspart_separator[MENUSTRSIZE];
   1117 static char fspart_title[2*MENUSTRSIZE];
   1118 
   1119 /*
   1120  * Format the header of the main partition editor menu.
   1121  */
   1122 static void
   1123 fmt_fspart_header(menudesc *menu, void *arg)
   1124 {
   1125 	struct partition_usage_set *pset = arg;
   1126 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
   1127 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
   1128 	const char *fstype, *flags;
   1129 	int i;
   1130 	size_t ptn;
   1131 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
   1132 
   1133 	with_clone = false;
   1134 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
   1135 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1136 			with_clone = true;
   1137 	humanize_number(total, sizeof total,
   1138 	    pset->parts->disk_size * 512,
   1139 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1140 	humanize_number(free_space, sizeof free_space,
   1141 	    pset->cur_free_space * 512,
   1142 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1143 
   1144 	if (with_clone)
   1145 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
   1146 	else
   1147 		desc[0] = 0;
   1148 	if (pset->parts->pscheme->part_flag_desc)
   1149 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
   1150 		sizeof desc);
   1151 
   1152 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
   1153 	    msg_string(pset->parts->pscheme->name),
   1154 	    msg_string(pset->parts->pscheme->short_name),
   1155 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
   1156 	    desc, total, free_space);
   1157 
   1158 	snprintf(scol, sizeof scol, "%s (%s)",
   1159 	    msg_string(MSG_ptnheaders_start), multname);
   1160 	snprintf(ecol, sizeof ecol, "%s (%s)",
   1161 	    msg_string(MSG_ptnheaders_end), multname);
   1162 	snprintf(szcol, sizeof szcol, "%s (%s)",
   1163 	    msg_string(MSG_ptnheaders_size), multname);
   1164 
   1165 	fstype = msg_string(MSG_ptnheaders_fstype);
   1166 	flags = msg_string(MSG_part_header_col_flag);
   1167 	fstype_width = max(strlen(fstype), 8);
   1168 	flags_width = strlen(flags);
   1169 	for (i = 0, p = sepline; i < fstype_width; i++)
   1170 		*p++ = '-';
   1171 	for (i = 0, *p++ = ' '; i < flags_width; i++)
   1172 		*p++ = '-';
   1173 	*p = 0;
   1174 
   1175 	snprintf(fspart_separator, sizeof(fspart_separator),
   1176 	    "------------ ------------ ------------ %s ----------------",
   1177 	    sepline);
   1178 
   1179 	snprintf(fspart_title, sizeof(fspart_title),
   1180 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
   1181 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
   1182 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
   1183 	    fspart_separator);
   1184 
   1185 	msg_table_add("\n\n");
   1186 }
   1187 
   1188 /*
   1189  * Format one partition entry in the main partition editor.
   1190  */
   1191 static void
   1192 fmt_fspart_row(menudesc *m, int ptn, void *arg)
   1193 {
   1194 	struct partition_usage_set *pset = arg;
   1195 	struct disk_part_info info;
   1196 	daddr_t poffset, psize, pend;
   1197 	const char *desc;
   1198 	static const char *Yes;
   1199 	char flag_str[MENUSTRSIZE], *fp;
   1200 	unsigned inst_flags;
   1201 #ifndef NO_CLONES
   1202 	size_t clone_cnt;
   1203 #endif
   1204 	bool with_inst_flag = pset->parts->parent == NULL;
   1205 
   1206 	if (Yes == NULL)
   1207 		Yes = msg_string(MSG_Yes);
   1208 
   1209 #ifndef NO_CLONES
   1210 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
   1211 	   pset->infos[ptn].cur_part_id == NO_PART) {
   1212 		psize = pset->infos[ptn].size / sizemult;
   1213 		if (pset->infos[ptn].clone_ndx <
   1214 		    pset->infos[ptn].clone_src->num_sel)
   1215 			clone_cnt = 1;
   1216 		else
   1217 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
   1218 		if (pset->infos[ptn].cur_part_id == NO_PART)
   1219 			wprintw(m->mw, "                          %12" PRIu64
   1220 			    " [%zu %s]", psize, clone_cnt,
   1221 			    msg_string(MSG_clone_target_disp));
   1222 		else {
   1223 			poffset = pset->infos[ptn].cur_start / sizemult;
   1224 			pend = (pset->infos[ptn].cur_start +
   1225 			    pset->infos[ptn].size) / sizemult - 1;
   1226 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1227 			    " [%zu %s]",
   1228 			    poffset, pend, psize, clone_cnt,
   1229 			    msg_string(MSG_clone_target_disp));
   1230 		}
   1231 		if (m->title == fspart_title)
   1232 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1233 		else
   1234 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1235 		return;
   1236 	}
   1237 #endif
   1238 
   1239 	if (!real_partition(pset, ptn))
   1240 		return;
   1241 
   1242 	if (!pset->parts->pscheme->get_part_info(pset->parts,
   1243 	    pset->infos[ptn].cur_part_id, &info))
   1244 		return;
   1245 
   1246 	/*
   1247 	 * We use this function in multiple menus, but only want it
   1248 	 * to play with enable/disable in a single one:
   1249 	 */
   1250 	if (m->title == fspart_title) {
   1251 		/*
   1252 		 * Enable / disable this line if it is something
   1253 		 * like RAW_PART
   1254 		 */
   1255 		if ((info.flags &
   1256 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1257 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
   1258 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1259 		else
   1260 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1261 	}
   1262 
   1263 	poffset = info.start / sizemult;
   1264 	psize = info.size / sizemult;
   1265 	if (psize == 0)
   1266 		pend = 0;
   1267 	else
   1268 		pend = (info.start + info.size) / sizemult - 1;
   1269 
   1270 	if (info.flags & PTI_WHOLE_DISK)
   1271 		desc = msg_string(MSG_NetBSD_partition_cant_change);
   1272 	else if (info.flags & PTI_RAW_PART)
   1273 		desc = msg_string(MSG_Whole_disk_cant_change);
   1274 	else if (info.flags & PTI_BOOT)
   1275 		desc = msg_string(MSG_Boot_partition_cant_change);
   1276 	else
   1277 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
   1278 
   1279 	fp = flag_str;
   1280 	inst_flags = pset->infos[ptn].instflags;
   1281 	if (with_inst_flag && info.start == pm->ptstart &&
   1282 	    info.nat_type->generic_ptype == PT_root) {
   1283 		static char inst_flag;
   1284 
   1285 		if (inst_flag == 0)
   1286 			inst_flag = msg_string(MSG_install_flag)[0];
   1287 		*fp++ = inst_flag;
   1288 	}
   1289 	if (inst_flags & PUIINST_NEWFS)
   1290 		*fp++ = msg_string(MSG_newfs_flag)[0];
   1291 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1292 		*fp++ = msg_string(MSG_clone_flag)[0];
   1293 	*fp = 0;
   1294 	if (pset->parts->pscheme->get_part_attr_str != NULL)
   1295 		pset->parts->pscheme->get_part_attr_str(pset->parts,
   1296 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
   1297 
   1298 	/* if the fstype description does not fit, check if we can overrun */
   1299 	if (strlen(desc) > (size_t)fstype_width &&
   1300 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
   1301 	     info.last_mounted[0] == 0))
   1302 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1303 		    " %s",
   1304 		    poffset, pend, psize, desc);
   1305 	else
   1306 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1307 		    " %*.*s %*s %s",
   1308 		    poffset, pend, psize, fstype_width, fstype_width, desc,
   1309 		    -flags_width, flag_str,
   1310 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
   1311 		     info.last_mounted[0] ? info.last_mounted : "");
   1312 }
   1313 
   1314 #ifndef NO_CLONES
   1315 static int
   1316 part_ext_clone(menudesc *m, void *arg)
   1317 {
   1318 	struct selected_partitions selected, *clone_src;
   1319 	struct clone_target_menu_data data;
   1320 	struct partition_usage_set *pset = arg;
   1321 	struct part_usage_info *p;
   1322 	struct disk_part_info sinfo, cinfo;
   1323 	struct disk_partitions *csrc;
   1324 	struct disk_part_free_space space;
   1325 	menu_ent *men;
   1326 	daddr_t clone_size, free_size, offset, align;
   1327 	int num_men, i;
   1328 	size_t s, clone_cnt;
   1329 	part_id cid;
   1330 	struct clone_data {
   1331 		struct disk_part_info info;
   1332 		part_id new_id;
   1333 		size_t ndx;
   1334 	};
   1335 	struct clone_data *clones = NULL;
   1336 
   1337 	if (!select_partitions(&selected, pm->parts))
   1338 		return 0;
   1339 
   1340 	clone_size = selected_parts_size(&selected);
   1341 	num_men = pset->num+1;
   1342 	men = calloc(num_men, sizeof *men);
   1343 	if (men == NULL)
   1344 		return 0;
   1345 	for (i = 0; i < num_men; i++) {
   1346 		men[i].opt_action = clone_target_select;
   1347 		if (i == 0)
   1348 			free_size = pset->infos[i].cur_start;
   1349 		else if (i > 0 && (size_t)i < pset->num)
   1350 			free_size = pset->infos[i].cur_start -
   1351 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
   1352 		else
   1353 			free_size = pset->parts->free_space;
   1354 		if (free_size < clone_size)
   1355 			men[i].opt_flags = OPT_IGNORE;
   1356 	}
   1357 	men[num_men-1].opt_name = MSG_clone_target_end;
   1358 
   1359 	memset(&data, 0, sizeof data);
   1360 	data.usage = *pset;
   1361 	data.res = -1;
   1362 
   1363 	data.usage.menu = new_menu(MSG_clone_target_hdr,
   1364 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
   1365 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
   1366 	process_menu(data.usage.menu, &data);
   1367 	free_menu(data.usage.menu);
   1368 	free(men);
   1369 
   1370 	if (data.res < 0)
   1371 		goto err;
   1372 
   1373 	/* create temporary infos for all clones that work out */
   1374 	clone_cnt = 0;
   1375 	clones = calloc(selected.num_sel, sizeof(*clones));
   1376 	if (clones == NULL)
   1377 		goto err;
   1378 
   1379 	clone_src = malloc(sizeof(selected));
   1380 	if (clone_src == NULL)
   1381 		goto err;
   1382 	*clone_src = selected;
   1383 
   1384 	/* find selected offset from data.res and insert clones there */
   1385 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
   1386 	offset = -1;
   1387 	if (data.res > 0)
   1388 		offset = pset->infos[data.res-1].cur_start
   1389 		    + pset->infos[data.res-1].size;
   1390 	else
   1391 		offset = 0;
   1392 	for (s = 0; s < selected.num_sel; s++) {
   1393 		csrc = selected.selection[s].parts;
   1394 		cid = selected.selection[s].id;
   1395 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
   1396 		if (!pset->parts->pscheme->adapt_foreign_part_info(
   1397 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
   1398 			continue;
   1399 		size_t cnt = pset->parts->pscheme->get_free_spaces(
   1400 		    pset->parts, &space, 1, cinfo.size-align, align,
   1401 		    offset, -1);
   1402 		if (cnt == 0)
   1403 			continue;
   1404 		cinfo.start = space.start;
   1405 		cid = pset->parts->pscheme->add_partition(
   1406 		    pset->parts, &cinfo, NULL);
   1407 		if (cid == NO_PART)
   1408 			continue;
   1409 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
   1410 		clones[clone_cnt].info = cinfo;
   1411 		clones[clone_cnt].new_id = cid;
   1412 		clones[clone_cnt].ndx = s;
   1413 		clone_cnt++;
   1414 		offset = rounddown(cinfo.start+cinfo.size+align, align);
   1415 	}
   1416 
   1417 	/* insert new clone records at offset data.res */
   1418 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
   1419 	if (men == NULL)
   1420 		goto err;
   1421 	pset->menu_opts = men;
   1422 	m->opts = men;
   1423 	m->numopts += clone_cnt;
   1424 
   1425 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
   1426 	if (p == NULL)
   1427 		goto err;
   1428 	pset->infos = p;
   1429 
   1430 	men += data.res;
   1431 	p += data.res;
   1432 	memmove(men+clone_cnt, men,
   1433 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
   1434 	if (pset->num > (size_t)data.res)
   1435 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
   1436 	memset(men, 0, sizeof(*men)*clone_cnt);
   1437 	memset(p, 0, sizeof(*p)*clone_cnt);
   1438 	for (s = 0; s < clone_cnt; s++) {
   1439 		p[s].cur_part_id = clones[s].new_id;
   1440 		p[s].cur_start = clones[s].info.start;
   1441 		p[s].size = clones[s].info.size;
   1442 		p[s].cur_flags = clones[s].info.flags;
   1443 		p[s].flags = PUIFLG_CLONE_PARTS;
   1444 		p[s].parts = pset->parts;
   1445 		p[s].clone_src = clone_src;
   1446 		p[s].clone_ndx = s;
   1447 	}
   1448 	free(clones);
   1449 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
   1450 	pset->num += clone_cnt;
   1451 	m->h = 0;
   1452 	resize_menu_height(m);
   1453 
   1454 	return -1;
   1455 
   1456 err:
   1457 	free(clones);
   1458 	free_selected_partitions(&selected);
   1459 	return 0;
   1460 }
   1461 #endif
   1462 
   1463 static int
   1464 edit_fspart_pack(menudesc *m, void *arg)
   1465 {
   1466 	struct partition_usage_set *pset = arg;
   1467 	char buf[STRSIZE];
   1468 
   1469 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
   1470 	    buf, sizeof buf))
   1471 		return 0;
   1472 
   1473 	msg_prompt_win(MSG_edit_disk_pack_hdr,
   1474 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
   1475 
   1476 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
   1477 	return 0;
   1478 }
   1479 
   1480 static int
   1481 edit_fspart_add(menudesc *m, void *arg)
   1482 {
   1483 	struct partition_usage_set *pset = arg;
   1484 	struct part_usage_info *ninfo;
   1485 	menu_ent *nmenopts;
   1486 	size_t cnt, off;
   1487 
   1488 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1489 	if (ninfo == NULL)
   1490 		return 0;
   1491 	pset->infos = ninfo;
   1492 	off = pset->parts->num_part;
   1493 	cnt = pset->num-pset->parts->num_part;
   1494 	if (cnt > 0)
   1495 		memmove(pset->infos+off+1,pset->infos+off,
   1496 		    cnt*sizeof(*pset->infos));
   1497 	memset(pset->infos+off, 0, sizeof(*pset->infos));
   1498 
   1499 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1500 	if (nmenopts == NULL)
   1501 		return 0;
   1502 	memmove(nmenopts+off+1, nmenopts+off,
   1503 	    (m->numopts-off)*sizeof(*nmenopts));
   1504 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1505 	nmenopts[off].opt_action = edit_ptn;
   1506 	pset->menu_opts = m->opts = nmenopts;
   1507 	m->numopts++;
   1508 	m->cursel = off;
   1509 	pset->num++;
   1510 
   1511 	/* now update edit menu to fit */
   1512 	m->h = 0;
   1513 	resize_menu_height(m);
   1514 
   1515 	/* and directly invoke the partition editor for the new part */
   1516 	edit_ptn(m, arg);
   1517 
   1518 	show_partition_adder(m, pset);
   1519 
   1520 	return -1;
   1521 }
   1522 
   1523 static void
   1524 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1525 {
   1526 	struct part_usage_info *ninfo;
   1527 	menu_ent *nmenopts;
   1528 	size_t off;
   1529 
   1530 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1531 	if (ninfo == NULL)
   1532 		return;
   1533 	pset->infos = ninfo;
   1534 	off = pset->parts->num_part+1;
   1535 
   1536 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1537 	if (nmenopts == NULL)
   1538 		return;
   1539 	memmove(nmenopts+off+1, nmenopts+off,
   1540 	    (m->numopts-off)*sizeof(*nmenopts));
   1541 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1542 
   1543 	nmenopts[off].opt_name = MSG_addpart;
   1544 	nmenopts[off].opt_flags = OPT_SUB;
   1545 	nmenopts[off].opt_action = edit_fspart_add;
   1546 
   1547 	m->opts = nmenopts;
   1548 	m->numopts++;
   1549 	pset->num++;
   1550 }
   1551 
   1552 static void
   1553 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1554 {
   1555 	size_t off;
   1556 
   1557 	off = pset->parts->num_part+1;
   1558 	memmove(m->opts+off, m->opts+off+1,
   1559 	    (m->numopts-off-1)*sizeof(*m->opts));
   1560 	m->numopts--;
   1561 	pset->num--;
   1562 }
   1563 
   1564 /*
   1565  * Called whenever the "add a partition" option may need to be removed
   1566  * or added
   1567  */
   1568 static void
   1569 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1570 {
   1571 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
   1572 	    pset->parts);
   1573 	bool part_adder_present =
   1574 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
   1575 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
   1576 
   1577 	if (can_add_partition == part_adder_present)
   1578 		return;
   1579 
   1580 	if (can_add_partition)
   1581 		add_partition_adder(m, pset);
   1582 	else
   1583 		remove_partition_adder(m, pset);
   1584 
   1585 	/* now update edit menu to fit */
   1586 	m->h = 0;
   1587 	resize_menu_height(m);
   1588 }
   1589 
   1590 static int
   1591 edit_fspart_abort(menudesc *m, void *arg)
   1592 {
   1593 	struct partition_usage_set *pset = arg;
   1594 
   1595 	pset->ok = false;
   1596 	return 1;
   1597 }
   1598 
   1599 /*
   1600  * Check a disklabel.
   1601  * If there are overlapping active partitions,
   1602  * Ask the user if they want to edit the partition or give up.
   1603  */
   1604 int
   1605 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset)
   1606 {
   1607 	menu_ent *op;
   1608 	size_t cnt, i;
   1609 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
   1610 	bool may_edit_pack =
   1611 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
   1612 	    pset->parts->pscheme->set_disk_pack_name != NULL;
   1613 
   1614 #ifdef NO_CLONES
   1615 #define	C_M_ITEMS	0
   1616 #else
   1617 #define	C_M_ITEMS	1
   1618 #endif
   1619 	pset->menu_opts = calloc(pset->parts->num_part
   1620 	     +3+C_M_ITEMS+may_add+may_edit_pack,
   1621 	     sizeof *pset->menu_opts);
   1622 	if (pset->menu_opts == NULL)
   1623 		return 0;
   1624 
   1625 	op = pset->menu_opts;
   1626 	for (i = 0; i < pset->parts->num_part; i++) {
   1627 		op->opt_action = edit_ptn;
   1628 		op++;
   1629 	}
   1630 	/* separator line between partitions and actions */
   1631 	op->opt_name = fspart_separator;
   1632 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
   1633 	op++;
   1634 
   1635 	/* followed by new partition adder */
   1636 	if (may_add) {
   1637 		op->opt_name = MSG_addpart;
   1638 		op->opt_flags = OPT_SUB;
   1639 		op->opt_action = edit_fspart_add;
   1640 		op++;
   1641 	}
   1642 
   1643 	/* and unit changer */
   1644 	op->opt_name = MSG_askunits;
   1645 	op->opt_menu = MENU_sizechoice;
   1646 	op->opt_flags = OPT_SUB;
   1647 	op->opt_action = NULL;
   1648 	op++;
   1649 
   1650 	if (may_edit_pack) {
   1651 		op->opt_name = MSG_editpack;
   1652 		op->opt_flags = OPT_SUB;
   1653 		op->opt_action = edit_fspart_pack;
   1654 		op++;
   1655 	}
   1656 
   1657 #ifndef NO_CLONES
   1658 	/* add a clone-from-elsewhere option */
   1659 	op->opt_name = MSG_clone_from_elsewhere;
   1660 	op->opt_action = part_ext_clone;
   1661 	op++;
   1662 #endif
   1663 
   1664 	/* and abort option */
   1665 	op->opt_name = MSG_cancel;
   1666 	op->opt_flags = OPT_EXIT;
   1667 	op->opt_action = edit_fspart_abort;
   1668 	op++;
   1669 	cnt = op - pset->menu_opts;
   1670 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
   1671 
   1672 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
   1673 			0, -1, 0, 74,
   1674 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
   1675 			MC_NOCLEAR|MC_CONTINUOUS,
   1676 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
   1677 			MSG_partition_sizes_ok);
   1678 
   1679 	if (pset->menu < 0) {
   1680 		free(pset->menu_opts);
   1681 		pset->menu_opts = NULL;
   1682 		return 0;
   1683 	}
   1684 
   1685 	p->current_cylsize = p->dlcylsize;
   1686 
   1687 	for (;;) {
   1688 		/* first give the user the option to edit the label... */
   1689 		pset->ok = true;
   1690 		process_menu(pset->menu, pset);
   1691 		if (!pset->ok) {
   1692 			i = 0;
   1693 			break;
   1694 		}
   1695 
   1696 		/* User thinks the label is OK. */
   1697 		i = verify_parts(pset);
   1698 		if (i == 1)
   1699 			continue;
   1700 		break;
   1701 	}
   1702 	free(pset->menu_opts);
   1703 	pset->menu_opts = NULL;
   1704 	free_menu(pset->menu);
   1705 	pset->menu = -1;
   1706 
   1707 	return i != 0;
   1708 }
   1709 
   1710 /*
   1711  * strip trailing / to avoid confusion in path comparisions later
   1712  */
   1713 void
   1714 canonicalize_last_mounted(char *path)
   1715 {
   1716 	char *p;
   1717 
   1718 	if (path == NULL)
   1719 		return;
   1720 
   1721 	if (strcmp(path, "/") == 0)
   1722 		return;	/* in this case a "trailing" slash is allowed */
   1723 
   1724 	for (;;) {
   1725 		p = strrchr(path, '/');
   1726 		if (p == NULL)
   1727 			return;
   1728 		if (p[1] != 0)
   1729 			return;
   1730 		p[0] = 0;
   1731 	}
   1732 }
   1733 
   1734 /*
   1735  * Try to get 'last mounted on' (or equiv) from fs superblock.
   1736  */
   1737 const char *
   1738 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
   1739     uint flags)
   1740 {
   1741 	static char sblk[SBLOCKSIZE];		/* is this enough? */
   1742 	struct fs *SB = (struct fs *)sblk;
   1743 	static const off_t sblocks[] = SBLOCKSEARCH;
   1744 	const off_t *sbp;
   1745 	const char *mnt = NULL;
   1746 	int len;
   1747 
   1748 	if (fd == -1)
   1749 		return "";
   1750 
   1751 	if (fs_type)
   1752 		*fs_type = 0;
   1753 	if (fs_sub_type)
   1754 		*fs_sub_type = 0;
   1755 
   1756 	/* Check UFS1/2 (and hence LFS) superblock */
   1757 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
   1758 		if (pread(fd, sblk, sizeof sblk,
   1759 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
   1760 			continue;
   1761 
   1762 		/*
   1763 		 * If start of partition and allowed by flags check
   1764 		 * for other fs types
   1765 		 */
   1766 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
   1767 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
   1768 			/* Probably a FAT filesystem, report volume name */
   1769 			size_t i;
   1770 			for (i = 0x51; i >= 0x47; i--) {
   1771 				if (sblk[i] != ' ')
   1772 					break;
   1773 				sblk[i] = 0;
   1774 			}
   1775 			sblk[0x52] = 0;
   1776 			if (fs_type)
   1777 				*fs_type = FS_MSDOS;
   1778 			if (fs_sub_type)
   1779 				*fs_sub_type = sblk[0x53];
   1780 			return sblk + 0x47;
   1781 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
   1782 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
   1783 			if (fs_type)
   1784 				*fs_type = FS_NTFS;
   1785 			if (fs_sub_type)
   1786 				*fs_sub_type = MBR_PTYPE_NTFS;
   1787 			/* XXX dig for volume name attribute ? */
   1788 			return "";
   1789 		}
   1790 
   1791 		if (!(flags & GLM_LIKELY_FFS))
   1792 			continue;
   1793 
   1794 		/* Maybe we should validate the checksum??? */
   1795 		switch (SB->fs_magic) {
   1796 		case FS_UFS1_MAGIC:
   1797 		case FS_UFS1_MAGIC_SWAPPED:
   1798 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
   1799 				if (*sbp == SBLOCK_UFS1)
   1800 					mnt = (const char *)SB->fs_fsmnt;
   1801 			} else {
   1802 				/* Check we have the main superblock */
   1803 				if (SB->fs_sblockloc == *sbp)
   1804 					mnt = (const char *)SB->fs_fsmnt;
   1805 			}
   1806 			if (fs_type)
   1807 				*fs_type = FS_BSDFFS;
   1808 			if (fs_sub_type)
   1809 				*fs_sub_type = 1;
   1810 			continue;
   1811 		case FS_UFS2_MAGIC:
   1812 		case FS_UFS2_MAGIC_SWAPPED:
   1813 			/* Check we have the main superblock */
   1814 			if (SB->fs_sblockloc == *sbp) {
   1815 				mnt = (const char *)SB->fs_fsmnt;
   1816 				if (fs_type)
   1817 					*fs_type = FS_BSDFFS;
   1818 				if (fs_sub_type)
   1819 					*fs_sub_type = 2;
   1820 			}
   1821 			continue;
   1822 		}
   1823 	}
   1824 
   1825 	if (mnt == NULL)
   1826 		return "";
   1827 
   1828 	/* If sysinst mounted this last then strip prefix */
   1829 	len = strlen(targetroot_mnt);
   1830 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
   1831 		if (mnt[len] == 0)
   1832 			return "/";
   1833 		if (mnt[len] == '/')
   1834 			return mnt + len;
   1835 	}
   1836 	return mnt;
   1837 	#undef SB
   1838 }
   1839 
   1840 /* Ask for a partition offset, check bounds and do the needed roundups */
   1841 daddr_t
   1842 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
   1843 {
   1844 	char defstart[24], isize[24], maxpart, minspace, maxspace,
   1845 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
   1846 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
   1847 	size_t num_freespace, spaces, ndx;
   1848 	struct disk_part_free_space *freespace;
   1849 	daddr_t i, localsizemult, ptn_alignment, min, max;
   1850 	part_id partn;
   1851 	struct disk_part_info info;
   1852 	const char *errmsg = NULL;
   1853 
   1854 	min = parts->disk_start;
   1855 	max = min + parts->disk_size;
   1856 
   1857 	/* upper bound on the number of free spaces, plus some slope */
   1858 	num_freespace = parts->num_part * 2 + 5;
   1859 	freespace = calloc(num_freespace, sizeof(*freespace));
   1860 	if (freespace == NULL)
   1861 		return -1;
   1862 
   1863 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
   1864 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
   1865 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
   1866 	    defpartstart);
   1867 
   1868 	maxpart = 'a' + parts->num_part -1;
   1869 	if (parts->num_part > 1) {
   1870 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
   1871 	} else if (parts->num_part == 1) {
   1872 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
   1873 	} else {
   1874 		strcpy(valid_parts, "---");
   1875 	}
   1876 	if (spaces > 1) {
   1877 		minspace = maxpart + 1;
   1878 		maxspace = minspace + spaces -1;
   1879 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
   1880 		    maxspace);
   1881 	} else if (spaces == 1) {
   1882 		maxspace = minspace = maxpart + 1;
   1883 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
   1884 	} else {
   1885 		minspace = 0;
   1886 		maxspace = 0;
   1887 		strcpy(valid_spaces, "---");
   1888 	}
   1889 
   1890 	/* Add description of start/size to user prompt */
   1891 	const char *mstr = msg_string(MSG_free_space_line);
   1892         space_prompt[0] = 0;
   1893         for (ndx = 0; ndx < spaces; ndx++) {
   1894                 char str_start[40], str_end[40], str_size[40], str_tag[4];
   1895 
   1896 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
   1897                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
   1898                 sprintf(str_end, "%" PRIu64,
   1899                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
   1900                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
   1901                 const char *args[4] = { str_start, str_end, str_size,
   1902                     multname };
   1903                 char *line = str_arg_subst(mstr, 4, args);
   1904 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
   1905                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
   1906                 free (line);
   1907                 if (len >= sizeof space_prompt)
   1908                         break;
   1909         }
   1910 
   1911 	const char *args[] = { valid_parts, valid_spaces, multname };
   1912 	hint_part = NULL;
   1913 	hint_space = NULL;
   1914 	head = str_arg_subst(msg_string(MSG_label_offset_head),
   1915 	    __arraycount(args), args);
   1916 	if (parts->num_part)
   1917 		hint_part = str_arg_subst(msg_string(
   1918 		    MSG_label_offset_part_hint), __arraycount(args), args);
   1919 	if (spaces)
   1920 		hint_space = str_arg_subst(msg_string(
   1921 		    MSG_label_offset_space_hint), __arraycount(args), args);
   1922 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
   1923 	    __arraycount(args), args);
   1924 
   1925 	if (hint_part && hint_space)
   1926 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
   1927 		    head, hint_part, hint_space, space_prompt, tail);
   1928 	else if (hint_part)
   1929 		asprintf(&label_msg, "%s\n%s\n\n%s",
   1930 		    head, hint_part, tail);
   1931 	else if (hint_space)
   1932 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
   1933 		    head, hint_space, space_prompt, tail);
   1934 	else
   1935 		asprintf(&label_msg, "%s\n\n%s",
   1936 		    head, tail);
   1937 	free(head); free(hint_part); free(hint_space); free(tail);
   1938 
   1939 	localsizemult = sizemult;
   1940 	errmsg = NULL;
   1941 	for (;;) {
   1942 		snprintf(defstart, sizeof defstart, "%" PRIu64,
   1943 		    defpartstart/sizemult);
   1944 		if (errmsg != NULL && errmsg[0] != 0)
   1945 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   1946 		else
   1947 			prompt = label_msg;
   1948 		msg_prompt_win(prompt, -1, 13, 70, -1,
   1949 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
   1950 		if (label_msg != prompt)
   1951 			free(prompt);
   1952 		if (strcmp(defstart, isize) == 0) {
   1953 			/* Don't do rounding if default accepted */
   1954 			i = defpartstart;
   1955 			break;
   1956 		}
   1957 		if (isize[1] == '\0' && isize[0] >= 'a' &&
   1958 		    isize[0] <= maxpart) {
   1959 			partn = isize[0] - 'a';
   1960 			if (parts->pscheme->get_part_info(parts, partn,
   1961 			    &info)) {
   1962 				i = info.start + info.size;
   1963 				localsizemult = 1;
   1964 			} else {
   1965 				errmsg = msg_string(MSG_invalid_sector_number);
   1966 				continue;
   1967 			}
   1968 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
   1969 		    isize[0] <= maxspace) {
   1970 			ndx = isize[0] - minspace;
   1971 			i = freespace[ndx].start;
   1972 			localsizemult = 1;
   1973 		} else if (atoi(isize) == -1) {
   1974 			i = min;
   1975 			localsizemult = 1;
   1976 		} else {
   1977 			i = parse_disk_pos(isize, &localsizemult, pm->dlcylsize, NULL);
   1978 			if (i < 0) {
   1979 				errmsg = msg_string(MSG_invalid_sector_number);
   1980 				continue;
   1981 			}
   1982 		}
   1983 		/* round to cylinder size if localsizemult != 1 */
   1984 		i = NUMSEC(i, localsizemult, pm->dlcylsize);
   1985 		/* Adjust to start of slice if needed */
   1986 		if ((i < min && (min - i) < localsizemult) ||
   1987 		    (i > min && (i - min) < localsizemult)) {
   1988 			i = min;
   1989 		}
   1990 		if (max == 0 || i <= max)
   1991 			break;
   1992 		errmsg = msg_string(MSG_startoutsidedisk);
   1993 	}
   1994 	free(label_msg);
   1995 	free(freespace);
   1996 
   1997 	return i;
   1998 }
   1999 
   2000 
   2001 /* Ask for a partition size, check bounds and do the needed roundups */
   2002 daddr_t
   2003 getpartsize(struct disk_partitions *parts, daddr_t partstart, daddr_t dflt)
   2004 {
   2005 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
   2006 	    *label_msg, *prompt, *head, *hint, *tail;
   2007 	const char *errmsg = NULL;
   2008 	daddr_t i, partend, localsizemult, max, max_r, dflt_r;
   2009 	struct disk_part_info info;
   2010 	part_id partn;
   2011 
   2012 	max = parts->pscheme->max_free_space_at(parts, partstart);
   2013 
   2014 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
   2015 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
   2016 	if (max == dflt)
   2017 		max_r = dflt_r;
   2018 	else
   2019 		max_r = max / sizemult;
   2020 	/* the partition may have been moved and now not fit any longer */
   2021 	if (dflt > max)
   2022 		dflt = max;
   2023 	if (dflt_r > max_r)
   2024 		dflt_r = max_r;
   2025 
   2026 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
   2027 
   2028 	maxpartc = 'a' + parts->num_part -1;
   2029 	if (parts->num_part > 1) {
   2030 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
   2031 	} else if (parts->num_part == 1) {
   2032 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
   2033 	} else {
   2034 		strcpy(valid_parts, "---");
   2035 	}
   2036 
   2037 	const char *args[] = { valid_parts, max_size, multname };
   2038 	hint = NULL;
   2039 	head = str_arg_subst(msg_string(MSG_label_size_head),
   2040 	    __arraycount(args), args);
   2041 	if (parts->num_part)
   2042 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
   2043 		    __arraycount(args), args);
   2044 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
   2045 	    __arraycount(args), args);
   2046 
   2047 	if (hint != NULL)
   2048 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
   2049 	else
   2050 		asprintf(&label_msg, "%s\n\n%s", head, tail);
   2051 	free(head); free(hint); free(tail);
   2052 
   2053 	localsizemult = sizemult;
   2054 	i = -1;
   2055 	for (;;) {
   2056 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
   2057 
   2058 		if (errmsg != NULL && errmsg[0] != 0)
   2059 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2060 		else
   2061 			prompt = label_msg;
   2062 		msg_prompt_win(prompt, -1, 12, 70, -1,
   2063 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
   2064 		if (prompt != label_msg)
   2065 			free(prompt);
   2066 
   2067 		if (strcmp(isize, dsize) == 0) {
   2068 			free(label_msg);
   2069 			return dflt;
   2070 		}
   2071 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
   2072 		    isize[0] <= maxpartc) {
   2073 			partn = isize[0] - 'a';
   2074 			if (parts->pscheme->get_part_info(parts, partn,
   2075 			    &info)) {
   2076 				i = info.start - partstart -1;
   2077 				localsizemult = 1;
   2078 				max_r = max;
   2079 			}
   2080 		} else if (atoi(isize) == -1) {
   2081 			i = max;
   2082 			localsizemult = 1;
   2083 			max_r = max;
   2084 		} else {
   2085 			i = parse_disk_pos(isize, &localsizemult,
   2086 			    pm->dlcylsize, NULL);
   2087 			if (localsizemult != sizemult)
   2088 				max_r = max;
   2089 		}
   2090 		if (i < 0) {
   2091 			errmsg = msg_string(MSG_Invalid_numeric);
   2092 			continue;
   2093 		} else if (i > max_r) {
   2094 			errmsg = msg_string(MSG_Too_large);
   2095 			continue;
   2096 		}
   2097 		/*
   2098 		 * partend is aligned to a cylinder if localsizemult
   2099 		 * is not 1 sector
   2100 		 */
   2101 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
   2102 		    localsizemult, pm->dlcylsize);
   2103 		/* Align to end-of-disk or end-of-slice if close enough */
   2104 		if (partend > (pm->dlsize - sizemult)
   2105 		    && partend < (pm->dlsize + sizemult))
   2106 			partend = pm->dlsize;
   2107 		if (partend > (partstart + max - sizemult)
   2108 		    && partend < (partstart + max + sizemult))
   2109 			partend = partstart + max;
   2110 		/* sanity checks */
   2111 		if (partend > (partstart + pm->dlsize)) {
   2112 			partend = pm->dlsize;
   2113 			errmsg = msg_string(MSG_endoutsidedisk);
   2114 			continue;
   2115 		}
   2116 		free(label_msg);
   2117 		if (partend < partstart)
   2118 			return 0;
   2119 		return (partend - partstart);
   2120 	}
   2121 	/* NOTREACHED */
   2122 }
   2123 
   2124 /*
   2125  * convert a string to a number of sectors, with a possible unit
   2126  * 150M = 150 Megabytes
   2127  * 2000c = 2000 cylinders
   2128  * 150256s = 150256 sectors
   2129  * Without units, use the default (sizemult).
   2130  * returns the raw input value, and the unit used. Caller needs to multiply!
   2131  * On invalid inputs, returns -1.
   2132  */
   2133 daddr_t
   2134 parse_disk_pos(
   2135 	const char *str,
   2136 	daddr_t *localsizemult,
   2137 	daddr_t cyl_size,
   2138 	bool *extend_this)
   2139 {
   2140 	daddr_t val;
   2141 	char *cp;
   2142 	bool mult_found;
   2143 
   2144 	if (str[0] == '\0') {
   2145 		return -1;
   2146 	}
   2147 	val = strtoull(str, &cp, 10);
   2148 	mult_found = false;
   2149 	if (extend_this)
   2150 		*extend_this = false;
   2151 	while (*cp != 0) {
   2152 		if (*cp == 'G' || *cp == 'g') {
   2153 			if (mult_found)
   2154 				return -1;
   2155 			*localsizemult = GIG / pm->sectorsize;
   2156 			goto next;
   2157 		}
   2158 		if (*cp == 'M' || *cp == 'm') {
   2159 			if (mult_found)
   2160 				return -1;
   2161 			*localsizemult = MEG / pm->sectorsize;
   2162 			goto next;
   2163 		}
   2164 		if (*cp == 'c' || *cp == 'C') {
   2165 			if (mult_found)
   2166 				return -1;
   2167 			*localsizemult = pm->dlcylsize;
   2168 			goto next;
   2169 		}
   2170 		if (*cp == 's' || *cp == 'S') {
   2171 			if (mult_found)
   2172 				return -1;
   2173 			*localsizemult = 1;
   2174 			goto next;
   2175 		}
   2176 		if (*cp == '+' && extend_this) {
   2177 			*extend_this = true;
   2178 			cp++;
   2179 			break;
   2180 		}
   2181 
   2182 		/* not a known unit */
   2183 		return -1;
   2184 
   2185 next:
   2186 		mult_found = true;
   2187 		cp++;
   2188 		continue;
   2189 	}
   2190 	if (*cp != 0)
   2191 		return -1;
   2192 
   2193 	return val;
   2194 }
   2195