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