#!/bin/bash
#
# $Id: crackimg,v 1.5 2008/04/15 01:33:26 cnguyen 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

export PATH=$PATH:$ROOT/usr/bin/x86:$ROOT/usr/bin/linux

#
# 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=bin2hex
BIN2HEX_FLAGS="-b"

AES_ENCODE=aes_encode
AES_ENCODE_FLAGS="-d"

FROMFLASH=fromflash
FROMFLASH_FLAGS=""

SMALL2BIG=small2big

CRACK_FLASH=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


