xref: /netbsd/usr.sbin/sysinst/label.c (revision c415ce7e)
1*c415ce7eSmartin /*	$NetBSD: label.c,v 1.48 2023/01/06 18:19:27 martin Exp $	*/
226165e63Sdholland 
326165e63Sdholland /*
426165e63Sdholland  * Copyright 1997 Jonathan Stone
526165e63Sdholland  * All rights reserved.
626165e63Sdholland  *
726165e63Sdholland  * Redistribution and use in source and binary forms, with or without
826165e63Sdholland  * modification, are permitted provided that the following conditions
926165e63Sdholland  * are met:
1026165e63Sdholland  * 1. Redistributions of source code must retain the above copyright
1126165e63Sdholland  *    notice, this list of conditions and the following disclaimer.
1226165e63Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
1326165e63Sdholland  *    notice, this list of conditions and the following disclaimer in the
1426165e63Sdholland  *    documentation and/or other materials provided with the distribution.
1526165e63Sdholland  * 3. All advertising materials mentioning features or use of this software
1626165e63Sdholland  *    must display the following acknowledgement:
1726165e63Sdholland  *      This product includes software developed for the NetBSD Project by
1826165e63Sdholland  *      Jonathan Stone.
1926165e63Sdholland  * 4. The name of Jonathan Stone may not be used to endorse
2026165e63Sdholland  *    or promote products derived from this software without specific prior
2126165e63Sdholland  *    written permission.
2226165e63Sdholland  *
2326165e63Sdholland  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
2426165e63Sdholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2526165e63Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2626165e63Sdholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2726165e63Sdholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2826165e63Sdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2926165e63Sdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3026165e63Sdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3126165e63Sdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3226165e63Sdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3326165e63Sdholland  * THE POSSIBILITY OF SUCH DAMAGE.
3426165e63Sdholland  *
3526165e63Sdholland  */
3626165e63Sdholland 
3726165e63Sdholland #include <sys/cdefs.h>
3826165e63Sdholland #if defined(LIBC_SCCS) && !defined(lint)
39*c415ce7eSmartin __RCSID("$NetBSD: label.c,v 1.48 2023/01/06 18:19:27 martin Exp $");
4026165e63Sdholland #endif
4126165e63Sdholland 
4226165e63Sdholland #include <sys/types.h>
4326165e63Sdholland #include <stddef.h>
44dcc57e24Smartin #include <assert.h>
4526165e63Sdholland #include <errno.h>
4626165e63Sdholland #include <stdio.h>
4726165e63Sdholland #include <fcntl.h>
4826165e63Sdholland #include <util.h>
4926165e63Sdholland #include <unistd.h>
5026165e63Sdholland #include <sys/dkio.h>
5126165e63Sdholland #include <sys/param.h>
52dcc57e24Smartin #include <sys/bootblock.h>
53b162b00bSmartin #include <sys/bitops.h>
5426165e63Sdholland #include <ufs/ffs/fs.h>
5526165e63Sdholland 
5626165e63Sdholland #include "defs.h"
5726165e63Sdholland #include "msg_defs.h"
5826165e63Sdholland #include "menu_defs.h"
5926165e63Sdholland 
6026165e63Sdholland /*
6126165e63Sdholland  * local prototypes
6226165e63Sdholland  */
63dcc57e24Smartin static bool boringpart(const struct disk_part_info *info);
642f0b36a1Smartin static bool checklabel(struct disk_partitions*, char *, char *);
65dcc57e24Smartin static void show_partition_adder(menudesc *, struct partition_usage_set*);
6626165e63Sdholland 
6726165e63Sdholland /*
68dcc57e24Smartin  * Return 1 if a partition should be ignored when checking
6926165e63Sdholland  * for overlapping partitions.
7026165e63Sdholland  */
71dcc57e24Smartin static bool
boringpart(const struct disk_part_info * info)72dcc57e24Smartin boringpart(const struct disk_part_info *info)
7326165e63Sdholland {
7426165e63Sdholland 
75dcc57e24Smartin 	if (info->size == 0)
76dcc57e24Smartin 		return true;
77*c415ce7eSmartin 	if (info->flags & PTI_SPECIAL_PARTS)
78dcc57e24Smartin 		return true;
79dcc57e24Smartin 
80dcc57e24Smartin 	return false;
8126165e63Sdholland }
8226165e63Sdholland 
83dcc57e24Smartin /*
84dcc57e24Smartin  * We have some partitions in our "wanted" list that we may not edit,
85dcc57e24Smartin  * like the RAW_PART in disklabel, some that just represent external
86dcc57e24Smartin  * mount entries for the final fstab or similar.
87dcc57e24Smartin  * We have previously sorted pset->parts and pset->infos to be in sync,
88dcc57e24Smartin  * but the former "array" may be shorter.
89dcc57e24Smartin  * Here are a few quick predicates to check for them.
90dcc57e24Smartin  */
91dcc57e24Smartin static bool
real_partition(const struct partition_usage_set * pset,int index)92dcc57e24Smartin real_partition(const struct partition_usage_set *pset, int index)
93dcc57e24Smartin {
94dcc57e24Smartin 	if (index < 0 || (size_t)index >= pset->num)
95dcc57e24Smartin 		return false;
9626165e63Sdholland 
97dcc57e24Smartin 	return pset->infos[index].cur_part_id != NO_PART;
98dcc57e24Smartin }
9926165e63Sdholland 
10026165e63Sdholland /*
101dcc57e24Smartin  * Check partitioning for overlapping partitions.
10256420644Smartin  * Returns true if no overlapping partition found.
10326165e63Sdholland  * Sets reference arguments ovly1 and ovly2 to the indices of
10426165e63Sdholland  * overlapping partitions if any are found.
10526165e63Sdholland  */
106dcc57e24Smartin static bool
checklabel(struct disk_partitions * parts,char * ovl1,char * ovl2)107dcc57e24Smartin checklabel(struct disk_partitions *parts,
1082f0b36a1Smartin     char *ovl1, char *ovl2)
10926165e63Sdholland {
110dcc57e24Smartin 	part_id i, j;
111dcc57e24Smartin 	struct disk_part_info info;
112dcc57e24Smartin 	daddr_t istart, iend, jstart, jend;
113dcc57e24Smartin 	unsigned int fs_type, fs_sub_type;
11426165e63Sdholland 
11556420644Smartin 	if (parts->num_part == 0)
11656420644Smartin 		return true;
11756420644Smartin 
118dcc57e24Smartin 	for (i = 0; i < parts->num_part - 1; i ++ ) {
119dcc57e24Smartin 		if (!parts->pscheme->get_part_info(parts, i, &info))
120dcc57e24Smartin 			continue;
12126165e63Sdholland 
12226165e63Sdholland 		/* skip unused or reserved partitions */
123dcc57e24Smartin 		if (boringpart(&info))
12426165e63Sdholland 			continue;
12526165e63Sdholland 
12626165e63Sdholland 		/*
12726165e63Sdholland 		 * check succeeding partitions for overlap.
1282f0b36a1Smartin 		 * O(n^2), but n is small.
12926165e63Sdholland 		 */
130dcc57e24Smartin 		istart = info.start;
131dcc57e24Smartin 		iend = istart + info.size;
132dcc57e24Smartin 		fs_type = info.fs_type;
133dcc57e24Smartin 		fs_sub_type = info.fs_sub_type;
13426165e63Sdholland 
135dcc57e24Smartin 		for (j = i+1; j < parts->num_part; j++) {
13626165e63Sdholland 
137dcc57e24Smartin 			if (!parts->pscheme->get_part_info(parts, j, &info))
13826165e63Sdholland 				continue;
13926165e63Sdholland 
140dcc57e24Smartin 			/* skip unused or reserved partitions */
141dcc57e24Smartin 			if (boringpart(&info))
142dcc57e24Smartin 				continue;
143dcc57e24Smartin 
144dcc57e24Smartin 			jstart = info.start;
145dcc57e24Smartin 			jend = jstart + info.size;
14626165e63Sdholland 
14726165e63Sdholland 			/* overlap? */
148dcc57e24Smartin 			if ((istart <= jstart && jstart < iend) ||
149dcc57e24Smartin 			    (jstart <= istart && istart < jend)) {
1502f0b36a1Smartin 				snprintf(ovl1, MENUSTRSIZE,
151dcc57e24Smartin 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
152dcc57e24Smartin 				    istart / sizemult, iend / sizemult,
153dcc57e24Smartin 				    multname,
154dcc57e24Smartin 				    getfslabelname(fs_type, fs_sub_type));
1552f0b36a1Smartin 				snprintf(ovl2, MENUSTRSIZE,
156dcc57e24Smartin 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
157dcc57e24Smartin 				    jstart / sizemult, jend / sizemult,
158dcc57e24Smartin 				    multname,
159dcc57e24Smartin 				    getfslabelname(info.fs_type,
160dcc57e24Smartin 				        info.fs_sub_type));
161dcc57e24Smartin 				return false;
16226165e63Sdholland 			}
16326165e63Sdholland 		}
16426165e63Sdholland 	}
16526165e63Sdholland 
166dcc57e24Smartin 	return true;
16726165e63Sdholland }
16826165e63Sdholland 
169da5a563bSmartin int
checkoverlap(struct disk_partitions * parts)170dcc57e24Smartin checkoverlap(struct disk_partitions *parts)
171da5a563bSmartin {
172dcc57e24Smartin 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
173dcc57e24Smartin 	if (!checklabel(parts, desc1, desc2)) {
174dcc57e24Smartin 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
175da5a563bSmartin 		return 1;
176da5a563bSmartin 	}
177da5a563bSmartin 	return 0;
178da5a563bSmartin }
179da5a563bSmartin 
180dcc57e24Smartin /*
181dcc57e24Smartin  * return (see post_edit_verify):
182dcc57e24Smartin  *  0 -> abort
183dcc57e24Smartin  *  1 -> re-edit
184dcc57e24Smartin  *  2 -> continue installation
185dcc57e24Smartin  */
18626165e63Sdholland static int
verify_parts(struct partition_usage_set * pset,bool install)18734f3e937Smartin verify_parts(struct partition_usage_set *pset, bool install)
18826165e63Sdholland {
189dcc57e24Smartin 	struct part_usage_info *wanted;
190dcc57e24Smartin 	struct disk_partitions *parts;
191dcc57e24Smartin 	size_t i, num_root;
19260778859Smartin 	daddr_t first_bsdstart, inst_start;
193dcc57e24Smartin 	int rv;
19426165e63Sdholland 
195bf7de24aSmartin 	first_bsdstart = inst_start = -1;
196dcc57e24Smartin 	num_root = 0;
197dcc57e24Smartin 	parts = pset->parts;
198dcc57e24Smartin 	for (i = 0; i < pset->num; i++) {
199dcc57e24Smartin 		wanted = &pset->infos[i];
200dcc57e24Smartin 
201dcc57e24Smartin 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
20226165e63Sdholland 			continue;
203dcc57e24Smartin 		if (wanted->cur_part_id == NO_PART)
20426165e63Sdholland 			continue;
205dcc57e24Smartin 		if (!(wanted->instflags & PUIINST_MOUNT))
20626165e63Sdholland 			continue;
207dcc57e24Smartin 		if (strcmp(wanted->mount, "/") != 0)
208dcc57e24Smartin 			continue;
209dcc57e24Smartin 		num_root++;
210dcc57e24Smartin 
211bf7de24aSmartin 		if (first_bsdstart <= 0) {
212dcc57e24Smartin 			first_bsdstart = wanted->cur_start;
21326165e63Sdholland 		}
214bf7de24aSmartin 		if (inst_start < 0 &&
215bf7de24aSmartin 		    (wanted->cur_flags & PTI_INSTALL_TARGET)) {
216dcc57e24Smartin 			inst_start = wanted->cur_start;
217dcc57e24Smartin 		}
218dcc57e24Smartin 	}
219dcc57e24Smartin 
22034f3e937Smartin 	if ((num_root == 0 && install) ||
221bf7de24aSmartin 	    (num_root > 1 && inst_start < 0)) {
22234f3e937Smartin 		if (num_root == 0 && install)
223dcc57e24Smartin 			msg_display_subst(MSG_must_be_one_root, 2,
224dcc57e24Smartin 			    msg_string(parts->pscheme->name),
225dcc57e24Smartin 			    msg_string(parts->pscheme->short_name));
226dcc57e24Smartin 		else
227dcc57e24Smartin 			msg_display_subst(MSG_multbsdpart, 2,
228dcc57e24Smartin 			    msg_string(parts->pscheme->name),
229dcc57e24Smartin 			    msg_string(parts->pscheme->short_name));
230dcc57e24Smartin 		rv = ask_reedit(parts);
231dcc57e24Smartin 		if (rv != 2)
232dcc57e24Smartin 			return rv;
233dcc57e24Smartin 	}
234dcc57e24Smartin 
235dcc57e24Smartin 	/* Check for overlaps */
236dcc57e24Smartin 	if (checkoverlap(parts) != 0) {
237dcc57e24Smartin 		rv = ask_reedit(parts);
238dcc57e24Smartin 		if (rv != 2)
239dcc57e24Smartin 			return rv;
240dcc57e24Smartin 	}
241dcc57e24Smartin 
242dcc57e24Smartin 	/*
243dcc57e24Smartin 	 * post_edit_verify returns:
244dcc57e24Smartin 	 *  0 -> abort
245dcc57e24Smartin 	 *  1 -> re-edit
246dcc57e24Smartin 	 *  2 -> continue installation
247dcc57e24Smartin 	 */
248dcc57e24Smartin 	if (parts->pscheme->post_edit_verify)
249dcc57e24Smartin 		return parts->pscheme->post_edit_verify(parts, false);
250dcc57e24Smartin 
251dcc57e24Smartin 	return 2;
25226165e63Sdholland }
25326165e63Sdholland 
25426165e63Sdholland static int
edit_fs_start(menudesc * m,void * arg)25526165e63Sdholland edit_fs_start(menudesc *m, void *arg)
25626165e63Sdholland {
257dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
258dcc57e24Smartin 	daddr_t start, end;
25926165e63Sdholland 
260dcc57e24Smartin 	start = getpartoff(edit->pset->parts, edit->info.start);
261dcc57e24Smartin 	if (edit->info.size != 0) {
262f10d97a4Smartin 		if (start < (edit->info.start+edit->info.size)) {
26326165e63Sdholland 			/* Try to keep end in the same place */
264dcc57e24Smartin 			end = edit->info.start + edit->info.size;
26526165e63Sdholland 			if (end < start)
266dcc57e24Smartin 				edit->info.size = edit->pset->parts->pscheme->
267dcc57e24Smartin 				    max_free_space_at(edit->pset->parts,
268dcc57e24Smartin 				    edit->info.start);
26926165e63Sdholland 			else
270dcc57e24Smartin 				edit->info.size = end - start;
271f10d97a4Smartin 		} else {
272f10d97a4Smartin 			edit->info.size = 0;
273f10d97a4Smartin 		}
27426165e63Sdholland 	}
275dcc57e24Smartin 	edit->info.start = start;
27626165e63Sdholland 	return 0;
27726165e63Sdholland }
27826165e63Sdholland 
27926165e63Sdholland static int
edit_fs_size(menudesc * m,void * arg)28026165e63Sdholland edit_fs_size(menudesc *m, void *arg)
28126165e63Sdholland {
282dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
2834888d49bSmartin 	struct disk_part_info pinfo;
284dcc57e24Smartin 	daddr_t size;
28526165e63Sdholland 
2864888d49bSmartin 	/* get original partition data, in case start moved already */
287f10d97a4Smartin 	if (!edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
288f10d97a4Smartin 	    edit->id, &pinfo))
289f10d97a4Smartin 		pinfo = edit->info;
2904888d49bSmartin 	/* ask for new size with old start and current values */
2914888d49bSmartin 	size = getpartsize(edit->pset->parts, pinfo.start,
2924888d49bSmartin 	    edit->info.start, edit->info.size);
293dcc57e24Smartin 	if (size < 0)
294dcc57e24Smartin 		return 0;
295dcc57e24Smartin 	if (size > edit->pset->parts->disk_size)
296dcc57e24Smartin 		size = edit->pset->parts->disk_size - edit->info.start;
297dcc57e24Smartin 	edit->info.size = size;
29826165e63Sdholland 	return 0;
29926165e63Sdholland }
30026165e63Sdholland 
30126165e63Sdholland static int
set_ffs_opt_pow2(menudesc * m,void * arg)302b162b00bSmartin set_ffs_opt_pow2(menudesc *m, void *arg)
303b162b00bSmartin {
304b162b00bSmartin 	struct single_part_fs_edit *edit = arg;
305b162b00bSmartin 	size_t val = 1 << (edit->offset+m->cursel);
306b162b00bSmartin 
307b162b00bSmartin 	if (edit->mode == 1) {
308b162b00bSmartin 		edit->info.fs_opt1 = val;
309b162b00bSmartin 		edit->wanted->fs_opt1 = val;
310b162b00bSmartin 	} else if (edit->mode == 2) {
311b162b00bSmartin 		edit->info.fs_opt2 = val;
312b162b00bSmartin 		edit->wanted->fs_opt2 = val;
313b162b00bSmartin 	}
314b162b00bSmartin 	return 0;
315b162b00bSmartin }
316b162b00bSmartin 
317b162b00bSmartin static int
edit_fs_ffs_opt(menudesc * m,void * arg,msg head,size_t min_val,size_t max_val)318b162b00bSmartin edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
319b162b00bSmartin     size_t min_val, size_t max_val)
320b162b00bSmartin {
321b162b00bSmartin 	struct single_part_fs_edit *edit = arg;
322b162b00bSmartin 	menu_ent opts[min(MAXPHYS/4096, 8)];
323b162b00bSmartin 	char names[min(MAXPHYS/4096, 8)][20];
324b162b00bSmartin 	size_t i, val;
325b162b00bSmartin 	int menu;
326b162b00bSmartin 
327b162b00bSmartin 	edit->offset = ilog2(min_val);
328b162b00bSmartin 	memset(opts, 0, sizeof opts);
329b162b00bSmartin 	for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
330b162b00bSmartin 		snprintf(names[i], sizeof names[i], "%zu", val);
331b162b00bSmartin 		opts[i].opt_name = names[i];
332b162b00bSmartin 		opts[i].opt_action = set_ffs_opt_pow2;
333b162b00bSmartin 		opts[i].opt_flags = OPT_EXIT;
334b162b00bSmartin 	}
335b162b00bSmartin 	menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
336b162b00bSmartin 	    NULL, NULL, NULL, NULL, NULL);
337b162b00bSmartin 	if (menu < 0)
338b162b00bSmartin 		return 1;
339b162b00bSmartin 	process_menu(menu, arg);
340b162b00bSmartin 	free_menu(menu);
341b162b00bSmartin 	return 0;
342b162b00bSmartin }
343b162b00bSmartin 
344b162b00bSmartin static int
edit_fs_ffs_block(menudesc * m,void * arg)345b162b00bSmartin edit_fs_ffs_block(menudesc *m, void *arg)
346b162b00bSmartin {
347b162b00bSmartin 	struct single_part_fs_edit *edit = arg;
348b162b00bSmartin 
349b162b00bSmartin 	edit->mode = 1;		/* edit fs_opt1 */
350b162b00bSmartin 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
351b162b00bSmartin 	    4096, MAXPHYS);
352b162b00bSmartin }
353b162b00bSmartin 
354b162b00bSmartin static int
edit_fs_ffs_frag(menudesc * m,void * arg)355b162b00bSmartin edit_fs_ffs_frag(menudesc *m, void *arg)
356b162b00bSmartin {
357b162b00bSmartin 	struct single_part_fs_edit *edit = arg;
358b162b00bSmartin 	size_t bsize, sec_size;
359b162b00bSmartin 
360b162b00bSmartin 	edit->mode = 2;		/* edit fs_opt2 */
361b162b00bSmartin 	bsize = edit->info.fs_opt1;
362b162b00bSmartin 	if (bsize == 0) {
363b162b00bSmartin 		sec_size = edit->wanted->parts->bytes_per_sector;
364b162b00bSmartin 		if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
365b162b00bSmartin 			bsize = 32*1024;
366b162b00bSmartin 		else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
367b162b00bSmartin 			bsize = 16*1024;
368b162b00bSmartin 		else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
369b162b00bSmartin 			bsize = 8*1024;
370b162b00bSmartin 		else
371b162b00bSmartin 			bsize = 4+1024;
372b162b00bSmartin 	}
373b162b00bSmartin 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
374b162b00bSmartin 		bsize / 8, bsize);
375b162b00bSmartin }
376b162b00bSmartin 
377b162b00bSmartin static int
edit_fs_ffs_avg_size(menudesc * m,void * arg)378b162b00bSmartin edit_fs_ffs_avg_size(menudesc *m, void *arg)
379b162b00bSmartin {
380b162b00bSmartin 	struct single_part_fs_edit *edit = arg;
381b162b00bSmartin 	char answer[12];
382b162b00bSmartin 
383b162b00bSmartin 	snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
384b162b00bSmartin 	msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
385b162b00bSmartin 		answer, answer, sizeof answer);
386b162b00bSmartin 	edit->info.fs_opt3 = atol(answer);
387b162b00bSmartin 	edit->wanted->fs_opt3 = edit->info.fs_opt3;
388b162b00bSmartin 
389b162b00bSmartin 	return 0;
390b162b00bSmartin }
391b162b00bSmartin 
392b162b00bSmartin static int
edit_fs_preserve(menudesc * m,void * arg)39326165e63Sdholland edit_fs_preserve(menudesc *m, void *arg)
39426165e63Sdholland {
395dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
39626165e63Sdholland 
397dcc57e24Smartin 	edit->wanted->instflags ^= PUIINST_NEWFS;
398dcc57e24Smartin 	return 0;
399dcc57e24Smartin }
400dcc57e24Smartin 
401dcc57e24Smartin static int
edit_install(menudesc * m,void * arg)402dcc57e24Smartin edit_install(menudesc *m, void *arg)
403dcc57e24Smartin {
404dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
405dcc57e24Smartin 
406bf7de24aSmartin 	edit->info.flags ^= PTI_INSTALL_TARGET;
40726165e63Sdholland 	return 0;
40826165e63Sdholland }
40926165e63Sdholland 
41026165e63Sdholland static int
edit_fs_mount(menudesc * m,void * arg)41126165e63Sdholland edit_fs_mount(menudesc *m, void *arg)
41226165e63Sdholland {
413dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
41426165e63Sdholland 
415dcc57e24Smartin 	edit->wanted->instflags ^= PUIINST_MOUNT;
41626165e63Sdholland 	return 0;
41726165e63Sdholland }
41826165e63Sdholland 
41926165e63Sdholland static int
edit_fs_mountpt(menudesc * m,void * arg)42026165e63Sdholland edit_fs_mountpt(menudesc *m, void *arg)
42126165e63Sdholland {
422dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
423dcc57e24Smartin 	char *p, *first, *last, buf[MOUNTLEN];
42426165e63Sdholland 
425dcc57e24Smartin 	strlcpy(buf, edit->wanted->mount, sizeof buf);
42626165e63Sdholland 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
427dcc57e24Smartin 		buf, buf, MOUNTLEN);
42826165e63Sdholland 
429dcc57e24Smartin 	/*
430dcc57e24Smartin 	 * Trim all leading and trailing whitespace
431dcc57e24Smartin 	 */
432dcc57e24Smartin 	for (first = NULL, last = NULL, p = buf; *p; p++) {
433dcc57e24Smartin 		if (isspace((unsigned char)*p))
434dcc57e24Smartin 			continue;
435dcc57e24Smartin 		if (first == NULL)
436dcc57e24Smartin 			first = p;
437dcc57e24Smartin 		last = p;
438dcc57e24Smartin 	}
439dcc57e24Smartin 	if (last != NULL)
440dcc57e24Smartin 		last[1] = 0;
441dcc57e24Smartin 
442dc60041fSmartin 	if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
443dcc57e24Smartin 		edit->wanted->mount[0] = 0;
444dcc57e24Smartin 		edit->wanted->instflags &= ~PUIINST_MOUNT;
44526165e63Sdholland 		return 0;
44626165e63Sdholland 	}
44726165e63Sdholland 
448dcc57e24Smartin 	if (*first != '/') {
449dcc57e24Smartin 		edit->wanted->mount[0] = '/';
450dcc57e24Smartin 		strlcpy(&edit->wanted->mount[1], first,
451dcc57e24Smartin 		    sizeof(edit->wanted->mount)-1);
452dcc57e24Smartin 	} else {
453dcc57e24Smartin 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
45426165e63Sdholland 	}
45526165e63Sdholland 
45626165e63Sdholland 	return 0;
45726165e63Sdholland }
45826165e63Sdholland 
45926165e63Sdholland static int
edit_restore(menudesc * m,void * arg)46026165e63Sdholland edit_restore(menudesc *m, void *arg)
46126165e63Sdholland {
462dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
46326165e63Sdholland 
464dcc57e24Smartin 	edit->info = edit->old_info;
465dcc57e24Smartin 	*edit->wanted = edit->old_usage;
466dcc57e24Smartin 	return 0;
467dcc57e24Smartin }
468dcc57e24Smartin 
469dcc57e24Smartin static int
edit_cancel(menudesc * m,void * arg)470dcc57e24Smartin edit_cancel(menudesc *m, void *arg)
471dcc57e24Smartin {
472dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
473dcc57e24Smartin 
474dcc57e24Smartin 	edit->rv = -1;
47526165e63Sdholland 	return 1;
47626165e63Sdholland }
47726165e63Sdholland 
47826165e63Sdholland static int
edit_delete_ptn(menudesc * m,void * arg)479dcc57e24Smartin edit_delete_ptn(menudesc *m, void *arg)
48026165e63Sdholland {
481dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
48226165e63Sdholland 
483dcc57e24Smartin 	edit->rv = -2;
484dcc57e24Smartin 	return 1;
485dcc57e24Smartin }
486dcc57e24Smartin 
487dcc57e24Smartin /*
488dcc57e24Smartin  * We have added/removed partitions, all cur_part_id values are
489dcc57e24Smartin  * out of sync. Re-fetch and reorder partitions accordingly.
490dcc57e24Smartin  */
491dcc57e24Smartin static void
renumber_partitions(struct partition_usage_set * pset)492dcc57e24Smartin renumber_partitions(struct partition_usage_set *pset)
493dcc57e24Smartin {
494dcc57e24Smartin 	struct part_usage_info *ninfos;
495dcc57e24Smartin 	struct disk_part_info info;
496dcc57e24Smartin 	size_t i;
497dcc57e24Smartin 	part_id pno;
498dcc57e24Smartin 
499dcc57e24Smartin 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
500dcc57e24Smartin 	if (ninfos == NULL) {
501dcc57e24Smartin 		err_msg_win(err_outofmem);
502dcc57e24Smartin 		return;
503dcc57e24Smartin 	}
504dcc57e24Smartin 
505dcc57e24Smartin 	for (pno = 0; pno < pset->parts->num_part; pno++) {
506dcc57e24Smartin 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
507dcc57e24Smartin 		    &info))
508dcc57e24Smartin 			continue;
509bd383ef2Smartin 		for (i = 0; i < pset->num; i++) {
510dcc57e24Smartin 			if (pset->infos[i].cur_start != info.start)
511dcc57e24Smartin 				continue;
51234f3e937Smartin 			if (pset->infos[i].cur_flags != info.flags)
51334f3e937Smartin 				continue;
514*c415ce7eSmartin 			if ((info.flags & PTI_SPECIAL_PARTS) !=
515*c415ce7eSmartin 			    (pset->infos[i].flags & PTI_SPECIAL_PARTS))
516*c415ce7eSmartin 				continue;
517ed67fd58Smartin 			if ((info.fs_type != FS_UNUSED &&
518ed67fd58Smartin 			    info.fs_type == pset->infos[i].fs_type) ||
519ed67fd58Smartin 			    (pset->infos[i].type ==
520ed67fd58Smartin 			    info.nat_type->generic_ptype)) {
521dcc57e24Smartin 				memcpy(&ninfos[pno], &pset->infos[i],
522dcc57e24Smartin 				    sizeof(ninfos[pno]));
523dcc57e24Smartin 				ninfos[pno].cur_part_id = pno;
524dcc57e24Smartin 				break;
525dcc57e24Smartin 			}
526dcc57e24Smartin 		}
527ed67fd58Smartin 	}
528dcc57e24Smartin 
529bd383ef2Smartin 	free(pset->infos);
530bd383ef2Smartin 	pset->infos = ninfos;
531bd383ef2Smartin 	pset->num = pset->parts->num_part;
532dcc57e24Smartin }
533dcc57e24Smartin 
534dcc57e24Smartin /*
535dcc57e24Smartin  * Most often used file system types, we offer them in a first level menu.
536dcc57e24Smartin  */
537dcc57e24Smartin static const uint edit_fs_common_types[] =
538410116bbSmartin     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_EFI_SP, FS_BSDLFS, FS_EX2FS };
539dcc57e24Smartin 
540dcc57e24Smartin /*
541dcc57e24Smartin  * Functions for uncommon file system types - we offer the full list,
5422d0843baSmartin  * but put FFSv2 and FFSv1 at the front and duplicate FS_MSDOS as
543410116bbSmartin  * EFI system partition.
544dcc57e24Smartin  */
545dcc57e24Smartin static void
init_fs_type_ext(menudesc * menu,void * arg)546dcc57e24Smartin init_fs_type_ext(menudesc *menu, void *arg)
547dcc57e24Smartin {
548dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
549dcc57e24Smartin 	uint t = edit->info.fs_type;
550dcc57e24Smartin 	size_t i, ndx, max = menu->numopts;
551dcc57e24Smartin 
552dcc57e24Smartin 	if (t == FS_BSDFFS) {
553e2c1461cSmartin 		if (edit->info.fs_sub_type == 3)
554dcc57e24Smartin 			menu->cursel = 0;
555e2c1461cSmartin 		else if (edit->info.fs_sub_type == 2)
556dcc57e24Smartin 			menu->cursel = 1;
557e2c1461cSmartin 		else
558e2c1461cSmartin 			menu->cursel = 2;
559dcc57e24Smartin 		return;
5600b93e29fSmartin 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
561e2c1461cSmartin 		menu->cursel = FSMAXTYPES+2;
5620b93e29fSmartin 		return;
563dcc57e24Smartin 	}
564dcc57e24Smartin 	/* skip the two FFS entries, and do not add FFS later again */
565e2c1461cSmartin 	for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
566dcc57e24Smartin 		if (i == FS_UNUSED)
567dcc57e24Smartin 			continue;
568dcc57e24Smartin 		if (i == FS_BSDFFS)
569dcc57e24Smartin 			continue;
570dcc57e24Smartin 		if (fstypenames[i] == NULL)
571dcc57e24Smartin 			continue;
572dcc57e24Smartin 
573dcc57e24Smartin 		if (i == t) {
574dcc57e24Smartin 			menu->cursel = ndx;
575dcc57e24Smartin 			break;
576dcc57e24Smartin 		}
577410116bbSmartin 		if (i == FS_MSDOS) {
578410116bbSmartin 			ndx++;
579410116bbSmartin 			if (t == FS_EFI_SP) {
580410116bbSmartin 				menu->cursel = ndx;
581410116bbSmartin 				break;
582410116bbSmartin 			}
583410116bbSmartin 		}
584dcc57e24Smartin 		ndx++;
585dcc57e24Smartin 	}
586dcc57e24Smartin }
587dcc57e24Smartin 
588dcc57e24Smartin static int
set_fstype_ext(menudesc * menu,void * arg)589dcc57e24Smartin set_fstype_ext(menudesc *menu, void *arg)
590dcc57e24Smartin {
591dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
592dcc57e24Smartin 	size_t i, ndx, max = menu->numopts;
593f6d1758bSmartin 	enum part_type pt;
594dcc57e24Smartin 
595e2c1461cSmartin 	if (menu->cursel >= 0 && menu->cursel <= 2) {
596dcc57e24Smartin 		edit->info.fs_type = FS_BSDFFS;
597e2c1461cSmartin 		edit->info.fs_sub_type = 3-menu->cursel;
5980b93e29fSmartin 		goto found_type;
599e2c1461cSmartin 	} else if (menu->cursel == FSMAXTYPES+2) {
6000b93e29fSmartin 		edit->info.fs_type = FS_EX2FS;
6010b93e29fSmartin 		edit->info.fs_sub_type = 1;
6020b93e29fSmartin 		goto found_type;
603dcc57e24Smartin 	}
604dcc57e24Smartin 
605e2c1461cSmartin 	for (ndx = 3, i = 0; i < FSMAXTYPES && ndx < max; i++) {
606dcc57e24Smartin 		if (i == FS_UNUSED)
607dcc57e24Smartin 			continue;
608dcc57e24Smartin 		if (i == FS_BSDFFS)
609dcc57e24Smartin 			continue;
610dcc57e24Smartin 		if (fstypenames[i] == NULL)
611dcc57e24Smartin 			continue;
612dcc57e24Smartin 
613dcc57e24Smartin 		if (ndx == (size_t)menu->cursel) {
614dcc57e24Smartin 			edit->info.fs_type = i;
615dcc57e24Smartin 			edit->info.fs_sub_type = 0;
6160b93e29fSmartin 			goto found_type;
617dcc57e24Smartin 		}
618dcc57e24Smartin 		ndx++;
619410116bbSmartin 		if (i == FS_MSDOS) {
620410116bbSmartin 			if (ndx == (size_t)menu->cursel) {
621410116bbSmartin 				edit->info.fs_type = FS_EFI_SP;
622410116bbSmartin 				edit->info.fs_sub_type = 0;
623410116bbSmartin 				goto found_type;
624410116bbSmartin 			}
625e2c1461cSmartin 			ndx++;
626410116bbSmartin 		}
627dcc57e24Smartin 	}
628dcc57e24Smartin 	return 1;
6290b93e29fSmartin 
6300b93e29fSmartin found_type:
631f6d1758bSmartin 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
6320b93e29fSmartin 	edit->info.nat_type = edit->pset->parts->pscheme->
633f6d1758bSmartin 	    get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
6340b93e29fSmartin 	if (edit->info.nat_type == NULL)
6350b93e29fSmartin 		edit->info.nat_type = edit->pset->parts->pscheme->
6360b93e29fSmartin 		    get_generic_part_type(PT_root);
6370b93e29fSmartin 	edit->wanted->type = edit->info.nat_type->generic_ptype;
6380b93e29fSmartin 	edit->wanted->fs_type = edit->info.fs_type;
6390b93e29fSmartin 	edit->wanted->fs_version = edit->info.fs_sub_type;
6400b93e29fSmartin 	return 1;
641dcc57e24Smartin }
642dcc57e24Smartin 
643dcc57e24Smartin /*
644dcc57e24Smartin  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
645dcc57e24Smartin  * skip later FFS entry in the generic list.
646dcc57e24Smartin  */
647dcc57e24Smartin static int
edit_fs_type_ext(menudesc * menu,void * arg)648dcc57e24Smartin edit_fs_type_ext(menudesc *menu, void *arg)
649dcc57e24Smartin {
650dcc57e24Smartin 	menu_ent *opts;
651dcc57e24Smartin 	int m;
652dcc57e24Smartin 	size_t i, ndx, cnt;
653dcc57e24Smartin 
654e2c1461cSmartin 	cnt = __arraycount(fstypenames)+2;
655dcc57e24Smartin 	opts = calloc(cnt, sizeof(*opts));
656dcc57e24Smartin 	if (opts == NULL)
657dcc57e24Smartin 		return 1;
658dcc57e24Smartin 
659dcc57e24Smartin 	ndx = 0;
660e2c1461cSmartin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2ea);
661e2c1461cSmartin 	opts[ndx].opt_action = set_fstype_ext;
662e2c1461cSmartin 	ndx++;
663dcc57e24Smartin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
664dcc57e24Smartin 	opts[ndx].opt_action = set_fstype_ext;
665dcc57e24Smartin 	ndx++;
666dcc57e24Smartin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
667dcc57e24Smartin 	opts[ndx].opt_action = set_fstype_ext;
668dcc57e24Smartin 	ndx++;
669dcc57e24Smartin 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
670dcc57e24Smartin 		if (i == FS_UNUSED)
671dcc57e24Smartin 			continue;
672dcc57e24Smartin 		if (i == FS_BSDFFS)
673dcc57e24Smartin 			continue;
674dcc57e24Smartin 		if (fstypenames[i] == NULL)
675dcc57e24Smartin 			continue;
676dcc57e24Smartin 		opts[ndx].opt_name = fstypenames[i];
677dcc57e24Smartin 		opts[ndx].opt_action = set_fstype_ext;
678dcc57e24Smartin 		ndx++;
679410116bbSmartin 		if (i == FS_MSDOS) {
680410116bbSmartin 			opts[ndx] = opts[ndx-1];
681410116bbSmartin 			opts[ndx].opt_name = getfslabelname(FS_EFI_SP, 0);
682410116bbSmartin 			ndx++;
683410116bbSmartin 		}
684dcc57e24Smartin 	}
6850b93e29fSmartin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
6860b93e29fSmartin 	opts[ndx].opt_action = set_fstype_ext;
6870b93e29fSmartin 	ndx++;
688dcc57e24Smartin 	assert(ndx == cnt);
689dcc57e24Smartin 	m = new_menu(MSG_Select_the_type, opts, ndx,
690dcc57e24Smartin 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
691dcc57e24Smartin 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
692dcc57e24Smartin 
693dcc57e24Smartin 	if (m < 0)
694dcc57e24Smartin 		return 1;
695dcc57e24Smartin 	process_menu(m, arg);
696dcc57e24Smartin 	free_menu(m);
697dcc57e24Smartin 	free(opts);
698dcc57e24Smartin 
69926165e63Sdholland 	return 1;
70026165e63Sdholland }
70126165e63Sdholland 
70226165e63Sdholland static void
init_fs_type(menudesc * menu,void * arg)703dcc57e24Smartin init_fs_type(menudesc *menu, void *arg)
70426165e63Sdholland {
705dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
706dcc57e24Smartin 	size_t i;
70726165e63Sdholland 
708dcc57e24Smartin 	/* init menu->cursel from fs type in arg */
709dcc57e24Smartin 	if (edit->info.fs_type == FS_BSDFFS) {
710e2c1461cSmartin 		if (edit->info.fs_sub_type == 3)
711dcc57e24Smartin 			menu->cursel = 0;
712e2c1461cSmartin 		else if (edit->info.fs_sub_type == 2)
713dcc57e24Smartin 			menu->cursel = 1;
714e2c1461cSmartin 		else
715e2c1461cSmartin 			menu->cursel = 2;
716dcc57e24Smartin 	}
717dcc57e24Smartin 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
718dcc57e24Smartin 		if (edit->info.fs_type == edit_fs_common_types[i]) {
719e2c1461cSmartin 			menu->cursel = i+2;
720dcc57e24Smartin 			break;
721dcc57e24Smartin 		}
722dcc57e24Smartin 	}
72326165e63Sdholland }
72426165e63Sdholland 
725dcc57e24Smartin static int
set_fstype(menudesc * menu,void * arg)726dcc57e24Smartin set_fstype(menudesc *menu, void *arg)
727dcc57e24Smartin {
728dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
729f6d1758bSmartin 	enum part_type pt;
730dcc57e24Smartin 	int ndx;
731dcc57e24Smartin 
732f6d1758bSmartin 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
733e2c1461cSmartin 	if (menu->cursel < 3) {
734dcc57e24Smartin 		edit->info.fs_type = FS_BSDFFS;
735e2c1461cSmartin 		edit->info.fs_sub_type = 3-menu->cursel;
736dcc57e24Smartin 		edit->info.nat_type = edit->pset->parts->pscheme->
737e2c1461cSmartin 		    get_fs_part_type(pt, FS_BSDFFS, edit->info.fs_sub_type);
738dcc57e24Smartin 		if (edit->info.nat_type == NULL)
739dcc57e24Smartin 			edit->info.nat_type = edit->pset->parts->
740dcc57e24Smartin 			    pscheme->get_generic_part_type(PT_root);
741dcc57e24Smartin 		edit->wanted->type = edit->info.nat_type->generic_ptype;
742dcc57e24Smartin 		edit->wanted->fs_type = edit->info.fs_type;
743dcc57e24Smartin 		edit->wanted->fs_version = edit->info.fs_sub_type;
744dcc57e24Smartin 		return 1;
745dcc57e24Smartin 	}
746e2c1461cSmartin 	ndx = menu->cursel-2;
747dcc57e24Smartin 
748dcc57e24Smartin 	if (ndx < 0 ||
749dcc57e24Smartin 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
750dcc57e24Smartin 		return 1;
751dcc57e24Smartin 
752dcc57e24Smartin 	edit->info.fs_type = edit_fs_common_types[ndx];
753dcc57e24Smartin 	edit->info.fs_sub_type = 0;
754dcc57e24Smartin 	edit->info.nat_type = edit->pset->parts->pscheme->
755f6d1758bSmartin 	    get_fs_part_type(pt, edit->info.fs_type, 0);
756dcc57e24Smartin 	if (edit->info.nat_type == NULL)
757dcc57e24Smartin 		edit->info.nat_type = edit->pset->parts->
758dcc57e24Smartin 		    pscheme->get_generic_part_type(PT_root);
759dcc57e24Smartin 	edit->wanted->type = edit->info.nat_type->generic_ptype;
760dcc57e24Smartin 	edit->wanted->fs_type = edit->info.fs_type;
761dcc57e24Smartin 	edit->wanted->fs_version = edit->info.fs_sub_type;
762dcc57e24Smartin 	return 1;
763dcc57e24Smartin }
764dcc57e24Smartin 
765dcc57e24Smartin /*
766dcc57e24Smartin  * Offer a menu selecting the common file system types
767dcc57e24Smartin  */
768dcc57e24Smartin static int
edit_fs_type(menudesc * menu,void * arg)769dcc57e24Smartin edit_fs_type(menudesc *menu, void *arg)
770dcc57e24Smartin {
771dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
772dcc57e24Smartin 	menu_ent *opts;
773dcc57e24Smartin 	int m, cnt;
774dcc57e24Smartin 	size_t i;
775dcc57e24Smartin 
776dcc57e24Smartin 	/*
777dcc57e24Smartin 	 * Shortcut to full menu if we have an exotic value
778dcc57e24Smartin 	 */
7790b93e29fSmartin 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
7800b93e29fSmartin 		edit_fs_type_ext(menu, arg);
7810b93e29fSmartin 		return 0;
7820b93e29fSmartin 	}
783dcc57e24Smartin 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
784dcc57e24Smartin 		if (edit->info.fs_type == edit_fs_common_types[i])
785dcc57e24Smartin 			break;
786dcc57e24Smartin 	if (i >= __arraycount(edit_fs_common_types)) {
787dcc57e24Smartin 		edit_fs_type_ext(menu, arg);
788dcc57e24Smartin 		return 0;
789dcc57e24Smartin 	}
790dcc57e24Smartin 
791dcc57e24Smartin 	/*
792dcc57e24Smartin 	 * Starting with a common type, show short menu first
793dcc57e24Smartin 	 */
794e2c1461cSmartin 	cnt = __arraycount(edit_fs_common_types) + 3;
795dcc57e24Smartin 	opts = calloc(cnt, sizeof(*opts));
796dcc57e24Smartin 	if (opts == NULL)
797dcc57e24Smartin 		return 0;
798dcc57e24Smartin 
799ca67d538Smartin 	/* special case entry 0 - 2: three FFS entries */
800dcc57e24Smartin 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
801e2c1461cSmartin 		opts[i+2].opt_name = getfslabelname(edit_fs_common_types[i], 0);
802e2c1461cSmartin 		opts[i+2].opt_action = set_fstype;
803dcc57e24Smartin 	}
804e2c1461cSmartin 	/* duplicate FFS (at offset 2) into first two entries */
805e2c1461cSmartin 	opts[0] = opts[1] = opts[2];
806e2c1461cSmartin 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2ea);
807e2c1461cSmartin 	opts[1].opt_name = msg_string(MSG_fs_type_ffsv2);
808e2c1461cSmartin 	opts[2].opt_name = msg_string(MSG_fs_type_ffs);
809dcc57e24Smartin 	/* add secondary sub-menu */
810e2c1461cSmartin 	assert(i+2 < (size_t)cnt);
811e2c1461cSmartin 	opts[i+2].opt_name = msg_string(MSG_other_fs_type);
812e2c1461cSmartin 	opts[i+2].opt_action = edit_fs_type_ext;
813dcc57e24Smartin 
814dcc57e24Smartin 	m = new_menu(MSG_Select_the_type, opts, cnt,
815dcc57e24Smartin 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
816dcc57e24Smartin 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
817dcc57e24Smartin 
818dcc57e24Smartin 	if (m < 0)
819dcc57e24Smartin 		return 0;
820dcc57e24Smartin 	process_menu(m, arg);
821dcc57e24Smartin 	free_menu(m);
822dcc57e24Smartin 	free(opts);
823dcc57e24Smartin 
824dcc57e24Smartin 	return 0;
825dcc57e24Smartin }
826dcc57e24Smartin 
827dcc57e24Smartin 
828dcc57e24Smartin static void update_edit_ptn_menu(menudesc *m, void *arg);
829dcc57e24Smartin static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
830dcc57e24Smartin static int edit_ptn_custom_type(menudesc *m, void *arg);
83126165e63Sdholland 
832f6bcb8e7Smartin static void
remember_deleted(struct partition_usage_set * pset,struct disk_partitions * parts)833f6bcb8e7Smartin remember_deleted(struct partition_usage_set *pset,
834f6bcb8e7Smartin     struct disk_partitions *parts)
835f6bcb8e7Smartin {
836f6bcb8e7Smartin 	size_t i, num;
837f6bcb8e7Smartin 	struct disk_partitions **tab;
838f6bcb8e7Smartin 
839f6bcb8e7Smartin 	/* do we have parts on record already? */
840f6bcb8e7Smartin 	for (i = 0; i < pset->num_write_back; i++)
841f6bcb8e7Smartin 		if (pset->write_back[i] == parts)
842f6bcb8e7Smartin 			return;
843f6bcb8e7Smartin 	/*
844f6bcb8e7Smartin 	 * Need to record this partition table for write back
845f6bcb8e7Smartin 	 */
846f6bcb8e7Smartin 	num = pset->num_write_back + 1;
847f6bcb8e7Smartin 	tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
848f6bcb8e7Smartin 	if (!tab)
849f6bcb8e7Smartin 		return;
850f6bcb8e7Smartin 	tab[pset->num_write_back] = parts;
851f6bcb8e7Smartin 	pset->write_back = tab;
852f6bcb8e7Smartin 	pset->num_write_back = num;
853f6bcb8e7Smartin }
854f6bcb8e7Smartin 
855da5a563bSmartin int
edit_ptn(menudesc * menu,void * arg)85626165e63Sdholland edit_ptn(menudesc *menu, void *arg)
85726165e63Sdholland {
858dcc57e24Smartin 	struct partition_usage_set *pset = arg;
859dcc57e24Smartin 	struct single_part_fs_edit edit;
860dcc57e24Smartin 	int fspart_menu, num_opts;
861dcc57e24Smartin 	const char *err;
862dcc57e24Smartin 	menu_ent *mopts, *popt;
863dcc57e24Smartin 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
864dcc57e24Smartin 
865dcc57e24Smartin 	static const menu_ent edit_ptn_fields_head[] = {
86622f633feSchristos 		{ .opt_action=edit_fs_type },
86722f633feSchristos 		{ .opt_action=edit_fs_start },
86822f633feSchristos 		{ .opt_action=edit_fs_size },
86922f633feSchristos 		{ .opt_flags=OPT_IGNORE },
870dcc57e24Smartin 	};
871dcc57e24Smartin 
872dcc57e24Smartin 	static const menu_ent edit_ptn_fields_head_add[] = {
87322f633feSchristos 		{ .opt_action=edit_install },
874dcc57e24Smartin 	};
875dcc57e24Smartin 
876dcc57e24Smartin 	static const menu_ent edit_ptn_fields_head2[] = {
87722f633feSchristos 		{ .opt_action=edit_fs_preserve },
87822f633feSchristos 		{ .opt_action=edit_fs_mount },
879b288bf5cSmartin 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
88022f633feSchristos 		{ .opt_action=edit_fs_mountpt },
881dcc57e24Smartin 	};
882b162b00bSmartin 
883b162b00bSmartin 	static const menu_ent edit_ptn_fields_ffs[] = {
884b162b00bSmartin 		{ .opt_action=edit_fs_ffs_avg_size },
885b162b00bSmartin 		{ .opt_action=edit_fs_ffs_block },
886b162b00bSmartin 		{ .opt_action=edit_fs_ffs_frag },
887b162b00bSmartin 	};
888b162b00bSmartin 
889dcc57e24Smartin 	static const menu_ent edit_ptn_fields_tail[] = {
890b288bf5cSmartin 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
891b288bf5cSmartin 		  .opt_flags=OPT_SUB },
89222f633feSchristos 		{ .opt_name=MSG_restore,
893b288bf5cSmartin 		  .opt_action=edit_restore},
89422f633feSchristos 		{ .opt_name=MSG_Delete_partition,
895dcc57e24Smartin 		  .opt_action=edit_delete_ptn},
89622f633feSchristos 		{ .opt_name=MSG_cancel,
897dcc57e24Smartin 		  .opt_action=edit_cancel},
89826165e63Sdholland 	};
89926165e63Sdholland 
900dcc57e24Smartin 	memset(&edit, 0, sizeof edit);
901dcc57e24Smartin 	edit.pset = pset;
902dcc57e24Smartin 	edit.index = menu->cursel;
903dcc57e24Smartin 	edit.wanted = &pset->infos[edit.index];
904dcc57e24Smartin 
905dcc57e24Smartin 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
906dcc57e24Smartin 		return 0;
907dcc57e24Smartin 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
908dcc57e24Smartin 
909dcc57e24Smartin 	num_opts = __arraycount(edit_ptn_fields_head) +
910dcc57e24Smartin 	    __arraycount(edit_ptn_fields_head2) +
911dcc57e24Smartin 	    __arraycount(edit_ptn_fields_tail);
912b162b00bSmartin 	if (edit.wanted->fs_type == FS_BSDFFS ||
913b162b00bSmartin 	    edit.wanted->fs_type == FS_BSDLFS)
914b162b00bSmartin 		num_opts += __arraycount(edit_ptn_fields_ffs);
915dcc57e24Smartin 	if (with_inst_opt)
916dcc57e24Smartin 		num_opts += __arraycount(edit_ptn_fields_head_add);
917dcc57e24Smartin 	if (is_new_part)
918dcc57e24Smartin 		num_opts--;
919dcc57e24Smartin 	else
920dcc57e24Smartin 		num_opts += pset->parts->pscheme->custom_attribute_count;
921dcc57e24Smartin 
922dcc57e24Smartin 	mopts = calloc(num_opts, sizeof(*mopts));
923dcc57e24Smartin 	if (mopts == NULL) {
924dcc57e24Smartin 		err_msg_win(err_outofmem);
925dcc57e24Smartin 		return 0;
926dcc57e24Smartin 	}
927dcc57e24Smartin 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
928dcc57e24Smartin 	popt = mopts + __arraycount(edit_ptn_fields_head);
929dcc57e24Smartin 	if (with_inst_opt) {
930dcc57e24Smartin 		memcpy(popt, edit_ptn_fields_head_add,
931dcc57e24Smartin 		    sizeof(edit_ptn_fields_head_add));
932dcc57e24Smartin 		popt +=  __arraycount(edit_ptn_fields_head_add);
933dcc57e24Smartin 	}
934dcc57e24Smartin 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
935dcc57e24Smartin 	popt +=  __arraycount(edit_ptn_fields_head2);
936b162b00bSmartin 	if (edit.wanted->fs_type == FS_BSDFFS ||
937b162b00bSmartin 	    edit.wanted->fs_type == FS_BSDLFS) {
938b162b00bSmartin 		memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
939b162b00bSmartin 		popt +=  __arraycount(edit_ptn_fields_ffs);
940b162b00bSmartin 	}
941dcc57e24Smartin 	edit.first_custom_attr = popt - mopts;
942dcc57e24Smartin 	if (!is_new_part) {
943dcc57e24Smartin 		for (size_t i = 0;
944dcc57e24Smartin 		    i < pset->parts->pscheme->custom_attribute_count;
945dcc57e24Smartin 		    i++, popt++) {
946dcc57e24Smartin 			popt->opt_action = edit_ptn_custom_type;
947dcc57e24Smartin 		}
948dcc57e24Smartin 	}
949dcc57e24Smartin 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
950dcc57e24Smartin 	popt += __arraycount(edit_ptn_fields_tail) - 1;
951dcc57e24Smartin 	if (is_new_part)
952dcc57e24Smartin 		memcpy(popt-1, popt, sizeof(*popt));
953dcc57e24Smartin 
954dcc57e24Smartin 	if (is_new_part) {
955dcc57e24Smartin 		struct disk_part_free_space space;
956dcc57e24Smartin 		daddr_t align = pset->parts->pscheme->get_part_alignment(
957dcc57e24Smartin 		    pset->parts);
958dcc57e24Smartin 
959dcc57e24Smartin 		edit.id = NO_PART;
960dcc57e24Smartin 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
961dcc57e24Smartin 		    &space, 1, align, align, -1, -1) == 1) {
962dcc57e24Smartin 			edit.info.start = space.start;
963dcc57e24Smartin 			edit.info.size = space.size;
964dcc57e24Smartin 			edit.info.fs_type = FS_BSDFFS;
965dcc57e24Smartin 			edit.info.fs_sub_type = 2;
966dcc57e24Smartin 			edit.info.nat_type = pset->parts->pscheme->
967f6d1758bSmartin 			    get_fs_part_type(PT_root, edit.info.fs_type,
968dcc57e24Smartin 			    edit.info.fs_sub_type);
969dcc57e24Smartin 			edit.wanted->instflags = PUIINST_NEWFS;
970dcc57e24Smartin 		}
971dcc57e24Smartin 	} else {
972dcc57e24Smartin 		edit.id = pset->infos[edit.index].cur_part_id;
973dcc57e24Smartin 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
974dcc57e24Smartin 		    &edit.info)) {
975dcc57e24Smartin 			free(mopts);
976dcc57e24Smartin 			return 0;
977dcc57e24Smartin 		}
97826165e63Sdholland 	}
97926165e63Sdholland 
980dcc57e24Smartin 	edit.old_usage = *edit.wanted;
981dcc57e24Smartin 	edit.old_info = edit.info;
982dcc57e24Smartin 
983dcc57e24Smartin 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
984dcc57e24Smartin 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
985dcc57e24Smartin 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
986dcc57e24Smartin 		NULL, MSG_OK);
987dcc57e24Smartin 
988dcc57e24Smartin 	process_menu(fspart_menu, &edit);
989dcc57e24Smartin 	free(mopts);
990dcc57e24Smartin 	free_menu(fspart_menu);
991dcc57e24Smartin 
992dcc57e24Smartin 	if (edit.rv == 0) {	/* OK, set new data */
993dcc57e24Smartin 		edit.info.last_mounted = edit.wanted->mount;
994dcc57e24Smartin 		if (is_new_part) {
995a38ad336Smartin 			edit.wanted->parts = pset->parts;
9962edc11f2Smartin 			if (!can_newfs_fstype(edit.info.fs_type))
99748b353b4Smartin 				edit.wanted->instflags &= ~PUIINST_NEWFS;
998dcc57e24Smartin 			edit.wanted->cur_part_id = pset->parts->pscheme->
999dcc57e24Smartin 			    add_partition(pset->parts, &edit.info, &err);
1000dcc57e24Smartin 			if (edit.wanted->cur_part_id == NO_PART)
1001dcc57e24Smartin 				err_msg_win(err);
1002dcc57e24Smartin 			else {
1003dcc57e24Smartin 				pset->parts->pscheme->get_part_info(
1004dcc57e24Smartin 				    pset->parts, edit.wanted->cur_part_id,
1005dcc57e24Smartin 				    &edit.info);
1006dcc57e24Smartin 				edit.wanted->cur_start = edit.info.start;
1007dcc57e24Smartin 				edit.wanted->size = edit.info.size;
1008dcc57e24Smartin 				edit.wanted->type =
1009dcc57e24Smartin 				    edit.info.nat_type->generic_ptype;
1010dcc57e24Smartin 				edit.wanted->fs_type = edit.info.fs_type;
1011dcc57e24Smartin 				edit.wanted->fs_version = edit.info.fs_sub_type;
10127873f2edSmartin 				edit.wanted->cur_flags = edit.info.flags;
1013dcc57e24Smartin 				/* things have changed, re-sort */
1014dcc57e24Smartin 				renumber_partitions(pset);
101526165e63Sdholland 			}
1016dcc57e24Smartin 		} else {
1017dcc57e24Smartin 			if (!pset->parts->pscheme->set_part_info(pset->parts,
1018dcc57e24Smartin 			    edit.id, &edit.info, &err))
1019dcc57e24Smartin 				err_msg_win(err);
102087a92e5dSmartin 			else
102187a92e5dSmartin 				pset->cur_free_space += edit.old_info.size -
102287a92e5dSmartin 				    edit.info.size;
102326165e63Sdholland 		}
102426165e63Sdholland 
1025dcc57e24Smartin 		/*
1026dcc57e24Smartin 		 * if size has changed, we may need to add or remove
1027dcc57e24Smartin 		 * the option to add partitions
1028dcc57e24Smartin 		 */
1029dcc57e24Smartin 		show_partition_adder(menu, pset);
1030dcc57e24Smartin 	} else if (edit.rv == -1) {	/* cancel edit */
1031dcc57e24Smartin 		if (is_new_part) {
1032dcc57e24Smartin 			memmove(pset->infos+edit.index,
1033dcc57e24Smartin 			    pset->infos+edit.index+1,
1034dcc57e24Smartin 			    sizeof(*pset->infos)*(pset->num-edit.index));
1035dcc57e24Smartin 			memmove(menu->opts+edit.index,
1036dcc57e24Smartin 			    menu->opts+edit.index+1,
1037dcc57e24Smartin 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
1038dcc57e24Smartin 			menu->numopts--;
1039dcc57e24Smartin 			menu->cursel = 0;
1040dcc57e24Smartin 			pset->num--;
1041dcc57e24Smartin 			return -1;
1042dcc57e24Smartin 		}
1043dcc57e24Smartin 		pset->infos[edit.index] = edit.old_usage;
1044dcc57e24Smartin 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
1045dcc57e24Smartin 		if (!pset->parts->pscheme->delete_partition(pset->parts,
1046dcc57e24Smartin 		    edit.id, &err)) {
1047dcc57e24Smartin 			err_msg_win(err);
1048dcc57e24Smartin 			return 0;
1049dcc57e24Smartin 		}
1050f6bcb8e7Smartin 		remember_deleted(pset,
1051f6bcb8e7Smartin 		    pset->infos[edit.index].parts);
105287a92e5dSmartin 		pset->cur_free_space += edit.info.size;
1053dcc57e24Smartin 		memmove(pset->infos+edit.index,
1054dcc57e24Smartin 		    pset->infos+edit.index+1,
1055dcc57e24Smartin 		    sizeof(*pset->infos)*(pset->num-edit.index));
1056dcc57e24Smartin 		memmove(menu->opts+edit.index,
1057dcc57e24Smartin 		    menu->opts+edit.index+1,
1058dcc57e24Smartin 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
1059dcc57e24Smartin 		menu->numopts--;
1060dcc57e24Smartin 		menu->cursel = 0;
1061391eccd4Smartin 		if (pset->parts->num_part == 0)
1062391eccd4Smartin 			menu->cursel = 1;	/* skip sentinel line */
1063dcc57e24Smartin 
1064dcc57e24Smartin 		/* things have changed, re-sort */
1065dcc57e24Smartin 		pset->num--;
1066dcc57e24Smartin 		renumber_partitions(pset);
1067dcc57e24Smartin 
1068dcc57e24Smartin 		/* we can likely add new partitions now */
1069dcc57e24Smartin 		show_partition_adder(menu, pset);
1070dcc57e24Smartin 
1071dcc57e24Smartin 		return -1;
107226165e63Sdholland 	}
107326165e63Sdholland 
107426165e63Sdholland 	return 0;
107526165e63Sdholland }
107626165e63Sdholland 
107726165e63Sdholland static void
update_edit_ptn_menu(menudesc * m,void * arg)1078dcc57e24Smartin update_edit_ptn_menu(menudesc *m, void *arg)
107926165e63Sdholland {
1080dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
108126165e63Sdholland 	int i;
1082dcc57e24Smartin 	uint t = edit->info.fs_type;
1083dcc57e24Smartin 	size_t attr_no;
108426165e63Sdholland 
108526165e63Sdholland 	/* Determine which of the properties can be changed */
1086dcc57e24Smartin 	for (i = 0; i < m->numopts; i++) {
1087dcc57e24Smartin 		if (m->opts[i].opt_action == NULL &&
1088dcc57e24Smartin 		    m->opts[i].opt_menu != MENU_mountoptions)
1089dcc57e24Smartin 			continue;
1090dcc57e24Smartin 
109126165e63Sdholland 		/* Default to disabled... */
109226165e63Sdholland 		m->opts[i].opt_flags |= OPT_IGNORE;
1093dcc57e24Smartin 		if ((t == FS_UNUSED || t == FS_SWAP) &&
1094dcc57e24Smartin 		    (m->opts[i].opt_action == edit_fs_preserve ||
1095dcc57e24Smartin 		     m->opts[i].opt_action == edit_fs_mount ||
1096dcc57e24Smartin 		     m->opts[i].opt_action == edit_fs_mountpt ||
1097dcc57e24Smartin 		     m->opts[i].opt_menu == MENU_mountoptions))
109826165e63Sdholland 			continue;
1099dcc57e24Smartin 		if (m->opts[i].opt_action == edit_install &&
1100dcc57e24Smartin 		    edit->info.nat_type &&
1101dcc57e24Smartin 		    edit->info.nat_type->generic_ptype != PT_root)
1102dcc57e24Smartin 			/* can only install onto PT_root partitions */
110326165e63Sdholland 			continue;
1104dcc57e24Smartin 		if (m->opts[i].opt_action == edit_fs_preserve &&
110548b353b4Smartin 		    !can_newfs_fstype(t)) {
1106bcf11b21Smartin 			/* Can not newfs this filesystem */
1107dcc57e24Smartin 			edit->wanted->instflags &= ~PUIINST_NEWFS;
110826165e63Sdholland 			continue;
110926165e63Sdholland 		}
1110dcc57e24Smartin 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
1111dcc57e24Smartin 			attr_no = (size_t)i - edit->first_custom_attr;
1112dcc57e24Smartin 			if (!edit->pset->parts->pscheme->
1113dcc57e24Smartin 			    custom_attribute_writable(
1114dcc57e24Smartin 			    edit->pset->parts, edit->id, attr_no))
111526165e63Sdholland 				continue;
111626165e63Sdholland 		}
1117e269f57bSmartin 		/*
1118e269f57bSmartin 		 * Do not allow editing of size/start/type when partition
1119e269f57bSmartin 		 * is defined in some outer partition table already
1120e269f57bSmartin 		 */
1121e269f57bSmartin 		if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
1122e269f57bSmartin 		    && (m->opts[i].opt_action == edit_fs_type
1123e269f57bSmartin 			|| m->opts[i].opt_action == edit_fs_start
1124e269f57bSmartin 			|| m->opts[i].opt_action == edit_fs_size))
1125e269f57bSmartin 				continue;
112626165e63Sdholland 		/* Ok: we want this one */
112726165e63Sdholland 		m->opts[i].opt_flags &= ~OPT_IGNORE;
112826165e63Sdholland 	}
1129e269f57bSmartin 
1130e269f57bSmartin 	/* Avoid starting at a (now) disabled menu item */
1131e269f57bSmartin 	while (m->cursel >= 0 && m->cursel < m->numopts
1132e269f57bSmartin 	    && (m->opts[m->cursel].opt_flags & OPT_IGNORE))
1133e269f57bSmartin 		m->cursel++;
113426165e63Sdholland }
113526165e63Sdholland 
113626165e63Sdholland static void
draw_edit_ptn_line(menudesc * m,int opt,void * arg)1137dcc57e24Smartin draw_edit_ptn_line(menudesc *m, int opt, void *arg)
113826165e63Sdholland {
1139dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
1140dcc57e24Smartin 	static int col_width;
1141dcc57e24Smartin 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
1142dcc57e24Smartin 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
1143b162b00bSmartin 	     *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
1144dcc57e24Smartin 	const char *c;
1145dcc57e24Smartin 	char val[MENUSTRSIZE];
1146dcc57e24Smartin 	const char *attrname;
1147dcc57e24Smartin 	size_t attr_no;
114826165e63Sdholland 
1149dcc57e24Smartin 	if (col_width == 0) {
1150dcc57e24Smartin 		int l;
1151dcc57e24Smartin 
1152dcc57e24Smartin #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
1153dcc57e24Smartin 			if (l > col_width) col_width = l
1154dcc57e24Smartin 
1155dcc57e24Smartin 		LOAD(ptn_type);
1156dcc57e24Smartin 		LOAD(ptn_start);
1157dcc57e24Smartin 		LOAD(ptn_size);
1158dcc57e24Smartin 		LOAD(ptn_end);
1159dcc57e24Smartin 		LOAD(ptn_install);
1160dcc57e24Smartin 		LOAD(ptn_newfs);
1161dcc57e24Smartin 		LOAD(ptn_mount);
1162dcc57e24Smartin 		LOAD(ptn_mount_options);
1163dcc57e24Smartin 		LOAD(ptn_mountpt);
1164b162b00bSmartin 		LOAD(ptn_bsize);
1165b162b00bSmartin 		LOAD(ptn_fsize);
1166b162b00bSmartin 		LOAD(ptn_isize);
1167dcc57e24Smartin #undef LOAD
1168dcc57e24Smartin 
1169dcc57e24Smartin 		for (size_t i = 0;
1170dcc57e24Smartin 		    i < edit->pset->parts->pscheme->custom_attribute_count;
1171dcc57e24Smartin 		    i++) {
1172dcc57e24Smartin 			attrname = msg_string(
1173dcc57e24Smartin 			    edit->pset->parts->pscheme->custom_attributes[i]
1174dcc57e24Smartin 			    .label);
1175dcc57e24Smartin 			l = strlen(attrname);
1176dcc57e24Smartin 			if (l > col_width) col_width = l;
117726165e63Sdholland 		}
117826165e63Sdholland 
1179dcc57e24Smartin 		col_width += 3;
1180dcc57e24Smartin 	}
118126165e63Sdholland 
118226165e63Sdholland 	if (m->opts[opt].opt_flags & OPT_IGNORE
1183dcc57e24Smartin 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
1184e269f57bSmartin 	    && m->opts[opt].opt_action != edit_ptn_custom_type
1185e269f57bSmartin 	    && m->opts[opt].opt_action != edit_fs_type
1186e269f57bSmartin 	    && m->opts[opt].opt_action != edit_fs_start
1187e269f57bSmartin 	    && m->opts[opt].opt_action != edit_fs_size) {
1188dcc57e24Smartin 		wprintw(m->mw, "%*s -", col_width, "");
118926165e63Sdholland 		return;
119026165e63Sdholland 	}
119126165e63Sdholland 
1192dcc57e24Smartin 	if (opt < 4) {
119326165e63Sdholland 		switch (opt) {
1194dcc57e24Smartin 		case 0:
1195ca67d538Smartin 			if (edit->info.fs_type == FS_BSDFFS) {
1196e2c1461cSmartin 				if (edit->info.fs_sub_type == 3)
1197e2c1461cSmartin 					c = msg_string(MSG_fs_type_ffsv2ea);
1198e2c1461cSmartin 				else if (edit->info.fs_sub_type == 2)
1199dcc57e24Smartin 					c = msg_string(MSG_fs_type_ffsv2);
120026165e63Sdholland 				else
1201dcc57e24Smartin 					c = msg_string(MSG_fs_type_ffs);
1202ca67d538Smartin 			} else {
1203dcc57e24Smartin 				c = getfslabelname(edit->info.fs_type,
1204dcc57e24Smartin 				    edit->info.fs_sub_type);
1205ca67d538Smartin 			}
1206dcc57e24Smartin 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
1207dcc57e24Smartin 			return;
1208dcc57e24Smartin 		case 1:
1209dcc57e24Smartin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1210dcc57e24Smartin 			    ptn_start, edit->info.start / sizemult, multname);
1211dcc57e24Smartin 			return;
1212dcc57e24Smartin 		case 2:
1213dcc57e24Smartin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1214dcc57e24Smartin 			    ptn_size, edit->info.size / sizemult, multname);
1215dcc57e24Smartin 			return;
1216dcc57e24Smartin 		case 3:
1217dcc57e24Smartin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1218dcc57e24Smartin 			    ptn_end, (edit->info.start + edit->info.size)
1219dcc57e24Smartin 			    / sizemult, multname);
1220dcc57e24Smartin 			return;
122126165e63Sdholland 		 }
122226165e63Sdholland 	}
1223dcc57e24Smartin 	if (m->opts[opt].opt_action == edit_install) {
1224dcc57e24Smartin 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1225bf7de24aSmartin 			msg_string((edit->info.flags & PTI_INSTALL_TARGET)
1226dcc57e24Smartin 			    ? MSG_Yes : MSG_No));
1227dcc57e24Smartin 		return;
1228dcc57e24Smartin 	}
1229dcc57e24Smartin 	if (m->opts[opt].opt_action == edit_fs_preserve) {
1230dcc57e24Smartin 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1231dcc57e24Smartin 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
1232dcc57e24Smartin 			    ? MSG_Yes : MSG_No));
1233dcc57e24Smartin 		return;
1234dcc57e24Smartin 	}
1235dcc57e24Smartin 	if (m->opts[opt].opt_action == edit_fs_mount) {
1236dcc57e24Smartin 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1237dcc57e24Smartin 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
1238dcc57e24Smartin 			    ? MSG_Yes : MSG_No));
1239dcc57e24Smartin 		return;
1240dcc57e24Smartin 	}
1241b162b00bSmartin 	if (m->opts[opt].opt_action == edit_fs_ffs_block) {
1242b162b00bSmartin 		wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
1243b162b00bSmartin 			edit->wanted->fs_opt1);
1244b162b00bSmartin 		return;
1245b162b00bSmartin 	}
1246b162b00bSmartin 	if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
1247b162b00bSmartin 		wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
1248b162b00bSmartin 			edit->wanted->fs_opt2);
1249b162b00bSmartin 		return;
1250b162b00bSmartin 	}
1251b162b00bSmartin 	if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
1252b162b00bSmartin 		if (edit->wanted->fs_opt3 == 0)
1253b162b00bSmartin 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1254b162b00bSmartin 				msg_string(MSG_ptn_isize_dflt));
1255b162b00bSmartin 		else {
1256b162b00bSmartin         	        char buf[24], *line;
1257b162b00bSmartin 			const char *t = buf;
1258b162b00bSmartin 
1259b162b00bSmartin 			snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
1260b162b00bSmartin 			line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
1261b162b00bSmartin 			    1, &t);
1262b162b00bSmartin 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1263b162b00bSmartin 				line);
1264b162b00bSmartin 			free(line);
1265b162b00bSmartin 		}
1266b162b00bSmartin 		return;
1267b162b00bSmartin 	}
1268dcc57e24Smartin 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
1269dcc57e24Smartin 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1270dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
1271dcc57e24Smartin 			wprintw(m->mw, "async ");
1272dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
1273dcc57e24Smartin 			wprintw(m->mw, "noatime ");
1274dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NODEV)
1275dcc57e24Smartin 			wprintw(m->mw, "nodev ");
1276dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1277dcc57e24Smartin 			wprintw(m->mw, "nodevmtime ");
1278dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1279dcc57e24Smartin 			wprintw(m->mw, "noexec ");
1280dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
1281dcc57e24Smartin 			wprintw(m->mw, "nosuid ");
1282dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_LOG)
1283dcc57e24Smartin 			wprintw(m->mw, "log ");
1284dcc57e24Smartin 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1285dcc57e24Smartin 			wprintw(m->mw, "noauto  ");
1286dcc57e24Smartin 		return;
1287dcc57e24Smartin 	}
1288dcc57e24Smartin 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
1289dcc57e24Smartin 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1290dcc57e24Smartin 		    edit->wanted->mount);
1291dcc57e24Smartin 		return;
1292dcc57e24Smartin 	}
1293dcc57e24Smartin 
1294dcc57e24Smartin 	attr_no = opt - edit->first_custom_attr;
1295dcc57e24Smartin 	edit->pset->parts->pscheme->format_custom_attribute(
1296dcc57e24Smartin 	    edit->pset->parts, edit->id, attr_no, &edit->info,
1297dcc57e24Smartin 	    val, sizeof val);
1298dcc57e24Smartin 	attrname = msg_string(edit->pset->parts->pscheme->
1299dcc57e24Smartin 	    custom_attributes[attr_no].label);
1300dcc57e24Smartin 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1301dcc57e24Smartin }
130226165e63Sdholland 
130326165e63Sdholland static int
edit_ptn_custom_type(menudesc * m,void * arg)1304dcc57e24Smartin edit_ptn_custom_type(menudesc *m, void *arg)
130526165e63Sdholland {
1306dcc57e24Smartin 	struct single_part_fs_edit *edit = arg;
1307dcc57e24Smartin 	size_t attr_no = m->cursel - edit->first_custom_attr;
1308dcc57e24Smartin 	char line[STRSIZE];
130926165e63Sdholland 
1310dcc57e24Smartin 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1311dcc57e24Smartin 	case pet_bool:
1312dcc57e24Smartin 		edit->pset->parts->pscheme->custom_attribute_toggle(
1313dcc57e24Smartin 		    edit->pset->parts, edit->id, attr_no);
1314dcc57e24Smartin 		break;
1315dcc57e24Smartin 	case pet_cardinal:
1316dcc57e24Smartin 	case pet_str:
1317dcc57e24Smartin 		edit->pset->parts->pscheme->format_custom_attribute(
1318dcc57e24Smartin 		    edit->pset->parts, edit->id, attr_no, &edit->info,
1319dcc57e24Smartin 		    line, sizeof(line));
1320dcc57e24Smartin 		msg_prompt_win(
1321dcc57e24Smartin 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
1322dcc57e24Smartin 		    label, -1, 18, 0, 0, line, line, sizeof(line));
1323dcc57e24Smartin 		edit->pset->parts->pscheme->custom_attribute_set_str(
1324dcc57e24Smartin 		    edit->pset->parts, edit->id, attr_no, line);
1325dcc57e24Smartin 		break;
1326dcc57e24Smartin 	}
1327dcc57e24Smartin 
132826165e63Sdholland 	return 0;
132926165e63Sdholland }
133026165e63Sdholland 
1331dcc57e24Smartin 
1332dcc57e24Smartin /*
1333dcc57e24Smartin  * Some column width depend on translation, we will set these in
1334dcc57e24Smartin  * fmt_fspart_header and later use it when formatting single entries
1335dcc57e24Smartin  * in fmt_fspart_row.
1336dcc57e24Smartin  * The table consist of 3 "size like" columns, all fixed width, then
1337dcc57e24Smartin  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1338dcc57e24Smartin  * (which is variable width).
1339dcc57e24Smartin  */
1340dcc57e24Smartin static int fstype_width, flags_width;
1341dcc57e24Smartin static char fspart_separator[MENUSTRSIZE];
1342dcc57e24Smartin static char fspart_title[2*MENUSTRSIZE];
1343dcc57e24Smartin 
1344dcc57e24Smartin /*
1345dcc57e24Smartin  * Format the header of the main partition editor menu.
1346dcc57e24Smartin  */
134726165e63Sdholland static void
fmt_fspart_header(menudesc * menu,void * arg)1348dcc57e24Smartin fmt_fspart_header(menudesc *menu, void *arg)
134926165e63Sdholland {
1350dcc57e24Smartin 	struct partition_usage_set *pset = arg;
1351dcc57e24Smartin 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
13528ac26f1aSmartin 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
1353dcc57e24Smartin 	const char *fstype, *flags;
1354dcc57e24Smartin 	int i;
13558ac26f1aSmartin 	size_t ptn;
13568ac26f1aSmartin 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
135726165e63Sdholland 
13588ac26f1aSmartin 	with_clone = false;
13598ac26f1aSmartin 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
13608ac26f1aSmartin 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
13618ac26f1aSmartin 			with_clone = true;
1362dcc57e24Smartin 	humanize_number(total, sizeof total,
13630ea03d33Smartin 	    pset->parts->disk_size * pset->parts->bytes_per_sector,
1364dcc57e24Smartin 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1365dcc57e24Smartin 	humanize_number(free_space, sizeof free_space,
13660ea03d33Smartin 	    pset->cur_free_space * pset->parts->bytes_per_sector,
1367dcc57e24Smartin 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
136826165e63Sdholland 
13698ac26f1aSmartin 	if (with_clone)
13708ac26f1aSmartin 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
13718ac26f1aSmartin 	else
13728ac26f1aSmartin 		desc[0] = 0;
13738ac26f1aSmartin 	if (pset->parts->pscheme->part_flag_desc)
13748ac26f1aSmartin 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
13758ac26f1aSmartin 		sizeof desc);
13768ac26f1aSmartin 
1377dcc57e24Smartin 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1378dcc57e24Smartin 	    msg_string(pset->parts->pscheme->name),
1379dcc57e24Smartin 	    msg_string(pset->parts->pscheme->short_name),
1380dcc57e24Smartin 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
13818ac26f1aSmartin 	    desc, total, free_space);
1382dcc57e24Smartin 
1383dcc57e24Smartin 	snprintf(scol, sizeof scol, "%s (%s)",
1384dcc57e24Smartin 	    msg_string(MSG_ptnheaders_start), multname);
1385dcc57e24Smartin 	snprintf(ecol, sizeof ecol, "%s (%s)",
1386dcc57e24Smartin 	    msg_string(MSG_ptnheaders_end), multname);
1387dcc57e24Smartin 	snprintf(szcol, sizeof szcol, "%s (%s)",
1388dcc57e24Smartin 	    msg_string(MSG_ptnheaders_size), multname);
1389dcc57e24Smartin 
1390dcc57e24Smartin 	fstype = msg_string(MSG_ptnheaders_fstype);
1391dcc57e24Smartin 	flags = msg_string(MSG_part_header_col_flag);
1392dcc57e24Smartin 	fstype_width = max(strlen(fstype), 8);
1393dcc57e24Smartin 	flags_width = strlen(flags);
1394dcc57e24Smartin 	for (i = 0, p = sepline; i < fstype_width; i++)
1395dcc57e24Smartin 		*p++ = '-';
1396dcc57e24Smartin 	for (i = 0, *p++ = ' '; i < flags_width; i++)
1397dcc57e24Smartin 		*p++ = '-';
1398dcc57e24Smartin 	*p = 0;
1399dcc57e24Smartin 
1400dcc57e24Smartin 	snprintf(fspart_separator, sizeof(fspart_separator),
1401dcc57e24Smartin 	    "------------ ------------ ------------ %s ----------------",
1402dcc57e24Smartin 	    sepline);
1403dcc57e24Smartin 
1404dcc57e24Smartin 	snprintf(fspart_title, sizeof(fspart_title),
1405dcc57e24Smartin 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
1406dcc57e24Smartin 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
1407dcc57e24Smartin 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1408dcc57e24Smartin 	    fspart_separator);
1409dcc57e24Smartin 
1410dcc57e24Smartin 	msg_table_add("\n\n");
141126165e63Sdholland }
141226165e63Sdholland 
1413dcc57e24Smartin /*
1414dcc57e24Smartin  * Format one partition entry in the main partition editor.
1415dcc57e24Smartin  */
1416dcc57e24Smartin static void
fmt_fspart_row(menudesc * m,int ptn,void * arg)1417dcc57e24Smartin fmt_fspart_row(menudesc *m, int ptn, void *arg)
1418dcc57e24Smartin {
1419dcc57e24Smartin 	struct partition_usage_set *pset = arg;
1420dcc57e24Smartin 	struct disk_part_info info;
1421dcc57e24Smartin 	daddr_t poffset, psize, pend;
1422dcc57e24Smartin 	const char *desc;
1423dcc57e24Smartin 	static const char *Yes;
1424dcc57e24Smartin 	char flag_str[MENUSTRSIZE], *fp;
1425dcc57e24Smartin 	unsigned inst_flags;
14264cde5629Smartin #ifndef NO_CLONES
14278ac26f1aSmartin 	size_t clone_cnt;
14284cde5629Smartin #endif
1429dcc57e24Smartin 	bool with_inst_flag = pset->parts->parent == NULL;
1430dcc57e24Smartin 
1431dcc57e24Smartin 	if (Yes == NULL)
1432dcc57e24Smartin 		Yes = msg_string(MSG_Yes);
1433dcc57e24Smartin 
14344cde5629Smartin #ifndef NO_CLONES
14358ac26f1aSmartin 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
14368ac26f1aSmartin 	   pset->infos[ptn].cur_part_id == NO_PART) {
14378ac26f1aSmartin 		psize = pset->infos[ptn].size / sizemult;
14388ac26f1aSmartin 		if (pset->infos[ptn].clone_ndx <
14398ac26f1aSmartin 		    pset->infos[ptn].clone_src->num_sel)
14408ac26f1aSmartin 			clone_cnt = 1;
14418ac26f1aSmartin 		else
14428ac26f1aSmartin 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
14438ac26f1aSmartin 		if (pset->infos[ptn].cur_part_id == NO_PART)
14448ac26f1aSmartin 			wprintw(m->mw, "                          %12" PRIu64
14458ac26f1aSmartin 			    " [%zu %s]", psize, clone_cnt,
14468ac26f1aSmartin 			    msg_string(MSG_clone_target_disp));
14478ac26f1aSmartin 		else {
14488ac26f1aSmartin 			poffset = pset->infos[ptn].cur_start / sizemult;
14498ac26f1aSmartin 			pend = (pset->infos[ptn].cur_start +
14508ac26f1aSmartin 			    pset->infos[ptn].size) / sizemult - 1;
14518ac26f1aSmartin 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
14528ac26f1aSmartin 			    " [%zu %s]",
14538ac26f1aSmartin 			    poffset, pend, psize, clone_cnt,
14548ac26f1aSmartin 			    msg_string(MSG_clone_target_disp));
14558ac26f1aSmartin 		}
14568ac26f1aSmartin 		if (m->title == fspart_title)
14578ac26f1aSmartin 			m->opts[ptn].opt_flags |= OPT_IGNORE;
14588ac26f1aSmartin 		else
14598ac26f1aSmartin 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
14608ac26f1aSmartin 		return;
14618ac26f1aSmartin 	}
14624cde5629Smartin #endif
14638ac26f1aSmartin 
1464dcc57e24Smartin 	if (!real_partition(pset, ptn))
1465dcc57e24Smartin 		return;
1466dcc57e24Smartin 
1467dcc57e24Smartin 	if (!pset->parts->pscheme->get_part_info(pset->parts,
1468dcc57e24Smartin 	    pset->infos[ptn].cur_part_id, &info))
1469dcc57e24Smartin 		return;
1470dcc57e24Smartin 
14718ac26f1aSmartin 	/*
14728ac26f1aSmartin 	 * We use this function in multiple menus, but only want it
14738ac26f1aSmartin 	 * to play with enable/disable in a single one:
14748ac26f1aSmartin 	 */
14758ac26f1aSmartin 	if (m->title == fspart_title) {
14768ac26f1aSmartin 		/*
14778ac26f1aSmartin 		 * Enable / disable this line if it is something
14788ac26f1aSmartin 		 * like RAW_PART
14798ac26f1aSmartin 		 */
14808ac26f1aSmartin 		if ((info.flags &
14818ac26f1aSmartin 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
14828ac26f1aSmartin 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
1483dcc57e24Smartin 			m->opts[ptn].opt_flags |= OPT_IGNORE;
1484dcc57e24Smartin 		else
1485dcc57e24Smartin 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
14868ac26f1aSmartin 	}
1487dcc57e24Smartin 
1488dcc57e24Smartin 	poffset = info.start / sizemult;
1489dcc57e24Smartin 	psize = info.size / sizemult;
1490dcc57e24Smartin 	if (psize == 0)
1491dcc57e24Smartin 		pend = 0;
1492dcc57e24Smartin 	else
1493dcc57e24Smartin 		pend = (info.start + info.size) / sizemult - 1;
1494dcc57e24Smartin 
1495dcc57e24Smartin 	if (info.flags & PTI_WHOLE_DISK)
1496dcc57e24Smartin 		desc = msg_string(MSG_NetBSD_partition_cant_change);
1497dcc57e24Smartin 	else if (info.flags & PTI_RAW_PART)
1498dcc57e24Smartin 		desc = msg_string(MSG_Whole_disk_cant_change);
1499dcc57e24Smartin 	else if (info.flags & PTI_BOOT)
1500dcc57e24Smartin 		desc = msg_string(MSG_Boot_partition_cant_change);
1501dcc57e24Smartin 	else
1502dcc57e24Smartin 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
1503dcc57e24Smartin 
1504dcc57e24Smartin 	fp = flag_str;
1505dcc57e24Smartin 	inst_flags = pset->infos[ptn].instflags;
1506bf7de24aSmartin 	if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
1507dcc57e24Smartin 	    info.nat_type->generic_ptype == PT_root) {
1508dcc57e24Smartin 		static char inst_flag;
1509dcc57e24Smartin 
1510dcc57e24Smartin 		if (inst_flag == 0)
1511dcc57e24Smartin 			inst_flag = msg_string(MSG_install_flag)[0];
1512dcc57e24Smartin 		*fp++ = inst_flag;
1513dcc57e24Smartin 	}
1514dcc57e24Smartin 	if (inst_flags & PUIINST_NEWFS)
1515dcc57e24Smartin 		*fp++ = msg_string(MSG_newfs_flag)[0];
15168ac26f1aSmartin 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
15178ac26f1aSmartin 		*fp++ = msg_string(MSG_clone_flag)[0];
1518dcc57e24Smartin 	*fp = 0;
1519dcc57e24Smartin 	if (pset->parts->pscheme->get_part_attr_str != NULL)
1520dcc57e24Smartin 		pset->parts->pscheme->get_part_attr_str(pset->parts,
1521cf40b1abSmartin 		    pset->infos[ptn].cur_part_id, fp,
1522cf40b1abSmartin 		    sizeof(flag_str)-1-(fp-flag_str));
1523dcc57e24Smartin 
1524dcc57e24Smartin 	/* if the fstype description does not fit, check if we can overrun */
1525dcc57e24Smartin 	if (strlen(desc) > (size_t)fstype_width &&
1526dcc57e24Smartin 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
1527dcc57e24Smartin 	     info.last_mounted[0] == 0))
1528dcc57e24Smartin 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1529dcc57e24Smartin 		    " %s",
1530dcc57e24Smartin 		    poffset, pend, psize, desc);
1531dcc57e24Smartin 	else
1532dcc57e24Smartin 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1533dcc57e24Smartin 		    " %*.*s %*s %s",
1534dcc57e24Smartin 		    poffset, pend, psize, fstype_width, fstype_width, desc,
1535dcc57e24Smartin 		    -flags_width, flag_str,
1536dcc57e24Smartin 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1537dcc57e24Smartin 		     info.last_mounted[0] ? info.last_mounted : "");
153826165e63Sdholland }
153926165e63Sdholland 
15404cde5629Smartin #ifndef NO_CLONES
1541dcc57e24Smartin static int
part_ext_clone(menudesc * m,void * arg)15428ac26f1aSmartin part_ext_clone(menudesc *m, void *arg)
15438ac26f1aSmartin {
15448ac26f1aSmartin 	struct selected_partitions selected, *clone_src;
15458ac26f1aSmartin 	struct clone_target_menu_data data;
15468ac26f1aSmartin 	struct partition_usage_set *pset = arg;
15478ac26f1aSmartin 	struct part_usage_info *p;
15488ac26f1aSmartin 	struct disk_part_info sinfo, cinfo;
15498ac26f1aSmartin 	struct disk_partitions *csrc;
15508ac26f1aSmartin 	struct disk_part_free_space space;
15518ac26f1aSmartin 	menu_ent *men;
15528ac26f1aSmartin 	daddr_t clone_size, free_size, offset, align;
15538ac26f1aSmartin 	int num_men, i;
15548ac26f1aSmartin 	size_t s, clone_cnt;
15558ac26f1aSmartin 	part_id cid;
15568ac26f1aSmartin 	struct clone_data {
15578ac26f1aSmartin 		struct disk_part_info info;
15588ac26f1aSmartin 		part_id new_id;
15598ac26f1aSmartin 		size_t ndx;
15608ac26f1aSmartin 	};
15618ac26f1aSmartin 	struct clone_data *clones = NULL;
15628ac26f1aSmartin 
15638ac26f1aSmartin 	if (!select_partitions(&selected, pm->parts))
15648ac26f1aSmartin 		return 0;
15658ac26f1aSmartin 
15668ac26f1aSmartin 	clone_size = selected_parts_size(&selected);
15678ac26f1aSmartin 	num_men = pset->num+1;
15688ac26f1aSmartin 	men = calloc(num_men, sizeof *men);
15698ac26f1aSmartin 	if (men == NULL)
15708ac26f1aSmartin 		return 0;
15718ac26f1aSmartin 	for (i = 0; i < num_men; i++) {
15728ac26f1aSmartin 		men[i].opt_action = clone_target_select;
15738ac26f1aSmartin 		if (i == 0)
15748ac26f1aSmartin 			free_size = pset->infos[i].cur_start;
15758ac26f1aSmartin 		else if (i > 0 && (size_t)i < pset->num)
15768ac26f1aSmartin 			free_size = pset->infos[i].cur_start -
15778ac26f1aSmartin 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
15788ac26f1aSmartin 		else
15798ac26f1aSmartin 			free_size = pset->parts->free_space;
15808ac26f1aSmartin 		if (free_size < clone_size)
15818ac26f1aSmartin 			men[i].opt_flags = OPT_IGNORE;
15828ac26f1aSmartin 	}
15838ac26f1aSmartin 	men[num_men-1].opt_name = MSG_clone_target_end;
15848ac26f1aSmartin 
15858ac26f1aSmartin 	memset(&data, 0, sizeof data);
15868ac26f1aSmartin 	data.usage = *pset;
15878ac26f1aSmartin 	data.res = -1;
15888ac26f1aSmartin 
15898ac26f1aSmartin 	data.usage.menu = new_menu(MSG_clone_target_hdr,
15908ac26f1aSmartin 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
15918ac26f1aSmartin 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
15928ac26f1aSmartin 	process_menu(data.usage.menu, &data);
15938ac26f1aSmartin 	free_menu(data.usage.menu);
15948ac26f1aSmartin 	free(men);
15958ac26f1aSmartin 
15968ac26f1aSmartin 	if (data.res < 0)
15978ac26f1aSmartin 		goto err;
15988ac26f1aSmartin 
15998ac26f1aSmartin 	/* create temporary infos for all clones that work out */
16008ac26f1aSmartin 	clone_cnt = 0;
16018ac26f1aSmartin 	clones = calloc(selected.num_sel, sizeof(*clones));
16028ac26f1aSmartin 	if (clones == NULL)
16038ac26f1aSmartin 		goto err;
16048ac26f1aSmartin 
16058ac26f1aSmartin 	clone_src = malloc(sizeof(selected));
16068ac26f1aSmartin 	if (clone_src == NULL)
16078ac26f1aSmartin 		goto err;
16088ac26f1aSmartin 	*clone_src = selected;
16098ac26f1aSmartin 
16108ac26f1aSmartin 	/* find selected offset from data.res and insert clones there */
16118ac26f1aSmartin 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
16128ac26f1aSmartin 	offset = -1;
16138ac26f1aSmartin 	if (data.res > 0)
16148ac26f1aSmartin 		offset = pset->infos[data.res-1].cur_start
16158ac26f1aSmartin 		    + pset->infos[data.res-1].size;
16168ac26f1aSmartin 	else
16178ac26f1aSmartin 		offset = 0;
16188ac26f1aSmartin 	for (s = 0; s < selected.num_sel; s++) {
16198ac26f1aSmartin 		csrc = selected.selection[s].parts;
16208ac26f1aSmartin 		cid = selected.selection[s].id;
16218ac26f1aSmartin 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
16228ac26f1aSmartin 		if (!pset->parts->pscheme->adapt_foreign_part_info(
16238ac26f1aSmartin 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
16248ac26f1aSmartin 			continue;
16258ac26f1aSmartin 		size_t cnt = pset->parts->pscheme->get_free_spaces(
16268ac26f1aSmartin 		    pset->parts, &space, 1, cinfo.size-align, align,
16278ac26f1aSmartin 		    offset, -1);
16288ac26f1aSmartin 		if (cnt == 0)
16298ac26f1aSmartin 			continue;
16308ac26f1aSmartin 		cinfo.start = space.start;
16318ac26f1aSmartin 		cid = pset->parts->pscheme->add_partition(
16328ac26f1aSmartin 		    pset->parts, &cinfo, NULL);
16338ac26f1aSmartin 		if (cid == NO_PART)
16348ac26f1aSmartin 			continue;
16358ac26f1aSmartin 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
16368ac26f1aSmartin 		clones[clone_cnt].info = cinfo;
16378ac26f1aSmartin 		clones[clone_cnt].new_id = cid;
16388ac26f1aSmartin 		clones[clone_cnt].ndx = s;
16398ac26f1aSmartin 		clone_cnt++;
1640dc7680deSmartin 		offset = roundup(cinfo.start+cinfo.size, align);
16418ac26f1aSmartin 	}
16428ac26f1aSmartin 
16438ac26f1aSmartin 	/* insert new clone records at offset data.res */
16448ac26f1aSmartin 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
16458ac26f1aSmartin 	if (men == NULL)
16468ac26f1aSmartin 		goto err;
16478ac26f1aSmartin 	pset->menu_opts = men;
16488ac26f1aSmartin 	m->opts = men;
16498ac26f1aSmartin 	m->numopts += clone_cnt;
16508ac26f1aSmartin 
16518ac26f1aSmartin 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
16528ac26f1aSmartin 	if (p == NULL)
16538ac26f1aSmartin 		goto err;
16548ac26f1aSmartin 	pset->infos = p;
16558ac26f1aSmartin 
16568ac26f1aSmartin 	men += data.res;
16578ac26f1aSmartin 	p += data.res;
16588ac26f1aSmartin 	memmove(men+clone_cnt, men,
16598ac26f1aSmartin 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
16608ac26f1aSmartin 	if (pset->num > (size_t)data.res)
16618ac26f1aSmartin 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
16628ac26f1aSmartin 	memset(men, 0, sizeof(*men)*clone_cnt);
16638ac26f1aSmartin 	memset(p, 0, sizeof(*p)*clone_cnt);
16648ac26f1aSmartin 	for (s = 0; s < clone_cnt; s++) {
16658ac26f1aSmartin 		p[s].cur_part_id = clones[s].new_id;
16668ac26f1aSmartin 		p[s].cur_start = clones[s].info.start;
16678ac26f1aSmartin 		p[s].size = clones[s].info.size;
16688ac26f1aSmartin 		p[s].cur_flags = clones[s].info.flags;
16698ac26f1aSmartin 		p[s].flags = PUIFLG_CLONE_PARTS;
16708ac26f1aSmartin 		p[s].parts = pset->parts;
16718ac26f1aSmartin 		p[s].clone_src = clone_src;
16728ac26f1aSmartin 		p[s].clone_ndx = s;
16738ac26f1aSmartin 	}
16748ac26f1aSmartin 	free(clones);
16758ac26f1aSmartin 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
16768ac26f1aSmartin 	pset->num += clone_cnt;
16778ac26f1aSmartin 	m->h = 0;
16788ac26f1aSmartin 	resize_menu_height(m);
16798ac26f1aSmartin 
16808ac26f1aSmartin 	return -1;
16818ac26f1aSmartin 
16828ac26f1aSmartin err:
16838ac26f1aSmartin 	free(clones);
16848ac26f1aSmartin 	free_selected_partitions(&selected);
16858ac26f1aSmartin 	return 0;
16868ac26f1aSmartin }
16874cde5629Smartin #endif
16888ac26f1aSmartin 
16898ac26f1aSmartin static int
edit_fspart_pack(menudesc * m,void * arg)1690dcc57e24Smartin edit_fspart_pack(menudesc *m, void *arg)
1691dcc57e24Smartin {
1692dcc57e24Smartin 	struct partition_usage_set *pset = arg;
1693dcc57e24Smartin 	char buf[STRSIZE];
169426165e63Sdholland 
1695dcc57e24Smartin 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1696dcc57e24Smartin 	    buf, sizeof buf))
1697dcc57e24Smartin 		return 0;
1698dcc57e24Smartin 
1699dcc57e24Smartin 	msg_prompt_win(MSG_edit_disk_pack_hdr,
1700dcc57e24Smartin 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
1701dcc57e24Smartin 
1702dcc57e24Smartin 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1703dcc57e24Smartin 	return 0;
1704dcc57e24Smartin }
1705dcc57e24Smartin 
1706dcc57e24Smartin static int
edit_fspart_add(menudesc * m,void * arg)1707dcc57e24Smartin edit_fspart_add(menudesc *m, void *arg)
1708dcc57e24Smartin {
1709dcc57e24Smartin 	struct partition_usage_set *pset = arg;
1710dcc57e24Smartin 	struct part_usage_info *ninfo;
1711dcc57e24Smartin 	menu_ent *nmenopts;
1712dcc57e24Smartin 	size_t cnt, off;
1713dcc57e24Smartin 
1714dcc57e24Smartin 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1715dcc57e24Smartin 	if (ninfo == NULL)
1716dcc57e24Smartin 		return 0;
1717dcc57e24Smartin 	pset->infos = ninfo;
1718dcc57e24Smartin 	off = pset->parts->num_part;
1719dcc57e24Smartin 	cnt = pset->num-pset->parts->num_part;
1720dcc57e24Smartin 	if (cnt > 0)
1721dcc57e24Smartin 		memmove(pset->infos+off+1,pset->infos+off,
1722dcc57e24Smartin 		    cnt*sizeof(*pset->infos));
1723dcc57e24Smartin 	memset(pset->infos+off, 0, sizeof(*pset->infos));
1724dcc57e24Smartin 
1725dcc57e24Smartin 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1726dcc57e24Smartin 	if (nmenopts == NULL)
1727dcc57e24Smartin 		return 0;
1728dcc57e24Smartin 	memmove(nmenopts+off+1, nmenopts+off,
1729dcc57e24Smartin 	    (m->numopts-off)*sizeof(*nmenopts));
1730dcc57e24Smartin 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1731dcc57e24Smartin 	nmenopts[off].opt_action = edit_ptn;
1732dcc57e24Smartin 	pset->menu_opts = m->opts = nmenopts;
1733dcc57e24Smartin 	m->numopts++;
1734dcc57e24Smartin 	m->cursel = off;
1735dcc57e24Smartin 	pset->num++;
1736dcc57e24Smartin 
1737dcc57e24Smartin 	/* now update edit menu to fit */
1738dcc57e24Smartin 	m->h = 0;
1739dcc57e24Smartin 	resize_menu_height(m);
1740dcc57e24Smartin 
1741dcc57e24Smartin 	/* and directly invoke the partition editor for the new part */
1742dcc57e24Smartin 	edit_ptn(m, arg);
1743dcc57e24Smartin 
1744dcc57e24Smartin 	show_partition_adder(m, pset);
1745dcc57e24Smartin 
1746dcc57e24Smartin 	return -1;
1747dcc57e24Smartin }
1748dcc57e24Smartin 
1749dcc57e24Smartin static void
add_partition_adder(menudesc * m,struct partition_usage_set * pset)1750dcc57e24Smartin add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1751dcc57e24Smartin {
1752dcc57e24Smartin 	struct part_usage_info *ninfo;
1753dcc57e24Smartin 	menu_ent *nmenopts;
1754dcc57e24Smartin 	size_t off;
1755dcc57e24Smartin 
1756dcc57e24Smartin 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1757dcc57e24Smartin 	if (ninfo == NULL)
1758dcc57e24Smartin 		return;
1759dcc57e24Smartin 	pset->infos = ninfo;
1760dcc57e24Smartin 	off = pset->parts->num_part+1;
1761dcc57e24Smartin 
1762dcc57e24Smartin 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1763dcc57e24Smartin 	if (nmenopts == NULL)
1764dcc57e24Smartin 		return;
1765dcc57e24Smartin 	memmove(nmenopts+off+1, nmenopts+off,
1766dcc57e24Smartin 	    (m->numopts-off)*sizeof(*nmenopts));
1767dcc57e24Smartin 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1768dcc57e24Smartin 
1769dcc57e24Smartin 	nmenopts[off].opt_name = MSG_addpart;
1770dcc57e24Smartin 	nmenopts[off].opt_flags = OPT_SUB;
1771dcc57e24Smartin 	nmenopts[off].opt_action = edit_fspart_add;
1772dcc57e24Smartin 
1773dcc57e24Smartin 	m->opts = nmenopts;
1774dcc57e24Smartin 	m->numopts++;
1775dcc57e24Smartin }
1776dcc57e24Smartin 
1777dcc57e24Smartin static void
remove_partition_adder(menudesc * m,struct partition_usage_set * pset)1778dcc57e24Smartin remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1779dcc57e24Smartin {
1780dcc57e24Smartin 	size_t off;
1781dcc57e24Smartin 
1782dcc57e24Smartin 	off = pset->parts->num_part+1;
1783dcc57e24Smartin 	memmove(m->opts+off, m->opts+off+1,
1784dcc57e24Smartin 	    (m->numopts-off-1)*sizeof(*m->opts));
1785dcc57e24Smartin 	m->numopts--;
1786dcc57e24Smartin }
1787dcc57e24Smartin 
1788dcc57e24Smartin /*
1789dcc57e24Smartin  * Called whenever the "add a partition" option may need to be removed
1790dcc57e24Smartin  * or added
1791dcc57e24Smartin  */
1792dcc57e24Smartin static void
show_partition_adder(menudesc * m,struct partition_usage_set * pset)1793dcc57e24Smartin show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1794dcc57e24Smartin {
179534f3e937Smartin 	if (m->opts == NULL)
179634f3e937Smartin 		return;
179734f3e937Smartin 
1798dcc57e24Smartin 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
1799dcc57e24Smartin 	    pset->parts);
1800dcc57e24Smartin 	bool part_adder_present =
1801dcc57e24Smartin 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1802dcc57e24Smartin 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1803dcc57e24Smartin 
1804dcc57e24Smartin 	if (can_add_partition == part_adder_present)
1805dcc57e24Smartin 		return;
1806dcc57e24Smartin 
1807dcc57e24Smartin 	if (can_add_partition)
1808dcc57e24Smartin 		add_partition_adder(m, pset);
1809dcc57e24Smartin 	else
1810dcc57e24Smartin 		remove_partition_adder(m, pset);
1811dcc57e24Smartin 
1812dcc57e24Smartin 	/* now update edit menu to fit */
1813dcc57e24Smartin 	m->h = 0;
1814dcc57e24Smartin 	resize_menu_height(m);
1815dcc57e24Smartin }
1816dcc57e24Smartin 
1817dcc57e24Smartin static int
edit_fspart_abort(menudesc * m,void * arg)1818dcc57e24Smartin edit_fspart_abort(menudesc *m, void *arg)
1819dcc57e24Smartin {
1820dcc57e24Smartin 	struct partition_usage_set *pset = arg;
1821dcc57e24Smartin 
1822dcc57e24Smartin 	pset->ok = false;
1823dcc57e24Smartin 	return 1;
182426165e63Sdholland }
182526165e63Sdholland 
182626165e63Sdholland /*
182726165e63Sdholland  * Check a disklabel.
182826165e63Sdholland  * If there are overlapping active partitions,
182926165e63Sdholland  * Ask the user if they want to edit the partition or give up.
183026165e63Sdholland  */
183126165e63Sdholland int
edit_and_check_label(struct pm_devs * p,struct partition_usage_set * pset,bool install)183234f3e937Smartin edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
183334f3e937Smartin     bool install)
183426165e63Sdholland {
1835dcc57e24Smartin 	menu_ent *op;
1836dcc57e24Smartin 	size_t cnt, i;
1837dcc57e24Smartin 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1838dcc57e24Smartin 	bool may_edit_pack =
1839dcc57e24Smartin 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
1840dcc57e24Smartin 	    pset->parts->pscheme->set_disk_pack_name != NULL;
184126165e63Sdholland 
18424cde5629Smartin #ifdef NO_CLONES
18434cde5629Smartin #define	C_M_ITEMS	0
18444cde5629Smartin #else
18454cde5629Smartin #define	C_M_ITEMS	1
18464cde5629Smartin #endif
1847dcc57e24Smartin 	pset->menu_opts = calloc(pset->parts->num_part
18484cde5629Smartin 	     +3+C_M_ITEMS+may_add+may_edit_pack,
1849dcc57e24Smartin 	     sizeof *pset->menu_opts);
1850dcc57e24Smartin 	if (pset->menu_opts == NULL)
1851dcc57e24Smartin 		return 0;
1852dcc57e24Smartin 
1853dcc57e24Smartin 	op = pset->menu_opts;
1854dcc57e24Smartin 	for (i = 0; i < pset->parts->num_part; i++) {
1855dcc57e24Smartin 		op->opt_action = edit_ptn;
1856dcc57e24Smartin 		op++;
1857dcc57e24Smartin 	}
1858dcc57e24Smartin 	/* separator line between partitions and actions */
1859dcc57e24Smartin 	op->opt_name = fspart_separator;
1860dcc57e24Smartin 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1861dcc57e24Smartin 	op++;
1862dcc57e24Smartin 
1863dcc57e24Smartin 	/* followed by new partition adder */
1864dcc57e24Smartin 	if (may_add) {
1865dcc57e24Smartin 		op->opt_name = MSG_addpart;
1866dcc57e24Smartin 		op->opt_flags = OPT_SUB;
1867dcc57e24Smartin 		op->opt_action = edit_fspart_add;
1868dcc57e24Smartin 		op++;
186926165e63Sdholland 	}
187026165e63Sdholland 
1871dcc57e24Smartin 	/* and unit changer */
1872dcc57e24Smartin 	op->opt_name = MSG_askunits;
1873dcc57e24Smartin 	op->opt_menu = MENU_sizechoice;
1874dcc57e24Smartin 	op->opt_flags = OPT_SUB;
1875dcc57e24Smartin 	op->opt_action = NULL;
1876dcc57e24Smartin 	op++;
1877dcc57e24Smartin 
1878dcc57e24Smartin 	if (may_edit_pack) {
1879dcc57e24Smartin 		op->opt_name = MSG_editpack;
1880dcc57e24Smartin 		op->opt_flags = OPT_SUB;
1881dcc57e24Smartin 		op->opt_action = edit_fspart_pack;
1882dcc57e24Smartin 		op++;
1883dcc57e24Smartin 	}
1884dcc57e24Smartin 
18854cde5629Smartin #ifndef NO_CLONES
18868ac26f1aSmartin 	/* add a clone-from-elsewhere option */
18878ac26f1aSmartin 	op->opt_name = MSG_clone_from_elsewhere;
18888ac26f1aSmartin 	op->opt_action = part_ext_clone;
18898ac26f1aSmartin 	op++;
18904cde5629Smartin #endif
18918ac26f1aSmartin 
1892dcc57e24Smartin 	/* and abort option */
1893dcc57e24Smartin 	op->opt_name = MSG_cancel;
1894dcc57e24Smartin 	op->opt_flags = OPT_EXIT;
1895dcc57e24Smartin 	op->opt_action = edit_fspart_abort;
1896dcc57e24Smartin 	op++;
1897dcc57e24Smartin 	cnt = op - pset->menu_opts;
18984cde5629Smartin 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
1899dcc57e24Smartin 
1900dcc57e24Smartin 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1901dcc57e24Smartin 			0, -1, 0, 74,
1902dcc57e24Smartin 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1903dcc57e24Smartin 			MC_NOCLEAR|MC_CONTINUOUS,
1904dcc57e24Smartin 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
190526165e63Sdholland 			MSG_partition_sizes_ok);
1906dcc57e24Smartin 
1907dcc57e24Smartin 	if (pset->menu < 0) {
1908dcc57e24Smartin 		free(pset->menu_opts);
1909dcc57e24Smartin 		pset->menu_opts = NULL;
1910dcc57e24Smartin 		return 0;
191126165e63Sdholland 	}
191226165e63Sdholland 
1913dcc57e24Smartin 	p->current_cylsize = p->dlcylsize;
191426165e63Sdholland 
191526165e63Sdholland 	for (;;) {
191626165e63Sdholland 		/* first give the user the option to edit the label... */
1917dcc57e24Smartin 		pset->ok = true;
1918dcc57e24Smartin 		process_menu(pset->menu, pset);
1919dcc57e24Smartin 		if (!pset->ok) {
1920dcc57e24Smartin 			i = 0;
1921dcc57e24Smartin 			break;
1922dcc57e24Smartin 		}
192326165e63Sdholland 
192426165e63Sdholland 		/* User thinks the label is OK. */
192534f3e937Smartin 		i = verify_parts(pset, install);
1926dcc57e24Smartin 		if (i == 1)
1927dcc57e24Smartin 			continue;
1928dcc57e24Smartin 		break;
192926165e63Sdholland 	}
1930dcc57e24Smartin 	free(pset->menu_opts);
1931dcc57e24Smartin 	pset->menu_opts = NULL;
1932dcc57e24Smartin 	free_menu(pset->menu);
1933dcc57e24Smartin 	pset->menu = -1;
193426165e63Sdholland 
1935dcc57e24Smartin 	return i != 0;
193626165e63Sdholland }
193726165e63Sdholland 
193826165e63Sdholland /*
19394a36d1aeSwiz  * strip trailing / to avoid confusion in path comparisons later
1940f4f78ddcSmartin  */
1941f4f78ddcSmartin void
canonicalize_last_mounted(char * path)1942f4f78ddcSmartin canonicalize_last_mounted(char *path)
1943f4f78ddcSmartin {
1944f4f78ddcSmartin 	char *p;
1945f4f78ddcSmartin 
19466c7e6cddSmartin 	if (path == NULL)
19476c7e6cddSmartin 		return;
19486c7e6cddSmartin 
19496c7e6cddSmartin 	if (strcmp(path, "/") == 0)
19506c7e6cddSmartin 		return;	/* in this case a "trailing" slash is allowed */
19516c7e6cddSmartin 
1952f4f78ddcSmartin 	for (;;) {
1953f4f78ddcSmartin 		p = strrchr(path, '/');
1954f4f78ddcSmartin 		if (p == NULL)
1955f4f78ddcSmartin 			return;
1956f4f78ddcSmartin 		if (p[1] != 0)
1957f4f78ddcSmartin 			return;
1958f4f78ddcSmartin 		p[0] = 0;
1959f4f78ddcSmartin 	}
1960f4f78ddcSmartin }
1961f4f78ddcSmartin 
1962f4f78ddcSmartin /*
196326165e63Sdholland  * Try to get 'last mounted on' (or equiv) from fs superblock.
196426165e63Sdholland  */
196526165e63Sdholland const char *
get_last_mounted(int fd,daddr_t partstart,uint * fs_type,uint * fs_sub_type,uint flags)1966dcc57e24Smartin get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1967dcc57e24Smartin     uint flags)
196826165e63Sdholland {
196926165e63Sdholland 	static char sblk[SBLOCKSIZE];		/* is this enough? */
197026165e63Sdholland 	struct fs *SB = (struct fs *)sblk;
1971dcc57e24Smartin 	static const off_t sblocks[] = SBLOCKSEARCH;
1972dcc57e24Smartin 	const off_t *sbp;
197326165e63Sdholland 	const char *mnt = NULL;
197426165e63Sdholland 	int len;
197526165e63Sdholland 
197626165e63Sdholland 	if (fd == -1)
197726165e63Sdholland 		return "";
197826165e63Sdholland 
1979dcc57e24Smartin 	if (fs_type)
1980dcc57e24Smartin 		*fs_type = 0;
1981dcc57e24Smartin 	if (fs_sub_type)
1982dcc57e24Smartin 		*fs_sub_type = 0;
1983dcc57e24Smartin 
198426165e63Sdholland 	/* Check UFS1/2 (and hence LFS) superblock */
198526165e63Sdholland 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
198626165e63Sdholland 		if (pread(fd, sblk, sizeof sblk,
1987dcc57e24Smartin 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
198826165e63Sdholland 			continue;
1989dcc57e24Smartin 
1990dcc57e24Smartin 		/*
1991dcc57e24Smartin 		 * If start of partition and allowed by flags check
1992dcc57e24Smartin 		 * for other fs types
1993dcc57e24Smartin 		 */
1994dcc57e24Smartin 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1995dcc57e24Smartin 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1996dcc57e24Smartin 			/* Probably a FAT filesystem, report volume name */
1997dcc57e24Smartin 			size_t i;
1998dcc57e24Smartin 			for (i = 0x51; i >= 0x47; i--) {
1999dcc57e24Smartin 				if (sblk[i] != ' ')
2000dcc57e24Smartin 					break;
2001dcc57e24Smartin 				sblk[i] = 0;
2002dcc57e24Smartin 			}
2003dcc57e24Smartin 			sblk[0x52] = 0;
2004dcc57e24Smartin 			if (fs_type)
2005dcc57e24Smartin 				*fs_type = FS_MSDOS;
2006dcc57e24Smartin 			if (fs_sub_type)
2007dcc57e24Smartin 				*fs_sub_type = sblk[0x53];
2008dcc57e24Smartin 			return sblk + 0x47;
2009dcc57e24Smartin 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
2010dcc57e24Smartin 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
2011dcc57e24Smartin 			if (fs_type)
2012dcc57e24Smartin 				*fs_type = FS_NTFS;
2013dcc57e24Smartin 			if (fs_sub_type)
2014dcc57e24Smartin 				*fs_sub_type = MBR_PTYPE_NTFS;
2015dcc57e24Smartin 			/* XXX dig for volume name attribute ? */
2016dcc57e24Smartin 			return "";
2017dcc57e24Smartin 		}
2018dcc57e24Smartin 
2019dcc57e24Smartin 		if (!(flags & GLM_LIKELY_FFS))
2020dcc57e24Smartin 			continue;
2021dcc57e24Smartin 
202226165e63Sdholland 		/* Maybe we should validate the checksum??? */
202326165e63Sdholland 		switch (SB->fs_magic) {
202426165e63Sdholland 		case FS_UFS1_MAGIC:
202526165e63Sdholland 		case FS_UFS1_MAGIC_SWAPPED:
202626165e63Sdholland 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
202726165e63Sdholland 				if (*sbp == SBLOCK_UFS1)
202826165e63Sdholland 					mnt = (const char *)SB->fs_fsmnt;
202926165e63Sdholland 			} else {
203026165e63Sdholland 				/* Check we have the main superblock */
203126165e63Sdholland 				if (SB->fs_sblockloc == *sbp)
203226165e63Sdholland 					mnt = (const char *)SB->fs_fsmnt;
203326165e63Sdholland 			}
2034dcc57e24Smartin 			if (fs_type)
2035dcc57e24Smartin 				*fs_type = FS_BSDFFS;
2036dcc57e24Smartin 			if (fs_sub_type)
20372db02755Smartin 				*fs_sub_type = 1;
203826165e63Sdholland 			continue;
203926165e63Sdholland 		case FS_UFS2_MAGIC:
2040fc045a9dSchs 		case FS_UFS2EA_MAGIC:
204126165e63Sdholland 		case FS_UFS2_MAGIC_SWAPPED:
2042fc045a9dSchs 		case FS_UFS2EA_MAGIC_SWAPPED:
204326165e63Sdholland 			/* Check we have the main superblock */
204426165e63Sdholland 			if (SB->fs_sblockloc == *sbp) {
204526165e63Sdholland 				mnt = (const char *)SB->fs_fsmnt;
2046dcc57e24Smartin 				if (fs_type)
2047dcc57e24Smartin 					*fs_type = FS_BSDFFS;
2048dcc57e24Smartin 				if (fs_sub_type)
2049dcc57e24Smartin 					*fs_sub_type = 2;
205026165e63Sdholland 			}
205126165e63Sdholland 			continue;
205226165e63Sdholland 		}
205326165e63Sdholland 	}
205426165e63Sdholland 
205526165e63Sdholland 	if (mnt == NULL)
205626165e63Sdholland 		return "";
205726165e63Sdholland 
205826165e63Sdholland 	/* If sysinst mounted this last then strip prefix */
205926165e63Sdholland 	len = strlen(targetroot_mnt);
206026165e63Sdholland 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
206126165e63Sdholland 		if (mnt[len] == 0)
206226165e63Sdholland 			return "/";
206326165e63Sdholland 		if (mnt[len] == '/')
206426165e63Sdholland 			return mnt + len;
206526165e63Sdholland 	}
206626165e63Sdholland 	return mnt;
206726165e63Sdholland 	#undef SB
206826165e63Sdholland }
206926165e63Sdholland 
207026165e63Sdholland /* Ask for a partition offset, check bounds and do the needed roundups */
2071dcc57e24Smartin daddr_t
getpartoff(struct disk_partitions * parts,daddr_t defpartstart)2072dcc57e24Smartin getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
207326165e63Sdholland {
2074dcc57e24Smartin 	char defstart[24], isize[24], maxpart, minspace, maxspace,
2075dcc57e24Smartin 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
2076dcc57e24Smartin 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
2077dcc57e24Smartin 	size_t num_freespace, spaces, ndx;
2078dcc57e24Smartin 	struct disk_part_free_space *freespace;
2079dcc57e24Smartin 	daddr_t i, localsizemult, ptn_alignment, min, max;
2080dcc57e24Smartin 	part_id partn;
2081dcc57e24Smartin 	struct disk_part_info info;
2082dcc57e24Smartin 	const char *errmsg = NULL;
208326165e63Sdholland 
2084dcc57e24Smartin 	min = parts->disk_start;
2085dcc57e24Smartin 	max = min + parts->disk_size;
2086dcc57e24Smartin 
2087dcc57e24Smartin 	/* upper bound on the number of free spaces, plus some slope */
2088dcc57e24Smartin 	num_freespace = parts->num_part * 2 + 5;
2089dcc57e24Smartin 	freespace = calloc(num_freespace, sizeof(*freespace));
2090dcc57e24Smartin 	if (freespace == NULL)
2091dcc57e24Smartin 		return -1;
2092dcc57e24Smartin 
2093dcc57e24Smartin 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
2094dcc57e24Smartin 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
2095dcc57e24Smartin 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
2096dcc57e24Smartin 	    defpartstart);
2097dcc57e24Smartin 
2098dcc57e24Smartin 	maxpart = 'a' + parts->num_part -1;
2099dcc57e24Smartin 	if (parts->num_part > 1) {
2100dcc57e24Smartin 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
2101dcc57e24Smartin 	} else if (parts->num_part == 1) {
2102dcc57e24Smartin 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
2103dcc57e24Smartin 	} else {
2104dcc57e24Smartin 		strcpy(valid_parts, "---");
2105dcc57e24Smartin 	}
2106dcc57e24Smartin 	if (spaces > 1) {
2107dcc57e24Smartin 		minspace = maxpart + 1;
2108dcc57e24Smartin 		maxspace = minspace + spaces -1;
2109dcc57e24Smartin 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
2110dcc57e24Smartin 		    maxspace);
2111dcc57e24Smartin 	} else if (spaces == 1) {
2112dcc57e24Smartin 		maxspace = minspace = maxpart + 1;
2113dcc57e24Smartin 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
2114dcc57e24Smartin 	} else {
2115dcc57e24Smartin 		minspace = 0;
2116dcc57e24Smartin 		maxspace = 0;
2117dcc57e24Smartin 		strcpy(valid_spaces, "---");
2118dcc57e24Smartin 	}
2119dcc57e24Smartin 
2120dcc57e24Smartin 	/* Add description of start/size to user prompt */
2121dcc57e24Smartin 	const char *mstr = msg_string(MSG_free_space_line);
2122dcc57e24Smartin         space_prompt[0] = 0;
2123dcc57e24Smartin         for (ndx = 0; ndx < spaces; ndx++) {
2124dcc57e24Smartin                 char str_start[40], str_end[40], str_size[40], str_tag[4];
2125dcc57e24Smartin 
2126dcc57e24Smartin 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
2127dcc57e24Smartin                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
2128dcc57e24Smartin                 sprintf(str_end, "%" PRIu64,
2129dcc57e24Smartin                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
2130dcc57e24Smartin                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
2131dcc57e24Smartin                 const char *args[4] = { str_start, str_end, str_size,
2132dcc57e24Smartin                     multname };
2133dcc57e24Smartin                 char *line = str_arg_subst(mstr, 4, args);
2134dcc57e24Smartin 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
2135dcc57e24Smartin                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
2136dcc57e24Smartin                 free (line);
2137dcc57e24Smartin                 if (len >= sizeof space_prompt)
2138dcc57e24Smartin                         break;
2139dcc57e24Smartin         }
2140dcc57e24Smartin 
2141dcc57e24Smartin 	const char *args[] = { valid_parts, valid_spaces, multname };
2142dcc57e24Smartin 	hint_part = NULL;
2143dcc57e24Smartin 	hint_space = NULL;
2144dcc57e24Smartin 	head = str_arg_subst(msg_string(MSG_label_offset_head),
2145dcc57e24Smartin 	    __arraycount(args), args);
2146dcc57e24Smartin 	if (parts->num_part)
2147dcc57e24Smartin 		hint_part = str_arg_subst(msg_string(
2148dcc57e24Smartin 		    MSG_label_offset_part_hint), __arraycount(args), args);
2149dcc57e24Smartin 	if (spaces)
2150dcc57e24Smartin 		hint_space = str_arg_subst(msg_string(
2151dcc57e24Smartin 		    MSG_label_offset_space_hint), __arraycount(args), args);
2152dcc57e24Smartin 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
2153dcc57e24Smartin 	    __arraycount(args), args);
2154dcc57e24Smartin 
2155dcc57e24Smartin 	if (hint_part && hint_space)
2156dcc57e24Smartin 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
2157dcc57e24Smartin 		    head, hint_part, hint_space, space_prompt, tail);
2158dcc57e24Smartin 	else if (hint_part)
2159dcc57e24Smartin 		asprintf(&label_msg, "%s\n%s\n\n%s",
2160dcc57e24Smartin 		    head, hint_part, tail);
2161dcc57e24Smartin 	else if (hint_space)
2162dcc57e24Smartin 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
2163dcc57e24Smartin 		    head, hint_space, space_prompt, tail);
2164dcc57e24Smartin 	else
2165dcc57e24Smartin 		asprintf(&label_msg, "%s\n\n%s",
2166dcc57e24Smartin 		    head, tail);
2167dcc57e24Smartin 	free(head); free(hint_part); free(hint_space); free(tail);
2168dcc57e24Smartin 
2169dcc57e24Smartin 	localsizemult = sizemult;
2170dcc57e24Smartin 	errmsg = NULL;
217126165e63Sdholland 	for (;;) {
2172dcc57e24Smartin 		snprintf(defstart, sizeof defstart, "%" PRIu64,
2173dcc57e24Smartin 		    defpartstart/sizemult);
2174dcc57e24Smartin 		if (errmsg != NULL && errmsg[0] != 0)
2175dcc57e24Smartin 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2176dcc57e24Smartin 		else
2177dcc57e24Smartin 			prompt = label_msg;
2178dcc57e24Smartin 		msg_prompt_win(prompt, -1, 13, 70, -1,
2179dcc57e24Smartin 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
2180dcc57e24Smartin 		if (label_msg != prompt)
2181dcc57e24Smartin 			free(prompt);
2182dcc57e24Smartin 		if (strcmp(defstart, isize) == 0) {
218326165e63Sdholland 			/* Don't do rounding if default accepted */
2184dcc57e24Smartin 			i = defpartstart;
2185dcc57e24Smartin 			break;
2186dcc57e24Smartin 		}
218726165e63Sdholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
2188dcc57e24Smartin 		    isize[0] <= maxpart) {
218926165e63Sdholland 			partn = isize[0] - 'a';
2190dcc57e24Smartin 			if (parts->pscheme->get_part_info(parts, partn,
2191dcc57e24Smartin 			    &info)) {
2192dcc57e24Smartin 				i = info.start + info.size;
219326165e63Sdholland 				localsizemult = 1;
219426165e63Sdholland 			} else {
2195dcc57e24Smartin 				errmsg = msg_string(MSG_invalid_sector_number);
2196dcc57e24Smartin 				continue;
2197dcc57e24Smartin 			}
2198dcc57e24Smartin 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
2199dcc57e24Smartin 		    isize[0] <= maxspace) {
2200dcc57e24Smartin 			ndx = isize[0] - minspace;
2201dcc57e24Smartin 			i = freespace[ndx].start;
2202dcc57e24Smartin 			localsizemult = 1;
2203dcc57e24Smartin 		} else if (atoi(isize) == -1) {
2204dcc57e24Smartin 			i = min;
2205dcc57e24Smartin 			localsizemult = 1;
2206dcc57e24Smartin 		} else {
2207ebf4b8fdSmartin 			i = parse_disk_pos(isize, &localsizemult,
22080ea03d33Smartin 			    parts->bytes_per_sector,
2209ebf4b8fdSmartin 			    parts->pscheme->get_cylinder_size(parts), NULL);
2210dcc57e24Smartin 			if (i < 0) {
221126165e63Sdholland 				errmsg = msg_string(MSG_invalid_sector_number);
221226165e63Sdholland 				continue;
221326165e63Sdholland 			}
221426165e63Sdholland 		}
221526165e63Sdholland 		/* round to cylinder size if localsizemult != 1 */
2216ebf4b8fdSmartin 		int cylsize = parts->pscheme->get_cylinder_size(parts);
2217ebf4b8fdSmartin 		i = NUMSEC(i, localsizemult, cylsize);
221826165e63Sdholland 		/* Adjust to start of slice if needed */
2219dcc57e24Smartin 		if ((i < min && (min - i) < localsizemult) ||
2220dcc57e24Smartin 		    (i > min && (i - min) < localsizemult)) {
2221dcc57e24Smartin 			i = min;
222226165e63Sdholland 		}
2223dcc57e24Smartin 		if (max == 0 || i <= max)
222426165e63Sdholland 			break;
222526165e63Sdholland 		errmsg = msg_string(MSG_startoutsidedisk);
222626165e63Sdholland 	}
2227dcc57e24Smartin 	free(label_msg);
2228dcc57e24Smartin 	free(freespace);
2229dcc57e24Smartin 
223026165e63Sdholland 	return i;
223126165e63Sdholland }
223226165e63Sdholland 
223326165e63Sdholland 
223426165e63Sdholland /* Ask for a partition size, check bounds and do the needed roundups */
2235dcc57e24Smartin daddr_t
getpartsize(struct disk_partitions * parts,daddr_t orig_start,daddr_t partstart,daddr_t dflt)22364888d49bSmartin getpartsize(struct disk_partitions *parts, daddr_t orig_start,
22374888d49bSmartin     daddr_t partstart, daddr_t dflt)
223826165e63Sdholland {
2239dcc57e24Smartin 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
2240dcc57e24Smartin 	    *label_msg, *prompt, *head, *hint, *tail;
2241dcc57e24Smartin 	const char *errmsg = NULL;
2242ebf4b8fdSmartin 	daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
2243dcc57e24Smartin 	struct disk_part_info info;
2244dcc57e24Smartin 	part_id partn;
224526165e63Sdholland 
2246ebf4b8fdSmartin 	diskend = parts->disk_start + parts->disk_size;
22474888d49bSmartin 	max = parts->pscheme->max_free_space_at(parts, orig_start);
22484888d49bSmartin 	max += orig_start - partstart;
22494888d49bSmartin 	if (sizemult == 1)
22504888d49bSmartin 		max--;	/* with hugher scale proper rounding later will be ok */
2251dcc57e24Smartin 
2252dcc57e24Smartin 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
2253dcc57e24Smartin 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
2254dcc57e24Smartin 	if (max == dflt)
2255dcc57e24Smartin 		max_r = dflt_r;
2256dcc57e24Smartin 	else
2257dcc57e24Smartin 		max_r = max / sizemult;
2258dcc57e24Smartin 	/* the partition may have been moved and now not fit any longer */
2259dcc57e24Smartin 	if (dflt > max)
2260dcc57e24Smartin 		dflt = max;
2261dcc57e24Smartin 	if (dflt_r > max_r)
2262dcc57e24Smartin 		dflt_r = max_r;
2263dcc57e24Smartin 
2264dcc57e24Smartin 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
2265dcc57e24Smartin 
2266dcc57e24Smartin 	maxpartc = 'a' + parts->num_part -1;
2267dcc57e24Smartin 	if (parts->num_part > 1) {
2268dcc57e24Smartin 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
2269dcc57e24Smartin 	} else if (parts->num_part == 1) {
2270dcc57e24Smartin 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
2271dcc57e24Smartin 	} else {
2272dcc57e24Smartin 		strcpy(valid_parts, "---");
2273dcc57e24Smartin 	}
2274dcc57e24Smartin 
2275dcc57e24Smartin 	const char *args[] = { valid_parts, max_size, multname };
2276dcc57e24Smartin 	hint = NULL;
2277dcc57e24Smartin 	head = str_arg_subst(msg_string(MSG_label_size_head),
2278dcc57e24Smartin 	    __arraycount(args), args);
2279dcc57e24Smartin 	if (parts->num_part)
2280dcc57e24Smartin 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
2281dcc57e24Smartin 		    __arraycount(args), args);
2282dcc57e24Smartin 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
2283dcc57e24Smartin 	    __arraycount(args), args);
2284dcc57e24Smartin 
2285dcc57e24Smartin 	if (hint != NULL)
2286dcc57e24Smartin 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
2287dcc57e24Smartin 	else
2288dcc57e24Smartin 		asprintf(&label_msg, "%s\n\n%s", head, tail);
2289dcc57e24Smartin 	free(head); free(hint); free(tail);
2290dcc57e24Smartin 
2291dcc57e24Smartin 	localsizemult = sizemult;
2292dcc57e24Smartin 	i = -1;
229326165e63Sdholland 	for (;;) {
2294dcc57e24Smartin 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
2295dcc57e24Smartin 
2296dcc57e24Smartin 		if (errmsg != NULL && errmsg[0] != 0)
2297dcc57e24Smartin 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2298dcc57e24Smartin 		else
2299dcc57e24Smartin 			prompt = label_msg;
2300dcc57e24Smartin 		msg_prompt_win(prompt, -1, 12, 70, -1,
2301dcc57e24Smartin 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
2302dcc57e24Smartin 		if (prompt != label_msg)
2303dcc57e24Smartin 			free(prompt);
2304dcc57e24Smartin 
2305dcc57e24Smartin 		if (strcmp(isize, dsize) == 0) {
2306dcc57e24Smartin 			free(label_msg);
2307dcc57e24Smartin 			return dflt;
2308dcc57e24Smartin 		}
2309dcc57e24Smartin 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
231026165e63Sdholland 		    isize[0] <= maxpartc) {
231126165e63Sdholland 			partn = isize[0] - 'a';
2312dcc57e24Smartin 			if (parts->pscheme->get_part_info(parts, partn,
2313dcc57e24Smartin 			    &info)) {
2314dcc57e24Smartin 				i = info.start - partstart -1;
231526165e63Sdholland 				localsizemult = 1;
2316dcc57e24Smartin 				max_r = max;
231726165e63Sdholland 			}
2318dcc57e24Smartin 		} else if (atoi(isize) == -1) {
2319dcc57e24Smartin 			i = max;
2320dcc57e24Smartin 			localsizemult = 1;
2321dcc57e24Smartin 			max_r = max;
2322dcc57e24Smartin 		} else {
2323dcc57e24Smartin 			i = parse_disk_pos(isize, &localsizemult,
23240ea03d33Smartin 			    parts->bytes_per_sector,
2325ebf4b8fdSmartin 			    parts->pscheme->get_cylinder_size(parts), NULL);
2326dcc57e24Smartin 			if (localsizemult != sizemult)
2327dcc57e24Smartin 				max_r = max;
2328dcc57e24Smartin 		}
2329dcc57e24Smartin 		if (i < 0) {
2330dcc57e24Smartin 			errmsg = msg_string(MSG_Invalid_numeric);
2331dcc57e24Smartin 			continue;
2332dcc57e24Smartin 		} else if (i > max_r) {
2333dcc57e24Smartin 			errmsg = msg_string(MSG_Too_large);
2334dcc57e24Smartin 			continue;
233526165e63Sdholland 		}
233626165e63Sdholland 		/*
233726165e63Sdholland 		 * partend is aligned to a cylinder if localsizemult
233826165e63Sdholland 		 * is not 1 sector
233926165e63Sdholland 		 */
2340ebf4b8fdSmartin 		int cylsize = parts->pscheme->get_cylinder_size(parts);
2341dcc57e24Smartin 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
2342ebf4b8fdSmartin 		    localsizemult, cylsize);
234326165e63Sdholland 		/* Align to end-of-disk or end-of-slice if close enough */
2344ebf4b8fdSmartin 		if (partend > (diskend - sizemult)
2345ebf4b8fdSmartin 		    && partend < (diskend + sizemult))
2346ebf4b8fdSmartin 			partend = diskend;
2347dcc57e24Smartin 		if (partend > (partstart + max - sizemult)
2348dcc57e24Smartin 		    && partend < (partstart + max + sizemult))
2349dcc57e24Smartin 			partend = partstart + max;
235026165e63Sdholland 		/* sanity checks */
2351ebf4b8fdSmartin 		if (partend > diskend) {
2352ebf4b8fdSmartin 			partend = diskend;
2353dcc57e24Smartin 			errmsg = msg_string(MSG_endoutsidedisk);
2354dcc57e24Smartin 			continue;
235526165e63Sdholland 		}
2356dcc57e24Smartin 		free(label_msg);
235726165e63Sdholland 		if (partend < partstart)
235826165e63Sdholland 			return 0;
235926165e63Sdholland 		return (partend - partstart);
236026165e63Sdholland 	}
236126165e63Sdholland 	/* NOTREACHED */
236226165e63Sdholland }
236326165e63Sdholland 
236426165e63Sdholland /*
236526165e63Sdholland  * convert a string to a number of sectors, with a possible unit
236626165e63Sdholland  * 150M = 150 Megabytes
236726165e63Sdholland  * 2000c = 2000 cylinders
236826165e63Sdholland  * 150256s = 150256 sectors
2369dcc57e24Smartin  * Without units, use the default (sizemult).
2370dcc57e24Smartin  * returns the raw input value, and the unit used. Caller needs to multiply!
2371dcc57e24Smartin  * On invalid inputs, returns -1.
237226165e63Sdholland  */
2373dcc57e24Smartin daddr_t
parse_disk_pos(const char * str,daddr_t * localsizemult,daddr_t bps,daddr_t cyl_size,bool * extend_this)2374dcc57e24Smartin parse_disk_pos(
2375dcc57e24Smartin 	const char *str,
2376dcc57e24Smartin 	daddr_t *localsizemult,
23770ea03d33Smartin 	daddr_t bps,
2378dcc57e24Smartin 	daddr_t cyl_size,
2379dcc57e24Smartin 	bool *extend_this)
238026165e63Sdholland {
2381dcc57e24Smartin 	daddr_t val;
2382dcc57e24Smartin 	char *cp;
2383dcc57e24Smartin 	bool mult_found;
238426165e63Sdholland 
238526165e63Sdholland 	if (str[0] == '\0') {
2386dcc57e24Smartin 		return -1;
238726165e63Sdholland 	}
2388dcc57e24Smartin 	val = strtoull(str, &cp, 10);
2389dcc57e24Smartin 	mult_found = false;
2390dcc57e24Smartin 	if (extend_this)
2391dcc57e24Smartin 		*extend_this = false;
2392dcc57e24Smartin 	while (*cp != 0) {
2393dcc57e24Smartin 		if (*cp == 'G' || *cp == 'g') {
2394dcc57e24Smartin 			if (mult_found)
2395dcc57e24Smartin 				return -1;
23960ea03d33Smartin 			*localsizemult = GIG / bps;
2397dcc57e24Smartin 			goto next;
2398dcc57e24Smartin 		}
2399dcc57e24Smartin 		if (*cp == 'M' || *cp == 'm') {
2400dcc57e24Smartin 			if (mult_found)
2401dcc57e24Smartin 				return -1;
24020ea03d33Smartin 			*localsizemult = MEG / bps;
2403dcc57e24Smartin 			goto next;
2404dcc57e24Smartin 		}
2405dcc57e24Smartin 		if (*cp == 'c' || *cp == 'C') {
2406dcc57e24Smartin 			if (mult_found)
2407dcc57e24Smartin 				return -1;
2408ebf4b8fdSmartin 			*localsizemult = cyl_size;
2409dcc57e24Smartin 			goto next;
2410dcc57e24Smartin 		}
2411dcc57e24Smartin 		if (*cp == 's' || *cp == 'S') {
2412dcc57e24Smartin 			if (mult_found)
2413dcc57e24Smartin 				return -1;
2414dcc57e24Smartin 			*localsizemult = 1;
2415dcc57e24Smartin 			goto next;
2416dcc57e24Smartin 		}
2417dcc57e24Smartin 		if (*cp == '+' && extend_this) {
2418dcc57e24Smartin 			*extend_this = true;
2419dcc57e24Smartin 			cp++;
2420dcc57e24Smartin 			break;
2421dcc57e24Smartin 		}
2422dcc57e24Smartin 
2423dcc57e24Smartin 		/* not a known unit */
2424dcc57e24Smartin 		return -1;
2425dcc57e24Smartin 
2426dcc57e24Smartin next:
2427dcc57e24Smartin 		mult_found = true;
2428dcc57e24Smartin 		cp++;
242926165e63Sdholland 		continue;
243026165e63Sdholland 	}
2431dcc57e24Smartin 	if (*cp != 0)
2432dcc57e24Smartin 		return -1;
2433dcc57e24Smartin 
2434dcc57e24Smartin 	return val;
243526165e63Sdholland }
2436