一、概述
Linux
内核启动的时候会解析设备树中各个节点的信息,并且在根文件系统的 /proc/devicetree
目录下根据节点名字创建不同文件夹。
/sys/firmware/devicetree/base # pwd
/sys/firmware/devicetree/base
/sys/firmware/devicetree/base # ls -al
total 0
-r--r--r-- 1 0 0 4 Jan 1 00:01 #address-cells
-r--r--r-- 1 0 0 4 Jan 1 00:01 #size-cells
drwxr-xr-x 15 0 0 0 Jan 1 00:01 .
drwxr-xr-x 3 0 0 0 Jan 1 00:00 ..
drwxr-xr-x 2 0 0 0 Jan 1 00:01 aliases
drwxr-xr-x 2 0 0 0 Jan 1 00:01 backlight
drwxr-xr-x 2 0 0 0 Jan 1 00:01 chosen
drwxr-xr-x 6 0 0 0 Jan 1 00:01 clocks
-r--r--r-- 1 0 0 34 Jan 1 00:01 compatible
drwxr-xr-x 3 0 0 0 Jan 1 00:01 cpus
drwxr-xr-x 2 0 0 0 Jan 1 00:01 interrupt-controller@00a01000
drwxr-xr-x 2 0 0 0 Jan 1 00:01 memory
-r--r--r-- 1 0 0 36 Jan 1 00:01 model
-r--r--r-- 1 0 0 1 Jan 1 00:01 name
drwxr-xr-x 2 0 0 0 Jan 1 00:01 pxp_v4l2
drwxr-xr-x 5 0 0 0 Jan 1 00:01 regulators
drwxr-xr-x 3 0 0 0 Jan 1 00:01 reserved-memory
drwxr-xr-x 12 0 0 0 Jan 1 00:01 soc
drwxr-xr-x 2 0 0 0 Jan 1 00:01 sound
drwxr-xr-x 3 0 0 0 Jan 1 00:01 spi4
/sys/firmware/devicetree/base #
// d-代表目录
使用设备树节点内容如下:
/ {
model = "Freescale i.MX6 ULL 14x14 EVK Board";
compatible = "fsl,imx6ull-14x14-evk", "fsl,imx6ull";
aliases {
};
cpus {
};
intc: interrupt-controller@00a01000 {
};
clocks {
};
soc {
};
chosen {
};
memory {
};
reserved-memory{
};
backlight {
};
pxp_v4l2 {
};
regulators {
};
sound {
};
spi4 {
};
}
通过以上内容可以确定 /sys/firmware/devicetree/base
目录下保存设备树根节点 "/"
下属性和相关子节点。每一个属性为一个单独文件,每一个子节点为一个单独目录。
二、设备树解析目录确定
内核初始化时调用 of_core_init
函数,将设备树解析到 "/sys/firmware/devicetree/base"
目录下。
void __init of_core_init(void)
{
struct device_node *np;
/* Create the kset, and register existing nodes */
mutex_lock(&of_mutex);
of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
if (!of_kset) {
mutex_unlock(&of_mutex);
pr_err("devicetree: failed to register existing nodes\n");
return;
}
for_each_of_allnodes(np)
__of_attach_node_sysfs(np);
mutex_unlock(&of_mutex);
/* Symlink in /proc as required by userspace ABI */
if (of_root)
proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
}
三、特殊节点
1、model
设备树中内容:
model = "Freescale i.MX6 ULL 14x14 EVK Board";
系统中内容:
/sys/firmware/devicetree/base # cat model
Freescale i.MX6 ULL 14x14 EVK Board
/sys/firmware/devicetree/base #
2、compatible
设备树中内容:
compatible = "fsl,imx6ull-14x14-evk", "fsl,imx6ull";
系统中内容:
/sys/firmware/devicetree/base # cat compatible
fsl,imx6ull-14x14-evkfsl,imx6ull
/sys/firmware/devicetree/base #
3、aliases
设备树中内容:
aliases {
can0 = &flexcan1;
can1 = &flexcan2;
ethernet0 = &fec1;
ethernet1 = &fec2;
gpio0 = &gpio1;
gpio1 = &gpio2;
gpio2 = &gpio3;
gpio3 = &gpio4;
gpio4 = &gpio5;
i2c0 = &i2c1;
i2c1 = &i2c2;
i2c2 = &i2c3;
i2c3 = &i2c4;
mmc0 = &usdhc1;
mmc1 = &usdhc2;
serial0 = &uart1;
serial1 = &uart2;
serial2 = &uart3;
serial3 = &uart4;
serial4 = &uart5;
serial5 = &uart6;
serial6 = &uart7;
serial7 = &uart8;
spi0 = &ecspi1;
spi1 = &ecspi2;
spi2 = &ecspi3;
spi3 = &ecspi4;
usbphy0 = &usbphy1;
usbphy1 = &usbphy2;
};
系统中内容:
/sys/firmware/devicetree/base/aliases # pwd
/sys/firmware/devicetree/base/aliases
/sys/firmware/devicetree/base/aliases # ls -al
total 0
drwxr-xr-x 2 0 0 0 Jan 1 00:01 .
drwxr-xr-x 15 0 0 0 Jan 1 00:01 ..
-r--r--r-- 1 0 0 36 Jan 1 00:39 can0
-r--r--r-- 1 0 0 36 Jan 1 00:39 can1
-r--r--r-- 1 0 0 41 Jan 1 00:39 ethernet0
-r--r--r-- 1 0 0 41 Jan 1 00:39 ethernet1
-r--r--r-- 1 0 0 37 Jan 1 00:39 gpio0
-r--r--r-- 1 0 0 37 Jan 1 00:39 gpio1
-r--r--r-- 1 0 0 37 Jan 1 00:39 gpio2
-r--r--r-- 1 0 0 37 Jan 1 00:39 gpio3
-r--r--r-- 1 0 0 37 Jan 1 00:39 gpio4
-r--r--r-- 1 0 0 36 Jan 1 00:39 i2c0
-r--r--r-- 1 0 0 36 Jan 1 00:39 i2c1
-r--r--r-- 1 0 0 36 Jan 1 00:39 i2c2
-r--r--r-- 1 0 0 36 Jan 1 00:39 i2c3
-r--r--r-- 1 0 0 38 Jan 1 00:39 mmc0
-r--r--r-- 1 0 0 38 Jan 1 00:39 mmc1
-r--r--r-- 1 0 0 8 Jan 1 00:39 name
-r--r--r-- 1 0 0 57 Jan 1 00:39 serial0
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial1
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial2
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial3
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial4
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial5
-r--r--r-- 1 0 0 57 Jan 1 00:39 serial6
-r--r--r-- 1 0 0 39 Jan 1 00:39 serial7
-r--r--r-- 1 0 0 56 Jan 1 00:39 spi0
-r--r--r-- 1 0 0 56 Jan 1 00:39 spi1
-r--r--r-- 1 0 0 56 Jan 1 00:39 spi2
-r--r--r-- 1 0 0 56 Jan 1 00:39 spi3
-r--r--r-- 1 0 0 39 Jan 1 00:39 usbphy0
-r--r--r-- 1 0 0 39 Jan 1 00:39 usbphy1
/sys/firmware/devicetree/base/aliases #
4、chosen
设备树中内容:
chosen {
stdout-path = &uart1;
};
系统中内容:
/sys/firmware/devicetree/base/chosen # pwd
/sys/firmware/devicetree/base/chosen
/sys/firmware/devicetree/base/chosen # ls -al
total 0
drwxr-xr-x 2 0 0 0 Jan 1 00:01 .
drwxr-xr-x 15 0 0 0 Jan 1 00:01 ..
-r--r--r-- 1 0 0 173 Jan 1 00:22 bootargs
-r--r--r-- 1 0 0 7 Jan 1 00:22 name
-r--r--r-- 1 0 0 57 Jan 1 00:22 stdout-path
/sys/firmware/devicetree/base/chosen # cat bootargs
console=ttymxc0,115200 root=/dev/nfs nfsroot=192.168.6.129:/home/onlylove/linux/nfs/rootfs-l,proto=tcp rw ip=192.168.6.200:192.168.6.129:192.168.6.2:255.255.255.0::eth0:off
/sys/firmware/devicetree/base/chosen #
/sys/firmware/devicetree/base/chosen #
/sys/firmware/devicetree/base/chosen # cat name
chosen
/sys/firmware/devicetree/base/chosen #
/sys/firmware/devicetree/base/chosen #
/sys/firmware/devicetree/base/chosen # cat stdout-path
/soc/aips-bus@02000000/spba-bus@02000000/serial@02020000
/sys/firmware/devicetree/base/chosen #
/sys/firmware/devicetree/base/chosen #
/soc/aips-bus@02000000/spba-bus@02000000/serial@02020000
为 uart1
全路径。
console=ttymxc0,115200 root=/dev/nfs nfsroot=192.168.6.129:/home/onlylove/linux/nfs/rootfs-l,proto=tcp rw ip=192.168.6.200:192.168.6.129:192.168.6.2:255.255.255.0::eth0:off
在 uboot
中设置参数。
uboot
在启动 Linux
内核的时候会将 bootargs
的值传递给 Linux
内核,bootargs
会作为 Linux
内核的命令行参数,Linux
内核启动的时候会打印出命令行参数。