|
5 | 5 | # data coming from the devicetree for configurations using
|
6 | 6 | # a bootloader.
|
7 | 7 |
|
8 |
| -function(get_address_from_dt_partition_nodelabel label address) |
9 |
| - dt_nodelabel(partition_node TARGET ${DEFAULT_IMAGE} NODELABEL ${label} REQUIRED) |
10 |
| - dt_reg_addr(partition_offset TARGET ${DEFAULT_IMAGE} PATH ${partition_node}) |
| 8 | +function(dt_get_parent node) |
| 9 | + string(FIND "${${node}}" "/" pos REVERSE) |
| 10 | + |
| 11 | + if(pos EQUAL -1) |
| 12 | + message(FATAL_ERROR "Unable to get parent of node: ${${node}}") |
| 13 | + endif() |
| 14 | + |
| 15 | + string(SUBSTRING "${${node}}" 0 ${pos} ${node}) |
| 16 | + set(${node} "${${node}}" PARENT_SCOPE) |
| 17 | +endfunction() |
| 18 | + |
| 19 | +# Usage: |
| 20 | +# dt_partition_addr(<var> [LABEL <label>|PATH <path>] [TARGET <target>] [ABSOLUTE]) |
| 21 | +# |
| 22 | +# Get the partition address, based on the path or label. |
| 23 | +# The value will be returned in the <var> parameter. |
| 24 | +# The returned address is either relative to the memory controller, or absolute if ABSOLUTE is |
| 25 | +# specified. |
| 26 | +# |
| 27 | +# The <path> value may be any of these: |
| 28 | +# |
| 29 | +# - absolute path to a node, like '/foo/bar' |
| 30 | +# - a node alias, like 'my-alias' |
| 31 | +# - a node alias followed by a path to a child node, like 'my-alias/child-node' |
| 32 | +# |
| 33 | +# Results can be: |
| 34 | +# - The base address of the register block |
| 35 | +# - <var> will be undefined if node does not exists or does not have a register block at the |
| 36 | +# requested index or with the requested name |
| 37 | +# |
| 38 | +# <var> : Return variable where the address value will be stored |
| 39 | +# LABEL <label> : Node label |
| 40 | +# PATH <path> : Node path |
| 41 | +# TARGET <target>: Optional target to retrieve devicetree information from |
| 42 | +# ABSOLUTE : Return absolute address rather than address relative to the memory controller |
| 43 | +# REQUIRED : Generate a fatal error if the node is not found |
| 44 | +function(dt_partition_addr var) |
| 45 | + cmake_parse_arguments(arg_DT_PARTITION "ABSOLUTE;REQUIRED" "LABEL;PATH;TARGET" "" ${ARGN}) |
| 46 | + |
| 47 | + zephyr_check_arguments_required(${CMAKE_CURRENT_FUNCTION} arg_DT_PARTITION PATH LABEL) |
| 48 | + zephyr_check_arguments_exclusive(${CMAKE_CURRENT_FUNCTION} arg_DT_PARTITION PATH LABEL) |
| 49 | + |
| 50 | + if(NOT arg_DT_PARTITION_PATH) |
| 51 | + # Calculate the partition path, based on the label. |
| 52 | + dt_nodelabel(arg_DT_PARTITION_PATH NODELABEL ${arg_DT_PARTITION_LABEL} TARGET |
| 53 | + ${arg_DT_PARTITION_TARGET}) |
| 54 | + if(NOT DEFINED arg_DT_PARTITION_PATH) |
| 55 | + if(arg_DT_PARTITION_REQUIRED) |
| 56 | + message(FATAL_ERROR "dt_partition_addr: Unable to find a node with label: " |
| 57 | + "${arg_DT_PARTITION_LABEL} for target: ${arg_DT_PARTITION_TARGET}") |
| 58 | + else() |
| 59 | + set(${var} "${var}-NOTFOUND" PARENT_SCOPE) |
| 60 | + return() |
| 61 | + endif() |
| 62 | + endif() |
| 63 | + else() |
| 64 | + # Check if a partition with the given path exists. |
| 65 | + dt_node_exists(arg_DT_PARTITION_EXISTS PATH ${arg_DT_PARTITION_PATH} TARGET |
| 66 | + ${arg_DT_PARTITION_TARGET}) |
| 67 | + if(NOT arg_DT_PARTITION_EXISTS) |
| 68 | + if(arg_DT_PARTITION_REQUIRED) |
| 69 | + message(FATAL_ERROR "dt_partition_addr: Unable to find a node with path: " |
| 70 | + "${arg_DT_PARTITION_PATH} for target: ${arg_DT_PARTITION_TARGET}") |
| 71 | + else() |
| 72 | + set(${var} "${var}-NOTFOUND" PARENT_SCOPE) |
| 73 | + return() |
| 74 | + endif() |
| 75 | + endif() |
| 76 | + endif() |
11 | 77 |
|
12 |
| - # Get the parent "two levels up" (../../) of the partition node |
13 |
| - # This is the partition flash area node |
14 |
| - string(REPLACE "/" ";" partition_node_split ${partition_node}) |
15 |
| - list(LENGTH partition_node_split child_path_length) |
16 |
| - math(EXPR parent_path_length "${child_path_length} - 2") |
17 |
| - list(SUBLIST partition_node_split 0 ${parent_path_length} parent_path_split) |
18 |
| - string(REPLACE ";" "/" flash_area_node "${parent_path_split}") |
| 78 | + # Get the list of partitions and subpartitions. |
| 79 | + dt_comp_path(fixed_partitions COMPATIBLE "fixed-partitions" TARGET ${arg_DT_PARTITION_TARGET}) |
| 80 | + dt_comp_path(fixed_subpartitions COMPATIBLE "fixed-subpartitions" TARGET |
| 81 | + ${arg_DT_PARTITION_TARGET}) |
19 | 82 |
|
20 |
| - dt_reg_addr(flash_area_addr TARGET ${DEFAULT_IMAGE} PATH ${flash_area_node}) |
| 83 | + # Read the partition offset. |
| 84 | + dt_reg_addr(dt_partition_base_addr PATH ${arg_DT_PARTITION_PATH} TARGET |
| 85 | + ${arg_DT_PARTITION_TARGET}) |
21 | 86 |
|
22 |
| - math(EXPR ${address} "${flash_area_addr} + ${partition_offset}") |
| 87 | + # The partition parent should be either a fixed-partitions or a fixed-subpartitions node. |
| 88 | + set(dt_partition_parent "${arg_DT_PARTITION_PATH}") |
| 89 | + dt_get_parent(dt_partition_parent) |
| 90 | + |
| 91 | + if("${dt_partition_parent}" IN_LIST fixed_subpartitions) |
| 92 | + # If the parent is a subpartition, add the parent partition address. |
| 93 | + dt_reg_addr(parent_addr PATH ${dt_partition_parent} TARGET ${arg_DT_PARTITION_TARGET}) |
| 94 | + math(EXPR dt_partition_base_addr "${dt_partition_base_addr} + ${parent_addr}" OUTPUT_FORMAT |
| 95 | + HEXADECIMAL) |
| 96 | + |
| 97 | + # Get the parent of the subpartition node, which should be a fixed-partitions node. |
| 98 | + dt_get_parent(dt_partition_parent) |
| 99 | + elseif(NOT "${dt_partition_parent}" IN_LIST fixed_partitions) |
| 100 | + message(FATAL_ERROR "dt_partition_addr: Node is not a partition or subpartition: " |
| 101 | + "${arg_DT_PARTITION_PATH} in target: ${arg_DT_PARTITION_TARGET}") |
| 102 | + endif() |
| 103 | + |
| 104 | + if(NOT arg_DT_PARTITION_ABSOLUTE) |
| 105 | + # A parent of the "fixed-partitions" node should be the memory controller. |
| 106 | + dt_get_parent(dt_partition_parent) |
| 107 | + # Add the memory controller base address to get an absolute address. |
| 108 | + dt_reg_addr(parent_addr PATH ${dt_partition_parent} TARGET ${arg_DT_PARTITION_TARGET}) |
| 109 | + math(EXPR dt_partition_base_addr "${dt_partition_base_addr} + ${parent_addr}" OUTPUT_FORMAT |
| 110 | + HEXADECIMAL) |
| 111 | + endif() |
| 112 | + |
| 113 | + set(${var} "${dt_partition_base_addr}" PARENT_SCOPE) |
| 114 | +endfunction() |
| 115 | + |
| 116 | +function(get_address_from_dt_partition_nodelabel label address) |
| 117 | + dt_partition_addr(${address} LABEL ${label} TARGET ${DEFAULT_IMAGE} ABSOLUTE REQUIRED) |
23 | 118 | set(${address} ${${address}} PARENT_SCOPE)
|
24 | 119 | endfunction()
|
0 commit comments