From patchwork Thu Sep 11 21:44:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 293 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=1757627105; bh=HNeEpu+yWlyhDtQ1HEzTVIUEIxGL8U5okGD4F7FXf+A=; 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=ZOUT/Jfr6mznDmWqNA7tL67kosanqCPXf0rqUm9SvXMdsNFTiNMjVN8E13S5gZuNM Gs75XiHK+K+UN/dlMxFP+eXzUH/+ID9kCA+HFaa8I21afEywhu57QB4+IdumrxZBAw Pt5WUH1sswuubApAkgBd1it3bgKRczZ+KL2y6szaKfrz4X0DJrTDyVhH399+UyGlXr KEqRJV8F2CU03Uwr3X4WVAsgFBr0Rutw/X5OWApqbZ+g4DO/RAlGvl4BsZIw9fV6Rw 905nllil6vL/kwwXl7E83uEgrE7hDRI8RbyZZad15BnyDpS8SU+SW+R6cYHjxB773D ao40J8KTh2SRQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3C88F679D6 for ; Thu, 11 Sep 2025 15:45:05 -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 TTDBA9hSxkTM for ; Thu, 11 Sep 2025 15:45:05 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757627105; bh=HNeEpu+yWlyhDtQ1HEzTVIUEIxGL8U5okGD4F7FXf+A=; 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=ZOUT/Jfr6mznDmWqNA7tL67kosanqCPXf0rqUm9SvXMdsNFTiNMjVN8E13S5gZuNM Gs75XiHK+K+UN/dlMxFP+eXzUH/+ID9kCA+HFaa8I21afEywhu57QB4+IdumrxZBAw Pt5WUH1sswuubApAkgBd1it3bgKRczZ+KL2y6szaKfrz4X0DJrTDyVhH399+UyGlXr KEqRJV8F2CU03Uwr3X4WVAsgFBr0Rutw/X5OWApqbZ+g4DO/RAlGvl4BsZIw9fV6Rw 905nllil6vL/kwwXl7E83uEgrE7hDRI8RbyZZad15BnyDpS8SU+SW+R6cYHjxB773D ao40J8KTh2SRQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2AC3467A5B for ; Thu, 11 Sep 2025 15:45:05 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757627103; bh=n5hvSx7rCfZUE4hdjkQgOJSDgUQgXJYz4lgxTdmxxik=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FaX0bfV0MTBCOxeOi1Z09xUg5UkYXv+N2LJhgM3hw1rt9OUGL7BiZ/hl5dND4va4j YHbtfOxa/3fqU0MWOyRUt4K6uCdu/LmrNHJESCfjFoFaGQ8RzAIHox2MgucOHZqv1/ 9vcgbek7FGDO52PhfNB9ykVflWDTNFoJI6nvTe5rwvBmJwBbZ2gXM15trDYL+ozjxZ 2jvob9QDDU1lkUhosMExbFC9auKNiS/fk6ITFj5D/WlheApx41vFi2loxKJPKIwm3X XEWUyctzvYI4Xu4dGCwMyBdI3YDTvG0D6CvLrU9cOtLo7s+Vfzh1oGK4PeO6kZa2c1 TMDMoovTabJhg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 62EBB679D6; Thu, 11 Sep 2025 15:45: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 Y4vz5jxCuVId; Thu, 11 Sep 2025 15:45:03 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757627099; bh=dO9HAtnK2/MpEE0E/U+JP0LJ1GuVC8Io846tyoQ+OYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PtqP5Ppue7PTKDavAHeFYfpolzd1MSwL4yd8Yzx2V4LXSXw3kGopqgSKOsN1ArkQD 3J2StQy/yYGhHOK7n6BNj2Vi6sOjK+mmP7zHWoQDhEToG1p0X9JvUUw+ieE49U6hRX N/+pPyPjdOJOfXPyJRwNcNFdP6TIt7rNHGmFhECM4QtB4KKlHAuCYu3WlxbJUsPfkV ChOUNJmpy3IHe+VAu+WPFsgdom2LYvtHmejDnRik+UJD9FQFP+3rd297cQO9kgIhSp Rj+xxDaEv9KbK5eVWu4+zDt+lZrmtKzZVt+4d0sU2d8RX8K/KkJPfnpcU5fi8Svtrb 8hteoVgq5N92Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 6379567A59; Thu, 11 Sep 2025 15:44:59 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Thu, 11 Sep 2025 15:44:11 -0600 Message-ID: <20250911214425.3687188-8-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911214425.3687188-1-sjg@u-boot.org> References: <20250911214425.3687188-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: I4TZOEZN2XC6INMH62GBAXMP4AGOHY66 X-Message-ID-Hash: I4TZOEZN2XC6INMH62GBAXMP4AGOHY66 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 07/14] ulib: Add a very basic rust library 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 Create a new lib/rust directory and add a very basic rust library in there. This will eventually expand to include more features. For now it only has a few ulib calls needed for the example programs. Co-developed-by: Claude Co-developed-by: Simon Glass Signed-off-by: Simon Glass --- lib/rust/.gitignore | 1 + lib/rust/Cargo.lock | 7 ++ lib/rust/Cargo.toml | 17 +++++ lib/rust/src/lib.rs | 173 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 lib/rust/.gitignore create mode 100644 lib/rust/Cargo.lock create mode 100644 lib/rust/Cargo.toml create mode 100644 lib/rust/src/lib.rs diff --git a/lib/rust/.gitignore b/lib/rust/.gitignore new file mode 100644 index 00000000000..2f7896d1d13 --- /dev/null +++ b/lib/rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/lib/rust/Cargo.lock b/lib/rust/Cargo.lock new file mode 100644 index 00000000000..dd8371f6255 --- /dev/null +++ b/lib/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "uboot-sys" +version = "0.1.0" diff --git a/lib/rust/Cargo.toml b/lib/rust/Cargo.toml new file mode 100644 index 00000000000..a321b31271e --- /dev/null +++ b/lib/rust/Cargo.toml @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0+ +[package] +name = "u-boot-sys" +version = "0.1.0" +edition = "2021" +license = "GPL-2.0+" +description = "Rust FFI bindings for U-Boot library functions" +authors = ["Simon Glass "] +repository = "https://concept.u-boot.org/u-boot/u-boot" +keywords = ["u-boot", "ffi", "bindings", "embedded"] +categories = ["external-ffi-bindings", "embedded"] + +[lib] +name = "u_boot_sys" +path = "src/lib.rs" + +[dependencies] diff --git a/lib/rust/src/lib.rs b/lib/rust/src/lib.rs new file mode 100644 index 00000000000..4ac9f9d32f1 --- /dev/null +++ b/lib/rust/src/lib.rs @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * U-Boot FFI bindings for Rust + * + * This crate provides Rust FFI bindings for U-Boot library functions, + * equivalent to include/u-boot-lib.h + * + * Copyright 2025 Canonical Ltd. + * Written by Simon Glass + */ + +//! # U-Boot Bindings +//! +//! This crate provides Rust FFI bindings for U-Boot library functions. +//! It includes bindings for: +//! +//! - U-Boot library management functions (`ulib_*`) +//! - U-Boot API functions (`ub_*`) +//! - OS abstraction functions (`os_*`) +//! +//! ## Usage +//! +//! ```rust,no_run +//! use uboot_sys::{ulib_init, ulib_uninit, ub_printf}; +//! use std::ffi::CString; +//! +//! let program_name = CString::new("my_program").unwrap(); +//! unsafe { +//! if ulib_init(program_name.as_ptr() as *mut _) == 0 { +//! ub_printf(b"Hello from U-Boot!\n\0".as_ptr() as *const _); +//! ulib_uninit(); +//! } +//! } +//! ``` + +#![allow(clippy::manual_c_str_literals)] +#![allow(non_camel_case_types)] + +use std::os::raw::{c_char, c_int}; + +/// U-Boot library management functions +pub mod uboot_lib { + use super::*; + + extern "C" { + /// Set up the U-Boot library + /// + /// # Arguments + /// + /// * `progname` - Program name to use (must be a writeable string, + /// typically argv\[0\]) + /// + /// # Returns + /// + /// 0 if OK, negative error code on error + pub fn ulib_init(progname: *mut c_char) -> c_int; + + /// Shut down the U-Boot library + /// + /// Call this when your program has finished using the library, + /// before it exits + pub fn ulib_uninit(); + + /// Get the version string + /// + /// # Returns + /// + /// Full U-Boot version string + pub fn ulib_get_version() -> *const c_char; + } +} + +/// U-Boot API functions +pub mod uboot_api { + use super::*; + + extern "C" { + /// Print directly to console (equivalent to printf) + /// + /// # Arguments + /// + /// * `fmt` - The format string to use + /// * `...` - Arguments for the format string + /// + /// # Returns + /// + /// Number of characters printed + pub fn ub_printf(fmt: *const c_char, ...) -> c_int; + + /// Format a string and place it in a buffer + /// + /// # Arguments + /// + /// * `buf` - The buffer to place the result into + /// * `size` - The size of the buffer, including the trailing null + /// space + /// * `fmt` - The format string to use + /// * `...` - Arguments for the format string + /// + /// # Returns + /// + /// The number of characters which would be generated for the given + /// input, excluding the trailing null, as per ISO C99 + pub fn ub_snprintf( + buf: *mut c_char, + size: usize, + fmt: *const c_char, + ... + ) -> c_int; + + /// Format a string and place it in a buffer + /// + /// # Arguments + /// + /// * `buf` - The buffer to place the result into + /// * `fmt` - The format string to use + /// * `...` - Arguments for the format string + /// + /// # Returns + /// + /// The number of characters written into buf + pub fn ub_sprintf(buf: *mut c_char, fmt: *const c_char, ...) -> c_int; + } +} + +/// OS abstraction functions +pub mod os { + use super::*; + + extern "C" { + /// Access the OS open() system call + /// + /// # Arguments + /// + /// * `pathname` - Path to the file to open + /// * `flags` - Open flags + /// + /// # Returns + /// + /// File descriptor, or negative error code on error + pub fn os_open(pathname: *const c_char, flags: c_int) -> c_int; + + /// Access the OS close() system call + /// + /// # Arguments + /// + /// * `fd` - File descriptor to close + /// + /// # Returns + /// + /// 0 on success, negative error code on error + pub fn os_close(fd: c_int) -> c_int; + + /// Read a string from a file stream + /// + /// # Arguments + /// + /// * `s` - Buffer to store the string + /// * `size` - Maximum number of characters to read + /// * `fd` - File descriptor to read from + /// + /// # Returns + /// + /// Pointer to the string on success, null pointer on EOF or + /// error + pub fn os_fgets(s: *mut c_char, size: c_int, fd: c_int) -> *mut c_char; + } +} + +// Re-export commonly used functions at crate root +pub use uboot_lib::{ulib_get_version, ulib_init, ulib_uninit}; +pub use uboot_api::{ub_printf, ub_snprintf, ub_sprintf}; +pub use os::{os_close, os_fgets, os_open};