Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.9
      1  1.9        he /*	$NetBSD: installboot.c,v 1.9 2003/04/07 11:45:55 he Exp $ */
      2  1.1     chuck 
      3  1.4        pk /*-
      4  1.4        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1     chuck  * All rights reserved.
      6  1.1     chuck  *
      7  1.4        pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4        pk  * by Paul Kranenburg.
      9  1.4        pk  *
     10  1.1     chuck  * Redistribution and use in source and binary forms, with or without
     11  1.1     chuck  * modification, are permitted provided that the following conditions
     12  1.1     chuck  * are met:
     13  1.1     chuck  * 1. Redistributions of source code must retain the above copyright
     14  1.1     chuck  *    notice, this list of conditions and the following disclaimer.
     15  1.1     chuck  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     chuck  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     chuck  *    documentation and/or other materials provided with the distribution.
     18  1.1     chuck  * 3. All advertising materials mentioning features or use of this software
     19  1.1     chuck  *    must display the following acknowledgement:
     20  1.4        pk  *        This product includes software developed by the NetBSD
     21  1.4        pk  *        Foundation, Inc. and its contributors.
     22  1.4        pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.4        pk  *    contributors may be used to endorse or promote products derived
     24  1.4        pk  *    from this software without specific prior written permission.
     25  1.1     chuck  *
     26  1.4        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.4        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.4        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.4        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.4        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.4        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.4        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.4        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.4        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.4        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.4        pk  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1     chuck  */
     38  1.1     chuck 
     39  1.1     chuck #include <sys/param.h>
     40  1.5  jdolecek #include <sys/cdefs.h>
     41  1.1     chuck #include <sys/mount.h>
     42  1.1     chuck #include <sys/time.h>
     43  1.1     chuck #include <sys/stat.h>
     44  1.1     chuck #include <ufs/ufs/dinode.h>
     45  1.1     chuck #include <ufs/ufs/dir.h>
     46  1.1     chuck #include <ufs/ffs/fs.h>
     47  1.1     chuck #include <err.h>
     48  1.1     chuck #include <fcntl.h>
     49  1.1     chuck #include <nlist.h>
     50  1.1     chuck #include <stdlib.h>
     51  1.1     chuck #include <stdio.h>
     52  1.1     chuck #include <string.h>
     53  1.1     chuck #include <unistd.h>
     54  1.3       scw #include <sys/disklabel.h>
     55  1.1     chuck 
     56  1.6       scw #include "loadfile.h"
     57  1.6       scw 
     58  1.1     chuck int	verbose, nowrite, hflag;
     59  1.1     chuck char	*boot, *proto, *dev;
     60  1.6       scw 
     61  1.1     chuck struct nlist nl[] = {
     62  1.1     chuck #define X_BLOCK_SIZE	0
     63  1.1     chuck #define X_BLOCK_COUNT	1
     64  1.1     chuck #define X_BLOCK_TABLE	2
     65  1.7       scw #ifdef __ELF__
     66  1.6       scw 	{ "block_size" },
     67  1.6       scw 	{ "block_count" },
     68  1.6       scw 	{ "block_table" },
     69  1.7       scw #else
     70  1.7       scw 	{ "_block_size" },
     71  1.7       scw 	{ "_block_count" },
     72  1.7       scw 	{ "_block_table" },
     73  1.7       scw #endif
     74  1.6       scw 	{ NULL }
     75  1.1     chuck };
     76  1.1     chuck 
     77  1.1     chuck int *block_size_p;		/* block size var. in prototype image */
     78  1.1     chuck int *block_count_p;		/* block count var. in prototype image */
     79  1.8      fvdl /* XXX ondisk32 */
     80  1.8      fvdl int32_t	*block_table;		/* block number array in prototype image */
     81  1.1     chuck int	maxblocknum;		/* size of this array */
     82  1.1     chuck 
     83  1.1     chuck 
     84  1.6       scw char		*loadprotoblocks __P((char *, size_t *));
     85  1.1     chuck int		loadblocknums __P((char *, int));
     86  1.1     chuck static void	devread __P((int, void *, daddr_t, size_t, char *));
     87  1.1     chuck static void	usage __P((void));
     88  1.1     chuck int 		main __P((int, char *[]));
     89  1.1     chuck 
     90  1.1     chuck 
     91  1.1     chuck static void
     92  1.1     chuck usage()
     93  1.1     chuck {
     94  1.1     chuck 	fprintf(stderr,
     95  1.1     chuck 		"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
     96  1.1     chuck 	exit(1);
     97  1.1     chuck }
     98  1.1     chuck 
     99  1.1     chuck int
    100  1.1     chuck main(argc, argv)
    101  1.1     chuck 	int argc;
    102  1.1     chuck 	char *argv[];
    103  1.1     chuck {
    104  1.1     chuck 	int	c;
    105  1.1     chuck 	int	devfd;
    106  1.1     chuck 	char	*protostore;
    107  1.6       scw 	size_t	protosize;
    108  1.1     chuck 
    109  1.2     lukem 	while ((c = getopt(argc, argv, "vnh")) != -1) {
    110  1.1     chuck 		switch (c) {
    111  1.1     chuck 		case 'h':
    112  1.1     chuck 			/* Don't strip a.out header */
    113  1.1     chuck 			hflag = 1;
    114  1.1     chuck 			break;
    115  1.1     chuck 		case 'n':
    116  1.1     chuck 			/* Do not actually write the bootblock to disk */
    117  1.1     chuck 			nowrite = 1;
    118  1.1     chuck 			break;
    119  1.1     chuck 		case 'v':
    120  1.1     chuck 			/* Chat */
    121  1.1     chuck 			verbose = 1;
    122  1.1     chuck 			break;
    123  1.1     chuck 		default:
    124  1.1     chuck 			usage();
    125  1.1     chuck 		}
    126  1.1     chuck 	}
    127  1.1     chuck 
    128  1.1     chuck 	if (argc - optind < 3) {
    129  1.1     chuck 		usage();
    130  1.1     chuck 	}
    131  1.1     chuck 
    132  1.1     chuck 	boot = argv[optind];
    133  1.1     chuck 	proto = argv[optind + 1];
    134  1.1     chuck 	dev = argv[optind + 2];
    135  1.1     chuck 
    136  1.1     chuck 	if (verbose) {
    137  1.1     chuck 		printf("boot: %s\n", boot);
    138  1.1     chuck 		printf("proto: %s\n", proto);
    139  1.1     chuck 		printf("device: %s\n", dev);
    140  1.1     chuck 	}
    141  1.1     chuck 
    142  1.1     chuck 	/* Load proto blocks into core */
    143  1.1     chuck 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
    144  1.1     chuck 		exit(1);
    145  1.1     chuck 
    146  1.1     chuck 	/* XXX - Paranoia: Make sure size is aligned! */
    147  1.1     chuck 	if (protosize & (DEV_BSIZE - 1))
    148  1.6       scw 		err(1, "proto bootblock bad size=%d", protosize);
    149  1.1     chuck 
    150  1.1     chuck 	/* Open and check raw disk device */
    151  1.1     chuck 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
    152  1.1     chuck 		err(1, "open: %s", dev);
    153  1.1     chuck 
    154  1.1     chuck 	/* Extract and load block numbers */
    155  1.1     chuck 	if (loadblocknums(boot, devfd) != 0)
    156  1.1     chuck 		exit(1);
    157  1.1     chuck 
    158  1.1     chuck 	(void)close(devfd);
    159  1.1     chuck 
    160  1.1     chuck 	if (nowrite)
    161  1.1     chuck 		return 0;
    162  1.1     chuck 
    163  1.1     chuck 	/* Write patched proto bootblocks into the superblock */
    164  1.9        he 	if (protosize > SBLOCKSIZE - DEV_BSIZE)
    165  1.1     chuck 		errx(1, "proto bootblocks too big");
    166  1.3       scw 
    167  1.3       scw 	/* The primary bootblock needs to be written to the raw partition */
    168  1.3       scw 	dev[strlen(dev) - 1] = 'a' + RAW_PART;
    169  1.1     chuck 
    170  1.1     chuck 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
    171  1.1     chuck 		err(1, "open: %s", dev);
    172  1.1     chuck 
    173  1.1     chuck 	if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
    174  1.1     chuck 		err(1, "lseek bootstrap");
    175  1.1     chuck 
    176  1.1     chuck 	/* Sync filesystems (to clean in-memory superblock?) */
    177  1.1     chuck 	sync();
    178  1.1     chuck 
    179  1.1     chuck 	if (write(devfd, protostore, protosize) != protosize)
    180  1.1     chuck 		err(1, "write bootstrap");
    181  1.1     chuck 	(void)close(devfd);
    182  1.1     chuck 	return 0;
    183  1.1     chuck }
    184  1.1     chuck 
    185  1.1     chuck char *
    186  1.1     chuck loadprotoblocks(fname, size)
    187  1.1     chuck 	char *fname;
    188  1.6       scw 	size_t *size;
    189  1.1     chuck {
    190  1.1     chuck 	int	fd;
    191  1.6       scw 	u_long	marks[MARK_MAX], bp, offs;
    192  1.1     chuck 
    193  1.1     chuck 	fd = -1;
    194  1.1     chuck 
    195  1.1     chuck 	/* Locate block number array in proto file */
    196  1.1     chuck 	if (nlist(fname, nl) != 0) {
    197  1.1     chuck 		warnx("nlist: %s: symbols not found", fname);
    198  1.1     chuck 		return NULL;
    199  1.1     chuck 	}
    200  1.1     chuck 
    201  1.6       scw 	marks[MARK_START] = 0;
    202  1.6       scw 	if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1)
    203  1.1     chuck 		return NULL;
    204  1.6       scw 	(void)close(fd);
    205  1.6       scw 
    206  1.6       scw 	*size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE);
    207  1.6       scw 	bp = (u_long)malloc(*size);
    208  1.6       scw 
    209  1.6       scw 	offs = marks[MARK_START];
    210  1.6       scw 	marks[MARK_START] = bp - offs;
    211  1.1     chuck 
    212  1.6       scw 	if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1)
    213  1.6       scw 		return NULL;
    214  1.6       scw 	(void)close(fd);
    215  1.1     chuck 
    216  1.1     chuck 	/* Calculate the symbols' locations within the proto file */
    217  1.6       scw 	block_size_p  =   (int *) (bp + (nl[X_BLOCK_SIZE ].n_value - offs));
    218  1.6       scw 	block_count_p =   (int *) (bp + (nl[X_BLOCK_COUNT].n_value - offs));
    219  1.8      fvdl 	/* XXX ondisk32 */
    220  1.8      fvdl 	block_table = (int32_t *) (bp + (nl[X_BLOCK_TABLE].n_value - offs));
    221  1.1     chuck 	maxblocknum = *block_count_p;
    222  1.1     chuck 
    223  1.1     chuck 	if (verbose) {
    224  1.6       scw 		printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]);
    225  1.6       scw 		printf("proto bootblock size %d\n", *size);
    226  1.5  jdolecek 		printf("room for %d filesystem blocks at %#lx\n",
    227  1.1     chuck 			maxblocknum, nl[X_BLOCK_TABLE].n_value);
    228  1.1     chuck 	}
    229  1.1     chuck 
    230  1.6       scw 	return (char *) bp;
    231  1.1     chuck 
    232  1.1     chuck 	if (bp)
    233  1.6       scw 		free((void *)bp);
    234  1.1     chuck 	return NULL;
    235  1.1     chuck }
    236  1.1     chuck 
    237  1.1     chuck static void
    238  1.1     chuck devread(fd, buf, blk, size, msg)
    239  1.1     chuck 	int	fd;
    240  1.1     chuck 	void	*buf;
    241  1.1     chuck 	daddr_t	blk;
    242  1.1     chuck 	size_t	size;
    243  1.1     chuck 	char	*msg;
    244  1.1     chuck {
    245  1.1     chuck 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    246  1.1     chuck 		err(1, "%s: devread: lseek", msg);
    247  1.1     chuck 
    248  1.1     chuck 	if (read(fd, buf, size) != size)
    249  1.1     chuck 		err(1, "%s: devread: read", msg);
    250  1.1     chuck }
    251  1.1     chuck 
    252  1.9        he static char sblock[SBLOCKSIZE];
    253  1.1     chuck 
    254  1.1     chuck int
    255  1.1     chuck loadblocknums(boot, devfd)
    256  1.1     chuck char	*boot;
    257  1.1     chuck int	devfd;
    258  1.1     chuck {
    259  1.1     chuck 	int		i, fd;
    260  1.1     chuck 	struct	stat	statbuf;
    261  1.1     chuck 	struct	statfs	statfsbuf;
    262  1.1     chuck 	struct fs	*fs;
    263  1.1     chuck 	char		*buf;
    264  1.1     chuck 	daddr_t		blk, *ap;
    265  1.9        he 	struct ufs1_dinode *ip;
    266  1.1     chuck 	int		ndb;
    267  1.1     chuck 
    268  1.1     chuck 	/*
    269  1.1     chuck 	 * Open 2nd-level boot program and record the block numbers
    270  1.1     chuck 	 * it occupies on the filesystem represented by `devfd'.
    271  1.1     chuck 	 */
    272  1.1     chuck 
    273  1.1     chuck 	/* Make sure the (probably new) boot file is on disk. */
    274  1.1     chuck 	sync(); sleep(1);
    275  1.1     chuck 
    276  1.1     chuck 	if ((fd = open(boot, O_RDONLY)) < 0)
    277  1.1     chuck 		err(1, "open: %s", boot);
    278  1.1     chuck 
    279  1.1     chuck 	if (fstatfs(fd, &statfsbuf) != 0)
    280  1.1     chuck 		err(1, "statfs: %s", boot);
    281  1.1     chuck 
    282  1.1     chuck 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
    283  1.1     chuck 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) {
    284  1.1     chuck 		errx(1, "%s: must be on an FFS filesystem", boot);
    285  1.1     chuck 	}
    286  1.1     chuck 
    287  1.1     chuck 	if (fsync(fd) != 0)
    288  1.1     chuck 		err(1, "fsync: %s", boot);
    289  1.1     chuck 
    290  1.1     chuck 	if (fstat(fd, &statbuf) != 0)
    291  1.1     chuck 		err(1, "fstat: %s", boot);
    292  1.1     chuck 
    293  1.1     chuck 	close(fd);
    294  1.1     chuck 
    295  1.1     chuck 	/* Read superblock */
    296  1.9        he 	devread(devfd, sblock, (daddr_t)(BBSIZE / DEV_BSIZE),
    297  1.9        he 	    SBLOCKSIZE, "superblock");
    298  1.1     chuck 	fs = (struct fs *)sblock;
    299  1.1     chuck 
    300  1.1     chuck 	/* Sanity-check super-block. */
    301  1.9        he 	if (fs->fs_magic != FS_UFS1_MAGIC)
    302  1.9        he 		errx(1, "Bad magic number in superblock, must be UFS1");
    303  1.1     chuck 	if (fs->fs_inopb <= 0)
    304  1.1     chuck 		err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
    305  1.1     chuck 
    306  1.1     chuck 	/* Read inode */
    307  1.1     chuck 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    308  1.1     chuck 		errx(1, "No memory for filesystem block");
    309  1.1     chuck 
    310  1.1     chuck 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
    311  1.1     chuck 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    312  1.9        he 	ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    313  1.1     chuck 
    314  1.1     chuck 	/*
    315  1.1     chuck 	 * Have the inode.  Figure out how many blocks we need.
    316  1.1     chuck 	 */
    317  1.1     chuck 	ndb = howmany(ip->di_size, fs->fs_bsize);
    318  1.1     chuck 	if (ndb > maxblocknum)
    319  1.1     chuck 		errx(1, "Too many blocks");
    320  1.1     chuck 	*block_count_p = ndb;
    321  1.1     chuck 	*block_size_p = fs->fs_bsize;
    322  1.1     chuck 	if (verbose)
    323  1.1     chuck 		printf("Will load %d blocks of size %d each.\n",
    324  1.1     chuck 			   ndb, fs->fs_bsize);
    325  1.1     chuck 
    326  1.1     chuck 	/*
    327  1.1     chuck 	 * Get the block numbers; we don't handle fragments
    328  1.1     chuck 	 */
    329  1.1     chuck 	ap = ip->di_db;
    330  1.1     chuck 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
    331  1.1     chuck 		blk = fsbtodb(fs, *ap);
    332  1.1     chuck 		if (verbose)
    333  1.1     chuck 			printf("%d: %d\n", i, blk);
    334  1.1     chuck 		block_table[i] = blk;
    335  1.1     chuck 	}
    336  1.1     chuck 	if (ndb == 0)
    337  1.1     chuck 		return 0;
    338  1.1     chuck 
    339  1.1     chuck 	/*
    340  1.1     chuck 	 * Just one level of indirections; there isn't much room
    341  1.1     chuck 	 * for more in the 1st-level bootblocks anyway.
    342  1.1     chuck 	 */
    343  1.1     chuck 	blk = fsbtodb(fs, ip->di_ib[0]);
    344  1.1     chuck 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    345  1.8      fvdl 	/* XXX ondisk32 */
    346  1.8      fvdl 	ap = (int32_t *)buf;
    347  1.1     chuck 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    348  1.1     chuck 		blk = fsbtodb(fs, *ap);
    349  1.1     chuck 		if (verbose)
    350  1.1     chuck 			printf("%d: %d\n", i, blk);
    351  1.1     chuck 		block_table[i] = blk;
    352  1.1     chuck 	}
    353  1.1     chuck 
    354  1.1     chuck 	return 0;
    355  1.1     chuck }
    356  1.1     chuck 
    357