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