[Concept,12/16] tkey: Allow selecting the TKey device by name

Message ID 20251115185212.539268-13-sjg@u-boot.org
State New
Headers
Series Continue TKey development |

Commit Message

Simon Glass Nov. 15, 2025, 6:52 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Enhance the 'tkey connect' command to allow the device name to be
specified. This will be useful in tests.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 cmd/tkey.c             | 36 +++++++++++++++++++++++++++++++-----
 doc/usage/cmd/tkey.rst | 24 ++++++++++++++++++++----
 2 files changed, 51 insertions(+), 9 deletions(-)
  

Patch

diff --git a/cmd/tkey.c b/cmd/tkey.c
index a269ca86085..3a31712990a 100644
--- a/cmd/tkey.c
+++ b/cmd/tkey.c
@@ -19,11 +19,18 @@ 
 #include <linux/string.h>
 #include <linux/errno.h>
 
+/* Static device pointer set by tkey connect command */
+static struct udevice *tkey_dev;
+
 static struct udevice *tkey_get_device(void)
 {
 	struct udevice *dev;
 	int ret;
 
+	/* If a device was set by tkey connect, return it */
+	if (tkey_dev)
+		return tkey_dev;
+
 	ret = uclass_first_device_err(UCLASS_TKEY, &dev);
 	if (ret) {
 		printf("No device found (err %dE)\n", ret);
@@ -47,10 +54,28 @@  static int do_tkey_connect(struct cmd_tbl *cmdtp, int flag, int argc,
 			   char *const argv[])
 {
 	struct udevice *dev;
+	int ret;
 
-	dev = tkey_get_device();
-	if (!dev)
-		return CMD_RET_FAILURE;
+	/* Check if device name is provided as optional first argument */
+	if (argc > 1) {
+		const char *dev_name = argv[1];
+
+		ret = uclass_get_device_by_name(UCLASS_TKEY, dev_name, &dev);
+		if (ret) {
+			printf("Failed to find TKey device '%s' (err %dE)\n",
+			       dev_name, ret);
+			return CMD_RET_FAILURE;
+		}
+	} else {
+		ret = uclass_first_device_err(UCLASS_TKEY, &dev);
+		if (ret) {
+			printf("No device found (err %dE)\n", ret);
+			return CMD_RET_FAILURE;
+		}
+	}
+
+	/* Set the static device pointer for subsequent commands */
+	tkey_dev = dev;
 
 	printf("Connected to TKey device\n");
 
@@ -276,7 +301,8 @@  static int do_tkey_loadapp(struct cmd_tbl *cmdtp, int flag, int argc,
 }
 
 U_BOOT_LONGHELP(tkey,
-	"connect    - Connect to TKey device\n"
+	"connect [device-name] - Connect to TKey device\n"
+	"    Optional device-name to connect to specific TKey device\n"
 	"tkey fwmode     - Check if device is in firmware or app mode\n"
 	"tkey getkey <uss> [verify-hash] - Get disk encryption key\n"
 	"    Loads app with USS, derives key. Same USS always produces same key.\n"
@@ -289,7 +315,7 @@  U_BOOT_LONGHELP(tkey,
 
 U_BOOT_CMD_WITH_SUBCMDS(tkey, "Tillitis TKey security token operations",
 			tkey_help_text,
-	U_BOOT_SUBCMD_MKENT(connect, 1, 1, do_tkey_connect),
+	U_BOOT_SUBCMD_MKENT(connect, 2, 1, do_tkey_connect),
 	U_BOOT_SUBCMD_MKENT(fwmode, 1, 1, do_tkey_fwmode),
 	U_BOOT_SUBCMD_MKENT(getkey, 3, 1, do_tkey_getkey),
 	U_BOOT_SUBCMD_MKENT(info, 1, 1, do_tkey_info),
diff --git a/doc/usage/cmd/tkey.rst b/doc/usage/cmd/tkey.rst
index 6a9f37354eb..b7d138f7307 100644
--- a/doc/usage/cmd/tkey.rst
+++ b/doc/usage/cmd/tkey.rst
@@ -11,7 +11,7 @@  Synopsis
 
 ::
 
-    tkey connect
+    tkey connect [device-name]
     tkey fwmode
     tkey getkey <uss> [verify-hash]
     tkey info
@@ -46,8 +46,19 @@  internal UDI to generate deterministic encryption keys.
 tkey connect
 ~~~~~~~~~~~~
 
-Test connectivity to a TKey device. This command attempts to find and connect
-to the first available TKey device in the system.
+Test connectivity to a TKey device and optionally select a specific device for
+subsequent commands.
+
+When called without arguments, this command connects to the first available TKey
+device in the system. When a device name is provided, it connects to that
+specific device.
+
+Once connected, the selected device is remembered and will be used by all
+subsequent tkey commands (info, getkey, loadapp, etc.) until a different device
+is selected with another connect command.
+
+device-name
+    Optional name of a specific TKey device to connect to
 
 
 tkey fwmode
@@ -160,11 +171,16 @@  password
 Example
 -------
 
-Connect to device::
+Connect to the first available device::
 
     => tkey connect
     Connected to TKey device
 
+Connect to a specific device by name::
+
+    => tkey connect tkey@0
+    Connected to TKey device
+
 Check device mode::
 
     => tkey fwmode