OPNsense Forum

English Forums => 23.7 Legacy Series => Topic started by: user1234 on November 16, 2023, 03:22:59 pm

Title: Serial console com port change
Post by: user1234 on November 16, 2023, 03:22:59 pm
It is possible to specify which serial port to use? e.g. /dev/ttyu1

My board has two serial ports and I can see the console on /dev/ttyu0 but not /dev/ttyu1.
I would like to use /dev/ttyu1 if possible.
Title: Re: Serial console com port change
Post by: meyergru on November 16, 2023, 03:57:28 pm
It depends on what you try to achieve.

First you should look if both serial ports are being recognized by examining the output of "dmesg | fgrep uart".

FreeBSD has some kernel parameters in /boot/loader.conf, namely:

Code: [Select]
# dynamically generated console settings follow
comconsole_speed="115200"
boot_multicons="YES"
boot_serial="YES"
#kern.vty
console="efi,comconsole"

However, OpnSense creates that file via rc.loader. Essentially, those settings are created from System->Settings->Administration->Console. I assume you have "Serial Console" (i.e. "comconsole") active there. Normally, the kernel decides by itself which serial device to use as the console and chooses the first one per default.

This can be changed by the uart flags, see "man uart (https://man.freebsd.org/cgi/man.cgi?query=uart&apropos=0&sektion=0&manpath=FreeBSD+12.1-RELEASE+and+Ports&arch=default&format=html)" and this page (https://wiki.freebsd.org/SerialConsole), which also explains how to set ports if they are non-standard. Again, this should be done in the "Tuneables" section in OpnSense, not in the FreeBSD files themselves.

Also, there is /etc/ttys (https://man.freebsd.org/cgi/man.cgi?query=ttys&apropos=0&sektion=5&manpath=FreeBSD+12.1-RELEASE&arch=default&format=html), where you can configure your serial ports as terminals. Usually, the first four serials are defined to be enabled if the kernel uses the for a console (onifconsole), but you can configure to use them if they exist at all by "onifexists":

Code: [Select]
# The 'dialup' keyword identifies dialin lines to login, fingerd etc.
ttyu0   "/usr/libexec/getty 3wire.115200"       vt100   onifconsole secure
ttyu1   "/usr/libexec/getty 3wire.115200"       vt100   onifconsole secure
ttyu2   "/usr/libexec/getty 3wire.115200"       vt100   onifconsole secure
ttyu3   "/usr/libexec/getty 3wire.115200"       vt100   onifconsole secure

So, in short:

1. Determine if all ports are detected and potentially correct this.
2. If you want to use ttyu1 as kernel console port, disable ttyu0 via uart flags and enable ttyu0. This will handle the switch of the terminal part automatically.
3. If you only want ttyu1 as an additional terminal, specify "onifexists" in /etc/ttys.

Title: Re: Serial console com port change
Post by: user1234 on November 16, 2023, 04:23:04 pm
Thanks that is really useful.
Ideally I would like to add ttyu1 as an additional console so this would be edit the /etc/ttys file to be "onifexists".

My follow up question is how do I make the /etc/ttys file persistent, can this be set via the opnsense interface?

Thanks
Title: Re: Serial console com port change
Post by: meyergru on November 16, 2023, 04:44:22 pm
By changing /etc/ttys, you will get just two terminals. I do not know if it is at all possible to have two kernel serial consoles. The specification allows for a serial and another EFI console, but I do not know if two serial ports would work through uart flags. The man page says the flag 10 tells this is a "potential system console", I would think that the kernel decides for one only.
Title: Re: Serial console com port change
Post by: user1234 on November 21, 2023, 12:04:38 pm
I have tried a number of different things now but nothing seems to work.
I have tried adding
Code: [Select]
hint.uart.0.disabled=1 to disable the first uart.

Also tried
Code: [Select]
hint.uart.1.flags=0x10 but no luck.

Strangely when I test the serial port via
Code: [Select]
echo "test" > /dev/ttyu1 it hangs and does not send. Doing this on /dev/ttyu0 works fine.

I know the hardware works as I booted into an Ubuntu live image and both serial ports work on there.

Any ideas?
Title: Re: Serial console com port change
Post by: user1234 on November 21, 2023, 12:17:00 pm
Comparing the two ports using
Code: [Select]
stty -a -f /dev/ttyu0 shows the difference between the ports is ttyu0 has "clocal" and ttyu1 has "-clocal"
Title: Re: Serial console com port change
Post by: meyergru on November 21, 2023, 09:17:39 pm
Yes, quoting myself:

This can be changed by the uart flags, see "man uart (https://man.freebsd.org/cgi/man.cgi?query=uart&apropos=0&sektion=0&manpath=FreeBSD+12.1-RELEASE+and+Ports&arch=default&format=html)" and this page (https://wiki.freebsd.org/SerialConsole), which also explains how to set ports if they are non-standard. Again, this should be done in the "Tuneables" section in OpnSense, not in the FreeBSD files themselves.

There are some default ISA port numbers on which the first four serial ports reside and which the kernels tests, but sometimes they are different, like on Supermicro mainboards. You have to find out which port number your second serial port is on.