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