forked from qualcomm-linux/qcom-dtb-metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-fitimage-metadata.sh
More file actions
executable file
·326 lines (279 loc) · 10.1 KB
/
check-fitimage-metadata.sh
File metadata and controls
executable file
·326 lines (279 loc) · 10.1 KB
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
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# SCRIPT: check-fitimage-metadata.sh
#
# PURPOSE:
# Validates the integrity of a QCOM FIT Image Source (ITS) file against
# a metadata device tree. It ensures that configurations point to valid
# image nodes and that metadata compatibility strings are correct.
#
# LOGIC FLOW:
# 1. VALIDATION: Checks if input files exist.
# 2. PARSE CONFIGS: Extracts 'compatible' strings and 'fdt' lists from
# '/configurations', handling multi-file and comma-quoted entries.
# 4. METADATA CHECK: Verifies 'compatible' strings against the metadata file.
# - Applies a whitelist (BLACKLIST_SKIP_PATTERNS) for specific failures.
# 5. LINKAGE CHECK: Ensures every 'fdt' entry in a configuration exactly
# matches a defined node name in the '/images' section.
#
# USAGE:
# ./check-fitimage-metadata.sh [its_file] [metadata_file]
# -----------------------------------------------------------------------------
# Optional positional arguments:
# $1 -> ITS file (qcom-fitimage.its)
# $2 -> META file (qcom-metadata.dts)
ITS_FILE="${1:-qcom-fitimage.its}"
META_FILE="${2:-qcom-metadata.dts}"
# -----------------------------------------------------------------------------
# CONFIGURATION
# -----------------------------------------------------------------------------
BLACKLIST_SKIP_PATTERNS=("camx" "el2kvm")
if [[ ! -f "$ITS_FILE" ]]; then
echo "fail FILE_NOT_FOUND $ITS_FILE" >&2
exit 1
fi
if [[ ! -f "$META_FILE" ]]; then
echo "fail FILE_NOT_FOUND $META_FILE" >&2
exit 1
fi
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
# -----------------------------------------------------------------------------
# FUNCTION: Validate Metadata
# -----------------------------------------------------------------------------
validate_metadata() {
if ! dtc -I dts -O dtb -o /dev/null "$META_FILE" >/dev/null 2>&1; then
echo "fail INVALID_DTS_SYNTAX $META_FILE" >&2
exit 1
else
echo "Metadata Syntax Check: Pass"
fi
}
validate_metadata "$META_FILE"
# -----------------------------------------------------------------------------
# FUNCTION: Validate ITS Syntax
# Checks:
# 1. Configuration nodes (conf-) have opening braces '{'
# 2. 'compatible' and 'fdt' properties end with a semicolon ';'
# 3. Configuration nodes are closed properly '};'
# -----------------------------------------------------------------------------
validate_its_syntax() {
local file="$1"
# We use awk to track state (inside a conf node or not)
# logic:
# - If line has 'conf-', ensure it has '{'
# - If inside conf node, check compatible/fdt lines for trailing ';'
# - Track braces to ensure closing
awk '
BEGIN {
in_conf = 0;
errors = 0;
}
# 1. Check for Configuration Node Start
# Matches "conf-" but ensures it has an opening brace
/conf-/ {
if ($0 ~ /\{/) {
in_conf = 1;
} else {
print "ERROR: Line " NR ": Configuration node missing opening brace -> " $0;
errors++;
}
}
# 2. Check Properties (only while inside a conf node)
in_conf && /^\s*(compatible|fdt)\s*=/ {
# Check if line ends with semicolon (ignoring trailing whitespace)
if ($0 !~ /;\s*$/) {
print "ERROR: Line " NR ": Property missing trailing semicolon -> " $0;
errors++;
}
}
# 3. Check for Node Closing
# If we see "};" and we were in a conf, mark it closed
in_conf && /^\s*};\s*$/ {
in_conf = 0;
}
END {
if (errors > 0) {
print "ITS Syntax Check: FAILED (" errors " errors found)";
exit 1;
} else {
print "ITS Syntax Check: PASS";
exit 0;
}
}
' "$file"
# Capture awk exit code
if [ $? -ne 0 ]; then
echo "Exiting due to ITS syntax errors."
exit 1
fi
}
validate_its_syntax "$ITS_FILE"
missing_any=0
###############################################################################
# 1. PARSE IMAGE NODES (The targets of the FDT links)
###############################################################################
# We extract the exact name of every subnode directly under /images
# Logic:
# 1. Find block starting 'images {'
# 2. Inside that block, find lines ending in '{' (these are subnodes)
# 3. Clean whitespace to get the raw node name.
awk -v out="$tmpdir/valid_images.txt" '
BEGIN { in_images = 0 }
# Start of images node
/^[[:space:]]*images[[:space:]]*\{/ { in_images = 1; next }
# End of images node (closing brace at same indentation level usually)
in_images && /^\};/ { in_images = 0; next }
in_images && /^\t\};/ { in_images = 0; next }
# Fallback: if we see a closing brace at start of line, assume end of block
in_images && /^\}/ { in_images = 0; next }
# Capture subnodes. Matches lines like: " fdt-name {"
in_images && /\{$/ {
line = $0
# 1. Remove comments if any (//...)
sub(/\/\/.*$/, "", line)
# 2. Trim trailing "{" and whitespace
sub(/[[:space:]]*\{[[:space:]]*$/, "", line)
# 3. Trim leading whitespace
sub(/^[[:space:]]+/, "", line)
#4. Extract image node names to exclude kernel, ramdisk, and setup entries
if (line != "" && line !~ /^(kernel|ramdisk|setup)/) {
print line >> out
}
}
' "$ITS_FILE"
# Make sure we got something (fallback for non-standard formatting)
# Re-run strict extraction if previous one was empty or to handle "fdt-..." specifically
if [[ ! -s "$tmpdir/valid_images.txt" ]]; then
# Simpler regex approach for standard ITS files
awk '
/images[[:space:]]*\{/ { in_img=1; next }
in_img && /\};/ { in_img=0; next }
in_img && /^[[:space:]]*fdt-.*\{/ {
node=$1
sub(/\{/, "", node)
print node
}
' "$ITS_FILE" > "$tmpdir/valid_images.txt"
fi
###############################################################################
# 2. PARSE CONFIGURATIONS
###############################################################################
# Extract: NodeName | Compatible | FDT_List
awk -v out="$tmpdir/config_data.txt" '
BEGIN {
in_configs = 0
in_node = 0
current_node = ""
current_compat = ""
current_fdt_list = ""
}
/configurations[[:space:]]*\{/ { in_configs = 1; next }
in_configs && /^\}/ { in_configs = 0; next }
in_configs && /^[[:space:]]*[^[:space:]]+[[:space:]]*\{/ {
current_node = $1
sub(/:$/, "", current_node)
in_node = 1
current_compat = ""
current_fdt_list = ""
next
}
in_node && /};[[:space:]]*$/ {
if (current_node != "") {
print current_node "|" current_compat "|" current_fdt_list >> out
}
in_node = 0
current_node = ""
next
}
in_node && /compatible[[:space:]]*=/ {
line = $0
if (match(line, /"[^"]*"/)) {
current_compat = substr(line, RSTART+1, RLENGTH-2)
}
}
in_node && /fdt[[:space:]]*=/ {
line = $0
while (match(line, /"[^"]*"/)) {
val = substr(line, RSTART+1, RLENGTH-2)
if (current_fdt_list == "") {
current_fdt_list = val
} else {
current_fdt_list = current_fdt_list " " val
}
line = substr(line, RSTART + RLENGTH)
}
}
' "$ITS_FILE"
###############################################################################
# 3. METADATA NODES
###############################################################################
meta_nodes="$tmpdir/meta_nodes.txt"
if [ -f "$META_FILE" ]; then
awk '
/^[[:space:]]*[^&].*\{/ {
line = $0
sub(/\{.*/, "", line)
sub(/^[[:space:]]+/, "", line)
sub(/[[:space:]]+$/, "", line)
n = split(line, a, /[[:space:]]+/)
if (n >= 1) {
node = a[n]
sub(/:$/, "", node)
if (node != "") print node
}
}
' "$META_FILE" | sort -u > "$meta_nodes"
else
touch "$meta_nodes"
fi
###############################################################################
# 4. VALIDATION LOOP
###############################################################################
while IFS='|' read -r cfg compat fdt_val_raw; do
# --- CHECK A: Metadata ---
if [[ -n "$compat" ]]; then
compat_no_prefix="${compat#qcom,}"
IFS='-' read -r -a parts <<< "$compat_no_prefix"
for part in "${parts[@]}"; do
[[ -z "$part" ]] && continue
if grep -qx "$part" "$meta_nodes"; then continue; fi
is_blacklisted=0
for pattern in "${BLACKLIST_SKIP_PATTERNS[@]}"; do
if [[ "$part" == "$pattern" ]]; then is_blacklisted=1; break; fi
done
if [[ "$is_blacklisted" -ne 1 ]]; then
echo "fail [METADATA] ${cfg}: '${part}' missing from metadata"
missing_any=1
fi
done
fi
# --- CHECK B: FDT Linkage ---
if [[ -z "$fdt_val_raw" ]]; then
echo "fail [FDT-PROP] ${cfg}: Missing 'fdt' property"
missing_any=1
continue
fi
read -r -a fdt_entries <<< "$fdt_val_raw"
for fdt_entry in "${fdt_entries[@]}"; do
# 1. Check Prefix
if [[ "$fdt_entry" != fdt-* ]]; then
echo "fail [FDT-NAME] ${cfg}: entry '$fdt_entry' does not start with 'fdt-'"
missing_any=1
fi
# 2. Check Existence in Images (Exact Match)
# -F: Fixed string (handles dots/commas literally)
# -x: Exact line match (avoids partial matches)
if ! grep -Fx -q "$fdt_entry" "$tmpdir/valid_images.txt"; then
echo "fail [FDT-LINK] ${cfg}: entry '$fdt_entry' NOT found in /images"
missing_any=1
fi
done
done < "$tmpdir/config_data.txt"
if [[ "$missing_any" -ne 0 ]]; then
echo "FAILED: One or more checks failed."
exit 2
fi
echo "success"
exit 0