Kernelite 0.1.0
Kernelite is a minimalist, educational operating system kernel built from scratch. The project aims to recreate core components of the Linux architecture to explore the fundamentals of operating system design and hardware-software interaction.
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions
fdt.h File Reference

Header that interfaces with libfdt for Device Tree Blob (DTB) parsing. More...

#include "mem_layout/mem_layout.h"
#include "utils/kprintf.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for fdt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  memory_region_t
 Represents a single physical memory span. More...
 
struct  memory_map_t
 Container for the system physical memory layout. More...
 
struct  interrupt_t
 Describes a single interrupt as parsed from an FDT "interrupts" specifier. More...
 

Macros

#define MAX_MEM_REGIONS   16
 Maximum number of distinct memory regions the kernel will track.
 

Typedefs

typedef struct memory_region_t memory_region_t
 Represents a single physical memory span.
 
typedef struct memory_map_t memory_map_t
 Container for the system physical memory layout.
 
typedef enum irq_type_t irq_type_t
 Interrupt category, as encoded in the first cell of an FDT "interrupts" property.
 
typedef enum irq_trigger_t irq_trigger_t
 Signal shape used to trigger an interrupt.
 
typedef struct interrupt_t interrupt_t
 Describes a single interrupt as parsed from an FDT "interrupts" specifier.
 

Enumerations

enum  irq_type_t { IRQ_TYPE_SPI = 0x0 , IRQ_TYPE_PPI = 0x1 }
 Interrupt category, as encoded in the first cell of an FDT "interrupts" property. More...
 
enum  irq_trigger_t { IRQ_TRIGGER_EDGE , IRQ_TRIGGER_LEVEL_HIGH }
 Signal shape used to trigger an interrupt. More...
 

Functions

static int32_t interrupt_to_intid (const interrupt_t *const interrupt)
 Converts a parsed FDT interrupt specifier to its GIC INTID.
 
bool check_fdt (const void *ptr)
 Check the validity of the Device Tree Blob (FDT).
 
int get_mem (const void *fdt, memory_map_t *mmap)
 Scans the FDT for memory nodes and populates the provided map.
 
int get_intc_node_offset (const void *fdt)
 Retrieves a pointer to the interrupt controller node in the FDT.
 
bool fdt_is_error (int offset)
 Checks if the given FDT offset represents an error.
 
char * get_compatible_string (const void *fdt, int node_offset)
 Retrieves the compatible string for a given node.
 
bool get_reg_property (const void *fdt, int node_offset, memory_map_t *reg_map)
 Retrieves the register property for a given node.
 
int get_intr_property (const void *fdt, int node_offset, interrupt_t *out_intr_array, size_t out_intr_array_len)
 Retrieves the interrupt property for a given node.
 
int get_nodes_by_compatible (const void *fdt, const char *compatible, int *out_node_offsets, size_t out_node_offsets_len)
 Finds all nodes in the FDT matching a given "compatible" string.
 

Detailed Description

Header that interfaces with libfdt for Device Tree Blob (DTB) parsing.

Declares the kernel-facing FDT helper APIs used to validate and extract memory mapping information from the device tree.

Author
Abhin Parekadan Jose
Date
2026-05-16

Enumeration Type Documentation

◆ irq_trigger_t

Signal shape used to trigger an interrupt.

Enumerator
IRQ_TRIGGER_EDGE 

Interrupt is asserted on a signal edge.

IRQ_TRIGGER_LEVEL_HIGH 

Interrupt is asserted while the signal level is high.

◆ irq_type_t

enum irq_type_t

Interrupt category, as encoded in the first cell of an FDT "interrupts" property.

Enumerator
IRQ_TYPE_SPI 

Shared Peripheral Interrupt (routable to any core).

IRQ_TYPE_PPI 

Private Peripheral Interrupt (local to a single core).

Function Documentation

◆ check_fdt()

bool check_fdt ( const void *  ptr)

Check the validity of the Device Tree Blob (FDT).

Parameters
ptrPointer to the FDT in memory.
Returns
true if the FDT is valid, false otherwise.

◆ fdt_is_error()

bool fdt_is_error ( int  offset)

Checks if the given FDT offset represents an error.

Parameters
offsetThe FDT offset to check.
Returns
true if the offset is an error code, false otherwise.

◆ get_compatible_string()

char * get_compatible_string ( const void *  fdt,
int  node_offset 
)

Retrieves the compatible string for a given node.

Parameters
fdtPointer to the FDT header in memory.
node_offsetOffset of the node for which to retrieve the compatible string.
Returns
Pointer to the compatible string, or NULL if not found.

◆ get_intc_node_offset()

int get_intc_node_offset ( const void *  fdt)

Retrieves a pointer to the interrupt controller node in the FDT.

Parameters
fdtPointer to the FDT header in memory.
Returns
Offset of the interrupt controller node, or a negative libfdt error code if not found.

◆ get_intr_property()

int get_intr_property ( const void *  fdt,
int  node_offset,
interrupt_t out_intr_array,
size_t  out_intr_array_len 
)

Retrieves the interrupt property for a given node.

Parameters
fdtPointer to the FDT header in memory.
node_offsetOffset of the node for which to retrieve the interrupt property.
out_intr_arrayArray to be populated with the parsed interrupt specifiers.
out_intr_array_lenNumber of elements available in out_intr_array.
Returns
The number of interrupt entries parsed (capped at out_intr_array_len), or a negative libfdt error code on failure.

◆ get_mem()

int get_mem ( const void *  fdt,
memory_map_t mmap 
)

Scans the FDT for memory nodes and populates the provided map.

This function searches the FDT for nodes with device_type = "memory", respects the root node's #address-cells and #size-cells, and handles both 32-bit and 64-bit address formats.

Parameters
fdtPointer to the FDT header in memory.
mmapPointer to a memory_map_t structure to be populated.
Returns
0 on success, or a negative libfdt error code on failure.

◆ get_nodes_by_compatible()

int get_nodes_by_compatible ( const void *  fdt,
const char *  compatible,
int *  out_node_offsets,
size_t  out_node_offsets_len 
)

Finds all nodes in the FDT matching a given "compatible" string.

Parameters
fdtPointer to the FDT header in memory.
compatibleCompatible string to search for.
out_node_offsetsArray to be populated with the offsets of matching nodes.
out_node_offsets_lenNumber of elements available in out_node_offsets.
Returns
The number of matching nodes found (capped at out_node_offsets_len), or a negative libfdt error code on failure.

◆ get_reg_property()

bool get_reg_property ( const void *  fdt,
int  node_offset,
memory_map_t reg_map 
)

Retrieves the register property for a given node.

Parameters
fdtPointer to the FDT header in memory.
node_offsetOffset of the node for which to retrieve the register property.
reg_mapPointer to a memory_map_t structure to be populated with register information.
Returns
true if the register property is found and successfully parsed, false otherwise.

◆ interrupt_to_intid()

static int32_t interrupt_to_intid ( const interrupt_t *const  interrupt)
inlinestatic

Converts a parsed FDT interrupt specifier to its GIC INTID.

Per the ARM GIC devicetree binding, the "interrupts" cell's id is an offset within its category rather than the absolute INTID, so the base offset must be added back in: PPIs start at INTID 16, SPIs at INTID 32.

Parameters
interruptInterrupt specifier to convert.
Returns
The GIC INTID, or -1 if interrupt has an unrecognized type.