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