From patchwork Sun Oct 19 07:23:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 632 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760858647; bh=O85K0sGUhuZPseHfHA6LpUsGUjIwVs2nT0gYf1NZaHI=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=Cw0MJ0nQKHFhkxiufj7RvNuSkLJkHM2SsNeduOhu7moePC0NsI0cTLe8QgYAobB6Z dT0rjqWnK3GRrUfxtCCQGkhRMhkdL0qn/7FnrNAYWbwe+xu84SpIt0l0oFi0qcFORk Z0oABm07cRbMS053G9US5Hkx+I2WczmxLs0iqMa/84yS9sMWrvJ3LiPwMGi6Wo36kM rbASaRjxoJxPsiCQ66ZVhH1RFWjJzOVV5JjRFGpVOnR7gVlhpNG8ee0ZdyzdVVfsVq 5uzntq98ar+PdnIc55umqNr4tMhzssN8CVzuT/MMa2735aS9NW5aFJ2RQ8jY2Yw3+/ HmaAPg+8sFaHQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 07B16681C2 for ; Sun, 19 Oct 2025 01:24:07 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id WwSFL8H7EBba for ; Sun, 19 Oct 2025 01:24:06 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760858646; bh=O85K0sGUhuZPseHfHA6LpUsGUjIwVs2nT0gYf1NZaHI=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=OZpNDUMC3+nHhICSamDmWv+ZqqsmalmMXToTQrCloT+6G5ji++QrUDe8IyQQzY1jC fXgu3QjsJZ7lCInEbjm5uJL+3ZJbyYCRPHhg8W0x5A7ACk0hYFOvI2oZlI37IuTHM8 fioQ1/HSB53jMXFxpMhovmf9PK2lAtJW+Q9OrEVe4lQVwd4+4iqJVbvWwDka6FJvcG 23JnaiOUknjX1FdmSpJotEtMMRO0y8QjdjhFTGS5f69dk01Q3Wow5MNrvt5Kgaud6p vv77dlDQYI28WODNQtVnwVasQx0goOQx09Zhue7pXmwN4iyTigSQ889e+xsgzzmeqO LoXobs+bEWF7Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 04387680DD for ; Sun, 19 Oct 2025 01:24:06 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760858644; bh=59E71ur1Kb1slqiVVPJh5tN3Un1EJ1cSL1TnCy7pMw4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hx5I62MTjS6hIxvaySxm1C+OTKWzPk4VkwDbr6V6jtz/ci4SRYwCGvptB3vxqvd3Z pL5T3d9G/UDNKnj4nXPZYsekkCk/wDHyd/G6c0FsRdMHKNGZk694M0OW5MmrZ83t7Z hMWCle3fHdN6TGfvnMcbjosCWncS4sIlE2S9dg9hlqe5ankVJvSdK65DWPAmsq1gT4 IGe4sm6Sl2PeEPo0gqsAMsy5NhN69U+PnYaeqGZsu804heKb4YwKbnAT4wqVk1wZkF 25nr8GkjQvyXaD25RxV3q5OUHMVs3Fw0kGxSPYXl/uKXRH7SQ9PlUDS+gD1ek86p9x ahp5EfT4BWhsQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 79B8D68198; Sun, 19 Oct 2025 01:24:04 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id orjfYghyAhoI; Sun, 19 Oct 2025 01:24:04 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760858644; bh=UuCokSPEBeQQaBSHhGbyMjuDvuHuLlohj1to1WkBHak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=knacEnGPeDw9VqZTxdY88/Kl9LwWRZrOQZKQUh3KTS85NW0uifB7mXevMJ6BFHxyR lLT4kZQs4uk/RHVBQfF5rWdOYVZqGL0bTVlIWQSi93d18DLAGMjH0HfHVbRWfDtUNn 1cFvmoHa7f8kVNjJHFJPO6Y/ojxDkRIAqYir5h8boNJlZK2Y8tHp+NxbVw2qrcdIG/ yXAT5/sPU3URbtqx2vZFQiyHFS7t311R/2N48pRTnUG0JQUiZIgzOMfcK38SF8ps6N vMFzmBIib5t9KlH1ybLU2tN8BQXUypY7q0SjFfnMFNBzo+57yjNjYcg+IfwqgTm+R5 ZvZuiWPVZWDbQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id CC8055E435; Sun, 19 Oct 2025 01:24:03 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Sun, 19 Oct 2025 01:23:08 -0600 Message-ID: <20251019072313.3235339-9-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251019072313.3235339-1-sjg@u-boot.org> References: <20251019072313.3235339-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: DKKKN7UA5BDI4GBEI4HA7GYSJBHKDAX6 X-Message-ID-Hash: DKKKN7UA5BDI4GBEI4HA7GYSJBHKDAX6 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 8/8] tkey: Add a sandbox driver for real TKey hardware List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a sandbox TKey driver that enables communication with physical TKey devices via serial port (/dev/ttyACM0). This allows testing TKey functionality in sandbox with real hardware. The driver: - Opens the configured device path from device tree - Configures TTY parameters using os_tty_set_params() - Implements read/write operations for TKey protocol - Supports both read() and read_all() operations Device tree configuration: tkey-test { compatible = "sandbox,tkey"; sandbox,device-path = "/dev/ttyACM0"; }; Co-developed-by: Claude Signed-off-by: Simon Glass --- arch/sandbox/dts/sandbox.dtsi | 5 + drivers/misc/Makefile | 1 + drivers/misc/tkey_sandbox.c | 170 ++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 drivers/misc/tkey_sandbox.c diff --git a/arch/sandbox/dts/sandbox.dtsi b/arch/sandbox/dts/sandbox.dtsi index 48e34a7aee5..e5d6be633fb 100644 --- a/arch/sandbox/dts/sandbox.dtsi +++ b/arch/sandbox/dts/sandbox.dtsi @@ -356,6 +356,11 @@ clock-frequency = <1000000>; }; + tkey-test { + compatible = "sandbox,tkey"; + sandbox,device-path = "/dev/ttyACM0"; + }; + tpm { compatible = "google,sandbox-tpm"; }; diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 298bc1c0a69..cd7170986ac 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -20,6 +20,7 @@ obj-$(CONFIG_SANDBOX) += p2sb_sandbox.o p2sb_emul.o obj-$(CONFIG_SANDBOX) += swap_case.o ifdef CONFIG_SANDBOX obj-$(CONFIG_TKEY) += tkey_emul.o +obj-$(CONFIG_TKEY) += tkey_sandbox.o endif endif diff --git a/drivers/misc/tkey_sandbox.c b/drivers/misc/tkey_sandbox.c new file mode 100644 index 00000000000..9b26bd36f1b --- /dev/null +++ b/drivers/misc/tkey_sandbox.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2025 Canonical Ltd + * + * Sandbox TKey driver for testing TKey functionality in sandbox + * Communicates with TKey devices via /dev/ttyACM0 + */ + +#define LOG_CATEGORY UCLASS_TKEY + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * struct tkey_sandbox_priv - private information about sandbox + * + * @path: Path to the tkey device, e.g. "/dev/ttyACM0" + * @fd: File descriptor + */ +struct tkey_sandbox_priv { + char *path; + int fd; +}; + +static int tkey_sandbox_read(struct udevice *dev, void *buffer, int len, + int timeout_ms) +{ + struct tkey_sandbox_priv *priv = dev_get_priv(dev); + u8 *buf = buffer; + int total, ret; + + if (priv->fd < 0) + return -ENODEV; + + log_debug("Reading %d bytes...\n", len); + + /* Read data in chunks until we get the full amount */ + for (total = 0; total < len; total += ret) { + ret = os_read(priv->fd, buf + total, len - total); + log_debug("Read attempt returned: %d (total: %d/%d)\n", ret, + total, len); + + if (ret < 0) { + log_debug("Read failed with error %d\n", ret); + return -EIO; + } + + if (!ret) { + if (!total) { + log_debug("Read timeout - no data received\n"); + return -EIO; + } + /* Partial read - break and return what we got */ + log_debug("Partial read, got %x/%x bytes\n", total, + len); + break; + } + } + + log_debug("Read %d bytes:", total); + for (int i = 0; i < total; i++) + log_debug(" %02x", buf[i]); + log_debug("\n"); + + return total; +} + +static int tkey_sandbox_write(struct udevice *dev, const void *buffer, int len) +{ + struct tkey_sandbox_priv *priv = dev_get_priv(dev); + int ret; + + if (priv->fd < 0) + return -ENODEV; + + log_debug("Writing %d bytes:", len); + for (int i = 0; i < len; i++) + log_debug(" %02x", ((u8*)buffer)[i]); + log_debug("\n"); + + ret = os_write(priv->fd, buffer, len); + if (ret < 0) { + log_debug("Write failed with error %d\n", ret); + return -EIO; + } + log_debug("Wrote %d bytes\n", ret); + + return ret; +} + +static int tkey_sandbox_probe(struct udevice *dev) +{ + struct tkey_sandbox_priv *priv = dev_get_priv(dev); + const char *device_path; + + /* Get device path from device tree or use default */ + device_path = dev_read_string(dev, "sandbox,device-path"); + if (!device_path) + device_path = "/dev/ttyACM0"; + + priv->path = strdup(device_path); + if (!priv->path) + return -ENOMEM; + + /* Open the serial device */ + priv->fd = os_open(priv->path, OS_O_RDWR); + if (priv->fd < 0) { + log_err("Failed to open %s (error %d)\n", priv->path, priv->fd); + free(priv->path); + return -ENODEV; + } + + /* Configure serial port for raw mode */ + if (os_tty_set_params(priv->fd) < 0) { + log_err("Failed to configure serial port %s\n", priv->path); + os_close(priv->fd); + free(priv->path); + return -ENODEV; + } + log_debug("Connected to %s with serial parameters configured\n", + priv->path); + + return 0; +} + +static int tkey_sandbox_remove(struct udevice *dev) +{ + struct tkey_sandbox_priv *priv = dev_get_priv(dev); + + if (priv->fd >= 0) { + os_close(priv->fd); + priv->fd = -1; + } + + if (priv->path) { + free(priv->path); + priv->path = NULL; + } + log_debug("Disconnected\n"); + + return 0; +} + +/* TKey uclass operations */ +static const struct tkey_ops tkey_sandbox_ops = { + .read = tkey_sandbox_read, + .write = tkey_sandbox_write, +}; + +static const struct udevice_id tkey_sandbox_ids[] = { + { .compatible = "sandbox,tkey" }, + { } +}; + +U_BOOT_DRIVER(tkey_sandbox) = { + .name = "tkey_sandbox", + .id = UCLASS_TKEY, + .of_match = tkey_sandbox_ids, + .probe = tkey_sandbox_probe, + .remove = tkey_sandbox_remove, + .ops = &tkey_sandbox_ops, + .priv_auto = sizeof(struct tkey_sandbox_priv), +};