2020-07-27 09:14:04 -04:00
|
|
|
#!/bin/sh
|
2020-05-06 19:43:17 -07:00
|
|
|
|
2023-01-21 16:17:08 +01:00
|
|
|
PYTHON=${PYTHON:-$(which python3 python|head -n 1)}
|
2025-06-03 03:35:26 +02:00
|
|
|
CHANGE_MODE=false
|
2021-03-21 18:29:20 -07:00
|
|
|
|
2024-11-06 19:43:34 -06:00
|
|
|
# Determine the correct esptool command to use
|
|
|
|
|
if "$PYTHON" -m esptool version >/dev/null 2>&1; then
|
|
|
|
|
ESPTOOL_CMD="$PYTHON -m esptool"
|
|
|
|
|
elif command -v esptool >/dev/null 2>&1; then
|
|
|
|
|
ESPTOOL_CMD="esptool"
|
|
|
|
|
elif command -v esptool.py >/dev/null 2>&1; then
|
|
|
|
|
ESPTOOL_CMD="esptool.py"
|
|
|
|
|
else
|
|
|
|
|
echo "Error: esptool not found"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2020-07-27 09:14:04 -04:00
|
|
|
# Usage info
|
|
|
|
|
show_help() {
|
2023-01-21 16:17:08 +01:00
|
|
|
cat << EOF
|
2025-06-07 21:04:31 -07:00
|
|
|
Usage: $(basename "$0") [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME|FILENAME] [--change-mode]
|
|
|
|
|
Flash image file to device, leave existing system intact."
|
2020-05-06 19:43:17 -07:00
|
|
|
|
2020-07-27 09:14:04 -04:00
|
|
|
-h Display this help and exit
|
2024-11-06 19:43:34 -06:00
|
|
|
-p ESPTOOL_PORT Set the environment variable for ESPTOOL_PORT. If not set, ESPTOOL iterates all ports (Dangerous).
|
2021-03-21 18:29:20 -07:00
|
|
|
-P PYTHON Specify alternate python interpreter to use to invoke esptool. (Default: "$PYTHON")
|
2022-11-24 12:30:15 +01:00
|
|
|
-f FILENAME The *update.bin file to flash. Custom to your device type.
|
2025-06-03 03:35:26 +02:00
|
|
|
--change-mode Attempt to place the device in correct mode. Some hardware requires this twice. (1200bps Reset)
|
|
|
|
|
|
2020-07-27 09:14:04 -04:00
|
|
|
EOF
|
|
|
|
|
}
|
2020-05-06 19:43:17 -07:00
|
|
|
|
2025-07-03 20:41:17 -04:00
|
|
|
# Check for --change-mode and remove it from arguments
|
|
|
|
|
NEW_ARGS=""
|
|
|
|
|
for arg in "$@"; do
|
|
|
|
|
if [ "$arg" = "--change-mode" ]; then
|
|
|
|
|
CHANGE_MODE=true
|
|
|
|
|
else
|
|
|
|
|
NEW_ARGS="$NEW_ARGS \"\$arg\""
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Reset positional parameters to filtered list
|
|
|
|
|
eval set -- $NEW_ARGS
|
2023-01-21 16:17:08 +01:00
|
|
|
|
2021-03-21 18:29:20 -07:00
|
|
|
while getopts ":hp:P:f:" opt; do
|
2023-01-21 16:17:08 +01:00
|
|
|
case "${opt}" in
|
|
|
|
|
h)
|
|
|
|
|
show_help
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
2025-03-18 02:16:16 +01:00
|
|
|
p) ESPTOOL_CMD="$ESPTOOL_CMD --port ${OPTARG}"
|
2025-06-07 21:04:31 -07:00
|
|
|
;;
|
2023-01-21 16:17:08 +01:00
|
|
|
P) PYTHON=${OPTARG}
|
|
|
|
|
;;
|
|
|
|
|
f) FILENAME=${OPTARG}
|
|
|
|
|
;;
|
|
|
|
|
*)
|
2025-06-07 21:04:31 -07:00
|
|
|
echo "Invalid flag."
|
2023-01-21 16:17:08 +01:00
|
|
|
show_help >&2
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
2020-07-27 09:14:04 -04:00
|
|
|
done
|
2023-01-21 16:17:08 +01:00
|
|
|
shift "$((OPTIND-1))"
|
2020-07-27 09:14:04 -04:00
|
|
|
|
2025-07-03 20:41:17 -04:00
|
|
|
if [ "$CHANGE_MODE" = true ]; then
|
2025-06-03 03:35:26 +02:00
|
|
|
$ESPTOOL_CMD --baud 1200 --after no_reset read_flash_status
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
2025-06-07 21:04:31 -07:00
|
|
|
[ -z "$FILENAME" ] && [ -n "$1" ] && {
|
|
|
|
|
FILENAME="$1"
|
2023-01-21 16:17:08 +01:00
|
|
|
shift
|
2021-04-09 20:02:25 -07:00
|
|
|
}
|
|
|
|
|
|
2022-12-03 08:24:11 -06:00
|
|
|
if [ -f "${FILENAME}" ] && [ -z "${FILENAME##*"update"*}" ]; then
|
2025-06-07 21:04:31 -07:00
|
|
|
echo "Trying to flash update ${FILENAME}"
|
|
|
|
|
$ESPTOOL_CMD --baud 115200 write_flash 0x10000 "${FILENAME}"
|
2020-07-27 09:14:04 -04:00
|
|
|
else
|
2025-06-07 21:04:31 -07:00
|
|
|
show_help
|
|
|
|
|
echo "Invalid file: ${FILENAME}"
|
2020-07-27 09:14:04 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
exit 0
|