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