* [PATCH v2 1/2] scripts: define le32_to_cpup and friends for host/target tools
@ 2025-03-20 5:21 Ahmad Fatoum
2025-03-20 5:21 ` [PATCH v2 2/2] booti: sanity check image magic before parsing header Ahmad Fatoum
0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-03-20 5:21 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We already define le32_to_cpu and friends, but were missing so far the
p-suffixed variant, which accepts a pointer.
This is easily implemented with a bit of macro boilerplate, so let's do
that.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
- new patch
---
scripts/compiler.h | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/scripts/compiler.h b/scripts/compiler.h
index d8d0e1b906df..d6807b027e03 100644
--- a/scripts/compiler.h
+++ b/scripts/compiler.h
@@ -160,6 +160,27 @@ typedef uint32_t __u32;
# define be64_to_cpu(x) (x)
#endif
+#define DEFINE_CONV_P(endian, bits) \
+ static inline __##endian##bits endian##bits##_to_cpup(const u##bits *p) \
+ { \
+ u##bits val; \
+ memmove(&val, p, sizeof(val)); \
+ return endian##bits##_to_cpu(val); \
+ } \
+ static inline u##bits cpu_to_##endian##bits##p(const __##endian##bits *p) \
+ { \
+ __##endian##bits val; \
+ memmove(&val, p, sizeof(val)); \
+ return cpu_to_##endian##bits(val); \
+ }
+
+DEFINE_CONV_P(le, 16)
+DEFINE_CONV_P(le, 32)
+DEFINE_CONV_P(le, 64)
+DEFINE_CONV_P(be, 16)
+DEFINE_CONV_P(be, 32)
+DEFINE_CONV_P(be, 64)
+
#ifndef min
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
--
2.39.5
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH v2 2/2] booti: sanity check image magic before parsing header
2025-03-20 5:21 [PATCH v2 1/2] scripts: define le32_to_cpup and friends for host/target tools Ahmad Fatoum
@ 2025-03-20 5:21 ` Ahmad Fatoum
0 siblings, 0 replies; 2+ messages in thread
From: Ahmad Fatoum @ 2025-03-20 5:21 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
booti_load_image is called directly for kernels in FIT images.
Let's have a simple check that the payload is indeed a booti
image before parsing it.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
- no change
---
common/booti.c | 11 +++++++++--
common/filetype.c | 6 +++---
include/filetype.h | 11 +++++++++++
3 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/common/booti.c b/common/booti.c
index 5efd4f97bbc2..c8f6b5d4fc6d 100644
--- a/common/booti.c
+++ b/common/booti.c
@@ -4,6 +4,7 @@
#define pr_fmt(fmt) "booti: " fmt
#include <common.h>
+#include <filetype.h>
#include <memory.h>
#include <bootm.h>
#include <linux/sizes.h>
@@ -38,13 +39,19 @@ void *booti_load_image(struct image_data *data, phys_addr_t *oftree)
int ret;
void *fdt;
+ print_hex_dump_bytes("header ", DUMP_PREFIX_OFFSET, kernel_header, 80);
+
+ if ((IS_ENABLED(CONFIG_RISCV) && !is_riscv_linux_bootimage(kernel_header)) ||
+ (IS_ENABLED(CONFIG_ARM64) && !is_arm64_linux_bootimage(kernel_header))) {
+ pr_err("Unexpected magic at offset 0x38!\n");
+ return ERR_PTR(-EINVAL);
+ }
+
text_offset = le64_to_cpup(kernel_header + 8);
image_size = le64_to_cpup(kernel_header + 16);
kernel = get_kernel_address(data->os_address, text_offset);
- print_hex_dump_bytes("header ", DUMP_PREFIX_OFFSET,
- kernel_header, 80);
pr_debug("Kernel to be loaded to %lx+%lx\n", kernel, image_size);
if (kernel == UIMAGE_INVALID_ADDRESS)
diff --git a/common/filetype.c b/common/filetype.c
index 73ea17e19bd7..2a68879ee5de 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -365,11 +365,11 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
if (bufsize < 64)
return filetype_unknown;
- if (le32_to_cpu(buf[14]) == 0x644d5241)
+ if (is_arm64_linux_bootimage(buf))
return is_dos_exe(buf8) ? filetype_arm64_efi_linux_image : filetype_arm64_linux_image;
- if (le32_to_cpu(buf[14]) == 0x05435352)
+ if (is_riscv_linux_bootimage(buf))
return is_dos_exe(buf8) ? filetype_riscv_efi_linux_image : filetype_riscv_linux_image;
- if (le32_to_cpu(buf[14]) == 0x56435352 && !memcmp(&buf[12], "barebox", 8))
+ if (is_riscv_linux_bootimage(buf) && !memcmp(&buf[12], "barebox", 8))
return filetype_riscv_barebox_image;
if (le32_to_cpu(buf[5]) == 0x504d5453)
diff --git a/include/filetype.h b/include/filetype.h
index c24d061e8ffa..d445140edba1 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -4,6 +4,7 @@
#include <linux/string.h>
#include <linux/types.h>
+#include <asm/byteorder.h>
/*
* List of file types we know
@@ -134,4 +135,14 @@ static inline int is_barebox_head(const char *head)
return is_barebox_arm_head(head) || is_barebox_mips_head(head);
}
+static inline bool is_arm64_linux_bootimage(const void *header)
+{
+ return le32_to_cpup(header + 56) == 0x644d5241;
+}
+
+static inline bool is_riscv_linux_bootimage(const void *header)
+{
+ return le32_to_cpup(header + 56) == 0x05435352;
+}
+
#endif /* __FILE_TYPE_H */
--
2.39.5
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-03-20 5:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-20 5:21 [PATCH v2 1/2] scripts: define le32_to_cpup and friends for host/target tools Ahmad Fatoum
2025-03-20 5:21 ` [PATCH v2 2/2] booti: sanity check image magic before parsing header Ahmad Fatoum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox