Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 2 | """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. |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 22 | |
| 23 | import sys |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 24 | import os |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 25 | import json |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 26 | from jsonschema import validate |
| 27 | from typing import Tuple |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 28 | import argparse |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 29 | import jinja2 |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 30 | from mbedtls_dev import build_tree |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 31 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 32 | def render(template_path: str, driver_jsoncontext: list) -> str: |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 33 | """ |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 34 | Render template from the input file and driver JSON. |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 35 | """ |
Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 36 | environment = jinja2.Environment( |
| 37 | loader=jinja2.FileSystemLoader(os.path.dirname(template_path)), |
| 38 | keep_trailing_newline=True) |
| 39 | template = environment.get_template(os.path.basename(template_path)) |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 40 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 41 | return template.render(drivers = driver_jsoncontext) |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 42 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 43 | |
| 44 | def generate_driver_wrapper_file(template_dir: str, output_dir: str, driver_jsoncontext: list ) -> None: |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 45 | """ |
| 46 | Generate the file psa_crypto_driver_wrapper.c. |
| 47 | """ |
| 48 | driver_wrapper_template_filename = \ |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 49 | os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja") |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 50 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 51 | result = render(driver_wrapper_template_filename, driver_jsoncontext) |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 52 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 53 | with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file: |
| 54 | out_file.write(result) |
Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 55 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 56 | |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 57 | def validate_json(driverjson_data: list, driverschema: list) -> bool: |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 58 | """ |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 59 | Validate the Driver JSON against schema |
| 60 | """ |
| 61 | try: |
| 62 | validate(instance = driverjson_data, schema = driverschema) |
| 63 | except jsonschema.exceptions.ValidationError as err: |
| 64 | print(err) |
| 65 | err = "The driver JSON data is InValid" |
| 66 | return False |
| 67 | |
| 68 | message = "The driver JSON data is Valid" |
| 69 | return True |
| 70 | |
| 71 | def merge_driverjsonfiles(mbedtls_root: str, json_directory: str, jsondriverlistName: str) -> Tuple[bool,list]: |
| 72 | """ |
| 73 | Merge driver JSON files into a single ordered JSON after validation. |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 74 | """ |
| 75 | result = list() |
| 76 | driverlist = list() |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 77 | with open(os.path.join(mbedtls_root, 'scripts/data_files/driver_jsons/driver_transparent_schema.json'), 'r') as file: |
| 78 | transparent_driver_schema = json.load(file) |
| 79 | with open(os.path.join(mbedtls_root, 'scripts/data_files/driver_jsons/driver_opaque_schema.json'), 'r') as file: |
| 80 | opaque_driver_schema = json.load(file) |
| 81 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 82 | with open(os.path.join(json_directory, jsondriverlistName), 'r') as driverlistfile: |
| 83 | driverlist = json.load(driverlistfile) |
| 84 | for file_name in driverlist: |
| 85 | with open(os.path.join(json_directory, file_name), 'r') as infile: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 86 | json_data = json.load(infile) |
| 87 | if json_data['type'] == 'transparent': |
| 88 | ret = validate_json(json_data, transparent_driver_schema) |
| 89 | elif json_data['type'] == 'opaque': |
| 90 | ret = validate_json(json_data, opaque_driver_schema) |
| 91 | else: |
| 92 | ret = False |
| 93 | print("Unknown Driver type") |
| 94 | if ret == False: |
| 95 | return ret, [] |
| 96 | result.append(json_data) |
| 97 | return True, result |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 98 | |
| 99 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 100 | def main() -> int: |
| 101 | """ |
| 102 | Main with command line arguments. |
| 103 | """ |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 104 | def_arg_mbedtls_root = build_tree.guess_mbedtls_root() |
| 105 | def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library') |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 106 | def_arg_template_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_templates/') |
| 107 | def_arg_json_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_jsons/') |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 108 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 109 | parser = argparse.ArgumentParser() |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 110 | parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root, |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 111 | help='root directory of mbedtls source code') |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 112 | parser.add_argument('--template_dir', nargs='?', default=def_arg_template_dir, |
| 113 | help='root directory of mbedtls source code') |
| 114 | parser.add_argument('--json_dir', nargs='?', default=def_arg_json_dir, |
| 115 | help='root directory of mbedtls source code') |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 116 | parser.add_argument('output_directory', nargs='?', |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 117 | default=def_arg_output_dir, help='output file\'s location') |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 118 | args = parser.parse_args() |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 119 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 120 | mbedtls_root = os.path.abspath(args.mbedtls_root) |
| 121 | output_directory = args.output_directory |
| 122 | template_directory = args.template_dir |
| 123 | json_directory = args.json_dir |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 124 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 125 | # load list of driver jsons from driverlist.json |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame^] | 126 | ret, merged_driverjson = merge_driverjsonfiles(mbedtls_root, json_directory, 'driverlist.json') |
| 127 | if ret == False: |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 128 | return 1 |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 129 | generate_driver_wrapper_file(template_directory, output_directory, merged_driverjson) |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 130 | |
| 131 | return 0 |
| 132 | |
| 133 | if __name__ == '__main__': |
| 134 | sys.exit(main()) |