#!/bin/bash
#
# $Id: makeimg,v 1.9 2006/04/07 20:00:44 jlivesey Exp $
#
if [ $# -lt 2 ]; then
	echo makeimg basename block_no
	exit 1
fi
BASENAME=$1
BLOCK_NO=$2

#
# Temporary directory for intermediate files.
#
TMP_DIR=/tmp/${USER}/flash

#
# Cross tools
#
CROSS_TOOL_ROOT=/opt/crosstool/arm-elf/bin
OBJDUMP=${CROSS_TOOL_ROOT}/objdump
OBJDUMP_FLAGS="-d"

#
# Native tools
#
if [ -z $ROOT ]; then
    ROOT=${HOME}/root
fi
TOOL_ROOT=${ROOT}/usr/bin/x86

ELF2HEX=${TOOL_ROOT}/elftohex
ELF2HEX_FLAGS="-H -v -M 0x0d417800"
#ELF2HEX=${TOOL_ROOT}/elf2hex
#ELF2HEX_FLAGS=""

HEX2BIN=${TOOL_ROOT}/hex2bin
BIN2HEX=${TOOL_ROOT}/bin2hex
BIN2HEX_FLAGS="-b"

AES_ENCODE=${TOOL_ROOT}/aes_encode

MAKE_SHA=${TOOL_ROOT}/make_sha

TOFLASH=${TOOL_ROOT}/toflash2
if [ $BLOCK_NO == "0" ]; then
    #
    # Block 0 has no header and no bad block table
    #
    echo block $BLOCK_NO
    TOFLASH_FLAGS="-f"
else
    #
    # Other block have a header and a bad block table
    #
    TOFLASH_FLAGS="-f -b -h"
fi

SMALL2BIG=${TOOL_ROOT}/small2big

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

BASE=${BASENAME}

#
# Start
#
echo making bootable image of the elf file
if [ -d ${TMP_DIR} ]; then
    mv ${TMP_DIR} ${TMP_DIR}.`date +%d%H%M%S`
fi
mkdir -p ${TMP_DIR}

#
# Create a listing of ${BASE} for reference
#
echo make listing of elf file
${OBJDUMP} ${BASE}.elf ${OBJDUMP_FLAGS} > ${BASE}.list

#
# Create a hexdump of ${BASE}
#
echo make hex from elf file
${ELF2HEX} -f ${BASE}.elf ${ELF2HEX_FLAGS} > ${TMP_DIR}/${BASE}.hex
#${ELF2HEX} ${BASE}.elf
if [ $? -ne 0 ]; then
    echo ${ELF2HEX} failed
    exit 1
fi

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

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

#
# Generate the hash from this binary file
#
${MAKE_SHA} ${TMP_DIR}/${BASE}.bin > ${BASE}.sha

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

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

#
# AES decode the encoded file to do a compare
#
echo AES decode the encoded file
${AES_ENCODE} -d ${TMP_DIR}/${BASE}.bin.encrypted ${TMP_DIR}/${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 ${TMP_DIR}/${BASE}.bin ${TMP_DIR}/${BASE}.bin.decrypted
if [ $? -ne 0 ]; then
    echo cmp failed
    exit 1
fi

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

#
# Convert the flash image to big-endian
#
echo convert the flash image to big-endian
${SMALL2BIG} ${TMP_DIR}/${BASE}.flash.bin ${BASE}.flash.bin.big > ${TMP_DIR}/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} ${TMP_DIR}/${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
