Blame SOURCES/update_ucode

539655
#! /bin/bash -eu
539655
539655
# Maintain kernel-version-specific symlinks in /lib/firmware based on
539655
# configuration present in /usr/share/microcode_ctl/ucode_with_caveats.
539655
#
539655
# SPDX-License-Identifier: CC0-1.0
539655
539655
usage()
539655
{
539655
	echo "Usage: update_ucode [--action {add|remove|refresh}]" \
539655
	     "[--kernel KERNELVER]* [--verbose] [--dry-run]" \
539655
	     "[--trigger-dracut] [--cleanup intel_ucode caveats_ucode]" >&2
539655
}
539655
539655
debug() { [ 0 = "$verbose" ] || echo "$*" >&2; }
539655
539655
MC_DIR=/usr/share/microcode_ctl
539655
INTEL_UCODE_DIR=intel-ucode
539655
DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats
539655
FW_DIR=/lib/firmware
539655
check_caveats=/usr/libexec/microcode_ctl/check_caveats
539655
539655
action=refresh
539655
kernel=
539655
verbose=0
539655
verbose_opt=
539655
dry_run=0
539655
trigger_dracut=0
539655
remove_cleanup=0
539655
cleanup_intel=
539655
cleanup_caveats=
539655
539655
while [ 1 -le "$#" ]; do
539655
	case "$1" in
539655
	-a|--action)
539655
		shift
539655
		action="$1"
539655
		;;
539655
	-k|--kernel)
539655
		shift
539655
		kernel="$kernel $1"
539655
		;;
539655
	-v|--verbose)
539655
		verbose=1
539655
		verbose_opt="-v"
539655
		;;
539655
	-n|--dry-run)
539655
		dry_run=1
539655
		;;
539655
	-d|--trigger-dracut)
539655
		trigger_dracut=1
539655
		;;
539655
	-c|--cleanup)
539655
		remove_cleanup=1
539655
		shift
539655
		cleanup_intel="$1"
539655
		shift
539655
		cleanup_caveats="$1"
539655
		;;
539655
	*)
539655
		echo "Unknown argument \"$1\"" >&2
539655
		usage
539655
		exit 1
539655
	esac
539655
	shift
539655
done
539655
539655
cmd=
539655
[ 0 -eq "$dry_run" ] || cmd=echo
539655
539655
case "$action" in
539655
add|remove|refresh)
539655
	# Scan all directories in FW_DIR and all existing kernels
539655
	if [ -z "$kernel" ]; then
539655
		debug "No kernel versions provided, scanning..."
539655
539655
		kvers=$(find /lib/modules/ -name '[2-9].*' -print)
539655
		for k_dir in $kvers; do
539655
			k="${k_dir#/lib/modules/}"
539655
			[ ! -e "/boot/symvers-$k.gz" ] || {
539655
				debug "  Adding $k (from /lib/modules)"
539655
				kernel="$kernel $k"
539655
			}
539655
		done
539655
539655
		kvers=$(find /lib/firmware/ -name '[2-9].*' -print)
539655
		for k_dir in $kvers; do
539655
			k="${k_dir#/lib/firmware/}"
539655
			[ ! -d "$k_dir" ] || {
539655
				debug "  Adding $k (from /lib/firmware)"
539655
				kernel="$kernel $k"
539655
			}
539655
		done
539655
	fi
539655
	;;
539655
*)
539655
	echo "Unknown action \"$action\"" >&2
539655
	usage
539655
	exit 1
539655
	;;
539655
esac
539655
539655
# Generic part: managing intel ucode
539655
debug "Running action \"$action\" on common Intel microcode directory"
539655
while :; do
539655
	[ ! -e "/etc/microcode_ctl/intel-ucode-disallow" ] || {
539655
		debug "  Skipping \"$i\":" \
539655
		      "\"/etc/microcode_ctl/intel-ucode-disallow\"" \
539655
		      "present"
539655
		break
539655
	}
539655
	[ ! -e "$FW_DIR/intel-ucode-disallow" ] || {
539655
		debug "  Found \"$FW_DIR/intel-ucode-disallow\"," \
539655
		      "skipping"
539655
		break
539655
	}
539655
539655
	# Removing old files
539655
	case "$action" in
539655
	refresh|remove)
539655
		debug "  Removing old files from ${MC_DIR}/${INTEL_UCODE_DIR}"
539655
		if [ 0 = "$remove_cleanup" ]; then
539655
			find "${MC_DIR}/${INTEL_UCODE_DIR}" \
539655
				-maxdepth 1 -mindepth 1 \
539655
				-type f -printf '%f\n'
539655
		else
539655
			cat "$cleanup_intel"
539655
		fi | while read -r fname; do
539655
			name="${FW_DIR}/${INTEL_UCODE_DIR}/${fname}"
539655
539655
			# Needed in case we downgrade to a version where
539655
			# no symlinks in /lib/firmware were used
539655
			if [ 1 = "$remove_cleanup" ]; then
539655
				[ -L "$name" ] || continue
539655
			fi
539655
539655
			$cmd rm -f $verbose_opt "$name"
539655
		done
539655
		$cmd rmdir -p $verbose_opt "${FW_DIR}/${INTEL_UCODE_DIR}" 2>/dev/null || true
539655
		;;
539655
	esac
539655
539655
	# Adding new ones
539655
	case "$action" in
539655
	add|refresh)
539655
		if grep -q '^flags[[:space:]]*:.*hypervisor' /proc/cpuinfo; then
539655
			debug "  A virtualised environment has been detected" \
539655
			      "(hypervisor flag is present in /proc/cpuinfo)," \
539655
			      "skipping"
539655
			break
539655
		fi
539655
539655
		debug "  Creating symlinks in ${MC_DIR}/${INTEL_UCODE_DIR}"
539655
		$cmd mkdir -p $verbose_opt "${FW_DIR}/${INTEL_UCODE_DIR}"
539655
		$cmd find "${MC_DIR}/${INTEL_UCODE_DIR}" -maxdepth 1 -mindepth 1 \
539655
			-type f -exec bash -c 'ln -s '"$verbose_opt"' '\''{}'\'' \
539655
				"'"${FW_DIR}/${INTEL_UCODE_DIR}/"'$(basename '\''{}'\'')"' \;
539655
		;;
539655
	esac
539655
539655
	break
539655
done
539655
539655
debug "Running action \"$action\" on kernels $kernel"
539655
539655
if [ 0 = "$remove_cleanup" ]; then
539655
	ls "$DATA_DIR"
539655
else
539655
	cat "$cleanup_caveats"
539655
fi | while read -r i; do
539655
	debug "Processing data directory \"$i\"..."
539655
	[ -e "$DATA_DIR/$i/readme" ] || {
539655
		debug "  Skipping \"$i\": no readme"
539655
		continue
539655
	}
539655
	[ -e "$DATA_DIR/$i/config" ] || {
539655
		debug "  Skipping \"$i\": no config"
539655
		continue
539655
	}
539655
	[ ! -e "/etc/microcode_ctl/ucode_with_caveats/disallow-$i" ] || {
539655
		debug "  Skipping \"$i\":" \
539655
		      "\"/etc/microcode_ctl/ucode_with_caveats/disallow-$i\"" \
539655
		      "present"
539655
		continue
539655
	}
539655
539655
	for k in $(echo "$kernel"); do
539655
		debug "    Processing kernel version \"$k\""
539655
		$check_caveats -k "$k" -c "$i" $verbose_opt > /dev/null || {
539655
			debug "    Checking for caveats failed" \
539655
			      "(kernel version \"$k\"), skipping"
539655
			continue
539655
		}
539655
		path=$($check_caveats -k "$k" -c "$i")
539655
539655
		case "$action" in
539655
		remove|refresh)
539655
			debug "    Removing $path (part of $action)..."
539655
539655
			if [ -e "$FW_DIR/$k/readme-$i" ]; then
539655
				debug "      Removing \"$FW_DIR/$k/$path\""
539655
				$cmd rm -f $verbose_opt "$FW_DIR/$k/$path"
539655
				$cmd rm -f $verbose_opt "$FW_DIR/$k/readme-$i"
539655
				$cmd rmdir -p $verbose_opt "$(dirname "$FW_DIR/$k/$path")" 2>/dev/null || true
539655
539655
				[ 0 -eq "$trigger_dracut" ] || {
539655
					debug "      Triggering dracut for kernel \"$k\""
539655
					$cmd dracut -f $verbose_opt --kver "$k"
539655
				}
539655
			else
539655
				debug "      \"$FW_DIR/$k/readme-$i\" is not found," \
539655
				      "skipping \"$FW_DIR/$k/$path\" removal"
539655
			fi
539655
			;;
539655
		esac
539655
539655
		case "$action" in
539655
		add|refresh)
539655
			debug "    Adding $path (part of $action)..."
539655
539655
			[ -e "/boot/symvers-$k.gz" ] || {
539655
				debug "      \"/boot/symvers-$k.gz\"" \
539655
				      "does not exist, skipping"
539655
				continue
539655
			}
539655
			if grep -q '^flags[[:space:]]*:.*hypervisor' /proc/cpuinfo; then
539655
				debug "      A virtualised environment has been detected" \
539655
				      "(hypervisor flag is present in /proc/cpuinfo)," \
539655
				      "skipping"
539655
				break
539655
			fi
539655
			[ ! -e "$FW_DIR/$k/disallow-$i" ] || {
539655
				debug "      Found \"$FW_DIR/$k/disallow-$i\"," \
539655
				      "skipping"
539655
				continue
539655
			}
539655
			[ ! -e "$FW_DIR/$k/$path" ] || {
539655
				debug "      \"$FW_DIR/$k/$path\" already exists," \
539655
				      "skipping"
539655
				continue
539655
			}
539655
539655
			debug "      Adding \"$FW_DIR/$k/$path\""
539655
			$cmd mkdir -p "$(dirname "$FW_DIR/$k/$path")"
539655
			$cmd ln -s "$DATA_DIR/$i/$path" "$FW_DIR/$k/$path"
539655
			$cmd cp "$DATA_DIR/$i/readme" "$FW_DIR/$k/readme-$i"
539655
539655
			[ 0 -eq "$trigger_dracut" ] || {
539655
				debug "      Triggering dracut for kernel \"$k\""
539655
				$cmd dracut -f $verbose_opt --kver "$k"
539655
			}
539655
			;;
539655
		remove)
539655
		esac
539655
	done
539655
done