Blame SOURCES/gen_provides.sh

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