aboutsummaryrefslogtreecommitdiff
path: root/scripts/uboot-script-gen
blob: 76d6fe813d03ecd4583d13303e3e451ffc667dd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/bin/bash

offset=$((2*1024*1024))
filesize=0
prog_req=(mkimage file fdtput)
tmp_files=()

function remove_tmp_files()
{
    for i in "${tmp_files[@]}"
    do
        rm -f "$i"
    done
}

function cleanup_and_return_err()
{
    rm -f $UBOOT_SOURCE $UBOOT_SCRIPT
    remove_tmp_files
    exit 1
}

function dt_mknode()
{
    local path=$1
    local addr=$2

    if test $UBOOT_SOURCE
    then
        echo "fdt mknod $path $addr" >> $UBOOT_SOURCE
    fi
}

# data_type is either
#   int
#   hex
#   str
#   str_a
function dt_set()
{
    local path=$1
    local var=$2
    local data_type=$3
    local data=$4


    if test $UBOOT_SOURCE
    then
        var=${var/\#/\\#}
        if test $data_type = "hex" || test $data_type = "int"
        then
            echo "fdt set $path $var <$data>" >> $UBOOT_SOURCE
        elif test $data_type = "str_a"
        then
            array=""
            for element in $data
            do
                array+=" \"$element\""
            done
            echo "fdt set $path $var $array" >> $UBOOT_SOURCE
        else
            echo "fdt set $path $var \"$data\"" >> $UBOOT_SOURCE
        fi
    fi

    if test $FDTEDIT
    then
        if test $data_type = "hex"
        then
            fdtput $FDTEDIT -p -t x $path $var $data
        elif test $data_type = "int"
        then
            fdtput $FDTEDIT -p -t i $path $var $data
        elif test $data_type = "str_a"
        then
            fdtput $FDTEDIT -p -t s $path $var $data
        else
            fdtput $FDTEDIT -p -t s $path $var "$data"
        fi
    fi
}

function add_device_tree_kernel()
{
    local path=$1
    local addr=$2
    local size=$3

    dt_mknode "$path" "module$addr"
    dt_set "$path/module$addr" "compatible" "str_a" "multiboot,kernel multiboot,module"
    dt_set "$path/module$addr" "reg" "hex"  "0x0 $addr 0x0 $(printf "0x%x" $size)"
    dt_set "$path/module$addr" "bootargs" "str" "console=ttyAMA0"
}


function add_device_tree_ramdisk()
{
    local path=$1
    local addr=$2
    local size=$3

    dt_mknode "$path"  "module$addr"
    dt_set "$path/module$addr" "compatible" "str_a" "multiboot,ramdisk multiboot,module"
    dt_set "$path/module$addr" "reg" "hex"  "0x0 $addr 0x0 $(printf "0x%x" $size)"
}


function add_device_tree_passthrough()
{
    local path=$1
    local addr=$2
    local size=$3

    dt_mknode "$path"  "module$addr"
    dt_set "$path/module$addr" "compatible" "str_a" "multiboot,device-tree multiboot,module"
    dt_set "$path/module$addr" "reg" "hex"  "0x0 $addr 0x0 $(printf "0x%x" $size)"
}

function device_tree_editing()
{
    local device_tree_addr=$1

    if test $UBOOT_SOURCE
    then
        echo "fdt addr $device_tree_addr" >> $UBOOT_SOURCE
        echo "fdt resize 1024" >> $UBOOT_SOURCE

        if test $NUM_DT_OVERLAY && test $NUM_DT_OVERLAY -gt 0
        then
            i=0
            while test $i -lt $NUM_DT_OVERLAY
            do
                echo "fdt apply ${dt_overlay_addr[$i]}" >> $UBOOT_SOURCE
                i=$(( $i + 1 ))
            done
        fi
    fi
    dt_set "/chosen" "#address-cells" "hex" "0x2"
    dt_set "/chosen" "#size-cells" "hex" "0x2"
    dt_set "/chosen" "xen,xen-bootargs" "str" "$XEN_CMD"
    dt_mknode "/chosen" "dom0"
    dt_set "/chosen/dom0" "compatible" "str_a" "xen,linux-zimage xen,multiboot-module multiboot,module"
    dt_set "/chosen/dom0" "reg" "hex" "0x0 $dom0_kernel_addr 0x0 $(printf "0x%x" $dom0_kernel_size)"
    dt_set "/chosen" "xen,dom0-bootargs" "str" "$DOM0_CMD"
    if test "$DOM0_RAMDISK" && test $dom0_ramdisk_addr != "-"
    then
        dt_mknode "/chosen" "dom0-ramdisk"
        dt_set "/chosen/dom0-ramdisk" "compatible" "str_a" "xen,linux-initrd xen,multiboot-module multiboot,module"
        dt_set "/chosen/dom0-ramdisk" "reg" "hex" "0x0 $dom0_ramdisk_addr 0x0 $(printf "0x%x" $dom0_ramdisk_size)"
    fi

    i=0
    while test $i -lt $NUM_DOMUS
    do
        if test "${DOMU_ROOTFS[$i]}" || test "${DOMU_NOBOOT[$i]}"
        then
            i=$(( $i + 1 ))
            continue
        fi
        dt_mknode "/chosen" "domU$i"
        dt_set "/chosen/domU$i" "compatible" "str" "xen,domain"
        dt_set "/chosen/domU$i" "#address-cells" "hex" "0x2"
        dt_set "/chosen/domU$i" "#size-cells" "hex" "0x2"
        dt_set "/chosen/domU$i" "memory" "int" "0 ${DOMU_MEM[$i]}"
        dt_set "/chosen/domU$i" "cpus" "int" "${DOMU_VCPUS[$i]}"
        dt_set "/chosen/domU$i" "vpl011" "hex" "0x1"
        add_device_tree_kernel "/chosen/domU$i" ${domU_kernel_addr[$i]} ${domU_kernel_size[$i]}
        if test "${domU_ramdisk_addr[$i]}"
        then
            add_device_tree_ramdisk "/chosen/domU$i" ${domU_ramdisk_addr[$i]} ${domU_ramdisk_size[$i]}
        fi
        if test "${domU_passthrough_dtb_addr[$i]}"
        then
            add_device_tree_passthrough "/chosen/domU$i" ${domU_passthrough_dtb_addr[$i]} ${domU_passthrough_dtb_size[$i]}
        fi
        i=$(( $i + 1 ))
    done
}

function add_size()
{
    local filename=$1
    local size=`stat -L --printf="%s" $filename`
    memaddr=$(( $memaddr + $size + $offset - 1))
    memaddr=$(( $memaddr & ~($offset - 1) ))
    memaddr=`printf "0x%X\n" $memaddr`
    filesize=$size
}

function load_file()
{
    local filename=$1
    local fit_scr_name=$2

    local relative_path="$(realpath --relative-to=$PWD $filename)"

    if test "$LOAD_CMD" = "imxtract"
    then
        echo "$LOAD_CMD \$fit_addr $fit_scr_name $memaddr" >> $UBOOT_SOURCE
    else
        echo "$LOAD_CMD $memaddr $relative_path" >> $UBOOT_SOURCE
    fi
    add_size $filename
}

function check_file_type()
{
    local filename=$1
    local type="$2"

    if [ ! -f $filename ]
    then
        echo "File $filename doesn't exist, exiting";
        cleanup_and_return_err
    fi

    # if file doesn't know what it is, it outputs data, so include that
    # since some executables aren't recongnized
    if [ "$type" = "executable" ]
    then
        type="executable\|data"
    # file in older distros (ex: RHEL 7.4) just output data for device
    # tree blobs
    elif [ "$type" = "Device Tree Blob" ]
    then
        type="Device Tree Blob\|data"
    fi

    file -L $filename | grep "$type" &> /dev/null
    if test $? != 0
    then
        echo Wrong file type "$filename". It should be "$type", exiting.
        cleanup_and_return_err
    fi
}

function check_compressed_file_type()
{
    local filename=$1
    local type="$2"

    if [ ! -f $filename ]
    then
        echo "File $filename doesn't exist, exiting";
        cleanup_and_return_err
    fi

    file -L $filename | grep "gzip compressed data" &> /dev/null
    if test $? == 0
    then
        local tmp=`mktemp`
        tmp_files+=($tmp)
        cat $filename | gunzip > $tmp
        filename=$tmp
    fi
    check_file_type $filename "$type"
}

function check_depends()
{
    for ((i=0; i<${#prog_req[@]}; i++))
    do
        if ! command -v ${prog_req[i]} > /dev/null
        then
            echo "Please install the needed program: ${prog_req[i]}."
            exit 1
        fi
    done
}

function print_help
{
    script=`basename "$0"`
    echo "usage:"
    echo "	$script -c CONFIG_FILE -t UBOOT_TYPE -d DIRECTORY [-o FILE] [-k KEY_DIR/HINT [-u U-BOOT_DTB]] [-e]"
    echo "	$script -h"
    echo "where:"
    echo "	CONFIG_FILE - configuration file"
    echo "	UBOOT_TYPE can be:"
    echo "		sd - alias for \"mmc load 0:1\" for uboot load commands"
    echo "		scsi - alias for \"scsi load 0:1\" for uboot load commands"
    echo "		tftp - alias for \"tftpb\" for uboot load cammnds"
    echo "		fit - creates a fit image with a boot script"
    echo "		fit_std - used for creating a standard fit image, not compatable with dom0less (not recommended)"
    echo "		< > - used for uboot load commands"
    echo "	DIRECTORY - root directory where the files of CONFIG_FILE are located"
    echo "	FILE - output filename for the uboot script and its source, overrides option in CONFIG_FILE"
    echo "	KEY_DIR - key directory used for signing a fit image"
    echo "	HINT - the file name of the crt and key file minus the suffix (ex, hint.crt and hint.key)"
    echo "	U-BOOT_DTB - u-boot control dtb so that the public key gets added to it"
    echo "	-e - uses efiboot rather than booti"
    echo "	-h - prints out the help message and exits "
    echo "Defaults:"
    echo "	CONFIG_FILE=$cfg_file, UBOOT_TYPE=\"LOAD_CMD\" env var, DIRECTORY=$uboot_dir"
    echo "Example:"
    echo "	$script -c ../config -d ./build42 -t \"scsi load 1:1\""
}

while getopts ":c:t:d:ho:k:u:e" opt; do
    case ${opt} in
    t )
        case $OPTARG in
        scsi )
            LOAD_CMD="load scsi 0:1"
            ;;
        sd )
            LOAD_CMD="load mmc 0:1"
            ;;
        tftp )
            LOAD_CMD="tftpb"
            ;;
        fit )
            LOAD_CMD="imxtract"
            ;;
        fit_std )
            LOAD_CMD="fit_std"
            ;;
        * )
            LOAD_CMD="$OPTARG"
            ;;
        esac
        ;;
    c )
        cfg_file=$OPTARG
        ;;
    d )
        uboot_dir=$OPTARG
        ;;
    o )
        UBOOT_SCRIPT_ARG=$OPTARG
        ;;
    k )
        FIT_ENC_KEY_DIR=$OPTARG
        ;;
    u )
        FIT_ENC_UB_DTB=$OPTARG
        ;;
    e )
        efi=y
        ;;
    h )
        print_help
        exit 0
        ;;
    * )
        echo "Unknown option, see \"$0 -h\""
        exit 1
        ;;
    esac
done
shift $((OPTIND -1))

if [ -z "$LOAD_CMD" ] || [ -z "$cfg_file" ] || [ -z "$uboot_dir" ]
then
    echo "Undefined arguments, see \"$0 -h\""
    exit 1
fi

check_depends

source "$cfg_file"

# CLI ARG overrides what's in the config file
if [ ! -z "$UBOOT_SCRIPT_ARG" ]
then
    UBOOT_SCRIPT="$UBOOT_SCRIPT_ARG".scr
    UBOOT_SOURCE="$UBOOT_SCRIPT_ARG".source
fi

if [ -z "$XEN_CMD" ]
then
    XEN_CMD="console=dtuart dtuart=serial0 dom0_mem=1G dom0_max_vcpus=1 bootscrub=0 vwfi=native sched=null"
fi

if [ -z "$DOM0_CMD" ]
then
    DOM0_CMD="console=hvc0 earlycon=xen earlyprintk=xen clk_ignore_unused"
fi
if [[ ! $DOM0_CMD =~ root= ]]
then
    if test -z "$DOM0_ROOTFS"
    then
        DOM0_CMD="$DOM0_CMD root=/dev/ram0"
    else
        DEV=${LOAD_CMD%:*}
        DEV=${DEV##* }
        PAR=${LOAD_CMD#*:}

        if [ -z "$DEV" ] || [ -z "$PAR" ]
        then
            echo "Could not parse device and partition."
            echo "Please make sure the load command is correct or manually set DOM0_CMD in the config file."
            exit 1
        fi

        PAR=$((PAR + 1))

        if [[ $LOAD_CMD =~ mmc ]]
        then
            DOM0_CMD="$DOM0_CMD root=/dev/mmcblk${DEV}p${PAR}"
        elif [[ $LOAD_CMD =~ scsi ]]
        then
            # converts number to a scsi device character
            DEV=$((DEV + 97))
            DEV=$(printf %x $DEV)
            DEV=$(printf "\x$DEV")

            DOM0_CMD="$DOM0_CMD root=/dev/sd${DEV}${PAR}"
        else
            echo "Only mmc and scsi are supported for automatically setting the root partition."
            echo "Manually set DOM0_CMD with the root device in the config file to bypass this."
            exit 1
        fi
    fi
fi
i=0
while test $i -lt $NUM_DOMUS
do
    if test -z "${DOMU_MEM[$i]}"
    then
        DOMU_MEM[$i]=512
    fi
    DOMU_MEM[$i]=$((${DOMU_MEM[$i]} * 1024))
    if test -z "${DOMU_VCPUS[$i]}"
    then
        DOMU_VCPUS[$i]=1
    fi
    i=$(( $i + 1 ))
done

fit_algo=$'hash {\n                algo = "md5";\n            };'
if test "$FIT_ENC_KEY_DIR" || test "$FIT_ENC_UB_DTB"
then
    if ! test "$FIT_ENC_KEY_DIR" && test "$FIT_ENC_UB_DTB"
    then
        echo "if encryption, you need to specify the key directory"
        exit 1
    fi

    key_hint="${FIT_ENC_KEY_DIR##*/}"
    key_dir="${FIT_ENC_KEY_DIR%/*}/"

    fit_enc_opt="-r -k $key_dir"
    if test "$FIT_ENC_UB_DTB"
    then
        fit_enc_opt+=" -K $FIT_ENC_UB_DTB"
    fi
    fit_algo=$'signature {\n                algo = \"sha1,rsa2048\";\n                key-name-hint = \"'"$key_hint"$'\";\n};'
fi

# the cd is needed so that the relative paths will match once we use
# tftp or move the files to a partition
cd "$uboot_dir"

if test "$LOAD_CMD" = "fit_std" || test "$LOAD_CMD" = "imxtract"
then
     if ! test $FDTEDIT && test "$LOAD_CMD" = "fit_std"
     then
         FDTEDIT=${DEVICE_TREE%.dtb}
         FDTEDIT+=-fit.dtb
     fi
     fit=${UBOOT_SOURCE%.source}
     its_file=$fit.its
     fit+=.fit
     rm -f "$its_file"
fi

if test $FDTEDIT
then
     rm -f $FDTEDIT
     cp $DEVICE_TREE $FDTEDIT
fi

rm -f $UBOOT_SOURCE $UBOOT_SCRIPT

if test "$LOAD_CMD" = "imxtract"
then
    echo 'fit_addr=$fileaddr' >>  $UBOOT_SOURCE
fi

memaddr=$(( $MEMORY_START + $offset ))
# 12582912 is 0xc00000, 12MB
if test $memaddr -lt 12582912
then
    memaddr="12582912"
fi
memaddr=`printf "0x%X\n" $memaddr`
uboot_addr=$memaddr
# 2MB are enough for a uboot script
memaddr=$(( $memaddr + $offset ))
memaddr=`printf "0x%X\n" $memaddr`

check_compressed_file_type $XEN "executable"
xen_addr=$memaddr
load_file "$XEN" "host_xen"

check_compressed_file_type $DOM0_KERNEL "executable"
dom0_kernel_addr=$memaddr
load_file $DOM0_KERNEL "dom0_linux"
dom0_kernel_size=$filesize

if test "$DOM0_RAMDISK"
then
    check_compressed_file_type $DOM0_RAMDISK "cpio archive"
    dom0_ramdisk_addr=$memaddr
    load_file "$DOM0_RAMDISK" "dom0_ramdisk"
    dom0_ramdisk_size=$filesize
else
    dom0_ramdisk_addr="-"
fi

i=0
while test $i -lt $NUM_DOMUS
do
    if test "${DOMU_ROOTFS[$i]}" || test "${DOMU_NOBOOT[$i]}"
    then
        if test -z "${DOMU_NOBOOT[$i]}"
        then
            echo "Skipping DomU[$i]: cannot handle non-ramdisk rootfs for dom0less VMs."
        fi
        i=$(( $i + 1 ))
        continue
    fi
    check_compressed_file_type ${DOMU_KERNEL[$i]} "executable"
    domU_kernel_addr[$i]=$memaddr
    load_file ${DOMU_KERNEL[$i]} "domU${i}_kernel"
    domU_kernel_size[$i]=$filesize
    if test "${DOMU_RAMDISK[$i]}"
    then
        check_compressed_file_type ${DOMU_RAMDISK[$i]} "cpio archive"
        domU_ramdisk_addr[$i]=$memaddr
        load_file ${DOMU_RAMDISK[$i]} "domU${i}_ramdisk"
        domU_ramdisk_size[$i]=$filesize
    fi
    if test "${DOMU_PASSTHROUGH_DTB[$i]}"
    then
        check_compressed_file_type ${DOMU_PASSTHROUGH_DTB[$i]} "Device Tree Blob"
        domU_passthrough_dtb_addr[$i]=$memaddr
        load_file ${DOMU_PASSTHROUGH_DTB[$i]} "domU${i}_fdt"
        domU_passthrough_dtb_size[$i]=$filesize
    fi
    i=$(( $i + 1 ))
done

if test $NUM_DT_OVERLAY && test $NUM_DT_OVERLAY -gt 0
then
    i=0
    while test $i -lt $NUM_DT_OVERLAY
    do
        if [ ! -f "${DT_OVERLAY[$i]}" ]
        then
            echo "Can not find ${DT_OVERLAY[$i]}, exiting"
            cleanup_and_return_err
        fi
        check_file_type "${DT_OVERLAY[$i]}" "Device Tree Blob"
        dt_overlay_addr[$i]=$memaddr
        load_file "${DT_OVERLAY[$i]}" "host_fdto${i}"
        i=$(( $i + 1 ))
    done
fi

check_file_type $DEVICE_TREE "Device Tree Blob"
device_tree_addr=$memaddr
load_file $DEVICE_TREE "host_fdt"
device_tree_editing $device_tree_addr

# disable device tree reloation
echo "setenv fdt_high 0xffffffffffffffff" >> $UBOOT_SOURCE
if test "$efi" && test "$efi" = "y"
then
    echo "bootefi $xen_addr $device_tree_addr" >> $UBOOT_SOURCE
else
    echo "booti $xen_addr - $device_tree_addr" >> $UBOOT_SOURCE
fi

if test "$fit"
then
    # create start along with necessary binaries
    load_files="\"dom0_linux\""
    if test "$LOAD_CMD" = "imxtract"
    then
        its_dt="$DEVICE_TREE"
    else
        its_dt="$FDTEDIT"
    fi
    cat >> "$its_file" <<- EOF
/dts-v1/;
/ {
    description = "Configuration to load Xen";
    #address-cells = <1>;
    images {
        host_xen {
            description = "host xen binary";
            data = /incbin/("$XEN");
            type = "kernel";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <$xen_addr>;
            entry = <$xen_addr>;
            $fit_algo
        };
        host_fdt {
            description = "host fdt";
            data = /incbin/("$its_dt");
            type = "flat_dt";
            arch = "arm64";
            compression = "none";
            load = <$device_tree_addr>;
            $fit_algo
        };
        dom0_linux {
            description = "dom0 linux kernel binary";
            data = /incbin/("$DOM0_KERNEL");
            type = "kernel";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <$dom0_kernel_addr>;
            $fit_algo
        };
	EOF
    if test "$DOM0_RAMDISK"
    then
        load_files+=", \"dom0_ramdisk\""
        cat >> "$its_file" <<- EOF
        dom0_ramdisk {
            description = "dom0 ramdisk";
            data = /incbin/("$DOM0_RAMDISK");
            type = "ramdisk";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <$dom0_ramdisk_addr>;
            $fit_algo
        };
	EOF
    fi
    # domUs
    i=0
    while test $i -lt $NUM_DOMUS
    do
        if test "${DOMU_ROOTFS[$i]}" || test "${DOMU_NOBOOT[$i]}"
        then
            i=$(( $i + 1 ))
            continue
        fi
        load_files+=", \"domU${i}_kernel\""
        cat >> "$its_file" <<- EOF
        domU${i}_kernel {
            description = "domU${i} kernel binary";
            data = /incbin/("${DOMU_KERNEL[$i]}");
            type = "kernel";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <${domU_kernel_addr[$i]}>;
            $fit_algo
        };
	EOF
        if test "${DOMU_RAMDISK[$i]}"
        then
            load_files+=", \"domU${i}_ramdisk\""
            cat >> "$its_file" <<- EOF
        domU${i}_ramdisk {
            description = "domU${i} ramdisk";
            data = /incbin/("${DOMU_RAMDISK[$i]}");
            type = "ramdisk";
            arch = "arm64";
            os = "linux";
            compression = "none";
            load = <${domU_ramdisk_addr[$i]}>;
            $fit_algo
        };
	EOF
        fi
        if test "${DOMU_PASSTHROUGH_DTB[$i]}"
        then
            load_files+=", \"domU${i}_fdt\""
            cat >> "$its_file" <<- EOF
        domU${i}_fdt {
            description = "domU${i} fdt";
            data = /incbin/("${DOMU_PASSTHROUGH_DTB[$i]}");
            type = "flat_dt";
            arch = "arm64";
            compression = "none";
            load = <${domU_passthrough_dtb_addr[$i]}>;
            $fit_algo
        };
	EOF
        fi
        i=$(( $i + 1 ))
    done
    fdt_line="fdt = \"host_fdt\""
    if test $NUM_DT_OVERLAY && test $NUM_DT_OVERLAY -gt 0
    then
        i=0
        while test $i -lt $NUM_DT_OVERLAY
        do
            cat >> "$its_file" <<- EOF
        host_fdto${i} {
            description = "host fdt overlay ${i}";
            data = /incbin/("${DT_OVERLAY[$i]}");
            type = "flat_dt";
            arch = "arm64";
            compression = "none";
            load = <${dt_overlay_addr[$i]}>;
            $fit_algo
        };
	EOF
            fdt_line+=", \"host_fdto${i}\""
            i=$(( $i + 1 ))
        done
    fi
    fdt_line+=";"
    # script for fit
    if test "$LOAD_CMD" = "imxtract"
    then
        cat >> "$its_file" <<- EOF
        boot_scr {
            description = "imagebuilder's boot script";
            data = /incbin/("$UBOOT_SOURCE");
            type = "script";
            compression = "none";
            load = <$uboot_addr>;
            entry = <$uboot_addr>;
            $fit_algo
        };
	EOF
    fi
    # end images
    echo '    };' >> "$its_file"
    # config, signing requires a config even if it isn't used
    cat >> "$its_file" <<- EOF
    configurations {
        default = "config";
        config {
            description = "Xen";
            kernel = "host_xen";
            $fdt_line
            loadables = $load_files;
        };
    };
	EOF
    # end
    echo '};' >> "$its_file"

    mkimage -q -f "$its_file" $fit_enc_opt "$fit"
else
    mkimage -A arm64 -T script -C none -a $uboot_addr -e $uboot_addr -d $UBOOT_SOURCE "$UBOOT_SCRIPT" &> /dev/null
fi

remove_tmp_files

fit_addr="$(printf "0x%x" $memaddr)"

if test "$fit"
then
    memaddr=$(( $MEMORY_END - 2 * ( $memaddr + $offset ) ))
else
    memaddr=$(( $MEMORY_END - $memaddr - $offset ))
fi
if test $memaddr -lt 0
then
    echo Error, not enough memory to load all binaries
    cleanup_and_return_err
fi

if test "$fit"
then
    echo "Generated uboot FIT image  $fit, to be loaded at or after address $fit_addr:"
    if test "$LOAD_CMD" = "imxtract"
    then
        echo "tftpb/load mmc 0:1/etc $fit_addr $fit; source $fit_addr:boot_scr"
    else
        echo "tftpb/load mmc 0:1/etc $fit_addr $fit; bootm $fit_addr#config"
    fi
else
    echo "Generated uboot script $UBOOT_SCRIPT, to be loaded at address $uboot_addr:"
    echo "$LOAD_CMD $uboot_addr $UBOOT_SCRIPT; source $uboot_addr"
fi