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