Bootblock installer - based heavily on the alpha installboot but with
some significant differences: * Because the pmax bootblocks don't have a fixed load address, we need to keep the first stage blocks in /usr/mdec with some sort of executable format that includes load and transfer address. Since ELF is native, /usr/mdec/bootxx_* are in ELF format and installboot (in loadbootstrap.c) needs knowledge of the ELF format. * Support for installing the first stage at an arbitiary position. -a adds the first stage to end of the "disk" (only on works on regular files) and -i <block> puts the first stage at ISO filesystem block number <block> for use in multi-arch boot CDs (see installboot(8) for more details and an example). * No checksum support. This should really be done in some sort of MI way rather than duplicating code between ports' installboot.c
This commit is contained in:
parent
b96c403e14
commit
50b389d4a6
6
sys/arch/pmax/stand/installboot/Makefile
Normal file
6
sys/arch/pmax/stand/installboot/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
# $NetBSD: Makefile,v 1.1 1999/11/28 00:32:29 simonb Exp $
|
||||
|
||||
PROG= installboot
|
||||
SRCS= installboot.c loadbootstrap.c
|
||||
|
||||
.include <bsd.prog.mk>
|
301
sys/arch/pmax/stand/installboot/installboot.c
Normal file
301
sys/arch/pmax/stand/installboot/installboot.c
Normal file
@ -0,0 +1,301 @@
|
||||
/* $NetBSD: installboot.c,v 1.1 1999/11/28 00:32:29 simonb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999 Ross Harvey. 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 Ross Harvey
|
||||
* for the NetBSD Project.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999 Christopher G. Demetriou. 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 Christopher G. Demetriou
|
||||
* for the NetBSD Project.
|
||||
* 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> /* XXX for roundup, howmany */
|
||||
#include <sys/stat.h>
|
||||
#include <sys/disklabel.h>
|
||||
#include <isofs/cd9660/iso.h>
|
||||
#include <assert.h>
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <include/dec_boot.h>
|
||||
|
||||
#include "installboot.h"
|
||||
|
||||
static void usage(void);
|
||||
static void clr_bootstrap(const char *disk);
|
||||
static void set_bootstrap(const char *disk, const char *bootstrap);
|
||||
|
||||
extern char *__progname;
|
||||
|
||||
int verbose, nowrite, append, isoblock;
|
||||
struct stat disksb;
|
||||
|
||||
static void
|
||||
usage()
|
||||
{
|
||||
fprintf(stderr, "usage:\n");
|
||||
fprintf(stderr, "\t%s [-nv] [-i block | -a] disk bootstrap\n", __progname);
|
||||
fprintf(stderr, "\t%s [-nv] -c disk\n", __progname);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *disk, *bootstrap;
|
||||
int c, clearflag;
|
||||
|
||||
clearflag = verbose = nowrite = append = isoblock = 0;
|
||||
|
||||
while ((c = getopt(argc, argv, "aci:nv")) != -1) {
|
||||
switch (c) {
|
||||
case 'a':
|
||||
/* Append to disk (image) */
|
||||
append = 1;
|
||||
break;
|
||||
case 'c':
|
||||
/* Clear any existing boot block */
|
||||
clearflag = 1;
|
||||
break;
|
||||
case 'i':
|
||||
/* Load bootstrap at iso block number */
|
||||
if ((isoblock = atoi(optarg)) <= 0)
|
||||
errx(1, "%s: bad iso block number", optarg);
|
||||
break;
|
||||
case 'n':
|
||||
/* Do not actually write the boot file */
|
||||
nowrite = 1;
|
||||
break;
|
||||
case 'v':
|
||||
/* Chat */
|
||||
verbose = 1;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
if ((clearflag && argc != 1) || (!clearflag && argc != 2))
|
||||
usage();
|
||||
if (append && isoblock)
|
||||
usage();
|
||||
|
||||
disk = argv[0];
|
||||
bootstrap = argv[1]; /* NULL if argc == 1 */
|
||||
|
||||
if (verbose) {
|
||||
fprintf(stderr, "disk: %s\n", disk);
|
||||
fprintf(stderr, "bootstrap: %s\n",
|
||||
bootstrap != NULL ? bootstrap : "to be cleared");
|
||||
}
|
||||
if (sizeof (struct boot_block) != BOOT_BLOCK_BLOCKSIZE)
|
||||
errx(EXIT_FAILURE,
|
||||
"boot_block structure badly sized (build error)");
|
||||
|
||||
if (append) {
|
||||
/* Only append to a regular file */
|
||||
if (stat(disk, &disksb) == -1)
|
||||
err(EXIT_FAILURE, "stat %s", disk);
|
||||
if (!S_ISREG(disksb.st_mode))
|
||||
errx(EXIT_FAILURE, "%s must be a regular file to append to", bootstrap);
|
||||
}
|
||||
|
||||
if (clearflag)
|
||||
clr_bootstrap(disk);
|
||||
else
|
||||
set_bootstrap(disk, bootstrap);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static void
|
||||
clr_bootstrap(const char *disk)
|
||||
{
|
||||
struct boot_block bb;
|
||||
ssize_t rv;
|
||||
int diskfd;
|
||||
|
||||
if ((diskfd = open(disk, nowrite ? O_RDONLY : O_RDWR)) == -1)
|
||||
err(EXIT_FAILURE, "open %s", disk);
|
||||
|
||||
rv = pread(diskfd, &bb, sizeof bb, BOOT_BLOCK_OFFSET);
|
||||
if (rv == -1)
|
||||
err(EXIT_FAILURE, "read %s", disk);
|
||||
else if (rv != sizeof bb)
|
||||
errx(EXIT_FAILURE, "read %s: short read", disk);
|
||||
|
||||
if (bb.magic != DEC_BOOT_MAGIC) {
|
||||
fprintf(stderr, "old boot block magic number invalid (%#x)\n",
|
||||
bb.magic);
|
||||
fprintf(stderr, "boot block invalid\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
bb.map[0].num_blocks = bb.map[0].start_block = bb.mode = 0;
|
||||
bb.magic = DEC_BOOT_MAGIC;
|
||||
|
||||
fprintf(stderr, "new boot block start sector: %#x\n",
|
||||
bb.map[0].start_block);
|
||||
fprintf(stderr, "new boot block size: %#x\n",
|
||||
bb.map[0].num_blocks);
|
||||
|
||||
if (nowrite) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "not writing\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "writing\n");
|
||||
|
||||
rv = pwrite(diskfd, &bb, sizeof bb, BOOT_BLOCK_OFFSET);
|
||||
if (rv == -1)
|
||||
err(EXIT_FAILURE, "write %s", disk);
|
||||
else if (rv != sizeof bb)
|
||||
errx(EXIT_FAILURE, "write %s: short write", disk);
|
||||
|
||||
done:
|
||||
(void)close(diskfd);
|
||||
}
|
||||
|
||||
static void
|
||||
set_bootstrap(const char *disk, const char *bootstrap)
|
||||
{
|
||||
struct stat bootstrapsb;
|
||||
struct boot_block bb;
|
||||
int diskfd, startblock;
|
||||
char *bootstrapbuf;
|
||||
size_t bootstrapsize;
|
||||
u_int32_t bootstrapload, bootstrapexec;
|
||||
ssize_t rv;
|
||||
|
||||
/* Open the input file and check it out */
|
||||
if (stat(bootstrap, &bootstrapsb) == -1)
|
||||
err(EXIT_FAILURE, "stat %s", bootstrap);
|
||||
if (!S_ISREG(bootstrapsb.st_mode))
|
||||
errx(EXIT_FAILURE, "%s must be a regular file", bootstrap);
|
||||
load_bootstrap(bootstrap, &bootstrapbuf, &bootstrapload, &bootstrapexec,
|
||||
&bootstrapsize);
|
||||
|
||||
if ((diskfd = open(disk, nowrite ? O_RDONLY : O_RDWR)) == -1)
|
||||
err(EXIT_FAILURE, "open %s", disk);
|
||||
|
||||
rv = pread(diskfd, &bb, sizeof bb, BOOT_BLOCK_OFFSET);
|
||||
if (rv == -1)
|
||||
err(EXIT_FAILURE, "read %s", disk);
|
||||
else if (rv != sizeof bb)
|
||||
errx(EXIT_FAILURE, "read %s: short read", disk);
|
||||
|
||||
/* fill in the updated boot block fields */
|
||||
if (append) {
|
||||
startblock = howmany(disksb.st_size, BOOT_BLOCK_BLOCKSIZE);
|
||||
} else if (isoblock) {
|
||||
startblock = isoblock * (ISO_DEFAULT_BLOCK_SIZE / BOOT_BLOCK_BLOCKSIZE);
|
||||
} else {
|
||||
startblock = BOOT_BLOCK_OFFSET / BOOT_BLOCK_BLOCKSIZE + 1;
|
||||
}
|
||||
|
||||
bb.map[0].start_block = startblock;
|
||||
bb.map[0].num_blocks = howmany(bootstrapsize, BOOT_BLOCK_BLOCKSIZE);
|
||||
bb.magic = DEC_BOOT_MAGIC;
|
||||
bb.load_addr = bootstrapload;
|
||||
bb.exec_addr = bootstrapexec;
|
||||
bb.mode = DEC_BOOTMODE_CONTIGUOUS;
|
||||
|
||||
if (verbose) {
|
||||
fprintf(stderr, "bootstrap starting sector: %i\n",
|
||||
bb.map[0].start_block);
|
||||
fprintf(stderr, "bootstrap sector count: %i\n",
|
||||
bb.map[0].num_blocks);
|
||||
fprintf(stderr, "bootstrap load address: %#x\n",
|
||||
bb.load_addr);
|
||||
fprintf(stderr, "bootstrap execute address: %#x\n",
|
||||
bb.exec_addr);
|
||||
}
|
||||
|
||||
if (nowrite) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "not writing\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "writing bootstrap\n");
|
||||
|
||||
rv = pwrite(diskfd, bootstrapbuf, bootstrapsize,
|
||||
startblock * BOOT_BLOCK_BLOCKSIZE);
|
||||
if (rv == -1)
|
||||
err(EXIT_FAILURE, "write %s", disk);
|
||||
else if (rv != bootstrapsize)
|
||||
errx(EXIT_FAILURE, "write %s: short write", disk);
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "writing boot block\n");
|
||||
|
||||
rv = pwrite(diskfd, &bb, sizeof bb, BOOT_BLOCK_OFFSET);
|
||||
if (rv == -1)
|
||||
err(EXIT_FAILURE, "write %s", disk);
|
||||
else if (rv != sizeof bb)
|
||||
errx(EXIT_FAILURE, "write %s: short write", disk);
|
||||
|
||||
done:
|
||||
(void)close(diskfd);
|
||||
}
|
3
sys/arch/pmax/stand/installboot/installboot.h
Normal file
3
sys/arch/pmax/stand/installboot/installboot.h
Normal file
@ -0,0 +1,3 @@
|
||||
/* $NetBSD: installboot.h,v 1.1 1999/11/28 00:32:29 simonb Exp $ */
|
||||
|
||||
void load_bootstrap __P((const char *, char **, u_int32_t *, u_int32_t *, size_t *));
|
114
sys/arch/pmax/stand/installboot/loadbootstrap.c
Normal file
114
sys/arch/pmax/stand/installboot/loadbootstrap.c
Normal file
@ -0,0 +1,114 @@
|
||||
/* $NetBSD: loadbootstrap.c,v 1.1 1999/11/28 00:32:29 simonb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Simon Burge.
|
||||
*
|
||||
* 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 the NetBSD
|
||||
* Foundation, Inc. and its contributors.
|
||||
* 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> /* XXX for roundup, howmany */
|
||||
#include <sys/types.h>
|
||||
#include <sys/exec_elf.h>
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <include/dec_boot.h>
|
||||
|
||||
#include "installboot.h"
|
||||
|
||||
#define MAX_SEGMENTS 10 /* We can load up to 10 segments */
|
||||
|
||||
struct seglist {
|
||||
Elf32_Addr addr;
|
||||
Elf32_Off f_offset;
|
||||
Elf32_Word f_size;
|
||||
};
|
||||
|
||||
void
|
||||
load_bootstrap(const char *bootstrap, char **data,
|
||||
u_int32_t *loadaddr, u_int32_t *execaddr, size_t *len)
|
||||
{
|
||||
int fd, i, nsegs;
|
||||
Elf32_Addr lowaddr, highaddr;
|
||||
Elf32_Ehdr ehdr;
|
||||
Elf32_Phdr phdr;
|
||||
struct seglist seglist[MAX_SEGMENTS];
|
||||
|
||||
if ((fd = open(bootstrap, O_RDONLY)) < 0)
|
||||
err(EXIT_FAILURE, "open %s", bootstrap);
|
||||
|
||||
if ((read(fd, &ehdr, sizeof(ehdr))) != sizeof(ehdr))
|
||||
err(EXIT_FAILURE, "read %s", bootstrap);
|
||||
if ((memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
|
||||
(ehdr.e_ident[EI_CLASS] != ELFCLASS32))
|
||||
errx(EXIT_FAILURE, "no ELF header in %s", bootstrap);
|
||||
|
||||
nsegs = highaddr = 0;
|
||||
lowaddr = ULONG_MAX;
|
||||
|
||||
for (i = 0; i < ehdr.e_phnum; i++) {
|
||||
if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
|
||||
err(1, "lseek %s", bootstrap);
|
||||
if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
|
||||
err(1, "read %s", bootstrap);
|
||||
if (phdr.p_type != PT_LOAD)
|
||||
continue;
|
||||
|
||||
seglist[nsegs].addr = phdr.p_paddr;
|
||||
seglist[nsegs].f_offset = phdr.p_offset;
|
||||
seglist[nsegs].f_size = phdr.p_filesz;
|
||||
nsegs++;
|
||||
|
||||
if (phdr.p_paddr < lowaddr)
|
||||
lowaddr = phdr.p_paddr;
|
||||
if (phdr.p_paddr + phdr.p_filesz > highaddr)
|
||||
highaddr = phdr.p_paddr + phdr.p_filesz;
|
||||
}
|
||||
|
||||
*loadaddr = lowaddr;
|
||||
*execaddr = ehdr.e_entry;
|
||||
*len = highaddr - lowaddr;
|
||||
if ((*data = malloc(roundup(*len, BOOT_BLOCK_BLOCKSIZE))) == NULL)
|
||||
err(1, "malloc %d bytes", roundup(*len, BOOT_BLOCK_BLOCKSIZE));
|
||||
|
||||
/* Now load the bootstrap into memory */
|
||||
for (i = 0; i < nsegs; i++) {
|
||||
if (lseek(fd, (off_t)seglist[i].f_offset, 0) < 0)
|
||||
err(1, "lseek %s", bootstrap);
|
||||
if (read(fd, *data + seglist[i].addr - lowaddr,
|
||||
seglist[i].f_size) != seglist[i].f_size)
|
||||
err(1, "read %s", bootstrap);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user