Blame SOURCES/dracut_99microcode_ctl-fw_dir_override_module_init.sh

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