-
Notifications
You must be signed in to change notification settings - Fork 41
/
FirmwareParser.py
executable file
·571 lines (453 loc) · 18.4 KB
/
FirmwareParser.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/usr/bin/env python
#
# Note: This is a defunct attempt as a class that will decode assembly instructions
# and data words from a stripped binary ARMv6 executable. This is effectively
# defunct, but the functionality of printing the vector table has been remained
# intact
#
# MIT License
#
# Copyright (c) 2017 Gregory Gluszek
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import getopt
import struct
class FirmwareParser:
"""Takes Steam Controller firmware and decodes it as best as possible.
"""
RESET_VEC_ADDR = 0x04
NMI_VEC_ADDR = 0x08
HARD_FAULT_VEC_ADDR = 0x0C
SV_CALL_VEC_ADDR = 0x2C
PEND_SV_VEC_ADDR = 0x38
SYS_TICK_VEC_ADDR = 0x3C
IRQ_N_VEC_ADDR = [
0x40, 0x44, 0x48, 0x4C, 0x50, 0x54, 0x58, 0x5C,
0x60, 0x64, 0x68, 0x6C, 0x70, 0x74, 0x78, 0x7C,
0x80, 0x84, 0x88, 0x8C, 0x90, 0x94, 0x98, 0x9C,
0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
]
# List of all data words read from firmware file. Combined and
# categorized as best as possible.
dataWords = []
## traceBuffer # Array of instructions that have been execute during emulation
## # TODO: create functions (read/write) to access memory (controls if emulator try to access inappropriate space, etc.)
## # 0x0000 0000 (SIZE 0x0002 0000) On Chip Flash
## binFile #TODO: able to query offset in file, or need separate variable (i.e. programCounter)?
## # 0x0000 0000 - 0x0000 00BC Vector Table
## vectorTable # Array of vector addresses? Create enums as logical means to access?
## SRAM0 # 0x1000 0000 (SIZE 0x0000 2000) SRAM0
## stackBuffer # Memory for what is pushed on the stack
## # 0x1FFF 0000 (SIZE 0x0000 4000) boot ROM
## SRAM1 # 0x2000 0000 (SIZE 0x0000 0800) SRAM1
## SRAM2 # 0x2000 4000 (SIZE 0x0000 0800) SRAM2
## # 0x4000 0000 (SIZE 0x0000 4000) APB Peripheral I2C Bus
## # 0x4000 4000 (SIZE 0x0000 4000) APB Peripheral WWDT
## # 0x4000 8000 (SIZE 0x0000 4000) APB Peripheral USART/SMART CARD
## # 0x4000 C000 (SIZE 0x0000 4000) APB Peripheral 16-bit counter/timer 0
## # 0x4001 0000 (SIZE 0x0000 4000) APB Peripheral 16-bit counter/timer 1
## # 0x4001 4000 (SIZE 0x0000 4000) APB Peripheral 32-bit counter/timer 0
## # 0x4001 8000 (SIZE 0x0000 4000) APB Peripheral 32-bit counter/timer 1
## # 0x4001 C000 (SIZE 0x0000 4000) APB Peripheral ADC
## # 0x4003 8000 (SIZE 0x0000 4000) APB Peripheral PMU
## # 0x4003 C000 (SIZE 0x0000 4000) APB Peripheral flash/EEPROM controller
## # 0x4004 0000 (SIZE 0x0000 4000) APB Peripheral SSP0
## # 0x4004 4000 (SIZE 0x0000 4000) APB Peripheral IOCON
## # 0x4004 8000 (SIZE 0x0000 4000) APB Peripheral system control
## # 0x4004 C000 (SIZE 0x0000 4000) APB Peripheral GPIO interrupts
## # 0x4005 8000 (SIZE 0x0000 4000) APB Peripheral SSP1
## # 0x4005 C000 (SIZE 0x0000 4000) APB Peripheral GPIO GROUP0 INT
## # 0x4006 0000 (SIZE 0x0000 4000) APB Peripheral GPIO GROUP1 INT
## # 0x4008 0000 (SIZE 0x0000 4000) USB
## # 0x5000 0000 (SIZE 0x0000 4000) GPIO
## # 0xE000 0000 (SIZE 0x0010 0000) Private Peripheral Bus
## # TODO registers for emulation
## # Stack pointer
## # Program counter
def __init__(self, binFilename):
"""Constructor
Params:
binFilename Path to binary firmware file
"""
# Create the initial list of dataWords by reading the fw file
binFile = open(binFilename, 'rb')
offset = 0
word16 = self.__read16(binFile)
while (word16 != -1):
self.dataWords += [DataWord(word16, offset)]
offset += 2
word16 = self.__read16(binFile)
binFile.close()
# Identify vector table data
self.__identifyVectorTable()
# Identify start instructions based on specific vector entries
dataWord = self.__getDataWord(self.RESET_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by Reset Vector; ')
dataWord = self.__getDataWord(self.NMI_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by NMI Vector; ')
dataWord = self.__getDataWord(self.HARD_FAULT_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by Hard Fault Vector; ')
dataWord = self.__getDataWord(self.SV_CALL_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by SV Call Vector; ')
dataWord = self.__getDataWord(self.PEND_SV_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by Pend SV Vector; ')
dataWord = self.__getDataWord(self.SYS_TICK_VEC_ADDR)
self.__markInstruction(dataWord.binData, 'Called by Sys Tick Vector; ')
for i, addr in enumerate(self.IRQ_N_VEC_ADDR):
dataWord = self.__getDataWord(addr)
self.__markInstruction(dataWord.binData, 'Called by IRQ ' + str(i) + ' Vector; ')
# Identify possible instructions
#for data in self.dataWords:
# try:
# data.instruction = Instruction(data)
# except ValueError:
# data.instruction = None
#self.__markRawData(0x244, 'Called by instruction at offset 0xbe')
def __read16(self, inFile):
"""Read 16 bit word (little endian) from file
Params:
inFile File to read from
"""
# Read byte 0
byte0 = inFile.read(1)
if (byte0 == ""):
return -1
# Read byte 1
byte1 = inFile.read(1)
if (byte1 == ""):
return struct.unpack('B', byte0)[0]
retval = struct.unpack('B', byte1)[0] << 8 | struct.unpack('B', byte0)[0]
return retval
def __str__(self):
retval = ''
for data in self.dataWords:
retval += str(data)
return retval
def __getDataWord(self, addr):
"""Get the DataWord at the given address in firmware
Params:
addr Address of DataWord
"""
return self.dataWords[addr/2]
def __identifyVectorTable(self):
"""Identify DataWords that are part of Vector Table
"""
self.__setVectorTableEntry(0x00, 'Initial SP Value')
self.__setVectorTableEntry(self.RESET_VEC_ADDR, 'Reset')
self.__setVectorTableEntry(self.NMI_VEC_ADDR, 'NMI')
self.__setVectorTableEntry(self.HARD_FAULT_VEC_ADDR, 'Hard Fault')
self.__setVectorTableEntry(0x10, 'RESERVED')
self.__setVectorTableEntry(0x14, 'RESERVED')
self.__setVectorTableEntry(0x18, 'RESERVED')
self.__setVectorTableEntry(0x1C, 'RESERVED (Checksum)')
self.__setVectorTableEntry(0x20, 'RESERVED')
self.__setVectorTableEntry(0x24, 'RESERVED')
self.__setVectorTableEntry(0x28, 'RESERVED')
self.__setVectorTableEntry(self.SV_CALL_VEC_ADDR, 'SVCall')
self.__setVectorTableEntry(0x30, 'RESERVED')
self.__setVectorTableEntry(0x34, 'RESERVED')
self.__setVectorTableEntry(self.PEND_SV_VEC_ADDR, 'PendSV')
self.__setVectorTableEntry(self.SYS_TICK_VEC_ADDR, 'SysTick')
for i, addr in enumerate(self.IRQ_N_VEC_ADDR):
self.__setVectorTableEntry(addr, 'IRQ' + str(i))
def __setVectorTableEntry(self, addr, desc):
"""Set the DataWord to a Vector Table Entry
Params:
addr Address of Vector Table entry
desc Describes the vector
"""
dataWordLo = self.__getDataWord(addr)
dataWordHi = self.__getDataWord(addr+2)
if (dataWordLo.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (dataWordLo.offset, dataWordLo.dataType))
if (dataWordHi.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (dataWordHi.offset, dataWordHi.dataType))
dataWordLo.decodeString = desc
dataWordLo.combine(dataWordHi)
dataWordLo.dataType = DataWord.TYPE_VECTOR_TABLE
def __markInstruction(self, addr, comment):
"""Mark that DataWord at given address is a known instruction
Params:
addr The address of the known instruction
comment Appended to decodeString (i.e. tell which instruction called this as instruction)
"""
dataWord = self.__getDataWord(addr)
if (addr == 0):
# It is valid for vector table to pointer to NULL. Ignore it
dataWord.decodeString += comment
return
# Checkif it was already decoded
if (dataWord.dataType == DataWord.TYPE_INSTRUCTION):
return
# Check if the addr does not point to an already labeled DataWord
if (dataWord.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (dataWord.offset, dataWord.dataType))
# Set children for 32-bit instructions so that instruction
# identification will have all necessary data
if Instruction.is32bit(dataWord.binData):
raise ValueError('Found a 32-bit instruction. Decode logic needed!')
# Should just need to set child and everything else falls through?
# Mark this DataWord is an instruction
dataWord.dataType = DataWord.TYPE_INSTRUCTION
# Decode dataword as instruction
dataWord.instruction = Instruction(dataWord)
dataWord.decodeString += comment
# Run through indentified DataWords
#TODO: Update instruction.identifiedDataWords with link back to dataWords owned by self?
for nextDataWord in dataWord.instruction.identifiedDataWords:
if nextDataWord.dataType == DataWord.TYPE_INSTRUCTION:
self.__markInstruction(nextDataWord.offset, 'Called by ' + str(hex(dataWord.offset)) + '; ')
elif nextDataWord.dataType == DataWord.TYPE_RAW_DATA:
self.__markRawData(nextDataWord.offset, 'Called by ' + str(hex(dataWord.offset)) + '; ')
else:
raise ValueError('nextDataWord at identified by instruction TODO')
def __markRawData(self, addr, comment):
"""Mark that DataWord at given address is a known raw data
Params:
addr The address of the known raw data
comment Appended to decodeString (i.e. tell which instruction called this as data)
"""
dataWord = self.__getDataWord(addr)
# Checkif it was already decoded
if (dataWord.dataType == DataWord.TYPE_RAW_DATA):
dataWord.decodeString += comment
return
# Check if the addr does not point to an already labeled DataWord
if (dataWord.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (dataWord.offset, dataWord.dataType))
# Raw Data is 32-bit (TODO: this may not be true... differentiate between 8, 16 and 32-bit loads (A4.6 in ISA)
child = self.__getDataWord(addr+2)
dataWord.combine(child)
# Mark this DataWord is an instruction
dataWord.dataType = DataWord.TYPE_RAW_DATA
dataWord.decodeString += comment
class DataWord(object):
"""Class to encapsulate a 32 or 16-bit data word read from the firmware file.
"""
# To be used for setting dataType
TYPE_UNKNOWN = "Unknown"
TYPE_VECTOR_TABLE = "Vector Table"
TYPE_INSTRUCTION = "Instruction"
TYPE_RAW_DATA = "Raw Data"
# Raw binary data from firmware file
__binData = 0
# Memory location in firmware where this data word can be found
__offset = 0
# Defines how the data has been categorized
__dataType = TYPE_UNKNOWN
# Type specific string to make understanding this instruction easier
decodeString = ''
# Set to another DataWord if this has been combined
__parent = None
# Tells us whether this is a 32-bit DataWord or not, and where the
# upper 16-bits of data are if this is 32-bit
__child = None
# Decode of __binData into an instruction. Check type to know is 100% valid.
instruction = None
def __init__(self, binData, offset):
"""Constructor
Params:
binData Raw binary data read from firmware file
offset Memory location offset in firmware where this data
was read from
"""
self.__binData = binData
self.__offset = offset
@property
def dataType(self):
return self.__dataType
@dataType.setter
def dataType(self, value):
if (self.__child != None):
self.__child.dataType = value
if (self.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (self.offset, self.dataType))
self.__dataType = value
@property
def offset(self):
return self.__offset
@property
def binData(self):
retval = 0;
if (self.__child != None):
retval = self.__child.binData << 16;
retval |= self.__binData
return retval
def __str__(self):
if (self.__parent != None):
return ''
retval = ''
# Offset address in hex
retval += '{0:06x}: '.format(self.offset)
# Upper 16 bits of raw binary data
if (self.__child != None):
retval += ' {0:04x}'.format(self.__child.binData)
else:
retval += ' '
# Lower 16 bits of raw binary data
retval += ' {0:04x}'.format(self.binData%0x10000)
# The identified data type
retval += ' {0:>16s}'.format(self.dataType)
# More specific info on the data
retval += ' '
if (self.instruction != None):
if (self.dataType == DataWord.TYPE_UNKNOWN):
retval += 'Possible Instruction: '
retval += self.instruction.description + ': '
retval += str(self.instruction) + '; '
retval += self.decodeString
retval += '\n'
return retval
def combine(self, dataWord):
"""Combine the given DataWord into this word.
Params:
dataWord DataWord higher in memory than self
"""
if (self.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (self.offset, self.dataType))
if (dataWord.dataType != DataWord.TYPE_UNKNOWN):
raise ValueError('DataWord at offset 0x%x is of type %s' % (dataWord.offset, dataWord.dataType))
self.__child = dataWord
dataWord.__parent = self
class Instruction:
"""Represents an assembly instruction
"""
description = ''
args = []
# Defines which DataWords can be idenfitied by actions of this instruction
identifiedDataWords = []
def __init__(self, dataWord):
"""Constructor
Params:
dataWord
"""
self.args = []
binData = dataWord.binData
if ((binData & 0xFC00) == 0x4400):
self.__decodeSpecialDataBranchExchange(dataWord)
elif ((binData & 0xF800) == 0x4800):
self.__decodeLoadFromLiteralPool(dataWord)
elif ((binData & 0xF000) == 0x5000 or \
(binData & 0xE000) == 0x6000 or \
(binData & 0xE000) == 0x8000):
self.__decodeLoadStoreSingle(dataWord)
#TODO: commented out to see how things playout when we reach an instruction we do not yet know how to decode
#else:
#raise ValueError('Not a valid instruction')
def __decodeSpecialDataBranchExchange(self, dataWord):
binData = dataWord.binData
opCode = (binData >> 6) & 0xF
if ((opCode & 0xC) == 0x0):
self.description = 'Add Registers'
#TODO: decode further and call more sub functions
self.args.append('TODO')
elif (opCode == 0x4):
self.description = 'Unpredictable'
#TODO: decode further and call more sub functions
self.args.append('TODO')
#TODO: throw exception?
elif (opCode == 0x5 or (opCode & 0xE) == 0x6):
self.description = 'Compare Registers'
#TODO: decode further and call more sub functions
self.args.append('TODO')
elif ((opCode & 0xC) == 0x8):
self.description = 'Move Registers'
#TODO: decode further and call more sub functions
self.args.append('TODO')
elif ((opCode & 0xE) == 0xC):
self.description = 'Branch and Exchange'
#TODO: decode further and call more sub functions
self.args.append('TODO')
elif ((opCode & 0xE) == 0xE):
self.description = 'Branch with Link and Exchange'
#TODO: decode further and call more sub functions
# Issue here is that our branch address is based on the value in a register.
# In order to decode this we need to know register values when we get to this instruction.
# Should each instruction have a register history?
# Or we just have one set of registers and reference them as we decode
self.args.append('TODO')
else:
#TODO: throw exception?
self.args.append('TODO')
def __decodeLoadFromLiteralPool(self, dataWord):
self.description = 'Load Register (literal)'
# Instruction name
self.args.append('LDR')
# Number of register to be loaded with raw data
load_reg_num = 0x3 & (dataWord.binData >> 8)
self.args.append('R' + str(load_reg_num))
# Offset of where to load from
offset = (0xFF & dataWord.binData) << 2
offset += dataWord.offset + 4
self.args.append(hex(offset))
# Identify DataWord we are loading data from
identDataWord = DataWord(0, offset)
identDataWord.dataType = DataWord.TYPE_RAW_DATA
self.identifiedDataWords.append(identDataWord)
# Identify instruction coming up next (as this is not a branch)
identDataWord = DataWord(0, dataWord.offset + 2)
identDataWord.dataType = DataWord.TYPE_INSTRUCTION
self.identifiedDataWords.append(identDataWord)
#TODO: At this point can we make any further assumptions or setup for how value loaded into register will be used?
# i.e. some of these values are clearly accessing peripherals, others are going to access data in firmware maybe?
# Is there a way to decode this one step further and say what is being loaded from calculated offset?
def __decodeLoadStoreSingle(self, dataWord):
self.description = 'Load/Store'
#TODO: decode further and call more sub functions
self.args.append('TODO')
# Identify instruction coming up next (as this is not a branch)
identDataWord = DataWord(0, dataWord.offset + 2)
identDataWord.dataType = DataWord.TYPE_INSTRUCTION
self.identifiedDataWords.append(identDataWord)
@staticmethod
def is32bit(dataWord16):
"""Check if 16-bit data word indicates it is part of 32-bit instruction
Params:
dataWord16 16-bit data word
"""
retval = False
bitsToCheck = dataWord16 & 0xF800
if (bitsToCheck == 0xF100 or bitsToCheck == 0xF000 or bitsToCheck == 0xE100):
retval = True
return retval
def __str__(self):
retval = ''
for arg in self.args:
retval += arg + ' '
return retval
def printUsage():
print 'usage: FirmwareParser.py -i <inputFile>'
def main(argv):
"""Entry point for command line interface for using FirmwareParser.py
"""
try:
opts, args = getopt.getopt(argv,"hi:",["inputfile="])
except getopt.GetoptError:
printUsage()
for opt, arg in opts:
if opt == '-h':
printUsage()
elif opt in ("-i", "--inputfile"):
parser = FirmwareParser(arg)
print parser
if len(opts) == 0:
printUsage()
if __name__ == "__main__":
main(sys.argv[1:])