Skip to content

Instantly share code, notes, and snippets.

@ozanerturk
Created March 5, 2025 14:49
Show Gist options
  • Save ozanerturk/1b89be472f8aeed0fcda3fde87dff881 to your computer and use it in GitHub Desktop.
Save ozanerturk/1b89be472f8aeed0fcda3fde87dff881 to your computer and use it in GitHub Desktop.
ESP binary merger script for production releases
chip=esp32
outfolder=production
mkdir -p $outfolder
rm -rf $outfolder/*
# get application bin name
application_bin_name=$(grep "project(" CMakeLists.txt | cut -d "(" -f2 | cut -d ")" -f1)
echo "application bin name: $application_bin_name"
# build application
idf.py build
#err
if [ $? -ne 0 ]; then
echo "build failed again, exiting..."
exit 1
fi
#generate merged binary
FLASHER_ARGS_FILE="build/flasher_args.json"
# Check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found. Please install it (e.g., sudo apt install jq)"
exit 1
fi
#
# Prepare flash file list dynamically from the "flash_files" section
FLASH_FILES=$(jq -r '.flash_files | to_entries[] | "\(.key) \(.value)"' $FLASHER_ARGS_FILE)
echo "Flash files: $FLASH_FILES"
# Construct the esptool.py merge_bin command dynamically
MERGE_CMD="esptool.py --chip $chip merge_bin -o ${outfolder}/$application_bin_name.merged.bin"
# Add each offset and corresponding file to the merge_bin command
while IFS= read -r entry; do
OFFSET=$(echo $entry | awk '{print $1}')
FILE=$(echo $entry | awk '{print $2}')
MERGE_CMD+=" $OFFSET build/$FILE"
done <<< "$FLASH_FILES"
# Display the full command
echo "Running command: $MERGE_CMD"
# Execute the command
eval $MERGE_CMD
if [ $? -ne 0 ]; then
echo "merge failed"
exit 1
fi
#copy production
cp ./build/$application_bin_name.bin ${outfolder}/$application_bin_name.bin
cp ./build/ota_data_initial.bin ${outfolder}/ota_data_initial.bin
cp ./build/partition_table/partition-table.bin ${outfolder}/partition_table.bin
cp ./build/bootloader/bootloader.bin ${outfolder}/bootloader.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment