1#!/bin/bash
2#
3# Licensed under the terms of the GNU GPL License version 2 or later.
4#
5# Author: Peter Tyser <ptyser@xes-inc.com>
6#
7# U-Boot firmware supports the booting of images in the Flattened Image
8# Tree (FIT) format.  The FIT format uses a device tree structure to
9# describe a kernel image, device tree blob, ramdisk, etc.  This script
10# creates an Image Tree Source (.its file) which can be passed to the
11# 'mkimage' utility to generate an Image Tree Blob (.itb file).  The .itb
12# file can then be booted by U-Boot (or other bootloaders which support
13# FIT images).  See doc/uImage.FIT/howto.txt in U-Boot source code for
14# additional information on FIT images.
15#
16
17usage() {
18	echo "Usage: `basename $0` -A arch -C comp -a addr -e entry" \
19		"-v version -k kernel [-D name -d dtb] -o its_file"
20	echo -e "\t-A ==> set architecture to 'arch'"
21	echo -e "\t-C ==> set compression type 'comp'"
22	echo -e "\t-a ==> set load address to 'addr' (hex)"
23	echo -e "\t-e ==> set entry point to 'entry' (hex)"
24	echo -e "\t-v ==> set kernel version to 'version'"
25	echo -e "\t-k ==> include kernel image 'kernel'"
26	echo -e "\t-D ==> human friendly Device Tree Blob 'name'"
27	echo -e "\t-d ==> include Device Tree Blob 'dtb'"
28	echo -e "\t-r ==> include ramdisk"
29	echo -e "\t-z ==> ramdisk compression type"
30	echo -e "\t-o ==> create output file 'its_file'"
31	exit 1
32}
33
34while getopts ":A:a:C:D:d:e:k:o:v:r:z:" OPTION
35do
36	case $OPTION in
37		A ) ARCH=$OPTARG;;
38		a ) LOAD_ADDR=$OPTARG;;
39		C ) COMPRESS=$OPTARG;;
40		D ) DEVICE=$OPTARG;;
41		d ) DTB=$OPTARG;;
42		e ) ENTRY_ADDR=$OPTARG;;
43		k ) KERNEL=$OPTARG;;
44		o ) OUTPUT=$OPTARG;;
45		v ) VERSION=$OPTARG;;
46		r ) RAMDISK=$OPTARG;;
47		z ) RD_COMPRESS=$OPTARG;;
48		* ) echo "Invalid option passed to '$0' (options:$@)"
49		usage;;
50	esac
51done
52
53# Make sure user entered all required parameters
54if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
55	[ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
56	[ -z "${OUTPUT}" ]; then
57	usage
58fi
59
60ARCH_UPPER=`echo $ARCH | tr '[:lower:]' '[:upper:]'`
61
62# Conditionally create fdt information
63if [ -n "${DTB}" ]; then
64	FDT="
65		fdt@1 {
66			description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
67			data = /incbin/(\"${DTB}\");
68			type = \"flat_dt\";
69			arch = \"${ARCH}\";
70			compression = \"none\";
71			hash@1 {
72				algo = \"crc32\";
73			};
74			hash@2 {
75				algo = \"sha1\";
76			};
77		};
78"
79		CONF="			fdt = \"fdt@1\";"
80fi
81
82# Conditionally create ramdisk node
83if [ -n "${RAMDISK}" ]; then
84	RD_COMPRESS=${RD_COMPRESS:-none}
85	RD="
86		ramdisk@1 {
87			description = \"${ARCH_UPPER} OpenWrt ${DEVICE} ramdisk\";
88			data = /incbin/(\"${RAMDISK}\");
89			type = \"ramdisk\";
90			arch = \"${ARCH}\";
91			os = \"linux\";
92			compression = \"${RD_COMPRESS}\";
93			hash@1 {
94				algo = \"crc32\";
95			};
96			hash@2 {
97				algo = \"sha1\";
98			};
99		};
100"
101	if [ -z "${CONF}" ]; then
102		CONF="			ramdisk = \"ramdisk@1\";"
103	else
104		CONF="$CONF
105			ramdisk = \"ramdisk@1\";"
106	fi
107fi
108
109# Create a default, fully populated DTS file
110DATA="/dts-v1/;
111
112/ {
113	description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
114	#address-cells = <1>;
115
116	images {
117		kernel@1 {
118			description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
119			data = /incbin/(\"${KERNEL}\");
120			type = \"kernel\";
121			arch = \"${ARCH}\";
122			os = \"linux\";
123			compression = \"${COMPRESS}\";
124			load = <${LOAD_ADDR}>;
125			entry = <${ENTRY_ADDR}>;
126			hash@1 {
127				algo = \"crc32\";
128			};
129			hash@2 {
130				algo = \"sha1\";
131			};
132		};
133
134${RD}
135${FDT}
136
137	};
138
139	configurations {
140		default = \"config@1\";
141		config@1 {
142			description = \"OpenWrt\";
143			kernel = \"kernel@1\";
144${CONF}
145		};
146	};
147};"
148
149# Write .its file to disk
150echo "$DATA" > ${OUTPUT}
151