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