NetBSD/sys/arch/sun3/stand/installboot/installboot.c

374 lines
9.2 KiB
C
Raw Normal View History

1995-11-08 02:01:40 +03:00
/* $NetBSD: installboot.c,v 1.4 1995/11/07 23:01:40 gwr Exp $ */
1995-02-15 01:56:36 +03: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>
#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-23 07:40:28 +04:00
int verbose, nowrite, hflag;
1995-02-15 01:56:36 +03:00
char *boot, *proto, *dev;
struct nlist nl[] = {
1995-06-02 00:37:44 +04:00
#define X_BLOCK_SIZE 0
{"_block_size"},
#define X_BLOCK_COUNT 1
{"_block_count"},
#define X_BLOCK_TABLE 2
{"_block_table"},
1995-02-15 01:56:36 +03:00
{NULL}
};
1995-06-02 00:37:44 +04:00
int *block_size_p; /* block size var. in prototype image */
int *block_count_p; /* block count var. in prototype image */
daddr_t *block_table; /* block number array in prototype image */
1995-02-15 01:56:36 +03:00
int maxblocknum; /* size of this array */
1995-09-23 07:40:28 +04:00
char *loadprotoblocks __P((char *, long *));
int loadblocknums __P((char *, int));
static void devread __P((int, void *, daddr_t, size_t, char *));
static void usage __P((void));
int main __P((int, char *[]));
1995-02-15 01:56:36 +03:00
1995-09-23 07:40:28 +04:00
static void
1995-02-15 01:56:36 +03:00
usage()
{
fprintf(stderr,
1995-09-23 07:40:28 +04:00
"usage: installboot [-n] [-v] [-h] <boot> <proto> <device>\n");
1995-02-15 01:56:36 +03:00
exit(1);
}
1995-06-02 00:37:44 +04:00
int
1995-02-15 01:56:36 +03:00
main(argc, argv)
int argc;
char *argv[];
{
int c;
int devfd;
char *protostore;
long protosize;
1995-09-23 07:40:28 +04:00
while ((c = getopt(argc, argv, "vnh")) != EOF) {
1995-02-15 01:56:36 +03:00
switch (c) {
1995-09-23 07:40:28 +04:00
case 'h':
/* Don't strip a.out header */
hflag = 1;
break;
1995-02-15 01:56:36 +03:00
case 'n':
1995-09-23 07:40:28 +04:00
/* Do not actually write the bootblock to disk */
1995-02-15 01:56:36 +03:00
nowrite = 1;
break;
case 'v':
1995-09-23 07:40:28 +04:00
/* Chat */
1995-02-15 01:56:36 +03: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);
}
/* Load proto blocks into core */
if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
exit(1);
1995-09-23 07:40:28 +04:00
/* XXX - Paranoia: Make sure size is aligned! */
if (protosize & (DEV_BSIZE - 1))
err(1, "proto bootblock bad size=%d", protosize);
1995-02-15 01:56:36 +03:00
/* 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-23 07:40:28 +04:00
/* Sync filesystems (to clean in-memory superblock?) */
sync();
1995-02-15 01:56:36 +03:00
if (write(devfd, protostore, protosize) != protosize)
err(1, "write bootstrap");
(void)close(devfd);
return 0;
}
1995-06-02 00:37:44 +04:00
char *
1995-02-15 01:56:36 +03:00
loadprotoblocks(fname, size)
char *fname;
long *size;
{
int fd;
1995-09-23 07:40:28 +04:00
size_t tdsize; /* text+data size */
size_t bbsize; /* boot block size (block aligned) */
1995-02-15 01:56:36 +03:00
char *bp;
1995-06-02 00:37:44 +04:00
struct nlist *nlp;
struct exec eh;
1995-02-15 01:56:36 +03:00
long off;
1995-06-02 00:37:44 +04:00
fd = -1;
bp = NULL;
1995-02-15 01:56:36 +03:00
/* Locate block number array in proto file */
if (nlist(fname, nl) != 0) {
warnx("nlist: %s: symbols not found", fname);
return NULL;
}
1995-06-02 00:37:44 +04:00
/* Validate symbol types (global data). */
for (nlp = nl; nlp->n_un.n_name; nlp++) {
if (nlp->n_type != (N_DATA | N_EXT)) {
warnx("nlist: %s: wrong type", nlp->n_un.n_name);
return NULL;
}
1995-02-15 01:56:36 +03:00
}
if ((fd = open(fname, O_RDONLY)) < 0) {
warn("open: %s", fname);
return NULL;
}
1995-06-02 00:37:44 +04:00
if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) {
warn("read: %s", fname);
goto bad;
1995-02-15 01:56:36 +03:00
}
1995-06-02 00:37:44 +04:00
if (N_GETMAGIC(eh) != OMAGIC) {
warn("bad magic: 0x%x", eh.a_midmag);
goto bad;
}
/*
1995-09-23 07:40:28 +04:00
* We have to include the exec header in the beginning of
* the buffer, and leave extra space at the end in case
* the actual write to disk wants to skip the header.
1995-06-02 00:37:44 +04:00
*/
1995-09-23 07:40:28 +04:00
tdsize = eh.a_text + eh.a_data;
bbsize = tdsize + sizeof(eh);
bbsize = roundup(bbsize, DEV_BSIZE);
1995-06-02 00:37:44 +04:00
1995-09-23 07:40:28 +04:00
/*
* Allocate extra space here because the caller may copy
* the boot block starting at the end of the exec header.
* This prevents reading beyond the end of the buffer.
*/
if ((bp = calloc(bbsize + sizeof(eh), 1)) == NULL) {
1995-02-15 01:56:36 +03:00
warnx("malloc: %s: no memory", fname);
1995-06-02 00:37:44 +04:00
goto bad;
1995-02-15 01:56:36 +03:00
}
1995-09-23 07:40:28 +04:00
/* Copy the exec header and read the rest of the file. */
1995-06-02 00:37:44 +04:00
memcpy(bp, &eh, sizeof(eh));
1995-09-23 07:40:28 +04:00
if (read(fd, bp+sizeof(eh), tdsize) != tdsize) {
1995-02-15 01:56:36 +03:00
warn("read: %s", fname);
1995-06-02 00:37:44 +04:00
goto bad;
1995-02-15 01:56:36 +03:00
}
1995-09-23 07:40:28 +04:00
*size = bbsize; /* aligned to DEV_BSIZE */
1995-02-15 01:56:36 +03:00
1995-06-02 00:37:44 +04:00
/* Calculate the symbols' locations within the proto file */
off = N_DATOFF(eh) - N_DATADDR(eh) - (eh.a_entry - N_TXTADDR(eh));
block_size_p = (int *) (bp + nl[X_BLOCK_SIZE ].n_value + off);
block_count_p = (int *) (bp + nl[X_BLOCK_COUNT].n_value + off);
block_table = (daddr_t *) (bp + nl[X_BLOCK_TABLE].n_value + off);
maxblocknum = *block_count_p;
1995-02-15 01:56:36 +03:00
if (verbose) {
1995-06-02 00:37:44 +04:00
printf("%s: entry point %#x\n", fname, eh.a_entry);
1995-02-15 01:56:36 +03:00
printf("proto bootblock size %ld\n", *size);
printf("room for %d filesystem blocks at %#x\n",
1995-06-02 00:37:44 +04:00
maxblocknum, nl[X_BLOCK_TABLE].n_value);
1995-02-15 01:56:36 +03:00
}
1995-06-02 00:37:44 +04:00
close(fd);
1995-09-23 07:40:28 +04:00
if (!hflag)
bp += sizeof(struct exec);
1995-02-15 01:56:36 +03:00
return bp;
1995-06-02 00:37:44 +04:00
bad:
if (bp)
free(bp);
if (fd >= 0)
close(fd);
return NULL;
1995-02-15 01:56:36 +03:00
}
1995-06-02 00:37:44 +04:00
static void
1995-02-15 01:56:36 +03:00
devread(fd, buf, blk, size, msg)
int fd;
1995-09-23 07:40:28 +04:00
void *buf;
1995-02-15 01:56:36 +03:00
daddr_t blk;
1995-09-23 07:40:28 +04:00
size_t size;
1995-02-15 01:56:36 +03: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;
struct stat statbuf;
struct statfs statfsbuf;
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);
if (fstatfs(fd, &statfsbuf) != 0)
err(1, "statfs: %s", boot);
1995-11-08 02:01:40 +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);
}
1995-02-15 01:56:36 +03:00
if (fsync(fd) != 0)
err(1, "fsync: %s", boot);
if (fstat(fd, &statbuf) != 0)
err(1, "fstat: %s", boot);
close(fd);
/* Read superblock */
1995-06-02 00:37:44 +04:00
devread(devfd, sblock, SBLOCK, SBSIZE, "superblock");
1995-02-15 01:56:36 +03:00
fs = (struct fs *)sblock;
1995-06-02 00:37:44 +04:00
/* Sanity-check super-block. */
if (fs->fs_magic != FS_MAGIC)
errx(1, "Bad magic number in superblock");
if (fs->fs_inopb <= 0)
err(1, "Bad inopb=%d in superblock", fs->fs_inopb);
1995-02-15 01:56:36 +03:00
/* 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-06-02 00:37:44 +04:00
* Have the inode. Figure out how many blocks we need.
1995-02-15 01:56:36 +03:00
*/
ndb = howmany(ip->di_size, fs->fs_bsize);
1995-06-02 00:37:44 +04:00
if (ndb > maxblocknum)
errx(1, "Too many blocks");
*block_count_p = ndb;
*block_size_p = fs->fs_bsize;
if (verbose)
printf("Will load %d blocks of size %d each.\n",
ndb, fs->fs_bsize);
/*
* Get the block numbers; we don't handle fragments
*/
1995-02-15 01:56:36 +03:00
ap = ip->di_db;
for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
1995-06-02 00:37:44 +04:00
blk = fsbtodb(fs, *ap);
if (verbose)
printf("%d: %d\n", i, blk);
block_table[i] = blk;
1995-02-15 01:56:36 +03: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-06-02 00:37:44 +04:00
blk = fsbtodb(fs, ip->di_ib[0]);
1995-02-15 01:56:36 +03:00
devread(devfd, buf, blk, fs->fs_bsize, "indirect block");
ap = (daddr_t *)buf;
for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
1995-06-02 00:37:44 +04:00
blk = fsbtodb(fs, *ap);
if (verbose)
printf("%d: %d\n", i, blk);
block_table[i] = blk;
1995-02-15 01:56:36 +03:00
}
return 0;
}