blob: 3b6032028b298ab0d1876c18bd5962e38a53ce9c [file] [log] [blame]
Archana1f1a34a2021-11-17 08:44:07 +05301#!/usr/bin/env python3
Archanae03960e2021-12-19 09:17:04 +05302"""Generate library/psa_crypto_driver_wrappers.c
3
4 This module is invoked by the build sripts to auto generate the
5 psa_crypto_driver_wrappers.c based on template files in
6 script/data_files/driver_templates/.
7"""
8# Copyright The Mbed TLS Contributors
9# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
Archana1f1a34a2021-11-17 08:44:07 +053022
23import sys
Archana1f1a34a2021-11-17 08:44:07 +053024import os
Archanae829cd62021-12-24 12:50:36 +053025import json
Archana04cfe342022-01-09 13:28:28 +053026from typing import Tuple
Archanae03960e2021-12-19 09:17:04 +053027import argparse
Archana31438052022-01-09 15:01:20 +053028import jsonschema
29from jsonschema import validate
Archana1f1a34a2021-11-17 08:44:07 +053030import jinja2
Archanae03960e2021-12-19 09:17:04 +053031from mbedtls_dev import build_tree
Archana1f1a34a2021-11-17 08:44:07 +053032
Archanae829cd62021-12-24 12:50:36 +053033def render(template_path: str, driver_jsoncontext: list) -> str:
Archanae03960e2021-12-19 09:17:04 +053034 """
Archanae829cd62021-12-24 12:50:36 +053035 Render template from the input file and driver JSON.
Archanae03960e2021-12-19 09:17:04 +053036 """
Archana6f21e452021-11-23 14:46:51 +053037 environment = jinja2.Environment(
38 loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
39 keep_trailing_newline=True)
40 template = environment.get_template(os.path.basename(template_path))
Archanae03960e2021-12-19 09:17:04 +053041
Archana31438052022-01-09 15:01:20 +053042 return template.render(drivers=driver_jsoncontext)
Archana1f1a34a2021-11-17 08:44:07 +053043
Archanae829cd62021-12-24 12:50:36 +053044
Archana31438052022-01-09 15:01:20 +053045def generate_driver_wrapper_file(template_dir: str, \
46 output_dir: str, driver_jsoncontext: list) -> None:
Archanae03960e2021-12-19 09:17:04 +053047 """
48 Generate the file psa_crypto_driver_wrapper.c.
49 """
50 driver_wrapper_template_filename = \
Archanae829cd62021-12-24 12:50:36 +053051 os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja")
Archana1f1a34a2021-11-17 08:44:07 +053052
Archanae829cd62021-12-24 12:50:36 +053053 result = render(driver_wrapper_template_filename, driver_jsoncontext)
Archana1f1a34a2021-11-17 08:44:07 +053054
Archanae03960e2021-12-19 09:17:04 +053055 with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file:
56 out_file.write(result)
Archana6f21e452021-11-23 14:46:51 +053057
Archanae829cd62021-12-24 12:50:36 +053058
Archana04cfe342022-01-09 13:28:28 +053059def validate_json(driverjson_data: list, driverschema: list) -> bool:
Archanae829cd62021-12-24 12:50:36 +053060 """
Archana04cfe342022-01-09 13:28:28 +053061 Validate the Driver JSON against schema
62 """
63 try:
Archana31438052022-01-09 15:01:20 +053064 validate(instance=driverjson_data, schema=driverschema)
Archana04cfe342022-01-09 13:28:28 +053065 except jsonschema.exceptions.ValidationError as err:
66 print(err)
Archana31438052022-01-09 15:01:20 +053067 print("The driver JSON data is InValid")
Archana04cfe342022-01-09 13:28:28 +053068 return False
69
Archana04cfe342022-01-09 13:28:28 +053070 return True
71
Archana31438052022-01-09 15:01:20 +053072def merge_driverjsonfiles(mbedtls_root: str, json_directory: str, \
73 jsondriver_list: str) -> Tuple[bool, list]:
Archana04cfe342022-01-09 13:28:28 +053074 """
75 Merge driver JSON files into a single ordered JSON after validation.
Archanae829cd62021-12-24 12:50:36 +053076 """
77 result = list()
78 driverlist = list()
Archana31438052022-01-09 15:01:20 +053079 with open(os.path.join(mbedtls_root, \
80 'scripts/data_files/driver_jsons/driver_transparent_schema.json'), 'r') as file:
Archana04cfe342022-01-09 13:28:28 +053081 transparent_driver_schema = json.load(file)
Archana31438052022-01-09 15:01:20 +053082 with open(os.path.join(mbedtls_root, \
83 'scripts/data_files/driver_jsons/driver_opaque_schema.json'), 'r') as file:
Archana04cfe342022-01-09 13:28:28 +053084 opaque_driver_schema = json.load(file)
85
Archana31438052022-01-09 15:01:20 +053086 with open(os.path.join(json_directory, jsondriver_list), 'r') as driverlistfile:
Archanae829cd62021-12-24 12:50:36 +053087 driverlist = json.load(driverlistfile)
88 for file_name in driverlist:
89 with open(os.path.join(json_directory, file_name), 'r') as infile:
Archana04cfe342022-01-09 13:28:28 +053090 json_data = json.load(infile)
91 if json_data['type'] == 'transparent':
92 ret = validate_json(json_data, transparent_driver_schema)
93 elif json_data['type'] == 'opaque':
94 ret = validate_json(json_data, opaque_driver_schema)
95 else:
96 ret = False
97 print("Unknown Driver type")
Archana31438052022-01-09 15:01:20 +053098 if ret is False:
Archana04cfe342022-01-09 13:28:28 +053099 return ret, []
100 result.append(json_data)
101 return True, result
Archanae829cd62021-12-24 12:50:36 +0530102
103
Archanae03960e2021-12-19 09:17:04 +0530104def main() -> int:
105 """
106 Main with command line arguments.
107 """
Archana4a9e0262021-12-19 13:34:30 +0530108 def_arg_mbedtls_root = build_tree.guess_mbedtls_root()
109 def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library')
Archana31438052022-01-09 15:01:20 +0530110 def_arg_template_dir = os.path.join(def_arg_mbedtls_root, \
111 'scripts/data_files/driver_templates/')
112 def_arg_json_dir = os.path.join(def_arg_mbedtls_root, \
113 'scripts/data_files/driver_jsons/')
Archana4a9e0262021-12-19 13:34:30 +0530114
Archanae03960e2021-12-19 09:17:04 +0530115 parser = argparse.ArgumentParser()
Archana4a9e0262021-12-19 13:34:30 +0530116 parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root,
Archanae03960e2021-12-19 09:17:04 +0530117 help='root directory of mbedtls source code')
Archanae829cd62021-12-24 12:50:36 +0530118 parser.add_argument('--template_dir', nargs='?', default=def_arg_template_dir,
119 help='root directory of mbedtls source code')
120 parser.add_argument('--json_dir', nargs='?', default=def_arg_json_dir,
121 help='root directory of mbedtls source code')
Archanae03960e2021-12-19 09:17:04 +0530122 parser.add_argument('output_directory', nargs='?',
Archana4a9e0262021-12-19 13:34:30 +0530123 default=def_arg_output_dir, help='output file\'s location')
Archanae03960e2021-12-19 09:17:04 +0530124 args = parser.parse_args()
Archana4a9e0262021-12-19 13:34:30 +0530125
Archana31438052022-01-09 15:01:20 +0530126 mbedtls_root = os.path.abspath(args.mbedtls_root)
127 output_directory = args.output_directory
Archanae829cd62021-12-24 12:50:36 +0530128 template_directory = args.template_dir
Archana31438052022-01-09 15:01:20 +0530129 json_directory = args.json_dir
Archanae03960e2021-12-19 09:17:04 +0530130
Archanae829cd62021-12-24 12:50:36 +0530131 # load list of driver jsons from driverlist.json
Archana04cfe342022-01-09 13:28:28 +0530132 ret, merged_driverjson = merge_driverjsonfiles(mbedtls_root, json_directory, 'driverlist.json')
Archana31438052022-01-09 15:01:20 +0530133 if ret is False:
Archanae829cd62021-12-24 12:50:36 +0530134 return 1
Archanae829cd62021-12-24 12:50:36 +0530135 generate_driver_wrapper_file(template_directory, output_directory, merged_driverjson)
Archanae03960e2021-12-19 09:17:04 +0530136
137 return 0
138
139if __name__ == '__main__':
140 sys.exit(main())