aboutsummaryrefslogtreecommitdiff
path: root/docker-extras/imagebuilder_run
blob: 40f88c567f73a56a9311b2f9a88d2c88d54c4d29 (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
#!/bin/bash

offset=$((2*1024*1024))

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

    echo "            module@$addr {" >> $temp
    echo "                compatible = \"multiboot,kernel\", \"multiboot,module\";" >> $temp
    echo "                reg = <0x0 "$addr" 0x0 "$size">;" >> $temp
    echo "                bootargs = \"console=ttyAMA0\";" >> $temp
    echo "            };" >> $temp
}

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

    echo "            module@$addr {" >> $temp
    echo "                compatible = \"multiboot,ramdisk\", \"multiboot,module\";" >> $temp
    echo "                reg = <0x0 "$addr" 0x0 "$size">;" >> $temp
    echo "            };" >> $temp
}

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

    echo "            module@$addr {" >> $temp
    echo "                compatible = \"multiboot,device-tree\", \"multiboot,module\";" >> $temp
    echo "                reg = <0x0 "$addr" 0x0 "$size">;" >> $temp
    echo "            };" >> $temp
}

function add_device_tree()
{
    local i=0
    local filename=$1

    echo "        #address-cells = <0x2>;" >> $temp
    echo "        #size-cells = <0x2>;" >> $temp
    echo "        xen,xen-bootargs = \"console=dtuart dtuart=serial0 dom0_mem=700M dom0_max_vcpus=1 bootscrub=0 serrors=forward vwfi=native sched=null\";" >> $temp
    echo "        dom0 {" >> $temp
    echo "           compatible = \"xen,linux-zimage\", \"xen,multiboot-module\";" >> $temp
    echo "           reg = <0x0 "$dom0_kernel_addr" 0x0 "$dom0_kernel_size">;" >> $temp
    echo "           bootargs = \"console=hvc0 earlycon=xen earlyprintk=xen\";" >> $temp
    echo "        };" >> $temp

    while test $i -lt $NUM_DOMUS
    do
        echo "        domU$i {" >> $temp
        echo "            compatible = \"xen,domain\";" >> $temp
        echo "            #address-cells = <0x2>;" >> $temp
        echo "            #size-cells = <0x2>;" >> $temp
        echo "            memory = <0x0 0x20000>;" >> $temp
        echo "            cpus = <0x1>;" >> $temp
        echo "            vpl011;" >> $temp
        add_device_tree_kernel ${domU_kernel_addr[$i]} ${domU_kernel_size[$i]}
        if test "${domU_ramdisk_addr[$i]}"
        then
            add_device_tree_ramdisk ${domU_ramdisk_addr[$i]} ${domU_ramdisk_size[$i]}
        fi
        if test "${domU_passthrough_dtb_addr[$i]}"
        then
            add_device_tree_passthrough ${domU_passthrough_dtb_addr[$i]} ${domU_passthrough_dtb_size[$i]}
        fi
        echo "        };" >> $temp
        i=$(( $i + 1 ))
    done
}

function filter_device_tree()
{
    local filename_dtb=$1
    local filename_dts="`basename -s .dtb $filename_dtb`".dts
    local temp=`mktemp`

    local skip=0
    local chosen=0

    mv -f $filename_dts "$filename_dts".bak
    dtc -I dtb -O dts $filename_dtb > $filename_dts 2>/dev/null

    while IFS= read -r line
    do
        if [[ $line == *"chosen"* ]]
        then
            chosen=1
        fi
        if [[ $chosen -eq 1 && ($line == *"address-cells"* || $line == *"size-cells"*) ]]
        then
            continue
        fi
        if [[ $line == *"dom0"* || $line == *"domU"* ]]
        then
            skip=1
            continue
        fi
        if [[ $skip -eq 1 && $line == *"{"* ]]
        then
            skip=2
        fi
        if [[ $skip -gt 0 && $line == *"};"* ]]
        then
            skip=$(( $skip - 1 ))
            continue
        fi
        if [[ $chosen -eq 1 && $skip -eq 0 && $line == *"};"* ]]
        then
            chosen=0;
            add_device_tree
        fi

        if [[ $skip -eq 0 ]]
        then
            echo "$line" >> $temp
        fi

    done < $filename_dts

    mv -f $filename_dtb "$filename_dtb".bak
    dtc -I dts -O dtb $temp > $filename_dtb 2>/dev/null
    mv $temp $filename_dts
}

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

function load_file()
{
    local filename=$1

    echo "$LOAD_CMD $memaddr $filename" >> $UBOOT_SOURCE
    add_size $filename
}

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

    file $filename | grep "$type" &> /dev/null
    if test $? != 0
    then
        echo Wrong file type "$filename". It shold be "$type".
    fi
}

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

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

rm -rf /tmp/output
mkdir /tmp/output

cd /home/builder
for d in $(ls); do
  pushd $d
  cp * /tmp/output
  popd
done

cd /tmp/output

. config

rm -f $UBOOT_SOURCE $UBOOT_SCRIPT
memaddr=$(( $MEMORY_START + $offset ))
memaddr=`printf "0x%X\n" $memaddr`

check_compressed_file_type $XEN "MS-DOS executable"
xen_addr=$memaddr
mkimage -A arm64 -T kernel -C none -a $xen_addr -e $xen_addr -d $XEN "$XEN".uboot &> /dev/null
load_file "$XEN".uboot

check_compressed_file_type $DOM0_KERNEL "MS-DOS executable"
dom0_kernel_addr=$memaddr
load_file $DOM0_KERNEL
dom0_kernel_size=$(( $memaddr - $dom0_kernel_addr ))

if test "$DOM0_RAMDISK"
then
    check_compressed_file_type $DOM0_RAMDISK "cpio archive"
    dom0_ramdisk_addr=$memaddr
    mkimage -A arm64 -T ramdisk -C gzip -a $dom0_ramdisk_addr -e $dom0_ramdisk_addr -d $DOM0_RAMDISK "$DOM0_RAMDISK".uboot &> /dev/null
    load_file "$DOM0_RAMDISK".uboot
else
    dom0_ramdisk_addr="-"
fi

i=0
while test $i -lt $NUM_DOMUS
do
    check_compressed_file_type ${DOMU_KERNEL[$i]} "MS-DOS executable"
    domU_kernel_addr[$i]=$memaddr
    load_file ${DOMU_KERNEL[$i]}
    domU_kernel_size[$i]=$(( $memaddr - ${domU_kernel_addr[$i]} ))
    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_ramdisk_size[$i]=$(( $memaddr - ${domU_ramdisk_addr[$i]} ))
    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_passthrough_dtb_size[$i]=$(( $memaddr - ${domU_passthrough_dtb_addr[$i]} ))
    fi
    i=$(( $i + 1 ))
done

check_file_type $DEVICE_TREE "Device Tree Blob"
filter_device_tree $DEVICE_TREE
device_tree_addr=$memaddr
load_file $DEVICE_TREE

memaddr=$(( $MEMORY_END - $memaddr ))
if test $memaddr -lt 0
then
    echo Error, not enough memory to load all binaries
    exit 1
fi

echo "bootm $xen_addr $dom0_ramdisk_addr $device_tree_addr" >> $UBOOT_SOURCE

memaddr=$(( $memaddr + $offset ))
memaddr=`printf "0x%X\n" $memaddr`
uboot_addr="$memaddr"
mkimage -A arm64 -T script -C none -a $uboot_addr -e $uboot_addr -d $UBOOT_SOURCE "$UBOOT_SCRIPT" &> /dev/null
echo "Generated uboot script $UBOOT_SCRIPT, to be loaded at address $uboot_addr:"
echo "$LOAD_CMD $uboot_addr $UBOOT_SCRIPT; source $uboot_addr"