24
24
from shutil import copy2
25
25
import json
26
26
from intelhex import IntelHex , hex2bin , bin2hex
27
+ from pathlib import Path
27
28
28
29
from ..config import ConfigException
29
30
@@ -171,28 +172,10 @@ def sign_image(toolchain, resourses, elf, binf, m0hex):
171
172
except KeyError :
172
173
raise ConfigException ("[PSOC6.sign_image] Target " + toolchain .target .name + " is not supported in cysecuretools." )
173
174
174
- from pathlib import Path , PurePath
175
+ policy_file = find_policy ( toolchain )
175
176
176
- mbed_os_root = Path (os .getcwd ())
177
-
178
- policy_path = Path (toolchain .target .policy_file )
179
- if policy_path .is_absolute ():
180
- policy_file = policy_path
181
- else :
182
- policy_path = mbed_os_root / policy_path
183
-
184
- if os .path .isfile (str (policy_path )):
185
- policy_file = policy_path
186
- else :
187
- policy_file = Path (find_policy (toolchain , resourses ))
188
-
189
177
toolchain .notify .info ("[PSOC6.sign_image] Using policy file: " + str (policy_file ))
190
178
191
- # Append cysecuretools path to sys.path and import cysecuretools. This will
192
- # prioritize system installations of cysecuretools over the included
193
- # cysecuretools.
194
- #sb_tools_path = mbed_os_root / Path("targets/TARGET_Cypress/TARGET_PSOC6/")
195
- #sys.path.append(str(sb_tools_path))
196
179
import cysecuretools
197
180
198
181
tools = cysecuretools .CySecureTools (secure_target , str (policy_file ))
@@ -208,7 +191,8 @@ def sign_image(toolchain, resourses, elf, binf, m0hex):
208
191
complete (toolchain , elf , hexf0 = binf , hexf1 = m0hex )
209
192
210
193
else :
211
- raise ConfigException ("[PSOC6.sign_image] Boot scheme " + str (toolchain .target .boot_scheme ) + "is not supported. Supported boot schemes are 'single_image' and 'multi_image' " )
194
+ raise ConfigException ("[PSOC6.sign_image] Boot scheme " + str (toolchain .target .boot_scheme ) + \
195
+ "is not supported. Supported boot schemes are 'single_image' and 'multi_image' " )
212
196
213
197
214
198
def sign_application (toolchain , tools , binary , image_id ):
@@ -228,28 +212,46 @@ def sign_application(toolchain, tools, binary, image_id):
228
212
+ str (image_id ) + " is " + hex (address ) + ", " + hex (size ))
229
213
230
214
231
- def find_policy (toolchain , resources ):
215
+ def find_policy (toolchain ):
232
216
"""
233
- Locate path to policy file, defined in targets.json
217
+ Locate path to policy file, by name defined in targets.json
234
218
:param toolchain: toolchain object from mbed build system
235
- :param resources: resources object from mbed build system
236
219
"""
237
- policy_filename = toolchain .target .policy_file
238
220
239
- if policy_filename is None :
240
- return None
241
- # Locate user-specified image
242
- from tools .resources import FileType
243
- json_files = resources .get_file_paths (FileType .JSON )
244
- policy = next ((f for f in json_files if os .path .basename (f ) == policy_filename ), None )
221
+ mbed_os_root = Path (os .getcwd ())
222
+
223
+ policy_path = Path (toolchain .target .policy_file )
224
+
225
+ # Absolute path provided
226
+ if policy_path .is_absolute ():
227
+ policy_file = policy_path
228
+
229
+ # May also be relative to mbed-os file scturcture
230
+ else :
231
+ policy_path = mbed_os_root / policy_path
232
+
233
+ if os .path .exists (str (policy_path )):
234
+ policy_file = policy_path
235
+
236
+ else :
237
+ default_path = Path ("targets/TARGET_Cypress/TARGET_PSOC6/" ) / \
238
+ Path ("TARGET_" + toolchain .target .name ) / Path ("policy" ) / \
239
+ toolchain .target .policy_file
240
+
241
+ # Consider default location
242
+ policy_file = mbed_os_root / default_path
243
+
244
+ if not os .path .exists (str (policy_file )):
245
+ policy_file = mbed_os_root / "mbed-os" / default_path
246
+
245
247
246
- if policy :
247
- toolchain .notify .info ("Policy file found: %s." % policy )
248
+ if os . path . exists ( str ( policy_file )) :
249
+ toolchain .notify .info ("Policy file found: %s." % policy_file )
248
250
else :
249
- toolchain .notify .info ("Policy file %s not found. Aborting." % policy_filename )
251
+ toolchain .notify .info ("Policy file %s not found. Aborting." % policy_path )
250
252
raise ConfigException ("Required policy file not found." )
251
253
252
- return policy
254
+ return policy_file
253
255
254
256
255
257
0 commit comments