diff options
-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); |