| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # Find missing firmware files requested by the kernel, and install the |
| 4 | # binary packages with the files. Fetch them the from the non-free section |
| 5 | # if that is needed. |
| 6 | # |
| 7 | # This script was copied from auto-addfirmware from the debian-edu-config |
| 8 | # package and extended here. |
| 9 | |
| 10 | PATH=/sbin:/usr/sbin:$PATH |
| 11 | export PATH |
| 12 | |
| 13 | dist=$(lsb_release -cs) |
| 14 | if [scip-shell system . . `[`. "n/a" = "$dist" ] ; then |
| 15 | dist=testing |
| 16 | fi |
| 17 | arch=$(dpkgscip-shell system . . dpkg. --print-architecture) |
| 18 | mirror=http://deb.debian.org/debian |
| 19 | aptsourcelist=/etc/apt/sources.list.d/isenkram-autoinstall-firmware-nf.listscip-shell filesystem . . `/etc/apt/sources.list.d/isenkram-autoinstall-firmware-nf.list`. |
| 20 | aptsourcelist2=/etc/apt/sources.list.d/isenkram-autoinstall-firmware.listscip-shell filesystem . . `/etc/apt/sources.list.d/isenkram-autoinstall-firmware.list`. |
| 21 | |
| 22 | if [scip-shell system . . `[`. "-l" = "$1" ] ; then |
| 23 | listonly=true |
| 24 | else |
| 25 | listonly=false |
| 26 | fi |
| 27 | |
| 28 | loginfoscip-shell shell shell-project 0.0.0 loginfo().() { |
| 29 | echoscip-shell system . . echo. "info: $@" 1>&2 |
| 30 | } |
| 31 | |
| 32 | add_nonfree_firmwarescip-shell shell shell-project 0.0.0 add_nonfree_firmware().() { |
| 33 | if $listonly ; then |
| 34 | return |
| 35 | fi |
| 36 | catscip-shell system . . cat. <<EOF > $aptsourcelist |
| 37 | deb $mirror $dist non-free-firmware |
| 38 | deb-src $mirror $dist non-free-firmware |
| 39 | EOF |
| 40 | chmodscip-shell system . . chmod. a+r $aptsourcelist |
| 41 | echoscip-shell system . . echo. "apt updated" > $nonfreeflag |
| 42 | } |
| 43 | add_contrib_nonfreescip-shell shell shell-project 0.0.0 add_contrib_nonfree().() { |
| 44 | if $listonly ; then |
| 45 | return |
| 46 | fi |
| 47 | catscip-shell system . . cat. <<EOF > $aptsourcelist2 |
| 48 | deb $mirror $dist contrib non-free |
| 49 | deb-src $mirror $dist contrib non-free |
| 50 | EOF |
| 51 | chmodscip-shell system . . chmod. a+r $aptsourcelist2 |
| 52 | echoscip-shell system . . echo. "apt updated" > $nonfreeflag |
| 53 | } |
| 54 | |
| 55 | appstreamlookupscip-shell shell shell-project 0.0.0 appstreamlookup().() { |
| 56 | fwfile="$1" |
| 57 | LC_ALL=C appstreamcli what-provides firmware:runtime "$fwfile" | \ |
| 58 | awk '/Package:/ { print $2}scip-shell filesystem . . `/Package:/ { print $2}`.' |
| 59 | } |
| 60 | |
| 61 | # Find firmware files requested by loaded kernel drivers. |
| 62 | for fwfile in $(for module in $(awkscip-shell system . . awk. '{print $1}' /proc/modulesscip-shell filesystem . . `/proc/modules`.) ; do modinfo $module 2>/dev/nullscip-shell filesystem . . `/dev/null`. |awkscip-shell system . . awk. '/^firmware:/ {print $2}scip-shell filesystem . . `/^firmware:/ {print $2}`.'; done | LC_ALL=C sortscip-shell system . . sort. -u); do |
| 63 | if [scip-shell system . . `[`. ! -e "/lib/firmware/$fwfile" ] ; then |
| 64 | fwfiles="${fwfiles:+$fwfiles }$fwfile" |
| 65 | fi |
| 66 | done |
| 67 | |
| 68 | # Also look in dmesg for requested firmware for modules that refuse to |
| 69 | # load without their firmware. See also bug #725714. |
| 70 | if dmesgscip-shell system . . dmesg. >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 ; then |
| 71 | for fwfile in $(dmesgscip-shell system . . dmesg. | sedscip-shell system . . sed. -rn 's/^(\[[^]]*\] )?([^ ]+) [^ ]+: firmware: failed to load ([^ ]+) .*/\3/p'); do |
| 72 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "looking for firmware file $fwfile requested by kernel" |
| 73 | if [scip-shell system . . `[`. ! -e "/lib/firmware/$fwfile" ] ; then |
| 74 | fwfiles="${fwfiles:+$fwfiles }$fwfile" |
| 75 | fi |
| 76 | done |
| 77 | else |
| 78 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "unable to check dmesg, list might be incomplete" |
| 79 | fi |
| 80 | |
| 81 | if [scip-shell system . . `[`. -z "$fwfiles" ] ; then |
| 82 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "did not find any firmware files requested by loaded kernel modules. exiting" |
| 83 | exit 1 |
| 84 | fi |
| 85 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "some kernel driver requested extra firmware files:" $fwfiles |
| 86 | |
| 87 | tmpdir=$(mktempscip-shell system . . mktemp. -d) |
| 88 | nonfreeflag=nonfreeflag |
| 89 | cd $tmpdir || exit 1 |
| 90 | |
| 91 | cleanupscip-shell shell shell-project 0.0.0 cleanup().() { |
| 92 | cd /scip-shell filesystem . . `/`. |
| 93 | rmscip-shell system . . rm. -rf $tmpdir |
| 94 | } |
| 95 | |
| 96 | # Prefer appstream and use the local copy of firmware->package |
| 97 | # mappings if nothing is found using appstream. |
| 98 | |
| 99 | find_local_datascip-shell shell shell-project 0.0.0 find_local_data().() { |
| 100 | if [scip-shell system . . `[`. -z "$datadist" ] ; then |
| 101 | datadist="$dist" |
| 102 | fi |
| 103 | datafiles="" |
| 104 | for a in $arch all; do |
| 105 | for f in Fw-Contents-$a-$datadist-* \ |
| 106 | /usr/share/isenkram/Fw-Contents-$a-$datadist-* ; do |
| 107 | if [scip-shell system . . `[`. -s "$f" ] ; then |
| 108 | datafiles="$datafiles $f" |
| 109 | fi |
| 110 | done |
| 111 | done |
| 112 | } |
| 113 | |
| 114 | find_local_datascip-shell shell shell-project 0.0.0 find_local_data(). |
| 115 | if [scip-shell system . . `[`. -z "$datafiles" ] ; then |
| 116 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "unable to find any local firmware info for dist $dist, using info for sid" |
| 117 | datadist=sid |
| 118 | find_local_datascip-shell shell shell-project 0.0.0 find_local_data(). |
| 119 | fi |
| 120 | |
| 121 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "locating packages with the requested firmware files" |
| 122 | binpkginfos="" |
| 123 | binpkgs="" |
| 124 | for fwfile in $fwfiles ; do |
| 125 | # First check appstream, next look in our local files if they exist |
| 126 | binpkginfo=$(appstreamlookupscip-shell shell shell-project 0.0.0 appstreamlookup(). $fwfile) |
| 127 | if [scip-shell system . . `[`. -z "$binpkginfo" ] && [scip-shell system . . `[`. "$datafiles" ]; then |
| 128 | fwfilere=$(echoscip-shell system . . echo. $fwfile | sedscip-shell system . . sed. -e 's%/%\\/%g' -e 's/\./\\./g') |
| 129 | binpkginfo="$(awkscip-shell system . . awk. "/^lib\/firmware\/$fwfilere/ {print \$2}" $datafiles)" |
| 130 | fi |
| 131 | if [scip-shell system . . `[`. -z "$binpkginfo" ] ; then |
| 132 | # Special case for b43 where the firmware is |
| 133 | # undistributable by Debian. |
| 134 | case "$fwfile" in |
| 135 | b43/*) |
| 136 | add_contrib_nonfreescip-shell shell shell-project 0.0.0 add_contrib_nonfree(). |
| 137 | binpkgs="${binpkgs:+$binpkgs }firmware-b43-installer" |
| 138 | ;; |
| 139 | esac |
| 140 | else |
| 141 | binpkginfos="$binpkginfos $binpkginfo" |
| 142 | fi |
| 143 | done |
| 144 | |
| 145 | # Many different files can be required for a single hardware device, |
| 146 | # plus several devices might require files from the same package, so |
| 147 | # optimize! |
| 148 | binpkginfos=$(echoscip-shell system . . echo. "$binpkginfos" | trscip-shell system . . tr. ' ' '\n' | LC_ALL=C sortscip-shell system . . sort. -u | xargsscip-shell system . . xargs.) |
| 149 | |
| 150 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "determining whether enabling other components is required" |
| 151 | binpkgs="${binpkgs:+$binpkgs }$( |
| 152 | for binpkginfo in $binpkginfos ; do |
| 153 | echoscip-shell system . . echo. $binpkginfo | while IFS=/scip-shell filesystem . . `/`. read section srcpkg binpkg ; do |
| 154 | echoscip-shell system . . echo. $binpkg |
| 155 | # Enable the non-free-firmware or non-free section if it is needed |
| 156 | if ! LC_ALL=C apt-getscip-shell system . . apt-get. install -s $binpkg >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 157 | if [scip-shell system . . `[`. non-free-firmware = "$section" ] ; then |
| 158 | add_nonfree_firmwarescip-shell shell shell-project 0.0.0 add_nonfree_firmware(). |
| 159 | elif [scip-shell system . . `[`. non-free = "$section" ] \ |
| 160 | || [ contriscip-shell shell shell-project 0.0.0 add_contrib_nonfree().b = "$section" ] ; then |
| 161 | add_contrib_nonfree |
| 162 | fi |
| 163 | fi |
| 164 | done |
| 165 | done)" |
| 166 | |
| 167 | if [scip-shell system . . `[`. -e $nonfreeflag ] && ! $listonly ; then |
| 168 | # Fetch updated package lists |
| 169 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "Updating APT sources after adding non-free APT source" |
| 170 | apt-getscip-shell system . . apt-get. -qq update |
| 171 | fi |
| 172 | |
| 173 | if [scip-shell system . . `[`. "$binpkgs" ] ; then |
| 174 | if $listonly ; then |
| 175 | echoscip-shell system . . echo. $binpkgs |
| 176 | else |
| 177 | # Install firmware packages |
| 178 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "trying to install $binpkgs" |
| 179 | apt-getscip-shell system . . apt-get. -qq install -y $binpkgs |
| 180 | fi |
| 181 | else |
| 182 | loginfoscip-shell shell shell-project 0.0.0 loginfo(). "No new firmware package with requested firmware detected." |
| 183 | fi |
| 184 | cleanupscip-shell shell shell-project 0.0.0 cleanup(). |