summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2022-08-24 09:19:08 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-08-24 23:06:09 +0000
commit0db8acf994dd4a431c216c0a01bfaa5d08598c1b (patch)
tree58efe3c3c6e40593aa6b40f0dcaf3097e6490a9d
parenta257b68f5dc6206da25469e605de8feded52b8de (diff)
downloadchrome-ec-0db8acf994dd4a431c216c0a01bfaa5d08598c1b.tar.gz
zephyr: emul: Implement flash protection
Implement a simple flash protection for RO and ALL. BRANCH=None BUG=b:236075598 TEST=The following CL tests the functions. Change-Id: I0eec87d7ebbf776a676103ae26df5460d3685431 Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3854310 Reviewed-by: Yuval Peress <peress@google.com>
-rw-r--r--zephyr/emul/emul_flash.c59
-rw-r--r--zephyr/include/emul/emul_flash.h20
2 files changed, 72 insertions, 7 deletions
diff --git a/zephyr/emul/emul_flash.c b/zephyr/emul/emul_flash.c
index e3c72c1c3d..49130d2d32 100644
--- a/zephyr/emul/emul_flash.c
+++ b/zephyr/emul/emul_flash.c
@@ -14,6 +14,8 @@ LOG_MODULE_REGISTER(emul_flash);
#include <drivers/cros_flash.h>
#include <zephyr/sys/__assert.h>
+#include "flash.h"
+
struct flash_emul_data {};
struct flash_emul_cfg {
@@ -21,6 +23,9 @@ struct flash_emul_cfg {
struct flash_emul_data *data;
};
+/* Variables to emulate the protection */
+bool ro_protected, all_protected;
+
void system_jump_to_booter(void)
{
}
@@ -60,26 +65,66 @@ static int cros_flash_emul_erase(const struct device *dev, int offset, int size)
static int cros_flash_emul_get_protect(const struct device *dev, int bank)
{
- __ASSERT(false, "Not implemented");
- return -EINVAL;
+ if (all_protected) {
+ return EC_ERROR_ACCESS_DENIED;
+ }
+ if (ro_protected && bank >= WP_BANK_OFFSET &&
+ bank < WP_BANK_OFFSET + WP_BANK_COUNT) {
+ return EC_ERROR_ACCESS_DENIED;
+ }
+
+ return EC_SUCCESS;
}
static uint32_t cros_flash_emul_get_protect_flags(const struct device *dev)
{
- return EC_FLASH_PROTECT_ERROR_UNKNOWN;
+ uint32_t flags = 0;
+
+ if (ro_protected) {
+ flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
+ }
+ if (all_protected) {
+ flags |= EC_FLASH_PROTECT_ALL_NOW;
+ }
+ return flags;
}
static int cros_flash_emul_protect_at_boot(const struct device *dev,
uint32_t new_flags)
{
- __ASSERT(false, "Not implemented");
- return -EINVAL;
+ if ((new_flags & (EC_FLASH_PROTECT_RO_AT_BOOT |
+ EC_FLASH_PROTECT_ALL_AT_BOOT)) == 0) {
+ /* Clear protection if allowed */
+ if (crec_flash_get_protect() & EC_FLASH_PROTECT_GPIO_ASSERTED) {
+ return EC_ERROR_ACCESS_DENIED;
+ }
+
+ ro_protected = all_protected = false;
+ return EC_SUCCESS;
+ }
+
+ ro_protected = true;
+
+ if (new_flags & EC_FLASH_PROTECT_ALL_AT_BOOT) {
+ all_protected = true;
+ }
+
+ return EC_SUCCESS;
}
static int cros_flash_emul_protect_now(const struct device *dev, int all)
{
- __ASSERT(false, "Not implemented");
- return -EINVAL;
+ /* Emulate ALL_NOW only */
+ if (all) {
+ all_protected = true;
+ }
+
+ return EC_SUCCESS;
+}
+
+void cros_flash_emul_protect_reset(void)
+{
+ ro_protected = all_protected = false;
}
static int cros_flash_emul_get_jedec_id(const struct device *dev,
diff --git a/zephyr/include/emul/emul_flash.h b/zephyr/include/emul/emul_flash.h
new file mode 100644
index 0000000000..7316a8496d
--- /dev/null
+++ b/zephyr/include/emul/emul_flash.h
@@ -0,0 +1,20 @@
+/* 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.
+ */
+
+/**
+ * @file
+ *
+ * @brief Backend API for Cros flash emulator
+ */
+
+#ifndef ZEPHYR_INCLUDE_EMUL_EMUL_FLASH_H_
+#define ZEPHYR_INCLUDE_EMUL_EMUL_FLASH_H_
+
+/**
+ * @brief Reset the protection.
+ */
+void cros_flash_emul_protect_reset(void);
+
+#endif /* ZEPHYR_INCLUDE_EMUL_EMUL_FLASH_H_ */