Blame SOURCES/update_ucode

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