Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 2 | # Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Chris Brand | f1d6b6f | 2022-07-22 11:49:01 -0700 | [diff] [blame] | 3 | # Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company) |
| 4 | # or an affiliate of Cypress Semiconductor Corporation. All rights reserved. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
| 7 | # |
| 8 | #------------------------------------------------------------------------------- |
| 9 | |
| 10 | import os |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 11 | import io |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 12 | import re |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 13 | import sys |
| 14 | import argparse |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 15 | import logging |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 16 | from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 17 | |
| 18 | try: |
| 19 | import yaml |
| 20 | except ImportError as e: |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 21 | logging.error (str(e) + " To install it, type:") |
| 22 | logging.error ("pip install PyYAML") |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 23 | exit(1) |
| 24 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 25 | donotedit_warning = \ |
Sherry Zhang | f58f2bd | 2022-01-10 17:21:11 +0800 | [diff] [blame] | 26 | ' WARNING: This is an auto-generated file. Do not edit! ' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 27 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 28 | TFM_ROOT_DIR = os.path.join(sys.path[0], '..') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 29 | OUT_DIR = None # The root directory that files are generated to |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 30 | |
Sherry Zhang | 87e6d2e | 2021-12-27 14:48:59 +0800 | [diff] [blame] | 31 | # PID[0, TFM_PID_BASE - 1] are reserved for TF-M SPM and test usages |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 32 | TFM_PID_BASE = 256 |
| 33 | |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 34 | # variable for checking for duplicated sid |
| 35 | sid_list = [] |
chesun01 | 2345aa5 | 2023-04-18 17:31:53 +0800 | [diff] [blame] | 36 | |
| 37 | # Summary of manifest attributes defined by FFM for use in the Secure Partition manifest file. |
| 38 | ffm_manifest_attributes = ['psa_framework_version', 'name', 'type', 'priority', 'model', 'entry_point', \ |
| 39 | 'stack_size', 'description', 'entry_init', 'heap_size', 'mmio_regions', 'services', 'irqs', 'dependencies'] |
| 40 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 41 | class TemplateLoader(BaseLoader): |
| 42 | """ |
| 43 | Template loader class. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 44 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 45 | An instance of this class is passed to the template engine. It is |
| 46 | responsible for reading the template file |
| 47 | """ |
| 48 | def __init__(self): |
| 49 | pass |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 50 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 51 | def get_source(self, environment, template): |
| 52 | """ |
| 53 | This function reads the template files. |
| 54 | For detailed documentation see: |
| 55 | http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source |
| 56 | |
| 57 | Please note that this function always return 'false' as 'uptodate' |
| 58 | value, so the output file will always be generated. |
| 59 | """ |
| 60 | if not os.path.isfile(template): |
| 61 | raise TemplateNotFound(template) |
| 62 | with open(template) as f: |
| 63 | source = f.read() |
| 64 | return source, template, False |
| 65 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 66 | def parse_configurations(file_paths): |
| 67 | """ |
| 68 | Parses the given config files and return a dict whose key-values are build |
| 69 | configurations and their values. |
| 70 | |
| 71 | Valid configurations should be in the format of: |
| 72 | "#define VAR [...]" in a single line. |
| 73 | The value of the config is optional. |
| 74 | """ |
| 75 | configurations = {} |
| 76 | |
| 77 | lines = [] |
| 78 | for file in file_paths: |
| 79 | with open(file, 'r') as config_file: |
| 80 | lines += config_file.readlines() |
| 81 | |
| 82 | for line in lines: |
| 83 | if not line.startswith('#define'): |
| 84 | continue |
| 85 | |
| 86 | line = line.rstrip('\r\n') |
| 87 | line_items = line.split(maxsplit=2) |
| 88 | if len(line_items) == 3: |
| 89 | configurations[line_items[1]] = line_items[2] |
| 90 | elif len(line_items) == 2: |
| 91 | configurations[line_items[1]] = '' |
| 92 | |
| 93 | logging.debug(configurations) |
| 94 | |
| 95 | return configurations |
| 96 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 97 | def manifest_validation(manifest, pid): |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 98 | """ |
| 99 | This function validates FF-M compliance for partition manifest, and sets |
| 100 | default values for optional attributes. |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 101 | The validation is skipped for TF-M specific Partitions (PID < TFM_PID_BASE). |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 102 | """ |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 103 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 104 | service_list = manifest.get('services', []) |
| 105 | irq_list = manifest.get('irqs', []) |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 106 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 107 | # "psa_framework_version" validation |
| 108 | if manifest['psa_framework_version'] not in [1.0, 1.1]: |
| 109 | raise Exception('Invalid psa_framework_version of {}'.format(manifest['name'])) |
| 110 | |
Chris Brand | f1d6b6f | 2022-07-22 11:49:01 -0700 | [diff] [blame] | 111 | # "type" validation |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 112 | if manifest['type'] not in ['PSA-ROT', 'APPLICATION-ROT']: |
| 113 | raise Exception('Invalid type of {}'.format(manifest['name'])) |
| 114 | |
Chris Brand | f1d6b6f | 2022-07-22 11:49:01 -0700 | [diff] [blame] | 115 | # "priority" validation |
| 116 | if manifest['priority'] not in ['HIGH', 'NORMAL', 'LOW']: |
| 117 | raise Exception('Invalid priority of {}'.format(manifest['name'])) |
| 118 | |
Chris Brand | c422cdd | 2022-07-21 13:32:47 -0700 | [diff] [blame] | 119 | if 'ns_agent' not in manifest: |
| 120 | manifest['ns_agent'] = False |
| 121 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 122 | # Every PSA Partition must have at least either a secure service or an IRQ |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 123 | if (pid == None or pid >= TFM_PID_BASE) \ |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 124 | and len(service_list) == 0 and len(irq_list) == 0: |
| 125 | raise Exception('{} must declare at least either a secure service or an IRQ!' |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 126 | .format(manifest['name'])) |
| 127 | |
| 128 | if manifest['psa_framework_version'] == 1.0: |
| 129 | # For 1.0 Partition, the model is IPC |
| 130 | manifest['model'] = 'IPC' |
| 131 | |
| 132 | # "model" validation: |
| 133 | model = manifest.get('model', None) |
| 134 | if model == None: |
| 135 | raise Exception('{} is missing the "model" attribute'.format(manifest['name'])) |
| 136 | |
| 137 | # Assign a unified 'entry' for templates |
| 138 | if model == 'IPC': |
| 139 | # entry_point is mandatory for IPC Partitions |
| 140 | if 'entry_point' not in manifest.keys(): |
| 141 | raise Exception('{} is missing the "entry_point" attribute'.format(manifest['name'])) |
| 142 | manifest['entry'] = manifest['entry_point'] |
| 143 | elif model == 'SFN': |
| 144 | if 'entry_init' in manifest.keys(): |
| 145 | manifest['entry'] = manifest['entry_init'] |
| 146 | else: |
| 147 | manifest['entry'] = 0 |
| 148 | else: |
| 149 | raise Exception('Invalid "model" of {}'.format(manifest['name'])) |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 150 | |
| 151 | # Service FF-M manifest validation |
| 152 | for service in service_list: |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 153 | if manifest['psa_framework_version'] == 1.0: |
| 154 | service['connection_based'] = True |
| 155 | elif 'connection_based' not in service: |
| 156 | raise Exception("'connection_based' is mandatory in FF-M 1.1 service!") |
| 157 | |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 158 | if 'version' not in service.keys(): |
| 159 | service['version'] = 1 |
| 160 | if 'version_policy' not in service.keys(): |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 161 | service['version_policy'] = 'STRICT' |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 162 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 163 | # SID duplication check |
| 164 | if service['sid'] in sid_list: |
| 165 | raise Exception('Service ID: {} has duplications!'.format(service['sid'])) |
| 166 | else: |
| 167 | sid_list.append(service['sid']) |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 168 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 169 | return manifest |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 170 | |
Xinyu Zhang | 7d0a5c8 | 2022-05-16 18:15:11 +0800 | [diff] [blame] | 171 | def check_circular_dependency(partitions, service_partition_map): |
| 172 | """ |
| 173 | This function detects if there is any circular partition dependency chain. |
| 174 | If a circular dependency is detected, the script exits with error. |
| 175 | |
| 176 | Inputs: |
| 177 | - partitions: dict of partition manifests |
| 178 | - service_partition_map: map between services and their owner Partitions |
| 179 | """ |
| 180 | |
| 181 | dependency_table = {} |
| 182 | for partition in partitions: |
| 183 | manifest = partition['manifest'] |
| 184 | dependencies = manifest['dependencies'].copy() \ |
| 185 | if 'dependencies' in manifest else [] |
| 186 | dependencies += manifest['weak_dependencies'].copy() \ |
| 187 | if 'weak_dependencies' in manifest else [] |
| 188 | dependency_table[manifest['name']] = { |
| 189 | 'dependencies': [service_partition_map[dependency] |
| 190 | for dependency in dependencies |
| 191 | if dependency in service_partition_map], |
| 192 | 'validated': False |
| 193 | } |
| 194 | |
| 195 | for partition in dependency_table.keys(): |
| 196 | validate_dependency_chain(partition, dependency_table, []) |
| 197 | |
| 198 | def validate_dependency_chain(partition, |
| 199 | dependency_table, |
| 200 | dependency_chain): |
| 201 | """ |
| 202 | Recursively validate if the given partition and its dependencies |
| 203 | have a circular dependency with the given dependency_chain. |
| 204 | Exit with error code once any circular is detected. |
| 205 | |
| 206 | Inputs: |
| 207 | - partition: next partition to be checked |
| 208 | - dependency_table: dict of partitions and their dependencies |
| 209 | - dependency_chain: list of dependencies in current chain |
| 210 | """ |
| 211 | |
| 212 | dependency_chain.append(partition) |
| 213 | if partition in dependency_chain[:-1]: |
| 214 | logging.error( |
| 215 | 'Circular dependency exists in chain: {}'.format( |
| 216 | ', '.join(dependency_chain))) |
| 217 | exit(1) |
| 218 | for dependency in dependency_table[partition]['dependencies']: |
| 219 | if dependency_table[dependency]['validated']: |
| 220 | continue |
| 221 | validate_dependency_chain(dependency, dependency_table, dependency_chain) |
| 222 | dependency_table[partition]['validated'] = True |
| 223 | |
chesun01 | 2345aa5 | 2023-04-18 17:31:53 +0800 | [diff] [blame] | 224 | def manifest_attribute_check(manifest, manifest_item): |
| 225 | """ |
| 226 | Check whether Non-FF-M compliant attributes are explicitly registered in manifest lists. |
| 227 | |
| 228 | Inputs: |
| 229 | - manifest: next manifest to be checked |
| 230 | - manifest_item: the manifest items in manifest lists |
| 231 | """ |
| 232 | allowed_attributes = ffm_manifest_attributes + manifest_item.get('non_ffm_attributes', []) |
| 233 | for keyword in manifest.keys(): |
| 234 | if keyword not in allowed_attributes: |
| 235 | logging.error('The Non-FFM attribute {} is used by {} without registration.'.format(keyword, manifest['name'])) |
| 236 | exit(1) |
| 237 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 238 | def process_partition_manifests(manifest_lists, configs): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 239 | """ |
Xinyu Zhang | 7d0a5c8 | 2022-05-16 18:15:11 +0800 | [diff] [blame] | 240 | Parse the input manifest lists, check if manifest settings are valid, |
Chris Brand | f1d6b6f | 2022-07-22 11:49:01 -0700 | [diff] [blame] | 241 | generate the data base for generated files |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 242 | and generate manifest header files. |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 243 | |
| 244 | Parameters |
| 245 | ---------- |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 246 | manifest_lists: |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 247 | A list of Secure Partition manifest lists |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 248 | |
| 249 | Returns |
| 250 | ------- |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 251 | The manifest data base. |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 252 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 253 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 254 | context = {} |
| 255 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 256 | partition_list = [] |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 257 | all_manifests = [] |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 258 | pid_list = [] |
| 259 | no_pid_manifest_idx = [] |
Xinyu Zhang | 7d0a5c8 | 2022-05-16 18:15:11 +0800 | [diff] [blame] | 260 | service_partition_map = {} |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 261 | partition_statistics = { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 262 | 'connection_based_srv_num': 0, |
Kevin Peng | 47c26ec | 2022-03-11 11:49:52 +0800 | [diff] [blame] | 263 | 'ipc_partitions': [], |
Sherry Zhang | 2ed48fd | 2022-09-06 11:33:22 +0800 | [diff] [blame] | 264 | 'mmio_region_num': 0, |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 265 | 'flih_num': 0, |
| 266 | 'slih_num': 0 |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 267 | } |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 268 | config_impl = { |
| 269 | 'CONFIG_TFM_SPM_BACKEND_SFN' : '0', |
| 270 | 'CONFIG_TFM_SPM_BACKEND_IPC' : '0', |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 271 | 'CONFIG_TFM_CONNECTION_BASED_SERVICE_API' : '0', |
Sherry Zhang | 2ed48fd | 2022-09-06 11:33:22 +0800 | [diff] [blame] | 272 | 'CONFIG_TFM_MMIO_REGION_ENABLE' : '0', |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 273 | 'CONFIG_TFM_FLIH_API' : '0', |
| 274 | 'CONFIG_TFM_SLIH_API' : '0' |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 275 | } |
chesun01 | 22abf0a | 2023-10-17 13:42:16 +0800 | [diff] [blame^] | 276 | priority_map = { |
| 277 | 'LOWEST' : '00', |
| 278 | 'LOW' : '01', |
| 279 | 'NORMAL' : '02', |
| 280 | 'HIGH' : '03', |
| 281 | 'HIGHEST' : '04' |
| 282 | } |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 283 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 284 | isolation_level = int(configs['TFM_ISOLATION_LEVEL'], base = 10) |
| 285 | backend = configs['CONFIG_TFM_SPM_BACKEND'] |
| 286 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 287 | # Get all the manifests information as a dictionary |
| 288 | for i, item in enumerate(manifest_lists): |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 289 | if not os.path.isfile(item): |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 290 | logging.error('Manifest list item [{}] must be a file'.format(i)) |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 291 | exit(1) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 292 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 293 | # The manifest list file generated by configure_file() |
| 294 | with open(item) as manifest_list_yaml_file: |
| 295 | manifest_dic = yaml.safe_load(manifest_list_yaml_file)['manifest_list'] |
| 296 | for dict in manifest_dic: |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 297 | # Replace environment variables in the manifest path and convert to absolute path. |
| 298 | # If it's already abspath, the path will not be changed. |
| 299 | manifest_path = os.path.join(os.path.dirname(item), # path of manifest list |
| 300 | os.path.expandvars(dict['manifest']))\ |
| 301 | .replace('\\', '/') |
| 302 | dict['manifest'] = manifest_path |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 303 | all_manifests.append(dict) |
| 304 | |
Chris Brand | 4b69fcf | 2022-06-06 09:37:49 -0700 | [diff] [blame] | 305 | logging.info("------------ Display partition configuration - start ------------") |
| 306 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 307 | # Parse the manifests |
| 308 | for i, manifest_item in enumerate(all_manifests): |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 309 | valid_enabled_conditions = ['1', 'on', 'true', 'enabled'] |
| 310 | valid_disabled_conditions = ['0', 'off', 'false', 'disabled', ''] |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 311 | is_enabled = '' |
| 312 | |
| 313 | if 'conditional' in manifest_item.keys(): |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 314 | if manifest_item['conditional'] not in configs.keys(): |
| 315 | logging.error('Configuration "{}" is not defined!'.format(manifest_item['conditional'])) |
| 316 | exit(1) |
| 317 | is_enabled = configs[manifest_item['conditional']].lower() |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 318 | else: |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 319 | # Partitions without 'conditional' is always on |
| 320 | is_enabled = '1' |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 321 | |
| 322 | if is_enabled in valid_disabled_conditions: |
BohdanHunko | 6cea95d | 2022-08-16 15:33:51 +0300 | [diff] [blame] | 323 | logging.info(" {:40s} OFF".format(manifest_item['description'])) |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 324 | continue |
Chris Brand | 4b69fcf | 2022-06-06 09:37:49 -0700 | [diff] [blame] | 325 | elif is_enabled in valid_enabled_conditions: |
BohdanHunko | 6cea95d | 2022-08-16 15:33:51 +0300 | [diff] [blame] | 326 | logging.info(" {:40s} ON".format(manifest_item['description'])) |
Chris Brand | 4b69fcf | 2022-06-06 09:37:49 -0700 | [diff] [blame] | 327 | else: |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 328 | raise Exception('Invalid "conditional" attribute: "{}" for {}. ' |
| 329 | 'Please set to one of {} or {}, case-insensitive.'\ |
| 330 | .format(manifest_item['conditional'], |
BohdanHunko | 6cea95d | 2022-08-16 15:33:51 +0300 | [diff] [blame] | 331 | manifest_item['description'], |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 332 | valid_enabled_conditions, valid_disabled_conditions)) |
| 333 | |
Xinyu Zhang | c46ee1f | 2021-04-01 10:10:43 +0800 | [diff] [blame] | 334 | # Check if partition ID is manually set |
| 335 | if 'pid' not in manifest_item.keys(): |
| 336 | no_pid_manifest_idx.append(i) |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 337 | pid = None |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 338 | else: |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 339 | pid = manifest_item['pid'] |
| 340 | |
| 341 | # Check if partition ID is duplicated |
| 342 | if pid in pid_list: |
Antonio de Angelis | baa2764 | 2022-05-25 11:07:12 +0100 | [diff] [blame] | 343 | raise Exception('PID No. {} has already been used!'.format(pid)) |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 344 | else: |
| 345 | pid_list.append(pid) |
Xinyu Zhang | 19504a5 | 2021-03-31 16:26:20 +0800 | [diff] [blame] | 346 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 347 | manifest_path = manifest_item['manifest'] |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 348 | with open(manifest_path) as manifest_file: |
Kevin Peng | ec0e5ce | 2022-02-17 13:48:51 +0800 | [diff] [blame] | 349 | manifest = yaml.safe_load(manifest_file) |
chesun01 | 22abf0a | 2023-10-17 13:42:16 +0800 | [diff] [blame^] | 350 | # Check manifest attribute validity |
chesun01 | 2345aa5 | 2023-04-18 17:31:53 +0800 | [diff] [blame] | 351 | manifest_attribute_check(manifest, manifest_item) |
| 352 | |
Kevin Peng | ec0e5ce | 2022-02-17 13:48:51 +0800 | [diff] [blame] | 353 | if manifest.get('model', None) == 'dual': |
| 354 | # If a Partition supports both models, it can set the "model" to "backend". |
| 355 | # The actual model used follows the backend being used. |
| 356 | manifest['model'] = backend |
| 357 | manifest = manifest_validation(manifest, pid) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 358 | |
chesun01 | 22abf0a | 2023-10-17 13:42:16 +0800 | [diff] [blame^] | 359 | # Priority mapping |
| 360 | numbered_priority = priority_map[manifest['priority']] |
| 361 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 362 | if pid == None or pid >= TFM_PID_BASE: |
Kevin Peng | d08f3ba | 2021-11-18 15:18:56 +0800 | [diff] [blame] | 363 | # Count the number of IPC/SFN partitions |
Kevin Peng | 47c26ec | 2022-03-11 11:49:52 +0800 | [diff] [blame] | 364 | if manifest['model'] == 'IPC': |
| 365 | partition_statistics['ipc_partitions'].append(manifest['name']) |
Mingyang Sun | eab7eae | 2021-09-30 13:06:52 +0800 | [diff] [blame] | 366 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 367 | # Set initial value to -1 to make (srv_idx + 1) reflect the correct |
| 368 | # number (0) when there are no services. |
| 369 | srv_idx = -1 |
| 370 | for srv_idx, service in enumerate(manifest.get('services', [])): |
Xinyu Zhang | 7d0a5c8 | 2022-05-16 18:15:11 +0800 | [diff] [blame] | 371 | service_partition_map[service['name']] = manifest['name'] |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 372 | if manifest['model'] == 'IPC': |
| 373 | # Assign signal value, the first 4 bits are reserved by FF-M |
| 374 | service['signal_value'] = (1 << (srv_idx + 4)) |
| 375 | else: |
| 376 | # Signals of SFN Partitions are SPM internal only, does not |
| 377 | # need to reserve 4 bits. |
| 378 | service['signal_value'] = (1 << srv_idx) |
| 379 | if service['connection_based']: |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 380 | partition_statistics['connection_based_srv_num'] += 1 |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 381 | logging.debug('{} has {} services'.format(manifest['name'], srv_idx +1)) |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 382 | |
Sherry Zhang | 2ed48fd | 2022-09-06 11:33:22 +0800 | [diff] [blame] | 383 | # Calculate the number of mmio region |
| 384 | mmio_region_list = manifest.get('mmio_regions', []) |
| 385 | partition_statistics['mmio_region_num'] += len(mmio_region_list) |
| 386 | |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 387 | # Set initial value to -1 to make (irq + 1) reflect the correct |
| 388 | # number (0) when there are no irqs. |
| 389 | irq_idx = -1 |
| 390 | for irq_idx, irq in enumerate(manifest.get('irqs', [])): |
| 391 | # Assign signal value, from the most significant bit |
| 392 | irq['signal_value'] = (1 << (31 - irq_idx)) |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 393 | if irq.get('handling', None) == 'FLIH': |
| 394 | partition_statistics['flih_num'] += 1 |
| 395 | else: |
| 396 | partition_statistics['slih_num'] += 1 |
Kevin Peng | 5c3fee7 | 2022-02-16 22:25:22 +0800 | [diff] [blame] | 397 | logging.debug('{} has {} IRQS'.format(manifest['name'], irq_idx +1)) |
| 398 | |
| 399 | if ((srv_idx + 1) + (irq_idx + 1)) > 28: |
| 400 | raise Exception('Total number of Services and IRQs of {} exceeds the limit (28)' |
| 401 | .format(manifest['name'])) |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 402 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 403 | manifest_out_basename = os.path.splitext(os.path.basename(manifest_path))[0] |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 404 | |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 405 | if 'output_path' in manifest_item: |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 406 | output_path = os.path.expandvars(manifest_item['output_path']) |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 407 | else: |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 408 | output_path = '' |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 409 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 410 | manifest_head_file = os.path.join(OUT_DIR, output_path, 'psa_manifest', |
| 411 | '{}.h'.format(manifest_out_basename))\ |
| 412 | .replace('\\', '/') |
| 413 | intermedia_file = os.path.join(OUT_DIR, output_path, 'auto_generated', |
| 414 | 'intermedia_{}.c'.format(manifest_out_basename))\ |
| 415 | .replace('\\', '/') |
| 416 | load_info_file = os.path.join(OUT_DIR, output_path, 'auto_generated', |
| 417 | 'load_info_{}.c'.format(manifest_out_basename))\ |
| 418 | .replace('\\', '/') |
Jianliang Shen | 785ed5e | 2022-02-08 14:16:06 +0800 | [diff] [blame] | 419 | output_dir = os.path.join(OUT_DIR, output_path).replace('\\', '/') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 420 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 421 | partition_list.append({'manifest': manifest, 'attr': manifest_item, |
| 422 | 'manifest_out_basename': manifest_out_basename, |
| 423 | 'header_file': manifest_head_file, |
| 424 | 'intermedia_file': intermedia_file, |
Jianliang Shen | 785ed5e | 2022-02-08 14:16:06 +0800 | [diff] [blame] | 425 | 'loadinfo_file': load_info_file, |
chesun01 | 22abf0a | 2023-10-17 13:42:16 +0800 | [diff] [blame^] | 426 | 'output_dir': output_dir, |
| 427 | 'numbered_priority': numbered_priority}) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 428 | |
Chris Brand | 4b69fcf | 2022-06-06 09:37:49 -0700 | [diff] [blame] | 429 | logging.info("------------ Display partition configuration - end ------------") |
| 430 | |
Xinyu Zhang | 7d0a5c8 | 2022-05-16 18:15:11 +0800 | [diff] [blame] | 431 | check_circular_dependency(partition_list, service_partition_map) |
| 432 | |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 433 | # Automatically assign PIDs for partitions without 'pid' attribute |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 434 | pid = max(pid_list, default = TFM_PID_BASE - 1) |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 435 | for idx in no_pid_manifest_idx: |
Kevin Peng | c424eec | 2021-06-25 17:26:11 +0800 | [diff] [blame] | 436 | pid += 1 |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 437 | all_manifests[idx]['pid'] = pid |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 438 | pid_list.append(pid) |
| 439 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 440 | # Set up configurations |
Kevin Peng | 76c0c16 | 2022-02-09 22:49:06 +0800 | [diff] [blame] | 441 | if backend == 'SFN': |
Kevin Peng | 47c26ec | 2022-03-11 11:49:52 +0800 | [diff] [blame] | 442 | if len(partition_statistics['ipc_partitions']) > 0: |
| 443 | logging.error('SFN backend does not support IPC Partitions:') |
| 444 | logging.error(partition_statistics['ipc_partitions']) |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 445 | exit(1) |
Kevin Peng | 76c0c16 | 2022-02-09 22:49:06 +0800 | [diff] [blame] | 446 | |
| 447 | if isolation_level > 1: |
| 448 | logging.error('SFN backend does not support high isolation levels.') |
| 449 | exit(1) |
| 450 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 451 | config_impl['CONFIG_TFM_SPM_BACKEND_SFN'] = '1' |
Kevin Peng | 76c0c16 | 2022-02-09 22:49:06 +0800 | [diff] [blame] | 452 | elif backend == 'IPC': |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 453 | config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] = '1' |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 454 | |
| 455 | if partition_statistics['connection_based_srv_num'] > 0: |
| 456 | config_impl['CONFIG_TFM_CONNECTION_BASED_SERVICE_API'] = 1 |
| 457 | |
Sherry Zhang | 2ed48fd | 2022-09-06 11:33:22 +0800 | [diff] [blame] | 458 | if partition_statistics['mmio_region_num'] > 0: |
| 459 | config_impl['CONFIG_TFM_MMIO_REGION_ENABLE'] = 1 |
| 460 | |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 461 | if partition_statistics['flih_num'] > 0: |
| 462 | config_impl['CONFIG_TFM_FLIH_API'] = 1 |
Joakim Andersson | eec5cd3 | 2022-06-10 12:02:07 +0200 | [diff] [blame] | 463 | if partition_statistics['slih_num'] > 0: |
Mingyang Sun | ed5fe7b | 2022-02-10 17:33:21 +0800 | [diff] [blame] | 464 | config_impl['CONFIG_TFM_SLIH_API'] = 1 |
| 465 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 466 | context['partitions'] = partition_list |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 467 | context['config_impl'] = config_impl |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 468 | context['stateless_services'] = process_stateless_services(partition_list) |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 469 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 470 | return context |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 471 | |
| 472 | def gen_per_partition_files(context): |
| 473 | """ |
| 474 | Generate per-partition files |
| 475 | |
| 476 | Parameters |
| 477 | ---------- |
| 478 | context: |
| 479 | context contains partition infos |
| 480 | """ |
| 481 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 482 | partition_context = {} |
Sherry Zhang | f58f2bd | 2022-01-10 17:21:11 +0800 | [diff] [blame] | 483 | partition_context['utilities'] = context['utilities'] |
Sherry Zhang | f865c4e | 2022-03-23 13:53:38 +0800 | [diff] [blame] | 484 | partition_context['config_impl'] = context['config_impl'] |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 485 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 486 | manifesttemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/manifestfilename.template')) |
| 487 | memorytemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/partition_intermedia.template')) |
| 488 | infotemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/partition_load_info.template')) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 489 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 490 | logging.info ("Start to generate partition files:") |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 491 | |
| 492 | for one_partition in context['partitions']: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 493 | partition_context['manifest'] = one_partition['manifest'] |
| 494 | partition_context['attr'] = one_partition['attr'] |
| 495 | partition_context['manifest_out_basename'] = one_partition['manifest_out_basename'] |
chesun01 | 22abf0a | 2023-10-17 13:42:16 +0800 | [diff] [blame^] | 496 | partition_context['numbered_priority'] = one_partition['numbered_priority'] |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 497 | |
BohdanHunko | 6cea95d | 2022-08-16 15:33:51 +0300 | [diff] [blame] | 498 | logging.info ('Generating {} in {}'.format(one_partition['attr']['description'], |
Jianliang Shen | 785ed5e | 2022-02-08 14:16:06 +0800 | [diff] [blame] | 499 | one_partition['output_dir'])) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 500 | outfile_path = os.path.dirname(one_partition['header_file']) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 501 | if not os.path.exists(outfile_path): |
| 502 | os.makedirs(outfile_path) |
| 503 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 504 | headerfile = io.open(one_partition['header_file'], 'w', newline=None) |
| 505 | headerfile.write(manifesttemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 506 | headerfile.close() |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 507 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 508 | intermediafile_path = os.path.dirname(one_partition['intermedia_file']) |
Mingyang Sun | d20999f | 2020-10-15 14:53:12 +0800 | [diff] [blame] | 509 | if not os.path.exists(intermediafile_path): |
| 510 | os.makedirs(intermediafile_path) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 511 | intermediafile = io.open(one_partition['intermedia_file'], 'w', newline=None) |
| 512 | intermediafile.write(memorytemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 513 | intermediafile.close() |
Mingyang Sun | d20999f | 2020-10-15 14:53:12 +0800 | [diff] [blame] | 514 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 515 | infofile_path = os.path.dirname(one_partition['loadinfo_file']) |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 516 | if not os.path.exists(infofile_path): |
| 517 | os.makedirs(infofile_path) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 518 | infooutfile = io.open(one_partition['loadinfo_file'], 'w', newline=None) |
| 519 | infooutfile.write(infotemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 520 | infooutfile.close() |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 521 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 522 | logging.info ("Per-partition files done:") |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 523 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 524 | def gen_summary_files(context, gen_file_lists): |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 525 | """ |
| 526 | Generate files according to the gen_file_list |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 527 | |
| 528 | Parameters |
| 529 | ---------- |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 530 | gen_file_lists: |
| 531 | The lists of files to generate |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 532 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 533 | file_list = [] |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 534 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 535 | for f in gen_file_lists: |
| 536 | with open(f) as file_list_yaml_file: |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 537 | file_list_yaml = yaml.safe_load(file_list_yaml_file) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 538 | file_list.extend(file_list_yaml['file_list']) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 539 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 540 | for file in file_list: |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 541 | # Replace environment variables in the output filepath |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 542 | manifest_out_file = os.path.expandvars(file['output']) |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 543 | # Replace environment variables in the template filepath |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 544 | templatefile_name = os.path.expandvars(file['template']) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 545 | |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 546 | manifest_out_file = os.path.join(OUT_DIR, manifest_out_file) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 547 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 548 | outfile_path = os.path.dirname(manifest_out_file) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 549 | if not os.path.exists(outfile_path): |
| 550 | os.makedirs(outfile_path) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 551 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 552 | template = ENV.get_template(templatefile_name) |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 553 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 554 | outfile = io.open(manifest_out_file, 'w', newline=None) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 555 | outfile.write(template.render(context)) |
| 556 | outfile.close() |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 557 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 558 | def process_stateless_services(partitions): |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 559 | """ |
| 560 | This function collects all stateless services together, and allocates |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 561 | stateless handles for them. |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 562 | Valid stateless handle in service will be converted to an index. If the |
| 563 | stateless handle is set as "auto", or not set, framework will allocate a |
| 564 | valid index for the service. |
| 565 | Framework puts each service into a reordered stateless service list at |
| 566 | position of "index". Other unused positions are left None. |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 567 | |
| 568 | Keep the variable names start with upper case 'STATIC_HANDLE_' the same |
| 569 | as the preprocessors in C sources. This could easier the upcomping |
| 570 | modification when developer searches these definitions for modification. |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 571 | """ |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 572 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 573 | collected_stateless_services = [] |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 574 | STATIC_HANDLE_NUM_LIMIT = 32 |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 575 | |
| 576 | # Collect all stateless services first. |
| 577 | for partition in partitions: |
| 578 | # Skip the FF-M 1.0 partitions |
| 579 | if partition['manifest']['psa_framework_version'] < 1.1: |
| 580 | continue |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 581 | |
| 582 | service_list = partition['manifest'].get('services', []) |
| 583 | |
| 584 | for service in service_list: |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 585 | if service['connection_based'] is False: |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 586 | collected_stateless_services.append(service) |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 587 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 588 | if len(collected_stateless_services) == 0: |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 589 | return [] |
| 590 | |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 591 | if len(collected_stateless_services) > STATIC_HANDLE_NUM_LIMIT: |
| 592 | raise Exception('Stateless service numbers range exceed {number}.'.format(number=STATIC_HANDLE_NUM_LIMIT)) |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 593 | |
| 594 | """ |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 595 | Allocate an empty stateless service list to store services. |
| 596 | Use "handle - 1" as the index for service, since handle value starts from |
| 597 | 1 and list index starts from 0. |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 598 | """ |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 599 | reordered_stateless_services = [None] * STATIC_HANDLE_NUM_LIMIT |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 600 | auto_alloc_services = [] |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 601 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 602 | for service in collected_stateless_services: |
| 603 | # If not set, it is "auto" by default |
| 604 | if 'stateless_handle' not in service: |
| 605 | auto_alloc_services.append(service) |
| 606 | continue |
| 607 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 608 | service_handle = service['stateless_handle'] |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 609 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 610 | # Fill in service list with specified stateless handle, otherwise skip |
| 611 | if isinstance(service_handle, int): |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 612 | if service_handle < 1 or service_handle > STATIC_HANDLE_NUM_LIMIT: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 613 | raise Exception('Invalid stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 614 | # Convert handle index to reordered service list index |
| 615 | service_handle = service_handle - 1 |
| 616 | |
| 617 | if reordered_stateless_services[service_handle] is not None: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 618 | raise Exception('Duplicated stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 619 | reordered_stateless_services[service_handle] = service |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 620 | elif service_handle == 'auto': |
| 621 | auto_alloc_services.append(service) |
| 622 | else: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 623 | raise Exception('Invalid stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 624 | |
Xinyu Zhang | b2d4ed4 | 2023-02-14 11:43:16 +0800 | [diff] [blame] | 625 | STATIC_HANDLE_IDX_BIT_WIDTH = 5 |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 626 | STATIC_HANDLE_IDX_MASK = (1 << STATIC_HANDLE_IDX_BIT_WIDTH) - 1 |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 627 | STATIC_HANDLE_INDICATOR_OFFSET = 30 |
| 628 | STATIC_HANDLE_VER_OFFSET = 8 |
| 629 | STATIC_HANDLE_VER_BIT_WIDTH = 8 |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 630 | STATIC_HANDLE_VER_MASK = (1 << STATIC_HANDLE_VER_BIT_WIDTH) - 1 |
| 631 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 632 | # Auto-allocate stateless handle and encode the stateless handle |
Ken Liu | 5094838 | 2022-10-27 12:45:53 +0800 | [diff] [blame] | 633 | for i in range(0, STATIC_HANDLE_NUM_LIMIT): |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 634 | service = reordered_stateless_services[i] |
| 635 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 636 | if service == None and len(auto_alloc_services) > 0: |
| 637 | service = auto_alloc_services.pop(0) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 638 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 639 | """ |
| 640 | Encode stateless flag and version into stateless handle |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 641 | """ |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 642 | stateless_handle_value = 0 |
| 643 | if service != None: |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 644 | stateless_index = (i & STATIC_HANDLE_IDX_MASK) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 645 | stateless_handle_value |= stateless_index |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 646 | stateless_handle_value |= (1 << STATIC_HANDLE_INDICATOR_OFFSET) |
| 647 | stateless_version = (service['version'] & STATIC_HANDLE_VER_MASK) << STATIC_HANDLE_VER_OFFSET |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 648 | stateless_handle_value |= stateless_version |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 649 | service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 650 | service['stateless_handle_index'] = stateless_index |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 651 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 652 | reordered_stateless_services[i] = service |
| 653 | |
| 654 | return reordered_stateless_services |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 655 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 656 | def parse_args(): |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 657 | parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list', |
| 658 | epilog='Note that environment variables in template files will be replaced with their values') |
| 659 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 660 | parser.add_argument('-o', '--outdir' |
| 661 | , dest='outdir' |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 662 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 663 | , metavar='out_dir' |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 664 | , help='The root directory for generated files') |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 665 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 666 | parser.add_argument('-m', '--manifest-lists' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 667 | , nargs='+' |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 668 | , dest='manifest_lists' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 669 | , required=True |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 670 | , metavar='manifest list' |
| 671 | , help='A list of Secure Partition manifest lists and their original paths.\n\ |
| 672 | The manifest lists might be processed by CMake and\n\ |
| 673 | the path might be different to the original one\n\ |
| 674 | The format must be [list A, orignal path A, list B, orignal path B, ...]') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 675 | |
| 676 | parser.add_argument('-f', '--file-list' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 677 | , nargs='+' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 678 | , dest='gen_file_args' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 679 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 680 | , metavar='file-list' |
Chris Brand | f1d6b6f | 2022-07-22 11:49:01 -0700 | [diff] [blame] | 681 | , help='These files describe the file list to generate') |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 682 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 683 | parser.add_argument('-c', '--config-files' |
| 684 | , nargs='+' |
| 685 | , dest='config_files' |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 686 | , required=True |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 687 | , metavar='config-files' |
| 688 | , help='A header file contains build configurations') |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 689 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 690 | parser.add_argument('-q', '--quiet' |
| 691 | , dest='quiet' |
| 692 | , required=False |
| 693 | , default=False |
| 694 | , action='store_true' |
| 695 | , help='Reduce log messages') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 696 | |
| 697 | args = parser.parse_args() |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 698 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 699 | return args |
| 700 | |
| 701 | ENV = Environment( |
| 702 | loader = TemplateLoader(), |
| 703 | autoescape = select_autoescape(['html', 'xml']), |
| 704 | lstrip_blocks = True, |
| 705 | trim_blocks = True, |
| 706 | keep_trailing_newline = True |
| 707 | ) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 708 | |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 709 | def main(): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 710 | """ |
| 711 | The entry point of the script. |
| 712 | |
| 713 | Generates the output files based on the templates and the manifests. |
| 714 | """ |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 715 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 716 | global OUT_DIR |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 717 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 718 | args = parse_args() |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 719 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 720 | logging.basicConfig(format='%(message)s' |
| 721 | , level=logging.WARNING if args.quiet else logging.INFO) |
| 722 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 723 | OUT_DIR = os.path.abspath(args.outdir) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 724 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 725 | manifest_lists = [os.path.abspath(x) for x in args.manifest_lists] |
| 726 | gen_file_lists = [os.path.abspath(x) for x in args.gen_file_args] |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 727 | |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 728 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 729 | Relative path to TF-M root folder is supported in the manifests |
| 730 | and default value of manifest list and generated file list are relative to TF-M root folder as well, |
| 731 | so first change directory to TF-M root folder. |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 732 | By doing this, the script can be executed anywhere |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 733 | The script is located in <TF-M root folder>/tools, so sys.path[0]<location of the script>/.. is TF-M root folder. |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 734 | """ |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 735 | os.chdir(os.path.join(sys.path[0], '..')) |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 736 | |
Kevin Peng | 76c0c16 | 2022-02-09 22:49:06 +0800 | [diff] [blame] | 737 | context = process_partition_manifests(manifest_lists, |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 738 | parse_configurations(args.config_files)) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 739 | |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 740 | utilities = {} |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 741 | utilities['donotedit_warning'] = donotedit_warning |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 742 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 743 | context['utilities'] = utilities |
Mingyang Sun | eab7eae | 2021-09-30 13:06:52 +0800 | [diff] [blame] | 744 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 745 | gen_per_partition_files(context) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 746 | gen_summary_files(context, gen_file_lists) |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 747 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 748 | if __name__ == '__main__': |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 749 | main() |