]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
Parse the kernel command line
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 19:00:29 +0000 (19:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Feb 2023 19:00:29 +0000 (19:00 +0000)
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 <michael.tremer@ipfire.org>
src/python/__init__.py

index 16c4d84050ed00921c9ac6e37581555cd6e6ab91..6c2f78a360621ffbba5c0300137f6ac6b66de8ca 100644 (file)
@@ -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