bundleBootapp2 2.49 KB
#!/bin/sh

if [ $# -lt 2 ] ; then
    echo "Usage: $0 [options] <SA1rom> <SA2rom>"
    exit -1;
fi

SA1=sa1
SA2=sa2
SA=sa
maxSAblocks=57 # SA should be smaller than 64 - 4(SK) - 1(T1) - 2(BAD) = 57 blocks

# Block Size
BLOCK_SIZE=16384

BOOT_DIR=`dirname $0`
#OPTIONS="-r 0xffffffff -R 0xffffffff"

# Use last two arguments as SA1 and SA2
nargs=$#
SA2rom=${!nargs}
((nargs--))
SA1rom=${!nargs}
((nargs--))

# Use remaining arguments as options to bundleBootApp
while [ $nargs -gt 0 ] ; do
    OPTIONS="${!nargs} $OPTIONS"
    ((nargs--))
done

# tools
BUNDLE_TOOL="$BOOT_DIR/bundleBootapp $OPTIONS"

# Check that SA1rom and SA2rom are valid files
if [ ! -r $SA1rom ] ; then
    echo "ERROR: Cannot read $SA1rom"
    exit -1;
fi

if [ ! -r $SA2rom ] ; then
    echo "ERROR: Cannot read $SA2rom"
    exit -1;
fi


# Bundle SA1
echo "Bundling SA1: $SA1rom with $BUNDLE_TOOL"
if ! $BUNDLE_TOOL $SA1rom $SA1.aes $SA1.tik ; then
    echo "FAILED: $BUNDLE_TOOL $SA1rom $SA1.aes $SA1.tik"
    exit -1
fi

# Create SA2 bundle
echo "Bundling SA2: $SA2rom with $BUNDLE_TOOL"
# GZIP SA2
gzip -c -9 -n $SA2rom | dd bs=10 skip=1 of=$SA2.rom.gz
# Bundle SA2 and encrypt T2
if ! $BUNDLE_TOOL $SA2.rom.gz $SA2.aes $SA2.tik ; then
    echo "FAILED: $BUNDLE_TOOL $SA2.rom.gz $SA2.aes $SA2.tik"
    exit -1
fi
    
# Create SA (Use dd to concatenate SA1.aes, SA2.tik, and SA2.aes together
out=`dd if=$SA1.aes of=$SA.aes bs=$BLOCK_SIZE conv=sync 2>&1 | grep out`;
blocks=`echo $out | awk 'BEGIN {FS="+"} {print $1;}'`
#echo "$SA1.aes copied to $SA.aes, occupying $blocks blocks"
((t2start=blocks))
((sa2start=blocks+1))

#echo "Appending $SA2.tik to $SA.aes at block $t2start"
out=`dd if=$SA2.tik of=$SA.aes bs=$BLOCK_SIZE conv=sync seek=$t2start 2>&1 | grep out`;
blocks=`echo $out | awk 'BEGIN {FS="+"} {print $1;}'`
if [ $blocks -ne 1 ]; then
    echo "ERROR: $SA2.tik is not 1 block but $blocks"
    exit -1
fi

#echo "Appending $SA2.aes to $SA.aes at block $sa2start"
out=`dd if=$SA2.aes of=$SA.aes bs=$BLOCK_SIZE conv=sync seek=$sa2start 2>&1 | grep out`;
blocks=`echo $out | awk 'BEGIN {FS="+"} {print $1;}'`

# Print summary information about the number of blocks for each SA1, SA2, and SA
echo "SA1 is $t2start blocks"
echo "SA2 is $blocks blocks"
((blocks=blocks+sa2start))
echo "SA is $blocks blocks"

# Make a copy of sysapp's ticket
cp $SA1.tik $SA.tik

# Remove temporary files
rm $SA2.rom.gz $SA2.aes $SA2.tik
rm $SA1.aes $SA1.tik

if [ $blocks -gt $maxSAblocks ]; then
    echo "WARNING: SA needs to be $maxSAblocks blocks or smaller"
    exit -2
fi