From patchwork Sun Oct 19 07:23:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 631 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=1760858644; bh=NsFG9Ch1ztC23cBsVmVbA5MKvhGrDlv+T7vWyTblzDI=; 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=kmdhGXSJXlqDDNMrvGW9sJwj/RC0in9OiDCFLrRYRaungktB9Re9NTZNSEcCcYncc sCUxf0E2MQQc+pss0k+oElpXMmknT9oOzNeHEP+3Ea82kZ/+ep4JNXlFdJRM5S49yL Nlp+GKc8iMipYR7rzaNJag+t3vfou/5Xk1HuF5XuWCMCOTmdmZaMfT/Q86lS9wOmIJ zMku/fcxOB2pemNgn1Dw9p84F5gSElPmrChvdP2B+DzE4fzHsFwRvCB5NTjn2hJX7V PaBlTCWhONmloZdypAsUoQxaN78nCL6O6cza477uanW/woGPhfd+19XPSY/b5kJtOd XMSyeguw2CV3A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 98333680DD for ; 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 10024) with ESMTP id 5dbyzzfkkvy2 for ; 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=NsFG9Ch1ztC23cBsVmVbA5MKvhGrDlv+T7vWyTblzDI=; 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=kmdhGXSJXlqDDNMrvGW9sJwj/RC0in9OiDCFLrRYRaungktB9Re9NTZNSEcCcYncc sCUxf0E2MQQc+pss0k+oElpXMmknT9oOzNeHEP+3Ea82kZ/+ep4JNXlFdJRM5S49yL Nlp+GKc8iMipYR7rzaNJag+t3vfou/5Xk1HuF5XuWCMCOTmdmZaMfT/Q86lS9wOmIJ zMku/fcxOB2pemNgn1Dw9p84F5gSElPmrChvdP2B+DzE4fzHsFwRvCB5NTjn2hJX7V PaBlTCWhONmloZdypAsUoQxaN78nCL6O6cza477uanW/woGPhfd+19XPSY/b5kJtOd XMSyeguw2CV3A== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 85F355E435 for ; 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=1760858643; bh=NcRsEMzKULOEF3upyeDsbaGQn3SK1WuT8FPSuJo3glw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JCXN3GbHUQk4X4n4rEY6eUKw5aYaDeryEUDce814wSwiCAv/BN79MkwMLI2Tbx6B1 DR5rlnnaJw1mmHTH7q1R+ao376HDbdrp4z5uajh18G2WOVE0dpBVNDUDo4n+ys62tp SA8dDuMFjtvjvaoGgqAcOrwEzVFHbxIPYU7d240JaNZJvFZpSkFwV9+sZCbnTXvvb8 8JErizsBrYx2jMo733vGN+gd2uj9cvfWEdhF0vxFYSvrLfZaKfW+fzkaYAkRQ90KF1 NolNV7K7rLh3qGLBB52afWIetLd3uC5FubBFlfDbCdrT51Gu+zhGql9wiTdOzd527f JxQw73wrGGOLA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 8BE7D68198; Sun, 19 Oct 2025 01:24:03 -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 Ix3X4Ke8t9bp; Sun, 19 Oct 2025 01:24:03 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1760858639; bh=qA5TfuN2G7i3uZXxYSKmY0muey7aby42ZuLzOBWwbGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gr7Rk88ECJkNN7b9IEJ3Jv9BeEGXvcJVNRKb1OjvqPto/ODq+XSvfbno4JnB2Oeg4 yjOsmSB/rH6W7JE1VoXcSO54ANkXsNgjKKarFtkA1dqRTC4Ophkyn52HYHq5FB85JX GqT10/FDT+lZeKELgOT/1+vSkVFMR5s4rnIFBNVLtaQ3QIHfHV47M2YGzx9te3nKtV TMrRt+qZcUovDGIaHPN8WAS4ostvaEO6ic06ZeFK6JXlGJVcZyqYx4GcyihVD1mMZR 4CX7/4MOet7YpUga0vYcDtalJ9iC88VlBFjnjV8zrMFB7JEnyuDTB3DyLiNJXdsdyy Jfqi4mdbd1mwA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3D6CC680DD; Sun, 19 Oct 2025 01:23:59 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Sun, 19 Oct 2025 01:23:07 -0600 Message-ID: <20251019072313.3235339-8-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: BZZFYRY6SJB4ZWJPMOBDM5L65BIP4LVY X-Message-ID-Hash: BZZFYRY6SJB4ZWJPMOBDM5L65BIP4LVY 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 7/8] sandbox: Add TTY configuration for TKey serial communication 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 os_tty_set_params() function to configure terminal devices for serial communication with TKey devices: - Custom baud rate of 62500 using termios2 - 8n1 configuration (8 data bits, no parity, 1 stop bit) - Raw mode for binary communication - Appropriate timeouts for frame-based protocols This is needed for serial-based TKey communication on sandbox, allowing U-Boot to communicate with TKey security tokens via a serial port. Co-developed-by: Claude Signed-off-by: Simon Glass --- arch/sandbox/cpu/Makefile | 4 +-- arch/sandbox/cpu/tty.c | 56 +++++++++++++++++++++++++++++++++++++++ include/os.h | 12 +++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 arch/sandbox/cpu/tty.c diff --git a/arch/sandbox/cpu/Makefile b/arch/sandbox/cpu/Makefile index 35f853776f7..ef9a01c5d7c 100644 --- a/arch/sandbox/cpu/Makefile +++ b/arch/sandbox/cpu/Makefile @@ -5,7 +5,7 @@ # (C) Copyright 2000-2003 # Wolfgang Denk, DENX Software Engineering, wd@denx.de. -obj-y := cache.o cpu.o mem.o state.o os.o +obj-y := cache.o cpu.o mem.o state.o os.o tty.o ifdef CONFIG_FUZZ obj-y += fuzz.o else @@ -17,7 +17,7 @@ obj-$(CONFIG_XPL_BUILD) += spl.o obj-$(CONFIG_ETH_SANDBOX_RAW) += eth-raw-os.o # Compile these files with system headers -CFLAGS_USE_SYSHDRS := eth-raw-os.o fuzz.o main.o os.o sdl.o +CFLAGS_USE_SYSHDRS := eth-raw-os.o fuzz.o main.o os.o sdl.o tty.o # sdl.c fails to build with -fshort-wchar using musl cmd_cc_sdl.o = $(CC) $(filter-out -nostdinc -fshort-wchar, \ diff --git a/arch/sandbox/cpu/tty.c b/arch/sandbox/cpu/tty.c new file mode 100644 index 00000000000..04c6fff9bbe --- /dev/null +++ b/arch/sandbox/cpu/tty.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2024 U-Boot TKey Support + * + * TTY configuration for TKey serial communication + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +int os_tty_set_params(int fd) +{ + struct termios2 tty2; + + /* Get current termios2 attributes */ + if (ioctl(fd, TCGETS2, &tty2) != 0) + return -errno; + + /* Configure for raw mode */ + tty2.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + tty2.c_oflag &= ~OPOST; + tty2.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + tty2.c_cflag &= ~(CSIZE | PARENB); + + /* 8N1 configuration */ + tty2.c_cflag |= CS8; /* 8 data bits */ + tty2.c_cflag &= ~PARENB; /* No parity */ + tty2.c_cflag &= ~CSTOPB; /* 1 stop bit */ + tty2.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, ignore modem lines */ + + /* Set custom baud rate using termios2 */ + tty2.c_cflag &= ~CBAUD; + tty2.c_cflag |= BOTHER; /* Use custom baud rate */ + tty2.c_ispeed = 62500; /* Input speed */ + tty2.c_ospeed = 62500; /* Output speed */ + + /* Blocking with timeout for complete frames */ + tty2.c_cc[VMIN] = 1; /* Wait for at least 1 character */ + tty2.c_cc[VTIME] = 50; /* 5 second timeout */ + + /* Apply termios2 settings */ + if (ioctl(fd, TCSETS2, &tty2) != 0) + return -errno; + + /* Flush buffers */ + if (ioctl(fd, TCFLSH, TCIOFLUSH) != 0) + return -errno; + + return 0; +} diff --git a/include/os.h b/include/os.h index bc4c9073cff..1b2243d46d4 100644 --- a/include/os.h +++ b/include/os.h @@ -185,6 +185,18 @@ void os_raise_sigalrm(void); */ void os_tty_raw(int fd, bool allow_sigs); +/** + * os_tty_set_params() - configure terminal parameters + * + * Configure the terminal device for serial communication with specific + * baud rate, data bits, parity, and flow control suitable for embedded + * device protocols like TKey. + * + * @fd: file descriptor of terminal device + * Return: 0 on success, -errno on error + */ +int os_tty_set_params(int fd); + /** * os_fd_restore() - restore the tty to its original mode *