mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 00/26] i.MX8M HAB Fixes
@ 2023-10-10 14:32 Marco Felsch
  2023-10-10 14:32 ` [PATCH 01/26] kbuild: clean start*_*.{p,ps}imximg files Marco Felsch
                   ` (25 more replies)
  0 siblings, 26 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

Hi all,

unfortunately with the addition of the FlexSPI image support I broke
the HAB support for i.MX8M devices because I didn't honored the new CSF
location. This PR will fix this for the MMC/SD/USB boot case.

Additional the PR adds the support to HAB boot the system from a QSPI
NOR as well. For the FlexSPI HAB boot a 2nd CSF slot/area is added to
keep the very user-friendly single-image approach.

Before reviewing Patch13 and onwards I do recommend to look at Patch26
first to get an overview of the final result.

Patch1-7:
Cleanup the hab command code and image artifacts and enable it support
for all i.MX8M devices.

Patch8-9:
Add the signing support to the Makefile.imx and convert the i.MX8M rules
to use the new macro to cleanup the code.

Patch10-12:
Prepare i.MX8M*-EVKs to be able to build signed images.

Patch13-14:
Add support for multiple CSF slots.

Patch15-23:
Fix the HAB support for MMC/SD card first and add/fix the HAB support
for FlexSPI images.

Patch24:
Make it possible to pass CST command line paramters via environment.
This is very useful for new CST versions which do support PKCS11
out-of-the box if the user specify the backend via '-b pkcs11'.

Patch25-26:
Documentation patches to become an overview of the FlexSPI HAB single
image implementation.

Testers are very welcome :)

Regards,
  Marco

Marco Felsch (17):
  i.MX: HAB: remove unused init() hook
  ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header
  ARM: boards: i.MX8M: make use of new flexspi-imx8m{m,p}-cfg.h header
  ARM: lds: introduce HAB_CSF_LEN define for the hab_csf section
  ARM: lds: add support for a 2nd CSF area
  scripts: imx: force flexspi and hab option order
  scripts: imx: move flexspi_image() into header
  scripts: imx: fix i.MX8M  CSF header placement
  scripts: imx: add imx8m_get_offset_size helper
  scripts: imx-image: header_v2: add CSF slots
  scripts: imx-image: hab_sign: refactor function
  scripts: imx: add helper to write CSF Blocks command
  scripts: imx: fix HAB for FlexSPI boot
  scripts: imx-image: add missing close()
  scripts: imx-image: add support for CST_EXTRA_CMDLINE_OPTIONS
  Documentation: boards: imx: replace i.MX8MQ with i.MX8M
  Documentation: boards: imx: add FlexSPI sub-section

Rouven Czerwinski (9):
  kbuild: clean start*_*.{p,ps}imximg files
  i.MX: HABv4: fix event status comparison
  i.MX: HABv4: fix i.MX8MQ device lockdown
  i.MX: HABv4: extend support to i.MX8M SoCs
  i.MX: HABv4: retrieve HAB ROM version for i.MX8M
  ARM: i.MX: allow HAB for i.MX8M family
  ARM: i.MX: introduce build_imx8m_habv4img
  ARM: i.MX: convert i.MX8M to helper
  ARM: boards: i.MX8M: add HAB image support

 Documentation/boards/imx.rst                  |  83 ++++++++++++-
 .../flash-header-imx8mm-evk.imxcfg            |   4 +-
 .../flash-header-imx8mn-evk.imxcfg            |   4 +-
 .../flash-header-imx8mp-evk.imxcfg            |   4 +-
 arch/arm/lib/pbl.lds.S                        |   8 +-
 arch/arm/mach-imx/Kconfig                     |   4 +-
 drivers/hab/hab.c                             |  49 +++++---
 drivers/hab/habv4.c                           |  40 +++++--
 images/Makefile                               |   3 +-
 images/Makefile.imx                           | 110 +++++++-----------
 include/mach/imx/flexspi-imx8mm-cfg.h         |  10 ++
 include/mach/imx/flexspi-imx8mp-cfg.h         |  10 ++
 include/mach/imx/imx-header.h                 |   2 +
 include/mach/imx/ocotp-fusemap.h              |   3 +
 scripts/imx/imx-image.c                       |  65 ++++++-----
 scripts/imx/imx.c                             |  90 ++++++++++++--
 scripts/imx/imx.h                             |  13 +++
 17 files changed, 355 insertions(+), 147 deletions(-)
 create mode 100644 include/mach/imx/flexspi-imx8mm-cfg.h
 create mode 100644 include/mach/imx/flexspi-imx8mp-cfg.h

-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 01/26] kbuild: clean start*_*.{p,ps}imximg files
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 02/26] i.MX: HABv4: fix event status comparison Marco Felsch
                   ` (24 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Cleanup files build during i.MX8M image generation.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 images/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/images/Makefile b/images/Makefile
index 9739a15c06ba..90d596deabcd 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -225,5 +225,6 @@ $(flash-list): $(image-y-path)
 clean-files := *.pbl *.pblb *.elf *.map start_*.imximg *.img barebox.z start_*.kwbimg \
 	start_*.kwbuartimg *.socfpgaimg *.mlo *.t20img *.t20img.cfg *.t30img \
 	*.t30img.cfg *.t124img *.t124img.cfg *.mlospi *.mlo *.mxsbs *.mxssd *.rkimg \
-	start_*.simximg start_*.usimximg *.zynqimg *.image *.swapped *.missing-firmware
+	start_*.simximg start_*.usimximg start_*.pimximg start_*.psimximg *.zynqimg \
+        *.image *.swapped *.missing-firmware
 clean-files += pbl.lds
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 02/26] i.MX: HABv4: fix event status comparison
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
  2023-10-10 14:32 ` [PATCH 01/26] kbuild: clean start*_*.{p,ps}imximg files Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 03/26] i.MX: HABv4: fix i.MX8MQ device lockdown Marco Felsch
                   ` (23 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Instead of only printing events belonging to a certain level, print all
events that have a higher status than the current event. This fixes
event printing for i.MX8M based boards.

Fixes: 8b35facbb971 ("i.MX: HABv4: Improve HAB event printing")

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 drivers/hab/habv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hab/habv4.c b/drivers/hab/habv4.c
index 9f54aed5f508..bbfbf697b520 100644
--- a/drivers/hab/habv4.c
+++ b/drivers/hab/habv4.c
@@ -224,7 +224,7 @@ static enum hab_status imx8m_read_sram_events(enum hab_status status,
 		}
 	}
 	while (i < num_events) {
-		if (events[i]->status == status) {
+		if (events[i]->status >= status) {
 			if (internal_index == index) {
 				*bytes = sizeof(struct hab_event_record) +
 					be16_to_cpu(events[i]->hdr.len);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 03/26] i.MX: HABv4: fix i.MX8MQ device lockdown
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
  2023-10-10 14:32 ` [PATCH 01/26] kbuild: clean start*_*.{p,ps}imximg files Marco Felsch
  2023-10-10 14:32 ` [PATCH 02/26] i.MX: HABv4: fix event status comparison Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 18:35   ` [PATCH] fixup! " Ahmad Fatoum
  2023-10-10 14:32 ` [PATCH 04/26] i.MX: HAB: remove unused init() hook Marco Felsch
                   ` (22 subsequent siblings)
  25 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

The i.MX8M family of processors moved the SEC_CONFIG_1 fuse to a
different location. Fix this by adding dedicated imx8mq ops which uses
the new hook.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
[m.felsch@pengutronix.de: split commit into two]
[m.felsch@pengutronix.de: integrate review feedback]
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 drivers/hab/hab.c                | 31 ++++++++++++++++++++++++++-----
 include/mach/imx/ocotp-fusemap.h |  3 +++
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/drivers/hab/hab.c b/drivers/hab/hab.c
index afe6a6682276..fa9912f784b4 100644
--- a/drivers/hab/hab.c
+++ b/drivers/hab/hab.c
@@ -150,7 +150,7 @@ static int imx_hab_permanent_write_enable_ocotp(int enable)
 	return imx_ocotp_permanent_write(enable);
 }
 
-static int imx_hab_lockdown_device_ocotp(void)
+static int imx6_hab_lockdown_device_ocotp(void)
 {
 	int ret;
 
@@ -161,6 +161,17 @@ static int imx_hab_lockdown_device_ocotp(void)
 	return imx_ocotp_write_field(OCOTP_SEC_CONFIG_1, 1);
 }
 
+static int imx8m_hab_lockdown_device_ocotp(void)
+{
+	int ret;
+
+	ret = imx_ocotp_write_field(MX8M_OCOTP_SEC_CONFIG_1, 1);
+	if (ret < 0)
+		return ret;
+
+	return imx_ocotp_write_field(MX8MQ_OCOTP_DIR_BT_DIS, 1);
+}
+
 static int imx_hab_device_locked_down_ocotp(void)
 {
 	int ret;
@@ -190,10 +201,18 @@ static struct imx_hab_ops imx_hab_ops_iim = {
 	.permanent_write_enable = imx_hab_permanent_write_enable_iim,
 };
 
-static struct imx_hab_ops imx_hab_ops_ocotp = {
+static struct imx_hab_ops imx6_hab_ops_ocotp = {
+	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
+	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
+	.lockdown_device = imx6_hab_lockdown_device_ocotp,
+	.device_locked_down = imx_hab_device_locked_down_ocotp,
+	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
+};
+
+static struct imx_hab_ops imx8m_hab_ops_ocotp = {
 	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
 	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
-	.lockdown_device = imx_hab_lockdown_device_ocotp,
+	.lockdown_device = imx8m_hab_lockdown_device_ocotp,
 	.device_locked_down = imx_hab_device_locked_down_ocotp,
 	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
 };
@@ -208,8 +227,10 @@ static struct imx_hab_ops *imx_get_hab_ops(void)
 
 	if (IS_ENABLED(CONFIG_HABV3) && (cpu_is_mx25() || cpu_is_mx35()))
 		tmp = &imx_hab_ops_iim;
-	else if (IS_ENABLED(CONFIG_HABV4) && (cpu_is_mx6() || cpu_is_mx8mq()))
-		tmp = &imx_hab_ops_ocotp;
+	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx6())
+		tmp = &imx6_hab_ops_ocotp;
+	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx8mq())
+		tmp = &imx8m_hab_ops_ocotp;
 	else
 		return NULL;
 
diff --git a/include/mach/imx/ocotp-fusemap.h b/include/mach/imx/ocotp-fusemap.h
index 6ba794c16619..823273895502 100644
--- a/include/mach/imx/ocotp-fusemap.h
+++ b/include/mach/imx/ocotp-fusemap.h
@@ -53,5 +53,8 @@
 #define OCOTP_GP1			(OCOTP_WORD(0x660) | OCOTP_BIT(0) | OCOTP_WIDTH(32))
 #define OCOTP_GP2			(OCOTP_WORD(0x670) | OCOTP_BIT(0) | OCOTP_WIDTH(32))
 #define OCOTP_PAD_SETTINGS		(OCOTP_WORD(0x6d0) | OCOTP_BIT(0) | OCOTP_WIDTH(6))
+/* i.MX8M moved the security related fuses */
+#define MX8M_OCOTP_SEC_CONFIG_1		(OCOTP_WORD(0x470) | OCOTP_BIT(25) | OCOTP_WIDTH(1))
+#define MX8MQ_OCOTP_DIR_BT_DIS		(OCOTP_WORD(0x470) | OCOTP_BIT(27) | OCOTP_WIDTH(1))
 
 #endif /* __MACH_IMX_OCOTP_FUSEMAP_H */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 04/26] i.MX: HAB: remove unused init() hook
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (2 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 03/26] i.MX: HABv4: fix i.MX8MQ device lockdown Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 05/26] i.MX: HABv4: extend support to i.MX8M SoCs Marco Felsch
                   ` (21 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

The init() hook was never used so we can remove it to cleanup the code a
bit.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 drivers/hab/hab.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/drivers/hab/hab.c b/drivers/hab/hab.c
index fa9912f784b4..a1c1e89eb249 100644
--- a/drivers/hab/hab.c
+++ b/drivers/hab/hab.c
@@ -185,7 +185,6 @@ static int imx_hab_device_locked_down_ocotp(void)
 }
 
 struct imx_hab_ops {
-	int (*init)(void);
 	int (*write_srk_hash)(const u8 *srk, unsigned flags);
 	int (*read_srk_hash)(u8 *srk);
 	int (*permanent_write_enable)(int enable);
@@ -219,29 +218,20 @@ static struct imx_hab_ops imx8m_hab_ops_ocotp = {
 
 static struct imx_hab_ops *imx_get_hab_ops(void)
 {
-	static struct imx_hab_ops *ops, *tmp;
-	int ret;
+	static struct imx_hab_ops *ops;
 
 	if (ops)
 		return ops;
 
 	if (IS_ENABLED(CONFIG_HABV3) && (cpu_is_mx25() || cpu_is_mx35()))
-		tmp = &imx_hab_ops_iim;
+		ops = &imx_hab_ops_iim;
 	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx6())
-		tmp = &imx6_hab_ops_ocotp;
+		ops = &imx6_hab_ops_ocotp;
 	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx8mq())
-		tmp = &imx8m_hab_ops_ocotp;
+		ops = &imx8m_hab_ops_ocotp;
 	else
 		return NULL;
 
-	if (tmp->init) {
-		ret = tmp->init();
-		if (ret)
-			return NULL;
-	}
-
-	ops = tmp;
-
 	return ops;
 }
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 05/26] i.MX: HABv4: extend support to i.MX8M SoCs
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (3 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 04/26] i.MX: HAB: remove unused init() hook Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 06/26] i.MX: HABv4: retrieve HAB ROM version for i.MX8M Marco Felsch
                   ` (20 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Instead of only supporting i.MX8MQ based boards, extend support to the
whole i.MX8M family.

OCRAM address values were taken from AN12263 Rev1 06/2020. While at it
turn the OCRAM addresses into defines as well and remove the superfluous
comment lines.

The fuses do match with the i.MX8MQ except for the DIR_BT_DIS fuse.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
[m.felsch@pengutronix.de: adapt commit message]
[m.felsch@pengutronix.de: integrate review feedback]
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 drivers/hab/hab.c   |  6 +++++-
 drivers/hab/habv4.c | 26 ++++++++++++++++++--------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/drivers/hab/hab.c b/drivers/hab/hab.c
index a1c1e89eb249..444472511771 100644
--- a/drivers/hab/hab.c
+++ b/drivers/hab/hab.c
@@ -169,6 +169,10 @@ static int imx8m_hab_lockdown_device_ocotp(void)
 	if (ret < 0)
 		return ret;
 
+	/* Only i.MX8MQ requires fusing of DIR_BT_DIS */
+	if (!cpu_is_mx8mq())
+		return ret;
+
 	return imx_ocotp_write_field(MX8MQ_OCOTP_DIR_BT_DIS, 1);
 }
 
@@ -227,7 +231,7 @@ static struct imx_hab_ops *imx_get_hab_ops(void)
 		ops = &imx_hab_ops_iim;
 	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx6())
 		ops = &imx6_hab_ops_ocotp;
-	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx8mq())
+	else if (IS_ENABLED(CONFIG_HABV4) && cpu_is_mx8m())
 		ops = &imx8m_hab_ops_ocotp;
 	else
 		return NULL;
diff --git a/drivers/hab/habv4.c b/drivers/hab/habv4.c
index bbfbf697b520..0238b98dfb7e 100644
--- a/drivers/hab/habv4.c
+++ b/drivers/hab/habv4.c
@@ -193,18 +193,34 @@ static enum hab_status hab_sip_report_status(enum hab_config *config,
 	return (enum hab_status)res.a0;
 }
 
+#define IMX8MQ_ROM_OCRAM_ADDRESS	0x9061C0
+#define IMX8MM_ROM_OCRAM_ADDRESS	0x908040
+#define IMX8MN_ROM_OCRAM_ADDRESS	0x908040
+#define IMX8MP_ROM_OCRAM_ADDRESS	0x90D040
+
 static enum hab_status imx8m_read_sram_events(enum hab_status status,
 					     uint32_t index, void *event,
 					     uint32_t *bytes)
 {
 	struct hab_event_record *events[10];
 	int num_events = 0;
-	char *sram = (char *)0x9061c0;
+	char *sram;
 	int i = 0;
 	int internal_index = 0;
 	char *end = 0;
 	struct hab_event_record *search;
 
+	if (cpu_is_mx8mq())
+		sram = (char *)IMX8MQ_ROM_OCRAM_ADDRESS;
+	else if (cpu_is_mx8mm())
+		sram = (char *)IMX8MM_ROM_OCRAM_ADDRESS;
+	else if (cpu_is_mx8mn())
+		sram = (char *)IMX8MN_ROM_OCRAM_ADDRESS;
+	else if (cpu_is_mx8mp())
+		sram = (char *)IMX8MP_ROM_OCRAM_ADDRESS;
+	else
+		return HAB_STATUS_FAILURE;
+
 	/*
 	 * AN12263 HABv4 Guidelines and Recommendations
 	 * recommends the address and size, however errors are usually contained
@@ -590,7 +606,7 @@ static int imx8m_hab_get_status(void)
 
 static int init_imx8m_hab_get_status(void)
 {
-	if (!cpu_is_mx8mq())
+	if (!cpu_is_mx8m())
 		/* can happen in multi-image builds and is not an error */
 		return 0;
 
@@ -602,12 +618,6 @@ static int init_imx8m_hab_get_status(void)
 
 	return 0;
 }
-
-/*
- *
- *
- *
- */
 postmmu_initcall(init_imx8m_hab_get_status);
 
 static int init_imx6_hab_get_status(void)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 06/26] i.MX: HABv4: retrieve HAB ROM version for i.MX8M
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (4 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 05/26] i.MX: HABv4: extend support to i.MX8M SoCs Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 07/26] ARM: i.MX: allow HAB for i.MX8M family Marco Felsch
                   ` (19 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

The HAB userguide mentions this additional API calls introduced in 2018
(which coincides with the introduction of i.MX8M SoCs).
Call this API call unconditionally on i.MX8M SoCs to print the HAB
version the ROM code supports.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 drivers/hab/habv4.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/hab/habv4.c b/drivers/hab/habv4.c
index 0238b98dfb7e..f74de009fce0 100644
--- a/drivers/hab/habv4.c
+++ b/drivers/hab/habv4.c
@@ -168,6 +168,7 @@ struct habv4_rvt {
 #define FSL_SIP_HAB_REPORT_STATUS       0x04
 #define FSL_SIP_HAB_FAILSAFE            0x05
 #define FSL_SIP_HAB_CHECK_TARGET        0x06
+#define FSL_SIP_HAB_GET_VERSION		0x07
 
 static enum hab_status hab_sip_report_status(enum hab_config *config,
 					     enum habv4_state *state)
@@ -193,6 +194,15 @@ static enum hab_status hab_sip_report_status(enum hab_config *config,
 	return (enum hab_status)res.a0;
 }
 
+static uint32_t hab_sip_get_version(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_GET_VERSION, 0, 0, 0, 0, 0, 0, &res);
+
+	return (uint32_t)res.a0;
+}
+
 #define IMX8MQ_ROM_OCRAM_ADDRESS	0x9061C0
 #define IMX8MM_ROM_OCRAM_ADDRESS	0x908040
 #define IMX8MN_ROM_OCRAM_ADDRESS	0x908040
@@ -610,6 +620,8 @@ static int init_imx8m_hab_get_status(void)
 		/* can happen in multi-image builds and is not an error */
 		return 0;
 
+	pr_info("ROM version: 0x%x\n", hab_sip_get_version());
+
 	/*
 	 * Nobody will check the return value if there were HAB errors, but the
 	 * initcall will fail spectaculously with a strange error message.
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 07/26] ARM: i.MX: allow HAB for i.MX8M family
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (5 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 06/26] i.MX: HABv4: retrieve HAB ROM version for i.MX8M Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 08/26] ARM: i.MX: introduce build_imx8m_habv4img Marco Felsch
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Since the HAB driver can now parse events for the complete family,
allow selection for the whole family.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 arch/arm/mach-imx/Kconfig | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
index 8da49a54b401..c60f1f949232 100644
--- a/arch/arm/mach-imx/Kconfig
+++ b/arch/arm/mach-imx/Kconfig
@@ -745,10 +745,10 @@ config HABV4
 	select HAB
 	select NVMEM
 	select IMX_OCOTP
-	depends on ARCH_IMX6 || ARCH_IMX8MQ
+	depends on ARCH_IMX6 || ARCH_IMX8M
 	depends on OFDEVICE
 	help
-	  High Assurance Boot, as found on i.MX28/i.MX6/i.MX8MQ.
+	  High Assurance Boot, as found on i.MX28/i.MX6/i.MX8M.
 
 config HAB_CERTS_ENV
 	depends on HAB
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 08/26] ARM: i.MX: introduce build_imx8m_habv4img
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (6 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 07/26] ARM: i.MX: allow HAB for i.MX8M family Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 09/26] ARM: i.MX: convert i.MX8M to helper Marco Felsch
                   ` (17 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Similar to the helper for i.MX6, this allows us to build HAB enabled
images by selecting KConfig symbols. So far I have only tested signed
images, so there is no support for usb-signed or encrypted images yet.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
---
 images/Makefile.imx | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/images/Makefile.imx b/images/Makefile.imx
index 8f6e8740d165..76a0f606fe35 100644
--- a/images/Makefile.imx
+++ b/images/Makefile.imx
@@ -21,6 +21,19 @@ endif
 )
 endef
 
+define build_imx8m_habv4img =
+$(eval
+ifeq ($($(strip $(1))), y)
+	pblb-y += $(strip $(2))
+	CFG_$(strip $(2)).pblb.imximg = $(board)/$(strip $(3)).imxcfg
+	FILE_barebox-$(strip $(4)).img    = $(strip $(2)).pblb.pimximg
+	FILE_barebox-$(strip $(4))-s.img  = $(strip $(2)).pblb.psimximg
+	image-y += barebox-$(strip $(4)).img
+	image-$(CONFIG_HABV4_IMAGE_SIGNED) += barebox-$(strip $(4))-s.img
+endif
+)
+endef
+
 # %.imximg - convert into i.MX image
 # ----------------------------------------------------------------
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 09/26] ARM: i.MX: convert i.MX8M to helper
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (7 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 08/26] ARM: i.MX: introduce build_imx8m_habv4img Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 10/26] ARM: boards: i.MX8M: add HAB image support Marco Felsch
                   ` (16 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Make use of the new helper function for all .pimximg images to cleanup
the Makefile and to have possible secure-boot image builds in place.

There are 3 i.MX8MQ platforms which don't use the .pimximg. I'm not sure
if this is intended but I left these out for now.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
[m.felsch@pengutronix.de: convert all possible make targets]
[m.felsch@pengutronix.de: adapt commit message]
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 images/Makefile.imx | 97 ++++++++++++---------------------------------
 1 file changed, 26 insertions(+), 71 deletions(-)

diff --git a/images/Makefile.imx b/images/Makefile.imx
index 76a0f606fe35..7fc40132fc95 100644
--- a/images/Makefile.imx
+++ b/images/Makefile.imx
@@ -444,83 +444,38 @@ FILE_barebox-kamstrup-mx7-concentrator.img = start_kamstrup_mx7_concentrator.pbl
 image-$(CONFIG_MACH_KAMSTRUP_MX7_CONCENTRATOR) += barebox-kamstrup-mx7-concentrator.img
 
 # ----------------------- i.MX8mm based boards --------------------------
-pblb-$(CONFIG_MACH_NXP_IMX8MM_EVK) += start_nxp_imx8mm_evk
-CFG_start_nxp_imx8mm_evk.pblb.imximg = $(board)/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
-FILE_barebox-nxp-imx8mm-evk.img = start_nxp_imx8mm_evk.pblb.pimximg
-image-$(CONFIG_MACH_NXP_IMX8MM_EVK) += barebox-nxp-imx8mm-evk.img
-
-pblb-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += start_phyboard_polis_rdk_ddr_autodetect
-CFG_start_phyboard_polis_rdk_ddr_autodetect.pblb.imximg = $(board)/phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk.imxcfg
-FILE_barebox-phyboard-polis-rdk-ddr-autodetect.img = start_phyboard_polis_rdk_ddr_autodetect.pblb.pimximg
-image-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += barebox-phyboard-polis-rdk-ddr-autodetect.img
-
-pblb-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += start_phyboard_polis_rdk_ddr_1g
-CFG_start_phyboard_polis_rdk_ddr_1g.pblb.imximg = $(board)/phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk.imxcfg
-FILE_barebox-phyboard-polis-rdk-ddr-1g.img = start_phyboard_polis_rdk_ddr_1g.pblb.pimximg
-image-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += barebox-phyboard-polis-rdk-ddr-1g.img
-
-pblb-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += start_phyboard_polis_rdk_ddr_2g
-CFG_start_phyboard_polis_rdk_ddr_2g.pblb.imximg = $(board)/phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk.imxcfg
-FILE_barebox-phyboard-polis-rdk-ddr-2g.img = start_phyboard_polis_rdk_ddr_2g.pblb.pimximg
-image-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += barebox-phyboard-polis-rdk-ddr-2g.img
-
-pblb-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += start_phyboard_polis_rdk_ddr_4g
-CFG_start_phyboard_polis_rdk_ddr_4g.pblb.imximg = $(board)/phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk.imxcfg
-FILE_barebox-phyboard-polis-rdk-ddr-4g.img = start_phyboard_polis_rdk_ddr_4g.pblb.pimximg
-image-$(CONFIG_MACH_PHYTEC_SOM_IMX8MM) += barebox-phyboard-polis-rdk-ddr-4g.img
-
-pblb-$(CONFIG_MACH_PROTONIC_IMX8M) += start_prt_prt8mm
-CFG_start_prt_prt8mm.pblb.imximg = $(board)/protonic-imx8m/flash-header-prt8mm.imxcfg
-FILE_barebox-prt-prt8mm.img = start_prt_prt8mm.pblb.pimximg
-image-$(CONFIG_MACH_PROTONIC_IMX8M) += barebox-prt-prt8mm.img
-
-pblb-$(CONFIG_MACH_INNOCOMM_WB15) += start_innocomm_wb15_evk
-CFG_start_innocomm_wb15_evk.pblb.imximg = $(board)/innocomm-imx8mm-wb15/flash-header-imx8mm-wb15.imxcfg
-FILE_barebox-innocomm-imx8mm-wb15-evk.img = start_innocomm_wb15_evk.pblb.pimximg
-image-$(CONFIG_MACH_INNOCOMM_WB15) += barebox-innocomm-imx8mm-wb15-evk.img
+$(call build_imx8m_habv4img, CONFIG_MACH_NXP_IMX8MM_EVK, start_nxp_imx8mm_evk, nxp-imx8mm-evk/flash-header-imx8mm-evk, nxp-imx8mm-evk)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_PHYTEC_SOM_IMX8MM, start_phyboard_polis_rdk_ddr_autodetect, phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk, phyboard-polis-rdk-ddr-autodetect)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_PHYTEC_SOM_IMX8MM, start_phyboard_polis_rdk_ddr_1g, phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk, phyboard-polis-rdk-ddr-1g)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_PHYTEC_SOM_IMX8MM, start_phyboard_polis_rdk_ddr_2g, phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk, phyboard-polis-rdk-ddr-2g)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_PHYTEC_SOM_IMX8MM, start_phyboard_polis_rdk_ddr_4g, phytec-som-imx8mm/flash-header-imx8mm-phyboard-polis-rdk, phyboard-polis-rdk-ddr-4g)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_PROTONIC_IMX8M, start_prt_prt8mm, protonic-imx8m/flash-header-prt8mm, prt-prt8mm)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_INNOCOMM_WB15, start_innocomm_wb15_evk, innocomm-imx8mm-wb15/flash-header-imx8mm-wb15, innocomm-imx8mm-wb15-evk)
 
 # ----------------------- i.MX8mn based boards --------------------------
-pblb-$(CONFIG_MACH_NXP_IMX8MN_EVK) += start_nxp_imx8mn_evk
-CFG_start_nxp_imx8mn_evk.pblb.imximg = $(board)/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
-FILE_barebox-nxp-imx8mn-evk.img = start_nxp_imx8mn_evk.pblb.pimximg
-image-$(CONFIG_MACH_NXP_IMX8MN_EVK) += barebox-nxp-imx8mn-evk.img
+$(call build_imx8m_habv4img, CONFIG_MACH_NXP_IMX8MN_EVK, start_nxp_imx8mn_evk, nxp-imx8mn-evk/flash-header-imx8mn-evk, nxp-imx8mn-evk)
 
 # ----------------------- i.MX8mp based boards --------------------------
-pblb-$(CONFIG_MACH_NXP_IMX8MP_EVK) += start_nxp_imx8mp_evk
-CFG_start_nxp_imx8mp_evk.pblb.imximg = $(board)/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
-FILE_barebox-nxp-imx8mp-evk.img = start_nxp_imx8mp_evk.pblb.pimximg
-image-$(CONFIG_MACH_NXP_IMX8MP_EVK) += barebox-nxp-imx8mp-evk.img
-
-pblb-$(CONFIG_MACH_SKOV_IMX8MP) += start_skov_imx8mp
-CFG_start_skov_imx8mp.pblb.imximg = $(board)/skov-imx8mp/flash-header-skov-imx8mp.imxcfg
-FILE_barebox-skov-imx8mp.img = start_skov_imx8mp.pblb.pimximg
-image-$(CONFIG_MACH_SKOV_IMX8MP) += barebox-skov-imx8mp.img
-
-pblb-$(CONFIG_MACH_TQ_MBA8MPXL) += start_tqma8mpxl
-CFG_start_tqma8mpxl.pblb.imximg = $(board)/tqma8mpxl/flash-header-tqma8mpxl.imxcfg
-FILE_barebox-tqma8mpxl.img = start_tqma8mpxl.pblb.pimximg
-image-$(CONFIG_MACH_TQ_MBA8MPXL) += barebox-tqma8mpxl.img
-
-pblb-$(CONFIG_MACH_POLYHEX_DEBIX) += start_polyhex_debix
-CFG_start_polyhex_debix.pblb.imximg = $(board)/polyhex-debix/flash-header-polyhex-debix.imxcfg
-FILE_barebox-polyhex-debix.img = start_polyhex_debix.pblb.pimximg
-image-$(CONFIG_MACH_POLYHEX_DEBIX) += barebox-polyhex-debix.img
-
-pblb-$(CONFIG_MACH_POLYHEX_DEBIX) += start_polyhex_debix_som_a_8g
-CFG_start_polyhex_debix_som_a_8g.pblb.imximg = $(board)/polyhex-debix/flash-header-polyhex-debix.imxcfg
-FILE_barebox-polyhex-debix-som-a-8g.img = start_polyhex_debix_som_a_8g.pblb.pimximg
-image-$(CONFIG_MACH_POLYHEX_DEBIX) += barebox-polyhex-debix-som-a-8g.img
-
-pblb-$(CONFIG_MACH_VARISCITE_DT8MCUSTOMBOARD_IMX8MP) += start_variscite_imx8mp_dart
-CFG_start_variscite_imx8mp_dart.pblb.imximg = $(board)/variscite-dt8mcustomboard-imx8mp/flash-header-imx8mp-dart.imxcfg
-FILE_barebox-variscite-imx8mp-dart-cb.img = start_variscite_imx8mp_dart.pblb.pimximg
-image-$(CONFIG_MACH_VARISCITE_DT8MCUSTOMBOARD_IMX8MP) += barebox-variscite-imx8mp-dart-cb.img
+$(call build_imx8m_habv4img, CONFIG_MACH_NXP_IMX8MP_EVK, start_nxp_imx8mp_evk, nxp-imx8mp-evk/flash-header-imx8mp-evk, nxp-imx8mp-evk)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_SKOV_IMX8MP, start_skov_imx8mp, skov-imx8mp/flash-header-skov-imx8mp, skov-imx8mp)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_TQ_MBA8MPXL, start_tqma8mpxl, tqma8mpxl/flash-header-tqma8mpxl, tqma8mpxl)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_POLYHEX_DEBIX, start_polyhex_debix, polyhex-debix/flash-header-polyhex-debix, polyhex-debix)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_POLYHEX_DEBIX, start_polyhex_debix_som_a_8g, polyhex-debix/flash-header-polyhex-debix, polyhex-debix-som-a-8g)
+
+$(call build_imx8m_habv4img, CONFIG_MACH_VARISCITE_DT8MCUSTOMBOARD_IMX8MP, start_variscite_imx8mp_dart, variscite-dt8mcustomboard-imx8mp/flash-header-imx8mp-dart, variscite-imx8mp-dart-cb)
 
 # ----------------------- i.MX8mq based boards --------------------------
-pblb-$(CONFIG_MACH_NXP_IMX8MQ_EVK) += start_nxp_imx8mq_evk
-CFG_start_nxp_imx8mq_evk.pblb.imximg = $(board)/nxp-imx8mq-evk/flash-header-imx8mq-evk.imxcfg
-FILE_barebox-nxp-imx8mq-evk.img = start_nxp_imx8mq_evk.pblb.pimximg
-image-$(CONFIG_MACH_NXP_IMX8MQ_EVK) += barebox-nxp-imx8mq-evk.img
+$(call build_imx8m_habv4img, CONFIG_MACH_NXP_IMX8MQ_EVK, start_nxp_imx8mq_evk, nxp-imx8mq-evk/flash-header-imx8mq-evk, nxp-imx8mq-evk)
 
 pblb-$(CONFIG_MACH_ZII_IMX8MQ_DEV) += start_zii_imx8mq_dev
 CFG_start_zii_imx8mq_dev.pblb.imximg = $(board)/zii-imx8mq-dev/flash-header-zii-imx8mq-dev.imxcfg
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 10/26] ARM: boards: i.MX8M: add HAB image support
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (8 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 09/26] ARM: i.MX: convert i.MX8M to helper Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-10 14:32 ` [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header Marco Felsch
                   ` (15 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

This adds the habv4-imx8-gencsf.h header to include HAB image commands
which are enabled if CONFIG_HABV4 is enabled.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
[m.felsch@pengutronix.de: split commit]
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg | 1 +
 arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg | 1 +
 arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg | 1 +
 3 files changed, 3 insertions(+)

diff --git a/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg b/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
index d6a536053e7d..a89243a78aed 100644
--- a/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
@@ -8,3 +8,4 @@ ivtofs 0x400
 
 flexspi_ivtofs 0x1000
 flexspi_fcfbofs 0x0
+#include <mach/imx/habv4-imx8-gencsf.h>
diff --git a/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg b/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
index a9b592e6242b..755140e7ce51 100644
--- a/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
@@ -8,3 +8,4 @@ ivtofs 0x0
 
 flexspi_ivtofs 0x0
 flexspi_fcfbofs 0x400
+#include <mach/imx/habv4-imx8-gencsf.h>
diff --git a/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg b/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
index 3bb44d199c6a..6b05c2c38c03 100644
--- a/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
@@ -8,3 +8,4 @@ ivtofs 0x0
 
 flexspi_ivtofs 0x0
 flexspi_fcfbofs 0x400
+#include <mach/imx/habv4-imx8-gencsf.h>
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (9 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 10/26] ARM: boards: i.MX8M: add HAB image support Marco Felsch
@ 2023-10-10 14:32 ` Marco Felsch
  2023-10-13  6:27   ` Sascha Hauer
  2023-10-10 14:33 ` [PATCH 12/26] ARM: boards: i.MX8M: make use of new " Marco Felsch
                   ` (14 subsequent siblings)
  25 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:32 UTC (permalink / raw)
  To: barebox

Add dedicated configuration headers which guard the flexspi_* options
like it is done for the HABV4 configuration block to be more
user-friendly.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 include/mach/imx/flexspi-imx8mm-cfg.h | 10 ++++++++++
 include/mach/imx/flexspi-imx8mp-cfg.h | 10 ++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 include/mach/imx/flexspi-imx8mm-cfg.h
 create mode 100644 include/mach/imx/flexspi-imx8mp-cfg.h

diff --git a/include/mach/imx/flexspi-imx8mm-cfg.h b/include/mach/imx/flexspi-imx8mm-cfg.h
new file mode 100644
index 000000000000..f7f0310a57f4
--- /dev/null
+++ b/include/mach/imx/flexspi-imx8mm-cfg.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * This snippet can be included from a i.MX flash header configuration
+ * file for generating FlexSPI compatible images.
+ */
+#if defined(CONFIG_SPI_NXP_FLEXSPI)
+flexspi_ivtofs 0x1000
+flexspi_fcfbofs 0x0
+#endif
diff --git a/include/mach/imx/flexspi-imx8mp-cfg.h b/include/mach/imx/flexspi-imx8mp-cfg.h
new file mode 100644
index 000000000000..bbc061241748
--- /dev/null
+++ b/include/mach/imx/flexspi-imx8mp-cfg.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * This snippet can be included from a i.MX flash header configuration
+ * file for generating FlexSPI compatible images.
+ */
+#if defined(CONFIG_SPI_NXP_FLEXSPI)
+flexspi_ivtofs 0x0
+flexspi_fcfbofs 0x400
+#endif
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 12/26] ARM: boards: i.MX8M: make use of new flexspi-imx8m{m,p}-cfg.h header
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (10 preceding siblings ...)
  2023-10-10 14:32 ` [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 13/26] ARM: lds: introduce HAB_CSF_LEN define for the hab_csf section Marco Felsch
                   ` (13 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Use the new config header to make it more user-friendly.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg | 3 +--
 arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg | 3 +--
 arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg b/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
index a89243a78aed..d1d223a8ee7a 100644
--- a/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mm-evk/flash-header-imx8mm-evk.imxcfg
@@ -6,6 +6,5 @@ loadaddr 0x007e1000
 max_load_size 0x3f000
 ivtofs 0x400
 
-flexspi_ivtofs 0x1000
-flexspi_fcfbofs 0x0
+#include <mach/imx/flexspi-imx8mm-cfg.h>
 #include <mach/imx/habv4-imx8-gencsf.h>
diff --git a/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg b/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
index 755140e7ce51..f47ea082667c 100644
--- a/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mn-evk/flash-header-imx8mn-evk.imxcfg
@@ -6,6 +6,5 @@ loadaddr 0x912000
 max_load_size 0x3f000
 ivtofs 0x0
 
-flexspi_ivtofs 0x0
-flexspi_fcfbofs 0x400
+#include <mach/imx/flexspi-imx8mp-cfg.h>
 #include <mach/imx/habv4-imx8-gencsf.h>
diff --git a/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg b/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
index 6b05c2c38c03..c896c9f24832 100644
--- a/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
+++ b/arch/arm/boards/nxp-imx8mp-evk/flash-header-imx8mp-evk.imxcfg
@@ -6,6 +6,5 @@ loadaddr 0x920000
 max_load_size 0x3f000
 ivtofs 0x0
 
-flexspi_ivtofs 0x0
-flexspi_fcfbofs 0x400
+#include <mach/imx/flexspi-imx8mp-cfg.h>
 #include <mach/imx/habv4-imx8-gencsf.h>
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 13/26] ARM: lds: introduce HAB_CSF_LEN define for the hab_csf section
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (11 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 12/26] ARM: boards: i.MX8M: make use of new " Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 14/26] ARM: lds: add support for a 2nd CSF area Marco Felsch
                   ` (12 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Introduce the HAB_CSF_LEN to make conditional size changes easier later
on. No functional change.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/lib/pbl.lds.S | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
index 2b4b1d6a9513..898d6be33b54 100644
--- a/arch/arm/lib/pbl.lds.S
+++ b/arch/arm/lib/pbl.lds.S
@@ -12,6 +12,8 @@
 #define BASE	(TEXT_BASE - SZ_2M)
 #endif
 
+#define HAB_CSF_LEN	0x2000
+
 OUTPUT_FORMAT(BAREBOX_OUTPUT_FORMAT)
 OUTPUT_ARCH(BAREBOX_OUTPUT_ARCH)
 
@@ -85,7 +87,7 @@ SECTIONS
 	__csf_start = .;
 	.hab_csf : {
 		BYTE(0x5a);
-		. += + 0x1fff;
+		. += + HAB_CSF_LEN - 1;
 	} = 0x5a
 	__csf_end = .;
 #endif /* CONFIG_CPU_64 && CONFIG_HABV4 */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 14/26] ARM: lds: add support for a 2nd CSF area
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (12 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 13/26] ARM: lds: introduce HAB_CSF_LEN define for the hab_csf section Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-13  6:37   ` Sascha Hauer
  2023-10-10 14:33 ` [PATCH 15/26] scripts: imx: force flexspi and hab option order Marco Felsch
                   ` (11 subsequent siblings)
  25 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Extend the CSF area to be able two store two CSF slots. The 2nd CSF slot
is required in case of FlexSPI HAB boot support. The first slot is used
for the MMC-IVT + barebox-pbl; the 2nd slot is used for the FlexSPI-IVT
+ barebox-pbl. This is necessary because of the fact that the HAB ROM
code requires that the IVT is signed as well.

Use the CONFIG_SPI_NXP_FLEXSPI Kconfig symbol to decide if we need to
add the support for FlexSPI image signatures as well since early SRAM
space is limited and 4K can make a difference here.

Please check the documentation for a more detailed information about the
i.MX8M image layout.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/lib/pbl.lds.S | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
index 898d6be33b54..9ab6d22eb59d 100644
--- a/arch/arm/lib/pbl.lds.S
+++ b/arch/arm/lib/pbl.lds.S
@@ -12,7 +12,11 @@
 #define BASE	(TEXT_BASE - SZ_2M)
 #endif
 
+#ifdef CONFIG_SPI_NXP_FLEXSPI
+#define HAB_CSF_LEN	0x4000
+#else
 #define HAB_CSF_LEN	0x2000
+#endif
 
 OUTPUT_FORMAT(BAREBOX_OUTPUT_FORMAT)
 OUTPUT_ARCH(BAREBOX_OUTPUT_ARCH)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 15/26] scripts: imx: force flexspi and hab option order
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (13 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 14/26] ARM: lds: add support for a 2nd CSF area Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 16/26] scripts: imx: move flexspi_image() into header Marco Felsch
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Force the order to ensure that the hab options are parsed after the
flexspi options. This is important since we need to know which memory
regions need to be verified and/or en-/decrypted and with the flexspi
support a 2nd header is added in between the first header and the
barebox pbl binary.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index e3169bace6a6..32488455b7a8 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -614,6 +614,12 @@ static int do_flexspi_ivtofs(struct config_data *data, int argc, char *argv[])
 	if (argc < 2)
 		return -EINVAL;
 
+	if (data->csf) {
+		fprintf(stderr, "#include <mach/imx/flexspi-imx8m*-cfg.h> must be placed in front "
+				"of #include <mach/imx/habv4-imx8-gencsf.h>\n");
+		return -EINVAL;
+	}
+
 	data->image_flexspi_ivt_offset = strtoul(argv[1], NULL, 0);
 
 	return 0;
@@ -624,6 +630,12 @@ static int do_flexspi_fcfbofs(struct config_data *data, int argc, char *argv[])
 	if (argc < 2)
 		return -EINVAL;
 
+	if (data->csf) {
+		fprintf(stderr, "#include <mach/imx/flexspi-imx8m*-cfg.h> must be placed in front "
+				"of #include <mach/imx/habv4-imx8-gencsf.h>\n");
+		return -EINVAL;
+	}
+
 	data->image_flexspi_fcfb_offset = strtoul(argv[1], NULL, 0);
 
 	return 0;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 16/26] scripts: imx: move flexspi_image() into header
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (14 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 15/26] scripts: imx: force flexspi and hab option order Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 17/26] scripts: imx: fix i.MX8M CSF header placement Marco Felsch
                   ` (9 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Move the flexspi_image() into the imx.h header to make the code reusable
between imx.c and imx-image.c. No functional change.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx-image.c | 12 ------------
 scripts/imx/imx.h       | 13 +++++++++++++
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 1f96b383901f..ecb16187de17 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -290,18 +290,6 @@ static int write_mem_v1(uint32_t addr, uint32_t val, int width, int set_bits, in
 	return 0;
 }
 
-static bool flexspi_image(const struct config_data *data)
-{
-	/*
-	 *           | FlexSPI-FCFB  | FlexSPI-IVT
-	 * -----------------------------------------
-	 * i.MX8MM   |   0x0         |  0x1000
-	 * i.MX8MN/P |   0x400       |  0x0
-	 */
-
-	return data->image_flexspi_ivt_offset || data->image_flexspi_fcfb_offset;
-}
-
 /*
  * ============================================================================
  * i.MX flash header v2 handling. Found on i.MX50, i.MX53 and i.MX6
diff --git a/scripts/imx/imx.h b/scripts/imx/imx.h
index 08cdf8321a09..65697a9b0d43 100644
--- a/scripts/imx/imx.h
+++ b/scripts/imx/imx.h
@@ -15,4 +15,17 @@ static inline int cpu_is_mx8m(const struct config_data *data)
 	}
 }
 
+static inline bool flexspi_image(const struct config_data *data)
+{
+	/*
+	 *           | FlexSPI-FCFB  | FlexSPI-IVT
+	 * -----------------------------------------
+	 * i.MX8MM   |   0x0         |  0x1000
+	 * i.MX8MN/P |   0x400       |  0x0
+	 */
+
+	return data->image_flexspi_ivt_offset ||
+	       data->image_flexspi_fcfb_offset;
+}
+
 int parse_config(struct config_data *data, const char *filename);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 17/26] scripts: imx: fix i.MX8M  CSF header placement
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (15 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 16/26] scripts: imx: move flexspi_image() into header Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 18/26] scripts: imx: add imx8m_get_offset_size helper Marco Felsch
                   ` (8 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

The current boot flow is broken if both the FlexSPI image and the HAB
support is enabled. The HAB/CST tool need to take the additional FlexSPI
IVT header into account which is added in between the first IVT header
and the barebox pbl code.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 include/mach/imx/imx-header.h | 1 +
 scripts/imx/imx-image.c       | 9 +++++----
 scripts/imx/imx.c             | 6 +++++-
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/include/mach/imx/imx-header.h b/include/mach/imx/imx-header.h
index b11b57c37227..0ad3485eb261 100644
--- a/include/mach/imx/imx-header.h
+++ b/include/mach/imx/imx-header.h
@@ -7,6 +7,7 @@
 
 #define HEADER_LEN 0x1000	/* length of the blank area + IVT + DCD */
 #define CSF_LEN 0x2000		/* length of the CSF (needed for HAB) */
+#define FLEXSPI_HEADER_LEN	HEADER_LEN
 
 #define DEK_BLOB_HEADER 8	/* length of DEK blob header */
 #define DEK_BLOB_KEY 32		/* length of DEK blob AES-256 key */
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index ecb16187de17..01da4cc36467 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -408,8 +408,6 @@ static size_t add_flexspi_fcfb_header(const struct config_data *data, void *buf)
 	return sizeof(nor_conf);
 }
 
-#define FLEXSPI_HEADER_LEN	HEADER_LEN
-
 static size_t
 add_flexspi_header(const struct config_data *data, void **_buf, size_t *header_len)
 {
@@ -812,8 +810,11 @@ static int hab_sign(struct config_data *data)
 		 * For i.MX8 insert the CSF data into the reserved CSF area
 		 * right behind the PBL
 		 */
-		offset = roundup(data->header_gap + data->pbl_code_size +
-				 HEADER_LEN, 0x1000);
+		offset = data->header_gap + data->pbl_code_size + HEADER_LEN;
+		if (flexspi_image(data))
+			offset += FLEXSPI_HEADER_LEN;
+
+		offset = roundup(offset, 0x1000);
 		if (data->signed_hdmi_firmware_file)
 			offset += PLUGIN_HDMI_SIZE;
 
diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index 32488455b7a8..b3c13510af84 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -332,6 +332,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	int i;
 	uint32_t signed_size = data->load_size;
 	uint32_t offset = data->image_ivt_offset;
+	unsigned int hdrlen = HEADER_LEN;
 
 	if (!data->csf)
 		return -EINVAL;
@@ -347,8 +348,11 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	 * Ensure we only sign the PBL for i.MX8MQ
 	 */
 	if (data->pbl_code_size && cpu_is_mx8m(data)) {
+		if (flexspi_image(data))
+			hdrlen += FLEXSPI_HEADER_LEN;
+
 		offset += data->header_gap;
-		signed_size = roundup(data->pbl_code_size + HEADER_LEN, 0x1000);
+		signed_size = roundup(data->pbl_code_size + hdrlen, 0x1000);
 		if (data->signed_hdmi_firmware_file)
 			offset += PLUGIN_HDMI_SIZE;
 	}
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 18/26] scripts: imx: add imx8m_get_offset_size helper
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (16 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 17/26] scripts: imx: fix i.MX8M CSF header placement Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 19/26] scripts: imx-image: header_v2: add CSF slots Marco Felsch
                   ` (7 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

No functional change, just move the offset and signed_size calculation
into a dedicated helper.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index b3c13510af84..933019eba449 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -325,6 +325,22 @@ static int do_hab(struct config_data *data, int argc, char *argv[])
 	return 0;
 }
 
+static void
+imx8m_get_offset_size(struct config_data *data,
+		      uint32_t *offset, uint32_t *signed_length)
+{
+	unsigned int hdrlen = HEADER_LEN;
+
+	if (flexspi_image(data))
+		hdrlen += FLEXSPI_HEADER_LEN;
+
+	*signed_size = roundup(data->pbl_code_size + hdrlen, 0x1000);
+
+	*offset += data->header_gap;
+	if (data->signed_hdmi_firmware_file)
+		*offset += PLUGIN_HDMI_SIZE;
+}
+
 static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 {
 	char *str;
@@ -332,7 +348,6 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	int i;
 	uint32_t signed_size = data->load_size;
 	uint32_t offset = data->image_ivt_offset;
-	unsigned int hdrlen = HEADER_LEN;
 
 	if (!data->csf)
 		return -EINVAL;
@@ -347,15 +362,8 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	/*
 	 * Ensure we only sign the PBL for i.MX8MQ
 	 */
-	if (data->pbl_code_size && cpu_is_mx8m(data)) {
-		if (flexspi_image(data))
-			hdrlen += FLEXSPI_HEADER_LEN;
-
-		offset += data->header_gap;
-		signed_size = roundup(data->pbl_code_size + hdrlen, 0x1000);
-		if (data->signed_hdmi_firmware_file)
-			offset += PLUGIN_HDMI_SIZE;
-	}
+	if (data->pbl_code_size && cpu_is_mx8m(data))
+		imx8m_get_offset_size(data, &offset, &signed_size)
 
 	if (signed_size > 0) {
 		ret = asprintf(&str, "Blocks = 0x%08x 0x%08x 0x%08x \"%s\"",
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 19/26] scripts: imx-image: header_v2: add CSF slots
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (17 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 18/26] scripts: imx: add imx8m_get_offset_size helper Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 20/26] scripts: imx-image: hab_sign: refactor function Marco Felsch
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Place the MMC/SD CSF data int slot-0 and the FlexSPI CSF data into
slot-1. This must be done since the MMC/SD and FlexSPI CSF data are
different.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx-image.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 01da4cc36467..e9eded7f0828 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -298,7 +298,7 @@ static int write_mem_v1(uint32_t addr, uint32_t val, int width, int set_bits, in
 
 static size_t
 add_header_v2(const struct config_data *data, void *buf, uint32_t offset,
-	      size_t header_len)
+	      size_t header_len, unsigned int csf_slot)
 {
 	struct imx_flash_header_v2 *hdr;
 	int dcdsize = curdcd * sizeof(uint32_t);
@@ -341,7 +341,7 @@ add_header_v2(const struct config_data *data, void *buf, uint32_t offset,
 		hdr->boot_data.size	= imagesize;
 
 	if (data->sign_image) {
-		hdr->csf = loadaddr + imagesize;
+		hdr->csf = loadaddr + imagesize + (csf_slot * CSF_LEN);
 		hdr->boot_data.size += CSF_LEN;
 	} else if (data->pbl_code_size && data->csf) {
 		/*
@@ -463,7 +463,7 @@ add_flexspi_header(const struct config_data *data, void **_buf, size_t *header_l
 	    data->cpu_type == IMX_CPU_IMX8MN)
 		buf += SZ_4K;
 
-	size += add_header_v2(data, buf, ivt_offset, len);
+	size += add_header_v2(data, buf, ivt_offset, len, 1);
 
 	*header_len += FLEXSPI_HEADER_LEN;
 
@@ -1018,7 +1018,7 @@ int main(int argc, char *argv[])
 		barebox_image_size += add_flexspi_header(&data, &buf, &header_len);
 		barebox_image_size += add_header_v2(&data, buf +
 						    signed_hdmi_firmware_size,
-						    data.image_ivt_offset, header_len);
+						    data.image_ivt_offset, header_len, 0);
 		break;
 	default:
 		fprintf(stderr, "Congratulations! You're welcome to implement header version %d\n",
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 20/26] scripts: imx-image: hab_sign: refactor function
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (18 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 19/26] scripts: imx-image: header_v2: add CSF slots Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command Marco Felsch
                   ` (5 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Refactor the code to make it possible to write multiple CSF blocks. This
is required for the upcoming FlexSPI CSF data fix.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx-image.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index e9eded7f0828..4372ff85067b 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -660,7 +660,8 @@ static int nop(const struct config_data *data)
  * The cst is expected to be executable as 'cst' or if exists, the content
  * of the environment variable 'CST' is used.
  */
-static int hab_sign(struct config_data *data)
+static int hab_sign(struct config_data *data, const char *csfcmds,
+		    unsigned int csf_slot)
 {
 	int fd, outfd, ret, lockfd;
 	char *csffile, *command;
@@ -674,7 +675,7 @@ static int hab_sign(struct config_data *data)
 	if (!cst)
 		cst = "cst";
 
-	ret = asprintf(&csffile, "%s.csfbin", data->outfile);
+	ret = asprintf(&csffile, "%s.slot%u.csfbin", data->outfile, csf_slot);
 	if (ret < 0)
 		exit(1);
 
@@ -743,7 +744,7 @@ static int hab_sign(struct config_data *data)
 		return -errno;
 	}
 
-	fwrite(data->csf, 1, strlen(data->csf) + 1, f);
+	fwrite(csfcmds, 1, strlen(csfcmds) + 1, f);
 
 	pclose(f);
 
@@ -814,6 +815,8 @@ static int hab_sign(struct config_data *data)
 		if (flexspi_image(data))
 			offset += FLEXSPI_HEADER_LEN;
 
+		offset += csf_slot * CSF_LEN;
+
 		offset = roundup(offset, 0x1000);
 		if (data->signed_hdmi_firmware_file)
 			offset += PLUGIN_HDMI_SIZE;
@@ -1104,7 +1107,7 @@ int main(int argc, char *argv[])
 	}
 
 	if (data.csf && data.sign_image) {
-		ret = hab_sign(&data);
+		ret = hab_sign(&data, data.csf, 0);
 		if (ret)
 			exit(1);
 	}
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (19 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 20/26] scripts: imx-image: hab_sign: refactor function Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-13  7:28   ` Sascha Hauer
  2023-10-10 14:33 ` [PATCH 22/26] scripts: imx: fix HAB for FlexSPI boot Marco Felsch
                   ` (4 subsequent siblings)
  25 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

The upcoming commit fixes the HAB support for FlexSPI enabled barebox
images. This commit prepares the source to keep the diff smaller.

For the upcoming fix we need a 2nd CSF command sequence which is
basically the same as the first except for the "Blocks = ...." command.
Therefore we need to handle the blocks command separately which is done
by this commit. At the moment there is no functional change.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index 933019eba449..acc8424e547d 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
 	return 0;
 }
 
+static int hab_add_barebox_blocks(struct config_data *data,
+				  const char *csf_str,
+				  const char *flexspi_csf_str)
+{
+	int len = strlen(csf_str);
+
+	if (data->csf_space < len)
+		return -ENOMEM;
+
+	strcat(data->csf, csf_str);
+	if (flexspi_csf_str)
+		strcat(data->flexspi_csf, flexspi_csf_str);
+
+	data->csf_space -= len;
+
+	return 0;
+}
+
 static int do_hab(struct config_data *data, int argc, char *argv[])
 {
 	int i, ret;
@@ -343,7 +361,7 @@ imx8m_get_offset_size(struct config_data *data,
 
 static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 {
-	char *str;
+	char *str, *flexspi_str = NULL;
 	int ret;
 	int i;
 	uint32_t signed_size = data->load_size;
@@ -378,7 +396,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	if (ret < 0)
 		return -ENOMEM;
 
-	ret = hab_add_str(data, str);
+	ret = hab_add_barebox_blocks(data, str, flexspi_str);
 	free(str);
 	if (ret)
 		return ret;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 22/26] scripts: imx: fix HAB for FlexSPI boot
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (20 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 23/26] scripts: imx-image: add missing close() Marco Felsch
                   ` (3 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

The HAB FlexSPI boot use-case is currently broken, albeit the BootROM
does the same steps as for MMC/SD. To fix this we need a 2nd CSF slot,
since the HAB ROM code expect the IVT and payload data to be signed by
the code signing tool (CST) and the MMC/SD IVT does differ slightly from
the FlexSPI IVT.

Please check the upcoming documentation patch for a in detail image
layout overview.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 include/mach/imx/imx-header.h |  1 +
 scripts/imx/imx-image.c       |  9 +++++++++
 scripts/imx/imx.c             | 38 ++++++++++++++++++++++++++++++++---
 3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/include/mach/imx/imx-header.h b/include/mach/imx/imx-header.h
index 0ad3485eb261..9e6b31ade462 100644
--- a/include/mach/imx/imx-header.h
+++ b/include/mach/imx/imx-header.h
@@ -117,6 +117,7 @@ struct config_data {
 	int (*nop)(const struct config_data *data);
 	int csf_space;
 	char *csf;
+	char *flexspi_csf;
 	int sign_image;
 	char *signed_hdmi_firmware_file;
 	int encrypt_image;
diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 4372ff85067b..8c4cc771ee28 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -343,6 +343,8 @@ add_header_v2(const struct config_data *data, void *buf, uint32_t offset,
 	if (data->sign_image) {
 		hdr->csf = loadaddr + imagesize + (csf_slot * CSF_LEN);
 		hdr->boot_data.size += CSF_LEN;
+		if (data->flexspi_csf)
+			hdr->boot_data.size += CSF_LEN;
 	} else if (data->pbl_code_size && data->csf) {
 		/*
 		 * For i.MX8 the CSF space is added via the linker script, so
@@ -350,6 +352,8 @@ add_header_v2(const struct config_data *data, void *buf, uint32_t offset,
 		 * signing is not.
 		 */
 		hdr->boot_data.size += CSF_LEN;
+		if (data->flexspi_csf)
+			hdr->boot_data.size += CSF_LEN;
 	}
 
 	buf += sizeof(*hdr);
@@ -1110,6 +1114,11 @@ int main(int argc, char *argv[])
 		ret = hab_sign(&data, data.csf, 0);
 		if (ret)
 			exit(1);
+		if (data.flexspi_csf) {
+			ret = hab_sign(&data, data.flexspi_csf, 1);
+			if (ret)
+				exit(1);
+		}
 	}
 
 	if (create_usb_image) {
diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index acc8424e547d..b5dd97491dc6 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -290,6 +290,8 @@ static int hab_add_str(struct config_data *data, const char *str)
 		return -ENOMEM;
 
 	strcat(data->csf, str);
+	if (flexspi_image(data))
+		strcat(data->flexspi_csf, str);
 
 	data->csf_space -= len;
 
@@ -324,6 +326,12 @@ static int do_hab(struct config_data *data, int argc, char *argv[])
 		data->csf = calloc(data->csf_space + 1, 1);
 		if (!data->csf)
 			return -ENOMEM;
+
+		if (flexspi_image(data)) {
+			data->flexspi_csf = calloc(data->csf_space + 1, 1);
+			if (!data->flexspi_csf)
+				return -ENOMEM;
+		}
 	}
 
 	for (i = 1; i < argc; i++) {
@@ -345,7 +353,8 @@ static int do_hab(struct config_data *data, int argc, char *argv[])
 
 static void
 imx8m_get_offset_size(struct config_data *data,
-		      uint32_t *offset, uint32_t *signed_length)
+		      uint32_t *offset, uint32_t *signed_size,
+		      uint32_t *flexspi_offset, uint32_t *flexspi_signed_size)
 {
 	unsigned int hdrlen = HEADER_LEN;
 
@@ -353,10 +362,22 @@ imx8m_get_offset_size(struct config_data *data,
 		hdrlen += FLEXSPI_HEADER_LEN;
 
 	*signed_size = roundup(data->pbl_code_size + hdrlen, 0x1000);
+	*flexspi_signed_size = roundup(data->pbl_code_size + FLEXSPI_HEADER_LEN,
+				       0x1000);
 
 	*offset += data->header_gap;
-	if (data->signed_hdmi_firmware_file)
+	*flexspi_offset += data->header_gap;
+	/*
+	 * Starting with i.MX8MP/N the FlexSPI IVT offset is 0x0 but the primary
+	 * image offset is at 0x1000.
+	 */
+	if (data->cpu_type != IMX_CPU_IMX8MM)
+		*flexspi_offset += HEADER_LEN;
+
+	if (data->signed_hdmi_firmware_file) {
 		*offset += PLUGIN_HDMI_SIZE;
+		*flexspi_offset += PLUGIN_HDMI_SIZE;
+	}
 }
 
 static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
@@ -365,7 +386,9 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	int ret;
 	int i;
 	uint32_t signed_size = data->load_size;
+	uint32_t flexspi_signed_size = signed_size;
 	uint32_t offset = data->image_ivt_offset;
+	uint32_t flexspi_offset = data->image_flexspi_ivt_offset;
 
 	if (!data->csf)
 		return -EINVAL;
@@ -381,12 +404,20 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 	 * Ensure we only sign the PBL for i.MX8MQ
 	 */
 	if (data->pbl_code_size && cpu_is_mx8m(data))
-		imx8m_get_offset_size(data, &offset, &signed_size)
+		imx8m_get_offset_size(data, &offset, &signed_size,
+				      &flexspi_offset, &flexspi_signed_size);
 
 	if (signed_size > 0) {
 		ret = asprintf(&str, "Blocks = 0x%08x 0x%08x 0x%08x \"%s\"",
 			data->image_load_addr + data->image_ivt_offset, offset,
 			signed_size - data->image_ivt_offset, data->outfile);
+		if (flexspi_image(data))
+			ret |= asprintf(&flexspi_str,
+					"Blocks = 0x%08x 0x%08x 0x%08x \"%s\"",
+					data->image_load_addr +
+					data->image_flexspi_ivt_offset,
+					flexspi_offset, flexspi_signed_size,
+					data->outfile);
 	} else {
 		fprintf(stderr, "Invalid signed size area 0x%08x\n",
 			signed_size);
@@ -398,6 +429,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
 
 	ret = hab_add_barebox_blocks(data, str, flexspi_str);
 	free(str);
+	free(flexspi_str);
 	if (ret)
 		return ret;
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 23/26] scripts: imx-image: add missing close()
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (21 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 22/26] scripts: imx: fix HAB for FlexSPI boot Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 24/26] scripts: imx-image: add support for CST_EXTRA_CMDLINE_OPTIONS Marco Felsch
                   ` (2 subsequent siblings)
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Close the csfbin file after we read it into buf.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx-image.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 8c4cc771ee28..4a0a44b3a9b3 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -796,6 +796,8 @@ static int hab_sign(struct config_data *data, const char *csfcmds,
 
 	xread(fd, buf, s.st_size);
 
+	close(fd);
+
 	/*
 	 * For i.MX8M, write into the reserved CSF section
 	 */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 24/26] scripts: imx-image: add support for CST_EXTRA_CMDLINE_OPTIONS
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (22 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 23/26] scripts: imx-image: add missing close() Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 25/26] Documentation: boards: imx: replace i.MX8MQ with i.MX8M Marco Felsch
  2023-10-10 14:33 ` [PATCH 26/26] Documentation: boards: imx: add FlexSPI sub-section Marco Felsch
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Add CST_EXTRA_CMDLINE_OPTIONS environment variable to allow build tools
like PTXdist or Yocto to pass additional cst command line options. This
becomes important for newer cst versions (> 3.3.1) since they support
multiple backends: ssl (default) and pkcs11.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/imx/imx-image.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/scripts/imx/imx-image.c b/scripts/imx/imx-image.c
index 4a0a44b3a9b3..8a5768ca8b62 100644
--- a/scripts/imx/imx-image.c
+++ b/scripts/imx/imx-image.c
@@ -670,7 +670,7 @@ static int hab_sign(struct config_data *data, const char *csfcmds,
 	int fd, outfd, ret, lockfd;
 	char *csffile, *command;
 	struct stat s;
-	char *cst;
+	char *cst, *cstopts;
 	void *buf;
 	size_t csf_space = CSF_LEN;
 	unsigned int offset = 0;
@@ -679,6 +679,10 @@ static int hab_sign(struct config_data *data, const char *csfcmds,
 	if (!cst)
 		cst = "cst";
 
+	cstopts = getenv("CST_EXTRA_CMDLINE_OPTIONS");
+	if (!cstopts)
+		cstopts = "";
+
 	ret = asprintf(&csffile, "%s.slot%u.csfbin", data->outfile, csf_slot);
 	if (ret < 0)
 		exit(1);
@@ -716,11 +720,11 @@ static int hab_sign(struct config_data *data, const char *csfcmds,
 	if (ret == -1)
 		return -EINVAL;
 	else if (ret == 0)
-		ret = asprintf(&command, "%s -o %s -i /dev/stdin",
-			       cst, csffile);
+		ret = asprintf(&command, "%s -o %s -i /dev/stdin %s",
+			       cst, csffile, cstopts);
 	else
-		ret = asprintf(&command, "%s -o %s;",
-			       cst, csffile);
+		ret = asprintf(&command, "%s -o %s %s;",
+			       cst, csffile, cstopts);
 	if (ret < 0)
 		return -ENOMEM;
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 25/26] Documentation: boards: imx: replace i.MX8MQ with i.MX8M
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (23 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 24/26] scripts: imx-image: add support for CST_EXTRA_CMDLINE_OPTIONS Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  2023-10-10 14:33 ` [PATCH 26/26] Documentation: boards: imx: add FlexSPI sub-section Marco Felsch
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Some parts of the documentation refer to the i.MX8MQ which are valid for
all i.MX8M SoCs. Also rename the i.MX8 to i.MX8M to make it clear which
family we meant.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 Documentation/boards/imx.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Documentation/boards/imx.rst b/Documentation/boards/imx.rst
index c64446aff640..a8f014dd1b91 100644
--- a/Documentation/boards/imx.rst
+++ b/Documentation/boards/imx.rst
@@ -21,7 +21,7 @@ The Internal Boot Mode is supported on:
 * i.MX53
 * i.MX6
 * i.MX7
-* i.MX8MQ
+* i.MX8M
 
 With the Internal Boot Mode, the images contain a header which describes
 where the binary shall be loaded and started. These headers also contain
@@ -64,8 +64,8 @@ of the image to the card, use:
 
   dd if=images/barebox-freescale-imx51-babbage.img of=/dev/sdd bs=1024 skip=1 seek=1
 
-Note that MaskROM on i.MX8 expects the image to start at the +33KiB mark, so the
-following command has to be used instead:
+Note that MaskROM on i.MX8M expects the image to start at the +33KiB mark, so
+the following command has to be used instead:
 
 .. code-block:: sh
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH 26/26] Documentation: boards: imx: add FlexSPI sub-section
  2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
                   ` (24 preceding siblings ...)
  2023-10-10 14:33 ` [PATCH 25/26] Documentation: boards: imx: replace i.MX8MQ with i.MX8M Marco Felsch
@ 2023-10-10 14:33 ` Marco Felsch
  25 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-10 14:33 UTC (permalink / raw)
  To: barebox

Document the FlexSPI image and boot feature to make it easier for users
to enable the support. Also document the image layout since it became
more complex with FlexSPI and HAB, so a detailed overview is very
helpful.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 Documentation/boards/imx.rst | 77 ++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/Documentation/boards/imx.rst b/Documentation/boards/imx.rst
index a8f014dd1b91..1b34c12cf4c6 100644
--- a/Documentation/boards/imx.rst
+++ b/Documentation/boards/imx.rst
@@ -394,6 +394,83 @@ with only the image name as argument:
 
   scripts/imx/imx-usb-loader images/barebox-freescale-imx51-babbage.img
 
+FlexSPI Boot
+^^^^^^^^^^^^
+
+FlexSPI boot is currently supported on: i.MX8MM, i.MX8MN and i.MX8MP.
+
+To generate FlexSPI/QSPI image(s) the board flash header file must specify:
+``flexspi_ivtofs`` and ``flexspi_fcfbofs``.
+
+It is recommended to do this via the ``include/mach/imx/flexspi-imx8m*-cfg.h``
+header files due to HAB boot. In this case ``CONFIG_SPI_NXP_FLEXSPI`` must be
+enabled too
+
+.. code-block:: none
+
+  #include <mach/imx/flexspi-imx8mp-cfg.h>
+
+There are two different headers, one for the i.MX8MM and one for the i.MX8MP/N.
+It's important to use the correct one because the BootROM expects the IVT and
+flash configuration block (FCB) on different offsets.
+
+Barebox doesn't generate a separate FlexSPI image instead the same image used
+for SD/MMC/USB is extended to support FlexSPI boot. This is done by adding a 2nd
+IVT header and the required FCB at the required boot offsets.
+
+The FlexSPI boot(-image) also supports `High Assurance Boot`_ if enabled. A fully
+featured image with FlexSPI and HAB support for an i.MX8MM looks like::
+
+                 0x0 +------------------------------------------+
+                     | Barebox Header                           |
+          header_gap +------------------------------------------+
+                     | FlexSPI Flash Configuration Block (FCFB) |
+  header_gap + 0x400 +------------------------------------------+                            ---
+                     | i.MX MMC/SD/USB IVT Header               |                             |
+                     | Boot Data                                +--.                          |
+                     | CSF Pointer                              +--|--.                       |
+ header_gap + 0x1000 +------------------------------------------+  |  |     ---               |
+                     | i.MX FlexSPI IVT Header                  |  |  |      |                | Signed Area
+                     | Boot Data                                +--+  |      |                |  MMC/SD/
+                     | CSF Pointer                              +--|--|--.   | Signed Area    |    USB
+ header_gap + 0x2000 +------------------------------------------+  |  |  |   |  FlexSPI       |
+                     | Barebox Prebootloader (PBL)              |<-´  |  |   |                |
+                     | Piggydata Hash (SHA256)                  |     |  |   |                |
+                     +------------------------------------------+     |  |  ---              ---
+                     | Command Sequence File (CSF) Slot-0       |<----´  |
+                     +------------------------------------------+        |
+                     | Command Sequence File (CSF) Slot-1       |<-------´
+                     +------------------------------------------+           ---
+                     | Piggydata (Main Barebox Binary)          |            | Hashed Area
+                     +------------------------------------------+           ---
+
+For i.MX8MP/N devices the image does differ slightly due to different offsets::
+
+                 0x0 +------------------------------------------+
+                     | Barebox Header                           |
+          header_gap +------------------------------------------+                            ---
+                     | i.MX MMC/SD/USB IVT Header               |                             |
+                     | Boot Data                                +--.                          |
+                     | CSF Pointer                              +--|--.                       |
+  header_gap + 0x400 +------------------------------------------+  |  |                       |
+                     | FlexSPI Flash Configuration Block (FCFB) |  |  |                       | Signed Area
+ header_gap + 0x1000 +------------------------------------------+  |  |     ---               |  MMC/SD/
+                     | i.MX FlexSPI IVT Header                  |  |  |      |                |    USB
+                     | Boot Data                                +--+  |      |                |
+                     | CSF Pointer                              +--|--|--.   | Signed Area    |
+ header_gap + 0x2000 +------------------------------------------+  |  |  |   |  FlexSPI       |
+                     | Barebox Prebootloader (PBL)              |<-´  |  |   |                |
+                     | Piggydata Hash (SHA256)                  |     |  |   |                |
+                     +------------------------------------------+     |  |  ---              ---
+                     | Command Sequence File (CSF) Slot-0       |<----´  |
+                     +------------------------------------------+        |
+                     | Command Sequence File (CSF) Slot-1       |<-------´
+                     +------------------------------------------+           ---
+                     | Piggydata (Main Barebox Binary)          |            | Hashed Area
+                     +------------------------------------------+           ---
+
+At the moment ``header_gap`` is always 32K for all supported devices.
+
 External Boot Mode
 ------------------
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* [PATCH] fixup! i.MX: HABv4: fix i.MX8MQ device lockdown
  2023-10-10 14:32 ` [PATCH 03/26] i.MX: HABv4: fix i.MX8MQ device lockdown Marco Felsch
@ 2023-10-10 18:35   ` Ahmad Fatoum
  2023-10-11  5:00     ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Ahmad Fatoum @ 2023-10-10 18:35 UTC (permalink / raw)
  To: barebox; +Cc: Marco Felsch, Ahmad Fatoum

Fixed up patch splits imx_hab_lockdown_device_ocotp into two
functions, one i.MX6- and another i.MX8M-specific, because they differ
in the fuse written: OCOTP_SEC_CONFIG_1 vs MX8M_OCOTP_SEC_CONFIG_1.

The same OCOTP_SEC_CONFIG_1 fuse is also read in
imx_hab_device_locked_down_ocotp, so split that two into imx6 and imx8m
variants.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/hab/hab.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/hab/hab.c b/drivers/hab/hab.c
index 444472511771..1edc495aee94 100644
--- a/drivers/hab/hab.c
+++ b/drivers/hab/hab.c
@@ -176,7 +176,7 @@ static int imx8m_hab_lockdown_device_ocotp(void)
 	return imx_ocotp_write_field(MX8MQ_OCOTP_DIR_BT_DIS, 1);
 }
 
-static int imx_hab_device_locked_down_ocotp(void)
+static int imx6_hab_device_locked_down_ocotp(void)
 {
 	int ret;
 	unsigned int v;
@@ -188,6 +188,18 @@ static int imx_hab_device_locked_down_ocotp(void)
 	return v;
 }
 
+static int imx8m_hab_device_locked_down_ocotp(void)
+{
+	int ret;
+	unsigned int v;
+
+	ret = imx_ocotp_read_field(MX8M_OCOTP_SEC_CONFIG_1, &v);
+	if (ret < 0)
+		return ret;
+
+	return v;
+}
+
 struct imx_hab_ops {
 	int (*write_srk_hash)(const u8 *srk, unsigned flags);
 	int (*read_srk_hash)(u8 *srk);
@@ -208,7 +220,7 @@ static struct imx_hab_ops imx6_hab_ops_ocotp = {
 	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
 	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
 	.lockdown_device = imx6_hab_lockdown_device_ocotp,
-	.device_locked_down = imx_hab_device_locked_down_ocotp,
+	.device_locked_down = imx6_hab_device_locked_down_ocotp,
 	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
 };
 
@@ -216,7 +228,7 @@ static struct imx_hab_ops imx8m_hab_ops_ocotp = {
 	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
 	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
 	.lockdown_device = imx8m_hab_lockdown_device_ocotp,
-	.device_locked_down = imx_hab_device_locked_down_ocotp,
+	.device_locked_down = imx8m_hab_device_locked_down_ocotp,
 	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
 };
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH] fixup! i.MX: HABv4: fix i.MX8MQ device lockdown
  2023-10-10 18:35   ` [PATCH] fixup! " Ahmad Fatoum
@ 2023-10-11  5:00     ` Marco Felsch
  0 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-11  5:00 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

Hi Ahmad,

On 23-10-10, Ahmad Fatoum wrote:
> Fixed up patch splits imx_hab_lockdown_device_ocotp into two
> functions, one i.MX6- and another i.MX8M-specific, because they differ
> in the fuse written: OCOTP_SEC_CONFIG_1 vs MX8M_OCOTP_SEC_CONFIG_1.
> 
> The same OCOTP_SEC_CONFIG_1 fuse is also read in
> imx_hab_device_locked_down_ocotp, so split that two into imx6 and imx8m
> variants.

good catch thanks a lot.

Regards,
  Marco

> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  drivers/hab/hab.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hab/hab.c b/drivers/hab/hab.c
> index 444472511771..1edc495aee94 100644
> --- a/drivers/hab/hab.c
> +++ b/drivers/hab/hab.c
> @@ -176,7 +176,7 @@ static int imx8m_hab_lockdown_device_ocotp(void)
>  	return imx_ocotp_write_field(MX8MQ_OCOTP_DIR_BT_DIS, 1);
>  }
>  
> -static int imx_hab_device_locked_down_ocotp(void)
> +static int imx6_hab_device_locked_down_ocotp(void)
>  {
>  	int ret;
>  	unsigned int v;
> @@ -188,6 +188,18 @@ static int imx_hab_device_locked_down_ocotp(void)
>  	return v;
>  }
>  
> +static int imx8m_hab_device_locked_down_ocotp(void)
> +{
> +	int ret;
> +	unsigned int v;
> +
> +	ret = imx_ocotp_read_field(MX8M_OCOTP_SEC_CONFIG_1, &v);
> +	if (ret < 0)
> +		return ret;
> +
> +	return v;
> +}
> +
>  struct imx_hab_ops {
>  	int (*write_srk_hash)(const u8 *srk, unsigned flags);
>  	int (*read_srk_hash)(u8 *srk);
> @@ -208,7 +220,7 @@ static struct imx_hab_ops imx6_hab_ops_ocotp = {
>  	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
>  	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
>  	.lockdown_device = imx6_hab_lockdown_device_ocotp,
> -	.device_locked_down = imx_hab_device_locked_down_ocotp,
> +	.device_locked_down = imx6_hab_device_locked_down_ocotp,
>  	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
>  };
>  
> @@ -216,7 +228,7 @@ static struct imx_hab_ops imx8m_hab_ops_ocotp = {
>  	.write_srk_hash = imx_hab_write_srk_hash_ocotp,
>  	.read_srk_hash =  imx_hab_read_srk_hash_ocotp,
>  	.lockdown_device = imx8m_hab_lockdown_device_ocotp,
> -	.device_locked_down = imx_hab_device_locked_down_ocotp,
> +	.device_locked_down = imx8m_hab_device_locked_down_ocotp,
>  	.permanent_write_enable = imx_hab_permanent_write_enable_ocotp,
>  };
>  
> -- 
> 2.39.2
> 
> 



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header
  2023-10-10 14:32 ` [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header Marco Felsch
@ 2023-10-13  6:27   ` Sascha Hauer
  2023-10-16  5:44     ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Sascha Hauer @ 2023-10-13  6:27 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Oct 10, 2023 at 04:32:59PM +0200, Marco Felsch wrote:
> Add dedicated configuration headers which guard the flexspi_* options
> like it is done for the HABV4 configuration block to be more
> user-friendly.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
>  include/mach/imx/flexspi-imx8mm-cfg.h | 10 ++++++++++
>  include/mach/imx/flexspi-imx8mp-cfg.h | 10 ++++++++++
>  2 files changed, 20 insertions(+)
>  create mode 100644 include/mach/imx/flexspi-imx8mm-cfg.h
>  create mode 100644 include/mach/imx/flexspi-imx8mp-cfg.h
> 
> diff --git a/include/mach/imx/flexspi-imx8mm-cfg.h b/include/mach/imx/flexspi-imx8mm-cfg.h
> new file mode 100644
> index 000000000000..f7f0310a57f4
> --- /dev/null
> +++ b/include/mach/imx/flexspi-imx8mm-cfg.h
> @@ -0,0 +1,10 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +
> +/*
> + * This snippet can be included from a i.MX flash header configuration
> + * file for generating FlexSPI compatible images.
> + */
> +#if defined(CONFIG_SPI_NXP_FLEXSPI)
> +flexspi_ivtofs 0x1000
> +flexspi_fcfbofs 0x0
> +#endif

What's this #ifdef good for?

CONFIG_SPI_NXP_FLEXSPI enables the flexspi driver, but this is not
necessary to boot from it, right?

Sascha

> diff --git a/include/mach/imx/flexspi-imx8mp-cfg.h b/include/mach/imx/flexspi-imx8mp-cfg.h
> new file mode 100644
> index 000000000000..bbc061241748
> --- /dev/null
> +++ b/include/mach/imx/flexspi-imx8mp-cfg.h
> @@ -0,0 +1,10 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +
> +/*
> + * This snippet can be included from a i.MX flash header configuration
> + * file for generating FlexSPI compatible images.
> + */
> +#if defined(CONFIG_SPI_NXP_FLEXSPI)
> +flexspi_ivtofs 0x0
> +flexspi_fcfbofs 0x400
> +#endif
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 14/26] ARM: lds: add support for a 2nd CSF area
  2023-10-10 14:33 ` [PATCH 14/26] ARM: lds: add support for a 2nd CSF area Marco Felsch
@ 2023-10-13  6:37   ` Sascha Hauer
  2023-10-16  5:45     ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Sascha Hauer @ 2023-10-13  6:37 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Oct 10, 2023 at 04:33:02PM +0200, Marco Felsch wrote:
> Extend the CSF area to be able two store two CSF slots. The 2nd CSF slot
> is required in case of FlexSPI HAB boot support. The first slot is used
> for the MMC-IVT + barebox-pbl; the 2nd slot is used for the FlexSPI-IVT
> + barebox-pbl. This is necessary because of the fact that the HAB ROM
> code requires that the IVT is signed as well.
> 
> Use the CONFIG_SPI_NXP_FLEXSPI Kconfig symbol to decide if we need to
> add the support for FlexSPI image signatures as well since early SRAM
> space is limited and 4K can make a difference here.
> 
> Please check the documentation for a more detailed information about the
> i.MX8M image layout.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
>  arch/arm/lib/pbl.lds.S | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
> index 898d6be33b54..9ab6d22eb59d 100644
> --- a/arch/arm/lib/pbl.lds.S
> +++ b/arch/arm/lib/pbl.lds.S
> @@ -12,7 +12,11 @@
>  #define BASE	(TEXT_BASE - SZ_2M)
>  #endif
>  
> +#ifdef CONFIG_SPI_NXP_FLEXSPI
> +#define HAB_CSF_LEN	0x4000
> +#else
>  #define HAB_CSF_LEN	0x2000
> +#endif

No. Enabling/Disabling the FlexSPI driver shouldn't influence the image
layout.

Having the driver enabled doesn't necessarily mean you want to use it
for booting. Having the driver disabled doesn't necessarily mean you are
*not* booting from it.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-10 14:33 ` [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command Marco Felsch
@ 2023-10-13  7:28   ` Sascha Hauer
  2023-10-16  5:50     ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Sascha Hauer @ 2023-10-13  7:28 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Oct 10, 2023 at 04:33:09PM +0200, Marco Felsch wrote:
> The upcoming commit fixes the HAB support for FlexSPI enabled barebox
> images. This commit prepares the source to keep the diff smaller.
> 
> For the upcoming fix we need a 2nd CSF command sequence which is
> basically the same as the first except for the "Blocks = ...." command.
> Therefore we need to handle the blocks command separately which is done
> by this commit. At the moment there is no functional change.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
>  scripts/imx/imx.c | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
> index 933019eba449..acc8424e547d 100644
> --- a/scripts/imx/imx.c
> +++ b/scripts/imx/imx.c
> @@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
>  	return 0;
>  }
>  
> +static int hab_add_barebox_blocks(struct config_data *data,
> +				  const char *csf_str,
> +				  const char *flexspi_csf_str)
> +{
> +	int len = strlen(csf_str);
> +
> +	if (data->csf_space < len)
> +		return -ENOMEM;
> +
> +	strcat(data->csf, csf_str);
> +	if (flexspi_csf_str)
> +		strcat(data->flexspi_csf, flexspi_csf_str);

Do we need a space check here as well?

Sascha

> +
> +	data->csf_space -= len;
> +
> +	return 0;
> +}
> +
>  static int do_hab(struct config_data *data, int argc, char *argv[])
>  {
>  	int i, ret;
> @@ -343,7 +361,7 @@ imx8m_get_offset_size(struct config_data *data,
>  
>  static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
>  {
> -	char *str;
> +	char *str, *flexspi_str = NULL;
>  	int ret;
>  	int i;
>  	uint32_t signed_size = data->load_size;
> @@ -378,7 +396,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
>  	if (ret < 0)
>  		return -ENOMEM;
>  
> -	ret = hab_add_str(data, str);
> +	ret = hab_add_barebox_blocks(data, str, flexspi_str);
>  	free(str);
>  	if (ret)
>  		return ret;
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header
  2023-10-13  6:27   ` Sascha Hauer
@ 2023-10-16  5:44     ` Marco Felsch
  0 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-16  5:44 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 23-10-13, Sascha Hauer wrote:
> On Tue, Oct 10, 2023 at 04:32:59PM +0200, Marco Felsch wrote:
> > Add dedicated configuration headers which guard the flexspi_* options
> > like it is done for the HABV4 configuration block to be more
> > user-friendly.
> > 
> > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > ---
> >  include/mach/imx/flexspi-imx8mm-cfg.h | 10 ++++++++++
> >  include/mach/imx/flexspi-imx8mp-cfg.h | 10 ++++++++++
> >  2 files changed, 20 insertions(+)
> >  create mode 100644 include/mach/imx/flexspi-imx8mm-cfg.h
> >  create mode 100644 include/mach/imx/flexspi-imx8mp-cfg.h
> > 
> > diff --git a/include/mach/imx/flexspi-imx8mm-cfg.h b/include/mach/imx/flexspi-imx8mm-cfg.h
> > new file mode 100644
> > index 000000000000..f7f0310a57f4
> > --- /dev/null
> > +++ b/include/mach/imx/flexspi-imx8mm-cfg.h
> > @@ -0,0 +1,10 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +
> > +/*
> > + * This snippet can be included from a i.MX flash header configuration
> > + * file for generating FlexSPI compatible images.
> > + */
> > +#if defined(CONFIG_SPI_NXP_FLEXSPI)
> > +flexspi_ivtofs 0x1000
> > +flexspi_fcfbofs 0x0
> > +#endif
> 
> What's this #ifdef good for?
> 
> CONFIG_SPI_NXP_FLEXSPI enables the flexspi driver, but this is not
> necessary to boot from it, right?

The flexspi_* options are used to decide if the barebox image should be
flexspi compatible. It also influence the HAB CSF generation since it
adds a 2nd CSF slot depending if enabled.

We can drop this guard as you already NACKed to use the
CONFIG_SPI_NXP_FLEXSPI for HAB decisions.

Regards,
  Marco

> 
> Sascha
> 
> > diff --git a/include/mach/imx/flexspi-imx8mp-cfg.h b/include/mach/imx/flexspi-imx8mp-cfg.h
> > new file mode 100644
> > index 000000000000..bbc061241748
> > --- /dev/null
> > +++ b/include/mach/imx/flexspi-imx8mp-cfg.h
> > @@ -0,0 +1,10 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +
> > +/*
> > + * This snippet can be included from a i.MX flash header configuration
> > + * file for generating FlexSPI compatible images.
> > + */
> > +#if defined(CONFIG_SPI_NXP_FLEXSPI)
> > +flexspi_ivtofs 0x0
> > +flexspi_fcfbofs 0x400
> > +#endif
> > -- 
> > 2.39.2
> > 
> > 
> > 
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 14/26] ARM: lds: add support for a 2nd CSF area
  2023-10-13  6:37   ` Sascha Hauer
@ 2023-10-16  5:45     ` Marco Felsch
  0 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-16  5:45 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 23-10-13, Sascha Hauer wrote:
> On Tue, Oct 10, 2023 at 04:33:02PM +0200, Marco Felsch wrote:
> > Extend the CSF area to be able two store two CSF slots. The 2nd CSF slot
> > is required in case of FlexSPI HAB boot support. The first slot is used
> > for the MMC-IVT + barebox-pbl; the 2nd slot is used for the FlexSPI-IVT
> > + barebox-pbl. This is necessary because of the fact that the HAB ROM
> > code requires that the IVT is signed as well.
> > 
> > Use the CONFIG_SPI_NXP_FLEXSPI Kconfig symbol to decide if we need to
> > add the support for FlexSPI image signatures as well since early SRAM
> > space is limited and 4K can make a difference here.
> > 
> > Please check the documentation for a more detailed information about the
> > i.MX8M image layout.
> > 
> > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > ---
> >  arch/arm/lib/pbl.lds.S | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
> > index 898d6be33b54..9ab6d22eb59d 100644
> > --- a/arch/arm/lib/pbl.lds.S
> > +++ b/arch/arm/lib/pbl.lds.S
> > @@ -12,7 +12,11 @@
> >  #define BASE	(TEXT_BASE - SZ_2M)
> >  #endif
> >  
> > +#ifdef CONFIG_SPI_NXP_FLEXSPI
> > +#define HAB_CSF_LEN	0x4000
> > +#else
> >  #define HAB_CSF_LEN	0x2000
> > +#endif
> 
> No. Enabling/Disabling the FlexSPI driver shouldn't influence the image
> layout.
> 
> Having the driver enabled doesn't necessarily mean you want to use it
> for booting. Having the driver disabled doesn't necessarily mean you are
> *not* booting from it.

You're right, it's a bit to simple. I will rework that.

Regards,
  Marco


> 
> Sascha
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-13  7:28   ` Sascha Hauer
@ 2023-10-16  5:50     ` Marco Felsch
  2023-10-16  7:44       ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-16  5:50 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 23-10-13, Sascha Hauer wrote:
> On Tue, Oct 10, 2023 at 04:33:09PM +0200, Marco Felsch wrote:
> > The upcoming commit fixes the HAB support for FlexSPI enabled barebox
> > images. This commit prepares the source to keep the diff smaller.
> > 
> > For the upcoming fix we need a 2nd CSF command sequence which is
> > basically the same as the first except for the "Blocks = ...." command.
> > Therefore we need to handle the blocks command separately which is done
> > by this commit. At the moment there is no functional change.
> > 
> > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > ---
> >  scripts/imx/imx.c | 22 ++++++++++++++++++++--
> >  1 file changed, 20 insertions(+), 2 deletions(-)
> > 
> > diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
> > index 933019eba449..acc8424e547d 100644
> > --- a/scripts/imx/imx.c
> > +++ b/scripts/imx/imx.c
> > @@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
> >  	return 0;
> >  }
> >  
> > +static int hab_add_barebox_blocks(struct config_data *data,
> > +				  const char *csf_str,
> > +				  const char *flexspi_csf_str)
> > +{
> > +	int len = strlen(csf_str);
> > +
> > +	if (data->csf_space < len)
> > +		return -ENOMEM;
> > +
> > +	strcat(data->csf, csf_str);
> > +	if (flexspi_csf_str)
> > +		strcat(data->flexspi_csf, flexspi_csf_str);
> 
> Do we need a space check here as well?

To be 100% accurate yes since the strings can have different lengths
albeit the difference would be <5 chars. I will rework that, thanks for
the review.

Regards,
  Marco

> 
> Sascha
> 
> > +
> > +	data->csf_space -= len;
> > +
> > +	return 0;
> > +}
> > +
> >  static int do_hab(struct config_data *data, int argc, char *argv[])
> >  {
> >  	int i, ret;
> > @@ -343,7 +361,7 @@ imx8m_get_offset_size(struct config_data *data,
> >  
> >  static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
> >  {
> > -	char *str;
> > +	char *str, *flexspi_str = NULL;
> >  	int ret;
> >  	int i;
> >  	uint32_t signed_size = data->load_size;
> > @@ -378,7 +396,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
> >  	if (ret < 0)
> >  		return -ENOMEM;
> >  
> > -	ret = hab_add_str(data, str);
> > +	ret = hab_add_barebox_blocks(data, str, flexspi_str);
> >  	free(str);
> >  	if (ret)
> >  		return ret;
> > -- 
> > 2.39.2
> > 
> > 
> > 
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-16  5:50     ` Marco Felsch
@ 2023-10-16  7:44       ` Marco Felsch
  2023-10-16  8:19         ` Sascha Hauer
  0 siblings, 1 reply; 38+ messages in thread
From: Marco Felsch @ 2023-10-16  7:44 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

Hi Sascha,

On 23-10-16, Marco Felsch wrote:
> On 23-10-13, Sascha Hauer wrote:
> > On Tue, Oct 10, 2023 at 04:33:09PM +0200, Marco Felsch wrote:
> > > The upcoming commit fixes the HAB support for FlexSPI enabled barebox
> > > images. This commit prepares the source to keep the diff smaller.
> > > 
> > > For the upcoming fix we need a 2nd CSF command sequence which is
> > > basically the same as the first except for the "Blocks = ...." command.
> > > Therefore we need to handle the blocks command separately which is done
> > > by this commit. At the moment there is no functional change.
> > > 
> > > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > > ---
> > >  scripts/imx/imx.c | 22 ++++++++++++++++++++--
> > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
> > > index 933019eba449..acc8424e547d 100644
> > > --- a/scripts/imx/imx.c
> > > +++ b/scripts/imx/imx.c
> > > @@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
> > >  	return 0;
> > >  }
> > >  
> > > +static int hab_add_barebox_blocks(struct config_data *data,
> > > +				  const char *csf_str,
> > > +				  const char *flexspi_csf_str)
> > > +{
> > > +	int len = strlen(csf_str);
> > > +
> > > +	if (data->csf_space < len)
> > > +		return -ENOMEM;
> > > +
> > > +	strcat(data->csf, csf_str);
> > > +	if (flexspi_csf_str)
> > > +		strcat(data->flexspi_csf, flexspi_csf_str);
> > 
> > Do we need a space check here as well?
> 
> To be 100% accurate yes since the strings can have different lengths
> albeit the difference would be <5 chars. I will rework that, thanks for
> the review.

While integrating your review feedback I noticed that my comment was
wrong. Both strings using the same size limited format string:

   "Blocks = 0x%08x 0x%08x 0x%08x \"%s\""

with "%s" always point to data->outfile. So both stings do not differ
and checking csf_str in enough.

Regards,
  Marco

> 
> Regards,
>   Marco
> 
> > 
> > Sascha
> > 
> > > +
> > > +	data->csf_space -= len;
> > > +
> > > +	return 0;
> > > +}
> > > +
> > >  static int do_hab(struct config_data *data, int argc, char *argv[])
> > >  {
> > >  	int i, ret;
> > > @@ -343,7 +361,7 @@ imx8m_get_offset_size(struct config_data *data,
> > >  
> > >  static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
> > >  {
> > > -	char *str;
> > > +	char *str, *flexspi_str = NULL;
> > >  	int ret;
> > >  	int i;
> > >  	uint32_t signed_size = data->load_size;
> > > @@ -378,7 +396,7 @@ static int do_hab_blocks(struct config_data *data, int argc, char *argv[])
> > >  	if (ret < 0)
> > >  		return -ENOMEM;
> > >  
> > > -	ret = hab_add_str(data, str);
> > > +	ret = hab_add_barebox_blocks(data, str, flexspi_str);
> > >  	free(str);
> > >  	if (ret)
> > >  		return ret;
> > > -- 
> > > 2.39.2
> > > 
> > > 
> > > 
> > 
> > -- 
> > Pengutronix e.K.                           |                             |
> > Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> > 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> > Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> > 
> 
> 



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-16  7:44       ` Marco Felsch
@ 2023-10-16  8:19         ` Sascha Hauer
  2023-10-17 14:58           ` Marco Felsch
  0 siblings, 1 reply; 38+ messages in thread
From: Sascha Hauer @ 2023-10-16  8:19 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Mon, Oct 16, 2023 at 09:44:50AM +0200, Marco Felsch wrote:
> Hi Sascha,
> 
> On 23-10-16, Marco Felsch wrote:
> > On 23-10-13, Sascha Hauer wrote:
> > > On Tue, Oct 10, 2023 at 04:33:09PM +0200, Marco Felsch wrote:
> > > > The upcoming commit fixes the HAB support for FlexSPI enabled barebox
> > > > images. This commit prepares the source to keep the diff smaller.
> > > > 
> > > > For the upcoming fix we need a 2nd CSF command sequence which is
> > > > basically the same as the first except for the "Blocks = ...." command.
> > > > Therefore we need to handle the blocks command separately which is done
> > > > by this commit. At the moment there is no functional change.
> > > > 
> > > > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > > > ---
> > > >  scripts/imx/imx.c | 22 ++++++++++++++++++++--
> > > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > > 
> > > > diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
> > > > index 933019eba449..acc8424e547d 100644
> > > > --- a/scripts/imx/imx.c
> > > > +++ b/scripts/imx/imx.c
> > > > @@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
> > > >  	return 0;
> > > >  }
> > > >  
> > > > +static int hab_add_barebox_blocks(struct config_data *data,
> > > > +				  const char *csf_str,
> > > > +				  const char *flexspi_csf_str)
> > > > +{
> > > > +	int len = strlen(csf_str);
> > > > +
> > > > +	if (data->csf_space < len)
> > > > +		return -ENOMEM;
> > > > +
> > > > +	strcat(data->csf, csf_str);
> > > > +	if (flexspi_csf_str)
> > > > +		strcat(data->flexspi_csf, flexspi_csf_str);
> > > 
> > > Do we need a space check here as well?
> > 
> > To be 100% accurate yes since the strings can have different lengths
> > albeit the difference would be <5 chars. I will rework that, thanks for
> > the review.
> 
> While integrating your review feedback I noticed that my comment was
> wrong. Both strings using the same size limited format string:
> 
>    "Blocks = 0x%08x 0x%08x 0x%08x \"%s\""
> 
> with "%s" always point to data->outfile. So both stings do not differ
> and checking csf_str in enough.

Indeed, so the current way is safe.

However, I wonder if we should rather add a little helper like:

static void *strcata(char *str, const char *add)
{
	str = realloc(str, (str ? strlen(str) : 0) + strlen(add) + 1);
	if (!str)
		return NULL;

	strcat(str, add);

	return str;
}

and get rid oof the fixed size allocation altogether.

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command
  2023-10-16  8:19         ` Sascha Hauer
@ 2023-10-17 14:58           ` Marco Felsch
  0 siblings, 0 replies; 38+ messages in thread
From: Marco Felsch @ 2023-10-17 14:58 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 23-10-16, Sascha Hauer wrote:
> On Mon, Oct 16, 2023 at 09:44:50AM +0200, Marco Felsch wrote:
> > Hi Sascha,
> > 
> > On 23-10-16, Marco Felsch wrote:
> > > On 23-10-13, Sascha Hauer wrote:
> > > > On Tue, Oct 10, 2023 at 04:33:09PM +0200, Marco Felsch wrote:
> > > > > The upcoming commit fixes the HAB support for FlexSPI enabled barebox
> > > > > images. This commit prepares the source to keep the diff smaller.
> > > > > 
> > > > > For the upcoming fix we need a 2nd CSF command sequence which is
> > > > > basically the same as the first except for the "Blocks = ...." command.
> > > > > Therefore we need to handle the blocks command separately which is done
> > > > > by this commit. At the moment there is no functional change.
> > > > > 
> > > > > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > > > > ---
> > > > >  scripts/imx/imx.c | 22 ++++++++++++++++++++--
> > > > >  1 file changed, 20 insertions(+), 2 deletions(-)
> > > > > 
> > > > > diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
> > > > > index 933019eba449..acc8424e547d 100644
> > > > > --- a/scripts/imx/imx.c
> > > > > +++ b/scripts/imx/imx.c
> > > > > @@ -296,6 +296,24 @@ static int hab_add_str(struct config_data *data, const char *str)
> > > > >  	return 0;
> > > > >  }
> > > > >  
> > > > > +static int hab_add_barebox_blocks(struct config_data *data,
> > > > > +				  const char *csf_str,
> > > > > +				  const char *flexspi_csf_str)
> > > > > +{
> > > > > +	int len = strlen(csf_str);
> > > > > +
> > > > > +	if (data->csf_space < len)
> > > > > +		return -ENOMEM;
> > > > > +
> > > > > +	strcat(data->csf, csf_str);
> > > > > +	if (flexspi_csf_str)
> > > > > +		strcat(data->flexspi_csf, flexspi_csf_str);
> > > > 
> > > > Do we need a space check here as well?
> > > 
> > > To be 100% accurate yes since the strings can have different lengths
> > > albeit the difference would be <5 chars. I will rework that, thanks for
> > > the review.
> > 
> > While integrating your review feedback I noticed that my comment was
> > wrong. Both strings using the same size limited format string:
> > 
> >    "Blocks = 0x%08x 0x%08x 0x%08x \"%s\""
> > 
> > with "%s" always point to data->outfile. So both stings do not differ
> > and checking csf_str in enough.
> 
> Indeed, so the current way is safe.
> 
> However, I wonder if we should rather add a little helper like:
> 
> static void *strcata(char *str, const char *add)
> {
> 	str = realloc(str, (str ? strlen(str) : 0) + strlen(add) + 1);
> 	if (!str)
> 		return NULL;
> 
> 	strcat(str, add);
> 
> 	return str;
> }
> 
> and get rid oof the fixed size allocation altogether.

FTR: I followed your suggestion, please see the v2.

Regards,
  Marco



^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2023-10-17 14:59 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-10 14:32 [PATCH 00/26] i.MX8M HAB Fixes Marco Felsch
2023-10-10 14:32 ` [PATCH 01/26] kbuild: clean start*_*.{p,ps}imximg files Marco Felsch
2023-10-10 14:32 ` [PATCH 02/26] i.MX: HABv4: fix event status comparison Marco Felsch
2023-10-10 14:32 ` [PATCH 03/26] i.MX: HABv4: fix i.MX8MQ device lockdown Marco Felsch
2023-10-10 18:35   ` [PATCH] fixup! " Ahmad Fatoum
2023-10-11  5:00     ` Marco Felsch
2023-10-10 14:32 ` [PATCH 04/26] i.MX: HAB: remove unused init() hook Marco Felsch
2023-10-10 14:32 ` [PATCH 05/26] i.MX: HABv4: extend support to i.MX8M SoCs Marco Felsch
2023-10-10 14:32 ` [PATCH 06/26] i.MX: HABv4: retrieve HAB ROM version for i.MX8M Marco Felsch
2023-10-10 14:32 ` [PATCH 07/26] ARM: i.MX: allow HAB for i.MX8M family Marco Felsch
2023-10-10 14:32 ` [PATCH 08/26] ARM: i.MX: introduce build_imx8m_habv4img Marco Felsch
2023-10-10 14:32 ` [PATCH 09/26] ARM: i.MX: convert i.MX8M to helper Marco Felsch
2023-10-10 14:32 ` [PATCH 10/26] ARM: boards: i.MX8M: add HAB image support Marco Felsch
2023-10-10 14:32 ` [PATCH 11/26] ARM: i.MX8M: add flexspi-imx8m{m,p}-cfg.h header Marco Felsch
2023-10-13  6:27   ` Sascha Hauer
2023-10-16  5:44     ` Marco Felsch
2023-10-10 14:33 ` [PATCH 12/26] ARM: boards: i.MX8M: make use of new " Marco Felsch
2023-10-10 14:33 ` [PATCH 13/26] ARM: lds: introduce HAB_CSF_LEN define for the hab_csf section Marco Felsch
2023-10-10 14:33 ` [PATCH 14/26] ARM: lds: add support for a 2nd CSF area Marco Felsch
2023-10-13  6:37   ` Sascha Hauer
2023-10-16  5:45     ` Marco Felsch
2023-10-10 14:33 ` [PATCH 15/26] scripts: imx: force flexspi and hab option order Marco Felsch
2023-10-10 14:33 ` [PATCH 16/26] scripts: imx: move flexspi_image() into header Marco Felsch
2023-10-10 14:33 ` [PATCH 17/26] scripts: imx: fix i.MX8M CSF header placement Marco Felsch
2023-10-10 14:33 ` [PATCH 18/26] scripts: imx: add imx8m_get_offset_size helper Marco Felsch
2023-10-10 14:33 ` [PATCH 19/26] scripts: imx-image: header_v2: add CSF slots Marco Felsch
2023-10-10 14:33 ` [PATCH 20/26] scripts: imx-image: hab_sign: refactor function Marco Felsch
2023-10-10 14:33 ` [PATCH 21/26] scripts: imx: add helper to write CSF Blocks command Marco Felsch
2023-10-13  7:28   ` Sascha Hauer
2023-10-16  5:50     ` Marco Felsch
2023-10-16  7:44       ` Marco Felsch
2023-10-16  8:19         ` Sascha Hauer
2023-10-17 14:58           ` Marco Felsch
2023-10-10 14:33 ` [PATCH 22/26] scripts: imx: fix HAB for FlexSPI boot Marco Felsch
2023-10-10 14:33 ` [PATCH 23/26] scripts: imx-image: add missing close() Marco Felsch
2023-10-10 14:33 ` [PATCH 24/26] scripts: imx-image: add support for CST_EXTRA_CMDLINE_OPTIONS Marco Felsch
2023-10-10 14:33 ` [PATCH 25/26] Documentation: boards: imx: replace i.MX8MQ with i.MX8M Marco Felsch
2023-10-10 14:33 ` [PATCH 26/26] Documentation: boards: imx: add FlexSPI sub-section Marco Felsch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox