diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2021-01-11 15:39:45 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-01-12 04:51:47 +0000 |
commit | c93e2ed12dec9f6b263091e5a08a4f1c27c1bc7c (patch) | |
tree | 5ae0160662991f52056f842e977b7f2ad1f7ab2c /zephyr/test/system | |
parent | 32b90cdae2520c2bb8e4f68da50575621dc270c2 (diff) | |
download | chrome-ec-c93e2ed12dec9f6b263091e5a08a4f1c27c1bc7c.tar.gz |
zephyr: copy zephyr-chrome/tests/app/ec to platform/ec
The previous Git history can be found here:
https://chromium.googlesource.com/chromiumos/platform/zephyr-chrome/+log/f500629b382077c1160ce9b1333afcf0f9c08d59/tests/app/ec
BUG=b:177003034
BRANCH=none
TEST=zmake testall
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Change-Id: Ia3fa7a2f3bd2861293ef78e53a98ae73257090e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2622898
Commit-Queue: Yuval Peress <peress@chromium.org>
Reviewed-by: Yuval Peress <peress@chromium.org>
Diffstat (limited to 'zephyr/test/system')
-rw-r--r-- | zephyr/test/system/CMakeLists.txt | 12 | ||||
-rw-r--r-- | zephyr/test/system/prj.conf | 22 | ||||
-rw-r--r-- | zephyr/test/system/test_system.c | 253 | ||||
-rw-r--r-- | zephyr/test/system/zmake.yaml | 10 |
4 files changed, 297 insertions, 0 deletions
diff --git a/zephyr/test/system/CMakeLists.txt b/zephyr/test/system/CMakeLists.txt new file mode 100644 index 0000000000..dda6be4a4b --- /dev/null +++ b/zephyr/test/system/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright 2020 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. + +cmake_minimum_required(VERSION 3.13.1) + +set(BOARD native_posix) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(system_test) + +FILE(GLOB app_sources *.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/zephyr/test/system/prj.conf b/zephyr/test/system/prj.conf new file mode 100644 index 0000000000..bcaca677c5 --- /dev/null +++ b/zephyr/test/system/prj.conf @@ -0,0 +1,22 @@ +CONFIG_ZTEST=y +CONFIG_PLATFORM_EC=y +CONFIG_PLATFORM_EC_I2C=n +CONFIG_CROS_EC=y +CONFIG_LOG=y + +# Disable all other shimmed code. +CONFIG_SHIMMED_TASKS=n +CONFIG_PLATFORM_EC_I2C=n +CONFIG_PLATFORM_EC_KEYBOARD=n +CONFIG_PLATFORM_EC_HOSTCMD=n +CONFIG_PLATFORM_EC_TIMER=n + +CONFIG_CROS_EC_PROGRAM_MEMORY_BASE=0x10090000 +CONFIG_CROS_EC_RAM_BASE=0x200c0000 +CONFIG_CROS_EC_DATA_RAM_SIZE=0x00010000 +CONFIG_CROS_EC_RAM_SIZE=0x0000f800 + +CONFIG_CROS_EC_RO_MEM_OFF=0x0 +CONFIG_CROS_EC_RO_SIZE=0xb000 +CONFIG_CROS_EC_RW_MEM_OFF=0xb000 +CONFIG_CROS_EC_RW_SIZE=0x75000 diff --git a/zephyr/test/system/test_system.c b/zephyr/test/system/test_system.c new file mode 100644 index 0000000000..e16a43193e --- /dev/null +++ b/zephyr/test/system/test_system.c @@ -0,0 +1,253 @@ +/* Copyright 2020 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. + */ + +#include <ztest.h> +#include <logging/log.h> +#include "ec_commands.h" +#include "system.h" +#include "sysjump.h" +LOG_MODULE_REGISTER(test); + +#define JUMP_TAG_TOTAL_SIZE 512 + +struct jump_memory { + uint8_t jump_tag_memory[JUMP_TAG_TOTAL_SIZE]; + struct jump_data jdata; +}; + +static struct jump_memory jump_memory; + +static void setup(void) +{ + system_common_reset_state(); + memset(&jump_memory, 0, sizeof(struct jump_memory)); +} + +static void test_set_reset_flags(void) +{ + zassert_equal(system_get_reset_flags(), 0, + "system_get_reset_flags() should be 0 at the start"); + system_set_reset_flags(EC_RESET_FLAG_OTHER); + zassert_equal( + system_get_reset_flags(), EC_RESET_FLAG_OTHER, + "system_get_reset_flags() should match exactly to EC_RESET_FLAG_OTHER"); +} + +static void test_clear_reset_flags(void) +{ + uint32_t flags = EC_RESET_FLAG_OTHER | EC_RESET_FLAG_STAY_IN_RO; + + system_set_reset_flags(flags); + zassert_not_equal(system_get_reset_flags(), 0, + "system_get_reset_flags() should be non-zero"); + /* Remove the reset hard flag. */ + system_clear_reset_flags(EC_RESET_FLAG_OTHER); + zassert_equal(system_get_reset_flags(), EC_RESET_FLAG_STAY_IN_RO, + "system_get_reset_flags() should have removed " + "EC_RESET_FLAG_OTHER after reset."); +} + +static void test_encode_save_flags_preserve(void) +{ + const uint32_t expected_flags = EC_RESET_FLAG_OTHER | + EC_RESET_FLAG_USB_RESUME | + EC_RESET_FLAG_EFS; + uint32_t save_flags; + + system_set_reset_flags(expected_flags); + + /* + * Preserve the existing flags, should add EC_RESET_FLAG_PRESERVED and + * EC_RESET_FLAG_SOFT. + */ + system_encode_save_flags(SYSTEM_RESET_PRESERVE_FLAGS, &save_flags); + zassert_equal(save_flags, + expected_flags | EC_RESET_FLAG_PRESERVED | + EC_RESET_FLAG_SOFT, + "All the reset flags should have been restored."); +} + +static void test_encode_save_flags_translate_system_to_ec(void) +{ + uint32_t save_flags; + + system_encode_save_flags(SYSTEM_RESET_LEAVE_AP_OFF, &save_flags); + zassert_equal( + save_flags, EC_RESET_FLAG_AP_OFF | EC_RESET_FLAG_SOFT, + "Expected save flags to be EC_RESET_FLAG_AP_OFF | EC_RESET_FLAG_SOFT"); + + system_encode_save_flags(SYSTEM_RESET_STAY_IN_RO, &save_flags); + zassert_equal( + save_flags, EC_RESET_FLAG_STAY_IN_RO | EC_RESET_FLAG_SOFT, + "Expected save flags to be EC_RESET_FLAG_STAY_IN_RO | EC_RESET_FLAG_SOFT"); + + system_encode_save_flags(SYSTEM_RESET_HARD, &save_flags); + zassert_equal(save_flags, EC_RESET_FLAG_HARD, + "Expected save flags to be EC_RESET_FLAG_HARD"); + + system_encode_save_flags(SYSTEM_RESET_WAIT_EXT, &save_flags); + zassert_equal(save_flags, EC_RESET_FLAG_HARD, + "Expected save flags to be EC_RESET_FLAG_HARD"); +} + +static void test_common_pre_init_fail_matching_magic_number(void) +{ + /* Put garbage values in test_jdata. */ + jump_memory.jdata.struct_size = sizeof(struct jump_data); + jump_memory.jdata.reset_flags = 0xff; + jump_memory.jdata.version = 3; + jump_memory.jdata.magic = 12345; + + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + + /* Verify that test_jdata was zeroed out. */ + for (size_t i = 0; i < sizeof(struct jump_data); ++i) { + zassert_equal(((uint8_t *)&jump_memory.jdata)[i], 0, + "Expecting byte %d of jdata to be 0.", i); + } +} + +static void test_common_pre_init_with_delta_struct_size(void) +{ + /* Set the old struct size to be 1 smaller than the current one. */ + jump_memory.jdata.struct_size = sizeof(struct jump_data) - 1; + jump_memory.jdata.version = JUMP_DATA_VERSION; + jump_memory.jdata.magic = JUMP_DATA_MAGIC; + jump_memory.jdata.jump_tag_total = JUMP_TAG_TOTAL_SIZE; + + /* + * Since we're telling the system component that the size is 1 smaller + * than it really is it should calculate that the delta is 1 and shift + * all the tags by 1 byte to the left. + */ + jump_memory.jump_tag_memory[1] = 0xff; + + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + + zassert_equal( + jump_memory.jump_tag_memory[0], 0xff, + "Expected byte 0 to have the value from previous position 1 in " + "jump tag memory"); + zassert_equal(jump_memory.jump_tag_memory[1], 0, + "Expected byte 1 to have moved to position 0 in jump tag " + "memory"); +} + +static void test_common_pre_init_resets_jdata_not_jump_tags(void) +{ + jump_memory.jdata.struct_size = sizeof(struct jump_data); + jump_memory.jdata.version = JUMP_DATA_VERSION; + jump_memory.jdata.magic = JUMP_DATA_MAGIC; + jump_memory.jdata.jump_tag_total = JUMP_TAG_TOTAL_SIZE; + jump_memory.jdata.reserved0 = 0xf0; + + for (size_t i = 0; i < JUMP_TAG_TOTAL_SIZE; ++i) + jump_memory.jump_tag_memory[i] = i & 0xff; + + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + + zassert_equal(jump_memory.jdata.jump_tag_total, 0, + "Expected jump_tag_total to be reset to 0"); + zassert_equal(jump_memory.jdata.struct_size, sizeof(struct jump_data), + "Expected struct_size to match sizeof(struct jump_data)"); + zassert_equal(jump_memory.jdata.reserved0, 0, + "Expected the reseved field to be reset to 0"); + zassert_equal(jump_memory.jdata.magic, 0, + "Expected the magic number to be reset to 0"); + + for (size_t i = 0; i < JUMP_TAG_TOTAL_SIZE; ++i) { + zassert_equal( + jump_memory.jump_tag_memory[i], i & 0xff, + "Expected jump_tag_memory[%d] to remain unchanged.", i); + } +} + +static void test_add_jump_tag_fail_no_init(void) +{ + zassert_equal( + system_add_jump_tag(0, 0, 0, NULL), -EINVAL, + "Can't set a jump tag without calling common_pre_init first."); + + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + zassert_equal(system_add_jump_tag(0, 0, 0, NULL), -EINVAL, + "Can't set a jump tag without valid jdata."); +} + +static void test_add_jump_tag_fail_size_out_of_bounds(void) +{ + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + jump_memory.jdata.magic = JUMP_DATA_MAGIC; + + zassert_equal(system_add_jump_tag(0, 0, -1, NULL), -EINVAL, + "Can't set jump tag with negative size"); + zassert_equal(system_add_jump_tag(0, 0, 256, NULL), -EINVAL, + "Can't set jump tag with size > 255"); +} + +static void test_add_jump_tag(void) +{ + const uint16_t data = 0x1234; + uint16_t tag = 0; + int version = 1; + const uint8_t *returned_data; + int returned_size; + + system_override_jdata(&jump_memory.jdata); + system_common_pre_init(); + jump_memory.jdata.magic = JUMP_DATA_MAGIC; + + zassert_equal(system_add_jump_tag(tag, version, sizeof(uint16_t), + &data), + 0, "Expected add_jump_tag to return 0"); + + returned_data = system_get_jump_tag(tag, &version, &returned_size); + zassert_not_null(returned_data, "Failed to get tag data for tag <%u>", + tag); + zassert_equal(version, 1, "Expected version to be 1 but got <%d>", + version); + zassert_equal(returned_size, sizeof(uint16_t), + "Expected returned size to be %u but got <%u>", + sizeof(uint16_t), returned_size); + zassert_equal(*((uint16_t *)returned_data), data, + "Expected returned data to be %u but got <%u>", data, + *((uint16_t *)returned_data)); +} + +void test_main(void) +{ + ztest_test_suite( + system, + ztest_unit_test_setup_teardown(test_set_reset_flags, setup, + unit_test_noop), + ztest_unit_test_setup_teardown(test_clear_reset_flags, setup, + unit_test_noop), + ztest_unit_test_setup_teardown(test_encode_save_flags_preserve, + setup, unit_test_noop), + ztest_unit_test_setup_teardown( + test_encode_save_flags_translate_system_to_ec, setup, + unit_test_noop), + ztest_unit_test_setup_teardown( + test_common_pre_init_fail_matching_magic_number, setup, + unit_test_noop), + ztest_unit_test_setup_teardown( + test_common_pre_init_with_delta_struct_size, setup, + unit_test_noop), + ztest_unit_test_setup_teardown( + test_common_pre_init_resets_jdata_not_jump_tags, setup, + unit_test_noop), + ztest_unit_test_setup_teardown(test_add_jump_tag_fail_no_init, + setup, unit_test_noop), + ztest_unit_test_setup_teardown( + test_add_jump_tag_fail_size_out_of_bounds, setup, + unit_test_noop), + ztest_unit_test_setup_teardown(test_add_jump_tag, setup, + unit_test_noop)); + ztest_run_test_suite(system); +} diff --git a/zephyr/test/system/zmake.yaml b/zephyr/test/system/zmake.yaml new file mode 100644 index 0000000000..d0d40dd473 --- /dev/null +++ b/zephyr/test/system/zmake.yaml @@ -0,0 +1,10 @@ +# Copyright 2020 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. + +board: native_posix +supported-zephyr-versions: + - v2.4 +output-type: elf +toolchain: llvm +is-test: true |