Skip to content

Commit

Permalink
refactor: unify merge_bin scripts (#189)
Browse files Browse the repository at this point in the history
Combines the functionality of merge_bin_update.sh
and merge_bin_with_config.sh into merge_bin.sh.
Also adds more verbose usage printing.
  • Loading branch information
tdb3 authored Jun 20, 2024
1 parent db49aa1 commit ce0be46
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 90 deletions.
125 changes: 109 additions & 16 deletions merge_bin.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
#!/bin/bash

# Check if the number of arguments is correct
if [ "$#" -ne 1 ]; then
echo "Usage: $0 output_file"
exit 1
fi
# Binary file paths and addresses
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
WWW_BIN="build/www.bin"
WWW_BIN_ADDR=0x410000
OTA_BIN="build/ota_data_initial.bin"
OTA_BIN_ADDR=0xf10000

# Extract output_file argument
output_file="$1"
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 " 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
}


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


#### MAIN ####

# Check if esptool.py is installed and accessible
if ! command -v esptool.py &> /dev/null; then
Expand All @@ -16,24 +52,81 @@ if ! command -v esptool.py &> /dev/null; then
exit 1
fi

# Check if the required bin files exist
required_files=("build/bootloader/bootloader.bin" "build/partition_table/partition-table.bin" "build/esp-miner.bin" "build/www.bin" "build/ota_data_initial.bin")
OPTIND=1 # Reset in case getops has been used previously

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

while getopts "huc" opt; do
case "$opt" in
h)
show_help
exit 0
;;
c) with_config=1
;;
u) update_only=1
;;
*)
show_help
exit 1
esac
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
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 8MB --flash_freq 80m"

for file in "${required_files[@]}"; do
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
echo "Required file $file does not exist. Make sure to build first."
echo "Exiting"
exit 1
print_with_error_header "Required file $file does not exist. Make sure to build first."
echo "Exiting"
exit 4
fi
done

# Call esptool.py with the specified arguments
esptool.py --chip esp32s3 merge_bin --flash_mode dio --flash_size 8MB --flash_freq 80m 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/esp-miner.bin 0x410000 build/www.bin 0xf10000 build/ota_data_initial.bin -o "$output_file"
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
echo "Failed to create $output_file"
exit 1
print_with_error_header "Failed to create $output_file"
exit 5
fi
38 changes: 1 addition & 37 deletions merge_bin_update.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
#!/bin/bash

# Check if the number of arguments is correct
if [ "$#" -ne 1 ]; then
echo "Usage: $0 output_file"
exit 1
fi

# Extract output_file argument
output_file="$1"

# Check if esptool.py is installed and accessible
if ! command -v esptool.py &> /dev/null; then
echo "esptool.py is not installed or not in PATH. Please install it first."
echo "pip install esptool"
exit 1
fi

# Check if the required bin files exist
required_files=("build/esp-miner.bin" "build/www.bin" "build/ota_data_initial.bin")

for file in "${required_files[@]}"; do
if [ ! -f "$file" ]; then
echo "Required file $file does not exist. Make sure to build first."
echo "Exiting"
exit 1
fi
done

# Call esptool.py with the specified arguments
esptool.py --chip esp32s3 merge_bin --format hex --flash_mode dio --flash_size 8MB --flash_freq 80m 0x10000 build/esp-miner.bin 0x410000 build/www.bin 0xf10000 build/ota_data_initial.bin -o "$output_file"

# Check if esptool.py command was successful
if [ $? -eq 0 ]; then
echo "Successfully created $output_file"
else
echo "Failed to create $output_file"
exit 1
fi
./merge_bin.sh -u $1
38 changes: 1 addition & 37 deletions merge_bin_with_config.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
#!/bin/bash

# Check if the number of arguments is correct
if [ "$#" -ne 1 ]; then
echo "Usage: $0 output_file"
exit 1
fi

# Extract output_file argument
output_file="$1"

# Check if esptool.py is installed and accessible
if ! command -v esptool.py &> /dev/null; then
echo "esptool.py is not installed or not in PATH. Please install it first."
echo "pip install esptool"
exit 1
fi

# Check if the required bin files exist
required_files=("config.bin" "build/bootloader/bootloader.bin" "build/partition_table/partition-table.bin" "build/esp-miner.bin" "build/www.bin" "build/ota_data_initial.bin")

for file in "${required_files[@]}"; do
if [ ! -f "$file" ]; then
echo "Required file $file does not exist. Make sure to build first."
echo "Exiting"
exit 1
fi
done

# Call esptool.py with the specified arguments
esptool.py --chip esp32s3 merge_bin --flash_mode dio --flash_size 8MB --flash_freq 80m 0x0 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x9000 config.bin 0x10000 build/esp-miner.bin 0x410000 build/www.bin 0xf10000 build/ota_data_initial.bin -o "$output_file"

# Check if esptool.py command was successful
if [ $? -eq 0 ]; then
echo "Successfully created $output_file"
else
echo "Failed to create $output_file"
exit 1
fi
./merge_bin.sh -c $1

0 comments on commit ce0be46

Please sign in to comment.