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