|
| 1 | +# Copyright (c) 2025 Nordic Semiconductor ASA |
| 2 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 3 | + |
| 4 | +# This file contains utility functions used for handling |
| 5 | +# data coming from the devicetree for configurations using |
| 6 | +# a bootloader. |
| 7 | +# It contains helper functions used to get the addresses of |
| 8 | +# specific bootloader patitions as well as |
| 9 | +# functions that are used to verify the |
| 10 | +# that devicetrees of the images are consistent with each other |
| 11 | +# and with the current bootloader configuration. |
| 12 | + |
| 13 | +function(get_address_from_dt_partition_nodelabel label address) |
| 14 | + dt_nodelabel(partition_node TARGET ${DEFAULT_IMAGE} NODELABEL ${label} REQUIRED) |
| 15 | + dt_reg_addr(partition_offset TARGET ${DEFAULT_IMAGE} PATH ${partition_node}) |
| 16 | + |
| 17 | + # Get the parent "two levels up" (../../) of the partition node |
| 18 | + # This is the partition flash area node |
| 19 | + string(REPLACE "/" ";" partition_node_split ${partition_node}) |
| 20 | + list(LENGTH partition_node_split child_path_length) |
| 21 | + math(EXPR parent_path_length "${child_path_length} - 2") |
| 22 | + list(SUBLIST partition_node_split 0 ${parent_path_length} parent_path_split) |
| 23 | + string(REPLACE ";" "/" flash_area_node "${parent_path_split}") |
| 24 | + |
| 25 | + dt_reg_addr(flash_area_addr TARGET ${DEFAULT_IMAGE} PATH ${flash_area_node}) |
| 26 | + |
| 27 | + math(EXPR ${address} "${flash_area_addr} + ${partition_offset}") |
| 28 | + set(${address} ${${address}} PARENT_SCOPE) |
| 29 | +endfunction() |
| 30 | + |
| 31 | +# |
| 32 | +# Verify that the code partition for a given image matches a specific node label. |
| 33 | +# |
| 34 | +function(code_partition_matches_label_verify label image) |
| 35 | + dt_nodelabel(label_node TARGET ${image} NODELABEL ${label}) |
| 36 | + dt_chosen(code_partition_node TARGET ${image} PROPERTY "zephyr,code-partition") |
| 37 | + |
| 38 | + if (NOT "${code_partition_node}" STREQUAL "${label_node}") |
| 39 | + message(FATAL_ERROR " |
| 40 | + ERROR: zephyr,code-partition for image ${image} |
| 41 | + does not match label ${label}. |
| 42 | + This is required by the current bootloader configuration. |
| 43 | + zephyr,code-partition node: |
| 44 | + ${code_partition_node} |
| 45 | + ${label} node: |
| 46 | + ${label_node} |
| 47 | + \n" |
| 48 | + ) |
| 49 | + endif() |
| 50 | +endfunction() |
| 51 | + |
| 52 | +# |
| 53 | +# Verify that the required node labels for all images point to the same nodes |
| 54 | +# as for the default image |
| 55 | +# |
| 56 | +function(labels_match_for_all_images_verify labels) |
| 57 | + foreach(label ${labels}) |
| 58 | + dt_nodelabel(default_image_node TARGET ${DEFAULT_IMAGE} NODELABEL ${label} REQUIRED) |
| 59 | + foreach(image ${IMAGES}) |
| 60 | + dt_nodelabel(node TARGET ${image} NODELABEL ${label} REQUIRED) |
| 61 | + if (NOT "${node}" STREQUAL "${default_image_node}") |
| 62 | + message(FATAL_ERROR " |
| 63 | + ERROR: Node pointed by ${label} for image ${image} |
| 64 | + does not match the node for the default image ${DEFAULT_IMAGE}. |
| 65 | + Node for default image ${DEFAULT_IMAGE}: ${default_image_node} |
| 66 | + Node for image ${image}: ${label_node} |
| 67 | + \n" |
| 68 | + ) |
| 69 | + endif() |
| 70 | + endforeach() |
| 71 | + endforeach() |
| 72 | +endfunction() |
| 73 | + |
| 74 | +function(verify_bootloader_dts_configuration) |
| 75 | + set(required_labels_list) |
| 76 | + set(nordic_bootloader_vars_names) |
| 77 | + |
| 78 | + # Create a list of all devicetree labels that are required for |
| 79 | + # the current bootloader configuration. |
| 80 | + if(SB_CONFIG_SECURE_BOOT) |
| 81 | + list(APPEND required_labels_list "b0_partition" "provision_partition" "s0_partition") |
| 82 | + endif() |
| 83 | + if(SB_CONFIG_BOOTLOADER_MCUBOOT) |
| 84 | + list(APPEND required_labels_list "boot_partition") |
| 85 | + list(APPEND required_labels_list "slot0_partition") |
| 86 | + list(APPEND required_labels_list "slot1_partition") |
| 87 | + endif() |
| 88 | + if(SB_CONFIG_SECURE_BOOT_BUILD_S1_VARIANT_IMAGE) |
| 89 | + list(APPEND required_labels_list "s1_partition") |
| 90 | + endif() |
| 91 | + |
| 92 | + # Check that the labels are present for all images and that they point to the same nodes |
| 93 | + # for each of the images. |
| 94 | + # This way we can treat a devicetree from any of the images as the source of truth for |
| 95 | + # the whole system when it comes to the required node labels. |
| 96 | + labels_match_for_all_images_verify("${required_labels_list}") |
| 97 | + |
| 98 | + # Verify that the zephyr,code-partition chosen node for each of the images |
| 99 | + # points to the expected node labels |
| 100 | + if(SB_CONFIG_BOOTLOADER_MCUBOOT) |
| 101 | + code_partition_matches_label_verify("slot0_partition" "${DEFAULT_IMAGE}") |
| 102 | + elseif(SB_CONFIG_SECURE_BOOT) |
| 103 | + code_partition_matches_label_verify("s0_partition" "${DEFAULT_IMAGE}") |
| 104 | + endif() |
| 105 | + |
| 106 | + if(SB_CONFIG_SECURE_BOOT) |
| 107 | + code_partition_matches_label_verify("b0_partition" "b0") |
| 108 | + endif() |
| 109 | + if(SB_CONFIG_SECURE_BOOT_BUILD_S1_VARIANT_IMAGE) |
| 110 | + code_partition_matches_label_verify("s1_partition" "s1_image") |
| 111 | + endif() |
| 112 | + |
| 113 | + if(SB_CONFIG_BOOTLOADER_MCUBOOT) |
| 114 | + code_partition_matches_label_verify("boot_partition" "mcuboot") |
| 115 | + if(SB_CONFIG_SECURE_BOOT) |
| 116 | + # In the B0 + MCUBOOT configuration s0_partition points to boot_partition |
| 117 | + code_partition_matches_label_verify("s0_partition" "mcuboot") |
| 118 | + endif() |
| 119 | + endif() |
| 120 | +endfunction() |
| 121 | + |
| 122 | +function(nsib_get_s0_address address) |
| 123 | + set(s0_label "s0_partition") |
| 124 | + get_address_from_dt_partition_nodelabel(${s0_label} ${address}) |
| 125 | + set(${address} ${${address}} PARENT_SCOPE) |
| 126 | +endfunction() |
| 127 | + |
| 128 | +function(nsib_get_s1_address address) |
| 129 | + set(s1_label "s1_partition") |
| 130 | + get_address_from_dt_partition_nodelabel(${s1_label} ${address}) |
| 131 | + set(${address} ${${address}} PARENT_SCOPE) |
| 132 | +endfunction() |
| 133 | + |
| 134 | +function(nsib_get_provision_address address) |
| 135 | + get_address_from_dt_partition_nodelabel("provision_partition" ${address}) |
| 136 | + set(${address} ${${address}} PARENT_SCOPE) |
| 137 | +endfunction() |
0 commit comments