forked from CamiAlfa/M365_DRV_STLINK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash_m365_PRO2.py
86 lines (81 loc) · 2.53 KB
/
flash_m365_PRO2.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
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
import sys
import math
import argparse
import struct
import enum
import os
import pathlib
import socket
import openocd
import shutil
serial ='26354/00040000'
KM=0
def word2bytes(word):
result=[(word)&0xff,(word>>8)&0xff,(word>>16)&0xff,(word>>24)&0xff]
return bytes(result)
def word16_2bytes(word):
result=[(word)&0xff,(word>>8)&0xff]
return bytes(result)
if __name__ == '__main__':
oocd = openocd.OpenOcd('localhost', 6666)
try:
oocd.connect()
except Exception as e:
sys.exit('Failed to connect to OpenOCD')
shutil.copy('data.bin','data_temp.bin')
scooter_data=open('data_temp.bin','r+b')
# Disable RDP
sys.stdout.write('unsecuring device...\n')
sys.stdout.flush()
oocd.send('init')
oocd.send('reset halt')
oocd.send('stm32f1x unlock 0')
oocd.send('reset')
sys.stdout.write('done\n')
# read UUID
oocd.send('init')
oocd.send('reset halt')
sys.stdout.write('reading UUID\n')
sys.stdout.flush()
UUID = oocd.read_memory(0x1FFFF7E8,3)
output_value = 'done: %08x %08x %08x\n' % (UUID[0],UUID[1],UUID[2])
sys.stdout.write(output_value)
sys.stdout.write('preparing sooter data...\n')
sys.stdout.flush()
scooter_data.seek(0x20)
scooter_data.write(serial.encode())
scooter_data.seek(0x1b4)
scooter_data.write(word2bytes(UUID[0]))
scooter_data.seek(0x1b8)
scooter_data.write(word2bytes(UUID[1]))
scooter_data.seek(0x1bc)
scooter_data.write(word2bytes(UUID[2]))
scooter_data.seek(0x52)
scooter_data.write(word2bytes(KM*1000))
scooter_data.close()
sys.stdout.write('flashing...\n')
sys.stdout.flush()
oocd.write_binary(0x08000000,'boot.bin')
oocd.write_binary(0x08001000,'DRV223.bin')
oocd.write_binary(0x0800f800,'data_temp.bin')
sys.stdout.write('done\n')
UUID2 = oocd.read_memory(0x0800F9B4,3)
output_value = '%08x %08x %08x\n' % (UUID2[0],UUID2[1],UUID2[2])
sys.stdout.write(output_value)
oocd.send('reset')
sys.stdout.write('done\n')
sys.stdout.flush()
input("Press Enter to continue...")