blob: a676a9bd7891a7b2080cd7b9afddc5736c05b442 [file] [log] [blame]
#!/bin/bash
exec_name=$0
set -o errtrace
trap 'echo Fatal error: script ${exec_name} aborting at line $LINENO, command \"$BASH_COMMAND\" returned $?; exit 1' ERR
NAND=${1:-toshiba}
[ "$FLASH_TYPE" = "" -o "$CUST_SELFKEY_IMAGE" = "" ] && {
source ../../berlin_config/config
if [ -z $CPUPLL ];then
CPUPLL=1000
fi
if [ -z $MEMPLL ];then
MEMPLL=1600
fi
if [ -z $SYSPLL ];then
SYSPLL=800
fi
export PLATFORM
export CPUPLL
export PV_COMP
export NFCECCCLK
export LINUX_ON_CPU1
export CPU_TYPE
export CPU_PLL
export MEMPLL
export SYSPLL
export SYSTEM_CONFIGURATION_PATH
export PLATFORM_INFO
export BUILD_GOOGLETV
export BOOT_TYPE
export RANDOMIZER
export CONFIG_NAND_READ_RETRY
export CONFIG_ENHANCED_SLC
export CONFIG_SLC
export SM_IMAGE_ACTIVE
export SM_IMAGE_PATH
export SM_IMAGE_FORMAT
export CPU0_START_ADDR
export CONFIG_GPT
export DISABLE_CRASH_COUNTER
export BERLIN_CHIP
top_data_dir=../../berlin_config/
export top_data_dir
}
pagesize=`echo $FLASH_TYPE | cut -d _ -f1`
page_per_block=`echo $FLASH_TYPE | cut -d _ -f3`
block_size=`expr $pagesize \* $page_per_block`
block_size_kB=`expr $block_size / 1024`
echo "pagesize=$pagesize , page_per_block=$page_per_block"
echo "block_size_kB=$block_size_kB"
WORKING_DIR=`pwd`
ADDHEADER_TOOL="../Scripts/addheader.sh"
LOCAL_CUST_SELFKEY_PATH=$WORKING_DIR/../../berlin_config/${CUST_SELFKEY_IMAGE}
SYSTEM_INIT_FILE_LOCAL=SI_part1.bin
SI_CUST_FIGO_IMAGE_LOCAL=SI_cust_figo.bin
SM_PATH=$WORKING_DIR/../../berlin_config/$SM_IMAGE_PATH
if [ $BOOT_TYPE = "NAND_BOOT" ]; then
BOOT_DEV=nand
else
BOOT_DEV=emmc
fi
# copy the linux parameters (bootargs)
if [ ! -f ../.temp/linux_param.h ]; then
echo run \"make linux_param\" under MV88DE3100_Tools/ first
exit 1
fi
cp -f ../.temp/linux_param.h .
# check platform info
if [ \( ! -f $top_data_dir/$PLATFORM_INFO \) -a \( ! -L $top_data_dir/$PLATFORM_INFO \) ]; then
echo "Error: Pin setting file $top_data_dir/$PLATFORM_INFO does not exist."
exit 1
fi
cp -f $top_data_dir/$PLATFORM_INFO pin_settings.h ||exit 1
# copy SM
if [ "$SM_IMAGE_FORMAT" = "binary" ]; then
echo "SM image is a binary file"
else
echo "SM image is a $SM_IMAGE_FORMAT file" do not support now
exit 1
fi
cp -f $SM_PATH sm.bin
if [ $CPU_TYPE = "Z1" -o $CPU_TYPE = "Z2" ];then
SYS_INIT_DIR=../sys_init_bg2cdp
else
SYS_INIT_DIR=../sys_init_bg2cdp_a0
fi
SYS_INIT_BIN_FILE=sys_init_${BOOT_DEV}_secure_CPU"$CPUPLL"_MEM"$MEMPLL"_SYS"$SYSPLL"_$CPU_TYPE.bin
[ $# = 1 ] && VT_PATH=$1 || VT_PATH=../.temp/version_table
if [ "$PRODUCTION_CHIP" = "0" ]; then
echo "Generating bootloader for engineer chip"
if [ ! -f ../DRM_Image/customer_keystore_figo.bin_ROM_Key ]; then
echo "../DRM_Image/customer_keystore_figo.bin_ROM_Key not found"
exit 1
fi
DRM_IMAGE_FILE=../DRM_Image/customer_keystore_figo.bin_ROM_Key
else
echo "Generating bootloader for production chip"
if [ ! -f ../DRM_Image/customer_keystore_figo.bin_Dist_Key ]; then
echo "../DRM_Image/customer_keystore_figo.bin_Dist_Key not found"
exit 1
fi
DRM_IMAGE_FILE=../DRM_Image/customer_keystore_figo.bin_Dist_Key
fi
# Copy system init part1 image and cust figo image to local
cp $DRM_IMAGE_FILE $SI_CUST_FIGO_IMAGE_LOCAL
SYS_INIT_BIN_DIR=$SYS_INIT_DIR/$PLATFORM
SYS_INIT_BIN_FILE=${NAND}_sysinit.bin
echo "sys_init binary file: $SYS_INIT_BIN_DIR/$SYS_INIT_BIN_FILE"
cp $SYS_INIT_BIN_DIR/image1/release/bcm_erom.bin init_erom.bin || exit 1
cp $SYS_INIT_BIN_DIR/image1/release/bcm_erom.info init_erom.info || exit 1
cp $SYS_INIT_BIN_DIR/image1/release/drm_erom.bin drm_erom.bin || exit 1
cp $SYS_INIT_BIN_DIR/image1/release/drm_erom.info drm_erom.info|| exit 1
cp $SYS_INIT_BIN_DIR/image2/release/${NAND}_sysinit.bin init_boot.bin || exit 1
cp $SYS_INIT_BIN_DIR/image2/release/${NAND}_sysinit.info init_boot.info || exit 1
cp $SYS_INIT_BIN_DIR/image2/release/tz_loader_en.bin tz_loader_en.bin || exit 1
# Preserve TZ (MPR enabled) version of TZ loader
cp $SYS_INIT_BIN_DIR/image2/release/tz_loader_en_tz.bin tz_loader_en_tz.bin || exit 1
cat init_erom.bin init_boot.bin >$SYSTEM_INIT_FILE_LOCAL
if [ ! -f $VT_PATH ];then
echo "please generate $VT_PATH first."
exit 1
fi
make clean &&\
make configure ASIC=$BERLIN_CHIP CPU_TYPE=$CPU_TYPE CPUPLL=$CPUPLL BOX_PRODUCT_RELEASE=$BOX_PRODUCT_RELEASE BOOT_TYPE=$BOOT_TYPE RANDOMIZER=$RANDOMIZER &&\
make && \
rm -f pin_settings.h || exit 1
TOOL_DIR=$WORKING_DIR/..
SIGN_TOOL=$TOOL_DIR/sign_image
SIGN_TOOL_CMD=$TOOL_DIR/sign_image_cmd.sh
readelf -h bootloader_en.elf | grep "Entry point" >bootloader_en.info
#echo " Entry point address: 0x680000" >bootloader.info
cp -f $SIGN_TOOL $SIGN_TOOL_CMD ../gen_layout.sh ../set_layout.sh . && \
$SIGN_TOOL_CMD $TOOL_DIR -d 0x4 -i bootloader.bin -o bootloader_en.bin -v -g > bootloader.log || {
echo fail to generate bootloader_en.img
exit 1
}
if [ "$ROM_KEY_DISABLE" = "" ];then
echo "ROM_KEY_DISABLE is not set in config file, please update your config file"
exit 1
fi
if [ "$ROM_KEY_DISABLE" = "1" ]; then
echo " ****************** ROM key has been disabled ************"
if [ "$OTP_RSA_KEY_FILE_NAME" = "" ]; then
echo "ROM KEY has been disabled, but OTP_RSA_KEY_FILE_NAME is not defined"
exit 1
fi
if [ ! -f ../../berlin_config/${OTP_RSA_KEY_FILE_NAME} ];then
echo " Can not find User RSA key file: ../../berlin_config/${OTP_RSA_KEY_FILE_NAME} "
exit 1
fi
cp ../../berlin_config/${OTP_RSA_KEY_FILE_NAME} ./
cp ../kms_release_bg2_32/seclab/bin/ChangeSign ./
if [ ! -f ./ChangeSign ];then
echo "ChangeSign doesn't exist"
exit 1
fi
echo " resign system init..."
./ChangeSign $SYSTEM_INIT_FILE_LOCAL 0
echo " resign cust figo image..."
./ChangeSign $SI_CUST_FIGO_IMAGE_LOCAL 0
fi
echo --------------------
echo Generate pre bootloader image
echo --------------------
target=init_loader.img
LAST4K=`expr $block_size - 4096`
#layout: erom, im2, tzl, tsm, version, lastk
#make version 1KiB
dd if=$VT_PATH of=version.bin conv=notrunc && \
SIZE=`stat -c %s version.bin` && \
dd if=random.bin of=version.bin bs=1 seek=$SIZE count=`expr 1024 - $SIZE` conv=notrunc || exit 1
echo " Entry point address: 0x600000" >version.info
#generate filler2
dd if=random.bin of=filler2.bin bs=1024 count=2 || exit 1
#eval tsm filler size
cat init_erom.bin init_boot.bin tz_loader_en.bin > $target || exit 1
s=`stat -c %s $target`
mask=$(( pagesize - 1 ))
d=$((( (s + mask) & ~mask ) - s))
if [ $d -gt 0 ]; then
s=`stat -c %s tz_loader_en.bin`
s_mpr=`stat -c %s tz_loader_en_tz.bin`
if (( $s_mpr != $s ))
then
echo "!!!ERROR: MPR TZ loader size does not match TZ loader"
ls -al tz_loader_en.bin tz_loader_en_tz.bin
exit 1
fi
dd if=random.bin of=tz_loader_en.bin bs=1 seek=$s count=$d conv=notrunc || exit 1
# Pad tz_loader_en_tz with same size padding as TZ loader
dd if=random.bin of=tz_loader_en_tz.bin bs=1 seek=$s count=$d conv=notrunc || exit 1
echo "expand tz_loader_en.bin and tz_loader_en_tz.bin from $s with $d"
fi
#eval filler size
cat init_erom.bin init_boot.bin tz_loader_en.bin drm_erom.bin > $target || exit 1
INIT_LOADER_FINAL_SIZE=`stat -c %s $target`
if [ $INIT_LOADER_FINAL_SIZE -gt $LAST4K ]; then
echo "Error: size of preloader images image size is too big."
exit 1;
fi
head -c `expr $LAST4K - $INIT_LOADER_FINAL_SIZE` random.bin >filler.bin
echo " Entry point address: 0x000000" >filler.info
if [ ! -f tz_loader.elf ]; then
# use last 16M for tz_loader
MEM_SIZE_NUM=${MEMORY_SIZE:0:${#MEMORY_SIZE}-1}
echo " Entry point address: $(printf "0x%08X" $((MEM_SIZE_NUM*1024*1024-0x01000000)))" >tz_loader_en.info
else
readelf -h tz_loader.elf | grep "Entry point" >tz_loader_en.info
fi
readelf -h bootloader.elf | grep "Entry point" >bootloader_en.info
#echo " Entry point address: 0x680000" >bootloader.info
#generate custk store
CUSTK_SDK_STORE=$TOOL_DIR/store/custk_sdk.store
[ -e $CUSTK_SDK_STORE ] || exit 1
## generate lastk from custk_sdk
SIZE=$(stat -c %s $CUSTK_SDK_STORE)
head -c `expr 64 - $SIZE` random.bin > var1
head -c 64 random.bin > var2
cat $CUSTK_SDK_STORE var1 var2 > lastk.bin
SIZE=$(stat -c %s lastk.bin)
dd if=/dev/zero of=lastk.bin bs=1 seek=$SIZE count=`expr 1024 - $SIZE` conv=notrunc
echo " Entry point address: 0x000000" > lastk.info
GEN_LAYOUT=./gen_layout.sh # tool to generate block1 layout data
SET_LAYOUT=./set_layout.sh # tool to inject block1 layout
echo "$WORKING_DIR/init_erom 0x10001" >layout.seq
echo "$WORKING_DIR/init_boot 2" >>layout.seq
echo "$WORKING_DIR/tz_loader_en 3" >>layout.seq
echo "$WORKING_DIR/drm_erom 0x10002" >>layout.seq
echo "$WORKING_DIR/filler 0x100200ff" >>layout.seq
echo "$WORKING_DIR/version 0x10020008" >>layout.seq
echo "$WORKING_DIR/filler2 0x100200ff" >>layout.seq
echo "$WORKING_DIR/lastk 0x10020009" >>layout.seq
$GEN_LAYOUT layout.seq layout.bin
$SET_LAYOUT layout.bin init_erom.bin init_layout.bin
cat init_layout.bin init_boot.bin tz_loader_en.bin drm_erom.bin filler.bin version.bin filler2.bin lastk.bin > init_loader.img || exit 1
#
# Pre bootloader image generation complete
#
#
# Generate post bootloader image for BG2CDP.
#
cat bootloader_en.bin > post-init_loader.img || exit 1
# Save a copy of the post-bootloader prior to padding
cp post-init_loader.img post-bootloader_nopadding.img
cp init_loader.img bootloader_nopadding.img
# Sanity check size of init_loader.img
INIT_LOADER_FINAL_SIZE=`stat -c %s init_loader.img`
if [ $INIT_LOADER_FINAL_SIZE -gt $block_size ]; then
echo "Error: bootloader partition image size is greater than block size."
exit 1;
fi
# expand post-init_loader to align to block_size
target2=post-init_loader.img
cat bootloader_en.bin > $target2 || exit 1
s=`stat -c %s $target2` && \
mask=$(( block_size - 1 ))
d=$((( (s + mask) & ~mask ) - s))
if [ $d -gt 0 ]; then
dd if=random.bin of=$target2 bs=1 seek=$s count=$d conv=notrunc || exit 1
echo "expand $target2 from $s with $d"
fi