Blame SOURCES/dracut_99microcode_ctl-fw_dir_override_module_init.sh

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