Blame SOURCES/gen_provides.sh

742279
#! /bin/bash -efux
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
742279
742279
	while :; do
742279
		[ "$skip" -lt "$file_sz" ] || break
742279
742279
		# Microcode header format description:
742279
		# https://gitlab.com/iucode-tool/iucode-tool/blob/master/intel_microcode.c
742279
		IFS=' ' read hdrver rev \
742279
		       date_y date_d date_m \
742279
		       cpuid cksum ldrver \
742279
		       pf_mask datasz totalsz <<- EOF
742279
		$(dd if="$f" bs=1 skip="$skip" count=36 status=none \
742279
			| hexdump -e '"" 1/4 "%u " 1/4 "%#x " \
742279
			                 1/2 "%04x " 1/1 "%02x " 1/1 "%02x " \
742279
					 1/4 "%08x " 1/4 "%x " 1/4 "%#x " \
742279
					 1/4 "%u " 1/4 "%u " 1/4 "%u" "\n"')
742279
		EOF
742279
742279
		[ 0 != "$datasz" ] || datasz=2000
742279
		[ 0 != "$totalsz" ] || totalsz=2048
742279
742279
		# TODO: add some sanity/safety checks here.  As of now, there's
742279
		#       a (pretty fragile) assumption that all the matched files
742279
		#       are valid Intel microcode files in the expected format.
742279
742279
		skip=$((skip + totalsz))
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
		cpuid_up="$(echo "$cpuid" | tr 'a-z' 'A-Z')"
742279
		if [ -e "$CODENAMES" ]; then
742279
			grep '	'"$cpuid_up"'	' "$CODENAMES" \
742279
			| while IFS=$'\t' read segm int_fname codename stepping candidate_pf rest; do
742279
				codename=$(echo "$codename" | tr ' (),' '_[];')
742279
				candidate_pf=$(printf "%u" "0x${candidate_pf}")
742279
				[ \( 0 -ne "$pf_mask" \) -a \
742279
				  \( "$candidate_pf" -ne "$((candidate_pf & pf_mask))" \) ] || { \
742279
					printf "iucode_rev(fname:%s;cpuid:%s;pf_mask:0x%x;segment:\"%s\";codename:\"%s\";stepping:\"%s\";pf_model:0x%x) = %s\n" \
742279
						"$ucode_fname" "$cpuid" "$pf_mask" \
742279
						"$segm" "$codename" "$stepping" "$candidate_pf" \
742279
						"$rev";
742279
					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" \
742279
						"$ucode_fname" "$cpuid" "$pf_mask" \
742279
						"$segm" "$codename" "$stepping" "$candidate_pf" \
742279
						"$date_y" "$date_m" "$date_d";
742279
				  }
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