#! /bin/bash -efu

# Trigger microcode reload with additional check for BDW-EP that can have
# microcode reloaded only in case kernel has specific patches.
#
# SPDX-License-Identifier: CC0-1.0

CFG_BDW=/usr/share/microcode_ctl/ucode_with_caveats/intel-06-4f-01/config
CHECK_KVER=/usr/libexec/microcode_ctl/check_kver
MC_FILE=/lib/firmware/microcode.dat

# Broadwell-EP/EX check
if /bin/grep -l GenuineIntel /proc/cpuinfo |
    /usr/bin/xargs /bin/grep -l -E "model[[:space:]]*: 79$" > /dev/null; then
	kver="$(/bin/uname -r)"
	read -r path kvers < "$CFG_BDW"

	# Do nothing if it's BDW-EP/EX and kernel is not new enough
	"$CHECK_KVER" "$(/bin/uname -r)" $kvers || exit 1

	MC_FILE=/lib/firmware/microcode-06-4f-01.dat
fi

/sbin/microcode_ctl -Qu -f "$MC_FILE"
