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