#!/bin/bash
# this example shows how to create a random AES key, encrypt the image
# program into the target flash, and set the AES key on the target
# usage: encrypt_and_program 

SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )" 
PATH=$SCRIPT_HOME:$SCRIPT_HOME/../bin:$PATH

binary=$1
binary_hdr=${binary}.AES.hdr 

# Generate an aeskey on the target device and assign it to a variable
# Makes use of the -x option to execute a single command
echo Generating AES key
aeskey=`lpcscrypt -x genkeytarget`

# now display that value
echo Generated AES key: $aeskey

echo
image_manager --key $aeskey -i $binary -o $binary_hdr --bin

lpcscrypt -e s -v binary_hdr=$binary_hdr -v aeskey=$aeskey -s $SCRIPT_HOME/encrypt_and_program.scy
if [ $? -ne 0 ]; then
	echo Script failed 
	exit 1
fi
