#!/bin/bash

# Input files
IOS=bin/broadon/ios.merged
INSN=bin/nintendo/16instruction.bin
BINFO=bin/nintendo/bootinfo.bin
BS1=bin/nintendo/BS1.img
BS2=bin/nintendo/BS2.img

#Tools
if [ -z $ROOT ]; then
    ROOT=`pwd`
fi
#CONVDIR=$ROOT/tools/linux/
SPLIT=/usr/bin/split
MAKEPAD=$ROOT/usr/bin/x86/makepad

# Encryption and flashing tools
#
# Native tools
#
TOOL_ROOT=${ROOT}/usr/bin/x86
SMALL2BIG=${TOOL_ROOT}/small2big
AES_ENCODE=${TOOL_ROOT}/aes_encode
TOFLASH=${TOOL_ROOT}/toflash2
#
# First block has a header and bad block indications
#
FLASH_1_FLAGS=" -b -h"
#
# Bemaining blocks have no header, but they do have
# bad block indications
#
FLASH_234_FLAGS=" -b"


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

#
# 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}

function Usage()
{
 echo "-----------------------------------------"
 echo "Scripts to create flash images for IOS + RVL OS"
 echo "Usage: createIOSFlashImg"
 echo "Needs following files as input:"
 echo "          ./bin/broadon/ios.merged"
 echo "          ./bin/nintendo/16instruction.bin"
 echo "          ./bin/nintendo/bootinfo.bin"
 echo "          ./bin/nintendo/BS1.img"
 echo "          ./bin/nintendo/BS2.img"
 echo "-----------------------------------------"
 echo "createIOSFlashImg clobber deletes all built images"
}
 

# Print usage message
Usage

if [ "$1" == "clobber" ]; then
    echo "Deleting block*.preflash"
    rm -f block*.preflash
    exit
fi

if [ ! -f $IOS ]; then
    echo $IOS missing
    exit 1
fi
if [ ! -f $INSN ]; then
    echo $INSN missing
    exit 1
fi
if [ ! -f $BINFO ]; then
    echo $BINFO missing
    exit 1
fi
if [ ! -f $BS1 ]; then
    echo $BS1 missing
    exit 1
fi
if [ ! -f $BS2 ]; then
    echo $BS2 missing
    exit 1
fi
 ###################
# Split IOS into a chunk of 126K and another with the rest
# Assume IOS fits into 2 blocks and is > 1 block

IOS_SIZE=`ls -l $IOS |awk '{print $5}'`

# If IOS > 1 block - 1 page, split it. Otherwise, create a 
# dummy part2.
if [ $IOS_SIZE -gt 129024 ]; then
    #split <input file> -b <bytes per chunk> <out_file_prefix>
    echo "bigger"
    split $IOS -b 129024 IOSX
    mv IOSXaa ios-part1.nohdr.block.bin
    mv IOSXab ios-part2.unpadded
else
    mv $IOS ios-part1.unpadded
    IOS_PART1_SIZE=`ls -l ios-part1.unpadded |awk '{print $5}'`
    IOS1_PAD_SIZE=$((129024 - $IOS_PART1_SIZE))
    $MAKEPAD $IOS1_PAD_SIZE tmp.pad
    cat ios-part1.unpadded tmp.pad >ios-part1.nohdr.block.bin
    echo > ios-part2.unpadded
fi


IOS_PART2_SIZE=`ls -l ios-part2.unpadded |awk '{print $5}'`
IOS2_PAD_SIZE=$((131072 - $IOS_PART2_SIZE))
$MAKEPAD $IOS2_PAD_SIZE tmp.pad

cat ios-part2.unpadded tmp.pad >ios-part2.block.bin

rm ios-part2.unpadded tmp.pad

Block1=ios-part1.nohdr.block.bin
Block2=ios-part2.block.bin
############ Block 1, 2 binaries ready #################

######################
# Generate blocks for Nintendo files
#

############ Block3 binary generation #################
## Pad insni, binfo, BS1 to 1 page
SIZETMP=`ls -l $INSN |awk '{print $5}'`
PADTMP=$((2048 - $SIZETMP))
$MAKEPAD $PADTMP tmp.pad
cat $INSN tmp.pad > insn.page.bin
rm -f tmp.pad

SIZETMP=`ls -l $BINFO|awk '{print $5}'`
PADTMP=$((2048 - $SIZETMP))
$MAKEPAD $PADTMP tmp.pad
cat $BINFO tmp.pad > binfo.page.bin
rm -f tmp.pad

SIZETMP=`ls -l $BS1 |awk '{print $5}'`
PADTMP=$((2048 - $SIZETMP))
$MAKEPAD $PADTMP tmp.pad
cat $BS1 tmp.pad > bs1.page.bin
rm -f tmp.pad
##

## Make block for insn+binfo+BS1
cat insn.page.bin binfo.page.bin bs1.page.bin >ibb.tmp
SIZETMP=`ls -l ibb.tmp |awk '{print $5}'`
PADTMP=$((131072 - $SIZETMP))
$MAKEPAD $PADTMP tmp.pad
cat  ibb.tmp tmp.pad > insnbinfoBS1.block.bin
rm -f tmp.pad

## delete temporaries
rm -f insn.page.bin binfo.page.bin bs1.page.bin ibb.tmp

############ Block3 binary ready#################
############ Block4 binary generation ###########
SIZETMP=`ls -l $BS2 |awk '{print $5}'`
PADTMP=$((131072 - $SIZETMP))
$MAKEPAD $PADTMP tmp.pad
cat  $BS2 tmp.pad > BS2.block.bin
rm -f tmp.pad
############ Block4 binary ready#################

Block3=insnbinfoBS1.block.bin
Block4=BS2.block.bin
############### creation of Nintendo block binaries done###

#### cat blocks for encryption ###############
cat $Block1 $Block2 $Block3 $Block4 > imageForEncryption.bin

########### Encrypt the above image #####
## XXX horti 
cp imageForEncryption.bin ${TMP_DIR}/imageForEncryption.bin

BASE=imageForEncryption

#
# AES encode the big-endian 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
mv ${TMP_DIR}/${BASE}.bin.encrypted.small imageEncrypted.bin

TOTAL_SIZE=`ls -l imageEncrypted.bin |awk '{print $5}'`

echo "XXX Need to encrypt blocks. Doing nothing for now!"
##################################

######### Split into blocks of size 126K, 128K, 128K, 128K

# Get the first piece
split imageEncrypted.bin -b 126k IOSXYZ
mv IOSXYZaa ${TMP_DIR}/block1.nohdr.preflash
cat IOSXYZ* >blocks.tmp
rm -f IOSXYZ*

split blocks.tmp -b 128k IOSABC
mv IOSABCaa ${TMP_DIR}/block2.preflash
mv IOSABCab ${TMP_DIR}/block3.preflash
mv IOSABCac ${TMP_DIR}/block4.preflash

rm blocks.tmp imageEncrypted.bin

echo "Created images to be flashified:"
echo "   block1.nohdr.preflash"
echo "   block2.preflash"
echo "   block3.preflash"
echo "   block4.preflash"
#########################
echo "Size to be put in block1's header = $TOTAL_SIZE"

#
# Block 1
#
BASE=block1.nohdr
echo make flash image from $BASE binary
${TOFLASH} ${TMP_DIR}/${BASE}.preflash ${TMP_DIR}/${BASE}.flash.bin ${FLASH_1_FLAGS} -F $TOTAL_SIZE > ${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
mv ${BASE}.flash.bin.big ${BASE}.flash


#
# Block 2
#
BASE=block2
echo make flash image from $BASE binary
${TOFLASH} ${TMP_DIR}/${BASE}.preflash ${TMP_DIR}/${BASE}.flash.bin ${FLASH_234_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
mv ${BASE}.flash.bin.big ${BASE}.flash


#
# Block 3
#
BASE=block3
echo make flash image from $BASE binary
${TOFLASH} ${TMP_DIR}/${BASE}.preflash ${TMP_DIR}/${BASE}.flash.bin ${FLASH_234_FLAGS} > ${TMP_DIR}/toflash.log
if [ $? -ne 0 ]; then
    echo ${TOFLASH} failed
    exit 1
fi
echo "block3 converted"

#
# 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
mv ${BASE}.flash.bin.big ${BASE}.flash

#
# Block 4
#
BASE=block4
echo make flash image from $BASE binary
${TOFLASH} ${TMP_DIR}/${BASE}.preflash ${TMP_DIR}/${BASE}.flash.bin ${FLASH_234_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
mv ${BASE}.flash.bin.big ${BASE}.flash
mv block1.nohdr.flash block1.flash


## Done
