import logging
import os
+import shlex
import subprocess
import sys
import tempfile
# Default timezone
"timezone" : "UTC",
- # Enable the serial console
- "serial-console" : False,
+ # Serial Console
+ "serial-console" : False,
+ "serial-console-device" : "ttyS0",
+ "serial-console-baudrate" : 115200,
}
+ # Parse the kernel command line
+ self._read_cmdline()
+
# Read OS information
self.os = self._read_os_release()
# Return all supported bootloaders
return [bl for bl in _bootloaders if bl.supported]
+
+ def _read_cmdline(self):
+ """
+ This function parses the kernel command line
+ """
+ with open("/proc/cmdline") as f:
+ # Read the entire content
+ line = f.readline()
+
+ # Process line for line...
+ for word in shlex.split(line):
+ # Split each word
+ key, delim, value = word.partition("=")
+
+ # Skip any keywords
+ if not key or not value:
+ continue
+
+ if key.startswith("bricklayer."):
+ self._cmdline_handle_keyword(key[11:], value)
+
+ elif key == "console":
+ self._cmdline_handle_console(value)
+
+ def _cmdline_handle_keyword(self, key, value):
+ """
+ Sets any configuration values from the commandline
+ """
+ self.settings[key] = value
+
+ def _cmdline_handle_console(self, value):
+ """
+ Parses the console= kernel parameter
+ """
+ # Fetch the first part which is the device name
+ device, delim, options = value.partition(",")
+
+ # Break if this option does NOT configure a serial console
+ if not device.startswith("ttyS"):
+ return
+
+ # This function does not properly handle all cases, but I think this
+ # is good enough for now...
+
+ # Remove the "n8" suffix
+ options = options.removesuffix("n8")
+
+ # The rest should
+ try:
+ baudrate = int(options)
+ except (TypeError, ValueError):
+ baudrate = None
+
+ # Store everything in settings
+ self.settings |= {
+ "serial-console" : True,
+ "serial-console-device" : device,
+ }
+
+ # Store baudrate if set
+ if baurate:
+ self.settings["serial-console-baudrate"] = baudrate