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