From patchwork Mon Aug 25 16:27:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 127 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=1756139271; bh=eWj6ZtXr9DUh9d/1V494zNWng2FZSaYoBX7MBf5EoEU=; 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=GfbsqxWer1UkWFLE89CoTexoo6iz4uj+LDi/0l9Mzi/T7UyeY7ES9r5Ah0ja4TB4f JEGJFkKPnRBkxs0EMhWSXLHArcORL3FEIFJrm8oftKjP1kIq7WpaSigb1a8Zb5EyLh b65VIg04J2JAeJe6itEa4vwqo9ol53uu0+2LjPQl1tewyT8yovU0Eoqsufe0OOtpef ofRg7WMfBSdR8ziGpP+eiC/IbaOTVEwopETrJeFt17ktjgDbgtWrKOrOcuHe0gDCOA PM6c+Fr9/3rCChLI9uhHWrgVrjhdsX4B81h8fdzdOsWNUnVULBJEe4TKvcgtbK3S8z 0zq9TFDrJF5Gg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6B35867791 for ; Mon, 25 Aug 2025 10:27:51 -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 HBaVJ0rTT0lu for ; Mon, 25 Aug 2025 10:27:51 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756139269; bh=eWj6ZtXr9DUh9d/1V494zNWng2FZSaYoBX7MBf5EoEU=; 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=t5O/ac7FpuyM2aWbHSpXONEjJLlI7hAUXy5yjkZUqDWrvjbrecA+lBBeQEbG/A9Jc VZYmtY1zLHN/8e9nDoFKZiRZyV27BSjI4gR4KEaSDwpri7P+X09xRtbDj7YELS0MXI GJFp43tjYLe3dExYule8e2Wko/uLW8NAgiva2SscLib+QGyBUy1umYMIrlIbvTbgD1 64gW9USrAB+wxU6eOdwedvj9duQ5rZKskynJOyEnRpTHnbTE3jTYa3smOIQT6jl42W AvSlLnW0SaVWtAgOTU7r6/ka1F2PBFD2OIiTxu8vHIXIs7dK+SWkGSo7gFGEQfTFfx cRMt6m59MZ8wQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 563EF677A5 for ; Mon, 25 Aug 2025 10:27:49 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756139266; bh=eSg8cuoI96F54Pbr8d/hs4ncdQSm2MI++PPwzrT8JZs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ke8tA9dkO8z9gK9UdsjWy4BHOFvluQxWMR1EBItpKWhybV+J8JoO4SmkvFSi41fM1 zoVrWTvNkcwR1/vCCTMmC7Bz3p6lWSui/1OLQ8e7eNUugzbps9GLsdEwogl2NvwsAG Q4/+Jb2PaqNySUYbnqvrxRI4CY1S4n0/8YVTkzvR74yYRGi6JKWA1YCDKIN9YheoF3 0hIazIwnZz7mZYTRGah9JtNA924Y0atchLOLkd3B0v+I/FjVjUsFmM8NpM+e0q7v+P efeQCJcUq+1RTKIumt4K7JB+kg/8G08iEfcF06tWoxXKxunJLhhTMWepAYoQZknHug WPoX53Oo5LXyA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 786316778C; Mon, 25 Aug 2025 10:27:46 -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 31EP-eBI-hny; Mon, 25 Aug 2025 10:27:46 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1756139263; bh=S58pxnVXbKnCv+C0ku527Vd4hvYWW09raxLMyF+TrTU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cXHv6Qzw5BqOwhRxvJzUfnG5uMMTbhOmn1adJ8HTLcXz6S9rt9jjbCYkN7bFYl7NV ebnMmo3GlexTpnHOE2L7s3qTK5K/eWD9sMfhpClDsU+ImHkmdGkxjIpVlLuHYQtlwZ rCVEI2GNKHY9MH0xn0ulS6tOfCK5tjnOrMDnFS8UEL8LrU3oxbYspMR071D96CaeNL 7mlsK7iciBFTuX5lvA6+8TyTi8rlypfxfRa/JdB/X78TyiifVSm9EI9wuFHvkONF/c Gfq3wixG7MCOm6ziaE1jnl3Qdlklw4XEvWs/oi+JtcrPK5vUKtTGrRxLhOZXHl/f7a DrKjlc876/F/Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id C76E267792; Mon, 25 Aug 2025 10:27:42 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Mon, 25 Aug 2025 10:27:09 -0600 Message-ID: <20250825162727.3185381-7-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250825162727.3185381-1-sjg@u-boot.org> References: <20250825162727.3185381-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: EMC3HJGI67QFISPVSIR2QH6G6ZQHAWX4 X-Message-ID-Hash: EMC3HJGI67QFISPVSIR2QH6G6ZQHAWX4 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: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH v2 06/16] sandbox: Add a function to detect terminal connection 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 serial_is_tty() function that determines if the serial console is connected to a terminal. For sandbox, this uses os_isatty() to check stdin, except for cooked mode, where we don't want to assume anything about the terminal. For other platforms, it always returns true. Co-developed-by: Claude Signed-off-by: Simon Glass --- (no changes since v1) arch/sandbox/cpu/os.c | 5 +++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/cpu/state.c | 7 +++++++ arch/sandbox/include/asm/state.h | 8 ++++++++ include/os.h | 8 ++++++++ include/serial.h | 21 +++++++++++++++++++++ 6 files changed, 53 insertions(+) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index c19f859d4e0..101d695a556 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -142,6 +142,11 @@ int os_close(int fd) return -1; } +int os_isatty(int fd) +{ + return isatty(fd); +} + int os_unlink(const char *pathname) { return unlink(pathname); diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index e4e4932c183..02d16bfe930 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -613,6 +613,10 @@ int sandbox_main(int argc, char *argv[]) if (os_parse_args(state, argc, argv)) return 1; + /* Detect if serial console is connected to a terminal */ + state->serial_is_tty = os_isatty(1) && + state->term_raw != STATE_TERM_COOKED; + if (state->ram_buf_fname) { ret = os_read_ram_buf(state->ram_buf_fname); if (ret) { diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index 949ca42de94..d883cf2132a 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -476,6 +476,13 @@ bool sandbox_sf_bootdev_enabled(void) return !state->disable_sf_bootdevs; } +bool sandbox_serial_is_tty(void) +{ + struct sandbox_state *state = state_get_current(); + + return state->serial_is_tty; +} + int state_init(void) { state = &main_state; diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h index 3aa35c112be..5350ee6b8fa 100644 --- a/arch/sandbox/include/asm/state.h +++ b/arch/sandbox/include/asm/state.h @@ -157,6 +157,7 @@ struct sandbox_state { bool ignore_missing_state_on_read; /* No error if state missing */ bool show_lcd; /* Show LCD on start-up */ bool double_lcd; /* Double display size for high-DPI */ + bool serial_is_tty; /* Serial console is connected to a tty */ enum sysreset_t last_sysreset; /* Last system reset type */ bool sysreset_allowed[SYSRESET_COUNT]; /* Allowed system reset types */ enum state_terminal_raw term_raw; /* Terminal raw/cooked */ @@ -377,6 +378,13 @@ int state_get_rel_filename(const char *rel_path, char *buf, int size); */ int state_load_other_fdt(const char **bufp, int *sizep); +/** + * sandbox_serial_is_tty() - check if serial console is connected to a tty + * + * Return: true if serial console is connected to a terminal, false if not + */ +bool sandbox_serial_is_tty(void); + /** * Initialize the test system state */ diff --git a/include/os.h b/include/os.h index 35757fc8bb8..3393acb435a 100644 --- a/include/os.h +++ b/include/os.h @@ -90,6 +90,14 @@ int os_open(const char *pathname, int flags); */ int os_close(int fd); +/** + * os_isatty() - check if file descriptor refers to a terminal + * + * @fd: File descriptor to check + * Return: 1 if fd is a terminal, 0 if not, -1 on error + */ +int os_isatty(int fd); + /** * os_unlink() - access to the OS unlink() system call * diff --git a/include/serial.h b/include/serial.h index 9ed3793b647..02c28f8605c 100644 --- a/include/serial.h +++ b/include/serial.h @@ -2,6 +2,9 @@ #define __SERIAL_H__ #include +#ifdef CONFIG_SANDBOX +#include +#endif /* Escape value */ #define cESC '\x1b' @@ -424,4 +427,22 @@ int serial_query_size(int *rowsp, int *colsp); */ int serial_get_size(struct udevice *dev, int *rowsp, int *colsp); +/* + * serial_is_tty() - check if the serial console is connected to a terminal + * + * This does not indicate that there is actually a terminal, only that if there + * is one, we can assume it is present and connected + * + * Return: true if any serial console is likely connected to a terminal, false if not + */ +static inline bool serial_is_tty(void) +{ +#ifdef CONFIG_SANDBOX + return sandbox_serial_is_tty(); +#else + /* assume that it is! */ + return true; +#endif +} + #endif