xref: /openbmc/openbmc/poky/meta/classes-recipe/devicetree.bbclass (revision 8460358c3d24c71d9d38fd126c745854a6301564)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# This bbclass implements device tree compilation for user provided device tree
8# 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}/sources"
44UNPACKDIR = "${S}"
45B = "${WORKDIR}/build"
46
47# Default kernel includes, these represent what are normally used for in-kernel
48# sources.
49KERNEL_INCLUDE ??= " \
50        ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts \
51        ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts/* \
52        ${STAGING_KERNEL_DIR}/scripts/dtc/include-prefixes \
53        "
54
55DT_INCLUDE[doc] = "Search paths to be made available to both the device tree compiler and preprocessor for inclusion."
56DT_INCLUDE ?= "${DT_FILES_PATH} ${KERNEL_INCLUDE}"
57DT_FILES_PATH[doc] = "Path to the directory containing dts files to build. Defaults to source directory."
58DT_FILES_PATH ?= "${S}"
59DT_FILES[doc] = "Space-separated list of dts or dtb files (relative to DT_FILES_PATH) to build. If empty, all dts files are built."
60DT_FILES ?= ""
61
62DT_PADDING_SIZE[doc] = "Size of padding on the device tree blob, used as extra space typically for additional properties during boot."
63DT_PADDING_SIZE ??= "0x3000"
64DT_RESERVED_MAP[doc] = "Number of reserved map entires."
65DT_RESERVED_MAP ??= "8"
66DT_BOOT_CPU[doc] = "The boot cpu, defaults to 0"
67DT_BOOT_CPU ??= "0"
68
69DTC_FLAGS ?= "-R ${DT_RESERVED_MAP} -b ${DT_BOOT_CPU}"
70DTC_PPFLAGS ?= "-nostdinc -undef -D__DTS__ -x assembler-with-cpp"
71DTC_BFLAGS ?= "-p ${DT_PADDING_SIZE} -@"
72DTC_OFLAGS ?= "-p 0 -@ -H epapr"
73
74python () {
75    if d.getVar("KERNEL_INCLUDE"):
76        # auto add dependency on kernel tree, but only if kernel include paths
77        # are specified.
78        d.appendVarFlag("do_compile", "depends", " virtual/kernel:do_configure")
79}
80
81def expand_includes(varname, d):
82    import glob
83    includes = set()
84    # expand all includes with glob
85    for i in (d.getVar(varname) or "").split():
86        for g in glob.glob(i):
87            if os.path.isdir(g): # only add directories to include path
88                includes.add(g)
89    return includes
90
91def devicetree_source_is_overlay(path):
92    # determine if a dts file is an overlay by checking if it uses "/plugin/;"
93    with open(path, "r") as f:
94        for i in f:
95            if i.startswith("/plugin/;"):
96                return True
97    return False
98
99def devicetree_compile(dtspath, includes, d):
100    import subprocess
101    dts = os.path.basename(dtspath)
102    dtname = os.path.splitext(dts)[0]
103    bb.note("Processing {0} [{1}]".format(dtname, dts))
104
105    # preprocess
106    ppargs = d.getVar("BUILD_CPP").split()
107    ppargs += (d.getVar("DTC_PPFLAGS") or "").split()
108    for i in includes:
109        ppargs.append("-I{0}".format(i))
110    ppargs += ["-o", "{0}.pp".format(dts), dtspath]
111    bb.note("Running {0}".format(" ".join(ppargs)))
112    subprocess.run(ppargs, check = True)
113
114    # determine if the file is an overlay or not (using the preprocessed file)
115    isoverlay = devicetree_source_is_overlay("{0}.pp".format(dts))
116
117    # compile
118    dtcargs = ["dtc"] + (d.getVar("DTC_FLAGS") or "").split()
119    if isoverlay:
120        dtcargs += (d.getVar("DTC_OFLAGS") or "").split()
121    else:
122        dtcargs += (d.getVar("DTC_BFLAGS") or "").split()
123    for i in includes:
124        dtcargs += ["-i", i]
125    dtcargs += ["-o", "{0}.{1}".format(dtname, "dtbo" if isoverlay else "dtb")]
126    dtcargs += ["-I", "dts", "-O", "dtb", "{0}.pp".format(dts)]
127    bb.note("Running {0}".format(" ".join(dtcargs)))
128    subprocess.run(dtcargs, check = True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
129
130python devicetree_do_compile() {
131    import re
132    includes = expand_includes("DT_INCLUDE", d)
133    dtfiles = d.getVar("DT_FILES").split()
134    dtfiles = [ re.sub(r"\.dtbo?$", ".dts", dtfile) for dtfile in dtfiles ]
135    listpath = d.getVar("DT_FILES_PATH")
136    for dts in dtfiles or os.listdir(listpath):
137        dtspath = os.path.join(listpath, dts)
138        try:
139            if not(os.path.isfile(dtspath)) or not(dts.endswith(".dts") or devicetree_source_is_overlay(dtspath)):
140                continue # skip non-.dts files and non-overlay files
141        except:
142            continue # skip if can't determine if overlay
143        devicetree_compile(dtspath, includes, d)
144}
145
146devicetree_do_install() {
147    for DTB_FILE in `ls *.dtb *.dtbo`; do
148        install -Dm 0644 ${B}/${DTB_FILE} ${D}/boot/devicetree/${DTB_FILE}
149    done
150}
151
152devicetree_do_deploy() {
153    for DTB_FILE in `ls *.dtb *.dtbo`; do
154        install -Dm 0644 ${B}/${DTB_FILE} ${DEPLOYDIR}/devicetree/${DTB_FILE}
155    done
156}
157addtask deploy before do_build after do_install
158
159EXPORT_FUNCTIONS do_compile do_install do_deploy
160
161