image: improve uImage.FIT device tree overlay support
[openwrt/openwrt.git] / scripts / mkits.sh
1 #!/bin/sh
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
17 usage() {
18 printf "Usage: %s -A arch -C comp -a addr -e entry" "$(basename "$0")"
19 printf " -v version -k kernel [-D name -n address -d dtb] -o its_file"
20
21 printf "\n\t-A ==> set architecture to 'arch'"
22 printf "\n\t-C ==> set compression type 'comp'"
23 printf "\n\t-c ==> set config name 'config'"
24 printf "\n\t-a ==> set load address to 'addr' (hex)"
25 printf "\n\t-e ==> set entry point to 'entry' (hex)"
26 printf "\n\t-f ==> set device tree compatible string"
27 printf "\n\t-i ==> include initrd Blob 'initrd'"
28 printf "\n\t-v ==> set kernel version to 'version'"
29 printf "\n\t-k ==> include kernel image 'kernel'"
30 printf "\n\t-D ==> human friendly Device Tree Blob 'name'"
31 printf "\n\t-n ==> fdt unit-address 'address'"
32 printf "\n\t-d ==> include Device Tree Blob 'dtb'"
33 printf "\n\t-r ==> include RootFS blob 'rootfs'"
34 printf "\n\t-H ==> specify hash algo instead of SHA1"
35 printf "\n\t-l ==> legacy mode character (@ etc otherwise -)"
36 printf "\n\t-o ==> create output file 'its_file'"
37 printf "\n\t-O ==> create config with dt overlay 'name:dtb'"
38 printf "\n\t-s ==> set FDT load address to 'addr' (hex)"
39 printf "\n\t\t(can be specified more than once)\n"
40 exit 1
41 }
42
43 REFERENCE_CHAR='-'
44 FDTNUM=1
45 ROOTFSNUM=1
46 INITRDNUM=1
47 HASH=sha1
48 LOADABLES=
49 DTOVERLAY=
50 DTADDR=
51
52 while getopts ":A:a:c:C:D:d:e:f:i:k:l:n:o:O:v:r:s:H:" OPTION
53 do
54 case $OPTION in
55 A ) ARCH=$OPTARG;;
56 a ) LOAD_ADDR=$OPTARG;;
57 c ) CONFIG=$OPTARG;;
58 C ) COMPRESS=$OPTARG;;
59 D ) DEVICE=$OPTARG;;
60 d ) DTB=$OPTARG;;
61 e ) ENTRY_ADDR=$OPTARG;;
62 f ) COMPATIBLE=$OPTARG;;
63 i ) INITRD=$OPTARG;;
64 k ) KERNEL=$OPTARG;;
65 l ) REFERENCE_CHAR=$OPTARG;;
66 n ) FDTNUM=$OPTARG;;
67 o ) OUTPUT=$OPTARG;;
68 O ) DTOVERLAY="$DTOVERLAY ${OPTARG}";;
69 r ) ROOTFS=$OPTARG;;
70 s ) FDTADDR=$OPTARG;;
71 H ) HASH=$OPTARG;;
72 v ) VERSION=$OPTARG;;
73 * ) echo "Invalid option passed to '$0' (options:$*)"
74 usage;;
75 esac
76 done
77
78 # Make sure user entered all required parameters
79 if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
80 [ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
81 [ -z "${OUTPUT}" ] || [ -z "${CONFIG}" ]; then
82 usage
83 fi
84
85 ARCH_UPPER=$(echo "$ARCH" | tr '[:lower:]' '[:upper:]')
86
87 if [ -n "${COMPATIBLE}" ]; then
88 COMPATIBLE_PROP="compatible = \"${COMPATIBLE}\";"
89 fi
90
91 [ "$DTOVERLAY" ] && {
92 dtbsize=$(wc -c "$DTB" | awk '{print $1}')
93 DTADDR=$(printf "0x%08x" $(($LOAD_ADDR - $dtbsize)) )
94 }
95
96 [ "$FDTADDR" ] && {
97 DTADDR="$FDTADDR"
98 }
99
100 # Conditionally create fdt information
101 if [ -n "${DTB}" ]; then
102 FDT_NODE="
103 fdt${REFERENCE_CHAR}$FDTNUM {
104 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
105 ${COMPATIBLE_PROP}
106 data = /incbin/(\"${DTB}\");
107 type = \"flat_dt\";
108 ${DTADDR:+load = <${DTADDR}>;}
109 arch = \"${ARCH}\";
110 compression = \"none\";
111 hash@1 {
112 algo = \"crc32\";
113 };
114 hash@2 {
115 algo = \"${HASH}\";
116 };
117 };
118 "
119 FDT_PROP="fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\";"
120 fi
121
122 if [ -n "${INITRD}" ]; then
123 INITRD_NODE="
124 initrd${REFERENCE_CHAR}$INITRDNUM {
125 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\";
126 ${COMPATIBLE_PROP}
127 data = /incbin/(\"${INITRD}\");
128 type = \"ramdisk\";
129 arch = \"${ARCH}\";
130 os = \"linux\";
131 hash@1 {
132 algo = \"crc32\";
133 };
134 hash@2 {
135 algo = \"${HASH}\";
136 };
137 };
138 "
139 INITRD_PROP="ramdisk=\"initrd${REFERENCE_CHAR}${INITRDNUM}\";"
140 fi
141
142
143 if [ -n "${ROOTFS}" ]; then
144 dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
145 ROOTFS_NODE="
146 rootfs${REFERENCE_CHAR}$ROOTFSNUM {
147 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\";
148 ${COMPATIBLE_PROP}
149 data = /incbin/(\"${ROOTFS}.pagesync\");
150 type = \"filesystem\";
151 arch = \"${ARCH}\";
152 compression = \"none\";
153 hash@1 {
154 algo = \"crc32\";
155 };
156 hash@2 {
157 algo = \"${HASH}\";
158 };
159 };
160 "
161 LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs${REFERENCE_CHAR}${ROOTFSNUM}\""
162 fi
163
164 # add DT overlay blobs
165 FDTOVERLAY_NODE=""
166 OVCONFIGS=""
167 [ "$DTOVERLAY" ] && for overlay in $DTOVERLAY ; do
168 overlay_blob=${overlay##*:}
169 ovname=${overlay%%:*}
170 ovnode="fdt-$ovname"
171 ovsize=$(wc -c "$overlay_blob" | awk '{print $1}')
172 echo "$ovname ($overlay_blob) : $ovsize" >&2
173 DTADDR=$(printf "0x%08x" $(($DTADDR - $ovsize)))
174 FDTOVERLAY_NODE="$FDTOVERLAY_NODE
175
176 $ovnode {
177 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree overlay $ovname\";
178 ${COMPATIBLE_PROP}
179 data = /incbin/(\"${overlay_blob}\");
180 type = \"flat_dt\";
181 arch = \"${ARCH}\";
182 load = <${DTADDR}>;
183 compression = \"none\";
184 hash@1 {
185 algo = \"crc32\";
186 };
187 hash@2 {
188 algo = \"${HASH}\";
189 };
190 };
191 "
192 OVCONFIGS="$OVCONFIGS
193
194 $ovname {
195 description = \"OpenWrt ${DEVICE} overlay $ovname\";
196 fdt = \"$ovnode\";
197 ${COMPATIBLE_PROP}
198 };
199 "
200 done
201
202 # Create a default, fully populated DTS file
203 DATA="/dts-v1/;
204
205 / {
206 description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
207 #address-cells = <1>;
208
209 images {
210 kernel${REFERENCE_CHAR}1 {
211 description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
212 data = /incbin/(\"${KERNEL}\");
213 type = \"kernel\";
214 arch = \"${ARCH}\";
215 os = \"linux\";
216 compression = \"${COMPRESS}\";
217 load = <${LOAD_ADDR}>;
218 entry = <${ENTRY_ADDR}>;
219 hash@1 {
220 algo = \"crc32\";
221 };
222 hash@2 {
223 algo = \"$HASH\";
224 };
225 };
226 ${INITRD_NODE}
227 ${FDT_NODE}
228 ${FDTOVERLAY_NODE}
229 ${ROOTFS_NODE}
230 };
231
232 configurations {
233 default = \"${CONFIG}\";
234 ${CONFIG} {
235 description = \"OpenWrt ${DEVICE}\";
236 kernel = \"kernel${REFERENCE_CHAR}1\";
237 ${FDT_PROP}
238 ${LOADABLES:+loadables = ${LOADABLES};}
239 ${COMPATIBLE_PROP}
240 ${INITRD_PROP}
241 };
242 ${OVCONFIGS}
243 };
244 };"
245
246 # Write .its file to disk
247 echo "$DATA" > "${OUTPUT}"