#!/bin/bash
#
# $Id: crackimg,v 1.4 2006/04/07 20:04:14 jlivesey Exp $
#
# This is the inverse of makeimg.   It takes a purported flash
# image and converts it back into an executable, finishing off
# by making a .list file of the executable, so that we can check
# that the conversion was done correctly.
#
if [ $# -lt 1 ]; then
	echo crackimg flashimage
	exit 1
fi
BASENAME=$1

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

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

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

AES_ENCODE=${TOOL_ROOT}/aes_encode
AES_ENCODE_FLAGS="-d"

FROMFLASH=${TOOL_ROOT}/fromflash
FROMFLASH_FLAGS=""

SMALL2BIG=${TOOL_ROOT}/small2big

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

BASE=${BASENAME}

#
# Byte swap the flash image
#
${SMALL2BIG} ${BASE}.flash.bin ${BASE}.flash.bin.big

#
# Turn the flash image into a binary (still AES encrypted)
#
${FROMFLASH} ${BASE}.flash.bin.big ${BASE}.bin

#
# AES decrypt the binary to get an unencoded binary
#
${AES_ENCODE} ${AES_ENCODE_FLAGS} ${BASE}.bin ${BASE}.bin.decrypted

#
# Make this binary small-endian
#
${SMALL2BIG} ${BASE}.bin.decrypted ${BASE}.bin.decrypted.big

#
# Make a hex image of the decrypted file
#
${BIN2HEX} ${BASE}.bin.decrypted.big ${BASE}.flash.hex >bin2hex.log

# eof


