xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/fit_nodes.sh (revision a962a5fdb4271e59bab38d4e9b59b4839b88656d)
1#!/bin/bash
2#
3# Copyright (C) 2021 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/fit_args.sh
10rm -f ${srctree}/*.digest ${srctree}/*.bin.gz ${srctree}/bl31_0x*.bin
11
12# compression
13if [ "${COMPRESSION}" == "gzip" ]; then
14	SUFFIX=".gz"
15else
16	COMPRESSION="none"
17	SUFFIX=
18fi
19
20# nodes
21function gen_uboot_node()
22{
23	echo "		uboot {
24			description = \"U-Boot\";
25			data = /incbin/(\"./u-boot-nodtb.bin${SUFFIX}\");
26			type = \"standalone\";
27			arch = \"${ARCH}\";
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 gen_fdt_node()
51{
52	echo "		fdt {
53			description = \"U-Boot dtb\";
54			data = /incbin/(\"./u-boot.dtb\");
55			type = \"flat_dt\";
56			arch = \"${ARCH}\";
57			compression = \"none\";
58			hash {
59				algo = \"sha256\";
60			};
61		};
62	};"
63};
64
65function gen_kfdt_node()
66{
67	KERN_DTB=`sed -n "/CONFIG_EMBED_KERNEL_DTB_PATH=/s/CONFIG_EMBED_KERNEL_DTB_PATH=//p" .config | tr -d '"'`
68	if [ -z "${KERN_DTB}" ]; then
69		return;
70	fi
71
72	if [ -f ${srctree}/${KERN_DTB} ]; then
73	PROP_KERN_DTB=', "kern-fdt"';
74	echo "		kern-fdt {
75			description = \"${KERN_DTB}\";
76			data = /incbin/(\"${KERN_DTB}\");
77			type = \"flat_dt\";
78			arch = \"${ARCH}\";
79			compression = \"none\";
80			hash {
81				algo = \"sha256\";
82			};
83		};"
84	fi
85}
86
87function gen_bl31_node()
88{
89	${srctree}/arch/arm/mach-rockchip/decode_bl31.py
90
91	NUM=1
92	for NAME in `ls -l bl31_0x*.bin | sort --key=5 -nr | awk '{ print $9 }'`
93	do
94		ATF_LOAD_ADDR=`echo ${NAME} | awk -F "_" '{ printf $2 }' | awk -F "." '{ printf $1 }'`
95		# only atf-1 support compress
96		if [ "${COMPRESSION}" == "gzip" -a ${NUM} -eq 1  ]; then
97			openssl dgst -sha256 -binary -out ${NAME}.digest ${NAME}
98			gzip -k -f -9 ${NAME}
99
100			echo "		atf-${NUM} {
101			description = \"ARM Trusted Firmware\";
102			data = /incbin/(\"./${NAME}${SUFFIX}\");
103			type = \"firmware\";
104			arch = \"${ARCH}\";
105			os = \"arm-trusted-firmware\";
106			compression = \"${COMPRESSION}\";
107			load = <"${ATF_LOAD_ADDR}">;
108			hash {
109				algo = \"sha256\";
110			};
111			digest {
112				value = /incbin/(\"./${NAME}.digest\");
113				algo = \"sha256\";
114			};
115		};"
116		else
117			echo "		atf-${NUM} {
118			description = \"ARM Trusted Firmware\";
119			data = /incbin/(\"./${NAME}\");
120			type = \"firmware\";
121			arch = \"${ARCH}\";
122			os = \"arm-trusted-firmware\";
123			compression = \"none\";
124			load = <"${ATF_LOAD_ADDR}">;
125			hash {
126				algo = \"sha256\";
127			};
128		};"
129		fi
130
131		if [ ${NUM} -gt 1 ]; then
132			LOADABLE_ATF=${LOADABLE_ATF}", \"atf-${NUM}\""
133		fi
134		NUM=`expr ${NUM} + 1`
135	done
136}
137
138function gen_bl32_node()
139{
140	if [ -z ${TEE_LOAD_ADDR} ]; then
141		return
142	fi
143
144	if [ "${ARCH}" == "arm" ]; then
145		ENTRY="entry = <0x${TEE_LOAD_ADDR}>;"
146	fi
147	echo "		optee {
148			description = \"OP-TEE\";
149			data = /incbin/(\"./tee.bin${SUFFIX}\");
150			type = \"firmware\";
151			arch = \"${ARCH}\";
152			os = \"op-tee\";
153			compression = \"${COMPRESSION}\";
154			load = <"0x${TEE_LOAD_ADDR}">;
155			${ENTRY}
156			hash {
157				algo = \"sha256\";
158			};"
159	if [ "${COMPRESSION}" == "gzip" ]; then
160		echo "			digest {
161				value = /incbin/(\"./tee.bin.digest\");
162				algo = \"sha256\";
163			};"
164		openssl dgst -sha256 -binary -out tee.bin.digest tee.bin
165		gzip -k -f -9 tee.bin
166	fi
167
168	LOADABLE_OPTEE=", \"optee\""
169	echo "		};"
170}
171
172function gen_mcu_node()
173{
174	for ((i=0, n=0; i<5; i++))
175	do
176		if [ ${i} -eq 0 ]; then
177			MCU_ADDR=${MCU0_LOAD_ADDR}
178		elif [ ${i} -eq 1 ]; then
179			MCU_ADDR=${MCU1_LOAD_ADDR}
180		elif [ ${i} -eq 2 ]; then
181			MCU_ADDR=${MCU2_LOAD_ADDR}
182		elif [ ${i} -eq 3 ]; then
183			MCU_ADDR=${MCU3_LOAD_ADDR}
184		elif [ ${i} -eq 4 ]; then
185			MCU_ADDR=${MCU4_LOAD_ADDR}
186		fi
187
188		if [ -z ${MCU_ADDR} ]; then
189			continue
190		fi
191		MCU="mcu${i}"
192		echo "		${MCU} {
193			description = \"${MCU}\";
194			type = \"standalone\";
195			arch = \"riscv\";
196			data = /incbin/(\"./${MCU}.bin${SUFFIX}\");
197			compression = \"${COMPRESSION}\";
198			load = <0x"${MCU_ADDR}">;
199			hash {
200				algo = \"sha256\";
201			};"
202		if [ "${COMPRESSION}" == "gzip" ]; then
203			echo "			digest {
204				value = /incbin/(\"./${MCU}.bin.digest\");
205				algo = \"sha256\";
206			};"
207			openssl dgst -sha256 -binary -out ${MCU}.bin.digest ${MCU}.bin
208			gzip -k -f -9 ${MCU}.bin
209		fi
210		echo "		};"
211		if [ ${n} -eq 0 ]; then
212			STANDALONE_LIST=${STANDALONE_LIST}"\"${MCU}\""
213		else
214			STANDALONE_LIST=${STANDALONE_LIST}", \"${MCU}\""
215		fi
216		n=`expr ${n} + 1`
217
218		STANDALONE_SIGN=", \"standalone\""
219		STANDALONE_MCU="standalone = ${STANDALONE_LIST};"
220	done
221}
222
223function gen_loadable_node()
224{
225	for ((i=0; i<5; i++))
226	do
227		if [ ${i} -eq 0 ]; then
228			LOAD_ADDR=${LOAD0_LOAD_ADDR}
229		elif [ ${i} -eq 1 ]; then
230			LOAD_ADDR=${LOAD1_LOAD_ADDR}
231		elif [ ${i} -eq 2 ]; then
232			LOAD_ADDR=${LOAD2_LOAD_ADDR}
233		elif [ ${i} -eq 3 ]; then
234			LOAD_ADDR=${LOAD3_LOAD_ADDR}
235		elif [ ${i} -eq 4 ]; then
236			LOAD_ADDR=${LOAD4_LOAD_ADDR}
237		fi
238
239		if [ -z ${LOAD_ADDR} ]; then
240			continue
241		fi
242		LOAD="load${i}"
243		echo "		${LOAD} {
244			description = \"${LOAD}\";
245			type = \"standalone\";
246			arch = \"${ARCH}\";
247			data = /incbin/(\"./${LOAD}.bin${SUFFIX}\");
248			compression = \"${COMPRESSION}\";
249			load = <0x"${LOAD_ADDR}">;
250			hash {
251				algo = \"sha256\";
252			};"
253		if [ "${COMPRESSION}" == "gzip" ]; then
254			echo "			digest {
255				value = /incbin/(\"./${LOAD}.bin.digest\");
256				algo = \"sha256\";
257			};"
258			openssl dgst -sha256 -binary -out ${LOAD}.bin.digest ${LOAD}.bin
259			gzip -k -f -9 ${LOAD}.bin
260		fi
261		echo "		};"
262
263		LOADABLE_OTHER=${LOADABLE_OTHER}", \"${LOAD}\""
264	done
265}
266
267function gen_header()
268{
269echo "
270/*
271 * Copyright (C) 2020 Rockchip Electronic Co.,Ltd
272 *
273 * Simple U-boot fit source file containing ATF/OP-TEE/U-Boot/dtb/MCU
274 */
275
276/dts-v1/;
277
278/ {
279	description = \"FIT Image with ATF/OP-TEE/U-Boot/MCU\";
280	#address-cells = <1>;
281
282	images {
283"
284}
285
286function gen_arm64_configurations()
287{
288PLATFORM=`sed -n "/CONFIG_DEFAULT_DEVICE_TREE/p" .config | awk -F "=" '{ print $2 }' | tr -d '"'`
289if grep  -q '^CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT=y' .config ; then
290	ALGO_PADDING="				padding = \"pss\";"
291fi
292echo "
293	configurations {
294		default = \"conf\";
295		conf {
296			description = \"${PLATFORM}\";
297			rollback-index = <0x0>;
298			firmware = \"atf-1\";
299			loadables = \"uboot\"${LOADABLE_ATF}${LOADABLE_OPTEE}${LOADABLE_OTHER};
300			${STANDALONE_MCU}
301			fdt = \"fdt\"${PROP_KERN_DTB};
302			signature {
303				algo = \"sha256,rsa2048\";
304				${ALGO_PADDING}
305				key-name-hint = \"dev\";
306				sign-images = \"fdt\", \"firmware\", \"loadables\"${STANDALONE_SIGN};
307			};
308		};
309	};
310};
311"
312}
313
314function gen_arm_configurations()
315{
316PLATFORM=`sed -n "/CONFIG_DEFAULT_DEVICE_TREE/p" .config | awk -F "=" '{ print $2 }' | tr -d '"'`
317if grep  -q '^CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT=y' .config ; then
318        ALGO_PADDING="                          padding = \"pss\";"
319fi
320echo "
321	configurations {
322		default = \"conf\";
323		conf {
324			description = \"${PLATFORM}\";
325			rollback-index = <0x0>;
326			firmware = \"optee\";
327			loadables = \"uboot\"${LOADABLE_OTHER};
328			${STANDALONE_MCU}
329			fdt = \"fdt\"${PROP_KERN_DTB};
330			signature {
331				algo = \"sha256,rsa2048\";
332				${ALGO_PADDING}
333				key-name-hint = \"dev\";
334				sign-images = \"fdt\", \"firmware\", \"loadables\"${STANDALONE_SIGN};
335			};
336		};
337	};
338};
339"
340}
341
342