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