Blame SOURCES/gen_provides.sh

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