Device Tree Overlays: What They Are and How to Apply Them

Compile a small device tree overlay with dtc, merge it into a base DTB offline with fdtoverlay, and see how the Linux kernel applies overlays at runtime.

Embedded Published 4 min read

Contents

A device tree overlay is a small piece of hardware description that modifies a base device tree instead of replacing it. In this guide you compile a base tree and an overlay with dtc, merge them offline with fdtoverlay, and check the result. Along the way you will see how the kernel handles overlays at runtime.

What a device tree overlay is

The devicetree is a data structure for describing hardware, handed to the operating system at boot, as the Devicetree.org project puts it. An overlay adds to or changes that description. The kernel documentation describes overlays as changes to the live device tree that the kernel should reflect in device state. A new enabled node should produce a device, and a removed or disabled node should deregister the device.

Overlay source format

An overlay source file starts with /dts-v1/; and /plugin/;, and the second line marks it as an overlay. The kernel notes show the simplest form, which targets a node by label:

/dts-v1/;
/plugin/;
&ocp {
        bar {
                compatible = "corp,bar";
        };
};

The kernel notes prefer label targets because the overlay then works with any base tree that has that label. If the base tree was not built with -@, you can target by path instead, with &{/ocp} { ... };.

When dtc compiles such a file with -@, it produces the fragment layout described in the dtc documentation. Each fragment@N node holds a target phandle and an __overlay__ block with the content to merge. Labels that the overlay cannot resolve itself are recorded in a generated __fixups__ node, and the loader fills in the real phandles. The base tree gets a __symbols__ node (also from -@) that maps labels to paths so those references can be resolved.

Install the tools

Both dtc and fdtoverlay come from the dtc project. The package name varies by distribution.

✓ tested · debian,ubuntu
apt-get update -qq
apt-get install -y device-tree-compiler
✓ tested · fedora
dnf install -y dtc
✓ tested · arch
pacman -Sy --noconfirm dtc

Build a base tree and an overlay

This base tree is a toy, not a real board. It has a labeled ocp node and one UART that is disabled.

✓ tested · debian,ubuntu,fedora,arch
mkdir -p /tmp/ovl && cd /tmp/ovl
cat > base.dts <<'EOF'
/dts-v1/;
/ {
	compatible = "example,toy-board";
	#address-cells = <1>;
	#size-cells = <1>;

	ocp: ocp {
		#address-cells = <1>;
		#size-cells = <1>;

		uart0: serial@1000 {
			compatible = "example,uart";
			reg = <0x1000 0x100>;
			status = "disabled";
		};
	};
};
EOF
cat > enable-uart.dtso <<'EOF'
/dts-v1/;
/plugin/;

&uart0 {
	status = "okay";
};

&ocp {
	sensor@2000 {
		compatible = "example,sensor";
		reg = <0x2000 0x10>;
	};
};
EOF
dtc -@ -I dts -O dtb -o base.dtb base.dts
dtc -@ -I dts -O dtb -o enable-uart.dtbo enable-uart.dtso
ls -l base.dtb enable-uart.dtbo

The -@ flag on the base tree emits __symbols__, which the overlay needs to find uart0 and ocp.

Merge offline with fdtoverlay

fdtoverlay applies one or more overlay blobs to a base blob. Its options, per the source, are -i for the input base blob, -o for the output blob, and -v for verbose messages. Overlays are listed as positional arguments.

✓ tested · debian,ubuntu,fedora,arch
cd /tmp/ovl
fdtoverlay -i base.dtb -o merged.dtb enable-uart.dtbo
dtc -I dtb -O dts merged.dtb

Decompiling the output shows the merged tree. The UART should now read status = "okay", and a sensor@2000 node should sit under ocp.

✓ tested · debian,ubuntu,fedora,arch
cd /tmp/ovl
dtc -I dtb -O dts merged.dtb 2>/dev/null | grep -E 'status|sensor@2000'

How the kernel applies overlays

The merged DTB above is the offline route: you pick the final tree before boot, and the bootloader passes it to the kernel. The kernel also has an in-kernel API for runtime use:

  • of_overlay_fdt_apply() creates and applies an overlay changeset and returns a cookie for it.
  • of_overlay_remove() removes an overlay. An overlay with another overlay stacked on top cannot be removed.
  • of_overlay_remove_all() removes all overlays in the right order.

The kernel notes warn that code must not keep pointers to overlay nodes after removal, because that memory is freed. They also warn that a driver loaded later may scan the whole tree, overlay nodes included, and hold on to such pointers.

How an overlay reaches the kernel on a given board, such as through a bootloader that merges .dtbo files or a userspace interface, depends on the platform. The pages used for this article do not cover those mechanisms, so check your board vendor’s documentation.

Summary

  • An overlay is a /plugin/; source file that patches a base tree.
  • Compile the base tree and the overlay with dtc -@ so labels can be resolved.
  • fdtoverlay -i base.dtb -o merged.dtb overlay.dtbo merges them offline, and dtc -I dtb -O dts lets you inspect the result.

For a broader look at building a minimal Linux image for an embedded target, see Buildroot: Build a Minimal Linux Image and Boot It in QEMU.

Sources

  1. Linux kernel documentation: Devicetree Overlay Notes
  2. Devicetree.org
  3. dtc: Documentation/dt-object-internal.txt (GitHub mirror)
  4. dtc: fdtoverlay.c (GitHub mirror)

#Device Tree #Overlays #Dtc #Fdtoverlay #Embedded

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.