From patchwork Sat Feb 14 02:12:57 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1863 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=1771035228; bh=64WjETbTsctgysZzDmzJuFcxLfI+WpWlSp/8eMgKsAM=; 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=hooG1PVcozX1UR/jVwsC1R1k7QYDkRE8lSAG4PAnTW7zrXsRJ6SELdJVdH0yn5aOc VBXNB8/zUhueIvQcxFl1yaUlDMtVqdbqtN2dqX5/NZBj4PAdzd5DfqLaQ9y64Osw3l LXzqAC3VpBBk2HMXntiOaM/glFg5qj0wLn31Fn40wI0uA9XKNeMx/WjkRI1dqP/cWw 1svEBBIhNLIBZFr1O+Oi7JQjsEDr5Ip4Rx0zOhkm17VnH8rRiMOXEGXpTXH5ToZyFN X6cdzw8i0ZiXVArMdblCpbij5JSopYSo+3NiRD30fKls1HEVONbrjbeS8j09KZ85P1 p5SLOcsavI7jg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B3EC169B64 for ; Fri, 13 Feb 2026 19:13:48 -0700 (MST) 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 KxzxW2OUsi6B for ; Fri, 13 Feb 2026 19:13:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771035228; bh=64WjETbTsctgysZzDmzJuFcxLfI+WpWlSp/8eMgKsAM=; 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=hooG1PVcozX1UR/jVwsC1R1k7QYDkRE8lSAG4PAnTW7zrXsRJ6SELdJVdH0yn5aOc VBXNB8/zUhueIvQcxFl1yaUlDMtVqdbqtN2dqX5/NZBj4PAdzd5DfqLaQ9y64Osw3l LXzqAC3VpBBk2HMXntiOaM/glFg5qj0wLn31Fn40wI0uA9XKNeMx/WjkRI1dqP/cWw 1svEBBIhNLIBZFr1O+Oi7JQjsEDr5Ip4Rx0zOhkm17VnH8rRiMOXEGXpTXH5ToZyFN X6cdzw8i0ZiXVArMdblCpbij5JSopYSo+3NiRD30fKls1HEVONbrjbeS8j09KZ85P1 p5SLOcsavI7jg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A41EF69B5C for ; Fri, 13 Feb 2026 19:13:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771035226; bh=CdVQuFbVnOBZUP19l7TsoOTaYfEsOI7FLq128UhbSuc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mQY80bCTUVQfxleOqZSyOkyfA7+rHm9PVRnghLIGXRjOsxtoLmYJDoKLZFZWP5qjC f7X2MMRvz2XDXzGOHQV6acu33cmnoIzrKA/BdNEYzu1/OMgXUjmGRY3OtdoVE8KjJk ejfbyFQf+MXYDNV66vLv9i1ocNM1gPuzx4dxGQU3iU8kMXq3iSQBu9wEvwQxpTKrFp XZyQInIBUBGm6jTrtNNJcqIq+IgtETXs+HtAAkBL834DAUVtsAsRwJQdxGL/1VgRlm yh+AVTLgVMoI/RYMcoGrplScojbxeqE4BETkRHTwUQZ9WtqCwyfElovkMtkm7eCGHN jlnqIU5V1LmUA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DAB0E69B56; Fri, 13 Feb 2026 19:13:46 -0700 (MST) 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 ZB2XbWeKbs9C; Fri, 13 Feb 2026 19:13:46 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771035226; bh=9ZNSFD88pQ4gZGYaBS8HE2H3kyMPG/0DUZetxjMLgoA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dQPW5VUEjnN4STMtG1rG0zQ6Vc1LsNfOaJ/gFkM3rXq4FRvdVa6AqgLKyG9eTq7n+ RIa35XfvRi2sSgbIBnKJdELPebzcN1cPHBV20JEUiJOPHMHUqIBEToOncCYH16rCAJ v5UAWB01IdQvAGL6m2NidK1mO5NGy9X3YXm7oDg7Wtur+imKMPKHceQC6V+tQRM8gH JpkARaYQsYsQrE0aVMzwDtE+ECpaqo5aq+r1WUq7kX0c1wFndHG9a5JZ60b3fC7XoE OESXOQHPFjAMVPTJG5E1pniBsmZYu9sjwYg5g+c9nAhkKB2K2WnNkVMFidK4cbp9Pf qNoeQ4iPNdT4w== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3D5D869A69; Fri, 13 Feb 2026 19:13:46 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Fri, 13 Feb 2026 19:12:57 -0700 Message-ID: <20260214021317.816170-4-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260214021317.816170-1-sjg@u-boot.org> References: <20260214021317.816170-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: CD7ANZDDEMBINV2CE6CXRLVAVFHR74ZF X-Message-ID-Hash: CD7ANZDDEMBINV2CE6CXRLVAVFHR74ZF 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 Opus 4 . 6" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 03/13] examples: ulib: Support both sandbox and linked-in demo 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 Refactor demo.c so it builds as a standalone sandbox binary (using ulib_init/ulib_uninit and os_* helpers) or as an example linked into U-Boot (providing a strong ulib_has_main() and main()). Extract demo_run() for the common path and use IS_ENABLED(CONFIG_SANDBOX) to gate the sandbox-specific initialisation and /proc/version reading. Update demo_helper.c to use printf() directly when built as part of U-Boot (CONFIG_ULIB) rather than the u-boot-api shim. Co-developed-by: Claude Opus 4.6 Signed-off-by: Simon Glass --- examples/ulib/demo.c | 87 +++++++++++++++++++++---------------- examples/ulib/demo_helper.c | 5 +++ test/py/tests/test_ulib.py | 10 +++-- 3 files changed, 62 insertions(+), 40 deletions(-) diff --git a/examples/ulib/demo.c b/examples/ulib/demo.c index 5077fcda0a6..0dbe5d233f5 100644 --- a/examples/ulib/demo.c +++ b/examples/ulib/demo.c @@ -2,66 +2,79 @@ /* * Demo program showing U-Boot library functionality * - * This demonstrates using U-Boot library functions in sandbox like os_* - * from external programs. + * This demonstrates using U-Boot library functions from external programs + * (sandbox) or as a standalone example linked into U-Boot. * * Copyright 2025 Canonical Ltd. * Written by Simon Glass */ -#include -#include -#include -#include - #include +#include #include #include #include "demo_helper.h" +#ifndef CONFIG_SANDBOX +bool ulib_has_main(void) +{ + return true; +} +#endif + +static const char *get_version(void) +{ + if (IS_ENABLED(CONFIG_SANDBOX)) + return ulib_get_version(); + return version_string; +} + +static int demo_run(void) +{ + demo_show_banner(); + printf("U-Boot version: %s\n", get_version()); + printf("\n"); + + demo_add_numbers(42, 13); + demo_show_footer(); + + return 0; +} + +#ifdef CONFIG_SANDBOX int main(int argc, char *argv[]) { - int fd, result, lines = 0; + int fd, lines = 0; char line[256]; + int ret; - /* Init U-Boot library */ if (ulib_init(argv[0]) < 0) { fprintf(stderr, "Failed to initialize U-Boot library\n"); return 1; } - demo_show_banner(); - printf("U-Boot version: %s\n", ulib_get_version()); - printf("\n"); + ret = demo_run(); - /* Use U-Boot's os_open to open a file */ + /* Also demonstrate using U-Boot's os_* functions to read a file */ fd = os_open("/proc/version", 0); - if (fd < 0) { - fprintf(stderr, "Failed to open /proc/version\n"); - ulib_uninit(); - return 1; - } - - printf("System version:\n"); - - /* Use U-Boot's os_fgets to read lines */ - while (os_fgets(line, sizeof(line), fd)) { - printf(" %s", line); - lines++; + if (fd >= 0) { + printf("\nSystem version:\n"); + while (os_fgets(line, sizeof(line), fd)) { + printf(" %s", line); + lines++; + } + os_close(fd); + printf("\nRead %d line(s) using U-Boot library functions.\n", + lines); } - os_close(fd); - - printf("\nRead %d line(s) using U-Boot library functions.\n", lines); - - /* Test the helper function */ - result = demo_add_numbers(42, 13); - printf("Helper function result: %d\n", result); - - demo_show_footer(); - - /* Clean up */ ulib_uninit(); - return 0; + return ret; +} +#else +int main(void) +{ + return demo_run(); } +#endif diff --git a/examples/ulib/demo_helper.c b/examples/ulib/demo_helper.c index e3a2c6bdcfb..167bbd26f64 100644 --- a/examples/ulib/demo_helper.c +++ b/examples/ulib/demo_helper.c @@ -6,7 +6,12 @@ * Written by Simon Glass */ +#ifndef CONFIG_ULIB #include +#else +#include +#define ub_printf printf +#endif void demo_show_banner(void) { diff --git a/test/py/tests/test_ulib.py b/test/py/tests/test_ulib.py index 9b8b7097db4..9e46d529c87 100644 --- a/test/py/tests/test_ulib.py +++ b/test/py/tests/test_ulib.py @@ -53,18 +53,22 @@ def check_demo_output(ubman, out): with open('/proc/version', 'r', encoding='utf-8') as f: proc_version = f.read().strip() + # demo.c uses U-Boot's printf (compiled with U-Boot headers) while + # demo_helper.c uses glibc's printf, so their output streams are + # buffered separately. The helper output appears first, then the + # U-Boot printf output is flushed at exit. expected = [ 'U-Boot Library Demo Helper\r', '==========================\r', - 'System version:helper: Adding 42 + 13 = 55\r', + 'helper: Adding 42 + 13 = 55\r', '=================================\r', 'Demo complete\r', - f'U-Boot version: {ubman.u_boot_version_string}', + '\r', + f'System version:U-Boot version: {ubman.u_boot_version_string}', '', f' {proc_version}', '', 'Read 1 line(s) using U-Boot library functions.', - 'Helper function result: 55', '' ]