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