aboutsummaryrefslogtreecommitdiff
path: root/scripts/common
blob: 2dd98fb94d3e8d0ee1f74a08c2e4d6a74253aa4c (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
#!/bin/bash

# scripts/common makes use of variables defined in the ImageBuilder
# config file as described in README.md, including but not limited to
# the following:
#
# - UBOOT_SOURCE
# - UBOOT_SCRIPT
# - NUM_DOMUS
# - DOMU_PASSTHROUGH_PATHS
# - DOMU_PASSTHROUGH_DTB
# - LOPPER_PATH
# - LOPPER_CMD
# - DEVICE_TREE

tmp_files=()
tmp_dirs=()

# Highest valid phandle. Will be decremented with each call to get_next_phandle
phandle_next="0xfffffffe"

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

    for i in "${tmp_dirs[@]}"
    do
        rm -rf "$i"
    done
}

# Get next phandle and set it as a value (in hex) of a variable whose name is
# passed as a first argument. Decrement global counter phandle_next.
function get_next_phandle()
{
    eval "$1=$(printf "0x%x" $phandle_next)"
    phandle_next=$(( $phandle_next - 1 ))
}

function sanity_check_partial_dts()
{
    local domU_passthrough_path="$1"
    local partial_dts_dir="$2"
    local address_cells_val
    local size_cells_val
    local tmpdtb
    local val
    local file

    for devpath in $domU_passthrough_path
    do
        file=${devpath##*/}
        file="$partial_dts_dir"/"$file".dts

        if ! test -f "$file"
        then
            echo "Device tree \"$file\" is not present"
            return 1
        fi

        tmpdtb=`mktemp`
        dtc -I dts -O dtb -o $tmpdtb "$file"
        tmp_files+=($tmpdtb)

        val=$(fdtget $tmpdtb /passthrough \#address-cells)
        if test -z "$address_cells_val"
        then
            address_cells_val="$val"
        else
            if test "$address_cells_val" -ne "$val"
            then
                echo "Address cells mismatch for ${DOMU_PASSTHROUGH_PATHS[$i]}"
                return 1
            fi
        fi

        val=$(fdtget $tmpdtb /passthrough \#size-cells)
        if test -z "$size_cells_val"
        then
            size_cells_val="$val"
        else
            if test "$size_cells_val" -ne "$val"
            then
                echo "Size cells mismatch for ${DOMU_PASSTHROUGH_PATHS[$i]}"
                return 1
            fi
        fi
    done

    return 0
}

function compile_merge_partial_dts()
{
    local dtb_dir=$1
    local repo=$(echo "$2" | awk '{print $1}')
    local dir=$(echo "$2" | awk '{print $2}')
    local partial_dts_dir
    local tmp
    local tmpdts
    local file
    local node
    local i
    local j

    if test "$repo"
    then
        # Partial dts will be obtained from PASSTHROUGH_DTS_REPO
        if [[ "$repo" =~ .*@.*:.* ]]
        then
            tmp=`mktemp -d`
            tmp_dirs+=($tmp)

            echo "Cloning git repo \"$git_repo\""
            git clone "$repo" $tmp
            if test $? -ne 0
            then
                echo "Error occurred while cloning \"$git_repo\""
                return 1
            fi

            repo=$tmp
        fi

        if test -z "$dir"
        then
            dir="."
        fi
        partial_dts_dir="$repo"/"$dir"
    else
        # Partial dts will be generated by the lopper
        tmp=`mktemp -d`
        tmp_dirs+=($tmp)
        partial_dts_dir="$tmp"
    fi

    i=0
    while test $i -lt $NUM_DOMUS
    do
        if test -z "${DOMU_PASSTHROUGH_PATHS[$i]}"
        then
            echo "DOMU_PASSTHROUGH_PATHS[$i] is not defined"
            return 1
        fi

        if test -z "$repo"
        then
            # Generate partial dts using lopper
            for devpath in ${DOMU_PASSTHROUGH_PATHS[$i]}
            do
                node=${devpath##*/}
                file="$partial_dts_dir"/"$node".dts

                # Execute lopper with the following assists:
                # - extract: used to take the target node, extract it from the
                #   system device tree, chase the phandle references and place
                #   it in a new extracted tree structure,
                # - extract-xen: used to perform Xen specific modifications
                #   on the extracted tree structure e.g. adding "xen,path",
                #   "xen,reg", interrupt-parent properties.
                # For additional information, please see the lopper's README
                # file as well as usage of the mentioned assists.
                $LOPPER_PATH --permissive -f $DEVICE_TREE \
                -- extract -t $devpath $LOPPER_CMD \
                -- extract-xen -t $node -o $file

                if test $? -ne 0
                then
                    return 1
                fi
            done
        fi

        sanity_check_partial_dts "${DOMU_PASSTHROUGH_PATHS[$i]}" "$partial_dts_dir"
        if test $? -ne 0
        then
            return 1
        fi

        tmp=`mktemp "$dtb_dir/partial-dts-domU$i-XXX"`
        tmp_files+=($tmp)

        j=1

        for devpath in ${DOMU_PASSTHROUGH_PATHS[$i]}
        do
            node=${devpath##*/}
            file="$partial_dts_dir"/"$node".dts

            # All the subsequent dts files should not have dts version mentioned
            if test $j -gt 1
            then
                tmpdts=`mktemp`
                cp "$file" "$tmpdts"
                tmp_files+=($tmpdts)
                file="$tmpdts"
                sed -i '/\/dts-v/d' "$file"
            fi

            echo "/include/ \"$file\"" >> "$tmp"

            j=$(( $j + 1 ))
        done

        dtc -I dts -O dtb -o "$tmp".dtb  "$tmp"
        if test $? -ne 0
        then
            echo "Could not compile \"$tmp\""
            return 1
        fi

        if test "${DOMU_PASSTHROUGH_DTB[$i]}"
        then
            echo "Can't set both PASSTHROUGH_DTS_REPO and DOMU_PASSTHROUGH_DTB"
            return 1
        fi

        DOMU_PASSTHROUGH_DTB[$i]="$tmp".dtb

        i=$(( $i + 1 ))
    done

    return 0
}