From 9be4a282c40b638062b6b66df4b54dbe61464e39 Mon Sep 17 00:00:00 2001 From: Junjie Mao Date: Sat, 26 Mar 2022 00:12:08 +0800 Subject: [PATCH] config_tools: extract serial ttys in board XML This patch extracts all serial ttys in the native environment and updates the XML schema to specify the XPATH that evaluates to the available ttys for users to choose as the hypervisor console port. Tracked-On: #6690 Signed-off-by: Junjie Mao --- .../extractors/70-device-classes.py | 47 ++++++++++++------- misc/config_tools/schema/config.xsd | 3 +- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/misc/config_tools/board_inspector/extractors/70-device-classes.py b/misc/config_tools/board_inspector/extractors/70-device-classes.py index 89ed6a18f..dbd50519e 100644 --- a/misc/config_tools/board_inspector/extractors/70-device-classes.py +++ b/misc/config_tools/board_inspector/extractors/70-device-classes.py @@ -7,36 +7,51 @@ import re, os import logging from extractors.helpers import add_child, get_node -SYS_DEVICES_CLASS_PATH = "/sys/class/input/" +SYS_INPUT_DEVICES_CLASS_PATH = "/sys/class/input/" +SYS_TTY_DEVICES_CLASS_PATH = "/sys/class/tty/" + +def add_child_with_file_contents(parent_node, tag, filepath, translations = {}): + try: + with open(filepath, "r") as f: + res = f.read().strip() + if res in translations.keys(): + add_child(parent_node, tag, translations[res]) + else: + add_child(parent_node, tag, res) + except Exception as e: + logging.warning(f"Failed to read the data from {filepath}: {e}") def get_input_ids(): input_ids = list() root_regex = re.compile("input([0-9]+)") - for root in filter(lambda x: x.startswith("input"), os.listdir(SYS_DEVICES_CLASS_PATH)): + for root in filter(lambda x: x.startswith("input"), os.listdir(SYS_INPUT_DEVICES_CLASS_PATH)): m = root_regex.match(root) if m: input_ids.append(int(m.group(1))) return sorted(input_ids) -def extract_topology(device_classes_node): +def extract_inputs(device_classes_node): inputs_node = add_child(device_classes_node, "inputs", None) input_ids = get_input_ids() for id in input_ids: input_node = add_child(inputs_node, "input", None) - try: - with open("/sys/class/input/input{}/name".format(id), "r") as f: - res = f.read().strip() - add_child(input_node, "name", res) - except Exception as e: - logging.warning(f"Failed to read the data of /sys/class/input/input{id}/name: {e}") + add_child_with_file_contents(input_node, "name", f"/sys/class/input/input{id}/name") + add_child_with_file_contents(input_node, "phys", f"/sys/class/input/input{id}/phys") - try: - with open("/sys/class/input/input{}/phys".format(id), "r") as f: - res = f.read().strip() - add_child(input_node, "phys", res) - except Exception as e: - logging.warning(f"Failed to read the data of /sys/class/input/input{id}/phys: {e}") +def get_serial_devs(): + return sorted(filter(lambda x: x.startswith("ttyS"), os.listdir(SYS_TTY_DEVICES_CLASS_PATH)), key=lambda x:int(x[4:])) + +def extract_ttys(device_classes_node): + ttys_node = add_child(device_classes_node, "ttys", None) + for serial_dev in get_serial_devs(): + serial_node = add_child(ttys_node, "serial") + add_child(serial_node, "dev_path", f"/dev/{serial_dev}") + add_child_with_file_contents(serial_node, "type", f"{SYS_TTY_DEVICES_CLASS_PATH}{serial_dev}/type") + +def extract_topology(device_classes_node): + extract_inputs(device_classes_node) + extract_ttys(device_classes_node) def extract(args, board_etree): device_classes_node = get_node(board_etree, "//device-classes") - extract_topology(device_classes_node) \ No newline at end of file + extract_topology(device_classes_node) diff --git a/misc/config_tools/schema/config.xsd b/misc/config_tools/schema/config.xsd index fde228dae..28129da7a 100644 --- a/misc/config_tools/schema/config.xsd +++ b/misc/config_tools/schema/config.xsd @@ -19,7 +19,8 @@ - + Select the host serial device used for hypervisor debugging.