import hashlib


def calculate_firmware_hash(firmware_stream):
    """Calculate the firmware hash for the given stream."""
    buffer = bytearray(1024 * 128)
    hasher = hashlib.sha256()
    while True:
        read = firmware_stream.readinto(buffer)
        if not read:
            break
        hasher.update(buffer[:read])
    return hasher.digest()