Blame SOURCES/update_ucode

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