summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2022-08-24 09:21:46 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-08-25 00:34:33 +0000
commit0f8843fb36cb619bdd14a3232851600cee3eac10 (patch)
treef3fceb8187ad8aeb19082a6eab15de8a1c19c508
parent3c2449bcf7f2c818247edc329fc6457fb2e40a0e (diff)
downloadchrome-ec-0f8843fb36cb619bdd14a3232851600cee3eac10.tar.gz
zephyr: test: Add flash protect host command test cases
Use GPIO emulator to change the WP_L assertion state and verify the flash protection sequence, e.g. enable/disable RO protection, enable/disable ALL protection. BRANCH=None BUG=b:236075598 TEST=./twister -s zephyr/test/drivers/drivers.default Change-Id: If287b3d302278fc9f3ccf9be287179435473721b Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3854311 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r--zephyr/test/drivers/default/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/default/src/flash.c157
2 files changed, 158 insertions, 0 deletions
diff --git a/zephyr/test/drivers/default/CMakeLists.txt b/zephyr/test/drivers/default/CMakeLists.txt
index 5ba4f95a7d..98327490de 100644
--- a/zephyr/test/drivers/default/CMakeLists.txt
+++ b/zephyr/test/drivers/default/CMakeLists.txt
@@ -30,6 +30,7 @@ target_sources(app PRIVATE
src/console_cmd/waitms.c
src/cros_cbi.c
src/espi.c
+ src/flash.c
src/gpio.c
src/host_cmd/battery_cut_off.c
src/host_cmd/get_pd_port_caps.c
diff --git a/zephyr/test/drivers/default/src/flash.c b/zephyr/test/drivers/default/src/flash.c
new file mode 100644
index 0000000000..67ecff82bd
--- /dev/null
+++ b/zephyr/test/drivers/default/src/flash.c
@@ -0,0 +1,157 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/drivers/emul.h>
+#include <zephyr/drivers/gpio.h>
+#include <zephyr/drivers/gpio/gpio_emul.h>
+#include <zephyr/zephyr.h>
+#include <zephyr/ztest.h>
+
+#include "ec_commands.h"
+#include "emul/emul_flash.h"
+#include "host_command.h"
+#include "test/drivers/test_state.h"
+
+#define WP_L_GPIO_PATH DT_PATH(named_gpios, wp_l)
+
+static int gpio_wp_l_set(int value)
+{
+ const struct device *wp_l_gpio_dev =
+ DEVICE_DT_GET(DT_GPIO_CTLR(WP_L_GPIO_PATH, gpios));
+
+ return gpio_emul_input_set(wp_l_gpio_dev,
+ DT_GPIO_PIN(WP_L_GPIO_PATH, gpios), value);
+}
+
+ZTEST_USER(flash, test_hostcmd_flash_protect_wp_asserted)
+{
+ struct ec_response_flash_protect response;
+ struct ec_params_flash_protect params = {
+ .mask = 0,
+ .flags = 0,
+ };
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params);
+ /* The original flags not 0 as GPIO WP_L asserted */
+ uint32_t expected_flags = EC_FLASH_PROTECT_GPIO_ASSERTED;
+
+ /* Get the flash protect */
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Enable RO_AT_BOOT */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
+ expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = 0;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Enable ALL_NOW */
+ params.mask = EC_FLASH_PROTECT_ALL_NOW;
+ params.flags = EC_FLASH_PROTECT_ALL_NOW;
+ expected_flags |= EC_FLASH_PROTECT_ALL_NOW;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Disable ALL_NOW; should change nothing as GPIO WP_L is asserted */
+ params.mask = EC_FLASH_PROTECT_ALL_NOW;
+ params.flags = 0;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = 0;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+}
+
+ZTEST_USER(flash, test_hostcmd_flash_protect_wp_deasserted)
+{
+ struct ec_response_flash_protect response;
+ struct ec_params_flash_protect params = {
+ .mask = 0,
+ .flags = 0,
+ };
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params);
+ /* The original flags 0 as GPIO WP_L deasserted */
+ uint32_t expected_flags = 0;
+
+ zassert_ok(gpio_wp_l_set(1), NULL);
+
+ /* Get the flash protect */
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Enable RO_AT_BOOT */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
+ expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Disable RO_AT_BOOT */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = 0;
+ expected_flags &=
+ ~(EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW);
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Enable RO_AT_BOOT */
+ params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
+ params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
+ expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+
+ /* Enable ALL_NOW; should change nothing as GPIO WP_L is deasserted */
+ params.mask = EC_FLASH_PROTECT_ALL_NOW;
+ params.flags = EC_FLASH_PROTECT_ALL_NOW;
+ zassert_ok(host_command_process(&args), NULL);
+ zassert_equal(response.flags, expected_flags, "response.flags = %d",
+ response.flags);
+}
+
+static void flash_reset(void)
+{
+ /* Set the GPIO WP_L to default */
+ gpio_wp_l_set(0);
+
+ /* Reset the protection flags */
+ cros_flash_emul_protect_reset();
+}
+
+static void flash_before(void *data)
+{
+ ARG_UNUSED(data);
+ flash_reset();
+}
+
+static void flash_after(void *data)
+{
+ ARG_UNUSED(data);
+ flash_reset();
+}
+
+ZTEST_SUITE(flash, drivers_predicate_post_main, NULL, flash_before, flash_after,
+ NULL);