/
backup.py
217 lines (168 loc) · 7.86 KB
/
backup.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
import abc
from collections import OrderedDict
import io
from ..backup import *
from ..util import *
class BaseBackupProp(abc.ABC):
def __init__(self, dataInterface, size):
self.dataInterface = dataInterface
self.size = size
@abc.abstractmethod
def read(self):
pass
@abc.abstractmethod
def write(self, data):
pass
class BackupProp(BaseBackupProp):
def __init__(self, dataInterface, id, size):
super(BackupProp, self).__init__(dataInterface, size)
self.id = id
def read(self):
data = self.dataInterface.readProp(self.id)
if len(data) != self.size:
raise Exception('Wrong size')
return data
def write(self, data):
if len(data) != self.size:
raise Exception('Wrong size')
self.dataInterface.writeProp(self.id, data)
class CompoundBackupProp(BaseBackupProp):
def __init__(self, dataInterface, props):
super(CompoundBackupProp, self).__init__(dataInterface, sum(size for id, size in props))
self._props = [BackupProp(dataInterface, id, size) for id, size in props]
def read(self):
return b''.join(prop.read() for prop in self._props)
def write(self, data):
if len(data) != self.size:
raise Exception('Wrong size')
for prop in self._props:
prop.write(data[:prop.size])
data = data[prop.size:]
class BackupDataInterface(abc.ABC):
@abc.abstractmethod
def getRegion(self):
pass
@abc.abstractmethod
def readProp(self, id):
pass
@abc.abstractmethod
def writeProp(self, id, data):
pass
class BackupPlatformDataInterface(BackupDataInterface):
def __init__(self, backend):
self.backend = backend
def getRegion(self):
return self.backend.getBackupStatus()[0x14:].decode('latin1').rstrip('\0')
def readProp(self, id):
return self.backend.readBackup(id)
def writeProp(self, id, data):
self.backend.writeBackup(id, data)
class BackupFileDataInterface(BackupDataInterface):
def __init__(self, file):
self.backup = BackupFile(file)
def getRegion(self):
return self.backup.getRegion()
def readProp(self, id):
return self.backup.getProperty(id).data
def writeProp(self, id, data):
self.backup.setProperty(id, data)
def setProtection(self, enabled):
self.backup.setId1(enabled)
def getSize(self):
return self.backup.size
def updateChecksum(self):
self.backup.updateChecksum()
class BackupPlatformFileDataInterface(BackupFileDataInterface):
def __init__(self, backend):
self.backend = backend
self.file = io.BytesIO(self.backend.getBackupData())
super(BackupPlatformFileDataInterface, self).__init__(self.file)
def apply(self):
self.updateChecksum()
data = self.file.getvalue()
self.backend.setBackupData(data)
if self.backend.getBackupData()[0x100:self.getSize()] != data[0x100:self.getSize()]:
raise Exception('Cannot overwrite backup')
class BackupPatchDataInterface(BackupPlatformFileDataInterface):
def __init__(self, backend):
super(BackupPatchDataInterface, self).__init__(backend)
self.patch = {}
def readProp(self, id):
if id in self.patch:
return self.patch[id]
return super(BackupPatchDataInterface, self).readProp(id)
def writeProp(self, id, data):
if len(data) != len(self.backup.getProperty(id).data):
raise Exception('Wrong data size')
self.patch[id] = data
def getPatch(self):
return self.patch
def setPatch(self, patch):
self.patch = patch
def apply(self):
if not self.patch:
return
patchAttr = {}
for id, data in self.patch.items():
p = self.backup.getProperty(id)
if p.data != data and p.attr & 1:
patchAttr[id] = p.attr
self.backup.setPropertyAttr(id, p.attr & ~1)
self.backup.setProperty(id, data)
try:
super(BackupPatchDataInterface, self).apply()
finally:
if patchAttr:
for id, attr in patchAttr.items():
self.backup.setPropertyAttr(id, attr)
super(BackupPatchDataInterface, self).apply()
class BackupInterface:
def __init__(self, dataInterface):
self.dataInterface = dataInterface
self._props = OrderedDict()
self.addProp('androidPlatformVersion', BackupProp(dataInterface, 0x01660024, 8))
self.addProp('modelCode', BackupProp(dataInterface, 0x00e70000, 5))
self.addProp('modelName', BackupProp(dataInterface, 0x003e0005, 16))
self.addProp('serialNumber', BackupProp(dataInterface, 0x00e70003, 4))
self.addProp('recLimit', CompoundBackupProp(dataInterface, [(0x003c0373 + i, 1) for i in range(3)]))
self.addProp('recLimit4k', BackupProp(dataInterface, 0x003c04b6, 2))
self.addProp('palNtscSelector', BackupProp(dataInterface, 0x01070148, 1))
self.addProp('language', CompoundBackupProp(dataInterface, [(0x010d008f + i, 1) for i in range(35)]))
self.addProp('usbAppInstaller', BackupProp(dataInterface, 0x01640001, 1))
def addProp(self, name, prop):
self._props[name] = prop
def readProp(self, name):
return self._props[name].read()
def writeProp(self, name, data):
return self._props[name].write(data)
def getRegion(self):
return self.dataInterface.getRegion()
def getDefaultLanguages(self, region):
return {
'ALLLANG': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
'AP2': [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'AU2': [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
'CA2': [1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'CE': [1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0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