#!/bin/sh
# run3_fixed.sh -- A/B re-run on the FIXED kernel (both fixes applied,
# umcs.c still compiled with -O0 semantics -- the codegen under which
# the baseline panicked).  Everything that panicked or misbehaved must
# now behave, and the good-device path must not regress.
set -e
cd /root/umcs
echo "=== DF-2343/DF-2344 fixed-kernel A/B (-O0 semantics) ==="
uname -a
grep -n "rate == 0" /usr/src/sys/bus/u4b/serial/umcs.c
grep -n "failed to read GPIO" /usr/src/sys/bus/u4b/serial/umcs.c

echo "--- DF-2343 fixed: rate==0 now rejected before the loop ---"
./umcshctl 1
./umcshctl 2 0 1				# was: Fatal trap 18; now: rc=-1
./umcshctl 2 9600
./umcshctl 3 0					# was: panic; now: EINVAL
./umcshctl 3 3145729				# still EINVAL (range guard)

echo "--- DF-2344 fixed: GPIO failure -> deterministic ENXIO ---"
./umcshctl 4 0xA5 3				# was: attached w/ poison-derived ports
./umcshctl 4 0x00 3
./umcshctl 4 0x01 3
./umcshctl 4 0x42 3
./umcshctl 7					# expect not attached
echo "--- DF-2344 no-regression: good device still attaches ---"
./umcshctl 4 0x01 0
./umcshctl 4 0x00 0
./umcshctl 5

echo "--- DF-2343 fixed: unprivileged first open (was: panic) ---"
./umcshctl 4 0x01 0
pw groupmod dialer -m maxx 2>/dev/null || true
cp /root/umcs/tioc0 /home/maxx/tioc0 2>/dev/null || true
chown maxx:maxx /home/maxx/tioc0 2>/dev/null || true
chmod 755 /home/maxx/tioc0 2>/dev/null || true
su -m maxx -c '/home/maxx/tioc0 firstopen /dev/cuaU0.0' || echo "su rc=$?"
echo "--- TIOCSETA variants still blocked, no panic ---"
./tioc0 tioc /dev/ttyU0.0
./umcshctl 5
echo "FIXED_RUN_OK"
