UPGRADE_FW_MAJOR="2026-10-19-0-46be68fe-26.10-plus"
UPGRADE_FW_VERSION="2026-11-24-0-05bd4b38-26.11-plus"
UPGRADE_FW_REQUIRE="2026-10-19-0-46be68fe-26.10-plus"
# Zynq (bm3-am2) sysupgrade COMMAND
#
# Sourced by the BOS nix sysupgrade:
#   check_image "$package_file" and
#   do_upgrade "$image_file" "$install_partition" "$install_offset"

# kernel FIT slot is 8 MiB and rootfs slot is 48 MiB inside `system`
NAND_KERNEL_BLOCK_COUNT="64"
NAND_ROOTFS_BLOCK_COUNT="384"
NAND_UBOOT_BLOCK_COUNT="64"
NAND_FPGA_BLOCK_COUNT="16"
NAND_BLOCK_SIZE="0x20000"

current_fw_version() {
    cat /etc/bos_version 2>/dev/null
}

current_fw_major() {
    cat /etc/bos_major 2>/dev/null || echo unknown
}

check_fw_version() {
    echo "Gathering current firmware information..."

    local fw_major=$(current_fw_major)
    local fw_version=$(current_fw_version)

    echo "version             : $fw_version"
    echo "major               : $fw_major"

    echo "Checking compatibility..."

    if [ "$UPGRADE_FW_VERSION" ">" "$fw_version" ]; then
        # firmware upgrade
        if [ "$UPGRADE_FW_REQUIRE" ">" "$fw_version" ]; then
            echo "Firmware upgrade to '$UPGRADE_FW_VERSION' is not possible!"
            echo "Firmware version '$UPGRADE_FW_REQUIRE' is required before upgrading to this version."
            return 1
        fi
    elif [ "$UPGRADE_FW_VERSION" "<" "$fw_version" ]; then
        # firmware downgrade
        if [ "$UPGRADE_FW_MAJOR" != "$fw_major" ]; then
            echo "Firmware downgrade to '$UPGRADE_FW_VERSION' is not possible!"
            echo "Downgrade is only possible among firmwares with major version '$UPGRADE_FW_MAJOR'."
            echo "Do the factory reset and try to upgrade to this version."
            return 1
        fi
    fi

    return 0
}

check_image() {
    # Legacy LEDE runs this in a subshell and ignores a failure under -F, so
    # kill its sysupgrade ($$ is the parent shell) before it writes anything
    if [ -f /etc/openwrt_release ]; then
        echo "Upgrading from LEDE is not supported, install 26.10 first" >&2
        kill $$
        return 1
    fi
    check_fw_version
}

# Backstop: only the legacy LEDE sysupgrade calls this, and only when
# check_image failed to stop it; reboot back into LEDE before any write
pre_upgrade() {
    echo "Upgrading from LEDE is not supported, install 26.10 first" >&2
    reboot -f
}

# Skip the write when NAND already holds the file's content, otherwise
# erase + write + verify (nand_write_verified from common.sh, sourced by do_upgrade)
nand_write_if_different() {
    local file="$1"
    local mtd="$2"
    local offset="$3"
    local block_count="$4"
    local size checksum

    size=$(wc -c < "$file")
    checksum="/tmp/nand_write.checksum"

    sha256sum <"$file" >"$checksum"
    # mtd-utils 1.5 nanddump pads -l up to a whole page, so cut the read-back
    # to the file size before hashing
    if nanddump -s "$offset" -q -l "$size" "$mtd" 2>/dev/null | head -c "$size" |
        sha256sum -c "$checksum" >/dev/null 2>&1; then
        rm -f "$checksum"
        return 0
    fi
    rm -f "$checksum"

    nand_write_verified "$file" "$mtd" "$offset" "$block_count"
}

# BOS nix -> nix upgrade; called by the BOS sysupgrade script which
# sources common.sh + system.sh beforehand (partition + offset vars)
do_upgrade() {
    local image_file="$1"
    local install_partition="$2"
    local install_offset="$3"
    # Get the package root directory from the image file path
    local package_root="$(dirname "$image_file")"

    # Load constants "$BOS_KERNEL_FILE", "$BOS_ENV_FILE", ...
    . /lib/functions/common.sh

    # The caller runs do_upgrade in an AND/OR list, which disables `set -e`
    # inside the whole function - every required erase/write must propagate
    # its failure explicitly, otherwise a failed write is reported as a
    # successful upgrade and the miner reboots with incomplete images.

    # kernel FIT
    flash_erase "$install_partition" "$LOCAL_BOS_KERNEL_OFFSET_BOS_LAYOUT" \
        "$NAND_KERNEL_BLOCK_COUNT" || return 1
    nandwrite -p -s "$LOCAL_BOS_KERNEL_OFFSET_BOS_LAYOUT" "$install_partition" \
        "$package_root/$BOS_KERNEL_FILE" || return 1

    # rootfs (squashfs ramdisk)
    flash_erase "$install_partition" "$install_offset" "$NAND_ROOTFS_BLOCK_COUNT" || return 1
    nandwrite -p -s "$install_offset" "$install_partition" "$image_file" || return 1

    # FPGA bitstream + U-Boot FIT + saved environment only when changed
    if [ -f "$package_root/fpga" ]; then
        nand_write_if_different "$package_root/fpga" "$BOS_FPGA1_MTD" 0 \
            "$NAND_FPGA_BLOCK_COUNT" || return 1
    fi
    if [ -f "$package_root/uboot" ]; then
        nand_write_if_different "$package_root/uboot" "$BOS_UBOOT_MTD" \
            "$LOCAL_BOS_UBOOT_OFFSET_BOS_LAYOUT" "$NAND_UBOOT_BLOCK_COUNT" || return 1
    fi
    if [ -f "$package_root/$BOS_ENV_FILE" ]; then
        nand_write_if_different "$package_root/$BOS_ENV_FILE" "$BOS_UBOOT_ENV_MTD" 0 4 ||
            return 1
    fi

    return 0
}
