From: Simon Glass <sjg@chromium.org> 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 <noreply@anthropic.com> Signed-off-by: Simon Glass <sjg@chromium.org> --- 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 <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <asm/termbits.h> +#include <sys/ioctl.h> +#include <linux/serial.h> + +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 * -- 2.43.0