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