Debian Codegraph @

isenkram-autoinstall-firmware

raw
1 #!/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 [ "n/a" = "$dist" ] ; then
15 dist=testing
16 fi
17 arch=$(dpkg --print-architecture)
18 mirror=http://deb.debian.org/debian
19 aptsourcelist=/etc/apt/sources.list.d/isenkram-autoinstall-firmware-nf.list
20 aptsourcelist2=/etc/apt/sources.list.d/isenkram-autoinstall-firmware.list
21
22 if [ "-l" = "$1" ] ; then
23 listonly=true
24 else
25 listonly=false
26 fi
27
28 loginfo() {
29 echo "info: $@" 1>&2
30 }
31
32 add_nonfree_firmware() {
33 if $listonly ; then
34 return
35 fi
36 cat <<EOF > $aptsourcelist
37 deb $mirror $dist non-free-firmware
38 deb-src $mirror $dist non-free-firmware
39 EOF
40 chmod a+r $aptsourcelist
41 echo "apt updated" > $nonfreeflag
42 }
43 add_contrib_nonfree() {
44 if $listonly ; then
45 return
46 fi
47 cat <<EOF > $aptsourcelist2
48 deb $mirror $dist contrib non-free
49 deb-src $mirror $dist contrib non-free
50 EOF
51 chmod a+r $aptsourcelist2
52 echo "apt updated" > $nonfreeflag
53 }
54
55 appstreamlookup() {
56 fwfile="$1"
57 LC_ALL=C appstreamcli what-provides firmware:runtime "$fwfile" | \
58 awk '/Package:/ { print $2}'
59 }
60
61 # Find firmware files requested by loaded kernel drivers.
62 for fwfile in $(for module in $(awk '{print $1}' /proc/modules) ; do modinfo $module 2>/dev/null |awk '/^firmware:/ {print $2}'; done | LC_ALL=C sort -u); do
63 if [ ! -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 dmesg >/dev/null 2>&1 ; then
71 for fwfile in $(dmesg | sed -rn 's/^(\[[^]]*\] )?([^ ]+) [^ ]+: firmware: failed to load ([^ ]+) .*/\3/p'); do
72 loginfo "looking for firmware file $fwfile requested by kernel"
73 if [ ! -e "/lib/firmware/$fwfile" ] ; then
74 fwfiles="${fwfiles:+$fwfiles }$fwfile"
75 fi
76 done
77 else
78 loginfo "unable to check dmesg, list might be incomplete"
79 fi
80
81 if [ -z "$fwfiles" ] ; then
82 loginfo "did not find any firmware files requested by loaded kernel modules. exiting"
83 exit 1
84 fi
85 loginfo "some kernel driver requested extra firmware files:" $fwfiles
86
87 tmpdir=$(mktemp -d)
88 nonfreeflag=nonfreeflag
89 cd $tmpdir || exit 1
90
91 cleanup() {
92 cd /
93 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_data() {
100 if [ -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 [ -s "$f" ] ; then
108 datafiles="$datafiles $f"
109 fi
110 done
111 done
112 }
113
114 find_local_data
115 if [ -z "$datafiles" ] ; then
116 loginfo "unable to find any local firmware info for dist $dist, using info for sid"
117 datadist=sid
118 find_local_data
119 fi
120
121 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=$(appstreamlookup $fwfile)
127 if [ -z "$binpkginfo" ] && [ "$datafiles" ]; then
128 fwfilere=$(echo $fwfile | sed -e 's%/%\\/%g' -e 's/\./\\./g')
129 binpkginfo="$(awk "/^lib\/firmware\/$fwfilere/ {print \$2}" $datafiles)"
130 fi
131 if [ -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_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=$(echo "$binpkginfos" | tr ' ' '\n' | LC_ALL=C sort -u | xargs)
149
150 loginfo "determining whether enabling other components is required"
151 binpkgs="${binpkgs:+$binpkgs }$(
152 for binpkginfo in $binpkginfos ; do
153 echo $binpkginfo | while IFS=/ read section srcpkg binpkg ; do
154 echo $binpkg
155 # Enable the non-free-firmware or non-free section if it is needed
156 if ! LC_ALL=C apt-get install -s $binpkg >/dev/null 2>&1; then
157 if [ non-free-firmware = "$section" ] ; then
158 add_nonfree_firmware
159 elif [ non-free = "$section" ] \
160 || [ contrib = "$section" ] ; then
161 add_contrib_nonfree
162 fi
163 fi
164 done
165 done)"
166
167 if [ -e $nonfreeflag ] && ! $listonly ; then
168 # Fetch updated package lists
169 loginfo "Updating APT sources after adding non-free APT source"
170 apt-get -qq update
171 fi
172
173 if [ "$binpkgs" ] ; then
174 if $listonly ; then
175 echo $binpkgs
176 else
177 # Install firmware packages
178 loginfo "trying to install $binpkgs"
179 apt-get -qq install -y $binpkgs
180 fi
181 else
182 loginfo "No new firmware package with requested firmware detected."
183 fi
184 cleanup