diff mbox

[U-Boot,19/26] mkimage: Support adding device tree files to a FIT

Message ID 1453999186-18747-20-git-send-email-sjg@chromium.org
State Superseded
Delegated to: Tom Rini
Headers show

Commit Message

Simon Glass Jan. 28, 2016, 4:39 p.m. UTC
To make the auto-FIT feature useful we need to be able to provide a list of
device tree files on the command line for mkimage to add into the FIT. Add
support for this feature.

So far there is no support for hashing or verified boot using this method.
For those cases, a .its file must still be provided.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 doc/mkimage.1     | 17 ++++++++++
 tools/fit_image.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tools/imagetool.h |  9 ++++++
 tools/mkimage.c   | 38 +++++++++++++++++++++-
 4 files changed, 151 insertions(+), 9 deletions(-)

Comments

Tom Rini Feb. 11, 2016, 4:36 p.m. UTC | #1
On Thu, Jan 28, 2016 at 09:39:39AM -0700, Simon Glass wrote:

> To make the auto-FIT feature useful we need to be able to provide a list of
> device tree files on the command line for mkimage to add into the FIT. Add
> support for this feature.
[snip]
> +.BI "\-b
> +Specifies that the following arguments are device tree binary files (.dtb).
> +
> +.TP
>  .BI "\-c [" "comment" "]"
>  Specifies a comment to be added when signing. This is typically a useful
>  message which describes how the image was signed or some other useful
> @@ -125,6 +129,10 @@ This can be used to sign images with additional keys after initial image
>  creation.
>  
>  .TP
> +.BI "\-i
> +Specifies that the following arguments are image files.
> +
> +.TP
>  .BI "\-k [" "key_directory" "]"
>  Specifies the directory containing keys to use for signing. This directory
>  should contain a private key file <name>.key for use with signing and a
> @@ -191,6 +199,15 @@ is required.
>  .br
>  .B -c """Kernel 4.4 image for production devices""" -d vmlinuz kernel.itb
>  .fi
> +.P
> +Create a FIT image containing a kernel and some device tree files, using
> +automatic mode. No .its file is required.
> +.nf
> +.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 rk3288-firefly.dts rk3288-jerry.dts -i kernel.itb
> +.fi

OK, I'm confused in a few ways here.  First, we don't need quotes around
the space separated list of dts files?  Second, we need dts not dtb?
Third, I think this would be a better example if (as I suspect we need
really) it was a full path to the output rather than assuming that we've
copied the files to $PWD.  Fourth, -i is inputs or outputs?  The help
reads like inputs and is where I assume we would say vmlinuz here and
not use -d and then end with our output of kernel.itb.  Thanks!
Simon Glass Feb. 12, 2016, 3:54 p.m. UTC | #2
Hi Tom,

On 11 February 2016 at 09:36, Tom Rini <trini@konsulko.com> wrote:
> On Thu, Jan 28, 2016 at 09:39:39AM -0700, Simon Glass wrote:
>
>> To make the auto-FIT feature useful we need to be able to provide a list of
>> device tree files on the command line for mkimage to add into the FIT. Add
>> support for this feature.
> [snip]
>> +.BI "\-b
>> +Specifies that the following arguments are device tree binary files (.dtb).
>> +
>> +.TP
>>  .BI "\-c [" "comment" "]"
>>  Specifies a comment to be added when signing. This is typically a useful
>>  message which describes how the image was signed or some other useful
>> @@ -125,6 +129,10 @@ This can be used to sign images with additional keys after initial image
>>  creation.
>>
>>  .TP
>> +.BI "\-i
>> +Specifies that the following arguments are image files.
>> +
>> +.TP
>>  .BI "\-k [" "key_directory" "]"
>>  Specifies the directory containing keys to use for signing. This directory
>>  should contain a private key file <name>.key for use with signing and a
>> @@ -191,6 +199,15 @@ is required.
>>  .br
>>  .B -c """Kernel 4.4 image for production devices""" -d vmlinuz kernel.itb
>>  .fi
>> +.P
>> +Create a FIT image containing a kernel and some device tree files, using
>> +automatic mode. No .its file is required.
>> +.nf
>> +.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 rk3288-firefly.dts rk3288-jerry.dts -i kernel.itb
>> +.fi
>
> OK, I'm confused in a few ways here.  First, we don't need quotes around
> the space separated list of dts files?

No, the -b option introduces the list, which can be as long as needed.
I want to make it possible to specify a list easily.

> Second, we need dts not dtb?

Eek, no, it should be dtb.

> Third, I think this would be a better example if (as I suspect we need
> really) it was a full path to the output rather than assuming that we've
> copied the files to $PWD.

OK

> Fourth, -i is inputs or outputs?  The help
> reads like inputs and is where I assume we would say vmlinuz here and
> not use -d and then end with our output of kernel.itb.  Thanks!

-i introduces the image file, whic his an output. The -d provides the
datafile, which is the file that is is put inside the FIT.

Maybe we can change things to make this a bit clearer. We could
perhaps have an option to end the list of dtb files, instead of the
option to signal the output file is next? Or maybe we can avoid -i
altogether since the last arg should be the output file.

Regards,
Simon
Tom Rini Feb. 12, 2016, 4:03 p.m. UTC | #3
On Fri, Feb 12, 2016 at 08:54:09AM -0700, Simon Glass wrote:
> Hi Tom,
> 
> On 11 February 2016 at 09:36, Tom Rini <trini@konsulko.com> wrote:
> > On Thu, Jan 28, 2016 at 09:39:39AM -0700, Simon Glass wrote:
> >
> >> To make the auto-FIT feature useful we need to be able to provide a list of
> >> device tree files on the command line for mkimage to add into the FIT. Add
> >> support for this feature.
> > [snip]
> >> +.BI "\-b
> >> +Specifies that the following arguments are device tree binary files (.dtb).
> >> +
> >> +.TP
> >>  .BI "\-c [" "comment" "]"
> >>  Specifies a comment to be added when signing. This is typically a useful
> >>  message which describes how the image was signed or some other useful
> >> @@ -125,6 +129,10 @@ This can be used to sign images with additional keys after initial image
> >>  creation.
> >>
> >>  .TP
> >> +.BI "\-i
> >> +Specifies that the following arguments are image files.
> >> +
> >> +.TP
> >>  .BI "\-k [" "key_directory" "]"
> >>  Specifies the directory containing keys to use for signing. This directory
> >>  should contain a private key file <name>.key for use with signing and a
> >> @@ -191,6 +199,15 @@ is required.
> >>  .br
> >>  .B -c """Kernel 4.4 image for production devices""" -d vmlinuz kernel.itb
> >>  .fi
> >> +.P
> >> +Create a FIT image containing a kernel and some device tree files, using
> >> +automatic mode. No .its file is required.
> >> +.nf
> >> +.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 rk3288-firefly.dts rk3288-jerry.dts -i kernel.itb
> >> +.fi
> >
> > OK, I'm confused in a few ways here.  First, we don't need quotes around
> > the space separated list of dts files?
> 
> No, the -b option introduces the list, which can be as long as needed.
> I want to make it possible to specify a list easily.

OK.  I don't see how that works in the code, but if that's the intent
and it works, OK :)

> > Second, we need dts not dtb?
> 
> Eek, no, it should be dtb.
> 
> > Third, I think this would be a better example if (as I suspect we need
> > really) it was a full path to the output rather than assuming that we've
> > copied the files to $PWD.
> 
> OK
> 
> > Fourth, -i is inputs or outputs?  The help
> > reads like inputs and is where I assume we would say vmlinuz here and
> > not use -d and then end with our output of kernel.itb.  Thanks!
> 
> -i introduces the image file, whic his an output. The -d provides the
> datafile, which is the file that is is put inside the FIT.
> 
> Maybe we can change things to make this a bit clearer. We could
> perhaps have an option to end the list of dtb files, instead of the
> option to signal the output file is next? Or maybe we can avoid -i
> altogether since the last arg should be the output file.

Yes, I don't think we need -i at all.  The last argument is the output,
always, and -f auto instead of -f some-file.its is all we need to know
it's a FIT output.  And, ah, so I guess this is how -b is implemented
then.  Yes, this seems rather awkward.  Maybe we instead implement a
sometimes optional (ie previous use cases) -o flag to say output file is
... and also make sure it's clear in the man page at least that -b
consumes following arguments as the DT list until another flag is
passed?
diff mbox

Patch

diff --git a/doc/mkimage.1 b/doc/mkimage.1
index 146c114..b1b45d7 100644
--- a/doc/mkimage.1
+++ b/doc/mkimage.1
@@ -97,6 +97,10 @@  Set XIP (execute in place) flag.
 .B Create FIT image:
 
 .TP
+.BI "\-b
+Specifies that the following arguments are device tree binary files (.dtb).
+
+.TP
 .BI "\-c [" "comment" "]"
 Specifies a comment to be added when signing. This is typically a useful
 message which describes how the image was signed or some other useful
@@ -125,6 +129,10 @@  This can be used to sign images with additional keys after initial image
 creation.
 
 .TP
+.BI "\-i
+Specifies that the following arguments are image files.
+
+.TP
 .BI "\-k [" "key_directory" "]"
 Specifies the directory containing keys to use for signing. This directory
 should contain a private key file <name>.key for use with signing and a
@@ -191,6 +199,15 @@  is required.
 .br
 .B -c """Kernel 4.4 image for production devices""" -d vmlinuz kernel.itb
 .fi
+.P
+Create a FIT image containing a kernel and some device tree files, using
+automatic mode. No .its file is required.
+.nf
+.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 rk3288-firefly.dts rk3288-jerry.dts -i kernel.itb
+.fi
 
 .SH HOMEPAGE
 http://www.denx.de/wiki/U-Boot/WebHome
diff --git a/tools/fit_image.c b/tools/fit_image.c
index 343de60..0d8007b 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -77,6 +77,7 @@  err_keydest:
  */
 static int fit_calc_size(struct image_tool_params *params)
 {
+	struct content_info *cont;
 	int size, total_size;
 
 	size = imagetool_get_filesize(params, params->datafile);
@@ -84,8 +85,14 @@  static int fit_calc_size(struct image_tool_params *params)
 		return -1;
 
 	total_size = size;
+	for (cont = params->content_head; cont; cont = cont->next) {
+		size = imagetool_get_filesize(params, cont->fname);
+		if (size < 0)
+			return -1;
 
-	/* TODO(sjg@chromium.org): Add in the size of any other files */
+		/* Add space for properties */
+		total_size += size + 300;
+	}
 
 	/* Add plenty of space for headers, properties, nodes, etc. */
 	total_size += 4096;
@@ -141,15 +148,40 @@  static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
 	return fdt_property_string(fdt, name, str);
 }
 
+static void get_basename(char *str, int size, const char *fname)
+{
+	const char *p, *start, *end;
+	int len;
+
+	/*
+	 * Use the base name as the 'name' field. So for example:
+	 *
+	 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
+	 * becomes "sun7i-a20-bananapro"
+	 */
+	p = strrchr(fname, '/');
+	start = p ? p + 1 : fname;
+	p = strrchr(fname, '.');
+	end = p ? p : fname + strlen(fname);
+	len = end - start;
+	if (len >= size)
+		len = size - 1;
+	memcpy(str, start, len);
+	str[len] = '\0';
+}
+
 /**
  * fit_write_images() - Write out a list of images to the FIT
  *
- * Include the main image (params->datafile).
+ * We always include the main image (params->datafile). If there are device
+ * tree files, we include an fdt@ node for each of those too.
  */
 static int fit_write_images(struct image_tool_params *params, char *fdt)
 {
+	struct content_info *cont;
 	const char *typename;
 	char str[100];
+	int upto;
 	int ret;
 
 	fdt_begin_node(fdt, "images");
@@ -176,6 +208,27 @@  static int fit_write_images(struct image_tool_params *params, char *fdt)
 		return ret;
 	fdt_end_node(fdt);
 
+	/* Now the device tree files if available */
+	upto = 0;
+	for (cont = params->content_head; cont; cont = cont->next) {
+		if (cont->type != IH_TYPE_FLATDT)
+			continue;
+		snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
+		fdt_begin_node(fdt, str);
+
+		get_basename(str, sizeof(str), cont->fname);
+		fdt_property_string(fdt, "description", str);
+		ret = fdt_property_file(params, fdt, "data", cont->fname);
+		if (ret)
+			return ret;
+		fdt_property_string(fdt, "type", typename);
+		fdt_property_string(fdt, "arch",
+				    genimg_get_arch_short_name(params->arch));
+		fdt_property_string(fdt, "compression",
+				    genimg_get_comp_short_name(IH_COMP_NONE));
+		fdt_end_node(fdt);
+	}
+
 	fdt_end_node(fdt);
 
 	return 0;
@@ -184,21 +237,48 @@  static int fit_write_images(struct image_tool_params *params, char *fdt)
 /**
  * fit_write_configs() - Write out a list of configurations to the FIT
  *
- * Create a configuration with the main image in it.
+ * If there are device tree files, we include a configuration for each, which
+ * selects the main image (params->datafile) and its corresponding device
+ * tree file.
+ *
+ * Otherwise we just create a configuration with the main image in it.
  */
 static void fit_write_configs(struct image_tool_params *params, char *fdt)
 {
+	struct content_info *cont;
 	const char *typename;
 	char str[100];
+	int upto;
 
 	fdt_begin_node(fdt, "configurations");
 	fdt_property_string(fdt, "default", "conf@1");
 
-	fdt_begin_node(fdt, "conf@1");
-	typename = genimg_get_type_short_name(params->fit_image_type);
-	snprintf(str, sizeof(str), "%s@1", typename);
-	fdt_property_string(fdt, typename, str);
-	fdt_end_node(fdt);
+	upto = 0;
+	for (cont = params->content_head; cont; cont = cont->next) {
+		if (cont->type != IH_TYPE_FLATDT)
+			continue;
+		typename = genimg_get_type_short_name(cont->type);
+		snprintf(str, sizeof(str), "conf@%d", ++upto);
+		fdt_begin_node(fdt, str);
+
+		get_basename(str, sizeof(str), cont->fname);
+		fdt_property_string(fdt, "description", str);
+
+		typename = genimg_get_type_short_name(params->fit_image_type);
+		snprintf(str, sizeof(str), "%s@1", typename);
+		fdt_property_string(fdt, typename, str);
+
+		snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
+		fdt_property_string(fdt, FIT_FDT_PROP, str);
+		fdt_end_node(fdt);
+	}
+	if (!upto) {
+		fdt_begin_node(fdt, "conf@1");
+		typename = genimg_get_type_short_name(params->fit_image_type);
+		snprintf(str, sizeof(str), "%s@1", typename);
+		fdt_property_string(fdt, typename, str);
+		fdt_end_node(fdt);
+	}
 
 	fdt_end_node(fdt);
 }
diff --git a/tools/imagetool.h b/tools/imagetool.h
index 4f426e4..3d30fbe 100644
--- a/tools/imagetool.h
+++ b/tools/imagetool.h
@@ -27,6 +27,13 @@ 
 
 #define IH_ARCH_DEFAULT		IH_ARCH_INVALID
 
+/* Information about a file that needs to be placed into the FIT */
+struct content_info {
+	struct content_info *next;
+	int type;		/* File type (IH_TYPE_...) */
+	const char *fname;
+};
+
 /*
  * This structure defines all such variables those are initialized by
  * mkimage and dumpimage main core and need to be referred by image
@@ -63,6 +70,8 @@  struct image_tool_params {
 	int orig_file_size;	/* Original size for file before padding */
 	bool auto_its;		/* Automatically create the .its file */
 	int fit_image_type;	/* Image type to put into the FIT */
+	struct content_info *content_head;	/* List of files to include */
+	struct content_info *content_tail;
 };
 
 /*
diff --git a/tools/mkimage.c b/tools/mkimage.c
index 5d4ed61..5e2e7e1 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -109,6 +109,24 @@  static void usage(const char *msg)
 	exit(EXIT_FAILURE);
 }
 
+static int add_content(int type, const char *fname)
+{
+	struct content_info *cont;
+
+	cont = calloc(1, sizeof(*cont));
+	if (!cont)
+		return -1;
+	cont->type = type;
+	cont->fname = fname;
+	if (params.content_tail)
+		params.content_tail->next = cont;
+	else
+		params.content_head = cont;
+	params.content_tail = cont;
+
+	return 0;
+}
+
 static void process_args(int argc, char **argv)
 {
 	char *ptr;
@@ -119,7 +137,7 @@  static void process_args(int argc, char **argv)
 
 	expecting = IH_TYPE_COUNT;	/* Unknown */
 	while ((opt = getopt(argc, argv,
-			     "-a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
+			     "-a:A:bcC:d:D:e:f:Fik:K:ln:O:rR:sT:vVx")) != -1) {
 		switch (opt) {
 		case 'a':
 			params.addr = strtoul(optarg, &ptr, 16);
@@ -134,6 +152,9 @@  static void process_args(int argc, char **argv)
 			if (params.arch < 0)
 				usage("Invalid architecture");
 			break;
+		case 'b':
+			expecting = IH_TYPE_FLATDT;
+			break;
 		case 'c':
 			params.comment = optarg;
 			break;
@@ -171,6 +192,9 @@  static void process_args(int argc, char **argv)
 			params.type = IH_TYPE_FLATDT;
 			params.fflag = 1;
 			break;
+		case 'i':
+			expecting = type;
+			break;
 		case 'k':
 			params.keydir = optarg;
 			break;
@@ -222,6 +246,18 @@  static void process_args(int argc, char **argv)
 			if (expecting == type || expecting == IH_TYPE_COUNT) {
 				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: