From bd3755bb336f7100c4c249271d0077d621cdf3f0 Mon Sep 17 00:00:00 2001 From: Jeremiah K <17190268+jeremiah-k@users.noreply.github.com> Date: Wed, 6 Nov 2024 19:43:34 -0600 Subject: [PATCH] Fix device flashing scripts so they work with esptool when it's installed via pipx (#5269) * Try esptool.py in device flashing scripts for pipx compatibility * esptool detection fixes in device flashing .bat's --- bin/device-install.bat | 28 ++++++++++++++++++---------- bin/device-install.sh | 24 ++++++++++++++++++------ bin/device-update.bat | 16 ++++++++++++---- bin/device-update.sh | 18 +++++++++++++++--- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/bin/device-install.bat b/bin/device-install.bat index 6c880185e8..f8ca9e4086 100755 --- a/bin/device-install.bat +++ b/bin/device-install.bat @@ -2,6 +2,14 @@ set PYTHON=python +:: Determine the correct esptool command to use +where esptool >nul 2>&1 +if %ERRORLEVEL% EQU 0 ( + set "ESPTOOL_CMD=esptool" +) else ( + set "ESPTOOL_CMD=%PYTHON% -m esptool" +) + goto GETOPTS :HELP echo Usage: %~nx0 [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME^|FILENAME] @@ -24,32 +32,32 @@ IF NOT "__%1__"=="____" goto GETOPTS IF "__%FILENAME%__" == "____" ( echo "Missing FILENAME" - goto HELP + goto HELP ) IF EXIST %FILENAME% IF x%FILENAME:update=%==x%FILENAME% ( echo Trying to flash update %FILENAME%, but first erasing and writing system information" - %PYTHON% -m esptool --baud 115200 erase_flash - %PYTHON% -m esptool --baud 115200 write_flash 0x00 %FILENAME% + %ESPTOOL_CMD% --baud 115200 erase_flash + %ESPTOOL_CMD% --baud 115200 write_flash 0x00 %FILENAME% @REM Account for S3 and C3 board's different OTA partition IF x%FILENAME:s3=%==x%FILENAME% IF x%FILENAME:v3=%==x%FILENAME% IF x%FILENAME:t-deck=%==x%FILENAME% IF x%FILENAME:wireless-paper=%==x%FILENAME% IF x%FILENAME:wireless-tracker=%==x%FILENAME% IF x%FILENAME:station-g2=%==x%FILENAME% IF x%FILENAME:unphone=%==x%FILENAME% ( IF x%FILENAME:esp32c3=%==x%FILENAME% ( - %PYTHON% -m esptool --baud 115200 write_flash 0x260000 bleota.bin + %ESPTOOL_CMD% --baud 115200 write_flash 0x260000 bleota.bin ) else ( - %PYTHON% -m esptool --baud 115200 write_flash 0x260000 bleota-c3.bin + %ESPTOOL_CMD% --baud 115200 write_flash 0x260000 bleota-c3.bin ) - ) else ( - %PYTHON% -m esptool --baud 115200 write_flash 0x260000 bleota-s3.bin + ) else ( + %ESPTOOL_CMD% --baud 115200 write_flash 0x260000 bleota-s3.bin ) for %%f in (littlefs-*.bin) do ( - %PYTHON% -m esptool --baud 115200 write_flash 0x300000 %%f + %ESPTOOL_CMD% --baud 115200 write_flash 0x300000 %%f ) ) else ( echo "Invalid file: %FILENAME%" - goto HELP + goto HELP ) else ( echo "Invalid file: %FILENAME%" - goto HELP + goto HELP ) :EOF diff --git a/bin/device-install.sh b/bin/device-install.sh index 563a87af47..b2a5684eef 100755 --- a/bin/device-install.sh +++ b/bin/device-install.sh @@ -2,6 +2,18 @@ PYTHON=${PYTHON:-$(which python3 python | head -n 1)} +# 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 + set -e # Usage info @@ -49,19 +61,19 @@ shift "$((OPTIND - 1))" if [ -f "${FILENAME}" ] && [ -n "${FILENAME##*"update"*}" ]; then echo "Trying to flash ${FILENAME}, but first erasing and writing system information" - "$PYTHON" -m esptool erase_flash - "$PYTHON" -m esptool write_flash 0x00 ${FILENAME} + $ESPTOOL_CMD erase_flash + $ESPTOOL_CMD write_flash 0x00 ${FILENAME} # Account for S3 board's different OTA partition if [ -n "${FILENAME##*"s3"*}" ] && [ -n "${FILENAME##*"-v3"*}" ] && [ -n "${FILENAME##*"t-deck"*}" ] && [ -n "${FILENAME##*"wireless-paper"*}" ] && [ -n "${FILENAME##*"wireless-tracker"*}" ] && [ -n "${FILENAME##*"station-g2"*}" ] && [ -n "${FILENAME##*"unphone"*}" ]; then if [ -n "${FILENAME##*"esp32c3"*}" ]; then - "$PYTHON" -m esptool write_flash 0x260000 bleota.bin + $ESPTOOL_CMD write_flash 0x260000 bleota.bin else - "$PYTHON" -m esptool write_flash 0x260000 bleota-c3.bin + $ESPTOOL_CMD write_flash 0x260000 bleota-c3.bin fi else - "$PYTHON" -m esptool write_flash 0x260000 bleota-s3.bin + $ESPTOOL_CMD write_flash 0x260000 bleota-s3.bin fi - "$PYTHON" -m esptool write_flash 0x300000 littlefs-*.bin + $ESPTOOL_CMD write_flash 0x300000 littlefs-*.bin else show_help diff --git a/bin/device-update.bat b/bin/device-update.bat index 2ac649be44..a52f3d33f6 100755 --- a/bin/device-update.bat +++ b/bin/device-update.bat @@ -2,6 +2,14 @@ set PYTHON=python +:: Determine the correct esptool command to use +where esptool >nul 2>&1 +if %ERRORLEVEL% EQU 0 ( + set "ESPTOOL_CMD=esptool" +) else ( + set "ESPTOOL_CMD=%PYTHON% -m esptool" +) + goto GETOPTS :HELP echo Usage: %~nx0 [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME^|FILENAME] @@ -24,17 +32,17 @@ IF NOT "__%1__"=="____" goto GETOPTS IF "__%FILENAME%__" == "____" ( echo "Missing FILENAME" - goto HELP + goto HELP ) IF EXIST %FILENAME% IF NOT x%FILENAME:update=%==x%FILENAME% ( echo Trying to flash update %FILENAME% - %PYTHON% -m esptool --baud 115200 write_flash 0x10000 %FILENAME% + %ESPTOOL_CMD% --baud 115200 write_flash 0x10000 %FILENAME% ) else ( echo "Invalid file: %FILENAME%" - goto HELP + goto HELP ) else ( echo "Invalid file: %FILENAME%" - goto HELP + goto HELP ) :EOF diff --git a/bin/device-update.sh b/bin/device-update.sh index 7233f61f6e..67281dc4f7 100755 --- a/bin/device-update.sh +++ b/bin/device-update.sh @@ -2,6 +2,18 @@ PYTHON=${PYTHON:-$(which python3 python|head -n 1)} +# 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 + # Usage info show_help() { cat << EOF @@ -9,7 +21,7 @@ Usage: $(basename $0) [-h] [-p ESPTOOL_PORT] [-P PYTHON] [-f FILENAME|FILENAME] Flash image file to device, leave existing system intact." -h Display this help and exit - -p ESPTOOL_PORT Set the environment variable for ESPTOOL_PORT. If not set, ESPTOOL iterates all ports (Dangerrous). + -p ESPTOOL_PORT Set the environment variable for ESPTOOL_PORT. If not set, ESPTOOL iterates all ports (Dangerous). -P PYTHON Specify alternate python interpreter to use to invoke esptool. (Default: "$PYTHON") -f FILENAME The *update.bin file to flash. Custom to your device type. @@ -30,7 +42,7 @@ while getopts ":hp:P:f:" opt; do f) FILENAME=${OPTARG} ;; *) - echo "Invalid flag." + echo "Invalid flag." show_help >&2 exit 1 ;; @@ -45,7 +57,7 @@ shift "$((OPTIND-1))" if [ -f "${FILENAME}" ] && [ -z "${FILENAME##*"update"*}" ]; then printf "Trying to flash update ${FILENAME}" - $PYTHON -m esptool --baud 115200 write_flash 0x10000 ${FILENAME} + $ESPTOOL_CMD --baud 115200 write_flash 0x10000 ${FILENAME} else show_help echo "Invalid file: ${FILENAME}"