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