summaryrefslogtreecommitdiff
path: root/sys/dev/microcode/bwi
diff options
context:
space:
mode:
authorMarcus Glocker <mglocker@cvs.openbsd.org>2007-10-04 17:46:10 +0000
committerMarcus Glocker <mglocker@cvs.openbsd.org>2007-10-04 17:46:10 +0000
commit6201e269cecd1123f4c298ef288cb4fa3f3a0b49 (patch)
treee96853cfc58ffd97a2bb02d5e86a31ac926ac9b0 /sys/dev/microcode/bwi
parent593cf8686b293ebd8a1ab87d26ff34474c5df585 (diff)
Import the bwi single firmware-file builder / extractor before we forget
about it. It's not linked into microcode/Makefile. OK deraadt@
Diffstat (limited to 'sys/dev/microcode/bwi')
-rw-r--r--sys/dev/microcode/bwi/Makefile5
-rw-r--r--sys/dev/microcode/bwi/build/Makefile8
-rw-r--r--sys/dev/microcode/bwi/build/build.c160
-rw-r--r--sys/dev/microcode/bwi/extract/Makefile8
-rw-r--r--sys/dev/microcode/bwi/extract/extract.c103
5 files changed, 284 insertions, 0 deletions
diff --git a/sys/dev/microcode/bwi/Makefile b/sys/dev/microcode/bwi/Makefile
new file mode 100644
index 00000000000..a32dda82218
--- /dev/null
+++ b/sys/dev/microcode/bwi/Makefile
@@ -0,0 +1,5 @@
+# $OpenBSD: Makefile,v 1.1 2007/10/04 17:46:09 mglocker Exp $
+
+SUBDIR= build extract
+
+.include <bsd.subdir.mk>
diff --git a/sys/dev/microcode/bwi/build/Makefile b/sys/dev/microcode/bwi/build/Makefile
new file mode 100644
index 00000000000..e48a0f181b7
--- /dev/null
+++ b/sys/dev/microcode/bwi/build/Makefile
@@ -0,0 +1,8 @@
+# $OpenBSD: Makefile,v 1.1 2007/10/04 17:46:09 mglocker Exp $
+
+NOMAN=
+
+PROG= build
+SRCS= build.c
+
+.include <bsd.prog.mk>
diff --git a/sys/dev/microcode/bwi/build/build.c b/sys/dev/microcode/bwi/build/build.c
new file mode 100644
index 00000000000..d93be003090
--- /dev/null
+++ b/sys/dev/microcode/bwi/build/build.c
@@ -0,0 +1,160 @@
+/* $OpenBSD: build.c,v 1.1 2007/10/04 17:46:09 mglocker Exp $ */
+
+/*
+ * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+//#define VERBOSE 1
+#define FILENAME "bwi-airforce"
+
+struct header {
+ char filename[64];
+ int filesize;
+ int fileoffset;
+};
+
+int
+main(int argc, char *argv[])
+{
+ void *p;
+ int i, j, offset;
+ int fdout, fdin;
+ int nfiles, headersize;
+ int fwsize, totalsize;
+ struct header h[argc - 1];
+ struct stat s;
+
+ if (argc < 2) {
+ printf("%s <firmware files>\n", argv[0]);
+ exit(1);
+ }
+
+ nfiles = argc - 1; /* number of firmware files */
+ headersize = sizeof(h) + sizeof(nfiles); /* size of file header */
+
+ /* initialize header struct */
+ for (i = 1, j = 0, fwsize = 0; i < argc; i++, j++) {
+ bzero(h[j].filename, sizeof(h[j].filename));
+ strlcpy(h[j].filename, argv[i], sizeof(h[j].filename));
+
+ if (stat(h[j].filename, &s) == -1)
+ err(1, "header initialization failed");
+
+ h[j].filesize = s.st_size;
+ h[j].fileoffset = 0;
+
+ fwsize += h[j].filesize;
+#ifdef VERBOSE
+ printf("create header entry for %s (%d bytes)\n",
+ h[j].filename, h[j].filesize);
+#endif
+ }
+
+ /* calculate total file size */
+ totalsize = headersize + fwsize;
+#if VERBOSE
+ printf("\n");
+ printf("header size = %d bytes, ", headersize);
+ printf("fw size = %d bytes, ", fwsize);
+ printf("total file size = %d bytes\n", totalsize);
+ printf("\n");
+#endif
+
+ /* calculating firmware offsets */
+ for (i = 0, offset = headersize; i < nfiles; i++) {
+ h[i].fileoffset = offset;
+ offset += h[i].filesize;
+#ifdef VERBOSE
+ printf("offset of %s = %d\n", h[i].filename, h[i].fileoffset);
+#endif
+ }
+
+ /* open output file */
+ if ((fdout = open(FILENAME, O_CREAT|O_TRUNC|O_RDWR, 0644)) == -1)
+ err(1, "open output file failed");
+
+ /* host to network byte order */
+ for (i = 0; i < nfiles; i++) {
+ h[i].filesize = htonl(h[i].filesize);
+ h[i].fileoffset = htonl(h[i].fileoffset);
+ }
+ nfiles = htonl(nfiles);
+
+ /* write header */
+ if (write(fdout, &nfiles, sizeof(nfiles)) < 1) {
+ close(fdout);
+ err(1, "write header 1 to output file failed\n");
+ }
+ if (write(fdout, h, headersize - sizeof(nfiles)) < 1) {
+ close(fdout);
+ err(1, "write header 2 to output file failed\n");
+ }
+
+ /* network to host byte order */
+ nfiles = ntohl(nfiles);
+ for (i = 0; i < nfiles; i++) {
+ h[i].filesize = ntohl(h[i].filesize);
+ h[i].fileoffset = ntohl(h[i].fileoffset);
+ }
+
+ /* write each file */
+ for (i = 0; i < nfiles; i++) {
+ if ((fdin = open(h[i].filename, O_RDONLY)) == -1) {
+ close(fdout);
+ err(1, "open input file failed\n");
+ }
+ if ((p = malloc(h[i].filesize)) == NULL) {
+ close(fdout);
+ close(fdin);
+ err(1, "malloc");
+ }
+ if (read(fdin, p, h[i].filesize) < 1) {
+ free(p);
+ close(fdout);
+ close(fdin);
+ err(1, "read input file failed\n");
+ }
+ if (write(fdout, p, h[i].filesize) < 1) {
+ free(p);
+ close(fdout);
+ close(fdin);
+ err(1, "write to output file failed\n");
+ }
+ free(p);
+ close(fdin);
+ }
+
+ close(fdout);
+
+#ifdef VERBOSE
+ printf("\n");
+#endif
+
+ /* game over */
+ printf("wrote %d files to %s (%d bytes).\n",
+ nfiles, FILENAME, totalsize);
+
+ return (0);
+}
diff --git a/sys/dev/microcode/bwi/extract/Makefile b/sys/dev/microcode/bwi/extract/Makefile
new file mode 100644
index 00000000000..1296c04a7b5
--- /dev/null
+++ b/sys/dev/microcode/bwi/extract/Makefile
@@ -0,0 +1,8 @@
+# $OpenBSD: Makefile,v 1.1 2007/10/04 17:46:09 mglocker Exp $
+
+NOMAN=
+
+PROG= extract
+SRCS= extract.c
+
+.include <bsd.prog.mk>
diff --git a/sys/dev/microcode/bwi/extract/extract.c b/sys/dev/microcode/bwi/extract/extract.c
new file mode 100644
index 00000000000..2aee0d8aea5
--- /dev/null
+++ b/sys/dev/microcode/bwi/extract/extract.c
@@ -0,0 +1,103 @@
+/* $OpenBSD: extract.c,v 1.1 2007/10/04 17:46:09 mglocker Exp $ */
+
+/*
+ * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+struct header {
+ char filename[64];
+ int filesize;
+ int fileoffset;
+};
+
+int
+main(int argc, char *argv[])
+{
+ int i, fdin, fdout, nfiles;
+ void *p;
+ struct header **h;
+
+ if (argc < 2) {
+ printf("%s <firmware file>\n", argv[0]);
+ exit(1);
+ }
+
+ /* open firmware file */
+ if ((fdin = open(argv[1], O_RDONLY)) == -1)
+ err(1, "open of input file failed");
+
+ /* read first header */
+ if (read(fdin, &nfiles, sizeof(nfiles)) < 1)
+ err(1, "first header parse failed");
+ nfiles = ntohl(nfiles);
+
+ /* allocate space for header struct */
+ if ((h = malloc(nfiles * sizeof(*h))) == NULL)
+ err(1, "malloc");
+ for (i = 0; i < nfiles; i++) {
+ if ((h[i] = malloc(sizeof(struct header))) == NULL)
+ err(1, "malloc");
+ }
+
+ /* read header */
+ for (i = 0; i < nfiles; i++) {
+ if (read(fdin, h[i]->filename, sizeof(h[i]->filename)) < 1)
+ err(1, "filename header read failed\n");
+ if (read(fdin, &h[i]->filesize, sizeof(h[i]->filesize)) < 1)
+ err(1, "filesize header read failed\n");
+ h[i]->filesize = htonl(h[i]->filesize);
+ if (read(fdin, &h[i]->fileoffset, sizeof(h[i]->fileoffset)) < 1)
+ err(1, "fileoffset header read failed\n");
+ h[i]->fileoffset = htonl(h[i]->fileoffset);
+ }
+
+ /* write each file */
+ for (i = 0; i < nfiles; i++) {
+ if ((fdout = open(h[i]->filename, O_CREAT|O_TRUNC|O_RDWR, 0644))
+ == -1)
+ err(1, "open of output file failed");
+ if ((p = malloc(h[i]->filesize)) == NULL)
+ err(1, "malloc");
+ if (lseek(fdin, h[i]->fileoffset, SEEK_SET) == -1)
+ err(1, "lseek");
+ if (read(fdin, p, h[i]->filesize) < 1)
+ err(1, "read from input file failed");
+ if (write(fdout, p, h[i]->filesize) < 1)
+ err(1, "write to output file failed");
+ free(p);
+ close(fdout);
+ printf("extracting %s (filesize %d, fileoffset %d)\n",
+ h[i]->filename, h[i]->filesize, h[i]->fileoffset);
+ }
+
+ /* free header space */
+ for (i = 0; i < nfiles; i++)
+ free(h[i]);
+ free(h);
+
+ /* game over */
+ close (fdin);
+
+ return (0);
+}