Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.7
      1 /*	$NetBSD: label.c,v 1.7 2019/06/12 06:20:17 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.7 2019/06/12 06:20:17 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 	}
    465 	/* skip the two FFS entries, and do not add FFS later again */
    466 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    467 		if (i == FS_UNUSED)
    468 			continue;
    469 		if (i == FS_BSDFFS)
    470 			continue;
    471 		if (fstypenames[i] == NULL)
    472 			continue;
    473 
    474 		if (i == t) {
    475 			menu->cursel = ndx;
    476 			break;
    477 		}
    478 		ndx++;
    479 	}
    480 }
    481 
    482 static int
    483 set_fstype_ext(menudesc *menu, void *arg)
    484 {
    485 	struct single_part_fs_edit *edit = arg;
    486 	size_t i, ndx, max = menu->numopts;
    487 
    488 	if (menu->cursel == 0 || menu->cursel == 1) {
    489 		edit->info.fs_type = FS_BSDFFS;
    490 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    491 		edit->info.nat_type = edit->pset->parts->pscheme->
    492 		    get_fs_part_type(edit->info.fs_type,
    493 		    edit->info.fs_sub_type);
    494 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    495 		edit->wanted->fs_type = edit->info.fs_type;
    496 		edit->wanted->fs_version = edit->info.fs_sub_type;
    497 		return 1;
    498 	}
    499 
    500 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    501 		if (i == FS_UNUSED)
    502 			continue;
    503 		if (i == FS_BSDFFS)
    504 			continue;
    505 		if (fstypenames[i] == NULL)
    506 			continue;
    507 
    508 		if (ndx == (size_t)menu->cursel) {
    509 			edit->info.fs_type = i;
    510 			edit->info.fs_sub_type = 0;
    511 			edit->info.nat_type = edit->pset->parts->pscheme->
    512 			    get_fs_part_type(i, 0);
    513 			if (edit->info.nat_type == NULL)
    514 				edit->info.nat_type = edit->pset->parts->
    515 				    pscheme->get_generic_part_type(PT_root);
    516 			edit->wanted->type = edit->info.nat_type->generic_ptype;
    517 			edit->wanted->fs_type = edit->info.fs_type;
    518 			edit->wanted->fs_version = edit->info.fs_sub_type;
    519 			break;
    520 		}
    521 		ndx++;
    522 	}
    523 	return 1;
    524 }
    525 
    526 /*
    527  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
    528  * skip later FFS entry in the generic list.
    529  */
    530 static int
    531 edit_fs_type_ext(menudesc *menu, void *arg)
    532 {
    533 	menu_ent *opts;
    534 	int m;
    535 	size_t i, ndx, cnt;
    536 
    537 	cnt = __arraycount(fstypenames)-1;
    538 	opts = calloc(cnt, sizeof(*opts));
    539 	if (opts == NULL)
    540 		return 1;
    541 
    542 	ndx = 0;
    543 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
    544 	opts[ndx].opt_menu = OPT_NOMENU;
    545 	opts[ndx].opt_action = set_fstype_ext;
    546 	ndx++;
    547 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
    548 	opts[ndx].opt_menu = OPT_NOMENU;
    549 	opts[ndx].opt_action = set_fstype_ext;
    550 	ndx++;
    551 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
    552 		if (i == FS_UNUSED)
    553 			continue;
    554 		if (i == FS_BSDFFS)
    555 			continue;
    556 		if (fstypenames[i] == NULL)
    557 			continue;
    558 		opts[ndx].opt_name = fstypenames[i];
    559 		opts[ndx].opt_menu = OPT_NOMENU;
    560 		opts[ndx].opt_action = set_fstype_ext;
    561 		ndx++;
    562 	}
    563 	assert(ndx == cnt);
    564 	m = new_menu(MSG_Select_the_type, opts, ndx,
    565 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    566 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
    567 
    568 	if (m < 0)
    569 		return 1;
    570 	process_menu(m, arg);
    571 	free_menu(m);
    572 	free(opts);
    573 
    574 	return 1;
    575 }
    576 
    577 static void
    578 init_fs_type(menudesc *menu, void *arg)
    579 {
    580 	struct single_part_fs_edit *edit = arg;
    581 	size_t i;
    582 
    583 	/* init menu->cursel from fs type in arg */
    584 	if (edit->info.fs_type == FS_BSDFFS) {
    585 		if (edit->info.fs_sub_type == 2)
    586 			menu->cursel = 0;
    587 		else
    588 			menu->cursel = 1;
    589 	}
    590 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
    591 		if (edit->info.fs_type == edit_fs_common_types[i]) {
    592 			menu->cursel = i+1;
    593 			break;
    594 		}
    595 	}
    596 }
    597 
    598 static int
    599 set_fstype(menudesc *menu, void *arg)
    600 {
    601 	struct single_part_fs_edit *edit = arg;
    602 	int ndx;
    603 
    604 	if (menu->cursel < 2) {
    605 		edit->info.fs_type = FS_BSDFFS;
    606 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    607 		edit->info.nat_type = edit->pset->parts->pscheme->
    608 		    get_fs_part_type(FS_BSDFFS, 2);
    609 		if (edit->info.nat_type == NULL)
    610 			edit->info.nat_type = edit->pset->parts->
    611 			    pscheme->get_generic_part_type(PT_root);
    612 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    613 		edit->wanted->fs_type = edit->info.fs_type;
    614 		edit->wanted->fs_version = edit->info.fs_sub_type;
    615 		return 1;
    616 	}
    617 	ndx = menu->cursel-1;
    618 
    619 	if (ndx < 0 ||
    620 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
    621 		return 1;
    622 
    623 	edit->info.fs_type = edit_fs_common_types[ndx];
    624 	edit->info.fs_sub_type = 0;
    625 	edit->info.nat_type = edit->pset->parts->pscheme->
    626 	    get_fs_part_type(edit->info.fs_type, 0);
    627 	if (edit->info.nat_type == NULL)
    628 		edit->info.nat_type = edit->pset->parts->
    629 		    pscheme->get_generic_part_type(PT_root);
    630 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    631 	edit->wanted->fs_type = edit->info.fs_type;
    632 	edit->wanted->fs_version = edit->info.fs_sub_type;
    633 	return 1;
    634 }
    635 
    636 /*
    637  * Offer a menu selecting the common file system types
    638  */
    639 static int
    640 edit_fs_type(menudesc *menu, void *arg)
    641 {
    642 	struct single_part_fs_edit *edit = arg;
    643 	menu_ent *opts;
    644 	int m, cnt;
    645 	size_t i;
    646 
    647 	/*
    648 	 * Shortcut to full menu if we have an exotic value
    649 	 */
    650 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
    651 		if (edit->info.fs_type == edit_fs_common_types[i])
    652 			break;
    653 	if (i >= __arraycount(edit_fs_common_types)) {
    654 		edit_fs_type_ext(menu, arg);
    655 		return 0;
    656 	}
    657 
    658 	/*
    659 	 * Starting with a common type, show short menu first
    660 	 */
    661 	cnt = __arraycount(edit_fs_common_types) + 2;
    662 	opts = calloc(cnt, sizeof(*opts));
    663 	if (opts == NULL)
    664 		return 0;
    665 
    666 	/* special case entry 0: two FFS entries */
    667 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
    668 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
    669 		opts[i+1].opt_menu = OPT_NOMENU;
    670 		opts[i+1].opt_action = set_fstype;
    671 	}
    672 	/* duplicate FFS (at offset 1) into first entry */
    673 	opts[0] = opts[1];
    674 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
    675 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
    676 	/* add secondary sub-menu */
    677 	assert(i+1 < (size_t)cnt);
    678 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
    679 	opts[i+1].opt_menu = OPT_NOMENU;
    680 	opts[i+1].opt_action = edit_fs_type_ext;
    681 
    682 	m = new_menu(MSG_Select_the_type, opts, cnt,
    683 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
    684 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
    685 
    686 	if (m < 0)
    687 		return 0;
    688 	process_menu(m, arg);
    689 	free_menu(m);
    690 	free(opts);
    691 
    692 	return 0;
    693 }
    694 
    695 
    696 static void update_edit_ptn_menu(menudesc *m, void *arg);
    697 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
    698 static int edit_ptn_custom_type(menudesc *m, void *arg);
    699 
    700 int
    701 edit_ptn(menudesc *menu, void *arg)
    702 {
    703 	struct partition_usage_set *pset = arg;
    704 	struct single_part_fs_edit edit;
    705 	int fspart_menu, num_opts;
    706 	const char *err;
    707 	menu_ent *mopts, *popt;
    708 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
    709 
    710 	static const menu_ent edit_ptn_fields_head[] = {
    711 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_type },
    712 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_start },
    713 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_size },
    714 		{ .opt_menu=OPT_NOMENU, .opt_flags=OPT_IGNORE },
    715 	};
    716 
    717 	static const menu_ent edit_ptn_fields_head_add[] = {
    718 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_install },
    719 	};
    720 
    721 	static const menu_ent edit_ptn_fields_head2[] = {
    722 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_preserve },
    723 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_mount },
    724 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    725 		{ .opt_menu=OPT_NOMENU, .opt_action=edit_fs_mountpt },
    726 	};
    727 	static const menu_ent edit_ptn_fields_tail[] = {
    728 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    729 		  .opt_flags=OPT_SUB },
    730 		{ .opt_name=MSG_restore, .opt_menu=OPT_NOMENU,
    731 		  .opt_action=edit_restore},
    732 		{ .opt_name=MSG_Delete_partition, .opt_menu=OPT_NOMENU,
    733 		  .opt_action=edit_delete_ptn},
    734 		{ .opt_name=MSG_cancel, .opt_menu=OPT_NOMENU,
    735 		  .opt_action=edit_cancel},
    736 	};
    737 
    738 	memset(&edit, 0, sizeof edit);
    739 	edit.pset = pset;
    740 	edit.index = menu->cursel;
    741 	edit.wanted = &pset->infos[edit.index];
    742 
    743 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
    744 		return 0;
    745 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
    746 
    747 	num_opts = __arraycount(edit_ptn_fields_head) +
    748 	    __arraycount(edit_ptn_fields_head2) +
    749 	    __arraycount(edit_ptn_fields_tail);
    750 	if (with_inst_opt)
    751 		num_opts += __arraycount(edit_ptn_fields_head_add);
    752 	if (is_new_part)
    753 		num_opts--;
    754 	else
    755 		num_opts += pset->parts->pscheme->custom_attribute_count;
    756 
    757 	mopts = calloc(num_opts, sizeof(*mopts));
    758 	if (mopts == NULL) {
    759 		err_msg_win(err_outofmem);
    760 		return 0;
    761 	}
    762 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
    763 	popt = mopts + __arraycount(edit_ptn_fields_head);
    764 	if (with_inst_opt) {
    765 		memcpy(popt, edit_ptn_fields_head_add,
    766 		    sizeof(edit_ptn_fields_head_add));
    767 		popt +=  __arraycount(edit_ptn_fields_head_add);
    768 	}
    769 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
    770 	popt +=  __arraycount(edit_ptn_fields_head2);
    771 	edit.first_custom_attr = popt - mopts;
    772 	if (!is_new_part) {
    773 		for (size_t i = 0;
    774 		    i < pset->parts->pscheme->custom_attribute_count;
    775 		    i++, popt++) {
    776 			popt->opt_menu = OPT_NOMENU;
    777 			popt->opt_action = edit_ptn_custom_type;
    778 		}
    779 	}
    780 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
    781 	popt += __arraycount(edit_ptn_fields_tail) - 1;
    782 	if (is_new_part)
    783 		memcpy(popt-1, popt, sizeof(*popt));
    784 
    785 	if (is_new_part) {
    786 		struct disk_part_free_space space;
    787 		daddr_t align = pset->parts->pscheme->get_part_alignment(
    788 		    pset->parts);
    789 
    790 		edit.id = NO_PART;
    791 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
    792 		    &space, 1, align, align, -1, -1) == 1) {
    793 			edit.info.start = space.start;
    794 			edit.info.size = space.size;
    795 			edit.info.fs_type = FS_BSDFFS;
    796 			edit.info.fs_sub_type = 2;
    797 			edit.info.nat_type = pset->parts->pscheme->
    798 			    get_fs_part_type(edit.info.fs_type,
    799 			    edit.info.fs_sub_type);
    800 			edit.wanted->instflags = PUIINST_NEWFS;
    801 		}
    802 	} else {
    803 		edit.id = pset->infos[edit.index].cur_part_id;
    804 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
    805 		    &edit.info)) {
    806 			free(mopts);
    807 			return 0;
    808 		}
    809 	}
    810 
    811 	edit.old_usage = *edit.wanted;
    812 	edit.old_info = edit.info;
    813 
    814 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
    815 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
    816 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
    817 		NULL, MSG_OK);
    818 
    819 	process_menu(fspart_menu, &edit);
    820 	free(mopts);
    821 	free_menu(fspart_menu);
    822 
    823 	if (edit.rv == 0) {	/* OK, set new data */
    824 		edit.info.last_mounted = edit.wanted->mount;
    825 		if (is_new_part) {
    826 			edit.wanted->cur_part_id = pset->parts->pscheme->
    827 			    add_partition(pset->parts, &edit.info, &err);
    828 			if (edit.wanted->cur_part_id == NO_PART)
    829 				err_msg_win(err);
    830 			else {
    831 				pset->parts->pscheme->get_part_info(
    832 				    pset->parts, edit.wanted->cur_part_id,
    833 				    &edit.info);
    834 				edit.wanted->cur_start = edit.info.start;
    835 				edit.wanted->size = edit.info.size;
    836 				edit.wanted->type =
    837 				    edit.info.nat_type->generic_ptype;
    838 				edit.wanted->fs_type = edit.info.fs_type;
    839 				edit.wanted->fs_version = edit.info.fs_sub_type;
    840 				/* things have changed, re-sort */
    841 				renumber_partitions(pset);
    842 			}
    843 		} else {
    844 			if (!pset->parts->pscheme->set_part_info(pset->parts,
    845 			    edit.id, &edit.info, &err))
    846 				err_msg_win(err);
    847 		}
    848 
    849 		/*
    850 		 * if size has changed, we may need to add or remove
    851 		 * the option to add partitions
    852 		 */
    853 		show_partition_adder(menu, pset);
    854 	} else if (edit.rv == -1) {	/* cancel edit */
    855 		if (is_new_part) {
    856 			memmove(pset->infos+edit.index,
    857 			    pset->infos+edit.index+1,
    858 			    sizeof(*pset->infos)*(pset->num-edit.index));
    859 			memmove(menu->opts+edit.index,
    860 			    menu->opts+edit.index+1,
    861 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
    862 			menu->numopts--;
    863 			menu->cursel = 0;
    864 			pset->num--;
    865 			return -1;
    866 		}
    867 		pset->infos[edit.index] = edit.old_usage;
    868 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
    869 		if (!pset->parts->pscheme->delete_partition(pset->parts,
    870 		    edit.id, &err)) {
    871 			err_msg_win(err);
    872 			return 0;
    873 		}
    874 		memmove(pset->infos+edit.index,
    875 		    pset->infos+edit.index+1,
    876 		    sizeof(*pset->infos)*(pset->num-edit.index));
    877 		memmove(menu->opts+edit.index,
    878 		    menu->opts+edit.index+1,
    879 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
    880 		menu->numopts--;
    881 		menu->cursel = 0;
    882 
    883 		/* things have changed, re-sort */
    884 		pset->num--;
    885 		renumber_partitions(pset);
    886 
    887 		/* we can likely add new partitions now */
    888 		show_partition_adder(menu, pset);
    889 
    890 		return -1;
    891 	}
    892 
    893 	return 0;
    894 }
    895 
    896 static void
    897 update_edit_ptn_menu(menudesc *m, void *arg)
    898 {
    899 	struct single_part_fs_edit *edit = arg;
    900 	int i;
    901 	uint t = edit->info.fs_type;
    902 	size_t attr_no;
    903 
    904 	/* Determine which of the properties can be changed */
    905 	for (i = 0; i < m->numopts; i++) {
    906 		if (m->opts[i].opt_action == NULL &&
    907 		    m->opts[i].opt_menu != MENU_mountoptions)
    908 			continue;
    909 
    910 		/* Default to disabled... */
    911 		m->opts[i].opt_flags |= OPT_IGNORE;
    912 		if ((t == FS_UNUSED || t == FS_SWAP) &&
    913 		    (m->opts[i].opt_action == edit_fs_preserve ||
    914 		     m->opts[i].opt_action == edit_fs_mount ||
    915 		     m->opts[i].opt_action == edit_fs_mountpt ||
    916 		     m->opts[i].opt_menu == MENU_mountoptions))
    917 			continue;
    918 		if (m->opts[i].opt_action == edit_install &&
    919 		    edit->info.nat_type &&
    920 		    edit->info.nat_type->generic_ptype != PT_root)
    921 			/* can only install onto PT_root partitions */
    922 			continue;
    923 		if (m->opts[i].opt_action == edit_fs_preserve &&
    924 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS) {
    925 			/* Can only newfs UFS and LFS filesystems */
    926 			edit->wanted->instflags &= ~PUIINST_NEWFS;
    927 			continue;
    928 		}
    929 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
    930 			attr_no = (size_t)i - edit->first_custom_attr;
    931 			if (!edit->pset->parts->pscheme->
    932 			    custom_attribute_writable(
    933 			    edit->pset->parts, edit->id, attr_no))
    934 				continue;
    935 		}
    936 		/* Ok: we want this one */
    937 		m->opts[i].opt_flags &= ~OPT_IGNORE;
    938 	}
    939 }
    940 
    941 static void
    942 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
    943 {
    944 	struct single_part_fs_edit *edit = arg;
    945 	static int col_width;
    946 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
    947 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
    948 	     *ptn_install;
    949 	const char *c;
    950 	char val[MENUSTRSIZE];
    951 	const char *attrname;
    952 	size_t attr_no;
    953 
    954 	if (col_width == 0) {
    955 		int l;
    956 
    957 #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
    958 			if (l > col_width) col_width = l
    959 
    960 		LOAD(ptn_type);
    961 		LOAD(ptn_start);
    962 		LOAD(ptn_size);
    963 		LOAD(ptn_end);
    964 		LOAD(ptn_install);
    965 		LOAD(ptn_newfs);
    966 		LOAD(ptn_mount);
    967 		LOAD(ptn_mount_options);
    968 		LOAD(ptn_mountpt);
    969 #undef LOAD
    970 
    971 		for (size_t i = 0;
    972 		    i < edit->pset->parts->pscheme->custom_attribute_count;
    973 		    i++) {
    974 			attrname = msg_string(
    975 			    edit->pset->parts->pscheme->custom_attributes[i]
    976 			    .label);
    977 			l = strlen(attrname);
    978 			if (l > col_width) col_width = l;
    979 		}
    980 
    981 		col_width += 3;
    982 	}
    983 
    984 	if (m->opts[opt].opt_flags & OPT_IGNORE
    985 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
    986 	    && m->opts[opt].opt_action != edit_ptn_custom_type) {
    987 		wprintw(m->mw, "%*s -", col_width, "");
    988 		return;
    989 	}
    990 
    991 	if (opt < 4) {
    992 		switch (opt) {
    993 		case 0:
    994 			if (edit->info.fs_type == FS_BSDFFS)
    995 				if (edit->info.fs_sub_type == 2)
    996 					c = msg_string(MSG_fs_type_ffsv2);
    997 				else
    998 					c = msg_string(MSG_fs_type_ffs);
    999 			else
   1000 				c = getfslabelname(edit->info.fs_type,
   1001 				    edit->info.fs_sub_type);
   1002 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
   1003 			return;
   1004 		case 1:
   1005 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1006 			    ptn_start, edit->info.start / sizemult, multname);
   1007 			return;
   1008 		case 2:
   1009 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1010 			    ptn_size, edit->info.size / sizemult, multname);
   1011 			return;
   1012 		case 3:
   1013 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1014 			    ptn_end, (edit->info.start + edit->info.size)
   1015 			    / sizemult, multname);
   1016 			return;
   1017 		 }
   1018 	}
   1019 	if (m->opts[opt].opt_action == edit_install) {
   1020 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
   1021 			msg_string(edit->info.start == pm->ptstart
   1022 			    ? MSG_Yes : MSG_No));
   1023 		return;
   1024 	}
   1025 	if (m->opts[opt].opt_action == edit_fs_preserve) {
   1026 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
   1027 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
   1028 			    ? MSG_Yes : MSG_No));
   1029 		return;
   1030 	}
   1031 	if (m->opts[opt].opt_action == edit_fs_mount) {
   1032 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
   1033 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
   1034 			    ? MSG_Yes : MSG_No));
   1035 		return;
   1036 	}
   1037 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
   1038 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
   1039 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
   1040 			wprintw(m->mw, "async ");
   1041 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
   1042 			wprintw(m->mw, "noatime ");
   1043 		if (edit->wanted->mountflags & PUIMNT_NODEV)
   1044 			wprintw(m->mw, "nodev ");
   1045 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
   1046 			wprintw(m->mw, "nodevmtime ");
   1047 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
   1048 			wprintw(m->mw, "noexec ");
   1049 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
   1050 			wprintw(m->mw, "nosuid ");
   1051 		if (edit->wanted->mountflags & PUIMNT_LOG)
   1052 			wprintw(m->mw, "log ");
   1053 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
   1054 			wprintw(m->mw, "noauto  ");
   1055 		return;
   1056 	}
   1057 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
   1058 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
   1059 		    edit->wanted->mount);
   1060 		return;
   1061 	}
   1062 
   1063 	attr_no = opt - edit->first_custom_attr;
   1064 	edit->pset->parts->pscheme->format_custom_attribute(
   1065 	    edit->pset->parts, edit->id, attr_no, &edit->info,
   1066 	    val, sizeof val);
   1067 	attrname = msg_string(edit->pset->parts->pscheme->
   1068 	    custom_attributes[attr_no].label);
   1069 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
   1070 }
   1071 
   1072 static int
   1073 edit_ptn_custom_type(menudesc *m, void *arg)
   1074 {
   1075 	struct single_part_fs_edit *edit = arg;
   1076 	size_t attr_no = m->cursel - edit->first_custom_attr;
   1077 	char line[STRSIZE];
   1078 
   1079 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
   1080 	case pet_bool:
   1081 		edit->pset->parts->pscheme->custom_attribute_toggle(
   1082 		    edit->pset->parts, edit->id, attr_no);
   1083 		break;
   1084 	case pet_cardinal:
   1085 	case pet_str:
   1086 		edit->pset->parts->pscheme->format_custom_attribute(
   1087 		    edit->pset->parts, edit->id, attr_no, &edit->info,
   1088 		    line, sizeof(line));
   1089 		msg_prompt_win(
   1090 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
   1091 		    label, -1, 18, 0, 0, line, line, sizeof(line));
   1092 		edit->pset->parts->pscheme->custom_attribute_set_str(
   1093 		    edit->pset->parts, edit->id, attr_no, line);
   1094 		break;
   1095 	}
   1096 
   1097 	return 0;
   1098 }
   1099 
   1100 
   1101 /*
   1102  * Some column width depend on translation, we will set these in
   1103  * fmt_fspart_header and later use it when formatting single entries
   1104  * in fmt_fspart_row.
   1105  * The table consist of 3 "size like" columns, all fixed width, then
   1106  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
   1107  * (which is variable width).
   1108  */
   1109 static int fstype_width, flags_width;
   1110 static char fspart_separator[MENUSTRSIZE];
   1111 static char fspart_title[2*MENUSTRSIZE];
   1112 
   1113 /*
   1114  * Format the header of the main partition editor menu.
   1115  */
   1116 static void
   1117 fmt_fspart_header(menudesc *menu, void *arg)
   1118 {
   1119 	struct partition_usage_set *pset = arg;
   1120 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
   1121 	    sepline[MENUSTRSIZE], *p;
   1122 	const char *fstype, *flags;
   1123 	int i;
   1124 	bool with_inst_flag = pset->parts->parent == NULL;
   1125 
   1126 	humanize_number(total, sizeof total,
   1127 	    pset->parts->disk_size * 512,
   1128 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1129 	humanize_number(free_space, sizeof free_space,
   1130 	    pset->cur_free_space * 512,
   1131 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1132 
   1133 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
   1134 	    msg_string(pset->parts->pscheme->name),
   1135 	    msg_string(pset->parts->pscheme->short_name),
   1136 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
   1137 	    pset->parts->pscheme->part_flag_desc ?
   1138 	        msg_string(pset->parts->pscheme->part_flag_desc)
   1139 		: "",
   1140 	    total, free_space);
   1141 
   1142 	snprintf(scol, sizeof scol, "%s (%s)",
   1143 	    msg_string(MSG_ptnheaders_start), multname);
   1144 	snprintf(ecol, sizeof ecol, "%s (%s)",
   1145 	    msg_string(MSG_ptnheaders_end), multname);
   1146 	snprintf(szcol, sizeof szcol, "%s (%s)",
   1147 	    msg_string(MSG_ptnheaders_size), multname);
   1148 
   1149 	fstype = msg_string(MSG_ptnheaders_fstype);
   1150 	flags = msg_string(MSG_part_header_col_flag);
   1151 	fstype_width = max(strlen(fstype), 8);
   1152 	flags_width = strlen(flags);
   1153 	for (i = 0, p = sepline; i < fstype_width; i++)
   1154 		*p++ = '-';
   1155 	for (i = 0, *p++ = ' '; i < flags_width; i++)
   1156 		*p++ = '-';
   1157 	*p = 0;
   1158 
   1159 	snprintf(fspart_separator, sizeof(fspart_separator),
   1160 	    "------------ ------------ ------------ %s ----------------",
   1161 	    sepline);
   1162 
   1163 	snprintf(fspart_title, sizeof(fspart_title),
   1164 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
   1165 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
   1166 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
   1167 	    fspart_separator);
   1168 
   1169 	msg_table_add("\n\n");
   1170 }
   1171 
   1172 /*
   1173  * Format one partition entry in the main partition editor.
   1174  */
   1175 static void
   1176 fmt_fspart_row(menudesc *m, int ptn, void *arg)
   1177 {
   1178 	struct partition_usage_set *pset = arg;
   1179 	struct disk_part_info info;
   1180 	daddr_t poffset, psize, pend;
   1181 	const char *desc;
   1182 	static const char *Yes;
   1183 	char flag_str[MENUSTRSIZE], *fp;
   1184 	unsigned inst_flags;
   1185 	bool with_inst_flag = pset->parts->parent == NULL;
   1186 
   1187 	if (Yes == NULL)
   1188 		Yes = msg_string(MSG_Yes);
   1189 
   1190 	if (!real_partition(pset, ptn))
   1191 		return;
   1192 
   1193 	if (!pset->parts->pscheme->get_part_info(pset->parts,
   1194 	    pset->infos[ptn].cur_part_id, &info))
   1195 		return;
   1196 
   1197 	/* enable / disable this line if it is something like RAW_PART */
   1198 	if (info.flags & (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1199 		m->opts[ptn].opt_flags |= OPT_IGNORE;
   1200 	else
   1201 		m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1202 
   1203 	poffset = info.start / sizemult;
   1204 	psize = info.size / sizemult;
   1205 	if (psize == 0)
   1206 		pend = 0;
   1207 	else
   1208 		pend = (info.start + info.size) / sizemult - 1;
   1209 
   1210 	if (info.flags & PTI_WHOLE_DISK)
   1211 		desc = msg_string(MSG_NetBSD_partition_cant_change);
   1212 	else if (info.flags & PTI_RAW_PART)
   1213 		desc = msg_string(MSG_Whole_disk_cant_change);
   1214 	else if (info.flags & PTI_BOOT)
   1215 		desc = msg_string(MSG_Boot_partition_cant_change);
   1216 	else
   1217 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
   1218 
   1219 	fp = flag_str;
   1220 	inst_flags = pset->infos[ptn].instflags;
   1221 	if (with_inst_flag && info.start == pm->ptstart &&
   1222 	    info.nat_type->generic_ptype == PT_root) {
   1223 		static char inst_flag;
   1224 
   1225 		if (inst_flag == 0)
   1226 			inst_flag = msg_string(MSG_install_flag)[0];
   1227 		*fp++ = inst_flag;
   1228 	}
   1229 	if (inst_flags & PUIINST_NEWFS)
   1230 		*fp++ = msg_string(MSG_newfs_flag)[0];
   1231 	*fp = 0;
   1232 	if (pset->parts->pscheme->get_part_attr_str != NULL)
   1233 		pset->parts->pscheme->get_part_attr_str(pset->parts,
   1234 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
   1235 
   1236 	/* if the fstype description does not fit, check if we can overrun */
   1237 	if (strlen(desc) > (size_t)fstype_width &&
   1238 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
   1239 	     info.last_mounted[0] == 0))
   1240 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1241 		    " %s",
   1242 		    poffset, pend, psize, desc);
   1243 	else
   1244 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1245 		    " %*.*s %*s %s",
   1246 		    poffset, pend, psize, fstype_width, fstype_width, desc,
   1247 		    -flags_width, flag_str,
   1248 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
   1249 		     info.last_mounted[0] ? info.last_mounted : "");
   1250 }
   1251 
   1252 static int
   1253 edit_fspart_pack(menudesc *m, void *arg)
   1254 {
   1255 	struct partition_usage_set *pset = arg;
   1256 	char buf[STRSIZE];
   1257 
   1258 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
   1259 	    buf, sizeof buf))
   1260 		return 0;
   1261 
   1262 	msg_prompt_win(MSG_edit_disk_pack_hdr,
   1263 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
   1264 
   1265 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
   1266 	return 0;
   1267 }
   1268 
   1269 static int
   1270 edit_fspart_add(menudesc *m, void *arg)
   1271 {
   1272 	struct partition_usage_set *pset = arg;
   1273 	struct part_usage_info *ninfo;
   1274 	menu_ent *nmenopts;
   1275 	size_t cnt, off;
   1276 
   1277 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1278 	if (ninfo == NULL)
   1279 		return 0;
   1280 	pset->infos = ninfo;
   1281 	off = pset->parts->num_part;
   1282 	cnt = pset->num-pset->parts->num_part;
   1283 	if (cnt > 0)
   1284 		memmove(pset->infos+off+1,pset->infos+off,
   1285 		    cnt*sizeof(*pset->infos));
   1286 	memset(pset->infos+off, 0, sizeof(*pset->infos));
   1287 
   1288 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1289 	if (nmenopts == NULL)
   1290 		return 0;
   1291 	memmove(nmenopts+off+1, nmenopts+off,
   1292 	    (m->numopts-off)*sizeof(*nmenopts));
   1293 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1294 	nmenopts[off].opt_menu = OPT_NOMENU;
   1295 	nmenopts[off].opt_action = edit_ptn;
   1296 	pset->menu_opts = m->opts = nmenopts;
   1297 	m->numopts++;
   1298 	m->cursel = off;
   1299 	pset->num++;
   1300 
   1301 	/* now update edit menu to fit */
   1302 	m->h = 0;
   1303 	resize_menu_height(m);
   1304 
   1305 	/* and directly invoke the partition editor for the new part */
   1306 	edit_ptn(m, arg);
   1307 
   1308 	show_partition_adder(m, pset);
   1309 
   1310 	return -1;
   1311 }
   1312 
   1313 static void
   1314 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1315 {
   1316 	struct part_usage_info *ninfo;
   1317 	menu_ent *nmenopts;
   1318 	size_t off;
   1319 
   1320 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1321 	if (ninfo == NULL)
   1322 		return;
   1323 	pset->infos = ninfo;
   1324 	off = pset->parts->num_part+1;
   1325 
   1326 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1327 	if (nmenopts == NULL)
   1328 		return;
   1329 	memmove(nmenopts+off+1, nmenopts+off,
   1330 	    (m->numopts-off)*sizeof(*nmenopts));
   1331 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1332 
   1333 	nmenopts[off].opt_name = MSG_addpart;
   1334 	nmenopts[off].opt_menu = OPT_NOMENU;
   1335 	nmenopts[off].opt_flags = OPT_SUB;
   1336 	nmenopts[off].opt_action = edit_fspart_add;
   1337 
   1338 	m->opts = nmenopts;
   1339 	m->numopts++;
   1340 	pset->num++;
   1341 }
   1342 
   1343 static void
   1344 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1345 {
   1346 	size_t off;
   1347 
   1348 	off = pset->parts->num_part+1;
   1349 	memmove(m->opts+off, m->opts+off+1,
   1350 	    (m->numopts-off-1)*sizeof(*m->opts));
   1351 	m->numopts--;
   1352 	pset->num--;
   1353 }
   1354 
   1355 /*
   1356  * Called whenever the "add a partition" option may need to be removed
   1357  * or added
   1358  */
   1359 static void
   1360 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1361 {
   1362 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
   1363 	    pset->parts);
   1364 	bool part_adder_present =
   1365 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
   1366 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
   1367 
   1368 	if (can_add_partition == part_adder_present)
   1369 		return;
   1370 
   1371 	if (can_add_partition)
   1372 		add_partition_adder(m, pset);
   1373 	else
   1374 		remove_partition_adder(m, pset);
   1375 
   1376 	/* now update edit menu to fit */
   1377 	m->h = 0;
   1378 	resize_menu_height(m);
   1379 }
   1380 
   1381 static int
   1382 edit_fspart_abort(menudesc *m, void *arg)
   1383 {
   1384 	struct partition_usage_set *pset = arg;
   1385 
   1386 	pset->ok = false;
   1387 	return 1;
   1388 }
   1389 
   1390 /*
   1391  * Check a disklabel.
   1392  * If there are overlapping active partitions,
   1393  * Ask the user if they want to edit the partition or give up.
   1394  */
   1395 int
   1396 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset)
   1397 {
   1398 	menu_ent *op;
   1399 	size_t cnt, i;
   1400 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
   1401 	bool may_edit_pack =
   1402 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
   1403 	    pset->parts->pscheme->set_disk_pack_name != NULL;
   1404 
   1405 	pset->menu_opts = calloc(pset->parts->num_part
   1406 	     +3+may_add+may_edit_pack,
   1407 	     sizeof *pset->menu_opts);
   1408 	if (pset->menu_opts == NULL)
   1409 		return 0;
   1410 
   1411 	op = pset->menu_opts;
   1412 	for (i = 0; i < pset->parts->num_part; i++) {
   1413 		op->opt_menu = OPT_NOMENU;
   1414 		op->opt_action = edit_ptn;
   1415 		op++;
   1416 	}
   1417 	/* separator line between partitions and actions */
   1418 	op->opt_name = fspart_separator;
   1419 	op->opt_menu = OPT_NOMENU;
   1420 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
   1421 	op++;
   1422 
   1423 	/* followed by new partition adder */
   1424 	if (may_add) {
   1425 		op->opt_name = MSG_addpart;
   1426 		op->opt_menu = OPT_NOMENU;
   1427 		op->opt_flags = OPT_SUB;
   1428 		op->opt_action = edit_fspart_add;
   1429 		op++;
   1430 	}
   1431 
   1432 	/* and unit changer */
   1433 	op->opt_name = MSG_askunits;
   1434 	op->opt_menu = MENU_sizechoice;
   1435 	op->opt_flags = OPT_SUB;
   1436 	op->opt_action = NULL;
   1437 	op++;
   1438 
   1439 	if (may_edit_pack) {
   1440 		op->opt_name = MSG_editpack;
   1441 		op->opt_menu = OPT_NOMENU;
   1442 		op->opt_flags = OPT_SUB;
   1443 		op->opt_action = edit_fspart_pack;
   1444 		op++;
   1445 	}
   1446 
   1447 	/* and abort option */
   1448 	op->opt_name = MSG_cancel;
   1449 	op->opt_menu = OPT_NOMENU;
   1450 	op->opt_flags = OPT_EXIT;
   1451 	op->opt_action = edit_fspart_abort;
   1452 	op++;
   1453 	cnt = op - pset->menu_opts;
   1454 	assert(cnt == pset->parts->num_part+3+may_add+may_edit_pack);
   1455 
   1456 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
   1457 			0, -1, 0, 74,
   1458 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
   1459 			MC_NOCLEAR|MC_CONTINUOUS,
   1460 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
   1461 			MSG_partition_sizes_ok);
   1462 
   1463 	if (pset->menu < 0) {
   1464 		free(pset->menu_opts);
   1465 		pset->menu_opts = NULL;
   1466 		return 0;
   1467 	}
   1468 
   1469 	p->current_cylsize = p->dlcylsize;
   1470 
   1471 	for (;;) {
   1472 		/* first give the user the option to edit the label... */
   1473 		pset->ok = true;
   1474 		process_menu(pset->menu, pset);
   1475 		if (!pset->ok) {
   1476 			i = 0;
   1477 			break;
   1478 		}
   1479 
   1480 		/* User thinks the label is OK. */
   1481 		i = verify_parts(pset);
   1482 		if (i == 1)
   1483 			continue;
   1484 		break;
   1485 	}
   1486 	free(pset->menu_opts);
   1487 	pset->menu_opts = NULL;
   1488 	free_menu(pset->menu);
   1489 	pset->menu = -1;
   1490 
   1491 	return i != 0;
   1492 }
   1493 
   1494 /*
   1495  * Try to get 'last mounted on' (or equiv) from fs superblock.
   1496  */
   1497 const char *
   1498 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
   1499     uint flags)
   1500 {
   1501 	static char sblk[SBLOCKSIZE];		/* is this enough? */
   1502 	struct fs *SB = (struct fs *)sblk;
   1503 	static const off_t sblocks[] = SBLOCKSEARCH;
   1504 	const off_t *sbp;
   1505 	const char *mnt = NULL;
   1506 	int len;
   1507 
   1508 	if (fd == -1)
   1509 		return "";
   1510 
   1511 	if (fs_type)
   1512 		*fs_type = 0;
   1513 	if (fs_sub_type)
   1514 		*fs_sub_type = 0;
   1515 
   1516 	/* Check UFS1/2 (and hence LFS) superblock */
   1517 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
   1518 		if (pread(fd, sblk, sizeof sblk,
   1519 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
   1520 			continue;
   1521 
   1522 		/*
   1523 		 * If start of partition and allowed by flags check
   1524 		 * for other fs types
   1525 		 */
   1526 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
   1527 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
   1528 			/* Probably a FAT filesystem, report volume name */
   1529 			size_t i;
   1530 			for (i = 0x51; i >= 0x47; i--) {
   1531 				if (sblk[i] != ' ')
   1532 					break;
   1533 				sblk[i] = 0;
   1534 			}
   1535 			sblk[0x52] = 0;
   1536 			if (fs_type)
   1537 				*fs_type = FS_MSDOS;
   1538 			if (fs_sub_type)
   1539 				*fs_sub_type = sblk[0x53];
   1540 			return sblk + 0x47;
   1541 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
   1542 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
   1543 			if (fs_type)
   1544 				*fs_type = FS_NTFS;
   1545 			if (fs_sub_type)
   1546 				*fs_sub_type = MBR_PTYPE_NTFS;
   1547 			/* XXX dig for volume name attribute ? */
   1548 			return "";
   1549 		}
   1550 
   1551 		if (!(flags & GLM_LIKELY_FFS))
   1552 			continue;
   1553 
   1554 		/* Maybe we should validate the checksum??? */
   1555 		switch (SB->fs_magic) {
   1556 		case FS_UFS1_MAGIC:
   1557 		case FS_UFS1_MAGIC_SWAPPED:
   1558 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
   1559 				if (*sbp == SBLOCK_UFS1)
   1560 					mnt = (const char *)SB->fs_fsmnt;
   1561 			} else {
   1562 				/* Check we have the main superblock */
   1563 				if (SB->fs_sblockloc == *sbp)
   1564 					mnt = (const char *)SB->fs_fsmnt;
   1565 			}
   1566 			if (fs_type)
   1567 				*fs_type = FS_BSDFFS;
   1568 			if (fs_sub_type)
   1569 				*fs_sub_type = 2;
   1570 			continue;
   1571 		case FS_UFS2_MAGIC:
   1572 		case FS_UFS2_MAGIC_SWAPPED:
   1573 			/* Check we have the main superblock */
   1574 			if (SB->fs_sblockloc == *sbp) {
   1575 				mnt = (const char *)SB->fs_fsmnt;
   1576 				if (fs_type)
   1577 					*fs_type = FS_BSDFFS;
   1578 				if (fs_sub_type)
   1579 					*fs_sub_type = 2;
   1580 			}
   1581 			continue;
   1582 		}
   1583 	}
   1584 
   1585 	if (mnt == NULL)
   1586 		return "";
   1587 
   1588 	/* If sysinst mounted this last then strip prefix */
   1589 	len = strlen(targetroot_mnt);
   1590 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
   1591 		if (mnt[len] == 0)
   1592 			return "/";
   1593 		if (mnt[len] == '/')
   1594 			return mnt + len;
   1595 	}
   1596 	return mnt;
   1597 	#undef SB
   1598 }
   1599 
   1600 /* Ask for a partition offset, check bounds and do the needed roundups */
   1601 daddr_t
   1602 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
   1603 {
   1604 	char defstart[24], isize[24], maxpart, minspace, maxspace,
   1605 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
   1606 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
   1607 	size_t num_freespace, spaces, ndx;
   1608 	struct disk_part_free_space *freespace;
   1609 	daddr_t i, localsizemult, ptn_alignment, min, max;
   1610 	part_id partn;
   1611 	struct disk_part_info info;
   1612 	const char *errmsg = NULL;
   1613 
   1614 	min = parts->disk_start;
   1615 	max = min + parts->disk_size;
   1616 
   1617 	/* upper bound on the number of free spaces, plus some slope */
   1618 	num_freespace = parts->num_part * 2 + 5;
   1619 	freespace = calloc(num_freespace, sizeof(*freespace));
   1620 	if (freespace == NULL)
   1621 		return -1;
   1622 
   1623 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
   1624 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
   1625 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
   1626 	    defpartstart);
   1627 
   1628 	maxpart = 'a' + parts->num_part -1;
   1629 	if (parts->num_part > 1) {
   1630 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
   1631 	} else if (parts->num_part == 1) {
   1632 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
   1633 	} else {
   1634 		strcpy(valid_parts, "---");
   1635 	}
   1636 	if (spaces > 1) {
   1637 		minspace = maxpart + 1;
   1638 		maxspace = minspace + spaces -1;
   1639 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
   1640 		    maxspace);
   1641 	} else if (spaces == 1) {
   1642 		maxspace = minspace = maxpart + 1;
   1643 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
   1644 	} else {
   1645 		minspace = 0;
   1646 		maxspace = 0;
   1647 		strcpy(valid_spaces, "---");
   1648 	}
   1649 
   1650 	/* Add description of start/size to user prompt */
   1651 	const char *mstr = msg_string(MSG_free_space_line);
   1652         space_prompt[0] = 0;
   1653         for (ndx = 0; ndx < spaces; ndx++) {
   1654                 char str_start[40], str_end[40], str_size[40], str_tag[4];
   1655 
   1656 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
   1657                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
   1658                 sprintf(str_end, "%" PRIu64,
   1659                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
   1660                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
   1661                 const char *args[4] = { str_start, str_end, str_size,
   1662                     multname };
   1663                 char *line = str_arg_subst(mstr, 4, args);
   1664 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
   1665                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
   1666                 free (line);
   1667                 if (len >= sizeof space_prompt)
   1668                         break;
   1669         }
   1670 
   1671 	const char *args[] = { valid_parts, valid_spaces, multname };
   1672 	hint_part = NULL;
   1673 	hint_space = NULL;
   1674 	head = str_arg_subst(msg_string(MSG_label_offset_head),
   1675 	    __arraycount(args), args);
   1676 	if (parts->num_part)
   1677 		hint_part = str_arg_subst(msg_string(
   1678 		    MSG_label_offset_part_hint), __arraycount(args), args);
   1679 	if (spaces)
   1680 		hint_space = str_arg_subst(msg_string(
   1681 		    MSG_label_offset_space_hint), __arraycount(args), args);
   1682 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
   1683 	    __arraycount(args), args);
   1684 
   1685 	if (hint_part && hint_space)
   1686 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
   1687 		    head, hint_part, hint_space, space_prompt, tail);
   1688 	else if (hint_part)
   1689 		asprintf(&label_msg, "%s\n%s\n\n%s",
   1690 		    head, hint_part, tail);
   1691 	else if (hint_space)
   1692 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
   1693 		    head, hint_space, space_prompt, tail);
   1694 	else
   1695 		asprintf(&label_msg, "%s\n\n%s",
   1696 		    head, tail);
   1697 	free(head); free(hint_part); free(hint_space); free(tail);
   1698 
   1699 	localsizemult = sizemult;
   1700 	errmsg = NULL;
   1701 	for (;;) {
   1702 		snprintf(defstart, sizeof defstart, "%" PRIu64,
   1703 		    defpartstart/sizemult);
   1704 		if (errmsg != NULL && errmsg[0] != 0)
   1705 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   1706 		else
   1707 			prompt = label_msg;
   1708 		msg_prompt_win(prompt, -1, 13, 70, -1,
   1709 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
   1710 		if (label_msg != prompt)
   1711 			free(prompt);
   1712 		if (strcmp(defstart, isize) == 0) {
   1713 			/* Don't do rounding if default accepted */
   1714 			i = defpartstart;
   1715 			break;
   1716 		}
   1717 		if (isize[1] == '\0' && isize[0] >= 'a' &&
   1718 		    isize[0] <= maxpart) {
   1719 			partn = isize[0] - 'a';
   1720 			if (parts->pscheme->get_part_info(parts, partn,
   1721 			    &info)) {
   1722 				i = info.start + info.size;
   1723 				localsizemult = 1;
   1724 			} else {
   1725 				errmsg = msg_string(MSG_invalid_sector_number);
   1726 				continue;
   1727 			}
   1728 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
   1729 		    isize[0] <= maxspace) {
   1730 			ndx = isize[0] - minspace;
   1731 			i = freespace[ndx].start;
   1732 			localsizemult = 1;
   1733 		} else if (atoi(isize) == -1) {
   1734 			i = min;
   1735 			localsizemult = 1;
   1736 		} else {
   1737 			i = parse_disk_pos(isize, &localsizemult, pm->dlcylsize, NULL);
   1738 			if (i < 0) {
   1739 				errmsg = msg_string(MSG_invalid_sector_number);
   1740 				continue;
   1741 			}
   1742 		}
   1743 		/* round to cylinder size if localsizemult != 1 */
   1744 		i = NUMSEC(i, localsizemult, pm->dlcylsize);
   1745 		/* Adjust to start of slice if needed */
   1746 		if ((i < min && (min - i) < localsizemult) ||
   1747 		    (i > min && (i - min) < localsizemult)) {
   1748 			i = min;
   1749 		}
   1750 		if (max == 0 || i <= max)
   1751 			break;
   1752 		errmsg = msg_string(MSG_startoutsidedisk);
   1753 	}
   1754 	free(label_msg);
   1755 	free(freespace);
   1756 
   1757 	return i;
   1758 }
   1759 
   1760 
   1761 /* Ask for a partition size, check bounds and do the needed roundups */
   1762 daddr_t
   1763 getpartsize(struct disk_partitions *parts, daddr_t partstart, daddr_t dflt)
   1764 {
   1765 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
   1766 	    *label_msg, *prompt, *head, *hint, *tail;
   1767 	const char *errmsg = NULL;
   1768 	daddr_t i, partend, localsizemult, max, max_r, dflt_r;
   1769 	struct disk_part_info info;
   1770 	part_id partn;
   1771 
   1772 	max = parts->pscheme->max_free_space_at(parts, partstart);
   1773 
   1774 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
   1775 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
   1776 	if (max == dflt)
   1777 		max_r = dflt_r;
   1778 	else
   1779 		max_r = max / sizemult;
   1780 	/* the partition may have been moved and now not fit any longer */
   1781 	if (dflt > max)
   1782 		dflt = max;
   1783 	if (dflt_r > max_r)
   1784 		dflt_r = max_r;
   1785 
   1786 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
   1787 
   1788 	maxpartc = 'a' + parts->num_part -1;
   1789 	if (parts->num_part > 1) {
   1790 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
   1791 	} else if (parts->num_part == 1) {
   1792 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
   1793 	} else {
   1794 		strcpy(valid_parts, "---");
   1795 	}
   1796 
   1797 	const char *args[] = { valid_parts, max_size, multname };
   1798 	hint = NULL;
   1799 	head = str_arg_subst(msg_string(MSG_label_size_head),
   1800 	    __arraycount(args), args);
   1801 	if (parts->num_part)
   1802 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
   1803 		    __arraycount(args), args);
   1804 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
   1805 	    __arraycount(args), args);
   1806 
   1807 	if (hint != NULL)
   1808 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
   1809 	else
   1810 		asprintf(&label_msg, "%s\n\n%s", head, tail);
   1811 	free(head); free(hint); free(tail);
   1812 
   1813 	localsizemult = sizemult;
   1814 	i = -1;
   1815 	for (;;) {
   1816 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
   1817 
   1818 		if (errmsg != NULL && errmsg[0] != 0)
   1819 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   1820 		else
   1821 			prompt = label_msg;
   1822 		msg_prompt_win(prompt, -1, 12, 70, -1,
   1823 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
   1824 		if (prompt != label_msg)
   1825 			free(prompt);
   1826 
   1827 		if (strcmp(isize, dsize) == 0) {
   1828 			free(label_msg);
   1829 			return dflt;
   1830 		}
   1831 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
   1832 		    isize[0] <= maxpartc) {
   1833 			partn = isize[0] - 'a';
   1834 			if (parts->pscheme->get_part_info(parts, partn,
   1835 			    &info)) {
   1836 				i = info.start - partstart -1;
   1837 				localsizemult = 1;
   1838 				max_r = max;
   1839 			}
   1840 		} else if (atoi(isize) == -1) {
   1841 			i = max;
   1842 			localsizemult = 1;
   1843 			max_r = max;
   1844 		} else {
   1845 			i = parse_disk_pos(isize, &localsizemult,
   1846 			    pm->dlcylsize, NULL);
   1847 			if (localsizemult != sizemult)
   1848 				max_r = max;
   1849 		}
   1850 		if (i < 0) {
   1851 			errmsg = msg_string(MSG_Invalid_numeric);
   1852 			continue;
   1853 		} else if (i > max_r) {
   1854 			errmsg = msg_string(MSG_Too_large);
   1855 			continue;
   1856 		}
   1857 		/*
   1858 		 * partend is aligned to a cylinder if localsizemult
   1859 		 * is not 1 sector
   1860 		 */
   1861 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
   1862 		    localsizemult, pm->dlcylsize);
   1863 		/* Align to end-of-disk or end-of-slice if close enough */
   1864 		if (partend > (pm->dlsize - sizemult)
   1865 		    && partend < (pm->dlsize + sizemult))
   1866 			partend = pm->dlsize;
   1867 		if (partend > (partstart + max - sizemult)
   1868 		    && partend < (partstart + max + sizemult))
   1869 			partend = partstart + max;
   1870 		/* sanity checks */
   1871 		if (partend > (partstart + pm->dlsize)) {
   1872 			partend = pm->dlsize;
   1873 			errmsg = msg_string(MSG_endoutsidedisk);
   1874 			continue;
   1875 		}
   1876 		free(label_msg);
   1877 		if (partend < partstart)
   1878 			return 0;
   1879 		return (partend - partstart);
   1880 	}
   1881 	/* NOTREACHED */
   1882 }
   1883 
   1884 /*
   1885  * convert a string to a number of sectors, with a possible unit
   1886  * 150M = 150 Megabytes
   1887  * 2000c = 2000 cylinders
   1888  * 150256s = 150256 sectors
   1889  * Without units, use the default (sizemult).
   1890  * returns the raw input value, and the unit used. Caller needs to multiply!
   1891  * On invalid inputs, returns -1.
   1892  */
   1893 daddr_t
   1894 parse_disk_pos(
   1895 	const char *str,
   1896 	daddr_t *localsizemult,
   1897 	daddr_t cyl_size,
   1898 	bool *extend_this)
   1899 {
   1900 	daddr_t val;
   1901 	char *cp;
   1902 	bool mult_found;
   1903 
   1904 	if (str[0] == '\0') {
   1905 		return -1;
   1906 	}
   1907 	val = strtoull(str, &cp, 10);
   1908 	mult_found = false;
   1909 	if (extend_this)
   1910 		*extend_this = false;
   1911 	while (*cp != 0) {
   1912 		if (*cp == 'G' || *cp == 'g') {
   1913 			if (mult_found)
   1914 				return -1;
   1915 			*localsizemult = GIG / pm->sectorsize;
   1916 			goto next;
   1917 		}
   1918 		if (*cp == 'M' || *cp == 'm') {
   1919 			if (mult_found)
   1920 				return -1;
   1921 			*localsizemult = MEG / pm->sectorsize;
   1922 			goto next;
   1923 		}
   1924 		if (*cp == 'c' || *cp == 'C') {
   1925 			if (mult_found)
   1926 				return -1;
   1927 			*localsizemult = pm->dlcylsize;
   1928 			goto next;
   1929 		}
   1930 		if (*cp == 's' || *cp == 'S') {
   1931 			if (mult_found)
   1932 				return -1;
   1933 			*localsizemult = 1;
   1934 			goto next;
   1935 		}
   1936 		if (*cp == '+' && extend_this) {
   1937 			*extend_this = true;
   1938 			cp++;
   1939 			break;
   1940 		}
   1941 
   1942 		/* not a known unit */
   1943 		return -1;
   1944 
   1945 next:
   1946 		mult_found = true;
   1947 		cp++;
   1948 		continue;
   1949 	}
   1950 	if (*cp != 0)
   1951 		return -1;
   1952 
   1953 	return val;
   1954 }
   1955