blob: 9b7a36a1a162e7eb77ef7e921e3785f96d8c085b [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 = []
112
Raef Colesf42f0882020-07-10 10:01:58 +0100113 for f in manifest_list_files:
114 with open(f) as manifest_list_yaml_file:
115 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
Kevin Peng655f2392019-11-27 16:33:02 +0800116 manifest_list.extend(manifest_dic["manifest_list"])
Ken Liu861b0782021-05-22 13:15:08 +0800117 manifest_list_yaml_file.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800118
David Hub2694202021-07-15 14:58:39 +0800119 # Out-of-tree secure partition build
120 if extra_manifests_list is not None:
121 for i, item in enumerate(extra_manifests_list):
122 # Skip if current item is the original manifest path
123 if os.path.isdir(item):
124 continue
125
126 # The manifest list file generated by configure_file()
127 with open(item) as manifest_list_yaml_file:
128 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
129 extra_manifest_dic = manifest_dic["manifest_list"]
130 for dict in extra_manifest_dic:
131 # Append original directory of out-of-tree partition's
132 # manifest list source code
133 dict['extra_path'] = extra_manifests_list[i + 1]
134 manifest_list.append(dict)
135 manifest_list_yaml_file.close()
136
Xinyu Zhang19504a52021-03-31 16:26:20 +0800137 pid_list = []
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800138 no_pid_manifest_idx = []
139 for i, manifest_item in enumerate(manifest_list):
140 # Check if partition ID is manually set
141 if 'pid' not in manifest_item.keys():
142 no_pid_manifest_idx.append(i)
143 continue
Xinyu Zhang19504a52021-03-31 16:26:20 +0800144 # Check if partition ID is duplicated
145 if manifest_item['pid'] in pid_list:
146 raise Exception("PID No. {pid} has already been used!".format(pid=manifest_item['pid']))
147 pid_list.append(manifest_item['pid'])
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800148 # Automatically generate PIDs for partitions without PID
149 pid = 256
150 for idx in no_pid_manifest_idx:
151 while pid in pid_list:
152 pid += 1
153 manifest_list[idx]['pid'] = pid
154 pid_list.append(pid)
Xinyu Zhang19504a52021-03-31 16:26:20 +0800155
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800156 for manifest_item in manifest_list:
Raef Coles558487a2020-10-29 13:09:44 +0000157 # Replace environment variables in the manifest path
Kevin Peng655f2392019-11-27 16:33:02 +0800158 manifest_path = os.path.expandvars(manifest_item['manifest'])
David Hub2694202021-07-15 14:58:39 +0800159
160 # Handle out-of-tree secure partition manifest file path
161 if 'extra_path' in manifest_item:
162 if not os.path.isabs(manifest_path):
163 # manifest file path provided by manifest list is relative to
164 # manifest list path
165 manifest_path = os.path.join(manifest_item['extra_path'], manifest_path).replace('\\', '/')
166
Kevin Peng655f2392019-11-27 16:33:02 +0800167 file = open(manifest_path)
Mingyang Sun294ce2e2021-06-11 11:58:24 +0800168 manifest = manifest_validation(yaml.safe_load(file))
Ken Liu861b0782021-05-22 13:15:08 +0800169 file.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800170
171 manifest_dir, manifest_name = os.path.split(manifest_path)
Ken Liu861b0782021-05-22 13:15:08 +0800172 manifest_out_basename = manifest_name.replace('.yaml', '').replace('.json', '')
Kevin Peng655f2392019-11-27 16:33:02 +0800173
Kevin Peng655f2392019-11-27 16:33:02 +0800174 if OUT_DIR is not None:
David Hub2694202021-07-15 14:58:39 +0800175 if 'output_path' in manifest_item:
176 # Build up generated files directory accroding to the relative
177 # path specified in output_path by the partition
178 output_path = os.path.expandvars(manifest_item['output_path'])
179 manifest_head_file = os.path.join(output_path, "psa_manifest", manifest_out_basename + '.h')
180 intermedia_file = os.path.join(output_path, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
181 load_info_file = os.path.join(output_path, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
182 else:
183 manifest_head_file = os.path.join(manifest_dir, "psa_manifest", manifest_out_basename + '.h')
184 intermedia_file = os.path.join(manifest_dir, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
185 load_info_file = os.path.join(manifest_dir, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
186
187 """
188 Remove the `source_path` portion of the filepaths, so that it can be
189 interpreted as a relative path from the OUT_DIR.
190 """
191 if 'source_path' in manifest_item:
192 # Replace environment variables in the source path
193 source_path = os.path.expandvars(manifest_item['source_path'])
194 manifest_head_file = os.path.relpath(manifest_head_file, start = source_path)
195 intermedia_file = os.path.relpath(intermedia_file, start = source_path)
196 load_info_file = os.path.relpath(load_info_file, start = source_path)
MartinaHanusovaNXP35957f12021-07-14 15:30:15 +0200197
198 manifest_head_file = os.path.join(OUT_DIR, manifest_head_file).replace('\\', '/')
199 intermedia_file = os.path.join(OUT_DIR, intermedia_file).replace('\\', '/')
200 load_info_file = os.path.join(OUT_DIR, load_info_file).replace('\\', '/')
Kevin Peng655f2392019-11-27 16:33:02 +0800201
Ken Liu861b0782021-05-22 13:15:08 +0800202 partition_list.append({"manifest": manifest, "attr": manifest_item,
203 "manifest_out_basename": manifest_out_basename,
204 "header_file": manifest_head_file,
205 "intermedia_file": intermedia_file,
206 "loadinfo_file": load_info_file})
207
Ruiqi Jiang71d361c2021-06-23 17:45:55 +0100208 if len(sid_duplicated_sid) != 0:
209 print("The following signals have duplicated sids."
210 "A Service requires a unique sid")
211 for i in range(len(sid_duplicated_sid)):
212 print("Partition: {parti} , Service: {servi} , SID: {sidn}".format(
213 parti = sid_duplicated_partition[i],
214 servi = sid_duplicated_service[i],
215 sidn = sid_duplicated_sid[i])
216 )
217
218 raise Exception("Duplicated SID found, check above for details")
219
Ken Liu861b0782021-05-22 13:15:08 +0800220 return partition_list
221
222def gen_per_partition_files(context):
223 """
224 Generate per-partition files
225
226 Parameters
227 ----------
228 context:
229 context contains partition infos
230 """
231
232 subutilities = {}
233 subutilities['donotedit_warning'] = donotedit_warning
234
235 subcontext = {}
236 subcontext['utilities'] = subutilities
237
Ken Liu72c031e2021-08-09 16:42:54 +0800238 manifesttemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/manifestfilename.template'))
239 memorytemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/partition_intermedia.template'))
240 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 +0800241
242 print ("Start to generate partition files:")
243
244 for one_partition in context['partitions']:
245 subcontext['manifest'] = one_partition['manifest']
246 subcontext['attr'] = one_partition['attr']
247 subcontext['manifest_out_basename'] = one_partition['manifest_out_basename']
248
249 print ("Generating Header: " + one_partition['header_file'])
250 outfile_path = os.path.dirname(one_partition['header_file'])
Kevin Peng655f2392019-11-27 16:33:02 +0800251 if not os.path.exists(outfile_path):
252 os.makedirs(outfile_path)
253
Ken Liu861b0782021-05-22 13:15:08 +0800254 headerfile = io.open(one_partition['header_file'], "w", newline=None)
255 headerfile.write(manifesttemplate.render(subcontext))
256 headerfile.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800257
Ken Liu861b0782021-05-22 13:15:08 +0800258 print ("Generating Intermedia: " + one_partition['intermedia_file'])
259 intermediafile_path = os.path.dirname(one_partition['intermedia_file'])
Mingyang Sund20999f2020-10-15 14:53:12 +0800260 if not os.path.exists(intermediafile_path):
261 os.makedirs(intermediafile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800262 intermediafile = io.open(one_partition['intermedia_file'], "w", newline=None)
263 intermediafile.write(memorytemplate.render(subcontext))
264 intermediafile.close()
Mingyang Sund20999f2020-10-15 14:53:12 +0800265
Ken Liu861b0782021-05-22 13:15:08 +0800266 print ("Generating Loadinfo: " + one_partition['loadinfo_file'])
267 infofile_path = os.path.dirname(one_partition['loadinfo_file'])
Mingyang Sunf6a78572021-04-02 16:51:05 +0800268 if not os.path.exists(infofile_path):
269 os.makedirs(infofile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800270 infooutfile = io.open(one_partition['loadinfo_file'], "w", newline=None)
271 infooutfile.write(infotemplate.render(subcontext))
272 infooutfile.close()
Mingyang Sunf6a78572021-04-02 16:51:05 +0800273
Ken Liu861b0782021-05-22 13:15:08 +0800274 print ("Per-partition files done:")
Mingyang Sunf6a78572021-04-02 16:51:05 +0800275
Ken Liu861b0782021-05-22 13:15:08 +0800276def gen_summary_files(context, gen_file_lists):
Kevin Peng655f2392019-11-27 16:33:02 +0800277 """
278 Generate files according to the gen_file_list
Edison Ai48b2d9e2019-06-24 14:39:45 +0800279
280 Parameters
281 ----------
Raef Colesf42f0882020-07-10 10:01:58 +0100282 gen_file_lists:
283 The lists of files to generate
Edison Ai48b2d9e2019-06-24 14:39:45 +0800284 """
Kevin Peng655f2392019-11-27 16:33:02 +0800285 file_list = []
Shawn Shana9ad1e02019-08-07 15:49:48 +0800286
Raef Colesf42f0882020-07-10 10:01:58 +0100287 for f in gen_file_lists:
288 with open(f) as file_list_yaml_file:
Kevin Peng655f2392019-11-27 16:33:02 +0800289 file_list_yaml = yaml.safe_load(file_list_yaml_file)
290 file_list.extend(file_list_yaml["file_list"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800291
edison.ai7b299f52020-07-16 15:44:18 +0800292 print("Start to generate file from the generated list:")
Kevin Peng655f2392019-11-27 16:33:02 +0800293 for file in file_list:
Raef Coles558487a2020-10-29 13:09:44 +0000294 # Replace environment variables in the output filepath
Ken Liu861b0782021-05-22 13:15:08 +0800295 manifest_out_file = os.path.expandvars(file["output"])
Raef Coles558487a2020-10-29 13:09:44 +0000296 # Replace environment variables in the template filepath
Kevin Peng1ec5e7c2019-11-29 10:52:00 +0800297 templatefile_name = os.path.expandvars(file["template"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800298
Kevin Peng655f2392019-11-27 16:33:02 +0800299 if OUT_DIR is not None:
Ken Liu861b0782021-05-22 13:15:08 +0800300 manifest_out_file = os.path.join(OUT_DIR, manifest_out_file)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800301
Ken Liu861b0782021-05-22 13:15:08 +0800302 print ("Generating " + manifest_out_file)
edison.ai7b299f52020-07-16 15:44:18 +0800303
Ken Liu861b0782021-05-22 13:15:08 +0800304 outfile_path = os.path.dirname(manifest_out_file)
Kevin Peng655f2392019-11-27 16:33:02 +0800305 if not os.path.exists(outfile_path):
306 os.makedirs(outfile_path)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800307
Kevin Peng655f2392019-11-27 16:33:02 +0800308 template = ENV.get_template(templatefile_name)
Edison Ai6e3f2a32019-06-11 15:29:05 +0800309
Ken Liu861b0782021-05-22 13:15:08 +0800310 outfile = io.open(manifest_out_file, "w", newline=None)
Kevin Peng655f2392019-11-27 16:33:02 +0800311 outfile.write(template.render(context))
312 outfile.close()
Edison Ai48b2d9e2019-06-24 14:39:45 +0800313
Kevin Peng655f2392019-11-27 16:33:02 +0800314 print ("Generation of files done")
Edison Ai48b2d9e2019-06-24 14:39:45 +0800315
Ken Liu861b0782021-05-22 13:15:08 +0800316def process_stateless_services(partitions, stateless_index_max_num):
Mingyang Suna1ca6112021-01-11 11:34:59 +0800317 """
318 This function collects all stateless services together, and allocates
Mingyang Sun4ecea992021-03-30 17:56:26 +0800319 stateless handles for them.
Kevin Pengc05319d2021-04-22 22:59:35 +0800320 Valid stateless handle in service will be converted to an index. If the
321 stateless handle is set as "auto", or not set, framework will allocate a
322 valid index for the service.
323 Framework puts each service into a reordered stateless service list at
324 position of "index". Other unused positions are left None.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800325 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800326 collected_stateless_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800327
328 # Collect all stateless services first.
329 for partition in partitions:
330 # Skip the FF-M 1.0 partitions
331 if partition['manifest']['psa_framework_version'] < 1.1:
332 continue
Mingyang Suna1ca6112021-01-11 11:34:59 +0800333 for service in partition['manifest']['services']:
334 if 'connection_based' not in service:
335 raise Exception("'connection_based' is mandatory in FF-M 1.1 service!")
336 if service['connection_based'] is False:
Kevin Pengc05319d2021-04-22 22:59:35 +0800337 collected_stateless_services.append(service)
Mingyang Suna1ca6112021-01-11 11:34:59 +0800338
Kevin Pengc05319d2021-04-22 22:59:35 +0800339 if len(collected_stateless_services) == 0:
Mingyang Suna1ca6112021-01-11 11:34:59 +0800340 return []
341
Ken Liu861b0782021-05-22 13:15:08 +0800342 if len(collected_stateless_services) > stateless_index_max_num:
343 raise Exception("Stateless service numbers range exceed {number}.".format(number=stateless_index_max_num))
Mingyang Suna1ca6112021-01-11 11:34:59 +0800344
345 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800346 Allocate an empty stateless service list to store services.
347 Use "handle - 1" as the index for service, since handle value starts from
348 1 and list index starts from 0.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800349 """
Ken Liu861b0782021-05-22 13:15:08 +0800350 reordered_stateless_services = [None] * stateless_index_max_num
Kevin Pengc05319d2021-04-22 22:59:35 +0800351 auto_alloc_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800352
Kevin Pengc05319d2021-04-22 22:59:35 +0800353 for service in collected_stateless_services:
354 # If not set, it is "auto" by default
355 if 'stateless_handle' not in service:
356 auto_alloc_services.append(service)
357 continue
358
Mingyang Sun4ecea992021-03-30 17:56:26 +0800359 service_handle = service['stateless_handle']
Mingyang Suna1ca6112021-01-11 11:34:59 +0800360
Mingyang Sun4ecea992021-03-30 17:56:26 +0800361 # Fill in service list with specified stateless handle, otherwise skip
362 if isinstance(service_handle, int):
Ken Liu861b0782021-05-22 13:15:08 +0800363 if service_handle < 1 or service_handle > stateless_index_max_num:
Kevin Pengc05319d2021-04-22 22:59:35 +0800364 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800365 # Convert handle index to reordered service list index
366 service_handle = service_handle - 1
367
368 if reordered_stateless_services[service_handle] is not None:
Kevin Pengc05319d2021-04-22 22:59:35 +0800369 raise Exception("Duplicated stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800370 reordered_stateless_services[service_handle] = service
Kevin Pengc05319d2021-04-22 22:59:35 +0800371 elif service_handle == 'auto':
372 auto_alloc_services.append(service)
373 else:
374 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800375
376 # Auto-allocate stateless handle and encode the stateless handle
Ken Liu861b0782021-05-22 13:15:08 +0800377 for i in range(0, stateless_index_max_num):
Mingyang Sun4ecea992021-03-30 17:56:26 +0800378 service = reordered_stateless_services[i]
379
Kevin Pengc05319d2021-04-22 22:59:35 +0800380 if service == None and len(auto_alloc_services) > 0:
381 service = auto_alloc_services.pop(0)
Mingyang Sun4ecea992021-03-30 17:56:26 +0800382
Mingyang Sun453ad402021-03-17 17:58:33 +0800383 """
384 Encode stateless flag and version into stateless handle
385 bit 30: stateless handle indicator
386 bit 15-8: stateless service version
387 bit 7-0: stateless handle index
388 """
Mingyang Sun4ecea992021-03-30 17:56:26 +0800389 stateless_handle_value = 0
390 if service != None:
391 stateless_index = (i & 0xFF)
392 stateless_handle_value |= stateless_index
Mingyang Sun453ad402021-03-17 17:58:33 +0800393 stateless_flag = 1 << 30
394 stateless_handle_value |= stateless_flag
Mingyang Sun4ecea992021-03-30 17:56:26 +0800395 stateless_version = (service['version'] & 0xFF) << 8
Mingyang Sun453ad402021-03-17 17:58:33 +0800396 stateless_handle_value |= stateless_version
Mingyang Sun4ecea992021-03-30 17:56:26 +0800397 service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value)
Ken Liu861b0782021-05-22 13:15:08 +0800398 service['stateless_handle_index'] = stateless_index
Mingyang Suna1ca6112021-01-11 11:34:59 +0800399
Mingyang Sun4ecea992021-03-30 17:56:26 +0800400 reordered_stateless_services[i] = service
401
402 return reordered_stateless_services
Mingyang Suna1ca6112021-01-11 11:34:59 +0800403
Kevin Peng655f2392019-11-27 16:33:02 +0800404def parse_args():
Raef Coles558487a2020-10-29 13:09:44 +0000405 parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list',
406 epilog='Note that environment variables in template files will be replaced with their values')
407
Kevin Peng655f2392019-11-27 16:33:02 +0800408 parser.add_argument('-o', '--outdir'
409 , dest='outdir'
410 , required=False
411 , default=None
412 , metavar='out_dir'
413 , help='The root directory for generated files, the default is TF-M root folder.')
Shawn Shana9ad1e02019-08-07 15:49:48 +0800414
Kevin Peng655f2392019-11-27 16:33:02 +0800415 parser.add_argument('-m', '--manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100416 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800417 , dest='manifest_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100418 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800419 , metavar='manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100420 , help='A set of secure partition manifest lists to parse')
Kevin Peng655f2392019-11-27 16:33:02 +0800421
422 parser.add_argument('-f', '--file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100423 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800424 , dest='gen_file_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100425 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800426 , metavar='file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100427 , help='These files descripe the file list to generate')
Kevin Peng655f2392019-11-27 16:33:02 +0800428
David Hub2694202021-07-15 14:58:39 +0800429 parser.add_argument('-e', '--extra-manifest'
430 , nargs='*'
431 , dest='extra_manifests_args'
432 , required=False
433 , default=None
434 , metavar='out-of-tree-manifest-list'
435 , help='Optional. Manifest lists and original paths for out-of-tree secure partitions.')
436
Kevin Peng655f2392019-11-27 16:33:02 +0800437 args = parser.parse_args()
438 manifest_args = args.manifest_args
439 gen_file_args = args.gen_file_args
440
Kevin Peng655f2392019-11-27 16:33:02 +0800441 return args
442
443ENV = Environment(
444 loader = TemplateLoader(),
445 autoescape = select_autoescape(['html', 'xml']),
446 lstrip_blocks = True,
447 trim_blocks = True,
448 keep_trailing_newline = True
449 )
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100450
Miklos Balint470919c2018-05-22 17:51:29 +0200451def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100452 """
453 The entry point of the script.
454
455 Generates the output files based on the templates and the manifests.
456 """
Shawn Shana9ad1e02019-08-07 15:49:48 +0800457
Kevin Peng655f2392019-11-27 16:33:02 +0800458 global OUT_DIR
Shawn Shana9ad1e02019-08-07 15:49:48 +0800459
Kevin Peng655f2392019-11-27 16:33:02 +0800460 args = parse_args()
Shawn Shana9ad1e02019-08-07 15:49:48 +0800461
Kevin Peng655f2392019-11-27 16:33:02 +0800462 manifest_args = args.manifest_args
463 gen_file_args = args.gen_file_args
David Hub2694202021-07-15 14:58:39 +0800464 extra_manifests_args = args.extra_manifests_args
Kevin Peng655f2392019-11-27 16:33:02 +0800465 OUT_DIR = args.outdir
Kevin Peng655f2392019-11-27 16:33:02 +0800466
Raef Coles558487a2020-10-29 13:09:44 +0000467 manifest_list = [os.path.abspath(x) for x in args.manifest_args]
468 gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]
Shawn Shana9ad1e02019-08-07 15:49:48 +0800469
David Hub2694202021-07-15 14:58:39 +0800470 if extra_manifests_args is not None:
471 extra_manifests_list = [os.path.abspath(x) for x in extra_manifests_args]
472 else:
473 extra_manifests_list = None
474
Shawn Shana9ad1e02019-08-07 15:49:48 +0800475 """
Kevin Peng655f2392019-11-27 16:33:02 +0800476 Relative path to TF-M root folder is supported in the manifests
477 and default value of manifest list and generated file list are relative to TF-M root folder as well,
478 so first change directory to TF-M root folder.
Shawn Shana9ad1e02019-08-07 15:49:48 +0800479 By doing this, the script can be executed anywhere
Kevin Peng655f2392019-11-27 16:33:02 +0800480 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 +0800481 """
482 os.chdir(os.path.join(sys.path[0], ".."))
483
David Hub2694202021-07-15 14:58:39 +0800484 partition_list = process_partition_manifests(manifest_list, extra_manifests_list)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100485
Edison Ai6e3f2a32019-06-11 15:29:05 +0800486 utilities = {}
Mingyang Suna1ca6112021-01-11 11:34:59 +0800487 utilities['donotedit_warning'] = donotedit_warning
Miklos Balint470919c2018-05-22 17:51:29 +0200488
Ken Liu861b0782021-05-22 13:15:08 +0800489 context = {}
490 context['partitions'] = partition_list
Kevin Peng655f2392019-11-27 16:33:02 +0800491 context['utilities'] = utilities
Ken Liu861b0782021-05-22 13:15:08 +0800492 context['stateless_services'] = process_stateless_services(partition_list, 32)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100493
Ken Liu861b0782021-05-22 13:15:08 +0800494 gen_per_partition_files(context)
495 gen_summary_files(context, gen_file_list)
Miklos Balint470919c2018-05-22 17:51:29 +0200496
497if __name__ == "__main__":
498 main()