/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn of Wasabi Systems.
*
* 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.
*/
/* set missing defaults */
if (params->machine == NULL) {
if (uname(&utsname) == -1)
err(1, "Determine uname");
getmachine(params, utsname.machine, "uname()");
}
/* Check that options are supported by this system */
unsupported_flags = params->flags & ~params->machine->valid_flags;
unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE | IB_CLEAR | IB_EDIT
| IB_FORCE);
if (unsupported_flags != 0) {
int ndx;
for (ndx = 0; options[ndx].name != NULL; ndx++) {
if (unsupported_flags & options[ndx].flag) {
unsupported_flags &= ~options[ndx].flag;
warnx("`-o %s' is not supported for %s",
options[ndx].name, params->machine->name);
}
}
if (unsupported_flags & IB_STAGE1START)
warnx("`-b bno' is not supported for %s",
params->machine->name);
if (unsupported_flags & IB_STAGE2START)
warnx("`-B bno' is not supported for %s",
params->machine->name);
unsupported_flags &= ~(IB_STAGE1START | IB_STAGE2START);
if (unsupported_flags != 0)
warnx("Unknown unsupported flag %#x (coding error!)",
unsupported_flags);
exit(1);
}
/* and some illegal combinations */
if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
warnx("Can't use `-b bno' with `-o append'");
exit(1);
}
if (params->flags & IB_CLEAR &&
params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
exit(1);
}
if (argc >= 3) {
params->stage2 = argv[2];
}
#if !HAVE_NBTOOL_CONFIG_H
special = getfsspecname(specname, sizeof(specname), argv[0]);
if (special == NULL)
err(1, "%s: %s", argv[0], specname);
raw = getdiskrawname(rawname, sizeof(rawname), special);
if (raw != NULL)
special = raw;
params->filesystem = special;
#else
params->filesystem = argv[0];
#endif
if (params->flags & IB_NOWRITE) {
op = "only";
mode = O_RDONLY;
} else {
op = "write";
mode = O_RDWR;
}
/* XXX should be specified via option */
params->sectorsize = DFL_SECSIZE;
if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
err(1, "Opening file system `%s' read-%s",
params->filesystem, op);
if (fstat(params->fsfd, ¶ms->fsstat) == -1)
err(1, "Examining file system `%s'", params->filesystem);
if (params->fstype != NULL) {
if (! params->fstype->match(params))
errx(1, "File system `%s' is not of type %s",
params->filesystem, params->fstype->name);
} else {
if (params->stage2 != NULL) {
params->fstype = &fstypes[0];
while (params->fstype->name != NULL &&
!params->fstype->match(params))
params->fstype++;
if (params->fstype->name == NULL)
errx(1, "File system `%s' is of an unknown type",
params->filesystem);
}
}
assert(params->machine != NULL);
if (argc >= 2) {
if ((params->s1fd = open(argv[1], O_RDONLY, 0600)) == -1)
err(1, "Opening primary bootstrap `%s'", argv[1]);
if (fstat(params->s1fd, ¶ms->s1stat) == -1)
err(1, "Examining primary bootstrap `%s'", argv[1]);
if (!S_ISREG(params->s1stat.st_mode)) {
/*
* If the platform uses u-boot, then the stage1
* spec might be the directory where the u-boot
* binaries for the system are located.
*/
if (params->machine->mach_flags & MF_UBOOT) {
if (!S_ISDIR(params->s1stat.st_mode)) {
errx(1, "`%s' must be a regular file "
"or a directory", argv[1]);
}
(void) close(params->s1fd);
params->s1fd = -1;
} else {
errx(1, "`%s' must be a regular file", argv[1]);
}
}
params->stage1 = argv[1];
}
if (params->flags & IB_VERBOSE) {
printf("File system: %s\n", params->filesystem);
if (params->fstype)
printf("File system type: %s (blocksize %u, "
"needswap %d)\n",
params->fstype->name, params->fstype->blocksize,
params->fstype->needswap);
if (!(params->flags & IB_EDIT))
printf("Primary bootstrap: %s%s\n",
(params->flags & IB_CLEAR) ? "(to be cleared)"
: params->stage1 ? params->stage1 : "(none)",
S_ISDIR(params->s1stat.st_mode) ? " (directory)"
: "");
if (params->stage2 != NULL)
printf("Secondary bootstrap: %s\n", params->stage2);
}
if (params->flags & IB_EDIT) {
op = "Edit";
rv = params->machine->editboot(params);
} else if (params->flags & IB_CLEAR) {
op = "Clear";
rv = params->machine->clearboot(params);
} else {
if (argc < 2) {
/*
* If the platform uses u-boot, then the stage1 spec is
* optional iff they specified a board (because we can
* infer a default location for u-boot binaries if the
* board type is given).
*/
if (!(params->machine->mach_flags & MF_UBOOT)) {
errx(EXIT_FAILURE,
"Please specify the primary bootstrap file");
}
}
op = "Set";
rv = params->machine->setboot(params);
}
if (rv == 0)
errx(1, "%s bootstrap operation failed", op);
if (S_ISREG(params->fsstat.st_mode)) {
if (fsync(params->fsfd) == -1)
err(1, "Synchronising file system `%s'",
params->filesystem);
} else {
/* Sync filesystems (to clean in-memory superblock?) */
sync();
}
if (close(params->fsfd) == -1)
err(1, "Closing file system `%s'", params->filesystem);
if (params->s1fd != -1)
if (close(params->s1fd) == -1)
err(1, "Closing primary bootstrap `%s'",
params->stage1);
exit(0);
/* NOTREACHED */
}
static void
parseoptions(ib_params *params, const char *option)
{
char *cp;
const struct option *opt;
int len;
unsigned long val;
assert(params != NULL);
assert(option != NULL);
for (;; option += len) {
option += strspn(option, ", \t");
if (*option == 0)
return;
len = strcspn(option, "=,");
for (opt = options; opt->name != NULL; opt++) {
if (memcmp(option, opt->name, len) == 0
&& opt->name[len] == 0)
break;
}
if (opt->name == NULL) {
len = strcspn(option, ",");
warnx("Unknown option `-o %.*s'", len, option);
break;
}
params->flags |= opt->flag;
if (opt->type == OPT_BOOL) {
if (option[len] != '=')
continue;
warnx("Option `%s' must not have a value", opt->name);
break;
}
if (option[len] != '=') {
warnx("Option `%s' must have a value", opt->name);
break;
}
option += len + 1;
len = strcspn(option, ",");
switch (opt->type) {
case OPT_STRING:
len = strlen(option);
/* FALLTHROUGH */
case OPT_WORD:
cp = strdup(option);
if (cp == NULL)
err(1, "strdup");
cp[len] = 0;
OPTION(params, char *, opt) = cp;
continue;
case OPT_INT:
val = strtoul(option, &cp, 0);
if (cp > option + len || (*cp != 0 && *cp != ','))
break;
if (val > INT_MAX)
break;
OPTION(params, int, opt) = (int)val;
continue;
default:
errx(1, "Internal error: option `%s' has invalid type %d",
opt->name, opt->type);
}
warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
break;
}
options_usage();
exit(1);
}
static void
options_usage(void)
{
int ndx;
const char *pfx;