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