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