Add support for ELF boot blocks.

This commit is contained in:
scw 2000-12-04 18:44:51 +00:00
parent b29bcb645b
commit e267f2466b
6 changed files with 92 additions and 106 deletions

View File

@ -1,7 +1,7 @@
# from: @(#)Makefile 8.1 (Berkeley) 6/10/93
# $NetBSD: Makefile,v 1.5 2000/07/24 18:39:20 jdolecek Exp $
# $NetBSD: Makefile,v 1.6 2000/12/04 18:44:51 scw Exp $
SRCS= bootxx.c conf.c
SRCS= bootxx.c conf.c block_x.S
PROG= bootxx
LIBS= ${LIBSA} ${LIBBUG}
SRTOBJ=

View File

@ -0,0 +1,15 @@
/* $NetBSD: block_x.S,v 1.1 2000/12/04 18:44:51 scw Exp $ */
#include <machine/asm.h>
#include "bootxx.h"
.text
GLOBAL(block_size)
.long 512
GLOBAL(block_count)
.long MAXBLOCKNUM
GLOBAL(block_table)
.space (MAXBLOCKNUM * 4)

View File

@ -1,4 +1,4 @@
/* $NetBSD: bootxx.c,v 1.5 2000/07/24 18:39:21 jdolecek Exp $ */
/* $NetBSD: bootxx.c,v 1.6 2000/12/04 18:44:51 scw Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@ -49,12 +49,14 @@
#include <sys/param.h>
#include <sys/time.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <machine/prom.h>
#include "stand.h"
#include "libsa.h"
#include "bootxx.h"
int copyboot __P((struct open_file *, char *));
int copyboot __P((struct open_file *, u_long *));
void main __P((void));
/*
@ -63,18 +65,9 @@ void main __P((void));
#define LOADADDR 0x11000 /* where to load level 2 bootstrap */
/* (l2 must relocate itself) */
/* This determines the largest boot program we can load. */
#define MAXBLOCKNUM 64
/*
* These three names are known by installboot.
* The block_table contains starting block numbers,
* in terms of 512-byte blocks. Each non-zero value
* will result in a read of block_size bytes.
*/
int block_size = 512; /* default */
int block_count = MAXBLOCKNUM; /* length of table */
daddr_t block_table[MAXBLOCKNUM] = { 0 };
extern int block_size;
extern int block_count; /* length of table */
extern daddr_t block_table[];
extern char bootprog_name[], bootprog_rev[];
@ -82,7 +75,8 @@ void
main()
{
struct open_file f;
char *addr;
u_long addr;
char *foo;
int error;
printf("Boot: bug device: ctrl=%d, dev=%d\n",
@ -91,16 +85,16 @@ main()
bootprog_name, bootprog_rev);
f.f_flags = F_RAW;
if (devopen(&f, 0, &addr)) {
if (devopen(&f, 0, &foo)) {
printf("bootxx: open failed\n");
_rtt();
}
addr = (char*)LOADADDR;
error = copyboot(&f, addr);
addr = LOADADDR;
error = copyboot(&f, &addr);
f.f_dev->dv_close(&f);
if (!error)
bugexec((void (*)__P((void)))addr);
bugexec((void *)addr);
/* copyboot had a problem... */
_rtt();
@ -109,13 +103,16 @@ main()
int
copyboot(fp, addr)
struct open_file *fp;
char *addr;
u_long *addr;
{
int n, i, blknum;
struct exec *x;
char *laddr = (char *) *addr;
union boothead {
struct exec ah;
Elf32_Ehdr eh;
} *x;
addr -= sizeof(struct exec); /* assume OMAGIC, verify below */
x = (struct exec *)addr;
x = (union boothead *)laddr;
if (!block_count) {
printf("bootxx: no data!?!\n");
@ -131,7 +128,7 @@ copyboot(fp, addr)
printf("bootxx: read block # %d = %d\n", i, blknum);
#endif
if ((fp->f_dev->dv_strategy)(fp->f_devdata, F_READ,
blknum, block_size, addr, &n))
blknum, block_size, laddr, &n))
{
printf("bootxx: read failed\n");
return -1;
@ -140,13 +137,19 @@ copyboot(fp, addr)
printf("bootxx: short read\n");
return -1;
}
addr += block_size;
laddr += block_size;
}
if (N_GETMAGIC(*x) != OMAGIC) {
printf("bootxx: secondary bootstrap isn't in OMAGIC format\n");
if (N_GETMAGIC(x->ah) == OMAGIC)
*addr += sizeof(x->ah);
else
if (memcmp(x->eh.e_ident, ELFMAG, SELFMAG) == 0) {
Elf32_Phdr *ep = (Elf32_Phdr *)(*addr + x->eh.e_phoff);
*addr += ep->p_offset;
} else {
printf("bootxx: secondary bootstrap isn't an executable\n");
return(-1);
}
return 0;
return (0);
}

View File

@ -0,0 +1,5 @@
/* $NetBSD: bootxx.h,v 1.1 2000/12/04 18:44:51 scw Exp $ */
/* This determines the largest boot program we can load. */
#define MAXBLOCKNUM 64

View File

@ -1,13 +1,18 @@
# $NetBSD: Makefile,v 1.5 2000/07/24 18:39:37 jdolecek Exp $
# $NetBSD: Makefile,v 1.6 2000/12/04 18:44:51 scw Exp $
.include <bsd.own.mk>
PROG= installboot
MKMAN= no
BINDIR=/usr/mdec
LIBSA=${.CURDIR}/../../../../lib/libsa
COPTS+= -Wall -Wstrict-prototypes -Wmissing-prototypes
CPPFLAGS+= -I${LIBSA}
# Need this to work in the miniroot
LDSTATIC?= -static
.PATH.c: ${LIBSA}
SRCS= installboot.c loadfile.c
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: installboot.c,v 1.5 2000/07/10 22:01:16 jdolecek Exp $ */
/* $NetBSD: installboot.c,v 1.6 2000/12/04 18:44:52 scw Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@ -45,7 +45,6 @@
#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>
@ -54,25 +53,28 @@
#include <unistd.h>
#include <sys/disklabel.h>
#include "loadfile.h"
int verbose, nowrite, hflag;
char *boot, *proto, *dev;
struct nlist nl[] = {
#define X_BLOCK_SIZE 0
{ { "_block_size" } },
#define X_BLOCK_COUNT 1
{ { "_block_count" } },
#define X_BLOCK_TABLE 2
{ { "_block_table" } },
{ {NULL} }
{ "block_size" },
{ "block_count" },
{ "block_table" },
{ NULL }
};
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 */
daddr_t *block_table; /* block number array in prototype image */
int maxblocknum; /* size of this array */
char *loadprotoblocks __P((char *, long *));
char *loadprotoblocks __P((char *, size_t *));
int loadblocknums __P((char *, int));
static void devread __P((int, void *, daddr_t, size_t, char *));
static void usage __P((void));
@ -95,7 +97,7 @@ main(argc, argv)
int c;
int devfd;
char *protostore;
long protosize;
size_t protosize;
while ((c = getopt(argc, argv, "vnh")) != -1) {
switch (c) {
@ -136,7 +138,7 @@ main(argc, argv)
/* XXX - Paranoia: Make sure size is aligned! */
if (protosize & (DEV_BSIZE - 1))
err(1, "proto bootblock bad size=%ld", protosize);
err(1, "proto bootblock bad size=%d", protosize);
/* Open and check raw disk device */
if ((devfd = open(dev, O_RDONLY, 0)) < 0)
@ -176,95 +178,51 @@ main(argc, argv)
char *
loadprotoblocks(fname, size)
char *fname;
long *size;
size_t *size;
{
int fd;
size_t tdsize; /* text+data size */
size_t bbsize; /* boot block size (block aligned) */
char *bp;
struct nlist *nlp;
struct exec eh;
long off;
u_long marks[MARK_MAX], bp, offs;
fd = -1;
bp = NULL;
/* Locate block number array in proto file */
if (nlist(fname, nl) != 0) {
warnx("nlist: %s: symbols not found", fname);
return NULL;
}
/* 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;
}
}
if ((fd = open(fname, O_RDONLY)) < 0) {
warn("open: %s", fname);
marks[MARK_START] = 0;
if ((fd = loadfile(fname, marks, COUNT_TEXT|COUNT_DATA)) == -1)
return NULL;
}
if (read(fd, &eh, sizeof(eh)) != sizeof(eh)) {
warn("read: %s", fname);
goto bad;
}
if (N_GETMAGIC(eh) != OMAGIC) {
warn("bad magic: 0x%lx", eh.a_midmag);
goto bad;
}
/*
* 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.
*/
tdsize = eh.a_text + eh.a_data;
bbsize = tdsize + sizeof(eh);
bbsize = roundup(bbsize, DEV_BSIZE);
(void)close(fd);
/*
* 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) {
warnx("malloc: %s: no memory", fname);
goto bad;
}
/* Copy the exec header and read the rest of the file. */
memcpy(bp, &eh, sizeof(eh));
if (read(fd, bp+sizeof(eh), tdsize) != tdsize) {
warn("read: %s", fname);
goto bad;
}
*size = roundup(marks[MARK_END] - marks[MARK_START], DEV_BSIZE);
bp = (u_long)malloc(*size);
*size = bbsize; /* aligned to DEV_BSIZE */
offs = marks[MARK_START];
marks[MARK_START] = bp - offs;
if ((fd = loadfile(fname, marks, LOAD_TEXT|LOAD_DATA)) == -1)
return NULL;
(void)close(fd);
/* 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);
block_size_p = (int *) (bp + (nl[X_BLOCK_SIZE ].n_value - offs));
block_count_p = (int *) (bp + (nl[X_BLOCK_COUNT].n_value - offs));
block_table = (daddr_t *) (bp + (nl[X_BLOCK_TABLE].n_value - offs));
maxblocknum = *block_count_p;
if (verbose) {
printf("%s: entry point %#lx\n", fname, eh.a_entry);
printf("proto bootblock size %ld\n", *size);
printf("%s: entry point %#lx\n", fname, marks[MARK_ENTRY]);
printf("proto bootblock size %d\n", *size);
printf("room for %d filesystem blocks at %#lx\n",
maxblocknum, nl[X_BLOCK_TABLE].n_value);
}
close(fd);
if (!hflag)
bp += sizeof(struct exec);
return bp;
return (char *) bp;
bad:
if (bp)
free(bp);
if (fd >= 0)
close(fd);
free((void *)bp);
return NULL;
}