[Concept,09/16] tkey: Provide some back-door functions for TKey tests
Commit Message
From: Simon Glass <simon.glass@canonical.com>
The tests need to reset the emulated TKey and put it into a known state.
Add a few more functions to help with this.
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
drivers/misc/tkey_emul.c | 41 ++++++++++++++++++++++++++++++++++++++++
include/tkey.h | 36 +++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)
@@ -262,6 +262,47 @@ static int tkey_emul_read_all(struct udevice *dev, void *buf, int maxlen,
return len;
}
+int tkey_emul_reset_for_test(struct udevice *dev)
+{
+ struct tkey_emul_priv *priv = dev_get_priv(dev);
+
+ /* Reset to firmware mode */
+ priv->app_loaded = false;
+ priv->total_loaded = 0;
+ priv->resp_len = 0;
+ log_debug("Reset emulator to firmware mode\n");
+
+ return 0;
+}
+
+int tkey_emul_set_pubkey_for_test(struct udevice *dev, const void *pubkey)
+{
+ struct tkey_emul_priv *priv = dev_get_priv(dev);
+
+ memcpy(priv->pubkey, pubkey, 32);
+ log_debug("Set test pubkey\n");
+
+ return 0;
+}
+
+int tkey_emul_set_app_mode_for_test(struct udevice *dev, bool app_mode)
+{
+ /*
+ * Only set app_loaded if device is active (has priv data).
+ * After device_remove(), priv is freed, so we can't access it.
+ * When device is re-probed, it will start in firmware mode by default.
+ */
+ if (device_active(dev)) {
+ struct tkey_emul_priv *priv = dev_get_priv(dev);
+
+ priv->app_loaded = app_mode;
+ }
+
+ log_debug("Set emulator to %s mode\n", app_mode ? "app" : "firmware");
+
+ return 0;
+}
+
int tkey_emul_set_connected_for_test(struct udevice *dev, bool connected)
{
struct tkey_emul_plat *plat = dev_get_plat(dev);
@@ -278,6 +278,42 @@ int tkey_derive_disk_key(struct udevice *dev, const void *app_data,
int tkey_derive_wrapping_key(struct udevice *dev, const char *password,
void *wrapping_key);
+/**
+ * tkey_emul_reset_for_test() - Reset emulator to firmware mode for testing
+ *
+ * This is a back-door function for tests to simulate TKey replug by resetting
+ * the emulator to firmware mode. Only works with tkey-emul driver.
+ *
+ * @dev: TKey device (must be tkey-emul)
+ * Return: 0 on success, -ve error on failure
+ */
+int tkey_emul_reset_for_test(struct udevice *dev);
+
+/**
+ * tkey_emul_set_pubkey_for_test() - Set public key returned by emulator
+ *
+ * This is a back-door function for tests to configure what public key the
+ * emulator returns from APP_CMD_GET_PUBKEY. The disk key will be derived
+ * from this pubkey using SHA256(hex(pubkey)).
+ *
+ * @dev: TKey device (must be tkey-emul)
+ * @pubkey: Public key to return (32 bytes)
+ * Return: 0 on success, -ve error on failure
+ */
+int tkey_emul_set_pubkey_for_test(struct udevice *dev, const void *pubkey);
+
+/**
+ * tkey_emul_set_app_mode_for_test() - Set emulator mode for testing
+ *
+ * This is a back-door function for tests to force the emulator into app mode
+ * or firmware mode, allowing tests to verify replug behavior.
+ *
+ * @dev: TKey device (must be tkey-emul)
+ * @app_mode: true for app mode, false for firmware mode
+ * Return: 0 on success, -ve error on failure
+ */
+int tkey_emul_set_app_mode_for_test(struct udevice *dev, bool app_mode);
+
/**
* tkey_emul_set_connected_for_test() - Simulate device connection state
*