Skip to content

Commit 3031898

Browse files
authored
Merge pull request #15027 from OpenNuvoton/nuvoton_m2354_tfm_fw_version
M2354: Generate unique MCUboot image version
2 parents 3eae29b + dcd30e0 commit 3031898

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

targets/TARGET_NUVOTON/scripts/NUVOTON.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,45 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
5454
# Find Python 3 command name across platforms
5555
python3_cmd = "python3" if shutil.which("python3") is not None else "python"
5656

57-
img_ver_major = 1
58-
img_ver_minor = 3
59-
img_ver_revision = 0
60-
img_ver_build = 0
57+
# Specify image version
58+
#
59+
# MCUboot image version format: Major.Minor.Revision+Build
60+
#
61+
# Requirements for image version:
62+
# 1. Major.Minor.Revision must be non-decremental when used to derive security
63+
# counter (-s 'auto').
64+
# 2. Make Major.Minor.Revision+Build incremental to identify the firmware
65+
# itself uniquely through psa_fwu_query().
66+
# 3. Get around MCUboot failure with:
67+
# [INF] Starting bootloader
68+
# [INF] Swap type: none
69+
# [ERR] Failed to add Image 0 data to shared memory area
70+
# [ERR] Unable to find bootable image
71+
# This is because TF-M underestimates MAX_BOOT_RECORD_SZ for boot record
72+
# where Major.Minor.Revision will pack into during signing. The more digits
73+
# of the Major.Minor.Revision, the larger the needed boot record size. And
74+
# then MCUboot errors in boot_save_boot_status().
75+
#
76+
# To meet all the above requirements, we apply the following policy:
77+
# 1. To not change MAX_BOOT_RECORD_SZ in TF-M, specify Major.Minor.Revision
78+
# with TF-M version instead of modified Unix timestamp. This needs less digits to
79+
# fit into MAX_BOOT_RECORD_SZ.
80+
# 2. To make Major.Minor.Revision+Build incremental, specify the Build part with
81+
# modified Unix timestamp.
82+
# 3. To make security counter non-decremental, we can derive it from
83+
# Major.Minor.Revision (-s 'auto') or explicitly specify it with modified
84+
# Unix timestamp, depending on security consideration.
85+
#
86+
# NOTE: To get around Y2038 problem, we modify Unix timestamp by setting new base
87+
# point. Using 32-bit unsigned integer to hold the modified Unix timestamp,
88+
# it will break (wrap around) after Y2156 (2106 + 2020 - 1970).
89+
# https://en.wikipedia.org/wiki/Year_2038_problem
90+
#
91+
modified_timestamp = int(datetime.now().timestamp()) - int(datetime(2020, 1, 1).timestamp())
92+
img_ver_major = 1 # Instead of (modified_timestamp >> 24) & 0xFF
93+
img_ver_minor = 3 # Instead of (modified_timestamp >> 16) & 0xFF
94+
img_ver_revision = 0 # Instead of modified_timestamp & 0xFFFF
95+
img_ver_build = modified_timestamp
6196

6297
# wrapper.py command template
6398
cmd_wrapper = [
@@ -82,7 +117,7 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
82117
'0x400',
83118
"--overwrite-only",
84119
"-s",
85-
'auto',
120+
'auto', # Or modified_timestamp
86121
"-d",
87122
'(IMAGE_ID,MAJOR.MINOR.REVISION+BUILD)',
88123
"RAW_BIN_PATH",

tools/targets/NU_M2354.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,45 @@ def m2354_tfm_bin(t_self, non_secure_image, secure_bin):
6262
# Find Python 3 command name across platforms
6363
python3_cmd = "python3" if shutil.which("python3") is not None else "python"
6464

65-
img_ver_major = 1
66-
img_ver_minor = 3
67-
img_ver_revision = 0
68-
img_ver_build = 0
65+
# Specify image version
66+
#
67+
# MCUboot image version format: Major.Minor.Revision+Build
68+
#
69+
# Requirements for image version:
70+
# 1. Major.Minor.Revision must be non-decremental when used to derive security
71+
# counter (-s 'auto').
72+
# 2. Make Major.Minor.Revision+Build incremental to identify the firmware
73+
# itself uniquely through psa_fwu_query().
74+
# 3. Get around MCUboot failure with:
75+
# [INF] Starting bootloader
76+
# [INF] Swap type: none
77+
# [ERR] Failed to add Image 0 data to shared memory area
78+
# [ERR] Unable to find bootable image
79+
# This is because TF-M underestimates MAX_BOOT_RECORD_SZ for boot record
80+
# where Major.Minor.Revision will pack into during signing. The more digits
81+
# of the Major.Minor.Revision, the larger the needed boot record size. And
82+
# then MCUboot errors in boot_save_boot_status().
83+
#
84+
# To meet all the above requirements, we apply the following policy:
85+
# 1. To not change MAX_BOOT_RECORD_SZ in TF-M, specify Major.Minor.Revision
86+
# with TF-M version instead of modified Unix timestamp. This needs less digits to
87+
# fit into MAX_BOOT_RECORD_SZ.
88+
# 2. To make Major.Minor.Revision+Build incremental, specify the Build part with
89+
# modified Unix timestamp.
90+
# 3. To make security counter non-decremental, we can derive it from
91+
# Major.Minor.Revision (-s 'auto') or explicitly specify it with modified
92+
# Unix timestamp, depending on security consideration.
93+
#
94+
# NOTE: To get around Y2038 problem, we modify Unix timestamp by setting new base
95+
# point. Using 32-bit unsigned integer to hold the modified Unix timestamp,
96+
# it will break (wrap around) after Y2156 (2106 + 2020 - 1970).
97+
# https://en.wikipedia.org/wiki/Year_2038_problem
98+
#
99+
modified_timestamp = int(datetime.now().timestamp()) - int(datetime(2020, 1, 1).timestamp())
100+
img_ver_major = 1 # Instead of (modified_timestamp >> 24) & 0xFF
101+
img_ver_minor = 3 # Instead of (modified_timestamp >> 16) & 0xFF
102+
img_ver_revision = 0 # Instead of modified_timestamp & 0xFFFF
103+
img_ver_build = modified_timestamp
69104

70105
# wrapper.py command template
71106
cmd_wrapper = [
@@ -90,7 +125,7 @@ def m2354_tfm_bin(t_self, non_secure_image, secure_bin):
90125
'0x400',
91126
"--overwrite-only",
92127
"-s",
93-
'auto',
128+
'auto', # Or modified_timestamp
94129
"-d",
95130
'(IMAGE_ID,MAJOR.MINOR.REVISION+BUILD)',
96131
"RAW_BIN_PATH",

0 commit comments

Comments
 (0)