Debian Codegraph @

debian/initramfs.hook

raw
1 #!/bin/sh
2 # intel-microcode initramfs-tools hook script version 3
3 # Copyright (C) 2012-2016 Henrique de Moraes Holschuh <[email protected]>
4 # Released under the GNU GPL v2 or later license
5 #
6 # Generates a copy of the Intel microcode (by default tailored to the
7 # running system), and installs it in the early initramfs.
8 #
9 # iucode_tool v1.0 or later is required.
10 #
11
12 PREREQ=""
13 IUCODE_CONFIG=/etc/default/intel-microcode
14
15 prereqs()
16 {
17 echo "$PREREQ"
18 }
19
20 case $1 in
21 prereqs)
22 prereqs
23 exit 0
24 ;;
25 esac
26
27 . /usr/share/initramfs-tools/hook-functions
28
29 verbose()
30 {
31 if [ "${verbose}" = "y" ] ; then
32 echo "intel-microcode: $*"
33 fi
34 :
35 }
36
37 if [ "${verbose}" = "y" ] ; then
38 IUCODE_TOOL_OPTIONS="-l"
39 else
40 IUCODE_TOOL_OPTIONS="-q"
41 fi
42
43 IUCODE_TOOL=$(command -v iucode_tool)
44 if [ -z "${IUCODE_TOOL}" ] && [ -x /usr/sbin/iucode_tool ] ; then
45 IUCODE_TOOL=/usr/sbin/iucode_tool
46 fi
47
48 IUCODE_FW_DIR=/usr/lib/firmware/intel-ucode
49 if [ "$MODULES" = "most" ]; then
50 IUCODE_TOOL_INITRAMFS=early
51 IUCODE_TOOL_SCANCPUS=no
52 else
53 IUCODE_TOOL_INITRAMFS=auto
54 IUCODE_TOOL_SCANCPUS=yes
55 fi
56 IUCODE_TOOL_EXTRA_OPTIONS=
57
58 [ -r ${IUCODE_CONFIG} ] && . ${IUCODE_CONFIG}
59
60 [ -z "${IUCODE_TOOL_INITRAMFS}" ] && IUCODE_TOOL_INITRAMFS=no
61
62 case "${IUCODE_TOOL_INITRAMFS}" in
63 no|0)
64 verbose "intel-microcode: disabled by ${IUCODE_CONFIG}"
65 exit 0
66 ;;
67 auto|early)
68 ;;
69 yes|1)
70 IUCODE_TOOL_INITRAMFS=early
71 echo "W: intel-microcode: initramfs mode not supported, using early initramfs mode" >&2
72 ;;
73 *)
74 echo "E: intel-microcode: invalid IUCODE_TOOL_INITRAMFS, using automatic mode" >&2
75 IUCODE_TOOL_INITRAMFS=auto
76 esac
77
78 # don't do anything unless there's an Intel processor in the system in auto mode
79 if [ "${IUCODE_TOOL_INITRAMFS}" = "auto" ] ; then
80 grep -q "^vendor_id[[:blank:]]*:[[:blank:]]*.*GenuineIntel" /proc/cpuinfo || {
81 verbose "no Intel processors detected, nothing to do"
82 exit 0
83 }
84 fi
85
86 # we require iucode_tool, but something is broken
87 if [ ! -x "${IUCODE_TOOL}" ] ; then
88 echo "E: intel-microcode: cannot run iucode_tool!" >&2
89 exit 0
90 fi
91
92 # Blacklist all kernel versions before v3.10, as they don't support early
93 # initramfs mode.
94 #
95 # This doesn't blacklist early 3.10 kernels in the LTS branches, we don't have
96 # enough information at the initramfs-tools layer, due to the way Debian and
97 # Ubuntu version kernel packages.
98 if dpkg --compare-versions "${version}" lt 3.10 ; then
99 echo "E: intel-microcode: unsupported kernel version!" >&2
100 exit 0
101 fi
102
103 if [ "${IUCODE_TOOL_SCANCPUS}" != "yes" ] ; then
104 verbose "adding microcode for either all or selected Intel processor models"
105 else
106 verbose "adding microcode for currently online and selected Intel processors"
107 grep -q cpu/cpuid /proc/devices || modprobe -q cpuid
108 IUCODE_TOOL_OPTIONS="${IUCODE_TOOL_OPTIONS} --scan-system"
109 fi
110
111 # paranoia
112 [ -z "${DESTDIR}" ] && {
113 echo "E: intel-microcode: DESTDIR empty!" >&2
114 exit 1
115 }
116 [ -z "${IUCODE_FW_DIR}" ] && {
117 echo "E: intel-microcode: IUCODE_FW_DIR empty!" >&2
118 exit 1
119 }
120
121 # include the microcode module in the initramfs for logging purposes, but
122 # ensure it will have no microcode data files to load. This is also a safety
123 # net: we don't want it to be acidentally loaded outside the initramfs.
124 #
125 # This shouldn't be expensive, as the in-kernel firmware loader is quite
126 # fast at detecting missing data files and doesn't wait for them.
127 #
128 # note: force_load will load a blacklisted module. We depend on that behavior.
129 #
130 # For 4.4 and later kernels, the microcode driver cannot be a module and will
131 # be built-in.
132 dpkg --compare-versions "${version}" lt 4.4 && {
133 [ -d "${DESTDIR}${IUCODE_FW_DIR}" ] && rm -fr "${DESTDIR}${IUCODE_FW_DIR}"
134
135 manual_add_modules microcode && {
136 # force_load has broken semanthics when the .ko file is missing
137 find "${DESTDIR}/${MODULESDIR}" -type f -print | grep -qc '/microcode\.ko$' && {
138 verbose "modular microcode driver detected"
139 force_load microcode
140 }
141 }
142 }
143
144 # generate early initramfs image and prepend
145 verbose "using early initramfs microcode update mode..."
146 EFW=$(mktemp "${TMPDIR:-/var/tmp}/mkinitramfs-EFW_XXXXXXXXXX") || {
147 echo "E: intel-microcode: cannot create temporary file" >&2
148 exit 1
149 }
150 ( find /usr/share/misc -maxdepth 1 -type f -name 'intel-microcode*' -print0 ;
151 find "${IUCODE_FW_DIR}" -maxdepth 0 -type d -print0 ) 2>/dev/null \
152 | xargs -0 -r -x ${IUCODE_TOOL} ${IUCODE_TOOL_OPTIONS} \
153 --write-earlyfw="${EFW}" --overwrite \
154 ${IUCODE_TOOL_EXTRA_OPTIONS} \
155 && prepend_earlyinitramfs "${EFW}" && {
156 rm "${EFW}"
157 exit 0
158 }
159
160 # usually we get here when initramfs-tools is missing prepend_earlyinitramfs()
161 # or when iucode_tool does not support --write-earlyfw, i.e. when old versions
162 # of these tools are installed.
163
164 rm "${EFW}" || true
165
166 echo "E: intel-microcode: failed to create or prepend the early initramfs to the initramfs" >&2
167
168 :