elinux-hwdetect

Crates.ioelinux-hwdetect
lib.rselinux-hwdetect
version0.1.1
created_at2026-01-02 10:51:04.786456+00
updated_at2026-01-02 11:10:47.689485+00
descriptionA production-ready Rust crate for Embedded Linux hardware detection using read-only /proc and /sys interfaces.
homepage
repositoryhttps://github.com/ma7moud111/elinux-hwdetect
max_upload_size
id2018307
size38,035
Mahmoud Sayed (ma7moud111)

documentation

README

elinux-hwdetect: Embedded Linux Hardware Detection Crate

A production-ready Rust crate for non-intrusive hardware and system information detection on Embedded Linux devices. It is designed to be robust, minimal, and work on systems with limited resources and minimal root filesystems like BusyBox.

Features (v1.0)

  • Board Detection: Detects board model and vendor by parsing device tree properties (/proc/device-tree/model, /proc/device-tree/compatible).
  • CPU Detection: Identifies CPU architecture, core count, and optional frequency information from /proc/cpuinfo and /sys.
  • Peripheral Detection: Scans /dev for common embedded peripherals:
    • GPIO chips (/dev/gpiochip*)
    • I2C buses (/dev/i2c-*)
    • SPI devices (/dev/spidev*)
    • UART devices (/dev/ttyS*, /dev/ttyAMA*, /dev/ttyUSB*)
  • Device Tree Awareness: Provides a utility to check for specific compatible strings in the device tree.
  • OS & Kernel Detection: Retrieves kernel version (uname -r) and distribution name from /etc/os-release.

Supported Platforms

The crate is primarily targeted at:

  • Embedded Linux distributions (Yocto, Buildroot)
  • Single-board computers (Raspberry Pi, BeagleBone)
  • Virtualized environments (QEMU ARM)
  • Minimal BusyBox root filesystems

The crate uses read-only Linux interfaces (/proc, /sys, /dev) and does not require any special kernel modules or root privileges for its core functionality.

Permissions Required

No special permissions are required. The crate only performs read operations on world-readable system files and device nodes.

Example Usage

Add elinux-hwdetect to your Cargo.toml:

[dependencies]
elinux-hwdetect = "0.1" # Use the latest version

Then, use the HardwareInfo::detect() function in your Rust code:

use elinux_hwdetect::HardwareInfo;

fn main() {
    match HardwareInfo::detect() {
        Ok(info) => {
            println!("Board Model: {:?}", info.board.model);
            println!("CPU Cores: {}", info.cpu.core_count);
            println!("Kernel Version: {}", info.os.kernel_version);
            // ... and so on
        },
        Err(e) => {
            eprintln!("Hardware detection failed: {}", e);
        }
    }
}

Example Output

The output will vary based on the host system. On a typical x86_64 development environment (like the one used for testing), the output will look similar to this:

--- Hardware Detection Successful ---
HardwareInfo {
    board: BoardInfo {
        model: None,
        vendor: None,
        compatible_strings: [],
    },
    cpu: CpuInfo {
        architecture: "x86_64",
        core_count: 6,
        frequency_mhz: None,
    },
    peripherals: PeripheralInfo {
        gpio: GpioInfo {
            chip_count: 0,
            chip_names: [],
        },
        i2c: I2cInfo {
            bus_count: 0,
            bus_names: [],
        },
        spi: SpiInfo {
            device_count: 0,
            device_names: [],
        },
        uart: UartInfo {
            device_count: 1,
            device_names: [
                "ttyS0",
            ],
        },
    },
    os: OsInfo {
        kernel_version: "6.1.102",
        distribution: Some(
            "Ubuntu 22.04.5 LTS",
        ),
    },
}

On an actual Embedded Linux device with a device tree, the board and peripherals fields will be populated with more specific information.

Yocto Usage Notes

When integrating elinux-hwdetect into a Yocto or Buildroot project, ensure that the target system's kernel configuration includes support for:

  1. Device Tree: Required for board and vendor detection.
  2. /proc and /sys filesystems: Essential for CPU and OS information.
  3. Device Nodes: Ensure the necessary device nodes (/dev/gpiochip*, /dev/i2c-*, etc.) are created by your system's udev or mdev setup.

Since this crate has minimal dependencies (only thiserror), it should integrate smoothly into most minimal Embedded Linux environments.

License

This project is licensed under the MIT OR Apache-2.0 License.

Commit count: 0

cargo fmt