#
# Copyright 2014, 2015 Cumulus Networks, Inc.
# All rights reserved
#
# U-Boot installer functions and utilities
#


##
## Image slot state set
##
## Each image slot is in one of the following states:
##
## 0 -- OK, ready to boot.
## 1 -- Installation in progress.
## 2 -- Installation failed.
## 3 -- Unitialized.  Installation never attempted.
##
cl_slot_state_set()
{
    local slot=$1
    local new_state=$2

    if [ "$dry_run" = "y" ] ; then
	_log_msg "Dry run: Skipping slot $slot state $new_state update.\n"
    else
	uboot_setenv -f slot_state${slot} $new_state
    fi
}

##
## Image slot version set
##
## Store the software version for the image slot.
##
cl_slot_version_set()
{
    local slot=$1
    local new_version=$2

    if [ "$dry_run" = "y" ] ; then
	_log_msg "Dry run: Skipping slot $slot version $new_version update.\n"
    else
	uboot_setenv -f cl.ver${slot} $new_version
    fi
}

##
## environment variable install operation handler
##
image_env_handler()
{

    log_pre=
    if [ "$dry_run" = "y" ] ; then
        log_pre="Dry run: "
    fi

    # verify the current architecture is equal to the "Architecture" variable.
    installer_arch=$(grep 'Architecture:' control | awk '{ print $2 }')
    if [ "$installer_arch" != "$arch" ] ; then
        log_failure_msg "${log_pre}Unexpected Architecture $installer_arch.  Expecting \`$arch'."
        return 1
    fi
    log_debug "${log_pre}Installing environment variables for architecture: $installer_arch ."

    log_debug "${log_pre}Installing environment variables for platform: $platform ."

    # verify integrity of uboot_env.tar.xz
    _data_sha1_verify uboot_env.tar.xz || return 1

    # extract uboot_env_tar from archive
    local uboot_env_tar=uboot_env.tar.xz
    cl_run $TAR xf $data_tar $uboot_env_tar
    [ -f "$uboot_env_tar" ] || {
	log_failure_msg "${log_pre}Unable to extract $uboot_env_tar from the archive."
	return 1
    }
    cl_run $XZ -d uboot_env.tar.xz
    cl_run $TAR -xf uboot_env.tar

    # create env script file
    local env_files=
    if [ -f "uboot_env/$platform.platform_clear.inc" ] ; then
        env_files="$platform.platform_clear.inc common_env.inc $platform.platform.inc"
    else
        env_files="clear_env.inc common_env.inc $platform.platform.inc"
    fi
    local env_script=env.txt
    cl_run touch $env_script
    for f in $env_files ; do
	[ -f "uboot_env/$f" ] || {
	    log_failure_msg "${log_pre}Missing script file: uboot_env/$f"
	    return 1
	}
	cat "uboot_env/$f" >> $env_script
    done

    if [ "$verbose" = "y" ] ; then
	_log_msg "Updating the following u-boot environment variables:\n"
	cat $env_script
    fi

    log_begin_msg "${log_pre}Updating u-boot environment variables"
    if [ "$dry_run" != "y" ] ; then
	uboot_setenv -f -s $env_script
    fi
    log_end_msg

}

# This function is run in the context of ONIE.
# Set default boot entry to slot 1
arch_post_provision()
{
    # Set some additional u-boot variables
    _log_msg "${log_pre}Updating u-boot environment variables.\n"
    if [ "$dry_run" != "y" ] ; then
	(cat <<EOF
debugargs
lbootargs
bootsource	flashboot
cl.active	1
bp		$boot_partition
EOF
	) > env.txt
	uboot_setenv -f -s env.txt || {
	    log_failure_msg "uboot_setenv -f -s env.txt failed.  env.txt contains:"
	    cat env.txt
	    log_failure_msg "Fix problem at u-boot prompt."
	    return 1
	}
    fi
}

# This function runs in ONIE context.
# Save various information about the install to the provisioned
# system:
arch_save_info()
{
    local save_dir="$1"
    uboot_printenv > $save_dir/u-boot-env.txt
}
