1#!/bin/sh 2set -e 3BOARD=$1 4SUBCMD=$1 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=$2 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######################################################################################################### 43help() 44{ 45 echo 46 echo "Usage: ./make.sh [board|subcmd] [O=<dir>]" 47 echo 48 echo "Example:" 49 echo 50 echo "1. Build:" 51 echo " ./make.sh evb-rk3399 ---- build for evb-rk3399_defconfig" 52 echo " ./make.sh evb-rk3399 O=rockdev ---- build for evb-rk3399_defconfig with output dir "./rockdev"" 53 echo " ./make.sh firefly-rk3288 ---- build for firefly-rk3288_defconfig" 54 echo " ./make.sh ---- build with exist .config" 55 echo 56 echo " After build, images of uboot, loader and trust are all generated." 57 echo 58 echo "2. Subcmd helper:" 59 echo " ./make.sh trust --- pack trust.img without rebuild project" 60 echo " ./make.sh loader --- pack loader bin without rebuild project" 61 echo " ./make.sh uboot --- pack uboot.img without rebuild project" 62 echo 63 echo "3. Debug helper:" 64 echo " ./make.sh elf --- dump elf file with -D(default)" 65 echo " ./make.sh elf-S --- dump elf file with -S" 66 echo " ./make.sh map --- cat u-boot.map" 67 echo " ./make.sh sym --- cat u-boot.sym" 68} 69 70prepare() 71{ 72 local absolute_path cmd dir count 73 74 # Parse output directory 75 cmd=${OUTDIR%=*} 76 if [ "${cmd}" = 'O' ]; then 77 OUTDIR=${OUTDIR#*=} 78 OUTOPT=O=${OUTDIR} 79 else 80 case $BOARD in 81 ''|elf*|trust|loader|uboot|map|sym) 82 count=`find -name .config | wc -l` 83 dir=`find -name .config` 84 if [ $count -eq 1 ]; then 85 dir=${dir%/*} 86 OUTDIR=${dir#*/} 87 if [ $OUTDIR != '.' ]; then 88 OUTOPT=O=${OUTDIR} 89 fi 90 elif [ $count -eq 0 ]; then 91 echo 92 echo "Build failed, Can't find .config" 93 help 94 exit 1 95 else 96 echo 97 echo "Build failed, find $count '.config': " 98 echo "$dir" 99 echo "Please leave only one of them" 100 exit 1 101 fi 102 ;; 103 104 *) 105 OUTDIR=. 106 ;; 107 esac 108 fi 109 110 # Parse help and make defconfig 111 case $BOARD in 112 #help 113 --help|-help|help|--h|-h) 114 help 115 exit 0 116 ;; 117 #subcmd 118 ''|elf*|trust|loader|uboot|map|sym) 119 ;; 120 121 *) 122 if [ ! -f configs/${BOARD}_defconfig ]; then 123 echo 124 echo "Can't find: configs/${BOARD}_defconfig" 125 echo 126 echo "******** Rockchip Support List *************" 127 echo "${SUPPORT_LIST}" 128 echo "********************************************" 129 echo 130 exit 1 131 else 132 echo "make for ${BOARD}_defconfig by -j${JOB}" 133 make ${BOARD}_defconfig ${OUTOPT} 134 fi 135 ;; 136 esac 137 138 # Initialize RKBIN and RKTOOLS 139 if [ -d ${RKBIN_TOOLS} ]; then 140 absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd) 141 RKBIN=${absolute_path} 142 RKTOOLS=${absolute_path}/tools 143 else 144 echo 145 echo "Can't find '../rkbin/' repository, please download it before pack image!" 146 echo "How to obtain? 3 ways:" 147 echo " 1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository" 148 echo " 2. Github repository: https://github.com/rockchip-linux/rkbin" 149 echo " 3. Download full release SDK repository" 150 exit 1 151 fi 152} 153 154select_toolchain() 155{ 156 local absolute_path 157 158 if grep -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then 159 if [ -d ${TOOLCHAIN_ARM64} ]; then 160 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd) 161 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64} 162 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64} 163 else 164 echo "Can't find toolchain: ${TOOLCHAIN_ARM64}" 165 exit 1 166 fi 167 else 168 if [ -d ${TOOLCHAIN_ARM32} ]; then 169 absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd) 170 TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32} 171 TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32} 172 else 173 echo "Can't find toolchain: ${TOOLCHAIN_ARM32}" 174 exit 1 175 fi 176 fi 177 178 echo "toolchain: ${TOOLCHAIN_GCC}" 179} 180 181sub_commands() 182{ 183 local cmd=${SUBCMD%-*} elfopt=${SUBCMD#*-} 184 185 case $cmd in 186 elf) 187 if [ ! -f ${OUTDIR}/u-boot ]; then 188 echo "Can't find elf file: ${OUTDIR}/u-boot" 189 exit 1 190 else 191 # default 'cmd' without option, use '-D' 192 if [ "${cmd}" = 'elf' -a "${elfopt}" = 'elf' ]; then 193 elfopt=D 194 fi 195 ${TOOLCHAIN_OBJDUMP} -${elfopt} ${OUTDIR}/u-boot | less 196 exit 0 197 fi 198 ;; 199 200 map) 201 cat ${OUTDIR}/u-boot.map | less 202 exit 0 203 ;; 204 205 sym) 206 cat ${OUTDIR}/u-boot.sym | less 207 exit 0 208 ;; 209 210 trust) 211 pack_trust_image 212 exit 0 213 ;; 214 215 loader) 216 pack_loader_image 217 exit 0 218 ;; 219 220 uboot) 221 pack_uboot_image 222 exit 0 223 ;; 224 225 *) 226 ;; 227 esac 228} 229 230# Support platform special configure 231# 1. fixup chip name; 232# 2. fixup pack mode; 233# 3. fixup image size 234# 4. fixup ARM64 cpu boot with AArch32 235fixup_platform_configure() 236{ 237 local count plat 238 239# <1> Get RKCHIP for searching trust/loader ini files 240 count=`grep -c '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config` 241 RKCHIP=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config` 242 243 if [ $count -eq 1 ]; then 244 RKCHIP=${RKCHIP%=*} 245 RKCHIP=${RKCHIP##*_} 246 elif [ $count -gt 1 ]; then 247 # Is RK3126 ? 248 plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3126=y/p"` 249 if [ "$plat" = 'CONFIG_ROCKCHIP_RK3126=y' ]; then 250 RKCHIP=RK3126 251 fi 252 # Is RK3326 ? 253 plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3326=y/p"` 254 if [ "$plat" = 'CONFIG_ROCKCHIP_RK3326=y' ]; then 255 RKCHIP=RK3326 256 fi 257 else 258 echo "Can't get Rockchip SoC definition in .config" 259 exit 1 260 fi 261 262# <2> Fixup rsa/sha pack mode for platforms 263 # RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3" 264 if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then 265 PLATFORM_RSA="--rsa 3" 266 # RK3368 use rk big endian SHA256, it's pack magic is "2" 267 elif [ $RKCHIP = "RK3368" ]; then 268 PLATFORM_SHA="--sha 2" 269 # other platforms use default configure 270 fi 271 272# <3> Fixup images size pack for platforms 273 if [ $RKCHIP = "RK3308" ]; then 274 if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then 275 PLATFORM_UBOOT_IMG_SIZE="--size 512 2" 276 PLATFORM_TRUST_IMG_SIZE="--size 512 2" 277 else 278 PLATFORM_UBOOT_IMG_SIZE="--size 1024 2" 279 PLATFORM_TRUST_IMG_SIZE="--size 1024 2" 280 fi 281 fi 282 283# <4> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms 284 if [ $RKCHIP = "RK3308" ]; then 285 if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then 286 PLATFORM_AARCH32="AARCH32" 287 fi 288 fi 289} 290 291pack_uboot_image() 292{ 293 local UBOOT_LOAD_ADDR 294 295 UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 296 ${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE} 297 298 # Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img 299 if [ -f ${OUTDIR}/u-boot.img ]; then 300 rm ${OUTDIR}/u-boot.img 301 fi 302 303 if [ -f ${OUTDIR}/u-boot-dtb.img ]; then 304 rm ${OUTDIR}/u-boot-dtb.img 305 fi 306 307 echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin" 308} 309 310pack_loader_image() 311{ 312 local files ini 313 314 if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then 315 echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini" 316 return 317 fi 318 319 cd ${RKBIN} 320 files=`ls ${RKBIN}/RKBOOT/${RKCHIP}MINIALL*.ini` 321 for ini in $files 322 do 323 if [ -f "$ini" ]; then 324 ${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ $ini 325 echo "pack loader okay! Input: $ini" 326 fi 327 done 328 329 cd - && mv ${RKBIN}/*_loader_*.bin ./ 330} 331 332pack_trust_image() 333{ 334 local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000 335 336 # ARM64 uses trust_merger 337 if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then 338 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini ]; then 339 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini" 340 return 341 fi 342 343 cd ${RKBIN} 344 ${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini 345 346 cd - && mv ${RKBIN}/trust.img ./trust.img 347 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini" 348 # ARM uses loaderimage 349 else 350 if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then 351 echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 352 return 353 fi 354 355 # OP-TEE is 132M(0x8400000) offset from DRAM base. 356 DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'` 357 TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET)) 358 359 # Convert Dec to Hex 360 TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc) 361 362 # Parse orignal path 363 TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 364 TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'` 365 366 # replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch 367 TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g") 368 TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g") 369 370 if [ $TOS_TA -a $TOS ]; then 371 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 372 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 373 echo "Both trust.img and trust_with_ta.img are ready" 374 elif [ $TOS ]; then 375 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 376 echo "trust.img is ready" 377 elif [ $TOS_TA ]; then 378 ${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE} 379 echo "trust.img with ta is ready" 380 else 381 echo "Can't find any tee bin" 382 exit 1 383 fi 384 385 echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini" 386 fi 387} 388 389finish() 390{ 391 echo 392 if [ "$BOARD" = '' ]; then 393 echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with exist .config" 394 else 395 echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)" 396 fi 397} 398 399prepare 400select_toolchain 401fixup_platform_configure 402sub_commands 403make CROSS_COMPILE=${TOOLCHAIN_GCC} all --jobs=${JOB} ${OUTOPT} 404pack_uboot_image 405pack_loader_image 406pack_trust_image 407finish 408