#!/bin/sh

## Copyright (C) 2019 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## Want to disable serial console?
## Feel free to delete this configuration file.

## Forum discussion:
## https://forums.whonix.org/t/how-do-i-enter-the-whonix-shell-from-cli/7271
## https://forums.whonix.org/t/serial-console-in-virtualbox/8021
## https://forums.whonix.org/t/grub-serial-menu-in-virsh-console/8097

## Cope up with "set -o nounset".
: "${GRUB_CMDLINE_LINUX:=""}" || echo "$0: Setting default GRUB_CMDLINE_LINUX failed! Please report this bug!" >&2
: "${GRUB_TERMINAL:=""}" || echo "$0: Setting default GRUB_TERMINAL failed! Please report this bug!" >&2
: "${GRUB_SERIAL_COMMAND:=""}" || echo "$0: Setting default GRUB_SERIAL_COMMAND failed! Please report this bug!" >&2

## Use 'serial' ONLY as the GRUB terminal (both input and output).
##
## A dual terminal such as "console serial" lets GRUB show the menu over serial
## but is unreliable for INPUT: on many setups GRUB then only accepts input from
## the primary (console) terminal, so the serial console can view the menu but
## cannot drive it. Under EFI it is worse: the firmware mirrors the EFI console
## to the serial port, so with "console" present GRUB's serial output is
## duplicated/garbled. serial-only is clean and driveable on both BIOS and EFI.
## https://lists.gnu.org/archive/html/help-grub/2020-11/msg00006.html
##
## Trade-off: the GRUB menu no longer renders on the graphical console (e.g. a
## VirtualBox window); the kernel and getty login still appear on both. This
## package is opt-in specifically to obtain a serial console, so serial wins.
##
## Set unconditionally (not appended): appending to a pre-existing value such as
## "console" would re-create the broken "console serial" dual terminal. A user
## who really wants a different terminal can still override this in the
## higher-numbered /etc/default/grub.d/50_user.cfg.
GRUB_TERMINAL="serial"

## Quote https://www.virtualbox.org/wiki/Serial_redirect but not specific to VirtualBox:
##
##     console=ttyS0 console=tty0 ignore_loglevel
##
## The first parameter will ensure that all kernel output is redirected to the serial console. The second parameter will ensure that the kernel output is still logged to the guest text console. The third parameter enforces the guest Linux kernel to print all kernel messages to the console.

if [ "$GRUB_CMDLINE_LINUX" = "" ]; then
   GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
else
   GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX console=tty0 console=ttyS0,115200n8"
fi

if [ "$GRUB_SERIAL_COMMAND" = "" ]; then
   GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
fi
