b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | [ "$FDTADDR" ] && { |
| 92 | DTADDR="$FDTADDR" |
| 93 | } |
| 94 | |
| 95 | # Conditionally create fdt information |
| 96 | if [ -n "${DTB}" ]; then |
| 97 | FDT_NODE=" |
| 98 | fdt${REFERENCE_CHAR}$FDTNUM { |
| 99 | description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\"; |
| 100 | ${COMPATIBLE_PROP} |
| 101 | data = /incbin/(\"${DTB}\"); |
| 102 | type = \"flat_dt\"; |
| 103 | ${DTADDR:+load = <${DTADDR}>;} |
| 104 | arch = \"${ARCH}\"; |
| 105 | compression = \"none\"; |
| 106 | hash${REFERENCE_CHAR}1 { |
| 107 | algo = \"crc32\"; |
| 108 | }; |
| 109 | hash${REFERENCE_CHAR}2 { |
| 110 | algo = \"${HASH}\"; |
| 111 | }; |
| 112 | }; |
| 113 | " |
| 114 | FDT_PROP="fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\";" |
| 115 | fi |
| 116 | |
| 117 | if [ -n "${INITRD}" ]; then |
| 118 | INITRD_NODE=" |
| 119 | initrd${REFERENCE_CHAR}$INITRDNUM { |
| 120 | description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\"; |
| 121 | ${COMPATIBLE_PROP} |
| 122 | data = /incbin/(\"${INITRD}\"); |
| 123 | type = \"ramdisk\"; |
| 124 | arch = \"${ARCH}\"; |
| 125 | os = \"linux\"; |
| 126 | hash${REFERENCE_CHAR}1 { |
| 127 | algo = \"crc32\"; |
| 128 | }; |
| 129 | hash${REFERENCE_CHAR}2 { |
| 130 | algo = \"${HASH}\"; |
| 131 | }; |
| 132 | }; |
| 133 | " |
| 134 | INITRD_PROP="ramdisk=\"initrd${REFERENCE_CHAR}${INITRDNUM}\";" |
| 135 | fi |
| 136 | |
| 137 | |
| 138 | if [ -n "${ROOTFS}" ]; then |
| 139 | dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync |
| 140 | ROOTFS_NODE=" |
| 141 | rootfs${REFERENCE_CHAR}$ROOTFSNUM { |
| 142 | description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\"; |
| 143 | ${COMPATIBLE_PROP} |
| 144 | data = /incbin/(\"${ROOTFS}.pagesync\"); |
| 145 | type = \"filesystem\"; |
| 146 | arch = \"${ARCH}\"; |
| 147 | compression = \"none\"; |
| 148 | hash${REFERENCE_CHAR}1 { |
| 149 | algo = \"crc32\"; |
| 150 | }; |
| 151 | hash${REFERENCE_CHAR}2 { |
| 152 | algo = \"${HASH}\"; |
| 153 | }; |
| 154 | }; |
| 155 | " |
| 156 | LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs${REFERENCE_CHAR}${ROOTFSNUM}\"" |
| 157 | fi |
| 158 | |
| 159 | # add DT overlay blobs |
| 160 | FDTOVERLAY_NODE="" |
| 161 | OVCONFIGS="" |
| 162 | [ "$DTOVERLAY" ] && for overlay in $DTOVERLAY ; do |
| 163 | overlay_blob=${overlay##*:} |
| 164 | ovname=${overlay%%:*} |
| 165 | ovnode="fdt-$ovname" |
| 166 | ovsize=$(wc -c "$overlay_blob" | awk '{print $1}') |
| 167 | echo "$ovname ($overlay_blob) : $ovsize" >&2 |
| 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 | compression = \"none\"; |
| 177 | hash${REFERENCE_CHAR}1 { |
| 178 | algo = \"crc32\"; |
| 179 | }; |
| 180 | hash${REFERENCE_CHAR}2 { |
| 181 | algo = \"${HASH}\"; |
| 182 | }; |
| 183 | }; |
| 184 | " |
| 185 | OVCONFIGS="$OVCONFIGS |
| 186 | |
| 187 | $ovname { |
| 188 | description = \"OpenWrt ${DEVICE} overlay $ovname\"; |
| 189 | fdt = \"$ovnode\"; |
| 190 | ${COMPATIBLE_PROP} |
| 191 | }; |
| 192 | " |
| 193 | done |
| 194 | |
| 195 | # Create a default, fully populated DTS file |
| 196 | DATA="/dts-v1/; |
| 197 | |
| 198 | / { |
| 199 | description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\"; |
| 200 | #address-cells = <1>; |
| 201 | |
| 202 | images { |
| 203 | kernel${REFERENCE_CHAR}1 { |
| 204 | description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\"; |
| 205 | data = /incbin/(\"${KERNEL}\"); |
| 206 | type = \"kernel\"; |
| 207 | arch = \"${ARCH}\"; |
| 208 | os = \"linux\"; |
| 209 | compression = \"${COMPRESS}\"; |
| 210 | load = <${LOAD_ADDR}>; |
| 211 | entry = <${ENTRY_ADDR}>; |
| 212 | hash${REFERENCE_CHAR}1 { |
| 213 | algo = \"crc32\"; |
| 214 | }; |
| 215 | hash${REFERENCE_CHAR}2 { |
| 216 | algo = \"$HASH\"; |
| 217 | }; |
| 218 | }; |
| 219 | ${INITRD_NODE} |
| 220 | ${FDT_NODE} |
| 221 | ${FDTOVERLAY_NODE} |
| 222 | ${ROOTFS_NODE} |
| 223 | }; |
| 224 | |
| 225 | configurations { |
| 226 | default = \"${CONFIG}\"; |
| 227 | ${CONFIG} { |
| 228 | description = \"OpenWrt ${DEVICE}\"; |
| 229 | kernel = \"kernel${REFERENCE_CHAR}1\"; |
| 230 | ${FDT_PROP} |
| 231 | ${LOADABLES:+loadables = ${LOADABLES};} |
| 232 | ${COMPATIBLE_PROP} |
| 233 | ${INITRD_PROP} |
| 234 | }; |
| 235 | ${OVCONFIGS} |
| 236 | }; |
| 237 | };" |
| 238 | |
| 239 | # Write .its file to disk |
| 240 | echo "$DATA" > "${OUTPUT}" |