blob: bd50d7fa1d38115b2e70d388758357de82238b33 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williams8e7b46e2023-05-01 14:19:06 -05007# This bbclass implements device tree compilation for user provided device tree
Patrick Williams92b42cb2022-09-03 06:53:57 -05008# sources. The compilation of the device tree sources is the same as the kernel
9# device tree compilation process, this includes being able to include sources
10# from the kernel such as soc dtsi files or header files such as gpio.h. In
11# addition to device trees this bbclass also handles compilation of device tree
12# overlays.
13#
14# The output of this class behaves similar to how kernel-devicetree.bbclass
15# operates in that the output files are installed into /boot/devicetree.
16# However this class on purpose separates the deployed device trees into the
17# 'devicetree' subdirectory. This prevents clashes with the kernel-devicetree
18# output. Additionally the device trees are populated into the sysroot for
19# access via the sysroot from within other recipes.
20
21SECTION ?= "bsp"
22
23# The default inclusion of kernel device tree includes and headers means that
24# device trees built with them are at least GPL-2.0-only (and in some cases dual
25# licensed). Default to GPL-2.0-only if the recipe does not specify a license.
26LICENSE ?= "GPL-2.0-only"
27LIC_FILES_CHKSUM ?= "file://${COMMON_LICENSE_DIR}/GPL-2.0-only;md5=801f80980d171dd6425610833a22dbe6"
28
29INHIBIT_DEFAULT_DEPS = "1"
30DEPENDS += "dtc-native"
31
32inherit deploy kernel-arch
33
34COMPATIBLE_MACHINE ?= "^$"
35
36PROVIDES = "virtual/dtb"
37
38PACKAGE_ARCH = "${MACHINE_ARCH}"
39
40SYSROOT_DIRS += "/boot/devicetree"
41FILES:${PN} = "/boot/devicetree/*.dtb /boot/devicetree/*.dtbo"
42
43S = "${WORKDIR}"
44B = "${WORKDIR}/build"
45
46# Default kernel includes, these represent what are normally used for in-kernel
47# sources.
48KERNEL_INCLUDE ??= " \
49 ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts \
50 ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts/* \
51 ${STAGING_KERNEL_DIR}/scripts/dtc/include-prefixes \
52 "
53
54DT_INCLUDE[doc] = "Search paths to be made available to both the device tree compiler and preprocessor for inclusion."
55DT_INCLUDE ?= "${DT_FILES_PATH} ${KERNEL_INCLUDE}"
Andrew Geissler028142b2023-05-05 11:29:21 -050056DT_FILES_PATH[doc] = "Path to the directory containing dts files to build. Defaults to source directory."
Patrick Williams92b42cb2022-09-03 06:53:57 -050057DT_FILES_PATH ?= "${S}"
Andrew Geissler028142b2023-05-05 11:29:21 -050058DT_FILES[doc] = "Space-separated list of dts or dtb files (relative to DT_FILES_PATH) to build. If empty, all dts files are built."
59DT_FILES ?= ""
Patrick Williams92b42cb2022-09-03 06:53:57 -050060
61DT_PADDING_SIZE[doc] = "Size of padding on the device tree blob, used as extra space typically for additional properties during boot."
62DT_PADDING_SIZE ??= "0x3000"
63DT_RESERVED_MAP[doc] = "Number of reserved map entires."
64DT_RESERVED_MAP ??= "8"
65DT_BOOT_CPU[doc] = "The boot cpu, defaults to 0"
66DT_BOOT_CPU ??= "0"
67
68DTC_FLAGS ?= "-R ${DT_RESERVED_MAP} -b ${DT_BOOT_CPU}"
69DTC_PPFLAGS ?= "-nostdinc -undef -D__DTS__ -x assembler-with-cpp"
70DTC_BFLAGS ?= "-p ${DT_PADDING_SIZE} -@"
71DTC_OFLAGS ?= "-p 0 -@ -H epapr"
72
73python () {
74 if d.getVar("KERNEL_INCLUDE"):
75 # auto add dependency on kernel tree, but only if kernel include paths
76 # are specified.
77 d.appendVarFlag("do_compile", "depends", " virtual/kernel:do_configure")
78}
79
80def expand_includes(varname, d):
81 import glob
82 includes = set()
83 # expand all includes with glob
84 for i in (d.getVar(varname) or "").split():
85 for g in glob.glob(i):
86 if os.path.isdir(g): # only add directories to include path
87 includes.add(g)
88 return includes
89
90def devicetree_source_is_overlay(path):
91 # determine if a dts file is an overlay by checking if it uses "/plugin/;"
92 with open(path, "r") as f:
93 for i in f:
94 if i.startswith("/plugin/;"):
95 return True
96 return False
97
98def devicetree_compile(dtspath, includes, d):
99 import subprocess
100 dts = os.path.basename(dtspath)
101 dtname = os.path.splitext(dts)[0]
102 bb.note("Processing {0} [{1}]".format(dtname, dts))
103
104 # preprocess
105 ppargs = d.getVar("BUILD_CPP").split()
106 ppargs += (d.getVar("DTC_PPFLAGS") or "").split()
107 for i in includes:
108 ppargs.append("-I{0}".format(i))
109 ppargs += ["-o", "{0}.pp".format(dts), dtspath]
110 bb.note("Running {0}".format(" ".join(ppargs)))
111 subprocess.run(ppargs, check = True)
112
113 # determine if the file is an overlay or not (using the preprocessed file)
114 isoverlay = devicetree_source_is_overlay("{0}.pp".format(dts))
115
116 # compile
117 dtcargs = ["dtc"] + (d.getVar("DTC_FLAGS") or "").split()
118 if isoverlay:
119 dtcargs += (d.getVar("DTC_OFLAGS") or "").split()
120 else:
121 dtcargs += (d.getVar("DTC_BFLAGS") or "").split()
122 for i in includes:
123 dtcargs += ["-i", i]
124 dtcargs += ["-o", "{0}.{1}".format(dtname, "dtbo" if isoverlay else "dtb")]
125 dtcargs += ["-I", "dts", "-O", "dtb", "{0}.pp".format(dts)]
126 bb.note("Running {0}".format(" ".join(dtcargs)))
127 subprocess.run(dtcargs, check = True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
128
129python devicetree_do_compile() {
Andrew Geissler028142b2023-05-05 11:29:21 -0500130 import re
Patrick Williams92b42cb2022-09-03 06:53:57 -0500131 includes = expand_includes("DT_INCLUDE", d)
Andrew Geissler028142b2023-05-05 11:29:21 -0500132 dtfiles = d.getVar("DT_FILES").split()
133 dtfiles = [ re.sub(r"\.dtbo?$", ".dts", dtfile) for dtfile in dtfiles ]
Patrick Williams92b42cb2022-09-03 06:53:57 -0500134 listpath = d.getVar("DT_FILES_PATH")
Andrew Geissler028142b2023-05-05 11:29:21 -0500135 for dts in dtfiles or os.listdir(listpath):
Patrick Williams92b42cb2022-09-03 06:53:57 -0500136 dtspath = os.path.join(listpath, dts)
137 try:
138 if not(os.path.isfile(dtspath)) or not(dts.endswith(".dts") or devicetree_source_is_overlay(dtspath)):
139 continue # skip non-.dts files and non-overlay files
140 except:
141 continue # skip if can't determine if overlay
142 devicetree_compile(dtspath, includes, d)
143}
144
145devicetree_do_install() {
146 for DTB_FILE in `ls *.dtb *.dtbo`; do
147 install -Dm 0644 ${B}/${DTB_FILE} ${D}/boot/devicetree/${DTB_FILE}
148 done
149}
150
151devicetree_do_deploy() {
152 for DTB_FILE in `ls *.dtb *.dtbo`; do
153 install -Dm 0644 ${B}/${DTB_FILE} ${DEPLOYDIR}/devicetree/${DTB_FILE}
154 done
155}
156addtask deploy before do_build after do_install
157
158EXPORT_FUNCTIONS do_compile do_install do_deploy
159