Blame SOURCES/check_caveats

539655
#! /bin/bash -eu
539655
539655
# Script for checking various microcode caveats
539655
#
539655
#
539655
# SPDX-License-Identifier: CC0-1.0
539655
539655
: ${MC_CAVEATS_DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats}
539655
539655
usage() {
539655
	echo 'Usage: check_caveats [-k TARGET_KVER] [-c CONFIG] [-m] [-v]'
539655
	echo
539655
	echo '   -k - target version to check against, $(uname -r) is used'
539655
	echo '        otherwise'
539655
	echo '   -c - caveat config(s) to check, all configs are checked'
539655
	echo '        otherwise'
539655
	echo '   -m - check that caveats actually apply to the current model'
539655
	echo '   -v - verbose output'
539655
	echo
539655
	echo 'Environment:'
539655
	echo '  MC_CAVEATS_DATA_DIR - directory that contains caveats'
539655
	echo '                        configuration data'
539655
}
539655
539655
debug() { [ 0 = "$verbose" ] || echo "$*" >&2; }
539655
539655
# A simplified RPM version comparison that takes into account knowledge about
539655
# Y- and Z-streams (so it compares versions inside Y-stram or Z-stream if
539655
# the version against which comparison is performed has appropriate versioning
539655
# scheme).
539655
#
539655
# $1 - kernel version to check
539655
# $* - list of kernel versions to check against
539655
check_kver()
539655
{
539655
	local t_major= t_minor= t_patch= t_y= t_z1= t_z2= t_rest=
539655
	local m_major= m_minor= m_patch= m_y= m_z1= m_z2= m_rest=
539655
	local cmp_type=
539655
539655
	# IFS=.- read -r t_major t_minor t_patch t_y t_z1 t_z2 t_rest <<<"$1"
539655
	# "cannot create temp file for here-document: Read-only file system"
539655
	# that's why we can't have nice things.
539655
	t_major=${1%%[.-]*}
539655
	t_rest=${1#${t_major}}
539655
	t_rest=${t_rest#[.-]}
539655
	t_minor=${t_rest%%[.-]*}
539655
	t_rest=${t_rest#${t_minor}}
539655
	t_rest=${t_rest#[.-]}
539655
	t_patch=${t_rest%%[.-]*}
539655
	t_rest=${t_rest#${t_patch}}
539655
	t_rest=${t_rest#[.-]}
539655
	t_y=${t_rest%%[.-]*}
539655
	t_rest=${t_rest#${t_y}}
539655
	t_rest=${t_rest#[.-]}
539655
	t_z1=${t_rest%%[.-]*}
539655
	t_rest=${t_rest#${t_z1}}
539655
	t_rest=${t_rest#[.-]}
539655
	t_z2=${t_rest%%[.-]*}
539655
539655
	# minor/major/patch/y should be numeric
539655
	[ -n "${t_major##*[!0-9]*}" ] || return 1
539655
	[ -n "${t_minor##*[!0-9]*}" ] || return 1
539655
	[ -n "${t_patch##*[!0-9]*}" ] || return 1
539655
	[ -n "${t_y##*[!0-9]*}" ] || return 1
539655
	# reset z1/z2 to zero if non-numeric
539655
	[ -n "${t_z1##*[!0-9]*}" ] || t_z1=0
539655
	[ -n "${t_z2##*[!0-9]*}" ] || t_z2=0
539655
539655
	while [ 1 -lt "$#" ]; do
539655
		cmp_type=upstream
539655
539655
		shift
539655
		m_major=${1%%[.-]*}
539655
		m_rest=${1#${m_major}}
539655
		m_rest=${m_rest#[.-]}
539655
		m_minor=${m_rest%%[.-]*}
539655
		m_rest=${m_rest#${m_minor}}
539655
		m_rest=${m_rest#[.-]}
539655
		m_patch=${m_rest%%[.-]*}
539655
		m_rest=${m_rest#${m_patch}}
539655
		m_rest=${m_rest#[.-]}
539655
		m_y=${m_rest%%[.-]*}
539655
		m_rest=${m_rest#${m_y}}
539655
		m_rest=${m_rest#[.-]}
539655
		m_z1=${m_rest%%[.-]*}
539655
		m_rest=${m_rest#${m_z1}}
539655
		m_rest=${m_rest#[.-]}
539655
		m_z2=${m_rest%%[.-]*}
539655
539655
		# minor/major/patch should be numeric
539655
		[ -n "${m_major##*[!0-9]*}" ] || continue
539655
		[ -n "${m_minor##*[!0-9]*}" ] || continue
539655
		[ -n "${m_patch##*[!0-9]*}" ] || continue
539655
		# reset z1/z2 to zero if non-numeric
539655
		[ -n "${m_y##*[!0-9]*}" ] && cmp_type=y || m_y=0
539655
		[ -n "${m_z1##*[!0-9]*}" ] && cmp_type=z || m_z1=0
539655
		[ -n "${m_z2##*[!0-9]*}" ] && cmp_type=z || m_z2=0
539655
539655
		# Comparing versions
539655
		case "$cmp_type" in
539655
		upstream)
539655
			[ "$t_major" -ge "$m_major" ] || continue
539655
			[ "$t_minor" -ge "$m_minor" ] || continue
539655
			[ "$t_patch" -ge "$m_patch" ] || continue
539655
			return 0
539655
			;;
539655
		y)
539655
			[ "$t_major" -eq "$m_major" ] || continue
539655
			[ "$t_minor" -eq "$m_minor" ] || continue
539655
			[ "$t_patch" -eq "$m_patch" ] || continue
539655
			[ "$t_y" -ge "$m_y" ] || continue
539655
			return 0
539655
			;;
539655
		z)
539655
			[ "$t_major" -eq "$m_major" ] || continue
539655
			[ "$t_minor" -eq "$m_minor" ] || continue
539655
			[ "$t_patch" -eq "$m_patch" ] || continue
539655
			[ "$t_y" -eq "$m_y" ] || continue
539655
			[ "$t_z1" -ge "$m_z1" ] || continue
539655
			[ "$t_z2" -ge "$m_z2" ] || continue
539655
			return 0
539655
			;;
539655
		esac
539655
	done
539655
539655
	return 1
539655
}
539655
539655
# Provides model in format "VENDOR_ID FAMILY-MODEL-STEPPING"
539655
#
539655
# We check only the first processor as we don't expect non-symmetrical setups
539655
# with CPUs with caveats
539655
get_model_string()
539655
{
539655
	/usr/bin/printf "%s %02x-%02x-%02x" \
539655
		$(/bin/sed -rn '1,/^$/{
539655
			s/^vendor_id[[:space:]]*: (.*)$/\1/p;
539655
			s/^cpu family[[:space:]]*: (.*)$/\1/p;
539655
			s/^model[[:space:]]*: (.*)$/\1/p;
539655
			s/^stepping[[:space:]]*: (.*)$/\1/p;
539655
		}' /proc/cpuinfo)
539655
}
539655
539655
get_model_name()
539655
{
539655
	/bin/sed -rn '1,/^$/s/^model name[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
539655
}
539655
539655
#check_kver "$@"
539655
#get_model_name
539655
539655
match_model=0
539655
configs=
539655
kver=$(/bin/uname -r)
539655
verbose=0
539655
539655
while getopts "k:c:mv" opt; do
539655
	case "${opt}" in
539655
	k)
539655
		kver="$OPTARG"
539655
		;;
539655
	c)
539655
		configs="$configs $OPTARG"
539655
		;;
539655
	m)
539655
		match_model=1
539655
		;;
539655
	v)
539655
		verbose=1
539655
		;;
539655
	*)
539655
		usage
539655
		exit 1;
539655
		;;
539655
	esac
539655
done
539655
539655
: ${configs:=$(find "${MC_CAVEATS_DATA_DIR}" -maxdepth 1 -mindepth 1 -type d -printf "%f\n")}
539655
539655
cpu_model=$(get_model_string)
539655
cpu_model_name=$(get_model_name)
539655
539655
for cfg in $(echo "${configs}"); do
539655
	dir="$MC_CAVEATS_DATA_DIR/$cfg"
539655
	[ -r "${dir}/config" ] || {
539655
		debug "File 'config' in ${dir} is not found, skipping"
539655
		continue
539655
	}
539655
539655
	cfg_model=
539655
	cfg_path=
539655
	cfg_kvers=
539655
	cfg_blacklist=
539655
539655
	while read -r key value; do
539655
		case "$key" in
539655
		model)
539655
			cfg_model="$value"
539655
			;;
539655
		path)
539655
			cfg_path="$cfg_path $value"
539655
			;;
539655
		kernel)
539655
			cfg_kvers="$cfg_kvers $value"
539655
			;;
539655
		blacklist)
539655
			cfg_blacklist=1
539655
			break
539655
			;;
539655
		esac
539655
	done < "${dir}/config"
539655
539655
	[ -z "${cfg_blacklist}" ] || \
539655
		cfg_blacklist=$(/bin/sed -n '/^blacklist$/,$p' "${dir}/config" |
539655
					/usr/bin/tail -n +2)
539655
539655
	debug "${cfg}: model '$cfg_model', path '$cfg_path', kvers '$cfg_kvers'"
539655
	debug "${cfg}: blacklist '$cfg_blacklist'"
539655
539655
	if [ -n "$cfg_model" ]; then
539655
		[ 0 -eq "$match_model" ] || {
539655
			[ "x$cpu_model" = "x$cfg_model" ] || {
539655
				debug "Current CPU model '$cpu_model' doesn't" \
539655
				      "match config CPU model '$cfg_model'," \
539655
				      "skipping"
539655
				continue
539655
			}
539655
		}
539655
	fi
539655
539655
	if [ -n "$cfg_kvers" ]; then
539655
		check_kver "$kver" $cfg_kvers || {
539655
			debug "${cfg}: kernel version check for '$kver'" \
539655
			      "against '$cfg_kvers' failed"
539655
			exit 1
539655
		}
539655
	fi
539655
539655
	if [ -n "$cfg_blacklist" ]; then
539655
		echo "$cfg_blacklist" | /bin/grep -vqFx "${cpu_model_name}" || {
539655
			debug "${cfg}: model '${cpu_model_name}' is blacklisted"
539655
			exit 1
539655
		}
539655
	fi
539655
539655
	#printf "%s " "$cfg"
539655
	#[ 0 -eq "$match_model" ] || printf "%s " "$cpu_model"
539655
	echo $cfg_path
539655
done
539655
539655
exit 0