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="0xa00" 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) 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 *) 166 help 167 exit 1 168 ;; 169 esac 170 done 171 172 if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then 173 ARG_SIGN="y" 174 fi 175} 176 177function fit_raw_compile() 178{ 179 # Verified-boot: should rebuild code but don't need to repack images. 180 if [ "${ARG_SIGN}" == "y" ]; then 181 ./make.sh --raw-compile 182 fi 183 rm ${FIT_DIR} -rf && mkdir -p ${FIT_DIR} 184} 185 186function fit_gen_uboot_itb() 187{ 188 ./make.sh itb ${ARG_INI_TRUST} >/dev/null 2>&1 189 check_its ${ITS_UBOOT} 190 191 if [ "${ARG_SIGN}" != "y" ]; then 192 ${MKIMAGE} -f ${ITS_UBOOT} -E -p ${OFFS_NS_UBOOT} ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 193 if [ "${ARG_SPL_NEW}" == "y" ]; then 194 ./make.sh --spl ${ARG_INI_LOADER} 195 echo "pack loader with new: spl/u-boot-spl.bin" 196 else 197 ./make.sh loader ${ARG_INI_LOADER} 198 fi 199 else 200 if [ ! -f ${RSA_PRI_KEY} ]; then 201 echo "ERROR: No ${RSA_PRI_KEY} " 202 exit 1 203 elif [ ! -f ${RSA_PUB_KEY} ]; then 204 echo "ERROR: No ${RSA_PUB_KEY} " 205 exit 1 206 fi 207 208 if ! grep -q '^CONFIG_SPL_FIT_SIGNATURE=y' .config ; then 209 echo "ERROR: CONFIG_SPL_FIT_SIGNATURE is disabled" 210 exit 1 211 fi 212 213 # rollback-index 214 if grep -q '^CONFIG_SPL_FIT_ROLLBACK_PROTECT=y' .config ; then 215 ARG_SPL_ROLLBACK_PROTECT="y" 216 if [ -z ${ARG_ROLLBACK_IDX_UBOOT} ]; then 217 echo "ERROR: No arg \"--rollback-index-uboot <n>\"" 218 exit 1 219 fi 220 fi 221 222 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 223 VERSION=`grep 'rollback-index' ${ITS_UBOOT} | awk -F '=' '{ printf $2 }' | tr -d ' '` 224 sed -i "s/rollback-index = ${VERSION}/rollback-index = <${ARG_ROLLBACK_IDX_UBOOT}>;/g" ${ITS_UBOOT} 225 fi 226 227 # u-boot.dtb must contains rsa key 228 if ! fdtget -l ${UBOOT_DTB} /signature >/dev/null 2>&1 ; then 229 ${MKIMAGE} -f ${ITS_UBOOT} -k ${KEY_DIR} -K ${UBOOT_DTB} -E -p ${OFFS_S_UBOOT} -r ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 230 echo "## Adding RSA public key into ${UBOOT_DTB}" 231 fi 232 233 # Pack 234 ${MKIMAGE} -f ${ITS_UBOOT} -k ${KEY_DIR} -K ${SPL_DTB} -E -p ${OFFS_S_UBOOT} -r ${ITB_UBOOT} -v ${ARG_VER_UBOOT} 235 mv ${SIG_BIN} ${SIG_UBOOT} 236 237 # rollback-index read back check 238 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 239 VERSION=`fdtget -ti ${ITB_UBOOT} /configurations/conf rollback-index` 240 if [ "${VERSION}" != "${ARG_ROLLBACK_IDX_UBOOT}" ]; then 241 echo "ERROR: Failed to set rollback-index for ${ITB_UBOOT}"; 242 exit 1 243 fi 244 fi 245 246 # host check signature 247 if [ "${ARG_NO_CHECK}" != "y" ]; then 248 if [ "${ARG_SPL_NEW}" == "y" ]; then 249 ${CHECK_SIGN} -f ${ITB_UBOOT} -k ${SPL_DTB} -s 250 else 251 spl_file="../rkbin/"`sed -n "/FlashBoot=/s/FlashBoot=//p" ${ARG_INI_LOADER} |tr -d '\r'` 252 offs=`fdtdump -s ${spl_file} | head -1 | awk -F ":" '{ print $2 }' | sed "s/ found fdt at offset //g" | tr -d " "` 253 if [ -z ${offs} ]; then 254 echo "ERROR: invalid ${spl_file} , unable to find fdt blob" 255 fi 256 offs=`printf %d ${offs} ` # hex -> dec 257 dd if=${spl_file} of=spl/u-boot-spl-old.dtb bs=${offs} skip=1 >/dev/null 2>&1 258 ${CHECK_SIGN} -f ${ITB_UBOOT} -k spl/u-boot-spl-old.dtb -s 259 fi 260 fi 261 262 # minimize u-boot-spl.dtb 263 if grep -q '^CONFIG_SPL_FIT_HW_CRYPTO=y' .config ; then 264 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,r-squared 0x0 265 if grep -q '^CONFIG_SPL_ROCKCHIP_CRYPTO_V1=y' .config ; then 266 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 267 else 268 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 269 fi 270 else 271 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 272 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 273 fdtput -tx ${SPL_DTB} ${SIGNATURE_KEY_NODE} rsa,exponent-BN 0x0 274 fi 275 276 # repack spl 277 rm -f *_loader_*.bin 278 if [ "${ARG_SPL_NEW}" == "y" ]; then 279 cat spl/u-boot-spl-nodtb.bin > spl/u-boot-spl.bin 280 if ! grep -q '^CONFIG_SPL_SEPARATE_BSS=y' .config ; then 281 cat spl/u-boot-spl-pad.bin >> spl/u-boot-spl.bin 282 fi 283 cat ${SPL_DTB} >> spl/u-boot-spl.bin 284 285 ./make.sh --spl ${ARG_INI_LOADER} 286 echo "pack loader with new: spl/u-boot-spl.bin" 287 else 288 ./make.sh loader ${ARG_INI_LOADER} 289 fi 290 fi 291 292 rm -f u-boot.itb u-boot.img u-boot-dtb.img 293 mv ${ITS_UBOOT} ${FIT_DIR} 294} 295 296function fit_gen_boot_itb() 297{ 298 if [ ! -z ${ARG_BOOT_IMG} ]; then 299 ${FIT_UNPACK} -f ${ARG_BOOT_IMG} -o ${FIT_DIR}/unpack 300 ITS_BOOT="${FIT_DIR}/unpack/image.its" 301 else 302 compression=`awk -F"," '/COMPRESSION=/ { printf $1 }' ${ARG_INI_TRUST} | tr -d ' ' | cut -c 13-` 303 if [ -z "${compression}" ]; then 304 compression="none" 305 fi 306 ./arch/arm/mach-rockchip/make_fit_boot.sh -c ${compression} > ${ITS_BOOT} 307 check_its ${ITS_BOOT} 308 fi 309 310 if [ "${ARG_SIGN}" != "y" ]; then 311 ${MKIMAGE} -f ${ITS_BOOT} -E -p ${OFFS_NS_BOOT} ${ITB_BOOT} -v ${ARG_VER_BOOT} 312 else 313 if [ ! -f ${RSA_PRI_KEY} ]; then 314 echo "ERROR: No ${RSA_PRI_KEY}" 315 exit 1 316 elif [ ! -f ${RSA_PUB_KEY} ]; then 317 echo "ERROR: No ${RSA_PUB_KEY}" 318 exit 1 319 fi 320 321 if ! grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then 322 echo "ERROR: CONFIG_FIT_SIGNATURE is disabled" 323 exit 1 324 fi 325 326 if grep -q '^CONFIG_FIT_ROLLBACK_PROTECT=y' .config ; then 327 ARG_ROLLBACK_PROTECT="y" 328 if [ -z ${ARG_ROLLBACK_IDX_BOOT} ]; then 329 echo "ERROR: No arg \"--rollback-index-boot <n>\"" 330 exit 1 331 fi 332 fi 333 334 # fixup 335 COMMON_FILE=`sed -n "/_common.h/p" ${CHIP_FILE} | awk '{ print $1 }'` 336 FDT_ADDR_R=`awk /fdt_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 337 KERNEL_ADDR_R=`awk /kernel_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 338 RMADISK_ADDR_R=`awk /ramdisk_addr_r/ ${COMMON_FILE} | awk -F '=' '{ print $2 }' | awk -F '\\' '{ print $1 }'` 339 sed -i "s/${FDT_ADDR_PLACEHOLDER}/${FDT_ADDR_R}/g" ${ITS_BOOT} 340 sed -i "s/${KERNEL_ADDR_PLACEHOLDER}/${KERNEL_ADDR_R}/g" ${ITS_BOOT} 341 sed -i "s/${RAMDISK_ADDR_PLACEHOLDER}/${RMADISK_ADDR_R}/g" ${ITS_BOOT} 342 if grep -q '^CONFIG_ARM64=y' .config ; then 343 sed -i 's/arch = "arm";/arch = "arm64";/g' ${ITS_BOOT} 344 fi 345 346 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 347 VERSION=`grep 'rollback-index' ${ITS_BOOT} | awk -F '=' '{ printf $2 }' | tr -d ' '` 348 sed -i "s/rollback-index = ${VERSION}/rollback-index = <${ARG_ROLLBACK_IDX_BOOT}>;/g" ${ITS_BOOT} 349 fi 350 351 ${MKIMAGE} -f ${ITS_BOOT} -k ${KEY_DIR} -K ${UBOOT_DTB} -E -p ${OFFS_S_BOOT} -r ${ITB_BOOT} -v ${ARG_VER_BOOT} 352 mv ${SIG_BIN} ${SIG_BOOT} 353 354 # rollback-index read back check 355 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 356 VERSION=`fdtget -ti ${ITB_BOOT} /configurations/conf rollback-index` 357 if [ "${VERSION}" != "${ARG_ROLLBACK_IDX_BOOT}" ]; then 358 echo "ERROR: Failed to set rollback-index for ${ITB_BOOT}"; 359 exit 1 360 fi 361 fi 362 363 if [ "${ARG_NO_CHECK}" != "y" ]; then 364 ${CHECK_SIGN} -f ${ITB_BOOT} -k ${UBOOT_DTB} 365 fi 366 367 # minimize u-boot.dtb 368 if grep -q '^CONFIG_FIT_HW_CRYPTO=y' .config ; then 369 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,r-squared 0x0 370 if grep -q '^CONFIG_ROCKCHIP_CRYPTO_V1=y' .config ; then 371 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 372 else 373 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 374 fi 375 else 376 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,c 0x0 377 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,np 0x0 378 fdtput -tx ${UBOOT_DTB} ${SIGNATURE_KEY_NODE} rsa,exponent-BN 0x0 379 fi 380 fi 381 382 mv ${ITS_BOOT} ${FIT_DIR} 383} 384 385function fit_gen_uboot_img() 386{ 387 ITB=$1 388 389 if [ -z ${ITB} ]; then 390 ITB=${ITB_UBOOT} 391 fi 392 393 ITB_MAX_NUM=`sed -n "/SPL_FIT_IMAGE_MULTIPLE/p" .config | awk -F "=" '{ print $2 }'` 394 ITB_MAX_KB=`sed -n "/SPL_FIT_IMAGE_KB/p" .config | awk -F "=" '{ print $2 }'` 395 ITB_MAX_BS=$((ITB_MAX_KB*1024)) 396 ITB_BS=`ls -l ${ITB} | awk '{ print $5 }'` 397 398 if [ ${ITB_BS} -gt ${ITB_MAX_BS} ]; then 399 echo "ERROR: pack ${IMG_UBOOT} failed! ${ITB} actual: ${ITB_BS} bytes, max limit: ${ITB_MAX_BS} bytes" 400 exit 1 401 fi 402 403 rm -f ${IMG_UBOOT} 404 for ((i = 0; i < ${ITB_MAX_NUM}; i++)); 405 do 406 cat ${ITB} >> ${IMG_UBOOT} 407 truncate -s %${ITB_MAX_KB}K ${IMG_UBOOT} 408 done 409} 410 411function fit_gen_boot_img() 412{ 413 ITB=$1 414 415 if [ -z ${ITB} ]; then 416 ITB=${ITB_BOOT} 417 fi 418 419 if [ "${ITB}" != "${IMG_BOOT}" ]; then 420 cp ${ITB} ${IMG_BOOT} -f 421 fi 422} 423 424function fit_msg_uboot() 425{ 426 if [ "${ARG_SIGN}" != "y" ]; then 427 MSG_SIGN="no-signed" 428 else 429 MSG_SIGN="signed" 430 fi 431 432 VERSION=`fdtget -ti ${ITB_UBOOT} / version` 433 if [ "${VERSION}" != "" ]; then 434 MSG_VER=", version=${VERSION}" 435 fi 436 437 if [ "${ARG_SPL_ROLLBACK_PROTECT}" == "y" ]; then 438 echo "Image(${MSG_SIGN}${MSG_VER}, rollback-index=${ARG_ROLLBACK_IDX_UBOOT}): ${IMG_UBOOT} (with uboot, trust...) is ready" 439 else 440 echo "Image(${MSG_SIGN}${MSG_VER}): ${IMG_UBOOT} (FIT with uboot, trust...) is ready" 441 fi 442} 443 444function fit_msg_boot() 445{ 446 if [ "${ARG_SIGN}" != "y" ]; then 447 MSG_SIGN="no-signed" 448 else 449 MSG_SIGN="signed" 450 fi 451 452 VERSION=`fdtget -ti ${ITB_BOOT} / version` 453 if [ "${VERSION}" != "" ]; then 454 MSG_VER=", version=${VERSION}" 455 fi 456 457 if [ "${ARG_ROLLBACK_PROTECT}" == "y" ]; then 458 echo "Image(${MSG_SIGN}${MSG_VER}, rollback-index=${ARG_ROLLBACK_IDX_BOOT}): ${IMG_BOOT} is ready" 459 else 460 echo "Image(${MSG_SIGN}${MSG_VER}): ${IMG_BOOT} (FIT with kernel, fdt, resource...) is ready" 461 fi 462} 463 464function fit_msg_loader() 465{ 466 LOADER=`ls *loader*.bin` 467 echo "Image(no-signed): ${LOADER} (with spl, ddr, usbplug) is ready" 468} 469 470function fit_generate_uboot() 471{ 472 fit_raw_compile 473 fit_gen_uboot_itb 474 fit_gen_uboot_img 475 echo 476 fit_msg_uboot 477} 478 479function fit_generate_uboot_boot() 480{ 481 fit_raw_compile 482 fit_gen_boot_itb 483 fit_gen_boot_img 484 fit_gen_uboot_itb 485 fit_gen_uboot_img 486 echo 487 488 fit_msg_uboot 489 fit_msg_boot 490 fit_msg_loader 491 echo 492} 493 494fit_process_args $* 495if [ ! -z "${ARG_VALIDATE}" ]; then 496 validate_arg ${ARG_VALIDATE} 497elif [ ! -z "${ARG_BOOT_IMG}" -o ! -z "${ARG_BOOT_IMG_DIR}" ]; then 498 fit_generate_uboot_boot 499else 500 fit_generate_uboot 501fi 502 503