Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.1
      1  1.1  tsubai /*	$NetBSD: installboot.c,v 1.1 1998/06/12 21:07:24 tsubai Exp $ */
      2  1.1  tsubai 
      3  1.1  tsubai /*
      4  1.1  tsubai  * Copyright (c) 1994 Paul Kranenburg
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsubai  * modification, are permitted provided that the following conditions
      9  1.1  tsubai  * are met:
     10  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     16  1.1  tsubai  *    must display the following acknowledgement:
     17  1.1  tsubai  *      This product includes software developed by Paul Kranenburg.
     18  1.1  tsubai  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  tsubai  *    derived from this software without specific prior written permission
     20  1.1  tsubai  *
     21  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  tsubai  */
     32  1.1  tsubai 
     33  1.1  tsubai #include <sys/param.h>
     34  1.1  tsubai #include <sys/mount.h>
     35  1.1  tsubai #include <sys/time.h>
     36  1.1  tsubai #include <sys/stat.h>
     37  1.1  tsubai #include <sys/sysctl.h>
     38  1.1  tsubai #include <ufs/ufs/dinode.h>
     39  1.1  tsubai #include <ufs/ufs/dir.h>
     40  1.1  tsubai #include <ufs/ffs/fs.h>
     41  1.1  tsubai #include <err.h>
     42  1.1  tsubai #ifdef BOOT_AOUT
     43  1.1  tsubai #include <a.out.h>
     44  1.1  tsubai #endif
     45  1.1  tsubai #include <sys/exec_elf.h>
     46  1.1  tsubai #include <fcntl.h>
     47  1.1  tsubai #include <nlist.h>
     48  1.1  tsubai #include <stdlib.h>
     49  1.1  tsubai #include <stdio.h>
     50  1.1  tsubai #include <string.h>
     51  1.1  tsubai #include <unistd.h>
     52  1.1  tsubai 
     53  1.1  tsubai int	verbose, nowrite;
     54  1.1  tsubai char	*boot, *proto, *dev;
     55  1.1  tsubai 
     56  1.1  tsubai #define BOOTSECTOR_OFFSET 2048
     57  1.1  tsubai 
     58  1.1  tsubai struct nlist nl[] = {
     59  1.1  tsubai #define X_BLOCKTABLE	0
     60  1.1  tsubai 	{"_block_table"},
     61  1.1  tsubai #define X_BLOCKCOUNT	1
     62  1.1  tsubai 	{"_block_count"},
     63  1.1  tsubai #define X_BLOCKSIZE	2
     64  1.1  tsubai 	{"_block_size"},
     65  1.1  tsubai 	{NULL}
     66  1.1  tsubai };
     67  1.1  tsubai 
     68  1.1  tsubai daddr_t	*block_table;		/* block number array in prototype image */
     69  1.1  tsubai int32_t	*block_count_p;		/* size of this array */
     70  1.1  tsubai int32_t	*block_size_p;		/* filesystem block size */
     71  1.1  tsubai int32_t	max_block_count;
     72  1.1  tsubai 
     73  1.1  tsubai char		*loadprotoblocks __P((char *, long *));
     74  1.1  tsubai int		loadblocknums __P((char *, int));
     75  1.1  tsubai static void	devread __P((int, void *, daddr_t, size_t, char *));
     76  1.1  tsubai static void	usage __P((void));
     77  1.1  tsubai int 		main __P((int, char *[]));
     78  1.1  tsubai 
     79  1.1  tsubai 
     80  1.1  tsubai static void
     81  1.1  tsubai usage()
     82  1.1  tsubai {
     83  1.1  tsubai 	fprintf(stderr,
     84  1.1  tsubai 		"usage: installboot [-n] [-v] <boot> <proto> <device>\n");
     85  1.1  tsubai 	exit(1);
     86  1.1  tsubai }
     87  1.1  tsubai 
     88  1.1  tsubai int
     89  1.1  tsubai main(argc, argv)
     90  1.1  tsubai 	int argc;
     91  1.1  tsubai 	char *argv[];
     92  1.1  tsubai {
     93  1.1  tsubai 	int	c;
     94  1.1  tsubai 	int	devfd;
     95  1.1  tsubai 	char	*protostore;
     96  1.1  tsubai 	long	protosize;
     97  1.1  tsubai 	int	mib[2];
     98  1.1  tsubai 	size_t	size;
     99  1.1  tsubai 
    100  1.1  tsubai 	while ((c = getopt(argc, argv, "vn")) != EOF) {
    101  1.1  tsubai 		switch (c) {
    102  1.1  tsubai 
    103  1.1  tsubai 		case 'n':
    104  1.1  tsubai 			/* Do not actually write the bootblock to disk */
    105  1.1  tsubai 			nowrite = 1;
    106  1.1  tsubai 			break;
    107  1.1  tsubai 		case 'v':
    108  1.1  tsubai 			/* Chat */
    109  1.1  tsubai 			verbose = 1;
    110  1.1  tsubai 			break;
    111  1.1  tsubai 		default:
    112  1.1  tsubai 			usage();
    113  1.1  tsubai 		}
    114  1.1  tsubai 	}
    115  1.1  tsubai 
    116  1.1  tsubai 	if (argc - optind < 3) {
    117  1.1  tsubai 		usage();
    118  1.1  tsubai 	}
    119  1.1  tsubai 
    120  1.1  tsubai 	boot = argv[optind];
    121  1.1  tsubai 	proto = argv[optind + 1];
    122  1.1  tsubai 	dev = argv[optind + 2];
    123  1.1  tsubai 
    124  1.1  tsubai 	if (verbose) {
    125  1.1  tsubai 		printf("boot: %s\n", boot);
    126  1.1  tsubai 		printf("proto: %s\n", proto);
    127  1.1  tsubai 		printf("device: %s\n", dev);
    128  1.1  tsubai 	}
    129  1.1  tsubai 
    130  1.1  tsubai 	/* Load proto blocks into core */
    131  1.1  tsubai 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
    132  1.1  tsubai 		exit(1);
    133  1.1  tsubai 
    134  1.1  tsubai 	/* Open and check raw disk device */
    135  1.1  tsubai 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
    136  1.1  tsubai 		err(1, "open: %s", dev);
    137  1.1  tsubai 
    138  1.1  tsubai 	/* Extract and load block numbers */
    139  1.1  tsubai 	if (loadblocknums(boot, devfd) != 0)
    140  1.1  tsubai 		exit(1);
    141  1.1  tsubai 
    142  1.1  tsubai 	(void)close(devfd);
    143  1.1  tsubai 
    144  1.1  tsubai 	if (nowrite)
    145  1.1  tsubai 		return 0;
    146  1.1  tsubai 
    147  1.1  tsubai 	/* Write patched proto bootblocks into the superblock */
    148  1.1  tsubai 	if (protosize > SBSIZE - DEV_BSIZE)
    149  1.1  tsubai 		errx(1, "proto bootblocks too big");
    150  1.1  tsubai 
    151  1.1  tsubai 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
    152  1.1  tsubai 		err(1, "open: %s", dev);
    153  1.1  tsubai 
    154  1.1  tsubai 	if (lseek(devfd, BOOTSECTOR_OFFSET, SEEK_SET) != BOOTSECTOR_OFFSET)
    155  1.1  tsubai 		err(1, "lseek bootstrap");
    156  1.1  tsubai 
    157  1.1  tsubai 	/* Sync filesystems (to clean in-memory superblock?) */
    158  1.1  tsubai 	sync();
    159  1.1  tsubai 
    160  1.1  tsubai 	if (write(devfd, protostore, protosize) != protosize)
    161  1.1  tsubai 		err(1, "write bootstrap");
    162  1.1  tsubai 	(void)close(devfd);
    163  1.1  tsubai 	return 0;
    164  1.1  tsubai }
    165  1.1  tsubai 
    166  1.1  tsubai char *
    167  1.1  tsubai loadprotoblocks(fname, size)
    168  1.1  tsubai 	char *fname;
    169  1.1  tsubai 	long *size;
    170  1.1  tsubai {
    171  1.1  tsubai 	int	fd, sz;
    172  1.1  tsubai 	char	*bp;
    173  1.1  tsubai 	struct	stat statbuf;
    174  1.1  tsubai #ifdef BOOT_AOUT
    175  1.1  tsubai 	struct	exec *hp;
    176  1.1  tsubai #endif
    177  1.1  tsubai 	long	off;
    178  1.1  tsubai 	Elf32_Ehdr *eh;
    179  1.1  tsubai 	Elf32_Phdr *ph;
    180  1.1  tsubai 
    181  1.1  tsubai 	/* Locate block number array in proto file */
    182  1.1  tsubai 	if (nlist(fname, nl) != 0) {
    183  1.1  tsubai 		warnx("nlist: %s: symbols not found", fname);
    184  1.1  tsubai 		return NULL;
    185  1.1  tsubai 	}
    186  1.1  tsubai #ifdef BOOT_AOUT
    187  1.1  tsubai 	if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
    188  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
    189  1.1  tsubai 		return NULL;
    190  1.1  tsubai 	}
    191  1.1  tsubai 	if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
    192  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
    193  1.1  tsubai 		return NULL;
    194  1.1  tsubai 	}
    195  1.1  tsubai 	if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
    196  1.1  tsubai 		warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
    197  1.1  tsubai 		return NULL;
    198  1.1  tsubai 	}
    199  1.1  tsubai #endif
    200  1.1  tsubai 
    201  1.1  tsubai 	if ((fd = open(fname, O_RDONLY)) < 0) {
    202  1.1  tsubai 		warn("open: %s", fname);
    203  1.1  tsubai 		return NULL;
    204  1.1  tsubai 	}
    205  1.1  tsubai 	if (fstat(fd, &statbuf) != 0) {
    206  1.1  tsubai 		warn("fstat: %s", fname);
    207  1.1  tsubai 		close(fd);
    208  1.1  tsubai 		return NULL;
    209  1.1  tsubai 	}
    210  1.1  tsubai 	if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
    211  1.1  tsubai 		warnx("malloc: %s: no memory", fname);
    212  1.1  tsubai 		close(fd);
    213  1.1  tsubai 		return NULL;
    214  1.1  tsubai 	}
    215  1.1  tsubai 	if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
    216  1.1  tsubai 		warn("read: %s", fname);
    217  1.1  tsubai 		free(bp);
    218  1.1  tsubai 		close(fd);
    219  1.1  tsubai 		return NULL;
    220  1.1  tsubai 	}
    221  1.1  tsubai 	close(fd);
    222  1.1  tsubai 
    223  1.1  tsubai #ifdef BOOT_AOUT
    224  1.1  tsubai 	hp = (struct exec *)bp;
    225  1.1  tsubai #endif
    226  1.1  tsubai 	eh = (Elf32_Ehdr *)bp;
    227  1.1  tsubai 	ph = (Elf32_Phdr *)(bp + eh->e_phoff);
    228  1.1  tsubai 	sz = 1024;
    229  1.1  tsubai 
    230  1.1  tsubai 	/* Calculate the symbols' location within the proto file */
    231  1.1  tsubai 	off = ph->p_offset - eh->e_entry;
    232  1.1  tsubai 	block_table = (daddr_t *)(bp + nl[X_BLOCKTABLE].n_value + off);
    233  1.1  tsubai 	block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
    234  1.1  tsubai 	block_size_p = (int32_t *)(bp + nl[X_BLOCKSIZE].n_value + off);
    235  1.1  tsubai 
    236  1.1  tsubai 	if ((int)block_table & 3) {
    237  1.1  tsubai 		warn("%s: invalid address: block_table = %x",
    238  1.1  tsubai 		     fname, block_table);
    239  1.1  tsubai 		free(bp);
    240  1.1  tsubai 		close(fd);
    241  1.1  tsubai 		return NULL;
    242  1.1  tsubai 	}
    243  1.1  tsubai 	if ((int)block_count_p & 3) {
    244  1.1  tsubai 		warn("%s: invalid address: block_count_p = %x",
    245  1.1  tsubai 		     fname, block_count_p);
    246  1.1  tsubai 		free(bp);
    247  1.1  tsubai 		close(fd);
    248  1.1  tsubai 		return NULL;
    249  1.1  tsubai 	}
    250  1.1  tsubai 	if ((int)block_size_p & 3) {
    251  1.1  tsubai 		warn("%s: invalid address: block_size_p = %x",
    252  1.1  tsubai 		     fname, block_size_p);
    253  1.1  tsubai 		free(bp);
    254  1.1  tsubai 		close(fd);
    255  1.1  tsubai 		return NULL;
    256  1.1  tsubai 	}
    257  1.1  tsubai 	max_block_count = *block_count_p;
    258  1.1  tsubai 
    259  1.1  tsubai 	if (verbose) {
    260  1.1  tsubai 		printf("proto bootblock size %ld\n", sz);
    261  1.1  tsubai 	}
    262  1.1  tsubai 
    263  1.1  tsubai 	/*
    264  1.1  tsubai 	 * We convert the a.out header in-vitro into something that
    265  1.1  tsubai 	 * Sun PROMs understand.
    266  1.1  tsubai 	 * Old-style (sun4) ROMs do not expect a header at all, so
    267  1.1  tsubai 	 * we turn the first two words into code that gets us past
    268  1.1  tsubai 	 * the 32-byte header where the actual code begins. In assembly
    269  1.1  tsubai 	 * speak:
    270  1.1  tsubai 	 *	.word	MAGIC		! a NOP
    271  1.1  tsubai 	 *	ba,a	start		!
    272  1.1  tsubai 	 *	.skip	24		! pad
    273  1.1  tsubai 	 * start:
    274  1.1  tsubai 	 */
    275  1.1  tsubai 
    276  1.1  tsubai 	*size = sz;
    277  1.1  tsubai 	return (bp + 0x74);
    278  1.1  tsubai }
    279  1.1  tsubai 
    280  1.1  tsubai static void
    281  1.1  tsubai devread(fd, buf, blk, size, msg)
    282  1.1  tsubai 	int	fd;
    283  1.1  tsubai 	void	*buf;
    284  1.1  tsubai 	daddr_t	blk;
    285  1.1  tsubai 	size_t	size;
    286  1.1  tsubai 	char	*msg;
    287  1.1  tsubai {
    288  1.1  tsubai 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    289  1.1  tsubai 		err(1, "%s: devread: lseek", msg);
    290  1.1  tsubai 
    291  1.1  tsubai 	if (read(fd, buf, size) != size)
    292  1.1  tsubai 		err(1, "%s: devread: read", msg);
    293  1.1  tsubai }
    294  1.1  tsubai 
    295  1.1  tsubai static char sblock[SBSIZE];
    296  1.1  tsubai 
    297  1.1  tsubai int
    298  1.1  tsubai loadblocknums(boot, devfd)
    299  1.1  tsubai char	*boot;
    300  1.1  tsubai int	devfd;
    301  1.1  tsubai {
    302  1.1  tsubai 	int		i, fd;
    303  1.1  tsubai 	struct	stat	statbuf;
    304  1.1  tsubai 	struct	statfs	statfsbuf;
    305  1.1  tsubai 	struct fs	*fs;
    306  1.1  tsubai 	char		*buf;
    307  1.1  tsubai 	daddr_t		blk, *ap;
    308  1.1  tsubai 	struct dinode	*ip;
    309  1.1  tsubai 	int		ndb;
    310  1.1  tsubai 
    311  1.1  tsubai 	/*
    312  1.1  tsubai 	 * Open 2nd-level boot program and record the block numbers
    313  1.1  tsubai 	 * it occupies on the filesystem represented by `devfd'.
    314  1.1  tsubai 	 */
    315  1.1  tsubai 	if ((fd = open(boot, O_RDONLY)) < 0)
    316  1.1  tsubai 		err(1, "open: %s", boot);
    317  1.1  tsubai 
    318  1.1  tsubai 	if (fstatfs(fd, &statfsbuf) != 0)
    319  1.1  tsubai 		err(1, "statfs: %s", boot);
    320  1.1  tsubai 
    321  1.1  tsubai 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
    322  1.1  tsubai 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
    323  1.1  tsubai 		errx(1, "%s: must be on an FFS filesystem", boot);
    324  1.1  tsubai 	}
    325  1.1  tsubai 
    326  1.1  tsubai 	if (fsync(fd) != 0)
    327  1.1  tsubai 		err(1, "fsync: %s", boot);
    328  1.1  tsubai 
    329  1.1  tsubai 	if (fstat(fd, &statbuf) != 0)
    330  1.1  tsubai 		err(1, "fstat: %s", boot);
    331  1.1  tsubai 
    332  1.1  tsubai 	close(fd);
    333  1.1  tsubai 
    334  1.1  tsubai 	/* Read superblock */
    335  1.1  tsubai 	devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
    336  1.1  tsubai 	fs = (struct fs *)sblock;
    337  1.1  tsubai 
    338  1.1  tsubai 	/* Read inode */
    339  1.1  tsubai 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    340  1.1  tsubai 		errx(1, "No memory for filesystem block");
    341  1.1  tsubai 
    342  1.1  tsubai 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
    343  1.1  tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    344  1.1  tsubai 	ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    345  1.1  tsubai 
    346  1.1  tsubai 	/*
    347  1.1  tsubai 	 * Register filesystem block size.
    348  1.1  tsubai 	 */
    349  1.1  tsubai 	*block_size_p = fs->fs_bsize;
    350  1.1  tsubai 
    351  1.1  tsubai 	/*
    352  1.1  tsubai 	 * Get the block numbers; we don't handle fragments
    353  1.1  tsubai 	 */
    354  1.1  tsubai 	ndb = howmany(ip->di_size, fs->fs_bsize);
    355  1.1  tsubai 	if (ndb > max_block_count)
    356  1.1  tsubai 		errx(1, "%s: Too many blocks", boot);
    357  1.1  tsubai 
    358  1.1  tsubai 	/*
    359  1.1  tsubai 	 * Register block count.
    360  1.1  tsubai 	 */
    361  1.1  tsubai 	*block_count_p = ndb;
    362  1.1  tsubai 
    363  1.1  tsubai 	if (verbose)
    364  1.1  tsubai 		printf("%s: block numbers: ", boot);
    365  1.1  tsubai 	ap = ip->di_db;
    366  1.1  tsubai 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
    367  1.1  tsubai 		blk = fsbtodb(fs, *ap);
    368  1.1  tsubai 		block_table[i] = blk;
    369  1.1  tsubai 		if (verbose)
    370  1.1  tsubai 			printf("%d ", blk);
    371  1.1  tsubai 	}
    372  1.1  tsubai 	if (verbose)
    373  1.1  tsubai 		printf("\n");
    374  1.1  tsubai 
    375  1.1  tsubai 	if (ndb == 0)
    376  1.1  tsubai 		return 0;
    377  1.1  tsubai 
    378  1.1  tsubai 	/*
    379  1.1  tsubai 	 * Just one level of indirections; there isn't much room
    380  1.1  tsubai 	 * for more in the 1st-level bootblocks anyway.
    381  1.1  tsubai 	 */
    382  1.1  tsubai 	if (verbose)
    383  1.1  tsubai 		printf("%s: block numbers (indirect): ", boot);
    384  1.1  tsubai 	blk = ip->di_ib[0];
    385  1.1  tsubai 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    386  1.1  tsubai 	ap = (daddr_t *)buf;
    387  1.1  tsubai 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    388  1.1  tsubai 		blk = fsbtodb(fs, *ap);
    389  1.1  tsubai 		block_table[i] = blk;
    390  1.1  tsubai 		if (verbose)
    391  1.1  tsubai 			printf("%d ", blk);
    392  1.1  tsubai 	}
    393  1.1  tsubai 	if (verbose)
    394  1.1  tsubai 		printf("\n");
    395  1.1  tsubai 
    396  1.1  tsubai 	if (ndb)
    397  1.1  tsubai 		errx(1, "%s: Too many blocks", boot);
    398  1.1  tsubai 	return 0;
    399  1.1  tsubai }
    400