How riscv-software-src/opensbi works
Project Type and Purpose
• OpenSBI is a low-level firmware project providing a reference implementation of the RISC-V Supervisor Binary Interface (SBI).
• It builds a platform-independent core library (libsbi.a), integrates it with platform-specific code to produce libplatsbi.a, and then links these into firmware images that run in M-mode on RISC-V hardware.
High-Level Architectural Overview
• Host Environment: Contains the toolchain (GCC/Clang), build tools (Make, Kconfiglib, device-tree-compiler, Python), and scripts.
• Build System: Top-level Makefile orchestrates compilation of:
• Core library (libsbi) under lib/sbi and include/sbi
• Utilities libraries (lib/utils and include/sbi_utils)
• Platform libraries (libplatsbi) under platform/* directories
• Firmware examples under firmware/ and payloads/
• Artifacts:
• libsbi.a (platform-independent static library)
• libplatsbi.a (platform-specific static library)
• Firmware images (ELF and binary) for each supported platform
• Runtime Environment:
• M-mode firmware (OpenSBI) on hardware
• S-mode software (bootloader, hypervisor, OS) invoking SBI calls
• Hardware platform (CPU harts, interrupt controller, timers, console, mailbox, etc.)
Main Components
A. Host Layer
• Cross-compile Toolchain (riscv*-gcc / Clang)
• Build Scripts & Makefile + Kconfig configuration
• Device-Tree Compiler (dtc)
• Python scripts & shell helpers (scripts/)
B. Build Artifacts Layer
• Core Library: libsbi.a (built from lib/sbi, include/sbi)
• Utility Libraries: lib/utils/*.a + headers in include/sbi_utils
• Platform Library: libplatsbi.a (built from platform/<name>)
• Firmware Examples: fw_base, fw_dynamic, fw_jump, fw_payload
C. Runtime Layer
• OpenSBI Firmware (M-mode)
• SBI API Interface exposed to S-mode
• Platform Drivers (console, IPI, timer, mailbox, interrupt controller, etc.)
• Hardware Blocks (CPU harts, PLIC, CLINT, UART, GPIO, DTB)
• S-mode Software (Bootloader / U-Boot / Linux / Hypervisor)
Relationships and Data Flow
• Linkage: libsbi + libutils → libplatsbi → firmware image
• Runtime Call Flow (S-mode → SBI trap → M-mode firmware → platform drivers → hardware)
• Device-Tree passes hardware description to OpenSBI at boot
Architectural Patterns and Design Principles
• Layered static-library composition (core vs utilities vs platform)
• Kconfig-driven compile-time customization
• Clear OS-firmware boundary using SBI trap interface
• Use of modular platform drivers selected via device tree and FDT helper APIs
• Separation of responsibilities: build vs interface vs platform specifics
Technologies and Frameworks
• GNU Make and Kconfiglib for configuration/build orchestration
• GCC/Clang cross-toolchain for RISC-V PIE firmware
• Device Tree Compiler (libfdt) for dynamic hardware discovery
• Doxygen for source documentation
• Shell/Python scripts under scripts/ for auxiliary tasks
Detailed Elements to Capture
• Kconfig layer feeding into Makefile targets
• Device-Tree (FDT) feeding into platform driver initialization
• Assembly stubs vs C implementations in libsbi/libutils
• Separate firmware payload types (test payload, Linux payload)
• Installation layout under build/ and install/ directories