2009-04-05 15:48:02 +04:00
|
|
|
/* $NetBSD: bbinfo.c,v 1.14 2009/04/05 11:55:39 lukem Exp $ */
|
2002-05-15 13:56:59 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Matt Fredette, Paul Kranenburg, and Luke Mewburn.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-05-15 13:56:59 +04:00
|
|
|
#include <sys/cdefs.h>
|
2003-10-27 03:12:41 +03:00
|
|
|
#if !defined(__lint)
|
2009-04-05 15:48:02 +04:00
|
|
|
__RCSID("$NetBSD: bbinfo.c,v 1.14 2009/04/05 11:55:39 lukem Exp $");
|
2002-05-15 13:56:59 +04:00
|
|
|
#endif /* !__lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2008-05-09 14:53:55 +04:00
|
|
|
#ifndef HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/dkio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2002-05-15 13:56:59 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "installboot.h"
|
|
|
|
|
|
|
|
int
|
2002-05-20 20:05:26 +04:00
|
|
|
shared_bbinfo_clearboot(ib_params *params, struct bbinfo_params *bbparams,
|
|
|
|
int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
|
2002-05-15 13:56:59 +04:00
|
|
|
{
|
2002-05-20 20:05:26 +04:00
|
|
|
uint8_t *bb;
|
2002-05-15 13:56:59 +04:00
|
|
|
ssize_t rv;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
assert(params != NULL);
|
|
|
|
assert(params->fsfd != -1);
|
|
|
|
assert(params->filesystem != NULL);
|
|
|
|
assert(bbparams != NULL);
|
|
|
|
assert((strlen(bbparams->magic) + 1) == 32);
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
if ((bb = malloc(bbparams->maxsize)) == NULL) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warn("Allocating %lu bytes for bbinfo",
|
2002-05-15 13:56:59 +04:00
|
|
|
(unsigned long) bbparams->maxsize);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/* First check that it _could_ exist here */
|
2002-05-15 13:56:59 +04:00
|
|
|
rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
|
|
|
|
if (rv == -1) {
|
|
|
|
warn("Reading `%s'", params->filesystem);
|
|
|
|
goto done;
|
2009-04-05 15:48:02 +04:00
|
|
|
} else if ((uint32_t)rv != bbparams->maxsize) {
|
2002-05-15 13:56:59 +04:00
|
|
|
warnx("Reading `%s': short read", params->filesystem);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 20:05:26 +04:00
|
|
|
/* Now clear out (past the header offset) */
|
|
|
|
memset(bb + bbparams->headeroffset, 0,
|
|
|
|
bbparams->maxsize - bbparams->headeroffset);
|
|
|
|
if (callback != NULL && ! (*callback)(params, bbparams, bb))
|
|
|
|
goto done;
|
2002-05-15 13:56:59 +04:00
|
|
|
|
|
|
|
if (params->flags & IB_VERBOSE)
|
|
|
|
printf("%slearing boot block\n",
|
|
|
|
(params->flags & IB_NOWRITE) ? "Not c" : "C");
|
|
|
|
if (params->flags & IB_NOWRITE) {
|
|
|
|
retval = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
|
2008-05-09 14:53:55 +04:00
|
|
|
#ifdef DIOCWLABEL
|
|
|
|
if (rv == -1 && errno == EROFS) {
|
|
|
|
/*
|
|
|
|
* The first sector might be protected by
|
|
|
|
* bounds_check_with_label(9)
|
|
|
|
*/
|
|
|
|
int enable;
|
|
|
|
|
|
|
|
enable = 1;
|
|
|
|
rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
|
|
|
|
if (rv != 0) {
|
|
|
|
warn("Cannot enable writes to the label sector");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pwrite(params->fsfd, bb, bbparams->maxsize,
|
|
|
|
bbparams->offset);
|
|
|
|
|
|
|
|
/* Reset write-protect. */
|
|
|
|
enable = 0;
|
|
|
|
(void)ioctl(params->fsfd, DIOCWLABEL, &enable);
|
|
|
|
}
|
|
|
|
#endif
|
2002-05-15 13:56:59 +04:00
|
|
|
if (rv == -1) {
|
|
|
|
warn("Writing `%s'", params->filesystem);
|
|
|
|
goto done;
|
2009-04-05 15:48:02 +04:00
|
|
|
} else if ((uint32_t)rv != bbparams->maxsize) {
|
2002-05-15 13:56:59 +04:00
|
|
|
warnx("Writing `%s': short write", params->filesystem);
|
|
|
|
goto done;
|
|
|
|
} else
|
|
|
|
retval = 1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (bb != NULL)
|
|
|
|
free(bb);
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
|
2002-05-20 20:05:26 +04:00
|
|
|
int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
|
2002-05-15 13:56:59 +04:00
|
|
|
{
|
2002-05-20 20:05:26 +04:00
|
|
|
uint8_t *bb;
|
2002-05-15 13:56:59 +04:00
|
|
|
int retval;
|
|
|
|
ssize_t rv;
|
|
|
|
size_t bbi;
|
|
|
|
struct shared_bbinfo *bbinfop; /* bbinfo in prototype image */
|
|
|
|
uint32_t maxblk, nblk, blk_i;
|
|
|
|
ib_block *blocks;
|
|
|
|
|
|
|
|
assert(params != NULL);
|
|
|
|
assert(params->fsfd != -1);
|
|
|
|
assert(params->filesystem != NULL);
|
|
|
|
assert(params->fstype != NULL);
|
|
|
|
assert(params->s1fd != -1);
|
|
|
|
assert(params->stage1 != NULL);
|
|
|
|
assert(bbparams != NULL);
|
|
|
|
assert((strlen(bbparams->magic) + 1) == 32);
|
|
|
|
|
2005-06-07 17:49:31 +04:00
|
|
|
bbinfop = NULL; /* XXXGCC -Wuninitialized [sparc64] */
|
2002-05-15 13:56:59 +04:00
|
|
|
retval = 0;
|
|
|
|
blocks = NULL;
|
|
|
|
if ((bb = malloc(bbparams->maxsize)) == NULL) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warn("Allocating %lu bytes for bbinfo",
|
2002-05-15 13:56:59 +04:00
|
|
|
(unsigned long) bbparams->maxsize);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params->stage2 == NULL) {
|
|
|
|
warnx("Name of secondary bootstrap not provided");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params->s1stat.st_size >
|
|
|
|
bbparams->maxsize - bbparams->headeroffset) {
|
|
|
|
warnx("`%s' cannot be larger than %lu bytes",
|
|
|
|
params->stage1, (unsigned long)(bbparams->maxsize -
|
|
|
|
bbparams->headeroffset));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bb, 0, bbparams->maxsize);
|
|
|
|
rv = read(params->s1fd, bb + bbparams->headeroffset,
|
|
|
|
bbparams->maxsize - bbparams->headeroffset);
|
|
|
|
if (rv == -1) {
|
|
|
|
warn("Reading `%s'", params->stage1);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/*
|
|
|
|
* Quick sanity check that the bootstrap given
|
|
|
|
* is *not* an ELF executable.
|
|
|
|
*/
|
2002-05-15 13:56:59 +04:00
|
|
|
if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
|
|
|
|
== 0) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warnx("`%s' is an ELF executable; need raw binary",
|
2002-05-15 13:56:59 +04:00
|
|
|
params->stage1);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 20:05:26 +04:00
|
|
|
#define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
|
2009-04-05 15:48:02 +04:00
|
|
|
? (uint32_t)htole32((x)) : (uint32_t)htobe32((x)))
|
2002-05-21 20:49:19 +04:00
|
|
|
#define TARGET32TOHOST(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
|
2009-04-05 15:48:02 +04:00
|
|
|
? (uint32_t)le32toh((x)) : (uint32_t)be32toh((x)))
|
2002-05-15 13:56:59 +04:00
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/* Look for the bbinfo structure. */
|
2005-06-05 23:08:28 +04:00
|
|
|
bbinfop = NULL;
|
2002-05-15 13:56:59 +04:00
|
|
|
for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
|
2002-05-20 20:05:26 +04:00
|
|
|
bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
|
2002-05-15 13:56:59 +04:00
|
|
|
if (memcmp(bbinfop->bbi_magic, bbparams->magic,
|
|
|
|
sizeof(bbinfop->bbi_magic)) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bbi >= bbparams->maxsize) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warnx("%s bbinfo structure not found in `%s'",
|
2002-05-15 13:56:59 +04:00
|
|
|
params->machine->name, params->stage1);
|
|
|
|
goto done;
|
|
|
|
}
|
2002-05-21 20:49:19 +04:00
|
|
|
maxblk = TARGET32TOHOST(bbinfop->bbi_block_count);
|
2002-05-15 13:56:59 +04:00
|
|
|
if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
|
|
|
|
warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
|
|
|
|
params->machine->name, params->stage1, maxblk);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/* Allocate space for our block list. */
|
2002-05-15 13:56:59 +04:00
|
|
|
blocks = malloc(sizeof(*blocks) * maxblk);
|
|
|
|
if (blocks == NULL) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warn("Allocating %lu bytes",
|
|
|
|
(unsigned long)sizeof(*blocks) * maxblk);
|
2002-05-15 13:56:59 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(params->fsstat.st_mode)) {
|
|
|
|
if (fsync(params->fsfd) == -1)
|
|
|
|
warn("Synchronising file system `%s'",
|
|
|
|
params->filesystem);
|
|
|
|
} else {
|
|
|
|
/* Ensure the secondary bootstrap is on disk. */
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/* Collect the blocks for the secondary bootstrap. */
|
2002-05-15 13:56:59 +04:00
|
|
|
nblk = maxblk;
|
|
|
|
if (! params->fstype->findstage2(params, &nblk, blocks))
|
|
|
|
goto done;
|
|
|
|
if (nblk == 0) {
|
|
|
|
warnx("Secondary bootstrap `%s' is empty",
|
2002-05-20 19:04:25 +04:00
|
|
|
params->stage2);
|
2002-05-15 13:56:59 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:56:10 +04:00
|
|
|
/* Save those blocks in the primary bootstrap. */
|
2002-05-15 13:56:59 +04:00
|
|
|
bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
|
|
|
|
bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
|
|
|
|
for (blk_i = 0; blk_i < nblk; blk_i++) {
|
2002-05-20 19:04:25 +04:00
|
|
|
bbinfop->bbi_block_table[blk_i] =
|
2002-05-15 13:56:59 +04:00
|
|
|
HOSTTOTARGET32(blocks[blk_i].block);
|
|
|
|
if (blocks[blk_i].blocksize < blocks[0].blocksize &&
|
|
|
|
blk_i + 1 != nblk) {
|
2002-05-20 19:04:25 +04:00
|
|
|
warnx("Secondary bootstrap `%s' blocks do not have "
|
2002-05-15 13:56:59 +04:00
|
|
|
"a uniform size", params->stage2);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2002-05-15 17:34:27 +04:00
|
|
|
if (callback != NULL && ! (*callback)(params, bbparams, bb))
|
2002-05-15 13:56:59 +04:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (params->flags & IB_VERBOSE) {
|
|
|
|
printf("Bootstrap start sector: %u\n",
|
|
|
|
bbparams->offset / bbparams->blocksize);
|
|
|
|
printf("Bootstrap byte count: %u\n", (unsigned)rv);
|
|
|
|
printf("Bootstrap block table: "
|
|
|
|
"%u entries of %u bytes available, %u used:",
|
|
|
|
maxblk, blocks[0].blocksize, nblk);
|
|
|
|
for (blk_i = 0; blk_i < nblk; blk_i++)
|
2003-04-02 14:39:19 +04:00
|
|
|
printf(" %llu",
|
|
|
|
(unsigned long long)blocks[blk_i].block);
|
2002-05-15 13:56:59 +04:00
|
|
|
printf("\n%sriting bootstrap\n",
|
|
|
|
(params->flags & IB_NOWRITE) ? "Not w" : "W");
|
|
|
|
}
|
|
|
|
if (params->flags & IB_NOWRITE) {
|
|
|
|
retval = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
|
2008-05-09 14:53:55 +04:00
|
|
|
#ifdef DIOCWLABEL
|
|
|
|
if (rv == -1 && errno == EROFS) {
|
|
|
|
/*
|
|
|
|
* The first sector might be protected by
|
|
|
|
* bounds_check_with_label(9)
|
|
|
|
*/
|
|
|
|
int enable;
|
|
|
|
|
|
|
|
enable = 1;
|
|
|
|
rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
|
|
|
|
if (rv != 0) {
|
|
|
|
warn("Cannot enable writes to the label sector");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = pwrite(params->fsfd, bb, bbparams->maxsize,
|
|
|
|
bbparams->offset);
|
|
|
|
|
|
|
|
/* Reset write-protect. */
|
|
|
|
enable = 0;
|
|
|
|
(void)ioctl(params->fsfd, DIOCWLABEL, &enable);
|
|
|
|
}
|
|
|
|
#endif
|
2002-05-15 13:56:59 +04:00
|
|
|
if (rv == -1) {
|
|
|
|
warn("Writing `%s'", params->filesystem);
|
|
|
|
goto done;
|
2009-04-05 15:48:02 +04:00
|
|
|
} else if ((uint32_t)rv != bbparams->maxsize) {
|
2002-05-15 13:56:59 +04:00
|
|
|
warnx("Writing `%s': short write", params->filesystem);
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
retval = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (blocks != NULL)
|
2002-05-20 19:04:25 +04:00
|
|
|
free(blocks);
|
2002-05-15 13:56:59 +04:00
|
|
|
if (bb != NULL)
|
2002-05-20 19:04:25 +04:00
|
|
|
free(bb);
|
2002-05-15 13:56:59 +04:00
|
|
|
return (retval);
|
|
|
|
}
|