Blame SOURCES/gen_provides.sh

c08efc
#! /bin/bash -efux
c08efc
c08efc
# Generator of RPM "Provides:" tags for Intel microcode files.
c08efc
#
c08efc
# SPDX-License-Identifier: CC0-1.0
c08efc
c08efc
IFS=$'\n'
c08efc
UPDATED="intel-beta"
c08efc
CODENAMES="codenames"
c08efc
c08efc
if [ "$#" -ge 1 ]; then
c08efc
	CODENAMES="$1"
c08efc
	shift
c08efc
fi
c08efc
c08efc
# Match only FF-MM-SS ucode files under intel-ucode/intel-ucode-with-caveats
c08efc
# directories.
c08efc
for f in $(grep -E '/intel-ucode.*/[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]-[0-9a-f][0-9a-f]$'); do
c08efc
	ucode=$(basename "$f")
c08efc
	ucode_caveat="$(basename "$(dirname "$(dirname "$f")")")"
c08efc
	ucode_fname="$ucode_caveat/$ucode"
c08efc
	file_sz="$(stat -c "%s" "$f")"
c08efc
	skip=0
c08efc
c08efc
	while :; do
c08efc
		[ "$skip" -lt "$file_sz" ] || break
c08efc
c08efc
		# Microcode header format description:
c08efc
		# https://gitlab.com/iucode-tool/iucode-tool/blob/master/intel_microcode.c
c08efc
		IFS=' ' read hdrver rev \
c08efc
		       date_y date_d date_m \
c08efc
		       cpuid cksum ldrver \
c08efc
		       pf_mask datasz totalsz <<- EOF
c08efc
		$(dd if="$f" bs=1 skip="$skip" count=36 status=none \
c08efc
			| hexdump -e '"" 1/4 "%u " 1/4 "%#x " \
c08efc
			                 1/2 "%04x " 1/1 "%02x " 1/1 "%02x " \
c08efc
					 1/4 "%08x " 1/4 "%x " 1/4 "%#x " \
c08efc
					 1/4 "%u " 1/4 "%u " 1/4 "%u" "\n"')
c08efc
		EOF
c08efc
c08efc
		[ 0 != "$datasz" ] || datasz=2000
c08efc
		[ 0 != "$totalsz" ] || totalsz=2048
c08efc
c08efc
		# TODO: add some sanity/safety checks here.  As of now, there's
c08efc
		#       a (pretty fragile) assumption that all the matched files
c08efc
		#       are valid Intel microcode files in the expected format.
c08efc
c08efc
		skip=$((skip + totalsz))
c08efc
c08efc
		#[ -n "$rev" ] || continue
c08efc
c08efc
		# Basic "Provides:" tag. Everything else is bells and whistles.
c08efc
		# It's possible that microcode files for different platform_id's
c08efc
		# and the same CPUID have the same version, that's why "sort -u"
c08efc
		# in the end.
c08efc
		printf "firmware(intel-ucode/%s) = %s\n" "$ucode" "$rev"
c08efc
c08efc
		# Generate extended "Provides:" tags with additional
c08efc
		# information, which allow more precise matching.
c08efc
		printf "iucode_date(fname:%s;cpuid:%s;pf_mask:0x%x) = %s.%s.%s\n" \
c08efc
			"$ucode_fname" "$cpuid" "$pf_mask" "$date_y" "$date_m" "$date_d"
c08efc
		printf "iucode_rev(fname:%s;cpuid:%s;pf_mask:0x%x) = %s\n" \
c08efc
			"$ucode_fname" "$cpuid" "$pf_mask" "$rev"
c08efc
c08efc
		# Generate tags for each possible platform_id
c08efc
		_pf=1
c08efc
		_pf_mask="$pf_mask"
c08efc
		while [ 0 -lt "$_pf_mask" ]; do
c08efc
			[ 1 -ne "$((_pf_mask % 2))" ] || \
c08efc
				# We try to provide a more specific firmware()
c08efc
				# dependency here.  It has incorrect file name,
c08efc
				# but allows constructing a required RPM
c08efc
				# capability name by (directly) using
c08efc
				# the contents of /proc/cpuinfo and
c08efc
				# /sys/devices/system/cpu/cpu*/microcode/processor_flags
c08efc
				# (except for a Deschutes CPU with sig 0x1632)
c08efc
				printf "iucode_rev(fname:%s;platform_id:0x%x) = %s\n" \
c08efc
					"$ucode_fname" "$_pf" "$rev"
c08efc
c08efc
			_pf_mask=$((_pf_mask / 2))
c08efc
			_pf=$((_pf * 2))
c08efc
		done
c08efc
c08efc
		# Generate tags with codename information, in case
c08efc
		# it is available
c08efc
		cpuid_up="$(echo "$cpuid" | tr 'a-z' 'A-Z')"
c08efc
		if [ -e "$CODENAMES" ]; then
c08efc
			grep '	'"$cpuid_up"'	' "$CODENAMES" \
c08efc
			| while IFS=$'\t' read segm int_fname codename stepping candidate_pf rest; do
c08efc
				codename=$(echo "$codename" | tr ' (),' '_[];')
c08efc
				candidate_pf=$(printf "%u" "0x${candidate_pf}")
c08efc
				[ \( 0 -ne "$pf_mask" \) -a \
c08efc
				  \( "$candidate_pf" -ne "$((candidate_pf & pf_mask))" \) ] || { \
c08efc
					printf "iucode_rev(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s\";stepping:\"%s\";pf_model:0x%x) = %s\n" \
c08efc
						"$ucode_fname" "$cpuid" "$pf_mask" \
c08efc
						"$segm" "$codename" "$stepping" "$candidate_pf" \
c08efc
						"$rev";
c08efc
					printf "iucode_date(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s\";stepping:\"%s\";pf_model:0x%x) = %s.%s.%s\n" \
c08efc
						"$ucode_fname" "$cpuid" "$pf_mask" \
c08efc
						"$segm" "$codename" "$stepping" "$candidate_pf" \
c08efc
						"$date_y" "$date_m" "$date_d";
c08efc
				  }
c08efc
			done
c08efc
		fi
c08efc
c08efc
		# Kludge squared: generate additional "Provides:" tags
c08efc
		# for the files in the overrides tarball (that a placed
c08efc
		# in a separate caveat with a specific name)
c08efc
		[ "x${ucode_caveat}" != "x${UPDATED}" ] || {
c08efc
			printf "firmware_updated(intel-ucode/%s) = %s\n" \
c08efc
				"$ucode" "$rev";
c08efc
		}
c08efc
	done
c08efc
done | sort -u