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