Blame SOURCES/check_caveats

7826d1
#! /bin/bash -eu
7826d1
7826d1
# Script for checking various microcode caveats
7826d1
#
7826d1
#
7826d1
# SPDX-License-Identifier: CC0-1.0
7826d1
7826d1
: ${MC_CAVEATS_DATA_DIR=/usr/share/microcode_ctl/ucode_with_caveats}
7826d1
: ${FW_DIR=/lib/firmware}
7826d1
: ${CFG_DIR=/etc/microcode_ctl/ucode_with_caveats}
7826d1
7826d1
MAX_NESTING_LEVEL=8
7826d1
7826d1
usage() {
7826d1
	echo 'Usage: check_caveats [-d] [-e] [-k TARGET_KVER] [-c CONFIG]'
7826d1
	echo '                     [-m] [-v]'
7826d1
	echo
7826d1
	echo '   -d - enables disclaimer printing mode'
7826d1
	echo '   -e - check for early microcode load possibility (instead of'
7826d1
	echo '        late microcode load)'
7826d1
	echo '   -k - target version to check against, $(uname -r) is used'
7826d1
	echo '        otherwise'
7826d1
	echo '   -c - caveat config(s) to check, all configs are checked'
7826d1
	echo '        otherwise'
7826d1
	echo '   -m - check that caveats actually apply to the current model'
7826d1
	echo '   -v - verbose output'
7826d1
	echo
7826d1
	echo 'Environment:'
7826d1
	echo '  MC_CAVEATS_DATA_DIR - directory that contains caveats'
7826d1
	echo '                        configuration data'
7826d1
}
7826d1
7826d1
debug() { [ 0 = "$verbose" ] || echo "$*" >&2; }
7826d1
7826d1
# A simplified RPM version comparison that takes into account knowledge about
7826d1
# Y- and Z-streams (so it compares versions inside Y-stram or Z-stream if
7826d1
# the version against which comparison is performed has appropriate versioning
7826d1
# scheme).
7826d1
#
7826d1
# $1 - kernel version to check
7826d1
# $* - list of kernel versions to check against
7826d1
check_kver()
7826d1
{
7826d1
	local t_major= t_minor= t_patch= t_y= t_z1= t_z2= t_rest=
7826d1
	local m_major= m_minor= m_patch= m_y= m_z1= m_z2= m_rest=
7826d1
	local cmp_type=
7826d1
7826d1
	# IFS=.- read -r t_major t_minor t_patch t_y t_z1 t_z2 t_rest <<<"$1"
7826d1
	# "cannot create temp file for here-document: Read-only file system"
7826d1
	# that's why we can't have nice things.
7826d1
	t_major=${1%%.*}
7826d1
	t_rest=${1#${t_major}}
7826d1
	t_rest=${t_rest#.}
7826d1
	t_minor=${t_rest%%.*}
7826d1
	t_rest=${t_rest#${t_minor}}
7826d1
	t_rest=${t_rest#.}
7826d1
	t_patch=${t_rest%%-*}
7826d1
	t_rest=${t_rest#${t_patch}}
7826d1
	t_rest=${t_rest#-}
7826d1
	t_y=${t_rest%%.*}
7826d1
	t_rest=${t_rest#${t_y}}
7826d1
	t_rest=${t_rest#.}
7826d1
	t_z1=${t_rest%%.*}
7826d1
	t_rest=${t_rest#${t_z1}}
7826d1
	t_rest=${t_rest#.}
7826d1
	t_z2=${t_rest%%.*}
7826d1
7826d1
	# minor/major/patch/y should be numeric
7826d1
	[ -n "${t_major##*[!0-9]*}" ] || return 1
7826d1
	[ -n "${t_minor##*[!0-9]*}" ] || return 1
7826d1
	[ -n "${t_patch##*[!0-9]*}" ] || return 1
7826d1
	[ -n "${t_y##*[!0-9]*}" ] || return 1
7826d1
	# reset z1/z2 to zero if non-numeric
7826d1
	[ -n "${t_z1##*[!0-9]*}" ] || t_z1=0
7826d1
	[ -n "${t_z2##*[!0-9]*}" ] || t_z2=0
7826d1
7826d1
	while [ 1 -lt "$#" ]; do
7826d1
		cmp_type=upstream
7826d1
7826d1
		shift
7826d1
		m_major=${1%%.*}
7826d1
		m_rest=${1#${m_major}}
7826d1
		m_rest=${m_rest#.}
7826d1
		m_minor=${m_rest%%.*}
7826d1
		m_rest=${m_rest#${m_minor}}
7826d1
		m_rest=${m_rest#.}
7826d1
		m_patch=${m_rest%%-*}
7826d1
		m_rest=${m_rest#${m_patch}}
7826d1
		m_rest=${m_rest#-}
7826d1
		m_y=${m_rest%%.*}
7826d1
		m_rest=${m_rest#${m_y}}
7826d1
		m_rest=${m_rest#.}
7826d1
		m_z1=${m_rest%%.*}
7826d1
		m_rest=${m_rest#${m_z1}}
7826d1
		m_rest=${m_rest#.}
7826d1
		m_z2=${m_rest%%.*}
7826d1
7826d1
		# minor/major/patch should be numeric
7826d1
		[ -n "${m_major##*[!0-9]*}" ] || continue
7826d1
		[ -n "${m_minor##*[!0-9]*}" ] || continue
7826d1
		[ -n "${m_patch##*[!0-9]*}" ] || continue
7826d1
		# reset z1/z2 to zero if non-numeric
7826d1
		[ -n "${m_y##*[!0-9]*}" ] && cmp_type=y || m_y=0
7826d1
		[ -n "${m_z1##*[!0-9]*}" ] && cmp_type=z || m_z1=0
7826d1
		[ -n "${m_z2##*[!0-9]*}" ] && cmp_type=z || m_z2=0
7826d1
7826d1
		# Comparing versions
7826d1
		case "$cmp_type" in
7826d1
		upstream)
7826d1
			[ "$t_major" -ge "$m_major" ] || continue
7826d1
			[ "$t_minor" -ge "$m_minor" ] || continue
7826d1
			[ "$t_patch" -ge "$m_patch" ] || continue
7826d1
			return 0
7826d1
			;;
7826d1
		y)
7826d1
			[ "$t_major" -eq "$m_major" ] || continue
7826d1
			[ "$t_minor" -eq "$m_minor" ] || continue
7826d1
			[ "$t_patch" -eq "$m_patch" ] || continue
7826d1
			[ "$t_y" -ge "$m_y" ] || continue
7826d1
			return 0
7826d1
			;;
7826d1
		z)
7826d1
			[ "$t_major" -eq "$m_major" ] || continue
7826d1
			[ "$t_minor" -eq "$m_minor" ] || continue
7826d1
			[ "$t_patch" -eq "$m_patch" ] || continue
7826d1
			[ "$t_y" -eq "$m_y" ] || continue
7826d1
			[ "$t_z1" -ge "$m_z1" ] || continue
7826d1
			[ "$t_z2" -ge "$m_z2" ] || continue
7826d1
			return 0
7826d1
			;;
7826d1
		esac
7826d1
	done
7826d1
7826d1
	return 1
7826d1
}
7826d1
7826d1
# It is needed for SKX[1] for which different product segments
7826d1
# are differentiated by a value in the CAPID0 field of PCU registers
7826d1
# device[2].
7826d1
# [1] https://github.com/intel/Intel-Linux-Processor-Microcode-Data-Files/issues/21
7826d1
# [2] https://www.intel.com/content/dam/www/public/us/en/documents/specification-updates/xeon-scalable-spec-update.pdf#page=13
7826d1
#
7826d1
# $1 - params in config file, space-separated, in key=value form:
7826d1
#   domain=* - PCI domain, '*' or number
7826d1
#   bus=* - PCI bus, '*' or number
7826d1
#   device=* - PCI device, '*' or number
7826d1
#   function=* - PCI function, '*' or number
7826d1
#   vid= - PCI vendor ID, empty or number
7826d1
#   did= - PCI device ID, empty or number
7826d1
#   offset=0 - offset in configuration space
7826d1
#   size=4 - field size
7826d1
#   mask=0 - mask applied to the data read
7826d1
#   val=0 - comma-separated list of possible values
7826d1
#   mode=success-any [ success-ail, fail-any, fail-all ] - matching mode:
7826d1
#     success-any: Returns 0 if there was at least one match, otherwise 1.
7826d1
#     success-all: Returns 0 if there was at least one device checked and all
7826d1
#                  the checked devices have matches, otherwise 1.
7826d1
#     fail-any:    Returns 1 if there was at least one match, otherwise 0.
7826d1
#     fail-all:    Returns 1 if there was at least one device checked and all
7826d1
#                  the checked devices have matches, otherwise 0.
7826d1
# $2 - whether model filter is engaged (if it is not '1', just return the result
7826d1
#      based on "mode" value that assumes that there were 0 checks/0 matches).
7826d1
check_pci_config_val()
7826d1
{
7826d1
	local domain='*' bus='*' device='*' func='*' vid= did=
7826d1
	local offset=0 size=4 mask=0 val=0 mode=success-any
7826d1
	local checked=0 matched=0 path=''
7826d1
	local dev_path dev_vid dev_did dev_val
7826d1
	local opts="${1:-}"
7826d1
	local match_model="${2:-0}"
7826d1
7826d1
	set -- $1
7826d1
	while [ "$#" -gt 0 ]; do
7826d1
		[ "x${1#domain=}" = "x${1}" ] || domain="${1#domain=}"
7826d1
		[ "x${1#bus=}" = "x${1}" ] || bus="${1#bus=}"
7826d1
		[ "x${1#device=}" = "x${1}" ] || device="${1#device=}"
7826d1
		[ "x${1#function=}" = "x${1}" ] || func="${1#function=}"
7826d1
		[ "x${1#vid=}" = "x${1}" ] || vid="${1#vid=}"
7826d1
		[ "x${1#did=}" = "x${1}" ] || did="${1#did=}"
7826d1
		[ "x${1#offset=}" = "x${1}" ] || offset="${1#offset=}"
7826d1
		[ "x${1#size=}" = "x${1}" ] || size="${1#size=}"
7826d1
		[ "x${1#mask=}" = "x${1}" ] || mask="${1#mask=}"
7826d1
		[ "x${1#val=}" = "x${1}" ] || val="${1#val=}"
7826d1
		[ "x${1#mode=}" = "x${1}" ] || mode="${1#mode=}"
7826d1
7826d1
		shift
7826d1
	done
7826d1
7826d1
	path="$domain"
7826d1
	if [ "x$bus" = 'x*' ]; then
7826d1
		path="$path:$bus";
7826d1
	else
7826d1
		path=$(printf '%s:%02x' "$path" "$bus")
7826d1
	fi
7826d1
	if [ "x$device" = 'x*' ]; then
7826d1
		path="$path:$device";
7826d1
	else
7826d1
		path=$(printf '%s:%02x' "$path" "$device")
7826d1
	fi
7826d1
	if [ "x$func" = 'x*' ]; then
7826d1
		path="$path.$func";
7826d1
	else
7826d1
		path=$(printf '%s.%01x' "$path" "$func")
7826d1
	fi
7826d1
7826d1
	# Normalise VID, DID
7826d1
	[ -n "$vid" ] || vid="$(printf '0x%04x' "$vid")"
7826d1
	[ -n "$did" ] || did="$(printf '0x%04x' "$did")"
7826d1
7826d1
	( [ 1 != "$match_model" ] \
7826d1
	  || /usr/bin/find /sys/bus/pci/devices/ -maxdepth 1 -name "$path" \
7826d1
	  || : ) | (
7826d1
		while read -r dev_path; do
7826d1
			# Filter VID, DID
7826d1
			if [ -n "$vid" ]; then
7826d1
				dev_vid=$(/bin/cat "$dev_path/vendor")
7826d1
				[ "x$vid" = "x$dev_vid" ] || continue
7826d1
			fi
7826d1
			if [ -n "$did" ]; then
7826d1
				dev_did=$(/bin/cat "$dev_path/device")
7826d1
				[ "x$did" = "x$dev_did" ] || continue
7826d1
			fi
7826d1
7826d1
			checked="$((checked + 1))"
7826d1
7826d1
			dev_val="$(/usr/bin/od -j "$offset" -N "$size" -A n \
7826d1
					       -t "u$size" "$dev_path/config")"
7826d1
7826d1
			val_rest="${val}"
7826d1
			while :; do
7826d1
				cur_val="${val_rest%%,*}"
7826d1
				if [ "$((dev_val & mask))" = "$((cur_val & mask))" ]
7826d1
				then
7826d1
					matched="$((matched + 1))"
7826d1
					break
7826d1
				fi
7826d1
				[ "x${val_rest}" != "x${val_rest#*,}" ] || break
7826d1
				val_rest="${val_rest#*,}"
7826d1
			done
7826d1
7826d1
			case "$mode" in
7826d1
			success-any) [ "$matched" -eq 0 ] || { echo 0; exit; } ;;
7826d1
			success-all) [ "$matched" -eq "$checked" ] || { echo 1; exit; } ;;
7826d1
			fail-any)    [ "$matched" -eq 0 ] || { echo 1; exit; } ;;
7826d1
			fail-all)    [ "$matched" -eq "$checked" ] || { echo 0; exit; } ;;
7826d1
			*)           echo 2; exit;;
7826d1
			esac
7826d1
		done
7826d1
7826d1
		debug "PCI config value check ($opts): checked $checked," \
7826d1
		      "matched $matched (model check is set to $match_model)"
7826d1
7826d1
		case "$mode" in
7826d1
		success-any) if [ "$matched" -eq 0 ]; then echo 1; else echo 0; fi ;;
7826d1
		success-all) if [ "$matched" -gt 0 -a "$matched" -eq "$checked" ]; then echo 0; else echo 1; fi ;;
7826d1
		fail-any)    if [ "$matched" -eq 0 ]; then echo 0; else echo 1; fi  ;;
7826d1
		fail-all)    if [ "$matched" -gt 0 -a "$matched" -eq "$checked" ]; then echo 1; else echo 0; fi ;;
7826d1
		*)           echo 2; exit;;
7826d1
		esac
7826d1
	)
7826d1
}
7826d1
7826d1
# It is needed for filtering by BIOS vendor name that is available in DMI data
7826d1
#
7826d1
# $1 - params in config file, space-separated, in key=value form:
7826d1
#   key= - DMI data record to check. Can be one of the following: bios_date,
7826d1
#          bios_vendor, bios_version, board_asset_tag, board_name, board_serial,
7826d1
#          board_vendor, board_version, chassis_asset_tag, chassis_serial,
7826d1
#          chassis_type, chassis_vendor, chassis_version, product_family,
7826d1
#          product_name, product_serial, product_uuid, product_version,
7826d1
#          sys_vendor.
7826d1
#   val= - a string to match DMI data against.  Can be enclosed in single
7826d1
#          or double quotes.
7826d1
#   keyval= - a string of format "KEY(!)?[=:]VAL" (so, one of "KEY=VAL",
7826d1
#             "KEY!=VAL", "KEY:VAL", "KEY!:VAL") that allows providing
7826d1
#             a key-value pair in a single parameter.  It is possible to provide
7826d1
#             multiple keyval= parameters.  "!" before :/= means negated match.
7826d1
#             The action supplied in the mode= parameter is executed upon
7826d1
#             successful (non-)matching of all the keyval pairs (as well
7826d1
#             as the pair provided in a pair of key= and val= parameters).
7826d1
#   mode=success-equal [ success-equal, fail-equal ] - matching mode:
7826d1
#     success-equal: Returns 0 if the all values present in the corresponding
7826d1
#                    files under /sys/devices/virtual/dmi/id/<KEY> are equal
7826d1
#                    (or not equal in case of a keyval= with negated match)
7826d1
#                    to the respective values supplied as the values
7826d1
#                    of the keyval= parameters or the pair of key= vand val=
7826d1
#                    parameters, otherwise 1.
7826d1
#     fail-equal:    Returns 1 if all the values present in DMI files in sysfs
7826d1
#                    match (as described above), otherwise 0.
7826d1
#   no-model-mode=success [ success, fail ] - return value if model filter
7826d1
#                                             is not enabled:
7826d1
#     success: Return 0.
7826d1
#     fail:    Return 1.
7826d1
# $2 - whether model filter is engaged (if it is not '1', just return the result
7826d1
#      based on "no-model-mode" value).
7826d1
check_dmi_val()
7826d1
{
7826d1
	local key= val= keyval= keyvals= mode='success-equal' nm_mode='success'
7826d1
	local opts="${1:-}" opt= opt_=
7826d1
	local match_model="${2:-0}"
7826d1
7826d1
	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 "
7826d1
	local success=1
7826d1
7826d1
	while [ -n "$opts" ]; do
7826d1
		opt="${opts%%[ 	]*}"
7826d1
		[ -n "${opt}" ] || { opts="${opts#[ 	]}"; continue; }
7826d1
7826d1
		[ "x${opt#key=}" = "x${opt}" ] || key="${opt#key=}"
7826d1
		[ "x${opt#mode=}" = "x${opt}" ] || mode="${opt#mode=}"
7826d1
		[ "x${opt#no-model-mode=}" = "x${opt}" ] || \
7826d1
			nm_mode="${opt#no-model-mode=}"
7826d1
7826d1
		# Handle possible quoting
7826d1
		[ "x${opt#val=}" = "x${opt}" ] || {
7826d1
			case "${opt#val=}" in
7826d1
			[\']*) opt_="${opts#val=\'}"; val="${opt_%%\'*}"; opt="val='${val}'" ;;
7826d1
			[\"]*) opt_="${opts#val=\"}"; val="${opt_%%\"*}"; opt="val=\"${val}\"" ;;
7826d1
			*)    val="${opt#val=}" ;;
7826d1
			esac
7826d1
		}
7826d1
		[ "x${opt#keyval=}" = "x${opt}" ] || {
7826d1
			case "${opt#keyval=}" in
7826d1
			[\']*)
7826d1
				opt_="${opts#keyval=\'}"
7826d1
				keyval="${opt_%%\'*}"
7826d1
				opt="keyval='${keyval}'"
7826d1
				keyvals="${keyvals}
7826d1
					${keyval}"
7826d1
				;;
7826d1
			[\"]*)
7826d1
				opt_="${opts#keyval=\"}"
7826d1
				keyval="${opt_%%\"*}"
7826d1
				opt="keyval=\"${keyval}\""
7826d1
				keyvals="${keyvals}
7826d1
					${keyval}"
7826d1
				;;
7826d1
			*)
7826d1
				keyvals="${keyvals}
7826d1
					${opt#keyval=}"
7826d1
				;;
7826d1
			esac
7826d1
		}
7826d1
7826d1
		opts="${opts#"${opt}"}"
7826d1
		continue
7826d1
	done
7826d1
7826d1
	[ -z "$key" -a -z "$val" ] || keyvals="${key}=${val}${keyvals}"
7826d1
7826d1
	[ -n "x${keyvals}" ] || {
7826d1
		debug "Neither key=, val=, nor keyval= parameters were privoded"
7826d1
		echo 2
7826d1
		return
7826d1
	}
7826d1
7826d1
	[ 1 = "$match_model" ] || {
7826d1
		case "$nm_mode" in
7826d1
		success) echo 0 ;;
7826d1
		fail)    echo 1 ;;
7826d1
		*)
7826d1
			debug "Invalid no-model-mode value: \"${nm_mode}\""
7826d1
			echo 2
7826d1
			;;
7826d1
		esac
7826d1
7826d1
		return
7826d1
	}
7826d1
7826d1
	case "$mode" in
7826d1
	success-equal|fail-equal) ;;
7826d1
	*) debug "Invalid mode value: \"${nm_mode}\""; echo 2; return ;;
7826d1
	esac
7826d1
7826d1
	printf "%s\n" "${keyvals}" | (
7826d1
		while read l; do
7826d1
			[ -n "$l" ] || continue
7826d1
			key="${l%%[=:]*}"
7826d1
			val="${l#${key}[=:]}"
7826d1
7826d1
			cmp="="
7826d1
			[ "x${key%!}" = "x${key}" ] || {
7826d1
				cmp="!="
7826d1
				key="${key%!}"
7826d1
			}
7826d1
7826d1
			# Check key for validity
7826d1
			[ "x${valid_keys#* ${key} *}" != "x${valid_keys}" ] || {
7826d1
				debug "Invalid \"key\" parameter value: \"${key}\""
7826d1
				echo 2
7826d1
				return
7826d1
			}
7826d1
7826d1
			[ -r "/sys/devices/virtual/dmi/id/${key}" ] || {
7826d1
				debug "Can't access /sys/devices/virtual/dmi/id/${key}"
7826d1
				echo 3
7826d1
				return
7826d1
			}
7826d1
7826d1
			file_val="$(/bin/cat "/sys/devices/virtual/dmi/id/${key}")"
7826d1
7826d1
			[ "x${val}" "${cmp}" "x${file_val}" ] || {
7826d1
				case "$mode" in
7826d1
				success-equal) echo 1 ;;
7826d1
				fail-equal)    echo 0 ;;
7826d1
				esac
7826d1
7826d1
				return
7826d1
			}
7826d1
		done
7826d1
7826d1
		case "$mode" in
7826d1
		success-equal) echo 0 ;;
7826d1
		fail-equal)    echo 1 ;;
7826d1
		esac
7826d1
	)
7826d1
}
7826d1
7826d1
# check_dependency CURLEVEL DEP_TYPE DEP_NAME OPTS
7826d1
# DEP_TYPE:
7826d1
#   required - caveat can be enabled only if dependency is enabled
7826d1
#              (is not forcefully disabled and meets caveat conditions)
7826d1
# OPTS:
7826d1
#   match-model-mode=same [ on, off, same ] - what mode matching mode is to be used for dependency
7826d1
#   skip=skip [ fail, skip, success ]
7826d1
#   force-skip=skip [ fail, skip, success ]
7826d1
#   nesting-too-deep=fail [ fail, skip, success ]
7826d1
# Return values:
7826d1
#   0 - success
7826d1
#   1 - fail
7826d1
#   2 - skip
7826d1
#   9 - error
7826d1
check_dependency()
7826d1
{
7826d1
	local cur_level="$1"
7826d1
	local dep_type="$2"
7826d1
	local dep_name="$3"
7826d1
	local match_model_mode=same old_match_model="${match_model}"
7826d1
	local skip=skip
7826d1
	local force_skip=skip
7826d1
	local nesting_too_deep=fail
7826d1
7826d1
	local check="Dependency check for ${dep_type} ${dep_name}"
7826d1
7826d1
	set -- ${4:-}
7826d1
	while [ "$#" -gt 0 ]; do
7826d1
		[ "x${1#match-model-mode=}" = "x${1}" ] || match_model_mode="${1#match-model-mode=}"
7826d1
		[ "x${1#skip=}" = "x${1}" ] || skip="${1#skip=}"
7826d1
		[ "x${1#force-skip=}" = "x${1}" ] || force_skip="${1#force-skip=}"
7826d1
		[ "x${1#nesting-too-deep=}" = "x${1}" ] || nesting_too_deep="${1#nesting-too-deep=}"
7826d1
7826d1
		shift
7826d1
	done
7826d1
7826d1
	case "${dep_type}" in
7826d1
	required)
7826d1
		[ "x${dep_name%/*}" = "x${dep_name}" ] || {
7826d1
			debug "${check} error: dependency name (${dep_name})" \
7826d1
			      "cannot contain slashes"
7826d1
			echo 9
7826d1
			return
7826d1
		}
7826d1
7826d1
		[ "${MAX_NESTING_LEVEL}" -ge "$cur_level" ] || {
7826d1
			local reason="nesting level is too deep (${cur_level}) and nesting-too-deep='${nesting_too_deep}'"
7826d1
7826d1
			case "$nesting_too_deep" in
7826d1
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
7826d1
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
7826d1
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
7826d1
			*)       debug "${check} error: invalid" \
7826d1
				       "nesting-too-deep mode" \
7826d1
				       "(${nesting_too_deep})"; echo 9 ;;
7826d1
			esac
7826d1
7826d1
			return
7826d1
		}
7826d1
7826d1
		case "${match_model_mode}" in
7826d1
		same) ;;
7826d1
		on)   match_model=1 ;;
7826d1
		off)  match_model=0 ;;
7826d1
		*)
7826d1
			debug "${check} error: invalid match-model-mode" \
7826d1
			      "(${match_model_mode})"
7826d1
			echo 9
7826d1
			return
7826d1
			;;
7826d1
		esac
7826d1
7826d1
		local result=0
7826d1
		debug "${check}: calling check_caveat '${dep_name}'" \
7826d1
		      "'$(($cur_level + 1))' match_model=${match_model}"
7826d1
		check_caveat "${dep_name}" "$(($cur_level + 1))" > /dev/null || result="$?"
7826d1
7826d1
		match_model="${old_match_model}"
7826d1
7826d1
		case "${result}" in
7826d1
		0) debug "${check} succeeded: result=${result}"; echo "${result}" ;;
7826d1
		1) debug "${check} failed: result=${result}"; echo "${result}" ;;
7826d1
		2)
7826d1
			local reason="result=${result} and skip='${skip}'"
7826d1
7826d1
			case "${skip}" in
7826d1
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
7826d1
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
7826d1
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
7826d1
			*)       debug "${check} error: unexpected skip=" \
7826d1
				       "setting (${skip})"; echo 9 ;;
7826d1
			esac
7826d1
			;;
7826d1
		3)
7826d1
			local reason="result=${result} and force_skip='${force_skip}'"
7826d1
7826d1
			case "${force_skip}" in
7826d1
			success) debug "${check} succeeded: ${reason}"; echo 0 ;;
7826d1
			fail)    debug "${check} failed: ${reason}"; echo 1 ;;
7826d1
			skip)    debug "${check} skipped: ${reason}"; echo 2 ;;
7826d1
			*)       debug "${check} error: unexpected force-skip=" \
7826d1
				       "setting (${skip})"; echo 9 ;;
7826d1
			esac
7826d1
			;;
7826d1
		*)
7826d1
			debug "${check} error: unexpected check_caveat result" \
7826d1
			      "(${result})"; echo 9 ;;
7826d1
		esac
7826d1
		;;
7826d1
	*)
7826d1
		debug "${check} error: unknown dependency type '${dep_type}'"
7826d1
		echo 9
7826d1
		;;
7826d1
	esac
7826d1
}
7826d1
7826d1
# Provides model in format "VENDOR_ID FAMILY-MODEL-STEPPING"
7826d1
#
7826d1
# We check only the first processor as we don't expect non-symmetrical setups
7826d1
# with CPUs with caveats
7826d1
get_model_string()
7826d1
{
7826d1
	/usr/bin/printf "%s %02x-%02x-%02x" \
7826d1
		$(/bin/sed -rn '1,/^$/{
7826d1
			s/^vendor_id[[:space:]]*: (.*)$/\1/p;
7826d1
			s/^cpu family[[:space:]]*: (.*)$/\1/p;
7826d1
			s/^model[[:space:]]*: (.*)$/\1/p;
7826d1
			s/^stepping[[:space:]]*: (.*)$/\1/p;
7826d1
		}' /proc/cpuinfo)
7826d1
}
7826d1
7826d1
get_model_name()
7826d1
{
7826d1
	/bin/sed -rn '1,/^$/s/^model name[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
7826d1
}
7826d1
7826d1
get_vendor_id()
7826d1
{
7826d1
	/bin/sed -rn '1,/^$/s/^vendor_id[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
7826d1
}
7826d1
7826d1
get_mc_path()
7826d1
{
7826d1
	case "$1" in
7826d1
	GenuineIntel)
7826d1
		echo "intel-ucode/$2"
7826d1
		;;
7826d1
	AuthenticAMD)
7826d1
		echo "amd-ucode/$2"
7826d1
		;;
7826d1
	*)
7826d1
		# We actually only support Intel ucode, but things may break
7826d1
		# if nothing is printed (input would be gotten from stdin
7826d1
		# otherwise).
7826d1
		echo "invalid"
7826d1
		;;
7826d1
	esac
7826d1
}
7826d1
7826d1
get_mc_ver()
7826d1
{
7826d1
	/bin/sed -rn '1,/^$/s/^microcode[[:space:]]*: (.*)$/\1/p' /proc/cpuinfo
7826d1
}
7826d1
7826d1
7826d1
match_model=0
7826d1
configs=
7826d1
kver=$(/bin/uname -r)
7826d1
verbose=0
7826d1
early_check=0
7826d1
print_disclaimers=0
7826d1
7826d1
ret=0
7826d1
7826d1
while getopts "dek:c:mv" opt; do
7826d1
	case "${opt}" in
7826d1
	d)
7826d1
		print_disclaimers=1
7826d1
		early_check=2
7826d1
		;;
7826d1
	e)
7826d1
		early_check=1
7826d1
		;;
7826d1
	k)
7826d1
		kver="$OPTARG"
7826d1
		;;
7826d1
	c)
7826d1
		configs="$configs $OPTARG"
7826d1
		;;
7826d1
	m)
7826d1
		match_model=1
7826d1
		;;
7826d1
	v)
7826d1
		verbose=1
7826d1
		;;
7826d1
	*)
7826d1
		usage
7826d1
		exit 1;
7826d1
		;;
7826d1
	esac
7826d1
done
7826d1
7826d1
: "${configs:=$(find "${MC_CAVEATS_DATA_DIR}" -maxdepth 1 -mindepth 1 -type d -printf "%f\n")}"
7826d1
7826d1
cpu_model=$(get_model_string)
7826d1
cpu_model_name=$(get_model_name)
7826d1
cpu_vendor=$(get_vendor_id)
7826d1
7826d1
ret_paths=""
7826d1
ok_paths=""
7826d1
fail_paths=""
7826d1
7826d1
ret_cfgs=""
7826d1
ok_cfgs=""
7826d1
fail_cfgs=""
7826d1
7826d1
skip_cfgs=""
7826d1
7826d1
if [ 1 -eq "$early_check" ]; then
7826d1
	stage="early"
7826d1
else
7826d1
	stage="late"
7826d1
fi
7826d1
7826d1
# check_caveat CFG [CHECK_LEVEL]
7826d1
# changes ret_paths, ok_paths, fail_paths, ret_cfgs, ok_cfgs, fail_cfgs,
7826d1
# skip_cfgs if CHECK_LEVEL is set to 0 (default).
7826d1
# CHECK_LEVEL is used for recursive configuration dependency checks,
7826d1
# and indicates nesting level.
7826d1
# Return value:
7826d1
#  0 - check is successful
7826d1
#  1 - check has been failed
7826d1
#  2 - configuration has been skipped
7826d1
#  3 - configuration has been skipped due to presence of an override file
7826d1
check_caveat() {
7826d1
	local cfg="$1"
7826d1
	local check_level="${2:-0}"
7826d1
	local dir="$MC_CAVEATS_DATA_DIR/$cfg"
7826d1
7826d1
	[ -r "${dir}/readme" ] || {
7826d1
		debug "File 'readme' in ${dir} is not found, skipping"
7826d1
		return 2
7826d1
	}
7826d1
7826d1
	[ -r "${dir}/config" ] || {
7826d1
		debug "File 'config' in ${dir} is not found, skipping"
7826d1
		return 2
7826d1
	}
7826d1
7826d1
	local cfg_model=
7826d1
	local cfg_vendor=
7826d1
	local cfg_path=
7826d1
	local cfg_kvers=
7826d1
	local cfg_kvers_early=
7826d1
	local cfg_mc_min_ver_late=
7826d1
	local cfg_disable=
7826d1
	local cfg_pci=
7826d1
	local cfg_dmi=
7826d1
	local cfg_dependency=
7826d1
7826d1
	local key
7826d1
	local value
7826d1
7826d1
	while read -r key value; do
7826d1
		case "$key" in
7826d1
		model)
7826d1
			cfg_model="$value"
7826d1
			;;
7826d1
		vendor)
7826d1
			cfg_vendor="$value"
7826d1
			;;
7826d1
		path)
7826d1
			cfg_path="$cfg_path $value"
7826d1
			;;
7826d1
		kernel)
7826d1
			cfg_kvers="$cfg_kvers $value"
7826d1
			;;
7826d1
		kernel_early)
7826d1
			cfg_kvers_early="$cfg_kvers_early $value"
7826d1
			;;
7826d1
		mc_min_ver_late)
7826d1
			cfg_mc_min_ver_late="$value"
7826d1
			;;
7826d1
		disable)
7826d1
			cfg_disable="$cfg_disable $value "
7826d1
			;;
7826d1
		pci_config_val)
7826d1
			cfg_pci="$cfg_pci
7826d1
				$value"
7826d1
			;;
7826d1
		dmi)
7826d1
			cfg_dmi="$cfg_dmi
7826d1
				$value"
7826d1
			;;
7826d1
		dependency)
7826d1
			cfg_dependency="$cfg_dependency
7826d1
				$value"
7826d1
			;;
7826d1
		'#'*|'')
7826d1
			continue
7826d1
			;;
7826d1
		*)
7826d1
			debug "Unknown key '$key' (value '$value') in config" \
7826d1
			      "'$cfg'"
7826d1
			;;
7826d1
		esac
7826d1
	done < "${dir}/config"
7826d1
7826d1
	debug "${cfg}: model '$cfg_model', path '$cfg_path', kvers '$cfg_kvers'"
7826d1
	echo "$cfg_path"
7826d1
7826d1
	# Check for override files in the following order:
7826d1
	#  - disallow early/late specific caveat for specific kernel
7826d1
	#  - force early/late specific caveat for specific kernel
7826d1
	#  - disallow specific caveat for specific kernel
7826d1
	#  - force specific caveat for specific kernel
7826d1
	#
7826d1
	#  - disallow early/late specific caveat for any kernel
7826d1
	#  - disallow early/late any caveat for specific kernel
7826d1
	#  - force early/late specific caveat for any kernel
7826d1
	#  - force early/late any caveat for specific kernel
7826d1
	#  - disallow specific caveat for any kernel
7826d1
	#  - disallow any caveat for specific kernel
7826d1
	#  - force specific caveat for any kernel
7826d1
	#  - force any caveat for specific kernel
7826d1
	#
7826d1
	#  - disallow early/late everything
7826d1
	#  - force early/late everyhting
7826d1
	#  - disallow everything
7826d1
	#  - force everyhting
7826d1
	local ignore_cfg=0
7826d1
	local force_cfg=0
7826d1
	local override_file=""
7826d1
	local overrides="
7826d1
	0:$FW_DIR/$kver/disallow-$stage-$cfg
7826d1
	1:$FW_DIR/$kver/force-$stage-$cfg
7826d1
	0:$FW_DIR/$kver/disallow-$cfg
7826d1
	1:$FW_DIR/$kver/force-$cfg
7826d1
	0:$FW_DIR/$kver/disallow-$stage
7826d1
	0:$CFG_DIR/disallow-$stage-$cfg
7826d1
	1:$FW_DIR/$kver/force-$stage
7826d1
	1:$CFG_DIR/force-$stage-$cfg
7826d1
	0:$FW_DIR/$kver/disallow
7826d1
	0:$CFG_DIR/disallow-$cfg
7826d1
	1:$FW_DIR/$kver/force
7826d1
	1:$CFG_DIR/force-$cfg
7826d1
	0:$CFG_DIR/disallow-$stage
7826d1
	1:$CFG_DIR/force-$stage
7826d1
	0:$CFG_DIR/disallow
7826d1
	1:$CFG_DIR/force"
7826d1
	local o
7826d1
	local o_force
7826d1
	local override_file
7826d1
	for o in $(echo "$overrides"); do
7826d1
		o_force=${o%%:*}
7826d1
		override_file=${o#$o_force:}
7826d1
7826d1
		[ -e "$override_file" ] || continue
7826d1
7826d1
		if [ 0 -eq "$o_force" ]; then
7826d1
			ignore_cfg=1
7826d1
		else
7826d1
			force_cfg=1
7826d1
		fi
7826d1
7826d1
		break
7826d1
	done
7826d1
7826d1
	[ 0 -eq "$ignore_cfg" ] || {
7826d1
		debug "Configuration \"$cfg\" is ignored due to presence of" \
7826d1
		      "\"$override_file\"."
7826d1
		return 3
7826d1
	}
7826d1
7826d1
	# Check model if model filter is enabled
7826d1
	if [ 1 -eq "$match_model" -a  -n "$cfg_model" ]; then
7826d1
		[ "x$cpu_model" = "x$cfg_model" ] || {
7826d1
			debug "Current CPU model '$cpu_model' doesn't" \
7826d1
			      "match configuration CPU model '$cfg_model'," \
7826d1
			      "skipping"
7826d1
			return 2
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check paths if model filter is enabled
7826d1
	local cpu_mc_path
7826d1
	local cfg_mc_present
7826d1
	if [ 1 -eq "$match_model" -a  -n "$cfg_path" ]; then
7826d1
		cpu_mc_path="$MC_CAVEATS_DATA_DIR/$cfg/$(get_mc_path \
7826d1
			"$cpu_vendor" "${cpu_model#* }")"
7826d1
		cfg_mc_present=0
7826d1
7826d1
		for p in $(printf "%s" "$cfg_path"); do
7826d1
			/usr/bin/find "$MC_CAVEATS_DATA_DIR/$cfg" \
7826d1
				-path "$MC_CAVEATS_DATA_DIR/$cfg/$p" -print0 \
7826d1
			    | /bin/grep -zFxc "$cpu_mc_path" > /dev/null \
7826d1
			    || continue
7826d1
7826d1
			cfg_mc_present=1
7826d1
			break
7826d1
		done
7826d1
7826d1
		[ 1 = "$cfg_mc_present" ] || {
7826d1
			debug "No matching microcode files in '$cfg_path'" \
7826d1
			      "for CPU model '$cpu_model', skipping"
7826d1
			return 2
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check vendor if model filter is enabled
7826d1
	if [ 1 -eq "$match_model" -a  -n "$cfg_vendor" ]; then
7826d1
		[ "x$cpu_vendor" = "x$cfg_vendor" ] || {
7826d1
			debug "Current CPU vendor '$cpu_vendor' doesn't" \
7826d1
			      "match configuration CPU vendor '$cfg_vendor'," \
7826d1
			      "skipping"
7826d1
			return 2
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Has to be performed before dependency checks
7826d1
	[ 0 -eq "$force_cfg" ] || {
7826d1
		debug "Checks for configuration \"$cfg\" are ignored due to" \
7826d1
		      "presence of \"$override_file\"."
7826d1
7826d1
		return 0
7826d1
	}
7826d1
7826d1
	# Check dependencies
7826d1
	# It has to be performed here (before adding configuration
7826d1
	# to $ret_cfgs/$ret_paths) since it may be skipped.
7826d1
	if [ -n "$cfg_dependency" ]; then
7826d1
		dep_line="$(printf "%s\n" "$cfg_dependency" | \
7826d1
			while read -r dep_type dep_name dep_opts
7826d1
			do
7826d1
				[ -n "$dep_type" ] || continue
7826d1
				dep_res=$(check_dependency "$check_level" \
7826d1
							   "$dep_type" \
7826d1
							   "$dep_name" \
7826d1
							   "$dep_opts")
7826d1
				[ 0 != "$dep_res" ] || continue
7826d1
				echo "$dep_res $dep_type $dep_name $dep_opts"
7826d1
				break
7826d1
			done
7826d1
			echo "0 ")"
7826d1
7826d1
		case "${dep_line%% *}" in
7826d1
		0) ;;
7826d1
		2)
7826d1
			debug "Dependency check '${dep_line#* }'" \
7826d1
			      "induced configuration skip"
7826d1
			return 2
7826d1
			;;
7826d1
		*)
7826d1
			debug "Dependency check '${dep_line#* }'" \
7826d1
			      "failed (with return code ${dep_line%% *})"
7826d1
			return 1
7826d1
			;;
7826d1
		esac
7826d1
	fi
7826d1
7826d1
	# Check configuration files
7826d1
7826d1
	[ "x${cfg_disable%%* $stage *}" = "x$cfg_disable" ] || {
7826d1
		debug "${cfg}: caveat is disabled in configuration"
7826d1
		return 1
7826d1
	}
7826d1
7826d1
	# Check late load kernel version
7826d1
	if [ 1 -ne "$early_check" -a -n "$cfg_kvers" ]; then
7826d1
		check_kver "$kver" $cfg_kvers || {
7826d1
			debug "${cfg}: late load kernel version check for" \
7826d1
			      " '$kver' against '$cfg_kvers' failed"
7826d1
			return 1
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check early load kernel version
7826d1
	if [ 0 -ne "$early_check" -a -n "$cfg_kvers_early" ]; then
7826d1
		check_kver "$kver" $cfg_kvers_early || {
7826d1
			debug "${cfg}: early load kernel version check for" \
7826d1
			      "'$kver' against '$cfg_kvers_early' failed"
7826d1
			return 1
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check current microcode version for the late update
7826d1
	if [ -n "$cfg_mc_min_ver_late" -a 1 -ne "$early_check" -a \
7826d1
	   "x$cpu_model" = "x$cfg_model" ]; then
7826d1
		cpu_mc_ver="$(get_mc_ver)"
7826d1
7826d1
		[ 1 -eq $((cpu_mc_ver >= cfg_mc_min_ver_late)) ] || {
7826d1
			debug "${cfg}: CPU microcode version $cpu_mc_ver" \
7826d1
			      "failed check (should be at least" \
7826d1
			      "${cfg_mc_min_ver_late})"
7826d1
			return 1
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check PCI devices if model filter is enabled
7826d1
	# Note that the model filter check is done inside check_pci_config_val
7826d1
	# based on the 'mode=' parameter.
7826d1
	if [ -n "$cfg_pci" ]; then
7826d1
		pci_line="$(printf "%s\n" "$cfg_pci" | while read -r pci_line; do
7826d1
				[ -n "$pci_line" ] || continue
7826d1
				pci_res=$(check_pci_config_val "$pci_line" \
7826d1
							       "$match_model")
7826d1
				[ 0 != "$pci_res" ] || continue
7826d1
				echo "$pci_res $pci_line"
7826d1
				break
7826d1
			done
7826d1
			echo "0 ")"
7826d1
7826d1
		[ -z "${pci_line#* }" ] || {
7826d1
			debug "PCI configuration word check '${pci_line#* }'" \
7826d1
			      "failed (with return code ${pci_line%% *})"
7826d1
			return 1
7826d1
		}
7826d1
	fi
7826d1
7826d1
	# Check DMI data if model filter is enabled
7826d1
	# Note that the model filter check is done inside check_dmi_val
7826d1
	# (which returns the value of 'no-model-mode=' parameter
7826d1
	# if it is disenaged).
7826d1
	if [ -n "$cfg_dmi" ]; then
7826d1
		dmi_line="$(printf "%s\n" "$cfg_dmi" | while read -r dmi_line
7826d1
			do
7826d1
				[ -n "$dmi_line" ] || continue
7826d1
				dmi_res=$(check_dmi_val "$dmi_line" \
7826d1
							"$match_model")
7826d1
				[ 0 != "$dmi_res" ] || continue
7826d1
				echo "$dmi_res $dmi_line"
7826d1
				break
7826d1
			done
7826d1
			echo "0 ")"
7826d1
7826d1
		[ -z "${dmi_line#* }" ] || {
7826d1
			debug "DMI data check '${dmi_line#* }'" \
7826d1
			      "failed (with return code ${dmi_line%% *})"
7826d1
			return 1
7826d1
		}
7826d1
	fi
7826d1
7826d1
	return 0
7826d1
}
7826d1
7826d1
for cfg in $(echo "${configs}"); do
7826d1
	if cfg_path=$(check_caveat "$cfg"; exit "$?")
7826d1
	then
7826d1
		ret_cfgs="$ret_cfgs $cfg"
7826d1
		ret_paths="$ret_paths $cfg_path"
7826d1
		ok_cfgs="$ok_cfgs $cfg"
7826d1
		ok_paths="$ok_paths $cfg_path"
7826d1
	else
7826d1
		case "$?" in
7826d1
		1)
7826d1
			ret=1
7826d1
7826d1
			ret_cfgs="$ret_cfgs $cfg"
7826d1
			ret_paths="$ret_paths $cfg_path"
7826d1
			fail_cfgs="$fail_cfgs $cfg"
7826d1
			fail_paths="$fail_paths $cfg_path"
7826d1
7826d1
			[ 0 -eq "$print_disclaimers" ] \
7826d1
				|| [ ! -e "${MC_CAVEATS_DATA_DIR}/${cfg}/disclaimer" ] \
7826d1
				|| /bin/cat "${MC_CAVEATS_DATA_DIR}/${cfg}/disclaimer"
7826d1
			;;
7826d1
		2|3)
7826d1
			skip_cfgs="$skip_cfgs $cfg";
7826d1
			;;
7826d1
		*)
7826d1
			debug "Unexpected check_caveat return code '$?'" \
7826d1
			      "for config '$cfg'"
7826d1
			;;
7826d1
		esac
7826d1
	fi
7826d1
done
7826d1
7826d1
[ 0 -eq "$print_disclaimers" ] || exit 0
7826d1
7826d1
echo "cfgs$ret_cfgs"
7826d1
echo "skip_cfgs$skip_cfgs"
7826d1
echo "paths$ret_paths"
7826d1
echo "ok_cfgs$ok_cfgs"
7826d1
echo "ok_paths$ok_paths"
7826d1
echo "fail_cfgs$fail_cfgs"
7826d1
echo "fail_paths$fail_paths"
7826d1
7826d1
exit $ret