Blame SOURCES/check_caveats

b24a43
#! /bin/bash -eu
b24a43
b24a43
# Script for checking various microcode caveats
b24a43
#
b24a43
#
b24a43
# SPDX-License-Identifier: CC0-1.0
b24a43
b24a43
: ${MC_CAVEATS_DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats}
b24a43
: ${FW_DIR=/lib/firmware}
b24a43
: ${CFG_DIR=/etc/microcode_ctl/ucode_with_caveats}
b24a43
b24a43
usage() {
5ebb7f
	echo 'Usage: check_caveats [-d] [-e] [-k TARGET_KVER] [-c CONFIG]'
5ebb7f
	echo '                     [-m] [-v]'
b24a43
	echo
5ebb7f
	echo '   -d - enables disclaimer printing mode'
b24a43
	echo '   -e - check for early microcode load possibility (instead of'
b24a43
	echo '        late microcode load)'
b24a43
	echo '   -k - target version to check against, $(uname -r) is used'
b24a43
	echo '        otherwise'
b24a43
	echo '   -c - caveat config(s) to check, all configs are checked'
b24a43
	echo '        otherwise'
b24a43
	echo '   -m - check that caveats actually apply to the current model'
b24a43
	echo '   -v - verbose output'
b24a43
	echo
b24a43
	echo 'Environment:'
b24a43
	echo '  MC_CAVEATS_DATA_DIR - directory that contains caveats'
b24a43
	echo '                        configuration data'
b24a43
}
b24a43
b24a43
debug() { [ 0 = "$verbose" ] || echo "$*" >&2; }
b24a43
b24a43
# A simplified RPM version comparison that takes into account knowledge about
b24a43
# Y- and Z-streams (so it compares versions inside Y-stram or Z-stream if
b24a43
# the version against which comparison is performed has appropriate versioning
b24a43
# scheme).
b24a43
#
b24a43
# $1 - kernel version to check
b24a43
# $* - list of kernel versions to check against
b24a43
check_kver()
b24a43
{
b24a43
	local t_major= t_minor= t_patch= t_y= t_z1= t_z2= t_rest=
b24a43
	local m_major= m_minor= m_patch= m_y= m_z1= m_z2= m_rest=
b24a43
	local cmp_type=
b24a43
b24a43
	# IFS=.- read -r t_major t_minor t_patch t_y t_z1 t_z2 t_rest <<<"$1"
b24a43
	# "cannot create temp file for here-document: Read-only file system"
b24a43
	# that's why we can't have nice things.
b24a43
	t_major=${1%%.*}
b24a43
	t_rest=${1#${t_major}}
b24a43
	t_rest=${t_rest#.}
b24a43
	t_minor=${t_rest%%.*}
b24a43
	t_rest=${t_rest#${t_minor}}
b24a43
	t_rest=${t_rest#.}
b24a43
	t_patch=${t_rest%%-*}
b24a43
	t_rest=${t_rest#${t_patch}}
b24a43
	t_rest=${t_rest#-}
b24a43
	t_y=${t_rest%%.*}
b24a43
	t_rest=${t_rest#${t_y}}
b24a43
	t_rest=${t_rest#.}
b24a43
	t_z1=${t_rest%%.*}
b24a43
	t_rest=${t_rest#${t_z1}}
b24a43
	t_rest=${t_rest#.}
b24a43
	t_z2=${t_rest%%.*}
b24a43
b24a43
	# minor/major/patch/y should be numeric
b24a43
	[ -n "${t_major##*[!0-9]*}" ] || return 1
b24a43
	[ -n "${t_minor##*[!0-9]*}" ] || return 1
b24a43
	[ -n "${t_patch##*[!0-9]*}" ] || return 1
b24a43
	[ -n "${t_y##*[!0-9]*}" ] || return 1
b24a43
	# reset z1/z2 to zero if non-numeric
b24a43
	[ -n "${t_z1##*[!0-9]*}" ] || t_z1=0
b24a43
	[ -n "${t_z2##*[!0-9]*}" ] || t_z2=0
b24a43
b24a43
	while [ 1 -lt "$#" ]; do
b24a43
		cmp_type=upstream
b24a43
b24a43
		shift
b24a43
		m_major=${1%%.*}
b24a43
		m_rest=${1#${m_major}}
b24a43
		m_rest=${m_rest#.}
b24a43
		m_minor=${m_rest%%.*}
b24a43
		m_rest=${m_rest#${m_minor}}
b24a43
		m_rest=${m_rest#.}
b24a43
		m_patch=${m_rest%%-*}
b24a43
		m_rest=${m_rest#${m_patch}}
b24a43
		m_rest=${m_rest#-}
b24a43
		m_y=${m_rest%%.*}
b24a43
		m_rest=${m_rest#${m_y}}
b24a43
		m_rest=${m_rest#.}
b24a43
		m_z1=${m_rest%%.*}
b24a43
		m_rest=${m_rest#${m_z1}}
b24a43
		m_rest=${m_rest#.}
b24a43
		m_z2=${m_rest%%.*}
b24a43
b24a43
		# minor/major/patch should be numeric
b24a43
		[ -n "${m_major##*[!0-9]*}" ] || continue
b24a43
		[ -n "${m_minor##*[!0-9]*}" ] || continue
b24a43
		[ -n "${m_patch##*[!0-9]*}" ] || continue
b24a43
		# reset z1/z2 to zero if non-numeric
b24a43
		[ -n "${m_y##*[!0-9]*}" ] && cmp_type=y || m_y=0
b24a43
		[ -n "${m_z1##*[!0-9]*}" ] && cmp_type=z || m_z1=0
b24a43
		[ -n "${m_z2##*[!0-9]*}" ] && cmp_type=z || m_z2=0
b24a43
b24a43
		# Comparing versions
b24a43
		case "$cmp_type" in
b24a43
		upstream)
b24a43
			[ "$t_major" -ge "$m_major" ] || continue
b24a43
			[ "$t_minor" -ge "$m_minor" ] || continue
b24a43
			[ "$t_patch" -ge "$m_patch" ] || continue
b24a43
			return 0
b24a43
			;;
b24a43
		y)
b24a43
			[ "$t_major" -eq "$m_major" ] || continue
b24a43
			[ "$t_minor" -eq "$m_minor" ] || continue
b24a43
			[ "$t_patch" -eq "$m_patch" ] || continue
b24a43
			[ "$t_y" -ge "$m_y" ] || continue
b24a43
			return 0
b24a43
			;;
b24a43
		z)
b24a43
			[ "$t_major" -eq "$m_major" ] || continue
b24a43
			[ "$t_minor" -eq "$m_minor" ] || continue
b24a43
			[ "$t_patch" -eq "$m_patch" ] || continue
b24a43
			[ "$t_y" -eq "$m_y" ] || continue
b24a43
			[ "$t_z1" -ge "$m_z1" ] || continue
b24a43
			[ "$t_z2" -ge "$m_z2" ] || continue
b24a43
			return 0
b24a43
			;;
b24a43
		esac
b24a43
	done
b24a43
b24a43
	return 1
b24a43
}
b24a43
494736
# It is needed for SKX[1] for which different product segments
494736
# are differentiated by a value in the CAPID0 field of PCU registers
494736
# device[2].
494736
# [1] https://github.com/intel/Intel-Linux-Processor-Microcode-Data-Files/issues/21
494736
# [2] https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/xeon-scalable-spec-update.pdf#page=13
494736
#
a5370a
# $1 - params in config file, space-separated, in key=value form:
494736
#   domain=* - PCI domain, '*' or number
494736
#   bus=* - PCI bus, '*' or number
494736
#   device=* - PCI device, '*' or number
494736
#   function=* - PCI function, '*' or number
494736
#   vid= - PCI vendor ID, empty or number
494736
#   did= - PCI device ID, empty or number
494736
#   offset=0 - offset in configuration space
494736
#   size=4 - field size
494736
#   mask=0 - mask applied to the data read
494736
#   val=0 - comma-separated list of possible values
494736
#   mode=success-any [ success-ail, fail-any, fail-all ] - matching mode:
494736
#     success-any: Returns 0 if there was at least one match, otherwise 1.
494736
#     success-all: Returns 0 if there was at least one device checked and all
494736
#                  the checked devices have matches, otherwise 1.
494736
#     fail-any:    Returns 1 if there was at least one match, otherwise 0.
494736
#     fail-all:    Returns 1 if there was at least one device checked and all
494736
#                  the checked devices have matches, otherwise 0.
494736
# $2 - whether model filter is engaged (if it is not '1', just return the result
494736
#      based on "mode" value that assumes that there were 0 checks/0 matches).
494736
check_pci_config_val()
494736
{
494736
	local domain='*' bus='*' device='*' func='*' vid= did=
494736
	local offset=0 size=4 mask=0 val=0 mode=success-any
494736
	local checked=0 matched=0 path=''
494736
	local dev_path dev_vid dev_did dev_val
494736
	local opts="${1:-}"
494736
	local match_model="${2:0}"
494736
494736
	set -- $1
494736
	while [ "$#" -gt 0 ]; do
494736
		[ "x${1#domain=}" = "x${1}" ] || domain="${1#domain=}"
494736
		[ "x${1#bus=}" = "x${1}" ] || bus="${1#bus=}"
494736
		[ "x${1#device=}" = "x${1}" ] || device="${1#device=}"
494736
		[ "x${1#function=}" = "x${1}" ] || func="${1#function=}"
494736
		[ "x${1#vid=}" = "x${1}" ] || vid="${1#vid=}"
494736
		[ "x${1#did=}" = "x${1}" ] || did="${1#did=}"
494736
		[ "x${1#offset=}" = "x${1}" ] || offset="${1#offset=}"
494736
		[ "x${1#size=}" = "x${1}" ] || size="${1#size=}"
494736
		[ "x${1#mask=}" = "x${1}" ] || mask="${1#mask=}"
494736
		[ "x${1#val=}" = "x${1}" ] || val="${1#val=}"
494736
		[ "x${1#mode=}" = "x${1}" ] || mode="${1#mode=}"
494736
494736
		shift
494736
	done
494736
494736
	path="$domain"
494736
	if [ "x$bus" = 'x*' ]; then
494736
		path="$path:$bus";
494736
	else
494736
		path=$(printf '%s:%02x' "$path" "$bus")
494736
	fi
494736
	if [ "x$device" = 'x*' ]; then
494736
		path="$path:$device";
494736
	else
494736
		path=$(printf '%s:%02x' "$path" "$device")
494736
	fi
494736
	if [ "x$func" = 'x*' ]; then
494736
		path="$path.$func";
494736
	else
494736
		path=$(printf '%s.%01x' "$path" "$func")
494736
	fi
494736
494736
	# Normalise VID, DID
494736
	[ -n "$vid" ] || vid="$(printf '0x%04x' "$vid")"
494736
	[ -n "$did" ] || did="$(printf '0x%04x' "$did")"
494736
494736
	( [ 1 != "$match_model" ] \
494736
	  || /usr/bin/find /sys/bus/pci/devices/ -maxdepth 1 -name "$path" \
494736
	  || : ) | (
494736
		while read -r dev_path; do
494736
			# Filter VID, DID
494736
			if [ -n "$vid" ]; then
494736
				dev_vid=$(/bin/cat "$dev_path/vendor")
494736
				[ "x$vid" = "x$dev_vid" ] || continue
494736
			fi
494736
			if [ -n "$did" ]; then
494736
				dev_did=$(/bin/cat "$dev_path/device")
494736
				[ "x$did" = "x$dev_did" ] || continue
494736
			fi
494736
494736
			checked="$((checked + 1))"
494736
494736
			dev_val="$(/usr/bin/od -j "$offset" -N "$size" -A n \
494736
					       -t "u$size" "$dev_path/config")"
494736
494736
			val_rest="${val}"
494736
			while :; do
494736
				cur_val="${val_rest%%,*}"
494736
				if [ "$((dev_val & mask))" = "$((cur_val & mask))" ]
494736
				then
494736
					matched="$((matched + 1))"
494736
					break
494736
				fi
494736
				[ "x${val_rest}" != "x${val_rest#*,}" ] || break
494736
				val_rest="${val_rest#*,}"
494736
			done
494736
494736
			case "$mode" in
494736
			success-any) [ "$matched" -eq 0 ] || { echo 0; exit; } ;;
494736
			success-all) [ "$matched" -eq "$checked" ] || { echo 1; exit; } ;;
494736
			fail-any)    [ "$matched" -eq 0 ] || { echo 1; exit; } ;;
494736
			fail-all)    [ "$matched" -eq "$checked" ] || { echo 0; exit; } ;;
494736
			*)           echo 2; exit;;
494736
			esac
494736
		done
494736
494736
		debug "PCI config value check ($opts): checked $checked," \
494736
		      "matched $matched (model check is set to $match_model)"
494736
494736
		case "$mode" in
494736
		success-any) if [ "$matched" -eq 0 ]; then echo 1; else echo 0; fi ;;
494736
		success-all) if [ "$matched" -gt 0 -a "$matched" -eq "$checked" ]; then echo 0; else echo 1; fi ;;
494736
		fail-any)    if [ "$matched" -eq 0 ]; then echo 0; else echo 1; fi  ;;
494736
		fail-all)    if [ "$matched" -gt 0 -a "$matched" -eq "$checked" ]; then echo 1; else echo 0; fi ;;
494736
		*)           echo 2; exit;;
494736
		esac
494736
	)
494736
}
494736
a5370a
# It is needed for filtering by BIOS vendor name that is available in DMI data
a5370a
#
a5370a
# $1 - params in config file, space-separated, in key=value form:
a5370a
#   key= - DMI value to check. Can be one of the following: bios_date,
a5370a
#          bios_vendor, bios_version, board_asset_tag, board_name, board_serial,
a5370a
#          board_vendor, board_version, chassis_asset_tag, chassis_serial,
a5370a
#          chassis_type, chassis_vendor, chassis_version, product_family,
a5370a
#          product_name, product_serial, product_uuid, product_version,
a5370a
#          sys_vendor.
a5370a
#   val= - a string to match DMI data against.  Can be enclosed in single
a5370a
#          or double quotes.
a5370a
#   mode=success-equal [ success-equal, fail-equal ] - matching mode:
a5370a
#     success-equal: Returns 0 if the value present in the corresponding file
a5370a
#                    under /sys/devices/virtual/dmi/id/<key> is equal
a5370a
#                    to the value supplied as a value of "val" parameter,
a5370a
#                    otherwise 1.
a5370a
#     fail-equal:    Returns 1 if the value present in the corresponding file
a5370a
#                    under /sys/devices/virtual/dmi/id/<key> is equal
a5370a
#                    to the value supplied as a value of "val" parameter,
a5370a
#                    otherwise 0.
a5370a
#   no-model-mode=success [ success, fail ] - return value if model filter
a5370a
#                                             is not enabled:
a5370a
#     success: Return 0.
a5370a
#     fail:    Return 1.
a5370a
# $2 - whether model filter is engaged (if it is not '1', just return the result
a5370a
#      based on "mode" value that assumes that the check has failed).
a5370a
check_dmi_val()
a5370a
{
a5370a
	local key= val= mode='success-equal' nm_mode='success'
a5370a
	local opts="${1:-}" opt= opt_=
a5370a
	local match_model="${2:0}"
a5370a
a5370a
	local valid_keys=" bios_date bios_vendor bios_version board_asset_tag board_name board_serial board_vendor board_version chassis_asset_tag chassis_serial chassis_type chassis_vendor chassis_version product_family product_name product_serial product_uuid product_version sys_vendor "
a5370a
	local success=1
a5370a
a5370a
	while [ -n "$opts" ]; do
a5370a
		opt="${opts%%[ 	]*}"
a5370a
		[ -n "${opt}" ] || { opts="${opts#[ 	]}"; continue; }
a5370a
a5370a
		[ "x${opt#key=}" = "x${opt}" ] || key="${opt#key=}"
a5370a
		[ "x${opt#mode=}" = "x${opt}" ] || mode="${opt#mode=}"
a5370a
		[ "x${opt#no-model-mode=}" = "x${opt}" ] || \
a5370a
			nm_mode="${opt#no-model-mode=}"
a5370a
a5370a
		# Handle possible quoting
a5370a
		[ "x${opt#val=}" = "x${opt}" ] || {
a5370a
			case "${opt#val=}" in
a5370a
			[']*) opt_="${opts#val=\'}"; val="${opt_%%\'*}"; opt="val=\'${val}\'" ;;
a5370a
			["]*) opt_="${opts#val=\"}"; val="${opt_%%\"*}"; opt="val=\"${val}\"" ;;
a5370a
			*)    val="${opt#val=}" ;;
a5370a
			esac
a5370a
		}
a5370a
a5370a
		opts="${opts#"${opt}"}"
a5370a
		continue
a5370a
	done
a5370a
a5370a
	# Check key for validity
a5370a
	[ "x${valid_keys#* ${key} *}" != "x${valid_keys}" ] || {
a5370a
		debug "Invalid \"key\" parameter value: \"${key}\""
a5370a
		echo 2
a5370a
		exit
a5370a
	}
a5370a
a5370a
	[ 1 = "$match_model" ] || {
a5370a
		case "$nm_mode" in
a5370a
		success) echo 0 ;;
a5370a
		fail)    echo 1 ;;
a5370a
		*)
a5370a
			debug "Invalid no-model-mode value: \"${nm_mode}\""
a5370a
			echo 2
a5370a
			;;
a5370a
		esac
a5370a
a5370a
		exit
a5370a
	}
a5370a
a5370a
	[ -r "/sys/devices/virtual/dmi/id/${key}" ] || {
a5370a
		debug "Can't access /sys/devices/virtual/dmi/id/${key}"
a5370a
		echo 3
a5370a
		exit
a5370a
	}
a5370a
a5370a
	file_val="$(cat "/sys/devices/virtual/dmi/id/${key}")"
a5370a
a5370a
	[ "x${val}" = "x${file_val}" ] || success=0
a5370a
a5370a
	case "$mode" in
a5370a
	success-equal) echo "$((1 - $success))" ;;
a5370a
	fail-equal)    echo "${success}" ;;
a5370a
	*)             debug "Invalid mode value: \"${nm_mode}\""; echo 2 ;;
a5370a
	esac
a5370a
}
a5370a
b24a43
# Provides model in format "VENDOR_ID FAMILY-MODEL-STEPPING"
b24a43
#
b24a43
# We check only the first processor as we don't expect non-symmetrical setups
b24a43
# with CPUs with caveats
b24a43
get_model_string()
b24a43
{
b24a43
	/usr/bin/printf "%s %02x-%02x-%02x" \
b24a43
		$(/bin/sed -rn '1,/^$/{
b24a43
			s/^vendor_id[[:space:]]*: (.*)$/\1/p;
b24a43
			s/^cpu family[[:space:]]*: (.*)$/\1/p;
b24a43
			s/^model[[:space:]]*: (.*)$/\1/p;
b24a43
			s/^stepping[[:space:]]*: (.*)$/\1/p;
b24a43
		}' /proc/cpuinfo)
b24a43
}
b24a43
b24a43
get_model_name()
b24a43
{
b24a43
	/bin/sed -rn '1,/^$/s/^model name[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
b24a43
}
b24a43
b24a43
get_vendor_id()
b24a43
{
b24a43
	/bin/sed -rn '1,/^$/s/^vendor_id[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
b24a43
}
b24a43
b24a43
get_mc_path()
b24a43
{
b24a43
	case "$1" in
b24a43
	GenuineIntel)
b24a43
		echo "intel-ucode/$2"
b24a43
		;;
b24a43
	AuthenticAMD)
b24a43
		echo "amd-ucode/$2"
b24a43
		;;
b24a43
	esac
b24a43
}
b24a43
b24a43
get_mc_ver()
b24a43
{
b24a43
	/bin/sed -rn '1,/^$/s/^microcode[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
b24a43
}
b24a43
b24a43
fail()
b24a43
{
b24a43
	ret=1
b24a43
b24a43
	fail_cfgs="$fail_cfgs $cfg"
b24a43
	fail_paths="$fail_paths $cfg_path"
5ebb7f
5ebb7f
	[ 0 -eq "$print_disclaimers" ] || [ ! -e "${dir}/disclaimer" ] \
494736
		|| /bin/cat "${dir}/disclaimer"
b24a43
}
b24a43
b24a43
#check_kver "$@"
b24a43
#get_model_name
b24a43
b24a43
match_model=0
b24a43
configs=
b24a43
kver=$(/bin/uname -r)
b24a43
verbose=0
b24a43
early_check=0
5ebb7f
print_disclaimers=0
b24a43
b24a43
ret=0
b24a43
5ebb7f
while getopts "dek:c:mv" opt; do
b24a43
	case "${opt}" in
5ebb7f
	d)
5ebb7f
		print_disclaimers=1
5ebb7f
		early_check=2
5ebb7f
		;;
b24a43
	e)
b24a43
		early_check=1
b24a43
		;;
b24a43
	k)
b24a43
		kver="$OPTARG"
b24a43
		;;
b24a43
	c)
b24a43
		configs="$configs $OPTARG"
b24a43
		;;
b24a43
	m)
b24a43
		match_model=1
b24a43
		;;
b24a43
	v)
b24a43
		verbose=1
b24a43
		;;
b24a43
	*)
b24a43
		usage
b24a43
		exit 1;
b24a43
		;;
b24a43
	esac
b24a43
done
b24a43
494736
: "${configs:=$(find "${MC_CAVEATS_DATA_DIR}" -maxdepth 1 -mindepth 1 -type d -printf "%f\n")}"
b24a43
b24a43
cpu_model=$(get_model_string)
b24a43
cpu_model_name=$(get_model_name)
b24a43
cpu_vendor=$(get_vendor_id)
b24a43
b24a43
ret_paths=""
b24a43
ok_paths=""
b24a43
fail_paths=""
b24a43
b24a43
ret_cfgs=""
b24a43
ok_cfgs=""
b24a43
fail_cfgs=""
b24a43
b24a43
skip_cfgs=""
b24a43
b24a43
if [ 1 -eq "$early_check" ]; then
b24a43
	stage="early"
b24a43
else
b24a43
	stage="late"
b24a43
fi
b24a43
b24a43
b24a43
for cfg in $(echo "${configs}"); do
b24a43
	dir="$MC_CAVEATS_DATA_DIR/$cfg"
b24a43
b24a43
	# We add cfg to the skip list first and then, if we do not skip it,
b24a43
	# we remove the configuration from the list.
b24a43
	skip_cfgs="$skip_cfgs $cfg"
b24a43
b24a43
	[ -r "${dir}/readme" ] || {
b24a43
		debug "File 'readme' in ${dir} is not found, skipping"
b24a43
		continue
b24a43
	}
b24a43
b24a43
	[ -r "${dir}/config" ] || {
b24a43
		debug "File 'config' in ${dir} is not found, skipping"
b24a43
		continue
b24a43
	}
b24a43
b24a43
	cfg_model=
b24a43
	cfg_vendor=
b24a43
	cfg_path=
b24a43
	cfg_kvers=
b24a43
	cfg_kvers_early=
b24a43
	cfg_blacklist=
b24a43
	cfg_mc_min_ver_late=
b24a43
	cfg_disable=
494736
	cfg_pci=
a5370a
	cfg_dmi=
b24a43
b24a43
	while read -r key value; do
b24a43
		case "$key" in
b24a43
		model)
b24a43
			cfg_model="$value"
b24a43
			;;
b24a43
		vendor)
b24a43
			cfg_vendor="$value"
b24a43
			;;
b24a43
		path)
b24a43
			cfg_path="$cfg_path $value"
b24a43
			;;
b24a43
		kernel)
b24a43
			cfg_kvers="$cfg_kvers $value"
b24a43
			;;
b24a43
		kernel_early)
b24a43
			cfg_kvers_early="$cfg_kvers_early $value"
b24a43
			;;
b24a43
		mc_min_ver_late)
b24a43
			cfg_mc_min_ver_late="$value"
b24a43
			;;
b24a43
		disable)
b24a43
			cfg_disable="$cfg_disable $value "
b24a43
			;;
b24a43
		blacklist)
b24a43
			cfg_blacklist=1
a5370a
			# "blacklist" is special: it stops entity parsing,
a5370a
			# and the rest of file is a list of blacklisted model
a5370a
			# names.
a5370a
			break
494736
			;;
494736
		pci_config_val)
494736
			cfg_pci="$cfg_pci
494736
				$value"
494736
			;;
a5370a
		dmi)
a5370a
			cfg_dmi="$cfg_dmi
a5370a
				$value"
a5370a
			;;
494736
		'#'*|'')
494736
			continue
494736
			;;
494736
		*)
494736
			debug "Unknown key '$key' (value '$value') in config" \
494736
			      "'$cfg'"
b24a43
			;;
b24a43
		esac
b24a43
	done < "${dir}/config"
b24a43
b24a43
	[ -z "${cfg_blacklist}" ] || \
b24a43
		cfg_blacklist=$(/bin/sed -n '/^blacklist$/,$p' "${dir}/config" |
b24a43
					/usr/bin/tail -n +2)
b24a43
b24a43
	debug "${cfg}: model '$cfg_model', path '$cfg_path', kvers '$cfg_kvers'"
b24a43
	debug "${cfg}: blacklist '$cfg_blacklist'"
b24a43
b24a43
	# Check for override files in the following order:
b24a43
	#  - disallow early/late specific caveat for specific kernel
b24a43
	#  - force early/late specific caveat for specific kernel
b24a43
	#  - disallow specific caveat for specific kernel
b24a43
	#  - force specific caveat for specific kernel
b24a43
	#
b24a43
	#  - disallow early/late specific caveat for any kernel
b24a43
	#  - disallow early/late any caveat for specific kernel
b24a43
	#  - force early/late specific caveat for any kernel
b24a43
	#  - force early/late any caveat for specific kernel
b24a43
	#  - disallow specific caveat for any kernel
b24a43
	#  - disallow any caveat for specific kernel
b24a43
	#  - force specific caveat for any kernel
b24a43
	#  - force any caveat for specific kernel
b24a43
	#
b24a43
	#  - disallow early/late everything
b24a43
	#  - force early/late everyhting
b24a43
	#  - disallow everything
b24a43
	#  - force everyhting
b24a43
	ignore_cfg=0
b24a43
	force_cfg=0
b24a43
	override_file=""
b24a43
	overrides="
b24a43
	0:$FW_DIR/$kver/disallow-$stage-$cfg
b24a43
	1:$FW_DIR/$kver/force-$stage-$cfg
b24a43
	0:$FW_DIR/$kver/disallow-$cfg
b24a43
	1:$FW_DIR/$kver/force-$cfg
b24a43
	0:$FW_DIR/$kver/disallow-$stage
b24a43
	0:$CFG_DIR/disallow-$stage-$cfg
b24a43
	1:$FW_DIR/$kver/force-$stage
b24a43
	1:$CFG_DIR/force-$stage-$cfg
b24a43
	0:$FW_DIR/$kver/disallow
b24a43
	0:$CFG_DIR/disallow-$cfg
b24a43
	1:$FW_DIR/$kver/force
b24a43
	1:$CFG_DIR/force-$cfg
b24a43
	0:$CFG_DIR/disallow-$stage
b24a43
	1:$CFG_DIR/force-$stage
b24a43
	0:$CFG_DIR/disallow
b24a43
	1:$CFG_DIR/force"
b24a43
	for o in $(echo "$overrides"); do
b24a43
		o_force=${o%%:*}
b24a43
		override_file=${o#$o_force:}
b24a43
b24a43
		[ -e "$override_file" ] || continue
b24a43
b24a43
		if [ 0 -eq "$o_force" ]; then
b24a43
			ignore_cfg=1
b24a43
		else
b24a43
			force_cfg=1
b24a43
		fi
b24a43
b24a43
		break
b24a43
	done
b24a43
b24a43
	[ 0 -eq "$ignore_cfg" ] || {
b24a43
		debug "Configuration \"$cfg\" is ignored due to presence of" \
b24a43
		      "\"$override_file\"."
b24a43
		continue
b24a43
	}
b24a43
b24a43
	# Check model if model filter is enabled
b24a43
	if [ 1 -eq "$match_model" -a  -n "$cfg_model" ]; then
b24a43
		[ "x$cpu_model" = "x$cfg_model" ] || {
b24a43
			debug "Current CPU model '$cpu_model' doesn't" \
b24a43
			      "match configuration CPU model '$cfg_model'," \
b24a43
			      "skipping"
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check paths if model filter is enabled
b24a43
	if [ 1 -eq "$match_model" -a  -n "$cfg_path" ]; then
b24a43
		cpu_mc_path="$MC_CAVEATS_DATA_DIR/$cfg/$(get_mc_path \
b24a43
			"$cpu_vendor" "${cpu_model#* }")"
b24a43
		cfg_mc_present=0
b24a43
b24a43
		for p in $(printf "%s" "$cfg_path"); do
494736
			{ /usr/bin/find "$MC_CAVEATS_DATA_DIR/$cfg" \
494736
				-path "$MC_CAVEATS_DATA_DIR/$cfg/$p" -print0;
494736
			  /bin/true; } \
494736
			    | /bin/grep -zFxq "$cpu_mc_path" \
b24a43
			    || continue
b24a43
b24a43
			cfg_mc_present=1
494736
			break
b24a43
		done
b24a43
b24a43
		[ 1 = "$cfg_mc_present" ] || {
b24a43
			debug "No matching microcode files in '$cfg_path'" \
b24a43
			      "for CPU model '$cpu_model', skipping"
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check vendor if model filter is enabled
b24a43
	if [ 1 -eq "$match_model" -a  -n "$cfg_vendor" ]; then
b24a43
		[ "x$cpu_vendor" = "x$cfg_vendor" ] || {
b24a43
			debug "Current CPU vendor '$cpu_vendor' doesn't" \
b24a43
			      "match configuration CPU vendor '$cfg_vendor'," \
b24a43
			      "skipping"
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check configuration files
b24a43
b24a43
	ret_cfgs="$ret_cfgs $cfg"
b24a43
	ret_paths="$ret_paths $cfg_path"
b24a43
	skip_cfgs="${skip_cfgs% $cfg}"
b24a43
b24a43
	[ 0 -eq "$force_cfg" ] || {
b24a43
		debug "Checks for configuration \"$cfg\" are ignored due to" \
b24a43
		      "presence of \"$override_file\"."
b24a43
b24a43
		ok_cfgs="$ok_cfgs $cfg"
b24a43
		ok_paths="$ok_paths $cfg_path"
b24a43
b24a43
		continue
b24a43
	}
b24a43
b24a43
	[ "x${cfg_disable%%* $stage *}" = "x$cfg_disable" ] || {
b24a43
		debug "${cfg}: caveat is disabled in configuration"
b24a43
		fail
b24a43
		continue
b24a43
	}
b24a43
b24a43
	# Check late load kernel version
b24a43
	if [ 1 -ne "$early_check" -a -n "$cfg_kvers" ]; then
b24a43
		check_kver "$kver" $cfg_kvers || {
b24a43
			debug "${cfg}: late load kernel version check for" \
b24a43
			      " '$kver' against '$cfg_kvers' failed"
b24a43
			fail
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check early load kernel version
b24a43
	if [ 0 -ne "$early_check" -a -n "$cfg_kvers_early" ]; then
b24a43
		check_kver "$kver" $cfg_kvers_early || {
b24a43
			debug "${cfg}: early load kernel version check for" \
b24a43
			      "'$kver' against '$cfg_kvers_early' failed"
b24a43
			fail
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check model blacklist
b24a43
	if [ -n "$cfg_blacklist" ]; then
b24a43
		echo "$cfg_blacklist" | /bin/grep -vqFx "${cpu_model_name}" || {
b24a43
			debug "${cfg}: model '${cpu_model_name}' is blacklisted"
b24a43
			fail
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check current microcode version for the late update
b24a43
	if [ -n "$cfg_mc_min_ver_late" -a 1 -ne "$early_check" -a \
b24a43
	   "x$cpu_model" = "x$cfg_model" ]; then
b24a43
		cpu_mc_ver="$(get_mc_ver)"
b24a43
b24a43
		[ 1 -eq $((cpu_mc_ver >= cfg_mc_min_ver_late)) ] || {
b24a43
			debug "${cfg}: CPU microcode version $cpu_mc_ver" \
b24a43
			      "failed check (should be at least" \
b24a43
			      "${cfg_mc_min_ver_late})"
b24a43
			fail
b24a43
			continue
b24a43
		}
b24a43
	fi
b24a43
494736
	# Check PCI devices if model filter is enabled
494736
	# Note that the model filter check is done inside check_pci_config_val
494736
	# based on the 'mode=' parameter.
494736
	if [ -n "$cfg_pci" ]; then
494736
		pci_line="$(printf "%s\n" "$cfg_pci" | while read -r pci_line; do
494736
				[ -n "$pci_line" ] || continue
494736
				pci_res=$(check_pci_config_val "$pci_line" \
494736
							       "$match_model")
494736
				[ 0 != "$pci_res" ] || continue
494736
				echo "$pci_res $pci_line"
494736
				break
494736
			done
494736
			echo "0 ")"
494736
494736
		[ -z "${pci_line#* }" ] || {
494736
			debug "PCI configuration word check '${pci_line#* }'" \
494736
			      "failed (with return code ${pci_line%% *})"
494736
			fail
494736
			continue
494736
		}
494736
	fi
494736
a5370a
	# Check DMI data if model filter is enabled
a5370a
	# Note that the model filter check is done inside check_pci_config_val
a5370a
	# based on the 'mode=' parameter.
a5370a
	if [ -n "$cfg_dmi" ]; then
a5370a
		dmi_line="$(printf "%s\n" "$cfg_dmi" | while read -r dmi_line
a5370a
			do
a5370a
				[ -n "$dmi_line" ] || continue
a5370a
				dmi_res=$(check_dmi_val "$dmi_line" \
a5370a
							"$match_model")
a5370a
				[ 0 != "$dmi_res" ] || continue
a5370a
				echo "$dmi_res $dmi_line"
a5370a
				break
a5370a
			done
a5370a
			echo "0 ")"
a5370a
a5370a
		[ -z "${dmi_line#* }" ] || {
a5370a
			debug "DMI data check '${dmi_line#* }'" \
a5370a
			      "failed (with return code ${dmi_line%% *})"
a5370a
			fail
a5370a
			continue
a5370a
		}
a5370a
	fi
a5370a
b24a43
	ok_cfgs="$ok_cfgs $cfg"
b24a43
	ok_paths="$ok_paths $cfg_path"
b24a43
done
b24a43
5ebb7f
[ 0 -eq "$print_disclaimers" ] || exit 0
5ebb7f
b24a43
echo "cfgs$ret_cfgs"
b24a43
echo "skip_cfgs$skip_cfgs"
b24a43
echo "paths$ret_paths"
b24a43
echo "ok_cfgs$ok_cfgs"
b24a43
echo "ok_paths$ok_paths"
b24a43
echo "fail_cfgs$fail_cfgs"
b24a43
echo "fail_paths$fail_paths"
b24a43
b24a43
exit $ret