Skip to content

Commit bf2d762

Browse files
committed
ci: Improve partition scheme check logic and logs
1 parent 7e604eb commit bf2d762

File tree

1 file changed

+178
-47
lines changed

1 file changed

+178
-47
lines changed

.github/scripts/validate_board.sh

Lines changed: 178 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,37 @@ validate_board() {
2020
local boards_file="boards.txt"
2121

2222
echo "Validating board: $board_name"
23+
echo ""
2324

2425
# Rule 1: Check build.board format
26+
echo "Rule 1: Build Board Format Validation"
27+
echo "====================================="
2528
validate_build_board_format "$board_name" "$boards_file"
29+
echo ""
2630

2731
# Rule 2: Check for required board properties
32+
echo "Rule 2: Required Properties Validation"
33+
echo "======================================"
2834
validate_required_properties "$board_name" "$boards_file"
35+
echo ""
2936

3037
# Rule 3: Check for valid partition schemes for available flash sizes
38+
echo "Rule 3: Partition Scheme Validation"
39+
echo "==================================="
3140
validate_partition_schemes "$board_name" "$boards_file"
41+
echo ""
3242

3343
# Rule 4: Check for VID and PID consistency
44+
echo "Rule 4: VID/PID Consistency Validation"
45+
echo "====================================="
3446
validate_vid_pid_consistency "$board_name" "$boards_file"
47+
echo ""
3548

3649
# Add more validation rules here as needed
3750
# Rule 5: Future validation rules can be added here
38-
print_success "Board '$board_name' validation passed"
51+
echo "=========================================="
52+
print_success "🎉 ALL VALIDATION RULES PASSED for board '$board_name'"
53+
echo "=========================================="
3954
}
4055

4156
# Rule 1: Check build.board format
@@ -66,15 +81,14 @@ validate_build_board_format() {
6681
exit 1
6782
fi
6883

69-
print_success "build.board is valid: '$build_board_value'"
84+
echo "build.board is valid: '$build_board_value'"
7085
}
7186

7287
# Rule 2: Check for required board properties
7388
validate_required_properties() {
7489
local board_name="$1"
7590
local boards_file="$2"
7691

77-
echo "Checking required board properties..."
7892
local required_props=("upload.flags" "upload.extra_flags")
7993
local missing_props=()
8094

@@ -89,31 +103,33 @@ validate_required_properties() {
89103
printf ' - %s\n' "${missing_props[@]}"
90104
exit 1
91105
fi
106+
107+
echo " ✓ Required properties validation completed"
92108
}
93109

94110
# Rule 3: Check for valid partition schemes for available flash sizes
95111
validate_partition_schemes() {
96112
local board_name="$1"
97113
local boards_file="$2"
98114

99-
echo "Checking partition schemes for available flash sizes..."
100-
101115
# Get all available flash sizes for this board
102116
local flash_sizes
103117
flash_sizes=$(grep "^$board_name.menu.FlashSize\." "$boards_file" | grep "\.build\.flash_size=" | cut -d'=' -f2 | sort -V)
104118

105119
# Check if board has menu.FlashSize entries
106120
if [ -z "$flash_sizes" ]; then
107-
# If no menu.FlashSize entries, check if board has any build.flash_size entry
121+
# If no menu.FlashSize entries, check if board has build.flash_size entry at least
108122
local has_flash_size
109123
has_flash_size=$(grep "^$board_name\." "$boards_file" | grep "\.build\.flash_size=" | head -1)
110124

111125
if [ -z "$has_flash_size" ]; then
112-
print_error "No flash size options found for board '$board_name' (needs either menu.FlashSize entries or build.flash_size entry)"
126+
print_error "No flash size options found for board '$board_name' (needs build.flash_size entry at least)"
113127
exit 1
114128
else
115-
print_success "Board '$board_name' has build.flash_size entry (no menu.FlashSize required)"
116-
return 0
129+
# Extract flash size from build.flash_size entry
130+
local flash_size_value
131+
flash_size_value=$(echo "$has_flash_size" | cut -d'=' -f2)
132+
flash_sizes="$flash_size_value"
117133
fi
118134
fi
119135

@@ -133,11 +149,11 @@ validate_partition_schemes() {
133149
fi
134150
done
135151

136-
echo " Maximum flash size available: ${max_flash_mb}MB"
152+
echo " ✓ Flash configuration found - maximum size: ${max_flash_mb}MB"
137153

138154
# Find all partition schemes for this board
139155
local partition_schemes
140-
partition_schemes=$(grep "^$board_name.menu.PartitionScheme\." "$boards_file" | grep "\.build\.partitions=" | cut -d'=' -f2 | sort -u)
156+
partition_schemes=$(grep "^$board_name.menu.PartitionScheme\." "$boards_file" | grep -v "\.build\." | grep -v "\.upload\." | sed 's/.*\.PartitionScheme\.\([^=]*\)=.*/\1/' | sort -u)
141157

142158
if [ -n "$partition_schemes" ]; then
143159
# Validate each partition scheme against the maximum flash size
@@ -147,7 +163,7 @@ validate_partition_schemes() {
147163
fi
148164

149165

150-
print_success "Partition scheme validation completed"
166+
echo "Partition scheme validation completed"
151167
}
152168

153169
# Helper function to validate individual partition scheme
@@ -164,6 +180,8 @@ validate_partition_scheme_size() {
164180
if [ "$scheme_size_mb" -gt "$max_flash_mb" ]; then
165181
print_error "Partition scheme '$scheme' (${scheme_size_mb}MB) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
166182
exit 1
183+
else
184+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (size indicator: ${scheme_size_mb}MB)"
167185
fi
168186
elif [[ "$scheme" =~ _([0-9]+)M$ ]]; then
169187
# Handle cases like "default_8M" (without B)
@@ -172,20 +190,163 @@ validate_partition_scheme_size() {
172190
if [ "$scheme_size_mb" -gt "$max_flash_mb" ]; then
173191
print_error "Partition scheme '$scheme' (${scheme_size_mb}MB) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
174192
exit 1
193+
else
194+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (size indicator: ${scheme_size_mb}MB)"
195+
fi
196+
elif [[ "$scheme" =~ _([0-9]+)$ ]]; then
197+
# Handle cases like "esp_sr_16" (just number at end)
198+
local scheme_size_mb="${BASH_REMATCH[1]}"
199+
200+
if [ "$scheme_size_mb" -gt "$max_flash_mb" ]; then
201+
print_error "Partition scheme '$scheme' (${scheme_size_mb}MB) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
202+
exit 1
203+
else
204+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (size indicator: ${scheme_size_mb}MB)"
175205
fi
176206
else
177-
# For schemes without size in name (like "default", "minimal"), check upload maximum size
178-
validate_scheme_upload_size "$scheme" "$board_name" "$boards_file" "$max_flash_mb"
207+
# For schemes without size in name, check description for size indicators
208+
local description_text
209+
description_text=$(grep "^$board_name.menu.PartitionScheme\.$scheme=" "$boards_file" | cut -d'=' -f2)
210+
211+
# First check main description for size indicators (before brackets)
212+
# Look for the largest size indicator in the main description
213+
local main_description_size_mb=0
214+
local main_description_size_text=""
215+
216+
# Check for MB and M values in main description (before brackets)
217+
local main_part=$(echo "$description_text" | sed 's/(.*//') # Remove bracket content
218+
219+
# Extract M values (not followed by B) and MB values
220+
local m_values=$(echo "$main_part" | grep -oE '([0-9]+\.?[0-9]*)M' | grep -v 'MB' | sed 's/M$//')
221+
local mb_values=$(echo "$main_part" | grep -oE '([0-9]+\.?[0-9]*)MB' | sed 's/MB//')
222+
223+
# Combine both M and MB values
224+
local all_mb_values=$(echo -e "$m_values\n$mb_values" | grep -v '^$')
225+
226+
# Find the largest MB value in main description
227+
local largest_mb_int=0
228+
while IFS= read -r value; do
229+
if [[ "$value" =~ ^([0-9]+)\.([0-9]+)$ ]]; then
230+
local whole="${BASH_REMATCH[1]}"
231+
local decimal="${BASH_REMATCH[2]}"
232+
local value_int=$((whole * 10 + decimal))
233+
elif [[ "$value" =~ ^([0-9]+)$ ]]; then
234+
local value_int=$((value * 10))
235+
else
236+
continue
237+
fi
238+
239+
if [ "$value_int" -gt "$largest_mb_int" ]; then
240+
largest_mb_int=$value_int
241+
main_description_size_text="${value}M"
242+
fi
243+
done <<< "$all_mb_values"
244+
245+
if [ "$largest_mb_int" -gt 0 ]; then
246+
# Found size in main description
247+
if [ "$largest_mb_int" -gt $((max_flash_mb * 10)) ]; then
248+
print_error "Partition scheme '$scheme' (${main_description_size_text} from description) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
249+
exit 1
250+
else
251+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (size from description: ${main_description_size_text})"
252+
fi
253+
else
254+
# No size in main description, check bracket content
255+
local bracket_content
256+
bracket_content=$(echo "$description_text" | grep -oE '\([^)]+\)' | head -1)
257+
258+
if [ -n "$bracket_content" ]; then
259+
# Calculate total size from all components in brackets
260+
local total_size_mb=0
261+
262+
# Extract and sum MB values
263+
local mb_sum=0
264+
while IFS= read -r value; do
265+
if [[ "$value" =~ ^([0-9]+)\.([0-9]+)$ ]]; then
266+
local whole="${BASH_REMATCH[1]}"
267+
local decimal="${BASH_REMATCH[2]}"
268+
# Convert decimal to integer (e.g., 1.3 -> 13, 6.93 -> 69)
269+
# Handle multi-digit decimals: 6.93 -> 6*10 + 9 = 69 (round down)
270+
local decimal_int=$((decimal / 10))
271+
mb_sum=$((mb_sum + whole * 10 + decimal_int))
272+
elif [[ "$value" =~ ^([0-9]+)$ ]]; then
273+
mb_sum=$((mb_sum + value * 10))
274+
fi
275+
done < <(echo "$bracket_content" | grep -oE '([0-9]+\.?[0-9]*)MB' | sed 's/MB//')
276+
277+
# Extract and sum KB values (convert to MB tenths)
278+
local kb_sum=0
279+
while IFS= read -r value; do
280+
if [[ "$value" =~ ^([0-9]+)\.([0-9]+)$ ]]; then
281+
local whole="${BASH_REMATCH[1]}"
282+
local decimal="${BASH_REMATCH[2]}"
283+
# Convert KB to MB tenths: (whole.decimal * 10) / 1024, rounded
284+
local kb_tenths=$((whole * 10 + decimal))
285+
kb_sum=$((kb_sum + (kb_tenths * 10 + 512) / 1024))
286+
elif [[ "$value" =~ ^([0-9]+)$ ]]; then
287+
# Convert KB to MB tenths: value * 10 / 1024, rounded
288+
kb_sum=$((kb_sum + (value * 10 + 512) / 1024))
289+
fi
290+
done < <(echo "$bracket_content" | grep -oE '([0-9]+\.?[0-9]*)KB' | sed 's/KB//')
291+
292+
# Sum all values and convert back to MB (divide by 10, rounded)
293+
total_size_mb=$(( (mb_sum + kb_sum + 5) / 10 ))
294+
295+
if [ "$total_size_mb" -gt 0 ]; then
296+
# Found size in description
297+
if [ "$total_size_mb" -gt "$max_flash_mb" ]; then
298+
print_error "Partition scheme '$scheme' (${total_size_mb}MB from description) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
299+
exit 1
300+
else
301+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (size from description: ${total_size_mb}MB)"
302+
fi
303+
else
304+
# No size indicator found in brackets, check upload maximum size
305+
validate_scheme_upload_size "$scheme" "$board_name" "$boards_file" "$max_flash_mb"
306+
fi
307+
else
308+
# No brackets found, check upload maximum size
309+
validate_scheme_upload_size "$scheme" "$board_name" "$boards_file" "$max_flash_mb"
310+
fi
311+
fi
312+
fi
313+
}
314+
315+
# Helper function to validate upload maximum size for a specific partition scheme
316+
validate_scheme_upload_size() {
317+
local scheme="$1"
318+
local board_name="$2"
319+
local boards_file="$3"
320+
local max_flash_mb="$4"
321+
322+
# Get upload maximum size for this specific scheme
323+
local upload_size
324+
upload_size=$(grep "^$board_name.menu.PartitionScheme\.$scheme\." "$boards_file" | grep "\.upload\.maximum_size=" | head -1 | cut -d'=' -f2)
325+
326+
if [ -z "$upload_size" ]; then
327+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (no upload size limit)"
328+
return 0
329+
fi
330+
331+
# Convert flash size to bytes for comparison
332+
local max_flash_bytes=$((max_flash_mb * 1024 * 1024))
333+
334+
# Check upload size against maximum flash size
335+
if [ "$upload_size" -gt "$max_flash_bytes" ]; then
336+
local upload_mb=$(( (upload_size + 524288) / 1048576 ))
337+
print_error "Partition scheme '$scheme' upload size (${upload_mb}MB, ${upload_size} bytes) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
338+
exit 1
179339
fi
340+
341+
local upload_mb=$(( (upload_size + 524288) / 1048576 ))
342+
echo " ✓ Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (upload size: ${upload_mb}MB)"
180343
}
181344

182345
# Rule 4: Check for VID and PID consistency
183346
validate_vid_pid_consistency() {
184347
local board_name="$1"
185348
local boards_file="$2"
186349

187-
echo "Checking VID and PID consistency..."
188-
189350
# Get all VID and PID entries for this board
190351
local vid_entries
191352
local pid_entries
@@ -228,37 +389,7 @@ validate_vid_pid_consistency() {
228389
exit 1
229390
fi
230391

231-
print_success "VID and PID consistency check passed"
232-
}
233-
234-
# Helper function to validate upload maximum size for a specific partition scheme
235-
validate_scheme_upload_size() {
236-
local scheme="$1"
237-
local board_name="$2"
238-
local boards_file="$3"
239-
local max_flash_mb="$4"
240-
241-
# Get upload maximum size for this specific scheme
242-
local upload_size
243-
upload_size=$(grep "^$board_name.menu.PartitionScheme\..*\.build\.partitions=$scheme" "$boards_file" -A1 | grep "\.upload\.maximum_size=" | head -1 | cut -d'=' -f2)
244-
245-
if [ -z "$upload_size" ]; then
246-
print_success "Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (no upload size limit)"
247-
return 0
248-
fi
249-
250-
# Convert flash size to bytes for comparison
251-
local max_flash_bytes=$((max_flash_mb * 1024 * 1024))
252-
253-
# Check upload size against maximum flash size
254-
if [ "$upload_size" -gt "$max_flash_bytes" ]; then
255-
local upload_mb=$((upload_size / 1024 / 1024))
256-
print_error "Partition scheme '$scheme' upload size (${upload_mb}MB, ${upload_size} bytes) exceeds available flash size (${max_flash_mb}MB) for board '$board_name'"
257-
exit 1
258-
fi
259-
260-
local upload_mb=$((upload_size / 1024 / 1024))
261-
print_success "Partition scheme '$scheme' is valid for ${max_flash_mb}MB flash (upload size: ${upload_mb}MB)"
392+
echo " ✓ VID and PID consistency check passed"
262393
}
263394

264395
# Future validation rules can be added here

0 commit comments

Comments
 (0)