From c7f305ab7f01519ed8ce349dbc5d971270c954df Mon Sep 17 00:00:00 2001 From: Randolph Sapp Date: Tue, 15 Jul 2025 02:42:41 -0500 Subject: [PATCH 1/2] feat(ifconfig): add a new validation function Add a new validation wrapper to guard against problematic ifconfig values. This also allows us to quickly make all conditionally masked sections visible and play with that logic quickly in a global fashion. This basically adds an early warning mechanism for config values that are no longer valid, misspellings, etc. This is a bit of a hack. The process_ifconfig_nodes in sphinx actually builds a local namespace for evaluating the Python given in the directive. As such only builtin functions are carried over. This means to get anything into that context we need to taint the global builtins definitions. This is fine so long as we don't collide with anything. Signed-off-by: Randolph Sapp --- conf.py | 2 +- scripts/ifconfig_helpers.py | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 scripts/ifconfig_helpers.py diff --git a/conf.py b/conf.py index 230f0d62d..e347c074e 100644 --- a/conf.py +++ b/conf.py @@ -25,7 +25,7 @@ rootdir = os.environ.get('ROOTDIR') sys.path.insert(0, os.path.abspath(rootdir)) -from scripts import interpretvalues, sectinc, replacevars +from scripts import interpretvalues, sectinc, replacevars, ifconfig_helpers # -- General configuration ------------------------------------------------ diff --git a/scripts/ifconfig_helpers.py b/scripts/ifconfig_helpers.py new file mode 100644 index 000000000..b77785629 --- /dev/null +++ b/scripts/ifconfig_helpers.py @@ -0,0 +1,64 @@ +"""Helper functions to guard against incorrect ifconfig usage + +SPDX-License-Identifier: MIT +Copyright (C) 2025 Texas Instruments Incorporated - https://www.ti.com +""" + +import re +import sys +from pathlib import Path + +# This is not a full regular expression for config values, as '\' is valid, but it's good enough for +# the processing below +_CONFIG_REGEX = re.compile(r"'(CONFIG_.*)'\s*:\s*'(.*)'") + + +def _load_internal_set(path): + """Populate the internal set we use to check that the expected values are sane + + :param path: Pathlib path to config directory + """ + internal_set_dict = {} + if not path.is_dir(): + return internal_set_dict + + for config_path in path.glob("**/*_config.txt"): + with config_path.open("r", encoding="utf-8") as file: + for line in file: + match = _CONFIG_REGEX.match(line) + if match: + key = match.group(1) + key_set = internal_set_dict.get(key, set()) + key_set.add(match.group(2)) + internal_set_dict[key] = key_set + + return internal_set_dict + + +_INTERNAL_SET_DICT = _load_internal_set( + Path(__file__).parent.parent.joinpath("configs/") +) + + +def key_within(config, expected): + """Check if the values specified are all valid and return if the value of the config option is + in that set + + :param config: Config value in current scope + :param expected: Any iterable set of values expected to be in the key + """ + real_set = set(expected) + if not real_set.issubset(_INTERNAL_SET_DICT[config]): + raise ValueError(f"Given expectations are unreasonable for '{config}'") + + return locals()[config] in expected + + +class CollisionException(Exception): + """Exception class to indicate when a function definition collision occurs""" + + +if __builtins__.get("key_within") is not None: + raise CollisionException("Function collides with an existing definition") + +__builtins__["key_within"] = key_within From 1612ac5d4af239f70c12045521661b14945ae13e Mon Sep 17 00:00:00 2001 From: Randolph Sapp Date: Tue, 15 Jul 2025 03:48:43 -0500 Subject: [PATCH 2/2] feat(cpsw-ptp): use new key_within helper Use the new key_within helper and add a buggy platform entry to see if it catches it. Signed-off-by: Randolph Sapp --- .../Kernel/Kernel_Drivers/Network/CPSW-PTP.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/linux/Foundational_Components/Kernel/Kernel_Drivers/Network/CPSW-PTP.rst b/source/linux/Foundational_Components/Kernel/Kernel_Drivers/Network/CPSW-PTP.rst index c3069ddde..d27c50285 100644 --- a/source/linux/Foundational_Components/Kernel/Kernel_Drivers/Network/CPSW-PTP.rst +++ b/source/linux/Foundational_Components/Kernel/Kernel_Drivers/Network/CPSW-PTP.rst @@ -240,7 +240,7 @@ CPWSxG CPTS GENF0 output to HW4_TS_PUSH input. The following block shows the Time Sync Router device-tree node with the mapping from CPSWxG CPTS GENF0 to HW4_TS_PUSH added. -.. ifconfig:: CONFIG_part_variant in ('AM62AX','AM62X') +.. ifconfig:: key_within('CONFIG_part_variant', ('AM62AX','AM62X','A')) ::