Skip to content

Commit 2641fb3

Browse files
author
Cruz Monrreal
authored
Merge pull request #10364 from kfnta/psa_codegen_refactor
Refactor PSA code generators
2 parents 582edf5 + ae4341e commit 2641fb3

7 files changed

+274
-365
lines changed

tools/psa/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ This document describes the following scripts:
55
* \_\_init\_\_.py
66
* generate_partition_code.py
77
* mbed_spm_tfm_common.py
8-
* generate_mbed_spm_partition_code.py
9-
* generate_tfm_partition_code.py
108
* release.py
119

1210
## \_\_init\_\_.py
@@ -25,8 +23,6 @@ Mbed OS holds two implementations of PSA:
2523
Each implementation requires a set of auto-generated files describing the secure partitions:
2624

2725
* `generate_partition_code.py` - Generate files for both implementations.
28-
* `generate_mbed_spm_partition_code.py` - Generate files for MBED_SPM.
29-
* `generate_tfm_partition_code.py` - Generate files for TF-M.
3026
* `mbed_spm_tfm_common.py` - Holds common functions for both.
3127

3228
## Secure image generation

tools/psa/generate_mbed_spm_partition_code.py

Lines changed: 0 additions & 214 deletions
This file was deleted.

tools/psa/generate_partition_code.py

Lines changed: 158 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,162 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
from generate_mbed_spm_partition_code import generate_psa_code
19-
from generate_tfm_partition_code import generate_tfm_code
18+
import itertools
19+
import json
20+
import os
21+
import sys
22+
from os.path import join as path_join
2023

21-
generate_psa_code()
22-
generate_tfm_code()
24+
# Be sure that the tools directory is in the search path
25+
ROOT = os.path.abspath(path_join(os.path.dirname(__file__), os.pardir, os.pardir))
26+
sys.path.insert(0, ROOT)
27+
28+
from tools.psa.mbed_spm_tfm_common import validate_partition_manifests, \
29+
manifests_discovery, parse_manifests, generate_source_files, \
30+
MBED_OS_ROOT, SERVICES_DIR, TESTS_DIR
31+
32+
__version__ = '1.0'
33+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
34+
MANIFEST_FILE_PATTERN = '*_psa.json'
35+
PSA_CORE_ROOT = path_join(MBED_OS_ROOT, 'components', 'TARGET_PSA')
36+
TFM_TEMPLATES_DESC = path_join(SCRIPT_DIR, 'tfm', 'tfm_generated_file_list.json')
37+
MBED_SPM_TEMPLATES_DESC = path_join(SCRIPT_DIR, 'mbed_spm', 'mbed_spm_generated_file_list.json')
38+
MBED_SPM_TEMPLATES_DIR = path_join(SCRIPT_DIR, 'mbed_spm', 'templates')
39+
40+
41+
def generate_partitions_sources(manifest_files, extra_filters=None):
42+
"""
43+
Process all the given manifest files and generate C code from them
44+
45+
:param manifest_files: List of manifest files
46+
:param extra_filters: Dictionary of extra filters to use in the rendering
47+
process
48+
:return: List of paths to the generated files
49+
"""
50+
51+
# Construct a list of all the manifests and sids.
52+
manifests, _ = parse_manifests(manifest_files, 'MBED_SPM')
53+
54+
with open(MBED_SPM_TEMPLATES_DESC, 'r') as fh:
55+
template_data = json.load(fh)
56+
manifest_template_list = [path_join(MBED_OS_ROOT, t['template'])
57+
for t in template_data['partition']]
58+
59+
generated_folders = set()
60+
for manifest in manifests:
61+
manifest_output_folder = manifest.autogen_folder
62+
render_args = {
63+
'partition': manifest,
64+
'dependent_partitions': manifest.find_dependencies(manifests),
65+
'script_ver': __version__
66+
}
67+
68+
manifest_output_folder = generate_source_files(
69+
manifest.templates_to_files(manifest_template_list,
70+
MBED_SPM_TEMPLATES_DIR,
71+
manifest_output_folder),
72+
render_args,
73+
manifest_output_folder,
74+
extra_filters=extra_filters
75+
)
76+
77+
generated_folders.add(manifest_output_folder)
78+
79+
return list(generated_folders)
80+
81+
82+
def generate_psa_setup(manifest_files, output_dir, weak_setup, extra_filters=None):
83+
"""
84+
Process all the given manifest files and generate C setup code from them
85+
:param manifest_files: List of manifest files
86+
:param output_dir: Output directory for the generated files
87+
:param weak_setup: Is the functions/data in the setup file weak
88+
(can be overridden by another setup file)
89+
:param extra_filters: Dictionary of extra filters to use in the rendering
90+
process
91+
:return: path to the setup generated files
92+
"""
93+
with open(MBED_SPM_TEMPLATES_DESC, 'r') as fh:
94+
template_data = json.load(fh)
95+
templates_dict = {
96+
path_join(MBED_OS_ROOT, t['template']):
97+
path_join(output_dir, t['target'])
98+
for t in template_data['common']
99+
}
100+
101+
# Construct lists of all the manifests and mmio_regions.
102+
manifests, region_list = parse_manifests(manifest_files, 'MBED_SPM')
103+
104+
# Validate the correctness of the manifest collection.
105+
validate_partition_manifests(manifests)
106+
107+
render_args = {
108+
'partitions': manifests,
109+
'regions': region_list,
110+
'region_pair_list': list(itertools.combinations(region_list, 2)),
111+
'weak': weak_setup,
112+
'script_ver': __version__
113+
}
114+
115+
return generate_source_files(
116+
templates_dict,
117+
render_args,
118+
output_dir,
119+
extra_filters=extra_filters
120+
)
121+
122+
123+
def generate_psa_code(service_files, test_files):
124+
# Generate partition code for each manifest file
125+
generate_partitions_sources(service_files + test_files)
126+
127+
# Generate default system psa setup file (only system partitions)
128+
generate_psa_setup(service_files, PSA_CORE_ROOT, weak_setup=True)
129+
130+
tests_dict = {}
131+
for test_manifest in test_files:
132+
test_dir = os.path.dirname(test_manifest)
133+
if test_dir not in tests_dict:
134+
tests_dict[test_dir] = [test_manifest]
135+
else:
136+
tests_dict[test_dir].append(test_manifest)
137+
138+
for test_dir in tests_dict:
139+
generate_psa_setup(service_files + tests_dict[test_dir],
140+
test_dir, weak_setup=False)
141+
142+
143+
def generate_tfm_code(service_files, test_files):
144+
# Construct lists of all the manifests and mmio_regions.
145+
service_manifests, service_region_list = parse_manifests(
146+
service_files, 'TFM')
147+
test_manifests, test_region_list = parse_manifests(
148+
test_files, 'TFM')
149+
150+
# Validate the correctness of the manifest collection.
151+
validate_partition_manifests(service_manifests + test_manifests)
152+
153+
render_args = {
154+
'service_partitions': service_manifests,
155+
'test_partitions': test_manifests
156+
}
157+
158+
with open(TFM_TEMPLATES_DESC, 'r') as fh:
159+
templates_data = json.load(fh)
160+
templates_dict = {
161+
path_join(MBED_OS_ROOT, t['template']):
162+
path_join(MBED_OS_ROOT, t['output']) for t in templates_data
163+
}
164+
165+
generate_source_files(templates_dict, render_args, MBED_OS_ROOT)
166+
167+
168+
def main():
169+
services, _ = manifests_discovery(root_dir=SERVICES_DIR)
170+
_, tests = manifests_discovery(root_dir=TESTS_DIR)
171+
generate_psa_code(services, tests)
172+
generate_tfm_code(services, tests)
173+
174+
175+
if __name__ == '__main__':
176+
main()

0 commit comments

Comments
 (0)