22
22
import sys
23
23
import subprocess
24
24
import logging
25
- import argparse
26
25
27
26
logger = logging .getLogger ('TF-M-Builder' )
28
27
logging .basicConfig (level = logging .INFO ,
29
28
format = '[%(name)s] %(asctime)s: %(message)s.' ,
30
29
datefmt = '%H:%M:%S' )
31
30
32
- ROOT = abspath (join (dirname (__file__ ),
33
- os .pardir , os .pardir ))
31
+ ROOT = abspath (join (dirname (__file__ ), os .pardir , os .pardir ))
34
32
sys .path .insert (0 , ROOT )
35
- from tools .targets import Target , TARGET_MAP , TARGET_NAMES
36
33
37
34
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 ):
35
+ VERSION_FILE_PATH = join (ROOT , 'features/FEATURE_PSA/FEATURE_TFM' )
36
+
37
+ dependencies = {
38
+ "trusted-firmware-m" :
39
+ ['https://git.trustedfirmware.org/trusted-firmware-m.git' ,
40
+ 'feature-twincpu' ],
41
+ "mbedtls" : ['https://github.com/ARMmbed/mbedtls.git' ,
42
+ 'mbedtls-2.7.9' ],
43
+ "mbed-crypto" : ['https://github.com/ARMmbed/mbed-crypto.git' ,
44
+ 'mbedcrypto-1.1.0' ],
45
+ "CMSIS_5" : ['https://github.com/ARM-software/CMSIS_5.git' , '5.5.0' ],
46
+ }
47
+
48
+ def run_cmd_and_return_output (command ):
50
49
"""
51
50
Run the command in the sytem and return output.
52
51
Commands are passed as a list of tokens.
@@ -62,57 +61,83 @@ def run_cmd_return_output(command):
62
61
output = subprocess .check_output (command , stderr = fnull )
63
62
except subprocess .CalledProcessError as e :
64
63
logger .error ("The command %s failed with return code: %s" ,
65
- (' ' .join (command )), e .returncode )
64
+ (' ' .join (command )), e .returncode )
66
65
sys .exit (1 )
67
66
return output
68
67
69
- def detect_write_tfm_version (tfm_dir , ):
68
+ def detect_and_write_tfm_version (tfm_dir ):
70
69
"""
71
- Identify the version of TF-M and write it to VERSION.TXT
70
+ Identify the version of TF-M and write it to VERSION.txt
72
71
:param tfm_dir: The filesystem path where TF-M repo is cloned
73
72
"""
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 ])
73
+ cmd = ['git' , '-C' , tfm_dir , 'describe' , '--tags' ,
74
+ '--abbrev=12' , '--dirty' , '--always' ]
75
+ tfm_version = run_cmd_and_return_output (cmd )
76
+ logger .info ('TF-M version: %s' , tfm_version )
77
+ if not isdir (VERSION_FILE_PATH ):
78
+ os .makedirs (VERSION_FILE_PATH )
79
+ with open (join (VERSION_FILE_PATH , 'VERSION.txt' ), 'w+' ) as f :
80
+ f .write (tfm_version )
81
+
82
+ def check_repo_version (name , deps ):
83
+ """
84
+ Compare the version of cloned and expected and exit if they dont match
85
+ :param name: Name of the git repository
86
+ :param deps: Dictionary containing dependency details
87
+ """
88
+ basedir = TF_M_BUILD_DIR
89
+ if name == 'trusted-firmware-m' :
90
+ cmd = ['git' , '-C' , join (basedir , name ),
91
+ 'rev-parse' , '--abbrev-ref' , 'HEAD' ]
92
+ else :
93
+ cmd = ['git' , '-C' , join (basedir , name ),
94
+ 'describe' , '--tags' ]
95
+ _out = run_cmd_and_return_output (cmd )
96
+ if _out .strip ('\n ' ) != deps .get (name )[1 ]:
97
+ logger .error ('Conflict: cloned "%s" and expected "%s"' ,
98
+ _out .strip ('\n ' ), deps .get (name )[1 ])
99
+ logger .error ('check and remove folder %s' ,
100
+ join (basedir , name ))
101
+ sys .exit (1 )
102
+ else :
103
+ logger .info ('%s: version check OK' , name )
79
104
80
- def check_clone_repo ( basedir , name , git_branch , git ):
105
+ def check_and_clone_repo ( name , deps ):
81
106
"""
82
107
Test if the repositories are already cloned. If not clone them
83
- :param basedir: Base directory into which the repos will be cloned
84
108
: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
109
+ :param deps: Dictionary containing dependency details
87
110
"""
111
+ basedir = TF_M_BUILD_DIR
88
112
if not isdir (join (basedir , name )):
89
113
logger .info ('Cloning %s repo' , name )
90
- cmd = ['git' , '-C' , basedir , 'clone' , '-b' , git_branch , git ]
91
- _out = run_cmd_return_output (cmd )
114
+ cmd = ['git' , '-C' , basedir , 'clone' , '-b' ,
115
+ deps .get (name )[1 ], deps .get (name )[0 ]]
116
+ _out = run_cmd_and_return_output (cmd )
92
117
logger .info ('Cloned %s repo successfully' , name )
93
118
else :
94
- logger .info ('%s repo exists' , name )
119
+ logger .info ('%s repo exists, checking git version...' , name )
120
+ check_repo_version (name , deps )
95
121
96
122
def clone_tfm_repo ():
97
123
"""
98
124
Clone TF-M git repos and it's dependencies
99
125
"""
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' ), )
126
+ check_and_clone_repo ('trusted-firmware-m' , dependencies )
127
+ check_and_clone_repo ('mbedtls' , dependencies )
128
+ check_and_clone_repo ('mbed-crypto' , dependencies )
129
+ check_and_clone_repo ('CMSIS_5' , dependencies )
130
+ detect_and_write_tfm_version (join (TF_M_BUILD_DIR , 'trusted-firmware-m' ), )
106
131
107
132
def main ():
108
133
"""
109
134
Build Trusted Firmware M (TF-M) image for mbed-os supported TF-M targets.
110
135
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 '
136
+ and creates a VERSION.txt file under 'features/FEATURE_PSA/FEATURE_TFM '
112
137
"""
113
138
if not isdir (TF_M_BUILD_DIR ):
114
139
os .mkdir (TF_M_BUILD_DIR )
115
140
clone_tfm_repo ()
116
141
117
142
if __name__ == '__main__' :
118
- main ()
143
+ main ()
0 commit comments