-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflasher.py
264 lines (245 loc) · 10.3 KB
/
flasher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#! /usr/bin/env python
import argparse
import configparser
import glob
import os
import os.path
import RPi.GPIO as GPIO
import serial
import subprocess
import sys
import threading
from time import sleep
PYTHON=sys.executable
configFilePath = ("/boot/code/config.ini")
config = configparser.ConfigParser()
config.read(configFilePath)
projectPath = config.get('DEFAULT', 'projectPath')
isEncrypt = config.get('DEFAULT', 'isEncrypt')
if isEncrypt == 'True':
bootloaderPath = config.get('ENCRYPT', 'bootloaderPath')
partitionsPath = config.get('ENCRYPT', 'partitionsPath')
otaDataPath = config.get('ENCRYPT', 'otaDataPath')
appDataPath = config.get('ENCRYPT', 'appDataPath')
secureBootloaderKeyPath = config.get('ENCRYPT', 'secureBootloaderKeyPath')
flashEcryptionKeyPath = config.get('ENCRYPT', 'flashEcryptionKeyPath')
else:
bootloaderPath = config.get('DEFAULT', 'bootloaderPath')
partitionsPath = config.get('DEFAULT', 'partitionsPath')
otaDataPath = config.get('DEFAULT', 'otaDataPath')
appDataPath = config.get('DEFAULT', 'appDataPath')
secureBootloaderKeyPath = config.get('DEFAULT', 'secureBootloaderKeyPath')
flashEcryptionKeyPath = config.get('DEFAULT', 'flashEcryptionKeyPath')
flashButton = int(config.get('DEFAULT', 'flashButton'))
reFlashButton = int(config.get('DEFAULT', 'reFlashButton'))
rebootButton = int(config.get('DEFAULT', 'rebootButton'))
flashingLED = int(config.get('DEFAULT', 'flashingLED'))
reFlashLED = int(config.get('DEFAULT', 'reFlashLED'))
readyLED = int(config.get('DEFAULT', 'readyLED'))
# config LED and button
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(flashButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(reFlashButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(rebootButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(flashingLED, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(reFlashLED, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(readyLED, GPIO.OUT, initial=GPIO.LOW)
reFlashPorts = [] # list reflash port
flashFlag = False
reFlashFlag = False
rebootFlag = False
def _get_args(type, esptool_path, port, baud):
if type == "burn_secure_key":
result = [ PYTHON, esptool_path ]
result += [ "--port", port ]
result += [ "--do-not-confirm" ]
result += [ "--before", "default_reset" ]
result += [ "burn_key" ]
result += [ "secure_boot", secureBootloaderKeyPath ]
return result
elif type == "burn_flash_encryption_key":
result = [ PYTHON, esptool_path ]
result += [ "--port", port ]
result += [ "--do-not-confirm" ]
result += [ "--before", "default_reset" ]
result += [ "burn_key" ]
result += [ "flash_encryption", flashEcryptionKeyPath ]
return result
elif type == "burn_efuse_cnt":
result = [ PYTHON, esptool_path ]
result += [ "--port", port ]
result += [ "--do-not-confirm" ]
result += [ "--before", "default_reset" ]
result += [ "burn_efuse" ]
result += [ "FLASH_CRYPT_CNT", "1" ]
return result
elif type == "burn_efuse_config":
result = [ PYTHON, esptool_path ]
result += [ "--port", port ]
result += [ "--do-not-confirm" ]
result += [ "--before", "default_reset" ]
result += [ "burn_efuse" ]
result += [ "FLASH_CRYPT_CONFIG", "0xf" ]
return result
elif type == "flash":
result = [ PYTHON, esptool_path ]
result += [ "--chip", "esp32" ]
result += [ "--port", port ]
result += [ "--baud", str(baud) ]
result += [ "--before", "default_reset" ]
result += [ "--after", "hard_reset" ]
result += [ "write_flash", "-z" ]
result += [ "--flash_mode", "dio" ]
result += [ "--flash_freq", "40m" ]
result += [ "--flash_size", "detect" ]
result += [ "0x10000", appDataPath ]
result += [ "0x8000", partitionsPath ]
result += [ "0x0", bootloaderPath ]
result += [ "0xe000", otaDataPath ]
return result
elif type == "erase_flash":
result = [ PYTHON, esptool_path ]
result += [ "--port", port ]
result += [ "erase_flash" ]
return result
else:
return 0
# get current USB ports
def _get_ports():
if sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/tty[Uu]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.U*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def _run_tool(tool_name, args1, args2, args3, args4, args5):
def quote_arg(arg):
" Quote 'arg' if necessary "
if " " in arg and not (arg.startswith('"') or arg.startswith("'")):
return "'" + arg + "'"
return arg
def display_command(command):
display_args = " ".join(quote_arg(arg) for arg in command)
print("Running %s in directory %s" % (tool_name, quote_arg(projectPath)))
print('Executing "%s"...' % display_args)
if isEncrypt == 'True':
burnKeyFlag = True
try:
display_command(args1)
subprocess.check_call(args1, env=os.environ, cwd=projectPath)
except subprocess.CalledProcessError as e:
burnKeyFlag = False
print("%s failed with exit code %d" % (tool_name, e.returncode))
try:
display_command(args2)
subprocess.check_call(args2, env=os.environ, cwd=projectPath)
except subprocess.CalledProcessError as e:
burnKeyFlag = False
print("%s failed with exit code %d" % (tool_name, e.returncode))
if burnKeyFlag:
try:
display_command(args3)
subprocess.check_call(args3, env=os.environ, cwd=projectPath)
except subprocess.CalledProcessError as e:
print("%s failed with exit code %d" % (tool_name, e.returncode))
try:
display_command(args4)
subprocess.check_call(args4, env=os.environ, cwd=projectPath)
except subprocess.CalledProcessError as e:
print("%s failed with exit code %d" % (tool_name, e.returncode))
try:
display_command(args5)
subprocess.check_call(args5, env=os.environ, cwd=projectPath)
except subprocess.CalledProcessError as e:
print("%s failed with exit code %d" % (tool_name, e.returncode))
GPIO.output(reFlashLED, GPIO.HIGH) # turn on reflash LED
if (args5[5] in reFlashPorts) == False:
reFlashPorts.append(args5[5]) # add false flash port into reFlashPorts list.
print("%s" % reFlashPorts)
sleep(1)
print("Done")
return 0
def _flash_callback(channel):
print("Flash button is pressed")
global flashFlag
flashFlag = True # enable flash
def _flash():
print("Number of threads: %s" %threading.active_count())
del reFlashPorts[:] # delete old reflash ports
GPIO.output(flashingLED, GPIO.HIGH) # turn on flashing LED (flashing...)
GPIO.output(reFlashLED, GPIO.LOW) # turn off reflash LED
ports = _get_ports() # list current ports
threads = []
for x in ports:
args1 = _get_args("burn_secure_key", os.path.join(projectPath, "esptool/espefuse.py"), x, 2000000)
args2 = _get_args("burn_flash_encryption_key", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args3 = _get_args("burn_efuse_cnt", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args4 = _get_args("burn_efuse_config", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args5 = _get_args("flash", os.path.join(projectPath,"esptool/esptool.py"), x, 2000000)
flashThread = threading.Thread(target=_run_tool, args=("esptool.py", args1, args2, args3, args4, args5))
threads.append(flashThread)
for x in threads:
x.start()
for x in threads:
x.join()
GPIO.output(flashingLED, GPIO.LOW) # turn off flashing LED (flash completed)
global flashFlag
flashFlag = False # disable flash
def _reflash_callback(channel):
print("Reflash button is pressed")
global reFlashFlag
reFlashFlag = True # enable reflash
def _reflash():
threads = []
GPIO.output(flashingLED, GPIO.HIGH) # turn on flashing LED (flashing...)
GPIO.output(reFlashLED, GPIO.LOW)
for x in reFlashPorts:
args1 = _get_args("burn_secure_key", os.path.join(projectPath, "esptool/espefuse.py"), x, 2000000)
args2 = _get_args("burn_flash_encryption_key", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args3 = _get_args("burn_efuse_cnt", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args4 = _get_args("burn_efuse_config", os.path.join(projectPath,"esptool/espefuse.py"), x, 2000000)
args5 = _get_args("flash", os.path.join(projectPath,"esptool/esptool.py"), x, 2000000)
reFlashThread = threading.Thread(target=_run_tool, args=("esptool.py", args1, args2, args3, args4, args5))
threads.append(reFlashThread)
del reFlashPorts[:] # delete old reflash ports
for x in threads:
x.start()
for x in threads:
x.join()
global reFlashFlag
reFlashFlag = False # disable reflash
GPIO.output(flashingLED, GPIO.LOW) # turn off flashing LED (flash completed)
def _reboot_callback(channel):
print("Reboot button is pressed")
GPIO.output(readyLED, GPIO.LOW)
sleep(1)
global rebootFlag
rebootFlag = True # enable reboot
def _reboot():
os.system("sudo reboot")
# add event detect buttons
GPIO.add_event_detect(reFlashButton, GPIO.FALLING, callback=_reflash_callback, bouncetime=2000)
GPIO.add_event_detect(flashButton, GPIO.FALLING, callback=_flash_callback, bouncetime=2000)
GPIO.add_event_detect(rebootButton, GPIO.FALLING, callback=_reboot_callback, bouncetime=2000)
try:
GPIO.output(readyLED, GPIO.HIGH)
while True:
if flashFlag == True:
_flash()
if reFlashFlag == True:
_reflash()
if rebootFlag == True:
_reboot()
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit