xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/make_fit_atf.sh (revision e091b6c996a68a6a0faa2bd3ffdd90b3ba5f44ce)
1#!/bin/bash
2#
3# Copyright (C) 2020 Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier:     GPL-2.0+
6#
7
8# Process args and auto set variables
9source ./${srctree}/arch/arm/mach-rockchip/make_fit_args.sh
10
11rm -f ${srctree}/*.digest ${srctree}/*.bin.gz ${srctree}/bl31_0x*.bin
12${srctree}/arch/arm/mach-rockchip/decode_bl31.py
13
14if [ "${COMPRESSION}" == "gzip" ]; then
15	SUFFIX=".gz"
16else
17	COMPRESSION="none"
18	SUFFIX=
19fi
20
21function generate_uboot_node()
22{
23	echo "		uboot {
24			description = \"U-Boot(64-bit)\";
25			data = /incbin/(\"./u-boot-nodtb.bin${SUFFIX}\");
26			type = \"standalone\";
27			arch = \"arm64\";
28			os = \"U-Boot\";
29			compression = \"${COMPRESSION}\";
30			load = <"${UBOOT_LOAD_ADDR}">;
31			hash {
32				algo = \"sha256\";
33			};"
34	if [ "${COMPRESSION}" == "gzip" ]; then
35		echo "			digest {
36				value = /incbin/(\"./u-boot-nodtb.bin.digest\");
37				algo = \"sha256\";
38			};"
39		openssl dgst -sha256 -binary -out u-boot-nodtb.bin.digest u-boot-nodtb.bin
40		UBOOT_SZ=`ls -l u-boot-nodtb.bin | awk '{ print $5 }'`
41		if [ ${UBOOT_SZ} -gt 0 ]; then
42			gzip -k -f -9 ${srctree}/u-boot-nodtb.bin
43		else
44			touch ${srctree}/u-boot-nodtb.bin.gz
45		fi
46	fi
47	echo "		};"
48}
49
50function generate_kfdt_node()
51{
52	KERN_DTB=`sed -n "/CONFIG_EMBED_KERNEL_DTB_PATH=/s/CONFIG_EMBED_KERNEL_DTB_PATH=//p" .config | tr -d '"'`
53	if [ -z "${KERN_DTB}" ]; then
54		return;
55	fi
56
57	if [ -f ${srctree}/${KERN_DTB} ]; then
58	PROP_KERN_DTB=', "kern-fdt"';
59	echo "		kern-fdt {
60			description = \"${KERN_DTB}\";
61			data = /incbin/(\"${KERN_DTB}\");
62			type = \"flat_dt\";
63			arch = \"${ARCH}\";
64			compression = \"none\";
65			hash {
66				algo = \"sha256\";
67			};
68		};"
69	fi
70}
71
72function generate_bl31_node()
73{
74	NUM=1
75	for NAME in `ls -l bl31_0x*.bin | sort --key=5 -nr | awk '{ print $9 }'`
76	do
77		ATF_LOAD_ADDR=`echo ${NAME} | awk -F "_" '{ printf $2 }' | awk -F "." '{ printf $1 }'`
78		# only atf-1 support compress
79		if [ "${COMPRESSION}" == "gzip" -a ${NUM} -eq 1  ]; then
80			openssl dgst -sha256 -binary -out ${NAME}.digest ${NAME}
81			gzip -k -f -9 ${NAME}
82
83			echo "		atf-${NUM} {
84			description = \"ARM Trusted Firmware\";
85			data = /incbin/(\"./${NAME}${SUFFIX}\");
86			type = \"firmware\";
87			arch = \"arm64\";
88			os = \"arm-trusted-firmware\";
89			compression = \"${COMPRESSION}\";
90			load = <"${ATF_LOAD_ADDR}">;
91			hash {
92				algo = \"sha256\";
93			};
94			digest {
95				value = /incbin/(\"./${NAME}.digest\");
96				algo = \"sha256\";
97			};
98		};"
99		else
100			echo "		atf-${NUM} {
101			description = \"ARM Trusted Firmware\";
102			data = /incbin/(\"./${NAME}\");
103			type = \"firmware\";
104			arch = \"arm64\";
105			os = \"arm-trusted-firmware\";
106			compression = \"none\";
107			load = <"${ATF_LOAD_ADDR}">;
108			hash {
109				algo = \"sha256\";
110			};
111		};"
112		fi
113
114		if [ ${NUM} -gt 1 ]; then
115			LOADABLE_ATF=${LOADABLE_ATF}", \"atf-${NUM}\""
116		fi
117		NUM=`expr ${NUM} + 1`
118	done
119}
120
121function generate_bl32_node()
122{
123	if [ -z ${TEE_LOAD_ADDR} ]; then
124		return
125	fi
126
127	echo "		optee {
128			description = \"OP-TEE\";
129			data = /incbin/(\"./tee.bin${SUFFIX}\");
130			type = \"firmware\";
131			arch = \"arm64\";
132			os = \"op-tee\";
133			compression = \"${COMPRESSION}\";
134			load = <"0x${TEE_LOAD_ADDR}">;
135			hash {
136				algo = \"sha256\";
137			};"
138	if [ "${COMPRESSION}" == "gzip" ]; then
139		echo "			digest {
140				value = /incbin/(\"./tee.bin.digest\");
141				algo = \"sha256\";
142			};"
143		openssl dgst -sha256 -binary -out tee.bin.digest tee.bin
144		gzip -k -f -9 tee.bin
145	fi
146
147	LOADABLE_OPTEE=", \"optee\""
148	echo "		};"
149}
150
151function generate_mcu_node()
152{
153	if [ -z ${MCU_LOAD_ADDR} ]; then
154		return
155	fi
156
157	echo "		mcu {
158			description = \"mcu\";
159			type = \"standalone\";
160			arch = \"riscv\";
161			data = /incbin/(\"./mcu.bin${SUFFIX}\");
162			compression = \"${COMPRESSION}\";
163			load = <0x"${MCU_LOAD_ADDR}">;
164			hash {
165				algo = \"sha256\";
166			};"
167	if [ "${COMPRESSION}" == "gzip" ]; then
168		echo "			digest {
169				value = /incbin/(\"./mcu.bin.digest\");
170				algo = \"sha256\";
171			};"
172		openssl dgst -sha256 -binary -out mcu.bin.digest mcu.bin
173		gzip -k -f -9 mcu.bin
174	fi
175
176	STANDALONE_SIGN=", \"standalone\""
177	STANDALONE_MCU="standalone = \"mcu\";"
178	echo "		};"
179}
180########################################################################################################
181THIS_PLAT=`sed -n "/CONFIG_DEFAULT_DEVICE_TREE/p" .config | awk -F "=" '{ print $2 }' | tr -d '"'`
182
183cat << EOF
184/*
185 * Copyright (C) 2020 Rockchip Electronic Co.,Ltd
186 *
187 * Simple U-boot fit source file containing ATF/OP-TEE/U-Boot/dtb
188 */
189
190/dts-v1/;
191
192/ {
193	description = "FIT Image with ATF/OP-TEE/U-Boot";
194	#address-cells = <1>;
195
196	images {
197EOF
198
199	# generate nodes dynamically
200	generate_uboot_node
201	generate_bl31_node
202	generate_bl32_node
203	generate_mcu_node
204	generate_kfdt_node
205
206cat << EOF
207		fdt {
208			description = "U-Boot dtb";
209			data = /incbin/("./u-boot.dtb");
210			type = "flat_dt";
211			arch = "${ARCH}";
212			compression = "none";
213			hash {
214				algo = "sha256";
215			};
216		};
217	};
218
219	configurations {
220		default = "conf";
221		conf {
222			description = "${THIS_PLAT}";
223			rollback-index = <0x0>;
224			firmware = "atf-1";
225			loadables = "uboot"${LOADABLE_ATF}${LOADABLE_OPTEE};
226			${STANDALONE_MCU}
227			fdt = "fdt"${PROP_KERN_DTB};
228			signature {
229				algo = "sha256,rsa2048";
230				padding = "pss";
231				key-name-hint = "dev";
232				sign-images = "fdt", "firmware", "loadables"${STANDALONE_SIGN};
233			};
234		};
235	};
236};
237EOF
238