scripts/make_fit: Support decomposing DTBs

The kernel tree builds some "composite" DTBs, where the final DTB is the
result of applying one or more DTB overlays on top of a base DTB with
fdtoverlay.

The FIT image specification already supports configurations having one
base DTB and overlays applied on top. It is then up to the bootloader to
apply said overlays and either use or pass on the final result. This
allows the FIT image builder to reuse the same FDT images for multiple
configurations, if such cases exist.

The decomposition function depends on the kernel build system, reading
back the .cmd files for the to-be-packaged DTB files to check for the
fdtoverlay command being called. This will not work outside the kernel
tree. The function is off by default to keep compatibility with possible
existing users.

To facilitate the decomposition and keep the code clean, the model and
compatitble string extraction have been moved out of the output_dtb
function. The FDT image description is replaced with the base file name
of the included image.

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
This commit is contained in:
Chen-Yu Tsai 2024-06-13 17:34:32 +08:00 committed by Masahiro Yamada
parent e61b190b1a
commit 17c31aded9
2 changed files with 65 additions and 22 deletions

View File

@ -522,6 +522,7 @@ quiet_cmd_fit = FIT $@
cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \ cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
--name '$(UIMAGE_NAME)' \ --name '$(UIMAGE_NAME)' \
$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \ $(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^) --compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
# XZ # XZ

View File

@ -22,6 +22,11 @@ the entire FIT.
Use -c to compress the data, using bzip2, gzip, lz4, lzma, lzo and Use -c to compress the data, using bzip2, gzip, lz4, lzma, lzo and
zstd algorithms. zstd algorithms.
Use -D to decompose "composite" DTBs into their base components and
deduplicate the resulting base DTBs and DTB overlays. This requires the
DTBs to be sourced from the kernel build directory, as the implementation
looks at the .cmd files produced by the kernel build.
The resulting FIT can be booted by bootloaders which support FIT, such The resulting FIT can be booted by bootloaders which support FIT, such
as U-Boot, Linuxboot, Tianocore, etc. as U-Boot, Linuxboot, Tianocore, etc.
@ -64,6 +69,8 @@ def parse_args():
help='Specifies the architecture') help='Specifies the architecture')
parser.add_argument('-c', '--compress', type=str, default='none', parser.add_argument('-c', '--compress', type=str, default='none',
help='Specifies the compression') help='Specifies the compression')
parser.add_argument('-D', '--decompose-dtbs', action='store_true',
help='Decompose composite DTBs into base DTB and overlays')
parser.add_argument('-E', '--external', action='store_true', parser.add_argument('-E', '--external', action='store_true',
help='Convert the FIT to use external data') help='Convert the FIT to use external data')
parser.add_argument('-n', '--name', type=str, required=True, parser.add_argument('-n', '--name', type=str, required=True,
@ -140,12 +147,12 @@ def finish_fit(fsw, entries):
fsw.end_node() fsw.end_node()
seq = 0 seq = 0
with fsw.add_node('configurations'): with fsw.add_node('configurations'):
for model, compat in entries: for model, compat, files in entries:
seq += 1 seq += 1
with fsw.add_node(f'conf-{seq}'): with fsw.add_node(f'conf-{seq}'):
fsw.property('compatible', bytes(compat)) fsw.property('compatible', bytes(compat))
fsw.property_string('description', model) fsw.property_string('description', model)
fsw.property_string('fdt', f'fdt-{seq}') fsw.property('fdt', bytes(''.join(f'fdt-{x}\x00' for x in files), "ascii"))
fsw.property_string('kernel', 'kernel') fsw.property_string('kernel', 'kernel')
fsw.end_node() fsw.end_node()
@ -193,21 +200,9 @@ def output_dtb(fsw, seq, fname, arch, compress):
fname (str): Filename containing the DTB fname (str): Filename containing the DTB
arch: FIT architecture, e.g. 'arm64' arch: FIT architecture, e.g. 'arm64'
compress (str): Compressed algorithm, e.g. 'gzip' compress (str): Compressed algorithm, e.g. 'gzip'
Returns:
tuple:
str: Model name
bytes: Compatible stringlist
""" """
with fsw.add_node(f'fdt-{seq}'): with fsw.add_node(f'fdt-{seq}'):
# Get the compatible / model information fsw.property_string('description', os.path.basename(fname))
with open(fname, 'rb') as inf:
data = inf.read()
fdt = libfdt.FdtRo(data)
model = fdt.getprop(0, 'model').as_str()
compat = fdt.getprop(0, 'compatible')
fsw.property_string('description', model)
fsw.property_string('type', 'flat_dt') fsw.property_string('type', 'flat_dt')
fsw.property_string('arch', arch) fsw.property_string('arch', arch)
fsw.property_string('compression', compress) fsw.property_string('compression', compress)
@ -215,9 +210,45 @@ def output_dtb(fsw, seq, fname, arch, compress):
with open(fname, 'rb') as inf: with open(fname, 'rb') as inf:
compressed = compress_data(inf, compress) compressed = compress_data(inf, compress)
fsw.property('data', compressed) fsw.property('data', compressed)
return model, compat
def process_dtb(fname, args):
"""Process an input DTB, decomposing it if requested and is possible
Args:
fname (str): Filename containing the DTB
args (Namespace): Program arguments
Returns:
tuple:
str: Model name string
str: Root compatible string
files: list of filenames corresponding to the DTB
"""
# Get the compatible / model information
with open(fname, 'rb') as inf:
data = inf.read()
fdt = libfdt.FdtRo(data)
model = fdt.getprop(0, 'model').as_str()
compat = fdt.getprop(0, 'compatible')
if args.decompose_dtbs:
# Check if the DTB needs to be decomposed
path, basename = os.path.split(fname)
cmd_fname = os.path.join(path, f'.{basename}.cmd')
with open(cmd_fname, 'r', encoding='ascii') as inf:
cmd = inf.read()
if 'scripts/dtc/fdtoverlay' in cmd:
# This depends on the structure of the composite DTB command
files = cmd.split()
files = files[files.index('-i') + 1:]
else:
files = [fname]
else:
files = [fname]
return (model, compat, files)
def build_fit(args): def build_fit(args):
"""Build the FIT from the provided files and arguments """Build the FIT from the provided files and arguments
@ -235,6 +266,7 @@ def build_fit(args):
fsw = libfdt.FdtSw() fsw = libfdt.FdtSw()
setup_fit(fsw, args.name) setup_fit(fsw, args.name)
entries = [] entries = []
fdts = {}
# Handle the kernel # Handle the kernel
with open(args.kernel, 'rb') as inf: with open(args.kernel, 'rb') as inf:
@ -243,12 +275,22 @@ def build_fit(args):
write_kernel(fsw, comp_data, args) write_kernel(fsw, comp_data, args)
for fname in args.dtbs: for fname in args.dtbs:
# Ignore overlay (.dtbo) files # Ignore non-DTB (*.dtb) files
if os.path.splitext(fname)[1] == '.dtb': if os.path.splitext(fname)[1] != '.dtb':
continue
(model, compat, files) = process_dtb(fname, args)
for fn in files:
if fn not in fdts:
seq += 1 seq += 1
size += os.path.getsize(fname) size += os.path.getsize(fn)
model, compat = output_dtb(fsw, seq, fname, args.arch, args.compress) output_dtb(fsw, seq, fn, args.arch, args.compress)
entries.append([model, compat]) fdts[fn] = seq
files_seq = [fdts[fn] for fn in files]
entries.append([model, compat, files_seq])
finish_fit(fsw, entries) finish_fit(fsw, entries)