mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/3] ARM: add support for booting ELF executables
Date: Wed, 13 Sep 2023 14:57:14 +0200	[thread overview]
Message-ID: <20230913125715.2142524-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230913125715.2142524-1-a.fatoum@pengutronix.de>

Unlike MIPS and kvx, where ELF is used as kernel image format, Linux
ARM support defines its own flattened format.

Other kernels may be distributed as ELF images though, so it makes
sense to enable booting of ELF images on ARM as well.

This has been tested booting FreeRTOS ELF executables on the ZynqMP.

Note that this will refuse to boot kernel ELF images as those have
type dyn, while the common ELF code in barebox will only boot type exec.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/cpu/Makefile      |  1 +
 arch/arm/cpu/bootm-elf.c   | 56 ++++++++++++++++++++++++++++++++++++++
 arch/arm/include/asm/elf.h |  5 ++++
 include/elf.h              |  1 +
 4 files changed, 63 insertions(+)
 create mode 100644 arch/arm/cpu/bootm-elf.c

diff --git a/arch/arm/cpu/Makefile b/arch/arm/cpu/Makefile
index 5baff2fad087..28161cd7d714 100644
--- a/arch/arm/cpu/Makefile
+++ b/arch/arm/cpu/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_ARM_PSCI_CLIENT) += psci-client.o
 obj-$(CONFIG_CMD_ARM_CPUINFO) += cpuinfo.o
 obj-$(CONFIG_MMUINFO) += mmuinfo.o mmuinfo_$(S64_32).o
 obj-$(CONFIG_OFDEVICE) += dtb.o
+obj-$(CONFIG_BOOTM_ELF)	+= bootm-elf.o
 
 ifeq ($(CONFIG_MMU),)
 obj-$(CONFIG_CPU_32v7) += no-mmu.o
diff --git a/arch/arm/cpu/bootm-elf.c b/arch/arm/cpu/bootm-elf.c
new file mode 100644
index 000000000000..bcca3931f22f
--- /dev/null
+++ b/arch/arm/cpu/bootm-elf.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "ELF: " fmt
+
+#include <bootm.h>
+#include <elf.h>
+#include <common.h>
+#include <init.h>
+#include <errno.h>
+
+static int do_bootm_elf(struct image_data *data)
+{
+	void (*fn)(unsigned long x0, unsigned long x1, unsigned long x2,
+		   unsigned long x3);
+	struct elf_image *elf = data->elf;
+	int ret;
+
+	if (elf_hdr_e_machine(elf, elf->hdr_buf) != ELF_ARCH) {
+		pr_err("Unsupported machine: 0x%02x, but 0x%02x expected\n",
+		       elf_hdr_e_machine(elf, elf->hdr_buf), ELF_ARCH);
+
+		return -EINVAL;
+	}
+
+	ret = bootm_load_os(data, data->os_address);
+	if (ret)
+		return ret;
+
+	if (data->dryrun)
+		return ret;
+
+	ret = of_overlay_load_firmware();
+	if (ret)
+		return ret;
+
+	shutdown_barebox();
+
+	fn = (void *) (unsigned long) data->os_address;
+
+	fn(0, 0, 0, 0);
+
+	pr_err("ELF application terminated\n");
+	return -EINVAL;
+}
+
+static struct image_handler elf_handler = {
+	.name = "ELF",
+	.bootm = do_bootm_elf,
+	.filetype = filetype_elf,
+};
+
+static int arm_register_elf_image_handler(void)
+{
+	return register_image_handler(&elf_handler);
+}
+late_initcall(arm_register_elf_image_handler);
diff --git a/arch/arm/include/asm/elf.h b/arch/arm/include/asm/elf.h
index 486275a3361d..4043e6fd5b99 100644
--- a/arch/arm/include/asm/elf.h
+++ b/arch/arm/include/asm/elf.h
@@ -22,6 +22,7 @@ typedef struct user_fp elf_fpregset_t;
 #endif
 
 #define EM_ARM	40
+#define EM_AARCH64	183
 #define EF_ARM_APCS26 0x08
 #define EF_ARM_SOFT_FLOAT 0x200
 #define EF_ARM_EABI_MASK 0xFF000000
@@ -44,7 +45,11 @@ typedef struct user_fp elf_fpregset_t;
 #else
 #define ELF_DATA	ELFDATA2LSB
 #endif
+#ifdef CONFIG_CPU_64
+#define ELF_ARCH	EM_AARCH64
+#else
 #define ELF_ARCH	EM_ARM
+#endif
 
 #ifdef __KERNEL__
 #ifndef __ASSEMBLY__
diff --git a/include/elf.h b/include/elf.h
index 12673e93ed0d..de1549ee8620 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -432,6 +432,7 @@ ELF_GET_FIELD(hdr, e_phnum, u16)
 ELF_GET_FIELD(hdr, e_phoff, u64)
 ELF_GET_FIELD(hdr, e_phentsize, u16)
 ELF_GET_FIELD(hdr, e_type, u16)
+ELF_GET_FIELD(hdr, e_machine, u16)
 ELF_GET_FIELD(phdr, p_paddr, u64)
 ELF_GET_FIELD(phdr, p_filesz, u64)
 ELF_GET_FIELD(phdr, p_memsz, u64)
-- 
2.39.2




  parent reply	other threads:[~2023-09-13 12:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary " Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 1/3] common: elf: support loading to address 0 Ahmad Fatoum
2023-09-13 12:57 ` Ahmad Fatoum [this message]
2023-09-13 12:57 ` [PATCH 3/3] kbuild: support generating stripped ELF files for PBL Ahmad Fatoum
2023-09-21  8:18 ` [PATCH 0/3] ARM: support booting arbitrary ELF executables Sascha Hauer

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=20230913125715.2142524-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@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