Skip to content

Commit efe7030

Browse files
committed
firmware-update-pacakge: Enhance min_version calculation
Improves the logic for calculating the `min_version` in the firmware update package generation script. The new implementation correctly handles various version formats, including those with multi-digit numbers and zero-padding (e.g., `V01.06.12`, `1.6.6`). It replaces any numeric part of the version string greater than 1 with a 1, while preserving the original padding and any non-numeric prefixes. This change makes the version handling more robust and adaptable to different versioning schemes. Additionally, the script has been updated to follow shellcheck best practices, including quoting variables and adding safety checks for directory changes. Signed-off-by: Li Hua Qian <huaqian.li@siemens.com>
1 parent 608da96 commit efe7030

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed
Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
#
3-
# Copyright (c) Siemens AG, 2023
3+
# Copyright (c) Siemens AG, 2023-2026
44
#
55
# Authors:
66
# Li Hua Qian <huaqian.li@siemens.com>
@@ -9,39 +9,82 @@
99
# COPYING.MIT file in the top-level directory.
1010
#
1111

12+
#
13+
# Updates the version fields in the provided JSON file.
14+
#
15+
# This function takes a JSON file and a version string as input. It updates
16+
# the "version" and "description" fields to the new version. It also calculates
17+
# a "min_version" by taking the base version string (before any git hash),
18+
# and replacing any numeric part greater than 1 with a 1, while preserving
19+
# any character prefix and zero-padding.
20+
#
21+
# For example:
22+
# "V01.06.12" -> "V01.01.01"
23+
# "2.0.0" -> "1.0.0"
24+
#
25+
# $1: Path to the JSON file to update.
26+
# $2: The full version string (e.g., "V01.06.12-0-g608da96").
27+
#
1228
update_json()
1329
{
14-
version_letter="$(echo $2 | head -c 1)"
15-
sed -i '/"version": ".*"/s/"V.*"/"'$2'"/g' $1
16-
sed -i '/"description": ".*"/s/V.*["$]/'$2\"'/g' $1
17-
sed -i '/"min_version": ".*"/s/"V/"'$version_letter'/g' $1
30+
sed -i '/"version": ".*"/s/"[^"]*"/"'"$2"'"/g' "$1"
31+
sed -i '/"description": ".*"/s/V.*"/'"$2"'"/g' "$1"
32+
33+
version_tag=$(echo "$2" | cut -d '-' -f 1)
34+
35+
new_version=""
36+
current_num=""
37+
for i in $(seq 1 $((${#version_tag}+1))); do
38+
char=$(echo "$version_tag" | cut -c "$i")
39+
40+
if echo "$char" | grep -q '[0-9]'; then
41+
current_num="$current_num$char"
42+
else
43+
if [ -n "$current_num" ]; then
44+
len=${#current_num}
45+
num_val=$(echo "$current_num" | sed 's/^0*//')
46+
if [ -z "$num_val" ]; then num_val=0; fi
47+
48+
if [ "$num_val" -gt 1 ]; then
49+
new_version="$new_version$(printf "%0${len}d" 1)"
50+
else
51+
new_version="$new_version$current_num"
52+
fi
53+
current_num=""
54+
fi
55+
new_version="$new_version$char"
56+
fi
57+
done
58+
min_version=$new_version
59+
60+
sed -i "/\"min_version\": \".*\"/s/\"[^\"]*\"/\"$min_version\"/g" "$1"
1861
}
1962

2063
generate_fwu_tarball()
2164
{
2265
echo "Generating the firmware tarball..."
2366

24-
if [ ! -e $2/iot2050-pg1-image-boot.bin ] || \
25-
[ ! -e $2/iot2050-pg2-image-boot.bin ]; then
67+
if [ ! -e "$2/iot2050-pg1-image-boot.bin" ] || \
68+
[ ! -e "$2/iot2050-pg2-image-boot.bin" ]; then
2669
echo "Error: iot2050-pg1/2-image-boot.bin doesn't exist!"
2770
exit 2
2871
fi
2972

30-
if [ ! -e $2/u-boot-initial-env ]; then
73+
if [ ! -e "$2/u-boot-initial-env" ]; then
3174
echo "Error: u-boot-initial-env doesn't exist!"
3275
exit 2
3376
fi
3477

35-
mkdir -p $2/.tarball
78+
mkdir -p "$2/.tarball"
3679

37-
cp $1/update.conf.json.tmpl $2/.tarball/update.conf.json
38-
update_json $2/.tarball/update.conf.json $3
39-
cp $2/iot2050-pg*-image-boot.bin $2/.tarball
40-
cp $2/u-boot-initial-env $2/.tarball
80+
cp "$1/update.conf.json.tmpl" "$2/.tarball/update.conf.json"
81+
update_json "$2/.tarball/update.conf.json" "$3"
82+
cp "$2"/iot2050-pg*-image-boot.bin "$2/.tarball"
83+
cp "$2/u-boot-initial-env" "$2/.tarball"
4184

42-
cd $2/.tarball
43-
tar -cJvf $2/IOT2050-FW-Update-PKG-$3.tar.xz *
44-
cd - && rm -rf $2/.tarball
85+
cd "$2/.tarball" || exit
86+
tar -cJvf "$2/IOT2050-FW-Update-PKG-$3.tar.xz" ./*
87+
cd - && rm -rf "$2/.tarball"
4588
}
4689

47-
generate_fwu_tarball $*
90+
generate_fwu_tarball "$@"

0 commit comments

Comments
 (0)