blob: 9f4906791a75c5f4a78066594deeb89a69c027b8 [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
Mate Toth-Pal36f21842018-11-08 16:12:51 +010028class TemplateLoader(BaseLoader):
29 """
30 Template loader class.
Miklos Balint470919c2018-05-22 17:51:29 +020031
Mate Toth-Pal36f21842018-11-08 16:12:51 +010032 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 Balint470919c2018-05-22 17:51:29 +020037
Mate Toth-Pal36f21842018-11-08 16:12:51 +010038 def get_source(self, environment, template):
39 """
40 This function reads the template files.
41 For detailed documentation see:
42 http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source
43
44 Please note that this function always return 'false' as 'uptodate'
45 value, so the output file will always be generated.
46 """
47 if not os.path.isfile(template):
48 raise TemplateNotFound(template)
49 with open(template) as f:
50 source = f.read()
51 return source, template, False
52
Mingyang Sun294ce2e2021-06-11 11:58:24 +080053def manifest_validation(partition_manifest):
54 """
55 This function validates FF-M compliance for partition manifest, and sets
56 default values for optional attributes.
57 More validation items will be added.
58 """
59 # Service FF-M manifest validation
60 if 'services' not in partition_manifest.keys():
61 return partition_manifest
62
63 for service in partition_manifest['services']:
64 if 'version' not in service.keys():
65 service['version'] = 1
66 if 'version_policy' not in service.keys():
67 service['version_policy'] = "STRICT"
68
69 return partition_manifest
70
David Hub2694202021-07-15 14:58:39 +080071def process_partition_manifests(manifest_list_files, extra_manifests_list):
Mate Toth-Pal36f21842018-11-08 16:12:51 +010072 """
Kevin Peng655f2392019-11-27 16:33:02 +080073 Parse the input manifest, generate the data base for genereated files
74 and generate manifest header files.
Mate Toth-Pal36f21842018-11-08 16:12:51 +010075
76 Parameters
77 ----------
Raef Colesf42f0882020-07-10 10:01:58 +010078 manifest_list_files:
79 The manifest lists to parse.
David Hub2694202021-07-15 14:58:39 +080080 extra_manifests_list:
81 The extra manifest list to parse and its original path.
Mate Toth-Pal36f21842018-11-08 16:12:51 +010082
83 Returns
84 -------
Kevin Peng578a8492020-12-31 10:22:59 +080085 The partition data base.
Edison Ai48b2d9e2019-06-24 14:39:45 +080086 """
Kevin Peng655f2392019-11-27 16:33:02 +080087
Ken Liu861b0782021-05-22 13:15:08 +080088 partition_list = []
Kevin Peng655f2392019-11-27 16:33:02 +080089 manifest_list = []
90
Raef Colesf42f0882020-07-10 10:01:58 +010091 for f in manifest_list_files:
92 with open(f) as manifest_list_yaml_file:
93 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
Kevin Peng655f2392019-11-27 16:33:02 +080094 manifest_list.extend(manifest_dic["manifest_list"])
Ken Liu861b0782021-05-22 13:15:08 +080095 manifest_list_yaml_file.close()
Kevin Peng655f2392019-11-27 16:33:02 +080096
David Hub2694202021-07-15 14:58:39 +080097 # Out-of-tree secure partition build
98 if extra_manifests_list is not None:
99 for i, item in enumerate(extra_manifests_list):
100 # Skip if current item is the original manifest path
101 if os.path.isdir(item):
102 continue
103
104 # The manifest list file generated by configure_file()
105 with open(item) as manifest_list_yaml_file:
106 manifest_dic = yaml.safe_load(manifest_list_yaml_file)
107 extra_manifest_dic = manifest_dic["manifest_list"]
108 for dict in extra_manifest_dic:
109 # Append original directory of out-of-tree partition's
110 # manifest list source code
111 dict['extra_path'] = extra_manifests_list[i + 1]
112 manifest_list.append(dict)
113 manifest_list_yaml_file.close()
114
Xinyu Zhang19504a52021-03-31 16:26:20 +0800115 pid_list = []
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800116 no_pid_manifest_idx = []
117 for i, manifest_item in enumerate(manifest_list):
118 # Check if partition ID is manually set
119 if 'pid' not in manifest_item.keys():
120 no_pid_manifest_idx.append(i)
121 continue
Xinyu Zhang19504a52021-03-31 16:26:20 +0800122 # Check if partition ID is duplicated
123 if manifest_item['pid'] in pid_list:
124 raise Exception("PID No. {pid} has already been used!".format(pid=manifest_item['pid']))
125 pid_list.append(manifest_item['pid'])
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800126 # Automatically generate PIDs for partitions without PID
127 pid = 256
128 for idx in no_pid_manifest_idx:
129 while pid in pid_list:
130 pid += 1
131 manifest_list[idx]['pid'] = pid
132 pid_list.append(pid)
Xinyu Zhang19504a52021-03-31 16:26:20 +0800133
Xinyu Zhangc46ee1f2021-04-01 10:10:43 +0800134 for manifest_item in manifest_list:
Raef Coles558487a2020-10-29 13:09:44 +0000135 # Replace environment variables in the manifest path
Kevin Peng655f2392019-11-27 16:33:02 +0800136 manifest_path = os.path.expandvars(manifest_item['manifest'])
David Hub2694202021-07-15 14:58:39 +0800137
138 # Handle out-of-tree secure partition manifest file path
139 if 'extra_path' in manifest_item:
140 if not os.path.isabs(manifest_path):
141 # manifest file path provided by manifest list is relative to
142 # manifest list path
143 manifest_path = os.path.join(manifest_item['extra_path'], manifest_path).replace('\\', '/')
144
Kevin Peng655f2392019-11-27 16:33:02 +0800145 file = open(manifest_path)
Mingyang Sun294ce2e2021-06-11 11:58:24 +0800146 manifest = manifest_validation(yaml.safe_load(file))
Ken Liu861b0782021-05-22 13:15:08 +0800147 file.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800148
149 manifest_dir, manifest_name = os.path.split(manifest_path)
Ken Liu861b0782021-05-22 13:15:08 +0800150 manifest_out_basename = manifest_name.replace('.yaml', '').replace('.json', '')
Kevin Peng655f2392019-11-27 16:33:02 +0800151
Kevin Peng655f2392019-11-27 16:33:02 +0800152 if OUT_DIR is not None:
David Hub2694202021-07-15 14:58:39 +0800153 if 'output_path' in manifest_item:
154 # Build up generated files directory accroding to the relative
155 # path specified in output_path by the partition
156 output_path = os.path.expandvars(manifest_item['output_path'])
157 manifest_head_file = os.path.join(output_path, "psa_manifest", manifest_out_basename + '.h')
158 intermedia_file = os.path.join(output_path, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
159 load_info_file = os.path.join(output_path, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
160 else:
161 manifest_head_file = os.path.join(manifest_dir, "psa_manifest", manifest_out_basename + '.h')
162 intermedia_file = os.path.join(manifest_dir, "auto_generated", 'intermedia_' + manifest_out_basename + '.c')
163 load_info_file = os.path.join(manifest_dir, "auto_generated", 'load_info_' + manifest_out_basename + '.c')
164
165 """
166 Remove the `source_path` portion of the filepaths, so that it can be
167 interpreted as a relative path from the OUT_DIR.
168 """
169 if 'source_path' in manifest_item:
170 # Replace environment variables in the source path
171 source_path = os.path.expandvars(manifest_item['source_path'])
172 manifest_head_file = os.path.relpath(manifest_head_file, start = source_path)
173 intermedia_file = os.path.relpath(intermedia_file, start = source_path)
174 load_info_file = os.path.relpath(load_info_file, start = source_path)
MartinaHanusovaNXP35957f12021-07-14 15:30:15 +0200175
176 manifest_head_file = os.path.join(OUT_DIR, manifest_head_file).replace('\\', '/')
177 intermedia_file = os.path.join(OUT_DIR, intermedia_file).replace('\\', '/')
178 load_info_file = os.path.join(OUT_DIR, load_info_file).replace('\\', '/')
Kevin Peng655f2392019-11-27 16:33:02 +0800179
Ken Liu861b0782021-05-22 13:15:08 +0800180 partition_list.append({"manifest": manifest, "attr": manifest_item,
181 "manifest_out_basename": manifest_out_basename,
182 "header_file": manifest_head_file,
183 "intermedia_file": intermedia_file,
184 "loadinfo_file": load_info_file})
185
186 return partition_list
187
188def gen_per_partition_files(context):
189 """
190 Generate per-partition files
191
192 Parameters
193 ----------
194 context:
195 context contains partition infos
196 """
197
198 subutilities = {}
199 subutilities['donotedit_warning'] = donotedit_warning
200
201 subcontext = {}
202 subcontext['utilities'] = subutilities
203
Ken Liu72c031e2021-08-09 16:42:54 +0800204 manifesttemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/manifestfilename.template'))
205 memorytemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/partition_intermedia.template'))
206 infotemplate = ENV.get_template(os.path.join(os.path.relpath(os.path.dirname(__file__)), 'templates/partition_load_info.template'))
Ken Liu861b0782021-05-22 13:15:08 +0800207
208 print ("Start to generate partition files:")
209
210 for one_partition in context['partitions']:
211 subcontext['manifest'] = one_partition['manifest']
212 subcontext['attr'] = one_partition['attr']
213 subcontext['manifest_out_basename'] = one_partition['manifest_out_basename']
214
215 print ("Generating Header: " + one_partition['header_file'])
216 outfile_path = os.path.dirname(one_partition['header_file'])
Kevin Peng655f2392019-11-27 16:33:02 +0800217 if not os.path.exists(outfile_path):
218 os.makedirs(outfile_path)
219
Ken Liu861b0782021-05-22 13:15:08 +0800220 headerfile = io.open(one_partition['header_file'], "w", newline=None)
221 headerfile.write(manifesttemplate.render(subcontext))
222 headerfile.close()
Kevin Peng655f2392019-11-27 16:33:02 +0800223
Ken Liu861b0782021-05-22 13:15:08 +0800224 print ("Generating Intermedia: " + one_partition['intermedia_file'])
225 intermediafile_path = os.path.dirname(one_partition['intermedia_file'])
Mingyang Sund20999f2020-10-15 14:53:12 +0800226 if not os.path.exists(intermediafile_path):
227 os.makedirs(intermediafile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800228 intermediafile = io.open(one_partition['intermedia_file'], "w", newline=None)
229 intermediafile.write(memorytemplate.render(subcontext))
230 intermediafile.close()
Mingyang Sund20999f2020-10-15 14:53:12 +0800231
Ken Liu861b0782021-05-22 13:15:08 +0800232 print ("Generating Loadinfo: " + one_partition['loadinfo_file'])
233 infofile_path = os.path.dirname(one_partition['loadinfo_file'])
Mingyang Sunf6a78572021-04-02 16:51:05 +0800234 if not os.path.exists(infofile_path):
235 os.makedirs(infofile_path)
Ken Liu861b0782021-05-22 13:15:08 +0800236 infooutfile = io.open(one_partition['loadinfo_file'], "w", newline=None)
237 infooutfile.write(infotemplate.render(subcontext))
238 infooutfile.close()
Mingyang Sunf6a78572021-04-02 16:51:05 +0800239
Ken Liu861b0782021-05-22 13:15:08 +0800240 print ("Per-partition files done:")
Mingyang Sunf6a78572021-04-02 16:51:05 +0800241
Ken Liu861b0782021-05-22 13:15:08 +0800242def gen_summary_files(context, gen_file_lists):
Kevin Peng655f2392019-11-27 16:33:02 +0800243 """
244 Generate files according to the gen_file_list
Edison Ai48b2d9e2019-06-24 14:39:45 +0800245
246 Parameters
247 ----------
Raef Colesf42f0882020-07-10 10:01:58 +0100248 gen_file_lists:
249 The lists of files to generate
Edison Ai48b2d9e2019-06-24 14:39:45 +0800250 """
Kevin Peng655f2392019-11-27 16:33:02 +0800251 file_list = []
Shawn Shana9ad1e02019-08-07 15:49:48 +0800252
Raef Colesf42f0882020-07-10 10:01:58 +0100253 for f in gen_file_lists:
254 with open(f) as file_list_yaml_file:
Kevin Peng655f2392019-11-27 16:33:02 +0800255 file_list_yaml = yaml.safe_load(file_list_yaml_file)
256 file_list.extend(file_list_yaml["file_list"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800257
edison.ai7b299f52020-07-16 15:44:18 +0800258 print("Start to generate file from the generated list:")
Kevin Peng655f2392019-11-27 16:33:02 +0800259 for file in file_list:
Raef Coles558487a2020-10-29 13:09:44 +0000260 # Replace environment variables in the output filepath
Ken Liu861b0782021-05-22 13:15:08 +0800261 manifest_out_file = os.path.expandvars(file["output"])
Raef Coles558487a2020-10-29 13:09:44 +0000262 # Replace environment variables in the template filepath
Kevin Peng1ec5e7c2019-11-29 10:52:00 +0800263 templatefile_name = os.path.expandvars(file["template"])
Edison Ai48b2d9e2019-06-24 14:39:45 +0800264
Kevin Peng655f2392019-11-27 16:33:02 +0800265 if OUT_DIR is not None:
Ken Liu861b0782021-05-22 13:15:08 +0800266 manifest_out_file = os.path.join(OUT_DIR, manifest_out_file)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800267
Ken Liu861b0782021-05-22 13:15:08 +0800268 print ("Generating " + manifest_out_file)
edison.ai7b299f52020-07-16 15:44:18 +0800269
Ken Liu861b0782021-05-22 13:15:08 +0800270 outfile_path = os.path.dirname(manifest_out_file)
Kevin Peng655f2392019-11-27 16:33:02 +0800271 if not os.path.exists(outfile_path):
272 os.makedirs(outfile_path)
Edison Ai48b2d9e2019-06-24 14:39:45 +0800273
Kevin Peng655f2392019-11-27 16:33:02 +0800274 template = ENV.get_template(templatefile_name)
Edison Ai6e3f2a32019-06-11 15:29:05 +0800275
Ken Liu861b0782021-05-22 13:15:08 +0800276 outfile = io.open(manifest_out_file, "w", newline=None)
Kevin Peng655f2392019-11-27 16:33:02 +0800277 outfile.write(template.render(context))
278 outfile.close()
Edison Ai48b2d9e2019-06-24 14:39:45 +0800279
Kevin Peng655f2392019-11-27 16:33:02 +0800280 print ("Generation of files done")
Edison Ai48b2d9e2019-06-24 14:39:45 +0800281
Ken Liu861b0782021-05-22 13:15:08 +0800282def process_stateless_services(partitions, stateless_index_max_num):
Mingyang Suna1ca6112021-01-11 11:34:59 +0800283 """
284 This function collects all stateless services together, and allocates
Mingyang Sun4ecea992021-03-30 17:56:26 +0800285 stateless handles for them.
Kevin Pengc05319d2021-04-22 22:59:35 +0800286 Valid stateless handle in service will be converted to an index. If the
287 stateless handle is set as "auto", or not set, framework will allocate a
288 valid index for the service.
289 Framework puts each service into a reordered stateless service list at
290 position of "index". Other unused positions are left None.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800291 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800292 collected_stateless_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800293
294 # Collect all stateless services first.
295 for partition in partitions:
296 # Skip the FF-M 1.0 partitions
297 if partition['manifest']['psa_framework_version'] < 1.1:
298 continue
Mingyang Suna1ca6112021-01-11 11:34:59 +0800299 for service in partition['manifest']['services']:
300 if 'connection_based' not in service:
301 raise Exception("'connection_based' is mandatory in FF-M 1.1 service!")
302 if service['connection_based'] is False:
Kevin Pengc05319d2021-04-22 22:59:35 +0800303 collected_stateless_services.append(service)
Mingyang Suna1ca6112021-01-11 11:34:59 +0800304
Kevin Pengc05319d2021-04-22 22:59:35 +0800305 if len(collected_stateless_services) == 0:
Mingyang Suna1ca6112021-01-11 11:34:59 +0800306 return []
307
Ken Liu861b0782021-05-22 13:15:08 +0800308 if len(collected_stateless_services) > stateless_index_max_num:
309 raise Exception("Stateless service numbers range exceed {number}.".format(number=stateless_index_max_num))
Mingyang Suna1ca6112021-01-11 11:34:59 +0800310
311 """
Kevin Pengc05319d2021-04-22 22:59:35 +0800312 Allocate an empty stateless service list to store services.
313 Use "handle - 1" as the index for service, since handle value starts from
314 1 and list index starts from 0.
Mingyang Suna1ca6112021-01-11 11:34:59 +0800315 """
Ken Liu861b0782021-05-22 13:15:08 +0800316 reordered_stateless_services = [None] * stateless_index_max_num
Kevin Pengc05319d2021-04-22 22:59:35 +0800317 auto_alloc_services = []
Mingyang Suna1ca6112021-01-11 11:34:59 +0800318
Kevin Pengc05319d2021-04-22 22:59:35 +0800319 for service in collected_stateless_services:
320 # If not set, it is "auto" by default
321 if 'stateless_handle' not in service:
322 auto_alloc_services.append(service)
323 continue
324
Mingyang Sun4ecea992021-03-30 17:56:26 +0800325 service_handle = service['stateless_handle']
Mingyang Suna1ca6112021-01-11 11:34:59 +0800326
Mingyang Sun4ecea992021-03-30 17:56:26 +0800327 # Fill in service list with specified stateless handle, otherwise skip
328 if isinstance(service_handle, int):
Ken Liu861b0782021-05-22 13:15:08 +0800329 if service_handle < 1 or service_handle > stateless_index_max_num:
Kevin Pengc05319d2021-04-22 22:59:35 +0800330 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800331 # Convert handle index to reordered service list index
332 service_handle = service_handle - 1
333
334 if reordered_stateless_services[service_handle] is not None:
Kevin Pengc05319d2021-04-22 22:59:35 +0800335 raise Exception("Duplicated stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800336 reordered_stateless_services[service_handle] = service
Kevin Pengc05319d2021-04-22 22:59:35 +0800337 elif service_handle == 'auto':
338 auto_alloc_services.append(service)
339 else:
340 raise Exception("Invalid stateless_handle setting: {handle}.".format(handle=service['stateless_handle']))
Mingyang Sun4ecea992021-03-30 17:56:26 +0800341
342 # Auto-allocate stateless handle and encode the stateless handle
Ken Liu861b0782021-05-22 13:15:08 +0800343 for i in range(0, stateless_index_max_num):
Mingyang Sun4ecea992021-03-30 17:56:26 +0800344 service = reordered_stateless_services[i]
345
Kevin Pengc05319d2021-04-22 22:59:35 +0800346 if service == None and len(auto_alloc_services) > 0:
347 service = auto_alloc_services.pop(0)
Mingyang Sun4ecea992021-03-30 17:56:26 +0800348
Mingyang Sun453ad402021-03-17 17:58:33 +0800349 """
350 Encode stateless flag and version into stateless handle
351 bit 30: stateless handle indicator
352 bit 15-8: stateless service version
353 bit 7-0: stateless handle index
354 """
Mingyang Sun4ecea992021-03-30 17:56:26 +0800355 stateless_handle_value = 0
356 if service != None:
357 stateless_index = (i & 0xFF)
358 stateless_handle_value |= stateless_index
Mingyang Sun453ad402021-03-17 17:58:33 +0800359 stateless_flag = 1 << 30
360 stateless_handle_value |= stateless_flag
Mingyang Sun4ecea992021-03-30 17:56:26 +0800361 stateless_version = (service['version'] & 0xFF) << 8
Mingyang Sun453ad402021-03-17 17:58:33 +0800362 stateless_handle_value |= stateless_version
Mingyang Sun4ecea992021-03-30 17:56:26 +0800363 service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value)
Ken Liu861b0782021-05-22 13:15:08 +0800364 service['stateless_handle_index'] = stateless_index
Mingyang Suna1ca6112021-01-11 11:34:59 +0800365
Mingyang Sun4ecea992021-03-30 17:56:26 +0800366 reordered_stateless_services[i] = service
367
368 return reordered_stateless_services
Mingyang Suna1ca6112021-01-11 11:34:59 +0800369
Kevin Peng655f2392019-11-27 16:33:02 +0800370def parse_args():
Raef Coles558487a2020-10-29 13:09:44 +0000371 parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list',
372 epilog='Note that environment variables in template files will be replaced with their values')
373
Kevin Peng655f2392019-11-27 16:33:02 +0800374 parser.add_argument('-o', '--outdir'
375 , dest='outdir'
376 , required=False
377 , default=None
378 , metavar='out_dir'
379 , help='The root directory for generated files, the default is TF-M root folder.')
Shawn Shana9ad1e02019-08-07 15:49:48 +0800380
Kevin Peng655f2392019-11-27 16:33:02 +0800381 parser.add_argument('-m', '--manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100382 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800383 , dest='manifest_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100384 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800385 , metavar='manifest'
Raef Colesf42f0882020-07-10 10:01:58 +0100386 , help='A set of secure partition manifest lists to parse')
Kevin Peng655f2392019-11-27 16:33:02 +0800387
388 parser.add_argument('-f', '--file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100389 , nargs='+'
Kevin Peng655f2392019-11-27 16:33:02 +0800390 , dest='gen_file_args'
Raef Colesf42f0882020-07-10 10:01:58 +0100391 , required=True
Kevin Peng655f2392019-11-27 16:33:02 +0800392 , metavar='file-list'
Raef Colesf42f0882020-07-10 10:01:58 +0100393 , help='These files descripe the file list to generate')
Kevin Peng655f2392019-11-27 16:33:02 +0800394
David Hub2694202021-07-15 14:58:39 +0800395 parser.add_argument('-e', '--extra-manifest'
396 , nargs='*'
397 , dest='extra_manifests_args'
398 , required=False
399 , default=None
400 , metavar='out-of-tree-manifest-list'
401 , help='Optional. Manifest lists and original paths for out-of-tree secure partitions.')
402
Kevin Peng655f2392019-11-27 16:33:02 +0800403 args = parser.parse_args()
404 manifest_args = args.manifest_args
405 gen_file_args = args.gen_file_args
406
Kevin Peng655f2392019-11-27 16:33:02 +0800407 return args
408
409ENV = Environment(
410 loader = TemplateLoader(),
411 autoescape = select_autoescape(['html', 'xml']),
412 lstrip_blocks = True,
413 trim_blocks = True,
414 keep_trailing_newline = True
415 )
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100416
Miklos Balint470919c2018-05-22 17:51:29 +0200417def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100418 """
419 The entry point of the script.
420
421 Generates the output files based on the templates and the manifests.
422 """
Shawn Shana9ad1e02019-08-07 15:49:48 +0800423
Kevin Peng655f2392019-11-27 16:33:02 +0800424 global OUT_DIR
Shawn Shana9ad1e02019-08-07 15:49:48 +0800425
Kevin Peng655f2392019-11-27 16:33:02 +0800426 args = parse_args()
Shawn Shana9ad1e02019-08-07 15:49:48 +0800427
Kevin Peng655f2392019-11-27 16:33:02 +0800428 manifest_args = args.manifest_args
429 gen_file_args = args.gen_file_args
David Hub2694202021-07-15 14:58:39 +0800430 extra_manifests_args = args.extra_manifests_args
Kevin Peng655f2392019-11-27 16:33:02 +0800431 OUT_DIR = args.outdir
Kevin Peng655f2392019-11-27 16:33:02 +0800432
Raef Coles558487a2020-10-29 13:09:44 +0000433 manifest_list = [os.path.abspath(x) for x in args.manifest_args]
434 gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]
Shawn Shana9ad1e02019-08-07 15:49:48 +0800435
David Hub2694202021-07-15 14:58:39 +0800436 if extra_manifests_args is not None:
437 extra_manifests_list = [os.path.abspath(x) for x in extra_manifests_args]
438 else:
439 extra_manifests_list = None
440
Shawn Shana9ad1e02019-08-07 15:49:48 +0800441 """
Kevin Peng655f2392019-11-27 16:33:02 +0800442 Relative path to TF-M root folder is supported in the manifests
443 and default value of manifest list and generated file list are relative to TF-M root folder as well,
444 so first change directory to TF-M root folder.
Shawn Shana9ad1e02019-08-07 15:49:48 +0800445 By doing this, the script can be executed anywhere
Kevin Peng655f2392019-11-27 16:33:02 +0800446 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 +0800447 """
448 os.chdir(os.path.join(sys.path[0], ".."))
449
David Hub2694202021-07-15 14:58:39 +0800450 partition_list = process_partition_manifests(manifest_list, extra_manifests_list)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100451
Edison Ai6e3f2a32019-06-11 15:29:05 +0800452 utilities = {}
Mingyang Suna1ca6112021-01-11 11:34:59 +0800453 utilities['donotedit_warning'] = donotedit_warning
Miklos Balint470919c2018-05-22 17:51:29 +0200454
Ken Liu861b0782021-05-22 13:15:08 +0800455 context = {}
456 context['partitions'] = partition_list
Kevin Peng655f2392019-11-27 16:33:02 +0800457 context['utilities'] = utilities
Ken Liu861b0782021-05-22 13:15:08 +0800458 context['stateless_services'] = process_stateless_services(partition_list, 32)
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100459
Ken Liu861b0782021-05-22 13:15:08 +0800460 gen_per_partition_files(context)
461 gen_summary_files(context, gen_file_list)
Miklos Balint470919c2018-05-22 17:51:29 +0200462
463if __name__ == "__main__":
464 main()