Blame SOURCES/update_ucode

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