1#!/usr/bin/env 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-o ==> create output file 'its_file'"
29	exit 1
30}
31
32while getopts ":A:a:C:D:d:e:k:o:v:" OPTION
33do
34	case $OPTION in
35		A ) ARCH=$OPTARG;;
36		a ) LOAD_ADDR=$OPTARG;;
37		C ) COMPRESS=$OPTARG;;
38		D ) DEVICE=$OPTARG;;
39		d ) DTB=$OPTARG;;
40		e ) ENTRY_ADDR=$OPTARG;;
41		k ) KERNEL=$OPTARG;;
42		o ) OUTPUT=$OPTARG;;
43		v ) VERSION=$OPTARG;;
44		* ) echo "Invalid option passed to '$0' (options:$@)"
45		usage;;
46	esac
47done
48
49# Make sure user entered all required parameters
50if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
51	[ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
52	[ -z "${OUTPUT}" ]; then
53	usage
54fi
55
56ARCH_UPPER=`echo $ARCH | tr '[:lower:]' '[:upper:]'`
57
58# Conditionally create fdt information
59if [ -n "${DTB}" ]; then
60	FDT="
61		fdt@1 {
62			description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
63			data = /incbin/(\"${DTB}\");
64			type = \"flat_dt\";
65			arch = \"${ARCH}\";
66			compression = \"none\";
67			hash@1 {
68				algo = \"crc32\";
69			};
70			hash@2 {
71				algo = \"sha1\";
72			};
73		};
74"
75fi
76
77# Create a default, fully populated DTS file
78DATA="/dts-v1/;
79
80/ {
81	description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
82	#address-cells = <1>;
83
84	images {
85		kernel@1 {
86			description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
87			data = /incbin/(\"${KERNEL}\");
88			type = \"kernel\";
89			arch = \"${ARCH}\";
90			os = \"linux\";
91			compression = \"${COMPRESS}\";
92			load = <${LOAD_ADDR}>;
93			entry = <${ENTRY_ADDR}>;
94			hash@1 {
95				algo = \"crc32\";
96			};
97			hash@2 {
98				algo = \"sha1\";
99			};
100		};
101
102${FDT}
103
104	};
105
106	configurations {
107		default = \"config@1\";
108		config@1 {
109			description = \"OpenWrt\";
110			kernel = \"kernel@1\";
111			fdt = \"fdt@1\";
112		};
113	};
114};"
115
116# Write .its file to disk
117echo "$DATA" > ${OUTPUT}
118