blob: 82acdb14828f49f1ce621f4dea7e02c191a6742a [file] [log] [blame]
Miklos Balint470919c2018-05-22 17:51:29 +02001#-------------------------------------------------------------------------------
Kevin Peng578a8492020-12-31 10:22:59 +08002# Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Miklos Balint470919c2018-05-22 17:51:29 +02003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8import os
Mate Toth-Pal36f21842018-11-08 16:12:51 +01009import io
Shawn Shana9ad1e02019-08-07 15:49:48 +080010import sys
11import argparse
Ken Liu1f345b02020-05-30 21:11:05 +080012from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound
Miklos Balint470919c2018-05-22 17:51:29 +020013
14try:
15 import yaml
16except ImportError as e:
Mate Toth-Pala99ec6b2019-05-07 11:00:56 +020017 print (str(e) + " To install it, type:")
Mate Toth-Pal36f21842018-11-08 16:12:51 +010018 print ("pip install PyYAML")
Miklos Balint470919c2018-05-22 17:51:29 +020019 exit(1)
20
Edison Ai48b2d9e2019-06-24 14:39:45 +080021donotedit_warning = \
22 "/*********** " + \
23 "WARNING: This is an auto-generated file. Do not edit!" + \
24 " ***********/"
Kevin Peng655f2392019-11-27 16:33:02 +080025
Kevin Peng655f2392019-11-27 16:33:02 +080026OUT_DIR = None # The root directory that files are generated to
Edison Ai48b2d9e2019-06-24 14:39:45 +080027
Ruiqi Jiang71d361c2021-06-23 17:45:55 +010028# variable for checking for duplicated sid
29sid_list = []
30partition_list_sid = []
31service_list = []
32sid_duplicated_partition = []
33sid_duplicated_sid = []
34sid_duplicated_service = []
35
Mate Toth-Pal36f21842018-11-08 16:12:51 +010036class TemplateLoader(BaseLoader):
37 """
38 Template loader class.
Miklos Balint470919c2018-05-22 17:51:29 +020039
Mate Toth-Pal36f21842018-11-08 16:12:51 +010040 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 Balint470919c2018-05-22 17:51:29 +020045
Mate Toth-Pal36f21842018-11-08 16:12:51 +010046 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 Sun294ce2e2021-06-11 11:58:24 +080061def 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 Jiang71d361c2021-06-23 17:45:55 +010077 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 Sun294ce2e2021-06-11 11:58:24 +080091 return partition_manifest
92
David Hub2694202021-07-15 14:58:39 +080093def process_partition_manifests(manifest_list_files, extra_manifests_list):
Mate Toth-Pal36f21842018-11-08 16:12:51 +010094 """
Kevin Peng655f2392019-11-27 16:33:02 +080095 Parse the input manifest, generate the data base for genereated files
96 and generate manifest header files.
Mate Toth-Pal36f21842018-11-08 16:12:51 +010097
98 Parameters
99 ----------
Raef Colesf42f0882020-07-10 10:01:58 +0100100 manifest_list_files:
101 The manifest lists to parse.
David Hub2694202021-07-15 14:58:39 +0800102 extra_manifests_list:
103 The extra manifest list to parse and its original path.
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100104
105 Returns
106 -------
Kevin Peng578a8492020-12-31 10:22:59 +0800107 The partition data base.
Edison Ai48b2d9e2019-06-24 14:39:45 +0800108 """
Kevin Peng655f2392019-11-27 16:33:02 +0800109
Ken Liu861b0782021-05-22 13:15:08 +0800110 partition_list = []
Kevin Peng655f2392019-11-27 16:33:02 +0800111 manifest_list = []
Mingyang Suneab7eae2021-09-30 13:06:52 +0800112 ipc_partition_num = 0
113 sfn_partition_num = 0
Kevin Peng655f2392019-11-27 16:33:02 +0800114
Raef Colesf42f0882020-07-10 10:01:58 +0100115 for f in manifest_list_files:
116 with open(f) as manifest_list_yaml_file:
117 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
Kevin Peng655f2392019-11-27 16:33:02 +0800118 manifest_list.extend(manifest_dic["manifest_list"])
Ken Liu861b0782021-05-22 13:15:08 +0800119 manifest_list_yaml_file.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800120
David Hub2694202021-07-15 14:58:39 +0800121 # Out-of-tree secure partition build
122 if extra_manifests_list is not None:
123 for i, item in enumerate(extra_manifests_list):
124 # Skip if current item is the original manifest path
125 if os.path.isdir(item):
126 continue
127
128 # The manifest list file generated by configure_file()
129 with open(item) as manifest_list_yaml_file:
130 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
131 extra_manifest_dic = manifest_dic["manifest_list"]
132 for dict in extra_manifest_dic:
133 # Append original directory of out-of-tree partition's
134 # manifest list source code
135 dict['extra_path'] = extra_manifests_list[i + 1]
136 manifest_list.append(dict)
137 manifest_list_yaml_file.close()
138
Xinyu Zhang19504a52021-03-31 16:26:20 +0800139 pid_list = []
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800140 no_pid_manifest_idx = []
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)
145 continue
Xinyu Zhang19504a52021-03-31 16:26:20 +0800146 # Check if partition ID is duplicated
147 if manifest_item['pid'] in pid_list:
148 raise Exception("PID No. {pid} has already been used!".format(pid=manifest_item['pid']))
149 pid_list.append(manifest_item['pid'])
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800150 # Automatically generate PIDs for partitions without PID
151 pid = 256
152 for idx in no_pid_manifest_idx:
153 while pid in pid_list:
154 pid += 1
155 manifest_list[idx]['pid'] = pid
156 pid_list.append(pid)
Xinyu Zhang19504a52021-03-31 16:26:20 +0800157
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800158 for manifest_item in manifest_list:
Raef Coles558487a2020-10-29 13:09:44 +0000159 # Replace environment variables in the manifest path
Kevin Peng655f2392019-11-27 16:33:02 +0800160 manifest_path = os.path.expandvars(manifest_item['manifest'])
David Hub2694202021-07-15 14:58:39 +0800161
162 # Handle out-of-tree secure partition manifest file path
163 if 'extra_path' in manifest_item:
164 if not os.path.isabs(manifest_path):
165 # manifest file path provided by manifest list is relative to
166 # manifest list path
167 manifest_path = os.path.join(manifest_item['extra_path'], manifest_path).replace('\\', '/')
168
Kevin Peng655f2392019-11-27 16:33:02 +0800169 file = open(manifest_path)
Mingyang Sun294ce2e2021-06-11 11:58:24 +0800170 manifest = manifest_validation(yaml.safe_load(file))
Ken Liu861b0782021-05-22 13:15:08 +0800171 file.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800172
Mingyang Suneab7eae2021-09-30 13:06:52 +0800173 # Count the number of IPC partitions
174 if manifest["psa_framework_version"] == 1.1 and manifest["model"] == 'IPC':
175 ipc_partition_num += 1
176 elif manifest["psa_framework_version"] == 1.1 and manifest["model"] == 'SFN':
177 sfn_partition_num += 1
178 elif "services" in manifest.keys():
179 ipc_partition_num += 1
180
Kevin Peng655f2392019-11-27 16:33:02 +0800181 manifest_dir, manifest_name = os.path.split(manifest_path)
Ken Liu861b0782021-05-22 13:15:08 +0800182 manifest_out_basename = manifest_name.replace('.yaml', '').replace('.json', '')
Kevin Peng655f2392019-11-27 16:33:02 +0800183
Kevin Peng655f2392019-11-27 16:33:02 +0800184 if OUT_DIR is not None:
David Hub2694202021-07-15 14:58:39 +0800185 if 'output_path' in manifest_item:
186 # Build up generated files directory accroding to the relative
187 # path specified in output_path by the partition
188 output_path = os.path.expandvars(manifest_item['output_path'])
189 manifest_head_file = os.path.join(output_path, "psa_manifest", manifest_out_basename + '.h')
190 intermedia_file = os.path.join(output_path, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
191 load_info_file = os.path.join(output_path, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
192 else:
193 manifest_head_file = os.path.join(manifest_dir, "psa_manifest", manifest_out_basename + '.h')
194 intermedia_file = os.path.join(manifest_dir, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
195 load_info_file = os.path.join(manifest_dir, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
196
197 """
198 Remove the `source_path` portion of the filepaths, so that it can be
199 interpreted as a relative path from the OUT_DIR.
200 """
201 if 'source_path' in manifest_item:
202 # Replace environment variables in the source path
203 source_path = os.path.expandvars(manifest_item['source_path'])
204 manifest_head_file = os.path.relpath(manifest_head_file, start = source_path)
205 intermedia_file = os.path.relpath(intermedia_file, start = source_path)
206 load_info_file = os.path.relpath(load_info_file, start = source_path)
MartinaHanusovaNXP35957f12021-07-14 15:30:15 +0200207
208 manifest_head_file = os.path.join(OUT_DIR, manifest_head_file).replace('\\', '/')
209 intermedia_file = os.path.join(OUT_DIR, intermedia_file).replace('\\', '/')
210 load_info_file = os.path.join(OUT_DIR, load_info_file).replace('\\', '/')
Kevin Peng655f2392019-11-27 16:33:02 +0800211
Ken Liu861b0782021-05-22 13:15:08 +0800212 partition_list.append({"manifest": manifest, "attr": manifest_item,
213 "manifest_out_basename": manifest_out_basename,
214 "header_file": manifest_head_file,
215 "intermedia_file": intermedia_file,
216 "loadinfo_file": load_info_file})
217
Ruiqi Jiang71d361c2021-06-23 17:45:55 +0100218 if len(sid_duplicated_sid) != 0:
219 print("The following signals have duplicated sids."
220 "A Service requires a unique sid")
221 for i in range(len(sid_duplicated_sid)):
222 print("Partition: {parti} , Service: {servi} , SID: {sidn}".format(
223 parti = sid_duplicated_partition[i],
224 servi = sid_duplicated_service[i],
225 sidn = sid_duplicated_sid[i])
226 )
227
228 raise Exception("Duplicated SID found, check above for details")
229
Mingyang Suneab7eae2021-09-30 13:06:52 +0800230 return partition_list, ipc_partition_num, sfn_partition_num
Ken Liu861b0782021-05-22 13:15:08 +0800231
232def gen_per_partition_files(context):
233 """
234 Generate per-partition files
235
236 Parameters
237 ----------
238 context:
239 context contains partition infos
240 """
241
242 subutilities = {}
243 subutilities['donotedit_warning'] = donotedit_warning
244
245 subcontext = {}
246 subcontext['utilities'] = subutilities
247
Ken Liu72c031e2021-08-09 16:42:54 +0800248 manifesttemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/manifestfilename.template'))
249 memorytemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/partition_intermedia.template'))
250 infotemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/partition_load_info.template'))
Ken Liu861b0782021-05-22 13:15:08 +0800251
252 print ("Start to generate partition files:")
253
254 for one_partition in context['partitions']:
255 subcontext['manifest'] = one_partition['manifest']
256 subcontext['attr'] = one_partition['attr']
257 subcontext['manifest_out_basename'] = one_partition['manifest_out_basename']
258
259 print ("Generating Header: " + one_partition['header_file'])
260 outfile_path = os.path.dirname(one_partition['header_file'])
Kevin Peng655f2392019-11-27 16:33:02 +0800261 if not os.path.exists(outfile_path):
262 os.makedirs(outfile_path)
263
Ken Liu861b0782021-05-22 13:15:08 +0800264 headerfile = io.open(one_partition['header_file'], "w", newline=None)
265 headerfile.write(manifesttemplate.render(subcontext))
266 headerfile.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800267
Ken Liu861b0782021-05-22 13:15:08 +0800268 print ("Generating Intermedia: " + one_partition['intermedia_file'])
269 intermediafile_path = os.path.dirname(one_partition['intermedia_file'])
Mingyang Sund20999f2020-10-15 14:53:12 +0800270 if not os.path.exists(intermediafile_path):
271 os.makedirs(intermediafile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800272 intermediafile = io.open(one_partition['intermedia_file'], "w", newline=None)
273 intermediafile.write(memorytemplate.render(subcontext))
274 intermediafile.close()
Mingyang Sund20999f2020-10-15 14:53:12 +0800275
Ken Liu861b0782021-05-22 13:15:08 +0800276 print ("Generating Loadinfo: " + one_partition['loadinfo_file'])
277 infofile_path = os.path.dirname(one_partition['loadinfo_file'])
Mingyang Sunf6a78572021-04-02 16:51:05 +0800278 if not os.path.exists(infofile_path):
279 os.makedirs(infofile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800280 infooutfile = io.open(one_partition['loadinfo_file'], "w", newline=None)
281 infooutfile.write(infotemplate.render(subcontext))
282 infooutfile.close()
Mingyang Sunf6a78572021-04-02 16:51:05 +0800283
Ken Liu861b0782021-05-22 13:15:08 +0800284 print ("Per-partition files done:")
Mingyang Sunf6a78572021-04-02 16:51:05 +0800285
Ken Liu861b0782021-05-22 13:15:08 +0800286def gen_summary_files(context, gen_file_lists):
Kevin Peng655f2392019-11-27 16:33:02 +0800287 """
288 Generate files according to the gen_file_list
Edison Ai48b2d9e2019-06-24 14:39:45 +0800289
290 Parameters
291 ----------
Raef Colesf42f0882020-07-10 10:01:58 +0100292 gen_file_lists:
293 The lists of files to generate
Edison Ai48b2d9e2019-06-24 14:39:45 +0800294 """
Kevin Peng655f2392019-11-27 16:33:02 +0800295 file_list = []
Shawn Shana9ad1e02019-08-07 15:49:48 +0800296
Raef Colesf42f0882020-07-10 10:01:58 +0100297 for f in gen_file_lists:
298 with open(f) as file_list_yaml_file:
Kevin Peng655f2392019-11-27 16:33:02 +0800299 file_list_yaml = yaml.safe_load(file_list_yaml_file)
300 file_list.extend(file_list_yaml["file_list"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800301
edison.ai7b299f52020-07-16 15:44:18 +0800302 print("Start to generate file from the generated list:")
Kevin Peng655f2392019-11-27 16:33:02 +0800303 for file in file_list:
Raef Coles558487a2020-10-29 13:09:44 +0000304 # Replace environment variables in the output filepath
Ken Liu861b0782021-05-22 13:15:08 +0800305 manifest_out_file = os.path.expandvars(file["output"])
Raef Coles558487a2020-10-29 13:09:44 +0000306 # Replace environment variables in the template filepath
Kevin Peng1ec5e7c2019-11-29 10:52:00 +0800307 templatefile_name = os.path.expandvars(file["template"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800308
Kevin Peng655f2392019-11-27 16:33:02 +0800309 if OUT_DIR is not None:
Ken Liu861b0782021-05-22 13:15:08 +0800310 manifest_out_file = os.path.join(OUT_DIR, manifest_out_file)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800311
Ken Liu861b0782021-05-22 13:15:08 +0800312 print ("Generating " + manifest_out_file)
edison.ai7b299f52020-07-16 15:44:18 +0800313
Ken Liu861b0782021-05-22 13:15:08 +0800314 outfile_path = os.path.dirname(manifest_out_file)
Kevin Peng655f2392019-11-27 16:33:02 +0800315 if not os.path.exists(outfile_path):
316 os.makedirs(outfile_path)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800317
Kevin Peng655f2392019-11-27 16:33:02 +0800318 template = ENV.get_template(templatefile_name)
Edison Ai6e3f2a32019-06-11 15:29:05 +0800319
Ken Liu861b0782021-05-22 13:15:08 +0800320 outfile = io.open(manifest_out_file, "w", newline=None)
Kevin Peng655f2392019-11-27 16:33:02 +0800321 outfile.write(template.render(context))
322 outfile.close()
Edison Ai48b2d9e2019-06-24 14:39:45 +0800323
Kevin Peng655f2392019-11-27 16:33:02 +0800324 print ("Generation of files done")
Edison Ai48b2d9e2019-06-24 14:39:45 +0800325
Ken Liu861b0782021-05-22 13:15:08 +0800326def process_stateless_services(partitions, stateless_index_max_num):
Mingyang Suna1ca6112021-01-11 11:34:59 +0800327 """
328 This function collects all stateless services together, and allocates
Mingyang Sun4ecea992021-03-30 17:56:26 +0800329 stateless handles for them.
Kevin Pengc05319d2021-04-22 22:59:35 +0800330 Valid stateless handle in service will be converted to an index. If the
331 stateless handle is set as "auto", or not set, framework will allocate a
332 valid index for the service.
333 Framework puts each service into a reordered stateless service list at
334 position of "index". Other unused positions are left None.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800335 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800336 collected_stateless_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800337
338 # Collect all stateless services first.
339 for partition in partitions:
340 # Skip the FF-M 1.0 partitions
341 if partition['manifest']['psa_framework_version'] < 1.1:
342 continue
Mingyang Suna1ca6112021-01-11 11:34:59 +0800343 for service in partition['manifest']['services']:
344 if 'connection_based' not in service:
345 raise Exception("'connection_based' is mandatory in FF-M 1.1 service!")
346 if service['connection_based'] is False:
Kevin Pengc05319d2021-04-22 22:59:35 +0800347 collected_stateless_services.append(service)
Mingyang Suna1ca6112021-01-11 11:34:59 +0800348
Kevin Pengc05319d2021-04-22 22:59:35 +0800349 if len(collected_stateless_services) == 0:
Mingyang Suna1ca6112021-01-11 11:34:59 +0800350 return []
351
Ken Liu861b0782021-05-22 13:15:08 +0800352 if len(collected_stateless_services) > stateless_index_max_num:
353 raise Exception("Stateless service numbers range exceed {number}.".format(number=stateless_index_max_num))
Mingyang Suna1ca6112021-01-11 11:34:59 +0800354
355 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800356 Allocate an empty stateless service list to store services.
357 Use "handle - 1" as the index for service, since handle value starts from
358 1 and list index starts from 0.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800359 """
Ken Liu861b0782021-05-22 13:15:08 +0800360 reordered_stateless_services = [None] * stateless_index_max_num
Kevin Pengc05319d2021-04-22 22:59:35 +0800361 auto_alloc_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800362
Kevin Pengc05319d2021-04-22 22:59:35 +0800363 for service in collected_stateless_services:
364 # If not set, it is "auto" by default
365 if 'stateless_handle' not in service:
366 auto_alloc_services.append(service)
367 continue
368
Mingyang Sun4ecea992021-03-30 17:56:26 +0800369 service_handle = service['stateless_handle']
Mingyang Suna1ca6112021-01-11 11:34:59 +0800370
Mingyang Sun4ecea992021-03-30 17:56:26 +0800371 # Fill in service list with specified stateless handle, otherwise skip
372 if isinstance(service_handle, int):
Ken Liu861b0782021-05-22 13:15:08 +0800373 if service_handle < 1 or service_handle > stateless_index_max_num:
Kevin Pengc05319d2021-04-22 22:59:35 +0800374 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800375 # Convert handle index to reordered service list index
376 service_handle = service_handle - 1
377
378 if reordered_stateless_services[service_handle] is not None:
Kevin Pengc05319d2021-04-22 22:59:35 +0800379 raise Exception("Duplicated stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800380 reordered_stateless_services[service_handle] = service
Kevin Pengc05319d2021-04-22 22:59:35 +0800381 elif service_handle == 'auto':
382 auto_alloc_services.append(service)
383 else:
384 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800385
386 # Auto-allocate stateless handle and encode the stateless handle
Ken Liu861b0782021-05-22 13:15:08 +0800387 for i in range(0, stateless_index_max_num):
Mingyang Sun4ecea992021-03-30 17:56:26 +0800388 service = reordered_stateless_services[i]
389
Kevin Pengc05319d2021-04-22 22:59:35 +0800390 if service == None and len(auto_alloc_services) > 0:
391 service = auto_alloc_services.pop(0)
Mingyang Sun4ecea992021-03-30 17:56:26 +0800392
Mingyang Sun453ad402021-03-17 17:58:33 +0800393 """
394 Encode stateless flag and version into stateless handle
395 bit 30: stateless handle indicator
396 bit 15-8: stateless service version
397 bit 7-0: stateless handle index
398 """
Mingyang Sun4ecea992021-03-30 17:56:26 +0800399 stateless_handle_value = 0
400 if service != None:
401 stateless_index = (i & 0xFF)
402 stateless_handle_value |= stateless_index
Mingyang Sun453ad402021-03-17 17:58:33 +0800403 stateless_flag = 1 << 30
404 stateless_handle_value |= stateless_flag
Mingyang Sun4ecea992021-03-30 17:56:26 +0800405 stateless_version = (service['version'] & 0xFF) << 8
Mingyang Sun453ad402021-03-17 17:58:33 +0800406 stateless_handle_value |= stateless_version
Mingyang Sun4ecea992021-03-30 17:56:26 +0800407 service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value)
Ken Liu861b0782021-05-22 13:15:08 +0800408 service['stateless_handle_index'] = stateless_index
Mingyang Suna1ca6112021-01-11 11:34:59 +0800409
Mingyang Sun4ecea992021-03-30 17:56:26 +0800410 reordered_stateless_services[i] = service
411
412 return reordered_stateless_services
Mingyang Suna1ca6112021-01-11 11:34:59 +0800413
Kevin Peng655f2392019-11-27 16:33:02 +0800414def parse_args():
Raef Coles558487a2020-10-29 13:09:44 +0000415 parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list',
416 epilog='Note that environment variables in template files will be replaced with their values')
417
Kevin Peng655f2392019-11-27 16:33:02 +0800418 parser.add_argument('-o', '--outdir'
419 , dest='outdir'
420 , required=False
421 , default=None
422 , metavar='out_dir'
423 , help='The root directory for generated files, the default is TF-M root folder.')
Shawn Shana9ad1e02019-08-07 15:49:48 +0800424
Kevin Peng655f2392019-11-27 16:33:02 +0800425 parser.add_argument('-m', '--manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100426 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800427 , dest='manifest_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100428 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800429 , metavar='manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100430 , help='A set of secure partition manifest lists to parse')
Kevin Peng655f2392019-11-27 16:33:02 +0800431
432 parser.add_argument('-f', '--file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100433 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800434 , dest='gen_file_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100435 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800436 , metavar='file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100437 , help='These files descripe the file list to generate')
Kevin Peng655f2392019-11-27 16:33:02 +0800438
David Hub2694202021-07-15 14:58:39 +0800439 parser.add_argument('-e', '--extra-manifest'
440 , nargs='*'
441 , dest='extra_manifests_args'
442 , required=False
443 , default=None
444 , metavar='out-of-tree-manifest-list'
445 , help='Optional. Manifest lists and original paths for out-of-tree secure partitions.')
446
Kevin Peng655f2392019-11-27 16:33:02 +0800447 args = parser.parse_args()
448 manifest_args = args.manifest_args
449 gen_file_args = args.gen_file_args
450
Kevin Peng655f2392019-11-27 16:33:02 +0800451 return args
452
453ENV = Environment(
454 loader = TemplateLoader(),
455 autoescape = select_autoescape(['html', 'xml']),
456 lstrip_blocks = True,
457 trim_blocks = True,
458 keep_trailing_newline = True
459 )
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100460
Miklos Balint470919c2018-05-22 17:51:29 +0200461def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100462 """
463 The entry point of the script.
464
465 Generates the output files based on the templates and the manifests.
466 """
Shawn Shana9ad1e02019-08-07 15:49:48 +0800467
Kevin Peng655f2392019-11-27 16:33:02 +0800468 global OUT_DIR
Shawn Shana9ad1e02019-08-07 15:49:48 +0800469
Kevin Peng655f2392019-11-27 16:33:02 +0800470 args = parse_args()
Shawn Shana9ad1e02019-08-07 15:49:48 +0800471
Kevin Peng655f2392019-11-27 16:33:02 +0800472 manifest_args = args.manifest_args
473 gen_file_args = args.gen_file_args
David Hub2694202021-07-15 14:58:39 +0800474 extra_manifests_args = args.extra_manifests_args
Kevin Peng655f2392019-11-27 16:33:02 +0800475 OUT_DIR = args.outdir
Kevin Peng655f2392019-11-27 16:33:02 +0800476
Raef Coles558487a2020-10-29 13:09:44 +0000477 manifest_list = [os.path.abspath(x) for x in args.manifest_args]
478 gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]
Shawn Shana9ad1e02019-08-07 15:49:48 +0800479
David Hub2694202021-07-15 14:58:39 +0800480 if extra_manifests_args is not None:
481 extra_manifests_list = [os.path.abspath(x) for x in extra_manifests_args]
482 else:
483 extra_manifests_list = None
484
Shawn Shana9ad1e02019-08-07 15:49:48 +0800485 """
Kevin Peng655f2392019-11-27 16:33:02 +0800486 Relative path to TF-M root folder is supported in the manifests
487 and default value of manifest list and generated file list are relative to TF-M root folder as well,
488 so first change directory to TF-M root folder.
Shawn Shana9ad1e02019-08-07 15:49:48 +0800489 By doing this, the script can be executed anywhere
Kevin Peng655f2392019-11-27 16:33:02 +0800490 The script is located in <TF-M root folder>/tools, so sys.path[0]<location of the script>/.. is TF-M root folder.
Shawn Shana9ad1e02019-08-07 15:49:48 +0800491 """
492 os.chdir(os.path.join(sys.path[0], ".."))
493
Mingyang Suneab7eae2021-09-30 13:06:52 +0800494 partition_list, ipc_partition_num, sfn_partition_num = process_partition_manifests(manifest_list, extra_manifests_list)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100495
Edison Ai6e3f2a32019-06-11 15:29:05 +0800496 utilities = {}
Mingyang Suna1ca6112021-01-11 11:34:59 +0800497 utilities['donotedit_warning'] = donotedit_warning
Miklos Balint470919c2018-05-22 17:51:29 +0200498
Ken Liu861b0782021-05-22 13:15:08 +0800499 context = {}
500 context['partitions'] = partition_list
Kevin Peng655f2392019-11-27 16:33:02 +0800501 context['utilities'] = utilities
Ken Liu861b0782021-05-22 13:15:08 +0800502 context['stateless_services'] = process_stateless_services(partition_list, 32)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100503
Mingyang Suneab7eae2021-09-30 13:06:52 +0800504 context['ipc_partition_num'] = ipc_partition_num
505 context['sfn_partition_num'] = sfn_partition_num
506
Ken Liu861b0782021-05-22 13:15:08 +0800507 gen_per_partition_files(context)
508 gen_summary_files(context, gen_file_list)
Miklos Balint470919c2018-05-22 17:51:29 +0200509
510if __name__ == "__main__":
511 main()