Skip to content

Instantly share code, notes, and snippets.

@rbreslow
Created April 3, 2023 21:58
Show Gist options
  • Save rbreslow/4c328c4a36f8265e635886c02d5d19da to your computer and use it in GitHub Desktop.
Save rbreslow/4c328c4a36f8265e635886c02d5d19da to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
function usage() {
echo -n \
"Usage: $(basename "$0") path_to_output_directory
Download BIOS update from ASUS, extract Intel Flash Descriptor/ME, neutralize, and shrink.
"
}
IFD_BIN_HASH="092caeee117de27c0eb30587defcb6449a33c7c325b6f3c47b5a7a79670b5c3f"
ME_BIN_HASH="8dda1e8360fbb2da05bfcd187f6e7b8a272a67d66bc0074bbfd1410eb35e3e17"
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
if [[ "${1:-}" == "--help" ]]; then
usage
else
if [[ -z "${COREBOOT_DIR}" ]]; then
echo "ERROR: No COREBOOT_DIR variable defined."
exit 1
fi
output_dir="$(realpath "${1:-./}")"
if [[ ! -f "${output_dir}/ifd.bin" || ! -f "${output_dir}/me.bin" ]]; then
# Obtain Recovery BIOS capsule (.cap) file from ASUS.
# https://www.asus.com/us/supportonly/p8z77-m%20pro/helpdesk_bios/
pushd "$(mktemp -d)"
curl -O https://dlcdnets.asus.com/pub/ASUS/mb/LGA1155/P8Z77-M_PRO/P8Z77-M-PRO-ASUS-2203.zip
unzip P8Z77-M-PRO-ASUS-2203.zip
# The first 2 KiB, or 2048 bytes, of the .cap file, consists of the
# capsule file header. The remainder of the file contains the raw
# ROM data.
# https://web.archive.org/web/20230403201708/https://www.overclock.net/threads/asus-z10pe-d16-ws-owners-thread.1579548/page-30
dd bs=1024 skip=2 if=P8Z77-M-PRO-ASUS-2203.CAP of=p8z77_m_pro_asus_2203.bin
mv p8z77_m_pro_asus_2203.bin "${COREBOOT_DIR}/util/me_cleaner"
popd
# Neutralize Intel ME and resize the Intel Flash Descriptor (IFD)
# layout.
# https://github.com/corna/me_cleaner/wiki/External-flashing#neutralize-and-shrink-intel-me-useful-only-for-coreboot
pushd "${COREBOOT_DIR}/util/me_cleaner"
python me_cleaner.py -S -r -t -d -O out.bin -D ifd_shrinked.bin -M me_shrinked.bin ./p8z77_m_pro_asus_2203.bin
# Clear each entry in the JEDEC/Vendor Specific Component
# Capabilities (VSCC) table. The VSCC table starts at offset 0xDF0,
# or 3568 bytes, into the file. The VSCC table contains eight
# entries taking up 32 bytes in the unmodified Flash Descriptor. So,
# we write 32 bytes worth of ones.
printf '\xFF%.0s' {1..32} | dd of="./ifd_shrinked.bin" bs=1 seek=3568 count=32 conv=notrunc
# Set VSCC Table Length (VTL) to zero. VTL is an 8-bit integer at
# offset 0xEFD, or 3837 bytes, into the file.
printf '\x00' | dd of="./ifd_shrinked.bin" bs=1 seek=3837 count=1 conv=notrunc
mv ifd_shrinked.bin "${output_dir}/ifd.bin"
mv me_shrinked.bin "${output_dir}/me.bin"
rm ./*.bin
popd
fi
if ! echo "${IFD_BIN_HASH} ${output_dir}/ifd.bin" | sha256sum --check; then
echo "ERROR: SHA256 checksum for ifd.bin doesn't match."
exit 1
fi
if ! echo "${ME_BIN_HASH} ${output_dir}/me.bin" | sha256sum --check; then
echo "ERROR: SHA256 checksum for me.bin doesn't match."
exit 1
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment