forked from nmatt0/firmwaretools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-uboot-dump.py
More file actions
executable file
·101 lines (87 loc) · 3.61 KB
/
parse-uboot-dump.py
File metadata and controls
executable file
·101 lines (87 loc) · 3.61 KB
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
#!/usr/bin/env python3
#
# 20241026 original from matt brown, modified by jens heine <binbash@gmx.net>
#
import os
import re
import sys
from optparse import OptionParser
from pathlib import Path
def main():
parser = OptionParser()
parser.add_option("-i", "--infile", dest="infile",
help="read data from FILE (required)", metavar="FILE")
parser.add_option("-o", "--outfile", dest="outfile",
help="write binary data to FILE (default: firmware.bin)", metavar="FILE")
parser.add_option("-l", "--little-endian", dest="little_endian",
help="convert data to little endian (default:big endian)", action="store_true")
parser.add_option("-f", "--force", dest="force",
help="force overwrite existing outfile", action="store_true")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="be verbose")
(options, args) = parser.parse_args()
infile = None
if options.infile:
infile = options.infile
outfile = "firmware.bin"
if options.outfile:
outfile = options.outfile
if infile is None:
print("Error: Missing argument, try -h for help")
sys.exit(1)
_little_endian = False
if options.little_endian:
_little_endian = options.little_endian
_force = False
if options.force:
_force = options.force
_verbose = False
if options.verbose:
_verbose = options.verbose
if not _force and Path(outfile).is_file():
print("Error: outfile '" + str(outfile) + "' already exists.")
answer = input("Overwrite (y/N)? : ")
print()
if answer != 'y':
sys.exit(0)
else:
os.remove(outfile)
i = open(infile, "r")
o = open(outfile, "wb")
data_count = 0
for line in i.readlines():
line = line.strip()
if re.match(r'^(0x)*[0-9A-Fa-f]{8}:', line):
line = line.split(":")
if len(line) == 2:
line = line[1]
line = line.replace("0x", "")
memory_address_contents = line.split(" ")
for memory_address_content in memory_address_contents:
if len(memory_address_content) == 0:
continue
if _verbose:
try:
decoded = bytes.fromhex(memory_address_content).decode(encoding="ascii")
except Exception as e:
decoded = '..'
print('Offset ' + str(data_count).rjust(8) + ' in : ' + str(memory_address_content)
+ ' : ' + decoded)
if _little_endian:
memory_address_content = (memory_address_content[6:8] + memory_address_content[4:6]
+ memory_address_content[2:4] + memory_address_content[0:2])
data = bytes.fromhex(memory_address_content)
if _verbose:
try:
decoded = bytes.fromhex(memory_address_content).decode(encoding="ascii")
except:
decoded = '..'
print('Offset ' + str(data_count).rjust(8) + ' out : ' + str(memory_address_content)
+ ' : ' + decoded)
o.write(data)
data_count += len(data)
print()
print(str(data_count) + ' bytes written to ' + outfile)
if __name__ == '__main__':
main()