1
+ #!/usr/bin/python
2
+ """
3
+ Copyright (c) 2019 ARM Limited. All rights reserved.
4
+
5
+ SPDX-License-Identifier: Apache-2.0
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+ """
19
+
20
+ import os
21
+ from os .path import join , abspath , dirname , isdir
22
+ import sys
23
+ import subprocess
24
+ import logging
25
+ import argparse
26
+
27
+ logger = logging .getLogger ('TF-M-Builder' )
28
+ logging .basicConfig (level = logging .INFO ,
29
+ format = '[%(name)s] %(asctime)s: %(message)s.' ,
30
+ datefmt = '%H:%M:%S' )
31
+
32
+ ROOT = abspath (join (dirname (__file__ ),
33
+ os .pardir , os .pardir ))
34
+ sys .path .insert (0 , ROOT )
35
+ from tools .targets import Target , TARGET_MAP , TARGET_NAMES
36
+
37
+ TF_M_BUILD_DIR = join (ROOT , os .pardir , 'tfm_build_dir' )
38
+ VERSION_FILE_PATH = join (ROOT ,'components/TARGET_PSA/TARGET_TFM' )
39
+
40
+ TFM_GIT = 'https://git.trustedfirmware.org/trusted-firmware-m.git'
41
+ TFM_GIT_BRANCH = 'feature-twincpu'
42
+ MBEDTLS_GIT = 'https://github.com/ARMmbed/mbedtls.git'
43
+ MBEDTLS_GIT_BRANCH = 'mbedtls-2.7.9'
44
+ MBEDCRYPTO_GIT = 'https://github.com/ARMmbed/mbed-crypto.git'
45
+ MBEDCRYPTO_GIT_BRANCH = 'mbedcrypto-1.1.0'
46
+ CMSIS_GIT = 'https://github.com/ARM-software/CMSIS_5.git'
47
+ CMSIS_GIT_BRANCH = '5.5.0'
48
+
49
+ def run_cmd_return_output (command ):
50
+ """
51
+ Run the command in the sytem and return output.
52
+ Commands are passed as a list of tokens.
53
+ E.g. The command 'git remote -v' would be passed in as:
54
+ ['git', 'remote', '-v']
55
+
56
+ :param command: System command as a list of tokens
57
+ """
58
+ output = ''
59
+ logger .debug ('[Exec] %s' , ' ' .join (command ))
60
+ try :
61
+ fnull = open (os .devnull , 'w' )
62
+ output = subprocess .check_output (command , stderr = fnull )
63
+ except subprocess .CalledProcessError as e :
64
+ logger .error ("The command %s failed with return code: %s" ,
65
+ (' ' .join (command )), e .returncode )
66
+ sys .exit (1 )
67
+ return output
68
+
69
+ def detect_write_tfm_version (tfm_dir , ):
70
+ """
71
+ Identify the version of TF-M and write it to VERSION.TXT
72
+ :param tfm_dir: The filesystem path where TF-M repo is cloned
73
+ """
74
+ cmd = ['git' , '-C' , tfm_dir , 'describe' , '--tags' , '--abbrev=12' , '--dirty' , '--always' ]
75
+ tfm_version = run_cmd_return_output (cmd )
76
+ logger .debug ('TF-M version: %s' , tfm_version )
77
+ with open (join (VERSION_FILE_PATH , 'VERSION.TXT' ), 'w+' ) as f :
78
+ f .writelines (tfm_version .splitlines (True )[0 ])
79
+
80
+ def check_clone_repo (basedir , name , git_branch , git ):
81
+ """
82
+ Test if the repositories are already cloned. If not clone them
83
+ :param basedir: Base directory into which the repos will be cloned
84
+ :param name: Name of the git repository
85
+ :param git_branch: Git branch to be cloned
86
+ :param git: Web link to the git respository
87
+ """
88
+ if not isdir (join (basedir , name )):
89
+ logger .info ('Cloning %s repo' , name )
90
+ cmd = ['git' , '-C' , basedir , 'clone' , '-b' , git_branch , git ]
91
+ _out = run_cmd_return_output (cmd )
92
+ logger .info ('Cloned %s repo successfully' , name )
93
+ else :
94
+ logger .info ('%s repo exists' , name )
95
+
96
+ def clone_tfm_repo ():
97
+ """
98
+ Clone TF-M git repos and it's dependencies
99
+ """
100
+ check_clone_repo (TF_M_BUILD_DIR , 'trusted-firmware-m' , TFM_GIT_BRANCH , TFM_GIT )
101
+ check_clone_repo (TF_M_BUILD_DIR , 'mbedtls' , MBEDTLS_GIT_BRANCH , MBEDTLS_GIT )
102
+ check_clone_repo (TF_M_BUILD_DIR , 'mbed-crypto' , MBEDCRYPTO_GIT_BRANCH , MBEDCRYPTO_GIT )
103
+ check_clone_repo (TF_M_BUILD_DIR , 'CMSIS_5' , CMSIS_GIT_BRANCH , CMSIS_GIT )
104
+
105
+ detect_write_tfm_version (join (TF_M_BUILD_DIR , 'trusted-firmware-m' ), )
106
+
107
+ def main ():
108
+ """
109
+ Build Trusted Firmware M (TF-M) image for mbed-os supported TF-M targets.
110
+ Current version of the script only clones TF-M git repo and dependencies
111
+ and creates a VERSION.TXT file under 'components/TARGET_PSA/TARGET_TFM'
112
+ """
113
+ if not isdir (TF_M_BUILD_DIR ):
114
+ os .mkdir (TF_M_BUILD_DIR )
115
+ clone_tfm_repo ()
116
+
117
+ if __name__ == '__main__' :
118
+ main ()
0 commit comments