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