From 5c2859cdc30287b3593d9df88f48c31eecb0bbed Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 10 Nov 2013 10:27:03 -0700 Subject: sandbox: Allow reading/writing of RAM buffer It is useful to be able to save and restore the RAM contents of sandbox U-Boot either for setting up tests, for later analysys, or for chaining together multiple tests which need to keep the same memory contents. Add a function to provide a memory file for U-Boot. This is read on start-up and written when shutting down. If the file does not exist on start-up, it will be created when shutting down. Signed-off-by: Simon Glass Signed-off-by: Simon Glass --- arch/sandbox/cpu/cpu.c | 4 ++++ arch/sandbox/cpu/os.c | 39 +++++++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 22 ++++++++++++++++++++-- arch/sandbox/cpu/state.c | 22 ++++++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) (limited to 'arch/sandbox/cpu') diff --git a/arch/sandbox/cpu/cpu.c b/arch/sandbox/cpu/cpu.c index bc7641a808..38019e0b48 100644 --- a/arch/sandbox/cpu/cpu.c +++ b/arch/sandbox/cpu/cpu.c @@ -5,11 +5,15 @@ #include #include +#include DECLARE_GLOBAL_DATA_PTR; void reset_cpu(ulong ignored) { + if (state_uninit()) + os_exit(2); + /* This is considered normal termination for now */ os_exit(0); } diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index ef6a651a60..725b505177 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -399,3 +399,42 @@ void os_puts(const char *str) while (*str) os_putc(*str++); } + +int os_write_ram_buf(const char *fname) +{ + struct sandbox_state *state = state_get_current(); + int fd, ret; + + fd = open(fname, O_CREAT | O_WRONLY, 0777); + if (fd < 0) + return -ENOENT; + ret = write(fd, state->ram_buf, state->ram_size); + close(fd); + if (ret != state->ram_size) + return -EIO; + + return 0; +} + +int os_read_ram_buf(const char *fname) +{ + struct sandbox_state *state = state_get_current(); + int fd, ret; + int size; + + size = os_get_filesize(fname); + if (size < 0) + return -ENOENT; + if (size != state->ram_size) + return -ENOSPC; + fd = open(fname, O_RDONLY); + if (fd < 0) + return -ENOENT; + + ret = read(fd, state->ram_buf, state->ram_size); + close(fd); + if (ret != state->ram_size) + return -EIO; + + return 0; +} diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index 579ece4471..452f2a98f3 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -4,12 +4,11 @@ */ #include +#include #include #include #include -#include - DECLARE_GLOBAL_DATA_PTR; int sandbox_early_getopt_check(void) @@ -108,6 +107,25 @@ static int sandbox_cmdline_cb_interactive(struct sandbox_state *state, SANDBOX_CMDLINE_OPT_SHORT(interactive, 'i', 0, "Enter interactive mode"); +static int sandbox_cmdline_cb_memory(struct sandbox_state *state, + const char *arg) +{ + int err; + + /* For now assume we always want to write it */ + state->write_ram_buf = true; + state->ram_buf_fname = arg; + + if (os_read_ram_buf(arg)) { + printf("Failed to read RAM buffer\n"); + return err; + } + + return 0; +} +SANDBOX_CMDLINE_OPT_SHORT(memory, 'm', 1, + "Read/write ram_buf memory contents from file"); + int main(int argc, char *argv[]) { struct sandbox_state *state; diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index 56d5041411..0380fe2791 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -4,6 +4,7 @@ */ #include +#include #include /* Main state record for the sandbox */ @@ -25,6 +26,10 @@ int state_init(void) { state = &main_state; + state->ram_size = CONFIG_SYS_SDRAM_SIZE; + state->ram_buf = os_malloc(state->ram_size); + assert(state->ram_buf); + /* * Example of how to use GPIOs: * @@ -33,3 +38,20 @@ int state_init(void) */ return 0; } + +int state_uninit(void) +{ + int err; + + state = &main_state; + + if (state->write_ram_buf) { + err = os_write_ram_buf(state->ram_buf_fname); + if (err) { + printf("Failed to write RAM buffer\n"); + return err; + } + } + + return 0; +} -- cgit