1#!/bin/sh 2set -e 3BOARD=$1 4SUBCMD=$2 5JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l` 6SUPPORT_LIST=`ls configs/*-[r,p][x,v,k][0-9][0-9]*_defconfig` 7 8########################################### User can modify ############################################# 9# User's rkbin tool relative path 10RKBIN_TOOLS=../rkbin/tools 11 12# User's GCC toolchain and relative path 13OBJ_ARM32=arm-linux-gnueabihf-objdump 14OBJ_ARM64=aarch64-linux-gnu-objdump 15GCC_ARM32=arm-linux-gnueabihf- 16GCC_ARM64=aarch64-linux-gnu- 17TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin 18TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 19 20########################################### User not touch ############################################# 21# Declare global INI file searching index name for every chip, update in fixup_platform_configure() 22RKCHIP= 23 24# Declare global rkbin RKTOOLS and rkbin repository path, updated in prepare() 25RKTOOLS= 26RKBIN= 27 28# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain() 29TOOLCHAIN_GCC= 30TOOLCHAIN_OBJDUMP= 31 32# Declare global default output dir and cmd, update in prepare() 33OUTDIR= 34OUTOPT= 35 36# Declare global plaform configure, updated in fixup_platform_configure() 37PLATFORM_RSA= 38PLATFORM_SHA= 39PLATFORM_UBOOT_IMG_SIZE= 40PLATFORM_TRUST_IMG_SIZE= 41PLATFORM_AARCH32= 42######################################################################################################### 43 44prepare() 45{ 46 local absolute_path cmd 47 48 # Assign output directory 49 cmd=${SUBCMD%=*} 50 if [ "${cmd}" = 'O' ]; then 51 OUTDIR=${SUBCMD#*=} 52 OUTOPT=O=${OUTDIR} 53 else 54 OUTDIR=. 55 fi 56 57 # Check invalid args and help 58 if [ "$BOARD" = '--help' -o "$BOARD" = '-help' -o "$BOARD" = 'help' -o "$BOARD" = '-h' -o "$BOARD" = '--h' ]; then 59 echo 60 echo "Usage: ./make.sh [board]" 61 echo "Example:" 62 echo "./make.sh ---- build with exist .config" 63 echo "./make.sh evb-rk3399 ---- build for evb-rk3399_defconfig" 64 echo "./make.sh firefly-rk3288 ---- build for firefly-rk3288_defconfig" 65 exit 1 66 elif [ $BOARD ] && [ ! -f configs/${BOARD}_defconfig ]; then 67 echo 68 echo "Can't find: configs/${BOARD}_defconfig" 69 echo 70 echo "******** Rockchip Support List *************" 71 echo "${SUPPORT_LIST}" 72 echo "********************************************" 73 echo 74 exit 1 75 fi 76 77 # Initialize RKBIN and RKTOOLS 78 if [ -d ${RKBIN_TOOLS} ]; then 79 absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd) 80 RKBIN=${absolute_path} 81 RKTOOLS=${absolute_path}/tools 82 else 83 echo 84 echo "Can't find '../rkbin/' repository, please download it before pack image!" 85 echo "How to obtain? 3 ways:" 86 echo " 1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository" 87 echo " 2. Github repository: https://github.com/rockchip-linux/rkbin" 88 echo " 3. Download full release SDK repository" 89 exit 1 90 fi 91} 92 93make_defconfig() 94{ 95 if [ $BOARD ]; then 96 echo "make for ${BOARD}_defconfig by -j${JOB}" 97 make ${BOARD}_defconfig ${OUTOPT} 98 fi 99} 100 101select_toolchain() 102{ 103 local absolute_path 104 105 if grep -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then 106 if [ -d ${TOOLCHAIN_ARM64} ]; then 107 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd) 108 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64} 109 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64} 110 else 111 echo "Can't find toolchain: ${TOOLCHAIN_ARM64}" 112 exit 1 113 fi 114 else 115 if [ -d ${TOOLCHAIN_ARM32} ]; then 116 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd) 117 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32} 118 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32} 119 else 120 echo "Can't find toolchain: ${TOOLCHAIN_ARM32}" 121 exit 1 122 fi 123 fi 124 125 echo "toolchain: ${TOOLCHAIN_GCC}" 126} 127 128# Support subcmd: 129# ./make.sh evb-rk3288 elf --- dump elf file with -D(default) 130# ./make.sh evb-rk3288 elf-S --- dump elf file with -S 131# ./make.sh evb-rk3288 trust --- pack trust.img without compile u-boot 132# ./make.sh evb-rk3288 loader --- pack loader bin without compile u-boot 133# ./make.sh evb-rk3288 uboot --- pack uboot.img without compile u-boot 134sub_commands() 135{ 136 local elf=${SUBCMD%-*} opt=${SUBCMD#*-} 137 138 if [ "$elf" = 'elf' ]; then 139 if [ ! -f ${OUTDIR}/u-boot ]; then 140 echo "Can't find elf file: ${OUTDIR}/u-boot" 141 exit 1 142 else 143 # default 'elf' without option, use '-D' 144 if [ "${elf}" = 'elf' -a "${opt}" = 'elf' ]; then 145 opt=D 146 fi 147 148 ${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less 149 exit 0 150 fi 151 elif [ "$SUBCMD" = 'trust' ]; then 152 pack_trust_image 153 exit 0 154 elif [ "$SUBCMD" = 'loader' ]; then 155 pack_loader_image 156 exit 0 157 elif [ "$SUBCMD" = 'uboot' ]; then 158 pack_uboot_image 159 exit 0 160 fi 161} 162 163# Support platform special configure 164# 1. fixup chip name; 165# 2. fixup pack mode; 166# 3. fixup image size 167# 4. fixup ARM64 cpu boot with AArch32 168fixup_platform_configure() 169{ 170 local count plat 171 172# <1> Get RKCHIP for searching trust/loader ini files 173 count=`grep -c '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config` 174 RKCHIP=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config` 175 176 if [ $count -eq 1 ]; then 177 RKCHIP=${RKCHIP%=*} 178 RKCHIP=${RKCHIP##*_} 179 elif [ $count -gt 1 ]; then 180 # Is RK3126 ? 181 plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3126=y/p"` 182 if [ "$plat" = 'CONFIG_ROCKCHIP_RK3126=y' ]; then 183 RKCHIP=RK3126 184 fi 185 # Is RK3326 ? 186 plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3326=y/p"` 187 if [ "$plat" = 'CONFIG_ROCKCHIP_RK3326=y' ]; then 188 RKCHIP=RK3326 189 fi 190 else 191 echo "Can't get Rockchip SoC definition in .config" 192 exit 1 193 fi 194 195# <2> Fixup rsa/sha pack mode for platforms 196 # RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3" 197 if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then 198 PLATFORM_RSA="--rsa 3" 199 # RK3368 use rk big endian SHA256, it's pack magic is "2" 200 elif [ $RKCHIP = "RK3368" ]; then 201 PLATFORM_SHA="--sha 2" 202 # other platforms use default configure 203 fi 204 205# <3> Fixup images size pack for platforms 206 if [ $RKCHIP = "RK3308" ]; then 207 if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then 208 PLATFORM_UBOOT_IMG_SIZE="--size 512 2" 209 PLATFORM_TRUST_IMG_SIZE="--size 512 2" 210 else 211 PLATFORM_UBOOT_IMG_SIZE="--size 1024 2" 212 PLATFORM_TRUST_IMG_SIZE="--size 1024 2" 213 fi 214 fi 215 216# <4> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms 217 if [ $RKCHIP = "RK3308" ]; then 218 if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then 219 PLATFORM_AARCH32="AARCH32" 220 fi 221 fi 222} 223 224pack_uboot_image() 225{ 226 local UBOOT_LOAD_ADDR 227 228 UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 229 ${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE} 230 231 # Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img 232 if [ -f ${OUTDIR}/u-boot.img ]; then 233 rm ${OUTDIR}/u-boot.img 234 fi 235 236 if [ -f ${OUTDIR}/u-boot-dtb.img ]; then 237 rm ${OUTDIR}/u-boot-dtb.img 238 fi 239 240 echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin" 241} 242 243pack_loader_image() 244{ 245 if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then 246 echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini" 247 return 248 fi 249 250 cd ${RKBIN} 251 ${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini 252 cd - && mv ${RKBIN}/*_loader_*.bin ./ 253 echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini" 254} 255 256pack_trust_image() 257{ 258 local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000 259 260 # ARM64 uses trust_merger 261 if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then 262 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini ]; then 263 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini" 264 return 265 fi 266 267 cd ${RKBIN} 268 ${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini 269 270 cd - && mv ${RKBIN}/trust.img ./trust.img 271 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini" 272 # ARM uses loaderimage 273 else 274 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then 275 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 276 return 277 fi 278 279 # OP-TEE is 132M(0x8400000) offset from DRAM base. 280 DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 281 TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET)) 282 283 # Convert Dec to Hex 284 TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc) 285 286 # Parse orignal path 287 TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 288 TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 289 290 # replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch 291 TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g") 292 TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g") 293 294 if [ $TOS_TA -a $TOS ]; then 295 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 296 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 297 echo "Both trust.img and trust_with_ta.img are ready" 298 elif [ $TOS ]; then 299 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 300 echo "trust.img is ready" 301 elif [ $TOS_TA ]; then 302 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 303 echo "trust.img with ta is ready" 304 else 305 echo "Can't find any tee bin" 306 exit 1 307 fi 308 309 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 310 fi 311} 312 313finish() 314{ 315 echo 316 if [ "$BOARD" = '' ]; then 317 echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with exist .config" 318 else 319 echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)" 320 fi 321} 322 323prepare 324make_defconfig 325select_toolchain 326fixup_platform_configure 327sub_commands 328make CROSS_COMPILE=${TOOLCHAIN_GCC} all --jobs=${JOB} ${OUTOPT} 329pack_uboot_image 330pack_loader_image 331pack_trust_image 332finish 333