diff mbox

[U-Boot,6/7] tools, fit: add fit_info host command

Message ID 1390632269-8971-7-git-send-email-hs@denx.de
State Superseded
Delegated to: Tom Rini
Headers show

Commit Message

Heiko Schocher Jan. 25, 2014, 6:44 a.m. UTC
add fit_info command to the host tools. This command prints
the name, offset and the len from a property from a node in
a fit file. This info can be used to extract a properties
data with linux tools, for example "dd".

Signed-off-by: Heiko Schocher <hs@denx.de>
---
 Makefile           |   1 +
 tools/Makefile     |  20 +++++++++
 tools/fit_common.c |  81 +++++++++++++++++++++++++++++++++++++
 tools/fit_common.h |  22 ++++++++++
 tools/fit_image.c  |  62 ++--------------------------
 tools/fit_info.c   | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 244 insertions(+), 58 deletions(-)
 create mode 100644 tools/fit_common.c
 create mode 100644 tools/fit_common.h
 create mode 100644 tools/fit_info.c

Comments

Marek Vasut Feb. 8, 2014, 2:16 p.m. UTC | #1
On Saturday, January 25, 2014 at 07:44:28 AM, Heiko Schocher wrote:
> add fit_info command to the host tools. This command prints
> the name, offset and the len from a property from a node in
> a fit file. This info can be used to extract a properties
> data with linux tools, for example "dd".
> 
> Signed-off-by: Heiko Schocher <hs@denx.de>

[...]

> +int mmap_fdt(char *cmdname, const char *fname, void **blobp,
> +		struct stat *sbuf)
> +{
> +	void *ptr;
> +	int fd;
> +
> +	/* Load FIT blob into memory (we need to write hashes/signatures) */
> +	fd = open(fname, O_RDWR | O_BINARY);
> +
> +	if (fd < 0) {
> +		fprintf(stderr, "%s: Can't open %s: %s\n",
> +			cmdname, fname, strerror(errno));
> +		unlink(fname);

Are you sure about this unlink() call here ? The unlink() might delete the file, 
dunno if that was intended ;-)

> +		return -1;
> +	}
> +
> +	if (fstat(fd, sbuf) < 0) {
> +		fprintf(stderr, "%s: Can't stat %s: %s\n",
> +			cmdname, fname, strerror(errno));
> +		unlink(fname);
> +		return -1;
> +	}
> +
> +	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> +	if (ptr == MAP_FAILED) {
> +		fprintf(stderr, "%s: Can't read %s: %s\n",
> +			cmdname, fname, strerror(errno));
> +		unlink(fname);

Here this might be a problem ;-)

Also, make sure to set errno = 0 before mmap() and check the errno afterwards 
here too.
[...]
Best regards,
Marek Vasut
Heiko Schocher Feb. 10, 2014, 6:28 a.m. UTC | #2
Hello Marek,

Am 08.02.2014 15:16, schrieb Marek Vasut:
> On Saturday, January 25, 2014 at 07:44:28 AM, Heiko Schocher wrote:
>> add fit_info command to the host tools. This command prints
>> the name, offset and the len from a property from a node in
>> a fit file. This info can be used to extract a properties
>> data with linux tools, for example "dd".
>>
>> Signed-off-by: Heiko Schocher<hs@denx.de>
>
> [...]
>
>> +int mmap_fdt(char *cmdname, const char *fname, void **blobp,
>> +		struct stat *sbuf)
>> +{
>> +	void *ptr;
>> +	int fd;
>> +
>> +	/* Load FIT blob into memory (we need to write hashes/signatures) */
>> +	fd = open(fname, O_RDWR | O_BINARY);
>> +
>> +	if (fd<  0) {
>> +		fprintf(stderr, "%s: Can't open %s: %s\n",
>> +			cmdname, fname, strerror(errno));
>> +		unlink(fname);
>
> Are you sure about this unlink() call here ? The unlink() might delete the file,
> dunno if that was intended ;-)

Heh.. good catch! I only moved this function from tools/fit_image.c
to a common place, so other function can use it too ... but I just
noticed, I do not really need this ...

>> +		return -1;
>> +	}
>> +
>> +	if (fstat(fd, sbuf)<  0) {
>> +		fprintf(stderr, "%s: Can't stat %s: %s\n",
>> +			cmdname, fname, strerror(errno));
>> +		unlink(fname);
>> +		return -1;
>> +	}
>> +
>> +	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
>> +	if (ptr == MAP_FAILED) {
>> +		fprintf(stderr, "%s: Can't read %s: %s\n",
>> +			cmdname, fname, strerror(errno));
>> +		unlink(fname);
>
> Here this might be a problem ;-)
>
> Also, make sure to set errno = 0 before mmap() and check the errno afterwards
> here too.
> [...]

Seems this function needs a rework! I drop this from my patch, as I
do not need it.

bye,
Heiko
Marek Vasut Feb. 12, 2014, 10:46 a.m. UTC | #3
On Monday, February 10, 2014 at 07:28:51 AM, Heiko Schocher wrote:
> Hello Marek,
> 
> Am 08.02.2014 15:16, schrieb Marek Vasut:
> > On Saturday, January 25, 2014 at 07:44:28 AM, Heiko Schocher wrote:
> >> add fit_info command to the host tools. This command prints
> >> the name, offset and the len from a property from a node in
> >> a fit file. This info can be used to extract a properties
> >> data with linux tools, for example "dd".
> >> 
> >> Signed-off-by: Heiko Schocher<hs@denx.de>
> > 
> > [...]
> > 
> >> +int mmap_fdt(char *cmdname, const char *fname, void **blobp,
> >> +		struct stat *sbuf)
> >> +{
> >> +	void *ptr;
> >> +	int fd;
> >> +
> >> +	/* Load FIT blob into memory (we need to write hashes/signatures) */
> >> +	fd = open(fname, O_RDWR | O_BINARY);
> >> +
> >> +	if (fd<  0) {
> >> +		fprintf(stderr, "%s: Can't open %s: %s\n",
> >> +			cmdname, fname, strerror(errno));
> >> +		unlink(fname);
> > 
> > Are you sure about this unlink() call here ? The unlink() might delete
> > the file, dunno if that was intended ;-)
> 
> Heh.. good catch! I only moved this function from tools/fit_image.c
> to a common place, so other function can use it too ... but I just
> noticed, I do not really need this ...

[...]

> Seems this function needs a rework! I drop this from my patch, as I
> do not need it.

Thanks!

Best regards,
Marek Vasut
diff mbox

Patch

diff --git a/Makefile b/Makefile
index 47a03e3..a2e424d 100644
--- a/Makefile
+++ b/Makefile
@@ -794,6 +794,7 @@  clean:
 	@rm -f $(obj)tools/bmp_logo	   $(obj)tools/easylogo/easylogo  \
 	       $(obj)tools/env/fw_printenv				  \
 	       $(obj)tools/envcrc					  \
+	       $(obj)tools/fit_info					  \
 	       $(obj)tools/gdb/{gdbcont,gdbsend}			  \
 	       $(obj)tools/gen_eth_addr    $(obj)tools/img2srec		  \
 	       $(obj)tools/dump{env,}image		  \
diff --git a/tools/Makefile b/tools/Makefile
index 5e36e5e..d079bc9 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -53,6 +53,7 @@  BIN_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes$(SFX)
 BIN_FILES-y += dumpimage$(SFX)
 BIN_FILES-y += mkenvimage$(SFX)
 BIN_FILES-y += mkimage$(SFX)
+BIN_FILES-y += fit_info$(SFX)
 BIN_FILES-$(CONFIG_EXYNOS5250) += mk$(BOARD)spl$(SFX)
 BIN_FILES-$(CONFIG_EXYNOS5420) += mk$(BOARD)spl$(SFX)
 BIN_FILES-$(CONFIG_MX23) += mxsboot$(SFX)
@@ -77,6 +78,7 @@  EXT_OBJ_FILES-y += lib/sha256.o
 NOPED_OBJ_FILES-y += aisimage.o
 NOPED_OBJ_FILES-y += default_image.o
 NOPED_OBJ_FILES-y += dumpimage.o
+NOPED_OBJ_FILES-y += fit_common.o
 NOPED_OBJ_FILES-y += fit_image.o
 NOPED_OBJ_FILES-y += image-host.o
 NOPED_OBJ_FILES-y += imximage.o
@@ -84,6 +86,7 @@  NOPED_OBJ_FILES-y += kwbimage.o
 NOPED_OBJ_FILES-y += imagetool.o
 NOPED_OBJ_FILES-y += mkenvimage.o
 NOPED_OBJ_FILES-y += mkimage.o
+NOPED_OBJ_FILES-y += fit_info.o
 NOPED_OBJ_FILES-y += mxsimage.o
 NOPED_OBJ_FILES-y += omapimage.o
 NOPED_OBJ_FILES-y += os_support.o
@@ -210,6 +213,7 @@  $(obj)dumpimage$(SFX):	$(obj)aisimage.o \
 			$(FIT_SIG_OBJS) \
 			$(obj)crc32.o \
 			$(obj)default_image.o \
+			$(obj)fit_common.o \
 			$(obj)fit_image.o \
 			$(obj)image-fit.o \
 			$(obj)image.o \
@@ -240,6 +244,7 @@  $(obj)mkimage$(SFX):	$(obj)aisimage.o \
 			$(FIT_SIG_OBJS) \
 			$(obj)crc32.o \
 			$(obj)default_image.o \
+			$(obj)fit_common.o \
 			$(obj)fit_image.o \
 			$(obj)image-fit.o \
 			$(obj)image-host.o \
@@ -261,6 +266,21 @@  $(obj)mkimage$(SFX):	$(obj)aisimage.o \
 	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTLIBS)
 	$(HOSTSTRIP) $@
 
+$(obj)fit_info$(SFX):	$(obj)fit_info.o \
+			$(FIT_SIG_OBJS) \
+			$(obj)crc32.o \
+			$(obj)fit_common.o \
+			$(obj)image-fit.o \
+			$(obj)image-host.o \
+			$(obj)image.o \
+			$(obj)md5.o \
+			$(obj)sha1.o \
+			$(obj)sha256.o \
+			$(LIBFDT_OBJS) \
+			$(RSA_OBJS)
+	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTLIBS)
+	$(HOSTSTRIP) $@
+
 $(obj)mk$(BOARD)spl$(SFX):	$(obj)mkexynosspl.o
 	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^
 	$(HOSTSTRIP) $@
diff --git a/tools/fit_common.c b/tools/fit_common.c
new file mode 100644
index 0000000..26b6c8d
--- /dev/null
+++ b/tools/fit_common.c
@@ -0,0 +1,81 @@ 
+/*
+ * (C) Copyright 2014
+ * DENX Software Engineering
+ * Heiko Schocher <hs@denx.de>
+ *
+ * (C) Copyright 2008 Semihalf
+ *
+ * (C) Copyright 2000-2004
+ * DENX Software Engineering
+ * Wolfgang Denk, wd@denx.de
+ *
+ * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
+ *		FIT image specific code abstracted from mkimage.c
+ *		some functions added to address abstraction
+ *
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include "imagetool.h"
+#include "mkimage.h"
+#include "fit_common.h"
+#include <image.h>
+#include <u-boot/crc.h>
+
+int fit_verify_header(unsigned char *ptr, int image_size,
+			struct image_tool_params *params)
+{
+	return fdt_check_header(ptr);
+}
+
+int fit_check_image_types(uint8_t type)
+{
+	if (type == IH_TYPE_FLATDT)
+		return EXIT_SUCCESS;
+	else
+		return EXIT_FAILURE;
+}
+
+int mmap_fdt(char *cmdname, const char *fname, void **blobp,
+		struct stat *sbuf)
+{
+	void *ptr;
+	int fd;
+
+	/* Load FIT blob into memory (we need to write hashes/signatures) */
+	fd = open(fname, O_RDWR | O_BINARY);
+
+	if (fd < 0) {
+		fprintf(stderr, "%s: Can't open %s: %s\n",
+			cmdname, fname, strerror(errno));
+		unlink(fname);
+		return -1;
+	}
+
+	if (fstat(fd, sbuf) < 0) {
+		fprintf(stderr, "%s: Can't stat %s: %s\n",
+			cmdname, fname, strerror(errno));
+		unlink(fname);
+		return -1;
+	}
+
+	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (ptr == MAP_FAILED) {
+		fprintf(stderr, "%s: Can't read %s: %s\n",
+			cmdname, fname, strerror(errno));
+		unlink(fname);
+		return -1;
+	}
+
+	/* check if ptr has a valid blob */
+	if (fdt_check_header(ptr)) {
+		fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
+		unlink(fname);
+		return -1;
+	}
+
+	*blobp = ptr;
+	return fd;
+}
diff --git a/tools/fit_common.h b/tools/fit_common.h
new file mode 100644
index 0000000..e745f10
--- /dev/null
+++ b/tools/fit_common.h
@@ -0,0 +1,22 @@ 
+/*
+ * (C) Copyright 2014
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef _FIT_COMMON_H_
+#define _FIT_COMMON_H_
+
+#include "imagetool.h"
+#include "mkimage.h"
+#include <image.h>
+
+int fit_verify_header(unsigned char *ptr, int image_size,
+			struct image_tool_params *params);
+
+int fit_check_image_types(uint8_t type);
+
+int mmap_fdt(char *cmdname, const char *fname, void **blobp,
+		struct stat *sbuf);
+
+#endif /* _FIT_COMMON_H_ */
diff --git a/tools/fit_image.c b/tools/fit_image.c
index 1466164..d4430bc 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -15,68 +15,13 @@ 
  */
 
 #include "imagetool.h"
+#include "fit_common.h"
 #include "mkimage.h"
 #include <image.h>
 #include <u-boot/crc.h>
 
 static image_header_t header;
 
-static int fit_verify_header (unsigned char *ptr, int image_size,
-			struct image_tool_params *params)
-{
-	return fdt_check_header(ptr);
-}
-
-static int fit_check_image_types (uint8_t type)
-{
-	if (type == IH_TYPE_FLATDT)
-		return EXIT_SUCCESS;
-	else
-		return EXIT_FAILURE;
-}
-
-int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp,
-		struct stat *sbuf)
-{
-	void *ptr;
-	int fd;
-
-	/* Load FIT blob into memory (we need to write hashes/signatures) */
-	fd = open(fname, O_RDWR | O_BINARY);
-
-	if (fd < 0) {
-		fprintf(stderr, "%s: Can't open %s: %s\n",
-			params->cmdname, fname, strerror(errno));
-		unlink(fname);
-		return -1;
-	}
-
-	if (fstat(fd, sbuf) < 0) {
-		fprintf(stderr, "%s: Can't stat %s: %s\n",
-			params->cmdname, fname, strerror(errno));
-		unlink(fname);
-		return -1;
-	}
-
-	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
-	if (ptr == MAP_FAILED) {
-		fprintf(stderr, "%s: Can't read %s: %s\n",
-			params->cmdname, fname, strerror(errno));
-		unlink(fname);
-		return -1;
-	}
-
-	/* check if ptr has a valid blob */
-	if (fdt_check_header(ptr)) {
-		fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname);
-		unlink(fname);
-		return -1;
-	}
-
-	*blobp = ptr;
-	return fd;
-}
-
 /**
  * fit_handle_file - main FIT file processing function
  *
@@ -129,13 +74,14 @@  static int fit_handle_file(struct image_tool_params *params)
 	}
 
 	if (params->keydest) {
-		destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf);
+		destfd = mmap_fdt(params->cmdname, params->keydest,
+				  &dest_blob, &sbuf);
 		if (destfd < 0)
 			goto err_keydest;
 		destfd_size = sbuf.st_size;
 	}
 
-	tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf);
+	tfd = mmap_fdt(params->cmdname, tmpfile, &ptr, &sbuf);
 	if (tfd < 0)
 		goto err_mmap;
 
diff --git a/tools/fit_info.c b/tools/fit_info.c
new file mode 100644
index 0000000..4c5a1a1
--- /dev/null
+++ b/tools/fit_info.c
@@ -0,0 +1,116 @@ 
+/*
+ * (C) Copyright 2014
+ * DENX Software Engineering
+ * Heiko Schocher <hs@denx.de>
+ *
+ * fit_info: print the offset and the len of a property from
+ *	     node in a fit file.
+ *
+ * Based on:
+ * (C) Copyright 2008 Semihalf
+ *
+ * (C) Copyright 2000-2004
+ * DENX Software Engineering
+ * Wolfgang Denk, wd@denx.de
+ *
+ * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
+ *		FIT image specific code abstracted from mkimage.c
+ *		some functions added to address abstraction
+ *
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include "mkimage.h"
+#include "fit_common.h"
+#include <image.h>
+#include <u-boot/crc.h>
+
+void usage(char *cmdname)
+{
+	fprintf(stderr, "Usage: %s -f fit file [<node> <property>]\n"
+			 "          -f ==> set fit file which is used'\n",
+		cmdname);
+	exit(EXIT_FAILURE);
+}
+
+void *key_blob;
+
+void *get_blob(void)
+{
+	return key_blob;
+}
+
+int main(int argc, char **argv)
+{
+	int ffd = -1;
+	struct stat fsbuf;
+	void *fit_blob;
+	int len;
+	int  nodeoffset;	/* node offset from libfdt */
+	const void *nodep;	/* property node pointer */
+	char *fdtfile = NULL;
+	char cmdname[50];
+
+	strcpy(cmdname, *argv);
+
+	while (--argc > 0 && **++argv == '-') {
+		while (*++*argv) {
+			switch (**argv) {
+			case 'f':
+				if (--argc <= 0)
+					usage(cmdname);
+				fdtfile = *++argv;
+				goto NXTARG;
+
+			default:
+				usage(cmdname);
+			}
+		}
+NXTARG:;
+	}
+
+	if (argc != 2)
+		usage(cmdname);
+
+	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf);
+
+	if (ffd < 0) {
+		printf("Could not open %s\n", fdtfile);
+		exit(EXIT_FAILURE);
+	}
+
+	nodeoffset = fdt_path_offset(fit_blob, argv[0]);
+	if (nodeoffset < 0) {
+		printf("%s not found.", argv[0]);
+		exit(EXIT_FAILURE);
+	}
+	nodep = fdt_getprop(fit_blob, nodeoffset, argv[1], &len);
+	if (len == 0) {
+		printf("len == 0 %s\n", argv[1]);
+		exit(EXIT_FAILURE);
+	}
+
+	printf("NAME: %s\n", fit_get_name(fit_blob, nodeoffset, NULL));
+	printf("LEN: %d\n", len);
+	printf("OFF: %d\n", (int)(nodep - fit_blob));
+	(void) munmap((void *)fit_blob, fsbuf.st_size);
+
+	/* We're a bit of paranoid */
+#if defined(_POSIX_SYNCHRONIZED_IO) && \
+	!defined(__sun__) && \
+	!defined(__FreeBSD__) && \
+	!defined(__APPLE__)
+	(void) fdatasync(ffd);
+#else
+	(void) fsync(ffd);
+#endif
+
+	if (close(ffd)) {
+		fprintf(stderr, "%s: Write error on %s: %s\n",
+			cmdname, fdtfile, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+	exit(EXIT_SUCCESS);
+}