1# Copyright (c) 2021-2022 iSoftStone Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13set -e 14 15usage() { 16 cat >&2 << USAGE 17usage: $0 [-h] --dtb DTB 18 19optional arguments: 20 -h, --help show this help message and exit 21 --dtb DTB the dtb file name 22USAGE 23} 24 25# Parse command-line arguments 26while [ $# -gt 0 ]; do 27 case $1 in 28 --dtb) 29 DTB=$2 30 shift 2 31 ;; 32 -h) 33 usage 34 exit 0 35 ;; 36 --help) 37 usage 38 exit 0 39 ;; 40 *) 41 shift 42 ;; 43 esac 44done 45 46srctree=${srctree-"."} 47objtree=${objtree-"."} 48if [ "${ARCH}" == "" ]; then 49 if [ "$($srctree/scripts/config --state CONFIG_ARM)" == "y" ]; then 50 ARCH=arm 51 else 52 ARCH=arm64 53 fi 54fi 55 56LOGO_PATH=${srctree}/logo.bmp 57[ -f ${LOGO_PATH} ] && LOGO=logo.bmp 58 59LOGO_KERNEL_PATH=${srctree}/logo_kernel.bmp 60[ -f ${LOGO_KERNEL_PATH} ] && LOGO_KERNEL=logo_kernel.bmp 61 62KERNEL_IMAGE_PATH=${objtree}/arch/${ARCH}/boot/Image 63KERNEL_IMAGE_ARG="--kernel ${KERNEL_IMAGE_PATH}" 64if [ "${ARCH}" == "arm" ]; then 65 DTB_PATH=${objtree}/arch/arm/boot/dts/${DTB} 66 KERNEL_ZIMAGE_PATH=${objtree}/arch/arm/boot/zImage 67 KERNEL_ZIMAGE_ARG="--kernel ${KERNEL_ZIMAGE_PATH}" 68 ZIMAGE=zImage 69else 70 DTB_PATH=${objtree}/arch/arm64/boot/dts/rockchip/${DTB} 71 KERNEL_ZIMAGE_ARG="--kernel ${objtree}/arch/arm64/boot/Image.lz4" 72 ZIMAGE=Image.lz4 73fi 74if [ ! -f ${DTB_PATH} ]; then 75 echo "No dtb" >&2 76 usage 77 exit 1 78fi 79 80OUT=out 81ITB=${BOOT_IMG} 82ITS=${OUT}/boot.its 83MKIMAGE=${MKIMAGE-"mkimage"} 84MKIMAGE_ARG="-E -p 0x800" 85 86make_boot_img() 87{ 88 RAMDISK_IMG_PATH=${objtree}/ramdisk.img 89 [ -f ${RAMDISK_IMG_PATH} ] && RAMDISK_IMG=ramdisk.img && RAMDISK_ARG="--ramdisk ${RAMDISK_IMG_PATH}" 90 91 ${srctree}/scripts/mkbootimg \ 92 ${KERNEL_IMAGE_ARG} \ 93 ${RAMDISK_ARG} \ 94 --second resource.img \ 95 -o boot.img && \ 96 echo " Image: boot.img (with Image ${RAMDISK_IMG} resource.img) is ready"; 97 ${srctree}/scripts/mkbootimg \ 98 ${KERNEL_ZIMAGE_ARG} \ 99 ${RAMDISK_ARG} \ 100 --second resource.img \ 101 -o zboot.img && \ 102 echo " Image: zboot.img (with ${ZIMAGE} ${RAMDISK_IMG} resource.img) is ready" 103} 104 105repack_boot_img() 106{ 107 ${srctree}/scripts/repack-bootimg \ 108 --boot_img ${BOOT_IMG} --out ${OUT} \ 109 ${KERNEL_IMAGE_ARG} \ 110 --second resource.img \ 111 --dtb ${DTB_PATH} \ 112 -o boot.img && 113 echo " Image: boot.img (${BOOT_IMG} + Image) is ready"; 114 ${srctree}/scripts/repack-bootimg \ 115 --boot_img ${BOOT_IMG} --out ${OUT} \ 116 ${KERNEL_ZIMAGE_ARG} \ 117 --second resource.img \ 118 --dtb ${DTB_PATH} \ 119 -o zboot.img && \ 120 echo " Image: zboot.img (${BOOT_IMG} + ${ZIMAGE}) is ready" 121} 122 123check_mkimage() 124{ 125 MKIMAGE=$(type -path ${MKIMAGE}) 126 if [ -z "${MKIMAGE}" ]; then 127 # Doesn't exist 128 echo '"mkimage" command not found - U-Boot images will not be built' >&2 129 exit 1; 130 fi 131} 132 133unpack_itb() 134{ 135 mkdir -p ${OUT} 136 rm -f ${OUT}/fdt ${OUT}/kernel ${OUT}/resource 137 138 for NAME in $(fdtget -l ${ITB} /images) 139 do 140 # generate image 141 NODE="/images/${NAME}" 142 OFFS=$(fdtget -ti ${ITB} ${NODE} data-position) 143 SIZE=$(fdtget -ti ${ITB} ${NODE} data-size) 144 if [ -z ${OFFS} ]; then 145 continue; 146 fi 147 148 if [ ${SIZE} -ne 0 ]; then 149 dd if=${ITB} of=${OUT}/${NAME} bs=${SIZE} count=1 skip=${OFFS} iflag=skip_bytes >/dev/null 2>&1 150 else 151 touch ${OUT}/${NAME} 152 fi 153 done 154 155 [ ! -f ${OUT}/kernel ] && echo "FIT ${ITB} no kernel" >&2 && exit 1 || true 156} 157 158gen_its() 159{ 160 TMP_ITB=${OUT}/boot.tmp 161 162 # add placeholder 163 cp -a ${ITB} ${TMP_ITB} 164 for NAME in $(fdtget -l ${ITB} /images); do 165 fdtput -t s ${TMP_ITB} /images/${NAME} data "/INCBIN/(${NAME})" 166 done 167 dtc -I dtb -O dts ${TMP_ITB} -o ${ITS} >/dev/null 2>&1 168 rm -f ${TMP_ITB} 169 170 # fixup placeholder: data = "/INCBIN/(...)"; -> data = /incbin/("..."); 171 sed -i "s/\"\/INCBIN\/(\(.*\))\"/\/incbin\/(\"\1\")/" ${ITS} 172 173 # remove 174 sed -i "/memreserve/d" ${ITS} 175 sed -i "/timestamp/d" ${ITS} 176 sed -i "/data-size/d" ${ITS} 177 sed -i "/data-position/d" ${ITS} 178 sed -i "/value/d" ${ITS} 179 sed -i "/hashed-strings/d" ${ITS} 180 sed -i "/hashed-nodes/d" ${ITS} 181 sed -i "/signer-version/d" ${ITS} 182 sed -i "/signer-name/d" ${ITS} 183} 184 185gen_itb() 186{ 187 [ -f ${OUT}/fdt ] && cp -a ${DTB_PATH} ${OUT}/fdt && FDT=" + ${DTB}" 188 [ -f ${OUT}/resource ] && cp -a resource.img ${OUT}/resource && RESOURCE=" + resource.img" 189 COMP=$(fdtget ${ITB} /images/kernel compression) 190 case "${COMP}" in 191 gzip) EXT=".gz";; 192 lz4) EXT=".lz4";; 193 bzip2) EXT=".bz2";; 194 lzma) EXT=".lzma";; 195 lzo) EXT=".lzo";; 196 esac 197 cp -a ${KERNEL_IMAGE_PATH}${EXT} ${OUT}/kernel && \ 198 ${MKIMAGE} ${MKIMAGE_ARG} -f ${ITS} boot.img >/dev/null && \ 199 echo " Image: boot.img (FIT ${BOOT_IMG} + Image${EXT}${FDT}${RESOURCE}) is ready"; 200 if [ "${EXT}" == "" ] && [ -f ${KERNEL_ZIMAGE_PATH} ]; then 201 cp -a ${KERNEL_ZIMAGE_PATH} ${OUT}/kernel && \ 202 ${MKIMAGE} ${MKIMAGE_ARG} -f ${ITS} zboot.img >/dev/null && \ 203 echo " Image: zboot.img (FIT ${BOOT_IMG} + zImage${FDT}${RESOURCE}) is ready"; 204 fi 205} 206 207repack_itb() 208{ 209 check_mkimage 210 unpack_itb 211 gen_its 212 gen_itb 213} 214 215if [ -x ${srctree}/scripts/bmpconvert ]; then 216 if [ -f ${LOGO_PATH} ]; then 217 ${srctree}/scripts/bmpconvert ${LOGO_PATH}; 218 fi 219 if [ -f ${LOGO_KERNEL_PATH} ]; then 220 ${srctree}/scripts/bmpconvert ${LOGO_KERNEL_PATH}; 221 fi 222fi 223 224if [ "${srctree}" != "${objtree}" ]; then 225 if [ -f ${LOGO_PATH} ]; then 226 cp -a ${LOGO_PATH} ${objtree}/; 227 fi 228 if [ -f ${LOGO_KERNEL_PATH} ]; then 229 cp -a ${LOGO_KERNEL_PATH} ${objtree}/; 230 fi 231fi 232scripts/resource_tool ${DTB_PATH} ${LOGO} ${LOGO_KERNEL} >/dev/null 233echo " Image: resource.img (with ${DTB} ${LOGO} ${LOGO_KERNEL}) is ready" 234 235if [ -f "${BOOT_IMG}" ]; then 236 if file -p -b ${BOOT_IMG} | grep -q 'Device Tree Blob' ; then 237 repack_itb; 238 elif [ -x ${srctree}/scripts/repack-bootimg ]; then 239 repack_boot_img; 240 fi 241elif [ -x ${srctree}/scripts/mkbootimg ]; then 242 make_boot_img; 243fi 244