Re: [PATCH] cmd: Add a SEAMA image load command

2023-02-07 Thread Tom Rini
On Wed, Feb 01, 2023 at 12:16:13AM +0100, Linus Walleij wrote:

> Add a command to load SEAMA (Seattle Image), a NAND flash
> on-flash storage format.
> 
> This type of flash image is found in some D-Link routers such
> as DIR-645, DIR-842, DIR-859, DIR-860L, DIR-885L, DIR890L and
> DCH-M225, as well as in WD and NEC routers on the ath79
> (MIPS), Broadcom BCM53xx, and RAMIPS platforms.
> 
> This U-Boot command will read and decode a SEAMA image from
> raw NAND flash on any platform. As it is always using big endian
> format for the data decoding is always necessary on platforms
> such as ARM.
> 
> The command is needed to read a SEAMA-encoded boot image on the
> D-Link DIR-890L router for boot from NAND flash in an upcoming
> port of U-Boot to the Broadcom Northstar (BCM4709, BCM53xx)
> architecture.
> 
> A basic test and documentation is added as well. The test must
> be run on a target with NAND flash support and at least one
> resident SEAMA image in flash.
> 
> Cc: Rafał Miłecki 
> Signed-off-by: Linus Walleij 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature


[PATCH] cmd: Add a SEAMA image load command

2023-01-31 Thread Linus Walleij
Add a command to load SEAMA (Seattle Image), a NAND flash
on-flash storage format.

This type of flash image is found in some D-Link routers such
as DIR-645, DIR-842, DIR-859, DIR-860L, DIR-885L, DIR890L and
DCH-M225, as well as in WD and NEC routers on the ath79
(MIPS), Broadcom BCM53xx, and RAMIPS platforms.

This U-Boot command will read and decode a SEAMA image from
raw NAND flash on any platform. As it is always using big endian
format for the data decoding is always necessary on platforms
such as ARM.

The command is needed to read a SEAMA-encoded boot image on the
D-Link DIR-890L router for boot from NAND flash in an upcoming
port of U-Boot to the Broadcom Northstar (BCM4709, BCM53xx)
architecture.

A basic test and documentation is added as well. The test must
be run on a target with NAND flash support and at least one
resident SEAMA image in flash.

Cc: Rafał Miłecki 
Signed-off-by: Linus Walleij 
---
 MAINTAINERS |   7 ++
 README  |   1 +
 cmd/Kconfig |   6 ++
 cmd/Makefile|   1 +
 cmd/seama.c | 158 
 doc/usage/cmd/seama.rst |  60 +++
 doc/usage/index.rst |   1 +
 include/test/suites.h   |   1 +
 test/cmd/Makefile   |   1 +
 test/cmd/seama.c|  71 ++
 test/cmd_ut.c   |   6 ++
 11 files changed, 313 insertions(+)
 create mode 100644 cmd/seama.c
 create mode 100644 doc/usage/cmd/seama.rst
 create mode 100644 test/cmd/seama.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 2dd2b46fcda6..359fde15936c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1329,6 +1329,13 @@ F:   arch/sandbox/
 F: doc/arch/sandbox.rst
 F: include/dt-bindings/*/sandbox*.h
 
+SEAMA
+M: Linus Walleij 
+S: Maintained
+F: cmd/seama.c
+F: doc/usage/cmd/seama.rst
+F: test/cmd/seama.c
+
 SEMIHOSTING
 R: Sean Anderson 
 S: Orphaned
diff --git a/README b/README
index edce7890c0f9..9cc0c981c43c 100644
--- a/README
+++ b/README
@@ -1810,6 +1810,7 @@ sspi  - SPI utility commands
 base   - print or set address offset
 printenv- print environment variables
 pwm- control pwm channels
+seama   - load SEAMA NAND image
 setenv - set environment variables
 saveenv - save environment variables to persistent storage
 protect - enable or disable FLASH write protection
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 4fe2c75de256..ead98c39cdee 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -337,6 +337,12 @@ config BOOTM_RTEMS
help
  Support booting RTEMS images via the bootm command.
 
+config CMD_SEAMA
+   bool "Support read SEAMA NAND images"
+   depends on MTD_RAW_NAND
+   help
+ Support reading NAND Seattle Image (SEAMA) images.
+
 config CMD_VBE
bool "vbe - Verified Boot for Embedded"
depends on BOOTMETH_VBE
diff --git a/cmd/Makefile b/cmd/Makefile
index 0b6a96c1d914..4fea084b44fa 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -156,6 +156,7 @@ obj-$(CONFIG_SANDBOX) += sb.o
 obj-$(CONFIG_CMD_SF) += sf.o
 obj-$(CONFIG_CMD_SCSI) += scsi.o disk.o
 obj-$(CONFIG_CMD_SHA1SUM) += sha1sum.o
+obj-$(CONFIG_CMD_SEAMA) += seama.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_CMD_SETEXPR_FMT) += printf.o
 obj-$(CONFIG_CMD_SPI) += spi.o
diff --git a/cmd/seama.c b/cmd/seama.c
new file mode 100644
index ..3aafb43c48a0
--- /dev/null
+++ b/cmd/seama.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2023 Linus Walleij 
+ * Support for the "SEAttle iMAge" SEAMA NAND image format
+ */
+
+#include 
+#include 
+#include 
+
+/*
+ * All SEAMA data is stored in the flash in "network endianness"
+ * i.e. big endian, which means that it needs to be byte-swapped
+ * on all little endian platforms.
+ *
+ * structure for a SEAMA entity in NAND flash:
+ *
+ * 32 bit SEAMA magic 0x5EA3A417
+ * 16 bit reserved
+ * 16 bit metadata size (following the header)
+ * 32 bit image size
+ * 16 bytes MD5 digest of the image
+ * meta data
+ * ... image data ...
+ *
+ * Then if a new SEAMA magic follows, that is the next image.
+ */
+
+#define SEAMA_MAGIC0x5EA3A417
+#define SEAMA_HDR_NO_META_SZ   28
+#define SEAMA_MAX_META_SZ  (1024 - SEAMA_HDR_NO_META_SZ)
+
+struct seama_header {
+   u32 magic;
+   u32 meta_size;
+   u32 image_size;
+   u8 md5[16];
+   u8 metadata[SEAMA_MAX_META_SZ];
+};
+
+static struct seama_header shdr;
+
+static int env_set_val(const char *varname, ulong val)
+{
+   int ret;
+
+   ret = env_set_hex(varname, val);
+   if (ret)
+   printf("Failed to %s env var\n", varname);
+
+   return ret;
+}
+
+static int do_seama_load_image(struct cmd_tbl *cmdtp, int flag, int argc,
+  char *const argv[])
+{
+   struct mtd_info *mtd;
+   uintptr_t load_addr;
+   unsigned long image_index;
+   u32 len;
+   size_t readsz;
+   int ret;
+   u32 *start;
+   u32 *offset;
+