Skip to content

Commit 26ceae6

Browse files
committed
chore: append metadata to the end of firmware file
1 parent 56b3aa4 commit 26ceae6

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

append_metadata.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# Credits: claude LMAO
3+
set -e
4+
5+
# Function to display usage information
6+
usage() {
7+
echo "Usage: $0 <binary_file> <version> <firmware> <hardware> <build_time>"
8+
echo " <binary_file>: Path to the .bin file"
9+
echo " <version>: Version string (max 32 chars)"
10+
echo " <firmware>: Firmware string (max 16 chars)"
11+
echo " <hardware>: Hardware string (max 16 chars)"
12+
echo " <build_time>: Build timestamp (unsigned 64-bit number)"
13+
exit 1
14+
}
15+
16+
# Function to validate string length
17+
validate_string() {
18+
local str="$1"
19+
local max_len="$2"
20+
local field_name="$3"
21+
22+
if [ ${#str} -gt $max_len ]; then
23+
echo "Error: $field_name exceeds maximum length of $max_len characters"
24+
exit 1
25+
fi
26+
}
27+
28+
# Function to pad string with zeros
29+
pad_string() {
30+
local str="$1"
31+
local max_len="$2"
32+
printf "%-${max_len}s" "$str" | tr ' ' '\0'
33+
}
34+
35+
# Check if all arguments are provided
36+
if [ $# -ne 5 ]; then
37+
usage
38+
fi
39+
40+
binary_file="$1"
41+
version="$2"
42+
firmware="$3"
43+
hardware="$4"
44+
build_time="$5"
45+
46+
# Check if file exists and has .bin extension
47+
if [ ! -f "$binary_file" ]; then
48+
echo "Error: File '$binary_file' does not exist"
49+
exit 1
50+
fi
51+
52+
if [[ "$binary_file" != *.bin ]]; then
53+
echo "Error: File must have .bin extension"
54+
exit 1
55+
fi
56+
57+
# Validate input lengths
58+
validate_string "$version" 32 "Version"
59+
validate_string "$firmware" 16 "Firmware"
60+
validate_string "$hardware" 16 "Hardware"
61+
62+
# Create temporary file
63+
temp_file=$(mktemp)
64+
65+
# Pad strings and write to temp file
66+
pad_string "$version" 32 > "$temp_file"
67+
pad_string "$firmware" 16 >> "$temp_file"
68+
pad_string "$hardware" 16 >> "$temp_file"
69+
70+
# Convert build_time to 8 bytes and append
71+
printf "%016x" "$build_time" | xxd -r -p >> "$temp_file"
72+
73+
# Append temp file to binary file
74+
if cat "$temp_file" >> "$binary_file"; then
75+
echo "Successfully appended metadata to '$binary_file'"
76+
echo " Version: $version"
77+
echo " Firmware: $firmware"
78+
echo " Hardware: $hardware"
79+
echo " Build Time: $build_time"
80+
else
81+
echo "Error: Failed to append metadata"
82+
rm "$temp_file"
83+
exit 1
84+
fi
85+
86+
# Clean up
87+
rm "$temp_file"

dev-esp32.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
#!/bin/bash
2+
set -e
23
source ~/export-esp.sh
3-
cargo build --no-default-features --features=esp32,"$@" --target=xtensa-esp32-none-elf -r && espflash save-image --chip esp32 ./target/xtensa-esp32-none-elf/release/fkm-firmware "/tmp/fkm-build/v2_STATION_$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2).bin"
4+
5+
cargo build --no-default-features --features=esp32,"$@" --target=xtensa-esp32-none-elf -r
6+
VERSION=$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2)
7+
EPOCH=$(date +%s)
8+
9+
espflash save-image --chip esp32 ./target/xtensa-esp32-none-elf/release/fkm-firmware "/tmp/fkm-build/v2_STATION_${VERSION}.bin"
10+
./append_metadata.sh "/tmp/fkm-build/v2_STATION_${VERSION}.bin" "$VERSION" "STATION" "v2" "$EPOCH"

dev-esp32c3.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
#!/bin/bash
2+
set -e
23
source ~/export-esp.sh
3-
cargo build --no-default-features --features=esp32c3,"$@" --target=riscv32imc-unknown-none-elf -r && espflash save-image --chip esp32c3 ./target/riscv32imc-unknown-none-elf/release/fkm-firmware "/tmp/fkm-build/v3_STATION_$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2).bin"
4+
5+
cargo build --no-default-features --features=esp32c3,"$@" --target=riscv32imc-unknown-none-elf -r
6+
VERSION=$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2)
7+
EPOCH=$(date +%s)
8+
9+
espflash save-image --chip esp32c3 ./target/riscv32imc-unknown-none-elf/release/fkm-firmware "/tmp/fkm-build/v3_STATION_${VERSION}.bin"
10+
./append_metadata.sh "/tmp/fkm-build/v3_STATION_${VERSION}.bin" "$VERSION" "STATION" "v3" "$EPOCH"

release.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
2+
set -e
33
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
44

55
if ! command -v gh &> /dev/null
@@ -27,8 +27,14 @@ source ~/export-esp.sh
2727
RELEASE_BUILD="$RELEASE_VERSION" cargo build -r
2828
RELEASE_BUILD="$RELEASE_VERSION" cargo esp32 -r
2929

30-
espflash save-image --chip esp32 ./target/xtensa-esp32-none-elf/release/fkm-firmware "/tmp/fkm-build/v2_STATION_$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2).bin"
31-
espflash save-image --chip esp32c3 ./target/riscv32imc-unknown-none-elf/release/fkm-firmware "/tmp/fkm-build/v3_STATION_$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2).bin"
30+
VERSION=$(cat ./src/version.rs | grep VERSION | cut -d'"' -f 2)
31+
EPOCH=$(date +%s)
32+
33+
espflash save-image --chip esp32 ./target/xtensa-esp32-none-elf/release/fkm-firmware "/tmp/fkm-build/v2_STATION_${VERSION}.bin"
34+
espflash save-image --chip esp32c3 ./target/riscv32imc-unknown-none-elf/release/fkm-firmware "/tmp/fkm-build/v3_STATION_${VERSION}.bin"
35+
./append_metadata.sh "/tmp/fkm-build/v2_STATION_${VERSION}.bin" "$VERSION" "STATION" "v2" "$EPOCH"
36+
./append_metadata.sh "/tmp/fkm-build/v3_STATION_${VERSION}.bin" "$VERSION" "STATION" "v3" "$EPOCH"
37+
exit 1
3238

3339
cd $SCRIPT_DIR
3440
VERSION=$(cat ./src/version.rs | grep 'VERSION' | cut -d'"' -f 2)

0 commit comments

Comments
 (0)