Blame SOURCES/dracut_99microcode_ctl-fw_dir_override_module_init.sh

c59b13
#!/bin/bash
c59b13
c59b13
# Hack in additional firmware directories for supported caveats.
c59b13
#
c59b13
# SPDX-License-Identifier: CC0-1.0
c59b13
c59b13
check() {
c59b13
	return 0
c59b13
}
c59b13
c59b13
install() {
c59b13
	local FW_DIR=/lib/firmware
c59b13
	local DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats
c59b13
	local CFG_DIR="/etc/microcode_ctl/ucode_with_caveats"
c59b13
	local check_caveats=/usr/libexec/microcode_ctl/check_caveats
c59b13
c59b13
	local verbose_opt
c59b13
	local cc_out
c59b13
	local path
c59b13
	local ignored
c59b13
	local do_skip_host_only
c59b13
	local p
c59b13
c59b13
	verbose_opt=
c59b13
	[ 4 -gt "$stdloglvl" ] || verbose_opt="-v"
c59b13
c59b13
	# HACK: we override external fw_dir variable in order to get
c59b13
	#       an additional ucode based on the kernel version.
c59b13
	dinfo "  microcode_ctl module: mangling fw_dir"
c59b13
c59b13
	[ -z "$fw_dir_l" ] || {
c59b13
		dinfo "    microcode_ctl: avoid touching fw_dir as" \
c59b13
		      "it has been changed (fw_dir_l is '$fw_dir_l')"
c59b13
c59b13
		return 0
c59b13
	}
c59b13
c59b13
	# Reset fw_dir to avoid inclusion of kernel-version-specific directories
c59b13
	# populated with microcode for the late load
c59b13
	[ "x$fw_dir" != \
c59b13
	  "x/lib/firmware/updates /lib/firmware /lib/firmware/$kernel" ] || {
c59b13
		fw_dir="/lib/firmware/updates /lib/firmware"
c59b13
		dinfo "    microcode_ctl: reset fw_dir to \"${fw_dir}\""
c59b13
	}
c59b13
078ac8
	fw_dir_add=""
078ac8
	while read -d $'\n' -r i; do
c59b13
		dinfo "    microcode_ctl: processing data directory " \
c59b13
		      "\"$DATA_DIR/$i\"..."
c59b13
c59b13
		if [ "x" != "x$hostonly" ]; then
c59b13
			do_skip_host_only=0
c59b13
c59b13
			local sho_overrides="
c59b13
				$CFG_DIR/skip-host-only-check
c59b13
				$CFG_DIR/skip-host-only-check-$i
c59b13
				$FW_DIR/$kernel/skip-host-only-check
c59b13
				$FW_DIR/$kernel/skip-host-only-check-$i"
c59b13
c59b13
			for p in $(echo "$sho_overrides"); do
c59b13
				[ -e "$p" ] || continue
c59b13
c59b13
				do_skip_host_only=1
c59b13
				dinfo "    microcode_ctl: $i; skipping" \
c59b13
				      "Host-Only check, since \"$p\" exists."
c59b13
				break
c59b13
			done
c59b13
		else
c59b13
			do_skip_host_only=1
c59b13
		fi
c59b13
3a6b56
		match_model_opt=""
3a6b56
		[ 1 = "$do_skip_host_only" ] || match_model_opt="-m"
c59b13
3a6b56
		if ! cc_out=$($check_caveats -e -k "$kernel" -c "$i" \
3a6b56
				$verbose_opt $match_model_opt)
3a6b56
		then
3a6b56
			dinfo "    microcode_ctl: kernel version \"$kernel\"" \
3a6b56
			      "failed early load check for \"$i\", skipping"
3a6b56
			continue
c59b13
		fi
c59b13
3a6b56
		path=$(printf "%s" "$cc_out" | sed -n 's/^paths //p')
3a6b56
		[ -n "$path" ] || {
3a6b56
			ignored=$(printf "%s" "$cc_out" | \
3a6b56
					sed -n 's/^skip_cfgs //p')
3a6b56
3a6b56
			if [ -n "$ignored" ]; then
3a6b56
				dinfo "    microcode_ctl: configuration" \
3a6b56
				      "\"$i\" is ignored"
3a6b56
			else
3a6b56
				dinfo "    microcode_ctl: no microcode paths" \
3a6b56
				      "are associated with \"$i\", skipping"
3a6b56
			fi
3a6b56
3a6b56
			continue
3a6b56
		}
3a6b56
c59b13
		dinfo "      microcode_ctl: $i: caveats check for kernel" \
c59b13
		      "version \"$kernel\" passed, adding" \
c59b13
		      "\"$DATA_DIR/$i\" to fw_dir variable"
c59b13
078ac8
		if [ 0 -eq "$do_skip_host_only" ]; then
078ac8
			fw_dir_add="$DATA_DIR/$i "
078ac8
		else
078ac8
			fw_dir_add="$DATA_DIR/$i $fw_dir_add"
078ac8
		fi
c59b13
	# The list of directories is reverse-sorted in order to preserve the
c59b13
	# "last wins" policy in case of presence of multiple microcode
c59b13
	# revisions.
c59b13
	#
c59b13
	# In case of hostonly == 0, all microcode revisions will be included,
c59b13
	# but since the microcode search is done with the "first wins" policy
c59b13
	# by the (early) microcode loading code, the correct microcode revision
c59b13
	# still has to be picked.
078ac8
	#
078ac8
	# Note that dracut without patch [1] puts only the last directory
078ac8
	# in the early cpio; we try to address this by putting only the last
078ac8
	# matching caveat in the search path, but that workaround works only
078ac8
	# for host-only mode; non-host-only mode early cpio generation is still
078ac8
	# broken without that patch.
078ac8
	#
078ac8
	# [1] https://github.com/dracutdevs/dracut/commit/c44d2252bb4b
c59b13
	done <<-EOF
078ac8
	$(find "$DATA_DIR" -maxdepth 1 -mindepth 1 -type d -printf "%f\n" \
078ac8
		| LC_ALL=C sort)
c59b13
	EOF
c59b13
078ac8
	fw_dir="${fw_dir_add}${fw_dir}"
c59b13
	dinfo "    microcode_ctl: final fw_dir: \"${fw_dir}\""
c59b13
}
c59b13