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