The device tree
The machine describing itself. Where RAM is, where the UART is, where the interrupt controller lives, how many CPUs exist, what firmware has already claimed.
The alternative is hardcoding, which is what milestone 1 did (0x0900_0000 for the UART,
read off a dtc dump by hand). Hardcoding gives you a kernel that runs on exactly one
board. The device tree gives you a kernel that can be told what board it's on.
QEMU hands us a pointer to one in x0, but only because we ship a flat arm64 Image.
See boot-protocol.md.
Everything is big-endian
The FDT format predates the little-endian consensus and never changed. Every integer in the blob is big-endian, on a machine that is little-endian.
Forget one byte-swap and you get a plausible-looking number that is wrong by a factor of
16 million, which is exactly the kind of bug that survives a code review. Our parser routes
every read through be32 or be64 so there is no path that forgets.
Even the magic: 0xd00dfeed, stored big-endian. The kernel test that validates the pointer
does u32::from_be(magic).
The layout
+------------------+
| header (40 B) | magic, totalsize, and offsets to the three blocks below
+------------------+
| memory reserve | (address, size) pairs. DON'T TOUCH THESE.
| block | Terminated by an all-zero entry.
+------------------+
| structure block | the tree itself, as a token stream
+------------------+
| strings block | every property name, deduplicated, null-terminated
+------------------+
The reservation block is deliberately dead simple, and it comes first, precisely so a
kernel can honor it without parsing anything. It's the firmware saying "I have things in
here." QEMU's virt leaves it empty; real boards often don't, and a kernel that skips it
will happily allocate over the firmware's own tables.
The structure block is a token stream
Five tokens, all 4-byte aligned:
| Token | Followed by |
|---|---|
FDT_BEGIN_NODE (1) |
a null-terminated node name, padded to 4 bytes |
FDT_END_NODE (2) |
nothing |
FDT_PROP (3) |
len, nameoff (an index into the strings block), then len bytes of value, padded |
FDT_NOP (4) |
nothing. Lets a bootloader blank out a node in place, without rewriting the blob. |
FDT_END (9) |
nothing. Done. |
Property names live in a separate strings block and are referenced by offset, because
#address-cells appears hundreds of times in a real tree and storing it once is worth the
indirection.
The part that will bite you: cells
A reg property is a list of (address, size) pairs. But how many 32-bit words each of
those takes is not fixed.
It's declared by #address-cells and #size-cells on the parent node. So to decode a
/memory node's reg, you first need the root's cell counts.
/ {
#address-cells = <0x02>; // addresses are 2 cells = 64 bits
#size-cells = <0x02>; // sizes are 2 cells = 64 bits
memory@40000000 {
reg = <0x00 0x40000000 0x00 0x8000000>;
// \______________/ \___________/
// address (2 cells) size (2 cells)
// = 0x4000_0000 = 128 MiB
};
};
The spec's defaults are 2 and 1, which almost nothing uses. Read them rather than assume; our parser does, and it's one of the two things (with endianness) most likely to be silently wrong.
Reading one yourself
qemu-system-aarch64 -machine virt,dumpdtb=virt.dtb -cpu cortex-a72 -nographic
dtc -I dtb -O dts virt.dtb | less
dtc ships with QEMU via Homebrew. Genuinely worth ten minutes of scrolling: it is a full
description of the machine we've been booting, and after milestone 3 we're only reading the
first two lines of it.
One gotcha: QEMU pads its dump to a full megabyte and says so in the header, so the raw
dump is a 1 MB file describing 7 KB of tree. Round-trip it through dtc -I dtb -O dtb to
compact it. That's how the test fixture in crates/machine_discovery/tests/fixtures/ was made.
What we read, and what we ignore
Milestone 3 read exactly two things: the /memory nodes (where RAM is) and the reservation block
(what not to touch). The list has grown as each subsystem stopped assuming its board:
| Node | Who reads it | Since |
|---|---|---|
/memory, the reservation block, /reserved-memory |
kernel/src/memory.rs |
milestone 3 |
intc@, plic@ |
the interrupt controllers | milestone 5 |
virtio_mmio@, the PCIe reg and interrupt-map |
the transports | milestone 8 and the PCIe port |
smmuv3@ |
the IOMMU | milestone 16b |
cpu@'s riscv,isa-extensions and mmu-type |
crates/machine_discovery |
milestone 60 |
/psci, and cpu@'s reg / status / enable-method |
SMP bring-up | milestone 100 |
/cpus/timebase-frequency |
the RISC-V timer | milestone 100 |
/chosen's initrd range |
the loader | milestone 12 |
the console UART node: register shape (reg-shift, reg-io-width, clock-frequency, compatible), and its interrupt line (interrupts, the inheritable interrupt-parent, the parent's #interrupt-cells) |
console::configure_from_dtb (riscv, the shape); memory::initialize via machine_discovery::interrupt_id (both ISAs, the line) |
the VisionFive 2 prep and its boot-13 fix, 2026-08-14/15 |
The UART's address is still hardcoded, and that is correct, for a nice chicken-and-egg
reason: the parser is the thing most likely to have a bug, and println! is how you would debug
it. So the console has to come up before the device tree is parsed, which means the console
cannot depend on it. What it can do is be checked against the tree afterwards, which
the device_tree_blob package's tests/qemu_aarch64_virt.rs does. Everything else about the UART now does
come from the tree, per the row above. That is the register shape adopted before the first
println!, and the interrupt line. Its QEMU constant armed an unrelated PLIC source on the JH7110
until boot 13 proved it (notes/visionfive2.md, BUGS). The constant survives only as the documented
fallback for a tree that does not say, and the boot prints which source won.
The Pi port wants all of it, because none of the addresses will match.
The parser's bounds
The blob comes from firmware, and firmware is the trust boundary here, so these bounds are boot
robustness rather than confinement. Each one costs a comparison and turns a crash on the boot path
into an Error.
MAXIMUM_TOTAL_SIZE_BYTESis 2 MiB.DeviceTreeBlob::from_pointermust trust the header'stotalsizebefore anything else is checked, and the magic says only that the pointer is at a blob. Atotalsizeof0xffff_ffffwould otherwise become a 4 GiB slice that the walkers read past the end of RAM. The figure is Linux'sMAX_FDT_SIZEfor the same slot. The largest blob this tree has met is QEMUvirt's, under 64 KiB, and a VisionFive 2's U-Boot tree is under 32 KiB.be32andbe64add withchecked_add, because every offset comes out of the blob. The verification module proves both total.- Every walker checks its cursor steps too.
property_headerreads a property's length and name offset, and refuses one whose value would pass the end of the structure block.skip_paddedandskip_namestep over padded data with a checked add. On a target whoseusizeis 32 bits, a length nearu32::MAXends the walk asTruncated, where an unchecked sum would wrap or panic. A 64-bit host cannot build that input through a blob. The unit tests insrc/lib.rshold the helpers to it, and the verification module provesskip_paddednever wraps. - Every walker stops at the end of the structure block.
from_bytesreadssize_dt_struct(version 17 and later; before that the block ends at the strings block, as libfdt reads it) and refuses a block that runs past the blob. A block with noFDT_ENDends the walk asTruncated, and nothing reads the strings block as tokens. - A
regpair whosestart + sizewraps isError::RegionOverflow.Region::endstill saturates, becauseRegionispubwithpubfields and anyone can build one. That is the argumentstatic_elf::Segment::page_rangemakes too: a type anyone can construct has to hold on its own, whatever the parser that usually builds it checked. The fuzz targetfuzz/fuzz_targets/device_tree_blob_walkfound the panic it closes. - Every lookup reads the structure block through one token walk, which refuses a node nested
deeper than
MAXIMUM_DEPTH(64, libfdt'sFDT_MAX_DEPTH) asError::TooDeepand a block whoseFDT_BEGIN_NODEandFDT_END_NODEtokens do not balance asError::BadStructure. Every walker answers a given blob the same way, and none indexes past its per-depth state.
Finding a node
node_register_regions matches a node-name prefix. It found the interrupt controller on both QEMU boards:
intc@8000000 on aarch64 virt, a direct child of the root, and plic@c000000 on riscv64
virt, nested under /soc. It decodes with the parent's cell counts even though QEMU writes 2
and 2 at both levels, because relying on that breaks the first board that differs. A name is only
a convention; a real driver matches compatible, such as "arm,cortex-a15-gic" or
"riscv,plic0", and the Pi port will need that.
node_register_regions_compatible is that lookup, first needed for the RTC of milestone 51 (wall-clock time).
The RTC is pl031@9010000 on aarch64 virt and rtc@101000 on riscv64, so a prefix that finds one
finds nothing on the other, while arm,pl031 and google,goldfish-rtc name what a driver can
drive. compatible lists the most specific entry first, and a match on any entry counts.
node_property_compatible, parent_property_compatible and pointer_handle_property follow the same pattern: the
deciding property may come after the wanted one in a node, so each open node's candidate is
remembered and the answer is taken when a matching node closes.
node_property_inherited exists because interrupt-parent sits in a different place on each machine.
QEMU's riscv64 virt writes it on the serial node, QEMU's aarch64 virt once on the root, and the
mainline JH7110 dtsi on /soc. A read of the device's own node alone works on one of the three.
node_properties reads every match, because a heterogeneous RISC-V machine describes each hart as its
own cpu@ node with its own riscv,isa. A kernel that read the first one and then scheduled onto
any hart would have read the wrong node, and the JH7110 is that machine.
BUGS
The blob is the first thing this kernel reads and the last thing it can check against anything else. The pointer comes from firmware, before there is a frame allocator or any way to report a failure, so every limitation here is a limitation on the boot path specifically.
- A
regthat aRegioncannot hold isError::BadValue: a cell count aboveMAXIMUM_CELLS(4, libfdt'sFDT_MAX_NCELLS), a nonzero cell above the low two, or a length that is not a whole number of pairs. Linux'sof_read_numberkeeps the low 64 bits of a wider value instead, and a PCI child's three-cell address is read raw throughnode_property. - A
regpair whosestart + sizewraps 64 bits isError::RegionOverflow, not a clamped region.kernel/src/memory.rsdecides where RAM is from these, and "the firmware's memory map is impossible" is something a boot path should be told.Region::end()saturates as a backstop, because the type ispubwithpubfields. node_register_regionsmatches a node-name prefix rather thancompatible, and answers for the first match only (zero regions when it has noreg, asnode_propertyanswersNone). That is a deliberate simplification for two boards, documented at the function.node_register_regions_compatibleis the correct one and is what milestone 51's RTC lookup uses.- Cell widths come from the nearest ancestor that declares them, not from the spec's default.
The Devicetree Specification says a node without
#address-cellsgives its children 2 and 1. The walkers carry the parent's widths down instead, as Linux'sof_n_addr_cellsdoes, so a bus that declares nothing decodesregwith whatever its parent said. Where the tree is silent, a spec-following reader such asdtoolkitreturns a different region and neither reports an error. The walkers ignore a declaration that is not exactly one cell. - nife reads device trees with its own parser,
device_tree_blob(partsfoundry/device_tree_blob), and does not switch to Google'sdtoolkit(ruled 2026-11-25 UTC).dtoolkitdoes not read every firmware tree thatdtcand Linux accept (Fdt::with_options, #104, covers long property names only), has noadd_childon the mutable tree, and would addzerocopy,zerocopy-deriveandthiserrorto the kernel's graph. The fixes nife sent upstream went in: #108 (Fdt::events, a linear walk), #111 and #112. The comparison holds the measurements, and milestone 1085 (device_tree_blob: what an outside maintainer would fix before it leaves) holds the ruling. The reports hold the upstream issues. - A
regshorter than one address and size pair yields no region rather than an error. The caller seesNone, the same as a node with noregat all. - Property placement is not checked. A property after a child node, or before the root node, breaks the spec's order, and the walkers read it anyway.
from_bytesrefuses a blob whose strings block is empty and ends it, asError::Truncated, because the header check requiresoff_dt_strings < totalsize.dtcnever writes such a blob, but the spec allows it.
the device_tree_blob package's tests/hostile.rs holds the regressions for the first two and for the
one-cell rule, hand-built rather than fuzzer-minimized, so a reader meets the attack next to the
code. dtoolkit against device_tree_blob
measured the cell-width, short-reg, placement and strings-block entries.
A tree written from ACPI: what it loses
uefi_loader/src/device_tree_from_acpi.rs writes a device tree for an aarch64 machine that offers
only ACPI, and its BUGS keep one line per loss. The full accounts:
- No PCI. An ECAM window is in the MCFG, but a PCI node also needs
ranges(the 32-bit and 64-bit BAR windows) and aninterrupt-map. On an ACPI machine both live in the DSDT's_CRSand_PRT, which are AML. So nopci-host-ecam-genericnode is written at all,memory::pci_regions()answersNone, and the boot takes the path it takes on the JH7110, which has no such node either. - No virtio-mmio and no
fw_cfg, for the same reason: QEMU describes them in AML. - No ITS node. The MADT's ITS entry is decoded and the loader prints it, but
machine_discovery::gicreads none and the kernel drives no ITS (milestone 317 (the interrupt-remapping flags, and where MSI confinement actually lives)). A node nothing reads would be output no host test can check against a reader. - The memory map is the UEFI one, merged. Runtime-services and ACPI regions are excluded, so the kernel will not hand out the tables it was described by. Conventional, boot-services and loader memory is offered as RAM, the same set the firmware's own device tree offers.
- Register block sizes are architectural, not stated. The MADT gives a GIC distributor's address and never its length, because the GIC architecture fixes the frame at 64 KiB. The same goes for the GICv2 CPU interface and the PL011's page. A machine whose frames differ would need this told rather than assumed, and nothing here would notice.
- One
/memorynode per merged run, capped atMAXIMUM_RAM. A machine with more runs than that is described short, and nothing says so.
Add to this file as new device tree concepts come up.