diff options
author | Vic Yang <victoryang@chromium.org> | 2013-05-08 00:21:03 +0800 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-05-07 16:02:30 -0700 |
commit | 0d99eadd7783a8b0a76241f36e3b2911526a7c8c (patch) | |
tree | 261b79811c08d480f6d72509c1d116cb6dd0dacc | |
parent | 0a45fa17086d4556b7cb4ea0a9f53894197bc897 (diff) | |
download | chrome-ec-0d99eadd7783a8b0a76241f36e3b2911526a7c8c.tar.gz |
Add persistent storage for emulator
This is needed for non-volatile register emulation. Also, this can be
used to implement system jump or reset flags.
BUG=chrome-os-partner:19235
TEST=Run utils test. Check persistent storage file exists.
BRANCH=None
Change-Id: I699f95718ef6f5de6c3bbb4e37619ee015fb6c4a
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/50313
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r-- | chip/host/build.mk | 2 | ||||
-rw-r--r-- | chip/host/persistence.c | 42 | ||||
-rw-r--r-- | chip/host/persistence.h | 17 | ||||
-rw-r--r-- | chip/host/system.c | 29 | ||||
-rw-r--r-- | test/utils.c | 10 |
5 files changed, 99 insertions, 1 deletions
diff --git a/chip/host/build.mk b/chip/host/build.mk index ddcbefdb0b..110daefb8b 100644 --- a/chip/host/build.mk +++ b/chip/host/build.mk @@ -8,5 +8,5 @@ CORE:=host -chip-y=system.o gpio.o uart.o +chip-y=system.o gpio.o uart.o persistence.o chip-$(HAS_TASK_KEYSCAN)+=keyboard_raw.o diff --git a/chip/host/persistence.c b/chip/host/persistence.c new file mode 100644 index 0000000000..bf3ce01788 --- /dev/null +++ b/chip/host/persistence.c @@ -0,0 +1,42 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Persistence module for emulator */ + +#include <unistd.h> +#include <stdio.h> +#include <string.h> + +#define BUF_SIZE 1024 + +static void get_storage_path(char *out) +{ + char buf[BUF_SIZE]; + + readlink("/proc/self/exe", buf, BUF_SIZE); + if (snprintf(out, BUF_SIZE, "%s_persist", buf) >= BUF_SIZE) + out[BUF_SIZE - 1] = '\0'; +} + +FILE *get_persistent_storage(const char *tag, const char *mode) +{ + char buf[BUF_SIZE]; + char path[BUF_SIZE]; + + /* + * The persistent storage with tag 'foo' for test 'bar' would + * be named 'bar_persist_foo' + */ + get_storage_path(buf); + if (snprintf(path, BUF_SIZE, "%s_%s", buf, tag) >= BUF_SIZE) + path[BUF_SIZE - 1] = '\0'; + + return fopen(path, mode); +} + +void release_persistent_storage(FILE *ps) +{ + fclose(ps); +} diff --git a/chip/host/persistence.h b/chip/host/persistence.h new file mode 100644 index 0000000000..37dbc5707a --- /dev/null +++ b/chip/host/persistence.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Persistence module for emulator */ + +#ifndef _PERSISTENCE_H +#define _PERSISTENCE_H + +#include <stdio.h> + +FILE *get_persistent_storage(const char *tag, const char *mode); + +void release_persistent_storage(FILE *ps); + +#endif /* _PERSISTENCE_H */ diff --git a/chip/host/system.c b/chip/host/system.c index 50da8db9c0..c290f9ef69 100644 --- a/chip/host/system.c +++ b/chip/host/system.c @@ -9,6 +9,7 @@ #include "host_test.h" #include "system.h" +#include "persistence.h" #define SHARED_MEM_SIZE 512 /* bytes */ char __shared_mem_buf[SHARED_MEM_SIZE]; @@ -63,6 +64,34 @@ int system_set_vbnvcontext(const uint8_t *block) return EC_ERROR_UNIMPLEMENTED; } +int system_set_scratchpad(uint32_t value) +{ + FILE *f = get_persistent_storage("scratchpad", "w"); + + fprintf(f, "%lu", value); + release_persistent_storage(f); + + return EC_SUCCESS; +} + +uint32_t system_get_scratchpad(void) +{ + FILE *f = get_persistent_storage("scratchpad", "r"); + uint32_t value; + int success; + + if (f == NULL) + return 0; + + success = fscanf(f, "%lu", &value); + release_persistent_storage(f); + + if (success) + return value; + else + return 0; +} + int system_usable_ram_end(void) { return (int)(__shared_mem_buf + SHARED_MEM_SIZE); diff --git a/test/utils.c b/test/utils.c index 559e8a0d94..81fe2ef072 100644 --- a/test/utils.c +++ b/test/utils.c @@ -8,6 +8,7 @@ #include "common.h" #include "console.h" #include "shared_mem.h" +#include "system.h" #include "timer.h" #include "util.h" @@ -100,6 +101,14 @@ static int test_shared_mem(void) return EC_SUCCESS; } +static int test_scratchpad(void) +{ + system_set_scratchpad(0xfeedfeed); + TEST_ASSERT(system_get_scratchpad() == 0xfeedfeed); + + return EC_SUCCESS; +} + void run_test(void) { error_count = 0; @@ -110,6 +119,7 @@ void run_test(void) RUN_TEST(test_atoi); RUN_TEST(test_uint64divmod); RUN_TEST(test_shared_mem); + RUN_TEST(test_scratchpad); if (error_count) ccprintf("Failed %d tests!\n", error_count); |