[PATCH] add-mkudfu-to-qi

xiangfu xiangfu at openmoko.org
Sun Aug 24 16:12:31 CEST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

move mkudfu code form uboot to QI.

- ---
 Makefile                |   14 ++-
 config.mk               |    3 +-
 tools/Makefile          |   39 ++++++
 tools/mkudfu.c          |  314
+++++++++++++++++++++++++++++++++++++++++++++++
 tools/usb_dfu_trailer.h |   31 +++++
 5 files changed, 396 insertions(+), 5 deletions(-)
 create mode 100644 tools/Makefile
 create mode 100644 tools/mkudfu.c
 create mode 100644 tools/usb_dfu_trailer.h

diff --git a/Makefile b/Makefile
index cdb075e..c1f9468 100644
- --- a/Makefile
+++ b/Makefile
@@ -25,20 +25,20 @@ BUILD_VERSION := ${BUILD_BRANCH}_${BUILD_HEAD}
 LDS	= src/qi.lds
 INCLUDE	= include
 IMAGE_DIR	= image
+TOOLS	= tools
 CFLAGS	= -Wall -Werror -I $(INCLUDE) -g -c -Os -fno-strict-aliasing
- -mlong-calls \
 	  -fno-common -ffixed-r8 -msoft-float -fno-builtin -ffreestanding \
 	  -march=armv4t -mno-thumb-interwork -Wstrict-prototypes \
 	  -DBUILD_HOST="${BUILD_HOST}" -DBUILD_VERSION="${BUILD_VERSION}" \
 	  -DBUILD_DATE="${BUILD_DATE}"
 LDFLAGS =
- -#START	= start.o lowlevel_init.o
+
 S_SRCS	= src/start.S src/lowlevel_init.S
 S_OBJS	= $(patsubst %.S,%.o, $(S_SRCS))
 C_SRCS	= $(wildcard src/*.c) $(wildcard src/gt*/*.c) \
 	  $(wildcard src/drivers/*.c)  $(wildcard src/fs/*.c)
 C_OBJS	= $(patsubst %.c,%.o, $(C_SRCS))

- -#SRCS	:= $(START: .o=.S) $(COBJS: .o=.c)
 SRCS	= ${S_SRCS} ${C_SRCS}
 OBJS	= ${S_OBJS} ${C_OBJS}
 LIBS	= -L${COMPILER_LIB_PATH} -lgcc
@@ -52,6 +52,8 @@ TARGET	= image/start_qi_all
 IMAGE = $(IMAGE_DIR)/qi
 UDFU_IMAGE = $(IMAGE_DIR)/qi.udfu

+MKUDFU = $(TOOLS)/mkudfu
+
 %.o: %.S
 	@$(CC) $(CFLAGS) -o $@ $<

@@ -60,9 +62,12 @@ UDFU_IMAGE = $(IMAGE_DIR)/qi.udfu

 all:${UDFU_IMAGE}

- -${OBJS}:${SRCS}
+${OBJS}:${SRCS} ${INCLUDE}/*.h
+
+${MKUDFU}:
+	 make -C $(TOOLS)

- -${UDFU_IMAGE}:${OBJS}
+${UDFU_IMAGE}:${OBJS} ${MKUDFU}
 	@$(LD) ${LDFLAGS} -T$(LDS) -g $(OBJS) -o ${TARGET} ${LIBS}
 	@$(OBJCOPY) -O binary -S ${TARGET} ${IMAGE}
 	@$(MKUDFU) -v ${UDFU_VID} -p ${UDFU_PID} -r ${UDFU_REV} \
@@ -71,3 +76,4 @@ ${UDFU_IMAGE}:${OBJS}

 clean:
 	@rm -f src/*.o  src/*~ include/*~ ${IMAGE}* ${TARGET} ${UDFU_IMAGE}
+	@make clean -C $(TOOLS)
\ No newline at end of file
diff --git a/config.mk b/config.mk
index b8cb833..1fee1a3 100644
- --- a/config.mk
+++ b/config.mk
@@ -14,8 +14,9 @@ LD	= $(CROSS_COMPILE)ld
 CC	= $(CROSS_COMPILE)gcc
 OBJCOPY = $(CROSS_COMPILE)objcopy
 OBJDUMP = $(CROSS_COMPILE)objdump
+HOSTCC	= gcc

 # we need the mkudfu tool from U-Boot build
- -MKUDFU = ../uboot/u-boot/tools/mkudfu
+#MKUDFU = ../uboot/u-boot/tools/mkudfu

 export CROSS_COMPILE AD LD CC OBJCOPY OBJDUMP MKUDFU
diff --git a/tools/Makefile b/tools/Makefile
new file mode 100644
index 0000000..9bb7f7b
- --- /dev/null
+++ b/tools/Makefile
@@ -0,0 +1,39 @@
+#(C) Copyright 2007 OpenMoko, Inc.
+# Author: xiangfu liu <xiangfu at openmoko.org>
+#
+# Configuation settings for the OPENMOKO Neo GTA02 Linux GSM phone
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+# MA 02111-1307 USA
+#
+include ../config.mk
+
+CC	= ${HOSTCC}
+CFLAGS	= -Wall
+C_SRCS	= $(wildcard *.c)
+C_OBJS	= $(patsubst %.c,%.o, $(C_SRCS))
+SRCS	= ${C_SRCS}
+OBJS	= ${C_OBJS}
+TARGET	= mkudfu
+
+%.o: %.c
+	CC $(CFLAGS) -o $@ $<
+
+all:${TARGET}
+
+${TARGET}:${SRCS}
+
+clean:
+	@rm -f *.o  *~  ${TARGET}
diff --git a/tools/mkudfu.c b/tools/mkudfu.c
new file mode 100644
index 0000000..57ac294
- --- /dev/null
+++ b/tools/mkudfu.c
@@ -0,0 +1,314 @@
+/*
+ * USB DFU file trailer tool
+ * (C) Copyright by OpenMoko, Inc.
+ * Author: Harald Welte <laforge at openmoko.org>
+ *
+ * based on mkimage.c, copyright information as follows:
+ *
+ * (C) Copyright 2000-2004
+ * DENX Software Engineering
+ * Wolfgang Denk, wd at denx.de
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifndef __WIN32__
+#include <netinet/in.h>		/* for host / network byte order conversions	*/
+#endif
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+#if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
+#include <inttypes.h>
+#endif
+
+#ifdef __WIN32__
+typedef unsigned int __u32;
+
+#define SWAP_LONG(x) \
+	((__u32)( \
+		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
+		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
+		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
+		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
+typedef		unsigned char	uint8_t;
+typedef		unsigned short	uint16_t;
+typedef		unsigned int	uint32_t;
+
+#define     ntohl(a)	SWAP_LONG(a)
+#define     htonl(a)	SWAP_LONG(a)
+#endif	/* __WIN32__ */
+
+#ifndef	O_BINARY		/* should be define'd on __WIN32__ */
+#define O_BINARY	0
+#endif
+
+#include "usb_dfu_trailer.h"
+
+extern int errno;
+
+#ifndef MAP_FAILED
+#define MAP_FAILED (-1)
+#endif
+
+static char *cmdname;
+
+static char *datafile;
+static char *imagefile;
+
+
+static void usage()
+{
+	fprintf (stderr, "%s - create / display u-boot DFU trailer\n", cmdname);
+	fprintf (stderr, "Usage: %s -l image\n"
+			 "          -l ==> list image header information\n"
+			 "       %s -v VID -p PID -r REV -d data_file image\n",
+		cmdname, cmdname);
+	fprintf (stderr, "          -v ==> set vendor ID to 'VID'\n"
+			 "          -p ==> set product ID system to 'PID'\n"
+			 "          -r ==> set hardware revision to 'REV'\n"
+			 "          -d ==> use 'data_file' as input file\n"
+		);
+	exit (EXIT_FAILURE);
+}
+
+static void print_trailer(struct uboot_dfu_trailer *trailer)
+{
+	printf("===> DFU Trailer information:\n");
+	printf("Trailer Vers.:	%d\n", trailer->version);
+	printf("Trailer Length:	%d\n", trailer->length);
+	printf("VendorID:	0x%04x\n", trailer->vendor);
+	printf("ProductID:	0x%04x\n", trailer->product);
+	printf("HW Revision:	0x%04x\n", trailer->revision);
+}
+
+static void copy_file (int ifd, const char *datafile, int pad)
+{
+	int dfd;
+	struct stat sbuf;
+	unsigned char *ptr;
+	int tail;
+	int zero = 0;
+	int offset = 0;
+	int size;
+
+	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
+		fprintf (stderr, "%s: Can't open %s: %s\n",
+			cmdname, datafile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	if (fstat(dfd, &sbuf) < 0) {
+		fprintf (stderr, "%s: Can't stat %s: %s\n",
+			cmdname, datafile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	ptr = (unsigned char *)mmap(0, sbuf.st_size,
+				    PROT_READ, MAP_SHARED, dfd, 0);
+	if (ptr == (unsigned char *)MAP_FAILED) {
+		fprintf (stderr, "%s: Can't read %s: %s\n",
+			cmdname, datafile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	size = sbuf.st_size - offset;
+	if (write(ifd, ptr + offset, size) != size) {
+		fprintf (stderr, "%s: Write error on %s: %s\n",
+			cmdname, imagefile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	if (pad && ((tail = size % 4) != 0)) {
+
+		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
+			fprintf (stderr, "%s: Write error on %s: %s\n",
+				cmdname, imagefile, strerror(errno));
+			exit (EXIT_FAILURE);
+		}
+	}
+
+	(void) munmap((void *)ptr, sbuf.st_size);
+	(void) close (dfd);
+}
+
+
+int main(int argc, char **argv)
+{
+	int ifd;
+	int lflag = 0;
+	struct stat sbuf;
+	u_int16_t opt_vendor, opt_product, opt_revision;
+	struct uboot_dfu_trailer _hdr, _mirror, *hdr = &_hdr;
+
+	opt_vendor = opt_product = opt_revision = 0;
+
+	cmdname = *argv;
+
+	while (--argc > 0 && **++argv == '-') {
+		while (*++*argv) {
+			switch (**argv) {
+			case 'l':
+				lflag = 1;
+				break;
+			case 'v':
+				if (--argc <= 0)
+					usage ();
+				opt_vendor = strtoul(*++argv, NULL, 16);
+				goto NXTARG;
+			case 'p':
+				if (--argc <= 0)
+					usage ();
+				opt_product = strtoul(*++argv, NULL, 16);
+				goto NXTARG;
+			case 'r':
+				if (--argc <= 0)
+					usage ();
+				opt_revision = strtoul(*++argv, NULL, 16);
+				goto NXTARG;
+			case 'd':
+				if (--argc <= 0)
+					usage ();
+				datafile = *++argv;
+				goto NXTARG;
+			case 'h':
+				usage();
+				break;
+			default:
+				usage();
+			}
+		}
+NXTARG:		;
+	}
+
+	if (argc != 1)
+		usage();
+
+	imagefile = *argv;
+
+	if (lflag)
+		ifd = open(imagefile, O_RDONLY|O_BINARY);
+	else
+		ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
+
+	if (ifd < 0) {
+		fprintf (stderr, "%s: Can't open %s: %s\n",
+			cmdname, imagefile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	if (lflag) {
+		unsigned char *ptr;
+		/* list header information of existing image */
+		if (fstat(ifd, &sbuf) < 0) {
+			fprintf (stderr, "%s: Can't stat %s: %s\n",
+				cmdname, imagefile, strerror(errno));
+			exit (EXIT_FAILURE);
+		}
+
+		if ((unsigned)sbuf.st_size < sizeof(struct uboot_dfu_trailer)) {
+			fprintf (stderr,
+				"%s: Bad size: \"%s\" is no valid image\n",
+				cmdname, imagefile);
+			exit (EXIT_FAILURE);
+		}
+
+		ptr = (unsigned char *)mmap(0, sbuf.st_size,
+					    PROT_READ, MAP_SHARED, ifd, 0);
+		if ((caddr_t)ptr == (caddr_t)-1) {
+			fprintf (stderr, "%s: Can't read %s: %s\n",
+				cmdname, imagefile, strerror(errno));
+			exit (EXIT_FAILURE);
+		}
+
+		dfu_trailer_mirror(hdr, ptr+sbuf.st_size);
+
+		if (hdr->magic != UBOOT_DFU_TRAILER_MAGIC) {
+			fprintf (stderr,
+				"%s: Bad Magic Number: \"%s\" is no valid image\n",
+				cmdname, imagefile);
+			exit (EXIT_FAILURE);
+		}
+
+		/* for multi-file images we need the data part, too */
+		print_trailer(hdr);
+
+		(void) munmap((void *)ptr, sbuf.st_size);
+		(void) close (ifd);
+
+		exit (EXIT_SUCCESS);
+	}
+
+	/* if we're not listing: */
+
+	copy_file (ifd, datafile, 0);
+
+	memset (hdr, 0, sizeof(struct uboot_dfu_trailer));
+
+	/* Build new header */
+	hdr->version	= UBOOT_DFU_TRAILER_V1;
+	hdr->magic	= UBOOT_DFU_TRAILER_MAGIC;
+	hdr->length	= sizeof(struct uboot_dfu_trailer);
+	hdr->vendor	= opt_vendor;
+	hdr->product	= opt_product;
+	hdr->revision	= opt_revision;
+
+	print_trailer(hdr);
+	dfu_trailer_mirror(&_mirror, (unsigned char *)hdr+sizeof(*hdr));
+
+	if (write(ifd, &_mirror, sizeof(struct uboot_dfu_trailer))
+					!= sizeof(struct uboot_dfu_trailer)) {
+		fprintf (stderr, "%s: Write error on %s: %s\n",
+			cmdname, imagefile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	/* We're a bit of paranoid */
+#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) &&
!defined(__FreeBSD__)
+	(void) fdatasync (ifd);
+#else
+	(void) fsync (ifd);
+#endif
+
+	if (fstat(ifd, &sbuf) < 0) {
+		fprintf (stderr, "%s: Can't stat %s: %s\n",
+			cmdname, imagefile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	/* We're a bit of paranoid */
+#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) &&
!defined(__FreeBSD__)
+	(void) fdatasync (ifd);
+#else
+	(void) fsync (ifd);
+#endif
+
+	if (close(ifd)) {
+		fprintf (stderr, "%s: Write error on %s: %s\n",
+			cmdname, imagefile, strerror(errno));
+		exit (EXIT_FAILURE);
+	}
+
+	exit (EXIT_SUCCESS);
+}
diff --git a/tools/usb_dfu_trailer.h b/tools/usb_dfu_trailer.h
new file mode 100644
index 0000000..3903b85
- --- /dev/null
+++ b/tools/usb_dfu_trailer.h
@@ -0,0 +1,31 @@
+#ifndef _USB_DFU_TRAILER_H
+#define _USB_DFU_TRAILER_H
+
+/* trailer handling for DFU files */
+
+#define UBOOT_DFU_TRAILER_V1	1
+#define UBOOT_DFU_TRAILER_MAGIC	0x19731978
+struct uboot_dfu_trailer {
+	u_int32_t	magic;
+	u_int16_t	version;
+	u_int16_t	length;
+	u_int16_t	vendor;
+	u_int16_t	product;
+	u_int32_t	revision;
+} __attribute__((packed));
+
+/* we mirror the trailer because we want it to be longer in later versions
+ * while keeping backwards compatibility */
+static inline void dfu_trailer_mirror(struct uboot_dfu_trailer *trailer,
+				      unsigned char *eof)
+{
+	int i;
+	int len = sizeof(struct uboot_dfu_trailer);
+	unsigned char *src = eof - len;
+	unsigned char *dst = (unsigned char *) trailer;
+
+	for (i = 0; i < len; i++)
+		dst[len-1-i] = src[i];
+}
+
+#endif /* _USB_DFU_TRAILER_H */
- --
1.5.5
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQEVAwUBSLFsThFuNemPXNFjAQJsOQgAl6PvXjWFPEGgCNiJbIKOh6I7XEOtI1QF
XGER2eoXHFHDFdjCvXkZh9P6MjJVlqTtlWXPZW0mtQZO7it/cu6HdJd1ZPKEMQ8g
hYGCWA58+KC6Ia77NMqnjbqIBs5uAz7ApP6/vFiq8gQIPZG+4FENiWha7Z13Re/P
2YfMhkA8eXJB7tVx8b5vuNjEyTFgnMxaJO+YeDKFmm013sksK3UuAR5HoaY8/ltl
mTOTNcEcyeDmQLTiHbOQ60pW74Cneadce9yrrh79gMcOVONoZpNX6UpUuLHRBy+a
d8ju52ZOQ6+KNOPI5G2+yuQWaOqAFDYwx2qLSd9xxhqvwSRbaFa2Yw==
=bXyG
-----END PGP SIGNATURE-----



More information about the openmoko-kernel mailing list