Skip to content

Commit 1eeec6f

Browse files
committed
Add devicetree-auto support for UKI
This adds support for matching multiple devicetrees in the `Devicetree` config option by using globs, and appending each one to a UKI with dtbauto. This does **not** support type 1 booting. Fixes #3827
1 parent eff0356 commit 1eeec6f

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

mkosi/__init__.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import hashlib
88
import itertools
99
import json
10+
import glob
1011
import logging
1112
import os
1213
import re
@@ -90,7 +91,7 @@
9091
from mkosi.installer import clean_package_manager_metadata
9192
from mkosi.installer.pacman import Pacman
9293
from mkosi.installer.zypper import Zypper
93-
from mkosi.kmod import gen_required_kernel_modules, is_valid_kdir, loaded_modules, process_kernel_modules
94+
from mkosi.kmod import gen_required_kernel_modules, is_valid_kdir, loaded_modules, process_kernel_modules, globs_match_filename
9495
from mkosi.log import ARG_DEBUG, complete_step, die, log_notice, log_step
9596
from mkosi.manifest import Manifest
9697
from mkosi.mounts import (
@@ -1598,6 +1599,7 @@ def build_kernel_modules_initrd(context: Context, kver: str) -> Path:
15981599

15991600
def find_devicetree(context: Context, kver: str) -> Path:
16001601
assert context.config.devicetree
1602+
assert not glob.has_magic(os.fspath(context.config.devicetree))
16011603

16021604
for d in (
16031605
context.root / f"usr/lib/firmware/{kver}/device-tree",
@@ -1611,6 +1613,38 @@ def find_devicetree(context: Context, kver: str) -> Path:
16111613
die(f"Requested devicetree {context.config.devicetree} not found")
16121614

16131615

1616+
1617+
def find_devicetrees(context: Context, kver: str) -> list[Path]:
1618+
assert context.config.devicetree
1619+
1620+
if not glob.has_magic(os.fspath(context.config.devicetree)):
1621+
# Single devicetree
1622+
return [find_devicetree(context, kver)]
1623+
1624+
# Glob pattern - search all directories
1625+
dtb_dirs = [
1626+
context.root / f"usr/lib/firmware/{kver}/device-tree",
1627+
context.root / f"usr/lib/linux-image-{kver}",
1628+
context.root / f"usr/lib/modules/{kver}/dtb",
1629+
]
1630+
1631+
matched_dtbs = []
1632+
for dtb_dir in dtb_dirs:
1633+
if not dtb_dir.exists():
1634+
continue
1635+
1636+
all_dtbs = [p for p in dtb_dir.rglob("*.dtb") if p.is_file()]
1637+
for dtb in all_dtbs:
1638+
rel_path = os.fspath(dtb.relative_to(dtb_dir))
1639+
if globs_match_filename(rel_path, [os.fspath(context.config.devicetree)], match_default=False):
1640+
matched_dtbs.append(dtb)
1641+
1642+
if not matched_dtbs:
1643+
logging.warning(f"Devicetree glob '{context.config.devicetree}' matched 0 files")
1644+
1645+
return matched_dtbs
1646+
1647+
16141648
def want_signed_pcrs(config: Config) -> bool:
16151649
return config.sign_expected_pcr == ConfigFeature.enabled or (
16161650
config.sign_expected_pcr == ConfigFeature.auto
@@ -1769,9 +1803,14 @@ def build_uki(
17691803
] # fmt: skip
17701804

17711805
if context.config.devicetree:
1772-
dtb = find_devicetree(context, kver)
1773-
arguments += ["--devicetree", workdir(dtb)]
1774-
options += ["--ro-bind", dtb, workdir(dtb)]
1806+
if glob.has_magic(os.fspath(context.config.devicetree)):
1807+
for dtb in find_devicetrees(context, kver):
1808+
arguments += ["--devicetree-auto", workdir(dtb)]
1809+
options += ["--ro-bind", dtb, workdir(dtb)]
1810+
else:
1811+
dtb = find_devicetree(context, kver)
1812+
arguments += ["--devicetree", workdir(dtb)]
1813+
options += ["--ro-bind", dtb, workdir(dtb)]
17751814

17761815
if context.config.splash:
17771816
splash = context.root / os.fspath(context.config.splash).lstrip("/")
@@ -2016,6 +2055,8 @@ def install_type1(
20162055

20172056
dtb = None
20182057
if context.config.devicetree:
2058+
if glob.has_magic(os.fspath(context.config.devicetree)):
2059+
die("Devicetree globs are not supported with Type 1 boot entries, use UKI builds instead")
20192060
dtb = dst / context.config.devicetree
20202061
with umask(~0o700):
20212062
dtb.parent.mkdir(parents=True, exist_ok=True)

mkosi/resources/man/mkosi.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,9 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
10851085
instead of the one provided by firmware. **mkosi** will search for the
10861086
specified file relative to common paths where Linux distributions install
10871087
Devicetree files. It should typically have the format `<vendor>/<board>.dtb`.
1088+
When this includes globs, multiple dtbs may be included **only** when building a UKI.
1089+
Internally this uses the dtbauto feature. Using globs/dtbauto is is **not**
1090+
supported for Type 1 booting.
10881091

10891092
`Splash=`, `--splash=`
10901093
: When set, the boot splash for any unified kernel image built by **mkosi** will

0 commit comments

Comments
 (0)