Repository navigation
Conversation
Co-authored-by: Cursor <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-io-ports |
adamsitnik
left a comment
There was a problem hiding this comment.
The changes LGTM, but it would be great if the owner (@krwq) could take a look as well.
|
On OpenBSD and NetBSD Probing with // OpenBSD and NetBSD have a static /dev with nodes for every unit MAKEDEV knows about. Fixed serial ports only
// attach at boot, so the dmesg copy saved at boot tells which of them exist (like OpenBSD's fw_update uses it).
// Returns the attached devices, both as "com0" and as the driver "com", or null if that can't be determined.
private static HashSet<string>? GetBootDevices()
{
HashSet<string> devices = [];
bool sawBootBanner = false;
try
{
foreach (string line in File.ReadLines("/var/run/dmesg.boot"))
{
// NetBSD may prefix messages with a timestamp, e.g. "[ 1.003637] com0 at isa0 port 0x3f8-0x3ff irq 4".
ReadOnlySpan<char> message = line.AsSpan();
if (message.StartsWith('['))
{
message = message.Slice(message.IndexOf(']') + 1).TrimStart();
}
// The message buffer can still hold an earlier boot, so only the last one counts.
if (message.StartsWith("OpenBSD ", StringComparison.Ordinal) || message.StartsWith("NetBSD ", StringComparison.Ordinal))
{
devices.Clear();
sawBootBanner = true;
continue;
}
// e.g. "com0 at acpi0 COM1 addr 0x3f8/0x8 irq 4: ns16550a, 16 byte fifo"
int at = message.IndexOf(" at ", StringComparison.Ordinal);
if (at > 0 && !message.Slice(0, at).ContainsAny(' ', ':'))
{
ReadOnlySpan<char> device = message.Slice(0, at);
devices.Add(device.ToString());
devices.Add(device.TrimEnd("0123456789").ToString());
}
}
}
catch (Exception e) when (e is IOException or UnauthorizedAccessException)
{
return null;
}
// Without the boot banner the start of the boot was lost, so attach messages may be missing too.
return sawBootBanner ? devices : null;
}The matchers then return the device behind a node instead of a bool ( if (GetOpenBSDSerialDevice(Path.GetFileName(path.AsSpan())) is string device &&
(device.Length == 0 || bootDevices is null || bootDevices.Contains(device)))
With real [Fact]
public void OpenBSD_OnlyListsPortsThatAttachedAtBoot()
{
CreateDev("tty00", "cua00", "tty01", "cua01", "ttyc0", "cuac0", "ttyU0", "cuaU0", "ttyVI00", "ttyp0", "console");
WriteDmesgBoot("""
OpenBSD 8.0 (GENERIC.MP) #84: Tue Oct 6 23:13:49 MDT 2026
com0 at acpi0 UAR1 addr 0x3f8/0x8 irq 4: ns16550a, 16 byte fifo
""");
Assert.Equal(new[] { "cua00", "cuaU0", "tty00", "ttyU0" }, GetPortNames_OpenBSD());
}
[Fact]
public void NetBSD_OnlyListsPortsThatAttachedAtBoot()
{
CreateDev("tty00", "dty00", "tty01", "dty01", "ttyU0", "dtyU0", "ttyCY000", "dtyCY000", "ttyCZ0000", "plcom0", "dplcom0", "ttyVI00", "ttyp0", "ttyE0");
WriteDmesgBoot("""
[ 1.000000] NetBSD 11.1 (GENERIC) #0: Mon Nov 2 16:34:32 UTC 2026
[ 1.003637] com0 at acpi0 (COM1, PNP0501-1): io 0x3f8-0x3ff irq 4
""");
Assert.Equal(new[] { "dty00", "dtyU0", "tty00", "ttyU0" }, GetPortNames_NetBSD());
}Note This comment was generated with the help of AI. Please review before applying. |
/dev is static on both, so keep only the nodes whose device is in /var/run/dmesg.boot. USB nodes are always listed, and everything is listed when the boot devices can't be determined. Co-Authored-By: Claude Opus 5.5 <[email protected]>
krwq
left a comment
There was a problem hiding this comment.
Thanks, and fair enough on the tests. Sorry, my suggestion missed one case: on OpenBSD/arm64 the console UART takes over com's device nodes. When pluart (Raspberry Pi, arm64 VMs like Hetzner's) or exuart/imxuart/mvuart/amluart/cduart is the console, it replaces com's device switch entry with its own (cdevsw[maj] = pluartdev; /* KLUDGE */ in sys/dev/ic/pluart.c), so /dev/tty00 is pluart0 and dmesg never mentions com0. With the current change that port gets filtered out, so a Raspberry Pi with a serial console gets only the USB nodes back (before, tty00 was at least listed). Tracking the serial console's driver fixes it, see the suggestions below (they can be committed as one batch).
With real dmesg output from a Raspberry Pi 3, a Raspberry Pi 5 and a Hetzner CAX11 this keeps tty00/cua00; on an ODROID HC4 and an M2 MacBook, where amluart/exuart isn't the console and so has no /dev node of its own, tty00 is still dropped. The same kind of temp-dir test as before fails on the current code (nothing is returned) and passes with the change:
[Fact]
public void OpenBSD_SerialConsoleOwnsTheComNodes()
{
CreateDev("tty00", "cua00", "tty01", "cua01");
WriteDmesgBoot("""
OpenBSD 8.0 (GENERIC.MP) #84: Tue Oct 6 23:13:49 MDT 2026
pluart0 at simplebus1: rev 3, 32 byte fifo
pluart0: console
""");
Assert.Equal(new[] { "cua00", "tty00" }, GetPortNames_OpenBSD());
}Note
This comment was generated with the help of AI. Please review before applying.
|
@pgrawehr @raffaeler please see if you want to take a look at this as well |
|
Applied in 5f17816, thanks. |
Co-authored-by: krwq <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
krwq
left a comment
There was a problem hiding this comment.
LGTM on my end now, would be nice if @wfurt could review as well and if we got some testing with real hardware but given it's currently not working I'm willing to also take it as is as long as you confirm it works for your scenario.
|
I have no experience with any of the newly supported OS types, so I don't think I can be of help here. |
|
@krwq I have no experience on BSD-like OSes. |
No description provided.