All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] imx: bootaux fix elf loading
@ 2021-03-11 17:18 Max Krummenacher
  2021-03-11 20:04 ` Oleksandr Suvorov
  2021-04-09 11:24 ` sbabic at denx.de
  0 siblings, 2 replies; 3+ messages in thread
From: Max Krummenacher @ 2021-03-11 17:18 UTC (permalink / raw)
  To: u-boot

This reverts the arch/arm/mach-imx/imx_bootaux.c changes of commit
805b3cac1e0c. The loader function name was changed so that it does
not clash with the generically available function in lib/elf.c.

imx-bootaux loads an elf file linked for an auxilary core. Thus the
loader function requires address translation from the auxilary core's
address space to where those are mapped into U-Boot's address space.

So the elf loader is specific and must not be replaced with a generic
loader which doesn't provide the address translation functionality.

Fixes commit 805b3cac1e0c ("lib: elf: Move the generic elf
loading/validating functions to lib")

Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>

---

 arch/arm/mach-imx/imx_bootaux.c | 67 ++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/arch/arm/mach-imx/imx_bootaux.c b/arch/arm/mach-imx/imx_bootaux.c
index e1fc4b3e19a..30fb45d48c2 100644
--- a/arch/arm/mach-imx/imx_bootaux.c
+++ b/arch/arm/mach-imx/imx_bootaux.c
@@ -14,6 +14,71 @@
 #include <linux/compiler.h>
 #include <cpu_func.h>
 
+#ifndef CONFIG_IMX8M
+const __weak struct rproc_att hostmap[] = { };
+
+static const struct rproc_att *get_host_mapping(unsigned long auxcore)
+{
+	const struct rproc_att *mmap = hostmap;
+
+	while (mmap && mmap->size) {
+		if (mmap->da <= auxcore &&
+		    mmap->da + mmap->size > auxcore)
+			return mmap;
+		mmap++;
+	}
+
+	return NULL;
+}
+
+/*
+ * A very simple elf loader for the auxilary core, assumes the image
+ * is valid, returns the entry point address.
+ * Translates load addresses in the elf file to the U-Boot address space.
+ */
+static unsigned long load_elf_image_m_core_phdr(unsigned long addr)
+{
+	Elf32_Ehdr *ehdr; /* ELF header structure pointer */
+	Elf32_Phdr *phdr; /* Program header structure pointer */
+	int i;
+
+	ehdr = (Elf32_Ehdr *)addr;
+	phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
+
+	/* Load each program header */
+	for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
+		const struct rproc_att *mmap = get_host_mapping(phdr->p_paddr);
+		void *dst, *src;
+
+		if (phdr->p_type != PT_LOAD)
+			continue;
+
+		if (!mmap) {
+			printf("Invalid aux core address: %08x",
+			       phdr->p_paddr);
+			return 0;
+		}
+
+		dst = (void *)(phdr->p_paddr - mmap->da) + mmap->sa;
+		src = (void *)addr + phdr->p_offset;
+
+		debug("Loading phdr %i to 0x%p (%i bytes)\n",
+		      i, dst, phdr->p_filesz);
+
+		if (phdr->p_filesz)
+			memcpy(dst, src, phdr->p_filesz);
+		if (phdr->p_filesz != phdr->p_memsz)
+			memset(dst + phdr->p_filesz, 0x00,
+			       phdr->p_memsz - phdr->p_filesz);
+		flush_cache((unsigned long)dst &
+			    ~(CONFIG_SYS_CACHELINE_SIZE - 1),
+			    ALIGN(phdr->p_filesz, CONFIG_SYS_CACHELINE_SIZE));
+	}
+
+	return ehdr->e_entry;
+}
+#endif
+
 int arch_auxiliary_core_up(u32 core_id, ulong addr)
 {
 	ulong stack, pc;
@@ -31,7 +96,7 @@ int arch_auxiliary_core_up(u32 core_id, ulong addr)
 	 */
 	if (valid_elf_image(addr)) {
 		stack = 0x0;
-		pc = load_elf_image_phdr(addr);
+		pc = load_elf_image_m_core_phdr(addr);
 		if (!pc)
 			return CMD_RET_FAILURE;
 
-- 
2.20.1

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

* [PATCH v1] imx: bootaux fix elf loading
  2021-03-11 17:18 [PATCH v1] imx: bootaux fix elf loading Max Krummenacher
@ 2021-03-11 20:04 ` Oleksandr Suvorov
  2021-04-09 11:24 ` sbabic at denx.de
  1 sibling, 0 replies; 3+ messages in thread
From: Oleksandr Suvorov @ 2021-03-11 20:04 UTC (permalink / raw)
  To: u-boot

On Thu, Mar 11, 2021 at 7:18 PM Max Krummenacher <max.oss.09@gmail.com> wrote:
>
> This reverts the arch/arm/mach-imx/imx_bootaux.c changes of commit
> 805b3cac1e0c. The loader function name was changed so that it does
> not clash with the generically available function in lib/elf.c.
>
> imx-bootaux loads an elf file linked for an auxilary core. Thus the
> loader function requires address translation from the auxilary core's
> address space to where those are mapped into U-Boot's address space.
>
> So the elf loader is specific and must not be replaced with a generic
> loader which doesn't provide the address translation functionality.
>
> Fixes commit 805b3cac1e0c ("lib: elf: Move the generic elf
> loading/validating functions to lib")
>
> Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>

Acked-by: Oleksandr Suvorov <oleksandr.suvorov@toradex.com>

>
> ---
>
>  arch/arm/mach-imx/imx_bootaux.c | 67 ++++++++++++++++++++++++++++++++-
>  1 file changed, 66 insertions(+), 1 deletion(-)
>
> diff --git a/arch/arm/mach-imx/imx_bootaux.c b/arch/arm/mach-imx/imx_bootaux.c
> index e1fc4b3e19a..30fb45d48c2 100644
> --- a/arch/arm/mach-imx/imx_bootaux.c
> +++ b/arch/arm/mach-imx/imx_bootaux.c
> @@ -14,6 +14,71 @@
>  #include <linux/compiler.h>
>  #include <cpu_func.h>
>
> +#ifndef CONFIG_IMX8M
> +const __weak struct rproc_att hostmap[] = { };
> +
> +static const struct rproc_att *get_host_mapping(unsigned long auxcore)
> +{
> +       const struct rproc_att *mmap = hostmap;
> +
> +       while (mmap && mmap->size) {
> +               if (mmap->da <= auxcore &&
> +                   mmap->da + mmap->size > auxcore)
> +                       return mmap;
> +               mmap++;
> +       }
> +
> +       return NULL;
> +}
> +
> +/*
> + * A very simple elf loader for the auxilary core, assumes the image
> + * is valid, returns the entry point address.
> + * Translates load addresses in the elf file to the U-Boot address space.
> + */
> +static unsigned long load_elf_image_m_core_phdr(unsigned long addr)
> +{
> +       Elf32_Ehdr *ehdr; /* ELF header structure pointer */
> +       Elf32_Phdr *phdr; /* Program header structure pointer */
> +       int i;
> +
> +       ehdr = (Elf32_Ehdr *)addr;
> +       phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
> +
> +       /* Load each program header */
> +       for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) {
> +               const struct rproc_att *mmap = get_host_mapping(phdr->p_paddr);
> +               void *dst, *src;
> +
> +               if (phdr->p_type != PT_LOAD)
> +                       continue;
> +
> +               if (!mmap) {
> +                       printf("Invalid aux core address: %08x",
> +                              phdr->p_paddr);
> +                       return 0;
> +               }
> +
> +               dst = (void *)(phdr->p_paddr - mmap->da) + mmap->sa;
> +               src = (void *)addr + phdr->p_offset;
> +
> +               debug("Loading phdr %i to 0x%p (%i bytes)\n",
> +                     i, dst, phdr->p_filesz);
> +
> +               if (phdr->p_filesz)
> +                       memcpy(dst, src, phdr->p_filesz);
> +               if (phdr->p_filesz != phdr->p_memsz)
> +                       memset(dst + phdr->p_filesz, 0x00,
> +                              phdr->p_memsz - phdr->p_filesz);
> +               flush_cache((unsigned long)dst &
> +                           ~(CONFIG_SYS_CACHELINE_SIZE - 1),
> +                           ALIGN(phdr->p_filesz, CONFIG_SYS_CACHELINE_SIZE));
> +       }
> +
> +       return ehdr->e_entry;
> +}
> +#endif
> +
>  int arch_auxiliary_core_up(u32 core_id, ulong addr)
>  {
>         ulong stack, pc;
> @@ -31,7 +96,7 @@ int arch_auxiliary_core_up(u32 core_id, ulong addr)
>          */
>         if (valid_elf_image(addr)) {
>                 stack = 0x0;
> -               pc = load_elf_image_phdr(addr);
> +               pc = load_elf_image_m_core_phdr(addr);
>                 if (!pc)
>                         return CMD_RET_FAILURE;
>
> --
> 2.20.1
>


-- 
Best regards

Oleksandr Suvorov
cryosay at gmail.com

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

* [PATCH v1] imx: bootaux fix elf loading
  2021-03-11 17:18 [PATCH v1] imx: bootaux fix elf loading Max Krummenacher
  2021-03-11 20:04 ` Oleksandr Suvorov
@ 2021-04-09 11:24 ` sbabic at denx.de
  1 sibling, 0 replies; 3+ messages in thread
From: sbabic at denx.de @ 2021-04-09 11:24 UTC (permalink / raw)
  To: u-boot

> This reverts the arch/arm/mach-imx/imx_bootaux.c changes of commit
> 805b3cac1e0c. The loader function name was changed so that it does
> not clash with the generically available function in lib/elf.c.
> imx-bootaux loads an elf file linked for an auxilary core. Thus the
> loader function requires address translation from the auxilary core's
> address space to where those are mapped into U-Boot's address space.
> So the elf loader is specific and must not be replaced with a generic
> loader which doesn't provide the address translation functionality.
> Fixes commit 805b3cac1e0c ("lib: elf: Move the generic elf
> loading/validating functions to lib")
> Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>
> Acked-by: Oleksandr Suvorov <oleksandr.suvorov@toradex.com>
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de
=====================================================================

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

end of thread, other threads:[~2021-04-09 11:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11 17:18 [PATCH v1] imx: bootaux fix elf loading Max Krummenacher
2021-03-11 20:04 ` Oleksandr Suvorov
2021-04-09 11:24 ` sbabic at denx.de

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.