Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.6
      1  1.6       scw /*	$NetBSD: installboot.c,v 1.6 2000/12/04 18:44:52 scw 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.6       scw 	{ "block_size" },
     66  1.6       scw 	{ "block_count" },
     67  1.6       scw 	{ "block_table" },
     68  1.6       scw 	{ NULL }
     69  1.1     chuck };
     70  1.1     chuck 
     71  1.1     chuck int *block_size_p;		/* block size var. in prototype image */
     72  1.1     chuck int *block_count_p;		/* block count var. in prototype image */
     73  1.6       scw daddr_t	*block_table;		/* block number array in prototype image */
     74  1.1     chuck int	maxblocknum;		/* size of this array */
     75  1.1     chuck 
     76  1.1     chuck 
     77  1.6       scw char		*loadprotoblocks __P((char *, size_t *));
     78  1.1     chuck int		loadblocknums __P((char *, int));
     79  1.1     chuck static void	devread __P((int, void *, daddr_t, size_t, char *));
     80  1.1     chuck static void	usage __P((void));
     81  1.1     chuck int 		main __P((int, char *[]));
     82  1.1     chuck 
     83  1.1     chuck 
     84  1.1     chuck static void
     85  1.1     chuck usage()
     86  1.1     chuck {
     87  1.1     chuck 	fprintf(stderr,
     88  1.1     chuck 		"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
     89  1.1     chuck 	exit(1);
     90  1.1     chuck }
     91  1.1     chuck 
     92  1.1     chuck int
     93  1.1     chuck main(argc, argv)
     94  1.1     chuck 	int argc;
     95  1.1     chuck 	char *argv[];
     96  1.1     chuck {
     97  1.1     chuck 	int	c;
     98  1.1     chuck 	int	devfd;
     99  1.1     chuck 	char	*protostore;
    100  1.6       scw 	size_t	protosize;
    101  1.1     chuck 
    102  1.2     lukem 	while ((c = getopt(argc, argv, "vnh")) != -1) {
    103  1.1     chuck 		switch (c) {
    104  1.1     chuck 		case 'h':
    105  1.1     chuck 			/* Don't strip a.out header */
    106  1.1     chuck 			hflag = 1;
    107  1.1     chuck 			break;
    108  1.1     chuck 		case 'n':
    109  1.1     chuck 			/* Do not actually write the bootblock to disk */
    110  1.1     chuck 			nowrite = 1;
    111  1.1     chuck 			break;
    112  1.1     chuck 		case 'v':
    113  1.1     chuck 			/* Chat */
    114  1.1     chuck 			verbose = 1;
    115  1.1     chuck 			break;
    116  1.1     chuck 		default:
    117  1.1     chuck 			usage();
    118  1.1     chuck 		}
    119  1.1     chuck 	}
    120  1.1     chuck 
    121  1.1     chuck 	if (argc - optind < 3) {
    122  1.1     chuck 		usage();
    123  1.1     chuck 	}
    124  1.1     chuck 
    125  1.1     chuck 	boot = argv[optind];
    126  1.1     chuck 	proto = argv[optind + 1];
    127  1.1     chuck 	dev = argv[optind + 2];
    128  1.1     chuck 
    129  1.1     chuck 	if (verbose) {
    130  1.1     chuck 		printf("boot: %s\n", boot);
    131  1.1     chuck 		printf("proto: %s\n", proto);
    132  1.1     chuck 		printf("device: %s\n", dev);
    133  1.1     chuck 	}
    134  1.1     chuck 
    135  1.1     chuck 	/* Load proto blocks into core */
    136  1.1     chuck 	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
    137  1.1     chuck 		exit(1);
    138  1.1     chuck 
    139  1.1     chuck 	/* XXX - Paranoia: Make sure size is aligned! */
    140  1.1     chuck 	if (protosize & (DEV_BSIZE - 1))
    141  1.6       scw 		err(1, "proto bootblock bad size=%d", protosize);
    142  1.1     chuck 
    143  1.1     chuck 	/* Open and check raw disk device */
    144  1.1     chuck 	if ((devfd = open(dev, O_RDONLY, 0)) < 0)
    145  1.1     chuck 		err(1, "open: %s", dev);
    146  1.1     chuck 
    147  1.1     chuck 	/* Extract and load block numbers */
    148  1.1     chuck 	if (loadblocknums(boot, devfd) != 0)
    149  1.1     chuck 		exit(1);
    150  1.1     chuck 
    151  1.1     chuck 	(void)close(devfd);
    152  1.1     chuck 
    153  1.1     chuck 	if (nowrite)
    154  1.1     chuck 		return 0;
    155  1.1     chuck 
    156  1.1     chuck 	/* Write patched proto bootblocks into the superblock */
    157  1.1     chuck 	if (protosize > SBSIZE - DEV_BSIZE)
    158  1.1     chuck 		errx(1, "proto bootblocks too big");
    159  1.3       scw 
    160  1.3       scw 	/* The primary bootblock needs to be written to the raw partition */
    161  1.3       scw 	dev[strlen(dev) - 1] = 'a' + RAW_PART;
    162  1.1     chuck 
    163  1.1     chuck 	if ((devfd = open(dev, O_RDWR, 0)) < 0)
    164  1.1     chuck 		err(1, "open: %s", dev);
    165  1.1     chuck 
    166  1.1     chuck 	if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
    167  1.1     chuck 		err(1, "lseek bootstrap");
    168  1.1     chuck 
    169  1.1     chuck 	/* Sync filesystems (to clean in-memory superblock?) */
    170  1.1     chuck 	sync();
    171  1.1     chuck 
    172  1.1     chuck 	if (write(devfd, protostore, protosize) != protosize)
    173  1.1     chuck 		err(1, "write bootstrap");
    174  1.1     chuck 	(void)close(devfd);
    175  1.1     chuck 	return 0;
    176  1.1     chuck }
    177  1.1     chuck 
    178  1.1     chuck char *
    179  1.1     chuck loadprotoblocks(fname, size)
    180  1.1     chuck 	char *fname;
    181  1.6       scw 	size_t *size;
    182  1.1     chuck {
    183  1.1     chuck 	int	fd;
    184  1.6       scw 	u_long	marks[MARK_MAX], bp, offs;
    185  1.1     chuck 
    186  1.1     chuck 	fd = -1;
    187  1.1     chuck 
    188  1.1     chuck 	/* Locate block number array in proto file */
    189  1.1     chuck 	if (nlist(fname, nl) != 0) {
    190  1.1     chuck 		warnx("nlist: %s: symbols not found", fname);
    191  1.1     chuck 		return NULL;
    192  1.1     chuck 	}
    193  1.1     chuck 
    194  1.6       scw 	marks[MARK_START] = 0;
    195  1.6       scw 	if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1)
    196  1.1     chuck 		return NULL;
    197  1.6       scw 	(void)close(fd);
    198  1.6       scw 
    199  1.6       scw 	*size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE);
    200  1.6       scw 	bp = (u_long)malloc(*size);
    201  1.6       scw 
    202  1.6       scw 	offs = marks[MARK_START];
    203  1.6       scw 	marks[MARK_START] = bp - offs;
    204  1.1     chuck 
    205  1.6       scw 	if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1)
    206  1.6       scw 		return NULL;
    207  1.6       scw 	(void)close(fd);
    208  1.1     chuck 
    209  1.1     chuck 	/* Calculate the symbols' locations within the proto file */
    210  1.6       scw 	block_size_p  =   (int *) (bp + (nl[X_BLOCK_SIZE ].n_value - offs));
    211  1.6       scw 	block_count_p =   (int *) (bp + (nl[X_BLOCK_COUNT].n_value - offs));
    212  1.6       scw 	block_table = (daddr_t *) (bp + (nl[X_BLOCK_TABLE].n_value - offs));
    213  1.1     chuck 	maxblocknum = *block_count_p;
    214  1.1     chuck 
    215  1.1     chuck 	if (verbose) {
    216  1.6       scw 		printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]);
    217  1.6       scw 		printf("proto bootblock size %d\n", *size);
    218  1.5  jdolecek 		printf("room for %d filesystem blocks at %#lx\n",
    219  1.1     chuck 			maxblocknum, nl[X_BLOCK_TABLE].n_value);
    220  1.1     chuck 	}
    221  1.1     chuck 
    222  1.6       scw 	return (char *) bp;
    223  1.1     chuck 
    224  1.1     chuck 	if (bp)
    225  1.6       scw 		free((void *)bp);
    226  1.1     chuck 	return NULL;
    227  1.1     chuck }
    228  1.1     chuck 
    229  1.1     chuck static void
    230  1.1     chuck devread(fd, buf, blk, size, msg)
    231  1.1     chuck 	int	fd;
    232  1.1     chuck 	void	*buf;
    233  1.1     chuck 	daddr_t	blk;
    234  1.1     chuck 	size_t	size;
    235  1.1     chuck 	char	*msg;
    236  1.1     chuck {
    237  1.1     chuck 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    238  1.1     chuck 		err(1, "%s: devread: lseek", msg);
    239  1.1     chuck 
    240  1.1     chuck 	if (read(fd, buf, size) != size)
    241  1.1     chuck 		err(1, "%s: devread: read", msg);
    242  1.1     chuck }
    243  1.1     chuck 
    244  1.1     chuck static char sblock[SBSIZE];
    245  1.1     chuck 
    246  1.1     chuck int
    247  1.1     chuck loadblocknums(boot, devfd)
    248  1.1     chuck char	*boot;
    249  1.1     chuck int	devfd;
    250  1.1     chuck {
    251  1.1     chuck 	int		i, fd;
    252  1.1     chuck 	struct	stat	statbuf;
    253  1.1     chuck 	struct	statfs	statfsbuf;
    254  1.1     chuck 	struct fs	*fs;
    255  1.1     chuck 	char		*buf;
    256  1.1     chuck 	daddr_t		blk, *ap;
    257  1.1     chuck 	struct dinode	*ip;
    258  1.1     chuck 	int		ndb;
    259  1.1     chuck 
    260  1.1     chuck 	/*
    261  1.1     chuck 	 * Open 2nd-level boot program and record the block numbers
    262  1.1     chuck 	 * it occupies on the filesystem represented by `devfd'.
    263  1.1     chuck 	 */
    264  1.1     chuck 
    265  1.1     chuck 	/* Make sure the (probably new) boot file is on disk. */
    266  1.1     chuck 	sync(); sleep(1);
    267  1.1     chuck 
    268  1.1     chuck 	if ((fd = open(boot, O_RDONLY)) < 0)
    269  1.1     chuck 		err(1, "open: %s", boot);
    270  1.1     chuck 
    271  1.1     chuck 	if (fstatfs(fd, &statfsbuf) != 0)
    272  1.1     chuck 		err(1, "statfs: %s", boot);
    273  1.1     chuck 
    274  1.1     chuck 	if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
    275  1.1     chuck 	    strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN) ) {
    276  1.1     chuck 		errx(1, "%s: must be on an FFS filesystem", boot);
    277  1.1     chuck 	}
    278  1.1     chuck 
    279  1.1     chuck 	if (fsync(fd) != 0)
    280  1.1     chuck 		err(1, "fsync: %s", boot);
    281  1.1     chuck 
    282  1.1     chuck 	if (fstat(fd, &statbuf) != 0)
    283  1.1     chuck 		err(1, "fstat: %s", boot);
    284  1.1     chuck 
    285  1.1     chuck 	close(fd);
    286  1.1     chuck 
    287  1.1     chuck 	/* Read superblock */
    288  1.1     chuck 	devread(devfd, sblock, SBLOCK, SBSIZE, "superblock");
    289  1.1     chuck 	fs = (struct fs *)sblock;
    290  1.1     chuck 
    291  1.1     chuck 	/* Sanity-check super-block. */
    292  1.1     chuck 	if (fs->fs_magic != FS_MAGIC)
    293  1.1     chuck 		errx(1, "Bad magic number in superblock");
    294  1.1     chuck 	if (fs->fs_inopb <= 0)
    295  1.1     chuck 		err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
    296  1.1     chuck 
    297  1.1     chuck 	/* Read inode */
    298  1.1     chuck 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    299  1.1     chuck 		errx(1, "No memory for filesystem block");
    300  1.1     chuck 
    301  1.1     chuck 	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
    302  1.1     chuck 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    303  1.1     chuck 	ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    304  1.1     chuck 
    305  1.1     chuck 	/*
    306  1.1     chuck 	 * Have the inode.  Figure out how many blocks we need.
    307  1.1     chuck 	 */
    308  1.1     chuck 	ndb = howmany(ip->di_size, fs->fs_bsize);
    309  1.1     chuck 	if (ndb > maxblocknum)
    310  1.1     chuck 		errx(1, "Too many blocks");
    311  1.1     chuck 	*block_count_p = ndb;
    312  1.1     chuck 	*block_size_p = fs->fs_bsize;
    313  1.1     chuck 	if (verbose)
    314  1.1     chuck 		printf("Will load %d blocks of size %d each.\n",
    315  1.1     chuck 			   ndb, fs->fs_bsize);
    316  1.1     chuck 
    317  1.1     chuck 	/*
    318  1.1     chuck 	 * Get the block numbers; we don't handle fragments
    319  1.1     chuck 	 */
    320  1.1     chuck 	ap = ip->di_db;
    321  1.1     chuck 	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
    322  1.1     chuck 		blk = fsbtodb(fs, *ap);
    323  1.1     chuck 		if (verbose)
    324  1.1     chuck 			printf("%d: %d\n", i, blk);
    325  1.1     chuck 		block_table[i] = blk;
    326  1.1     chuck 	}
    327  1.1     chuck 	if (ndb == 0)
    328  1.1     chuck 		return 0;
    329  1.1     chuck 
    330  1.1     chuck 	/*
    331  1.1     chuck 	 * Just one level of indirections; there isn't much room
    332  1.1     chuck 	 * for more in the 1st-level bootblocks anyway.
    333  1.1     chuck 	 */
    334  1.1     chuck 	blk = fsbtodb(fs, ip->di_ib[0]);
    335  1.1     chuck 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    336  1.1     chuck 	ap = (daddr_t *)buf;
    337  1.1     chuck 	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    338  1.1     chuck 		blk = fsbtodb(fs, *ap);
    339  1.1     chuck 		if (verbose)
    340  1.1     chuck 			printf("%d: %d\n", i, blk);
    341  1.1     chuck 		block_table[i] = blk;
    342  1.1     chuck 	}
    343  1.1     chuck 
    344  1.1     chuck 	return 0;
    345  1.1     chuck }
    346  1.1     chuck 
    347