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