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