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