forked from rpw/flsloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflsloader.py
309 lines (279 loc) · 9.74 KB
/
flsloader.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
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
# flsloader
# =========
#
# IDA Pro loader module for IFX iPhone baseband firmwares and Samsung Exynos devices up to GT-i9500
# Based on a universal scatter loader script by roxfan.
#
# Supports loading of:
#
# * iPhone 2G (S-Gold 2)
# * iPhone 3G (X-Gold 608)
# * iPhone 3GS (X-Gold 608)
# * iPhone 4 (X-Gold 61x)
# * Samsung Galaxy S3 GT-i9300 (X-Gold 626)
#
# baseband firmwares into IDA Pro.
#
# Tested with IDA Pro Advanced 6.6
from idaapi import *
import struct
import re
IFX_FLS_SIGNATURE = 'CJKT'
def dwordAt(li, off):
li.seek(off)
s = li.read(4)
if len(s) < 4:
return 0
return struct.unpack('<I', s)[0]
def matchBytes(addr,match):
n = len(match)
if n & 1:
idc.Warning("Bad match string in matchBytes: %s",match)
return 0
i = 0
while i < n:
s = match[i:(i+2)]
byteashex = "%02X" % idc.Byte(addr)
if s != "??" and byteashex != s:
return 0
i += 2
addr += 1
return 1
def is_copy(ea):
return matchBytes(ea, "102052E27800B0287800A128FBFFFF8A822EB0E13000B0283000A1280040904500408145??????E1")
def is_zeroinit(ea):
return matchBytes(ea, "0030A0E30040A0E30050A0E30060A0E3102052E27800A128FCFFFF8A822EB0E13000A12800308145??????E1")
def is_decomp1(ea):
return (matchBytes(ea, "01C08FE21CFF2FE18A18037801305C07640F01D1047801301D1101D105780130") or
matchBytes(ea, "01C08FE21CFF2FE18A18037801309C07A40F01D1047801301D1101D105780130"))
def is_decomp0(ea):
return (matchBytes(ea, "01C08FE21CFF2FE18A180478013025072D0F01D105780130240901D104780130") or
matchBytes(ea, "012082E00140D0E40F5014E20150D0042442B0E10140D004015055E20300000A0130D0E4015055E20130C1E4FBFFFF1A014054E20200000A014054E20150C1E4FCFFFF1A020051E1EDFFFF3A0EF0A0E1"))
def decompress1(src, dst, dstsize):
orig_src = src
orig_dst = dst
end = dst+dstsize
while dst < end:
r3 = idc.Byte(src)
src += 1
r4 = r3&7
if r4 == 0:
r4 = idc.Byte(src)
src += 1
r5 = r3 >> 4
if r5 == 0:
r5 = idc.Byte(src)
src += 1
# r4: length of region to copy from src
while r4 > 1:
idaapi.patch_byte(dst, idaapi.get_full_byte(src))
dst += 1
src += 1
r4 -= 1
if r3&8:
r4 = idc.Byte(src)
src += 1
r5 += 1
r3 = dst - r4
# fill region with constant value
while r5 >= 0:
idaapi.patch_byte(dst, idaapi.get_full_byte(src))
dst +=1
r3 += 1
r5 -= 1
else:
while r5>0:
# zero-fill region
idaapi.patch_byte(dst, 0)
dst += 1
r5 -= 1
idc.Message("consumed=%0x, produced=%0x\n" % (src-orig_src, dst-orig_dst))
return (dst-orig_dst)==dstsize
def decompress0(src, dst, dstsize):
orig_src = src
orig_dst = dst
end = dst + dstsize
while dst < end:
r3 = idc.Byte(src)
src += 1
r4 = r3&0xF
if r4 == 0:
r4 = idc.Byte(src)
src += 1
r5 = r3 >> 4
if r5 == 0:
r5 = idc.Byte(src)
src += 1
while r4 > 1:
idaapi.patch_byte(dst, idaapi.get_full_byte(src))
src += 1
dst += 1
r4 -= 1
while r5 > 1:
idaapi.patch_byte(dst, 0)
dst += 1
r5 -= 1
idc.Message("consumed=%0x, produced=%0x\n" % (src-orig_src, dst-orig_dst))
return (dst-orig_dst)==dstsize;
def ProcessEntry(entry):
src = idc.Dword(entry);
dst = idc.Dword(entry+4);
size = idc.Dword(entry+8);
proc = idc.Dword(entry+12);
idc.Message("0x%08x: src=%08X dst=%08X size=%08X func=%08X\n" % (entry, src, dst, size, proc))
if (idc.SegStart(dst) == idc.BADADDR) or (idc.SegEnd(dst) <(dst+size)):
idc.AddSeg(dst,(dst+size),0,1,1,0)
if is_copy(proc):
idc.SetSegClass(dst, 'CODE')
idc.Message("0x%08x: copy(0x%08x, 0x%08x, %x)..." % (proc, src, dst, size))
buf = idaapi.get_many_bytes(src, size)
idaapi.patch_many_bytes(dst, buf)
idc.Message("OK\n")
elif is_zeroinit(proc):
idc.Message("0x%08x: zeroinit(0x%08x, %x)..." % (proc, dst, size))
idaapi.patch_many_bytes(dst, '\0' * size)
Message("OK\n")
elif is_decomp1(proc):
idc.Message("0x%08x: uncompress1(0x%08x, 0x%08x, %x)..." % (proc, src, dst, size))
decompress1(src, dst, size)
Message("OK\n")
elif is_decomp0(proc):
idc.Message("0x%08x: uncompress0(0x%08x, 0x%08x, %x)..." % (proc, src, dst, size))
decompress0(src, dst, size)
idc.Message("OK\n")
else:
idc.Message("0x%08x: unknown scatterload function\n" % proc)
return 0
return 1
def ProcessRegionTable(segstart):
scatter_rt2 = idc.FindBinary(segstart, 1, "0F 00 BA E8 14 E0 4F E2 01 00 13 E3 03 F0 47 10")
if scatter_rt2 == idc.BADADDR:
idc.Message("Can't find __scatter_rt2\n")
return 0
idc.Message("__scatter_rt2 is at 0x%08x\n" % scatter_rt2)
scatter_rt2 += 0x14
tblstart = scatter_rt2+idc.Dword(scatter_rt2)
tblend = scatter_rt2+idc.Dword(scatter_rt2+4)
idc.Message("Scatterload table: 0x%08x-0x%08x\n" % (tblstart, tblend))
if (tblstart>=tblend) or ((tblend-tblstart)%16):
idc.Message("Bad table\n")
return 0
idc.MakeStruct(tblstart, "ScatterEntry")
idc.MakeArray(tblstart, (tblend-tblstart)/16)
idc.MakeName(tblstart, "RegionTable_%08X" % tblstart)
i = 0
x = tblstart
while tblstart < tblend:
if ProcessEntry(tblstart) == 0:
break
b = idc.Dword(tblstart);
tblstart += 16
if (tblstart < tblend) and (is_zeroinit(idc.Dword(tblstart-4)) == 0):
c = idc.Dword(tblstart);
if c > b:
idc.HideArea(b,c, "Scatterload source data for region %d" % i, "", "", 0xff0000)
i += 1
def setup_structs():
id = idc.AddStrucEx(-1,"ScatterEntry",0)
id = idc.GetStrucIdByName("ScatterEntry")
# FF_DWRD | FF_DATA | FF_0OFF
idc.AddStrucMember(id, "src", 0*4, 0x20500400, 0xFFFFFFFF, 4)
idc.AddStrucMember(id, "dst", 1*4, 0x20500400, 0xFFFFFFFF, 4)
idc.AddStrucMember(id,"size", 2*4, 0x20000400, 0xFFFFFFFF, 4)
idc.AddStrucMember(id,"proc", 3*4, 0x20500400, 0xFFFFFFFF, 4)
return id
# Verify the input file format
# li - loader_input_t object. it is positioned at the file start
# n - invocation number. if the loader can handle only one format,
# it should return failure on n != 0
# Returns: if the input file is not recognized
# return 0
# else
# return object with 2 attributes:
# format: description of the file format
# options:1 or ACCEPT_FIRST. it is ok not to set this attr
def accept_file(li, n):
flsversion = None
if n > 0:
return 0
li.seek(0x9e0, SEEK_SET)
if li.read(4) == IFX_FLS_SIGNATURE:
flsversion = 'iPhone S-GOLD2 baseband'
arch = "arm:ARMv5TE"
li.seek(0x1104, SEEK_SET)
if li.read(4) == IFX_FLS_SIGNATURE:
li.seek(0x794)
chipid = li.read(7)
if chipid == 'Sibley\0':
flsversion = 'iPhone X-GOLD 608 baseband'
arch = "arm:ARMv5TE"
elif chipid == 'XMM6180':
flsversion = 'iPhone X-GOLD 61x baseband'
arch = "arm:ARMv6"
li.seek(0x2803c, SEEK_SET)
if li.read(4) == IFX_FLS_SIGNATURE:
flsversion = 'Samsung X-GOLD 626 baseband'
arch = "arm:ARMv6"
if flsversion != None:
idaapi.set_processor_type(arch, SETPROC_ALL|SETPROC_FATAL)
idc.ChangeConfig('ARM_SIMPLIFY = NO')
return flsversion
else:
return 0
def load_file(li, neflags, format):
"""
Load the file into the database
@param li: a file-like object which can be used to access the input data
@param neflags: options selected by the user, see loader.hpp
@return: 0-failure, 1-ok
"""
if format == 'iPhone S-GOLD2 baseband':
# ARM 926
arch = "arm:ARMv5TE"
start = 0xA0000000
offset = 0x9A4
entry_point = dwordAt(li, 0x9dc)
scattertbl_search_from = start
li.seek(0, SEEK_END)
size = li.tell() - offset
elif format == 'iPhone X-GOLD 608 baseband':
# ARM 926
arch = "arm:ARMv5TE"
start = 0x20040000
offset = 0xCF8
entry_point = dwordAt(li, 0x1100)
scattertbl_search_from = entry_point
li.seek(0, SEEK_END)
size = li.tell() - offset
elif format == 'iPhone X-GOLD 61x baseband':
# ARM 1176
arch = "arm:ARMv6"
start = 0x40040000
offset = 0xCF8
entry_point = dwordAt(li, 0x1100)
scattertbl_search_from = entry_point
li.seek(0, SEEK_END)
size = li.tell() - offset
elif format == 'Samsung X-GOLD 626 baseband':
# ARM 1176
arch = "arm:ARMv6"
start = 0x60300000
offset = 0x28000
entry_point = 0x60349000 #dwordAt(li, 0x28038)
scattertbl_search_from = start #entry_point
size = 0x9D7800
else:
Warning("Unknown format name: '%s'" % format)
return 0
if entry_point != 0:
idc.Message("Entry point = 0x%08x" % entry_point)
idaapi.set_processor_type(arch, SETPROC_ALL|SETPROC_FATAL)
idc.ChangeConfig('ARM_SIMPLIFY = NO')
AddSeg(start, start+size, 0, 1, idaapi.saRelPara, idaapi.scPub)
SetSegClass(start, 'CODE')
li.seek(0)
li.file2base(offset, start, start+size, 0)
setup_structs()
ProcessRegionTable(scattertbl_search_from)
idaapi.add_entry(entry_point, entry_point, "start", 1)
return 1