Home | History | Annotate | Line # | Download | only in inst
inst.c revision 1.16
      1  1.16   tsutsui /*	$NetBSD: inst.c,v 1.16 2007/12/29 16:48:03 tsutsui Exp $	*/
      2   1.1   thorpej 
      3   1.4   thorpej /*-
      4   1.4   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.4   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.4   thorpej  * by Jason R. Thorpe.
      9   1.4   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.1   thorpej  *    must display the following acknowledgement:
     20   1.4   thorpej  *	This product includes software developed by the NetBSD
     21   1.4   thorpej  *	Foundation, Inc. and its contributors.
     22   1.4   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.4   thorpej  *    contributors may be used to endorse or promote products derived
     24   1.4   thorpej  *    from this software without specific prior written permission.
     25   1.1   thorpej  *
     26   1.4   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.4   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.4   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.4   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.4   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.4   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.4   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.4   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.4   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.4   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.4   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.4   thorpej  */
     38   1.4   thorpej 
     39   1.4   thorpej /*
     40   1.1   thorpej  * Portions of this program are inspired by (and have borrowed code from)
     41   1.1   thorpej  * the `editlabel' program that accompanies NetBSD/vax, which carries
     42   1.1   thorpej  * the following notice:
     43   1.1   thorpej  *
     44   1.1   thorpej  * Copyright (c) 1995 Ludd, University of Lule}, Sweden.
     45   1.1   thorpej  * All rights reserved.
     46   1.1   thorpej  *
     47   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     48   1.1   thorpej  * modification, are permitted provided that the following conditions
     49   1.1   thorpej  * are met:
     50   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     51   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     52   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     53   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     54   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     55   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     56   1.1   thorpej  *    must display the following acknowledgement:
     57   1.1   thorpej  *	This product includes software developed at Ludd, University of
     58   1.1   thorpej  *	Lule}, Sweden and its contributors.
     59   1.1   thorpej  * 4. The name of the author may not be used to endorse or promote products
     60   1.1   thorpej  *    derived from this software without specific prior written permission
     61   1.1   thorpej  *
     62   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     63   1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     64   1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     65   1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     66   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     67   1.1   thorpej  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     68   1.1   thorpej  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     69   1.1   thorpej  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     70   1.1   thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72   1.1   thorpej  * SUCH DAMAGE.
     73   1.1   thorpej  */
     74   1.1   thorpej 
     75   1.1   thorpej #define DKTYPENAMES
     76   1.1   thorpej 
     77   1.1   thorpej #include <sys/param.h>
     78   1.1   thorpej #include <sys/reboot.h>
     79   1.1   thorpej #include <sys/disklabel.h>
     80   1.1   thorpej 
     81   1.1   thorpej #include <lib/libsa/stand.h>
     82  1.11   tsutsui #include <lib/libkern/libkern.h>
     83   1.1   thorpej 
     84   1.1   thorpej #include <hp300/stand/common/samachdep.h>
     85   1.1   thorpej 
     86   1.1   thorpej char line[100];
     87   1.1   thorpej 
     88   1.1   thorpej extern	u_int opendev;
     89   1.1   thorpej extern	char *lowram;
     90   1.1   thorpej extern	int noconsole;
     91   1.1   thorpej extern	int netio_ask;
     92   1.1   thorpej 
     93   1.1   thorpej char	*kernel_name = "/netbsd";
     94   1.1   thorpej 
     95  1.11   tsutsui void	main(void);
     96  1.11   tsutsui void	dsklabel(void);
     97  1.11   tsutsui void	miniroot(void);
     98  1.11   tsutsui void	bootmini(void);
     99  1.11   tsutsui void	resetsys(void);
    100  1.11   tsutsui void	gethelp(void);
    101  1.11   tsutsui int	opendisk(char *, char *, int, char, int *);
    102  1.11   tsutsui void	disklabel_edit(struct disklabel *);
    103  1.11   tsutsui void	disklabel_show(struct disklabel *);
    104  1.11   tsutsui int	disklabel_write(char *, int, struct open_file *);
    105  1.11   tsutsui void	get_fstype(struct disklabel *lp, int);
    106  1.11   tsutsui int	a2int(char *);
    107   1.1   thorpej 
    108   1.1   thorpej struct	inst_command {
    109   1.1   thorpej 	char	*ic_cmd;		/* command name */
    110   1.1   thorpej 	char	*ic_desc;		/* command description */
    111  1.16   tsutsui 	void	(*ic_func)(void);	/* handling function */
    112   1.1   thorpej } inst_commands[] = {
    113   1.1   thorpej 	{ "disklabel",	"place partition map on disk",	dsklabel },
    114   1.1   thorpej 	{ "miniroot",	"place miniroot on disk",	miniroot },
    115   1.1   thorpej 	{ "boot",	"boot from miniroot",		bootmini },
    116   1.1   thorpej 	{ "reset",	"reset the system",		resetsys },
    117   1.1   thorpej 	{ "help",	"display command list",		gethelp },
    118   1.1   thorpej };
    119   1.1   thorpej #define NCMDS	(sizeof(inst_commands) / sizeof(inst_commands[0]))
    120   1.1   thorpej 
    121  1.11   tsutsui void
    122  1.13   tsutsui main(void)
    123   1.1   thorpej {
    124  1.11   tsutsui 	int i;
    125   1.1   thorpej 
    126   1.1   thorpej 	/*
    127   1.1   thorpej 	 * We want netopen() to ask for IP address, etc, rather
    128   1.1   thorpej 	 * that using bootparams.
    129   1.1   thorpej 	 */
    130   1.1   thorpej 	netio_ask = 1;
    131   1.1   thorpej 
    132   1.1   thorpej 	printf("\n");
    133   1.1   thorpej 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    134   1.1   thorpej 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    135   1.3   thorpej 	printf(">> HP 9000/%s SPU\n", getmachineid());
    136   1.1   thorpej 	gethelp();
    137   1.1   thorpej 
    138   1.1   thorpej 	for (;;) {
    139   1.1   thorpej 		printf("sys_inst> ");
    140  1.11   tsutsui 		memset(line, 0, sizeof(line));
    141   1.1   thorpej 		gets(line);
    142   1.1   thorpej 		if (line[0] == '\n' || line[0] == '\0')
    143   1.1   thorpej 			continue;
    144   1.1   thorpej 
    145   1.1   thorpej 		for (i = 0; i < NCMDS; ++i)
    146   1.1   thorpej 			if (strcmp(line, inst_commands[i].ic_cmd) == 0) {
    147   1.1   thorpej 				(*inst_commands[i].ic_func)();
    148   1.1   thorpej 				break;
    149   1.1   thorpej 			}
    150   1.1   thorpej 
    151   1.1   thorpej 
    152   1.1   thorpej 		if (i == NCMDS)
    153   1.1   thorpej 			printf("unknown command: %s\n", line);
    154   1.1   thorpej 	}
    155   1.1   thorpej }
    156   1.1   thorpej 
    157   1.1   thorpej void
    158  1.13   tsutsui gethelp(void)
    159   1.1   thorpej {
    160   1.1   thorpej 	int i;
    161   1.1   thorpej 
    162   1.1   thorpej 	printf(">> Available commands:\n");
    163   1.1   thorpej 	for (i = 0; i < NCMDS; ++i)
    164   1.1   thorpej 		printf(">>     %s - %s\n", inst_commands[i].ic_cmd,
    165   1.1   thorpej 		    inst_commands[i].ic_desc);
    166   1.1   thorpej }
    167   1.1   thorpej 
    168   1.1   thorpej /*
    169   1.1   thorpej  * Do all the steps necessary to place a disklabel on a disk.
    170   1.1   thorpej  * Note, this assumes 512 byte sectors.
    171   1.1   thorpej  */
    172   1.1   thorpej void
    173  1.13   tsutsui dsklabel(void)
    174   1.1   thorpej {
    175   1.1   thorpej 	struct disklabel *lp;
    176   1.1   thorpej 	struct open_file *disk_ofp;
    177   1.1   thorpej 	int dfd, error;
    178   1.1   thorpej 	size_t xfersize;
    179   1.1   thorpej 	char block[DEV_BSIZE], diskname[64];
    180   1.1   thorpej 	extern struct open_file files[];
    181   1.1   thorpej 
    182   1.8   thorpej 	printf(
    183   1.8   thorpej "You will be asked several questions about your disk, most of which\n"
    184   1.8   thorpej "require prior knowledge of the disk's geometry.  There is no easy way\n"
    185   1.8   thorpej "for the system to provide this information for you.  If you do not have\n"
    186   1.8   thorpej "this information, please consult your disk's manual or another\n"
    187   1.8   thorpej "informative source.\n\n");
    188   1.1   thorpej 
    189   1.1   thorpej 	/* Error message printed by opendisk() */
    190   1.1   thorpej 	if (opendisk("Disk to label?", diskname, sizeof(diskname),
    191   1.1   thorpej 	    ('a' + RAW_PART), &dfd))
    192   1.1   thorpej 		return;
    193   1.1   thorpej 
    194   1.1   thorpej 	disk_ofp = &files[dfd];
    195   1.1   thorpej 
    196  1.11   tsutsui 	memset(block, 0, sizeof(block));
    197  1.11   tsutsui 	if ((error = (*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
    198  1.11   tsutsui 	    F_READ, LABELSECTOR, sizeof(block), block, &xfersize)) != 0) {
    199   1.1   thorpej 		printf("cannot read disk %s, errno = %d\n", diskname, error);
    200   1.1   thorpej 		return;
    201   1.1   thorpej 	}
    202   1.1   thorpej 
    203  1.10       wiz 	printf("Successfully read %d bytes from %s\n", xfersize, diskname);
    204   1.1   thorpej 
    205   1.1   thorpej 	lp = (struct disklabel *)((void *)(&block[LABELOFFSET]));
    206   1.1   thorpej 
    207   1.1   thorpej  disklabel_loop:
    208  1.11   tsutsui 	memset(line, 0, sizeof(line));
    209   1.1   thorpej 	printf("(z)ap, (e)dit, (s)how, (w)rite, (d)one > ");
    210   1.1   thorpej 	gets(line);
    211   1.1   thorpej 	if (line[0] == '\n' || line[0] == '\0')
    212   1.1   thorpej 		goto disklabel_loop;
    213   1.1   thorpej 
    214   1.1   thorpej 	switch (line[0]) {
    215   1.1   thorpej 	case 'z':
    216   1.1   thorpej 	case 'Z': {
    217   1.1   thorpej 		char zap[DEV_BSIZE];
    218  1.11   tsutsui 		memset(zap, 0, sizeof(zap));
    219   1.1   thorpej 		(void)(*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
    220   1.1   thorpej 		    F_WRITE, LABELSECTOR, sizeof(zap), zap, &xfersize);
    221   1.1   thorpej 		}
    222   1.1   thorpej 		goto out;
    223   1.1   thorpej 		/* NOTREACHED */
    224   1.1   thorpej 
    225   1.1   thorpej 	case 'e':
    226   1.1   thorpej 	case 'E':
    227   1.1   thorpej 		disklabel_edit(lp);
    228   1.1   thorpej 		break;
    229   1.1   thorpej 
    230   1.1   thorpej 	case 's':
    231   1.1   thorpej 	case 'S':
    232   1.1   thorpej 		disklabel_show(lp);
    233   1.1   thorpej 		break;
    234   1.1   thorpej 
    235   1.1   thorpej 	case 'w':
    236   1.1   thorpej 	case 'W':
    237   1.1   thorpej 		/*
    238   1.1   thorpej 		 * Error message will be displayed by disklabel_write()
    239   1.1   thorpej 		 */
    240   1.1   thorpej 		if (disklabel_write(block, sizeof(block), disk_ofp))
    241   1.1   thorpej 			goto out;
    242   1.1   thorpej 		else
    243  1.10       wiz 			printf("Successfully wrote label to %s\n", diskname);
    244   1.1   thorpej 		break;
    245   1.1   thorpej 
    246   1.1   thorpej 	case 'd':
    247   1.1   thorpej 	case 'D':
    248   1.1   thorpej 		goto out;
    249   1.1   thorpej 		/* NOTREACHED */
    250   1.1   thorpej 
    251   1.1   thorpej 	default:
    252   1.9       wiz 		printf("unknown command: %s\n", line);
    253   1.1   thorpej 	}
    254   1.1   thorpej 
    255   1.1   thorpej 	goto disklabel_loop;
    256   1.1   thorpej 	/* NOTREACHED */
    257   1.1   thorpej 
    258   1.1   thorpej  out:
    259   1.1   thorpej 	/*
    260   1.1   thorpej 	 * Close disk.  Marks disk `not alive' so that partition
    261   1.1   thorpej 	 * information will be reloaded upon next open.
    262   1.1   thorpej 	 */
    263   1.1   thorpej 	(void)close(dfd);
    264   1.1   thorpej }
    265   1.1   thorpej 
    266   1.1   thorpej #define GETNUM(out, num)						\
    267   1.1   thorpej 	printf((out), (num));						\
    268  1.11   tsutsui 	memset(line, 0, sizeof(line));					\
    269   1.1   thorpej 	gets(line);							\
    270   1.1   thorpej 	if (line[0])							\
    271   1.1   thorpej 		(num) = atoi(line);
    272   1.1   thorpej 
    273   1.1   thorpej #define GETNUM2(out, num1, num2)					\
    274   1.1   thorpej 	printf((out), (num1), (num2));					\
    275  1.11   tsutsui 	memset(line, 0, sizeof(line));					\
    276   1.1   thorpej 	gets(line);							\
    277   1.1   thorpej 	if (line[0])							\
    278   1.1   thorpej 		(num2) = atoi(line);
    279   1.1   thorpej 
    280   1.1   thorpej #define GETSTR(out, str)						\
    281   1.1   thorpej 	printf((out), (str));						\
    282  1.11   tsutsui 	memset(line, 0, sizeof(line));					\
    283   1.1   thorpej 	gets(line);							\
    284   1.1   thorpej 	if (line[0])							\
    285   1.1   thorpej 		strcpy((str), line);
    286   1.1   thorpej 
    287   1.1   thorpej #define FLAGS(out, flag)						\
    288   1.1   thorpej 	printf((out), lp->d_flags & (flag) ? 'y' : 'n');		\
    289  1.11   tsutsui 	memset(line, 0, sizeof(line));					\
    290   1.1   thorpej 	gets(line);							\
    291   1.1   thorpej 	if (line[0] == 'y' || line[0] == 'Y')				\
    292   1.1   thorpej 		lp->d_flags |= (flag);					\
    293   1.1   thorpej 	else								\
    294   1.1   thorpej 		lp->d_flags &= ~(flag);
    295   1.1   thorpej 
    296   1.5   thorpej struct fsname_to_type {
    297   1.5   thorpej 	const char *name;
    298  1.12   tsutsui 	uint8_t type;
    299   1.5   thorpej } n_to_t[] = {
    300   1.5   thorpej 	{ "unused",	FS_UNUSED },
    301   1.5   thorpej 	{ "ffs",	FS_BSDFFS },
    302   1.5   thorpej 	{ "swap",	FS_SWAP },
    303   1.5   thorpej 	{ "boot",	FS_BOOT },
    304   1.5   thorpej 	{ NULL,		0 },
    305   1.5   thorpej };
    306   1.5   thorpej 
    307   1.5   thorpej void
    308  1.13   tsutsui get_fstype(struct disklabel *lp, int partno)
    309   1.5   thorpej {
    310   1.5   thorpej 	static int blocksize = 8192;	/* XXX */
    311   1.5   thorpej 	struct partition *pp = &lp->d_partitions[partno];
    312   1.5   thorpej 	struct fsname_to_type *np;
    313   1.5   thorpej 	int fragsize;
    314   1.5   thorpej 	char line[80], str[80];
    315   1.5   thorpej 
    316   1.5   thorpej 	if (pp->p_size == 0) {
    317   1.5   thorpej 		/*
    318   1.5   thorpej 		 * No need to bother asking for a zero-sized partition.
    319   1.5   thorpej 		 */
    320   1.5   thorpej 		pp->p_fstype = FS_UNUSED;
    321   1.5   thorpej 		return;
    322   1.5   thorpej 	}
    323   1.5   thorpej 
    324   1.5   thorpej 	/*
    325   1.5   thorpej 	 * Select a default.
    326   1.5   thorpej 	 * XXX Should we check what might be in the label already?
    327   1.5   thorpej 	 */
    328   1.5   thorpej 	if (partno == 1)
    329   1.5   thorpej 		strcpy(str, "swap");
    330   1.5   thorpej 	else if (partno == RAW_PART)
    331   1.5   thorpej 		strcpy(str, "boot");
    332   1.5   thorpej 	else
    333   1.5   thorpej 		strcpy(str, "ffs");
    334   1.5   thorpej 
    335   1.5   thorpej  again:
    336   1.5   thorpej 	GETSTR("             fstype? [%s] ", str);
    337   1.5   thorpej 
    338   1.5   thorpej 	for (np = n_to_t; np->name != NULL; np++)
    339   1.5   thorpej 		if (strcmp(str, np->name) == 0)
    340   1.5   thorpej 			break;
    341   1.5   thorpej 
    342   1.5   thorpej 	if (np->name == NULL) {
    343   1.5   thorpej 		printf("Please use one of: ");
    344   1.5   thorpej 		for (np = n_to_t; np->name != NULL; np++)
    345   1.5   thorpej 			printf(" %s", np->name);
    346   1.5   thorpej 		printf(".\n");
    347   1.5   thorpej 		goto again;
    348   1.5   thorpej 	}
    349   1.5   thorpej 
    350   1.5   thorpej 	pp->p_fstype = np->type;
    351   1.5   thorpej 
    352   1.5   thorpej 	if (pp->p_fstype != FS_BSDFFS)
    353   1.5   thorpej 		return;
    354   1.5   thorpej 
    355   1.5   thorpej 	/*
    356   1.5   thorpej 	 * Get additional information needed for FFS.
    357   1.5   thorpej 	 */
    358   1.5   thorpej  ffs_again:
    359   1.5   thorpej 	GETNUM("             FFS block size? [%d] ", blocksize);
    360   1.5   thorpej 	if (blocksize < NBPG || (blocksize % NBPG) != 0) {
    361   1.5   thorpej 		printf("FFS block size must be a multiple of %d.\n", NBPG);
    362   1.5   thorpej 		goto ffs_again;
    363   1.5   thorpej 	}
    364   1.5   thorpej 
    365   1.5   thorpej 	fragsize = blocksize / 8;	/* XXX */
    366   1.5   thorpej 	fragsize = max(fragsize, lp->d_secsize);
    367   1.5   thorpej 	GETNUM("             FFS fragment size? [%d] ", fragsize);
    368   1.5   thorpej 	if (fragsize < lp->d_secsize || (fragsize % lp->d_secsize) != 0) {
    369   1.5   thorpej 		printf("FFS fragment size must be a multiple of sector size"
    370   1.5   thorpej 		    " (%d).\n", lp->d_secsize);
    371   1.5   thorpej 		goto ffs_again;
    372   1.5   thorpej 	}
    373   1.5   thorpej 	if ((blocksize % fragsize) != 0) {
    374   1.5   thorpej 		printf("FFS fragment size must be an even divisor of FFS"
    375   1.5   thorpej 		    " block size (%d).\n", blocksize);
    376   1.5   thorpej 		goto ffs_again;
    377   1.5   thorpej 	}
    378   1.5   thorpej 
    379   1.5   thorpej 	/*
    380   1.5   thorpej 	 * XXX Better sanity checking?
    381   1.5   thorpej 	 */
    382   1.5   thorpej 
    383   1.5   thorpej 	pp->p_frag = blocksize / fragsize;
    384   1.5   thorpej 	pp->p_fsize = fragsize;
    385   1.5   thorpej }
    386   1.5   thorpej 
    387   1.1   thorpej void
    388  1.13   tsutsui disklabel_edit(struct disklabel *lp)
    389   1.1   thorpej {
    390   1.1   thorpej 	int i;
    391   1.1   thorpej 
    392   1.1   thorpej 	printf("Select disk type.  Valid types:\n");
    393   1.1   thorpej 	for (i = 0; i < DKMAXTYPES; i++)
    394   1.1   thorpej 		printf("%d     %s\n", i, dktypenames[i]);
    395   1.1   thorpej 	printf("\n");
    396   1.1   thorpej 
    397   1.1   thorpej 	GETNUM("Disk type (number)? [%d] ", lp->d_type);
    398   1.1   thorpej 	GETSTR("Disk model name? [%s] ", lp->d_typename);
    399   1.1   thorpej 	GETSTR("Disk pack name? [%s] ", lp->d_packname);
    400   1.1   thorpej 	FLAGS("Bad sectoring? [%c] ", D_BADSECT);
    401   1.1   thorpej 	FLAGS("Ecc? [%c] ", D_ECC);
    402   1.1   thorpej 	FLAGS("Removable? [%c] ", D_REMOVABLE);
    403   1.1   thorpej 
    404   1.1   thorpej 	printf("\n");
    405   1.1   thorpej 
    406   1.1   thorpej 	GETNUM("Interleave? [%d] ", lp->d_interleave);
    407   1.1   thorpej 	GETNUM("Rpm? [%d] ", lp->d_rpm);
    408   1.1   thorpej 	GETNUM("Trackskew? [%d] ", lp->d_trackskew);
    409   1.1   thorpej 	GETNUM("Cylinderskew? [%d] ", lp->d_cylskew);
    410   1.1   thorpej 	GETNUM("Headswitch? [%d] ", lp->d_headswitch);
    411   1.1   thorpej 	GETNUM("Track-to-track? [%d] ", lp->d_trkseek);
    412   1.1   thorpej 	GETNUM("Drivedata 0? [%d] ", lp->d_drivedata[0]);
    413   1.1   thorpej 	GETNUM("Drivedata 1? [%d] ", lp->d_drivedata[1]);
    414   1.1   thorpej 	GETNUM("Drivedata 2? [%d] ", lp->d_drivedata[2]);
    415   1.1   thorpej 	GETNUM("Drivedata 3? [%d] ", lp->d_drivedata[3]);
    416   1.1   thorpej 	GETNUM("Drivedata 4? [%d] ", lp->d_drivedata[4]);
    417   1.1   thorpej 
    418   1.1   thorpej 	printf("\n");
    419   1.1   thorpej 
    420   1.1   thorpej 	GETNUM("Bytes/sector? [%d] ", lp->d_secsize);
    421   1.1   thorpej 	GETNUM("Sectors/track? [%d] ", lp->d_nsectors);
    422   1.1   thorpej 	GETNUM("Tracks/cylinder? [%d] ", lp->d_ntracks);
    423   1.6    scottr 	if (lp->d_secpercyl == 0)
    424   1.6    scottr 		lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    425   1.1   thorpej 	GETNUM("Sectors/cylinder? [%d] ", lp->d_secpercyl);
    426   1.1   thorpej 	GETNUM("Cylinders? [%d] ", lp->d_ncylinders);
    427   1.6    scottr 	if (lp->d_secperunit == 0)
    428   1.6    scottr 		lp->d_secperunit = lp->d_ncylinders * lp->d_secpercyl;
    429   1.6    scottr 	GETNUM("Total sectors? [%d] ", lp->d_secperunit);
    430   1.1   thorpej 
    431   1.8   thorpej 	printf(
    432   1.8   thorpej "Enter partition table.  Note, sizes and offsets are in sectors.\n\n");
    433   1.1   thorpej 
    434   1.1   thorpej 	lp->d_npartitions = MAXPARTITIONS;
    435   1.1   thorpej 	for (i = 0; i < lp->d_npartitions; ++i) {
    436   1.1   thorpej 		GETNUM2("%c partition: offset? [%d] ", ('a' + i),
    437   1.1   thorpej 		    lp->d_partitions[i].p_offset);
    438   1.1   thorpej 		GETNUM("             size? [%d] ", lp->d_partitions[i].p_size);
    439   1.5   thorpej 		get_fstype(lp, i);
    440   1.1   thorpej 	}
    441   1.1   thorpej 
    442   1.1   thorpej 	/* Perform magic. */
    443   1.1   thorpej 	lp->d_magic = lp->d_magic2 = DISKMAGIC;
    444   1.1   thorpej 
    445   1.1   thorpej 	/* Calculate disklabel checksum. */
    446   1.1   thorpej 	lp->d_checksum = 0;
    447   1.1   thorpej 	lp->d_checksum = dkcksum(lp);
    448   1.1   thorpej }
    449   1.1   thorpej 
    450   1.1   thorpej void
    451  1.13   tsutsui disklabel_show(struct disklabel *lp)
    452   1.1   thorpej {
    453  1.11   tsutsui 	int i;
    454   1.1   thorpej 	struct partition *pp;
    455   1.1   thorpej 
    456   1.1   thorpej 	/*
    457   1.1   thorpej 	 * Check for valid disklabel.
    458   1.1   thorpej 	 */
    459   1.1   thorpej 	if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
    460   1.1   thorpej 		printf("No disklabel to show.\n");
    461   1.1   thorpej 		return;
    462   1.1   thorpej 	}
    463   1.1   thorpej 
    464   1.1   thorpej 	if (lp->d_npartitions > MAXPARTITIONS || dkcksum(lp) != 0) {
    465   1.1   thorpej 		printf("Corrupted disklabel.\n");
    466   1.1   thorpej 		return;
    467   1.1   thorpej 	}
    468   1.1   thorpej 
    469   1.1   thorpej 	printf("\ndisk type %d (%s), %s: %s%s%s\n", lp->d_type,
    470   1.1   thorpej 	    lp->d_type < DKMAXTYPES ? dktypenames[lp->d_type] :
    471   1.1   thorpej 	    dktypenames[0], lp->d_typename,
    472   1.1   thorpej 	    (lp->d_flags & D_REMOVABLE) ? " removable" : "",
    473   1.1   thorpej 	    (lp->d_flags & D_ECC) ? " ecc" : "",
    474   1.1   thorpej 	    (lp->d_flags & D_BADSECT) ? " badsect" : "");
    475   1.1   thorpej 
    476   1.1   thorpej 	printf("interleave %d, rpm %d, trackskew %d, cylinderskew %d\n",
    477   1.1   thorpej 	    lp->d_interleave, lp->d_rpm, lp->d_trackskew, lp->d_cylskew);
    478   1.1   thorpej 
    479   1.1   thorpej 	printf("headswitch %d, track-to-track %d, drivedata: %d %d %d %d %d\n",
    480   1.1   thorpej 	    lp->d_headswitch, lp->d_trkseek, lp->d_drivedata[0],
    481   1.1   thorpej 	    lp->d_drivedata[1], lp->d_drivedata[2], lp->d_drivedata[3],
    482   1.1   thorpej 	    lp->d_drivedata[4]);
    483   1.1   thorpej 
    484   1.1   thorpej 	printf("\nbytes/sector: %d\n", lp->d_secsize);
    485   1.1   thorpej 	printf("sectors/track: %d\n", lp->d_nsectors);
    486   1.1   thorpej 	printf("tracks/cylinder: %d\n", lp->d_ntracks);
    487   1.1   thorpej 	printf("sectors/cylinder: %d\n", lp->d_secpercyl);
    488   1.1   thorpej 	printf("cylinders: %d\n", lp->d_ncylinders);
    489   1.6    scottr 	printf("total sectors: %d\n", lp->d_secperunit);
    490   1.1   thorpej 
    491   1.1   thorpej 	printf("\n%d partitions:\n", lp->d_npartitions);
    492   1.1   thorpej 	printf("     size   offset\n");
    493   1.1   thorpej 	pp = lp->d_partitions;
    494   1.1   thorpej 	for (i = 0; i < lp->d_npartitions; i++) {
    495   1.1   thorpej 		printf("%c:   %d,    %d\n", 97 + i, lp->d_partitions[i].p_size,
    496   1.1   thorpej 		    lp->d_partitions[i].p_offset);
    497   1.1   thorpej 	}
    498   1.1   thorpej 	printf("\n");
    499   1.1   thorpej }
    500   1.1   thorpej 
    501   1.1   thorpej int
    502  1.13   tsutsui disklabel_write(char *block, int len, struct open_file *ofp)
    503   1.1   thorpej {
    504   1.1   thorpej 	int error = 0;
    505   1.1   thorpej 	size_t xfersize;
    506   1.1   thorpej 
    507  1.11   tsutsui 	if ((error = (*ofp->f_dev->dv_strategy)(ofp->f_devdata, F_WRITE,
    508  1.11   tsutsui 	    LABELSECTOR, len, block, &xfersize)) != 0)
    509   1.1   thorpej 		printf("cannot write disklabel, errno = %d\n", error);
    510   1.1   thorpej 
    511   1.1   thorpej 	return (error);
    512   1.1   thorpej }
    513   1.1   thorpej 
    514   1.1   thorpej int
    515  1.13   tsutsui opendisk(char *question, char *diskname, int len, char partition, int *fdp)
    516   1.1   thorpej {
    517  1.11   tsutsui 	char fulldiskname[64];
    518  1.11   tsutsui 	int i;
    519   1.1   thorpej 
    520   1.1   thorpej  getdiskname:
    521   1.1   thorpej 	printf("%s ", question);
    522  1.11   tsutsui 	memset(diskname, 0, len);
    523  1.11   tsutsui 	memset(fulldiskname, 0, sizeof(fulldiskname));
    524   1.1   thorpej 	gets(diskname);
    525   1.1   thorpej 	if (diskname[0] == '\n' || diskname[0] == '\0')
    526   1.1   thorpej 		goto getdiskname;
    527   1.1   thorpej 
    528   1.1   thorpej 	/*
    529   1.1   thorpej 	 * devopen() is picky.  Make sure it gets the sort of string it
    530   1.1   thorpej 	 * wants.
    531   1.1   thorpej 	 */
    532  1.11   tsutsui 	memcpy(fulldiskname, diskname,
    533   1.1   thorpej 	    len < sizeof(fulldiskname) ? len : sizeof(fulldiskname));
    534   1.1   thorpej 	for (i = 0; fulldiskname[i + 1] != '\0'; ++i)
    535   1.1   thorpej 		/* Nothing. */ ;
    536   1.1   thorpej 	if (fulldiskname[i] < '0' || fulldiskname[i] > '9') {
    537   1.1   thorpej 		printf("invalid disk name %s\n", diskname);
    538   1.1   thorpej 		goto getdiskname;
    539   1.1   thorpej 	}
    540   1.1   thorpej 	fulldiskname[++i] = partition; fulldiskname[++i] = ':';
    541   1.1   thorpej 
    542   1.1   thorpej 	/*
    543   1.1   thorpej 	 * We always open for writing.
    544   1.1   thorpej 	 */
    545   1.1   thorpej 	if ((*fdp = open(fulldiskname, 1)) < 0) {
    546   1.1   thorpej 		printf("cannot open %s\n", diskname);
    547  1.11   tsutsui 		return 1;
    548   1.1   thorpej 	}
    549   1.1   thorpej 
    550  1.11   tsutsui 	return 0;
    551   1.1   thorpej }
    552   1.1   thorpej 
    553   1.1   thorpej /*
    554   1.1   thorpej  * Copy a miniroot image from an NFS server or tape to the `b' partition
    555   1.1   thorpej  * of the specified disk.  Note, this assumes 512 byte sectors.
    556   1.1   thorpej  */
    557   1.1   thorpej void
    558  1.13   tsutsui miniroot(void)
    559   1.1   thorpej {
    560   1.1   thorpej 	int sfd, dfd, i, nblks;
    561   1.1   thorpej 	char diskname[64], minirootname[128];
    562   1.1   thorpej 	char block[DEV_BSIZE];
    563   1.1   thorpej 	char tapename[64];
    564   1.2   thorpej 	int fileno, ignoreshread, eof, len;
    565   1.1   thorpej 	struct stat st;
    566   1.1   thorpej 	size_t xfersize;
    567   1.1   thorpej 	struct open_file *disk_ofp;
    568   1.1   thorpej 	extern struct open_file files[];
    569   1.1   thorpej 
    570   1.1   thorpej 	/* Error message printed by opendisk() */
    571   1.1   thorpej 	if (opendisk("Disk for miniroot?", diskname, sizeof(diskname),
    572   1.1   thorpej 	    'b', &dfd))
    573   1.1   thorpej 		return;
    574   1.1   thorpej 
    575   1.1   thorpej 	disk_ofp = &files[dfd];
    576   1.1   thorpej 
    577   1.1   thorpej  getsource:
    578   1.1   thorpej 	printf("Source? (N)FS, (t)ape, (d)one > ");
    579  1.11   tsutsui 	memset(line, 0, sizeof(line));
    580   1.1   thorpej 	gets(line);
    581   1.1   thorpej 	if (line[0] == '\0')
    582   1.1   thorpej 		goto getsource;
    583   1.1   thorpej 
    584   1.1   thorpej 	switch (line[0]) {
    585   1.1   thorpej 	case 'n':
    586   1.1   thorpej 	case 'N':
    587   1.1   thorpej  name_of_nfs_miniroot:
    588   1.1   thorpej 		printf("Name of miniroot file? ");
    589  1.11   tsutsui 		memset(line, 0, sizeof(line));
    590  1.11   tsutsui 		memset(minirootname, 0, sizeof(minirootname));
    591   1.1   thorpej 		gets(line);
    592   1.1   thorpej 		if (line[0] == '\0')
    593   1.1   thorpej 			goto name_of_nfs_miniroot;
    594   1.1   thorpej 		(void)strcat(minirootname, "le0a:");
    595   1.1   thorpej 		(void)strcat(minirootname, line);
    596   1.1   thorpej 		if ((sfd = open(minirootname, 0)) < 0) {
    597   1.1   thorpej 			printf("can't open %s\n", line);
    598   1.1   thorpej 			return;
    599   1.1   thorpej 		}
    600   1.1   thorpej 
    601   1.1   thorpej 		/*
    602   1.2   thorpej 		 * Find out how big the miniroot is... we can't
    603   1.2   thorpej 		 * check for size because it may be compressed.
    604   1.1   thorpej 		 */
    605   1.2   thorpej 		ignoreshread = 1;
    606   1.1   thorpej 		if (fstat(sfd, &st) < 0) {
    607   1.1   thorpej 			printf("can't stat %s\n", line);
    608   1.1   thorpej 			goto done;
    609   1.1   thorpej 		}
    610   1.1   thorpej 		nblks = (int)(st.st_size / sizeof(block));
    611   1.1   thorpej 
    612   1.2   thorpej 		printf("Copying miniroot from %s to %s...", line,
    613   1.1   thorpej 		    diskname);
    614   1.1   thorpej 		break;
    615   1.1   thorpej 
    616   1.1   thorpej 	case 't':
    617   1.1   thorpej 	case 'T':
    618   1.1   thorpej  name_of_tape_miniroot:
    619   1.1   thorpej 		printf("Which tape device? ");
    620  1.11   tsutsui 		memset(line, 0, sizeof(line));
    621  1.11   tsutsui 		memset(minirootname, 0, sizeof(minirootname));
    622  1.11   tsutsui 		memset(tapename, 0, sizeof(tapename));
    623   1.1   thorpej 		gets(line);
    624   1.1   thorpej 		if (line[0] == '\0')
    625   1.1   thorpej 			goto name_of_tape_miniroot;
    626   1.1   thorpej 		strcat(minirootname, line);
    627   1.1   thorpej 		strcat(tapename, line);
    628   1.1   thorpej 
    629   1.1   thorpej 		printf("File number (first == 1)? ");
    630  1.11   tsutsui 		memset(line, 0, sizeof(line));
    631   1.1   thorpej 		gets(line);
    632   1.1   thorpej 		fileno = a2int(line);
    633   1.1   thorpej 		if (fileno < 1 || fileno > 8) {
    634   1.1   thorpej 			printf("Invalid file number: %s\n", line);
    635   1.1   thorpej 			goto getsource;
    636   1.1   thorpej 		}
    637   1.1   thorpej 		for (i = 0; i < sizeof(minirootname); ++i) {
    638   1.1   thorpej 			if (minirootname[i] == '\0')
    639   1.1   thorpej 				break;
    640   1.1   thorpej 		}
    641   1.1   thorpej 		if (i == sizeof(minirootname) ||
    642   1.1   thorpej 		    (sizeof(minirootname) - i) < 8) {
    643   1.1   thorpej 			printf("Invalid device name: %s\n", tapename);
    644   1.1   thorpej 			goto getsource;
    645   1.1   thorpej 		}
    646   1.1   thorpej 		minirootname[i++] = 'a' + (fileno - 1);
    647   1.1   thorpej 		minirootname[i++] = ':';
    648   1.1   thorpej 		strcat(minirootname, "XXX");	/* lameness in open() */
    649   1.1   thorpej 
    650   1.2   thorpej 		ignoreshread = 0;
    651   1.1   thorpej 		printf("Copy how many %d byte blocks? ", DEV_BSIZE);
    652  1.11   tsutsui 		memset(line, 0, sizeof(line));
    653   1.1   thorpej 		gets(line);
    654   1.1   thorpej 		nblks = a2int(line);
    655   1.1   thorpej 		if (nblks < 0) {
    656   1.1   thorpej 			printf("Invalid block count: %s\n", line);
    657   1.1   thorpej 			goto getsource;
    658   1.1   thorpej 		} else if (nblks == 0) {
    659   1.1   thorpej 			printf("Zero blocks?  Ok, aborting.\n");
    660   1.1   thorpej 			return;
    661   1.1   thorpej 		}
    662   1.1   thorpej 
    663   1.1   thorpej 		if ((sfd = open(minirootname, 0)) < 0) {
    664   1.1   thorpej 			printf("can't open %s file %c\n", tapename, fileno);
    665   1.1   thorpej 			return;
    666   1.1   thorpej 		}
    667   1.1   thorpej 
    668   1.2   thorpej 		printf("Copying %s file %d to %s...", tapename, fileno,
    669   1.1   thorpej 		    diskname);
    670   1.1   thorpej 		break;
    671   1.1   thorpej 
    672   1.1   thorpej 	case 'd':
    673   1.1   thorpej 	case 'D':
    674   1.1   thorpej 		return;
    675   1.1   thorpej 
    676   1.1   thorpej 	default:
    677   1.1   thorpej 		printf("Unknown source: %s\n", line);
    678   1.1   thorpej 		goto getsource;
    679   1.1   thorpej 	}
    680   1.1   thorpej 
    681   1.1   thorpej 	/*
    682   1.1   thorpej 	 * Copy loop...
    683   1.1   thorpej 	 * This is fairly slow... if someone wants to speed it
    684   1.1   thorpej 	 * up, they'll get no complaints from me.
    685   1.1   thorpej 	 */
    686   1.2   thorpej 	for (i = 0, eof = 0; i < nblks || ignoreshread == 0; i++) {
    687   1.2   thorpej 		if ((len = read(sfd, block, sizeof(block))) < 0) {
    688   1.2   thorpej 			printf("Read error, errno = %d\n", errno);
    689   1.2   thorpej 			goto out;
    690   1.2   thorpej 		}
    691   1.2   thorpej 
    692   1.2   thorpej 		/*
    693   1.2   thorpej 		 * Check for end-of-file.
    694   1.2   thorpej 		 */
    695   1.2   thorpej 		if (len == 0)
    696   1.1   thorpej 			goto done;
    697   1.2   thorpej 		else if (len < sizeof(block))
    698   1.2   thorpej 			eof = 1;
    699   1.2   thorpej 
    700   1.1   thorpej 		if ((*disk_ofp->f_dev->dv_strategy)(disk_ofp->f_devdata,
    701   1.2   thorpej 		    F_WRITE, i, len, block, &xfersize) || xfersize != len) {
    702   1.1   thorpej 			printf("Bad write at block %d, errno = %d\n",
    703   1.1   thorpej 			    i, errno);
    704   1.2   thorpej 			goto out;
    705   1.2   thorpej 		}
    706   1.2   thorpej 
    707   1.2   thorpej 		if (eof)
    708   1.1   thorpej 			goto done;
    709   1.1   thorpej 	}
    710   1.2   thorpej  done:
    711   1.1   thorpej 	printf("done\n");
    712   1.1   thorpej 
    713   1.1   thorpej 	printf("Successfully copied miniroot image.\n");
    714   1.1   thorpej 
    715   1.2   thorpej  out:
    716   1.1   thorpej 	close(sfd);
    717   1.1   thorpej 	close(dfd);
    718   1.1   thorpej }
    719   1.1   thorpej 
    720   1.1   thorpej /*
    721   1.1   thorpej  * Boot the kernel from the miniroot image into single-user.
    722   1.1   thorpej  */
    723   1.1   thorpej void
    724  1.13   tsutsui bootmini(void)
    725   1.1   thorpej {
    726   1.1   thorpej 	char diskname[64], bootname[64];
    727   1.1   thorpej 	int i;
    728   1.1   thorpej 
    729   1.1   thorpej  getdiskname:
    730   1.1   thorpej 	printf("Disk to boot from? ");
    731  1.11   tsutsui 	memset(diskname, 0, sizeof(diskname));
    732  1.11   tsutsui 	memset(bootname, 0, sizeof(bootname));
    733   1.1   thorpej 	gets(diskname);
    734   1.1   thorpej 	if (diskname[0] == '\n' || diskname[0] == '\0')
    735   1.1   thorpej 		goto getdiskname;
    736   1.1   thorpej 
    737   1.1   thorpej 	/*
    738   1.1   thorpej 	 * devopen() is picky.  Make sure it gets the sort of string it
    739   1.1   thorpej 	 * wants.
    740   1.1   thorpej 	 */
    741   1.1   thorpej 	(void)strcat(bootname, diskname);
    742   1.1   thorpej 	for (i = 0; bootname[i + 1] != '\0'; ++i)
    743   1.1   thorpej 		/* Nothing. */ ;
    744   1.1   thorpej 	if (bootname[i] < '0' || bootname[i] > '9') {
    745   1.1   thorpej 		printf("invalid disk name %s\n", diskname);
    746   1.1   thorpej 		goto getdiskname;
    747   1.1   thorpej 	}
    748   1.1   thorpej 	bootname[++i] = 'b'; bootname[++i] = ':';
    749   1.1   thorpej 	(void)strcat(bootname, kernel_name);
    750   1.1   thorpej 
    751   1.1   thorpej 	howto = RB_SINGLE;	/* _Always_ */
    752   1.1   thorpej 
    753   1.1   thorpej 	printf("booting: %s -s\n", bootname);
    754   1.7  gmcgarry 	exec_hp300(bootname, (u_long)lowram, howto);
    755   1.1   thorpej 	printf("boot: %s\n", strerror(errno));
    756   1.1   thorpej }
    757   1.1   thorpej 
    758   1.1   thorpej /*
    759   1.1   thorpej  * Reset the system.
    760   1.1   thorpej  */
    761   1.1   thorpej void
    762  1.13   tsutsui resetsys(void)
    763   1.1   thorpej {
    764   1.1   thorpej 
    765   1.1   thorpej 	call_req_reboot();
    766   1.1   thorpej 	printf("panic: can't reboot, halting\n");
    767  1.15     perry 	__asm("stop #0x2700");
    768   1.1   thorpej }
    769   1.1   thorpej 
    770   1.1   thorpej /*
    771   1.1   thorpej  * XXX Should have a generic atoi for libkern/libsa.
    772   1.1   thorpej  */
    773   1.1   thorpej int
    774  1.13   tsutsui a2int(char *cp)
    775   1.1   thorpej {
    776   1.1   thorpej 	int i = 0;
    777   1.1   thorpej 
    778   1.1   thorpej 	if (*cp == '\0')
    779   1.1   thorpej 		return (-1);
    780   1.1   thorpej 
    781   1.1   thorpej 	while (*cp != '\0')
    782   1.1   thorpej 		i = i * 10 + *cp++ - '0';
    783   1.1   thorpej 	return (i);
    784   1.1   thorpej }
    785