summaryrefslogtreecommitdiff
path: root/arch/sandbox/cpu/state.c
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-09-06 20:27:08 -0600
committerTom Rini <trini@konsulko.com>2022-09-29 16:09:56 -0400
commit73c5cb9dacbcf2e988fffa66750c4206fccc8cbc (patch)
tree2665b1f5aaf1519c9600694ff0be67da23e9de58 /arch/sandbox/cpu/state.c
parent2b90e0d54efe6e83c9313c863184b71eb811853e (diff)
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/state.c')
-rw-r--r--arch/sandbox/cpu/state.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
index e0d01125bb5..787e0216594 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;