File jupiter-hw-support-download.sh.in of Package jupiter-hw-support

#!/bin/sh
set -eu

ARCHIVE_URL="https://gitlab.com/evlaV/jupiter-hw-support/-/archive/jupiter-__VERSION__/jupiter-hw-support-jupiter-__VERSION__.tar.gz"
ARCHIVE_NAME="$(basename "$ARCHIVE_URL")"

PKG_NAME="__NAME__"
PKG_VERSION="__VERSION__"
PKG_REL="__RELEASE__"

CHECKSUM_FILE="/usr/share/${PKG_NAME}/jupiter-hw-support.archive.sha256"

STATE_DIR="/usr/share/${PKG_NAME}/state"
STATE_FILE="${STATE_DIR}/installed-${PKG_NAME}.sha256"

POST_MESSAGE="/usr/share/${PKG_NAME}/update-messages/${PKG_NAME}-${PKG_VERSION}-${PKG_REL}-1"

# Install destinations
DEST_BIOS_DIR="/usr/share/jupiter_bios"
DEST_BIOS_UPDATER_DIR="/usr/share/jupiter_bios_updater"
DEST_CTRL_DIR="/usr/share/jupiter_controller_fw_updater"
DEST_RA_DIR="/usr/share/jupiter_controller_fw_updater/RA_bootloader_updater"
DEST_RA_LHT_DIR="/usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools"
DEST_RA_RFP_DIR="/usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64"

log_msg() {
    if [ -n "${POST_MESSAGE:-}" ]; then
        mkdir -p "$(dirname "$POST_MESSAGE")" 2>/dev/null || true
        echo "$*" | tee -a "$POST_MESSAGE"
    else
        echo "$*"
    fi
}

# Default: fail unless told otherwise
exit_code=1
for arg in "$@"; do
    if [ "$arg" = "--no-fail" ]; then
        exit_code=0
        break
    fi
done

get_expected_sha256() {
    # sha256sum format: "<hash>  <filename>"
    if [ ! -f "$CHECKSUM_FILE" ]; then
        return 1
    fi
    awk -v n="$ARCHIVE_NAME" '$2==n {print $1; exit}' "$CHECKSUM_FILE"
}

expected_sha256="$(get_expected_sha256 2>/dev/null || true)"
if [ -z "${expected_sha256:-}" ]; then
    log_msg "*** jupiter-hw-support not installed: checksum entry for ${ARCHIVE_NAME} not found in ${CHECKSUM_FILE}. ***"
    exit 0
fi

# If already installed with same checksum, do nothing
if [ -f "$STATE_FILE" ]; then
    installed_sha256="$(cat "$STATE_FILE" 2>/dev/null || true)"
    if [ "$installed_sha256" = "$expected_sha256" ]; then
        log_msg "*** No update necessary. jupiter-hw-support already up-to-date. ***"
        exit 0
    fi
fi

tmpname="$(basename "$0")"
tmpdir="$(mktemp -d "/tmp/$tmpname.XXXXXX" 2>/dev/null || true)"
trap 'rm -rf "$tmpdir"' EXIT

if [ -z "${tmpdir:-}" ] || [ ! -d "$tmpdir" ]; then
    log_msg "$0: Can't create temp dir, exiting..."
    exit "$exit_code"
fi

cd "$tmpdir" || exit "$exit_code"

log_msg "jupiter-hw-support archive:"
log_msg "  ${ARCHIVE_NAME}"
log_msg "  URL: ${ARCHIVE_URL}"
log_msg -n "  Fetching   ... "

if ! curl -L -s -o "$ARCHIVE_NAME" "$ARCHIVE_URL"; then
    log_msg "failed!"
    rm -f "$ARCHIVE_NAME"
    exit "$exit_code"
fi
log_msg "done"

log_msg -n "  Verifying ... "
actual_sha256="$(sha256sum "$ARCHIVE_NAME" | awk '{print $1}')"
if [ "$actual_sha256" != "$expected_sha256" ]; then
    log_msg "failed!"
    log_msg "  Expected: $expected_sha256"
    log_msg "  Actual:   $actual_sha256"
    rm -f "$ARCHIVE_NAME"
    exit "$exit_code"
fi
log_msg "done"

log_msg -n "  Extracting ... "
if ! tar -xzf "$ARCHIVE_NAME"; then
    log_msg "failed!"
    exit "$exit_code"
fi
log_msg "done"

# Find extracted top directory
topdir=""
expected_top="jupiter-hw-support-jupiter-${PKG_VERSION}"
if [ -d "$expected_top" ]; then
    topdir="$expected_top"
else
    topdir="$(find . -maxdepth 1 -type d -name 'jupiter-hw-support-*' | head -n1 | sed 's|^\./||')"
fi

if [ -z "${topdir:-}" ] || [ ! -d "$topdir" ]; then
    log_msg "Expected extracted directory not found."
    exit "$exit_code"
fi

# Helper to install a file from within the extracted tree
# $1: src relative path inside extracted root
# $2: dest absolute file path
# $3: mode (octal)
install_from_tree() {
    src="$topdir/$1"
    dst="$2"
    mode="$3"

    if [ ! -f "$src" ]; then
        log_msg "Warning: missing in archive: $1"
        return 1
    fi

    mkdir -p "$(dirname "$dst")" 2>/dev/null || true
    install -m "$mode" "$src" "$dst"
    return 0
}

install_file() {
    install_from_tree "$@" || {
        log_msg "ERROR: file missing: $1"
        exit "$exit_code"
    }
}

# --- Controller calibration ---
install_file "usr/bin/thumbstick_cal"      "/usr/bin/thumbstick_cal"      0755
install_file "usr/bin/thumbstick_fine_cal" "/usr/bin/thumbstick_fine_cal" 0755
install_file "usr/bin/trigger_cal"         "/usr/bin/trigger_cal"         0755

# --- BIOS blobs ---
install_file "usr/share/jupiter_bios/F7A0133_sign.fd"     "${DEST_BIOS_DIR}/F7A0133_sign.fd"    0644
install_file "usr/share/jupiter_bios/F7G0014T20_sign.fd"  "${DEST_BIOS_DIR}/F7G0014T20_sign.fd" 0644
install_file "usr/share/jupiter_bios/F7G0112_sign.fd"     "${DEST_BIOS_DIR}/F7G0112_sign.fd"    0644

# --- BIOS updater ---
install_file "usr/share/jupiter_bios_updater/BatCtrl"        "${DEST_BIOS_UPDATER_DIR}/BatCtrl"        0755
install_file "usr/share/jupiter_bios_updater/H2OFFTx64.sh"   "${DEST_BIOS_UPDATER_DIR}/H2OFFTx64.sh"   0755
install_file "usr/share/jupiter_bios_updater/H2OFFTx64-G.sh" "${DEST_BIOS_UPDATER_DIR}/H2OFFTx64-G.sh" 0755
install_file "usr/share/jupiter_bios_updater/h2offt"         "${DEST_BIOS_UPDATER_DIR}/h2offt"         0755
install_file "usr/share/jupiter_bios_updater/h2offt-g"       "${DEST_BIOS_UPDATER_DIR}/h2offt-g"       0755
install_file "usr/share/jupiter_bios_updater/h2osde-lx64"    "${DEST_BIOS_UPDATER_DIR}/h2osde-lx64"    0755
install_file "usr/share/jupiter_bios_updater/Logo.png"       "${DEST_BIOS_UPDATER_DIR}/Logo.png"       0644
install_file "usr/share/jupiter_bios_updater/msg_cht.ini"    "${DEST_BIOS_UPDATER_DIR}/msg_cht.ini"    0644
install_file "usr/share/jupiter_bios_updater/msg_eng.ini"    "${DEST_BIOS_UPDATER_DIR}/msg_eng.ini"    0644
install_file "usr/share/jupiter_bios_updater/platform.ini"   "${DEST_BIOS_UPDATER_DIR}/platform.ini"   0644

# --- Controller FW updater ---
install_file "usr/share/jupiter_controller_fw_updater/D20_APP_REL_6876D01A.bin" "${DEST_CTRL_DIR}/D20_APP_REL_6876D01A.bin" 0644
install_file "usr/share/jupiter_controller_fw_updater/D21_APP_REL_6876D013.bin" "${DEST_CTRL_DIR}/D21_APP_REL_6876D013.bin" 0644
install_file "usr/share/jupiter_controller_fw_updater/RA_APP_REL_6876D00D.bin"  "${DEST_CTRL_DIR}/RA_APP_REL_6876D00D.bin"  0644
install_file "usr/share/jupiter_controller_fw_updater/d20bootloader.py"         "${DEST_CTRL_DIR}/d20bootloader.py"         0755
install_file "usr/share/jupiter_controller_fw_updater/d21bootloader16.py"       "${DEST_CTRL_DIR}/d21bootloader16.py"       0755

# --- RA bootloader updater ---
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/boot_ra_Release.srec"     "${DEST_RA_DIR}/boot_ra_Release.srec" 0644
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/rfp_cli_linux.sh"         "${DEST_RA_DIR}/rfp_cli_linux.sh"     0755
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/BatCtrl" "${DEST_RA_LHT_DIR}/BatCtrl"          0755

# --- rfp-linux-x64 content ---
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64/Devices.xml"           "${DEST_RA_RFP_DIR}/Devices.xml"           0644
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64/License_Agreement.txt" "${DEST_RA_RFP_DIR}/License_Agreement.txt" 0644
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64/Messages.xml"          "${DEST_RA_RFP_DIR}/Messages.xml"          0644
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64/libRFP.so"             "${DEST_RA_RFP_DIR}/libRFP.so"             0644
install_file "usr/share/jupiter_controller_fw_updater/RA_bootloader_updater/linux_host_tools/rfp-linux-x64/rfp-cli"               "${DEST_RA_RFP_DIR}/rfp-cli"               0755

# Record installed checksum
mkdir -p "$STATE_DIR" 2>/dev/null || true
echo "$expected_sha256" > "$STATE_FILE"

log_msg "*** BIOS/firmware payload installed. ***"

# Show the license agreement content
license_path="${DEST_RA_RFP_DIR}/License_Agreement.txt"
if [ -f "$license_path" ]; then
    log_msg ""
    log_msg "===== License Agreement (RA bootloader updater) ====="
    cat "$license_path" | tee -a "$POST_MESSAGE"
    log_msg "===== End License Agreement ====="
else
    log_msg "License agreement file not found at: $license_path"
    exit "$exit_code"
fi

exit 0
openSUSE Build Service is sponsored by