Home | History | Annotate | Line # | Download | only in sysinst
part_edit.c revision 1.12
      1 /*	$NetBSD: part_edit.c,v 1.12 2019/11/13 18:57:26 martin Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 
     30 /* part_edit.c -- generic partition editing code */
     31 
     32 #include <sys/param.h>
     33 #include <sys/types.h>
     34 #include <assert.h>
     35 #include <stdio.h>
     36 #include <unistd.h>
     37 #include <fcntl.h>
     38 #include <util.h>
     39 #include "defs.h"
     40 #include "md.h"
     41 #include "msg_defs.h"
     42 #include "menu_defs.h"
     43 #include "defsizes.h"
     44 #include "endian.h"
     45 
     46 
     47 /*
     48  * A structure passed to various menu functions for partition editing
     49  */
     50 struct part_edit_info {
     51 	struct disk_partitions *parts;	/* the partitions we edit */
     52 	struct disk_part_info cur;	/* current value (maybe incomplete) */
     53 	part_id cur_id;			/* which partition is it? */
     54 	int first_custom_opt;		/* scheme specific menu options
     55 					 * start here */
     56 	bool cancelled;			/* do not apply changes */
     57 	bool num_changed;		/* number of partitions has changed */
     58 };
     59 
     60 #ifndef NO_CLONES
     61 struct single_clone_data {
     62 	struct selected_partitions clone_src;
     63 	part_id *clone_ids;	/* partition IDs in target */
     64 };
     65 #endif
     66 struct outer_parts_data {
     67 	struct arg_rv av;
     68 #ifndef NO_CLONES
     69 	struct single_clone_data *clones;
     70 	size_t num_clone_entries;
     71 #endif
     72 };
     73 
     74 static menu_ent *part_menu_opts;		/* the currently edited partitions */
     75 static menu_ent *outer_fill_part_menu_opts(const struct disk_partitions *parts, size_t *cnt);
     76 static void draw_outer_part_line(menudesc *m, int opt, void *arg);
     77 
     78 static char 	outer_part_sep_line[MENUSTRSIZE],
     79 		outer_part_title[2*MENUSTRSIZE];
     80 
     81 static int
     82 maxline(const char *p, int *count)
     83 {
     84 	int m = 0, i = 0;
     85 
     86 	for (;; p++) {
     87 		if (*p == '\n' || *p == 0) {
     88 			if (i > m)
     89 				m = i;
     90 			(*count)++;
     91 			if (*p == 0)
     92 				return m;
     93 			i = 0;
     94 		} else {
     95 			i++;
     96 		}
     97 	}
     98 }
     99 
    100 int
    101 err_msg_win(const char *errmsg)
    102 {
    103 	const char *cont;
    104 	int l, l1, lines;
    105 
    106 	errmsg = msg_string(errmsg);
    107 	cont = msg_string(MSG_Hit_enter_to_continue);
    108 
    109 	lines = 0;
    110 	l = maxline(errmsg, &lines);
    111 	l1 = maxline(cont, &lines);
    112 	if (l < l1)
    113 		l = l1;
    114 
    115 	msg_fmt_prompt_win("%s.\n%s", -1, 18, l + 5, 2+lines,
    116 			NULL, NULL, 1, "%s%s", errmsg, cont);
    117 	return 0;
    118 }
    119 
    120 static int
    121 set_part_type(menudesc *m, void *arg)
    122 {
    123 	struct part_edit_info *info = arg;
    124 	const struct part_type_desc *desc;
    125 	char buf[STRSIZE];
    126 	const char *err;
    127 
    128 	if (m->cursel == 0)
    129 		return 1;	/* no change */
    130 
    131 	desc = info->parts->pscheme->get_part_type(m->cursel-1);
    132 	if (desc == NULL) {
    133 		/* Create custom type */
    134 		if (info->cur.nat_type != NULL)
    135 			strlcpy(buf, info->cur.nat_type->short_desc,
    136 			    sizeof(buf));
    137 		else
    138 			buf[0] = 0;
    139 		for (;;) {
    140 			msg_prompt_win(info->parts->pscheme->new_type_prompt,
    141 			     -1, 18, 0, 0,
    142 			    buf, buf, sizeof(buf));
    143 			if (buf[0] == 0)
    144 				break;
    145 			desc = info->parts->pscheme->create_custom_part_type(
    146 			    buf, &err);
    147 			if (desc != NULL)
    148 				break;
    149 			err_msg_win(err);
    150 		}
    151 	}
    152 
    153 	info->cur.nat_type = desc;
    154 	return 1;
    155 }
    156 
    157 static void
    158 set_type_label(menudesc *m, int opt, void *arg)
    159 {
    160 	struct part_edit_info *info = arg;
    161 	const struct part_type_desc *desc;
    162 
    163 	if (opt == 0) {
    164 		wprintw(m->mw, "%s", msg_string(MSG_Dont_change));
    165 		return;
    166 	}
    167 
    168 	desc = info->parts->pscheme->get_part_type(opt-1);
    169 	if (desc == NULL) {
    170 		wprintw(m->mw, "%s", msg_string(MSG_Other_kind));
    171 		return;
    172 	}
    173 	wprintw(m->mw, "%s", desc->description);
    174 }
    175 
    176 static int
    177 edit_part_type(menudesc *m, void *arg)
    178 {
    179 	struct part_edit_info *info = arg;
    180 	menu_ent *type_opts;
    181 	int type_menu = -1;
    182 	size_t popt_cnt, i;
    183 
    184 	/*
    185 	 * We add one line at the start of the menu, and one at the
    186 	 * bottom, see "set_type_label" above.
    187 	 */
    188 	popt_cnt =  info->parts->pscheme->get_part_types_count() + 2;
    189 	type_opts = calloc(popt_cnt, sizeof(*type_opts));
    190 	for (i = 0; i < popt_cnt; i++) {
    191 		type_opts[i].opt_action = set_part_type;
    192 	}
    193 	type_menu = new_menu(NULL, type_opts, popt_cnt,
    194 		13, 12, 0, 30,
    195 		MC_SUBMENU | MC_SCROLL | MC_NOEXITOPT | MC_NOCLEAR,
    196 		NULL, set_type_label, NULL,
    197 		NULL, NULL);
    198 
    199 	if (type_menu != -1) {
    200 		process_menu(type_menu, arg);
    201 		info->num_changed = true;	/* force reload of menu */
    202 	}
    203 
    204 	free_menu(type_menu);
    205 	free(type_opts);
    206 
    207 	return -1;
    208 }
    209 
    210 static int
    211 edit_part_start(menudesc *m, void *arg)
    212 {
    213 	struct part_edit_info *marg = arg;
    214 	daddr_t max_size;
    215 
    216 	marg->cur.start = getpartoff(marg->parts, marg->cur.start);
    217 	max_size = marg->parts->pscheme->max_free_space_at(marg->parts,
    218 	    marg->cur.start);
    219 	if (marg->cur.size > max_size)
    220 		marg->cur.size = max_size;
    221 
    222 	return 0;
    223 }
    224 
    225 static int
    226 edit_part_size(menudesc *m, void *arg)
    227 {
    228 	struct part_edit_info *marg = arg;
    229 
    230 	marg->cur.size = getpartsize(marg->parts, marg->cur.start,
    231 	    marg->cur.size);
    232 
    233 	return 0;
    234 }
    235 
    236 static int
    237 edit_part_install(menudesc *m, void *arg)
    238 {
    239 	struct part_edit_info *marg = arg;
    240 
    241 	if (pm->ptstart == marg->cur.start) {
    242 		pm->ptstart = 0;
    243 		pm->ptsize = 0;
    244 	} else {
    245 		pm->ptstart = marg->cur.start;
    246 		pm->ptsize = marg->cur.size;
    247 	}
    248 	return 0;
    249 }
    250 
    251 static void
    252 menu_opts_reload(menudesc *m, const struct disk_partitions *parts)
    253 {
    254 	size_t new_num;
    255 
    256 	free(part_menu_opts);
    257 	part_menu_opts = outer_fill_part_menu_opts(parts, &new_num);
    258 	m->opts = part_menu_opts;
    259 	m->numopts = new_num;
    260 }
    261 
    262 static int
    263 delete_part(menudesc *m, void *arg)
    264 {
    265 	struct part_edit_info *marg = arg;
    266 	const char *err_msg = NULL;
    267 
    268 	if (marg->cur_id == NO_PART)
    269 		return 0;
    270 
    271 	if (!marg->parts->pscheme->delete_partition(marg->parts, marg->cur_id,
    272 	    &err_msg))
    273 		err_msg_win(err_msg);
    274 
    275 	marg->num_changed = true;	/* reload list of partitions */
    276 	marg->cancelled = true;		/* do not write back cur data */
    277 
    278 	return 0;
    279 }
    280 
    281 static void draw_outer_ptn_line(menudesc *m, int line, void *arg);
    282 static void draw_outer_ptn_header(menudesc *m, void *arg);
    283 
    284 static int
    285 part_rollback(menudesc *m, void *arg)
    286 {
    287 	struct part_edit_info *marg = arg;
    288 
    289 	marg->cancelled = true;
    290 	return 0;
    291 }
    292 
    293 static menu_ent common_ptn_edit_opts[] = {
    294 #define PTN_OPT_TYPE		0
    295 	{ .opt_action=edit_part_type },
    296 #define PTN_OPT_START		1
    297 	{ .opt_action=edit_part_start },
    298 #define PTN_OPT_SIZE		2
    299 	{ .opt_action=edit_part_size },
    300 #define PTN_OPT_END		3
    301 	{ .opt_flags=OPT_IGNORE }, /* read only "end" */
    302 
    303 	/*
    304 	 * Only the part upto here will be used when adding a new partition
    305 	 */
    306 
    307 #define PTN_OPT_INSTALL		4
    308 	{ .opt_action=edit_part_install },
    309 
    310 #define	PTN_OPTS_COMMON		PTN_OPT_INSTALL	/* cut off from here for add */
    311 };
    312 
    313 static int
    314 edit_custom_opt(menudesc *m, void *arg)
    315 {
    316 	struct part_edit_info *marg = arg;
    317 	size_t attr_no = m->cursel - marg->first_custom_opt;
    318 	char line[STRSIZE];
    319 
    320 	switch (marg->parts->pscheme->custom_attributes[attr_no].type) {
    321 	case pet_bool:
    322 		marg->parts->pscheme->custom_attribute_toggle(
    323 		    marg->parts, marg->cur_id, attr_no);
    324 		break;
    325 	case pet_cardinal:
    326 	case pet_str:
    327 		marg->parts->pscheme->format_custom_attribute(
    328 		    marg->parts, marg->cur_id, attr_no, &marg->cur,
    329 		    line, sizeof(line));
    330 		msg_prompt_win(
    331 		    marg->parts->pscheme->custom_attributes[attr_no].label,
    332 		    -1, 18, 0, 0, line, line, sizeof(line));
    333 		marg->parts->pscheme->custom_attribute_set_str(
    334 		    marg->parts, marg->cur_id, attr_no, line);
    335 		break;
    336 	}
    337 
    338 	return 0;
    339 }
    340 
    341 static menu_ent ptn_edit_opts[] = {
    342 	{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    343 	  .opt_flags=OPT_SUB },
    344 
    345 	{ .opt_name=MSG_Delete_partition,
    346 	  .opt_action = delete_part, .opt_flags = OPT_EXIT },
    347 
    348 	{ .opt_name=MSG_cancel,
    349 	  .opt_action = part_rollback, .opt_flags = OPT_EXIT },
    350 };
    351 
    352 static menu_ent ptn_add_opts[] = {
    353 	{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    354 	  .opt_flags=OPT_SUB },
    355 
    356 	{ .opt_name=MSG_cancel,
    357 	  .opt_action = part_rollback, .opt_flags = OPT_EXIT },
    358 };
    359 
    360 /*
    361  * Concatenate common_ptn_edit_opts, the partitioning scheme specific
    362  * custom options and the given suffix to a single menu options array.
    363  */
    364 static menu_ent *
    365 fill_part_edit_menu_opts(struct disk_partitions *parts,
    366     bool with_custom_attrs,
    367     const menu_ent *suffix, size_t suffix_count, size_t *res_cnt)
    368 {
    369 	size_t i;
    370 	menu_ent *opts, *p;
    371 	size_t count, hdr_cnt;
    372 
    373 	if (with_custom_attrs) {
    374 		hdr_cnt = __arraycount(common_ptn_edit_opts);
    375 		count = hdr_cnt + parts->pscheme->custom_attribute_count
    376 		    + suffix_count;
    377 	} else {
    378 		hdr_cnt = PTN_OPTS_COMMON;
    379 		count = hdr_cnt + suffix_count;
    380 	}
    381 
    382 	opts = calloc(count, sizeof(*opts));
    383 	if (opts == NULL) {
    384 		*res_cnt = 0;
    385 		return NULL;
    386 	}
    387 
    388 	memcpy(opts, common_ptn_edit_opts,
    389 	    sizeof(*opts)*hdr_cnt);
    390 	p = opts + hdr_cnt;
    391 	if (with_custom_attrs) {
    392 		for (i = 0; i < parts->pscheme->custom_attribute_count; i++) {
    393 			p->opt_action = edit_custom_opt;
    394 			p++;
    395 		}
    396 	}
    397 	memcpy(p, suffix, sizeof(*opts)*suffix_count);
    398 
    399 	*res_cnt = count;
    400 	return opts;
    401 }
    402 
    403 static int
    404 edit_part_entry(menudesc *m, void *arg)
    405 {
    406 	struct outer_parts_data *pdata = arg;
    407 	struct part_edit_info data = { .parts = pdata->av.arg,
    408 	    .cur_id = m->cursel,
    409 	    .first_custom_opt = __arraycount(common_ptn_edit_opts) };
    410 	int ptn_menu;
    411 	const char *err;
    412 	menu_ent *opts;
    413 	size_t num_opts;
    414 
    415 	opts = fill_part_edit_menu_opts(data.parts, true, ptn_edit_opts,
    416 	    __arraycount(ptn_edit_opts), &num_opts);
    417 	if (opts == NULL)
    418 		return 1;
    419 
    420 	if (data.cur_id < data.parts->num_part)
    421 		data.parts->pscheme->get_part_info(data.parts, data.cur_id,
    422 		    &data.cur);
    423 
    424 	ptn_menu = new_menu(NULL, opts, num_opts,
    425 		15, 2, 0, 54,
    426 		MC_SUBMENU | MC_SCROLL | MC_NOCLEAR,
    427 		draw_outer_ptn_header, draw_outer_ptn_line, NULL,
    428 		NULL, MSG_Partition_OK);
    429 	if (ptn_menu == -1) {
    430 		free(opts);
    431 		return 1;
    432 	}
    433 
    434 	process_menu(ptn_menu, &data);
    435 	free_menu(ptn_menu);
    436 	free(opts);
    437 
    438 	if (!data.cancelled && data.cur_id < data.parts->num_part)
    439 		if (!data.parts->pscheme->set_part_info(data.parts,
    440 		    data.cur_id, &data.cur, &err))
    441 			err_msg_win(err);
    442 
    443 	if (data.num_changed) {
    444 		menu_opts_reload(m, data.parts);
    445 		m->cursel = data.parts->num_part > 0 ? 0 : 2;
    446 		return -1;
    447 	}
    448 
    449 	return 0;
    450 }
    451 
    452 #ifndef NO_CLONES
    453 static int
    454 add_part_clone(menudesc *menu, void *arg)
    455 {
    456 	struct outer_parts_data *pdata = arg;
    457 	struct disk_partitions *parts = pdata->av.arg;
    458 	struct clone_target_menu_data data;
    459 	menu_ent *men;
    460 	int num_men, i;
    461 	struct disk_part_info sinfo, cinfo;
    462 	struct disk_partitions *csrc;
    463 	struct disk_part_free_space space;
    464 	daddr_t offset, align;
    465 	size_t s, clone_cnt;
    466 	part_id cid;
    467 	struct selected_partitions selected;
    468 	struct single_clone_data *new_clones;
    469 
    470 	if (!select_partitions(&selected, parts))
    471 		return 0;
    472 
    473 	new_clones = realloc(pdata->clones,
    474 	    sizeof(*pdata->clones)*(pdata->num_clone_entries+1));
    475 	if (new_clones == NULL)
    476 		return 0;
    477 	pdata->num_clone_entries++;
    478 	pdata->clones = new_clones;
    479 	new_clones += (pdata->num_clone_entries-1);
    480 	memset(new_clones, 0, sizeof *new_clones);
    481 	new_clones->clone_src = selected;
    482 
    483 	memset(&data, 0, sizeof data);
    484 	data.usage.parts = parts;
    485 
    486 	/* if we already have partitions, ask for the target position */
    487 	if (parts->num_part > 0) {
    488 		data.res = -1;
    489 		num_men = parts->num_part+1;
    490 		men = calloc(num_men, sizeof *men);
    491 		if (men == NULL)
    492 			return 0;
    493 		for (i = 0; i < num_men; i++)
    494 			men[i].opt_action = clone_target_select;
    495 		men[num_men-1].opt_name = MSG_clone_target_end;
    496 
    497 		data.usage.menu = new_menu(MSG_clone_target_hdr,
    498 		    men, num_men, 3, 2, 0, 65, MC_SCROLL,
    499 		    NULL, draw_outer_part_line, NULL, NULL, MSG_cancel);
    500 		process_menu(data.usage.menu, &data);
    501 		free_menu(data.usage.menu);
    502 		free(men);
    503 
    504 		if (data.res < 0)
    505 			goto err;
    506 	} else {
    507 		data.res = 0;
    508 	}
    509 
    510 	/* find selected offset from data.res and insert clones there */
    511 	align = parts->pscheme->get_part_alignment(parts);
    512 	offset = -1;
    513 	if (data.res > 0) {
    514 		for (cid = 0; cid < (size_t)data.res; cid++) {
    515 			if (!parts->pscheme->get_part_info(parts, cid, &sinfo))
    516 			continue;
    517 			offset = sinfo.start + sinfo.size;
    518 		}
    519 	} else {
    520 		offset = 0;
    521 	}
    522 
    523 	new_clones->clone_ids = calloc(selected.num_sel,
    524 	    sizeof(*new_clones->clone_ids));
    525 	if (new_clones->clone_ids == NULL)
    526 		goto err;
    527 	for (s = 0; s < selected.num_sel; s++) {
    528 		csrc = selected.selection[s].parts;
    529 		cid = selected.selection[s].id;
    530 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
    531 		if (!parts->pscheme->adapt_foreign_part_info(
    532 		    parts, &cinfo, csrc->pscheme, &sinfo))
    533 			continue;
    534 		size_t cnt = parts->pscheme->get_free_spaces(
    535 		    parts, &space, 1, cinfo.size-align, align,
    536 		    offset, -1);
    537 		if (cnt == 0)
    538 			continue;
    539 		cinfo.start = space.start;
    540 		cid = parts->pscheme->add_partition(
    541 		    parts, &cinfo, NULL);
    542 		new_clones->clone_ids[s] = cid;
    543 		if (cid == NO_PART)
    544 			continue;
    545 		parts->pscheme->get_part_info(parts, cid, &cinfo);
    546 			clone_cnt++;
    547 		offset = rounddown(cinfo.start+cinfo.size+align, align);
    548 	}
    549 
    550 	/* reload menu and start again */
    551 	menu_opts_reload(menu, parts);
    552 	menu->cursel = parts->num_part+1;
    553 	if (parts->num_part == 0)
    554 		menu->cursel++;
    555 	return -1;
    556 
    557 err:
    558 	free_selected_partitions(&selected);
    559 	return -1;
    560 }
    561 #endif
    562 
    563 static int
    564 add_part_entry(menudesc *m, void *arg)
    565 {
    566 	struct outer_parts_data *pdata = arg;
    567 	struct part_edit_info data = { .parts = pdata->av.arg,
    568 	    .first_custom_opt = PTN_OPTS_COMMON };
    569 	int ptn_menu;
    570 	daddr_t ptn_alignment;
    571 	menu_ent *opts;
    572 	size_t num_opts;
    573 	struct disk_part_free_space space;
    574 	const char *err;
    575 
    576 	opts = fill_part_edit_menu_opts(data.parts, false, ptn_add_opts,
    577 	    __arraycount(ptn_add_opts), &num_opts);
    578 	if (opts == NULL)
    579 		return 1;
    580 
    581 	ptn_alignment = data.parts->pscheme->get_part_alignment(data.parts);
    582 	data.cur_id = NO_PART;
    583 	memset(&data.cur, 0, sizeof(data.cur));
    584 	data.cur.nat_type = data.parts->pscheme->
    585 	    get_generic_part_type(PT_root);
    586 	if (data.parts->pscheme->get_free_spaces(data.parts, &space, 1,
    587 	    max(sizemult, ptn_alignment), ptn_alignment, -1, -1) > 0) {
    588 		data.cur.start = space.start;
    589 		data.cur.size = space.size;
    590 	} else {
    591 		return 0;
    592 	}
    593 
    594 	ptn_menu = new_menu(NULL, opts, num_opts,
    595 		15, -1, 0, 54,
    596 		MC_SUBMENU | MC_SCROLL | MC_NOCLEAR,
    597 		draw_outer_ptn_header, draw_outer_ptn_line, NULL,
    598 		NULL, MSG_Partition_OK);
    599 	if (ptn_menu == -1) {
    600 		free(opts);
    601 		return 1;
    602 	}
    603 
    604 	process_menu(ptn_menu, &data);
    605 	free_menu(ptn_menu);
    606 	free(opts);
    607 
    608 	if (!data.cancelled &&
    609 	    data.parts->pscheme->add_partition(data.parts, &data.cur, &err)
    610 	    == NO_PART)
    611 		err_msg_win(err);
    612 
    613 	menu_opts_reload(m, data.parts);
    614 	m->cursel = data.parts->num_part+1;
    615 	if (data.parts->num_part == 0)
    616 		m->cursel++;
    617 	return -1;
    618 }
    619 
    620 static void
    621 draw_outer_ptn_line(menudesc *m, int line, void *arg)
    622 {
    623 	struct part_edit_info *marg = arg;
    624 	char value[STRSIZE];
    625 	static int col_width;
    626 	static const char *yes, *no, *ptn_type, *ptn_start, *ptn_size,
    627 	    *ptn_end, *ptn_install;
    628 
    629 	if (yes == NULL) {
    630 		int i;
    631 
    632 #define CHECK(str)	i = strlen(str); if (i > col_width) col_width = i;
    633 
    634 		col_width = 0;
    635 		yes = msg_string(MSG_Yes); CHECK(yes);
    636 		no = msg_string(MSG_No); CHECK(no);
    637 		ptn_type = msg_string(MSG_ptn_type); CHECK(ptn_type);
    638 		ptn_start = msg_string(MSG_ptn_start); CHECK(ptn_start);
    639 		ptn_size = msg_string(MSG_ptn_size); CHECK(ptn_size);
    640 		ptn_end = msg_string(MSG_ptn_end); CHECK(ptn_end);
    641 		ptn_install = msg_string(MSG_ptn_install); CHECK(ptn_install);
    642 
    643 #undef CHECK
    644 
    645 		for (size_t n = 0;
    646 		    n < marg->parts->pscheme->custom_attribute_count; n++) {
    647 			i = strlen(msg_string(
    648 			    marg->parts->pscheme->custom_attributes[n].label));
    649 			if (i > col_width)
    650 				col_width = i;
    651 		}
    652 		col_width += 3;
    653 	}
    654 
    655 	if (line >= marg->first_custom_opt) {
    656 		size_t attr_no = line-marg->first_custom_opt;
    657 		marg->parts->pscheme->format_custom_attribute(
    658 		    marg->parts, marg->cur_id, attr_no, &marg->cur,
    659 		    value, sizeof(value));
    660 		wprintw(m->mw, "%*s : %s", col_width,
    661 		    msg_string(
    662 		    marg->parts->pscheme->custom_attributes[attr_no].label),
    663 		    value);
    664 		return;
    665 	}
    666 
    667 	switch (line) {
    668 	case PTN_OPT_TYPE:
    669 		wprintw(m->mw, "%*s : %s", col_width, ptn_type,
    670 		    marg->cur.nat_type != NULL
    671 		    ? marg->cur.nat_type->description
    672 		    : "-");
    673 		break;
    674 	case PTN_OPT_START:
    675 		wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_start,
    676 		    marg->cur.start / (daddr_t)sizemult, multname);
    677 		break;
    678 	case PTN_OPT_SIZE:
    679 		wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_size,
    680 		    marg->cur.size / (daddr_t)sizemult, multname);
    681 		break;
    682 	case PTN_OPT_END:
    683 		wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width, ptn_end,
    684 		    (marg->cur.start + marg->cur.size - 1) / (daddr_t)sizemult,
    685 		    multname);
    686 		break;
    687 	case PTN_OPT_INSTALL:
    688 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
    689 		    (marg->cur.nat_type->generic_ptype == PT_root &&
    690 		    marg->cur.start == pm->ptstart) ? yes : no);
    691 		break;
    692 	}
    693 
    694 }
    695 
    696 static void
    697 draw_outer_ptn_header(menudesc *m, void *arg)
    698 {
    699 	struct part_edit_info *marg = arg;
    700 	size_t attr_no;
    701 	bool may_change_type;
    702 
    703 #define DISABLE(opt,cond) \
    704 	if (cond) \
    705 		m->opts[opt].opt_flags |= OPT_IGNORE; \
    706 	else \
    707 		m->opts[opt].opt_flags &= ~OPT_IGNORE;
    708 
    709 	/* e.g. MBR extended partitions can only change if empty */
    710 	may_change_type = marg->cur_id == NO_PART
    711 	     || marg->parts->pscheme->part_type_can_change == NULL
    712 	     || marg->parts->pscheme->part_type_can_change(
    713 		    marg->parts, marg->cur_id);
    714 
    715 	DISABLE(PTN_OPT_TYPE, !may_change_type);
    716 	if (!may_change_type && m->cursel == PTN_OPT_TYPE)
    717 		m->cursel++;
    718 	if (marg->cur_id != NO_PART) {
    719 		for (int i = 0; i < m->numopts; i++) {
    720 			if (m->opts[i].opt_action == delete_part) {
    721 				DISABLE(i, !may_change_type);
    722 			}
    723 		}
    724 	}
    725 
    726 	/* Can only install into NetBSD partition */
    727 	if (marg->cur_id != NO_PART) {
    728 		DISABLE(PTN_OPT_INSTALL, marg->cur.nat_type == NULL
    729 		    || marg->cur.nat_type->generic_ptype != PT_root);
    730 	}
    731 
    732 	if (marg->cur_id == NO_PART)
    733 		return;
    734 
    735 	for (attr_no = 0; attr_no <
    736 	    marg->parts->pscheme->custom_attribute_count; attr_no++) {
    737 		bool writable =
    738 		    marg->parts->pscheme->custom_attribute_writable(
    739 			marg->parts, marg->cur_id, attr_no);
    740 		DISABLE(attr_no+marg->first_custom_opt, !writable);
    741 	}
    742 }
    743 
    744 static void
    745 draw_outer_part_line(menudesc *m, int opt, void *arg)
    746 {
    747 	struct outer_parts_data *pdata = arg;
    748 	struct disk_partitions *parts = pdata->av.arg;
    749 	int len;
    750 	part_id pno = opt;
    751 	struct disk_part_info info;
    752 	char buf[SSTRSIZE], *astr, colval[STRSIZE], line[STRSIZE];
    753 	size_t astr_avail, x;
    754 	static char install_flag = 0;
    755 
    756 #define PART_ROW_USED_FMT	"%13" PRIu64 " %13" PRIu64 " %-4s"
    757 
    758 	len = snprintf(0, 0, PART_ROW_USED_FMT, (daddr_t)0, (daddr_t)0, "");
    759 
    760 	if (pno >= parts->num_part ||
    761 	    !parts->pscheme->get_part_info(parts, pno, &info)) {
    762 		wprintw(m->mw, "%*s", len, "");
    763 		// XXX
    764 		return;
    765 	}
    766 
    767 	if (info.start == pm->ptstart &&
    768 	    info.nat_type->generic_ptype == PT_root) {
    769 		if (install_flag == 0)
    770 			install_flag = msg_string(MSG_install_flag)[0];
    771 		astr_avail = sizeof(buf)-1;
    772 		buf[0] = install_flag;
    773 		buf[1] = 0;
    774 		astr = buf+1;
    775 	} else {
    776 		buf[0] = 0;
    777 		astr = buf;
    778 		astr_avail = sizeof(buf);
    779 	}
    780 	if (parts->pscheme->get_part_attr_str != NULL)
    781 		parts->pscheme->get_part_attr_str(parts, pno, astr,
    782 		    astr_avail);
    783 
    784 	daddr_t start = info.start / sizemult;
    785 	daddr_t size = info.size / sizemult;
    786 	wprintw(m->mw, PART_ROW_USED_FMT,
    787 	    start, size, buf);
    788 
    789 	line[0] = 0; x = 0;
    790 	for (size_t col = 0; col < parts->pscheme->edit_columns_count; col++) {
    791 		if (parts->pscheme->format_partition_table_str(parts, pno,
    792 		    col, colval, sizeof(colval)) && colval[0] != 0
    793 		    && x < sizeof(line)-2) {
    794 			for (size_t i = strlen(line); i < x; i++)
    795 				line[i] = ' ';
    796 			line[x] = ' ';
    797 			strlcpy(line+x+1, colval, sizeof(line)-x-1);
    798 		}
    799 		x += parts->pscheme->edit_columns[col].width + 1;
    800 	}
    801 	wprintw(m->mw, "%s", line);
    802 }
    803 
    804 static int
    805 part_edit_abort(menudesc *m, void *arg)
    806 {
    807 	struct outer_parts_data *pdata = arg;
    808 
    809 	pdata->av.rv = -1;
    810 	return 0;
    811 }
    812 
    813 static menu_ent *
    814 outer_fill_part_menu_opts(const struct disk_partitions *parts, size_t *cnt)
    815 {
    816 	menu_ent *opts, *op;
    817 	size_t num_opts;
    818 	size_t i;
    819 	bool may_add;
    820 
    821 	may_add = parts->pscheme->can_add_partition(parts);
    822 	num_opts = 3 + parts->num_part;
    823 #ifndef NO_CLONES
    824 	num_opts++;
    825 #endif
    826 	if (parts->num_part == 0)
    827 		num_opts++;
    828 	if (may_add)
    829 		num_opts++;
    830 	opts = calloc(num_opts, sizeof *opts);
    831 	if (opts == NULL) {
    832 		*cnt = 0;
    833 		return NULL;
    834 	}
    835 
    836 	/* add all exisiting partitions */
    837 	for (op = opts, i = 0; i < parts->num_part && i < (num_opts-2);
    838 	    op++, i++) {
    839 		op->opt_flags = OPT_SUB;
    840 		op->opt_action = edit_part_entry;
    841 	}
    842 
    843 	/* if empty, hint that partitions are missing */
    844 	if (parts->num_part == 0) {
    845 		op->opt_name = MSG_nopart;
    846 		op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
    847 		op++;
    848 	}
    849 
    850 	/* separator line between partitions and actions */
    851 	op->opt_name = outer_part_sep_line;
    852 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
    853 	op++;
    854 
    855 	/* followed by new partition adder */
    856 	if (may_add) {
    857 		op->opt_name = MSG_addpart;
    858 		op->opt_flags = OPT_SUB;
    859 		op->opt_action = add_part_entry;
    860 		op++;
    861 	}
    862 
    863 #ifndef NO_CLONES
    864 	/* and a partition cloner */
    865 	op->opt_name = MSG_clone_from_elsewhere;
    866 	op->opt_action = add_part_clone;
    867 	op++;
    868 #endif
    869 
    870 	/* and unit changer */
    871 	op->opt_name = MSG_askunits;
    872 	op->opt_menu = MENU_sizechoice;
    873 	op->opt_flags = OPT_SUB;
    874 	op->opt_action = NULL;
    875 	op++;
    876 
    877 	/* and abort option */
    878 	op->opt_name = MSG_cancel;
    879 	op->opt_flags = OPT_EXIT;
    880 	op->opt_action = part_edit_abort;
    881 	op++;
    882 
    883 	/* counts are consistent? */
    884 	assert((op - opts) >= 0 && (size_t)(op - opts) == num_opts);
    885 
    886 	*cnt = num_opts;
    887 	return opts;
    888 }
    889 
    890 static void
    891 draw_outer_part_header(menudesc *m, void *arg)
    892 {
    893 	struct outer_parts_data *pdata = arg;
    894 	struct disk_partitions *parts = pdata->av.arg;
    895 	char start[SSTRSIZE], size[SSTRSIZE], col[SSTRSIZE],
    896 	    *disk_info, total[SSTRSIZE], avail[SSTRSIZE];
    897 	size_t sep;
    898 	const char *args[3];
    899 
    900 	msg_display_subst(MSG_editparttable, 4,
    901 	    parts->disk,
    902 	    msg_string(parts->pscheme->name),
    903 	    msg_string(parts->pscheme->short_name),
    904 	    parts->pscheme->part_flag_desc ?
    905 		msg_string(parts->pscheme->part_flag_desc)
    906 		: "");
    907 
    908 	snprintf(total, sizeof(total), "%" PRIu64 " %s",
    909 	    parts->disk_size / sizemult, multname);
    910 	snprintf(avail, sizeof(total), "%" PRIu64 " %s",
    911 	    parts->free_space / sizemult, multname);
    912 	args[0] = parts->disk;
    913 	args[1] = total;
    914 	args[2] = avail;
    915 	disk_info = str_arg_subst(msg_string(MSG_part_header), 3, args);
    916 	msg_table_add(disk_info);
    917 	free(disk_info);
    918 
    919 
    920 	strcpy(outer_part_sep_line, "------------- ------------- ----");
    921 	sep = strlen(outer_part_sep_line);
    922 	snprintf(start, sizeof(start), "%s(%s)",
    923 	    msg_string(MSG_part_header_col_start), multname);
    924 	snprintf(size, sizeof(size), "%s(%s)",
    925 	    msg_string(MSG_part_header_col_size), multname);
    926 	snprintf(outer_part_title, sizeof(outer_part_title),
    927 	    "   %13s %13s %-4s", start, size,
    928 	    msg_string(MSG_part_header_col_flag));
    929 
    930 	for (size_t i = 0; i < parts->pscheme->edit_columns_count; i++) {
    931 		char *np = outer_part_sep_line+sep;
    932 		unsigned int w = parts->pscheme->edit_columns[i].width;
    933 		snprintf(col, sizeof(col), " %*s", -w,
    934 		    msg_string(parts->pscheme->edit_columns[i].title));
    935 		strlcat(outer_part_title, col, sizeof(outer_part_title));
    936 		if (sep < sizeof(outer_part_sep_line)-1) {
    937 			*np++ = ' ';
    938 			sep++;
    939 		}
    940 		for (unsigned int p = 0; p < w &&
    941 		    sep < sizeof(outer_part_sep_line)-1; p++)
    942 			*np++ = '-', sep++;
    943 		*np = 0;
    944 	}
    945 
    946 	strlcat(outer_part_title, "\n   ", sizeof(outer_part_title));
    947 	strlcat(outer_part_title, outer_part_sep_line,
    948 	    sizeof(outer_part_title));
    949 
    950 	msg_table_add("\n\n");
    951 }
    952 
    953 /*
    954  * Use the whole disk for NetBSD, but (if any) create required helper
    955  * partitions (usually for booting and stuff).
    956  */
    957 bool
    958 parts_use_wholedisk(struct disk_partitions *parts,
    959     size_t add_ext_parts, const struct disk_part_info *ext_parts)
    960 {
    961 	part_id nbsd;
    962 	struct disk_part_info info;
    963 	struct disk_part_free_space space;
    964 	daddr_t align;
    965 	size_t i;
    966 
    967 	parts->pscheme->delete_all_partitions(parts);
    968 	align = parts->pscheme->get_part_alignment(parts);
    969 
    970 	if (ext_parts != NULL) {
    971 		for (i = 0; i < add_ext_parts; i++) {
    972 			info = ext_parts[i];
    973 			if (parts->pscheme->get_free_spaces(parts, &space,
    974 			    1, info.size, align, -1, -1) != 1)
    975 				return false;
    976 			info.start = space.start;
    977 			if (parts->pscheme->add_partition(parts, &info, NULL)
    978 			    == NO_PART)
    979 				return false;
    980 		}
    981 	}
    982 
    983 	if (parts->pscheme->get_free_spaces(parts, &space, 1, 3*align,
    984 	    align, -1, -1) != 1)
    985 		return false;
    986 
    987 	memset(&info, 0, sizeof(info));
    988 	info.start = space.start;
    989 	info.size = space.size;
    990 	info.nat_type = parts->pscheme->get_generic_part_type(PT_root);
    991 	nbsd = parts->pscheme->add_partition(parts, &info, NULL);
    992 
    993 	if (nbsd == NO_PART)
    994 		return false;
    995 
    996 	if (!parts->pscheme->get_part_info(parts, nbsd, &info))
    997 		return false;
    998 
    999 	if (parts->pscheme->secondary_scheme != NULL) {
   1000 		/* force empty secondary partitions */
   1001 		parts->pscheme->secondary_partitions(parts, info.start, true);
   1002 	}
   1003 
   1004 	pm->ptstart = info.start;
   1005 	pm->ptsize = info.size;
   1006 	return true;
   1007 }
   1008 
   1009 static int
   1010 set_keep_existing(menudesc *m, void *arg)
   1011 {
   1012 	((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
   1013 	return 0;
   1014 }
   1015 
   1016 static int
   1017 set_use_only_part(menudesc *m, void *arg)
   1018 {
   1019 	((arg_rep_int*)arg)->rv = LY_SETSIZES;
   1020 	return 0;
   1021 }
   1022 
   1023 static int
   1024 set_use_entire_disk(menudesc *m, void *arg)
   1025 {
   1026 	((arg_rep_int*)arg)->rv = LY_USEFULL;
   1027 	return 0;
   1028 }
   1029 
   1030 static enum layout_type
   1031 ask_fullpart(struct disk_partitions *parts)
   1032 {
   1033 	arg_rep_int ai;
   1034 	const char *args[2];
   1035 	int menu;
   1036 	size_t num_opts;
   1037 	menu_ent options[3], *opt;
   1038 	daddr_t start, size;
   1039 
   1040 	args[0] = msg_string(pm->parts->pscheme->name);
   1041 	args[1] = msg_string(pm->parts->pscheme->short_name);
   1042 	ai.args.argv = args;
   1043 	ai.args.argc = 2;
   1044 	ai.rv = LY_SETSIZES;
   1045 
   1046 	memset(options, 0, sizeof(options));
   1047 	num_opts = 0;
   1048 	opt = &options[0];
   1049 	if (parts->pscheme->guess_install_target != NULL &&
   1050 	    parts->pscheme->guess_install_target(parts, &start, &size)) {
   1051 		opt->opt_name = MSG_Keep_existing_partitions;
   1052 		opt->opt_flags = OPT_EXIT;
   1053 		opt->opt_action = set_keep_existing;
   1054 		opt++;
   1055 		num_opts++;
   1056 	}
   1057 	opt->opt_name = MSG_Use_only_part_of_the_disk;
   1058 	opt->opt_flags = OPT_EXIT;
   1059 	opt->opt_action = set_use_only_part;
   1060 	opt++;
   1061 	num_opts++;
   1062 
   1063 	opt->opt_name = MSG_Use_the_entire_disk;
   1064 	opt->opt_flags = OPT_EXIT;
   1065 	opt->opt_action = set_use_entire_disk;
   1066 	opt++;
   1067 	num_opts++;
   1068 
   1069 	menu = new_menu(MSG_Select_your_choice, options, num_opts,
   1070 	    -1, -10, 0, 0, MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
   1071 	if (menu != -1) {
   1072 		get_menudesc(menu)->expand_act = expand_all_option_texts;
   1073 		process_menu(menu, &ai);
   1074 		free_menu(menu);
   1075 	}
   1076 
   1077 	return ai.rv;
   1078 }
   1079 
   1080 /*
   1081  * return (see post_edit_verify):
   1082  *  0 -> abort
   1083  *  1 -> re-edit
   1084  *  2 -> continue installation
   1085  */
   1086 static int
   1087 verify_outer_parts(struct disk_partitions *parts, bool quiet)
   1088 {
   1089 	part_id i;
   1090 	int num_bsdparts;
   1091 	daddr_t first_bsdstart, first_bsdsize, inst_start, inst_size;
   1092 
   1093 	first_bsdstart = first_bsdsize = 0;
   1094 	inst_start = inst_size = 0;
   1095 	num_bsdparts = 0;
   1096 	for (i = 0; i < parts->num_part; i++) {
   1097 		struct disk_part_info info;
   1098 		if (!parts->pscheme->get_part_info(parts, i, &info))
   1099 			continue;
   1100 		if (!(info.flags & PTI_SEC_CONTAINER))
   1101 			continue;
   1102 		if (info.nat_type->generic_ptype != PT_root)
   1103 			continue;
   1104 		num_bsdparts++;
   1105 
   1106 		if (first_bsdstart == 0) {
   1107 			first_bsdstart = info.start;
   1108 			first_bsdsize = info.size;
   1109 		}
   1110 		if (inst_start == 0 && info.start == pm->ptstart) {
   1111 			inst_start = info.start;
   1112 			inst_size = info.size;
   1113 		}
   1114 	}
   1115 
   1116 	if (num_bsdparts == 0 ||
   1117 	    (num_bsdparts > 1 && inst_start == 0)) {
   1118 		if (quiet && num_bsdparts == 0)
   1119 			return 0;
   1120 		if (quiet && parts->pscheme->guess_install_target &&
   1121 		    parts->pscheme->guess_install_target(parts,
   1122 		    &inst_start, &inst_size)) {
   1123 			pm->ptstart = inst_start;
   1124 			pm->ptsize = inst_size;
   1125 		} else {
   1126 			if (num_bsdparts == 0)
   1127 				msg_display_subst(MSG_nobsdpart, 2,
   1128 				    msg_string(parts->pscheme->name),
   1129 				    msg_string(parts->pscheme->short_name));
   1130 			else
   1131 				msg_display_subst(MSG_multbsdpart, 2,
   1132 				    msg_string(parts->pscheme->name),
   1133 				    msg_string(parts->pscheme->short_name));
   1134 
   1135 			return ask_reedit(parts);
   1136 		}
   1137 	}
   1138 
   1139 	if (pm->ptstart == 0) {
   1140 		if (inst_start > 0) {
   1141 			pm->ptstart = inst_start;
   1142 			pm->ptsize = inst_size;
   1143 		} else if (first_bsdstart > 0) {
   1144 			pm->ptstart = first_bsdstart;
   1145 			pm->ptsize = first_bsdsize;
   1146 		} else if (parts->pscheme->guess_install_target &&
   1147 			   parts->pscheme->guess_install_target(
   1148 			   parts, &inst_start, &inst_size)) {
   1149 			pm->ptstart = inst_start;
   1150 			pm->ptsize = inst_size;
   1151 		}
   1152 	}
   1153 
   1154 	/*
   1155 	 * post_edit_verify returns:
   1156 	 *  0 -> abort
   1157 	 *  1 -> re-edit
   1158 	 *  2 -> continue installation
   1159 	 */
   1160 	if (parts->pscheme->post_edit_verify)
   1161 		return parts->pscheme->post_edit_verify(parts, quiet);
   1162 
   1163 	return 2;
   1164 }
   1165 
   1166 static bool
   1167 ask_outer_partsizes(struct disk_partitions *parts)
   1168 {
   1169 	int j;
   1170 	int part_menu;
   1171 	size_t num_opts;
   1172 #ifndef NO_CLONES
   1173 	size_t i, ci;
   1174 #endif
   1175 	struct outer_parts_data data;
   1176 
   1177 	part_menu_opts = outer_fill_part_menu_opts(parts, &num_opts);
   1178 	part_menu = new_menu(outer_part_title, part_menu_opts, num_opts,
   1179 			0, -1, 15, 70,
   1180 			MC_NOBOX|MC_ALWAYS_SCROLL|MC_NOCLEAR|MC_CONTINUOUS,
   1181 			draw_outer_part_header, draw_outer_part_line, NULL,
   1182 			NULL, MSG_Partition_table_ok);
   1183 	if (part_menu == -1) {
   1184 		free(part_menu_opts);
   1185 		return false;
   1186 	}
   1187 
   1188 	/* Default to MB, and use bios geometry for cylinder size */
   1189 	set_default_sizemult(MEG/512);
   1190 	if (pm->current_cylsize == 0)
   1191 		pm->current_cylsize = 16065;	/* noone cares nowadays */
   1192 	pm->ptstart = 0;
   1193 	pm->ptsize = 0;
   1194 	memset(&data, 0, sizeof data);
   1195 	data.av.arg = parts;
   1196 
   1197 	for (;;) {
   1198 		data.av.rv = 0;
   1199 		process_menu(part_menu, &data);
   1200 		if (data.av.rv < 0)
   1201 			break;
   1202 
   1203 		j = verify_outer_parts(parts, false);
   1204 		if (j == 0) {
   1205 			data.av.rv = -1;
   1206 			return false;
   1207 		} else if (j == 1) {
   1208 			continue;
   1209 		}
   1210 		break;
   1211 	}
   1212 
   1213 #ifndef NO_CLONES
   1214 	/* handle cloned partitions content copies now */
   1215 	for (i = 0; i < data.num_clone_entries; i++) {
   1216 		for (ci = 0; ci < data.clones[i].clone_src.num_sel; ci++) {
   1217 			if (data.clones[i].clone_src.with_data)
   1218 				clone_partition_data(parts,
   1219 				    data.clones[i].clone_ids[ci],
   1220 				    data.clones[i].clone_src.selection[ci].
   1221 				    parts,
   1222 				    data.clones[i].clone_src.selection[ci].id);
   1223 		}
   1224 	}
   1225 
   1226 	/* free clone data */
   1227 	if (data.clones) {
   1228 		for (i = 0; i < data.num_clone_entries; i++)
   1229 			free_selected_partitions(&data.clones[i].clone_src);
   1230 		free(data.clones);
   1231 	}
   1232 #endif
   1233 
   1234 	free_menu(part_menu);
   1235 	free(part_menu_opts);
   1236 
   1237 	return data.av.rv == 0;
   1238 }
   1239 
   1240 bool
   1241 edit_outer_parts(struct disk_partitions *parts)
   1242 {
   1243 	part_id i;
   1244 	enum layout_type layout;
   1245 	int num_foreign_parts;
   1246 
   1247 	/* If targeting a wedge, do not ask for further partitioning */
   1248 	if (pm && (pm->no_part || pm->no_mbr))
   1249 		return true;
   1250 
   1251 	/* Make sure parts has been properly initialized */
   1252 	assert(parts && parts->pscheme);
   1253 
   1254 	if (parts->pscheme->secondary_scheme == NULL)
   1255 		return true;	/* no outer parts */
   1256 
   1257 	if (partman_go) {
   1258 		layout = LY_SETSIZES;
   1259 	} else {
   1260 		/* Ask full/part */
   1261 		const struct disk_partitioning_scheme *sec =
   1262 		    parts->pscheme->secondary_scheme;
   1263 
   1264 		uint64_t m_size =
   1265 		    DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB;
   1266 		char min_size[5], build_size[5];
   1267 		const char
   1268 		    *prim_name = msg_string(parts->pscheme->name),
   1269 		    *prim_short = msg_string(parts->pscheme->short_name),
   1270 		    *sec_name = msg_string(sec->name),
   1271 		    *sec_short = msg_string(sec->short_name);
   1272 
   1273 		humanize_number(min_size, sizeof(min_size),
   1274 		    m_size * MEG,
   1275 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1276 		humanize_number(build_size, sizeof(build_size),
   1277 		     SYSTEM_BUILD_SIZE * MEG, "", HN_AUTOSCALE,
   1278 		     HN_B | HN_NOSPACE | HN_DECIMAL);
   1279 
   1280 		msg_display_subst(MSG_fullpart, 7,
   1281 		    pm->diskdev,
   1282 		    prim_name, sec_name,
   1283 		    prim_short, sec_short,
   1284 		    min_size, build_size);
   1285 		msg_display_add("\n\n");
   1286 
   1287 		layout = ask_fullpart(parts);
   1288 	}
   1289 
   1290 	if (layout == LY_USEFULL) {
   1291 		struct disk_part_info info;
   1292 
   1293 		/* Count nonempty, non-BSD partitions. */
   1294 		num_foreign_parts = 0;
   1295 		for (i = 0; i < parts->num_part; i++) {
   1296 			if (!parts->pscheme->get_part_info(parts, i, &info))
   1297 				continue;
   1298 			if (info.size == 0)
   1299 				continue;
   1300 			if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1301 				continue;
   1302 			if (info.nat_type != NULL
   1303 			    && info.nat_type->generic_ptype != PT_root
   1304 			    && info.nat_type->generic_ptype != PT_swap)
   1305 				num_foreign_parts++;
   1306 		}
   1307 
   1308 		/* Ask if we really want to blow away non-NetBSD stuff */
   1309 		if (num_foreign_parts > 0) {
   1310 			msg_display(MSG_ovrwrite);
   1311 			if (!ask_noyes(NULL)) {
   1312 				if (logfp)
   1313 					(void)fprintf(logfp,
   1314 					    "User answered no to destroy "
   1315 					    "other data, aborting.\n");
   1316 				return false;
   1317 			}
   1318 		}
   1319 		if (!md_parts_use_wholedisk(parts)) {
   1320 			hit_enter_to_continue(MSG_No_free_space, NULL);
   1321 			return false;
   1322 		}
   1323 		if (parts->pscheme->post_edit_verify) {
   1324 			return
   1325 			    parts->pscheme->post_edit_verify(parts, true) == 2;
   1326 		}
   1327 		return true;
   1328 	} else if (layout == LY_SETSIZES) {
   1329 		return ask_outer_partsizes(parts);
   1330 	} else {
   1331 		return verify_outer_parts(parts, true) == 2;
   1332 	}
   1333 }
   1334 
   1335 static int
   1336 set_part_scheme(menudesc *m, void *arg)
   1337 {
   1338 	size_t *res = arg;
   1339 
   1340 	*res = (size_t)m->cursel;
   1341 	return 1;
   1342 }
   1343 
   1344 const struct disk_partitioning_scheme *
   1345 select_part_scheme(
   1346 	struct pm_devs *dev,
   1347 	const struct disk_partitioning_scheme *skip,
   1348 	bool bootable,
   1349 	const char *hdr)
   1350 {
   1351 	int ps_menu = -1;
   1352 	menu_ent *opt;
   1353 	char **str, *ms = NULL;
   1354 	const struct disk_partitioning_scheme **options, *res;
   1355 	const char *title;
   1356 	size_t ndx, selected = ~0U, used;
   1357 	const struct disk_partitioning_scheme *p;
   1358 	bool showing_limit = false;
   1359 
   1360 	if (hdr == NULL)
   1361 		hdr = MSG_select_part_scheme;
   1362 
   1363 	opt = calloc(num_available_part_schemes, sizeof *opt);
   1364 	if (!opt)
   1365 		return NULL;
   1366 	str = calloc(num_available_part_schemes, sizeof *str);
   1367 	if (!str) {
   1368 		free(opt);
   1369 		return NULL;
   1370 	}
   1371 	options = calloc(num_available_part_schemes, sizeof *options);
   1372 	if (!options) {
   1373 		free(str);
   1374 		free(opt);
   1375 		return NULL;
   1376 	}
   1377 
   1378 	for (used = 0, ndx = 0; ndx < num_available_part_schemes; ndx++) {
   1379 		p = available_part_schemes[ndx];
   1380 		if (skip != NULL && p == skip)
   1381 			continue;
   1382 		if (bootable && p->have_boot_support != NULL &&
   1383 		    !p->have_boot_support(dev->diskdev))
   1384 			continue;
   1385 #ifdef HAVE_MBR
   1386 		if (dev->no_mbr && p->name == MSG_parttype_mbr)
   1387 			continue;
   1388 #endif
   1389 		if (p->size_limit && dev->dlsize > p->size_limit) {
   1390 			char buf[255], hum_lim[5];
   1391 
   1392 			humanize_number(hum_lim, sizeof(hum_lim),
   1393 			    (uint64_t)p->size_limit*512UL,
   1394 			    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1395 			sprintf(buf, "%s [%s %s]", msg_string(p->name),
   1396 			    msg_string(MSG_size_limit), hum_lim);
   1397 			str[used] = strdup(buf);
   1398 			showing_limit = true;
   1399 		} else {
   1400 			str[used] = strdup(msg_string(p->name));
   1401 		}
   1402 		if (!str[used])
   1403 			goto out;
   1404 
   1405 		opt[used].opt_name = str[used];
   1406 		opt[used].opt_action = set_part_scheme;
   1407 		options[used] = p;
   1408 		used++;
   1409 	}
   1410 
   1411 	/* do not bother to ask if there are no options */
   1412 	if (used <= 1) {
   1413 		selected = (used == 1) ? 0 : ~0U;
   1414 		goto out;
   1415 	}
   1416 
   1417 	if (showing_limit) {
   1418 		char hum_lim[5], *tmp;
   1419 		size_t total;
   1420 
   1421 		const char *p1 = msg_string(hdr);
   1422 		const char *p2 = msg_string(MSG_select_part_limit);
   1423 
   1424 		humanize_number(hum_lim, sizeof(hum_lim),
   1425 		    (uint64_t)dev->dlsize*512, "",
   1426 		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1427 
   1428 		const char *args[] = { dev->diskdev, hum_lim };
   1429 		char *p3 = str_arg_subst(msg_string(MSG_part_limit_disksize),
   1430 		    __arraycount(args), args);
   1431 
   1432 		total = strlen(p1) + strlen(p2) + strlen(p3)
   1433 		    + sizeof(hum_lim) + 5;
   1434 		ms = tmp = malloc(total);
   1435 		title = tmp;
   1436 		strcpy(tmp, p1); tmp += strlen(p1);
   1437 		*tmp++ = '\n'; *tmp++ = '\n';
   1438 		strcpy(tmp, p2); tmp += strlen(p2);
   1439 		*tmp++ = '\n'; *tmp++ = '\n';
   1440 		strcpy(tmp, p3);
   1441 		free(p3);
   1442 		assert(strlen(ms) < total);
   1443 	} else {
   1444 		title = msg_string(hdr);
   1445 	}
   1446 	ps_menu = new_menu(title, opt, used,
   1447 	    5, 5, 0, 0, 0, NULL, NULL, NULL, NULL, MSG_exit_menu_generic);
   1448 	if (ps_menu != -1)
   1449 		process_menu(ps_menu, &selected);
   1450 out:
   1451 	res = selected >= used ? NULL : options[selected];
   1452 	for (ndx = 0; ndx < used; ndx++)
   1453 		free(str[ndx]);
   1454 	if (showing_limit && ms)
   1455 		free(ms);
   1456 	free(str);
   1457 	free(opt);
   1458 	free(options);
   1459 	if (ps_menu != -1)
   1460 		free_menu(ps_menu);
   1461 
   1462 	return res;
   1463 }
   1464