1#!/bin/sh 2set -e 3BOARD=$1 4SUBCMD=$2 5RKCHIP=${BOARD##*-} 6RKCHIP=$(echo ${RKCHIP} | tr '[a-z]' '[A-Z]') 7JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l` 8SUPPROT_LIST=`ls configs/*-r[v,k][0-9][0-9][0-9][0-9]_defconfig` 9 10# Declare global default output dir and cmd, update in prepare() 11OUTDIR=. 12OUTOPT= 13 14# Declare global rkbin tools and rkbin Responsity path, updated in prepare() 15TOOLCHAIN_RKBIN=./ 16RKBIN=./ 17# RKTOOL path 18RKBIN_TOOLS=../rkbin/tools 19 20# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain() 21TOOLCHAIN_GCC=./ 22TOOLCHAIN_OBJDUMP=./ 23# GCC toolchain 24GCC_ARM32=arm-linux-gnueabihf- 25GCC_ARM64=aarch64-linux-gnu- 26TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin 27TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 28# OBJDMP 29OBJ_ARM32=arm-linux-gnueabihf-objdump 30OBJ_ARM64=aarch64-linux-gnu-objdump 31 32prepare() 33{ 34 local absolute_path cmd 35 36 # Check invaid args and help 37 if [ "$BOARD" = '--help' -o "$BOARD" = '-h' -o "$BOARD" = '--h' -o "$BOARD" = '' ]; then 38 echo 39 echo "Usage: ./make.sh [board]" 40 echo "Example:" 41 echo "./make.sh evb-rk3399 ---- build for evb-rk3399_defconfig" 42 echo "./make.sh firefly-rk3288 ---- build for firefly-rk3288_defconfig" 43 exit 1 44 elif [ ! -f configs/${BOARD}_defconfig ]; then 45 echo "Can't find: configs/${BOARD}_defconfig" 46 echo 47 echo "*************** Support list ***************" 48 echo "$SUPPROT_LIST" 49 echo "********************************************" 50 echo 51 exit 1 52 fi 53 54 # Initialize RKBIN and TOOLCHAIN_RKBIN 55 if [ -d ${RKBIN_TOOLS} ]; then 56 absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd) 57 RKBIN=${absolute_path} 58 TOOLCHAIN_RKBIN=${absolute_path}/tools 59 else 60 echo 61 echo "Can't find '../rkbin/' Responsity, please download it before pack image!" 62 echo "How to obtain? 3 ways:" 63 echo " 1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" Responsity" 64 echo " 2. Github Responsity: https://github.com/rockchip-linux/rkbin" 65 echo " 3. Download full release SDK Responsity" 66 exit 1 67 fi 68 69 # Assign output directory 70 cmd=${SUBCMD%=*} 71 if [ "${cmd}" = 'O' ]; then 72 OUTDIR=${SUBCMD#*=} 73 OUTOPT=O=${OUTDIR} 74 fi 75} 76 77select_toolchain() 78{ 79 local absolute_path 80 81 if grep -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then 82 if [ -d ${TOOLCHAIN_ARM64} ]; then 83 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd) 84 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64} 85 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64} 86 else 87 echo "Can't find toolchain: ${TOOLCHAIN_ARM64}" 88 exit 1 89 fi 90 else 91 if [ -d ${TOOLCHAIN_ARM32} ]; then 92 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd) 93 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32} 94 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32} 95 else 96 echo "Can't find toolchain: ${TOOLCHAIN_ARM32}" 97 exit 1 98 fi 99 fi 100 101 echo "toolchain: ${TOOLCHAIN_GCC}" 102} 103 104sub_commands() 105{ 106 local elf=${SUBCMD%-*} opt=${SUBCMD#*-} 107 108 if [ "$elf" = 'elf' ]; then 109 if [ ! -f ${OUTDIR}/u-boot ]; then 110 echo "Can't find elf file: ${OUTDIR}/u-boot" 111 exit 1 112 else 113 # default 'elf' without option, use '-D' 114 if [ "${elf}" = 'elf' -a "${opt}" = 'elf' ]; then 115 opt=D 116 fi 117 118 ${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less 119 exit 0 120 fi 121 elif [ "$SUBCMD" = 'trust' ]; then 122 pack_trust_image 123 exit 0 124 elif [ "$SUBCMD" = 'loader' ]; then 125 pack_loader_image 126 exit 0 127 fi 128} 129 130fixup_chip_name() 131{ 132 if [ "$RKCHIP" = 'RK3228' -o "$RKCHIP" = 'RK3229' ]; then 133 RKCHIP=RK322X 134 fi 135} 136 137pack_uboot_image() 138{ 139 local UBOOT_LOAD_ADDR 140 141 UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 142 ${TOOLCHAIN_RKBIN}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} 143 144 if [ -f ${OUTDIR}/u-boot.img ]; then 145 rm ${OUTDIR}/u-boot.img 146 fi 147 148 if [ -f ${OUTDIR}/u-boot-dtb.img ]; then 149 rm ${OUTDIR}/u-boot-dtb.img 150 fi 151 152 echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin" 153} 154 155pack_loader_image() 156{ 157 if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then 158 echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini" 159 return 160 fi 161 162 cd ${RKBIN} 163 ${TOOLCHAIN_RKBIN}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini 164 cd - 165 mv ${RKBIN}/*_loader_*.bin ./ 166 echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini" 167} 168 169pack_trust_image() 170{ 171 local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000 AARCH32 172 173 # ARM64 uses trust_merger 174 if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then 175 if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then 176 AARCH32=AARCH32 177 fi 178 179 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini ]; then 180 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini" 181 return 182 fi 183 184 cd ${RKBIN} 185 186 # RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3" 187 if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then 188 ${TOOLCHAIN_RKBIN}/trust_merger --rsa 3 --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${AARCH32}TRUST.ini 189 # RK3368 use rk big endian SHA256, it's pack magic is "2" 190 elif [ $RKCHIP = "RK3368" ]; then 191 ${TOOLCHAIN_RKBIN}/trust_merger --sha 2 --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${AARCH32}TRUST.ini 192 else 193 ${TOOLCHAIN_RKBIN}/trust_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${AARCH32}TRUST.ini 194 fi 195 196 cd - 197 mv ${RKBIN}/trust.img ./trust.img 198 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}${AARCH32}TRUST.ini" 199 # ARM uses loaderimage 200 else 201 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then 202 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 203 return 204 fi 205 206 # OP-TEE is 132M(0x8400000) offset from DRAM base. 207 DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 208 TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET)) 209 210 # Convert Dec to Hex 211 TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc) 212 213 # Parse orignal path 214 TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 215 TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 216 217 # replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch 218 TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g") 219 TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g") 220 221 if [ $TOS_TA -a $TOS ]; then 222 ${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} 223 ${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} 224 echo "Both trust.img and trust_with_ta.img are ready" 225 elif [ $TOS ]; then 226 ${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} 227 echo "trust.img is ready" 228 elif [ $TOS_TA ]; then 229 ${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} 230 echo "trust.img with ta is ready" 231 else 232 echo "Can't find any tee bin" 233 exit 1 234 fi 235 236 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 237 fi 238} 239 240prepare 241echo "make for ${BOARD}_defconfig by -j${JOB}" 242make ${BOARD}_defconfig ${OUTOPT} 243select_toolchain 244fixup_chip_name 245sub_commands 246make CROSS_COMPILE=${TOOLCHAIN_GCC} all --jobs=${JOB} ${OUTOPT} 247pack_uboot_image 248pack_loader_image 249pack_trust_image 250