Expand description
Flattened Device Tree (FDT) management module.
This module provides comprehensive functionality for managing the Flattened Device Tree (FDT), which is a data structure used to describe the hardware components of a system in various architectures including RISC-V, ARM, and PowerPC.
§Overview
The FDT is passed by the bootloader to the kernel and contains critical information about the hardware configuration of the system. This module parses, relocates, and provides access to that information through a unified interface that integrates with the kernel’s BootInfo structure.
§Core Components
FdtManager: A singleton that manages access to the parsed FDT with relocation supportinit_fdt(): Function to initialize the FDT subsystem from bootloader-provided addressrelocate_fdt(): Function to safely relocate FDT to kernel-managed memorycreate_bootinfo_from_fdt(): Function to extract BootInfo from FDT data
§Boot Integration
The module integrates with the kernel boot process through the BootInfo structure:
- Initialization: Architecture code calls
init_fdt()with bootloader-provided address - Relocation: FDT is moved to safe memory using
relocate_fdt() - BootInfo Creation:
create_bootinfo_from_fdt()extracts system information - Kernel Handoff: BootInfo is passed to
start_kernel()for unified initialization
§Memory Management
The module provides advanced memory management for FDT data:
- Safe Relocation: Copies FDT to kernel-controlled memory to prevent corruption
- Initramfs Handling: Automatically relocates initramfs to prevent memory conflicts
- Memory Area Calculation: Computes usable memory areas excluding kernel and FDT regions
§Architecture Support
This module is architecture-agnostic and supports any platform using FDT:
- RISC-V: Primary device tree platform
- ARM/AArch32: Standard hardware description method
- AArch64: Alternative to UEFI for hardware description
- PowerPC: Traditional FDT usage
- Other FDT platforms: Any architecture supporting device trees
§Usage
§Basic Initialization
// Initialize FDT from bootloader-provided address
init_fdt(fdt_addr);
// Relocate to safe memory
let dest_ptr = safe_memory_area as *mut u8;
let relocated_area = relocate_fdt(dest_ptr);
// Create BootInfo with FDT data
let bootinfo = create_bootinfo_from_fdt(cpu_id, relocated_area.start);§FDT Data Access
let fdt_manager = FdtManager::get_manager();
if let Some(fdt) = fdt_manager.get_fdt() {
// Access FDT nodes and properties
let memory_node = fdt.find_node("/memory");
let chosen_node = fdt.find_node("/chosen");
}§Hardware Information Extraction
The module extracts essential hardware information:
- Memory Layout: Total system memory from
/memorynodes - Initramfs Location: Initial filesystem from
/chosennode - Command Line: Boot arguments from
/chosen/bootargs - Device Tree: Complete hardware description for device enumeration
§Safety and Error Handling
The module provides robust error handling:
- Validation: All FDT operations include proper error checking
- Memory Safety: Relocation prevents use-after-free and corruption
- Graceful Degradation: Missing optional components (like initramfs) are handled gracefully
- Panic Conditions: Clear documentation of when functions may panic
§Implementation Details
The module uses the fdt crate for low-level parsing while providing high-level
abstractions for kernel integration. It maintains a static global manager instance
to provide access throughout the kernel, with careful synchronization to prevent
data races during initialization.
Structs§
Statics§
Functions§
- bytes_
to_ 🔒cstr - create_
bootinfo_ from_ fdt - Create BootInfo from FDT data
- init_
fdt - Initializes the FDT subsystem with the given address.
- relocate_
fdt - Relocates the FDT to safe memory.