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