-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiap_gui.py
More file actions
490 lines (395 loc) · 16.7 KB
/
Copy pathiap_gui.py
File metadata and controls
490 lines (395 loc) · 16.7 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python3
import sys
import asyncio
import zipfile
import hashlib
from PyQt6.QtWidgets import (
QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout,
QLabel, QListWidget, QFileDialog, QProgressBar, QTextEdit,
QCheckBox, QFrame, QSizePolicy
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from bleak import BleakScanner, BleakClient
from iap_flash_v2 import IapFlasher, extract_firmware
from ninebot_ble import NinebotSession
# ------------------------------------------------------------
# Version decode + Register read (G3/G2/GT/P-Series)
# ------------------------------------------------------------
def u16_le(b: bytes) -> int:
return b[0] | (b[1] << 8)
def to_version_str(val: int) -> str:
n = val & 0xFFFFFFFF
parts = []
while n > 0 or len(parts) < 3:
parts.insert(0, n & 0xF)
n >>= 4
return ".".join(str(x) for x in parts)
async def read_reg(session, dst, reg, length=2):
data = bytes((length & 0xFF, (length >> 8) & 0xFF))
resp = await session.request(dst, 0x01, reg, data)
return bytes(resp["data"])
async def fetch_versions(session):
versions = {}
# BLE-Version: dst=0x04, reg=0x01
ble_raw = await read_reg(session, 0x04, 0x01)
versions["ble"] = to_version_str(u16_le(ble_raw)) if len(ble_raw) >= 2 else "?"
# VCU-Version: dst=0x16, reg=0x17
vcu_raw = await read_reg(session, 0x16, 0x17)
versions["vcu"] = to_version_str(u16_le(vcu_raw)) if len(vcu_raw) >= 2 else "?"
# MCU-Version: erst direkt über MCU (dst=0x02, reg=0x19)
mcu_raw = await read_reg(session, 0x02, 0x19)
if len(mcu_raw) < 2:
mcu_raw = await read_reg(session, 0x16, 0x18)
versions["mcu"] = to_version_str(u16_le(mcu_raw)) if len(mcu_raw) >= 2 else "?"
# BMS-Version: über VCU (dst=0x16, reg=0x19)
bms_raw = await read_reg(session, 0x16, 0x19)
versions["bms"] = to_version_str(u16_le(bms_raw)) if len(bms_raw) >= 2 else "?"
return versions
# ------------------------------------------------------------
# Worker-Thread für Flash
# ------------------------------------------------------------
class FlashWorker(QThread):
log = pyqtSignal(str)
progress = pyqtSignal(int)
finished = pyqtSignal()
versions = pyqtSignal(dict)
def __init__(self, address, ble_name, zip_path, partition, safe_mode):
super().__init__()
self.address = address
self.ble_name = ble_name
self.zip_path = zip_path
self.partition = partition
self.safe_mode = safe_mode
async def _run_async(self):
self.log.emit("[*] Lade Firmware...")
fw_bytes = extract_firmware(self.zip_path)
self.log.emit(f"[*] Firmware: {len(fw_bytes)} Bytes")
async with BleakClient(self.address) as client:
session = NinebotSession(client, self.ble_name)
await session.start()
# Versionen holen (NEU)
self.log.emit("[*] Lese Firmware-Versionen...")
try:
vers = await fetch_versions(session)
self.versions.emit(vers)
self.log.emit(f"[+] FW: BLE {vers['ble']} | VCU {vers['vcu']} | MCU {vers['mcu']} | BMS {vers['bms']}")
except Exception as e:
self.log.emit(f"[!] Versions-Fehler: {e}")
self.log.emit("[*] Pairing...")
await session.pair()
self.log.emit("[+] Authentifiziert.")
flasher = IapFlasher(session, self.partition, live=True)
self.log.emit("[*] Sende BEGIN...")
await flasher.begin(len(fw_bytes))
page_size = flasher.cfg["page_size"]
total = len(fw_bytes)
total_pages = -(-total // page_size)
done = 0
for page in range(total_pages):
payload = fw_bytes[page * page_size : page * page_size + page_size]
await flasher._request(
0x08, page & 0xFF, payload, {0x08, 0x0B}, flasher.cfg["page_timeout"]
)
done += min(page_size, total - done)
pct = int((done / total) * 100)
self.progress.emit(pct)
self.log.emit(f"[{pct}%] Page {page+1}/{total_pages}")
self.log.emit("[*] Sende Checksumme...")
await flasher.verify_checksum(fw_bytes)
if self.safe_mode:
self.log.emit("[!] Safe-Mode aktiv: RESET wird NICHT gesendet.")
else:
self.log.emit("[*] Sende RESET...")
await flasher.reset()
self.log.emit("[+] Flash abgeschlossen.")
self.finished.emit()
def run(self):
asyncio.run(self._run_async())
# ------------------------------------------------------------
# GUI
# ------------------------------------------------------------
class FlashGUI(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("ScooterHacking – Ninebot Max G3 Flasher")
self.resize(800, 600)
# Dark Theme StyleSheet (SH-Style)
self.setStyleSheet("""
QWidget {
background-color: #0d0f12;
color: #e8e8e8;
font-family: Segoe UI, Arial;
}
QFrame#MainPanel {
background-color: #15181d;
border-radius: 16px;
border: 1px solid #22252b;
}
QLabel#TitleLabel {
font-size: 26px;
font-weight: 600;
color: #e8e8e8;
}
QLabel#SubLabel {
font-size: 13px;
color: #9aa0a6;
}
QListWidget {
background-color: #101318;
border-radius: 8px;
border: 1px solid #22252b;
}
QTextEdit {
background-color: #101318;
border-radius: 8px;
border: 1px solid #22252b;
color: #e8e8e8;
}
""")
outer = QVBoxLayout()
outer.setContentsMargins(40, 40, 40, 40)
panel = QFrame()
panel.setObjectName("MainPanel")
panel_layout = QVBoxLayout()
panel_layout.setContentsMargins(30, 30, 30, 30)
panel_layout.setSpacing(20)
# Header
title = QLabel("Ready to Flash")
title.setObjectName("TitleLabel")
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
subtitle = QLabel("Select your scooter and a firmware package to get started.")
subtitle.setObjectName("SubLabel")
subtitle.setAlignment(Qt.AlignmentFlag.AlignCenter)
panel_layout.addWidget(title)
panel_layout.addWidget(subtitle)
# BLE list
mid_row = QHBoxLayout()
mid_row.setSpacing(20)
left_col = QVBoxLayout()
left_label = QLabel("Scooters")
left_label.setObjectName("SubLabel")
left_col.addWidget(left_label)
self.ble_list = QListWidget()
self.ble_list.setMinimumHeight(150)
left_col.addWidget(self.ble_list)
scan_btn = QPushButton("Scan")
scan_btn.clicked.connect(self.scan_ble)
left_col.addWidget(scan_btn)
mid_row.addLayout(left_col)
# Firmware info
right_col = QVBoxLayout()
right_label = QLabel("Firmware")
right_label.setObjectName("SubLabel")
right_col.addWidget(right_label)
self.zip_info = QLabel("Keine Firmware ausgewählt")
self.zip_info.setObjectName("SubLabel")
right_col.addWidget(self.zip_info)
btn_row = QHBoxLayout()
self.btn_select_zip = QPushButton("Load ZIP")
self.btn_select_zip.clicked.connect(self.select_zip)
btn_row.addWidget(self.btn_select_zip)
self.btn_flash = QPushButton("FLASH FIRMWARE")
self.btn_flash.clicked.connect(self.start_flash)
btn_row.addWidget(self.btn_flash)
right_col.addLayout(btn_row)
self.safe_mode = QCheckBox("Safe Mode (kein RESET)")
right_col.addWidget(self.safe_mode)
mid_row.addLayout(right_col)
panel_layout.addLayout(mid_row)
# Progress
self.progress = QProgressBar()
panel_layout.addWidget(self.progress)
# Log
log_label = QLabel("Log")
log_label.setObjectName("SubLabel")
panel_layout.addWidget(log_label)
self.log = QTextEdit()
self.log.setReadOnly(True)
panel_layout.addWidget(self.log)
# Version info panel
self.version_info = QLabel("BMS: ? BLE: ? VCU: ? MCU: ?")
self.version_info.setObjectName("SubLabel")
panel_layout.addWidget(self.version_info)
panel.setLayout(panel_layout)
outer.addWidget(panel)
self.setLayout(outer)
self.zip_path = None
self.worker = None
self.scan_thread = None
self.auto_thread = None
def log_msg(self, msg):
self.log.append(msg)
# ------------------------------------------------------------
# ZIP-Auswahl + ZIP-Info
# ------------------------------------------------------------
def select_zip(self):
path, _ = QFileDialog.getOpenFileName(self, "Firmware ZIP auswählen", "", "ZIP Dateien (*.zip)")
if not path:
return
self.zip_path = path
try:
with zipfile.ZipFile(path, "r") as z:
names = z.namelist()
size = sum(z.getinfo(n).file_size for n in names)
md5 = hashlib.md5(open(path, "rb").read()).hexdigest()
self.zip_info.setText(
f"ZIP: {path}\n"
f"Files: {len(names)}\n"
f"Size: {size} Bytes\n"
f"MD5: {md5}"
)
except Exception as e:
self.zip_info.setText(f"[!] Fehler beim Lesen: {e}")
self.log_msg(f"[+] Firmware ausgewählt: {path}")
# ------------------------------------------------------------
# Auto-Connect + Auto-Pair + Versionen abrufen (NEU)
# ------------------------------------------------------------
def auto_connect(self, address, name):
if self.auto_thread and self.auto_thread.isRunning():
return
self.log_msg(f"[*] Verbinde automatisch mit {name} ({address}) ...")
class AutoThread(QThread):
log = pyqtSignal(str)
versions = pyqtSignal(dict)
def run(self_inner):
async def _run():
try:
client = BleakClient(address)
await client.connect()
session = NinebotSession(client, name)
await session.start()
self_inner.log.emit("[*] Pairing... Bitte Power-Button drücken!")
await session.pair()
self_inner.log.emit("[+] Pairing erfolgreich!")
# Versionen abrufen (NEU)
self_inner.log.emit("[*] Lese Firmware-Versionen...")
try:
vers = await fetch_versions(session)
self_inner.versions.emit(vers)
self_inner.log.emit(f"[+] FW: BLE {vers['ble']} | VCU {vers['vcu']} | MCU {vers['mcu']} | BMS {vers['bms']}")
except Exception as e:
self_inner.log.emit(f"[!] Versions-Fehler: {e}")
await client.disconnect()
except Exception as e:
self_inner.log.emit(f"[!] Auto-Connect Fehler: {e}")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(_run())
loop.close()
def update_versions(v):
self.version_info.setText(
f"BMS: {v.get('bms','?')} "
f"BLE: {v.get('ble','?')} "
f"VCU: {v.get('vcu','?')} "
f"MCU: {v.get('mcu','?')}"
)
self.auto_thread = AutoThread()
self.auto_thread.log.connect(self.log_msg)
self.auto_thread.versions.connect(update_versions)
self.auto_thread.start()
# ------------------------------------------------------------
# BLE-SCAN
# ------------------------------------------------------------
def scan_ble(self):
self.log_msg("[*] Scanne BLE-Geräte...")
self.ble_list.clear()
class ScanThread(QThread):
result = pyqtSignal(list)
log = pyqtSignal(str)
def run(self_inner):
async def _scan():
try:
devices = await BleakScanner.discover(timeout=2.0)
return devices
except Exception as e:
return e
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
devices = loop.run_until_complete(_scan())
loop.close()
if isinstance(devices, Exception):
self_inner.log.emit(f"[!] Scan-Fehler: {devices}")
self_inner.result.emit([])
else:
self_inner.result.emit(devices)
def update_list(devices):
self.ble_list.clear()
for d in devices:
name = d.name or ""
if not name.startswith("1C"):
continue
entry = f"{d.address} | {name}"
self.ble_list.addItem(entry)
# Auto-Connect starten
self.auto_connect(d.address, name)
self.log_msg("[+] Scan abgeschlossen.")
self.scan_thread = ScanThread()
self.scan_thread.result.connect(update_list)
self.scan_thread.log.connect(self.log_msg)
self.scan_thread.start()
# ------------------------------------------------------------
# Flash starten
# ------------------------------------------------------------
def start_flash(self):
if not self.zip_path:
self.log_msg("[!] Keine Firmware ausgewählt.")
return
item = self.ble_list.currentItem()
if not item:
self.log_msg("[!] Kein BLE-Gerät ausgewählt.")
return
address, ble_name = item.text().split(" | ")
self.worker = FlashWorker(
address=address,
ble_name=ble_name,
zip_path=self.zip_path,
partition="mcu",
safe_mode=self.safe_mode.isChecked()
)
self.worker.log.connect(self.log_msg)
self.worker.progress.connect(self.progress.setValue)
self.worker.versions.connect(self.update_versions)
def on_finished():
self.log_msg("[+] Vorgang abgeschlossen.")
self.worker.quit()
self.worker.wait()
self.worker.finished.connect(on_finished)
self.worker.start()
# ------------------------------------------------------------
# Versionen aktualisieren
# ------------------------------------------------------------
def update_versions(self, v):
try:
self.version_info.setText(
f"BMS: {v.get('bms','?')} "
f"BLE: {v.get('ble','?')} "
f"VCU: {v.get('vcu','?')} "
f"MCU: {v.get('mcu','?')}"
)
except:
self.version_info.setText("BMS: ? BLE: ? VCU: ? MCU: ?")
# ------------------------------------------------------------
# Thread sauber beenden
# ------------------------------------------------------------
def closeEvent(self, event):
try:
if self.worker and self.worker.isRunning():
self.worker.quit()
self.worker.wait()
if self.scan_thread and self.scan_thread.isRunning():
self.scan_thread.quit()
self.scan_thread.wait()
if self.auto_thread and self.auto_thread.isRunning():
self.auto_thread.quit()
self.auto_thread.wait()
except:
pass
event.accept()
# ------------------------------------------------------------
# Start
# ------------------------------------------------------------
if __name__ == "__main__":
app = QApplication(sys.argv)
gui = FlashGUI()
gui.show()
sys.exit(app.exec())