#!/bin/sh
# Pick a REAL serial console or sleep forever (init must never rate-limit).
# ttyAMA0: node presence is sufficient (PL011 registers only when real --
#   qemu/virt). ttySn: /dev/ttySn nodes exist even with NO port behind them
#   (8250 makes 4 placeholder nodes; GB10 metal has zero bound ports and an
#   empty /proc/tty/driver/serial) -- getty on a dead node respawn-loops,
#   which is exactly the "Id T0 respawning too fast" symptom. Demand a
#   known uart type from the driver before trusting a ttySn.
[ -c /dev/ttyAMA0 ] && exec /sbin/getty -L ttyAMA0 115200 vt100
for n in 0 1 2 3; do
  if grep -q "^$n: uart:" /proc/tty/driver/serial 2>/dev/null && \
     ! grep -q "^$n: uart:unknown" /proc/tty/driver/serial 2>/dev/null; then
    exec /sbin/getty -L "ttyS$n" 115200 vt100
  fi
done
exec sleep infinity
