Skip to content

Commit fc42c74

Browse files
committed
Rework find_policy() in post build script to enable default locations
1 parent 66f6e2a commit fc42c74

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

tools/targets/PSOC6.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from shutil import copy2
2525
import json
2626
from intelhex import IntelHex, hex2bin, bin2hex
27+
from pathlib import Path
2728

2829
from ..config import ConfigException
2930

@@ -171,28 +172,10 @@ def sign_image(toolchain, resourses, elf, binf, m0hex):
171172
except KeyError:
172173
raise ConfigException("[PSOC6.sign_image] Target " + toolchain.target.name + " is not supported in cysecuretools.")
173174

174-
from pathlib import Path, PurePath
175+
policy_file = find_policy(toolchain)
175176

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-
189177
toolchain.notify.info("[PSOC6.sign_image] Using policy file: " + str(policy_file))
190178

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))
196179
import cysecuretools
197180

198181
tools = cysecuretools.CySecureTools(secure_target, str(policy_file))
@@ -208,7 +191,8 @@ def sign_image(toolchain, resourses, elf, binf, m0hex):
208191
complete(toolchain, elf, hexf0=binf, hexf1=m0hex)
209192

210193
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' ")
212196

213197

214198
def sign_application(toolchain, tools, binary, image_id):
@@ -228,28 +212,46 @@ def sign_application(toolchain, tools, binary, image_id):
228212
+ str(image_id) + " is " + hex(address) + ", " + hex(size))
229213

230214

231-
def find_policy(toolchain, resources):
215+
def find_policy(toolchain):
232216
"""
233-
Locate path to policy file, defined in targets.json
217+
Locate path to policy file, by name defined in targets.json
234218
:param toolchain: toolchain object from mbed build system
235-
:param resources: resources object from mbed build system
236219
"""
237-
policy_filename = toolchain.target.policy_file
238220

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+
245247

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)
248250
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)
250252
raise ConfigException("Required policy file not found.")
251253

252-
return policy
254+
return policy_file
253255

254256

255257

0 commit comments

Comments
 (0)