summaryrefslogtreecommitdiff
path: root/test/flash.c
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-04-14 20:01:55 +0800
committerChromeBot <chrome-bot@google.com>2013-04-15 20:40:02 -0700
commit216bb93128c2477d2c4053110e0b0df4ddd0b40f (patch)
tree4378ec4f8c12cf3f572851049518dc8c46608189 /test/flash.c
parent108235225d2536f75a3100cd535f44f732b486c3 (diff)
downloadchrome-ec-216bb93128c2477d2c4053110e0b0df4ddd0b40f.tar.gz
Fix flash tests
This merges flash_overwrite and flash_rw_erase to a single test binary. BUG=chrome-os-partner:18598 TEST=Run on Spring BRANCH=None Change-Id: I1da7577cb5dc196178930dda3a07bb942d959866 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/48090 Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'test/flash.c')
-rw-r--r--test/flash.c207
1 files changed, 200 insertions, 7 deletions
diff --git a/test/flash.c b/test/flash.c
index 1cc40382fd..23623ad59a 100644
--- a/test/flash.c
+++ b/test/flash.c
@@ -9,15 +9,208 @@
#include "console.h"
#include "ec_commands.h"
#include "host_command.h"
-#include "uart.h"
+#include "system.h"
+#include "timer.h"
#include "util.h"
-static int ro_image_size(int argc, char **argv)
+static int error_count;
+
+static int last_write_offset;
+static int last_write_size;
+static char last_write_data[64];
+
+static int last_erase_offset;
+static int last_erase_size;
+
+/*****************************************************************************/
+/* Mock functions */
+void host_send_response(struct host_cmd_handler_args *args)
+{
+ /* Do nothing */
+}
+
+int flash_write(int offset, int size, const char *data)
+{
+ last_write_offset = offset;
+ last_write_size = size;
+ memcpy(last_write_data, data, size);
+ return EC_SUCCESS;
+}
+
+int flash_erase(int offset, int size)
+{
+ last_erase_offset = offset;
+ last_erase_size = size;
+ return EC_SUCCESS;
+}
+
+/*****************************************************************************/
+/* Test utilities */
+#define RUN_TEST(n) \
+ do { \
+ ccprintf("Running %s...", #n); \
+ cflush(); \
+ if (n() == EC_SUCCESS) { \
+ ccputs("OK\n"); \
+ } else { \
+ ccputs("Fail\n"); \
+ error_count++; \
+ } \
+ } while (0)
+
+static void begin_verify(void)
+{
+ last_write_offset = -1;
+ last_write_size = -1;
+ last_write_data[0] = '\0';
+ last_erase_offset = -1;
+ last_erase_size = -1;
+}
+
+static int verify_write(int offset, int size, const char *data)
{
- uart_printf("RO image size = 0x%x\n", CONFIG_SECTION_RO_SIZE);
+ int i;
+
+ if (offset != last_write_offset || size != last_write_size)
+ return EC_ERROR_UNKNOWN;
+ for (i = 0; i < size; ++i)
+ if (data[i] != last_write_data[i])
+ return EC_ERROR_UNKNOWN;
+
return EC_SUCCESS;
}
-DECLARE_CONSOLE_COMMAND(rosize, ro_image_size,
- NULL,
- "Report size of RO image",
- NULL);
+
+#define VERIFY_NO_WRITE(off, sz, d) \
+ do { \
+ begin_verify(); \
+ host_command_write(off, sz, d); \
+ if (last_write_offset != -1 || last_write_size != -1) \
+ return EC_ERROR_UNKNOWN; \
+ } while (0)
+
+#define VERIFY_NO_ERASE(off, sz) \
+ do { \
+ begin_verify(); \
+ host_command_erase(off, sz); \
+ if (last_erase_offset != -1 || last_erase_size != -1) \
+ return EC_ERROR_UNKNOWN; \
+ } while (0)
+
+#define VERIFY_WRITE(off, sz, d) \
+ do { \
+ begin_verify(); \
+ host_command_write(off, sz, d); \
+ if (verify_write(off, sz, d) != EC_SUCCESS) \
+ return EC_ERROR_UNKNOWN; \
+ } while (0)
+
+#define VERIFY_ERASE(off, sz) \
+ do { \
+ begin_verify(); \
+ host_command_erase(off, sz); \
+ if (last_erase_offset != off || last_erase_size != sz) \
+ return EC_ERROR_UNKNOWN; \
+ } while (0)
+
+int host_command_write(int offset, int size, const char *data)
+{
+ struct host_cmd_handler_args args;
+ struct ec_params_flash_write params;
+
+ params.offset = offset;
+ params.size = size;
+ memcpy(params.data, data, size);
+
+ args.version = 0;
+ args.command = EC_CMD_FLASH_WRITE;
+ args.params = &params;
+ args.params_size = sizeof(params);
+ args.response = NULL;
+ args.response_max = 0;
+ args.response_size = 0;
+
+ return host_command_process(&args);
+}
+
+int host_command_erase(int offset, int size)
+{
+ struct host_cmd_handler_args args;
+ struct ec_params_flash_write params;
+
+ params.offset = offset;
+ params.size = size;
+
+ args.version = 0;
+ args.command = EC_CMD_FLASH_ERASE;
+ args.params = &params;
+ args.params_size = sizeof(params);
+ args.response = NULL;
+ args.response_max = 0;
+ args.response_size = 0;
+
+ return host_command_process(&args);
+}
+
+/*****************************************************************************/
+/* Tests */
+static int test_overwrite_current(void)
+{
+ uint32_t offset, size;
+ const char *d = "TestData0000000"; /* 16 bytes */
+
+ /* Test that we cannot overwrite current image */
+ if (system_get_image_copy() == SYSTEM_IMAGE_RO) {
+ offset = CONFIG_FW_RO_OFF;
+ size = CONFIG_FW_RO_SIZE;
+ } else {
+ offset = CONFIG_FW_RW_OFF;
+ size = CONFIG_FW_RW_SIZE;
+ }
+
+ VERIFY_NO_ERASE(offset, sizeof(d));
+ VERIFY_NO_ERASE(offset + size - sizeof(d), sizeof(d));
+ VERIFY_NO_WRITE(offset, sizeof(d), d);
+ VERIFY_NO_WRITE(offset + size - sizeof(d), sizeof(d), d);
+
+ return EC_SUCCESS;
+}
+
+static int test_overwrite_other(void)
+{
+ uint32_t offset, size;
+ const char *d = "TestData0000000"; /* 16 bytes */
+
+ /* Test that we can overwrite the other image */
+ if (system_get_image_copy() == SYSTEM_IMAGE_RW) {
+ offset = CONFIG_FW_RO_OFF;
+ size = CONFIG_FW_RO_SIZE;
+ } else {
+ offset = CONFIG_FW_RW_OFF;
+ size = CONFIG_FW_RW_SIZE;
+ }
+
+ VERIFY_ERASE(offset, sizeof(d));
+ VERIFY_ERASE(offset + size - sizeof(d), sizeof(d));
+ VERIFY_WRITE(offset, sizeof(d), d);
+ VERIFY_WRITE(offset + size - sizeof(d), sizeof(d), d);
+
+ return EC_SUCCESS;
+}
+
+static int command_run_test(int argc, char **argv)
+{
+ error_count = 0;
+
+ RUN_TEST(test_overwrite_current);
+ RUN_TEST(test_overwrite_other);
+
+ if (error_count) {
+ ccprintf("Failed %d tests!\n", error_count);
+ return EC_ERROR_UNKNOWN;
+ } else {
+ ccprintf("Pass!\n");
+ return EC_SUCCESS;
+ }
+}
+DECLARE_CONSOLE_COMMAND(runtest, command_run_test,
+ NULL, NULL, NULL);