Files
nvdriver/tools/vbios_flash.sh

242 lines
8.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Safe NVIDIA GPU VBIOS flashing helper for Linux multi-GPU / PLX-switch servers.
#
# Solves two common Linux failure modes that cause `nvflash` to fail with
# `ERROR: A timeout occurred waiting for the PMU` / `NV_UCODE_CMD_COMMAND_PREP failed`:
# 1. A kernel GPU driver (`nvidia`, `nvidia_rs`, `nouveau`, `vfio-pci`) is bound
# or already initialized ACR/PMU since boot.
# 2. With no driver bound, Linux PCIe runtime PM suspends upstream PLX/PCIe
# switch downstream ports into D3hot, causing BAR0 reads to return 0xffffffff.
#
# Safety defaults:
# - Dry-run (pre-flight inspection) by default; pass `--flash` to write EEPROM.
# - Never bundles or downloads proprietary binaries/ROMs.
# - Never passes override flags (`-6`, `--overridesub`), keeping NVIDIA's
# cryptographic signature, PCI ID, Subsystem ID, and Board ID checks intact.
# - Always backs up the current VBIOS before flashing unless `--no-backup` is set.
# - Never triggers a PCI reset or system reboot automatically.
set -euo pipefail
NVFLASH=""
ROM=""
BACKUP_DIR=""
DO_FLASH=0
DO_BACKUP=1
ONLY_INDEX=""
usage() {
cat <<'EOF'
Usage:
sudo tools/vbios_flash.sh --nvflash <path> --rom <path> [options]
Options:
--nvflash <path> Path to NVIDIA nvflash binary (required)
--rom <path> Path to .rom or .nvr VBIOS image (required)
--flash Actually flash matching GPUs (default is dry-run pre-flight)
--index <N> Operate only on nvflash adapter index N (default: all GPUs)
--backup-dir <dir> Directory for pre-flash VBIOS backup (default: ./vbios-backups)
--no-backup Skip pre-flash VBIOS backup
-h, --help Show this help
Typical workflow on a server with nvdriver.service:
1. sudo systemctl disable nvdriver.service && sudo reboot
2. sudo tools/vbios_flash.sh --nvflash /path/to/nvflash --rom /path/to/image.nvr
3. sudo tools/vbios_flash.sh --nvflash /path/to/nvflash --rom /path/to/image.nvr --flash
4. sudo systemctl enable nvdriver.service && sudo reboot
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--nvflash) NVFLASH="${2:-}"; shift 2 ;;
--rom) ROM="${2:-}"; shift 2 ;;
--flash) DO_FLASH=1; shift ;;
--index) ONLY_INDEX="${2:-}"; shift 2 ;;
--backup-dir) BACKUP_DIR="${2:-}"; shift 2 ;;
--no-backup) DO_BACKUP=0; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1" >&2; usage; exit 2 ;;
esac
done
if [[ -z "$NVFLASH" || -z "$ROM" ]]; then
echo "ERROR: --nvflash and --rom are required." >&2
usage
exit 2
fi
if [[ $EUID -ne 0 ]]; then
echo "ERROR: must be run as root (use sudo)." >&2
exit 1
fi
if [[ ! -x "$NVFLASH" ]]; then
echo "ERROR: nvflash binary not found or not executable: $NVFLASH" >&2
exit 1
fi
if [[ ! -r "$ROM" ]]; then
echo "ERROR: VBIOS image not found or not readable: $ROM" >&2
exit 1
fi
if [[ -z "$BACKUP_DIR" ]]; then
BACKUP_DIR="$(pwd)/vbios-backups"
fi
# 1. Discover all NVIDIA display/3D controller PCI devices (vendor 0x10de, class 0x0300/0x0302).
mapfile -t GPU_BDFS < <(
for dev in /sys/bus/pci/devices/*; do
[[ -r "$dev/vendor" && -r "$dev/class" ]] || continue
[[ "$(cat "$dev/vendor")" == "0x10de" ]] || continue
cls="$(cat "$dev/class")"
case "$cls" in
0x0300*|0x0302*) basename "$dev" ;;
esac
done | sort
)
if [[ ${#GPU_BDFS[@]} -eq 0 ]]; then
echo "ERROR: no NVIDIA GPUs found on PCI bus." >&2
exit 1
fi
echo "Found ${#GPU_BDFS[@]} NVIDIA GPU(s): ${GPU_BDFS[*]}"
# 2. Refuse to proceed if any GPU currently has a kernel driver bound.
BOUND=()
for bdf in "${GPU_BDFS[@]}"; do
if [[ -L "/sys/bus/pci/devices/$bdf/driver" ]]; then
drv="$(basename "$(readlink -f "/sys/bus/pci/devices/$bdf/driver")")"
BOUND+=("$bdf ($drv)")
fi
done
if [[ ${#BOUND[@]} -gt 0 ]]; then
echo "ERROR: refusing to run nvflash while GPUs have kernel drivers bound:" >&2
printf ' - %s\n' "${BOUND[@]}" >&2
echo "" >&2
echo "Unbinding after a driver has booted ACR/PMU is often insufficient for nvflash." >&2
echo "Disable the GPU service and reboot cleanly first, e.g.:" >&2
echo " sudo systemctl disable nvdriver.service && sudo reboot" >&2
exit 1
fi
# 3. Wake all upstream PCIe bridges AND the GPUs themselves to D0, and enable
# PCI Memory Space (sysfs `enable=1`) so BAR0 does not read 0xffffffff on a
# fresh boot with no GPU driver bound.
echo "Waking PCIe hierarchy to D0 (power/control=on) and enabling BAR0 memory space..."
for bdf in "${GPU_BDFS[@]}"; do
cur="$(readlink -f "/sys/bus/pci/devices/$bdf")"
# Collect GPU + all PCIe ancestor bridges, then wake top-down.
chain=()
while [[ "$cur" == */0000:* ]]; do
chain=("$cur" "${chain[@]}")
cur="$(dirname "$cur")"
done
for node in "${chain[@]}"; do
if [[ -w "$node/power/control" ]]; then
echo on > "$node/power/control"
fi
done
dev_path="/sys/bus/pci/devices/$bdf"
en_before="$(cat "$dev_path/enable" 2>/dev/null || echo 0)"
if [[ "$en_before" == "0" && -w "$dev_path/enable" ]]; then
echo 1 > "$dev_path/enable"
fi
en_after="$(cat "$dev_path/enable" 2>/dev/null || echo unknown)"
st_after="$(cat "$dev_path/power_state" 2>/dev/null || echo unknown)"
echo " $bdf: power_state=$st_after enable=$en_before->$en_after"
done
# 4. Verify BAR0 PMC_BOOT_0 is readable (not 0xffffffff) on every GPU.
echo "Verifying BAR0 accessibility (PMC_BOOT_0)..."
python3 - "${GPU_BDFS[@]}" <<'PY'
import mmap, os, struct, sys
bad = []
for bdf in sys.argv[1:]:
path = f"/sys/bus/pci/devices/{bdf}/resource0"
fd = os.open(path, os.O_RDONLY)
try:
mm = mmap.mmap(fd, 0x1000, mmap.MAP_SHARED, mmap.PROT_READ)
try:
boot0 = struct.unpack_from("<I", mm, 0)[0]
finally:
mm.close()
finally:
os.close(fd)
print(f" {bdf}: PMC_BOOT_0 = {boot0:#010x}")
if boot0 in (0, 0xffffffff):
bad.append(bdf)
if bad:
print(f"ERROR: BAR0 unreadable (0xffffffff/0) on: {', '.join(bad)}", file=sys.stderr)
sys.exit(1)
PY
# 5. Inspect target ROM image and enumerate adapters via nvflash.
echo ""
echo "=== Target VBIOS image metadata ==="
"$NVFLASH" --version "$ROM" | grep -E "Image [0-9]+:|Image Name|Device Name|Vendor ID|Device ID|Subsystem|Project|Version :|Build Date" || true
echo ""
echo "=== Detected nvflash adapters ==="
LIST_OUT="$("$NVFLASH" --list)"
echo "$LIST_OUT"
mapfile -t ADAPTER_INDICES < <(
printf '%s\n' "$LIST_OUT" | sed -n 's/^<\([0-9][0-9]*\)> .*/\1/p'
)
if [[ ${#ADAPTER_INDICES[@]} -eq 0 ]]; then
echo "ERROR: nvflash --list found no adapters." >&2
exit 1
fi
if [[ -n "$ONLY_INDEX" ]]; then
ADAPTER_INDICES=("$ONLY_INDEX")
fi
echo ""
echo "=== Current VBIOS versions ==="
for idx in "${ADAPTER_INDICES[@]}"; do
"$NVFLASH" --index="$idx" -A --version | grep -E "Adapter:|Version :"
done
if [[ $DO_FLASH -eq 0 ]]; then
echo ""
echo "DRY-RUN PASSED: all ${#ADAPTER_INDICES[@]} adapter(s) are awake, readable, and ready."
echo "Re-run with --flash to back up and flash: $ROM"
exit 0
fi
# 6. Back up current VBIOS image before flashing.
if [[ $DO_BACKUP -eq 1 ]]; then
mkdir -p "$BACKUP_DIR"
ts="$(date -u +%Y%m%dT%H%M%SZ)"
backup_file="$BACKUP_DIR/vbios-backup-idx${ADAPTER_INDICES[0]}-$ts.rom"
echo ""
echo "=== Backing up adapter ${ADAPTER_INDICES[0]} VBIOS to $backup_file ==="
"$NVFLASH" --index="${ADAPTER_INDICES[0]}" -A --save "$backup_file"
fi
# 7. Flash each selected adapter sequentially without any override flags.
for idx in "${ADAPTER_INDICES[@]}"; do
echo ""
echo "=== Flashing adapter $idx ==="
"$NVFLASH" --index="$idx" -A "$ROM"
done
# 8. Verify flashed VBIOS versions via read-back.
echo ""
echo "=== Post-flash VBIOS verification ==="
for idx in "${ADAPTER_INDICES[@]}"; do
"$NVFLASH" --index="$idx" -A --version | grep -E "Adapter:|Version :"
done
echo ""
echo "ALL ${#ADAPTER_INDICES[@]} ADAPTER(S) FLASHED AND VERIFIED."
echo "Re-enable your GPU service (e.g. sudo systemctl enable nvdriver.service) and reboot to activate the new VBIOS."