image: allow building FIT and uImage with ramdisk
[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'\n"
36 exit 1
37 }
38
39 FDTNUM=1
40 ROOTFSNUM=1
41 INITRDNUM=1
42 HASH=sha1
43 LOADABLES=
44
45 while getopts ":A:a:c:C:D:d:e:f:i:k:n:o:v:r:S" OPTION
46 do
47 case $OPTION in
48 A ) ARCH=$OPTARG;;
49 a ) LOAD_ADDR=$OPTARG;;
50 c ) CONFIG=$OPTARG;;
51 C ) COMPRESS=$OPTARG;;
52 D ) DEVICE=$OPTARG;;
53 d ) DTB=$OPTARG;;
54 e ) ENTRY_ADDR=$OPTARG;;
55 f ) COMPATIBLE=$OPTARG;;
56 i ) INITRD=$OPTARG;;
57 k ) KERNEL=$OPTARG;;
58 n ) FDTNUM=$OPTARG;;
59 o ) OUTPUT=$OPTARG;;
60 r ) ROOTFS=$OPTARG;;
61 S ) HASH=$OPTARG;;
62 v ) VERSION=$OPTARG;;
63 * ) echo "Invalid option passed to '$0' (options:$*)"
64 usage;;
65 esac
66 done
67
68 # Make sure user entered all required parameters
69 if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
70 [ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
71 [ -z "${OUTPUT}" ] || [ -z "${CONFIG}" ]; then
72 usage
73 fi
74
75 ARCH_UPPER=$(echo "$ARCH" | tr '[:lower:]' '[:upper:]')
76
77 if [ -n "${COMPATIBLE}" ]; then
78 COMPATIBLE_PROP="compatible = \"${COMPATIBLE}\";"
79 fi
80
81 # Conditionally create fdt information
82 if [ -n "${DTB}" ]; then
83 FDT_NODE="
84 fdt@$FDTNUM {
85 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
86 ${COMPATIBLE_PROP}
87 data = /incbin/(\"${DTB}\");
88 type = \"flat_dt\";
89 arch = \"${ARCH}\";
90 compression = \"none\";
91 hash@1 {
92 algo = \"crc32\";
93 };
94 hash@2 {
95 algo = \"${HASH}\";
96 };
97 };
98 "
99 FDT_PROP="fdt = \"fdt@$FDTNUM\";"
100 fi
101
102 if [ -n "${INITRD}" ]; then
103 INITRD_NODE="
104 initrd@$INITRDNUM {
105 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\";
106 ${COMPATIBLE_PROP}
107 data = /incbin/(\"${INITRD}\");
108 type = \"ramdisk\";
109 arch = \"${ARCH}\";
110 os = \"linux\";
111 hash@1 {
112 algo = \"crc32\";
113 };
114 hash@2 {
115 algo = \"${HASH}\";
116 };
117 };
118 "
119 INITRD_PROP="ramdisk=\"initrd@${INITRDNUM}\";"
120 fi
121
122
123 if [ -n "${ROOTFS}" ]; then
124 dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
125 ROOTFS_NODE="
126 rootfs@$ROOTFSNUM {
127 description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\";
128 ${COMPATIBLE_PROP}
129 data = /incbin/(\"${ROOTFS}.pagesync\");
130 type = \"filesystem\";
131 arch = \"${ARCH}\";
132 compression = \"none\";
133 hash@1 {
134 algo = \"crc32\";
135 };
136 hash@2 {
137 algo = \"${HASH}\";
138 };
139 };
140 "
141 LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs@${ROOTFSNUM}\""
142 fi
143
144 # Create a default, fully populated DTS file
145 DATA="/dts-v1/;
146
147 / {
148 description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
149 #address-cells = <1>;
150
151 images {
152 kernel@1 {
153 description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
154 data = /incbin/(\"${KERNEL}\");
155 type = \"kernel\";
156 arch = \"${ARCH}\";
157 os = \"linux\";
158 compression = \"${COMPRESS}\";
159 load = <${LOAD_ADDR}>;
160 entry = <${ENTRY_ADDR}>;
161 hash@1 {
162 algo = \"crc32\";
163 };
164 hash@2 {
165 algo = \"$HASH\";
166 };
167 };
168 ${INITRD_NODE}
169 ${FDT_NODE}
170 ${ROOTFS_NODE}
171 };
172
173 configurations {
174 default = \"${CONFIG}\";
175 ${CONFIG} {
176 description = \"OpenWrt ${DEVICE}\";
177 kernel = \"kernel@1\";
178 ${FDT_PROP}
179 ${LOADABLES:+loadables = ${LOADABLES};}
180 ${COMPATIBLE_PROP}
181 ${INITRD_PROP}
182 };
183 };
184 };"
185
186 # Write .its file to disk
187 echo "$DATA" > "${OUTPUT}"