summaryrefslogtreecommitdiff
path: root/sys/stand/mkuboot
diff options
context:
space:
mode:
authorDale Rahn <drahn@cvs.openbsd.org>2010-06-30 02:02:02 +0000
committerDale Rahn <drahn@cvs.openbsd.org>2010-06-30 02:02:02 +0000
commitf840506322055d184c1724f3e33544d173d08510 (patch)
tree5364919f471463dd44fefea98a598dcde813150d /sys/stand/mkuboot
parentfcce5eb94d61a5966c73a4f8730b7c075e715459 (diff)
Add support for naming images and setting the type, including boot scripts.
Input from deraadt@ and mk@
Diffstat (limited to 'sys/stand/mkuboot')
-rw-r--r--sys/stand/mkuboot/mkuboot.820
-rw-r--r--sys/stand/mkuboot/mkuboot.c70
2 files changed, 81 insertions, 9 deletions
diff --git a/sys/stand/mkuboot/mkuboot.8 b/sys/stand/mkuboot/mkuboot.8
index 622826d84a8..d61c4a8f2b0 100644
--- a/sys/stand/mkuboot/mkuboot.8
+++ b/sys/stand/mkuboot/mkuboot.8
@@ -1,4 +1,4 @@
-.\" $OpenBSD: mkuboot.8,v 1.2 2010/03/10 14:44:17 sobrado Exp $
+.\" $OpenBSD: mkuboot.8,v 1.3 2010/06/30 02:02:01 drahn Exp $
.\"
.\" Copyright (c) 2008 Mark Kettenis <kettenis@openbsd.org>
.\"
@@ -14,7 +14,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: March 10 2010 $
+.Dd $Mdocdate: June 30 2010 $
.Dt MKUBOOT 8
.Os
.Sh NAME
@@ -25,7 +25,9 @@
.Op Fl a Ar arch
.Op Fl e Ar entry
.Op Fl l Ar loadaddr
+.Op Fl n Ar name
.Op Fl o Ar os
+.Op Fl t Ar type
.Ar infile outfile
.Sh DESCRIPTION
The
@@ -44,6 +46,8 @@ Sets the entry point to
.It Fl l Ar loadaddr
Sets the load address to
.Ar loadaddr .
+.It Fl n Ar name
+Sets the name of the loaded object inside the generated image.
.It Fl o Ar os
Sets the image OS to
.Ar os .
@@ -52,6 +56,9 @@ can be either
.Dq Linux
or
.Dq OpenBSD .
+.It Fl t Ar type
+Sets the type of the object to be loaded.
+For a list of valid arguments, see below.
.El
.Pp
The following arguments are valid as the
@@ -70,6 +77,15 @@ sparc
sparc64
superh
.Ed
+.Pp
+The following arguments are valid as the
+.Ar type
+parameter:
+.Bd -unfilled -offset indent -compact
+standalone
+kernel
+script
+.Ed
.Sh HISTORY
An
.Nm
diff --git a/sys/stand/mkuboot/mkuboot.c b/sys/stand/mkuboot/mkuboot.c
index 516288a6ee4..34cc34b9ac5 100644
--- a/sys/stand/mkuboot/mkuboot.c
+++ b/sys/stand/mkuboot/mkuboot.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mkuboot.c,v 1.1 2010/02/12 17:30:41 mk Exp $ */
+/* $OpenBSD: mkuboot.c,v 1.2 2010/06/30 02:02:01 drahn Exp $ */
/*
* Copyright (c) 2008 Mark Kettenis
@@ -17,6 +17,7 @@
*/
#include <sys/types.h>
+#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
@@ -44,6 +45,7 @@
#define IH_TYPE_STANDALONE 1 /* Standalone */
#define IH_TYPE_KERNEL 2 /* OS Kernel Image */
+#define IH_TYPE_SCRIPT 6 /* Script file */
#define IH_COMP_NONE 0 /* No compression */
@@ -89,6 +91,17 @@ static const struct arch_map archmap[] = {
{ 0, NULL }
};
+struct type_map {
+ int id;
+ const char *type;
+};
+static const struct type_map typemap[] = {
+ { IH_TYPE_STANDALONE, "standalone" },
+ { IH_TYPE_KERNEL, "kernel" },
+ { IH_TYPE_SCRIPT, "script" },
+ { 0, NULL }
+};
+
struct os_map {
int id;
const char *arch;
@@ -105,19 +118,24 @@ int
main(int argc, char *argv[])
{
struct image_header ih;
+ struct stat sb;
const struct arch_map *mapptr;
const struct os_map *osmapptr;
+ const struct type_map *typemapptr;
const char *iname, *oname;
const char *arch = MACHINE_ARCH;
const char *os = "OpenBSD";
+ const char *type = "kernel";
+ const char *imgname = "boot";
int ifd, ofd;
+ uint32_t fsize;
u_long crc;
ssize_t nbytes;
char buf[BUFSIZ];
int c, ep, load;
ep = load = 0;
- while ((c = getopt(argc, argv, "a:e:l:o:")) != -1) {
+ while ((c = getopt(argc, argv, "a:e:l:n:o:t:")) != -1) {
switch (c) {
case 'a':
arch = optarg;
@@ -128,9 +146,15 @@ main(int argc, char *argv[])
case 'l':
sscanf(optarg, "0x%x", &load);
break;
+ case 'n':
+ imgname = optarg;
+ break;
case 'o':
os = optarg;
break;
+ case 't':
+ type = optarg;
+ break;
default:
usage();
}
@@ -154,6 +178,15 @@ main(int argc, char *argv[])
usage();
}
+ for (typemapptr = typemap; typemapptr->type; typemapptr++)
+ if (strcasecmp(type, typemapptr->type) == 0)
+ break;
+
+ if (typemapptr->type == NULL) {
+ printf("unknown type '%s'\n", os);
+ usage();
+ }
+
if (argc - optind != 2)
usage();
@@ -168,9 +201,9 @@ main(int argc, char *argv[])
ih.ih_ep = htobe32(ep);
ih.ih_os = osmapptr->id;
ih.ih_arch = mapptr->id;
- ih.ih_type = IH_TYPE_KERNEL;
+ ih.ih_type = typemapptr->id;
ih.ih_comp = IH_COMP_NONE;
- strlcpy(ih.ih_name, "boot", sizeof ih.ih_name);
+ strlcpy(ih.ih_name, imgname, sizeof ih.ih_name);
ifd = open(iname, O_RDONLY);
if (ifd < 0)
@@ -180,12 +213,30 @@ main(int argc, char *argv[])
if (ofd < 0)
err(1, "%s", oname);
+ if (stat(iname, &sb) == -1) {
+ err(1, "%s", oname);
+ }
+
/* Write initial header. */
if (write(ofd, &ih, sizeof ih) != sizeof ih)
err(1, "%s", oname);
- /* Copy data, calculating the data CRC as we go. */
+ /* Write data, calculating the data CRC as we go. */
crc = crc32(0L, Z_NULL, 0);
+
+ if (ih.ih_type == IH_TYPE_SCRIPT) {
+ /* scripts have two extra words of size/pad */
+ fsize = htobe32(sb.st_size);
+ printf("size %d\n", fsize);
+ crc = crc32(crc, (void *)&fsize, sizeof(fsize));
+ if (write(ofd, &fsize, sizeof fsize) != sizeof fsize)
+ err(1, "%s", oname);
+ fsize = 0;
+ crc = crc32(crc, (void *)&fsize, sizeof(fsize));
+ if (write(ofd, &fsize, sizeof fsize) != sizeof fsize)
+ err(1, "%s", oname);
+ }
+
while ((nbytes = read(ifd, buf, sizeof buf)) != 0) {
if (nbytes == -1)
err(1, "%s", iname);
@@ -195,6 +246,11 @@ main(int argc, char *argv[])
ih.ih_size += nbytes;
}
ih.ih_dcrc = htobe32(crc);
+
+ if (ih.ih_type == IH_TYPE_SCRIPT) {
+ ih.ih_size += 8; /* two extra pad words */
+ }
+
ih.ih_size = htobe32(ih.ih_size);
/* Calculate header CRC. */
@@ -217,8 +273,8 @@ usage(void)
const struct os_map *osmapptr;
(void)fprintf(stderr,
- "usage: %s [-a arch] [-e entry] [-l loadaddr] [-o os] "
- "infile outfile\n", __progname);
+ "usage: %s [-a arch] [-e entry] [-l loadaddr] [-n name] [-o os] "
+ "[-t type ] infile outfile\n", __progname);
(void)fprintf(stderr,
"arch is one of:");
for (mapptr = archmap; mapptr->arch; mapptr++)