Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # This bbclass implements device tree compliation 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 | |
| 21 | SECTION ?= "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. |
| 26 | LICENSE ?= "GPL-2.0-only" |
| 27 | LIC_FILES_CHKSUM ?= "file://${COMMON_LICENSE_DIR}/GPL-2.0-only;md5=801f80980d171dd6425610833a22dbe6" |
| 28 | |
| 29 | INHIBIT_DEFAULT_DEPS = "1" |
| 30 | DEPENDS += "dtc-native" |
| 31 | |
| 32 | inherit deploy kernel-arch |
| 33 | |
| 34 | COMPATIBLE_MACHINE ?= "^$" |
| 35 | |
| 36 | PROVIDES = "virtual/dtb" |
| 37 | |
| 38 | PACKAGE_ARCH = "${MACHINE_ARCH}" |
| 39 | |
| 40 | SYSROOT_DIRS += "/boot/devicetree" |
| 41 | FILES:${PN} = "/boot/devicetree/*.dtb /boot/devicetree/*.dtbo" |
| 42 | |
| 43 | S = "${WORKDIR}" |
| 44 | B = "${WORKDIR}/build" |
| 45 | |
| 46 | # Default kernel includes, these represent what are normally used for in-kernel |
| 47 | # sources. |
| 48 | KERNEL_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 | |
| 54 | DT_INCLUDE[doc] = "Search paths to be made available to both the device tree compiler and preprocessor for inclusion." |
| 55 | DT_INCLUDE ?= "${DT_FILES_PATH} ${KERNEL_INCLUDE}" |
| 56 | DT_FILES_PATH[doc] = "Defaults to source directory, can be used to select dts files that are not in source (e.g. generated)." |
| 57 | DT_FILES_PATH ?= "${S}" |
| 58 | |
| 59 | DT_PADDING_SIZE[doc] = "Size of padding on the device tree blob, used as extra space typically for additional properties during boot." |
| 60 | DT_PADDING_SIZE ??= "0x3000" |
| 61 | DT_RESERVED_MAP[doc] = "Number of reserved map entires." |
| 62 | DT_RESERVED_MAP ??= "8" |
| 63 | DT_BOOT_CPU[doc] = "The boot cpu, defaults to 0" |
| 64 | DT_BOOT_CPU ??= "0" |
| 65 | |
| 66 | DTC_FLAGS ?= "-R ${DT_RESERVED_MAP} -b ${DT_BOOT_CPU}" |
| 67 | DTC_PPFLAGS ?= "-nostdinc -undef -D__DTS__ -x assembler-with-cpp" |
| 68 | DTC_BFLAGS ?= "-p ${DT_PADDING_SIZE} -@" |
| 69 | DTC_OFLAGS ?= "-p 0 -@ -H epapr" |
| 70 | |
| 71 | python () { |
| 72 | if d.getVar("KERNEL_INCLUDE"): |
| 73 | # auto add dependency on kernel tree, but only if kernel include paths |
| 74 | # are specified. |
| 75 | d.appendVarFlag("do_compile", "depends", " virtual/kernel:do_configure") |
| 76 | } |
| 77 | |
| 78 | def expand_includes(varname, d): |
| 79 | import glob |
| 80 | includes = set() |
| 81 | # expand all includes with glob |
| 82 | for i in (d.getVar(varname) or "").split(): |
| 83 | for g in glob.glob(i): |
| 84 | if os.path.isdir(g): # only add directories to include path |
| 85 | includes.add(g) |
| 86 | return includes |
| 87 | |
| 88 | def devicetree_source_is_overlay(path): |
| 89 | # determine if a dts file is an overlay by checking if it uses "/plugin/;" |
| 90 | with open(path, "r") as f: |
| 91 | for i in f: |
| 92 | if i.startswith("/plugin/;"): |
| 93 | return True |
| 94 | return False |
| 95 | |
| 96 | def devicetree_compile(dtspath, includes, d): |
| 97 | import subprocess |
| 98 | dts = os.path.basename(dtspath) |
| 99 | dtname = os.path.splitext(dts)[0] |
| 100 | bb.note("Processing {0} [{1}]".format(dtname, dts)) |
| 101 | |
| 102 | # preprocess |
| 103 | ppargs = d.getVar("BUILD_CPP").split() |
| 104 | ppargs += (d.getVar("DTC_PPFLAGS") or "").split() |
| 105 | for i in includes: |
| 106 | ppargs.append("-I{0}".format(i)) |
| 107 | ppargs += ["-o", "{0}.pp".format(dts), dtspath] |
| 108 | bb.note("Running {0}".format(" ".join(ppargs))) |
| 109 | subprocess.run(ppargs, check = True) |
| 110 | |
| 111 | # determine if the file is an overlay or not (using the preprocessed file) |
| 112 | isoverlay = devicetree_source_is_overlay("{0}.pp".format(dts)) |
| 113 | |
| 114 | # compile |
| 115 | dtcargs = ["dtc"] + (d.getVar("DTC_FLAGS") or "").split() |
| 116 | if isoverlay: |
| 117 | dtcargs += (d.getVar("DTC_OFLAGS") or "").split() |
| 118 | else: |
| 119 | dtcargs += (d.getVar("DTC_BFLAGS") or "").split() |
| 120 | for i in includes: |
| 121 | dtcargs += ["-i", i] |
| 122 | dtcargs += ["-o", "{0}.{1}".format(dtname, "dtbo" if isoverlay else "dtb")] |
| 123 | dtcargs += ["-I", "dts", "-O", "dtb", "{0}.pp".format(dts)] |
| 124 | bb.note("Running {0}".format(" ".join(dtcargs))) |
| 125 | subprocess.run(dtcargs, check = True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 126 | |
| 127 | python devicetree_do_compile() { |
| 128 | includes = expand_includes("DT_INCLUDE", d) |
| 129 | listpath = d.getVar("DT_FILES_PATH") |
| 130 | for dts in os.listdir(listpath): |
| 131 | dtspath = os.path.join(listpath, dts) |
| 132 | try: |
| 133 | if not(os.path.isfile(dtspath)) or not(dts.endswith(".dts") or devicetree_source_is_overlay(dtspath)): |
| 134 | continue # skip non-.dts files and non-overlay files |
| 135 | except: |
| 136 | continue # skip if can't determine if overlay |
| 137 | devicetree_compile(dtspath, includes, d) |
| 138 | } |
| 139 | |
| 140 | devicetree_do_install() { |
| 141 | for DTB_FILE in `ls *.dtb *.dtbo`; do |
| 142 | install -Dm 0644 ${B}/${DTB_FILE} ${D}/boot/devicetree/${DTB_FILE} |
| 143 | done |
| 144 | } |
| 145 | |
| 146 | devicetree_do_deploy() { |
| 147 | for DTB_FILE in `ls *.dtb *.dtbo`; do |
| 148 | install -Dm 0644 ${B}/${DTB_FILE} ${DEPLOYDIR}/devicetree/${DTB_FILE} |
| 149 | done |
| 150 | } |
| 151 | addtask deploy before do_build after do_install |
| 152 | |
| 153 | EXPORT_FUNCTIONS do_compile do_install do_deploy |
| 154 | |