| 179 |
theseven |
1 |
#!/usr/bin/env python
|
|
|
2 |
#
|
|
|
3 |
#
|
|
|
4 |
# Copyright 2010 TheSeven
|
|
|
5 |
#
|
|
|
6 |
#
|
|
|
7 |
# This file is part of emBIOS.
|
|
|
8 |
#
|
|
|
9 |
# emBIOS is free software: you can redistribute it and/or
|
|
|
10 |
# modify it under the terms of the GNU General Public License as
|
|
|
11 |
# published by the Free Software Foundation, either version 2 of the
|
|
|
12 |
# License, or (at your option) any later version.
|
|
|
13 |
#
|
|
|
14 |
# emBIOS is distributed in the hope that it will be useful,
|
|
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
17 |
# See the GNU General Public License for more details.
|
|
|
18 |
#
|
|
|
19 |
# You should have received a copy of the GNU General Public License
|
|
|
20 |
# along with emBIOS. If not, see <http://www.gnu.org/licenses/>.
|
|
|
21 |
#
|
|
|
22 |
#
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
import sys
|
|
|
26 |
import struct
|
|
|
27 |
import time
|
|
|
28 |
import libembios
|
|
|
29 |
from libembios import Error
|
|
|
30 |
import libembiosdata
|
|
|
31 |
|
|
|
32 |
|
| 272 |
theseven |
33 |
def s5l8701cryptdfu(data):
|
| 179 |
theseven |
34 |
data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
|
|
|
35 |
header = "87011.0\0\0\x08\0\0" + struct.pack("<I", len(data))
|
|
|
36 |
embios = libembios.Embios()
|
|
|
37 |
embios.write(0x08000000, header.ljust(0x800, "\0") + data)
|
| 180 |
theseven |
38 |
embios.lib.dev.timeout = 20000
|
| 179 |
theseven |
39 |
embios.hmac_sha1(0x08000800, len(data), 0x08000010)
|
|
|
40 |
embios.hmac_sha1(0x08000000, 0x40, 0x08000040)
|
|
|
41 |
embios.aesencrypt(0x08000000, len(data) + 0x800, 1)
|
|
|
42 |
return embios.read(0x08000000, len(data) + 0x800)
|
|
|
43 |
|
|
|
44 |
|
| 272 |
theseven |
45 |
def s5l8701decryptdfu(data):
|
| 179 |
theseven |
46 |
embios = libembios.Embios()
|
|
|
47 |
embios.write(0x08000000, data)
|
| 180 |
theseven |
48 |
embios.lib.dev.timeout = 20000
|
| 179 |
theseven |
49 |
embios.aesdecrypt(0x08000000, len(data), 1)
|
|
|
50 |
return embios.read(0x08000800, len(data) - 0x800)
|
|
|
51 |
|
|
|
52 |
|
| 272 |
theseven |
53 |
def s5l8701cryptfirmware(data):
|
| 179 |
theseven |
54 |
data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
|
| 186 |
theseven |
55 |
header = "\0\0\0\0\x02\0\0\0\x01\0\0\0\x40\0\0\0\0\0\0\0" + struct.pack("<I", len(data))
|
| 179 |
theseven |
56 |
embios = libembios.Embios()
|
|
|
57 |
embios.write(0x08000000, header.ljust(0x800, "\0") + data)
|
| 180 |
theseven |
58 |
embios.lib.dev.timeout = 20000
|
| 179 |
theseven |
59 |
embios.hmac_sha1(0x08000800, len(data), 0x0800001c)
|
|
|
60 |
embios.hmac_sha1(0x08000000, 0x200, 0x080001d4)
|
|
|
61 |
embios.aesencrypt(0x08000800, len(data), 1)
|
|
|
62 |
return embios.read(0x08000000, len(data) + 0x800)
|
|
|
63 |
|
|
|
64 |
|
| 272 |
theseven |
65 |
def s5l8701decryptfirmware(data):
|
| 179 |
theseven |
66 |
embios = libembios.Embios()
|
|
|
67 |
embios.write(0x08000000, data)
|
| 180 |
theseven |
68 |
embios.lib.dev.timeout = 20000
|
| 179 |
theseven |
69 |
embios.aesdecrypt(0x08000800, len(data) - 0x800, 1)
|
|
|
70 |
return embios.read(0x08000800, len(data) - 0x800)
|
|
|
71 |
|
|
|
72 |
|
| 272 |
theseven |
73 |
def s5l8701cryptdfufile(infile, outfile):
|
| 179 |
theseven |
74 |
print(outfile)
|
|
|
75 |
infile = open(infile, "rb")
|
|
|
76 |
outfile = open(outfile, "wb")
|
| 272 |
theseven |
77 |
outfile.write(s5l8701cryptdfu(infile.read()))
|
| 179 |
theseven |
78 |
infile.close()
|
|
|
79 |
outfile.close()
|
|
|
80 |
|
|
|
81 |
|
| 272 |
theseven |
82 |
def s5l8701decryptdfufile(infile, outfile):
|
| 179 |
theseven |
83 |
infile = open(infile, "rb")
|
|
|
84 |
outfile = open(outfile, "wb")
|
| 272 |
theseven |
85 |
outfile.write(s5l8701decryptdfu(infile.read()))
|
| 179 |
theseven |
86 |
infile.close()
|
|
|
87 |
outfile.close()
|
|
|
88 |
|
|
|
89 |
|
| 272 |
theseven |
90 |
def s5l8701cryptfirmwarefile(infile, outfile):
|
| 179 |
theseven |
91 |
infile = open(infile, "rb")
|
|
|
92 |
outfile = open(outfile, "wb")
|
| 272 |
theseven |
93 |
outfile.write(s5l8701cryptfirmware(infile.read()))
|
| 179 |
theseven |
94 |
infile.close()
|
|
|
95 |
outfile.close()
|
|
|
96 |
|
|
|
97 |
|
| 272 |
theseven |
98 |
def s5l8701decryptfirmwarefile(infile, outfile):
|
| 179 |
theseven |
99 |
infile = open(infile, "rb")
|
|
|
100 |
outfile = open(outfile, "wb")
|
| 272 |
theseven |
101 |
outfile.write(s5l8701decryptfirmware(infile.read()))
|
| 179 |
theseven |
102 |
infile.close()
|
|
|
103 |
outfile.close()
|