Blame SOURCES/gen_provides.sh

078ac8
#! /bin/bash -efu
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
ebb8cf
	ext_hdr=0
ebb8cf
	ext_sig_cnt=0
ebb8cf
	ext_sig_pos=0
ebb8cf
	next_skip=0
c59b13
ebb8cf
	# Microcode header format description:
ebb8cf
	# https://gitlab.com/iucode-tool/iucode-tool/blob/master/intel_microcode.c
c59b13
	while :; do
c59b13
		[ "$skip" -lt "$file_sz" ] || break
c59b13
ebb8cf
		# Do we parse ext_sig table or another microcode header?
ebb8cf
		if [ 0 != "$next_skip" ]; then
ebb8cf
			# Check whether we should abort ext_sig table parsing
ebb8cf
			[ \( "${skip}" -lt "${next_skip}" \) -a \
ebb8cf
			  \( "${ext_sig_pos}" -lt "${ext_sig_cnt}" \) ] || {
ebb8cf
				skip="${next_skip}"
ebb8cf
				next_skip=0
ebb8cf
				continue
ebb8cf
			}
ebb8cf
ebb8cf
			# ext_sig, 12 bytes in size
ebb8cf
			IFS=' ' read cpuid pf_mask <<- EOF
ebb8cf
			$(hexdump -s "$skip" -n 8 \
ebb8cf
				-e '"" 1/4 "%08x " 1/4 "%u" "\n"' "$f")
ebb8cf
			EOF
ebb8cf
ebb8cf
			skip="$((skip + 12))"
ebb8cf
			ext_sig_pos="$((ext_sig_pos + 1))"
ebb8cf
		else
ebb8cf
			# Microcode header, 48 bytes, last 3 fields reserved
ebb8cf
			IFS=' ' read hdrver rev \
ebb8cf
			       date_y date_d date_m \
ebb8cf
			       cpuid cksum ldrver \
ebb8cf
			       pf_mask datasz totalsz <<- EOF
ebb8cf
			$(hexdump -s "$skip" -n 36 \
ebb8cf
				-e '"" 1/4 "%u " 1/4 "%#x " \
ebb8cf
			               1/2 "%04x " 1/1 "%02x " 1/1 "%02x " \
ebb8cf
				       1/4 "%08x " 1/4 "%x " 1/4 "%#x " \
ebb8cf
				       1/4 "%u " 1/4 "%u " 1/4 "%u" "\n"' "$f")
ebb8cf
			EOF
ebb8cf
ebb8cf
			[ 0 != "$datasz" ] || datasz=2000
ebb8cf
			[ 0 != "$totalsz" ] || totalsz=2048
ebb8cf
ebb8cf
			# TODO: add some sanity/safety checks here.  As of now,
ebb8cf
			#       there's a (pretty fragile) assumption that all
ebb8cf
			#       the matched files are valid Intel microcode
ebb8cf
			#       files in the expected format.
ebb8cf
ebb8cf
			# ext_sig table is after the microcode payload,
ebb8cf
			# check for its presence
ebb8cf
			if [ 48 -lt "$((totalsz - datasz))" ]; then
ebb8cf
				next_skip="$((skip + totalsz))"
ebb8cf
				skip="$((skip + datasz + 48))"
ebb8cf
				ext_sig_pos=0
ebb8cf
ebb8cf
				# ext_sig table header, 20 bytes in size,
ebb8cf
				# last 3 fields are reserved.
ebb8cf
				IFS=' ' read ext_sig_cnt  <<- EOF
ebb8cf
				$(hexdump -s "$skip" -n 4 \
ebb8cf
					-e '"" 1/4 "%u" "\n"' "$f")
ebb8cf
				EOF
ebb8cf
ebb8cf
				skip="$((skip + 20))"
ebb8cf
			else
ebb8cf
				skip="$((skip + totalsz))"
ebb8cf
				next_skip=0
ebb8cf
			fi
ebb8cf
		fi
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
		if [ -e "$CODENAMES" ]; then
36ce7f
			cpuid_up="$(echo "$cpuid" | tr 'a-z' 'A-Z')"
36ce7f
			cpuid_short="$(printf "%x" "0x$cpuid")"
36ce7f
			(grep '	'"$cpuid_up"'	' "$CODENAMES" || :; grep ';'"$cpuid_short"';' "$CODENAMES" || :) \
36ce7f
			| while IFS=$';\t' read segm int_fname codename stepping candidate_pf cpuid_cn cname variants rest; do
36ce7f
				[ "x${segm###}" = "x$segm" ] || continue
36ce7f
				[ -n "${segm}" ] || continue
c59b13
				codename=$(echo "$codename" | tr ' (),' '_[];')
c59b13
				candidate_pf=$(printf "%u" "0x${candidate_pf}")
36ce7f
				(IFS=','; for s in $stepping; do
36ce7f
					[ \( 0 -ne "$pf_mask" \) -a \
36ce7f
					  \( 0 -eq "$((candidate_pf & pf_mask))" \) ] || { \
36ce7f
						printf "iucode_rev(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s\";stepping:\"%s\";pf_model:0x%x) = %s\n" \
36ce7f
							"$ucode_fname" "$cpuid" "$pf_mask" \
36ce7f
							"$segm" "$codename" "$s" "$candidate_pf" \
36ce7f
							"$rev";
36ce7f
						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" \
36ce7f
							"$ucode_fname" "$cpuid" "$pf_mask" \
36ce7f
							"$segm" "$codename" "$s" "$candidate_pf" \
36ce7f
							"$date_y" "$date_m" "$date_d";
36ce7f
						if [ "$cpuid_short" = "$cpuid_cn" -a -n "$variants" ]; then
36ce7f
							(IFS=','; for v in $variants; do
36ce7f
								v=$(echo "$v" | tr ' (),' '_[];')
36ce7f
								printf "iucode_rev(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s_%s\";stepping:\"%s\";pf_model:0x%x) = %s\n" \
36ce7f
									"$ucode_fname" "$cpuid" "$pf_mask" \
36ce7f
									"$segm" "$codename" "$v" "$s" "$candidate_pf" \
36ce7f
									"$rev";
36ce7f
								printf "iucode_date(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s_%s\";stepping:\"%s\";pf_model:0x%x) = %s.%s.%s\n" \
36ce7f
									"$ucode_fname" "$cpuid" "$pf_mask" \
36ce7f
									"$segm" "$codename" "$v" "$s" "$candidate_pf" \
36ce7f
									"$date_y" "$date_m" "$date_d";
36ce7f
							done)
36ce7f
						fi
36ce7f
					  }
36ce7f
				done)
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