1995-11-08 12:09:20 +03:00
|
|
|
/* $NetBSD: installboot.c,v 1.11 1995/11/08 09:09:20 pk Exp $ */
|
1994-11-20 23:51:32 +03:00
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Paul Kranenburg
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Paul Kranenburg.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
1995-09-27 12:03:13 +03:00
|
|
|
#include <sys/sysctl.h>
|
1994-07-21 00:47:02 +04:00
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <a.out.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
int verbose, nowrite, hflag;
|
1994-07-21 00:47:02 +04:00
|
|
|
char *boot, *proto, *dev;
|
1995-09-27 12:03:13 +03:00
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
struct nlist nl[] = {
|
1995-09-27 12:03:13 +03:00
|
|
|
#define X_BLOCKTABLE 0
|
|
|
|
{"_block_table"},
|
|
|
|
#define X_BLOCKCOUNT 1
|
|
|
|
{"_block_count"},
|
|
|
|
#define X_BLOCKSIZE 2
|
|
|
|
{"_block_size"},
|
1994-07-21 00:47:02 +04:00
|
|
|
{NULL}
|
|
|
|
};
|
1995-09-27 12:03:13 +03:00
|
|
|
daddr_t *block_table; /* block number array in prototype image */
|
|
|
|
int32_t *block_count_p; /* size of this array */
|
|
|
|
int32_t *block_size_p; /* filesystem block size */
|
|
|
|
int32_t max_block_count;
|
|
|
|
|
|
|
|
char cpumodel[100];
|
1994-07-21 00:47:02 +04:00
|
|
|
|
|
|
|
|
1995-05-16 19:28:01 +04:00
|
|
|
char *loadprotoblocks __P((char *, long *));
|
|
|
|
int loadblocknums __P((char *, int));
|
|
|
|
static void devread __P((int, void *, daddr_t, size_t, char *));
|
1995-09-17 03:20:22 +04:00
|
|
|
static void usage __P((void));
|
|
|
|
int main __P((int, char *[]));
|
1994-07-21 00:47:02 +04:00
|
|
|
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
static void
|
1994-07-21 00:47:02 +04:00
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
1995-09-17 03:20:22 +04:00
|
|
|
"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
|
1994-07-21 00:47:02 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
int
|
1994-07-21 00:47:02 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int devfd;
|
|
|
|
char *protostore;
|
|
|
|
long protosize;
|
1995-09-27 12:03:13 +03:00
|
|
|
int mib[2];
|
|
|
|
size_t size;
|
1994-07-21 00:47:02 +04:00
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
while ((c = getopt(argc, argv, "vnh")) != EOF) {
|
1994-07-21 00:47:02 +04:00
|
|
|
switch (c) {
|
1995-09-17 03:20:22 +04:00
|
|
|
case 'h':
|
|
|
|
/* Don't strip a.out header */
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
1994-07-21 00:47:02 +04:00
|
|
|
case 'n':
|
1995-09-17 03:20:22 +04:00
|
|
|
/* Do not actually write the bootblock to disk */
|
1994-07-21 00:47:02 +04:00
|
|
|
nowrite = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
1995-09-17 03:20:22 +04:00
|
|
|
/* Chat */
|
1994-07-21 00:47:02 +04:00
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind < 3) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
boot = argv[optind];
|
|
|
|
proto = argv[optind + 1];
|
|
|
|
dev = argv[optind + 2];
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf("boot: %s\n", boot);
|
|
|
|
printf("proto: %s\n", proto);
|
|
|
|
printf("device: %s\n", dev);
|
|
|
|
}
|
|
|
|
|
1995-09-27 12:03:13 +03:00
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_MODEL;
|
|
|
|
size = sizeof(cpumodel);
|
|
|
|
if (sysctl(mib, 2, cpumodel, &size, NULL, 0) == -1)
|
|
|
|
err(1, "sysctl");
|
|
|
|
|
1995-11-07 14:45:24 +03:00
|
|
|
if (size < 5 || strncmp(cpumodel, "SUN-4", 5) != 0) /*XXX*/
|
1995-09-27 12:03:13 +03:00
|
|
|
/* Assume a sun4c/sun4m */
|
|
|
|
hflag = 1;
|
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
/* Load proto blocks into core */
|
|
|
|
if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
/* Open and check raw disk device */
|
|
|
|
if ((devfd = open(dev, O_RDONLY, 0)) < 0)
|
|
|
|
err(1, "open: %s", dev);
|
|
|
|
|
|
|
|
/* Extract and load block numbers */
|
|
|
|
if (loadblocknums(boot, devfd) != 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
(void)close(devfd);
|
|
|
|
|
|
|
|
if (nowrite)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Write patched proto bootblocks into the superblock */
|
|
|
|
if (protosize > SBSIZE - DEV_BSIZE)
|
|
|
|
errx(1, "proto bootblocks too big");
|
|
|
|
|
|
|
|
if ((devfd = open(dev, O_RDWR, 0)) < 0)
|
|
|
|
err(1, "open: %s", dev);
|
|
|
|
|
|
|
|
if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
|
|
|
|
err(1, "lseek bootstrap");
|
|
|
|
|
1995-09-19 02:36:19 +04:00
|
|
|
/* Sync filesystems (to clean in-memory superblock?) */
|
|
|
|
sync();
|
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
if (write(devfd, protostore, protosize) != protosize)
|
|
|
|
err(1, "write bootstrap");
|
|
|
|
(void)close(devfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
char *
|
1994-07-21 00:47:02 +04:00
|
|
|
loadprotoblocks(fname, size)
|
|
|
|
char *fname;
|
|
|
|
long *size;
|
|
|
|
{
|
1995-09-17 03:20:22 +04:00
|
|
|
int fd, sz;
|
1994-07-21 00:47:02 +04:00
|
|
|
char *bp;
|
|
|
|
struct stat statbuf;
|
|
|
|
struct exec *hp;
|
|
|
|
long off;
|
|
|
|
|
|
|
|
/* Locate block number array in proto file */
|
|
|
|
if (nlist(fname, nl) != 0) {
|
|
|
|
warnx("nlist: %s: symbols not found", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-09-27 12:03:13 +03:00
|
|
|
if (nl[X_BLOCKTABLE].n_type != N_DATA + N_EXT) {
|
|
|
|
warnx("nlist: %s: wrong type", nl[X_BLOCKTABLE].n_un.n_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (nl[X_BLOCKCOUNT].n_type != N_DATA + N_EXT) {
|
|
|
|
warnx("nlist: %s: wrong type", nl[X_BLOCKCOUNT].n_un.n_name);
|
1994-07-21 00:47:02 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-09-27 12:03:13 +03:00
|
|
|
if (nl[X_BLOCKSIZE].n_type != N_DATA + N_EXT) {
|
|
|
|
warnx("nlist: %s: wrong type", nl[X_BLOCKSIZE].n_un.n_name);
|
1994-07-21 00:47:02 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open(fname, O_RDONLY)) < 0) {
|
|
|
|
warn("open: %s", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &statbuf) != 0) {
|
|
|
|
warn("fstat: %s", fname);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((bp = calloc(roundup(statbuf.st_size, DEV_BSIZE), 1)) == NULL) {
|
|
|
|
warnx("malloc: %s: no memory", fname);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
|
|
|
|
warn("read: %s", fname);
|
|
|
|
free(bp);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
hp = (struct exec *)bp;
|
1995-09-17 03:20:22 +04:00
|
|
|
sz = (hflag ? sizeof(*hp) : 0) + hp->a_text + hp->a_data;
|
|
|
|
sz = roundup(sz, DEV_BSIZE);
|
1994-07-21 00:47:02 +04:00
|
|
|
|
|
|
|
/* Calculate the symbols' location within the proto file */
|
|
|
|
off = N_DATOFF(*hp) - N_DATADDR(*hp) - (hp->a_entry - N_TXTADDR(*hp));
|
1995-09-27 12:03:13 +03:00
|
|
|
block_table = (daddr_t *) (bp + nl[X_BLOCKTABLE].n_value + off);
|
|
|
|
block_count_p = (int32_t *)(bp + nl[X_BLOCKCOUNT].n_value + off);
|
|
|
|
block_size_p = (int32_t *) (bp + nl[X_BLOCKSIZE].n_value + off);
|
|
|
|
if ((int)block_table & 3) {
|
|
|
|
warn("%s: invalid address: block_table = %x",
|
|
|
|
fname, block_table);
|
|
|
|
free(bp);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((int)block_count_p & 3) {
|
|
|
|
warn("%s: invalid address: block_count_p = %x",
|
|
|
|
fname, block_count_p);
|
|
|
|
free(bp);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((int)block_size_p & 3) {
|
|
|
|
warn("%s: invalid address: block_size_p = %x",
|
|
|
|
fname, block_size_p);
|
|
|
|
free(bp);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
max_block_count = *block_count_p;
|
1994-07-21 00:47:02 +04:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf("%s: entry point %#x\n", fname, hp->a_entry);
|
1995-09-27 12:03:13 +03:00
|
|
|
printf("%s: a.out header %s\n", fname,
|
|
|
|
hflag?"left on":"stripped off");
|
1995-09-17 03:20:22 +04:00
|
|
|
printf("proto bootblock size %ld\n", sz);
|
1994-07-21 00:47:02 +04:00
|
|
|
printf("room for %d filesystem blocks at %#x\n",
|
1995-09-27 12:03:13 +03:00
|
|
|
max_block_count, nl[X_BLOCKTABLE].n_value);
|
1994-07-21 00:47:02 +04:00
|
|
|
}
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
*size = sz;
|
|
|
|
return (hflag ? bp : (bp + sizeof(struct exec)));
|
1994-07-21 00:47:02 +04:00
|
|
|
}
|
|
|
|
|
1995-09-17 03:20:22 +04:00
|
|
|
static void
|
1994-07-21 00:47:02 +04:00
|
|
|
devread(fd, buf, blk, size, msg)
|
|
|
|
int fd;
|
1995-05-16 19:28:01 +04:00
|
|
|
void *buf;
|
1994-07-21 00:47:02 +04:00
|
|
|
daddr_t blk;
|
1995-05-16 19:28:01 +04:00
|
|
|
size_t size;
|
1994-07-21 00:47:02 +04:00
|
|
|
char *msg;
|
|
|
|
{
|
|
|
|
if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
|
|
|
|
err(1, "%s: devread: lseek", msg);
|
|
|
|
|
|
|
|
if (read(fd, buf, size) != size)
|
|
|
|
err(1, "%s: devread: read", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char sblock[SBSIZE];
|
|
|
|
|
|
|
|
int
|
|
|
|
loadblocknums(boot, devfd)
|
|
|
|
char *boot;
|
|
|
|
int devfd;
|
|
|
|
{
|
|
|
|
int i, fd;
|
1994-08-13 12:33:08 +04:00
|
|
|
struct stat statbuf;
|
|
|
|
struct statfs statfsbuf;
|
1994-07-21 00:47:02 +04:00
|
|
|
struct fs *fs;
|
|
|
|
char *buf;
|
|
|
|
daddr_t blk, *ap;
|
|
|
|
struct dinode *ip;
|
|
|
|
int ndb;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open 2nd-level boot program and record the block numbers
|
|
|
|
* it occupies on the filesystem represented by `devfd'.
|
|
|
|
*/
|
|
|
|
if ((fd = open(boot, O_RDONLY)) < 0)
|
|
|
|
err(1, "open: %s", boot);
|
|
|
|
|
1994-08-13 12:33:08 +04:00
|
|
|
if (fstatfs(fd, &statfsbuf) != 0)
|
|
|
|
err(1, "statfs: %s", boot);
|
|
|
|
|
1995-11-08 12:09:20 +03:00
|
|
|
if (strncmp(statfsbuf.f_fstypename, "ffs", MFSNAMELEN) &&
|
|
|
|
strncmp(statfsbuf.f_fstypename, "ufs", MFSNAMELEN)) {
|
|
|
|
errx(1, "%s: must be on an FFS filesystem", boot);
|
|
|
|
}
|
1994-08-13 12:33:08 +04:00
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
if (fsync(fd) != 0)
|
|
|
|
err(1, "fsync: %s", boot);
|
|
|
|
|
|
|
|
if (fstat(fd, &statbuf) != 0)
|
|
|
|
err(1, "fstat: %s", boot);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* Read superblock */
|
|
|
|
devread(devfd, sblock, btodb(SBOFF), SBSIZE, "superblock");
|
|
|
|
fs = (struct fs *)sblock;
|
|
|
|
|
|
|
|
/* Read inode */
|
|
|
|
if ((buf = malloc(fs->fs_bsize)) == NULL)
|
|
|
|
errx(1, "No memory for filesystem block");
|
|
|
|
|
|
|
|
blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
|
|
|
|
devread(devfd, buf, blk, fs->fs_bsize, "inode");
|
|
|
|
ip = (struct dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);
|
|
|
|
|
1995-09-27 12:03:13 +03:00
|
|
|
/*
|
|
|
|
* Register filesystem block size.
|
|
|
|
*/
|
|
|
|
*block_size_p = fs->fs_bsize;
|
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
/*
|
|
|
|
* Get the block numbers; we don't handle fragments
|
|
|
|
*/
|
|
|
|
ndb = howmany(ip->di_size, fs->fs_bsize);
|
1995-09-27 12:03:13 +03:00
|
|
|
if (ndb > max_block_count)
|
|
|
|
errx(1, "%s: Too many blocks", boot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register block count.
|
|
|
|
*/
|
|
|
|
*block_count_p = ndb;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("%s: block numbers: ", boot);
|
1994-07-21 00:47:02 +04:00
|
|
|
ap = ip->di_db;
|
|
|
|
for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
|
1995-09-27 12:03:13 +03:00
|
|
|
blk = fsbtodb(fs, *ap);
|
|
|
|
block_table[i] = blk;
|
|
|
|
if (verbose)
|
|
|
|
printf("%d ", blk);
|
1994-07-21 00:47:02 +04:00
|
|
|
}
|
1995-09-27 12:03:13 +03:00
|
|
|
if (verbose)
|
|
|
|
printf("\n");
|
|
|
|
|
1994-07-21 00:47:02 +04:00
|
|
|
if (ndb == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just one level of indirections; there isn't much room
|
|
|
|
* for more in the 1st-level bootblocks anyway.
|
|
|
|
*/
|
1995-09-27 12:03:13 +03:00
|
|
|
if (verbose)
|
|
|
|
printf("%s: block numbers (indirect): ", boot);
|
1994-07-21 00:47:02 +04:00
|
|
|
blk = ip->di_ib[0];
|
|
|
|
devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
|
|
|
|
ap = (daddr_t *)buf;
|
|
|
|
for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
|
1995-09-27 12:03:13 +03:00
|
|
|
blk = fsbtodb(fs, *ap);
|
|
|
|
block_table[i] = blk;
|
|
|
|
if (verbose)
|
|
|
|
printf("%d ", blk);
|
1994-07-21 00:47:02 +04:00
|
|
|
}
|
1995-09-27 12:03:13 +03:00
|
|
|
if (verbose)
|
|
|
|
printf("\n");
|
1994-07-21 00:47:02 +04:00
|
|
|
|
|
|
|
if (ndb)
|
1995-09-27 12:03:13 +03:00
|
|
|
errx(1, "%s: Too many blocks", boot);
|
1994-07-21 00:47:02 +04:00
|
|
|
return 0;
|
|
|
|
}
|