|
| 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" |
0 commit comments