testsk 6.73 KB
#!/bin/sh

SKDIR=`pwd`


#
# general testing params
#

OBJCOPY=mips-linux-objcopy
OBJCOPY_REMOVE_SECTS="-R .reginfo -R .note -R .comment -R .mdebug"

# tools
HOST_TOOL_DIR=$ROOT/usr/sbin
HOST_DATA_DIR=$ROOT/usr/host_data
TICKROM=$HOST_TOOL_DIR/tickrom
PUBROM=$HOST_TOOL_DIR/pubrom
WRITE_FLASH_TOOL=$HOST_TOOL_DIR/writeflash
V2_TOOL2=$HOST_TOOL_DIR/v2Create
SIMOS_BIN=$HOST_TOOL_DIR/simos

# values, with defaults, that may be altered by env settings
bbid_local= bbprivkey_local=
if [[ -z $BBID ]]; then
    bbid_local=1
    BBID="1"
fi
if [[ -z $BB_PRIV_KEY ]]; then
    bbprivkey_local=1
    BB_PRIV_KEY="a81902767e25db170f3449c5d94b162fa81902767e25db170f3449c5d94b162f"
fi
if [[ -z $BB_ROM_HW_RIGHTS ]]; then
    BB_ROM_HW_RIGHTS="-r 0xffffffff" 
fi
if [[ -z $BB_ROM_SW_RIGHTS ]]; then
    BB_ROM_SW_RIGHTS="-R 0"
fi
if [[ -z $BB_CERTS_FILE ]]; then
    BB_CERTS_FILE=$HOST_DATA_DIR/cert.sys
fi
if [[ -z $BB_ROM_RECRYPT ]]; then
    BB_ROM_RECRYPT=-E
fi
if [[ -z $BB_CRL_FILE ]]; then
    # placeholder for clarity
    BB_CRL_FILE=$HOST_DATA_DIR/crl.sys
fi

# generated in this script
BB_ECCCERT=ecccert.bin
BB_CERT=cert.bin
V2_CONSTFILE=$HOST_DATA_DIR/virage2.in
V2_TMPFILE=v2.bin

# specify sysapp to use
SYSAPP_DIR=../../bbapps/boot/sysapp
SYSAPP_AES=$SYSAPP_DIR/sysapp.aes
SYSAPP_TIK=$SYSAPP_DIR/sysapp.tik

# packaged rom cid and fs name
SYSAPP_LOADED_ROM_CID=3150822347
SYSAPP_LOADED_ROM_AES=bbcdbbcb.app

# to test server integration
SERVER_TEST_DIR=~/source/bb/server
SERVER_SK_AES=$SERVER_TEST_DIR/sk.aes
SERVER_SYSAPP_AES=$SERVER_TEST_DIR/sysapp.aes
SERVER_SYSAPP_TIK=$SERVER_TEST_DIR/sysapp.tik
SERVER_TIK_FILE=$SERVER_TEST_DIR/ticket.sys 
SERVER_CERTS_FILE=$SERVER_TEST_DIR/cert.sys 
SERVER_GAME_FILE=$SERVER_TEST_DIR/00000002.app

# support SIMOS-based testing
TMPFILE=j.j
ROM_ELF=../../lib/bbboot/final/rom.elf
WRITE_FLASH_OUT=flash.img
# size for flashimg in MB
WRITE_FLASH_FSIZE=64


#
# general purpose functions to support simulations
#

function build_v2 {
    # build v2 packaged as ascii hex
    $V2_TOOL2 -b $BBID -p $BB_PRIV_KEY -e $BB_ECCCERT -c $BB_CERT $V2_CONSTFILE $V2_TMPFILE
	
    # XXX: this make depends on V2_TMPFILE, above, being v2.bin
    if ! make v2.hex
    then
	echo "failed to convert virage 2 to hex"
	exit 1
    fi
}


# package a prebuilt rom that will be loaded by the sysapp. creates
# cmd and eticket. takes an args: 
#      first) full pathname of game rom (.bin file) to be packaged
#  remainder) args to make used to build testapp 
function package_rom {

    # package and generate cmd
    if ! $PUBROM $BB_ROM_RECRYPT $BB_ROM_SW_RIGHTS $BB_ROM_HW_RIGHTS $1 \
	 $SYSAPP_LOADED_ROM_CID
    then
	echo "failed to package sysapp-loadable game rom and create cmd"
        exit 1
    fi

    # generate the ticket

    # for now, only one ticket per file
    rm -f ticket.sys

    if ! $TICKROM -c $BB_ECCCERT $SYSAPP_LOADED_ROM_CID ticket.sys
    then
        echo "failed to create rom eticket"
	exit 1
    fi

}


# create a nand file with sk, sysapp "ticket" block, and sysapp.
# args are regular fs files to be loaded to fs.
function create_bootable_nand_file {
    # create module nand-flash image
    if ! $WRITE_FLASH_TOOL -s $WRITE_FLASH_FSIZE -k sk.aes \
            -b $SYSAPP_AES -l $SYSAPP_TIK -v $@
    then
	echo "failed to create module nand flash image"
        exit 1
    fi
}


# create a nand file with sk, sysapp "ticket" block, and sysapp.
# args are regular fs files to be loaded to fs.
function create_server_nand_file {
    # create module nand-flash image
    if ! $WRITE_FLASH_TOOL -s $WRITE_FLASH_FSIZE -k $SERVER_SK_AES \
            -b $SERVER_SYSAPP_AES -l $SERVER_SYSAPP_TIK -v $@
    then
	echo "failed to create module nand flash image"
        exit 1
    fi
}

function build_simos_bootrom
{
    # build romsimos.elf loadable by simos
    $OBJCOPY -O binary -S $OBJCOPY_REMOVE_SECTS $ROM_ELF rom.bin
    $OBJCOPY -j .bss --add-section .text=./rom.bin $ROM_ELF $TMPFILE
    $OBJCOPY --set-section-flags .text=alloc,load,readonly,code \
		--adjust-section-vma .text=0xbfc00000 $TMPFILE \
		romsimos.elf
}

function run_simos
{
    # run simos
    export SIMOS_FLASH=$WRITE_FLASH_OUT; 
    export SIMOS_VIRAGE2=v2.hex;
    echo r | $SIMOS_BIN
}


#
# functions to drive the specific simulation
#


# The first arg must be the rom binary (full path), while the remainder
# are consistent with what package_loadapp expects.
function build_appload {
    build_v2
    build_simos_bootrom
    package_rom $1
    if [[ -a $BB_CRL_FILE ]]; then
        cp $BB_CRL_FILE /tmp/crl.sys
        create_bootable_nand_file ticket.sys $BB_CERTS_FILE \
             $SYSAPP_LOADED_ROM_AES /tmp/crl.sys
	rm -f /tmp/crl.sys
    else
        create_bootable_nand_file ticket.sys $BB_CERTS_FILE $SYSAPP_LOADED_ROM_AES
    fi
}


# used to simulate with server generated content and secure bundle.
#
function build_server_test {
    build_v2
    build_simos_bootrom
    create_server_nand_file $SERVER_TIK_FILE $SERVER_CERTS_FILE \
        $SERVER_GAME_FILE
}

function cleanup {
    make clobber
    rm -f $V2_TMPFILE romsimos.elf rom.bin $WRITE_FLASH_OUT \
       $BB_CERT $BB_ECCCERT $TMPFILE
}

function usage {
    echo "testsk [-s] [-S] [-m] <build_type>"
    echo "   where -s runs simos after building,"
    echo "   -S builds with the server PKI,"
    echo "   -m build for dram memory debug output, and"
    echo "   build_type may be:"
    echo "     appload clean."
    echo "  NOTE: rtl runs the rtl simulator, so -s"
    echo "   should not be used for this build."
    exit 1
}

#
#  Entry for script processing
#

simos_sim=false
dram_log=false
server_pki=false

while getopts "smS" a
do
   case $a in
      s) simos_sim=true;;
      m) dram_log=true;;
      S) server_pki=true;;
      *) usage;;
   esac
done

shift $(($OPTIND - 1))

if (($# < 1)); then
    echo "build_type argument is mandatory."
    usage
    exit 1
fi


SK_DRAM_LOG="SK_LOG_RDB=1"
if [[ $dram_log = true ]]; then
    SK_DRAM_LOG="SK_LOG_MEM=1"
fi

SK_SERVER_PKI=
if [[ $server_pki = true ]]; then
    SK_SERVER_PKI="SK_SERVER_PKI=1"
fi

# other args that may be included for SK_MAKE_ARGS:
#  DEBUG=1
#  NO_ECC_KEYGEN=1
SK_MAKE_ARGS="$SK_DRAM_LOG $SK_SERVER_PKI"


if [[ $1 = appload ]]; then
    cleanup
    make exports $SK_MAKE_ARGS || (echo "failed to build SK lib" && exit 1)
    # $2, below, should be full path to rom we're going to load
    build_appload $2
elif [[ $1 = server ]]; then
    # precede with content pub ritual on server side to produce 
    # sk/sysapp and content. set SERVER_ defines accordingly.
    if [[ -n $bbid_local || -n $bbprivkey_local ]]; then
	echo Must set env BBID and BB_PRIV_KEY to match server target BB
	exit 1
    fi
    build_server_test
elif [[ $1 = clean ]]; then
    cleanup
else
    echo "must supply an arg: simos, rtl, clean\n"
    exit 1
fi

if [[ $simos_sim = true ]]; then
    run_simos
fi