#!/bin/bash
#
# $Id: makebsimg,v 1.5 2006/02/28 02:51:33 jlivesey Exp $
#
# This takes two input images, which should be BS1 and BS2, and
# creates a flash image from them which ought to be written to
# block #2 os the flash
#
# In the flash image, BS1 is placed in page 0, and BS2 is placed 
# in pages 1..63.   (TBD: If BS2 is very large, then we may spill 
# over into block 3 of the flash.
#
# Other than the two input image files, and the fact that they are
# in .dol rather than .elf format, this script is very similar to
# makeimg.    I chose to make it a separate script to avoid makeimg
# becoming very complicated and difficult to read.
if [ $# -lt 3 ]; then
	echo makeimg BS1-name BS2-name block_no
	exit 1
fi
BS1NAME=$1
BS2NAME=$2
BLOCK_NO=$3
BASE="joint"

#
# Aes_encode now has $KEY and $IV compiled into the code, so we
# don't need to include keys for the encryption phase of 
# constructing the flash image
#

#
# Cross tools
#
CROSS_TOOL_ROOT=/opt/crosstool/arm-elf/bin

#
# Native tools
#
ROOT=${HOME}/root
TOOL_ROOT=${ROOT}/usr/bin/x86

DOL2HEX=${TOOL_ROOT}/doltohex
DOL2HEX_FLAGS="-v"
HEX2BIN=${TOOL_ROOT}/hex2bin
BIN2HEX=${TOOL_ROOT}/bin2hex
BIN2HEX_FLAGS="-b"

AES_ENCODE=${TOOL_ROOT}/aes_encode

TOFLASH=${TOOL_ROOT}/toflash2
TOFLASH_FLAGS="-f -b"

SMALL2BIG=${TOOL_ROOT}/small2big

CRACK_FLASH=${TOOL_ROOT}/crack_flash.py
CRACK_FLASH_FLAGS="-h -sv -pv"

#
# Start
#
echo making flash image of BS1 and BS2

#
# Make a hex image of BS1.    BS1 is a raw binary file and can
# be converted straight to hex.   We pad BS1 out to 2048 bytes 
# so that BS2 begins in the second page of the block.
#
echo make hex image of BS1
if [ ! -f ${BS1NAME}.img ]; then
    echo ${BS1NAME} not found
    exit 1
fi
${BIN2HEX} ${BS1NAME}.img ${BS1NAME}.hex -m 2048 > bin2hex1.log
if [ $? -ne 0 ]; then
    echo ${BIN2HEX} failed
    exit 1
fi

#
# Make a hex image of BS2.    BS2 is also a raw binary file
# and can be converted straight to hex.
#
echo make hex image of BS2
if [ ! -f ${BS2NAME}.img ]; then
    echo ${BS2NAME}.img not found
    exit 1
fi
${BIN2HEX} ${BS2NAME}.img ${BS2NAME}.hex > bin2hex2.log
if [ $? -ne 0 ]; then
    echo ${DOL2HEX} failed
    exit 1
fi

#
# Concatenate these two hex images.
#
echo concatenate the two hex images
cat ${BS1NAME}.hex ${BS2NAME}.hex > ${BASE}.hex

#
# Convert the hex file into bin
#
echo make binary from hex
${HEX2BIN} ${BASE}.hex ${BASE}.bin > hex2bin.log
if [ $? -ne 0 ]; then
    echo ${HEX2BIN} failed
    exit 1
fi

#
# Make big-endian
#
echo make big-endian
mv ${BASE}.bin ${BASE}.bin.small
${SMALL2BIG} ${BASE}.bin.small ${BASE}.bin >small2big1.log
if [ $? -ne 0 ]; then
    echo ${SMALL2BIG} failed
    exit 1
fi

#
# AES encode the binary file
#
echo AES encode the binary file
echo AES encode the binary file
echo ${AES_ENCODE} ${BASE}.bin ${BASE}.bin.encrypted
${AES_ENCODE} ${BASE}.bin ${BASE}.bin.encrypted
if [ $? -ne 0 ]; then
    echo ${AES_ENCODE} failed
    exit 1
fi

#
# Make small-endian
#
echo make small-endian
${SMALL2BIG} ${BASE}.bin.encrypted ${BASE}.bin.encrypted.small >small2big2.log

#
# AES decode the encoded file to do a compare
#
echo AES decode the encoded file
${AES_ENCODE} -d ${BASE}.bin.encrypted ${BASE}.bin.decrypted
if [ $? -ne 0 ]; then
    echo ${AES_ENCODE} failed
    exit 1
fi

#
# Compare the decrypted file with the original
#
echo comparing the original and the encrypted/decrypted file
cmp ${BASE}.bin ${BASE}.bin.decrypted

#
# Create a flash image
#
echo make flash image from binary
${TOFLASH} ${BASE}.bin.encrypted.small ${BASE}.flash.bin ${TOFLASH_FLAGS} > toflash.log
if [ $? -ne 0 ]; then
    echo ${TOFLASH} failed
    exit 1
fi

#
# Convert the flash image to big-endian
#
echo make flash image from binary
${SMALL2BIG} ${BASE}.flash.bin ${BASE}.flash.bin.big > small2big2.log
if [ $? -ne 0 ]; then
    echo ${SMALL2BIG} failed
    exit 1
fi

#
# In order to verify the flash image, turn it into hex and
# run crack_flash over it
#
echo convert the flash back into hex one byte wide
${BIN2HEX} ${BASE}.flash.bin ${BASE}.flash.bin.hex ${BIN2HEX_FLAGS} > bin2hex.log
if [ $? -ne 0 ]; then
    echo ${BIN2HEX} failed
    exit 1
fi

#
# Verify the hex flash image
#
echo verify the flash image
${CRACK_FLASH} ${BASE}.flash.bin.hex ${CRACK_FLASH_FLAGS} > crack_flash.log
if [ $? -ne 0 ]; then
    echo ${CRACK_FLASH} failed
    exit 1
fi
grep spare crack_flash.log


# eof
