sandbox: Add TTY configuration for TKey serial communication

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>
This commit is contained in:
Simon Glass
2025-10-19 04:09:07 +01:00
parent b7d758612a
commit db0f44ec8e
3 changed files with 70 additions and 2 deletions

View File

@@ -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, \

56
arch/sandbox/cpu/tty.c Normal file
View File

@@ -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;
}

View File

@@ -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
*