diff --git a/src/plugins/analysis/device_tree/code/device_tree.py b/src/plugins/analysis/device_tree/code/device_tree.py index 0d997901c3..2744288d9c 100644 --- a/src/plugins/analysis/device_tree/code/device_tree.py +++ b/src/plugins/analysis/device_tree/code/device_tree.py @@ -1,5 +1,6 @@ from __future__ import annotations +import re from typing import TYPE_CHECKING, Dict from semver import Version @@ -13,13 +14,16 @@ if TYPE_CHECKING: import io +DT_BINARY_DATA_REGEX_1 = re.compile(r'\[[0-9a-f ]{32,}]') +DT_BINARY_DATA_REGEX_2 = re.compile(r'<(0x[0-9a-f]+ ?){10,}>') + class AnalysisPlugin(AnalysisPluginV0): def __init__(self): metadata = self.MetaData( name='device_tree', description='get the device tree in text from the device tree blob', - version=Version(2, 0, 1), + version=Version(3, 0, 0), system_version=None, mime_blacklist=[*MIME_BLACKLIST_COMPRESSED, 'audio', 'image', 'video'], timeout=10, @@ -28,6 +32,9 @@ def __init__(self): super().__init__(metadata=metadata) def summarize(self, result: Schema) -> list[str]: + if not result.device_trees: + return [] + models = [device_tree.model for device_tree in result.device_trees if device_tree.model] if not models: @@ -50,6 +57,7 @@ def analyze( while (offset := binary.find(DeviceTree.Header.MAGIC, offset)) >= 0: try: device_tree = DeviceTree.from_binary(binary, offset=offset) + device_tree.string = self.replace_binary_data(device_tree.string) # We found a valid device tree. # Skip only the header because device trees may contain device trees themselves. offset += DeviceTree.Header.SIZE @@ -68,3 +76,12 @@ def get_tags(self, result: Schema, summary: list[str]) -> list[Tag]: color=TagColor.ORANGE, ), ] + + @staticmethod + def replace_binary_data(device_tree: str) -> str: + # textual device tree data can contain huge chunks of binary data + # -> remove them from the result if they are too large + return DT_BINARY_DATA_REGEX_2.sub( + '(BINARY DATA ...)', + DT_BINARY_DATA_REGEX_1.sub('(BINARY DATA ...)', device_tree), + ) diff --git a/src/plugins/analysis/device_tree/test/test_device_tree.py b/src/plugins/analysis/device_tree/test/test_device_tree.py index 842a202afb..ea9e2f9921 100644 --- a/src/plugins/analysis/device_tree/test/test_device_tree.py +++ b/src/plugins/analysis/device_tree/test/test_device_tree.py @@ -38,4 +38,21 @@ def test_multiple_device_trees(file, analysis_plugin): @pytest.mark.parametrize('file', [TEST_FP, TEST_BROKEN]) def test_no_device_trees(file, analysis_plugin): result = analysis_plugin.analyze(io.FileIO(file), {}, {}) + summary = analysis_plugin.summarize(result) assert result.device_trees == [] + assert summary == [] + + +@pytest.mark.AnalysisPluginTestConfig(plugin_class=AnalysisPlugin) +@pytest.mark.parametrize( + ('input_dts', 'expected_result'), + [ + ('', ''), + ('data = [01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef];', 'data = (BINARY DATA ...);'), + ('data = <0x01 0x2345 0x67 0x89 0xabcdef 0x1234 0x56 0x78 0x90 0xab 0xcd>;', 'data = (BINARY DATA ...);'), + ('data = [01 23 45 67];', 'data = [01 23 45 67];'), # short entries should not be replaced + ('data = <0x01 0x2345 0x67>;', 'data = <0x01 0x2345 0x67>;'), # short entries should not be replaced + ], +) +def test_replace_binary_data(analysis_plugin, input_dts, expected_result): + assert analysis_plugin.replace_binary_data(input_dts) == expected_result diff --git a/src/plugins/analysis/device_tree/view/device_tree.html b/src/plugins/analysis/device_tree/view/device_tree.html index 2c49758753..f567b95440 100644 --- a/src/plugins/analysis/device_tree/view/device_tree.html +++ b/src/plugins/analysis/device_tree/view/device_tree.html @@ -3,7 +3,7 @@ {% macro device_tree_table_cell(dt_data, colspan="1") %}
{{ dt_data | hide_dts_binary_data | nice_generic | safe }}
+ {{ dt_data | nice_generic | safe }}