summaryrefslogtreecommitdiff
path: root/arch/sandbox/cpu
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-09-07 05:27:08 +0300
committerTom Rini <trini@konsulko.com>2022-09-29 23:09:56 +0300
commit73c5cb9dacbcf2e988fffa66750c4206fccc8cbc (patch)
tree2665b1f5aaf1519c9600694ff0be67da23e9de58 /arch/sandbox/cpu
parent2b90e0d54efe6e83c9313c863184b71eb811853e (diff)
downloadu-boot-73c5cb9dacbcf2e988fffa66750c4206fccc8cbc.tar.xz
sandbox: Add a function to load a relative file path
At present this implementation is specific to loading the test FDT. We plan to load others, so create a generic function to handle this. The path is now limited to 256 characters, to simplify the code. When there is an empty argv[0] (which should not happen), the function now just uses the path as is, with no prefix. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'arch/sandbox/cpu')
-rw-r--r--arch/sandbox/cpu/start.c16
-rw-r--r--arch/sandbox/cpu/state.c22
2 files changed, 29 insertions, 9 deletions
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 90a84e93c7..642be164a3 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -205,21 +205,19 @@ SANDBOX_CMDLINE_OPT_SHORT(default_fdt, 'D', 0,
static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
const char *arg)
{
- const char *fmt = "/arch/sandbox/dts/test.dtb";
- char *p;
+ char buf[256];
char *fname;
int len;
- len = strlen(state->argv[0]) + strlen(fmt) + 1;
+ len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf,
+ sizeof(buf));
+ if (len < 0)
+ return len;
+
fname = os_malloc(len);
if (!fname)
return -ENOMEM;
- strcpy(fname, state->argv[0]);
- p = strrchr(fname, '/');
- if (!p)
- p = fname + strlen(fname);
- len -= p - fname;
- snprintf(p, len, fmt);
+ strcpy(fname, buf);
state->fdt_fname = fname;
return 0;
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
index e0d01125bb..787e021659 100644
--- a/arch/sandbox/cpu/state.c
+++ b/arch/sandbox/cpu/state.c
@@ -396,6 +396,28 @@ bool autoboot_set_keyed(bool autoboot_keyed)
return old_val;
}
+int state_get_rel_filename(const char *rel_path, char *buf, int size)
+{
+ struct sandbox_state *state = state_get_current();
+ int rel_len, prog_len;
+ char *p;
+ int len;
+
+ rel_len = strlen(rel_path);
+ p = strrchr(state->argv[0], '/');
+ prog_len = p ? p - state->argv[0] : 0;
+
+ /* allow space for a / and a terminator */
+ len = prog_len + 1 + rel_len + 1;
+ if (len > size)
+ return -ENOSPC;
+ strncpy(buf, state->argv[0], prog_len);
+ buf[prog_len] = '/';
+ strcpy(buf + prog_len + 1, rel_path);
+
+ return len;
+}
+
int state_init(void)
{
state = &main_state;