summary refs log tree commit diff
path: root/pkgs/os-specific/linux/device-tree
diff options
context:
space:
mode:
authorrnhmjoj <rnhmjoj@inventati.org>2022-08-19 21:34:32 +0200
committerrnhmjoj <rnhmjoj@inventati.org>2022-08-20 13:34:14 +0200
commit916ca8f2b0c208def051f8ea9760c534a40309db (patch)
treed7cbae3f6c7ec0a2dd68b4c9fb5bf382abe043f9 /pkgs/os-specific/linux/device-tree
parent802ea456991723a936e302b91d004345482b569a (diff)
downloadnixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar.gz
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar.bz2
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar.lz
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar.xz
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.tar.zst
nixpkgs-916ca8f2b0c208def051f8ea9760c534a40309db.zip
nixos/hardware/device-tree: make overlays more reliable
This make the process of applying overlays more reliable by:

1. Ignoring dtb files that are not really device trees. [^1]

2. Adding a `filter` option (per-overlay, there already is a global one)
   to limit the files to which the overlay applies. This is useful
   in cases where the `compatible` string is ambiguous and multiple
   unrelated files match.

Previously the script would fail in both cases.

[^1]: For example, there is dtbs/overlays/overlay_map.dtb in the
      Raspberry Pi 1 kernel.
Diffstat (limited to 'pkgs/os-specific/linux/device-tree')
-rw-r--r--pkgs/os-specific/linux/device-tree/default.nix35
1 files changed, 24 insertions, 11 deletions
diff --git a/pkgs/os-specific/linux/device-tree/default.nix b/pkgs/os-specific/linux/device-tree/default.nix
index 88791a1fb1d..8b8cca911a9 100644
--- a/pkgs/os-specific/linux/device-tree/default.nix
+++ b/pkgs/os-specific/linux/device-tree/default.nix
@@ -8,22 +8,35 @@ with lib; {
       overlays = toList overlays';
     in ''
       mkdir -p $out
-      cd ${base}
+      cd "${base}"
       find . -type f -name '*.dtb' -print0 \
-        | xargs -0 cp -v --no-preserve=mode --target-directory $out --parents
+        | xargs -0 cp -v --no-preserve=mode --target-directory "$out" --parents
 
-      for dtb in $(find $out -type f -name '*.dtb'); do
-        dtbCompat="$( fdtget -t s $dtb / compatible )"
+      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
 
         ${flip (concatMapStringsSep "\n") overlays (o: ''
-        overlayCompat="$( fdtget -t s ${o.dtboFile} / compatible )"
-        # overlayCompat in dtbCompat
-        if [[ "$dtbCompat" =~ "$overlayCompat" ]]; then
-          echo "Applying overlay ${o.name} to $( basename $dtb )"
-          mv $dtb{,.in}
-          fdtoverlay -o "$dtb" -i "$dtb.in" ${o.dtboFile};
-          rm $dtb.in
+        overlayCompat="$(fdtget -t s "${o.dtboFile}" / compatible)"
+
+        # skip incompatible and non-matching overlays
+        if [[ ! "$dtbCompat" =~ "$overlayCompat" ]]; then
+          echo -n "Skipping overlay ${o.name}: incompatible with $(basename "$dtb")"
+          continue
+        fi
+        ${optionalString (o.filter != null) ''
+        if [[ "''${dtb//${o.filter}/}" ==  "$dtb" ]]; then
+          echo -n "Skipping overlay ${o.name}: filter does not match $(basename "$dtb")"
+          continue
         fi
+        ''}
+
+        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"
         '')}
 
       done