File: psa_autogen.py

package info (click to toggle)
mbedtls 3.6.5-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,488 kB
  • sloc: ansic: 164,842; sh: 25,443; python: 15,512; makefile: 3,131; perl: 1,043; tcl: 4
file content (163 lines) | stat: -rwxr-xr-x 5,655 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
"""This hacky script generates a partition from a manifest file"""

# Copyright The Mbed TLS Contributors
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

import json
import os
import sys
from os import listdir

if len(sys.argv) != 2:
    print("Usage: psa_autogen <manifest_file>")
    sys.exit(1)

FILENAME = str(sys.argv[1])


with open(str(FILENAME), "r") as read_file:
    data = json.load(read_file)
    FILENAME = os.path.basename(FILENAME)
    FILENAME = FILENAME.split('.')[0]
    print("Base filename is " + str(FILENAME))

    if str(data['psa_framework_version'] == "1.0"):
        entry_point = str(data['entry_point'])
        partition_name = str(data['name'])
        services = data['services']
        try:
            irqs = data['irqs']
        except KeyError:
            irqs = []

        try:
            os.mkdir("psa_manifest")
            print("Generating psa_manifest directory")
        except OSError:
            print ("PSA manifest directory already exists")

        man  = open(str("psa_manifest/" + FILENAME + ".h"), "w")
        pids = open("psa_manifest/pid.h", "a")
        sids = open("psa_manifest/sid.h", "a")

        if len(services) > 28:
           print ("Unsupported number of services")

        count = 4 # For creating SID array
        nsacl = "const int ns_allowed[32] = {"
        policy = "const int strict_policy[32] = {"
        qcode = "const char * psa_queues[] = { "
        versions = "const uint32_t versions[32] = {"
        queue_path = "/tmp/psa_service_"
        start = False

        for x in range(0, count):
            qcode = qcode + "\"\", "
            nsacl = nsacl + " 0,"
            policy = policy + "0,"
            versions = versions + " 0,"

        # Go through all the services to make sid.h and pid.h
        for svc in services:
            man.write("#define " + str(svc['signal']) + "_SIGNAL " + str(2 ** (count)) + 'u\n')
            sids.write("#define " + str(svc['name']) + "_SID " + str(svc['sid'] + '\n'))
            qcode = qcode + "\"" + queue_path + str(int(svc['sid'], 16)) + "\","
            ns_clients = svc['non_secure_clients']
            print(str(svc))
            if ns_clients == "true":
                nsacl = nsacl + " 1,"
            else:
                nsacl = nsacl + " 0,"
            try:
                versions = versions + str(svc['minor_version']) + ","
            except KeyError:
                versions = versions + "1,"

            strict = 0
            try:
                if str(svc['minor_policy']).lower() == "strict":
                    strict = 1
                    policy = policy + "1,"
                else:
                    policy = policy + "0,"
            except KeyError:
                strict = 0
                policy = policy + "0,"

            count = count+1

        sigcode = ""
        handlercode = "void __sig_handler(int signo) {\n"
        irqcount = count
        for irq in irqs:
            man.write("#define " + str(irq['signal']) + " " + str(2 ** (irqcount)) + 'u\n')
            sigcode = sigcode + "   signal(" + str(irq['source']) + ", __sig_handler);\n"
            handlercode = handlercode + "   if (signo == " + str(irq['source']) + ") { raise_signal(" + str(2 ** (irqcount)) + 'u);' + " };\n"
            irqcount = irqcount+1

        handlercode = handlercode + "}\n"

        while (count < 32):
            qcode = qcode + "\"\","
            nsacl = nsacl + "0,"
            versions = versions + "0,"
            policy = policy + "0,"
            count = count + 1

        qcode = qcode + "};\n"
        nsacl = nsacl + "};\n"
        versions = versions + "};\n"
        policy = policy + "};\n"

        pids.close()
        sids.close()
        man.close()

        symbols = []
        # Go through all the files in the current directory and look for the entrypoint

        for root, directories, filenames in os.walk('.'):
            for filename in filenames:

                if "psa_ff_bootstrap" in filename or filename == "psa_manifest":
                    continue

                try:
                    fullpath = os.path.join(root,filename)
                    with open(fullpath, encoding='utf-8') as currentFile:
                        text = currentFile.read()
                        if str(entry_point + "(") in text:
                            symbols.append(fullpath)
                except IOError:
                    print("Couldn't open " + filename)

                except UnicodeDecodeError:
                    pass

        print(str("Number of entrypoints detected: " + str(len(symbols))))
        if len(symbols) < 1:
            print("Couldn't find function " + entry_point)
            sys.exit(1)
        elif len(symbols) > 1:
            print("Duplicate entrypoint symbol detected: " + str(symbols))
            sys.exit(2)
        else:
            bs = open(str("psa_ff_bootstrap_" + str(partition_name) + ".c"), "w")
            bs.write("#include <psasim/init.h>\n")
            bs.write("#include \"" + symbols[0] + "\"\n")
            bs.write("#include <signal.h>\n")
            bs.write(qcode)
            bs.write("\n")
            bs.write(nsacl + "\n")
            bs.write(policy + "\n")
            bs.write(versions + "\n")
            bs.write(handlercode)
            bs.write("\n")
            bs.write("int main() {\n")
            bs.write(sigcode)
            bs.write("   __init_psasim(psa_queues, 32, ns_allowed, versions, strict_policy);\n")
            bs.write("   " + entry_point + "();\nfor(;;);\n}\n")
            bs.close()

            print("Success")