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