[HASS] Backup và Restore firmware gốc của sonoff

Software cần thiết

  1. Python (giải nén hoặc cài đặt)
  2. Esptool (giải nén)

Khi cài đặt Python nhớ đặt path cho dòng lệnh
setx path “%path%;path-to-python”

Mở cửa sổ Command Prompt (Start > Run > cmd) và chuyển đến thư mục Esptool.

Chạy lệnh

python setup.py install

C:\Users\xxxx\Desktop\esptool-2.6>python setup.py install
running install
running bdist_egg
running egg_info
creating esptool.egg-info
writing esptool.egg-info\PKG-INFO
writing dependency_links to esptool.egg-info\dependency_links.txt
writing entry points to esptool.egg-info\entry_points.txt
writing requirements to esptool.egg-info\requires.txt
writing top-level names to esptool.egg-info\top_level.txt
writing manifest file 'esptool.egg-info\SOURCES.txt'
reading manifest file 'esptool.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'ecdsa'
no previously-included directories found matching 'pyaes'
no previously-included directories found matching 'flasher_stub'
no previously-included directories found matching '.github'
warning: no previously-included files found matching '.git*'
warning: no previously-included files found matching '.travis*'
writing manifest file 'esptool.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
copying esptool.py -> build\lib
copying espsecure.py -> build\lib
copying espefuse.py -> build\lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
copying build\lib\espefuse.py -> build\bdist.win-amd64\egg
copying build\lib\espsecure.py -> build\bdist.win-amd64\egg
copying build\lib\esptool.py -> build\bdist.win-amd64\egg
byte-compiling build\bdist.win-amd64\egg\espefuse.py to espefuse.cpython-37.pyc
byte-compiling build\bdist.win-amd64\egg\espsecure.py to espsecure.cpython-37.pyc
byte-compiling build\bdist.win-amd64\egg\esptool.py to esptool.cpython-37.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\entry_points.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying esptool.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
__pycache__.esptool.cpython-37: module MAY be using inspect.trace
creating dist
creating 'dist\esptool-2.6-py3.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing esptool-2.6-py3.7.egg
creating c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages\esptool-2.6-py3.7.egg
Extracting esptool-2.6-py3.7.egg to c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages
Adding esptool 2.6 to easy-install.pth file
Installing espefuse.py-script.py script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts
Installing espefuse.py.exe script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts
Installing espsecure.py-script.py script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts
Installing espsecure.py.exe script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts
Installing esptool.py-script.py script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts
Installing esptool.py.exe script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts

Installed c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages\esptool-2.6-py3.7.egg
Processing dependencies for esptool==2.6
Searching for ecdsa
Reading https://pypi.org/simple/ecdsa/
Downloading https://files.pythonhosted.org/packages/63/f4/73669d51825516ce8c43b816c0a6b64cd6eb71d08b99820c00792cb42222/ecdsa-0.13-py2.py3-none-any.whl#sha256=40d002cf360d0e035cf2cb985e1308d41aaa087cbfc135b2dc2d844296ea546c
Best match: ecdsa 0.13
Processing ecdsa-0.13-py2.py3-none-any.whl
Installing ecdsa-0.13-py2.py3-none-any.whl to c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages
Adding ecdsa 0.13 to easy-install.pth file

Installed c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages\ecdsa-0.13-py3.7.egg
Searching for pyaes
Reading https://pypi.org/simple/pyaes/
Downloading https://files.pythonhosted.org/packages/44/66/2c17bae31c906613795711fc78045c285048168919ace2220daa372c7d72/pyaes-1.6.1.tar.gz#sha256=02c1b1405c38d3c370b085fb952dd8bea3fadcee6411ad99f312cc129c536d8f
Best match: pyaes 1.6.1
Processing pyaes-1.6.1.tar.gz
Writing C:\Users\xxxx\AppData\Local\Temp\easy_install-dbhhp9kx\pyaes-1.6.1\setup.cfg
Running pyaes-1.6.1\setup.py -q bdist_egg --dist-dir C:\Users\xxxx\AppData\Local\Temp\easy_install-dbhhp9kx\pyaes-1.6.1\egg-dist-tmp-1w1pjo0w
zip_safe flag not set; analyzing archive contents...
Copying pyaes-1.6.1-py3.7.egg to c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages
Adding pyaes 1.6.1 to easy-install.pth file

Installed c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages\pyaes-1.6.1-py3.7.egg
Searching for pyserial>=3.0
Reading https://pypi.org/simple/pyserial/
Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl#sha256=e0770fadba80c31013896c7e6ef703f72e7834965954a78e71a3049488d4d7d8
Best match: pyserial 3.4
Processing pyserial-3.4-py2.py3-none-any.whl
Installing pyserial-3.4-py2.py3-none-any.whl to c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages
Adding pyserial 3.4 to easy-install.pth file
Installing miniterm.py script to C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Scripts

Installed c:\users\xxxx\appdata\local\programs\python\python37\lib\site-packages\pyserial-3.4-py3.7.egg
Finished processing dependencies for esptool==2.6

A. Chuẩn bị board sonoff ở flash mode như ở bài này, xem sonoff dùng port nào (Control Panel > System > Device Manager > Ports)

B. Xác định flash size

esptool.py --port COMxxx flash_id
C:\Users\xxxx\Desktop\esptool-2.6>esptool.py --port COM5 flash_id
esptool.py v2.6
Serial port COM5
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: dc:4f:xx:xx:xx:xx
Uploading stub...
Running stub...
Stub running...
Manufacturer: 51
Device: 4014
Detected flash size: 1MB
Hard resetting via RTS pin...

C. Backup

esptool.py --port COM5 read_flash 0x00000 0x100000 image1M.bin
  • Giá trị 0x00000 0x100000 để chép về 1M. Flash rom của sonoff T1 chỉ có khoảng 500KB nên chỉ backup 1MB là đủ
  • Giá trị 0x00000 0x400000 để backup 4MB

C:\Users\xxxx\Desktop\esptool-2.6>esptool.py –port COM5 read_flash 0x00000 0x100000 image1M.bin
esptool.py v2.6
Serial port COM5
Connecting….
Detecting chip type… ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: dc:4f:xx:xx:xx:xx
Uploading stub…
Running stub…
Stub running…
1048576 (100 %)
1048576 (100 %)
Read 1048576 bytes at 0x0 in 95.5 seconds (87.8 kbit/s)…
Hard resetting via RTS pin…

D. Xóa bộ nhớ flash

Luôn luôn nên xóa bộ nhớ flash trước khi flash rom khác.

esptool.py –port COM5 erase_flash

C:\Users\xxxx\Desktop\esptool-2.6>esptool.py –port COM5 erase_flash
esptool.py v2.6
Serial port COM5
Connecting….
Detecting chip type… ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: 60:01:94:c8:07:f6
Uploading stub…
Running stub…
Stub running…
Erasing flash (this may take a while)…
Chip erase completed successfully in 2.7s
Hard resetting via RTS pin…

E. Flash rom khác

esptool.py –port COM5 write_flash -fs 1MB -fm dout 0x0 sonoff.bin

Thay đổi 1MB thành 4MB nếu dùng rom 4MB

esptool.py v2.6
Serial port COM5
Connecting……
Detecting chip type… ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: 60:01:xx:xx:xx:xx
Uploading stub…
Running stub…
Stub running…
Configuring flash size…
Compressed 1048576 bytes to 307043…
Wrote 1048576 bytes (307043 compressed) at 0x00000000 in 27.2 seconds (effective 308.3 kbit/s)…
Hash of data verified.

Leaving…
Hard resetting via RTS pin…

Chú thích

  • Mỗi bước đều reset lại RTS pin nên phải chuẩn bị lại board mỗi khi sang bước khác.
  • Nếu flash một rom backup cho nhiều board thì các board đó giống hệt nhau. Mỗi lúc chỉ có một board kết nối wifi để làm việc!

Comments Off on [HASS] Backup và Restore firmware gốc của sonoff

Filed under Software

Comments are closed.