Skip to content

Commit a726385

Browse files
committed
iot2050-firmware-update: Improve min_version calculation in backup
Refactor the firmware update script to more robustly calculate the `min_version` during the backup process. The previous implementation was brittle, as it incorrectly assumed the version prefix was always a single character. This caused failures with version strings that did not have a prefix (e.g., "1.6.2"). This change introduces a new `_get_min_version` helper method that uses a regular expression to parse the version string. It correctly identifies numeric components and replaces any number greater than 1 with a '1', while preserving the original zero-padding. The logic now first isolates the main version tag (the portion before any hyphen) and then passes it to the new helper method to ensure a correct and reliable `min_version` is generated for the backup configuration. Signed-off-by: Li Hua Qian <huaqian.li@siemens.com>
1 parent efe7030 commit a726385

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

meta-example/recipes-app/iot2050-firmware-update/files/iot2050-firmware-update.tmpl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
#
3-
# Copyright (c) Siemens AG, 2020-2024
3+
# Copyright (c) Siemens AG, 2020-2026
44
#
55
# Authors:
66
# Chao Zeng <chao.zeng@siemens.com>
@@ -122,6 +122,7 @@ from io import StringIO
122122
from progress.bar import Bar
123123
from threading import Thread, Event
124124
from types import SimpleNamespace as Namespace
125+
import re
125126

126127
class ErrorCode(Enum):
127128
"""The ErrorCode class describes the return codes"""
@@ -489,6 +490,20 @@ class FirmwareUpdate():
489490
except UpgradeError as e:
490491
raise UpgradeError(e.err, e.code)
491492

493+
def _get_min_version(self, version_str):
494+
"""Calculate the minimal version from a version string component."""
495+
parts = re.split(r'(\d+)', version_str)
496+
result = []
497+
for part in parts:
498+
if part.isdigit():
499+
if int(part) > 1:
500+
result.append('1'.zfill(len(part)))
501+
else:
502+
result.append(part)
503+
else:
504+
result.append(part)
505+
return "".join(result)
506+
492507
def backup(self):
493508
"""Backup the original firmware from flash"""
494509
print("\nFirmware backup started")
@@ -521,10 +536,10 @@ class FirmwareUpdate():
521536
if "BUILD_ID" in key:
522537
break
523538
target_board = model_f.read().replace("\u0000", "")
539+
version_tag = value.replace('"', '').split('-')[0]
524540
tmpl_json['firmware'][0]['target_boards'] = target_board
525541
tmpl_json['target_os'][0]['min_version'] = \
526-
value.replace('"', '')[:1] + \
527-
tmpl_json['target_os'][0]['min_version'][1:]
542+
self._get_min_version(version_tag)
528543
file_content = bytes(json.dumps(tmpl_json, indent=4), "utf8")
529544
else:
530545
raise UpgradeError("Wrong Firmware Type!")

0 commit comments

Comments
 (0)