Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | // SPDX-License-Identifier: GPL-2.0+ /* * Rust demo program showing U-Boot library functionality * * This demonstrates using U-Boot library functions from Rust, * modeled after examples/ulib/demo.c * * Copyright 2025 Canonical Ltd. * Written by Simon Glass <simon.glass@canonical.com> */ #![allow(clippy::manual_c_str_literals)] use std::ffi::CString; use std::os::raw::{c_char, c_int}; mod rust_helper; use rust_helper::{demo_add_numbers, demo_show_banner, demo_show_footer}; use u_boot_sys::{os_close, os_fgets, os_open, ub_printf, ulib_get_version, ulib_init, ulib_uninit}; fn main() -> Result<(), Box<dyn std::error::Error>> { // Get program name for ulib_init let args: Vec<String> = std::env::args().collect(); let program_name = CString::new(args[0].clone())?; // Init U-Boot library let ret = unsafe { ulib_init(program_name.as_ptr() as *mut c_char) }; if ret != 0 { eprintln!("Failed to initialize U-Boot library"); return Err("ulib_init failed".into()); } demo_show_banner(); // Display U-Boot version using ulib_get_version() let version_ptr = unsafe { ulib_get_version() }; unsafe { ub_printf( b"U-Boot version: %s\n\0".as_ptr() as *const c_char, version_ptr, ); ub_printf(b"\n\0".as_ptr() as *const c_char); } // Use U-Boot's os_open() to open a file let filename = CString::new("/proc/version")?; let fd = unsafe { os_open(filename.as_ptr(), 0) }; if fd < 0 { eprintln!("Failed to open /proc/version"); unsafe { ulib_uninit(); } return Err("os_open failed".into()); } unsafe { ub_printf( b"System version:\n\0".as_ptr() as *const c_char, ); } // Read lines using U-Boot's os_fgets() let mut lines = 0; let mut buffer = [0i8; 256]; // Use array instead of Vec to avoid heap // allocation loop { let result = unsafe { os_fgets(buffer.as_mut_ptr(), buffer.len() as c_int, fd) }; if result.is_null() { break; } unsafe { ub_printf( b" %s\0".as_ptr() as *const c_char, buffer.as_ptr(), ); } lines += 1; } unsafe { os_close(fd) }; unsafe { ub_printf( b"\nRead %d line(s) using U-Boot library functions.\n\0" .as_ptr() as *const c_char, lines, ); } // Test the helper function let result = demo_add_numbers(42, 13); unsafe { ub_printf( b"Helper function result: %d\n\0".as_ptr() as *const c_char, result, ); } demo_show_footer(); // Clean up unsafe { ulib_uninit(); } Ok(()) } |