Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.2.4.1
      1  1.2.4.1       snj /*	$NetBSD: label.c,v 1.2.4.1 2015/05/14 07:58:49 snj 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.2.4.1       snj __RCSID("$NetBSD: label.c,v 1.2.4.1 2015/05/14 07:58:49 snj 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.1  dholland 		if (p->pi_size < (20 * 1024 * (1024 / 512)))
    237      1.1  dholland 			p->pi_fsize = 512;
    238      1.1  dholland 		else if (p->pi_size < (1000 * 1024 * (1024 / 512)))
    239      1.1  dholland 			p->pi_fsize = 1024;
    240      1.1  dholland 		else if (p->pi_size < (128 * 1024 * 1024 * (1024 / 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.1  dholland 	    {NULL, MENU_selfskind, OPT_SUB, NULL},
    378      1.1  dholland #define PTN_MENU_START		1
    379      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_start},
    380      1.1  dholland #define PTN_MENU_SIZE		2
    381      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_size},
    382      1.1  dholland #define PTN_MENU_END		3
    383      1.1  dholland 	    {NULL, OPT_NOMENU, OPT_IGNORE, NULL},	/* displays 'end' */
    384      1.1  dholland #define PTN_MENU_NEWFS		4
    385      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_preserve},
    386      1.1  dholland #define PTN_MENU_ISIZE		5
    387      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_isize},
    388      1.1  dholland #define PTN_MENU_BSIZE		6
    389      1.1  dholland 	    {NULL, MENU_selbsize, OPT_SUB, NULL},
    390      1.1  dholland #define PTN_MENU_FSIZE		7
    391      1.1  dholland 	    {NULL, MENU_selfsize, OPT_SUB, NULL},
    392      1.1  dholland #define PTN_MENU_MOUNT		8
    393      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_mount},
    394      1.1  dholland #define PTN_MENU_MOUNTOPT	9
    395      1.1  dholland 	    {NULL, MENU_mountoptions, OPT_SUB, NULL},
    396      1.1  dholland #define PTN_MENU_MOUNTPT	10
    397      1.1  dholland 	    {NULL, OPT_NOMENU, 0, edit_fs_mountpt},
    398      1.1  dholland 	    {MSG_askunits, MENU_sizechoice, OPT_SUB, NULL},
    399      1.1  dholland 	    {MSG_restore, OPT_NOMENU, 0, edit_restore},
    400      1.1  dholland 	};
    401      1.1  dholland 	static int fspart_menu = -1;
    402      1.1  dholland 	static menu_ent all_fstypes[FSMAXTYPES];
    403      1.1  dholland 	partinfo *p, p_save;
    404      1.1  dholland 	unsigned int i;
    405      1.1  dholland 
    406      1.1  dholland 	if (fspart_menu == -1) {
    407      1.1  dholland 		fspart_menu = new_menu(NULL, fs_fields, nelem(fs_fields),
    408      1.1  dholland 			0, -1, 0, 70,
    409      1.1  dholland 			MC_NOBOX | MC_NOCLEAR | MC_SCROLL,
    410      1.1  dholland 			set_ptn_header, set_ptn_label, NULL,
    411      1.1  dholland 			NULL, MSG_partition_sizes_ok);
    412      1.1  dholland 	}
    413      1.1  dholland 
    414      1.1  dholland 	if (all_fstype_menu == -1) {
    415      1.1  dholland 		for (i = 0; i < nelem(all_fstypes); i++) {
    416      1.1  dholland 			all_fstypes[i].opt_name = getfslabelname(i);
    417      1.1  dholland 			all_fstypes[i].opt_menu = OPT_NOMENU;
    418      1.1  dholland 			all_fstypes[i].opt_flags = 0;
    419      1.1  dholland 			all_fstypes[i].opt_action = set_fstype;
    420      1.1  dholland 		}
    421      1.1  dholland 		all_fstype_menu = new_menu(MSG_Select_the_type,
    422      1.1  dholland 			all_fstypes, nelem(all_fstypes),
    423      1.1  dholland 			30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    424      1.1  dholland 			get_fstype, NULL, NULL, NULL, MSG_unchanged);
    425      1.1  dholland 	}
    426      1.1  dholland 
    427      1.2    martin 	p = pm->bsdlabel + menu->cursel;
    428      1.1  dholland 	p->pi_flags &= ~PIF_RESET;
    429      1.1  dholland 	p_save = *p;
    430      1.1  dholland 	for (;;) {
    431      1.1  dholland 		process_menu(fspart_menu, p);
    432      1.1  dholland 		if (!(p->pi_flags & PIF_RESET))
    433      1.1  dholland 			break;
    434      1.1  dholland 		*p = p_save;
    435      1.1  dholland 	}
    436      1.1  dholland 
    437      1.1  dholland 	return 0;
    438      1.1  dholland }
    439      1.1  dholland 
    440      1.1  dholland static void
    441      1.1  dholland set_ptn_header(menudesc *m, void *arg)
    442      1.1  dholland {
    443      1.1  dholland 	partinfo *p = arg;
    444      1.1  dholland 	int i;
    445      1.1  dholland 	int t;
    446      1.1  dholland 
    447      1.1  dholland 	msg_clear();
    448      1.2    martin 	msg_table_add(MSG_edfspart, 'a' + (p - pm->bsdlabel));
    449      1.1  dholland 
    450      1.1  dholland 	/* Determine which of the properties can be changed */
    451      1.1  dholland 	for (i = PTN_MENU_START; i <= PTN_MENU_MOUNTPT; i++) {
    452      1.1  dholland 		/* Default to disabled... */
    453      1.1  dholland 		m->opts[i].opt_flags |= OPT_IGNORE;
    454      1.1  dholland 		t = p->pi_fstype;
    455      1.1  dholland 		if (i == PTN_MENU_END)
    456      1.1  dholland 			/* The 'end address' is calculated from the size */
    457      1.1  dholland 			continue;
    458      1.1  dholland 		if (t == FS_UNUSED || (t == FS_SWAP && i > PTN_MENU_END)) {
    459      1.1  dholland 			/* Nothing after 'size' can be set for swap/unused */
    460      1.1  dholland 			p->pi_flags &= ~(PIF_NEWFS | PIF_MOUNT);
    461      1.1  dholland 			p->pi_mount[0] = 0;
    462      1.1  dholland 			continue;
    463      1.1  dholland 		}
    464      1.1  dholland 		if (i == PTN_MENU_NEWFS && t != FS_BSDFFS && t != FS_BSDLFS
    465      1.1  dholland 		    && t != FS_APPLEUFS) {
    466      1.1  dholland 			/* Can only newfs UFS and LFS filesystems */
    467      1.1  dholland 			p->pi_flags &= ~PIF_NEWFS;
    468      1.1  dholland 			continue;
    469      1.1  dholland 		}
    470      1.1  dholland 		if (i >= PTN_MENU_ISIZE && i <= PTN_MENU_FSIZE) {
    471      1.1  dholland 			/* Parameters for newfs... */
    472      1.1  dholland 			if (!(p->pi_flags & PIF_NEWFS))
    473      1.1  dholland 				/* Not if we aren't going to run newfs */
    474      1.1  dholland 				continue;
    475      1.1  dholland 			 if (t == FS_APPLEUFS && i != PTN_MENU_ISIZE)
    476      1.1  dholland 				/* Can only set # inodes for appleufs */
    477      1.1  dholland 				continue;
    478      1.1  dholland 			 if (t == FS_BSDLFS && i != PTN_MENU_BSIZE)
    479      1.1  dholland 				/* LFS doesn't have fragments */
    480      1.1  dholland 				continue;
    481      1.1  dholland 		}
    482      1.1  dholland 		/* Ok: we want this one */
    483      1.1  dholland 		m->opts[i].opt_flags &= ~OPT_IGNORE;
    484      1.1  dholland 	}
    485      1.1  dholland }
    486      1.1  dholland 
    487      1.1  dholland static void
    488      1.1  dholland disp_sector_count(menudesc *m, msg fmt, uint s)
    489      1.1  dholland {
    490      1.2    martin 	uint ms = MEG / pm->sectorsize;
    491      1.1  dholland 
    492      1.1  dholland 	wprintw(m->mw, msg_string(fmt),
    493      1.2    martin 		s / ms, s / pm->dlcylsize, s % pm->dlcylsize ? '*' : ' ', s );
    494      1.1  dholland }
    495      1.1  dholland 
    496      1.1  dholland static void
    497      1.1  dholland set_ptn_label(menudesc *m, int opt, void *arg)
    498      1.1  dholland {
    499      1.1  dholland 	partinfo *p = arg;
    500      1.1  dholland 	const char *c;
    501      1.1  dholland 
    502      1.1  dholland 	if (m->opts[opt].opt_flags & OPT_IGNORE
    503      1.1  dholland 	    && (opt != PTN_MENU_END || p->pi_fstype == FS_UNUSED)) {
    504      1.1  dholland 		wprintw(m->mw, "            -");
    505      1.1  dholland 		return;
    506      1.1  dholland 	}
    507      1.1  dholland 
    508      1.1  dholland 	switch (opt) {
    509      1.1  dholland 	case PTN_MENU_FSKIND:
    510      1.1  dholland 		if (p->pi_fstype == FS_BSDFFS)
    511      1.1  dholland 			if (p->pi_flags & PIF_FFSv2)
    512      1.1  dholland 				c = "FFSv2";
    513      1.1  dholland 			else
    514      1.1  dholland 				c = "FFSv1";
    515      1.1  dholland 		else
    516      1.1  dholland 			c = getfslabelname(p->pi_fstype);
    517      1.1  dholland 		wprintw(m->mw, msg_string(MSG_fstype_fmt), c);
    518      1.1  dholland 		break;
    519      1.1  dholland 	case PTN_MENU_START:
    520      1.1  dholland 		disp_sector_count(m, MSG_start_fmt, p->pi_offset);
    521      1.1  dholland 		break;
    522      1.1  dholland 	case PTN_MENU_SIZE:
    523      1.1  dholland 		disp_sector_count(m, MSG_size_fmt, p->pi_size);
    524      1.1  dholland 		break;
    525      1.1  dholland 	case PTN_MENU_END:
    526      1.1  dholland 		disp_sector_count(m, MSG_end_fmt, p->pi_offset + p->pi_size);
    527      1.1  dholland 		break;
    528      1.1  dholland 	case PTN_MENU_NEWFS:
    529      1.1  dholland 		wprintw(m->mw, msg_string(MSG_newfs_fmt),
    530      1.1  dholland 			msg_string(p->pi_flags & PIF_NEWFS ? MSG_Yes : MSG_No));
    531      1.1  dholland 		break;
    532      1.1  dholland 	case PTN_MENU_ISIZE:
    533      1.1  dholland 		wprintw(m->mw, msg_string(p->pi_isize > 0 ?
    534      1.1  dholland 			MSG_isize_fmt : MSG_isize_fmt_dflt), p->pi_isize);
    535      1.1  dholland 		break;
    536      1.1  dholland 	case PTN_MENU_BSIZE:
    537      1.1  dholland 		wprintw(m->mw, msg_string(MSG_bsize_fmt),
    538      1.1  dholland 			p->pi_fsize * p->pi_frag);
    539      1.1  dholland 		break;
    540      1.1  dholland 	case PTN_MENU_FSIZE:
    541      1.1  dholland 		wprintw(m->mw, msg_string(MSG_fsize_fmt), p->pi_fsize);
    542      1.1  dholland 		break;
    543      1.1  dholland 	case PTN_MENU_MOUNT:
    544      1.1  dholland 		wprintw(m->mw, msg_string(MSG_mount_fmt),
    545      1.1  dholland 			msg_string(p->pi_flags & PIF_MOUNT ? MSG_Yes : MSG_No));
    546      1.1  dholland 		break;
    547      1.1  dholland 	case PTN_MENU_MOUNTOPT:
    548      1.1  dholland 		wprintw(m->mw, "%s", msg_string(MSG_mount_options_fmt));
    549      1.1  dholland 		if (p->pi_flags & PIF_ASYNC)
    550      1.1  dholland 			wprintw(m->mw, "async ");
    551      1.1  dholland 		if (p->pi_flags & PIF_NOATIME)
    552      1.1  dholland 			wprintw(m->mw, "noatime ");
    553      1.1  dholland 		if (p->pi_flags & PIF_NODEV)
    554      1.1  dholland 			wprintw(m->mw, "nodev ");
    555      1.1  dholland 		if (p->pi_flags & PIF_NODEVMTIME)
    556      1.1  dholland 			wprintw(m->mw, "nodevmtime ");
    557      1.1  dholland 		if (p->pi_flags & PIF_NOEXEC)
    558      1.1  dholland 			wprintw(m->mw, "noexec ");
    559      1.1  dholland 		if (p->pi_flags & PIF_NOSUID)
    560      1.1  dholland 			wprintw(m->mw, "nosuid ");
    561      1.1  dholland 		if (p->pi_flags & PIF_LOG)
    562      1.1  dholland 			wprintw(m->mw, "log ");
    563      1.1  dholland 		break;
    564      1.1  dholland 	case PTN_MENU_MOUNTPT:
    565      1.1  dholland 		wprintw(m->mw, msg_string(MSG_mountpt_fmt), p->pi_mount);
    566      1.1  dholland 		break;
    567      1.1  dholland 	}
    568      1.1  dholland }
    569      1.1  dholland 
    570      1.1  dholland static int
    571      1.1  dholland show_all_unused(menudesc *m, void *arg)
    572      1.1  dholland {
    573      1.1  dholland 	struct ptn_menu_info *pi = arg;
    574      1.1  dholland 
    575      1.1  dholland 	pi->flags |= PIF_SHOW_UNUSED;
    576      1.1  dholland 	return 0;
    577      1.1  dholland }
    578      1.1  dholland 
    579      1.1  dholland static void
    580      1.1  dholland set_label_texts(menudesc *menu, void *arg)
    581      1.1  dholland {
    582      1.1  dholland 	struct ptn_menu_info *pi = arg;
    583      1.1  dholland 	menu_ent *m;
    584      1.1  dholland 	int ptn, show_unused_ptn;
    585      1.1  dholland 	int rawptn = getrawpartition();
    586      1.1  dholland 	int maxpart = getmaxpartitions();
    587      1.1  dholland 
    588      1.1  dholland 	msg_display(MSG_fspart);
    589      1.1  dholland 	msg_table_add(MSG_fspart_header, multname, multname, multname);
    590      1.1  dholland 
    591      1.1  dholland 	for (show_unused_ptn = 0, ptn = 0; ptn < maxpart; ptn++) {
    592      1.1  dholland 		m = &menu->opts[ptn];
    593      1.1  dholland 		m->opt_menu = OPT_NOMENU;
    594      1.1  dholland 		m->opt_name = NULL;
    595      1.1  dholland 		m->opt_action = edit_ptn;
    596      1.1  dholland 		if (ptn == rawptn
    597      1.1  dholland #ifdef PART_BOOT
    598      1.1  dholland 		    || ptn == PART_BOOT
    599      1.1  dholland #endif
    600      1.1  dholland 		    || ptn == PART_C) {
    601      1.1  dholland 			m->opt_flags = OPT_IGNORE;
    602      1.1  dholland 		} else {
    603      1.1  dholland 			m->opt_flags = 0;
    604      1.2    martin 			if (pm->bsdlabel[ptn].pi_fstype == FS_UNUSED)
    605      1.1  dholland 				continue;
    606      1.1  dholland 		}
    607      1.1  dholland 		show_unused_ptn = ptn + 2;
    608      1.1  dholland 	}
    609      1.1  dholland 
    610      1.1  dholland 	if (!(pi->flags & PIF_SHOW_UNUSED) && ptn > show_unused_ptn) {
    611      1.1  dholland 		ptn = show_unused_ptn;
    612      1.1  dholland 		m = &menu->opts[ptn];
    613      1.1  dholland 		m->opt_name = MSG_show_all_unused_partitions;
    614      1.1  dholland 		m->opt_action = show_all_unused;
    615      1.1  dholland 		ptn++;
    616      1.1  dholland 	}
    617      1.1  dholland 
    618      1.1  dholland 	m = &menu->opts[ptn];
    619      1.1  dholland 	m->opt_menu = MENU_sizechoice;
    620      1.1  dholland 	m->opt_flags = OPT_SUB;
    621      1.1  dholland 	m->opt_action = NULL;
    622      1.1  dholland 	m->opt_name = MSG_askunits;
    623      1.1  dholland 
    624      1.1  dholland 	menu->numopts = ptn + 1;
    625      1.1  dholland }
    626      1.1  dholland 
    627      1.1  dholland /*
    628      1.1  dholland  * Check a disklabel.
    629      1.1  dholland  * If there are overlapping active partitions,
    630      1.1  dholland  * Ask the user if they want to edit the partition or give up.
    631      1.1  dholland  */
    632      1.1  dholland int
    633      1.1  dholland edit_and_check_label(partinfo *lp, int nparts, int rawpart, int bsdpart)
    634      1.1  dholland {
    635      1.1  dholland 	static struct menu_ent *menu;
    636      1.1  dholland 	static int menu_no = -1;
    637      1.1  dholland 	static struct ptn_menu_info pi;
    638      1.1  dholland 	int maxpart = getmaxpartitions();
    639      1.1  dholland 
    640      1.1  dholland 	if (menu == NULL) {
    641      1.1  dholland 		menu = malloc((maxpart + 1) * sizeof *menu);
    642      1.1  dholland 		if (!menu)
    643      1.1  dholland 			return 1;
    644      1.1  dholland 	}
    645      1.1  dholland 
    646      1.1  dholland 	if (menu_no == -1) {
    647      1.1  dholland 		menu_no = new_menu(NULL, menu, maxpart + 1,
    648      1.1  dholland 			0, -1, maxpart + 2, 74,
    649      1.1  dholland 			MC_ALWAYS_SCROLL | MC_NOBOX | MC_DFLTEXIT,
    650      1.1  dholland 			set_label_texts, fmt_fspart, NULL, NULL,
    651      1.1  dholland 			MSG_partition_sizes_ok);
    652      1.1  dholland 	}
    653      1.1  dholland 
    654      1.1  dholland 	if (menu_no < 0)
    655      1.1  dholland 		return 1;
    656      1.1  dholland 
    657      1.1  dholland 	pi.flags = 0;
    658      1.2    martin 	pm->current_cylsize = pm->dlcylsize;
    659      1.1  dholland 
    660      1.1  dholland 	for (;;) {
    661      1.1  dholland 		/* first give the user the option to edit the label... */
    662      1.1  dholland 		process_menu(menu_no, &pi);
    663      1.1  dholland 
    664      1.1  dholland 		/* User thinks the label is OK. */
    665      1.1  dholland 		/* check we have a single root fs */
    666      1.2    martin 		if (check_one_root(lp, nparts) == 0 && partman_go == 0)
    667      1.1  dholland 			msg_display(MSG_must_be_one_root);
    668      1.1  dholland 		else
    669      1.1  dholland 			/* Check for overlaps */
    670      1.2    martin 			if (checkoverlap(lp, nparts, rawpart, bsdpart) == 0)
    671      1.1  dholland 				return 1;
    672      1.1  dholland 
    673      1.1  dholland 		/*XXX ???*/
    674      1.1  dholland 		msg_display_add(MSG_edit_partitions_again);
    675  1.2.4.1       snj 		if (!ask_yesno(NULL))
    676      1.1  dholland 			return(0);
    677      1.1  dholland 	}
    678      1.1  dholland 
    679      1.1  dholland 	/*NOTREACHED*/
    680      1.1  dholland }
    681      1.1  dholland 
    682      1.1  dholland /*
    683      1.1  dholland  * Read a label from disk into a sysinst label structure.
    684      1.1  dholland  */
    685      1.1  dholland int
    686      1.1  dholland incorelabel(const char *dkname, partinfo *lp)
    687      1.1  dholland {
    688      1.1  dholland 	struct disklabel lab;
    689      1.1  dholland 	int i, maxpart;
    690      1.1  dholland 	struct partition *pp;
    691      1.1  dholland 	int fd;
    692      1.1  dholland 	char buf[64];
    693      1.1  dholland 
    694      1.1  dholland 	if (get_real_geom(dkname, &lab) == 0)
    695      1.1  dholland 		return -1;
    696      1.1  dholland 
    697      1.1  dholland 	touchwin(stdscr);
    698      1.1  dholland 	maxpart = getmaxpartitions();
    699      1.1  dholland 	if (maxpart > MAXPARTITIONS)
    700      1.1  dholland 		maxpart = MAXPARTITIONS;
    701      1.1  dholland 	if (maxpart > lab.d_npartitions)
    702      1.1  dholland 		maxpart = lab.d_npartitions;
    703      1.1  dholland 
    704      1.1  dholland 	/*
    705      1.1  dholland 	 * Historically sysinst writes the name to d_typename rather
    706      1.1  dholland 	 * than d_diskname.  Who knows why, but pull the value back here.
    707      1.1  dholland 	 */
    708      1.1  dholland 	if (lab.d_typename[0] != 0 && strcmp(lab.d_typename, "unknown") != 0)
    709      1.2    martin 		strlcpy(pm->bsddiskname, lab.d_typename, sizeof pm->bsddiskname);
    710      1.1  dholland 
    711      1.1  dholland 	fd = opendisk(dkname, O_RDONLY, buf, sizeof buf, 0);
    712      1.1  dholland 	pp = &lab.d_partitions[0];
    713      1.1  dholland 	for (i = 0; i < maxpart; lp++, pp++, i++) {
    714      1.1  dholland 		lp->pi_partition = *pp;
    715      1.1  dholland 		if (lp->pi_fstype >= FSMAXTYPES)
    716      1.1  dholland 			lp->pi_fstype = FS_OTHER;
    717      1.1  dholland 		strlcpy(lp->pi_mount, get_last_mounted(fd, pp->p_offset, lp),
    718      1.1  dholland 			sizeof lp->pi_mount);
    719      1.1  dholland 	}
    720      1.1  dholland 	if (fd != -1)
    721      1.1  dholland 		close(fd);
    722      1.1  dholland 
    723      1.1  dholland 	return 0;
    724      1.1  dholland }
    725      1.1  dholland 
    726      1.1  dholland /*
    727      1.1  dholland  * Try to get 'last mounted on' (or equiv) from fs superblock.
    728      1.1  dholland  */
    729      1.1  dholland const char *
    730      1.1  dholland get_last_mounted(int fd, int partstart, partinfo *lp)
    731      1.1  dholland {
    732      1.1  dholland 	static char sblk[SBLOCKSIZE];		/* is this enough? */
    733      1.1  dholland 	struct fs *SB = (struct fs *)sblk;
    734      1.1  dholland 	static const int sblocks[] = SBLOCKSEARCH;
    735      1.1  dholland 	const int *sbp;
    736      1.1  dholland 	char *cp;
    737      1.1  dholland 	const char *mnt = NULL;
    738      1.1  dholland 	int len;
    739      1.1  dholland 
    740      1.1  dholland 	if (fd == -1)
    741      1.1  dholland 		return "";
    742      1.1  dholland 
    743      1.1  dholland 	/* Check UFS1/2 (and hence LFS) superblock */
    744      1.1  dholland 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
    745      1.1  dholland 		if (pread(fd, sblk, sizeof sblk,
    746      1.1  dholland 		    partstart * (off_t)512 + *sbp) != sizeof sblk)
    747      1.1  dholland 			continue;
    748      1.1  dholland 		/* Maybe we should validate the checksum??? */
    749      1.1  dholland 		switch (SB->fs_magic) {
    750      1.1  dholland 		case FS_UFS1_MAGIC:
    751      1.1  dholland 		case FS_UFS1_MAGIC_SWAPPED:
    752      1.1  dholland 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
    753      1.1  dholland 				if (*sbp == SBLOCK_UFS1)
    754      1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
    755      1.1  dholland 			} else {
    756      1.1  dholland 				/* Check we have the main superblock */
    757      1.1  dholland 				if (SB->fs_sblockloc == *sbp)
    758      1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
    759      1.1  dholland 			}
    760      1.1  dholland 			continue;
    761      1.1  dholland 		case FS_UFS2_MAGIC:
    762      1.1  dholland 		case FS_UFS2_MAGIC_SWAPPED:
    763      1.1  dholland 			/* Check we have the main superblock */
    764      1.1  dholland 			if (SB->fs_sblockloc == *sbp) {
    765      1.1  dholland 				mnt = (const char *)SB->fs_fsmnt;
    766      1.1  dholland 				if (lp != NULL)
    767      1.1  dholland 					lp->pi_flags |= PIF_FFSv2;
    768      1.1  dholland 			}
    769      1.1  dholland 			continue;
    770      1.1  dholland 		}
    771      1.1  dholland 
    772      1.1  dholland #if 0	/* Requires fs/ext2fs/ext2fs.h which collides badly... */
    773      1.1  dholland 		if ((struct ext2fs *)sblk)->e2fs_magic == E2FS_MAGIC) {
    774      1.1  dholland 			mnt = ((struct ext2fs *)sblk)->e2fs_fsmnt;
    775      1.1  dholland 			continue;
    776      1.1  dholland 		}
    777      1.1  dholland #endif
    778      1.1  dholland 		if (*sbp != 0)
    779      1.1  dholland 			continue;
    780      1.1  dholland 
    781      1.1  dholland 		/* If start of partition check for other fs types */
    782      1.1  dholland 		if (sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
    783      1.1  dholland 			/* Probably a FAT filesystem, report volume name */
    784      1.1  dholland 			cp = strchr(sblk + 0x47, ' ');
    785      1.1  dholland 			if (cp == NULL || cp - (sblk + 0x47) > 11)
    786      1.1  dholland 				cp = sblk + 0x47 + 11;
    787      1.1  dholland 			*cp = 0;
    788      1.1  dholland 			return sblk + 0x47;
    789      1.1  dholland 		}
    790      1.1  dholland 	}
    791      1.1  dholland 
    792      1.1  dholland 	if (mnt == NULL)
    793      1.1  dholland 		return "";
    794      1.1  dholland 
    795      1.1  dholland 	/* If sysinst mounted this last then strip prefix */
    796      1.1  dholland 	len = strlen(targetroot_mnt);
    797      1.1  dholland 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
    798      1.1  dholland 		if (mnt[len] == 0)
    799      1.1  dholland 			return "/";
    800      1.1  dholland 		if (mnt[len] == '/')
    801      1.1  dholland 			return mnt + len;
    802      1.1  dholland 	}
    803      1.1  dholland 	return mnt;
    804      1.1  dholland 	#undef SB
    805      1.1  dholland }
    806      1.1  dholland 
    807      1.1  dholland /* Ask for a partition offset, check bounds and do the needed roundups */
    808      1.1  dholland static uint32_t
    809      1.1  dholland getpartoff(uint32_t defpartstart)
    810      1.1  dholland {
    811      1.1  dholland 	char defsize[20], isize[20], maxpartc;
    812      1.1  dholland 	uint32_t i;
    813      1.1  dholland 	uint32_t localsizemult;
    814      1.1  dholland 	int partn;
    815      1.1  dholland 	const char *errmsg = "\n";
    816      1.1  dholland 
    817      1.1  dholland 	maxpartc = 'a' + getmaxpartitions() - 1;
    818      1.1  dholland 	for (;;) {
    819      1.1  dholland 		snprintf(defsize, sizeof defsize, "%d", defpartstart/sizemult);
    820      1.1  dholland 		msg_prompt_win(MSG_label_offset, -1, 13, 70, 9,
    821      1.1  dholland 		    (defpartstart > 0) ? defsize : NULL, isize, sizeof isize,
    822      1.1  dholland 		    errmsg, maxpartc, maxpartc, multname);
    823      1.1  dholland 		if (strcmp(defsize, isize) == 0)
    824      1.1  dholland 			/* Don't do rounding if default accepted */
    825      1.1  dholland 			return defpartstart;
    826      1.1  dholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
    827      1.1  dholland 		    isize[0] <= maxpartc) {
    828      1.1  dholland 			partn = isize[0] - 'a';
    829      1.2    martin 			i = pm->bsdlabel[partn].pi_size + pm->bsdlabel[partn].pi_offset;
    830      1.1  dholland 			localsizemult = 1;
    831      1.1  dholland 		} else if (atoi(isize) == -1) {
    832      1.2    martin 			i = pm->ptstart;
    833      1.1  dholland 			localsizemult = 1;
    834      1.1  dholland 		} else {
    835      1.1  dholland 			if (atofsb(isize, &i, &localsizemult)) {
    836      1.1  dholland 				errmsg = msg_string(MSG_invalid_sector_number);
    837      1.1  dholland 				continue;
    838      1.1  dholland 			}
    839      1.1  dholland 		}
    840      1.1  dholland 		/* round to cylinder size if localsizemult != 1 */
    841      1.2    martin 		i = NUMSEC(i/localsizemult, localsizemult, pm->dlcylsize);
    842      1.1  dholland 		/* Adjust to start of slice if needed */
    843      1.2    martin 		if ((i < pm->ptstart && (pm->ptstart - i) < localsizemult) ||
    844      1.2    martin 		    (i > pm->ptstart && (i - pm->ptstart) < localsizemult)) {
    845      1.2    martin 			i = pm->ptstart;
    846      1.1  dholland 		}
    847      1.2    martin 		if (i <= pm->dlsize)
    848      1.1  dholland 			break;
    849      1.1  dholland 		errmsg = msg_string(MSG_startoutsidedisk);
    850      1.1  dholland 	}
    851      1.1  dholland 	return i;
    852      1.1  dholland }
    853      1.1  dholland 
    854      1.1  dholland 
    855      1.1  dholland /* Ask for a partition size, check bounds and do the needed roundups */
    856      1.1  dholland static uint32_t
    857      1.1  dholland getpartsize(uint32_t partstart, uint32_t defpartsize)
    858      1.1  dholland {
    859      1.1  dholland 	char dsize[20], isize[20], maxpartc;
    860      1.1  dholland 	const char *errmsg = "\n";
    861      1.1  dholland 	uint32_t i, partend, localsizemult;
    862      1.2    martin 	uint32_t fsptend = pm->ptstart + pm->ptsize;
    863      1.1  dholland 	int partn;
    864      1.1  dholland 
    865      1.1  dholland 	maxpartc = 'a' + getmaxpartitions() - 1;
    866      1.1  dholland 	for (;;) {
    867      1.1  dholland 		snprintf(dsize, sizeof dsize, "%d", defpartsize/sizemult);
    868      1.1  dholland 		msg_prompt_win(MSG_label_size, -1, 12, 70, 9,
    869      1.1  dholland 		    (defpartsize != 0) ? dsize : 0, isize, sizeof isize,
    870      1.1  dholland 		    errmsg, maxpartc, multname);
    871      1.1  dholland 		if (strcmp(isize, dsize) == 0)
    872      1.1  dholland 			return defpartsize;
    873      1.1  dholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
    874      1.1  dholland 		    isize[0] <= maxpartc) {
    875      1.1  dholland 			partn = isize[0] - 'a';
    876      1.2    martin 			i = pm->bsdlabel[partn].pi_offset - partstart;
    877      1.1  dholland 			localsizemult = 1;
    878      1.1  dholland 		} else if (atoi(isize) == -1) {
    879      1.1  dholland 			i = fsptend - partstart;
    880      1.1  dholland 			localsizemult = 1;
    881      1.1  dholland 		} else {
    882      1.1  dholland 			if (atofsb(isize, &i, &localsizemult)) {
    883      1.1  dholland 				errmsg = msg_string(MSG_invalid_sector_number);
    884      1.1  dholland 				continue;
    885      1.1  dholland 			}
    886      1.1  dholland 		}
    887      1.1  dholland 		/*
    888      1.1  dholland 		 * partend is aligned to a cylinder if localsizemult
    889      1.1  dholland 		 * is not 1 sector
    890      1.1  dholland 		 */
    891      1.1  dholland 		partend = NUMSEC((partstart + i) / localsizemult,
    892      1.2    martin 		    localsizemult, pm->dlcylsize);
    893      1.1  dholland 		/* Align to end-of-disk or end-of-slice if close enough */
    894      1.2    martin 		if (partend > (pm->dlsize - localsizemult)
    895      1.2    martin 		    && partend < (pm->dlsize + localsizemult))
    896      1.2    martin 			partend = pm->dlsize;
    897      1.1  dholland 		if (partend > (fsptend - localsizemult)
    898      1.1  dholland 		    && partend < (fsptend + localsizemult))
    899      1.1  dholland 			partend = fsptend;
    900      1.1  dholland 		/* sanity checks */
    901      1.2    martin 		if (partend > pm->dlsize) {
    902      1.2    martin 			partend = pm->dlsize;
    903      1.1  dholland 			msg_prompt_win(MSG_endoutsidedisk, -1, 13, 70, 6,
    904      1.1  dholland 			    NULL, isize, 1,
    905      1.1  dholland 			    (partend - partstart) / sizemult, multname);
    906      1.1  dholland 		}
    907      1.1  dholland 		if (partend < partstart)
    908      1.1  dholland 			return 0;
    909      1.1  dholland 		return (partend - partstart);
    910      1.1  dholland 	}
    911      1.1  dholland 	/* NOTREACHED */
    912      1.1  dholland }
    913      1.1  dholland 
    914      1.1  dholland /*
    915      1.1  dholland  * convert a string to a number of sectors, with a possible unit
    916      1.1  dholland  * 150M = 150 Megabytes
    917      1.1  dholland  * 2000c = 2000 cylinders
    918      1.1  dholland  * 150256s = 150256 sectors
    919      1.1  dholland  * Without units, use the default (sizemult)
    920      1.1  dholland  * returns the number of sectors, and the unit used (for roundups).
    921      1.1  dholland  */
    922      1.1  dholland 
    923      1.1  dholland static int
    924      1.1  dholland atofsb(const char *str, uint32_t *p_val, uint32_t *localsizemult)
    925      1.1  dholland {
    926      1.1  dholland 	int i;
    927      1.1  dholland 	uint32_t val;
    928      1.1  dholland 
    929      1.1  dholland 	*localsizemult = sizemult;
    930      1.1  dholland 	if (str[0] == '\0') {
    931      1.1  dholland 		return 1;
    932      1.1  dholland 	}
    933      1.1  dholland 	val = 0;
    934      1.1  dholland 	for (i = 0; str[i] != '\0'; i++) {
    935      1.1  dholland 		if (str[i] >= '0' && str[i] <= '9') {
    936      1.1  dholland 			val = val * 10 + str[i] - '0';
    937      1.1  dholland 			continue;
    938      1.1  dholland 		}
    939      1.1  dholland 		if (str[i + 1] != '\0') {
    940      1.1  dholland 			/* A non-digit caracter, not at the end */
    941      1.1  dholland 			return 1;
    942      1.1  dholland 		}
    943      1.1  dholland 		if (str[i] == 'G' || str[i] == 'g') {
    944      1.1  dholland 			val *= 1024;
    945      1.2    martin 			*localsizemult = MEG / pm->sectorsize;
    946      1.1  dholland 			break;
    947      1.1  dholland 		}
    948      1.1  dholland 		if (str[i] == 'M' || str[i] == 'm') {
    949      1.2    martin 			*localsizemult = MEG / pm->sectorsize;
    950      1.1  dholland 			break;
    951      1.1  dholland 		}
    952      1.1  dholland 		if (str[i] == 'c') {
    953      1.2    martin 			*localsizemult = pm->dlcylsize;
    954      1.1  dholland 			break;
    955      1.1  dholland 		}
    956      1.1  dholland 		if (str[i] == 's') {
    957      1.1  dholland 			*localsizemult = 1;
    958      1.1  dholland 			break;
    959      1.1  dholland 		}
    960      1.1  dholland 		/* not a known unit */
    961      1.1  dholland 		return 1;
    962      1.1  dholland 	}
    963      1.1  dholland 	*p_val = val * (*localsizemult);
    964      1.1  dholland 	return 0;
    965      1.1  dholland }
    966