1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0+
3#
4# script to generate FIT image source for RISC-V boards with OpenSBI
5# and, optionally, multiple device trees (given on the command line).
6#
7# usage: $0 [<dt_name> [<dt_name] ...]
8
9[ -z "$OPENSBI" ] && OPENSBI="fw_dynamic.bin"
10
11if [ -z "$UBOOT_LOAD_ADDR" ]; then
12	UBOOT_LOAD_ADDR="$(grep "^CONFIG_SYS_TEXT_BASE=" .config | awk 'BEGIN{FS="="} {print $2}')"
13fi
14
15if [ -z "$OPENSBI_LOAD_ADDR" ]; then
16	OPENSBI_LOAD_ADDR="$(grep "^CONFIG_SPL_OPENSBI_LOAD_ADDR=" .config | awk 'BEGIN{FS="="} {print $2}')"
17fi
18
19if [ ! -f $OPENSBI ]; then
20	echo "WARNING: OpenSBI binary \"$OPENSBI\" not found, resulting binary is not functional." >&2
21	OPENSBI=/dev/null
22fi
23
24cat << __HEADER_EOF
25/dts-v1/;
26
27/ {
28	description = "Configuration to load OpenSBI before U-Boot";
29
30	images {
31		uboot {
32			description = "U-Boot";
33			data = /incbin/("u-boot-nodtb.bin");
34			type = "standalone";
35			os = "U-Boot";
36			arch = "riscv";
37			compression = "none";
38			load = <$UBOOT_LOAD_ADDR>;
39		};
40		opensbi {
41			description = "RISC-V OpenSBI";
42			data = /incbin/("$OPENSBI");
43			type = "firmware";
44			os = "opensbi";
45			arch = "riscv";
46			compression = "none";
47			load = <$OPENSBI_LOAD_ADDR>;
48			entry = <$OPENSBI_LOAD_ADDR>;
49		};
50__HEADER_EOF
51
52cnt=1
53for dtname in $*
54do
55	cat << __FDT_IMAGE_EOF
56		fdt_$cnt {
57			description = "$(basename $dtname .dtb)";
58			data = /incbin/("$dtname");
59			type = "flat_dt";
60			compression = "none";
61		};
62__FDT_IMAGE_EOF
63cnt=$((cnt+1))
64done
65
66cat << __CONF_HEADER_EOF
67	};
68	configurations {
69		default = "config_1";
70
71__CONF_HEADER_EOF
72
73if [ $# -eq 0 ]; then
74cat << __CONF_SECTION_EOF
75		config_1 {
76			description = "U-Boot FIT";
77			firmware = "opensbi";
78			loadables = "uboot";
79		};
80__CONF_SECTION_EOF
81else
82cnt=1
83for dtname in $*
84do
85cat << __CONF_SECTION_EOF
86		config_$cnt {
87			description = "$(basename $dtname .dtb)";
88			firmware = "opensbi";
89			loadables = "uboot";
90			fdt = "fdt_$cnt";
91		};
92__CONF_SECTION_EOF
93cnt=$((cnt+1))
94done
95fi
96
97cat << __ITS_EOF
98	};
99};
100__ITS_EOF
101