Skip to contentSkip to conversation

mkimage: fix argument parsing on BSD systems

PATCH In review Andreas Bießmann 6 participants 7 messages May 1, 2016 – May 3, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkimage: fix argument parsing on BSD systems

The getopt(3) optstring '-' is a GNU extension which is not available on BSD systems like OS X.

Remove this dependency by implementing argument parsing in another way. This will also change the lately introduced '-b' switch behaviour.

---

 Makefile        |  2 +-
 doc/mkimage.1   |  6 +++---
 tools/mkimage.c | 33 ++++++++++++---------------------
 3 files changed, 16 insertions(+), 25 deletions(-)
Makefile +1 -1
index f03fec1..293fad0 100644
@@ -898,7 +898,7 @@ ifdef CONFIG_SPL_LOAD_FIT
 MKIMAGEFLAGS_u-boot.img = -f auto -A $(ARCH) -T firmware -C none -O u-boot \
 	-a $(CONFIG_SYS_TEXT_BASE) -e $(CONFIG_SYS_UBOOT_START) \
 	-n "U-Boot $(UBOOTRELEASE) for $(BOARD) board" -E \
-	-b $(patsubst %,arch/$(ARCH)/dts/%.dtb,$(subst ",,$(CONFIG_OF_LIST)))
+	$(patsubst %,-b arch/$(ARCH)/dts/%.dtb,$(subst ",,$(CONFIG_OF_LIST)))
 else
 MKIMAGEFLAGS_u-boot.img = -A $(ARCH) -T firmware -C none -O u-boot \
 	-a $(CONFIG_SYS_TEXT_BASE) -e $(CONFIG_SYS_UBOOT_START) \
doc/mkimage.1 +3 -3
index e0f210a..4b3a255 100644
@@ -97,8 +97,8 @@ Set XIP (execute in place) flag.
 .B Create FIT image:
 
 .TP
-.BI "\-b
-Specifies that the following arguments are device tree binary files (.dtb).
+.BI "\-b [" "device tree file" "]
+Appends the device tree binary file (.dtb) to the FIT.
 
 .TP
 .BI "\-c [" "comment" "]"
@@ -211,7 +211,7 @@ automatic mode. No .its file is required.
 .B mkimage -f auto -A arm -O linux -T kernel -C none -a 43e00000 -e 0 \\\\
 .br
 .B -c """Kernel 4.4 image for production devices""" -d vmlinuz \\\\
-.B -b /path/to/rk3288-firefly.dtb /path/to/rk3288-jerry.dtb kernel.itb
+.B -b /path/to/rk3288-firefly.dtb -b /path/to/rk3288-jerry.dtb kernel.itb
 .fi
 
 .SH HOMEPAGE
tools/mkimage.c +12 -21
index 2931783..b407aed 100644
@@ -85,8 +85,8 @@ static void usage(const char *msg)
 		"          -x ==> set XIP (execute in place)\n",
 		params.cmdname);
 	fprintf(stderr,
-		"       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb_list>] fit-image\n"
-		"           <dtb_list> is used with -f auto, and is a space-separated list of .dtb files\n",
+		"       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
+		"           <dtb> file is used with -f auto, it may occour multiple times.\n",
 		params.cmdname);
 	fprintf(stderr,
 		"          -D => set all options for device tree compiler\n"
@@ -138,7 +138,7 @@ static void process_args(int argc, char **argv)
 
 	expecting = IH_TYPE_COUNT;	/* Unknown */
 	while ((opt = getopt(argc, argv,
-			     "-a:A:bcC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) {
+			     "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) {
 		switch (opt) {
 		case 'a':
 			params.addr = strtoull(optarg, &ptr, 16);
@@ -155,6 +155,12 @@ static void process_args(int argc, char **argv)
 			break;
 		case 'b':
 			expecting = IH_TYPE_FLATDT;
+			if (add_content(expecting, optarg)) {
+				fprintf(stderr,
+					"%s: Out of memory adding content '%s'",
+					params.cmdname, optarg);
+				exit(EXIT_FAILURE);
+			}
 			break;
 		case 'c':
 			params.comment = optarg;
@@ -243,29 +249,14 @@ static void process_args(int argc, char **argv)
 		case 'x':
 			params.xflag++;
 			break;
-		case 1:
-			if (expecting == type || optind == argc) {
-				params.imagefile = optarg;
-				expecting = IH_TYPE_INVALID;
-			} else if (expecting == IH_TYPE_INVALID) {
-				fprintf(stderr,
-					"%s: Unknown content type: use -b before device tree files",
-					params.cmdname);
-				exit(EXIT_FAILURE);
-			} else {
-				if (add_content(expecting, optarg)) {
-					fprintf(stderr,
-						"%s: Out of memory adding content '%s'",
-						params.cmdname, optarg);
-					exit(EXIT_FAILURE);
-				}
-			}
-			break;
 		default:
 			usage("Invalid option");
 		}
 	}
 
+	if (optind < argc && expecting == type)
+		params.imagefile = argv[optind];
+
 	/*
 	 * For auto-generated FIT images we need to know the image type to put
 	 * in the FIT, which is separate from the file's image type (which
Signature · 1 line
2.6.4 (Apple Git-63)
#1 lore raw

PATCH 1/1 mkimage: fix argument parsing on BSD systems

Hi Andreas,

Quoted 6 lines · Andreas Bie?mann

The getopt(3) optstring '-' is a GNU extension which is not available on BSD systems like OS X.

Remove this dependency by implementing argument parsing in another way. This will also change the lately introduced '-b' switch behaviour.

Quoted 7 lines

---

 Makefile        |  2 +-
 doc/mkimage.1   |  6 +++---
 tools/mkimage.c | 33 ++++++++++++---------------------
 3 files changed, 16 insertions(+), 25 deletions(-)

This looks good to me and the code is cleaner but I'm a bit worried about the -b flag change. It makes it impossible to do something like

mkimage ... -b *.dtb output.fit

Perhaps this is a small price to pay?

Regards,
Simon

#2 lore raw
In reply to Andreas Bießmann
Quoted 8 lines · Andreas Bie?mann

The getopt(3) optstring '-' is a GNU extension which is not available on BSD systems like OS X.

Remove this dependency by implementing argument parsing in another way. This will also change the lately introduced '-b' switch behaviour.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160502/e2c64542/attachment.sig>
#3 lore raw

Hi Tom,

2016-05-03 1:32 GMT+02:00 Tom Rini <[email protected]>:

Quoted 6 lines · Andreas Bie?mann
Quoted 2 lines

The getopt(3) optstring '-' is a GNU extension which is not available on

BSD

Quoted 2 lines

systems like OS X.

Quoted 10 lines
Quoted 1 line

Remove this dependency by implementing argument parsing in another way.

This

Quoted 4 lines

will also change the lately introduced '-b' switch behaviour.

Applied to u-boot/master, thanks!

This is breaking my mkimage for
CONFIG_SPL_LOAD_FIT=y
CONFIG_OF_LIST="zynq_zc702 zynq_zc706"
(for zynq you have to apply this [PATCH] ARM: zynq: Add support for SPL_LOAD_FIT)

expecting value doesn't match type in this condition.

    if (optind < argc && expecting == type)
        params.imagefile = argv[optind];

Thanks,
Michal

./tools/mkimage -f auto -A arm -T firmware -C none -O u-boot -a 0x4000000
-e 0x4000000 -n "U-Boot 2016.05-rc3-00060-g5eb59224edcb-dirty for zynq board" -E -b arch/arm/dts/zynq_zc702.dtb -b arch/arm/dts/zynq_zc706.dtb -d u-boot-nodtb.bin u-boot.img
Error: Missing output filename
Usage: ./tools/mkimage -l image
-l ==> list image header information
./tools/mkimage [-x] -A arch -O os -T type -C comp -a addr -e ep -n
name -d data_file[:data_file...] image

          -A ==> set architecture to 'arch'
          -O ==> set operating system to 'os'
          -T ==> set image type to 'type'
          -C ==> set compression type 'comp'
          -a ==> set load address to 'addr' (hex)
          -e ==> set entry point to 'ep' (hex)
          -n ==> set image name to 'name'
          -d ==> use image data from 'datafile'
          -x ==> set XIP (execute in place)
       ./tools/mkimage [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b

<dtb> [-b <dtb>]] fit-image
<dtb> file is used with -f auto, it may occour multiple times.
-D => set all options for device tree compiler
-f => input filename for FIT source
Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]
-k => set directory containing private keys
-K => write public keys to this .dtb file
-c => add comment in signature node
-F => re-sign existing FIT image
-r => mark keys used as 'required' in dtb
./tools/mkimage -V ==> print version information and exit
Use -T to see a list of available image types
make: *** [u-boot.img] Error 1

Thanks,
Michal
---
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Microblaze cpu - http://www.monstr.eu/fdt/
Maintainer of Linux kernel - Xilinx Zynq ARM architecture Microblaze U-BOOT custodian and responsible for u-boot arm zynq platform

#4 lore raw

PATCH 1/1 mkimage: fix generation of FIT image

Commit 7a439cadcf3192eb012a2432ca34670b676c74d2 broke generation of SPL loadable FIT images (CONFIG_SPL_LOAD_FIT).
Fix it by removing the unnecessary storage of expected image type. This was a left over of the previous implementation. It is not longer necessary since the mkimage -b switch always has one parameter.

---

 tools/mkimage.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/mkimage.c +3 -6
index b407aed..93d1c16 100644
@@ -133,10 +133,8 @@ static void process_args(int argc, char **argv)
 	char *ptr;
 	int type = IH_TYPE_INVALID;
 	char *datafile = NULL;
-	int expecting;
 	int opt;
 
-	expecting = IH_TYPE_COUNT;	/* Unknown */
 	while ((opt = getopt(argc, argv,
 			     "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) {
 		switch (opt) {
@@ -154,8 +152,7 @@ static void process_args(int argc, char **argv)
 				usage("Invalid architecture");
 			break;
 		case 'b':
-			expecting = IH_TYPE_FLATDT;
-			if (add_content(expecting, optarg)) {
+			if (add_content(IH_TYPE_FLATDT, optarg)) {
 				fprintf(stderr,
 					"%s: Out of memory adding content '%s'",
 					params.cmdname, optarg);
@@ -238,7 +235,6 @@ static void process_args(int argc, char **argv)
 				show_image_types();
 				usage("Invalid image type");
 			}
-			expecting = type;
 			break;
 		case 'v':
 			params.vflag++;
@@ -254,7 +250,8 @@ static void process_args(int argc, char **argv)
 		}
 	}
 
-	if (optind < argc && expecting == type)
+	/* The last parameter is expected to be the imagefile */
+	if (optind < argc)
 		params.imagefile = argv[optind];
 
 	/*
Signature · 1 line
2.1.4
#5 lore raw

PATCH 1/1 mkimage: fix generation of FIT image

Quoted 5 lines · Andreas Bie?mann

Commit 7a439cadcf3192eb012a2432ca34670b676c74d2 broke generation of SPL loadable FIT images (CONFIG_SPL_LOAD_FIT).
Fix it by removing the unnecessary storage of expected image type. This was a left over of the previous implementation. It is not longer necessary since the mkimage -b switch always has one parameter.

Thanks for the Patch. This fixes the build with CONFIG_SPL_LOAD_FIT enabled.

Thanks and regards,
Lokesh

Quoted 51 lines

---

 tools/mkimage.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
tools/mkimage.c +3 -6
index b407aed..93d1c16 100644
@@ -133,10 +133,8 @@ static void process_args(int argc, char **argv)
 	char *ptr;
 	int type = IH_TYPE_INVALID;
 	char *datafile = NULL;
-	int expecting;
 	int opt;
 
-	expecting = IH_TYPE_COUNT;	/* Unknown */
 	while ((opt = getopt(argc, argv,
 			     "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) {
 		switch (opt) {
@@ -154,8 +152,7 @@ static void process_args(int argc, char **argv)
 				usage("Invalid architecture");
 			break;
 		case 'b':
-			expecting = IH_TYPE_FLATDT;
-			if (add_content(expecting, optarg)) {
+			if (add_content(IH_TYPE_FLATDT, optarg)) {
 				fprintf(stderr,
 					"%s: Out of memory adding content '%s'",
 					params.cmdname, optarg);
@@ -238,7 +235,6 @@ static void process_args(int argc, char **argv)
 				show_image_types();
 				usage("Invalid image type");
 			}
-			expecting = type;
 			break;
 		case 'v':
 			params.vflag++;
@@ -254,7 +250,8 @@ static void process_args(int argc, char **argv)
 		}
 	}
 
-	if (optind < argc && expecting == type)
+	/* The last parameter is expected to be the imagefile */
+	if (optind < argc)
 		params.imagefile = argv[optind];
 
 	/*
#6 lore raw

PATCH 1/1 mkimage: fix generation of FIT image

In reply to Andreas Bießmann
Quoted 8 lines · Andreas Bie?mann

Commit 7a439cadcf3192eb012a2432ca34670b676c74d2 broke generation of SPL loadable FIT images (CONFIG_SPL_LOAD_FIT).
Fix it by removing the unnecessary storage of expected image type. This was a left over of the previous implementation. It is not longer necessary since the mkimage -b switch always has one parameter.

Thanks for the quick fix, applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160503/d0e6234e/attachment-0001.sig>