Home | History | Annotate | Line # | Download | only in installboot
      1 /*	$NetBSD: installboot.c,v 1.19 2014/09/21 16:33:48 christos 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 
    224 static void
    225 devread(int fd, void *buf, daddr_t blk, size_t size, char *msg)
    226 {
    227 
    228 	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
    229 		err(1, "%s: devread: lseek", msg);
    230 
    231 	if (read(fd, buf, size) != size)
    232 		err(1, "%s: devread: read", msg);
    233 }
    234 
    235 static char sblock[SBLOCKSIZE];
    236 
    237 int
    238 loadblocknums(char *boot, int devfd)
    239 {
    240 	int		i, fd;
    241 	struct	stat	statbuf;
    242 	struct	statvfs	statvfsbuf;
    243 	struct fs	*fs;
    244 	char		*buf;
    245 	daddr_t		blk, *ap;
    246 	struct ufs1_dinode *ip;
    247 	int		ndb;
    248 
    249 	/*
    250 	 * Open 2nd-level boot program and record the block numbers
    251 	 * it occupies on the filesystem represented by `devfd'.
    252 	 */
    253 
    254 	/* Make sure the (probably new) boot file is on disk. */
    255 	sync(); sleep(1);
    256 
    257 	if ((fd = open(boot, O_RDONLY)) < 0)
    258 		err(1, "open: %s", boot);
    259 
    260 	if (fstatvfs(fd, &statvfsbuf) != 0)
    261 		err(1, "statfs: %s", boot);
    262 
    263 	if (strncmp(statvfsbuf.f_fstypename, "ffs",
    264 	    sizeof(statvfsbuf.f_fstypename)) &&
    265 	    strncmp(statvfsbuf.f_fstypename, "ufs",
    266 	    sizeof(statvfsbuf.f_fstypename))) {
    267 		errx(1, "%s: must be on an FFS filesystem", boot);
    268 	}
    269 
    270 	if (fsync(fd) != 0)
    271 		err(1, "fsync: %s", boot);
    272 
    273 	if (fstat(fd, &statbuf) != 0)
    274 		err(1, "fstat: %s", boot);
    275 
    276 	close(fd);
    277 
    278 	/* Read superblock */
    279 	devread(devfd, sblock, (daddr_t)(BBSIZE / DEV_BSIZE),
    280 	    SBLOCKSIZE, "superblock");
    281 	fs = (struct fs *)sblock;
    282 
    283 	/* Sanity-check super-block. */
    284 	if (fs->fs_magic != FS_UFS1_MAGIC)
    285 		errx(1, "Bad magic number in superblock, must be UFS1");
    286 	if (fs->fs_inopb <= 0)
    287 		err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
    288 
    289 	/* Read inode */
    290 	if ((buf = malloc(fs->fs_bsize)) == NULL)
    291 		errx(1, "No memory for filesystem block");
    292 
    293 	blk = FFS_FSBTODB(fs, ino_to_fsba(fs, statbuf.st_ino));
    294 	devread(devfd, buf, blk, fs->fs_bsize, "inode");
    295 	ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
    296 
    297 	/*
    298 	 * Have the inode.  Figure out how many blocks we need.
    299 	 */
    300 	ndb = howmany(ip->di_size, fs->fs_bsize);
    301 	if (ndb > maxblocknum)
    302 		errx(1, "Too many blocks");
    303 	*block_count_p = ndb;
    304 	*block_size_p = fs->fs_bsize;
    305 	if (verbose)
    306 		printf("Will load %d blocks of size %d each.\n",
    307 		    ndb, fs->fs_bsize);
    308 
    309 	/*
    310 	 * Get the block numbers; we don't handle fragments
    311 	 */
    312 	ap = ip->di_db;
    313 	for (i = 0; i < UFS_NDADDR && *ap && ndb; i++, ap++, ndb--) {
    314 		blk = FFS_FSBTODB(fs, *ap);
    315 		if (verbose)
    316 			printf("%d: %d\n", i, blk);
    317 		block_table[i] = blk;
    318 	}
    319 	if (ndb == 0)
    320 		return 0;
    321 
    322 	/*
    323 	 * Just one level of indirections; there isn't much room
    324 	 * for more in the 1st-level bootblocks anyway.
    325 	 */
    326 	blk = FFS_FSBTODB(fs, ip->di_ib[0]);
    327 	devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
    328 	/* XXX ondisk32 */
    329 	ap = (int32_t *)buf;
    330 	for (; i < FFS_NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
    331 		blk = FFS_FSBTODB(fs, *ap);
    332 		if (verbose)
    333 			printf("%d: %d\n", i, blk);
    334 		block_table[i] = blk;
    335 	}
    336 
    337 	return 0;
    338 }
    339