2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Author: Jason Wu <jason.hy.wu@gmail.com>
5 # with modifications for multi-DTB-same-image by:
6 # Mathew McBride <matt@traverse.com.au>
8 # U-Boot firmware supports the booting of images in the Flattened Image
9 # Tree (FIT) format. The FIT format uses a device tree structure to
10 # describe a kernel image, device tree blob, ramdisk, etc. This script
11 # creates an Image Tree Source (.its file) which can be passed to the
12 # 'mkimage' utility to generate an Image Tree Blob (.itb file). The .itb
13 # file can then be booted by U-Boot (or other bootloaders which support
14 # FIT images). See doc/uImage.FIT/howto.txt in U-Boot source code for
15 # additional information on FIT images.
17 # This tools supports:
18 # - multi-configuration
19 # - multi-image support - multiple kernel/fdt/ramdsik
20 # - per image configuration:
21 # - hash algorithm and generated required subnodes
23 # - signature and generated required subnodes
32 # declare main data array
36 # initialize array with empty values
37 for (( index
=1; index
<=$MAX_IMG; index
++ )); do
40 eval img
${index}[$i]=""
44 for (( index
=1; index
<=$MAX_CONF; index
++ )); do
47 eval conf
${index}[$i]=""
51 # imgX array index information
52 # 0: type of image - kernel, fdt, ramdsik
55 # 3: loadaddr of image
56 # 4: entrypoint of image
59 # 7: part of the configuration
60 # 8: Human friend name for the image
63 # 11: conf friendly name
65 # confX array index information
73 # 7: kernel sign_algorithm
74 # 8: fdt sign_algorithm
75 # 9: rootfs sign_algorithm
76 # 10: conf friendly name
79 echo "Usage: `basename $0` -A arch -v version -o its_file" \
80 "-k kernel -a addr -e entry [-C none] [-h sha1] [-c conf]"
81 echo -e "Example1:\n\tkernel image ker_img1 with no compression +"
82 echo -e "\tsha1 hash + fdt dtb1 with sha1 and crc32 hash for conf 1"
83 echo -e "\t $ `basename $0` -A arm -v 4.4 \ "
84 echo -e "\t -k ker_img1 -C none -h sha1 -e 0x8000 -a 0x8000 -c 1 \ "
85 echo -e "\t -d dtb1 -h sha1 -h crc32 -c 1\n"
86 echo "General settings:"
87 echo -e "\t-A ==> set architecture to 'arch'"
88 echo -e "\t-v ==> set kernel version to 'version'"
89 echo -e "\t-o ==> create output file 'its_file' [optional]"
90 echo "Input image type:"
91 echo -e "\t-k ==> kernel image 'kernel'"
92 echo -e "\t-d ==> Device Tree Blob 'dtb'"
93 echo -e "\t-r ==> ramdisk image 'ramdisk"
94 echo "Per image configurations:"
95 echo -e "\t-C ==> set compression type 'comp'"
96 echo -e "\t-c ==> set image config (multiple -c allowed)"
97 echo -e "\t-a ==> set load address to 'addr' (hex)"
98 echo -e "\t-e ==> set entry point to 'entry' (hex)"
99 echo -e "\t-D ==> human friendly 'name' (one word only)"
100 echo -e "\t-h ==> set hash algorithm (multiple -h allowed)"
101 echo -e "\t-s ==> set signature for given config image"
102 echo -e "\t-K ==> set key file for given config image"
112 if echo $1 |
grep -q img
; then
115 let a
=$
(echo $1 |
awk -F "img" '{print $2}')
116 elif echo $1 |
grep -q conf
; then
119 let a
=$
(echo $1 |
awk -F "conf" '{print $2}')
121 if [ ${a} -lt 0 -o ${a} -gt ${max_a} -o \
122 ${2} -lt 0 -o ${2} -gt ${max_i} ]; then
123 echo "WARNING: Invalid array name, skipping!!!"
132 # $4: append operation
136 # check if array is declared
137 array_check
$1 $2 ||
return 0
141 eval $1[$2]=\"\
${$1[$2]} $3\"
157 local i
=-1 k
=-1 d
=-1 r
=-1
158 while getopts ":A:a:C:c:D:d:e:h:k:K:o:v:r:s:n:" OPTION
; do
161 a
) array_put img
$i 3 $OPTARG;;
162 C
) value_sanity_chk compression
$OPTARG;
163 array_put img
$i 5 $OPTARG;;
164 c
) array_put img
$i 7 $OPTARG append
;;
165 D
) array_put img
$i 8 $OPTARG;;
169 array_put img
$i 0 fdt
;
170 array_put img
$i 1 $OPTARG;
171 array_put img
$i 2 $d;
173 e
) array_put img
$i 4 $OPTARG;;
174 h
) value_sanity_chk
hash $OPTARG;
175 array_put img
$i 6 $OPTARG append
;;
179 array_put img
$i 0 "kernel";
180 array_put img
$i 1 $OPTARG;
181 array_put img
$i 2 $k;
183 K
) array_put img
$i 9 $OPTARG;;
184 n
) array_put img
$i 11 $OPTARG;;
186 v
) VERSION
=$OPTARG;;
190 array_put img
$i 0 "ramdisk";
191 array_put img
$i 1 $OPTARG;
192 array_put img
$i 2 $r;
194 s
) value_sanity_chk signature
$OPTARG;
195 array_put img
$i 10 $OPTARG;
197 * ) echo "Invalid option passed to '$0' (options:$@)"
201 shift $
(($OPTIND - 1))
203 echo "Failed to parse all passed arguments (unrecognized: \"$@\")"
206 [ -n "${OUTPUT}" ] || OUTPUT
=fitimage.its
207 [ -n "${VERSION}" ] || VERSION
="Unknown"
208 [ -n "${ARCH}" ] || ARCH
=arm
212 # sanity check for signature, compression and hash
218 signature
) valid
="sha-1,rsa-2048 sha-256,rsa-2048 sha-256,rsa-4096";;
219 compression
) valid
="gzip bzip2 none";;
220 hash) valid
="sha1 md5 crc32";;
222 if ! echo $valid |
grep -q "$2"; then
223 echo "Error: Invalid $1 provided '$2'"
224 echo "Valid options are: $valid"
230 # Emit the fitImage section bits
232 # $1: Section bit type: fitstart - its header
233 # imagestart - image section start
234 # confstart - configuration section start
235 # sectend - section end
236 # fitend - fitimage end
237 # $2: optional variable for confstart section
242 cat << EOF > ${OUTPUT}
246 description = "U-Boot fitImage for ${VERSION} kernel";
247 #address-cells = <1>;
251 echo -e "\n\timages {" >> ${OUTPUT};;
253 # echo -e "\tconfigurations {\n\t\tdefault = \"conf@${2:-0}\";" \
254 echo -e "\tconfigurations {\n" \
257 echo -e "\t};" >> ${OUTPUT};;
259 echo -e "};" >> ${OUTPUT};;
264 # Emit kernel image node
268 local count
=${2:-${MAX_IMG}}
269 local loaddaddr
=${3:-0x8000}
270 local entrypoint
=${4:-0x8000}
271 local compresson
=${5:-none}
272 local checksum
=${6:-sha1}
275 [ -z "${name}" ] || name
=" ${name}"
276 cat << EOF >> ${OUTPUT}
278 description = "Linux Kernel${name}";
279 data = /incbin/("${image}");
283 compression = "${compresson}";
284 load = <${loaddaddr}>;
285 entry = <${entrypoint}>;
287 emit_cksum
${checksum}
289 if [ -z "$SIGN_IN_CONF" ] ; then
290 emit_signature
"$9" "" "" "$8" "" ""
293 echo " };" >> ${OUTPUT}
301 local count
=${2:-${MAX_IMG}}
302 local compresson
=${3:-none}
303 local checksum
=${4:-sha1}
307 [ -z "${name}" ] || name
=" ${name}"
308 cat << EOF >> ${OUTPUT}
310 description = "Flattened Device Tree blob${name}";
311 data = /incbin/("${image}");
314 load = <${loadaddr}>;
315 compression = "none";
317 emit_cksum
${checksum}
318 if [ -z "$SIGN_IN_CONF" ] ; then
319 emit_signature
"" "$7" "" "" "$6" ""
321 echo " };" >> ${OUTPUT}
329 local count
=${2:-${MAX_IMG}}
330 local compresson
=${3:-none}
331 local checksum
=${4:-sha1}
334 [ -z "${name}" ] || name
=" ${name}"
335 cat << EOF >> ${OUTPUT}
337 description = "ramdisk${name}";
338 data = /incbin/("${image}");
342 compression = "${compresson}";
344 emit_cksum
${checksum}
345 if [ -z "$SIGN_IN_CONF" ] ; then
346 emit_signature
"" "" "$7" "" "" "$6"
348 echo " };" >> ${OUTPUT}
352 # Emit check sum sub node
357 for csum
in ${csum_list}; do
358 cat << EOF >> ${OUTPUT}
363 count
=`expr ${count} + 1`
368 # Emit signature sub node
379 local chk_list
="" algo
="" algos
="" i
=""
381 for i
in kernel fdt rootfs
; do
384 [ -n "$algo" ] ||
continue
385 if ! echo "$algos" |
grep -q $algo; then
386 if [ -z "$algos" ]; then
389 algos
="${algos} $algo"
392 if ! echo "$keys" |
grep -q $key; then
393 if [ -z "$keys" ]; then
401 for algo
in $algos; do
404 for i
in kernel fdt rootfs
; do
406 eval tmp_key
=\$
${i}_key
407 [ "$tmp_algo" == "$algo" ] ||
continue
408 [ "$tmp_key" == "$key" ] ||
continue
409 if [ -z "$img" ]; then
416 [ -n "$img" ] ||
continue
417 cat << EOF >> ${OUTPUT}
420 key-name-hint = "${key}";
422 if [ -n "$SIGN_IN_CONF" ] ; then
423 echo " sign-images = \"$img\";" >> ${OUTPUT}
425 echo " };" >> ${OUTPUT}
427 count
=`expr ${count} + 1`
433 # Emit config sub nodes
436 local conf_csum
="sha1"
438 config_name
="conf@${1}"
439 if [ ! -z "${11}" ]; then
442 if [ -z "${2}" ]; then
443 echo "Error: config has no kernel img, skipping conf node!"
447 # Test if we have any DTBs at all
448 if [ -z "${3}" ] ; then
449 conf_desc
="Boot Linux kernel"
452 conf_desc
="Boot Linux kernel with FDT blob"
457 # Test if we have any ROOTFS at all
458 if [ -n "${4}" ] ; then
459 conf_desc
="$conf_desc + ramdisk"
460 fdt_line
="${fdt_line}
461 ramdisk = \"ramdisk@${4}\";"
464 kernel_line
="kernel = \"kernel@${2}\";"
466 cat << EOF >> ${OUTPUT}
468 description = "${conf_desc}";
469 ${kernel_line}${fdt_line}
471 algo = "${conf_csum}";
474 if [ -n "$SIGN_IN_CONF" ] ; then
475 emit_signature
"$5" "$6" "$7" "$8" "$9" "${10}"
478 echo " };" >> ${OUTPUT}
482 # remove prefix space
484 remove_prefix_space
()
486 echo "$@" |
sed "s:^ ::g"
490 # generate image nodes and its subnodes
494 local t img_c img_i img_index chk
495 local img_type img_path img_count img_loadadr img_entrypoint \
496 img_compression img_hash img_conf img_name img_key img_sign \
500 for t
in "kernel" "fdt" "ramdisk"; do
502 for a
in ${img_array[@]}; do
503 img_type
=$
(array_get
$a 0)
504 img_path
=$
(array_get
$a 1)
505 img_count
=$
(array_get
$a 2)
506 img_loadadr
=$
(array_get
$a 3)
507 img_entrypoint
=$
(array_get
$a 4)
508 img_compression
=$
(array_get
$a 5)
509 img_hash
=$
(array_get
$a 6)
510 img_conf
=$
(array_get
$a 7)
511 img_name
=$
(array_get
$a 8)
512 img_key
=$
(array_get
$a 9)
513 img_sign
=$
(array_get
$a 10)
514 img_cname
=$
(array_get
$a 11)
516 img_conf
=$
(remove_prefix_space
$img_conf)
517 img_hash
=$
(remove_prefix_space
$img_hash)
519 [ "${img_type}" == $t ] ||
continue
522 [ -n "${chk}" ] ||
eval DEF_
$t=$img_count
524 kernel
) emit_kernel
"$img_path" "$img_count" \
525 "$img_loadadr" "$img_entrypoint" \
526 "$img_compression" "$img_hash" \
527 "$img_name" "$img_key" "$img_sign";;
528 fdt
) emit_fdt
"$img_path" "$img_count" \
529 "$img_compression" "$img_hash" \
530 "$img_name" "$img_loadadr" "$img_key" "$img_sign" ;;
532 ramdisk
) emit_ramdisk
"$img_path" "$img_count" \
533 "$img_compression" "$img_hash" \
534 "$img_name" "$img_key" "$img_sign";;
537 # set up configuration data
538 for img_c
in $img_conf; do
540 #set up default configuration if its not set
541 [ -n "$DEF_CONFIG" ] || DEF_CONFIG
=$img_c
542 [ -z "${img_c}" ] || conf_array
[$img_c]=conf
$img_c
543 array_put conf
$img_c 0 ${img_c}
549 array_put conf
$img_c $img_i $img_index
550 array_put conf
$img_c $
(($img_i + 3)) ${img_sign}
551 array_put conf
$img_c $
(($img_i + 6)) ${img_key}
552 array_put conf
$img_c 10 $img_cname
554 img_index
=$
((img_index
+ 1))
561 # generate configuration node and its subnodes
563 emit_configuration_nodes
()
565 local count kernel fdt ramdisk ker_file fdt_file rfs_file ker_sign \
567 emit_its confstart
$DEF_CONFIG
568 for a
in ${conf_array[@]}; do
569 count
=$
(array_get
$a 0)
570 kernel
=$
(array_get
$a 1)
571 fdt
=$
(array_get
$a 2)
572 ramdisk
=$
(array_get
$a 3)
573 er_file
=$
(array_get
$a 4)
574 fdt_file
=$
(array_get
$a 5)
575 rfs_file
=$
(array_get
$a 6)
576 ker_sign
=$
(array_get
$a 7)
577 fdt_sign
=$
(array_get
$a 8)
578 rfs_sign
=$
(array_get
$a 9)
579 cname
=$
(array_get
$a 10)
580 emit_config
"$count" "$kernel" "$fdt" "$ramdisk" "$ker_file" \
581 "$fdt_file" "$rfs_file" "$ker_sign" "$fdt_sign" \
582 "$rfs_sign" "${cname}"
584 if [ -z "${DEF_CONFIG}" ]; then
585 emit_config
"0" "$DEF_kernel" "$DEF_fdt" "$DEF_ramdisk"
590 # Set to none empty to create signature sub node under images node
591 SIGN_IN_CONF
=${SIGN_IN_CONF:-""}
592 # Set to default config used
593 DEF_CONFIG
=${DEF_CONFIG:-""}
599 emit_configuration_nodes