Module Name:    src
Committed By:   jmcneill
Date:           Sat Dec 29 15:11:56 UTC 2012

Modified Files:
        src/usr.bin/mkubootimage: crc32.c mkubootimage.1 mkubootimage.c uboot.h

Log Message:
add support for generating boot.scr scripts with -T script


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/mkubootimage/crc32.c
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/mkubootimage/mkubootimage.1 \
    src/usr.bin/mkubootimage/uboot.h
cvs rdiff -u -r1.16 -r1.17 src/usr.bin/mkubootimage/mkubootimage.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/mkubootimage/crc32.c
diff -u src/usr.bin/mkubootimage/crc32.c:1.2 src/usr.bin/mkubootimage/crc32.c:1.3
--- src/usr.bin/mkubootimage/crc32.c:1.2	Tue Jun 22 14:54:11 2010
+++ src/usr.bin/mkubootimage/crc32.c	Sat Dec 29 15:11:56 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: crc32.c,v 1.2 2010/06/22 14:54:11 dogcow Exp $ */
+/* $NetBSD: crc32.c,v 1.3 2012/12/29 15:11:56 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2002 Marcel Moolenaar
@@ -33,9 +33,10 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: crc32.c,v 1.2 2010/06/22 14:54:11 dogcow Exp $");
+__RCSID("$NetBSD: crc32.c,v 1.3 2012/12/29 15:11:56 jmcneill Exp $");
 
 #include <sys/types.h>
+#include <sys/uio.h>
 #include <stdint.h>
 
 uint32_t	crc32(const void *, size_t);
@@ -87,16 +88,31 @@ static uint32_t crc32_tab[] = {
 };
 
 uint32_t
-crc32(const void *buf, size_t size)
+crc32v(const struct iovec *iov, int cnt)
 {
 	const uint8_t *p;
 	uint32_t crc;
+	int i, len;
 
-	p = buf;
 	crc = ~0U;
 
-	while (size--)
-		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+	for (i = 0; i < cnt; i++) {
+		p = iov[i].iov_base;
+		len = iov[i].iov_len;
+		while (len--)
+			crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+	}
 
 	return crc ^ ~0U;
 }
+
+uint32_t
+crc32(const void *buf, size_t size)
+{
+	struct iovec iov[1];
+
+	iov[0].iov_base = __UNCONST(buf);
+	iov[0].iov_len = size;
+
+	return crc32v(iov, 1);
+}

Index: src/usr.bin/mkubootimage/mkubootimage.1
diff -u src/usr.bin/mkubootimage/mkubootimage.1:1.4 src/usr.bin/mkubootimage/mkubootimage.1:1.5
--- src/usr.bin/mkubootimage/mkubootimage.1:1.4	Sat Dec  1 08:16:25 2012
+++ src/usr.bin/mkubootimage/mkubootimage.1	Sat Dec 29 15:11:56 2012
@@ -1,4 +1,4 @@
-.\"	$NetBSD: mkubootimage.1,v 1.4 2012/12/01 08:16:25 wiz Exp $
+.\"	$NetBSD: mkubootimage.1,v 1.5 2012/12/29 15:11:56 jmcneill Exp $
 .\"
 .\" Copyright (c) 2012 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd November 28, 2012
+.Dd December 29, 2012
 .Dt MKUBOOTIMAGE 1
 .Os
 .Sh NAME
@@ -108,7 +108,7 @@ This is required.
 Defines the operating system type.
 The default OS name is
 .Qq netbsd .
-.It Fl T No ( fs Ns | Ns kernel Ns | Ns ramdisk Ns | Ns standalone )
+.It Fl T No ( fs Ns | Ns kernel Ns | Ns ramdisk Ns | Ns standalone Ns | Ns script )
 Defines the image type.
 This is required.
 .El
Index: src/usr.bin/mkubootimage/uboot.h
diff -u src/usr.bin/mkubootimage/uboot.h:1.4 src/usr.bin/mkubootimage/uboot.h:1.5
--- src/usr.bin/mkubootimage/uboot.h:1.4	Wed Aug  3 17:46:40 2011
+++ src/usr.bin/mkubootimage/uboot.h	Sat Dec 29 15:11:56 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: uboot.h,v 1.4 2011/08/03 17:46:40 matt Exp $ */
+/* $NetBSD: uboot.h,v 1.5 2012/12/29 15:11:56 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. McNeill <[email protected]>
@@ -49,6 +49,7 @@ enum uboot_image_type {
 	IH_TYPE_STANDALONE = 1,
 	IH_TYPE_KERNEL = 2,
 	IH_TYPE_RAMDISK = 3,
+	IH_TYPE_SCRIPT = 6,
 	IH_TYPE_FILESYSTEM = 7,
 };
 

Index: src/usr.bin/mkubootimage/mkubootimage.c
diff -u src/usr.bin/mkubootimage/mkubootimage.c:1.16 src/usr.bin/mkubootimage/mkubootimage.c:1.17
--- src/usr.bin/mkubootimage/mkubootimage.c:1.16	Fri Feb 17 08:28:36 2012
+++ src/usr.bin/mkubootimage/mkubootimage.c	Sat Dec 29 15:11:56 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: mkubootimage.c,v 1.16 2012/02/17 08:28:36 matt Exp $ */
+/* $NetBSD: mkubootimage.c,v 1.17 2012/12/29 15:11:56 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2010 Jared D. McNeill <[email protected]>
@@ -30,11 +30,12 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: mkubootimage.c,v 1.16 2012/02/17 08:28:36 matt Exp $");
+__RCSID("$NetBSD: mkubootimage.c,v 1.17 2012/12/29 15:11:56 jmcneill Exp $");
 
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/endian.h>
+#include <sys/uio.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -53,6 +54,7 @@ __RCSID("$NetBSD: mkubootimage.c,v 1.16 
 #endif
 
 extern uint32_t crc32(const void *, size_t);
+extern uint32_t crc32v(const struct iovec *, int);
 
 static enum uboot_image_os image_os = IH_OS_NETBSD;
 static enum uboot_image_arch image_arch = IH_ARCH_UNKNOWN;
@@ -143,6 +145,7 @@ static const struct uboot_type {
 	{ IH_TYPE_KERNEL,	"kernel" },
 	{ IH_TYPE_RAMDISK,	"ramdisk" },
 	{ IH_TYPE_FILESYSTEM,	"fs" },
+	{ IH_TYPE_SCRIPT,	"script" },
 };
 
 static enum uboot_image_type
@@ -214,7 +217,7 @@ usage(void)
 	fprintf(stderr, "usage: mkubootimage -A <arm|mips|mips64|powerpc>");
 	fprintf(stderr, " -C <none|bz2|gz|lzma|lzo>");
 	fprintf(stderr, " -O <openbsd|netbsd|freebsd|linux>");
-	fprintf(stderr, " -T <standalone|kernel|ramdisk|fs>");
+	fprintf(stderr, " -T <standalone|kernel|ramdisk|fs|script>");
 	fprintf(stderr, " -a <addr> [-e <ep>] [-m <magic>] -n <name>");
 	fprintf(stderr, " <srcfile> <dstfile>\n");
 
@@ -249,7 +252,7 @@ generate_header(struct uboot_image_heade
 {
 	uint8_t *p;
 	struct stat st;
-	uint32_t crc;
+	uint32_t crc, dsize, size_buf[2];
 	int error;
 
 	error = fstat(kernel_fd, &st);
@@ -268,13 +271,28 @@ generate_header(struct uboot_image_heade
 		perror("mmap kernel");
 		return EINVAL;
 	}
-	crc = crc32(p, st.st_size);
+	if (image_type == IH_TYPE_SCRIPT) {
+		struct iovec iov[3];
+		dsize = st.st_size + (sizeof(uint32_t) * 2);
+		size_buf[0] = htonl(st.st_size);
+		size_buf[1] = htonl(0);
+		iov[0].iov_base = &size_buf[0];
+		iov[0].iov_len = sizeof(size_buf[0]);
+		iov[1].iov_base = &size_buf[1];
+		iov[1].iov_len = sizeof(size_buf[1]);
+		iov[2].iov_base = p;
+		iov[2].iov_len = st.st_size;
+		crc = crc32v(iov, 3);
+	} else {
+		dsize = st.st_size;
+		crc = crc32(p, st.st_size);
+	}
 	munmap(p, st.st_size);
 
 	memset(hdr, 0, sizeof(*hdr));
 	hdr->ih_magic = htonl(image_magic);
 	hdr->ih_time = htonl(st.st_mtime);
-	hdr->ih_size = htonl(st.st_size);
+	hdr->ih_size = htonl(dsize);
 	hdr->ih_load = htonl(image_loadaddr);
 	hdr->ih_ep = htonl(image_entrypoint);
 	hdr->ih_dcrc = htonl(crc);
@@ -296,6 +314,15 @@ write_image(struct uboot_image_header *h
 {
 	uint8_t buf[4096];
 	ssize_t rlen, wlen;
+	struct stat st;
+	uint32_t size_buf[2];
+	int error;
+
+	error = fstat(kernel_fd, &st);
+	if (error == -1) {
+		perror("stat");
+		return errno;
+	}
 
 	wlen = write(image_fd, hdr, sizeof(*hdr));
 	if (wlen != sizeof(*hdr)) {
@@ -303,6 +330,16 @@ write_image(struct uboot_image_header *h
 		return errno;
 	}
 
+	if (image_type == IH_TYPE_SCRIPT) {
+		size_buf[0] = htonl(st.st_size);
+		size_buf[1] = htonl(0);
+		wlen = write(image_fd, &size_buf, sizeof(size_buf));
+		if (wlen != sizeof(size_buf)) {
+			perror("short write");
+			return errno;
+		}
+	}
+
 	while ((rlen = read(kernel_fd, buf, sizeof(buf))) > 0) {
 		wlen = write(image_fd, buf, rlen);
 		if (wlen != rlen) {
@@ -388,7 +425,7 @@ main(int argc, char *argv[])
 
 	if (image_arch == IH_ARCH_UNKNOWN ||
 	    image_type == IH_TYPE_UNKNOWN ||
-	    image_loadaddr == 0 ||
+	    (image_type != IH_TYPE_SCRIPT && image_loadaddr == 0) ||
 	    image_name == NULL)
 		usage();
 

Reply via email to