Skip to content

Commit f05a343

Browse files
author
Cruz Monrreal
authored
Merge pull request #9894 from kfnta/find_secure_image
PSA tools: Find secure image at post-build
2 parents f3ecc0b + 1f7a829 commit f05a343

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

tools/psa/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
# Copyright (c) 2019 ARM Limited
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
from os.path import basename, splitext
19+
from tools.resources import FileType
20+
21+
22+
def find_secure_image(notify, resources, ns_image_path, configured_s_image_filename, image_type):
23+
""" Find secure image. """
24+
25+
assert ns_image_path and configured_s_image_filename, 'ns_image_path and configured_s_image_path are mandatory'
26+
assert image_type in [FileType.BIN, FileType.HEX], 'image_type must be of type BIN or HEX'
27+
28+
image_files = resources.get_file_paths(image_type)
29+
assert image_files, 'No image files found for this target'
30+
31+
secure_image = next((f for f in image_files if basename(f) == configured_s_image_filename), None)
32+
secure_image = next(
33+
(f for f in image_files if splitext(basename(f))[0] == splitext(basename(ns_image_path))[0]),
34+
secure_image
35+
)
36+
37+
if secure_image:
38+
notify.debug("Secure image file found: %s." % secure_image)
39+
else:
40+
notify.debug("Secure image file %s not found. Aborting." % configured_s_image_filename)
41+
raise Exception("Required secure image not found.")
42+
43+
return secure_image
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2019 ARM Limited
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import pytest
18+
import os
19+
from tools.notifier.mock import MockNotifier
20+
from tools.resources import Resources, FileType
21+
from tools.psa import find_secure_image
22+
23+
24+
def test_find_secure_image():
25+
mock_notifier = MockNotifier()
26+
mock_resources = Resources(mock_notifier)
27+
ns_image_path = os.path.join('BUILD', 'TARGET_NS', 'app.bin')
28+
ns_test_path = os.path.join('BUILD', 'TARGET_NS', 'test.bin')
29+
config_s_image_name = 'target_config.bin'
30+
default_bin = os.path.join('prebuilt', config_s_image_name)
31+
test_bin = os.path.join('prebuilt', 'test.bin')
32+
33+
with pytest.raises(Exception, match='ns_image_path and configured_s_image_path are mandatory'):
34+
find_secure_image(mock_notifier, mock_resources, None, None, FileType.BIN)
35+
find_secure_image(mock_notifier, mock_resources, ns_image_path, None, FileType.BIN)
36+
find_secure_image(mock_notifier, mock_resources, None, config_s_image_name, FileType.BIN)
37+
38+
with pytest.raises(Exception, match='image_type must be of type BIN or HEX'):
39+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, None)
40+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.C_SRC)
41+
42+
with pytest.raises(Exception, match='No image files found for this target'):
43+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
44+
45+
dummy_bin = os.path.join('path', 'to', 'dummy.bin')
46+
mock_resources.add_file_ref(FileType.BIN, dummy_bin, dummy_bin)
47+
48+
with pytest.raises(Exception, match='Required secure image not found'):
49+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
50+
51+
mock_resources.add_file_ref(FileType.BIN, default_bin, default_bin)
52+
mock_resources.add_file_ref(FileType.BIN, test_bin, test_bin)
53+
secure_image = find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
54+
assert secure_image == default_bin
55+
56+
secure_image = find_secure_image(mock_notifier, mock_resources, ns_test_path, config_s_image_name, FileType.BIN)
57+
assert secure_image == test_bin

0 commit comments

Comments
 (0)