mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 07/21] mci: imx-esdhc-pbl: move imx_load_image into common xload code
Date: Thu, 09 Mar 2023 11:40:39 +0100	[thread overview]
Message-ID: <20230228-v2023-02-0-topic-flexspi-v1-7-7b3c3fa295f5@pengutronix.de> (raw)
In-Reply-To: <20230228-v2023-02-0-topic-flexspi-v1-0-7b3c3fa295f5@pengutronix.de>

Make the function public available so we can reuse it for other boot
mediums like QSPI. No functional changes

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/xload-common.c | 120 +++++++++++++++++++++++++++++++++++++++
 drivers/mci/imx-esdhc-pbl.c      | 118 --------------------------------------
 include/mach/imx/xload.h         |   5 ++
 3 files changed, 125 insertions(+), 118 deletions(-)

diff --git a/arch/arm/mach-imx/xload-common.c b/arch/arm/mach-imx/xload-common.c
index 5a437b185d..0d3e6be1b1 100644
--- a/arch/arm/mach-imx/xload-common.c
+++ b/arch/arm/mach-imx/xload-common.c
@@ -1,11 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <common.h>
+#include <asm/cache.h>
 #include <asm/sections.h>
 #include <linux/sizes.h>
 #include <mach/imx/xload.h>
 #include <mach/imx/esdctl.h>
 #include <mach/imx/imx8m-regs.h>
+#include <mach/imx/imx-header.h>
 #include <asm/barebox-arm.h>
 
 int imx_image_size(void)
@@ -26,3 +28,121 @@ struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth)
 
 	return (void *)__arm_mem_scratch(endmem);
 }
+
+#define HDR_SIZE	512
+
+static int
+imx_search_header(struct imx_flash_header_v2 **header_pointer,
+		  void *buffer, u32 *offset, u32 ivt_offset,
+		  int (*read)(void *dest, size_t len, void *priv),
+		  void *priv)
+{
+	int ret;
+	int i, header_count = 1;
+	void *buf = buffer;
+	struct imx_flash_header_v2 *hdr;
+
+	for (i = 0; i < header_count; i++) {
+		ret = read(buf, *offset + ivt_offset + HDR_SIZE, priv);
+		if (ret)
+			return ret;
+
+		hdr = buf + *offset + ivt_offset;
+
+		if (!is_imx_flash_header_v2(hdr)) {
+			pr_debug("No IVT header! "
+				 "Found tag: 0x%02x length: 0x%04x "
+				 "version: %02x\n",
+				 hdr->header.tag, hdr->header.length,
+				 hdr->header.version);
+			return -EINVAL;
+		}
+
+		if (IS_ENABLED(CONFIG_ARCH_IMX8MQ) &&
+		    hdr->boot_data.plugin & PLUGIN_HDMI_IMAGE) {
+			/*
+			 * In images that include signed HDMI
+			 * firmware, first v2 header would be
+			 * dedicated to that and would not contain any
+			 * useful for us information. In order for us
+			 * to pull the rest of the bootloader image
+			 * in, we need to re-read header from SD/MMC,
+			 * this time skipping anything HDMI firmware
+			 * related.
+			 */
+			*offset += hdr->boot_data.size + hdr->header.length;
+			header_count++;
+		}
+	}
+	*header_pointer = hdr;
+	return 0;
+}
+
+int imx_load_image(ptrdiff_t address, ptrdiff_t entry, u32 offset,
+		   u32 ivt_offset, bool start, unsigned int alignment,
+		   int (*read)(void *dest, size_t len, void *priv),
+		   void *priv)
+{
+
+	void *buf = (void *)address;
+	struct imx_flash_header_v2 *hdr = NULL;
+	int ret, len;
+	void __noreturn (*bb)(void);
+	unsigned int ofs;
+
+	len = imx_image_size();
+	if (alignment)
+		len = ALIGN(len, alignment);
+
+	ret = imx_search_header(&hdr, buf, &offset, ivt_offset, read, priv);
+	if (ret)
+		return ret;
+
+	pr_debug("Check ok, loading image\n");
+
+	ofs = offset + hdr->entry - hdr->boot_data.start;
+
+	if (entry != address) {
+		/*
+		 * Passing entry different from address is interpreted
+		 * as a request to place the image such that its entry
+		 * point would be exactly at 'entry', that is:
+		 *
+		 *     buf + ofs = entry
+		 *
+		 * solving the above for 'buf' gives us the
+		 * adjustment that needs to be made:
+		 *
+		 *     buf = entry - ofs
+		 *
+		 */
+		if (WARN_ON(entry - ofs < address)) {
+			/*
+			 * We want to make sure we won't try to place
+			 * the start of the image before the beginning
+			 * of the memory buffer we were given in
+			 * address.
+			 */
+			return -EINVAL;
+		}
+
+		buf = (void *)(entry - ofs);
+	}
+
+	ret = read(buf, ofs + len, priv);
+	if (ret) {
+		pr_err("Loading image failed with %d\n", ret);
+		return ret;
+	}
+
+	pr_debug("Image loaded successfully\n");
+
+	if (!start)
+		return 0;
+
+	bb = buf + ofs;
+
+	sync_caches_for_execution();
+
+	bb();
+}
diff --git a/drivers/mci/imx-esdhc-pbl.c b/drivers/mci/imx-esdhc-pbl.c
index 4884a68837..7c5febb7a8 100644
--- a/drivers/mci/imx-esdhc-pbl.c
+++ b/drivers/mci/imx-esdhc-pbl.c
@@ -104,124 +104,6 @@ static int esdhc_read_blocks(struct fsl_esdhc_host *host, void *dst, size_t len)
 }
 
 #ifdef CONFIG_ARCH_IMX
-#define HDR_SIZE	512
-
-static int
-imx_search_header(struct imx_flash_header_v2 **header_pointer,
-		  void *buffer, u32 *offset, u32 ivt_offset,
-		  int (*read)(void *dest, size_t len, void *priv),
-		  void *priv)
-{
-	int ret;
-	int i, header_count = 1;
-	void *buf = buffer;
-	struct imx_flash_header_v2 *hdr;
-
-	for (i = 0; i < header_count; i++) {
-		ret = read(buf, *offset + ivt_offset + HDR_SIZE, priv);
-		if (ret)
-			return ret;
-
-		hdr = buf + *offset + ivt_offset;
-
-		if (!is_imx_flash_header_v2(hdr)) {
-			pr_debug("No IVT header! "
-				 "Found tag: 0x%02x length: 0x%04x "
-				 "version: %02x\n",
-				 hdr->header.tag, hdr->header.length,
-				 hdr->header.version);
-			return -EINVAL;
-		}
-
-		if (IS_ENABLED(CONFIG_ARCH_IMX8MQ) &&
-		    hdr->boot_data.plugin & PLUGIN_HDMI_IMAGE) {
-			/*
-			 * In images that include signed HDMI
-			 * firmware, first v2 header would be
-			 * dedicated to that and would not contain any
-			 * useful for us information. In order for us
-			 * to pull the rest of the bootloader image
-			 * in, we need to re-read header from SD/MMC,
-			 * this time skipping anything HDMI firmware
-			 * related.
-			 */
-			*offset += hdr->boot_data.size + hdr->header.length;
-			header_count++;
-		}
-	}
-	*header_pointer = hdr;
-	return 0;
-}
-
-int imx_load_image(ptrdiff_t address, ptrdiff_t entry, u32 offset,
-		   u32 ivt_offset, bool start, unsigned int alignment,
-		   int (*read)(void *dest, size_t len, void *priv),
-		   void *priv)
-{
-
-	void *buf = (void *)address;
-	struct imx_flash_header_v2 *hdr = NULL;
-	int ret, len;
-	void __noreturn (*bb)(void);
-	unsigned int ofs;
-
-	len = imx_image_size();
-	if (alignment)
-		len = ALIGN(len, alignment);
-
-	ret = imx_search_header(&hdr, buf, &offset, ivt_offset, read, priv);
-	if (ret)
-		return ret;
-
-	pr_debug("Check ok, loading image\n");
-
-	ofs = offset + hdr->entry - hdr->boot_data.start;
-
-	if (entry != address) {
-		/*
-		 * Passing entry different from address is interpreted
-		 * as a request to place the image such that its entry
-		 * point would be exactly at 'entry', that is:
-		 *
-		 *     buf + ofs = entry
-		 *
-		 * solving the above for 'buf' gives us the
-		 * adjustment that needs to be made:
-		 *
-		 *     buf = entry - ofs
-		 *
-		 */
-		if (WARN_ON(entry - ofs < address)) {
-			/*
-			 * We want to make sure we won't try to place
-			 * the start of the image before the beginning
-			 * of the memory buffer we were given in
-			 * address.
-			 */
-			return -EINVAL;
-		}
-
-		buf = (void *)(entry - ofs);
-	}
-
-	ret = read(buf, ofs + len, priv);
-	if (ret) {
-		pr_err("Loading image failed with %d\n", ret);
-		return ret;
-	}
-
-	pr_debug("Image loaded successfully\n");
-
-	if (!start)
-		return 0;
-
-	bb = buf + ofs;
-
-	sync_caches_for_execution();
-
-	bb();
-}
-
 static int imx_read_blocks(void *dest, size_t len, void *priv)
 {
 	return esdhc_read_blocks(priv, dest, len);
diff --git a/include/mach/imx/xload.h b/include/mach/imx/xload.h
index 86270b03dc..e9d41adc97 100644
--- a/include/mach/imx/xload.h
+++ b/include/mach/imx/xload.h
@@ -25,6 +25,11 @@ void __noreturn imx8mm_load_and_start_image_via_tfa(void);
 void __noreturn imx8mn_load_and_start_image_via_tfa(void);
 void __noreturn imx8mp_load_and_start_image_via_tfa(void);
 
+int imx_load_image(ptrdiff_t address, ptrdiff_t entry, u32 offset,
+		   u32 ivt_offset, bool start, unsigned int alignment,
+		   int (*read)(void *dest, size_t len, void *priv),
+		   void *priv);
+
 int imx_image_size(void);
 int piggydata_size(void);
 

-- 
2.30.2




  parent reply	other threads:[~2023-03-09 10:42 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-09 10:40 [PATCH 00/21] FlexSPI image/boot/update support Marco Felsch
2023-03-09 10:40 ` [PATCH 01/21] spi: remove flash_platform_data support Marco Felsch
2023-03-09 10:40 ` [PATCH 02/21] bbu: make it possible to check multiple of-compatibles Marco Felsch
2023-03-09 10:40 ` [PATCH 03/21] ARM: i.MX8MM: add missing IMD_USED_OF image metadata entry Marco Felsch
2023-03-09 10:40 ` [PATCH 04/21] ARM: i.MX8MN: add missing IMD_USED_OF image metadata entries Marco Felsch
2023-03-09 10:40 ` [PATCH 05/21] mci: imx-esdhc-pbl: fix number of read blocks Marco Felsch
2023-03-09 10:40 ` [PATCH 06/21] mci: imx-esdhc-pbl: refactor the esdhc_load_image function Marco Felsch
2023-03-09 10:40 ` Marco Felsch [this message]
2023-03-09 10:40 ` [PATCH 08/21] ARM: i.MX8M: Add QSPI image load support Marco Felsch
2023-03-09 10:40 ` [PATCH 09/21] ARM: i.MX8MM bootsource: fix QSPI boot source detection Marco Felsch
2023-03-09 10:40 ` [PATCH 10/21] ARM: i.MX8M: Add QSPI boot support Marco Felsch
2023-03-09 10:40 ` [PATCH 11/21] scripts: imx-image: convert flag variables into bool Marco Felsch
2023-03-09 10:40 ` [PATCH 12/21] scripts: imx-image: header_v2: factor out offset parameter Marco Felsch
2023-03-09 10:40 ` [PATCH 13/21] scripts: imx-image: header_v2: add header_len parameter Marco Felsch
2023-03-09 10:40 ` [PATCH 14/21] scripts: imx-image: add FlexSPI image support Marco Felsch
2023-03-09 10:40 ` [PATCH 15/21] filetype: add NXP FlexSPI filetype Marco Felsch
2023-03-09 10:40 ` [PATCH 16/21] ARM: i.MX: bbu: rename IMX_INTERNAL_FLAG_ERASE to IMX_BBU_FLAG_ERASE Marco Felsch
2023-03-09 10:40 ` [PATCH 17/21] ARM: i.MX: bbu: add target device offset Marco Felsch
2023-03-09 10:40 ` [PATCH 18/21] ARM: i.MX: bbu: add FlexSPI update handler Marco Felsch
2023-03-09 10:40 ` [PATCH 19/21] ARM: i.MX8M: enable FlexSPI image support Marco Felsch
2023-03-09 10:40 ` [PATCH 20/21] ARM: i.MX8M: add qspi barebox and barebox-environment partitions Marco Felsch
2023-03-09 10:40 ` [PATCH 21/21] ARM: i.MX8M: add QSPI update handler Marco Felsch
2023-03-09 11:28 ` [PATCH 00/21] FlexSPI image/boot/update support Ahmad Fatoum
2023-03-09 11:41   ` Marco Felsch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230228-v2023-02-0-topic-flexspi-v1-7-7b3c3fa295f5@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox