Re: [RFC PATCH v3 9/9] tools: spkgimage: add Renesas SPKG format

2023-02-22 Thread Simon Glass
Hi Ralph,

On Wed, 22 Feb 2023 at 14:17, Ralph Siemsen  wrote:
>
> Hi Simon,
>
> Thanks for your review!
>
> On Wed, Feb 22, 2023 at 2:17 PM Simon Glass  wrote:
> >
> > Can you please add some details to doc/ for this SoC and how it boots,
> > the use of mkimage, etc.?
>
> Sure, I will cobble something together. Any particular good examples
> to look at for inspiration?

You could try doc/board/rockchip

>
> > Also as a follow-on, can you add a binman entry type for this so that
> > building a functioning image is automatic?
>
> I'm talking a look at this as well. In this case though, it would just
> automate the call to mkimage, as there are no other binaries.
>
> > Finally, please add comments to struct spkg_hdr members below
>
> Yes, although they will be pretty much verbatim from the user manual,
> which documents the format. The docs are available publicly, but you
> need to click through a few links to reach them.
>

OK. Still, it is handy to have it in the code. Downloading a PDF to
understand the code it not great.

Regards,
Simon


Re: [RFC PATCH v3 9/9] tools: spkgimage: add Renesas SPKG format

2023-02-22 Thread Ralph Siemsen
Hi Simon,

Thanks for your review!

On Wed, Feb 22, 2023 at 2:17 PM Simon Glass  wrote:
>
> Can you please add some details to doc/ for this SoC and how it boots,
> the use of mkimage, etc.?

Sure, I will cobble something together. Any particular good examples
to look at for inspiration?

> Also as a follow-on, can you add a binman entry type for this so that
> building a functioning image is automatic?

I'm talking a look at this as well. In this case though, it would just
automate the call to mkimage, as there are no other binaries.

> Finally, please add comments to struct spkg_hdr members below

Yes, although they will be pretty much verbatim from the user manual,
which documents the format. The docs are available publicly, but you
need to click through a few links to reach them.

Regards,
Ralph


Re: [RFC PATCH v3 9/9] tools: spkgimage: add Renesas SPKG format

2023-02-22 Thread Simon Glass
Hi Ralph,

On Wed, 22 Feb 2023 at 08:44, Ralph Siemsen  wrote:
>
> Renesas RZ/N1 devices contain BootROM code that loads a custom SPKG
> image from QSPI, NAND or USB DFU. Support this format in mkimage tool.
>
> SPKGs can optionally be signed, however creation of signed SPKG is not
> currently supported.
>
> Example of how to use it:
>
> tools/mkimage -n board/schneider/rzn1-snarc/spkgimage.cfg \
> -T spkgimage -a 0x2004 -e 0x2004 \
> -d u-boot.bin u-boot.bin.spkg
>
> The config file (spkgimage.cfg in this example) contains additional
> parameters such as NAND ECC settings.
>
> Signed-off-by: Ralph Siemsen 
> ---
>
> Changes in v3:
> - provide definition of __packed (as done in kwbimage.h)
> - explain why a local copy of roundup() is needed
> - document spkgimage in doc/mkimage.1
> - add range checks when parsing config file values
> - add line numbers for reporting errors in config file
> - rename SPKG_HEADER_SIGNATURE to SPKG_HEADER_MARKER
> - fix segfault when image is padded by less than 4 bytes
> - minor style and typo fixes
>
> Changes in v2:
> - rewrote the stand-alone spkg_utility to integrate into mkimage
>
>  board/schneider/rzn1-snarc/spkgimage.cfg |  26 ++
>  boot/image.c |   1 +
>  doc/mkimage.1|  45 
>  include/image.h  |   1 +
>  tools/Makefile   |   1 +
>  tools/spkgimage.c| 330 +++
>  tools/spkgimage.h|  45 
>  7 files changed, 449 insertions(+)
>  create mode 100644 board/schneider/rzn1-snarc/spkgimage.cfg
>  create mode 100644 tools/spkgimage.c
>  create mode 100644 tools/spkgimage.h

[..]

Reviewed-by: Simon Glass 

Can you please add some details to doc/ for this SoC and how it boots,
the use of mkimage, etc.?

Also as a follow-on, can you add a binman entry type for this so that
building a functioning image is automatic?

Finally, please add comments to struct spkg_hdr members below


> diff --git a/tools/spkgimage.h b/tools/spkgimage.h
> new file mode 100644
> index 00..7be127b50f
> --- /dev/null
> +++ b/tools/spkgimage.h
> @@ -0,0 +1,45 @@
> +/* SPDX-License-Identifier: BSD-2-Clause */
> +/*
> + * Renesas RZ/N1 Package Table format
> + * (C) 2015-2016 Renesas Electronics Europe, LTD
> + * All rights reserved.
> + *
> + * Converted to mkimage plug-in
> + * (C) Copyright 2022 Schneider Electric
> + */
> +
> +#ifndef _SPKGIMAGE_H_
> +#define _SPKGIMAGE_H_
> +
> +#ifdef __GNUC__
> +#define __packed __attribute((packed))
> +#else
> +#define __packed
> +#endif
> +
> +#define SPKG_HEADER_MARKER {'R', 'Z', 'N', '1'}
> +#define SPKG_HEADER_SIZE   24
> +#define SPKG_HEADER_COUNT  8
> +#define SPKG_BLP_SIZE  264
> +#define SPKG_CRC_SIZE  4
> +
> +/* SPKG header */
> +struct spkg_hdr {
> +   uint8_t marker[4];  /* aka magic */
> +   uint8_t version;
> +   uint8_t ecc;
> +   uint8_t ecc_scheme;
> +   uint8_t ecc_bytes;
> +   uint32_tpayload_length; /* only HIGHER 24 bits */
> +   uint32_tload_address;
> +   uint32_texecution_offset;
> +   uint32_tcrc; /* of this header */
> +} __packed;
> +
> +struct spkg_file {
> +   struct spkg_hdr header[SPKG_HEADER_COUNT];
> +   uint8_t payload[0];
> +   /* then the CRC */
> +} __packed;
> +
> +#endif
> --
> 2.25.1
>

Regards,
Simon


[RFC PATCH v3 9/9] tools: spkgimage: add Renesas SPKG format

2023-02-22 Thread Ralph Siemsen
Renesas RZ/N1 devices contain BootROM code that loads a custom SPKG
image from QSPI, NAND or USB DFU. Support this format in mkimage tool.

SPKGs can optionally be signed, however creation of signed SPKG is not
currently supported.

Example of how to use it:

tools/mkimage -n board/schneider/rzn1-snarc/spkgimage.cfg \
-T spkgimage -a 0x2004 -e 0x2004 \
-d u-boot.bin u-boot.bin.spkg

The config file (spkgimage.cfg in this example) contains additional
parameters such as NAND ECC settings.

Signed-off-by: Ralph Siemsen 
---

Changes in v3:
- provide definition of __packed (as done in kwbimage.h)
- explain why a local copy of roundup() is needed
- document spkgimage in doc/mkimage.1
- add range checks when parsing config file values
- add line numbers for reporting errors in config file
- rename SPKG_HEADER_SIGNATURE to SPKG_HEADER_MARKER
- fix segfault when image is padded by less than 4 bytes
- minor style and typo fixes

Changes in v2:
- rewrote the stand-alone spkg_utility to integrate into mkimage

 board/schneider/rzn1-snarc/spkgimage.cfg |  26 ++
 boot/image.c |   1 +
 doc/mkimage.1|  45 
 include/image.h  |   1 +
 tools/Makefile   |   1 +
 tools/spkgimage.c| 330 +++
 tools/spkgimage.h|  45 
 7 files changed, 449 insertions(+)
 create mode 100644 board/schneider/rzn1-snarc/spkgimage.cfg
 create mode 100644 tools/spkgimage.c
 create mode 100644 tools/spkgimage.h

diff --git a/board/schneider/rzn1-snarc/spkgimage.cfg 
b/board/schneider/rzn1-snarc/spkgimage.cfg
new file mode 100644
index 00..b5faf96b00
--- /dev/null
+++ b/board/schneider/rzn1-snarc/spkgimage.cfg
@@ -0,0 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# (C) Copyright 2022 Schneider Electric
+#
+# SPKG image header, for booting on RZ/N1
+
+# b[35:32] SPKG version
+VERSION1
+
+# b[42:41]  ECC Block size: 0=256 bytes, 1=512 bytes, 2=1024 bytes
+NAND_ECC_BLOCK_SIZE1
+
+# b[45] NAND enable (boolean)
+NAND_ECC_ENABLE1
+
+# b[50:48]  ECC Scheme: 0=BCH2 1=BCH4 2=BCH8 3=BCH16 4=BCH24 5=BCH32
+NAND_ECC_SCHEME3
+
+# b[63:56]  ECC bytes per block
+NAND_BYTES_PER_ECC_BLOCK 28
+
+# Provide dummy BLp header (boolean)
+ADD_DUMMY_BLP  1
+
+# Pad the image to a multiple of
+PADDING64K
diff --git a/boot/image.c b/boot/image.c
index 958dbf8534..5c4f9b807d 100644
--- a/boot/image.c
+++ b/boot/image.c
@@ -181,6 +181,7 @@ static const table_entry_t uimage_type[] = {
{   IH_TYPE_SUNXI_EGON, "sunxi_egon",  "Allwinner eGON Boot Image" 
},
{   IH_TYPE_SUNXI_TOC0, "sunxi_toc0",  "Allwinner TOC0 Boot Image" 
},
{   IH_TYPE_FDT_LEGACY, "fdt_legacy", "legacy Image with Flat 
Device Tree ", },
+   {   IH_TYPE_RENESAS_SPKG, "spkgimage", "Renesas SPKG Image" },
{   -1, "",   "",   },
 };
 
diff --git a/doc/mkimage.1 b/doc/mkimage.1
index d8727ec73c..76c7859bb0 100644
--- a/doc/mkimage.1
+++ b/doc/mkimage.1
@@ -662,6 +662,51 @@ rk3568
 .TE
 .RE
 .
+.SS spkgimage
+The primary configuration file consists of lines containing key/value pairs
+delimited by whitespace. An example follows.
+.PP
+.RS
+.EX
+# Comments and blank lines may be used
+.I key1 value1
+.I key2 value2
+.EE
+.RE
+.P
+The supported
+.I key
+types are as follows.
+.TP
+.B VERSION
+.TQ
+.B NAND_ECC_BLOCK_SIZE
+.TQ
+.B NAND_ECC_ENABLE
+.TQ
+.B NAND_ECC_SCHEME
+.TQ
+.B NAND_BYTES_PER_ECC_BLOCK
+These all take a positive integer value as their argument.
+The value will be copied directly into the respective field
+of the SPKG header structure. For details on these values,
+refer to Section 7.4 of the Renesas RZ/N1 User's Manual.
+.
+.TP
+.B ADD_DUMMY_BLP
+Takes a numeric argument, which is treated as a boolean. Any nonzero
+value will cause a fake BLp security header to be included in the SPKG
+output.
+.
+.TP
+.B PADDING
+Takes a positive integer value, with an optional
+.B K
+or
+.B M
+suffix, indicating KiB / MiB respectively.
+The output SPKG file will be padded to a multiple of this value.
+.
 .SS sunxi_egon
 The primary configuration is the name to use for the device tree.
 .
diff --git a/include/image.h b/include/image.h
index 7717a4c13d..acfbb6a53a 100644
--- a/include/image.h
+++ b/include/image.h
@@ -230,6 +230,7 @@ enum image_type_t {
IH_TYPE_SUNXI_EGON, /* Allwinner eGON Boot Image */
IH_TYPE_SUNXI_TOC0, /* Allwinner TOC0 Boot Image */
IH_TYPE_FDT_LEGACY, /* Binary Flat Device Tree Blob in a 
Legacy Image */
+   IH_TYPE_RENESAS_SPKG,   /* Renesas SPKG image */
 
IH_TYPE_COUNT,  /* Number of image types */
 };
diff --git a/tools/Makefile b/tools/Makefile
index e13effbb66..1d7abcb916 100644
--- a/tools/Makefile
+