mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image
@ 2020-01-07 10:25 Lucas Stach
  2020-01-07 10:25 ` [PATCH 2/3] ARM: nxp-imx8mq-evk: fix second stage booting Lucas Stach
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Lucas Stach @ 2020-01-07 10:25 UTC (permalink / raw)
  To: barebox

Add an option that allows to just load the image into memory, but
return to the calling function instead of directly jumping to the
loaded image.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 arch/arm/boards/phytec-som-imx8mq/lowlevel.c |  2 +-
 arch/arm/boards/zii-imx8mq-dev/lowlevel.c    |  2 +-
 arch/arm/mach-imx/include/mach/xload.h       |  2 +-
 drivers/mci/imx-esdhc-pbl.c                  | 22 ++++++++++++--------
 4 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/arch/arm/boards/phytec-som-imx8mq/lowlevel.c b/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
index 4e52b92ad305..4cacabb1fbfc 100644
--- a/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
+++ b/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
@@ -56,7 +56,7 @@ static void phytec_imx8mq_som_sram_setup(void)
 	imx8_get_boot_source(&src, &instance);
 
 	if (src == BOOTSOURCE_MMC)
-		ret = imx8_esdhc_start_image(instance);
+		ret = imx8_esdhc_load_image(instance, true);
 
 	BUG_ON(ret);
 }
diff --git a/arch/arm/boards/zii-imx8mq-dev/lowlevel.c b/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
index 795c98cb660e..f12d79ee6eee 100644
--- a/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
+++ b/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
@@ -78,7 +78,7 @@ static void zii_imx8mq_dev_sram_setup(void)
 	imx8_get_boot_source(&src, &instance);
 
 	if (src == BOOTSOURCE_MMC)
-		ret = imx8_esdhc_start_image(instance);
+		ret = imx8_esdhc_load_image(instance, true);
 
 	BUG_ON(ret);
 }
diff --git a/arch/arm/mach-imx/include/mach/xload.h b/arch/arm/mach-imx/include/mach/xload.h
index a605e76339c8..a9b9d93f246f 100644
--- a/arch/arm/mach-imx/include/mach/xload.h
+++ b/arch/arm/mach-imx/include/mach/xload.h
@@ -5,7 +5,7 @@ int imx53_nand_start_image(void);
 int imx6_spi_load_image(int instance, unsigned int flash_offset, void *buf, int len);
 int imx6_spi_start_image(int instance);
 int imx6_esdhc_start_image(int instance);
-int imx8_esdhc_start_image(int instance);
+int imx8_esdhc_load_image(int instance, bool start);
 int imx8_esdhc_load_piggy(int instance);
 
 int imx_image_size(void);
diff --git a/drivers/mci/imx-esdhc-pbl.c b/drivers/mci/imx-esdhc-pbl.c
index c0d27fb7eb35..db4116fa9315 100644
--- a/drivers/mci/imx-esdhc-pbl.c
+++ b/drivers/mci/imx-esdhc-pbl.c
@@ -121,8 +121,8 @@ static int esdhc_search_header(struct fsl_esdhc_host *host,
 }
 
 static int
-esdhc_start_image(struct fsl_esdhc_host *host, ptrdiff_t address, ptrdiff_t entry,
-		  u32 offset)
+esdhc_load_image(struct fsl_esdhc_host *esdhc, ptrdiff_t address,
+		 ptrdiff_t entry, u32 offset, bool start)
 {
 
 	void *buf = (void *)address;
@@ -177,6 +177,9 @@ esdhc_start_image(struct fsl_esdhc_host *host, ptrdiff_t address, ptrdiff_t entr
 
 	pr_debug("Image loaded successfully\n");
 
+	if (!start)
+		return 0;
+
 	bb = buf + ofs;
 
 	sync_caches_for_execution();
@@ -254,22 +257,23 @@ int imx6_esdhc_start_image(int instance)
 
 	imx_esdhc_init(&host, &data);
 
-	return esdhc_start_image(&host, 0x10000000, 0x10000000, 0);
+	return esdhc_load_image(&host, 0x10000000, 0x10000000, 0, true);
 }
 
 /**
- * imx8_esdhc_start_image - Load and start an image from USDHC controller
+ * imx8_esdhc_load_image - Load and optionally start an image from USDHC controller
  * @instance: The USDHC controller instance (0..2)
+ * @start: Whether to directly start the loaded image
  *
  * This uses esdhc_start_image() to load an image from SD/MMC.  It is
  * assumed that the image is the currently running barebox image (This
  * information is used to calculate the length of the image). The
  * image is started afterwards.
  *
- * Return: If successful, this function does not return. A negative error
- * code is returned when this function fails.
+ * Return: If successful, this function does not return (if directly started)
+ * or 0. A negative error code is returned when this function fails.
  */
-int imx8_esdhc_start_image(int instance)
+int imx8_esdhc_load_image(int instance, bool start)
 {
 	struct esdhc_soc_data data;
 	struct fsl_esdhc_host host;
@@ -279,8 +283,8 @@ int imx8_esdhc_start_image(int instance)
 	if (ret)
 		return ret;
 
-	return esdhc_start_image(&host, MX8MQ_DDR_CSD1_BASE_ADDR,
-				 MX8MQ_ATF_BL33_BASE_ADDR, SZ_32K);
+	return esdhc_load_image(&host, MX8MQ_DDR_CSD1_BASE_ADDR,
+				MX8MQ_ATF_BL33_BASE_ADDR, SZ_32K, start);
 }
 
 int imx8_esdhc_load_piggy(int instance)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH 2/3] ARM: nxp-imx8mq-evk: fix second stage booting
  2020-01-07 10:25 [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Lucas Stach
@ 2020-01-07 10:25 ` Lucas Stach
  2020-01-07 10:25 ` [PATCH 3/3] esdhc-pbl: remove now unused imx8_esdhc_load_piggy Lucas Stach
  2020-01-08 11:41 ` [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Lucas Stach @ 2020-01-07 10:25 UTC (permalink / raw)
  To: barebox

If the whole image already resides in DRAM, e.g. by starting the
image via the bootm handler we try to load the piggydata from storage,
which may well be different from our expected piggydata, already
present in DRAM.

Fix this by avoiding the special piggydata load function, but instead
load the whole image after DRAM is up and just replace the PBL part to
ensure we are still running the HAB validated code after TF-A hands
back control to our code in DRAM.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 arch/arm/boards/nxp-imx8mq-evk/lowlevel.c | 37 +++++++++++------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/arch/arm/boards/nxp-imx8mq-evk/lowlevel.c b/arch/arm/boards/nxp-imx8mq-evk/lowlevel.c
index eb96c0805169..101ce607a55f 100644
--- a/arch/arm/boards/nxp-imx8mq-evk/lowlevel.c
+++ b/arch/arm/boards/nxp-imx8mq-evk/lowlevel.c
@@ -50,23 +50,16 @@ static void setup_uart(void)
  * 1. MaskROM uploads PBL into OCRAM and that's where this function is
  *    executed for the first time. At entry the exception level is EL3.
  *
- * 2. DDR is initialized and the PBL is copied from OCRAM to the TF-A return
- *    address in DRAM.
+ * 2. DDR is initialized and the image is loaded from storage into DRAM. The PBL
+ *    part is copied from OCRAM to the TF-A return address in DRAM.
  *
  * 3. TF-A is executed and exits into the PBL code in DRAM. TF-A has taken us
  *    from EL3 to EL2.
  *
- * 4. The piggydata is loaded from the SD card and copied to the expected
- *    location in the DRAM.
- *
- * 5. Standard barebox boot flow continues
+ * 4. Standard barebox boot flow continues
  */
 static __noreturn noinline void nxp_imx8mq_evk_start(void)
 {
-	enum bootsource src = BOOTSOURCE_UNKNOWN;
-	int instance = BOOTSOURCE_INSTANCE_UNKNOWN;
-	int ret = -ENOTSUPP;
-
 	if (IS_ENABLED(CONFIG_DEBUG_LL))
 		setup_uart();
 
@@ -76,14 +69,27 @@ static __noreturn noinline void nxp_imx8mq_evk_start(void)
 	 * to DRAM in EL2.
 	 */
 	if (current_el() == 3) {
-		const u8 *bl31;
+		enum bootsource src = BOOTSOURCE_UNKNOWN;
+		int instance = BOOTSOURCE_INSTANCE_UNKNOWN;
+		int ret = -ENOTSUPP;
 		size_t bl31_size;
+		const u8 *bl31;
 
 		ddr_init();
+
 		/*
 		 * On completion the TF-A will jump to MX8MQ_ATF_BL33_BASE_ADDR
-		 * in EL2. Copy ourselves there.
+		 * in EL2. Copy the image there, but replace the PBL part of
+		 * that image with ourselves. On a high assurance boot only the
+		 * currently running code is validated and contains the checksum
+		 * for the piggy data, so we need to ensure that we are running
+		 * the same code in DRAM.
 		 */
+		imx8_get_boot_source(&src, &instance);
+		if (src == BOOTSOURCE_MMC)
+			ret = imx8_esdhc_load_image(instance, false);
+		BUG_ON(ret);
+
 		memcpy((void *)MX8MQ_ATF_BL33_BASE_ADDR,
 		       __image_start, barebox_pbl_size);
 
@@ -92,13 +98,6 @@ static __noreturn noinline void nxp_imx8mq_evk_start(void)
 		/* not reached */
 	}
 
-	imx8_get_boot_source(&src, &instance);
-
-	if (src == BOOTSOURCE_MMC)
-		ret = imx8_esdhc_load_piggy(instance);
-
-	BUG_ON(ret);
-
 	/*
 	 * Standard entry we hit once we initialized both DDR and ATF
 	 */
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH 3/3] esdhc-pbl: remove now unused imx8_esdhc_load_piggy
  2020-01-07 10:25 [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Lucas Stach
  2020-01-07 10:25 ` [PATCH 2/3] ARM: nxp-imx8mq-evk: fix second stage booting Lucas Stach
@ 2020-01-07 10:25 ` Lucas Stach
  2020-01-08 11:41 ` [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Lucas Stach @ 2020-01-07 10:25 UTC (permalink / raw)
  To: barebox

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 arch/arm/mach-imx/include/mach/xload.h |  1 -
 drivers/mci/imx-esdhc-pbl.c            | 42 --------------------------
 2 files changed, 43 deletions(-)

diff --git a/arch/arm/mach-imx/include/mach/xload.h b/arch/arm/mach-imx/include/mach/xload.h
index a9b9d93f246f..9709b13dfb6a 100644
--- a/arch/arm/mach-imx/include/mach/xload.h
+++ b/arch/arm/mach-imx/include/mach/xload.h
@@ -6,7 +6,6 @@ int imx6_spi_load_image(int instance, unsigned int flash_offset, void *buf, int
 int imx6_spi_start_image(int instance);
 int imx6_esdhc_start_image(int instance);
 int imx8_esdhc_load_image(int instance, bool start);
-int imx8_esdhc_load_piggy(int instance);
 
 int imx_image_size(void);
 int piggydata_size(void);
diff --git a/drivers/mci/imx-esdhc-pbl.c b/drivers/mci/imx-esdhc-pbl.c
index db4116fa9315..7bc7c64caf33 100644
--- a/drivers/mci/imx-esdhc-pbl.c
+++ b/drivers/mci/imx-esdhc-pbl.c
@@ -286,48 +286,6 @@ int imx8_esdhc_load_image(int instance, bool start)
 	return esdhc_load_image(&host, MX8MQ_DDR_CSD1_BASE_ADDR,
 				MX8MQ_ATF_BL33_BASE_ADDR, SZ_32K, start);
 }
-
-int imx8_esdhc_load_piggy(int instance)
-{
-	void *buf, *piggy;
-	struct imx_flash_header_v2 *hdr = NULL;
-	struct esdhc_soc_data data;
-	struct fsl_esdhc_host host;
-	int ret, len;
-	int offset = SZ_32K;
-
-	ret = imx8_esdhc_init(&host, &data, instance);
-	if (ret)
-		return ret;
-
-	/*
-	 * We expect to be running at MX8MQ_ATF_BL33_BASE_ADDR where the atf
-	 * has jumped to. Use a temporary buffer where we won't overwrite
-	 * ourselves.
-	 */
-	buf = (void *)MX8MQ_ATF_BL33_BASE_ADDR + SZ_32M;
-
-	ret = esdhc_search_header(&host, &hdr, buf, &offset);
-	if (ret)
-		return ret;
-
-	len = offset + hdr->boot_data.size + piggydata_size();
-	len = ALIGN(len, SECTOR_SIZE);
-
-	ret = esdhc_read_blocks(&host, buf, len);
-
-	/*
-	 * Calculate location of the piggydata at the offset loaded into RAM
-	 */
-	piggy = buf + offset + hdr->boot_data.size;
-
-	/*
-	 * Copy the piggydata where the uncompressing code expects it
-	 */
-	memcpy(input_data, piggy, piggydata_size());
-
-	return ret;
-}
 #endif
 
 #ifdef CONFIG_ARCH_LS1046
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* Re: [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image
  2020-01-07 10:25 [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Lucas Stach
  2020-01-07 10:25 ` [PATCH 2/3] ARM: nxp-imx8mq-evk: fix second stage booting Lucas Stach
  2020-01-07 10:25 ` [PATCH 3/3] esdhc-pbl: remove now unused imx8_esdhc_load_piggy Lucas Stach
@ 2020-01-08 11:41 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2020-01-08 11:41 UTC (permalink / raw)
  To: Lucas Stach; +Cc: barebox

On Tue, Jan 07, 2020 at 11:25:13AM +0100, Lucas Stach wrote:
> Add an option that allows to just load the image into memory, but
> return to the calling function instead of directly jumping to the
> loaded image.
> 
> Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
> ---
>  arch/arm/boards/phytec-som-imx8mq/lowlevel.c |  2 +-
>  arch/arm/boards/zii-imx8mq-dev/lowlevel.c    |  2 +-
>  arch/arm/mach-imx/include/mach/xload.h       |  2 +-
>  drivers/mci/imx-esdhc-pbl.c                  | 22 ++++++++++++--------
>  4 files changed, 16 insertions(+), 12 deletions(-)

Applied, thanks

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 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

end of thread, other threads:[~2020-01-08 11:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-07 10:25 [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Lucas Stach
2020-01-07 10:25 ` [PATCH 2/3] ARM: nxp-imx8mq-evk: fix second stage booting Lucas Stach
2020-01-07 10:25 ` [PATCH 3/3] esdhc-pbl: remove now unused imx8_esdhc_load_piggy Lucas Stach
2020-01-08 11:41 ` [PATCH 1/3] esdhc-pbl: allow to skip starting i.MX8 image Sascha Hauer

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