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