From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH] commands: go: start files 4 KiB aligned
Date: Sun, 8 Nov 2026 21:15:42 +0100 [thread overview]
Message-ID: <20261108201605.1401306-1-a.fatoum@barebox.org> (raw)
go memmaps the file and jumps to wherever its data happens to be. For a
file in ramfs, that's a malloc'ed chunk, but barebox on ARM64 addresses
its data with adrp and needs to run 4 KiB aligned.
Let's read files that can't be memmapped 4 KiB aligned into an aligned
buffer instead. This also makes go work with files that can't be
memmapped at all, like those on TFTP.
Assisted-by: Claude:opus-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
commands/go.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 44 insertions(+), 4 deletions(-)
diff --git a/commands/go.c b/commands/go.c
index 2e201c6ff3d4..2b549f3d9581 100644
--- a/commands/go.c
+++ b/commands/go.c
@@ -11,13 +11,51 @@
#include <getopt.h>
#include <linux/ctype.h>
#include <errno.h>
+#include <libfile.h>
+#include <malloc.h>
+#include <linux/sizes.h>
#include <security/config.h>
#define INT_ARGS_MAX 4
+/* adrp, as used by e.g. barebox on ARM64, needs 4 KiB alignment */
+#define GO_ALIGN SZ_4K
+
+static void *go_map(int fd, void **buf)
+{
+ struct stat st;
+ void *addr;
+ int ret;
+
+ addr = memmap(fd, PROT_READ);
+ if (addr != MAP_FAILED && IS_ALIGNED((ulong)addr, GO_ALIGN))
+ return addr;
+
+ ret = fstat(fd, &st);
+ if (ret)
+ return MAP_FAILED;
+
+ if (st.st_size == FILE_SIZE_STREAM || st.st_size > SIZE_MAX) {
+ errno = EFBIG;
+ return MAP_FAILED;
+ }
+
+ *buf = memalign(GO_ALIGN, st.st_size);
+ if (!*buf) {
+ errno = ENOMEM;
+ return MAP_FAILED;
+ }
+
+ ret = read_full(fd, *buf, st.st_size);
+ if (ret < 0)
+ return MAP_FAILED;
+
+ return *buf;
+}
+
static int do_go(int argc, char *argv[])
{
- void *addr;
+ void *addr, *buf = NULL;
int rcode = 1;
int fd = -1;
void (*func)(ulong r0, ulong r1, ulong r2, ulong r3);
@@ -55,9 +93,9 @@ static int do_go(int argc, char *argv[])
goto out;
}
- addr = memmap(fd, PROT_READ);
+ addr = go_map(fd, &buf);
if (addr == MAP_FAILED) {
- perror("memmap");
+ perror(argv[0]);
goto out;
}
} else
@@ -100,6 +138,7 @@ static int do_go(int argc, char *argv[])
while (1)
;
out:
+ free(buf);
if (fd > 0)
close(fd);
@@ -110,7 +149,8 @@ BAREBOX_CMD_HELP_START(go)
BAREBOX_CMD_HELP_TEXT("Start application at ADDR passing ARG as arguments.")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("If addr does not start with a digit it is interpreted as a filename")
-BAREBOX_CMD_HELP_TEXT("in which case the file is memmapped and executed")
+BAREBOX_CMD_HELP_TEXT("in which case the file is memmapped and executed. Files that can't be")
+BAREBOX_CMD_HELP_TEXT("memmapped at a 4 KiB aligned address are read into memory first.")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-s", "pass all arguments as strings referenced by argc, argv arguments (default)")
BAREBOX_CMD_HELP_OPT("-i", "pass up to four integer arguments using default calling convention")
--
2.47.3
next reply other threads:[~2026-11-08 20:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 20:15 Ahmad Fatoum [this message]
2026-11-09 9:00 ` 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=20261108201605.1401306-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--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