2023-10-09 19:29:22 +00:00
|
|
|
{ lib, stdenv, stdenvNoCC, dtc }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
with lib; {
|
2023-10-09 19:29:22 +00:00
|
|
|
# Compile single Device Tree overlay source
|
|
|
|
# file (.dts) into its compiled variant (.dtb)
|
|
|
|
compileDTS = ({
|
|
|
|
name,
|
|
|
|
dtsFile,
|
|
|
|
includePaths ? [],
|
|
|
|
extraPreprocessorFlags ? []
|
|
|
|
}: stdenv.mkDerivation {
|
|
|
|
inherit name;
|
|
|
|
|
|
|
|
nativeBuildInputs = [ dtc ];
|
|
|
|
|
|
|
|
buildCommand =
|
|
|
|
let
|
|
|
|
includeFlagsStr = lib.concatMapStringsSep " " (includePath: "-I${includePath}") includePaths;
|
|
|
|
extraPreprocessorFlagsStr = lib.concatStringsSep " " extraPreprocessorFlags;
|
|
|
|
in
|
|
|
|
''
|
|
|
|
$CC -E -nostdinc ${includeFlagsStr} -undef -D__DTS__ -x assembler-with-cpp ${extraPreprocessorFlagsStr} ${dtsFile} | \
|
|
|
|
dtc -I dts -O dtb -@ -o $out
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
applyOverlays = (base: overlays': stdenvNoCC.mkDerivation {
|
2020-04-24 23:36:52 +00:00
|
|
|
name = "device-tree-overlays";
|
2022-04-27 09:35:20 +00:00
|
|
|
nativeBuildInputs = [ dtc ];
|
2020-04-24 23:36:52 +00:00
|
|
|
buildCommand = let
|
2020-09-25 04:45:31 +00:00
|
|
|
overlays = toList overlays';
|
2020-04-24 23:36:52 +00:00
|
|
|
in ''
|
2020-09-25 04:45:31 +00:00
|
|
|
mkdir -p $out
|
2022-09-09 14:08:57 +00:00
|
|
|
cd "${base}"
|
2020-09-25 04:45:31 +00:00
|
|
|
find . -type f -name '*.dtb' -print0 \
|
2022-09-09 14:08:57 +00:00
|
|
|
| xargs -0 cp -v --no-preserve=mode --target-directory "$out" --parents
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
for dtb in $(find "$out" -type f -name '*.dtb'); do
|
|
|
|
dtbCompat=$(fdtget -t s "$dtb" / compatible 2>/dev/null || true)
|
|
|
|
# skip files without `compatible` string
|
|
|
|
test -z "$dtbCompat" && continue
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
${flip (concatMapStringsSep "\n") overlays (o: ''
|
2022-09-09 14:08:57 +00:00
|
|
|
overlayCompat="$(fdtget -t s "${o.dtboFile}" / compatible)"
|
|
|
|
|
|
|
|
# skip incompatible and non-matching overlays
|
|
|
|
if [[ ! "$dtbCompat" =~ "$overlayCompat" ]]; then
|
2023-02-02 18:25:31 +00:00
|
|
|
echo "Skipping overlay ${o.name}: incompatible with $(basename "$dtb")"
|
|
|
|
elif ${if (o.filter == null) then "false" else ''
|
|
|
|
[[ "''${dtb//${o.filter}/}" == "$dtb" ]]
|
2022-09-09 14:08:57 +00:00
|
|
|
''}
|
2023-02-02 18:25:31 +00:00
|
|
|
then
|
|
|
|
echo "Skipping overlay ${o.name}: filter does not match $(basename "$dtb")"
|
|
|
|
else
|
|
|
|
echo -n "Applying overlay ${o.name} to $(basename "$dtb")... "
|
|
|
|
mv "$dtb"{,.in}
|
|
|
|
fdtoverlay -o "$dtb" -i "$dtb.in" "${o.dtboFile}"
|
|
|
|
echo "ok"
|
|
|
|
rm "$dtb.in"
|
|
|
|
fi
|
2020-09-25 04:45:31 +00:00
|
|
|
'')}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
done
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
}
|