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