aboutsummaryrefslogtreecommitdiff
path: root/tools/tfm_parse_manifest_list.py
blob: d54ebdd8c5477a462684066b1938f18f2fdf0bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#-------------------------------------------------------------------------------
# Copyright (c) 2018-2021, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------

import os
import io
import sys
import argparse
from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound

try:
    import yaml
except ImportError as e:
    print (str(e) + " To install it, type:")
    print ("pip install PyYAML")
    exit(1)

donotedit_warning = \
                    "/*********** " + \
                    "WARNING: This is an auto-generated file. Do not edit!" + \
                    " ***********/"

OUT_DIR = None # The root directory that files are generated to

class TemplateLoader(BaseLoader):
    """
    Template loader class.

    An instance of this class is passed to the template engine. It is
    responsible for reading the template file
    """
    def __init__(self):
        pass

    def get_source(self, environment, template):
        """
        This function reads the template files.
        For detailed documentation see:
        http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source

        Please note that this function always return 'false' as 'uptodate'
        value, so the output file will always be generated.
        """
        if not os.path.isfile(template):
            raise TemplateNotFound(template)
        with open(template) as f:
            source = f.read()
        return source, template, False

def process_manifest(manifest_list_files):
    """
    Parse the input manifest, generate the data base for genereated files
    and generate manifest header files.

    Parameters
    ----------
    manifest_list_files:
        The manifest lists to parse.

    Returns
    -------
    The partition data base.
    """

    partition_db = []
    manifest_list = []

    for f in manifest_list_files:
        with open(f) as manifest_list_yaml_file:
            manifest_dic = yaml.safe_load(manifest_list_yaml_file)
            manifest_list.extend(manifest_dic["manifest_list"])

    manifesttemplate = ENV.get_template('secure_fw/partitions/manifestfilename.template')
    memorytemplate = ENV.get_template('secure_fw/partitions/partition_intermedia.template')

    pid_list = []
    no_pid_manifest_idx = []
    for i, manifest_item in enumerate(manifest_list):
        # Check if partition ID is manually set
        if 'pid' not in manifest_item.keys():
            no_pid_manifest_idx.append(i)
            continue
        # Check if partition ID is duplicated
        if manifest_item['pid'] in pid_list:
            raise Exception("PID No. {pid} has already been used!".format(pid=manifest_item['pid']))
        pid_list.append(manifest_item['pid'])
    # Automatically generate PIDs for partitions without PID
    pid = 256
    for idx in no_pid_manifest_idx:
        while pid in pid_list:
            pid += 1
        manifest_list[idx]['pid'] = pid
        pid_list.append(pid)

    print("Start to generate PSA manifests:")
    for manifest_item in manifest_list:
        # Replace environment variables in the manifest path
        manifest_path = os.path.expandvars(manifest_item['manifest'])
        file = open(manifest_path)
        manifest = yaml.safe_load(file)

        utilities = {}
        utilities['donotedit_warning']=donotedit_warning

        context = {}
        context['manifest'] = manifest
        context['attr'] = manifest_item
        context['utilities'] = utilities

        manifest_dir, manifest_name = os.path.split(manifest_path)
        outfile_name = manifest_name.replace('yaml', 'h').replace('json', 'h')
        context['file_name'] = outfile_name.replace('.h', '')
        outfile_name = os.path.join(manifest_dir, "psa_manifest", outfile_name).replace('\\', '/')
        intermediafile_name = os.path.join(manifest_dir, "auto_generated", 'intermedia_' + context['file_name'] + '.c').replace('\\', '/')

        """
        Remove the `source_path` portion of the filepaths, so that it can be
        interpreted as a relative path from the OUT_DIR.
        """
        if 'source_path' in manifest_item:
            # Replace environment variables in the source path
            source_path = os.path.expandvars(manifest_item['source_path'])
            outfile_name = os.path.relpath(outfile_name, start = source_path)
            intermediafile_name = os.path.relpath(intermediafile_name, start = source_path)

        partition_db.append({"manifest": manifest, "attr": manifest_item, "header_file": outfile_name})

        if OUT_DIR is not None:
            outfile_name = os.path.join(OUT_DIR, outfile_name)
            intermediafile_name = os.path.join(OUT_DIR, intermediafile_name)

        outfile_path = os.path.dirname(outfile_name)
        if not os.path.exists(outfile_path):
            os.makedirs(outfile_path)

        print ("Generating " + outfile_name)

        outfile = io.open(outfile_name, "w", newline=None)
        outfile.write(manifesttemplate.render(context))
        outfile.close()

        intermediafile_path = os.path.dirname(intermediafile_name)
        if not os.path.exists(intermediafile_path):
            os.makedirs(intermediafile_path)

        print ("Generating " + intermediafile_name)

        memoutfile = io.open(intermediafile_name, "w", newline=None)
        memoutfile.write(memorytemplate.render(context))
        memoutfile.close()

    return partition_db

def gen_files(context, gen_file_lists):
    """
    Generate files according to the gen_file_list

    Parameters
    ----------
    gen_file_lists:
        The lists of files to generate
    """
    file_list = []

    for f in gen_file_lists:
        with open(f) as file_list_yaml_file:
            file_list_yaml = yaml.safe_load(file_list_yaml_file)
            file_list.extend(file_list_yaml["file_list"])

    print("Start to generate file from the generated list:")
    for file in file_list:
        # Replace environment variables in the output filepath
        outfile_name = os.path.expandvars(file["output"])
        # Replace environment variables in the template filepath
        templatefile_name = os.path.expandvars(file["template"])

        if OUT_DIR is not None:
            outfile_name = os.path.join(OUT_DIR, outfile_name)

        print ("Generating " + outfile_name)

        outfile_path = os.path.dirname(outfile_name)
        if not os.path.exists(outfile_path):
            os.makedirs(outfile_path)

        template = ENV.get_template(templatefile_name)

        outfile = io.open(outfile_name, "w", newline=None)
        outfile.write(template.render(context))
        outfile.close()

    print ("Generation of files done")

def process_stateless_services(partitions, static_handle_max_num):
    """
    This function collects all stateless services together, and allocates
    stateless handles for them.
    If the stateless handle is set to a valid value in yaml/json file, it is
    converted to a index directly, if the stateless handle is set as "auto",
    or not set, framework will allocate a valid index for the service.
    After that, framework puts each service into a reordered stateless service
    list at position of "index". Other positions in list are left "None".
    """
    raw_stateless_services = []

    # Collect all stateless services first.
    for partition in partitions:
        # Skip the FF-M 1.0 partitions
        if partition['manifest']['psa_framework_version'] < 1.1:
            continue
        # Skip the Non-IPC partitions
        if partition['attr']['tfm_partition_ipc'] is not True:
            continue
        for service in partition['manifest']['services']:
            if 'connection_based' not in service:
                raise Exception("'connection_based' is mandatory in FF-M 1.1 service!")
            if service['connection_based'] is False:
                raw_stateless_services.append(service)

    if len(raw_stateless_services) == 0:
        return []

    if len(raw_stateless_services) > static_handle_max_num:
        raise Exception("Stateless service numbers range exceed.")

    """
    Allocate an empty stateless service list to store services and find the
    service easily by handle.
    Use service stateless handle values as indexes. Put service in the list
    at index "handle - 1", since handle value starts from 1 and list index
    starts from 0.
    """
    reordered_stateless_services = [None] * static_handle_max_num

    for service in raw_stateless_services:
        service_handle = service['stateless_handle']

        # Fill in service list with specified stateless handle, otherwise skip
        if isinstance(service_handle, int):
            if service_handle < 1 or service_handle > static_handle_max_num:
                raise Exception("Invalid stateless_handle setting.")
            # Convert handle index to reordered service list index
            service_handle = service_handle - 1

            if reordered_stateless_services[service_handle] is not None:
                raise Exception("Duplicated stateless_handle setting.")
            reordered_stateless_services[service_handle] = service
            # Remove recorded node from the existing list
            raw_stateless_services.remove(service)

    # Auto-allocate stateless handle and encode the stateless handle
    for i in range(0, static_handle_max_num):
        service = reordered_stateless_services[i]

        if service == None and len(raw_stateless_services) > 0:
            service = raw_stateless_services.pop(0)

        """
        Encode stateless flag and version into stateless handle
        bit 30: stateless handle indicator
        bit 15-8: stateless service version
        bit 7-0: stateless handle index
        """
        stateless_handle_value = 0
        if service != None:
            stateless_index = (i & 0xFF)
            stateless_handle_value |= stateless_index
            stateless_flag = 1 << 30
            stateless_handle_value |= stateless_flag
            stateless_version = (service['version'] & 0xFF) << 8
            stateless_handle_value |= stateless_version
            service['stateless_handle_value'] = '0x{0:08x}'.format(stateless_handle_value)

        reordered_stateless_services[i] = service

    return reordered_stateless_services

def parse_args():
    parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list',
                                     epilog='Note that environment variables in template files will be replaced with their values')

    parser.add_argument('-o', '--outdir'
                        , dest='outdir'
                        , required=False
                        , default=None
                        , metavar='out_dir'
                        , help='The root directory for generated files, the default is TF-M root folder.')

    parser.add_argument('-m', '--manifest'
                        , nargs='+'
                        , dest='manifest_args'
                        , required=True
                        , metavar='manifest'
                        , help='A set of secure partition manifest lists to parse')

    parser.add_argument('-f', '--file-list'
                        , nargs='+'
                        , dest='gen_file_args'
                        , required=True
                        , metavar='file-list'
                        , help='These files descripe the file list to generate')

    args = parser.parse_args()
    manifest_args = args.manifest_args
    gen_file_args = args.gen_file_args

    return args

ENV = Environment(
        loader = TemplateLoader(),
        autoescape = select_autoescape(['html', 'xml']),
        lstrip_blocks = True,
        trim_blocks = True,
        keep_trailing_newline = True
    )

def main():
    """
    The entry point of the script.

    Generates the output files based on the templates and the manifests.
    """

    global OUT_DIR

    args = parse_args()

    manifest_args = args.manifest_args
    gen_file_args = args.gen_file_args
    OUT_DIR = args.outdir

    manifest_list = [os.path.abspath(x) for x in args.manifest_args]
    gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]

    # Arguments could be relative path.
    # Convert to absolute path as we are going to change diretory later
    if OUT_DIR is not None:
        OUT_DIR = os.path.abspath(OUT_DIR)

    """
    Relative path to TF-M root folder is supported in the manifests
    and default value of manifest list and generated file list are relative to TF-M root folder as well,
    so first change directory to TF-M root folder.
    By doing this, the script can be executed anywhere
    The script is located in <TF-M root folder>/tools, so sys.path[0]<location of the script>/.. is TF-M root folder.
    """
    os.chdir(os.path.join(sys.path[0], ".."))

    partition_db = process_manifest(manifest_list)

    utilities = {}
    context = {}

    utilities['donotedit_warning'] = donotedit_warning

    context['partitions'] = partition_db
    context['utilities'] = utilities
    context['stateless_services'] = process_stateless_services(partition_db, 32)

    gen_files(context, gen_file_list)

if __name__ == "__main__":
    main()