Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
TTornblom | 7f92a73 | 2020-03-05 13:12:20 +0100 | [diff] [blame] | 2 | # Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import os |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 9 | import io |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 10 | import sys |
| 11 | import argparse |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 12 | from jinja2 import Environment, BaseLoader, select_autoescape, TemplateNotFound |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 13 | |
| 14 | try: |
| 15 | import yaml |
| 16 | except ImportError as e: |
Mate Toth-Pal | a99ec6b | 2019-05-07 11:00:56 +0200 | [diff] [blame] | 17 | print (str(e) + " To install it, type:") |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 18 | print ("pip install PyYAML") |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 19 | exit(1) |
| 20 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 21 | donotedit_warning = \ |
| 22 | "/*********** " + \ |
| 23 | "WARNING: This is an auto-generated file. Do not edit!" + \ |
| 24 | " ***********/" |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 25 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 26 | OUT_DIR = None # The root directory that files are generated to |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 27 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 28 | class TemplateLoader(BaseLoader): |
| 29 | """ |
| 30 | Template loader class. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 31 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 32 | An instance of this class is passed to the template engine. It is |
| 33 | responsible for reading the template file |
| 34 | """ |
| 35 | def __init__(self): |
| 36 | pass |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 37 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 38 | def get_source(self, environment, template): |
| 39 | """ |
| 40 | This function reads the template files. |
| 41 | For detailed documentation see: |
| 42 | http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source |
| 43 | |
| 44 | Please note that this function always return 'false' as 'uptodate' |
| 45 | value, so the output file will always be generated. |
| 46 | """ |
| 47 | if not os.path.isfile(template): |
| 48 | raise TemplateNotFound(template) |
| 49 | with open(template) as f: |
| 50 | source = f.read() |
| 51 | return source, template, False |
| 52 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 53 | def process_manifest(manifest_list_files): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 54 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 55 | Parse the input manifest, generate the data base for genereated files |
| 56 | and generate manifest header files. |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 57 | |
| 58 | Parameters |
| 59 | ---------- |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 60 | manifest_list_files: |
| 61 | The manifest lists to parse. |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 62 | |
| 63 | Returns |
| 64 | ------- |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 65 | The manifest header list and the data base. |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 66 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 67 | |
| 68 | db = [] |
| 69 | manifest_header_list = [] |
| 70 | manifest_list = [] |
| 71 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 72 | for f in manifest_list_files: |
| 73 | with open(f) as manifest_list_yaml_file: |
| 74 | manifest_dic = yaml.safe_load(manifest_list_yaml_file) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 75 | manifest_list.extend(manifest_dic["manifest_list"]) |
| 76 | |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 77 | templatefile_name = 'secure_fw/partitions/manifestfilename.template' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 78 | template = ENV.get_template(templatefile_name) |
| 79 | |
edison.ai | 7b299f5 | 2020-07-16 15:44:18 +0800 | [diff] [blame] | 80 | print("Start to generate PSA manifests:") |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 81 | for manifest_item in manifest_list: |
| 82 | manifest_path = os.path.expandvars(manifest_item['manifest']) |
| 83 | file = open(manifest_path) |
| 84 | manifest = yaml.safe_load(file) |
| 85 | |
| 86 | db.append({"manifest": manifest, "attr": manifest_item}) |
| 87 | |
| 88 | utilities = {} |
| 89 | utilities['donotedit_warning']=donotedit_warning |
| 90 | |
| 91 | context = {} |
| 92 | context['manifest'] = manifest |
| 93 | context['attr'] = manifest_item |
| 94 | context['utilities'] = utilities |
| 95 | |
| 96 | manifest_dir, manifest_name = os.path.split(manifest_path) |
| 97 | outfile_name = manifest_name.replace('yaml', 'h').replace('json', 'h') |
| 98 | context['file_name'] = outfile_name.replace('.h', '') |
TTornblom | 7f92a73 | 2020-03-05 13:12:20 +0100 | [diff] [blame] | 99 | outfile_name = os.path.join(manifest_dir, "psa_manifest", outfile_name).replace('\\', '/') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 100 | |
| 101 | manifest_header_list.append(outfile_name) |
| 102 | |
| 103 | if OUT_DIR is not None: |
| 104 | outfile_name = os.path.join(OUT_DIR, outfile_name) |
| 105 | |
| 106 | outfile_path = os.path.dirname(outfile_name) |
| 107 | if not os.path.exists(outfile_path): |
| 108 | os.makedirs(outfile_path) |
| 109 | |
| 110 | print ("Generating " + outfile_name) |
| 111 | |
TTornblom | 441a070 | 2020-04-28 12:40:42 +0200 | [diff] [blame] | 112 | outfile = io.open(outfile_name, "w", newline=None) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 113 | outfile.write(template.render(context)) |
| 114 | outfile.close() |
| 115 | |
| 116 | return manifest_header_list, db |
| 117 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 118 | def gen_files(context, gen_file_lists): |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 119 | """ |
| 120 | Generate files according to the gen_file_list |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 121 | |
| 122 | Parameters |
| 123 | ---------- |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 124 | gen_file_lists: |
| 125 | The lists of files to generate |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 126 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 127 | file_list = [] |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 128 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 129 | for f in gen_file_lists: |
| 130 | with open(f) as file_list_yaml_file: |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 131 | file_list_yaml = yaml.safe_load(file_list_yaml_file) |
| 132 | file_list.extend(file_list_yaml["file_list"]) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 133 | |
edison.ai | 7b299f5 | 2020-07-16 15:44:18 +0800 | [diff] [blame] | 134 | print("Start to generate file from the generated list:") |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 135 | for file in file_list: |
| 136 | outfile_name = os.path.expandvars(file["output"]) |
Kevin Peng | 1ec5e7c | 2019-11-29 10:52:00 +0800 | [diff] [blame] | 137 | templatefile_name = os.path.expandvars(file["template"]) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 138 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 139 | if OUT_DIR is not None: |
| 140 | outfile_name = os.path.join(OUT_DIR, outfile_name) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 141 | |
edison.ai | 7b299f5 | 2020-07-16 15:44:18 +0800 | [diff] [blame] | 142 | print ("Generating " + outfile_name) |
| 143 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 144 | outfile_path = os.path.dirname(outfile_name) |
| 145 | if not os.path.exists(outfile_path): |
| 146 | os.makedirs(outfile_path) |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 147 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 148 | template = ENV.get_template(templatefile_name) |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 149 | |
TTornblom | 441a070 | 2020-04-28 12:40:42 +0200 | [diff] [blame] | 150 | outfile = io.open(outfile_name, "w", newline=None) |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 151 | outfile.write(template.render(context)) |
| 152 | outfile.close() |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 153 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 154 | print ("Generation of files done") |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame] | 155 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 156 | def parse_args(): |
| 157 | parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list') |
| 158 | parser.add_argument('-o', '--outdir' |
| 159 | , dest='outdir' |
| 160 | , required=False |
| 161 | , default=None |
| 162 | , metavar='out_dir' |
| 163 | , help='The root directory for generated files, the default is TF-M root folder.') |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 164 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 165 | parser.add_argument('-m', '--manifest' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 166 | , nargs='+' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 167 | , dest='manifest_args' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 168 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 169 | , metavar='manifest' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 170 | , help='A set of secure partition manifest lists to parse') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 171 | |
| 172 | parser.add_argument('-f', '--file-list' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 173 | , nargs='+' |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 174 | , dest='gen_file_args' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 175 | , required=True |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 176 | , metavar='file-list' |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 177 | , help='These files descripe the file list to generate') |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 178 | |
| 179 | args = parser.parse_args() |
| 180 | manifest_args = args.manifest_args |
| 181 | gen_file_args = args.gen_file_args |
| 182 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 183 | return args |
| 184 | |
| 185 | ENV = Environment( |
| 186 | loader = TemplateLoader(), |
| 187 | autoescape = select_autoescape(['html', 'xml']), |
| 188 | lstrip_blocks = True, |
| 189 | trim_blocks = True, |
| 190 | keep_trailing_newline = True |
| 191 | ) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 192 | |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 193 | def main(): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 194 | """ |
| 195 | The entry point of the script. |
| 196 | |
| 197 | Generates the output files based on the templates and the manifests. |
| 198 | """ |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 199 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 200 | global OUT_DIR |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 201 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 202 | args = parse_args() |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 203 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 204 | manifest_args = args.manifest_args |
| 205 | gen_file_args = args.gen_file_args |
| 206 | OUT_DIR = args.outdir |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 207 | |
| 208 | if len(manifest_args) == 0: |
| 209 | manifest_list = DEFAULT_MANIFEST_LIST |
| 210 | else: |
| 211 | """ |
| 212 | Only convert to abs path when value is not default |
| 213 | Because the default value is a fixed relative path to TF-M root folder, |
| 214 | it will be various to different execution path if converted to absolute path. |
| 215 | The same for gen_file_list |
| 216 | """ |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 217 | manifest_list = [os.path.abspath(x) for x in args.manifest_args] |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 218 | if len(gen_file_args) == 0: |
| 219 | gen_file_list = DEFAULT_GEN_FILE_LIST |
| 220 | else: |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 221 | gen_file_list = [os.path.abspath(x) for x in args.gen_file_args] |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 222 | |
| 223 | # Arguments could be relative path. |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 224 | # Convert to absolute path as we are going to change diretory later |
| 225 | if OUT_DIR is not None: |
| 226 | OUT_DIR = os.path.abspath(OUT_DIR) |
| 227 | |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 228 | """ |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 229 | Relative path to TF-M root folder is supported in the manifests |
| 230 | and default value of manifest list and generated file list are relative to TF-M root folder as well, |
| 231 | so first change directory to TF-M root folder. |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 232 | By doing this, the script can be executed anywhere |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 233 | The script is located in <TF-M root folder>/tools, so sys.path[0]<location of the script>/.. is TF-M root folder. |
Shawn Shan | a9ad1e0 | 2019-08-07 15:49:48 +0800 | [diff] [blame] | 234 | """ |
| 235 | os.chdir(os.path.join(sys.path[0], "..")) |
| 236 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 237 | manifest_header_list, db = process_manifest(manifest_list) |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 238 | |
Edison Ai | 6e3f2a3 | 2019-06-11 15:29:05 +0800 | [diff] [blame] | 239 | utilities = {} |
| 240 | context = {} |
| 241 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 242 | utilities['donotedit_warning']=donotedit_warning |
| 243 | utilities['manifest_header_list']=manifest_header_list |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 244 | |
Kevin Peng | 655f239 | 2019-11-27 16:33:02 +0800 | [diff] [blame] | 245 | context['manifests'] = db |
| 246 | context['utilities'] = utilities |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 247 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame^] | 248 | gen_files(context, gen_file_list) |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 249 | |
| 250 | if __name__ == "__main__": |
| 251 | main() |