1#!/bin/bash 2# 3# Copyright (c) 2020 Fuzhou Rockchip Electronics Co., Ltd 4# 5# SPDX-License-Identifier: GPL-2.0 6# 7set -e 8 9FIT_DIR="fit" 10IMG_UBOOT="uboot.img" 11IMG_BOOT="boot.img" 12ITB_UBOOT="${FIT_DIR}/uboot.itb" 13ITB_BOOT="${FIT_DIR}/boot.itb" 14SIG_BIN="data2sign.bin" 15SIG_UBOOT="${FIT_DIR}/uboot.data2sign" 16SIG_BOOT="${FIT_DIR}/boot.data2sign" 17# offs 18OFFS_NS_UBOOT="0xc00" 19OFFS_S_UBOOT="0xc00" 20OFFS_NS_BOOT="0x800" 21OFFS_S_BOOT="0xc00" 22# file 23CHIP_FILE="arch/arm/lib/.asm-offsets.s.cmd" 24# placeholder address 25FDT_ADDR_PLACEHOLDER="0xffffff00" 26KERNEL_ADDR_PLACEHOLDER="0xffffff01" 27RAMDISK_ADDR_PLACEHOLDER="0xffffff02" 28# tools 29MKIMAGE="./tools/mkimage" 30FIT_UNPACK="./scripts/fit-unpack.sh" 31CHECK_SIGN="./tools/fit_check_sign" 32# key 33KEY_DIR="keys/" 34RSA_PRI_KEY="keys/dev.key" 35RSA_PUB_KEY="keys/dev.crt" 36SIGNATURE_KEY_NODE="/signature/key-dev" 37SPL_DTB="spl/u-boot-spl.dtb" 38UBOOT_DTB="u-boot.dtb" 39# its 40ITS_UBOOT="u-boot.its" 41ITS_BOOT="boot.its" 42ARG_VER_UBOOT="0" 43ARG_VER_BOOT="0" 44 45function help() 46{ 47 echo 48 echo "usage:" 49 echo " $0 [args]" 50 echo 51 echo "args:" 52 echo " --rollback-index-boot <decimal integer>" 53 echo " --rollback-index-uboot <decimal integer>" 54 echo " --version-uboot <decimal integer>" 55 echo " --version-boot <decimal integer>" 56 echo " --ini-trust" 57 echo " --ini-loader" 58 echo " --no-check" 59 echo " --spl-new" 60 echo " --boot_img" 61 echo " --args" 62 echo 63} 64 65function arg_check_decimal() 66{ 67 if [ -z $1 ]; then 68 help 69 exit 1 70 fi 71 72 decimal=`echo $1 |sed 's/[0-9]//g'` 73 if [ ! -z ${decimal} ]; then 74 echo "ERROR: $1 is not decimal integer" 75 help 76 exit 1 77 fi 78} 79 80function check_its() 81{ 82 cat $1 | while read line 83 do 84 file=`echo ${line} | sed -n "/incbin/p" | awk -F '"' '{ printf $2 }' | tr -d ' '` 85 if [ ! -f ${file} ]; then 86 echo "ERROR: No ${file}" 87 exit 1 88 fi 89 done 90} 91 92function validate_arg() 93{ 94 case $1 in 95 --no-check|--spl-new|--burn-key-hash) 96 shift=1 97 ;; 98 --ini-trust|--ini-loader|--rollback-index-boot|--rollback-index-uboot|--boot_img|--version-uboot|--version-boot) 99 shift=2 100 ;; 101 *) 102 shift=0 103 ;; 104 esac 105 echo ${shift} 106} 107 108function fit_process_args() 109{ 110 if [ $# -eq 0 ]; then 111 help 112 exit 0 113 fi 114 115 while [ $# -gt 0 ]; do 116 case $1 in 117 --args) 118 ARG_VALIDATE=$2 119 shift 2 120 ;; 121 --boot_img) # boot.img 122 ARG_BOOT_IMG=$2 123 shift 2 124 ;; 125 --boot_img_dir) # boot.img components directory 126 ARG_BOOT_IMG_DIR=$2 127 shift 2 128 ;; 129 --no-check) # No hostcc fit signature check 130 ARG_NO_CHECK="y" 131 shift 1 132 ;; 133 --ini-trust) # Assign trust ini file 134 ARG_INI_TRUST=$2 135 shift 2 136 ;; 137 --ini-loader) # Assign loader ini file 138 ARG_INI_LOADER=$2 139 shift 2 140 ;; 141 --spl-new) # Use current build u-boot-spl.bin to pack loader 142 ARG_SPL_NEW="y" 143 shift 1 144 ;; 145 --rollback-index-boot) 146 ARG_ROLLBACK_IDX_BOOT=$2 147 arg_check_decimal $2 148 shift 2 149 ;; 150 --rollback-index-uboot) 151 ARG_ROLLBACK_IDX_UBOOT=$2 152 arg_check_decimal $2 153 shift 2 154 ;; 155 --version-uboot) 156 ARG_VER_UBOOT=$2 157 arg_check_decimal $2 158 shift 2 159 ;; 160 --version-boot) 161 ARG_VER_BOOT=$2 162 arg_check_decimal $2 163 shift 2 164 ;; 165 --burn-key-hash) 166 ARG_BURN_KEY_HASH="y" 167 shift 1 168 ;; 169 *) 170 help 171 exit 1 172 ;; 173 esac 174 done 175 176 if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then 177 ARG_SIGN="y" 178 fi 179} 180 181function fit_raw_compile() 182{ 183 # Verified-boot: should rebuild code but don't need to repack images. 184 if [ "${ARG_SIGN}" == "y" ]; then 185 ./make.sh --raw-compile 186 fi 187 rm ${FIT_DIR} -rf && mkdir -p ${FIT_DIR} 188} 189 190function fit_gen_uboot_itb() 191{ 192 ./make.sh itb ${ARG_INI_TRUST} 193 check_its ${ITS_UBOOT} 194 195 if [ "${ARG_SIGN}" != "y" ]; then 196 ${MKIMAGE} -f ${ITS_UBOOT} -E -p ${OFFS_NS_UBOOT} ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 197 if [ "${ARG_SPL_NEW}" == "y" ]; then 198 ./make.sh --spl ${ARG_INI_LOADER} 199 echo "pack loader with new: spl/u-boot-spl.bin" 200 else 201 ./make.sh loader ${ARG_INI_LOADER} 202 fi 203 else 204 if [ ! -f ${RSA_PRI_KEY} ]; then 205 echo "ERROR: No ${RSA_PRI_KEY} " 206 exit 1 207 elif [ ! -f ${RSA_PUB_KEY} ]; then 208 echo "ERROR: No ${RSA_PUB_KEY} " 209 exit 1 210 fi 211 212 if ! grep -q '^CONFIG_SPL_FIT_SIGNATURE=y' .config ; then 213 echo "ERROR: CONFIG_SPL_FIT_SIGNATURE is disabled" 214 exit 1 215 fi 216 217 # rollback-index 218 if grep -q '^CONFIG_SPL_FIT_ROLLBACK_PROTECT=y' .config ; then 219 ARG_SPL_ROLLBACK_PROTECT="y" 220 if [ -z ${ARG_ROLLBACK_IDX_UBOOT} ]; then 221 echo "ERROR: No arg \"--rollback-index-uboot <n>\"" 222 exit 1 223 fi 224 fi 225 226 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 227 VERSION=`grep 'rollback-index' ${ITS_UBOOT} | awk -F '=' '{ printf $2 }' | tr -d ' '` 228 sed -i "s/rollback-index = ${VERSION}/rollback-index = <${ARG_ROLLBACK_IDX_UBOOT}>;/g" ${ITS_UBOOT} 229 fi 230 231 # u-boot.dtb must contains rsa key 232 if ! fdtget -l ${UBOOT_DTB} /signature >/dev/null 2>&1 ; then 233 ${MKIMAGE} -f ${ITS_UBOOT} -k ${KEY_DIR} -K ${UBOOT_DTB} -E -p ${OFFS_S_UBOOT} -r ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 234 echo "## Adding RSA public key into ${UBOOT_DTB}" 235 fi 236 237 # Pack 238 ${MKIMAGE} -f ${ITS_UBOOT} -k ${KEY_DIR} -K ${SPL_DTB} -E -p ${OFFS_S_UBOOT} -r ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 239 mv ${SIG_BIN} ${SIG_UBOOT} 240 241 # burn-key-hash 242 if [ "${ARG_BURN_KEY_HASH}" == "y" ]; then 243 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} burn-key-hash 0x1 244 fi 245 # rollback-index read back check 246 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 247 VERSION=`fdtget -ti ${ITB_UBOOT} /configurations/conf rollback-index` 248 if [ "${VERSION}" != "${ARG_ROLLBACK_IDX_UBOOT}" ]; then 249 echo "ERROR: Failed to set rollback-index for ${ITB_UBOOT}"; 250 exit 1 251 fi 252 fi 253 254 # burn-key-hash read back check 255 if [ "${ARG_BURN_KEY_HASH}" == "y" ]; then 256 if [ "`fdtget -ti ${SPL_DTB} ${SIGNATURE_KEY_NODE} burn-key-hash`" != "1" ]; then 257 echo "ERROR: Failed to set burn-key-hash for ${SPL_DTB}"; 258 exit 1 259 fi 260 fi 261 262 # host check signature 263 if [ "${ARG_NO_CHECK}" != "y" ]; then 264 if [ "${ARG_SPL_NEW}" == "y" ]; then 265 ${CHECK_SIGN} -f ${ITB_UBOOT} -k ${SPL_DTB} -s 266 else 267 spl_file="../rkbin/"`sed -n "/FlashBoot=/s/FlashBoot=//p" ${ARG_INI_LOADER} |tr -d '\r'` 268 offs=`fdtdump -s ${spl_file} | head -1 | awk -F ":" '{ print $2 }' | sed "s/ found fdt at offset //g" | tr -d " "` 269 if [ -z ${offs} ]; then 270 echo "ERROR: invalid ${spl_file} , unable to find fdt blob" 271 fi 272 offs=`printf %d ${offs} ` # hex -> dec 273 dd if=${spl_file} of=spl/u-boot-spl-old.dtb bs=${offs} skip=1 >/dev/null 2>&1 274 ${CHECK_SIGN} -f ${ITB_UBOOT} -k spl/u-boot-spl-old.dtb -s 275 fi 276 fi 277 278 # minimize u-boot-spl.dtb 279 if grep -q '^CONFIG_SPL_FIT_HW_CRYPTO=y' .config ; then 280 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,r-squared 0x0 281 if grep -q '^CONFIG_SPL_ROCKCHIP_CRYPTO_V1=y' .config ; then 282 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 283 fdtput -r ${SPL_DTB} ${SIGNATURE_KEY_NODE}/hash@np 284 else 285 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 286 fdtput -r ${SPL_DTB} ${SIGNATURE_KEY_NODE}/hash@c 287 fi 288 else 289 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 290 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 291 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,exponent-BN 0x0 292 fdtput -r ${SPL_DTB} ${SIGNATURE_KEY_NODE}/hash@c 293 fdtput -r ${SPL_DTB} ${SIGNATURE_KEY_NODE}/hash@np 294 fi 295 296 # repack spl 297 rm -f *_loader_*.bin 298 if [ "${ARG_SPL_NEW}" == "y" ]; then 299 cat spl/u-boot-spl-nodtb.bin > spl/u-boot-spl.bin 300 if ! grep -q '^CONFIG_SPL_SEPARATE_BSS=y' .config ; then 301 cat spl/u-boot-spl-pad.bin >> spl/u-boot-spl.bin 302 fi 303 cat ${SPL_DTB} >> spl/u-boot-spl.bin 304 305 ./make.sh --spl ${ARG_INI_LOADER} 306 echo "## pack loader with new: spl/u-boot-spl.bin" 307 else 308 ./make.sh loader ${ARG_INI_LOADER} 309 fi 310 311 if [ "${ARG_BURN_KEY_HASH}" == "y" ]; then 312 echo "## ${SPL_DTB}: burn-key-hash=1" 313 fi 314 fi 315 316 rm -f u-boot.itb u-boot.img u-boot-dtb.img 317 mv ${ITS_UBOOT} ${FIT_DIR} 318} 319 320function fit_gen_boot_itb() 321{ 322 if [ ! -z ${ARG_BOOT_IMG} ]; then 323 ${FIT_UNPACK} -f ${ARG_BOOT_IMG} -o ${FIT_DIR}/unpack 324 ITS_BOOT="${FIT_DIR}/unpack/image.its" 325 else 326 compression=`awk -F"," '/COMPRESSION=/ { printf $1 }' ${ARG_INI_TRUST} | tr -d ' ' | cut -c 13-` 327 if [ -z "${compression}" ]; then 328 compression="none" 329 fi 330 ./arch/arm/mach-rockchip/make_fit_boot.sh -c ${compression} > ${ITS_BOOT} 331 check_its ${ITS_BOOT} 332 fi 333 334 if [ "${ARG_SIGN}" != "y" ]; then 335 ${MKIMAGE} -f ${ITS_BOOT} -E -p ${OFFS_NS_BOOT} ${ITB_BOOT} -v ${ARG_VER_BOOT} 336 else 337 if [ ! -f ${RSA_PRI_KEY} ]; then 338 echo "ERROR: No ${RSA_PRI_KEY}" 339 exit 1 340 elif [ ! -f ${RSA_PUB_KEY} ]; then 341 echo "ERROR: No ${RSA_PUB_KEY}" 342 exit 1 343 fi 344 345 if ! grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then 346 echo "ERROR: CONFIG_FIT_SIGNATURE is disabled" 347 exit 1 348 fi 349 350 if grep -q '^CONFIG_FIT_ROLLBACK_PROTECT=y' .config ; then 351 ARG_ROLLBACK_PROTECT="y" 352 if [ -z ${ARG_ROLLBACK_IDX_BOOT} ]; then 353 echo "ERROR: No arg \"--rollback-index-boot <n>\"" 354 exit 1 355 fi 356 fi 357 358 # fixup 359 COMMON_FILE=`sed -n "/_common.h/p" ${CHIP_FILE} | awk '{ print $1 }'` 360 FDT_ADDR_R=`awk /fdt_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 361 KERNEL_ADDR_R=`awk /kernel_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 362 RMADISK_ADDR_R=`awk /ramdisk_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 363 sed -i "s/${FDT_ADDR_PLACEHOLDER}/${FDT_ADDR_R}/g" ${ITS_BOOT} 364 sed -i "s/${KERNEL_ADDR_PLACEHOLDER}/${KERNEL_ADDR_R}/g" ${ITS_BOOT} 365 sed -i "s/${RAMDISK_ADDR_PLACEHOLDER}/${RMADISK_ADDR_R}/g" ${ITS_BOOT} 366 if grep -q '^CONFIG_ARM64=y' .config ; then 367 sed -i 's/arch = "arm";/arch = "arm64";/g' ${ITS_BOOT} 368 fi 369 370 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 371 VERSION=`grep 'rollback-index' ${ITS_BOOT} | awk -F '=' '{ printf $2 }' | tr -d ' '` 372 sed -i "s/rollback-index = ${VERSION}/rollback-index = <${ARG_ROLLBACK_IDX_BOOT}>;/g" ${ITS_BOOT} 373 fi 374 375 ${MKIMAGE} -f ${ITS_BOOT} -k ${KEY_DIR} -K ${UBOOT_DTB} -E -p ${OFFS_S_BOOT} -r ${ITB_BOOT} -v ${ARG_VER_BOOT} 376 mv ${SIG_BIN} ${SIG_BOOT} 377 378 # rollback-index read back check 379 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 380 VERSION=`fdtget -ti ${ITB_BOOT} /configurations/conf rollback-index` 381 if [ "${VERSION}" != "${ARG_ROLLBACK_IDX_BOOT}" ]; then 382 echo "ERROR: Failed to set rollback-index for ${ITB_BOOT}"; 383 exit 1 384 fi 385 fi 386 387 if [ "${ARG_NO_CHECK}" != "y" ]; then 388 ${CHECK_SIGN} -f ${ITB_BOOT} -k ${UBOOT_DTB} 389 fi 390 391 # minimize u-boot.dtb 392 if grep -q '^CONFIG_FIT_HW_CRYPTO=y' .config ; then 393 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,r-squared 0x0 394 if grep -q '^CONFIG_ROCKCHIP_CRYPTO_V1=y' .config ; then 395 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 396 else 397 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 398 fi 399 else 400 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 401 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 402 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,exponent-BN 0x0 403 fi 404 fdtput -r ${UBOOT_DTB} ${SIGNATURE_KEY_NODE}/hash@c 405 fdtput -r ${UBOOT_DTB} ${SIGNATURE_KEY_NODE}/hash@np 406 fi 407 408 mv ${ITS_BOOT} ${FIT_DIR} 409} 410 411function fit_gen_uboot_img() 412{ 413 ITB=$1 414 415 if [ -z ${ITB} ]; then 416 ITB=${ITB_UBOOT} 417 fi 418 419 ITB_MAX_NUM=`sed -n "/SPL_FIT_IMAGE_MULTIPLE/p" .config | awk -F "=" '{ print $2 }'` 420 ITB_MAX_KB=`sed -n "/SPL_FIT_IMAGE_KB/p" .config | awk -F "=" '{ print $2 }'` 421 ITB_MAX_BS=$((ITB_MAX_KB*1024)) 422 ITB_BS=`ls -l ${ITB} | awk '{ print $5 }'` 423 424 if [ ${ITB_BS} -gt ${ITB_MAX_BS} ]; then 425 echo "ERROR: pack ${IMG_UBOOT} failed! ${ITB} actual: ${ITB_BS} bytes, max limit: ${ITB_MAX_BS} bytes" 426 exit 1 427 fi 428 429 rm -f ${IMG_UBOOT} 430 for ((i = 0; i < ${ITB_MAX_NUM}; i++)); 431 do 432 cat ${ITB} >> ${IMG_UBOOT} 433 truncate -s %${ITB_MAX_KB}K ${IMG_UBOOT} 434 done 435} 436 437function fit_gen_boot_img() 438{ 439 ITB=$1 440 441 if [ -z ${ITB} ]; then 442 ITB=${ITB_BOOT} 443 fi 444 445 if [ "${ITB}" != "${IMG_BOOT}" ]; then 446 cp ${ITB} ${IMG_BOOT} -f 447 fi 448} 449 450function fit_msg_uboot() 451{ 452 if [ "${ARG_SIGN}" != "y" ]; then 453 MSG_SIGN="no-signed" 454 else 455 MSG_SIGN="signed" 456 fi 457 458 VERSION=`fdtget -ti ${ITB_UBOOT} / version` 459 if [ "${VERSION}" != "" ]; then 460 MSG_VER=", version=${VERSION}" 461 fi 462 463 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 464 echo "Image(${MSG_SIGN}${MSG_VER}, rollback-index=${ARG_ROLLBACK_IDX_UBOOT}): ${IMG_UBOOT} (with uboot, trust...) is ready" 465 else 466 echo "Image(${MSG_SIGN}${MSG_VER}): ${IMG_UBOOT} (FIT with uboot, trust...) is ready" 467 fi 468} 469 470function fit_msg_boot() 471{ 472 if [ "${ARG_SIGN}" != "y" ]; then 473 MSG_SIGN="no-signed" 474 else 475 MSG_SIGN="signed" 476 fi 477 478 VERSION=`fdtget -ti ${ITB_BOOT} / version` 479 if [ "${VERSION}" != "" ]; then 480 MSG_VER=", version=${VERSION}" 481 fi 482 483 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 484 echo "Image(${MSG_SIGN}${MSG_VER}, rollback-index=${ARG_ROLLBACK_IDX_BOOT}): ${IMG_BOOT} is ready" 485 else 486 echo "Image(${MSG_SIGN}${MSG_VER}): ${IMG_BOOT} (FIT with kernel, fdt, resource...) is ready" 487 fi 488} 489 490function fit_msg_loader() 491{ 492 LOADER=`ls *loader*.bin` 493 echo "Image(no-signed): ${LOADER} (with spl, ddr, usbplug) is ready" 494} 495 496function fit_generate_uboot() 497{ 498 fit_raw_compile 499 fit_gen_uboot_itb 500 fit_gen_uboot_img 501 echo 502 fit_msg_uboot 503} 504 505function fit_generate_uboot_boot() 506{ 507 fit_raw_compile 508 fit_gen_boot_itb 509 fit_gen_boot_img 510 fit_gen_uboot_itb 511 fit_gen_uboot_img 512 echo 513 514 fit_msg_uboot 515 fit_msg_boot 516 fit_msg_loader 517 echo 518} 519 520fit_process_args $* 521if [ ! -z "${ARG_VALIDATE}" ]; then 522 validate_arg ${ARG_VALIDATE} 523elif [ ! -z "${ARG_BOOT_IMG}" -o ! -z "${ARG_BOOT_IMG_DIR}" ]; then 524 fit_generate_uboot_boot 525else 526 fit_generate_uboot 527fi 528 529