Module fdt

Module fdt 

Source
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 support
  • init_fdt(): Function to initialize the FDT subsystem from bootloader-provided address
  • relocate_fdt(): Function to safely relocate FDT to kernel-managed memory
  • create_bootinfo_from_fdt(): Function to extract BootInfo from FDT data

§Boot Integration

The module integrates with the kernel boot process through the BootInfo structure:

  1. Initialization: Architecture code calls init_fdt() with bootloader-provided address
  2. Relocation: FDT is moved to safe memory using relocate_fdt()
  3. BootInfo Creation: create_bootinfo_from_fdt() extracts system information
  4. 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 /memory nodes
  • Initramfs Location: Initial filesystem from /chosen node
  • 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§

FdtManager
SyncUnsafeCell 🔒

Statics§

MANAGER 🔒
MANAGER_INITIALIZED 🔒

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.