#!/bin/ksh
#
# Build the ixl-<device>-firmware-<version>.tgz bundles for fw_update(8)
# from Intel's 700 series NVM update package for Linux.
#
# One bundle per PCI device ID that ixl(4) updates.  A bundle holds
# the NVM images Intel lists for that device ID and an index for the
# driver, one line per image:
#
#	<eetrack> <reboot|power> <image file> <eetrack it replaces> ...
#
# Entries Intel limits to certain boards (PBA) or that need an external
# PHY image are left out, as are images no entry uses.  nvmupdate.cfg
# itself is not shipped.
#
# usage: mkixlbundles.sh 700Series_NVMUpdatePackage_vX_YY_Linux.tar.gz outdir

set -eu

# Must match ixl_nvm_devices[] in if_ixl.c and fw_update(8) patterns.c.
DEVICES="1572 1580 1581 1583 1584 158a 158b 15ff 37d0"

src=$1 out=$2
ver=${src##*_v}
ver=${ver%%_Linux*}
ver=$(echo "$ver" | tr _ .)
work=$out/work.$$

rm -rf "$work"
mkdir -p "$work/x"
tar -C "$work/x" -xzf "$src"
d=$work/x/700Series/Linux_x64

tr -d '\r' < "$d/nvmupdate.cfg" | awk -v dir="$work" '
function flush() {
	if (vendor == "8086" && image != "" && eepid != "" &&
	    reset != "" && replaces != "" && !skip)
		print eepid, reset, image, replaces >> (dir "/index." device)
}
/^BEGIN DEVICE$/ {
	vendor = device = image = eepid = reset = replaces = ""
	skip = 0
	next
}
/^END DEVICE$/ { flush(); next }
{
	i = index($0, ":")
	if (i == 0)
		next
	key = substr($0, 1, i - 1)
	val = substr($0, i + 1)
	sub(/^[ \t]+/, "", val)
	sub(/[ \t]+$/, "", val)
}
key == "VENDOR"		{ vendor = tolower(val) }
key == "DEVICE"		{ device = tolower(val) }
key == "NVM IMAGE"	{ image = val }
key == "EEPID"		{ eepid = tolower(val) }
key == "REPLACES"	{ replaces = tolower(val) }
key == "RESET TYPE"	{ reset = tolower(val) }
key == "PHY IMAGE" || key == "PBA" { skip = 1 }
'

for dev in $DEVICES; do
	[ -s "$work/index.$dev" ] || continue
	name=ixl-$dev
	pkg=$work/$name
	mkdir -p "$pkg/firmware/$name"

	while read -r _eepid _reset _image _replaces; do
		case $_reset in
		reboot|power) ;;
		*) echo "$name: $_image: bad reset type $_reset" >&2; exit 1 ;;
		esac
		cp "$d/$_image" "$pkg/firmware/$name/"
	done < "$work/index.$dev"
	cp "$work/index.$dev" "$pkg/firmware/$name/index"
	cp "$d/Intel_firmware_license.txt" "$pkg/firmware/$name-license"
	chown -R root:bin "$pkg/firmware"
	chmod 644 "$pkg/firmware/$name"/* "$pkg/firmware/$name-license"

	cat > "$pkg/+DESC" <<EOF
NVM images for Intel 700 series Ethernet controllers, device $dev
Intel NVM images for ixl(4) controllers with PCI device ID 0x$dev,
written to the controller by the driver when the system boots.
EOF
	{
		echo "@name $name-firmware-$ver"
		echo "@comment pkgpath=sysutils/firmware/$name ftp=yes"
		echo "@arch *"
		echo "+DESC"
		echo "@cwd /etc"
		echo "firmware/$name/"
		echo "firmware/$name-license"
		(cd "$pkg" && ls firmware/$name/* | sort)
	} > "$pkg/+CONTENTS"

	(cd "$pkg" && tar -czf "$out/$name-firmware-$ver.tgz" \
	    +CONTENTS +DESC firmware)
	echo "$name-firmware-$ver.tgz: $(wc -l < "$work/index.$dev")" \
	    "images, $(du -sk "$pkg/firmware" | cut -f1) KB installed," \
	    "$(du -k "$out/$name-firmware-$ver.tgz" | cut -f1) KB package"
done

rm -rf "$work"
