lib: add dtbutils library module

with a function for concatenating DTBOs onto DTBs.

Signed-off-by: Matt Madison <matt@madison.systems>
This commit is contained in:
Matt Madison
2023-03-30 13:44:25 -07:00
committed by Matt Madison
parent b28462f16d
commit 802f7d985c
2 changed files with 53 additions and 0 deletions

4
lib/oe4t/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
BBIMPORTS = ["dtbutils"]

49
lib/oe4t/dtbutils.py Normal file
View File

@@ -0,0 +1,49 @@
def concat_dtb_overlays(dtbfile, overlays, outfile, d):
"""
Produce an output DTB file that has zero or more
overlay DTBs concatenated to it, for processing by
NVIDIA's OverlayManager UEFI driver.
Source DTB and overlay files can reside either under
${EXTERNAL_KERNEL_DEVICETREE} or in ${DEPLOY_DIR_IMAGE}.
dtbfile: Main DTB file
overlays: space-separated list of zero or more DTBO files
outfile: name of output file
d: recipe context
"""
import os
import bb.utils
def find_file_under(fname, rootdir):
for dirname, _, f in os.walk(rootdir):
if fname in f:
return os.path.join(dirname, fname)
return None
def locate_dtb_files(dtbnames):
result = []
extern_root = d.getVar('EXTERNAL_KERNEL_DEVICETREE')
imgdeploydir = d.getVar('DEPLOY_DIR_IMAGE')
for dtb in dtbnames:
if os.path.isabs(dtb):
result.append(dtb)
continue
if extern_root:
dtbpath = find_file_under(dtb, extern_root)
if dtbpath:
result.append(dtbpath)
continue
result.append(os.path.join(imgdeploydir, dtb))
return result
infiles = locate_dtb_files([dtbfile] + overlays.split())
bb.note("Creating concatenated device tree: ", outfile)
with open(outfile, "wb") as outf:
for infile in infiles:
# the overlay manager expects all DTBOs to start
# on a 4K page boundary
outf.write(bytearray(int((outf.tell() + 4095) / 4096) * 4096))
bb.note(" Adding: ", infile)
with open(infile, "rb") as inf:
outf.write(inf.read())