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