# 设备树

设备树是一种数据结构，也是一种配套的语言，用于以操作系统在启动时可读取的形式描述计算机系统的硬件，从而使操作系统无需将硬件细节硬编码于自身<sup>[1](https://www.devicetree.org/)</sup>。引导加载程序或固件不再把板卡的每一处细节编译进内核，而是将设备树传递给操作系统，由操作系统据此识别平台、配置自身并填充其设备模型<sup>[1](https://www.devicetree.org/)</sup><sup> • </sup><sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup>。该格式被 OpenFirmware、OpenPOWER 抽象层（OPAL）、Power 架构平台要求（PAPR）所使用，也以独立的扁平设备树（FDT）形式常见于嵌入式 ARM 和 RISC-V 系统<sup>[1](https://www.devicetree.org/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 用途 | 将硬件作为数据在启动时传递给操作系统，使硬件配置与内核代码解耦<sup>[1](https://www.devicetree.org/)</sup><sup> • </sup><sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup> |
| 结构 | 一棵由具名节点组成的树（无环图），每个节点携带具名属性；节点之间也可以在树结构之外建立链接<sup>[3](https://www.chiark.greenend.org.uk/doc/linux-doc/html/devicetree/usage-model.html)</sup> |
| 二进制格式 | 扁平设备树（DTB）包含头部、内存保留块、结构块和字符串块<sup>[4](https://devicetree-specification.readthedocs.io/_/downloads/en/stable/pdf/)</sup> |
| 规范 | 设备树规范 v0.4，2023 年 6 月 28 日发布；由 devicetree.org 维护<sup>[5](https://www.devicetree.org/specifications/)</sup> |
| Linux 中的用途 | 平台识别、运行时配置和设备填充<sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup> |
| 驱动匹配 | `compatible` 属性是一个从最具体到最一般排序的字符串列表，用于选择设备驱动<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup> |
| 引导加载程序的作用 | U-Boot 维护一棵供自身驱动模型使用的控制 FDT，以及一棵传递给操作系统的工作 FDT<sup>[7](https://docs.u-boot.org/en/latest/usage/cmd/fdt.html)</sup> |

## 数据模型

在结构上，设备树是一棵由具名节点组成的树，每个节点可以携带任意数量的具名属性，属性可保存任意数据；此外还存在一种机制，可以在自然树结构之外建立节点间的链接<sup>[3](https://www.chiark.greenend.org.uk/doc/linux-doc/html/devicetree/usage-model.html)</sup>。除根节点外，每个节点恰有一个父节点，节点名遵循 `node-name@unit-address` 的约定，名称长度为 1 到 31 个字符<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。此语境中的“设备”可以是 UART 这样的物理设备，也可以是某个设备的一部分（如 TPM 内部的随机数生成器）、一个虚拟化设备，或由固件实现的功能<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。

若干标准属性承载了大部分硬件描述。`compatible` 属性保存一个或多个以空字符结尾的字符串，按从最具体到最一般的顺序排列，推荐形式为 `"manufacturer,model"`；客户端程序按顺序用列表中的条目尝试驱动，因此声明 `compatible = "fsl,mpc8641", "ns16550"` 的设备会先寻找 `fsl,mpc8641` 驱动，找不到时回退到通用的 `ns16550`<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。`reg` 属性描述设备的资源，通常是内存映射的寄存器块，位于其父总线定义的地址空间中；地址和大小所用的单元数量由父节点的 `#address-cells` 和 `#size-cells` 属性设定<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。`phandle` 是一个唯一的 32 位数，使一个节点能够引用另一个节点，例如将一条中断线指向其中断控制器<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。`status` 属性将节点标记为 `"okay"` 或 `"disabled"`，后者表示设备当前不可用，但将来可能可用<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。

## 绑定

仅有树格式还只讲了一半：一套称为绑定的使用约定定义了数据应如何呈现，以描述典型的硬件特征，包括数据总线、中断线、GPIO 连接和外设<sup>[3](https://www.chiark.greenend.org.uk/doc/linux-doc/html/devicetree/usage-model.html)</sup>。由于属性和节点名称是文本字符串，现有绑定可以被扩展，也可以通过定义新的节点和属性来创建新的绑定<sup>[3](https://www.chiark.greenend.org.uk/doc/linux-doc/html/devicetree/usage-model.html)</sup>。

在 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)中，绑定文档以 json-schema 文件的形式编写，采用 YAML 的 JSON 兼容子集，通过 `dt_binding_check` 和 `dtbs_check` 构建目标进行校验<sup>[8](https://docs.kernel.org/devicetree/bindings/writing-schema.html)</sup>。内核的绑定审查规则规定：绑定应描述硬件具有什么，而不是操作系统或驱动当前支持什么；不应提及 Linux 或“设备驱动”；应使用与设备树规范中定义的设备类别相匹配的节点名<sup>[9](https://docs.kernel.org/devicetree/bindings/writing-bindings.html)</sup>。绑定的变更被视为 ABI：其影响超出 Linux 内核，波及其他开源项目，破坏 ABI 的变更需要明确而详细的理由<sup>[9](https://docs.kernel.org/devicetree/bindings/writing-bindings.html)</sup>。实时操作系统 Zephyr 也使用设备树，但采用自己的 YAML 绑定格式，而非 Linux 使用的 dt-schema 工具；在 Zephyr 中，设备树同时充当硬件描述语言和配置语言<sup>[10](https://docs.zephyrproject.org/latest/build/dts/bindings.html)</sup>。

## 二进制格式与工具

内存中的编码即扁平设备树，由头部、内存保留块、结构块和字符串块依次组成<sup>[4](https://devicetree-specification.readthedocs.io/_/downloads/en/stable/pdf/)</sup>。大多数 DTS 文件不包含显式的 `phandle` 属性；编译器在生成二进制形式时会自动插入它们<sup>[6](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)</sup>。

## 在 Linux 中的使用

Linux 将设备树数据用于三大目的：平台识别、运行时配置和设备填充<sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup>。在早期启动阶段、分页机制建立之前，架构代码使用 `early_init_dt_scan_chosen()` 等辅助函数扫描扁平树，从 `chosen` 节点获取内核参数；用 `early_init_dt_scan_root()` 初始化地址空间模型；用 `early_init_dt_scan_memory()` 确定可用 RAM 的大小和位置<sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup>。之后，`unflatten_device_tree()` 将数据转换为更高效的运行时表示，设备模型便从树中填充，而非从每块板卡各自的 C 文件中填充<sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup>。

内核还将设备树暴露给用户空间：当硬件通过 OpenFirmware 或扁平设备树枚举时，其结构出现在 `/sys/firmware/devicetree/` 下，该接口于 2013 年 11 月加入<sup>[11](https://github.com/torvalds/linux/blob/master/Documentation/ABI/testing/sysfs-firmware-ofw)</sup>。

**叠加层。** 叠加层在启动后修改内核中运行时的设备树，修改会反映到设备状态：新增设备节点会导致创建设备，而禁用或移除节点会导致设备注销。内核内 API 使用 `of_overlay_fdt_apply()` 应用叠加层，该函数返回一个用于标识变更集的标识符，并使用 `of_overlay_remove()` 移除叠加层；堆叠的叠加层必须按顺序移除<sup>[12](https://cdn.kernel.org/doc/html/latest/devicetree/overlay-notes.html)</sup>。

## 在引导加载程序中的使用

U-Boot 通过扁平设备树提供运行时配置，目标是让单个 U-Boot 二进制文件支持多块板卡，这正是 Linux 在 ARM 和 RISC-V 上采用的方法，PowerPC 也已使用了一段时间<sup>[13](https://docs.u-boot-project.org/en/v2023.10/develop/devicetree/control.html)</sup>。`fdt` 命令在内存中操作这些二进制块，U-Boot 区分其中两个：控制 FDT，供 U-Boot 自身使用，用于驱动模型等功能；以及工作 FDT，即启动时传递给操作系统的那个，可以自由修改，因为它不影响 U-Boot 的运行<sup>[7](https://docs.u-boot.org/en/latest/usage/cmd/fdt.html)</sup>。

U-Boot 采用设备树的时间与 Linux 大致相同，取代了其 CONFIG 选项已膨胀到近 10000 项的板卡配置文件<sup>[14](https://docs.u-boot.org/en/latest/develop/devicetree/control.html)</sup>。两个项目并行发展，仍存在一些绑定差异；实践中，设备树文件存放在 Linux 内核的 Git 仓库中，U-Boot 通过 devicetree-rebasing 仓库获取副本，并在 `u-boot.dtsi` 文件中添加板卡特定的调整<sup>[14](https://docs.u-boot.org/en/latest/develop/devicetree/control.html)</sup>。U-Boot 的约束与内核不同：在其 SPL（二级程序加载器）阶段，设备树会被裁剪以移除不需要的节点，甚至转换为 C 代码以避免访问开销<sup>[14](https://docs.u-boot.org/en/latest/develop/devicetree/control.html)</sup>。fdt 在平台和驱动已编译进来的选项中进行选择，但不能添加新驱动；Kconfig 选项控制引入哪些驱动文件，fdt 控制它们如何工作，例如指定 UART 的时钟或外设地址<sup>[14](https://docs.u-boot.org/en/latest/develop/devicetree/control.html)</sup>。

## 历史与采用

设备树源自 PowerPC 上的 Open Firmware，FDT 基础设施后来被推广到其他架构；在内核文档撰写之时，已有六个主线架构（arm、microblaze、mips、powerpc、sparc 和 x86）以及一个主线之外的架构（nios）具备某种程度的设备树支持<sup>[2](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)</sup>。ARM 上的采用工作是在 2011 年全年组织推进的：当时负责核心基础设施以及 [Tegra](https://www.edgepedia.org/zh/tegra) 和 Versatile 支持的 Grant Likely 定期发布状态报告，跟踪诸如在 U-Boot 中启用 dtb 支持、内核基本 dtb 支持、从树中探测时钟和中断控制器，以及在 OMAP、i.MX51、Tegra、Versatile、三星（Samsung）和 Xilinx 平台上从树中注册设备等任务<sup>[15](https://lwn.net/Articles/426606/)</sup>。

## 设备树与 ACPI

在 x86 服务器和 PC 上，硬件通过 ACPI 表描述给内核；而在大多数嵌入式设备上，引导加载程序应提供 OpenFirmware 设备树<sup>[16](https://bootlin.com/doc/training/sessions/online.linux-kernel.sep2023/linux-kernel-slides.pdf)</sup>。两种机制服务于相似的目的：都表示一棵设备及其资源的树，Linux 的 ACPI 实现尽可能遵循设备树的方式，枚举平台、SPI、I2C 以及有时 UART 总线背后的设备，并将它们绑定到 ACPI 句柄<sup>[17](https://cdn.kernel.org/doc/html/latest/firmware-guide/acpi/enumeration.html)</sup>。一些驱动同时携带 OF 和 ACPI 匹配表，使同一个驱动可以在任一描述机制下工作<sup>[18](https://www.youtube.com/watch?v=UIXtM1IlnG4)</sup>。

## References

1. [The Devicetree Project, devicetree.org](https://www.devicetree.org/)
2. [Linux and the Devicetree, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/devicetree/usage-model.html)
3. [Linux and the Device Tree, The Linux Kernel documentation (5.10)](https://www.chiark.greenend.org.uk/doc/linux-doc/html/devicetree/usage-model.html)
4. [Devicetree Specification, readthedocs PDF](https://devicetree-specification.readthedocs.io/_/downloads/en/stable/pdf/)
5. [Specifications, devicetree.org](https://www.devicetree.org/specifications/)
6. [chapter2-devicetree-basics.rst, devicetree-org/devicetree-specification on GitHub](https://github.com/devicetree-org/devicetree-specification/blob/main/source/chapter2-devicetree-basics.rst)
7. [fdt command, Das U-Boot documentation](https://docs.u-boot.org/en/latest/usage/cmd/fdt.html)
8. [Writing Devicetree Bindings in json-schema, The Linux Kernel documentation](https://docs.kernel.org/devicetree/bindings/writing-schema.html)
9. [DOs and DON'Ts for designing and writing Devicetree bindings, The Linux Kernel documentation](https://docs.kernel.org/devicetree/bindings/writing-bindings.html)
10. [Devicetree bindings, Zephyr Project Documentation](https://docs.zephyrproject.org/latest/build/dts/bindings.html)
11. [sysfs-firmware-ofw, torvalds/linux on GitHub](https://github.com/torvalds/linux/blob/master/Documentation/ABI/testing/sysfs-firmware-ofw)
12. [Devicetree Overlay Notes, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/devicetree/overlay-notes.html)
13. [Devicetree Control in U-Boot, Das U-Boot v2023.10 documentation](https://docs.u-boot-project.org/en/v2023.10/develop/devicetree/control.html)
14. [Devicetree Control in U-Boot, Das U-Boot latest documentation](https://docs.u-boot.org/en/latest/develop/devicetree/control.html)
15. [Device Tree on ARM status report, LWN.net](https://lwn.net/Articles/426606/)
16. [Hardware description, Bootlin Linux kernel training slides](https://bootlin.com/doc/training/sessions/online.linux-kernel.sep2023/linux-kernel-slides.pdf)
17. [ACPI Based Device Enumeration, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/firmware-guide/acpi/enumeration.html)
18. [Linux Device Configuration Mechanisms - ACPI vs Device Tree, Linux Kernel Foundation (YouTube)](https://www.youtube.com/watch?v=UIXtM1IlnG4)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Operating system concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/device-tree

English original: https://www.edgepedia.org/device-tree
