summaryrefslogtreecommitdiff
path: root/zephyr
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-01-15 12:06:31 -0700
committerCommit Bot <commit-bot@chromium.org>2021-02-03 23:49:02 +0000
commit41f52b709177f7ddff01f5b7a568c663bc495897 (patch)
tree147857c0886e21b1a8d62c2eef0b8c24dd548ce5 /zephyr
parent236e03723b4eb389a7846257ac5b6fb9e2faab5c (diff)
downloadchrome-ec-41f52b709177f7ddff01f5b7a568c663bc495897.tar.gz
zephyr: flash/system: enable common/flash.c & common/system.c
Bring both flash.c and system.c into the build. zephyr/shim/src/system.c now includes only code that is needed to stub out the chip specific system.c implementation in platform/ec (which is now using the devicetree values). BRANCH=none BUG=b:176828988, b:174481378 TEST=zmake testall TEST=build volteer, flash, and test flashinfo. Cq-Depend: chromium:2631353 Change-Id: I5c542fca01dcc4af98401bcbbf402e579cd45e36 Signed-off-by: Yuval Peress <peress@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2649463 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Commit-Queue: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'zephyr')
-rw-r--r--zephyr/Kconfig32
-rw-r--r--zephyr/drivers/cros_flash/Kconfig2
-rw-r--r--zephyr/shim/include/bbram.h5
-rw-r--r--zephyr/shim/include/config_chip.h51
-rw-r--r--zephyr/shim/src/CMakeLists.txt9
-rw-r--r--zephyr/shim/src/system.c281
-rw-r--r--zephyr/shim/src/tasks.c14
-rw-r--r--zephyr/shim/src/ztest_system.c19
-rw-r--r--zephyr/test/system/CMakeLists.txt4
-rw-r--r--zephyr/test/system/test_system.c274
10 files changed, 205 insertions, 486 deletions
diff --git a/zephyr/Kconfig b/zephyr/Kconfig
index 002dbcaa9c..3051e0831c 100644
--- a/zephyr/Kconfig
+++ b/zephyr/Kconfig
@@ -190,9 +190,9 @@ config PLATFORM_EC_EXTPOWER_GPIO
project should define a GPIO pin named GPIO_AC_PRESENT, with
extpower_interrupt configured as the handler in gpio_map.h.
-# TODO(b/176828988): enable by default once the code can compile
config PLATFORM_EC_FLASH
bool "Enable flash support"
+ default y if FLASH_SIZE > 0
help
Enables access to the device's flash through a simple API. With
this is it possible for the EC to update its flash while running,
@@ -201,6 +201,36 @@ config PLATFORM_EC_FLASH
if PLATFORM_EC_FLASH
+config PLATFORM_EC_PROTECTED_STORAGE_OFF
+ hex "Position of the RO image in Flash memory"
+ default 0x0
+ help
+ Sets the position in flash memory where the RO image begins. This is
+ the address that will be used to start copying the image into RAM.
+
+config PLATFORM_EC_PROTECTED_STORAGE_SIZE
+ hex "Size of the RO image in Flash memory"
+ default 0x40000
+ help
+ The total size of the RO image in flash memory. This will dictate the
+ ending position of the RO image being copied into RAM when combined
+ with PLATFORM_EC_PROTECTED_STORAGE_OFF.
+
+config PLATFORM_EC_WRITABLE_STORAGE_OFF
+ hex "Position of the RW image in Flash memory"
+ default 0x40000
+ help
+ Sets the position in flash memory where the RW image begins. This is
+ the address that will be used to start copying the image into RAM.
+
+config PLATFORM_EC_WRITABLE_STORAGE_SIZE
+ hex "Size of the RW image in Flash memory"
+ default 0x40000
+ help
+ The total size of the RW image in flash memory. This will dictate the
+ ending position of the RW image being copied into RAM when combined
+ with PLATFORM_EC_WRITABLE_STORAGE_OFF.
+
config PLATFORM_EC_CONSOLE_CMD_FLASH
bool "Console commands: flasherase, flashread, flashwp, flashwrite"
default y
diff --git a/zephyr/drivers/cros_flash/Kconfig b/zephyr/drivers/cros_flash/Kconfig
index d1b1fb5e0f..dd4c7e7a9c 100644
--- a/zephyr/drivers/cros_flash/Kconfig
+++ b/zephyr/drivers/cros_flash/Kconfig
@@ -2,10 +2,10 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-# TODO(b/176828988): enable by default once the code can compile
menuconfig CROS_FLASH_NPCX
bool "Nuvoton NPCX flash driver for the Zephyr shim"
depends on SOC_FAMILY_NPCX
+ default y if FLASH_SIZE > 0
help
This option enables a flash unit interface (FIU) driver for the NPCX
chip. This is used instead of the flash memory interface so we can
diff --git a/zephyr/shim/include/bbram.h b/zephyr/shim/include/bbram.h
index c781bc0439..e0ccf88f36 100644
--- a/zephyr/shim/include/bbram.h
+++ b/zephyr/shim/include/bbram.h
@@ -35,4 +35,9 @@ enum bbram_data_index {
BBRM_DATA_INDEX_LCT_TIME = 64,
};
+#define BBRAM_SIZE DT_REG_SIZE(DT_NODELABEL(bbram))
+#define BBRAM_ADDR DT_REG_ADDR(DT_NODELABEL(bbram))
+#define BBRAM(offset) REG8(BBRAM_ADDR + offset)
+#define BBRAM_BKUP_STS BBRAM(CONFIG_BBRAM_BKUP_STS)
+
#endif /* ZEPHYR_SHIM_INCLUDE_BBRAM_H_ */
diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h
index 3e3d076cff..f178447d25 100644
--- a/zephyr/shim/include/config_chip.h
+++ b/zephyr/shim/include/config_chip.h
@@ -170,6 +170,30 @@ enum battery_type {
#endif /* CONFIG_PLATFORM_EC_ESPI */
+#if DT_NODE_EXISTS(DT_NODELABEL(flash0))
+#define CONFIG_PROGRAM_MEMORY_BASE DT_REG_ADDR(DT_NODELABEL(flash0))
+#else
+#define CONFIG_PROGRAM_MEMORY_BASE 0X0
+#endif
+
+#if DT_NODE_EXISTS(DT_NODELABEL(sram0))
+#define CONFIG_RAM_BASE DT_REG_ADDR(DT_NODELABEL(sram0))
+#define CONFIG_DATA_RAM_SIZE DT_REG_SIZE(DT_NODELABEL(sram0))
+#else
+#define CONFIG_RAM_BASE 0x0
+#define CONFIG_DATA_RAM_SIZE 0x0
+#endif
+
+#define CONFIG_RO_MEM_OFF CONFIG_CROS_EC_RO_MEM_OFF
+#define CONFIG_RO_MEM_SIZE CONFIG_CROS_EC_RO_MEM_SIZE
+#define CONFIG_RW_MEM_OFF CONFIG_CROS_EC_RW_MEM_OFF
+#define CONFIG_RW_MEM_SIZE CONFIG_CROS_EC_RW_MEM_SIZE
+
+#define CONFIG_WP_STORAGE_OFF CONFIG_EC_PROTECTED_STORAGE_OFF
+#define CONFIG_WP_STORAGE_SIZE CONFIG_EC_PROTECTED_STORAGE_SIZE
+#define CONFIG_RO_SIZE CONFIG_CROS_EC_RO_SIZE
+#define CONFIG_RW_SIZE CONFIG_CROS_EC_RW_SIZE
+
/* Flash settings */
#undef CONFIG_EXTERNAL_STORAGE
#undef CONFIG_MAPPED_STORAGE
@@ -180,34 +204,27 @@ enum battery_type {
#undef CONFIG_CMD_FLASH
#define CONFIG_FLASH
#define CONFIG_SPI_FLASH_W25Q80 /* Internal SPI flash type. */
-#define CONFIG_FLASH_SIZE_BYTES 0x80000
+#ifdef CONFIG_FLASH_SIZE
+#define CONFIG_FLASH_SIZE_BYTES (CONFIG_FLASH_SIZE * 1024)
+#else
+#define CONFIG_FLASH_SIZE_BYTES 0x0
+#endif
/* TODO(b:176490413): use DT_PROP(DT_INST(inst, DT_DRV_COMPAT), size) ? */
#define CONFIG_MAPPED_STORAGE_BASE 0x64000000
#define CONFIG_FLASH_WRITE_SIZE 0x1 /* minimum write size */
#define CONFIG_FLASH_WRITE_IDEAL_SIZE 256 /* one page size for write */
#define CONFIG_FLASH_ERASE_SIZE 0x1000
#define CONFIG_FLASH_BANK_SIZE CONFIG_FLASH_ERASE_SIZE
-#define CONFIG_EC_PROTECTED_STORAGE_OFF 0
-#define CONFIG_EC_PROTECTED_STORAGE_SIZE 0x40000
-#define CONFIG_EC_WRITABLE_STORAGE_OFF 0x40000
-#define CONFIG_EC_WRITABLE_STORAGE_SIZE 0x40000
-#define CONFIG_WP_STORAGE_OFF CONFIG_EC_PROTECTED_STORAGE_OFF
-#define CONFIG_WP_STORAGE_SIZE CONFIG_EC_PROTECTED_STORAGE_SIZE
-#define CONFIG_RO_SIZE CONFIG_CROS_EC_RO_SIZE
-#define CONFIG_RW_SIZE CONFIG_CROS_EC_RW_SIZE
+#define CONFIG_EC_PROTECTED_STORAGE_OFF CONFIG_PLATFORM_EC_PROTECTED_STORAGE_OFF
+#define CONFIG_EC_PROTECTED_STORAGE_SIZE \
+ CONFIG_PLATFORM_EC_PROTECTED_STORAGE_SIZE
+#define CONFIG_EC_WRITABLE_STORAGE_OFF CONFIG_PLATFORM_EC_WRITABLE_STORAGE_OFF
+#define CONFIG_EC_WRITABLE_STORAGE_SIZE CONFIG_PLATFORM_EC_WRITABLE_STORAGE_SIZE
#define CONFIG_RO_HDR_SIZE 0x40
/* RO image resides at start of protected region, right after header */
#define CONFIG_RO_STORAGE_OFF CONFIG_RO_HDR_SIZE
-#define CONFIG_PROGRAM_MEMORY_BASE DT_REG_ADDR(DT_NODELABEL(flash0))
-#define CONFIG_RAM_BASE DT_REG_ADDR(DT_NODELABEL(sram0))
-#define CONFIG_DATA_RAM_SIZE DT_REG_SIZE(DT_NODELABEL(sram0))
-
-#define CONFIG_RO_MEM_OFF CONFIG_CROS_EC_RO_MEM_OFF
-#define CONFIG_RO_MEM_SIZE CONFIG_CROS_EC_RO_MEM_SIZE
-#define CONFIG_RW_MEM_OFF CONFIG_CROS_EC_RW_MEM_OFF
-#define CONFIG_RW_MEM_SIZE CONFIG_CROS_EC_RW_MEM_SIZE
#define CONFIG_RW_STORAGE_OFF 0
#define CONFIG_RAM_SIZE \
(CONFIG_DATA_RAM_SIZE - CONFIG_PLATFORM_EC_BOOT_RAM_SIZE)
diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt
index c0cf045fd4..1820104d71 100644
--- a/zephyr/shim/src/CMakeLists.txt
+++ b/zephyr/shim/src/CMakeLists.txt
@@ -3,10 +3,16 @@
# found in the LICENSE file.
zephyr_sources(console.c)
+zephyr_sources(crc.c)
zephyr_sources(gpio.c)
zephyr_sources(util.c)
-zephyr_sources(crc.c)
+if (DEFINED CONFIG_ARCH_POSIX)
+ zephyr_sources(ztest_system.c)
+else()
+ zephyr_sources(system.c)
+ zephyr_sources("${PLATFORM_EC}/common/system.c")
+endif()
zephyr_sources_ifdef(no_libgcc libgcc_${ARCH}.S)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_ESPI espi.c)
@@ -19,5 +25,4 @@ zephyr_sources_ifdef(CONFIG_PLATFORM_EC_PWM pwm.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_RTC rtc.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TIMER hwtimer.c)
zephyr_sources_ifdef(CONFIG_PLATFORM_EC_I2C i2c.c)
-zephyr_sources_ifdef(CONFIG_CROS_EC system.c)
zephyr_sources_ifdef(CONFIG_SHIMMED_TASKS tasks.c)
diff --git a/zephyr/shim/src/system.c b/zephyr/shim/src/system.c
index c407410931..a931897903 100644
--- a/zephyr/shim/src/system.c
+++ b/zephyr/shim/src/system.c
@@ -3,217 +3,62 @@
* found in the LICENSE file.
*/
-#include <errno.h>
-#include <string.h>
-#include <sys/util.h>
+#include <device.h>
+#include <drivers/cros_bbram.h>
-#include "chipset.h"
-#include "config.h"
-#include "ec_commands.h"
-#include "panic.h"
-#include "sysjump.h"
+#include "bbram.h"
+#include "common.h"
+#include "cros_version.h"
#include "system.h"
-/* Ongoing actions preventing going into deep-sleep mode. */
-uint32_t sleep_mask;
+STATIC_IF_NOT(CONFIG_ZTEST) const struct device *bbram_dev;
-/* Round up to a multiple of 4. */
-#define ROUNDUP4(x) (((x) + 3) & ~3)
-
-/** Data for an individual jump tag. */
-struct jump_tag {
- /** Tag ID. */
- uint16_t tag;
- /** Size of the data which follows. */
- uint8_t data_size;
- /** Version of the data. */
- uint8_t data_version;
-
- /* Followed by data_size bytes of data. */
-};
-
-/** Jump data (at end of RAM, or preceding panic data). */
-static struct jump_data *jdata;
-
-static enum ec_reboot_cmd reboot_at_shutdown;
-
-STATIC_IF(CONFIG_HIBERNATE) uint32_t hibernate_seconds;
-STATIC_IF(CONFIG_HIBERNATE) uint32_t hibernate_microseconds;
-
-/**
- * The flags set by the reset cause. These will be a combination of
- * EC_RESET_FLAG_*s and will be used to control the logic of initializing the
- * system image.
- */
-static uint32_t reset_flags;
-
-/** Whether or not we've successfully loaded/jumped the current image. */
-static bool jumped_to_image;
-
-/* static void jump_to_image */
-
-void system_common_pre_init(void)
-{
- /* TODO check for watchdog reset. */
-
- /*
- * In testing we will override the jdata address via
- * system_override_jdata.
- */
- if (!IS_ENABLED(CONFIG_ZTEST)) {
- uintptr_t addr = get_panic_data_start();
-
- if (!addr)
- addr = CONFIG_CROS_EC_RAM_BASE +
- CONFIG_CROS_EC_RAM_SIZE;
-
- jdata = (struct jump_data *)(addr - sizeof(struct jump_data));
- }
-
- /* Check jump data if this is a jump between images. */
- if (jdata->magic == JUMP_DATA_MAGIC && jdata->version >= 1) {
- /*
- * Change in jump data struct size between the previous image
- * and this one.
- */
- int delta;
-
- /* Set the flag so others know we jumped. */
- jumped_to_image = true;
- /* Restore the reset_flags. */
- reset_flags = jdata->reset_flags | EC_RESET_FLAG_SYSJUMP;
-
- /*
- * If the jump data structure isn't the same size as the
- * current one, shift the jump tags to immediately before the
- * current jump data structure, to make room for initializing
- * the new fields below.
- */
- delta = sizeof(struct jump_data) - jdata->struct_size;
-
- if (delta && jdata->jump_tag_total) {
- uint8_t *d = (uint8_t *)system_usable_ram_end();
-
- memmove(d, d + delta, jdata->jump_tag_total);
- }
-
- jdata->jump_tag_total = 0;
-
- jdata->reserved0 = 0;
-
- /* Struct size is now the current struct size */
- jdata->struct_size = sizeof(struct jump_data);
-
- /*
- * Clear the jump struct's magic number. This prevents
- * accidentally detecting a jump when there wasn't one, and
- * disallows use of system_add_jump_tag().
- */
- jdata->magic = 0;
- } else {
- memset(jdata, 0, sizeof(struct jump_data));
- }
-}
-
-uintptr_t system_usable_ram_end(void)
-{
- return (uintptr_t)jdata - jdata->jump_tag_total;
-}
-
-int system_add_jump_tag(uint16_t tag, int version, int size, const void *data)
+#if DT_NODE_EXISTS(DT_NODELABEL(bbram))
+static int system_init(const struct device *unused)
{
- struct jump_tag *t;
-
- /* Only allowed during a sysjump. */
- if (!jdata || jdata->magic != JUMP_DATA_MAGIC)
- return -EINVAL;
-
- /* Make room for the new tag. */
- if (size < 0 || size > 0xff)
- return -EINVAL;
- jdata->jump_tag_total += ROUNDUP4(size) + sizeof(struct jump_tag);
-
- t = (struct jump_tag *)system_usable_ram_end();
- t->tag = tag;
- t->data_size = size;
- t->data_version = version;
- if (size)
- memcpy(t + 1, data, size);
+ ARG_UNUSED(unused);
+ bbram_dev = device_get_binding(DT_LABEL(DT_NODELABEL(bbram)));
return 0;
}
-const uint8_t *system_get_jump_tag(uint16_t tag, int *version, int *size)
-{
- const struct jump_tag *t;
- int used = 0;
-
- if (!jdata)
- return NULL;
-
- /* Search through tag data for a match. */
- while (used < jdata->jump_tag_total) {
- /* Check the next tag. */
- t = (const struct jump_tag *)(system_usable_ram_end() + used);
- used += sizeof(struct jump_tag) + ROUNDUP4(t->data_size);
- if (t->tag != tag)
- continue;
-
- /* Found a match. */
- if (size)
- *size = t->data_size;
- if (version)
- *version = t->data_version;
-
- return (const uint8_t *)(t + 1);
- }
-
- /* If we got here, there was no match. */
- return NULL;
-}
+SYS_INIT(system_init, PRE_KERNEL_1, 50);
+#endif
-void system_encode_save_flags(int reset_flags, uint32_t *save_flags)
+/* Return true if index is stored as a single byte in bbram */
+static int bbram_is_byte_access(enum bbram_data_index index)
{
- *save_flags = 0;
-
- /* Save current reset reasons if necessary. */
- if (reset_flags & SYSTEM_RESET_PRESERVE_FLAGS)
- *save_flags = system_get_reset_flags() |
- EC_RESET_FLAG_PRESERVED;
-
- /* Add in AP off flag into saved flags. */
- if (reset_flags & SYSTEM_RESET_LEAVE_AP_OFF)
- *save_flags |= EC_RESET_FLAG_AP_OFF;
-
- /* Add in stay in RO flag into saved flags. */
- if (reset_flags & SYSTEM_RESET_STAY_IN_RO)
- *save_flags |= EC_RESET_FLAG_STAY_IN_RO;
-
- /* Save reset flag. */
- if (reset_flags & (SYSTEM_RESET_HARD | SYSTEM_RESET_WAIT_EXT))
- *save_flags |= EC_RESET_FLAG_HARD;
- else
- *save_flags |= EC_RESET_FLAG_SOFT;
+ return index == BBRM_DATA_INDEX_PD0 || index == BBRM_DATA_INDEX_PD1 ||
+ index == BBRM_DATA_INDEX_PD2 ||
+ index == BBRM_DATA_INDEX_PANIC_FLAGS;
}
-uint32_t system_get_reset_flags(void)
+/* Map idx to a returned BBRM_DATA_INDEX_*, or return -1 on invalid idx */
+static int bbram_idx_lookup(enum system_bbram_idx idx)
{
- return reset_flags;
+ if (idx == SYSTEM_BBRAM_IDX_PD0)
+ return BBRM_DATA_INDEX_PD0;
+ if (idx == SYSTEM_BBRAM_IDX_PD1)
+ return BBRM_DATA_INDEX_PD1;
+ if (idx == SYSTEM_BBRAM_IDX_PD2)
+ return BBRM_DATA_INDEX_PD2;
+ if (idx == SYSTEM_BBRAM_IDX_TRY_SLOT)
+ return BBRM_DATA_INDEX_TRY_SLOT;
+ return -1;
}
-void system_set_reset_flags(uint32_t flags)
+int system_get_bbram(enum system_bbram_idx idx, uint8_t *value)
{
- reset_flags |= flags;
-}
+ int bbram_idx = bbram_idx_lookup(idx);
+ int bytes, rc;
-void system_clear_reset_flags(uint32_t flags)
-{
- reset_flags &= ~flags;
-}
+ if (bbram_idx < 0 || bbram_dev == NULL)
+ return EC_ERROR_INVAL;
-int system_jumped_to_this_image(void)
-{
- return jumped_to_image;
+ bytes = bbram_is_byte_access(bbram_idx) ? 1 : 4;
+ rc = ((struct cros_bbram_driver_api *)bbram_dev->api)
+ ->read(bbram_dev, bbram_idx, bytes, value);
+ return rc ? EC_ERROR_INVAL : EC_SUCCESS;
}
void system_hibernate(uint32_t seconds, uint32_t microseconds)
@@ -224,57 +69,19 @@ void system_hibernate(uint32_t seconds, uint32_t microseconds)
*/
}
-__test_only void system_common_reset_state(void)
+const char *system_get_chip_vendor(void)
{
- jdata = 0;
- reset_flags = 0;
- jumped_to_image = false;
+ return "chromeos";
}
-__test_only void system_override_jdata(void *test_jdata)
+const char *system_get_chip_name(void)
{
- jdata = (struct jump_data *)test_jdata;
+ return "emu";
}
-void system_enter_hibernate(uint32_t seconds, uint32_t microseconds)
+const char *system_get_chip_revision(void)
{
- if (!IS_ENABLED(CONFIG_HIBERNATE))
- return;
-
- /*
- * If chipset is already off, then call system_hibernate directly. Else,
- * let chipset_task bring down the power rails and transition to proper
- * state before system_hibernate is called.
- */
- if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
- system_hibernate(seconds, microseconds);
- } else {
- reboot_at_shutdown = EC_REBOOT_HIBERNATE;
- hibernate_seconds = seconds;
- hibernate_microseconds = microseconds;
-
- chipset_force_shutdown(CHIPSET_SHUTDOWN_CONSOLE_CMD);
- }
-}
-
-int system_jumped_late(void)
-{
- return !(reset_flags & EC_RESET_FLAG_EFS) && jumped_to_image;
-}
-
-/* TODO(b/176171847): Implement these stubs fully */
-int system_is_locked(void)
-{
- return 0;
-}
-
-enum ec_image system_get_image_copy(void)
-{
- return EC_IMAGE_UNKNOWN;
+ return "";
}
-int system_is_in_rw(void)
-{
- /* Return true for now, since that makes more things work */
- return true;
-}
+const void *__image_size;
diff --git a/zephyr/shim/src/tasks.c b/zephyr/shim/src/tasks.c
index 435c0d0f55..b8406dd1e6 100644
--- a/zephyr/shim/src/tasks.c
+++ b/zephyr/shim/src/tasks.c
@@ -224,3 +224,17 @@ int init_signals(const struct device *unused)
return 0;
}
SYS_INIT(init_signals, POST_KERNEL, 50);
+
+int task_start_called(void)
+{
+ return 1;
+}
+
+void interrupt_disable(void)
+{
+ /*
+ * TODO (b:174481378) system.c needed an implementation of this. Though
+ * it's not yet clear where we call interrupt_enable() from. These two
+ * calls should be replaced with irq_lock and irq_unlock.
+ */
+}
diff --git a/zephyr/shim/src/ztest_system.c b/zephyr/shim/src/ztest_system.c
new file mode 100644
index 0000000000..8c0450cd7e
--- /dev/null
+++ b/zephyr/shim/src/ztest_system.c
@@ -0,0 +1,19 @@
+/* Copyright 2021 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 "system.h"
+
+/* Ongoing actions preventing going into deep-sleep mode. */
+uint32_t sleep_mask;
+
+int system_add_jump_tag(uint16_t tag, int version, int size, const void *data)
+{
+ return EC_SUCCESS;
+}
+
+const uint8_t *system_get_jump_tag(uint16_t tag, int *version, int *size)
+{
+ return NULL;
+}
diff --git a/zephyr/test/system/CMakeLists.txt b/zephyr/test/system/CMakeLists.txt
index 2afa9d6301..f91786841e 100644
--- a/zephyr/test/system/CMakeLists.txt
+++ b/zephyr/test/system/CMakeLists.txt
@@ -6,5 +6,5 @@ cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(system_test)
-FILE(GLOB app_sources *.c)
-target_sources(app PRIVATE ${app_sources})
+target_sources(app PRIVATE test_system.c
+ ${PLATFORM_EC}/zephyr/shim/src/system.c)
diff --git a/zephyr/test/system/test_system.c b/zephyr/test/system/test_system.c
index e16a43193e..3dc9f95a0f 100644
--- a/zephyr/test/system/test_system.c
+++ b/zephyr/test/system/test_system.c
@@ -3,251 +3,73 @@
* found in the LICENSE file.
*/
-#include <ztest.h>
+#include <device.h>
+#include <drivers/cros_bbram.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");
-}
+#include <ztest.h>
-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;
+#include "bbram.h"
+#include "system.h"
- system_override_jdata(&jump_memory.jdata);
- system_common_pre_init();
+LOG_MODULE_REGISTER(test);
- /* 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 char mock_data[64] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@";
-static void test_common_pre_init_with_delta_struct_size(void)
+static int mock_bbram_read(const struct device *unused, int offset, int size,
+ char *data)
{
- /* 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");
+ if (offset < 0 || offset + size >= ARRAY_SIZE(mock_data))
+ return -1;
+ memcpy(data, mock_data + offset, size);
+ return EC_SUCCESS;
}
-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 const struct cros_bbram_driver_api bbram_api = {
+ .ibbr = NULL,
+ .reset_ibbr = NULL,
+ .vsby = NULL,
+ .reset_vsby = NULL,
+ .vcc1 = NULL,
+ .reset_vcc1 = NULL,
+ .read = mock_bbram_read,
+ .write = NULL,
+};
-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.");
+static const struct device bbram_dev_instance = {
+ .name = "TEST_BBRAM_DEV",
+ .config = NULL,
+ .api = &bbram_api,
+ .data = NULL,
+};
- 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.");
-}
+const struct device *bbram_dev = &bbram_dev_instance;
-static void test_add_jump_tag_fail_size_out_of_bounds(void)
+static void test_bbram_get(void)
{
- system_override_jdata(&jump_memory.jdata);
- system_common_pre_init();
- jump_memory.jdata.magic = JUMP_DATA_MAGIC;
+ uint8_t output[10];
+ int rc;
- 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;
+ rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD0, output);
+ zassert_equal(rc, 0, NULL);
+ zassert_mem_equal(output, mock_data + BBRM_DATA_INDEX_PD0, 1, NULL);
- system_override_jdata(&jump_memory.jdata);
- system_common_pre_init();
- jump_memory.jdata.magic = JUMP_DATA_MAGIC;
+ rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD1, output);
+ zassert_equal(rc, 0, NULL);
+ zassert_mem_equal(output, mock_data + BBRM_DATA_INDEX_PD1, 1, NULL);
- zassert_equal(system_add_jump_tag(tag, version, sizeof(uint16_t),
- &data),
- 0, "Expected add_jump_tag to return 0");
+ rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD2, output);
+ zassert_equal(rc, 0, NULL);
+ zassert_mem_equal(output, mock_data + BBRM_DATA_INDEX_PD2, 1, NULL);
- 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));
+ rc = system_get_bbram(SYSTEM_BBRAM_IDX_TRY_SLOT, output);
+ zassert_equal(rc, 0, NULL);
+ zassert_mem_equal(output, mock_data + BBRM_DATA_INDEX_TRY_SLOT, 1,
+ NULL);
}
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_test_suite(system, ztest_unit_test(test_bbram_get));
ztest_run_test_suite(system);
}