#!/bin/bash
# Regression check against each new kernel. Fails if upstream changes the D0 tree.
# Applies the bcm2712d0.dtbo we ship (a/hwm-bw-raspberrypi) to the mainline C0 DTB
# and compares the result with the mainline D0 DTB. If this fails, update the overlay.

dtbdir=/boot/dtb/broadcom
c0dtb=${dtbdir}/bcm2712-rpi-5-b.dtb
d0dtb=${dtbdir}/bcm2712-d-rpi-5-b.dtb
dtbo=/usr/share/hwm-bw-raspberrypi/bootloader-firmware/overlays/bcm2712d0.dtbo

# Ensure the latest 'a/hwm-bw-raspberrypi' package is installed first:
fdtoverlay -i ${c0dtb} -o /tmp/d0.dtb ${dtbo} || exit 1

diff <(dtc -q -I dtb -O dts -s /tmp/d0.dtb) \
     <(dtc -q -I dtb -O dts -s ${d0dtb}) \
     || { echo "bcm2712d0 overlay out of sync with kernel"; exit 1; }
echo "bcm2712d0 overlay matches ${d0dtb}"

