wiflash 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #! /bin/bash
  2. #
  3. # Flash an esp8266 over wifi. This communicates with the esphttpd's /flash handlers
  4. # and POSTS the correct binary depending on the parittion that needs to be flashed
  5. # next.
  6. #
  7. # ----------------------------------------------------------------------------
  8. # "THE BEER-WARE LICENSE" (Revision 42):
  9. # Thorsten von Eicken wrote this file. As long as you retain
  10. # this notice you can do whatever you want with this stuff. If we meet some day,
  11. # and you think this stuff is worth it, you can buy me a beer in return.
  12. # ----------------------------------------------------------------------------
  13. show_help() {
  14. cat <<EOT
  15. Usage: ${0##*/} [-options...] hostname user1.bin user2.bin
  16. Flash the esp8266 running esphttpd at <hostname> with either <user1.bin> or <user2.bin>
  17. depending on its current state. Reboot the esp8266 after flashing and wait for it to come
  18. up again.
  19. -v Be verbose
  20. -h show this help
  21. Example: ${0##*/} -v esp8266 firmware/user1.bin firmware/user2.bin
  22. ${0##*/} 192.168.4.1 firmware/user1.bin firmware/user2.bin
  23. EOT
  24. }
  25. if ! which curl >/dev/null; then
  26. echo "ERROR: Cannot find curl: it is required for this script." >&2
  27. exit 1
  28. fi
  29. start=`date +%s`
  30. # ===== Parse arguments
  31. verbose=
  32. while getopts "hvx:" opt; do
  33. case "$opt" in
  34. h) show_help; exit 0 ;;
  35. v) verbose=1 ;;
  36. x) foo="$OPTARG" ;;
  37. '?') show_help >&2; exit 1 ;;
  38. esac
  39. done
  40. # Shift off the options and optional --.
  41. shift "$((OPTIND-1))"
  42. # Get the fixed arguments
  43. if [[ $# != 3 ]]; then
  44. show_help >&2
  45. exit 1
  46. fi
  47. hostname=$1
  48. user1=$2
  49. user2=$3
  50. re='[-A-Za-z0-9.]+'
  51. if [[ ! "$hostname" =~ $re ]]; then
  52. echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2
  53. exit 1
  54. fi
  55. if [[ ! -r "$user1" ]]; then
  56. echo "ERROR: cannot read user1 firmware file ($user1)" >&2
  57. exit 1
  58. fi
  59. if [[ ! -r "$user2" ]]; then
  60. echo "ERROR: cannot read user2 firmware file ($user2)" >&2
  61. exit 1
  62. fi
  63. # ===== Retrieve the 'next' firmware required
  64. fw=
  65. while true; do
  66. [[ -n "$verbose" ]] && echo "Fetching http://$hostname/flash/next" >&2
  67. v=; [[ -n "$verbose" ]] && v=-v
  68. next=`curl -m 10 $v -s "http://$hostname/flash/next"`
  69. if [[ $? != 0 ]]; then
  70. echo "Error retrieving http://$hostname/flash/next" >&2
  71. exit 1
  72. fi
  73. case "$next" in
  74. user1.bin)
  75. echo "Flashing user1.bin" >&2
  76. fw="$user1"
  77. break;;
  78. user2.bin)
  79. echo "Flashing user2.bin" >&2
  80. fw="$user2"
  81. break;;
  82. *)
  83. echo "Error retrieving or parsing http://$hostname/flash/next" >&2
  84. exit 1
  85. ;;
  86. esac
  87. done
  88. #silent=-s
  89. [[ -n "$verbose" ]] && silent=
  90. res=`curl $silent -XPOST --data-binary "@$fw" "http://$hostname/flash/upload"`
  91. if [[ $? != 0 ]]; then
  92. echo "Error flashing $fw" >&2
  93. exit 1
  94. fi
  95. sleep 2
  96. echo "Rebooting into new firmware" >&2
  97. curl -m 10 -s "http://$hostname/flash/reboot"
  98. sleep 2
  99. echo "Waiting for ESP8266 to come back"
  100. while true; do
  101. [[ -n "$verbose" ]] && echo "Fetching http://$hostname/flash/next" >&2
  102. next2=`curl -m 10 $v -s "http://$hostname/flash/next"`
  103. [[ -n "$verbose" ]] && echo "got: $next2"
  104. re='user[12]\.bin'
  105. if [[ "$next2" =~ $re ]]; then
  106. if [[ "$next2" != "$next" ]]; then
  107. sec=$(( `date +%s` - $start ))
  108. echo "Success, took $sec seconds" >&2
  109. exit 0
  110. else
  111. echo "Flashing seems to have failed and it reverted to the old firmware?" >&2
  112. exit 1
  113. fi
  114. fi
  115. sleep 1
  116. done