Skip to content

Commit 00cc4ed

Browse files
authored
Merge pull request #98 from sysprog21/fix-dtb
Automate DTB regeneration for SMP configuration
2 parents 7b3120b + 366f380 commit 00cc4ed

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ S := $E $E
205205
CFLAGS += -D SEMU_BOOT_TARGET_TIME=10
206206

207207
SMP ?= 1
208+
209+
# Track SMP value changes to force DTB regeneration
210+
.smp_stamp: FORCE
211+
@if [ ! -f .smp_stamp ] || [ "$$(cat .smp_stamp 2>/dev/null)" != "$(SMP)" ]; then \
212+
echo "$(SMP)" > .smp_stamp; \
213+
rm -f riscv-harts.dtsi minimal.dtb; \
214+
fi
215+
208216
.PHONY: riscv-harts.dtsi
209-
riscv-harts.dtsi:
217+
riscv-harts.dtsi: .smp_stamp
210218
$(Q)python3 scripts/gen-hart-dts.py $@ $(SMP) $(CLOCK_FREQ)
211219

212220
minimal.dtb: minimal.dts riscv-harts.dtsi
@@ -216,6 +224,9 @@ minimal.dtb: minimal.dts riscv-harts.dtsi
216224
$(subst ^,$S,$(filter -D^SEMU_FEATURE_%, $(subst -D$(S)SEMU_FEATURE,-D^SEMU_FEATURE,$(CFLAGS)))) $< \
217225
| $(DTC) - > $@
218226

227+
.PHONY: FORCE
228+
FORCE:
229+
219230
# Rules for downloading prebuilt Linux kernel image
220231
include mk/external.mk
221232

@@ -245,6 +256,7 @@ clean:
245256
distclean: clean
246257
$(Q)$(RM) riscv-harts.dtsi
247258
$(Q)$(RM) minimal.dtb
259+
$(Q)$(RM) .smp_stamp
248260
$(Q)$(RM) Image rootfs.cpio
249261
$(Q)$(RM) ext4.img
250262

scripts/verify-dtb.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# Verify that DTB CPU count matches expected count
3+
4+
DTB_FILE="${1}"
5+
EXPECTED_COUNT="${2}"
6+
7+
if [ -z "$DTB_FILE" ] || [ -z "$EXPECTED_COUNT" ]; then
8+
echo "Usage: $0 <dtb_file> <expected_cpu_count>"
9+
exit 1
10+
fi
11+
12+
if [ ! -f "$DTB_FILE" ]; then
13+
echo "Error: DTB file '$DTB_FILE' not found"
14+
exit 1
15+
fi
16+
17+
# Count CPUs in DTB using dtc
18+
DTC=$(which dtc 2>/dev/null)
19+
if [ -z "$DTC" ]; then
20+
echo "Error: dtc tool not found in PATH"
21+
echo "DTB verification requires the device tree compiler (dtc)"
22+
echo "Please install dtc and try again"
23+
exit 1
24+
fi
25+
26+
CPU_COUNT=$($DTC -I dtb -O dts "$DTB_FILE" 2>/dev/null | grep -c "cpu@")
27+
28+
if [ "$CPU_COUNT" -ne "$EXPECTED_COUNT" ]; then
29+
echo "========================================="
30+
echo "DTB Configuration Mismatch Detected!"
31+
echo "========================================="
32+
echo "DTB file '$DTB_FILE' contains $CPU_COUNT CPU(s)"
33+
echo "But you requested $EXPECTED_COUNT CPU(s)"
34+
echo ""
35+
echo "Solution:"
36+
echo " make SMP=$EXPECTED_COUNT riscv-harts.dtsi minimal.dtb"
37+
echo ""
38+
echo "This will regenerate the DTB with correct CPU count."
39+
echo "========================================="
40+
exit 1
41+
fi
42+
43+
exit 0

0 commit comments

Comments
 (0)