Skip to content

Commit

Permalink
tests: lib: devicetree: add test for zephyr,memory-region-flags attr
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
soburi authored and nashif committed Dec 5, 2024
1 parent 624e051 commit 1d40a7e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/lib/devicetree/memory_region_flags/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
37 changes: 37 additions & 0 deletions tests/lib/devicetree/memory_region_flags/app.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
*/

/ {
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 = "";
};
};
};
1 change: 1 addition & 0 deletions tests/lib/devicetree/memory_region_flags/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
# 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"")
10 changes: 10 additions & 0 deletions tests/lib/devicetree/memory_region_flags/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2024, TOKITA Hiroshi <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

int main(void)
{
return 0;
}
17 changes: 17 additions & 0 deletions tests/lib/devicetree/memory_region_flags/testcase.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1d40a7e

Please sign in to comment.