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
b9f9de
MAX_NESTING_LEVEL=8
b9f9de
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:-}"
2c8f3d
	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:
b9f9de
#   key= - DMI data record 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.
b9f9de
#   keyval= - a string of format "KEY(!)?[=:]VAL" (so, one of "KEY=VAL",
b9f9de
#             "KEY!=VAL", "KEY:VAL", "KEY!:VAL") that allows providing
b9f9de
#             a key-value pair in a single parameter.  It is possible to provide
b9f9de
#             multiple keyval= parameters.  "!" before :/= means negated match.
b9f9de
#             The action supplied in the mode= parameter is executed upon
b9f9de
#             successful (non-)matching of all the keyval pairs (as well
b9f9de
#             as the pair provided in a pair of key= and val= parameters).
a5370a
#   mode=success-equal [ success-equal, fail-equal ] - matching mode:
b9f9de
#     success-equal: Returns 0 if the all values present in the corresponding
b9f9de
#                    files under /sys/devices/virtual/dmi/id/<KEY> are equal
b9f9de
#                    (or not equal in case of a keyval= with negated match)
b9f9de
#                    to the respective values supplied as the values
b9f9de
#                    of the keyval= parameters or the pair of key= vand val=
b9f9de
#                    parameters, otherwise 1.
b9f9de
#     fail-equal:    Returns 1 if all the values present in DMI files in sysfs
b9f9de
#                    match (as described above), 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
b9f9de
#      based on "no-model-mode" value).
a5370a
check_dmi_val()
a5370a
{
b9f9de
	local key= val= keyval= keyvals= mode='success-equal' nm_mode='success'
a5370a
	local opts="${1:-}" opt= opt_=
2c8f3d
	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
b9f9de
			[\']*) opt_="${opts#val=\'}"; val="${opt_%%\'*}"; opt="val='${val}'" ;;
b9f9de
			[\"]*) opt_="${opts#val=\"}"; val="${opt_%%\"*}"; opt="val=\"${val}\"" ;;
a5370a
			*)    val="${opt#val=}" ;;
a5370a
			esac
a5370a
		}
b9f9de
		[ "x${opt#keyval=}" = "x${opt}" ] || {
b9f9de
			case "${opt#keyval=}" in
b9f9de
			[\']*)
b9f9de
				opt_="${opts#keyval=\'}"
b9f9de
				keyval="${opt_%%\'*}"
b9f9de
				opt="keyval='${keyval}'"
b9f9de
				keyvals="${keyvals}
b9f9de
					${keyval}"
b9f9de
				;;
b9f9de
			[\"]*)
b9f9de
				opt_="${opts#keyval=\"}"
b9f9de
				keyval="${opt_%%\"*}"
b9f9de
				opt="keyval=\"${keyval}\""
b9f9de
				keyvals="${keyvals}
b9f9de
					${keyval}"
b9f9de
				;;
b9f9de
			*)
b9f9de
				keyvals="${keyvals}
b9f9de
					${opt#keyval=}"
b9f9de
				;;
b9f9de
			esac
b9f9de
		}
a5370a
a5370a
		opts="${opts#"${opt}"}"
a5370a
		continue
a5370a
	done
a5370a
b9f9de
	[ -z "$key" -a -z "$val" ] || keyvals="${key}=${val}${keyvals}"
b9f9de
b9f9de
	[ -n "x${keyvals}" ] || {
b9f9de
		debug "Neither key=, val=, nor keyval= parameters were privoded"
a5370a
		echo 2
b9f9de
		return
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
b9f9de
		return
a5370a
	}
a5370a
b9f9de
	case "$mode" in
b9f9de
	success-equal|fail-equal) ;;
b9f9de
	*) debug "Invalid mode value: \"${nm_mode}\""; echo 2; return ;;
b9f9de
	esac
a5370a
b9f9de
	printf "%s\n" "${keyvals}" | (
b9f9de
		while read l; do
b9f9de
			[ -n "$l" ] || continue
b9f9de
			key="${l%%[=:]*}"
b9f9de
			val="${l#${key}[=:]}"
b9f9de
b9f9de
			cmp="="
b9f9de
			[ "x${key%!}" = "x${key}" ] || {
b9f9de
				cmp="!="
b9f9de
				key="${key%!}"
b9f9de
			}
b9f9de
b9f9de
			# Check key for validity
b9f9de
			[ "x${valid_keys#* ${key} *}" != "x${valid_keys}" ] || {
b9f9de
				debug "Invalid \"key\" parameter value: \"${key}\""
b9f9de
				echo 2
b9f9de
				return
b9f9de
			}
b9f9de
b9f9de
			[ -r "/sys/devices/virtual/dmi/id/${key}" ] || {
b9f9de
				debug "Can't access /sys/devices/virtual/dmi/id/${key}"
b9f9de
				echo 3
b9f9de
				return
b9f9de
			}
b9f9de
b9f9de
			file_val="$(/bin/cat "/sys/devices/virtual/dmi/id/${key}")"
b9f9de
b9f9de
			[ "x${val}" "${cmp}" "x${file_val}" ] || {
b9f9de
				case "$mode" in
b9f9de
				success-equal) echo 1 ;;
b9f9de
				fail-equal)    echo 0 ;;
b9f9de
				esac
b9f9de
b9f9de
				return
b9f9de
			}
b9f9de
		done
a5370a
b9f9de
		case "$mode" in
b9f9de
		success-equal) echo 0 ;;
b9f9de
		fail-equal)    echo 1 ;;
b9f9de
		esac
b9f9de
	)
b9f9de
}
a5370a
b9f9de
# check_dependency CURLEVEL DEP_TYPE DEP_NAME OPTS
b9f9de
# DEP_TYPE:
b9f9de
#   required - caveat can be enabled only if dependency is enabled
b9f9de
#              (is not forcefully disabled and meets caveat conditions)
b9f9de
# OPTS:
b9f9de
#   match-model-mode=same [ on, off, same ] - what mode matching mode is to be used for dependency
b9f9de
#   skip=skip [ fail, skip, success ]
b9f9de
#   force-skip=skip [ fail, skip, success ]
b9f9de
#   nesting-too-deep=fail [ fail, skip, success ]
b9f9de
# Return values:
b9f9de
#   0 - success
b9f9de
#   1 - fail
b9f9de
#   2 - skip
b9f9de
#   9 - error
b9f9de
check_dependency()
b9f9de
{
b9f9de
	local cur_level="$1"
b9f9de
	local dep_type="$2"
b9f9de
	local dep_name="$3"
b9f9de
	local match_model_mode=same old_match_model="${match_model}"
b9f9de
	local skip=skip
b9f9de
	local force_skip=skip
b9f9de
	local nesting_too_deep=fail
b9f9de
b9f9de
	local check="Dependency check for ${dep_type} ${dep_name}"
b9f9de
b9f9de
	set -- ${4:-}
b9f9de
	while [ "$#" -gt 0 ]; do
b9f9de
		[ "x${1#match-model-mode=}" = "x${1}" ] || match_model_mode="${1#match-model-mode=}"
b9f9de
		[ "x${1#skip=}" = "x${1}" ] || skip="${1#skip=}"
b9f9de
		[ "x${1#force-skip=}" = "x${1}" ] || force_skip="${1#force-skip=}"
b9f9de
		[ "x${1#nesting-too-deep=}" = "x${1}" ] || nesting_too_deep="${1#nesting-too-deep=}"
b9f9de
b9f9de
		shift
b9f9de
	done
b9f9de
b9f9de
	case "${dep_type}" in
b9f9de
	required)
b9f9de
		[ "x${dep_name%/*}" = "x${dep_name}" ] || {
b9f9de
			debug "${check} error: dependency name (${dep_name})" \
b9f9de
			      "cannot contain slashes"
b9f9de
			echo 9
b9f9de
			return
b9f9de
		}
b9f9de
b9f9de
		[ "${MAX_NESTING_LEVEL}" -ge "$cur_level" ] || {
b9f9de
			local reason="nesting level is too deep (${cur_level}) and nesting-too-deep='${nesting_too_deep}'"
b9f9de
b9f9de
			case "$nesting_too_deep" in
b9f9de
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
b9f9de
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
b9f9de
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
b9f9de
			*)       debug "${check} error: invalid" \
b9f9de
				       "nesting-too-deep mode" \
b9f9de
				       "(${nesting_too_deep})"; echo 9 ;;
b9f9de
			esac
b9f9de
b9f9de
			return
b9f9de
		}
b9f9de
b9f9de
		case "${match_model_mode}" in
b9f9de
		same) ;;
b9f9de
		on)   match_model=1 ;;
b9f9de
		off)  match_model=0 ;;
b9f9de
		*)
b9f9de
			debug "${check} error: invalid match-model-mode" \
b9f9de
			      "(${match_model_mode})"
b9f9de
			echo 9
b9f9de
			return
b9f9de
			;;
b9f9de
		esac
b9f9de
b9f9de
		local result=0
b9f9de
		debug "${check}: calling check_caveat '${dep_name}'" \
b9f9de
		      "'$(($cur_level + 1))' match_model=${match_model}"
b9f9de
		check_caveat "${dep_name}" "$(($cur_level + 1))" > /dev/null || result="$?"
b9f9de
b9f9de
		match_model="${old_match_model}"
b9f9de
b9f9de
		case "${result}" in
b9f9de
		0) debug "${check} succeeded: result=${result}"; echo "${result}" ;;
b9f9de
		1) debug "${check} failed: result=${result}"; echo "${result}" ;;
b9f9de
		2)
b9f9de
			local reason="result=${result} and skip='${skip}'"
b9f9de
b9f9de
			case "${skip}" in
b9f9de
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
b9f9de
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
b9f9de
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
b9f9de
			*)       debug "${check} error: unexpected skip=" \
b9f9de
				       "setting (${skip})"; echo 9 ;;
b9f9de
			esac
b9f9de
			;;
b9f9de
		3)
b9f9de
			local reason="result=${result} and force_skip='${force_skip}'"
b9f9de
b9f9de
			case "${force_skip}" in
b9f9de
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
b9f9de
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
b9f9de
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
b9f9de
			*)       debug "${check} error: unexpected force-skip=" \
b9f9de
				       "setting (${skip})"; echo 9 ;;
b9f9de
			esac
b9f9de
			;;
b9f9de
		*)
b9f9de
			debug "${check} error: unexpected check_caveat result" \
b9f9de
			      "(${result})"; echo 9 ;;
b9f9de
		esac
b9f9de
		;;
b9f9de
	*)
b9f9de
		debug "${check} error: unknown dependency type '${dep_type}'"
b9f9de
		echo 9
b9f9de
		;;
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
		;;
2c8f3d
	*)
2c8f3d
		# We actually only support Intel ucode, but things may break
2c8f3d
		# if nothing is printed (input would be gotten from stdin
2c8f3d
		# otherwise).
2c8f3d
		echo "invalid"
2c8f3d
		;;
b24a43
	esac
b24a43
}
b24a43
b24a43
get_mc_ver()
b24a43
{
b24a43
	/bin/sed -rn '1,/^$/s/^microcode[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
b24a43
}
b24a43
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
b9f9de
# check_caveat CFG [CHECK_LEVEL]
2c8f3d
# changes ret_paths, ok_paths, fail_paths, ret_cfgs, ok_cfgs, fail_cfgs,
b9f9de
# skip_cfgs if CHECK_LEVEL is set to 0 (default).
b9f9de
# CHECK_LEVEL is used for recursive configuration dependency checks,
b9f9de
# and indicates nesting level.
2c8f3d
# Return value:
2c8f3d
#  0 - check is successful
2c8f3d
#  1 - check has been failed
2c8f3d
#  2 - configuration has been skipped
b9f9de
#  3 - configuration has been skipped due to presence of an override file
2c8f3d
check_caveat() {
2c8f3d
	local cfg="$1"
b9f9de
	local check_level="${2:-0}"
2c8f3d
	local dir="$MC_CAVEATS_DATA_DIR/$cfg"
b24a43
b24a43
	[ -r "${dir}/readme" ] || {
b24a43
		debug "File 'readme' in ${dir} is not found, skipping"
2c8f3d
		return 2
b24a43
	}
b24a43
b24a43
	[ -r "${dir}/config" ] || {
b24a43
		debug "File 'config' in ${dir} is not found, skipping"
2c8f3d
		return 2
b24a43
	}
b24a43
2c8f3d
	local cfg_model=
2c8f3d
	local cfg_vendor=
2c8f3d
	local cfg_path=
2c8f3d
	local cfg_kvers=
2c8f3d
	local cfg_kvers_early=
2c8f3d
	local cfg_mc_min_ver_late=
2c8f3d
	local cfg_disable=
2c8f3d
	local cfg_pci=
2c8f3d
	local cfg_dmi=
b9f9de
	local cfg_dependency=
2c8f3d
2c8f3d
	local key
2c8f3d
	local value
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
			;;
494736
		pci_config_val)
494736
			cfg_pci="$cfg_pci
494736
				$value"
494736
			;;
a5370a
		dmi)
a5370a
			cfg_dmi="$cfg_dmi
a5370a
				$value"
a5370a
			;;
b9f9de
		dependency)
b9f9de
			cfg_dependency="$cfg_dependency
b9f9de
				$value"
b9f9de
			;;
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
	debug "${cfg}: model '$cfg_model', path '$cfg_path', kvers '$cfg_kvers'"
b9f9de
	echo "$cfg_path"
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
2c8f3d
	local ignore_cfg=0
2c8f3d
	local force_cfg=0
2c8f3d
	local override_file=""
2c8f3d
	local 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"
2c8f3d
	local o
2c8f3d
	local o_force
2c8f3d
	local override_file
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\"."
b9f9de
		return 3
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"
2c8f3d
			return 2
b24a43
		}
b24a43
	fi
b24a43
b24a43
	# Check paths if model filter is enabled
2c8f3d
	local cpu_mc_path
2c8f3d
	local cfg_mc_present
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
c0f195
			/usr/bin/find "$MC_CAVEATS_DATA_DIR/$cfg" \
c0f195
				-path "$MC_CAVEATS_DATA_DIR/$cfg/$p" -print0 \
c0f195
			    | /bin/grep -zFxc "$cpu_mc_path" > /dev/null \
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"
2c8f3d
			return 2
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"
2c8f3d
			return 2
b24a43
		}
b24a43
	fi
b24a43
b9f9de
	# Has to be performed before dependency checks
b24a43
	[ 0 -eq "$force_cfg" ] || {
b24a43
		debug "Checks for configuration \"$cfg\" are ignored due to" \
b24a43
		      "presence of \"$override_file\"."
b24a43
2c8f3d
		return 0
b24a43
	}
b24a43
b9f9de
	# Check dependencies
b9f9de
	# It has to be performed here (before adding configuration
b9f9de
	# to $ret_cfgs/$ret_paths) since it may be skipped.
b9f9de
	if [ -n "$cfg_dependency" ]; then
b9f9de
		dep_line="$(printf "%s\n" "$cfg_dependency" | \
b9f9de
			while read -r dep_type dep_name dep_opts
b9f9de
			do
b9f9de
				[ -n "$dep_type" ] || continue
b9f9de
				dep_res=$(check_dependency "$check_level" \
b9f9de
							   "$dep_type" \
b9f9de
							   "$dep_name" \
b9f9de
							   "$dep_opts")
b9f9de
				[ 0 != "$dep_res" ] || continue
b9f9de
				echo "$dep_res $dep_type $dep_name $dep_opts"
b9f9de
				break
b9f9de
			done
b9f9de
			echo "0 ")"
b9f9de
b9f9de
		case "${dep_line%% *}" in
b9f9de
		0) ;;
b9f9de
		2)
b9f9de
			debug "Dependency check '${dep_line#* }'" \
b9f9de
			      "induced configuration skip"
b9f9de
			return 2
b9f9de
			;;
b9f9de
		*)
b9f9de
			debug "Dependency check '${dep_line#* }'" \
b9f9de
			      "failed (with return code ${dep_line%% *})"
b9f9de
			return 1
b9f9de
			;;
b9f9de
		esac
b9f9de
	fi
b9f9de
b9f9de
	# Check configuration files
b9f9de
b24a43
	[ "x${cfg_disable%%* $stage *}" = "x$cfg_disable" ] || {
b24a43
		debug "${cfg}: caveat is disabled in configuration"
2c8f3d
		return 1
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"
2c8f3d
			return 1
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"
2c8f3d
			return 1
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})"
2c8f3d
			return 1
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%% *})"
2c8f3d
			return 1
494736
		}
494736
	fi
494736
a5370a
	# Check DMI data if model filter is enabled
b9f9de
	# Note that the model filter check is done inside check_dmi_val
b9f9de
	# (which returns the value of 'no-model-mode=' parameter
b9f9de
	# if it is disenaged).
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%% *})"
2c8f3d
			return 1
a5370a
		}
a5370a
	fi
a5370a
2c8f3d
	return 0
2c8f3d
}
2c8f3d
2c8f3d
for cfg in $(echo "${configs}"); do
b9f9de
	if cfg_path=$(check_caveat "$cfg"; exit "$?")
b9f9de
	then
b9f9de
		ret_cfgs="$ret_cfgs $cfg"
b9f9de
		ret_paths="$ret_paths $cfg_path"
b9f9de
		ok_cfgs="$ok_cfgs $cfg"
b9f9de
		ok_paths="$ok_paths $cfg_path"
b9f9de
	else
b9f9de
		case "$?" in
b9f9de
		1)
b9f9de
			ret=1
b9f9de
b9f9de
			ret_cfgs="$ret_cfgs $cfg"
b9f9de
			ret_paths="$ret_paths $cfg_path"
b9f9de
			fail_cfgs="$fail_cfgs $cfg"
b9f9de
			fail_paths="$fail_paths $cfg_path"
b9f9de
b9f9de
			[ 0 -eq "$print_disclaimers" ] \
b9f9de
				|| [ ! -e "${MC_CAVEATS_DATA_DIR}/${cfg}/disclaimer" ] \
b9f9de
				|| /bin/cat "${MC_CAVEATS_DATA_DIR}/${cfg}/disclaimer"
b9f9de
			;;
b9f9de
		2|3)
b9f9de
			skip_cfgs="$skip_cfgs $cfg";
b9f9de
			;;
b9f9de
		*)
b9f9de
			debug "Unexpected check_caveat return code '$?'" \
b9f9de
			      "for config '$cfg'"
b9f9de
			;;
b9f9de
		esac
b9f9de
	fi
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