Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.3.16.1
      1  1.3.16.1  christos /*	$NetBSD: label.c,v 1.3.16.1 2019/06/10 22:10:38 christos 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.3.16.1  christos __RCSID("$NetBSD: label.c,v 1.3.16.1 2019/06/10 22:10:38 christos 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.1  dholland #include <errno.h>
     45       1.1  dholland #include <stdio.h>
     46       1.1  dholland #include <fcntl.h>
     47       1.1  dholland #include <util.h>
     48       1.1  dholland #include <unistd.h>
     49       1.1  dholland #include <sys/dkio.h>
     50       1.1  dholland #include <sys/param.h>
     51       1.1  dholland #include <ufs/ffs/fs.h>
     52       1.1  dholland 
     53       1.1  dholland #include "defs.h"
     54       1.1  dholland #include "msg_defs.h"
     55       1.1  dholland #include "menu_defs.h"
     56       1.1  dholland 
     57       1.1  dholland struct ptn_menu_info {
     58       1.1  dholland 	int	flags;
     59       1.1  dholland #define PIF_SHOW_UNUSED		1
     60       1.1  dholland };
     61       1.1  dholland 
     62       1.1  dholland /*
     63       1.1  dholland  * local prototypes
     64       1.1  dholland  */
     65       1.1  dholland static int boringpart(partinfo *, int, int, int);
     66       1.1  dholland static uint32_t getpartoff(uint32_t);
     67       1.1  dholland static uint32_t getpartsize(uint32_t, uint32_t);
     68       1.1  dholland 
     69       1.1  dholland static int	checklabel(partinfo *, int, int, int, int *, int *);
     70       1.1  dholland static int	atofsb(const char *, uint32_t *, uint32_t *);
     71       1.1  dholland 
     72       1.1  dholland 
     73       1.1  dholland /*
     74       1.1  dholland  * Return 1 if partition i in lp should be ignored when checking
     75       1.1  dholland  * for overlapping partitions.
     76       1.1  dholland  */
     77       1.1  dholland static int
     78       1.1  dholland boringpart(partinfo *lp, int i, int rawpart, int bsdpart)
     79       1.1  dholland {
     80       1.1  dholland 
     81       1.1  dholland 	if (i == rawpart || i == bsdpart ||
     82       1.1  dholland 	    lp[i].pi_fstype == FS_UNUSED || lp[i].pi_size == 0)
     83       1.1  dholland 		return 1;
     84       1.1  dholland 	return 0;
     85       1.1  dholland }
     86       1.1  dholland 
     87       1.1  dholland 
     88       1.1  dholland 
     89       1.1  dholland /*
     90       1.1  dholland  * Check a sysinst label structure for overlapping partitions.
     91       1.1  dholland  * Returns 0 if no overlapping partition found, nonzero otherwise.
     92       1.1  dholland  * Sets reference arguments ovly1 and ovly2 to the indices of
     93       1.1  dholland  * overlapping partitions if any are found.
     94       1.1  dholland  */
     95       1.1  dholland static int
     96       1.1  dholland checklabel(partinfo *lp, int nparts, int rawpart, int bsdpart,
     97       1.1  dholland 	int *ovly1, int *ovly2)
     98       1.1  dholland {
     99       1.1  dholland 	int i;
    100       1.1  dholland 	int j;
    101       1.1  dholland 
    102       1.1  dholland 	*ovly1 = -1;
    103       1.1  dholland 	*ovly2 = -1;
    104       1.1  dholland 
    105       1.1  dholland 	for (i = 0; i < nparts - 1; i ++ ) {
    106       1.1  dholland 		partinfo *ip = &lp[i];
    107       1.1  dholland 		uint32_t istart, istop;
    108       1.1  dholland 
    109       1.1  dholland 		/* skip unused or reserved partitions */
    110       1.1  dholland 		if (boringpart(lp, i, rawpart, bsdpart))
    111       1.1  dholland 			continue;
    112       1.1  dholland 
    113       1.1  dholland 		/*
    114       1.1  dholland 		 * check succeeding partitions for overlap.
    115       1.1  dholland 		 * O(n^2), but n is small (currently <= 16).
    116       1.1  dholland 		 */
    117       1.1  dholland 		istart = ip->pi_offset;
    118       1.1  dholland 		istop = istart + ip->pi_size;
    119       1.1  dholland 
    120       1.1  dholland 		for (j = i+1; j < nparts; j++) {
    121       1.1  dholland 			partinfo *jp = &lp[j];
    122       1.1  dholland 			uint32_t jstart, jstop;
    123       1.1  dholland 
    124       1.1  dholland 			/* skip unused or reserved partitions */
    125       1.1  dholland 			if (boringpart(lp, j, rawpart, bsdpart))
    126       1.1  dholland 				continue;
    127       1.1  dholland 
    128       1.1  dholland 			jstart = jp->pi_offset;
    129       1.1  dholland 			jstop = jstart + jp->pi_size;
    130       1.1  dholland 
    131       1.1  dholland 			/* overlap? */
    132       1.1  dholland 			if ((istart <= jstart && jstart < istop) ||
    133       1.1  dholland 			    (jstart <= istart && istart < jstop)) {
    134       1.1  dholland 				*ovly1 = i;
    135       1.1  dholland 				*ovly2 = j;
    136       1.1  dholland 				return (1);
    137       1.1  dholland 			}
    138       1.1  dholland 		}
    139       1.1  dholland 	}
    140       1.1  dholland 
    141       1.1  dholland 	return (0);
    142       1.1  dholland }
    143       1.1  dholland 
    144       1.2    martin int
    145       1.2    martin checkoverlap(partinfo *lp, int nparts, int rawpart, int bsdpart)
    146       1.2    martin {
    147       1.2    martin 	int i, j;
    148       1.2    martin 	if (checklabel(lp, nparts, rawpart, bsdpart, &i, &j)) {
    149       1.2    martin 		msg_display(MSG_partitions_overlap,'a'+i,'a'+j);
    150       1.2    martin 		return 1;
    151       1.2    martin 	}
    152       1.2    martin 	return 0;
    153       1.2    martin }
    154       1.2    martin 
    155       1.1  dholland static int
    156       1.1  dholland check_one_root(partinfo *lp, int nparts)
    157       1.1  dholland {
    158       1.1  dholland 	int part;
    159       1.1  dholland 	int foundroot = 0;
    160       1.1  dholland 
    161       1.1  dholland 	for (part = 0; part < nparts; lp++, part++) {
    162       1.1  dholland #if 0
    163       1.1  dholland 		if (!PI_ISBSDFS(lp))
    164       1.1  dholland 			continue;
    165       1.1  dholland #endif
    166       1.1  dholland 		if (!(lp->pi_flags & PIF_MOUNT))
    167       1.1  dholland 			continue;
    168       1.1  dholland 		if (strcmp(lp->pi_mount, "/") != 0)
    169       1.1  dholland 			continue;
    170       1.1  dholland 		if (foundroot)
    171       1.1  dholland 			/* Duplicate */
    172       1.1  dholland 			return 0;
    173       1.1  dholland 		foundroot = 1;
    174       1.1  dholland 		/* Save partition number, a few things need to know it */
    175       1.2    martin 		pm->rootpart = part;
    176       1.1  dholland 	}
    177       1.1  dholland 	return foundroot;
    178       1.1  dholland }
    179       1.1  dholland 
    180       1.1  dholland static int
    181       1.1  dholland edit_fs_start(menudesc *m, void *arg)
    182       1.1  dholland {
    183       1.1  dholland 	partinfo *p = arg;
    184       1.1  dholland 	uint32_t start, end;
    185       1.1  dholland 
    186       1.1  dholland 	start = getpartoff(p->pi_offset);
    187       1.1  dholland 	if (p->pi_size != 0) {
    188       1.1  dholland 		/* Try to keep end in the same place */
    189       1.1  dholland 		end = p->pi_offset + p->pi_size;
    190       1.1  dholland 		if (end < start)
    191       1.1  dholland 			p->pi_size = 0;
    192       1.1  dholland 		else
    193       1.1  dholland 			p->pi_size = end - start;
    194       1.1  dholland 	}
    195       1.1  dholland 	p->pi_offset = start;
    196       1.1  dholland 	return 0;
    197       1.1  dholland }
    198       1.1  dholland 
    199       1.1  dholland static int
    200       1.1  dholland edit_fs_size(menudesc *m, void *arg)
    201       1.1  dholland {
    202       1.1  dholland 	partinfo *p = arg;
    203       1.1  dholland 	uint32_t size;
    204       1.1  dholland 
    205       1.1  dholland 	size = getpartsize(p->pi_offset, p->pi_size);
    206       1.1  dholland 	if (size == ~0u)
    207       1.2    martin 		size = pm->dlsize - p->pi_offset;
    208       1.1  dholland 	p->pi_size = size;
    209       1.1  dholland 	if (size == 0) {
    210       1.1  dholland 		p->pi_offset = 0;
    211       1.1  dholland 		p->pi_fstype = FS_UNUSED;
    212       1.1  dholland 	}
    213       1.1  dholland 	return 0;
    214       1.1  dholland }
    215       1.1  dholland 
    216       1.1  dholland void
    217       1.1  dholland set_ptype(partinfo *p, int fstype, int flag)
    218       1.1  dholland {
    219       1.1  dholland 	p->pi_flags = (p->pi_flags & ~PIF_FFSv2) | flag;
    220       1.1  dholland 
    221       1.1  dholland 	if (p->pi_fstype == fstype)
    222       1.1  dholland 		return;
    223       1.1  dholland 
    224       1.1  dholland 	p->pi_fstype = fstype;
    225       1.1  dholland 	if (fstype == FS_BSDFFS || fstype == FS_BSDLFS) {
    226       1.1  dholland 		p->pi_frag = 8;
    227       1.1  dholland 		/*
    228       1.1  dholland 		 * match newfs defaults for fragments size:
    229       1.1  dholland 		 * fs size	frag size
    230       1.1  dholland 		 * < 20 MB	0.5 KB
    231       1.1  dholland 		 * < 1000 MB	1 KB
    232       1.1  dholland 		 * < 128 GB	2 KB
    233       1.1  dholland 		 * >= 128 GB	4 KB
    234       1.1  dholland 		 */
    235       1.1  dholland 	 	/* note pi_size is uint32_t so we have to avoid overflow */
    236  1.3.16.1  christos 		if (p->pi_size < (20 * MEG / 512))
    237       1.1  dholland 			p->pi_fsize = 512;
    238  1.3.16.1  christos 		else if (p->pi_size < (1000 * MEG / 512))
    239       1.1  dholland 			p->pi_fsize = 1024;
    240  1.3.16.1  christos 		else if (p->pi_size < (128 * (GIG / 512)))
    241       1.1  dholland 			p->pi_fsize = 2048;
    242       1.1  dholland 		else
    243       1.1  dholland 			p->pi_fsize = 4096;
    244       1.1  dholland 	} else {
    245       1.1  dholland 		/* zero - fields not used */
    246       1.1  dholland 		p->pi_frag = 0;
    247       1.1  dholland 		p->pi_fsize = 0;
    248       1.1  dholland 	}
    249       1.1  dholland }
    250       1.1  dholland 
    251       1.1  dholland void
    252       1.1  dholland set_bsize(partinfo *p, int size)
    253       1.1  dholland {
    254       1.1  dholland 	int frags, sz;
    255       1.1  dholland 
    256       1.1  dholland 	sz = p->pi_fsize;
    257       1.1  dholland 	if (sz <= 0)
    258       1.1  dholland 		sz = 512;
    259       1.1  dholland 	frags = size / sz;
    260       1.1  dholland 	if (frags > 8)
    261       1.1  dholland 		frags = 8;
    262       1.1  dholland 	if (frags <= 0)
    263       1.1  dholland 		frags = 1;
    264       1.1  dholland 
    265       1.1  dholland 	p->pi_frag = frags;
    266       1.1  dholland 	p->pi_fsize = size / frags;
    267       1.1  dholland }
    268       1.1  dholland 
    269       1.1  dholland void
    270       1.1  dholland set_fsize(partinfo *p, int fsize)
    271       1.1  dholland {
    272       1.1  dholland 	int bsz = p->pi_fsize * p->pi_frag;
    273       1.1  dholland 	int frags = bsz / fsize;
    274       1.1  dholland 
    275       1.1  dholland 	if (frags > 8)
    276       1.1  dholland 		frags = 8;
    277       1.1  dholland 	if (frags <= 0)
    278       1.1  dholland 		frags = 1;
    279       1.1  dholland 
    280       1.1  dholland 	p->pi_fsize = fsize;
    281       1.1  dholland 	p->pi_frag = frags;
    282       1.1  dholland }
    283       1.1  dholland 
    284       1.1  dholland static int
    285       1.1  dholland edit_fs_isize(menudesc *m, void *arg)
    286       1.1  dholland {
    287       1.1  dholland 	partinfo *p = arg;
    288       1.1  dholland 	char answer[12];
    289       1.1  dholland 
    290       1.1  dholland 	snprintf(answer, sizeof answer, "%u", p->pi_isize);
    291       1.1  dholland 	msg_prompt_win(MSG_fs_isize, -1, 18, 0, 0,
    292       1.1  dholland 		answer, answer, sizeof answer);
    293       1.1  dholland 	p->pi_isize = atol(answer);
    294       1.1  dholland 	return 0;
    295       1.1  dholland }
    296       1.1  dholland 
    297       1.1  dholland 
    298       1.1  dholland static int
    299       1.1  dholland edit_fs_preserve(menudesc *m, void *arg)
    300       1.1  dholland {
    301       1.1  dholland 	partinfo *p = arg;
    302       1.1  dholland 
    303       1.1  dholland 	p->pi_flags ^= PIF_NEWFS;
    304       1.1  dholland 	return 0;
    305       1.1  dholland }
    306       1.1  dholland 
    307       1.1  dholland static int
    308       1.1  dholland edit_fs_mount(menudesc *m, void *arg)
    309       1.1  dholland {
    310       1.1  dholland 	partinfo *p = arg;
    311       1.1  dholland 
    312       1.1  dholland 	p->pi_flags ^= PIF_MOUNT;
    313       1.1  dholland 	return 0;
    314       1.1  dholland }
    315       1.1  dholland 
    316       1.1  dholland static int
    317       1.1  dholland edit_fs_mountpt(menudesc *m, void *arg)
    318       1.1  dholland {
    319       1.1  dholland 	partinfo *p = arg;
    320       1.1  dholland 
    321       1.1  dholland 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
    322       1.1  dholland 		p->pi_mount, p->pi_mount, sizeof p->pi_mount);
    323       1.1  dholland 
    324       1.1  dholland 	if (p->pi_mount[0] == ' ' || strcmp(p->pi_mount, "none") == 0) {
    325       1.1  dholland 		p->pi_mount[0] = 0;
    326       1.1  dholland 		return 0;
    327       1.1  dholland 	}
    328       1.1  dholland 
    329       1.1  dholland 	if (p->pi_mount[0] != '/') {
    330       1.1  dholland 		memmove(p->pi_mount + 1, p->pi_mount,
    331       1.1  dholland 		    sizeof p->pi_mount - 1);
    332       1.1  dholland 		p->pi_mount[sizeof p->pi_mount - 1] = 0;
    333       1.1  dholland 		p->pi_mount[0] = '/';
    334       1.1  dholland 	}
    335       1.1  dholland 
    336       1.1  dholland 	return 0;
    337       1.1  dholland }
    338       1.1  dholland 
    339       1.1  dholland static int
    340       1.1  dholland edit_restore(menudesc *m, void *arg)
    341       1.1  dholland {
    342       1.1  dholland 	partinfo *p = arg;
    343       1.1  dholland 
    344       1.1  dholland 	p->pi_flags |= PIF_RESET;
    345       1.1  dholland 	return 1;
    346       1.1  dholland }
    347       1.1  dholland 
    348       1.1  dholland static int
    349       1.1  dholland set_fstype(menudesc *m, void *arg)
    350       1.1  dholland {
    351       1.1  dholland 	partinfo *p = arg;
    352       1.1  dholland 
    353       1.1  dholland 	if (m->cursel == FS_UNUSED)
    354       1.1  dholland 		memset(p, 0, sizeof *p);
    355       1.1  dholland 	else
    356       1.1  dholland 		p->pi_fstype = m->cursel;
    357       1.1  dholland 	return 1;
    358       1.1  dholland }
    359       1.1  dholland 
    360       1.1  dholland static void
    361       1.1  dholland get_fstype(menudesc *m, void *arg)
    362       1.1  dholland {
    363       1.1  dholland 	partinfo *p = arg;
    364       1.1  dholland 
    365       1.1  dholland 	m->cursel = p->pi_fstype;
    366       1.1  dholland }
    367       1.1  dholland 
    368       1.1  dholland static void set_ptn_header(menudesc *m, void *arg);
    369       1.1  dholland static void set_ptn_label(menudesc *m, int opt, void *arg);
    370       1.1  dholland int all_fstype_menu = -1;
    371       1.1  dholland 
    372       1.2    martin int
    373       1.1  dholland edit_ptn(menudesc *menu, void *arg)
    374       1.1  dholland {
    375       1.1  dholland 	static menu_ent fs_fields[] = {
    376       1.1  dholland #define PTN_MENU_FSKIND		0
    377  1.3.16.1  christos 	    { .opt_menu=MENU_selfskind, .opt_flags=OPT_SUB },
    378       1.1  dholland #define PTN_MENU_START		1
    379  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_start },
    380       1.1  dholland #define PTN_MENU_SIZE		2
    381  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_size },
    382       1.1  dholland #define PTN_MENU_END		3
    383  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_flags=OPT_IGNORE },	/* displays 'end' */
    384       1.1  dholland #define PTN_MENU_NEWFS		4
    385  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_preserve },
    386       1.1  dholland #define PTN_MENU_ISIZE		5
    387  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_isize },
    388       1.1  dholland #define PTN_MENU_BSIZE		6
    389  1.3.16.1  christos 	    { .opt_menu=MENU_selbsize, .opt_flags=OPT_SUB },
    390       1.1  dholland #define PTN_MENU_FSIZE		7
    391  1.3.16.1  christos 	    { .opt_menu=MENU_selfsize, .opt_flags=OPT_SUB },
    392       1.1  dholland #define PTN_MENU_MOUNT		8
    393  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_mount },
    394       1.1  dholland #define PTN_MENU_MOUNTOPT	9
    395  1.3.16.1  christos 	    { .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    396       1.1  dholland #define PTN_MENU_MOUNTPT	10
    397  1.3.16.1  christos 	    { .opt_menu=OPT_NOMENU, .opt_action=edit_fs_mountpt },
    398  1.3.16.1  christos 	    { .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    399  1.3.16.1  christos 	      .opt_flags=OPT_SUB },
    400  1.3.16.1  christos 	    { .opt_name=MSG_restore, .opt_menu=OPT_NOMENU,
    401  1.3.16.1  christos 	      .opt_action=edit_restore},
    402       1.1  dholland 	};
    403       1.1  dholland 	static int fspart_menu = -1;
    404       1.1  dholland 	static menu_ent all_fstypes[FSMAXTYPES];
    405       1.1  dholland 	partinfo *p, p_save;
    406       1.1  dholland 	unsigned int i;
    407       1.1  dholland 
    408       1.1  dholland 	if (fspart_menu == -1) {
    409       1.1  dholland 		fspart_menu = new_menu(NULL, fs_fields, nelem(fs_fields),
    410       1.1  dholland 			0, -1, 0, 70,
    411       1.1  dholland 			MC_NOBOX | MC_NOCLEAR | MC_SCROLL,
    412       1.1  dholland 			set_ptn_header, set_ptn_label, NULL,
    413       1.1  dholland 			NULL, MSG_partition_sizes_ok);
    414       1.1  dholland 	}
    415       1.1  dholland 
    416       1.1  dholland 	if (all_fstype_menu == -1) {
    417       1.1  dholland 		for (i = 0; i < nelem(all_fstypes); i++) {
    418       1.1  dholland 			all_fstypes[i].opt_name = getfslabelname(i);
    419       1.1  dholland 			all_fstypes[i].opt_menu = OPT_NOMENU;
    420       1.1  dholland 			all_fstypes[i].opt_flags = 0;
    421       1.1  dholland 			all_fstypes[i].opt_action = set_fstype;
    422       1.1  dholland 		}
    423       1.1  dholland 		all_fstype_menu = new_menu(MSG_Select_the_type,
    424       1.1  dholland 			all_fstypes, nelem(all_fstypes),
    425       1.1  dholland 			30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    426       1.1  dholland 			get_fstype, NULL, NULL, NULL, MSG_unchanged);
    427       1.1  dholland 	}
    428       1.1  dholland 
    429       1.2    martin 	p = pm->bsdlabel + menu->cursel;
    430       1.1  dholland 	p->pi_flags &= ~PIF_RESET;
    431       1.1  dholland 	p_save = *p;
    432       1.1  dholland 	for (;;) {
    433       1.1  dholland 		process_menu(fspart_menu, p);
    434       1.1  dholland 		if (!(p->pi_flags & PIF_RESET))
    435       1.1  dholland 			break;
    436       1.1  dholland 		*p = p_save;
    437       1.1  dholland 	}
    438       1.1  dholland 
    439       1.1  dholland 	return 0;
    440       1.1  dholland }
    441       1.1  dholland 
    442       1.1  dholland static void
    443       1.1  dholland set_ptn_header(menudesc *m, void *arg)
    444       1.1  dholland {
    445       1.1  dholland 	partinfo *p = arg;
    446       1.1  dholland 	int i;
    447       1.1  dholland 	int t;
    448       1.1  dholland 
    449       1.1  dholland 	msg_clear();
    450       1.2    martin 	msg_table_add(MSG_edfspart, 'a' + (p - pm->bsdlabel));
    451       1.1  dholland 
    452       1.1  dholland 	/* Determine which of the properties can be changed */
    453       1.1  dholland 	for (i = PTN_MENU_START; i <= PTN_MENU_MOUNTPT; i++) {
    454       1.1  dholland 		/* Default to disabled... */
    455       1.1  dholland 		m->opts[i].opt_flags |= OPT_IGNORE;
    456       1.1  dholland 		t = p->pi_fstype;
    457       1.1  dholland 		if (i == PTN_MENU_END)
    458       1.1  dholland 			/* The 'end address' is calculated from the size */
    459       1.1  dholland 			continue;
    460       1.1  dholland 		if (t == FS_UNUSED || (t == FS_SWAP && i > PTN_MENU_END)) {
    461       1.1  dholland 			/* Nothing after 'size' can be set for swap/unused */
    462       1.1  dholland 			p->pi_flags &= ~(PIF_NEWFS | PIF_MOUNT);
    463       1.1  dholland 			p->pi_mount[0] = 0;
    464       1.1  dholland 			continue;
    465       1.1  dholland 		}
    466       1.1  dholland 		if (i == PTN_MENU_NEWFS && t != FS_BSDFFS && t != FS_BSDLFS
    467       1.1  dholland 		    && t != FS_APPLEUFS) {
    468       1.1  dholland 			/* Can only newfs UFS and LFS filesystems */
    469       1.1  dholland 			p->pi_flags &= ~PIF_NEWFS;
    470       1.1  dholland 			continue;
    471       1.1  dholland 		}
    472       1.1  dholland 		if (i >= PTN_MENU_ISIZE && i <= PTN_MENU_FSIZE) {
    473       1.1  dholland 			/* Parameters for newfs... */
    474       1.1  dholland 			if (!(p->pi_flags & PIF_NEWFS))
    475       1.1  dholland 				/* Not if we aren't going to run newfs */
    476       1.1  dholland 				continue;
    477       1.1  dholland 			 if (t == FS_APPLEUFS && i != PTN_MENU_ISIZE)
    478       1.1  dholland 				/* Can only set # inodes for appleufs */
    479       1.1  dholland 				continue;
    480       1.1  dholland 			 if (t == FS_BSDLFS && i != PTN_MENU_BSIZE)
    481       1.1  dholland 				/* LFS doesn't have fragments */
    482       1.1  dholland 				continue;
    483       1.1  dholland 		}
    484       1.1  dholland 		/* Ok: we want this one */
    485       1.1  dholland 		m->opts[i].opt_flags &= ~OPT_IGNORE;
    486       1.1  dholland 	}
    487       1.1  dholland }
    488       1.1  dholland 
    489       1.1  dholland static void
    490       1.1  dholland disp_sector_count(menudesc *m, msg fmt, uint s)
    491       1.1  dholland {
    492       1.2    martin 	uint ms = MEG / pm->sectorsize;
    493       1.1  dholland 
    494       1.1  dholland 	wprintw(m->mw, msg_string(fmt),
    495       1.2    martin 		s / ms, s / pm->dlcylsize, s % pm->dlcylsize ? '*' : ' ', s );
    496       1.1  dholland }
    497       1.1  dholland 
    498       1.1  dholland static void
    499       1.1  dholland set_ptn_label(menudesc *m, int opt, void *arg)
    500       1.1  dholland {
    501       1.1  dholland 	partinfo *p = arg;
    502       1.1  dholland 	const char *c;
    503       1.1  dholland 
    504       1.1  dholland 	if (m->opts[opt].opt_flags & OPT_IGNORE
    505       1.1  dholland 	    && (opt != PTN_MENU_END || p->pi_fstype == FS_UNUSED)) {
    506       1.1  dholland 		wprintw(m->mw, "            -");
    507       1.1  dholland 		return;
    508       1.1  dholland 	}
    509       1.1  dholland 
    510       1.1  dholland 	switch (opt) {
    511       1.1  dholland 	case PTN_MENU_FSKIND:
    512       1.1  dholland 		if (p->pi_fstype == FS_BSDFFS)
    513       1.1  dholland 			if (p->pi_flags & PIF_FFSv2)
    514       1.1  dholland 				c = "FFSv2";
    515       1.1  dholland 			else
    516       1.1  dholland 				c = "FFSv1";
    517       1.1  dholland 		else
    518       1.1  dholland 			c = getfslabelname(p->pi_fstype);
    519       1.1  dholland 		wprintw(m->mw, msg_string(MSG_fstype_fmt), c);
    520       1.1  dholland 		break;
    521       1.1  dholland 	case PTN_MENU_START:
    522       1.1  dholland 		disp_sector_count(m, MSG_start_fmt, p->pi_offset);
    523       1.1  dholland 		break;
    524       1.1  dholland 	case PTN_MENU_SIZE:
    525       1.1  dholland 		disp_sector_count(m, MSG_size_fmt, p->pi_size);
    526       1.1  dholland 		break;
    527       1.1  dholland 	case PTN_MENU_END:
    528       1.1  dholland 		disp_sector_count(m, MSG_end_fmt, p->pi_offset + p->pi_size);
    529       1.1  dholland 		break;
    530       1.1  dholland 	case PTN_MENU_NEWFS:
    531       1.1  dholland 		wprintw(m->mw, msg_string(MSG_newfs_fmt),
    532       1.1  dholland 			msg_string(p->pi_flags & PIF_NEWFS ? MSG_Yes : MSG_No));
    533       1.1  dholland 		break;
    534       1.1  dholland 	case PTN_MENU_ISIZE:
    535       1.1  dholland 		wprintw(m->mw, msg_string(p->pi_isize > 0 ?
    536       1.1  dholland 			MSG_isize_fmt : MSG_isize_fmt_dflt), p->pi_isize);
    537       1.1  dholland 		break;
    538       1.1  dholland 	case PTN_MENU_BSIZE:
    539       1.1  dholland 		wprintw(m->mw, msg_string(MSG_bsize_fmt),
    540       1.1  dholland 			p->pi_fsize * p->pi_frag);
    541       1.1  dholland 		break;
    542       1.1  dholland 	case PTN_MENU_FSIZE:
    543       1.1  dholland 		wprintw(m->mw, msg_string(MSG_fsize_fmt), p->pi_fsize);
    544       1.1  dholland 		break;
    545       1.1  dholland 	case PTN_MENU_MOUNT:
    546       1.1  dholland 		wprintw(m->mw, msg_string(MSG_mount_fmt),
    547       1.1  dholland 			msg_string(p->pi_flags & PIF_MOUNT ? MSG_Yes : MSG_No));
    548       1.1  dholland 		break;
    549       1.1  dholland 	case PTN_MENU_MOUNTOPT:
    550       1.1  dholland 		wprintw(m->mw, "%s", msg_string(MSG_mount_options_fmt));
    551       1.1  dholland 		if (p->pi_flags & PIF_ASYNC)
    552       1.1  dholland 			wprintw(m->mw, "async ");
    553       1.1  dholland 		if (p->pi_flags & PIF_NOATIME)
    554       1.1  dholland 			wprintw(m->mw, "noatime ");
    555       1.1  dholland 		if (p->pi_flags & PIF_NODEV)
    556       1.1  dholland 			wprintw(m->mw, "nodev ");
    557       1.1  dholland 		if (p->pi_flags & PIF_NODEVMTIME)
    558       1.1  dholland 			wprintw(m->mw, "nodevmtime ");
    559       1.1  dholland 		if (p->pi_flags & PIF_NOEXEC)
    560       1.1  dholland 			wprintw(m->mw, "noexec ");
    561       1.1  dholland 		if (p->pi_flags & PIF_NOSUID)
    562       1.1  dholland 			wprintw(m->mw, "nosuid ");
    563       1.1  dholland 		if (p->pi_flags & PIF_LOG)
    564       1.1  dholland 			wprintw(m->mw, "log ");
    565       1.1  dholland 		break;
    566       1.1  dholland 	case PTN_MENU_MOUNTPT:
    567       1.1  dholland 		wprintw(m->mw, msg_string(MSG_mountpt_fmt), p->pi_mount);
    568       1.1  dholland 		break;
    569       1.1  dholland 	}
    570       1.1  dholland }
    571       1.1  dholland 
    572       1.1  dholland static int
    573       1.1  dholland show_all_unused(menudesc *m, void *arg)
    574       1.1  dholland {
    575       1.1  dholland 	struct ptn_menu_info *pi = arg;
    576       1.1  dholland 
    577       1.1  dholland 	pi->flags |= PIF_SHOW_UNUSED;
    578       1.1  dholland 	return 0;
    579       1.1  dholland }
    580       1.1  dholland 
    581       1.1  dholland static void
    582       1.1  dholland set_label_texts(menudesc *menu, void *arg)
    583       1.1  dholland {
    584       1.1  dholland 	struct ptn_menu_info *pi = arg;
    585       1.1  dholland 	menu_ent *m;
    586       1.1  dholland 	int ptn, show_unused_ptn;
    587       1.1  dholland 	int rawptn = getrawpartition();
    588       1.1  dholland 	int maxpart = getmaxpartitions();
    589       1.1  dholland 
    590       1.1  dholland 	msg_display(MSG_fspart);
    591       1.1  dholland 	msg_table_add(MSG_fspart_header, multname, multname, multname);
    592       1.1  dholland 
    593       1.1  dholland 	for (show_unused_ptn = 0, ptn = 0; ptn < maxpart; ptn++) {
    594       1.1  dholland 		m = &menu->opts[ptn];
    595       1.1  dholland 		m->opt_menu = OPT_NOMENU;
    596       1.1  dholland 		m->opt_name = NULL;
    597       1.1  dholland 		m->opt_action = edit_ptn;
    598       1.1  dholland 		if (ptn == rawptn
    599       1.1  dholland #ifdef PART_BOOT
    600       1.1  dholland 		    || ptn == PART_BOOT
    601       1.1  dholland #endif
    602       1.1  dholland 		    || ptn == PART_C) {
    603       1.1  dholland 			m->opt_flags = OPT_IGNORE;
    604       1.1  dholland 		} else {
    605       1.1  dholland 			m->opt_flags = 0;
    606       1.2    martin 			if (pm->bsdlabel[ptn].pi_fstype == FS_UNUSED)
    607       1.1  dholland 				continue;
    608       1.1  dholland 		}
    609       1.1  dholland 		show_unused_ptn = ptn + 2;
    610       1.1  dholland 	}
    611       1.1  dholland 
    612       1.1  dholland 	if (!(pi->flags & PIF_SHOW_UNUSED) && ptn > show_unused_ptn) {
    613       1.1  dholland 		ptn = show_unused_ptn;
    614       1.1  dholland 		m = &menu->opts[ptn];
    615       1.1  dholland 		m->opt_name = MSG_show_all_unused_partitions;
    616       1.1  dholland 		m->opt_action = show_all_unused;
    617       1.1  dholland 		ptn++;
    618       1.1  dholland 	}
    619       1.1  dholland 
    620       1.1  dholland 	m = &menu->opts[ptn];
    621       1.1  dholland 	m->opt_menu = MENU_sizechoice;
    622       1.1  dholland 	m->opt_flags = OPT_SUB;
    623       1.1  dholland 	m->opt_action = NULL;
    624       1.1  dholland 	m->opt_name = MSG_askunits;
    625       1.1  dholland 
    626       1.1  dholland 	menu->numopts = ptn + 1;
    627       1.1  dholland }
    628       1.1  dholland 
    629       1.1  dholland /*
    630       1.1  dholland  * Check a disklabel.
    631       1.1  dholland  * If there are overlapping active partitions,
    632       1.1  dholland  * Ask the user if they want to edit the partition or give up.
    633       1.1  dholland  */
    634       1.1  dholland int
    635       1.1  dholland edit_and_check_label(partinfo *lp, int nparts, int rawpart, int bsdpart)
    636       1.1  dholland {
    637       1.1  dholland 	static struct menu_ent *menu;
    638       1.1  dholland 	static int menu_no = -1;
    639       1.1  dholland 	static struct ptn_menu_info pi;
    640       1.1  dholland 	int maxpart = getmaxpartitions();
    641       1.1  dholland 
    642       1.1  dholland 	if (menu == NULL) {
    643       1.1  dholland 		menu = malloc((maxpart + 1) * sizeof *menu);
    644       1.1  dholland 		if (!menu)
    645       1.1  dholland 			return 1;
    646       1.1  dholland 	}
    647       1.1  dholland 
    648       1.1  dholland 	if (menu_no == -1) {
    649       1.1  dholland 		menu_no = new_menu(NULL, menu, maxpart + 1,
    650       1.1  dholland 			0, -1, maxpart + 2, 74,
    651       1.1  dholland 			MC_ALWAYS_SCROLL | MC_NOBOX | MC_DFLTEXIT,
    652       1.1  dholland 			set_label_texts, fmt_fspart, NULL, NULL,
    653       1.1  dholland 			MSG_partition_sizes_ok);
    654       1.1  dholland 	}
    655       1.1  dholland 
    656       1.1  dholland 	if (menu_no < 0)
    657       1.1  dholland 		return 1;
    658       1.1  dholland 
    659       1.1  dholland 	pi.flags = 0;
    660       1.2    martin 	pm->current_cylsize = pm->dlcylsize;
    661       1.1  dholland 
    662       1.1  dholland 	for (;;) {
    663       1.1  dholland 		/* first give the user the option to edit the label... */
    664       1.1  dholland 		process_menu(menu_no, &pi);
    665       1.1  dholland 
    666       1.1  dholland 		/* User thinks the label is OK. */
    667       1.1  dholland 		/* check we have a single root fs */
    668       1.2    martin 		if (check_one_root(lp, nparts) == 0 && partman_go == 0)
    669       1.1  dholland 			msg_display(MSG_must_be_one_root);
    670       1.1  dholland 		else
    671       1.1  dholland 			/* Check for overlaps */
    672       1.2    martin 			if (checkoverlap(lp, nparts, rawpart, bsdpart) == 0)
    673       1.1  dholland 				return 1;
    674       1.1  dholland 
    675       1.1  dholland 		/*XXX ???*/
    676       1.1  dholland 		msg_display_add(MSG_edit_partitions_again);
    677       1.3    martin 		if (!ask_yesno(NULL))
    678       1.1  dholland 			return(0);
    679       1.1  dholland 	}
    680       1.1  dholland 
    681       1.1  dholland 	/*NOTREACHED*/
    682       1.1  dholland }
    683       1.1  dholland 
    684       1.1  dholland /*
    685       1.1  dholland  * Read a label from disk into a sysinst label structure.
    686       1.1  dholland  */
    687       1.1  dholland int
    688       1.1  dholland incorelabel(const char *dkname, partinfo *lp)
    689       1.1  dholland {
    690       1.1  dholland 	struct disklabel lab;
    691       1.1  dholland 	int i, maxpart;
    692       1.1  dholland 	struct partition *pp;
    693       1.1  dholland 	int fd;
    694       1.1  dholland 	char buf[64];
    695       1.1  dholland 
    696       1.1  dholland 	if (get_real_geom(dkname, &lab) == 0)
    697       1.1  dholland 		return -1;
    698       1.1  dholland 
    699       1.1  dholland 	touchwin(stdscr);
    700       1.1  dholland 	maxpart = getmaxpartitions();
    701       1.1  dholland 	if (maxpart > MAXPARTITIONS)
    702       1.1  dholland 		maxpart = MAXPARTITIONS;
    703       1.1  dholland 	if (maxpart > lab.d_npartitions)
    704       1.1  dholland 		maxpart = lab.d_npartitions;
    705       1.1  dholland 
    706       1.1  dholland 	/*
    707       1.1  dholland 	 * Historically sysinst writes the name to d_typename rather
    708       1.1  dholland 	 * than d_diskname.  Who knows why, but pull the value back here.
    709       1.1  dholland 	 */
    710       1.1  dholland 	if (lab.d_typename[0] != 0 && strcmp(lab.d_typename, "unknown") != 0)
    711       1.2    martin 		strlcpy(pm->bsddiskname, lab.d_typename, sizeof pm->bsddiskname);
    712       1.1  dholland 
    713       1.1  dholland 	fd = opendisk(dkname, O_RDONLY, buf, sizeof buf, 0);
    714       1.1  dholland 	pp = &lab.d_partitions[0];
    715       1.1  dholland 	for (i = 0; i < maxpart; lp++, pp++, i++) {
    716       1.1  dholland 		lp->pi_partition = *pp;
    717       1.1  dholland 		if (lp->pi_fstype >= FSMAXTYPES)
    718       1.1  dholland 			lp->pi_fstype = FS_OTHER;
    719       1.1  dholland 		strlcpy(lp->pi_mount, get_last_mounted(fd, pp->p_offset, lp),
    720       1.1  dholland 			sizeof lp->pi_mount);
    721       1.1  dholland 	}
    722       1.1  dholland 	if (fd != -1)
    723       1.1  dholland 		close(fd);
    724       1.1  dholland 
    725       1.1  dholland 	return 0;
    726       1.1  dholland }
    727       1.1  dholland 
    728       1.1  dholland /*
    729       1.1  dholland  * Try to get 'last mounted on' (or equiv) from fs superblock.
    730       1.1  dholland  */
    731       1.1  dholland const char *
    732       1.1  dholland get_last_mounted(int fd, int partstart, partinfo *lp)
    733       1.1  dholland {
    734       1.1  dholland 	static char sblk[SBLOCKSIZE];		/* is this enough? */
    735       1.1  dholland 	struct fs *SB = (struct fs *)sblk;
    736       1.1  dholland 	static const int sblocks[] = SBLOCKSEARCH;
    737       1.1  dholland 	const int *sbp;
    738       1.1  dholland 	char *cp;
    739       1.1  dholland 	const char *mnt = NULL;
    740       1.1  dholland 	int len;
    741       1.1  dholland 
    742       1.1  dholland 	if (fd == -1)
    743       1.1  dholland 		return "";
    744       1.1  dholland 
    745       1.1  dholland 	/* Check UFS1/2 (and hence LFS) superblock */
    746       1.1  dholland 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
    747       1.1  dholland 		if (pread(fd, sblk, sizeof sblk,
    748       1.1  dholland 		    partstart * (off_t)512 + *sbp) != sizeof sblk)
    749       1.1  dholland 			continue;
    750       1.1  dholland 		/* Maybe we should validate the checksum??? */
    751       1.1  dholland 		switch (SB->fs_magic) {
    752       1.1  dholland 		case FS_UFS1_MAGIC:
    753       1.1  dholland 		case FS_UFS1_MAGIC_SWAPPED:
    754       1.1  dholland 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
    755       1.1  dholland 				if (*sbp == SBLOCK_UFS1)
    756       1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
    757       1.1  dholland 			} else {
    758       1.1  dholland 				/* Check we have the main superblock */
    759       1.1  dholland 				if (SB->fs_sblockloc == *sbp)
    760       1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
    761       1.1  dholland 			}
    762       1.1  dholland 			continue;
    763       1.1  dholland 		case FS_UFS2_MAGIC:
    764       1.1  dholland 		case FS_UFS2_MAGIC_SWAPPED:
    765       1.1  dholland 			/* Check we have the main superblock */
    766       1.1  dholland 			if (SB->fs_sblockloc == *sbp) {
    767       1.1  dholland 				mnt = (const char *)SB->fs_fsmnt;
    768       1.1  dholland 				if (lp != NULL)
    769       1.1  dholland 					lp->pi_flags |= PIF_FFSv2;
    770       1.1  dholland 			}
    771       1.1  dholland 			continue;
    772       1.1  dholland 		}
    773       1.1  dholland 
    774       1.1  dholland #if 0	/* Requires fs/ext2fs/ext2fs.h which collides badly... */
    775       1.1  dholland 		if ((struct ext2fs *)sblk)->e2fs_magic == E2FS_MAGIC) {
    776       1.1  dholland 			mnt = ((struct ext2fs *)sblk)->e2fs_fsmnt;
    777       1.1  dholland 			continue;
    778       1.1  dholland 		}
    779       1.1  dholland #endif
    780       1.1  dholland 		if (*sbp != 0)
    781       1.1  dholland 			continue;
    782       1.1  dholland 
    783       1.1  dholland 		/* If start of partition check for other fs types */
    784       1.1  dholland 		if (sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
    785       1.1  dholland 			/* Probably a FAT filesystem, report volume name */
    786       1.1  dholland 			cp = strchr(sblk + 0x47, ' ');
    787       1.1  dholland 			if (cp == NULL || cp - (sblk + 0x47) > 11)
    788       1.1  dholland 				cp = sblk + 0x47 + 11;
    789       1.1  dholland 			*cp = 0;
    790       1.1  dholland 			return sblk + 0x47;
    791       1.1  dholland 		}
    792       1.1  dholland 	}
    793       1.1  dholland 
    794       1.1  dholland 	if (mnt == NULL)
    795       1.1  dholland 		return "";
    796       1.1  dholland 
    797       1.1  dholland 	/* If sysinst mounted this last then strip prefix */
    798       1.1  dholland 	len = strlen(targetroot_mnt);
    799       1.1  dholland 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
    800       1.1  dholland 		if (mnt[len] == 0)
    801       1.1  dholland 			return "/";
    802       1.1  dholland 		if (mnt[len] == '/')
    803       1.1  dholland 			return mnt + len;
    804       1.1  dholland 	}
    805       1.1  dholland 	return mnt;
    806       1.1  dholland 	#undef SB
    807       1.1  dholland }
    808       1.1  dholland 
    809       1.1  dholland /* Ask for a partition offset, check bounds and do the needed roundups */
    810       1.1  dholland static uint32_t
    811       1.1  dholland getpartoff(uint32_t defpartstart)
    812       1.1  dholland {
    813       1.1  dholland 	char defsize[20], isize[20], maxpartc;
    814       1.1  dholland 	uint32_t i;
    815       1.1  dholland 	uint32_t localsizemult;
    816       1.1  dholland 	int partn;
    817       1.1  dholland 	const char *errmsg = "\n";
    818       1.1  dholland 
    819       1.1  dholland 	maxpartc = 'a' + getmaxpartitions() - 1;
    820       1.1  dholland 	for (;;) {
    821       1.1  dholland 		snprintf(defsize, sizeof defsize, "%d", defpartstart/sizemult);
    822       1.1  dholland 		msg_prompt_win(MSG_label_offset, -1, 13, 70, 9,
    823       1.1  dholland 		    (defpartstart > 0) ? defsize : NULL, isize, sizeof isize,
    824       1.1  dholland 		    errmsg, maxpartc, maxpartc, multname);
    825       1.1  dholland 		if (strcmp(defsize, isize) == 0)
    826       1.1  dholland 			/* Don't do rounding if default accepted */
    827       1.1  dholland 			return defpartstart;
    828       1.1  dholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
    829       1.1  dholland 		    isize[0] <= maxpartc) {
    830       1.1  dholland 			partn = isize[0] - 'a';
    831       1.2    martin 			i = pm->bsdlabel[partn].pi_size + pm->bsdlabel[partn].pi_offset;
    832       1.1  dholland 			localsizemult = 1;
    833       1.1  dholland 		} else if (atoi(isize) == -1) {
    834       1.2    martin 			i = pm->ptstart;
    835       1.1  dholland 			localsizemult = 1;
    836       1.1  dholland 		} else {
    837       1.1  dholland 			if (atofsb(isize, &i, &localsizemult)) {
    838       1.1  dholland 				errmsg = msg_string(MSG_invalid_sector_number);
    839       1.1  dholland 				continue;
    840       1.1  dholland 			}
    841       1.1  dholland 		}
    842       1.1  dholland 		/* round to cylinder size if localsizemult != 1 */
    843       1.2    martin 		i = NUMSEC(i/localsizemult, localsizemult, pm->dlcylsize);
    844       1.1  dholland 		/* Adjust to start of slice if needed */
    845       1.2    martin 		if ((i < pm->ptstart && (pm->ptstart - i) < localsizemult) ||
    846       1.2    martin 		    (i > pm->ptstart && (i - pm->ptstart) < localsizemult)) {
    847       1.2    martin 			i = pm->ptstart;
    848       1.1  dholland 		}
    849       1.2    martin 		if (i <= pm->dlsize)
    850       1.1  dholland 			break;
    851       1.1  dholland 		errmsg = msg_string(MSG_startoutsidedisk);
    852       1.1  dholland 	}
    853       1.1  dholland 	return i;
    854       1.1  dholland }
    855       1.1  dholland 
    856       1.1  dholland 
    857       1.1  dholland /* Ask for a partition size, check bounds and do the needed roundups */
    858       1.1  dholland static uint32_t
    859       1.1  dholland getpartsize(uint32_t partstart, uint32_t defpartsize)
    860       1.1  dholland {
    861       1.1  dholland 	char dsize[20], isize[20], maxpartc;
    862       1.1  dholland 	const char *errmsg = "\n";
    863       1.1  dholland 	uint32_t i, partend, localsizemult;
    864       1.2    martin 	uint32_t fsptend = pm->ptstart + pm->ptsize;
    865       1.1  dholland 	int partn;
    866       1.1  dholland 
    867       1.1  dholland 	maxpartc = 'a' + getmaxpartitions() - 1;
    868       1.1  dholland 	for (;;) {
    869       1.1  dholland 		snprintf(dsize, sizeof dsize, "%d", defpartsize/sizemult);
    870       1.1  dholland 		msg_prompt_win(MSG_label_size, -1, 12, 70, 9,
    871       1.1  dholland 		    (defpartsize != 0) ? dsize : 0, isize, sizeof isize,
    872       1.1  dholland 		    errmsg, maxpartc, multname);
    873       1.1  dholland 		if (strcmp(isize, dsize) == 0)
    874       1.1  dholland 			return defpartsize;
    875       1.1  dholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
    876       1.1  dholland 		    isize[0] <= maxpartc) {
    877       1.1  dholland 			partn = isize[0] - 'a';
    878       1.2    martin 			i = pm->bsdlabel[partn].pi_offset - partstart;
    879       1.1  dholland 			localsizemult = 1;
    880       1.1  dholland 		} else if (atoi(isize) == -1) {
    881       1.1  dholland 			i = fsptend - partstart;
    882       1.1  dholland 			localsizemult = 1;
    883       1.1  dholland 		} else {
    884       1.1  dholland 			if (atofsb(isize, &i, &localsizemult)) {
    885       1.1  dholland 				errmsg = msg_string(MSG_invalid_sector_number);
    886       1.1  dholland 				continue;
    887       1.1  dholland 			}
    888       1.1  dholland 		}
    889       1.1  dholland 		/*
    890       1.1  dholland 		 * partend is aligned to a cylinder if localsizemult
    891       1.1  dholland 		 * is not 1 sector
    892       1.1  dholland 		 */
    893       1.1  dholland 		partend = NUMSEC((partstart + i) / localsizemult,
    894       1.2    martin 		    localsizemult, pm->dlcylsize);
    895       1.1  dholland 		/* Align to end-of-disk or end-of-slice if close enough */
    896       1.2    martin 		if (partend > (pm->dlsize - localsizemult)
    897       1.2    martin 		    && partend < (pm->dlsize + localsizemult))
    898       1.2    martin 			partend = pm->dlsize;
    899       1.1  dholland 		if (partend > (fsptend - localsizemult)
    900       1.1  dholland 		    && partend < (fsptend + localsizemult))
    901       1.1  dholland 			partend = fsptend;
    902       1.1  dholland 		/* sanity checks */
    903       1.2    martin 		if (partend > pm->dlsize) {
    904       1.2    martin 			partend = pm->dlsize;
    905       1.1  dholland 			msg_prompt_win(MSG_endoutsidedisk, -1, 13, 70, 6,
    906       1.1  dholland 			    NULL, isize, 1,
    907       1.1  dholland 			    (partend - partstart) / sizemult, multname);
    908       1.1  dholland 		}
    909       1.1  dholland 		if (partend < partstart)
    910       1.1  dholland 			return 0;
    911       1.1  dholland 		return (partend - partstart);
    912       1.1  dholland 	}
    913       1.1  dholland 	/* NOTREACHED */
    914       1.1  dholland }
    915       1.1  dholland 
    916       1.1  dholland /*
    917       1.1  dholland  * convert a string to a number of sectors, with a possible unit
    918       1.1  dholland  * 150M = 150 Megabytes
    919       1.1  dholland  * 2000c = 2000 cylinders
    920       1.1  dholland  * 150256s = 150256 sectors
    921       1.1  dholland  * Without units, use the default (sizemult)
    922       1.1  dholland  * returns the number of sectors, and the unit used (for roundups).
    923       1.1  dholland  */
    924       1.1  dholland 
    925       1.1  dholland static int
    926       1.1  dholland atofsb(const char *str, uint32_t *p_val, uint32_t *localsizemult)
    927       1.1  dholland {
    928       1.1  dholland 	int i;
    929       1.1  dholland 	uint32_t val;
    930       1.1  dholland 
    931       1.1  dholland 	*localsizemult = sizemult;
    932       1.1  dholland 	if (str[0] == '\0') {
    933       1.1  dholland 		return 1;
    934       1.1  dholland 	}
    935       1.1  dholland 	val = 0;
    936       1.1  dholland 	for (i = 0; str[i] != '\0'; i++) {
    937       1.1  dholland 		if (str[i] >= '0' && str[i] <= '9') {
    938       1.1  dholland 			val = val * 10 + str[i] - '0';
    939       1.1  dholland 			continue;
    940       1.1  dholland 		}
    941       1.1  dholland 		if (str[i + 1] != '\0') {
    942       1.1  dholland 			/* A non-digit caracter, not at the end */
    943       1.1  dholland 			return 1;
    944       1.1  dholland 		}
    945       1.1  dholland 		if (str[i] == 'G' || str[i] == 'g') {
    946       1.1  dholland 			val *= 1024;
    947       1.2    martin 			*localsizemult = MEG / pm->sectorsize;
    948       1.1  dholland 			break;
    949       1.1  dholland 		}
    950       1.1  dholland 		if (str[i] == 'M' || str[i] == 'm') {
    951       1.2    martin 			*localsizemult = MEG / pm->sectorsize;
    952       1.1  dholland 			break;
    953       1.1  dholland 		}
    954       1.1  dholland 		if (str[i] == 'c') {
    955       1.2    martin 			*localsizemult = pm->dlcylsize;
    956       1.1  dholland 			break;
    957       1.1  dholland 		}
    958       1.1  dholland 		if (str[i] == 's') {
    959       1.1  dholland 			*localsizemult = 1;
    960       1.1  dholland 			break;
    961       1.1  dholland 		}
    962       1.1  dholland 		/* not a known unit */
    963       1.1  dholland 		return 1;
    964       1.1  dholland 	}
    965       1.1  dholland 	*p_val = val * (*localsizemult);
    966       1.1  dholland 	return 0;
    967       1.1  dholland }
    968