* [PATCH 0/3] ARM: support booting arbitrary ELF executables
@ 2023-09-13 12:57 Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 1/3] common: elf: support loading to address 0 Ahmad Fatoum
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 12:57 UTC (permalink / raw)
To: barebox
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.
Ahmad Fatoum (3):
common: elf: support loading to address 0
ARM: add support for booting ELF executables
kbuild: support generating stripped ELF files for PBL
Makefile | 4 +--
arch/arm/cpu/Makefile | 1 +
arch/arm/cpu/bootm-elf.c | 56 ++++++++++++++++++++++++++++++++++++++
arch/arm/include/asm/elf.h | 5 ++++
common/elf.c | 17 ++++++++----
images/Makefile | 10 +++++--
include/elf.h | 1 +
7 files changed, 83 insertions(+), 11 deletions(-)
create mode 100644 arch/arm/cpu/bootm-elf.c
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] common: elf: support loading to address 0
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary ELF executables Ahmad Fatoum
@ 2023-09-13 12:57 ` Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 2/3] ARM: add support for booting ELF executables Ahmad Fatoum
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 12:57 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
Many platforms have DRAM starting at address 0, which clashes with
barebox placing kernel images there, as barebox does that before
shutting down and turning off the MMU.
The <zero_page.h> header allows temporarily disabling the faulting
zero page and is already used for boot other types of kernel images,
so use it for ELF images as well.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/elf.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/common/elf.c b/common/elf.c
index c9b3d7bb3791..62f793010fad 100644
--- a/common/elf.c
+++ b/common/elf.c
@@ -10,6 +10,7 @@
#include <libfile.h>
#include <memory.h>
#include <unistd.h>
+#include <zero_page.h>
#include <linux/fs.h>
#include <linux/list_sort.h>
@@ -100,7 +101,7 @@ static int elf_section_cmp(void *priv, struct list_head *a, struct list_head *b)
static int load_elf_to_memory(struct elf_image *elf)
{
void *dst;
- int ret, fd = -1;
+ int ret = 0, fd = -1;
u64 p_filesz, p_memsz, p_offset;
struct elf_section *r;
struct list_head *list = &elf->list;
@@ -113,6 +114,8 @@ static int load_elf_to_memory(struct elf_image *elf)
}
}
+ zero_page_access();
+
list_for_each_entry(r, list, list) {
p_offset = elf_phdr_p_offset(elf, r->phdr);
p_filesz = elf_phdr_p_filesz(elf, r->phdr);
@@ -127,14 +130,13 @@ static int load_elf_to_memory(struct elf_image *elf)
if (ret == -1) {
pr_err("lseek at offset 0x%llx failed\n",
p_offset);
- close(fd);
- return ret;
+ goto out;
}
if (read_full(fd, dst, p_filesz) < 0) {
pr_err("could not read elf segment: %m\n");
- close(fd);
- return -errno;
+ ret = -errno;
+ goto out;
}
} else {
memcpy(dst, elf->hdr_buf + p_offset, p_filesz);
@@ -144,9 +146,12 @@ static int load_elf_to_memory(struct elf_image *elf)
memset(dst + p_filesz, 0x00, p_memsz - p_filesz);
}
+out:
+ zero_page_faulting();
+
close(fd);
- return 0;
+ return ret >= 0 ? 0 : ret;
}
static int load_elf_image_segments(struct elf_image *elf)
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/3] ARM: add support for booting ELF executables
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary ELF executables 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
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
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 12:57 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/3] kbuild: support generating stripped ELF files for PBL
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary ELF executables Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 1/3] common: elf: support loading to address 0 Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 2/3] ARM: add support for booting ELF executables Ahmad Fatoum
@ 2023-09-13 12:57 ` Ahmad Fatoum
2023-09-21 8:18 ` [PATCH 0/3] ARM: support booting arbitrary ELF executables Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 12:57 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
For testing remoteproc and ELF loading mechanisms, it can be useful
to have the consumed ELF files result from the barebox build itself.
The *.pblb intermediate artifacts can be used for this purpose, but they
are at least 64K larger than need be, because of generous alignment in
addition to debug and symbol information.
Let's add a separate %.elf target, that behaves like %.pblb with the
difference that the result is stripped and alignment of sections on-disk
is disabled.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 4 ++--
images/Makefile | 10 +++++++---
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
index f86d76076fb7..0e579eb804f2 100644
--- a/Makefile
+++ b/Makefile
@@ -470,6 +470,7 @@ LDFLAGS_common += $(call ld-option,--no-warn-rwx-segments)
LDFLAGS_barebox += $(LDFLAGS_common)
LDFLAGS_pbl += $(LDFLAGS_common)
+LDFLAGS_elf += $(LDFLAGS_common) --nmagic -s
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL PYTHON3 UTS_MACHINE
@@ -483,8 +484,7 @@ export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
-export LDFLAGS_barebox
-export LDFLAGS_pbl
+export LDFLAGS_barebox LDFLAGS_pbl LDFLAGS_elf
export CFLAGS_UBSAN
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
diff --git a/images/Makefile b/images/Makefile
index c1cb56f5b189..9739a15c06ba 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -57,9 +57,9 @@ $(pbl-lds): $(obj)/../arch/$(SRCARCH)/lib/pbl.lds.S FORCE
$(call if_changed_dep,cpp_lds_S)
quiet_cmd_elf__ ?= LD $@
- cmd_elf__ ?= $(LD) $(LDFLAGS_pbl) --gc-sections \
+ cmd_elf__ ?= $(LD) $(LDFLAGS_$(patsubst .%,%,$(suffix $(@F)))) \
-e $(2) -Map $@.map $(LDFLAGS_$(@F)) -o $@ \
- --defsym=__pbl_board_entry=$(2) \
+ --gc-sections --defsym=__pbl_board_entry=$(2) \
-T $(pbl-lds) \
--whole-archive $(BAREBOX_PBL_OBJS) $(obj)/piggy.o \
$(obj)/sha_sum.o
@@ -70,6 +70,10 @@ $(obj)/%.pbl: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(obj)/piggy.o $(obj)/sha_sum.o FOR
$(call if_changed,elf__,$(*F))
$(call cmd,prelink__)
+$(obj)/%.elf: $(pbl-lds) $(BAREBOX_PBL_OBJS) $(obj)/piggy.o $(obj)/sha_sum.o FORCE
+ $(call if_changed,elf__,$(*F))
+ $(call cmd,prelink__)
+
$(obj)/%.pblb: $(obj)/%.pbl FORCE
$(call if_changed,objcopy_bin,$(*F))
$(Q)$(OBJCOPY) -O binary --only-section=.missing_fw $< $@.missing-firmware
@@ -218,7 +222,7 @@ $(flash-link): $(link-dest) FORCE
$(flash-list): $(image-y-path)
@for i in $^; do if [ -s $$i ]; then echo $$i; fi; done > $@
-clean-files := *.pbl *.pblb *.map start_*.imximg *.img barebox.z start_*.kwbimg \
+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
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/3] ARM: support booting arbitrary ELF executables
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary ELF executables Ahmad Fatoum
` (2 preceding siblings ...)
2023-09-13 12:57 ` [PATCH 3/3] kbuild: support generating stripped ELF files for PBL Ahmad Fatoum
@ 2023-09-21 8:18 ` Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2023-09-21 8:18 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox
On Wed, Sep 13, 2023 at 02:57:12PM +0200, Ahmad Fatoum wrote:
> 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.
>
> Ahmad Fatoum (3):
> common: elf: support loading to address 0
> ARM: add support for booting ELF executables
> kbuild: support generating stripped ELF files for PBL
Applied, thanks
Sascha
>
> Makefile | 4 +--
> arch/arm/cpu/Makefile | 1 +
> arch/arm/cpu/bootm-elf.c | 56 ++++++++++++++++++++++++++++++++++++++
> arch/arm/include/asm/elf.h | 5 ++++
> common/elf.c | 17 ++++++++----
> images/Makefile | 10 +++++--
> include/elf.h | 1 +
> 7 files changed, 83 insertions(+), 11 deletions(-)
> create mode 100644 arch/arm/cpu/bootm-elf.c
>
> --
> 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] 5+ messages in thread
end of thread, other threads:[~2023-09-21 8:19 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-13 12:57 [PATCH 0/3] ARM: support booting arbitrary ELF executables Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 1/3] common: elf: support loading to address 0 Ahmad Fatoum
2023-09-13 12:57 ` [PATCH 2/3] ARM: add support for booting ELF executables Ahmad Fatoum
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox