Blame SOURCES/gen_provides.sh

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