1{ lib, stdenv, stdenvNoCC, dtc }:
2
3with lib; {
4 # Compile single Device Tree overlay source
5 # file (.dts) into its compiled variant (.dtb)
6 compileDTS = ({
7 name,
8 dtsFile,
9 includePaths ? [],
10 extraPreprocessorFlags ? []
11 }: stdenv.mkDerivation {
12 inherit name;
13
14 nativeBuildInputs = [ dtc ];
15
16 buildCommand =
17 let
18 includeFlagsStr = lib.concatMapStringsSep " " (includePath: "-I${includePath}") includePaths;
19 extraPreprocessorFlagsStr = lib.concatStringsSep " " extraPreprocessorFlags;
20 in
21 ''
22 $CC -E -nostdinc ${includeFlagsStr} -undef -D__DTS__ -x assembler-with-cpp ${extraPreprocessorFlagsStr} ${dtsFile} | \
23 dtc -I dts -O dtb -@ -o $out
24 '';
25 });
26
27 applyOverlays = (base: overlays': stdenvNoCC.mkDerivation {
28 name = "device-tree-overlays";
29 nativeBuildInputs = [ dtc ];
30 buildCommand = let
31 overlays = toList overlays';
32 in ''
33 mkdir -p $out
34 cd "${base}"
35 find . -type f -name '*.dtb' -print0 \
36 | xargs -0 cp -v --no-preserve=mode --target-directory "$out" --parents
37
38 for dtb in $(find "$out" -type f -name '*.dtb'); do
39 dtbCompat=$(fdtget -t s "$dtb" / compatible 2>/dev/null || true)
40 # skip files without `compatible` string
41 test -z "$dtbCompat" && continue
42
43 ${flip (concatMapStringsSep "\n") overlays (o: ''
44 overlayCompat="$(fdtget -t s "${o.dtboFile}" / compatible)"
45
46 # skip incompatible and non-matching overlays
47 if [[ ! "$dtbCompat" =~ "$overlayCompat" ]]; then
48 echo "Skipping overlay ${o.name}: incompatible with $(basename "$dtb")"
49 elif ${if (o.filter == null) then "false" else ''
50 [[ "''${dtb//${o.filter}/}" == "$dtb" ]]
51 ''}
52 then
53 echo "Skipping overlay ${o.name}: filter does not match $(basename "$dtb")"
54 else
55 echo -n "Applying overlay ${o.name} to $(basename "$dtb")... "
56 mv "$dtb"{,.in}
57 fdtoverlay -o "$dtb" -i "$dtb.in" "${o.dtboFile}"
58 echo "ok"
59 rm "$dtb.in"
60 fi
61 '')}
62
63 done
64 '';
65 });
66}