From 1d40a7ee3415d8bb6a3772c41ce580bc8a8c2db4 Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 16 Jun 2024 13:03:57 +0900 Subject: [PATCH] tests: lib: devicetree: add test for `zephyr,memory-region-flags` attr Add a test for zephyr,memory-region-flags. This test tests the attributes of memory regions defined in the linker script, but this information cannot be obtained at runtime. It verifies that the expected memory region attributes are defined in the linker.cmd. Since no syntax analysis is performed, this is not an entirely rigorous test but sufficient for practical purposes. Signed-off-by: TOKITA Hiroshi --- .../memory_region_flags/CMakeLists.txt | 9 +++ .../memory_region_flags/app.overlay | 37 ++++++++++ .../devicetree/memory_region_flags/prj.conf | 1 + .../pytest/test_memory_region_flags.py | 73 +++++++++++++++++++ .../devicetree/memory_region_flags/src/main.c | 10 +++ .../memory_region_flags/testcase.yaml | 17 +++++ 6 files changed, 147 insertions(+) create mode 100644 tests/lib/devicetree/memory_region_flags/CMakeLists.txt create mode 100644 tests/lib/devicetree/memory_region_flags/app.overlay create mode 100644 tests/lib/devicetree/memory_region_flags/prj.conf create mode 100644 tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py create mode 100644 tests/lib/devicetree/memory_region_flags/src/main.c create mode 100644 tests/lib/devicetree/memory_region_flags/testcase.yaml diff --git a/tests/lib/devicetree/memory_region_flags/CMakeLists.txt b/tests/lib/devicetree/memory_region_flags/CMakeLists.txt new file mode 100644 index 00000000000000..178264b34a13d8 --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(memory_region_flags) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/lib/devicetree/memory_region_flags/app.overlay b/tests/lib/devicetree/memory_region_flags/app.overlay new file mode 100644 index 00000000000000..481ac6ce8aaf4d --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/app.overlay @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024, TOKITA Hiroshi + */ + +/ { + test { + #address-cells = < 0x1 >; + #size-cells = < 0x1 >; + + test_region_r: sram@20010000 { + compatible = "zephyr,memory-region"; + reg = < 0x20010000 0x100 >; + zephyr,memory-region = "TEST_REGION_R"; + zephyr,memory-region-flags = "r"; + }; + + test_region_nrwxail: sram@20010100 { + compatible = "zephyr,memory-region"; + reg = < 0x20010100 0x100 >; + zephyr,memory-region = "TEST_REGION_NRWXAIL"; + zephyr,memory-region-flags = "!rwxail"; + }; + + test_region_no_flags: sram@20010200 { + compatible = "zephyr,memory-region"; + reg = < 0x20010200 0x100 >; + zephyr,memory-region = "TEST_REGION_NO_FLAGS"; + }; + + test_region_none: sram@20010300 { + compatible = "zephyr,memory-region"; + reg = < 0x20010300 0x100 >; + zephyr,memory-region = "TEST_REGION_NONE"; + zephyr,memory-region-flags = ""; + }; + }; +}; diff --git a/tests/lib/devicetree/memory_region_flags/prj.conf b/tests/lib/devicetree/memory_region_flags/prj.conf new file mode 100644 index 00000000000000..1bb8bf6d7fd4c8 --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/prj.conf @@ -0,0 +1 @@ +# empty diff --git a/tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py b/tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py new file mode 100644 index 00000000000000..d25a03a49aa736 --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/pytest/test_memory_region_flags.py @@ -0,0 +1,73 @@ +# Copyright (c) 2024, TOKITA Hiroshi +# SPDX-License-Identifier: Apache-2.0 + +import os +import sys +import logging +import re +from twister_harness import DeviceAdapter + +ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") +sys.path.insert( + 0, os.path.join(ZEPHYR_BASE, "scripts", "dts", "python-devicetree", "src") +) +from devicetree import edtlib + +logger = logging.getLogger(__name__) + + +def smart_int(numstr): + if numstr.startswith("0x"): + return int(numstr[2:], 16) + else: + return int(numstr) + + +def verify_memory_region_flags(build_dir, label, expect): + logger.info(label) + + linkercmd = os.path.join(build_dir, "zephyr", "linker.cmd") + logger.info(linkercmd) + + edt = edtlib.EDT( + os.path.join(build_dir, "zephyr", "zephyr.dts"), + [os.path.join(ZEPHYR_BASE, "dts", "bindings")], + ) + + node = edt.label2node[label] + logger.info(node) + + region = node.props["zephyr,memory-region"].val + logger.info(region) + + origin = node.props["reg"].val[0] + length = node.props["reg"].val[1] + pattern = ( + region + r"\s*" + expect + + r"\s*:\s*ORIGIN\s*=\s*\(?([0-9A-Fa-fx]*)\)?,\s*LENGTH\s*=\s*\(?([0-9A-Fa-fx]*)\)?" + ) + + logger.info(pattern) + + found = False + + with open(linkercmd) as f: + for line in f: + m = re.search(pattern, line) + if m and smart_int(m[1]) == origin and smart_int(m[2]) == length: + found = True + + assert found + + +def test_region_r(dut: DeviceAdapter): + verify_memory_region_flags(dut.device_config.build_dir, "test_region_r", r"\(\s*r\s*\)") + +def test_region_nrwxail(dut: DeviceAdapter): + verify_memory_region_flags(dut.device_config.build_dir, "test_region_nrwxail", r"\(\s*!rwxail\s*\)") + +def test_region_no_flags(dut: DeviceAdapter): + verify_memory_region_flags(dut.device_config.build_dir, "test_region_no_flags", r"\(\s*rw\s*\)") + +def test_region_none(dut: DeviceAdapter): + verify_memory_region_flags(dut.device_config.build_dir, "test_region_none", r"") diff --git a/tests/lib/devicetree/memory_region_flags/src/main.c b/tests/lib/devicetree/memory_region_flags/src/main.c new file mode 100644 index 00000000000000..af84bb23479274 --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/src/main.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2024, TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +int main(void) +{ + return 0; +} diff --git a/tests/lib/devicetree/memory_region_flags/testcase.yaml b/tests/lib/devicetree/memory_region_flags/testcase.yaml new file mode 100644 index 00000000000000..893e118976d2d5 --- /dev/null +++ b/tests/lib/devicetree/memory_region_flags/testcase.yaml @@ -0,0 +1,17 @@ +tests: + + libraries.devicetree.memory_region_flags: + harness: pytest + platform_allow: qemu_cortex_m3 + tags: + - devicetree + - pytest + + libraries.devicetree.memory_region_flags.linker_generator: + harness: pytest + platform_allow: qemu_cortex_m3 + tags: + - devicetree + - pytest + extra_configs: + - CONFIG_CMAKE_LINKER_GENERATOR=y