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. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import os |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 9 | import io |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 10 | import re |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 11 | import sys |
| 12 | import argparse |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 13 | import logging |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 14 | from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 15 | |
| 16 | try: |
| 17 | import yaml |
| 18 | except ImportError as e: |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 19 | logging.error (str(e) + " To install it, type:") |
| 20 | logging.error ("pip install PyYAML") |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 21 | exit(1) |
| 22 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 23 | donotedit_warning = \ |
Sherry Zhang | f58f2bd | 2022-01-10 17:21:11 +0800 | [diff] [blame] | 24 | ' WARNING: This is an auto-generated file. Do not edit! ' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 25 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 26 | TFM_ROOT_DIR = os.path.join(sys.path[0], '..') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 27 | OUT_DIR = None # The root directory that files are generated to |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 28 | |
Sherry Zhang | 87e6d2e | 2021-12-27 14:48:59 +0800 | [diff] [blame] | 29 | # 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] | 30 | TFM_PID_BASE = 256 |
| 31 | |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 32 | # variable for checking for duplicated sid |
| 33 | sid_list = [] |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 34 | class TemplateLoader(BaseLoader): |
| 35 | """ |
| 36 | Template loader class. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 37 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 38 | An instance of this class is passed to the template engine. It is |
| 39 | responsible for reading the template file |
| 40 | """ |
| 41 | def __init__(self): |
| 42 | pass |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 43 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 44 | def get_source(self, environment, template): |
| 45 | """ |
| 46 | This function reads the template files. |
| 47 | For detailed documentation see: |
| 48 | http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source |
| 49 | |
| 50 | Please note that this function always return 'false' as 'uptodate' |
| 51 | value, so the output file will always be generated. |
| 52 | """ |
| 53 | if not os.path.isfile(template): |
| 54 | raise TemplateNotFound(template) |
| 55 | with open(template) as f: |
| 56 | source = f.read() |
| 57 | return source, template, False |
| 58 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 59 | def get_single_macro_def_from_file(file_name, macro_name): |
| 60 | """ |
| 61 | This function parses the given file_name to get the definition of the given |
| 62 | C Macro (macro_name). |
| 63 | |
| 64 | It assumes that the target Macro has no multiple definitions in different |
| 65 | build configurations. |
| 66 | |
| 67 | It supports Macros defined in multi-line, for example: |
| 68 | #define SOME_MACRO \ |
| 69 | the_macro_value |
| 70 | |
| 71 | Inputs: |
| 72 | - file_name: the file to get the Macro from |
| 73 | - macro_name: the name of the Macro to get |
| 74 | Returns: |
| 75 | - The Macro definition with '()' stripped, or Exception if not found |
| 76 | """ |
| 77 | |
| 78 | with open(file_name, 'r') as f: |
| 79 | pattern = re.compile(r'#define\s+{}[\\\s]+.*'.format(macro_name)) |
| 80 | result = pattern.findall(f.read()) |
| 81 | |
| 82 | if len(result) != 1: |
| 83 | raise Exception('{} not defined or has multiple definitions'.format(macro_name)) |
| 84 | |
| 85 | macro_def = result[0].split()[-1].strip('()') |
| 86 | |
| 87 | return macro_def |
| 88 | |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 89 | def manifest_validation(partition_manifest, pid): |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 90 | """ |
| 91 | This function validates FF-M compliance for partition manifest, and sets |
| 92 | default values for optional attributes. |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 93 | 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] | 94 | More validation items will be added. |
| 95 | """ |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 96 | |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 97 | service_list = partition_manifest.get('services', []) |
| 98 | irq_list = partition_manifest.get('irqs', []) |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 99 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 100 | if (pid == None or pid >= TFM_PID_BASE) \ |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 101 | and len(service_list) == 0 and len(irq_list) == 0: |
| 102 | raise Exception('{} must declare at least either a secure service or an IRQ!' |
| 103 | .format(partition_manifest['name'])) |
| 104 | |
| 105 | # Service FF-M manifest validation |
| 106 | for service in service_list: |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 107 | if 'version' not in service.keys(): |
| 108 | service['version'] = 1 |
| 109 | if 'version_policy' not in service.keys(): |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 110 | service['version_policy'] = 'STRICT' |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 111 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 112 | # SID duplication check |
| 113 | if service['sid'] in sid_list: |
| 114 | raise Exception('Service ID: {} has duplications!'.format(service['sid'])) |
| 115 | else: |
| 116 | sid_list.append(service['sid']) |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 117 | |
Mingyang Sun | 294ce2e | 2021-06-11 11:58:24 +0800 | [diff] [blame] | 118 | return partition_manifest |
| 119 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 120 | def process_partition_manifests(manifest_lists, isolation_level): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 121 | """ |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 122 | Parse the input manifest lists, generate the data base for genereated files |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 123 | and generate manifest header files. |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 124 | |
| 125 | Parameters |
| 126 | ---------- |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 127 | manifest_lists: |
| 128 | A list of Secure Partition manifest lists and their original paths. |
| 129 | The manifest lists might be processed by CMake and the paths might be |
| 130 | different to the original ones. Original paths are needed to handle |
| 131 | relative paths in the lists. |
| 132 | The format must be [list A, orignal path A, list B, orignal path B, ...] |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 133 | |
| 134 | Returns |
| 135 | ------- |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 136 | The manifest data base. |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 137 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 138 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 139 | context = {} |
| 140 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 141 | partition_list = [] |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 142 | all_manifests = [] |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 143 | pid_list = [] |
| 144 | no_pid_manifest_idx = [] |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 145 | partition_statistics = { |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 146 | 'connection_based_srv_num': 0, |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 147 | 'ipc_partition_num': 0, |
| 148 | 'sfn_partition_num': 0 |
| 149 | } |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 150 | config_impl = { |
| 151 | 'CONFIG_TFM_SPM_BACKEND_SFN' : '0', |
| 152 | 'CONFIG_TFM_SPM_BACKEND_IPC' : '0', |
| 153 | 'CONFIG_TFM_PSA_API_SFN_CALL' : '0', |
| 154 | 'CONFIG_TFM_PSA_API_CROSS_CALL' : '0', |
| 155 | 'CONFIG_TFM_PSA_API_SUPERVISOR_CALL' : '0', |
| 156 | 'CONFIG_TFM_CONNECTION_BASED_SERVICE_API' : '0' |
| 157 | } |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 158 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 159 | # Get all the manifests information as a dictionary |
| 160 | for i, item in enumerate(manifest_lists): |
| 161 | if i % 2 == 0 and not os.path.isfile(item): |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 162 | logging.error('Manifest list item [{}] must be a file'.format(i)) |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 163 | exit(1) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 164 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 165 | if i % 2 == 1: |
| 166 | if not os.path.isdir(item): |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 167 | logging.error('Manifest list item [{}] must be a directory'.format(i)) |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 168 | exit(1) |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 169 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 170 | # Skip original manifest paths |
| 171 | continue |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 172 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 173 | # The manifest list file generated by configure_file() |
| 174 | with open(item) as manifest_list_yaml_file: |
| 175 | manifest_dic = yaml.safe_load(manifest_list_yaml_file)['manifest_list'] |
| 176 | for dict in manifest_dic: |
| 177 | # Add original path of manifest list. |
| 178 | # The validation will be done in the next loop. |
| 179 | dict['list_path'] = manifest_lists[i + 1] |
| 180 | all_manifests.append(dict) |
| 181 | |
| 182 | # Parse the manifests |
| 183 | for i, manifest_item in enumerate(all_manifests): |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 184 | valid_enabled_conditions = ['on', 'true', 'enabled'] |
Kevin Peng | 50f413c | 2021-11-12 10:31:45 +0800 | [diff] [blame] | 185 | valid_disabled_conditions = ['off', 'false', 'disabled', ''] |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 186 | is_enabled = '' |
| 187 | |
| 188 | if 'conditional' in manifest_item.keys(): |
Kevin Peng | 50f413c | 2021-11-12 10:31:45 +0800 | [diff] [blame] | 189 | is_enabled = str(manifest_item['conditional']).lower() |
Kevin Peng | 68d9a3a | 2021-10-18 11:39:54 +0800 | [diff] [blame] | 190 | else: |
| 191 | # Partitions without 'conditional' is alwasy on |
| 192 | is_enabled = 'on' |
| 193 | |
| 194 | if is_enabled in valid_disabled_conditions: |
| 195 | continue |
| 196 | elif is_enabled not in valid_enabled_conditions: |
| 197 | raise Exception('Invalid "conditional" attribute: "{}" for {}. ' |
| 198 | 'Please set to one of {} or {}, case-insensitive.'\ |
| 199 | .format(manifest_item['conditional'], |
| 200 | manifest_item['name'], |
| 201 | valid_enabled_conditions, valid_disabled_conditions)) |
| 202 | |
Xinyu Zhang | c46ee1f | 2021-04-01 10:10:43 +0800 | [diff] [blame] | 203 | # Check if partition ID is manually set |
| 204 | if 'pid' not in manifest_item.keys(): |
| 205 | no_pid_manifest_idx.append(i) |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 206 | pid = None |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 207 | else: |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 208 | pid = manifest_item['pid'] |
| 209 | |
| 210 | # Check if partition ID is duplicated |
| 211 | if pid in pid_list: |
| 212 | raise Exception('PID No. {pid} has already been used!'.format(pid)) |
| 213 | else: |
| 214 | pid_list.append(pid) |
Xinyu Zhang | 19504a5 | 2021-03-31 16:26:20 +0800 | [diff] [blame] | 215 | |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 216 | # Replace environment variables in the manifest path |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 217 | manifest_path = os.path.expandvars(manifest_item['manifest']) |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 218 | # Convert to absolute path. If it's already abspath, the path will not be changed. |
| 219 | manifest_path = os.path.join(manifest_item['list_path'], manifest_path).replace('\\', '/') |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 220 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 221 | with open(manifest_path) as manifest_file: |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 222 | manifest = manifest_validation(yaml.safe_load(manifest_file), pid) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 223 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 224 | if pid == None or pid >= TFM_PID_BASE: |
Kevin Peng | d08f3ba | 2021-11-18 15:18:56 +0800 | [diff] [blame] | 225 | # Count the number of IPC/SFN partitions |
| 226 | if manifest['psa_framework_version'] == 1.1 and manifest['model'] == 'SFN': |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 227 | partition_statistics['sfn_partition_num'] += 1 |
Kevin Peng | d08f3ba | 2021-11-18 15:18:56 +0800 | [diff] [blame] | 228 | else: |
Xinyu Zhang | 90f08dc | 2022-01-12 15:55:17 +0800 | [diff] [blame] | 229 | partition_statistics['ipc_partition_num'] += 1 |
Mingyang Sun | eab7eae | 2021-09-30 13:06:52 +0800 | [diff] [blame] | 230 | |
Xinyu Zhang | 2bc4d57 | 2021-12-27 16:37:46 +0800 | [diff] [blame] | 231 | for service in manifest.get('services', []): |
| 232 | if 'connection_based' not in service.keys(): |
| 233 | partition_statistics['connection_based_srv_num'] += 1 |
| 234 | elif service['connection_based']: |
| 235 | partition_statistics['connection_based_srv_num'] += 1 |
| 236 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 237 | manifest_out_basename = os.path.splitext(os.path.basename(manifest_path))[0] |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 238 | |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 239 | if 'output_path' in manifest_item: |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 240 | output_path = os.path.expandvars(manifest_item['output_path']) |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 241 | else: |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 242 | output_path = '' |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 243 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 244 | manifest_head_file = os.path.join(OUT_DIR, output_path, 'psa_manifest', |
| 245 | '{}.h'.format(manifest_out_basename))\ |
| 246 | .replace('\\', '/') |
| 247 | intermedia_file = os.path.join(OUT_DIR, output_path, 'auto_generated', |
| 248 | 'intermedia_{}.c'.format(manifest_out_basename))\ |
| 249 | .replace('\\', '/') |
| 250 | load_info_file = os.path.join(OUT_DIR, output_path, 'auto_generated', |
| 251 | 'load_info_{}.c'.format(manifest_out_basename))\ |
| 252 | .replace('\\', '/') |
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 | partition_list.append({'manifest': manifest, 'attr': manifest_item, |
| 255 | 'manifest_out_basename': manifest_out_basename, |
| 256 | 'header_file': manifest_head_file, |
| 257 | 'intermedia_file': intermedia_file, |
| 258 | 'loadinfo_file': load_info_file}) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 259 | |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 260 | # Automatically assign PIDs for partitions without 'pid' attribute |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 261 | pid = max(pid_list, default = TFM_PID_BASE - 1) |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 262 | for idx in no_pid_manifest_idx: |
Kevin Peng | c424eec | 2021-06-25 17:26:11 +0800 | [diff] [blame] | 263 | pid += 1 |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 264 | all_manifests[idx]['pid'] = pid |
Kevin Peng | 56b0ea6 | 2021-10-18 11:32:57 +0800 | [diff] [blame] | 265 | pid_list.append(pid) |
| 266 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 267 | # Set up configurations |
| 268 | if partition_statistics['ipc_partition_num'] == 0 and \ |
| 269 | partition_statistics['sfn_partition_num'] > 0: |
| 270 | if isolation_level > 1: |
| 271 | print('High isolation level SFN model is not supported.') |
| 272 | exit(1) |
| 273 | config_impl['CONFIG_TFM_SPM_BACKEND_SFN'] = '1' |
| 274 | config_impl['CONFIG_TFM_PSA_API_SFN_CALL'] = '1' |
| 275 | elif partition_statistics['ipc_partition_num'] > 0 and \ |
| 276 | partition_statistics['sfn_partition_num'] == 0: |
| 277 | config_impl['CONFIG_TFM_SPM_BACKEND_IPC'] = '1' |
| 278 | if isolation_level > 1: |
| 279 | config_impl['CONFIG_TFM_PSA_API_SUPERVISOR_CALL'] = '1' |
| 280 | else: |
| 281 | config_impl['CONFIG_TFM_PSA_API_CROSS_CALL'] = '1' |
| 282 | elif partition_statistics['ipc_partition_num'] > 0 and \ |
| 283 | partition_statistics['sfn_partition_num'] > 0: |
| 284 | print('IPC and SFN co-work not supported yet.') |
| 285 | exit(1) |
| 286 | else: |
| 287 | print('Invalid partition number input, check configurations.') |
| 288 | exit(1) |
| 289 | |
| 290 | if partition_statistics['connection_based_srv_num'] > 0: |
| 291 | config_impl['CONFIG_TFM_CONNECTION_BASED_SERVICE_API'] = 1 |
| 292 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 293 | context['partitions'] = partition_list |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 294 | context['config_impl'] = config_impl |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 295 | context['stateless_services'] = process_stateless_services(partition_list) |
Ruiqi Jiang | 71d361c | 2021-06-23 17:45:55 +0100 | [diff] [blame] | 296 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 297 | return context |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 298 | |
| 299 | def gen_per_partition_files(context): |
| 300 | """ |
| 301 | Generate per-partition files |
| 302 | |
| 303 | Parameters |
| 304 | ---------- |
| 305 | context: |
| 306 | context contains partition infos |
| 307 | """ |
| 308 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 309 | partition_context = {} |
Sherry Zhang | f58f2bd | 2022-01-10 17:21:11 +0800 | [diff] [blame] | 310 | partition_context['utilities'] = context['utilities'] |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 311 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 312 | manifesttemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/manifestfilename.template')) |
| 313 | memorytemplate = ENV.get_template(os.path.join(sys.path[0], 'templates/partition_intermedia.template')) |
| 314 | 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] | 315 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 316 | logging.info ("Start to generate partition files:") |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 317 | |
| 318 | for one_partition in context['partitions']: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 319 | partition_context['manifest'] = one_partition['manifest'] |
| 320 | partition_context['attr'] = one_partition['attr'] |
| 321 | partition_context['manifest_out_basename'] = one_partition['manifest_out_basename'] |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 322 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 323 | logging.info ("Generating Header: " + one_partition['header_file']) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 324 | outfile_path = os.path.dirname(one_partition['header_file']) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 325 | if not os.path.exists(outfile_path): |
| 326 | os.makedirs(outfile_path) |
| 327 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 328 | headerfile = io.open(one_partition['header_file'], 'w', newline=None) |
| 329 | headerfile.write(manifesttemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 330 | headerfile.close() |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 331 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 332 | logging.info ("Generating Intermedia: " + one_partition['intermedia_file']) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 333 | intermediafile_path = os.path.dirname(one_partition['intermedia_file']) |
Mingyang Sun | d20999f | 2020-10-15 14:53:12 +0800 | [diff] [blame] | 334 | if not os.path.exists(intermediafile_path): |
| 335 | os.makedirs(intermediafile_path) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 336 | intermediafile = io.open(one_partition['intermedia_file'], 'w', newline=None) |
| 337 | intermediafile.write(memorytemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 338 | intermediafile.close() |
Mingyang Sun | d20999f | 2020-10-15 14:53:12 +0800 | [diff] [blame] | 339 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 340 | logging.info ("Generating Loadinfo: " + one_partition['loadinfo_file']) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 341 | infofile_path = os.path.dirname(one_partition['loadinfo_file']) |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 342 | if not os.path.exists(infofile_path): |
| 343 | os.makedirs(infofile_path) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 344 | infooutfile = io.open(one_partition['loadinfo_file'], 'w', newline=None) |
| 345 | infooutfile.write(infotemplate.render(partition_context)) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 346 | infooutfile.close() |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 347 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 348 | logging.info ("Per-partition files done:") |
Mingyang Sun | f6a7857 | 2021-04-02 16:51:05 +0800 | [diff] [blame] | 349 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 350 | def gen_summary_files(context, gen_file_lists): |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 351 | """ |
| 352 | Generate files according to the gen_file_list |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 353 | |
| 354 | Parameters |
| 355 | ---------- |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 356 | gen_file_lists: |
| 357 | The lists of files to generate |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 358 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 359 | file_list = [] |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 360 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 361 | for f in gen_file_lists: |
| 362 | with open(f) as file_list_yaml_file: |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 363 | file_list_yaml = yaml.safe_load(file_list_yaml_file) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 364 | file_list.extend(file_list_yaml['file_list']) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 365 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 366 | logging.info("Start to generate file from the generated list:") |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 367 | for file in file_list: |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 368 | # Replace environment variables in the output filepath |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 369 | manifest_out_file = os.path.expandvars(file['output']) |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 370 | # Replace environment variables in the template filepath |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 371 | templatefile_name = os.path.expandvars(file['template']) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 372 | |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 373 | manifest_out_file = os.path.join(OUT_DIR, manifest_out_file) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 374 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 375 | logging.info ("Generating " + manifest_out_file) |
| 376 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 377 | outfile_path = os.path.dirname(manifest_out_file) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 378 | if not os.path.exists(outfile_path): |
| 379 | os.makedirs(outfile_path) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 380 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 381 | template = ENV.get_template(templatefile_name) |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 382 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 383 | outfile = io.open(manifest_out_file, 'w', newline=None) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 384 | outfile.write(template.render(context)) |
| 385 | outfile.close() |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 386 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 387 | logging.info ("Generation of files done") |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 388 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 389 | def process_stateless_services(partitions): |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 390 | """ |
| 391 | This function collects all stateless services together, and allocates |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 392 | stateless handles for them. |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 393 | Valid stateless handle in service will be converted to an index. If the |
| 394 | stateless handle is set as "auto", or not set, framework will allocate a |
| 395 | valid index for the service. |
| 396 | Framework puts each service into a reordered stateless service list at |
| 397 | position of "index". Other unused positions are left None. |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 398 | """ |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 399 | |
| 400 | STATIC_HANDLE_CONFIG_FILE = 'secure_fw/spm/cmsis_psa/spm_ipc.h' |
| 401 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 402 | collected_stateless_services = [] |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 403 | stateless_index_max_num = \ |
| 404 | int(get_single_macro_def_from_file(STATIC_HANDLE_CONFIG_FILE, 'STATIC_HANDLE_NUM_LIMIT'), base = 10) |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 405 | |
| 406 | # Collect all stateless services first. |
| 407 | for partition in partitions: |
| 408 | # Skip the FF-M 1.0 partitions |
| 409 | if partition['manifest']['psa_framework_version'] < 1.1: |
| 410 | continue |
Kevin Peng | 8849b6a | 2021-11-09 14:17:35 +0800 | [diff] [blame] | 411 | |
| 412 | service_list = partition['manifest'].get('services', []) |
| 413 | |
| 414 | for service in service_list: |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 415 | if 'connection_based' not in service: |
| 416 | raise Exception("'connection_based' is mandatory in FF-M 1.1 service!") |
| 417 | if service['connection_based'] is False: |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 418 | collected_stateless_services.append(service) |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 419 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 420 | if len(collected_stateless_services) == 0: |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 421 | return [] |
| 422 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 423 | if len(collected_stateless_services) > stateless_index_max_num: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 424 | raise Exception('Stateless service numbers range exceed {number}.'.format(number=stateless_index_max_num)) |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 425 | |
| 426 | """ |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 427 | Allocate an empty stateless service list to store services. |
| 428 | Use "handle - 1" as the index for service, since handle value starts from |
| 429 | 1 and list index starts from 0. |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 430 | """ |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 431 | reordered_stateless_services = [None] * stateless_index_max_num |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 432 | auto_alloc_services = [] |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 433 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 434 | for service in collected_stateless_services: |
| 435 | # If not set, it is "auto" by default |
| 436 | if 'stateless_handle' not in service: |
| 437 | auto_alloc_services.append(service) |
| 438 | continue |
| 439 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 440 | service_handle = service['stateless_handle'] |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 441 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 442 | # Fill in service list with specified stateless handle, otherwise skip |
| 443 | if isinstance(service_handle, int): |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 444 | if service_handle < 1 or service_handle > stateless_index_max_num: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 445 | raise Exception('Invalid stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 446 | # Convert handle index to reordered service list index |
| 447 | service_handle = service_handle - 1 |
| 448 | |
| 449 | if reordered_stateless_services[service_handle] is not None: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 450 | raise Exception('Duplicated stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 451 | reordered_stateless_services[service_handle] = service |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 452 | elif service_handle == 'auto': |
| 453 | auto_alloc_services.append(service) |
| 454 | else: |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 455 | raise Exception('Invalid stateless_handle setting: {handle}.'.format(handle=service['stateless_handle'])) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 456 | |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 457 | STATIC_HANDLE_IDX_BIT_WIDTH = \ |
| 458 | int(get_single_macro_def_from_file(STATIC_HANDLE_CONFIG_FILE, 'STATIC_HANDLE_IDX_BIT_WIDTH'), base = 10) |
| 459 | STATIC_HANDLE_IDX_MASK = (1 << STATIC_HANDLE_IDX_BIT_WIDTH) - 1 |
| 460 | |
| 461 | STATIC_HANDLE_INDICATOR_OFFSET = \ |
| 462 | int(get_single_macro_def_from_file(STATIC_HANDLE_CONFIG_FILE, 'STATIC_HANDLE_INDICATOR_OFFSET'), base = 10) |
| 463 | |
| 464 | STATIC_HANDLE_VER_OFFSET = \ |
| 465 | int(get_single_macro_def_from_file(STATIC_HANDLE_CONFIG_FILE, 'STATIC_HANDLE_VER_OFFSET'), base = 10) |
| 466 | |
| 467 | STATIC_HANDLE_VER_BIT_WIDTH = \ |
| 468 | int(get_single_macro_def_from_file(STATIC_HANDLE_CONFIG_FILE, 'STATIC_HANDLE_VER_BIT_WIDTH'), base = 10) |
| 469 | STATIC_HANDLE_VER_MASK = (1 << STATIC_HANDLE_VER_BIT_WIDTH) - 1 |
| 470 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 471 | # Auto-allocate stateless handle and encode the stateless handle |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 472 | for i in range(0, stateless_index_max_num): |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 473 | service = reordered_stateless_services[i] |
| 474 | |
Kevin Peng | c05319d | 2021-04-22 22:59:35 +0800 | [diff] [blame] | 475 | if service == None and len(auto_alloc_services) > 0: |
| 476 | service = auto_alloc_services.pop(0) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 477 | |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 478 | """ |
| 479 | Encode stateless flag and version into stateless handle |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 480 | Check STATIC_HANDLE_CONFIG_FILE for details |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 481 | """ |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 482 | stateless_handle_value = 0 |
| 483 | if service != None: |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 484 | stateless_index = (i & STATIC_HANDLE_IDX_MASK) |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 485 | stateless_handle_value |= stateless_index |
Kevin Peng | ce99e5d | 2021-11-09 18:06:53 +0800 | [diff] [blame] | 486 | stateless_handle_value |= (1 << STATIC_HANDLE_INDICATOR_OFFSET) |
| 487 | stateless_version = (service['version'] & STATIC_HANDLE_VER_MASK) << STATIC_HANDLE_VER_OFFSET |
Mingyang Sun | 453ad40 | 2021-03-17 17:58:33 +0800 | [diff] [blame] | 488 | stateless_handle_value |= stateless_version |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 489 | service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value) |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 490 | service['stateless_handle_index'] = stateless_index |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 491 | |
Mingyang Sun | 4ecea99 | 2021-03-30 17:56:26 +0800 | [diff] [blame] | 492 | reordered_stateless_services[i] = service |
| 493 | |
| 494 | return reordered_stateless_services |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 495 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 496 | def parse_args(): |
Raef Coles | 558487a | 2020-10-29 13:09:44 +0000 | [diff] [blame] | 497 | parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list', |
| 498 | epilog='Note that environment variables in template files will be replaced with their values') |
| 499 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 500 | parser.add_argument('-o', '--outdir' |
| 501 | , dest='outdir' |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 502 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 503 | , metavar='out_dir' |
Kevin Peng | 4fade07 | 2021-10-26 17:57:50 +0800 | [diff] [blame] | 504 | , help='The root directory for generated files') |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 505 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 506 | parser.add_argument('-m', '--manifest-lists' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 507 | , nargs='+' |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 508 | , dest='manifest_lists' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 509 | , required=True |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 510 | , metavar='manifest list' |
| 511 | , help='A list of Secure Partition manifest lists and their original paths.\n\ |
| 512 | The manifest lists might be processed by CMake and\n\ |
| 513 | the path might be different to the original one\n\ |
| 514 | 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] | 515 | |
| 516 | parser.add_argument('-f', '--file-list' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 517 | , nargs='+' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 518 | , dest='gen_file_args' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 519 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 520 | , metavar='file-list' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 521 | , help='These files descripe the file list to generate') |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 522 | |
| 523 | parser.add_argument('-l', '--isolation-level' |
| 524 | , dest='isolation_level' |
| 525 | , required=True |
| 526 | , choices=['1', '2', '3'] |
| 527 | , metavar='isolation-level' |
| 528 | , help='The isolation level') |
| 529 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 530 | parser.add_argument('-q', '--quiet' |
| 531 | , dest='quiet' |
| 532 | , required=False |
| 533 | , default=False |
| 534 | , action='store_true' |
| 535 | , help='Reduce log messages') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 536 | |
| 537 | args = parser.parse_args() |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 538 | |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 539 | if len(args.manifest_lists) % 2 != 0: |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 540 | logging.error('Invalid structure in manifest lists.\n' |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 541 | 'Each element shall consist of a manifest list and its original path') |
| 542 | exit(1) |
| 543 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 544 | return args |
| 545 | |
| 546 | ENV = Environment( |
| 547 | loader = TemplateLoader(), |
| 548 | autoescape = select_autoescape(['html', 'xml']), |
| 549 | lstrip_blocks = True, |
| 550 | trim_blocks = True, |
| 551 | keep_trailing_newline = True |
| 552 | ) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 553 | |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 554 | def main(): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 555 | """ |
| 556 | The entry point of the script. |
| 557 | |
| 558 | Generates the output files based on the templates and the manifests. |
| 559 | """ |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 560 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 561 | global OUT_DIR |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 562 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 563 | args = parse_args() |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 564 | |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 565 | logging.basicConfig(format='%(message)s' |
| 566 | , level=logging.WARNING if args.quiet else logging.INFO) |
| 567 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 568 | OUT_DIR = os.path.abspath(args.outdir) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 569 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 570 | manifest_lists = [os.path.abspath(x) for x in args.manifest_lists] |
| 571 | 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] | 572 | |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 573 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 574 | Relative path to TF-M root folder is supported in the manifests |
| 575 | and default value of manifest list and generated file list are relative to TF-M root folder as well, |
| 576 | so first change directory to TF-M root folder. |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 577 | By doing this, the script can be executed anywhere |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 578 | 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] | 579 | """ |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 580 | os.chdir(os.path.join(sys.path[0], '..')) |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 581 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame^] | 582 | context = process_partition_manifests(manifest_lists, int(args.isolation_level)) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 583 | |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 584 | utilities = {} |
Mingyang Sun | a1ca611 | 2021-01-11 11:34:59 +0800 | [diff] [blame] | 585 | utilities['donotedit_warning'] = donotedit_warning |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 586 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 587 | context['utilities'] = utilities |
Mingyang Sun | eab7eae | 2021-09-30 13:06:52 +0800 | [diff] [blame] | 588 | |
Ken Liu | 861b078 | 2021-05-22 13:15:08 +0800 | [diff] [blame] | 589 | gen_per_partition_files(context) |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 590 | gen_summary_files(context, gen_file_lists) |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 591 | |
Kevin Peng | 5bc82d2 | 2021-10-19 11:18:40 +0800 | [diff] [blame] | 592 | if __name__ == '__main__': |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 593 | main() |