Skip to content

Commit

Permalink
update merge script to merge all configs automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
WantClue committed Oct 29, 2024
1 parent 7e6e0cc commit 334fd67
Showing 1 changed file with 31 additions and 71 deletions.
102 changes: 31 additions & 71 deletions merge_bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ BOOTLOADER_BIN="build/bootloader/bootloader.bin"
BOOTLOADER_BIN_ADDR=0x0
PARTITION_TABLE="build/partition_table/partition-table.bin"
PARTITION_TABLE_ADDR=0x8000
CONFIG_BIN="config.bin"
CONFIG_BIN_ADDR=0x9000
MINER_BIN="build/esp-miner.bin"
MINER_BIN_ADDR=0x10000
Expand All @@ -17,32 +16,19 @@ OTA_BIN_ADDR=0xf10000
BINS_DEFAULT=($BOOTLOADER_BIN $PARTITION_TABLE $MINER_BIN $WWW_BIN $OTA_BIN)
BINS_AND_ADDRS_DEFAULT=($BOOTLOADER_BIN_ADDR $BOOTLOADER_BIN $PARTITION_TABLE_ADDR $PARTITION_TABLE $MINER_BIN_ADDR $MINER_BIN $WWW_BIN_ADDR $WWW_BIN $OTA_BIN_ADDR $OTA_BIN)

BINS_WITH_CONFIG=($BOOTLOADER_BIN $PARTITION_TABLE $CONFIG_BIN $MINER_BIN $WWW_BIN $OTA_BIN)
BINS_AND_ADDRS_WITH_CONFIG=($BOOTLOADER_BIN_ADDR $BOOTLOADER_BIN $PARTITION_TABLE_ADDR $PARTITION_TABLE $CONFIG_BIN_ADDR $CONFIG_BIN $MINER_BIN_ADDR $MINER_BIN $WWW_BIN_ADDR $WWW_BIN $OTA_BIN_ADDR $OTA_BIN)

BINS_UPDATE=($MINER_BIN $WWW_BIN $OTA_BIN)
BINS_AND_ADDRS_UPDATE=($MINER_BIN_ADDR $MINER_BIN $WWW_BIN_ADDR $WWW_BIN $OTA_BIN_ADDR $OTA_BIN)


function show_help() {
echo "Creates a combined binary using esptool's merge_bin command"
echo "Usage: $0 [OPTION] output_file"
echo " output_file: The combined binary"
echo "Creates combined binaries using esptool's merge_bin command for multiple config files"
echo "Usage: $0 [OPTION]"
echo " Options:"
echo " -c: Include $CONFIG_BIN in addition to default binaries into output_file"
echo " -u: Only include the following binaries into output_file:"
echo " ${BINS_UPDATE[@]}"
echo " If no options are specified, by default the following binaries are included:"
echo " ${BINS_DEFAULT[@]}"
echo " -c: Process all config-*.bin files in the current directory"
echo " -h: Show this help message"
echo
}


function print_with_error_header() {
echo "ERROR:" $1
}


#### MAIN ####

# Check if esptool.py is installed and accessible
Expand All @@ -55,19 +41,15 @@ fi
OPTIND=1 # Reset in case getops has been used previously

# default values
output_file=""
update_only=0
with_config=0
process_configs=0

while getopts "huc" opt; do
while getopts "hc" opt; do
case "$opt" in
h)
show_help
exit 0
;;
c) with_config=1
;;
u) update_only=1
c) process_configs=1
;;
*)
show_help
Expand All @@ -77,56 +59,34 @@ done

shift $((OPTIND-1))

[ "${1:-}" = "--" ] && shift

# Extract output_file argument
output_file="$1"

if [ -z "$output_file" ]; then
print_with_error_header "output_file missing"
echo
if [ "$process_configs" -eq 0 ]; then
print_with_error_header "No option specified. Use -c to process config files."
show_help
exit 2
fi

if [ "$update_only" -eq 1 ] && [ "$with_config" -eq 1 ]; then
print_with_error_header "Include only one of '-c' and '-u'"
exit 3
fi

selected_bins=()
selected_bins_and_addrs=()
esptool_leading_args="--chip esp32s3 merge_bin --flash_mode dio --flash_size 16MB --flash_freq 80m"

if [ "$update_only" -eq 1 ]; then
selected_bins+=(${BINS_UPDATE[@]})
selected_bins_and_addrs+=(${BINS_AND_ADDRS_UPDATE[@]})
esptool_leading_args+=" --format hex"
else
if [ "$with_config" -eq 1 ]; then
selected_bins+=(${BINS_WITH_CONFIG[@]})
selected_bins_and_addrs+=(${BINS_AND_ADDRS_WITH_CONFIG[@]})
else
selected_bins+=(${BINS_DEFAULT[@]})
selected_bins_and_addrs+=(${BINS_AND_ADDRS_DEFAULT[@]})
fi
fi

for file in "${selected_bins[@]}"; do
if [ ! -f "$file" ]; then
print_with_error_header "Required file $file does not exist. Make sure to build first."
echo "Exiting"
exit 4
# Process all config-*.bin files
for config_file in config-*.bin; do
if [ -f "$config_file" ]; then
# Extract the number from the config filename
config_number=$(echo $config_file | sed 's/config-\(.*\)\.bin/\1/')

# Create the output filename
output_file="esp-miner-factory-$config_number.bin"

# Prepare the bins and addresses array with the current config file
BINS_AND_ADDRS_WITH_CONFIG=(${BINS_AND_ADDRS_DEFAULT[@]} $CONFIG_BIN_ADDR $config_file)

# Call esptool.py with the specified arguments
esptool.py --chip esp32s3 merge_bin --flash_mode dio --flash_size 16MB --flash_freq 80m "${BINS_AND_ADDRS_WITH_CONFIG[@]}" -o "$output_file"

# Check if esptool.py command was successful
if [ $? -eq 0 ]; then
echo "Successfully created $output_file"
else
print_with_error_header "Failed to create $output_file"
fi
fi
done

# Call esptool.py with the specified arguments
esptool.py $esptool_leading_args "${selected_bins_and_addrs[@]}" -o "$output_file"

# Check if esptool.py command was successful
if [ $? -eq 0 ]; then
echo "Successfully created $output_file"
else
print_with_error_header "Failed to create $output_file"
exit 5
fi
echo "Processing complete."

0 comments on commit 334fd67

Please sign in to comment.