From: Michael Tremer Date: Sun, 5 Feb 2023 19:00:29 +0000 (+0000) Subject: Parse the kernel command line X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=078a04b7257e92f1e100b609f28e3ff82fd068d7;p=people%2Fms%2Fbricklayer.git Parse the kernel command line This allows to change any configuration values, but mainly tries to figure out whether we are being launched on a serial console. Signed-off-by: Michael Tremer --- diff --git a/src/python/__init__.py b/src/python/__init__.py index 16c4d84..6c2f78a 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -20,6 +20,7 @@ import logging import os +import shlex import subprocess import sys import tempfile @@ -80,10 +81,15 @@ class Bricklayer(object): # 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() @@ -333,3 +339,65 @@ class Bricklayer(object): # 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