-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_uboot_header.py
99 lines (76 loc) · 2.71 KB
/
add_uboot_header.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
import os
from optparse import OptionParser
import struct
import math
EntryPoint = 0xC1080000
LoadAddress = EntryPoint
#memtype='NAND'
memtype='SPI'
#NAND stuff
data_page_length = 4096
oob_page_length = 224
FirstDataPage = 1
BlockNum = 6
#SPI stuff
spi_addr = 0x80000
if memtype == 'NAND':
MagicNum = 0xA1ACED11
elif memtype == 'SPI':
MagicNum = 0xA1ACED00
class UBootHeader:
def __init__(self, in_file, out_file):
self.file_size = os.path.getsize(in_file)
self.num_pages = int(math.ceil(self.file_size / float(data_page_length)))
self.in_file = open(in_file, 'rb')
self.out_file = open(out_file, 'wb')
def close(self):
self.out_file.close()
self.in_file.close()
def add_header(self):
if memtype == 'NAND':
header = struct.pack('<6I', MagicNum,
EntryPoint,
self.num_pages,
BlockNum,
FirstDataPage,
LoadAddress
)
elif memtype == 'SPI':
header = struct.pack('<5I', MagicNum,
EntryPoint,
self.file_size,
spi_addr + 20, #Size of this header
LoadAddress
)
for v in header:
self.out_file.write(v)
if memtype == 'NAND':
ff = struct.pack('>B', 0xFF)
for i in range(data_page_length - 6*4):
self.out_file.write(ff[0])
in_byte = 1
while in_byte != "" :
in_byte = self.in_file.read(1)
self.out_file.write(in_byte)
def print_output(self):
""" Wow this is broken """
f = self.out_file
in_byte = 1
while in_byte != "" :
for i in range(data_page_length / 16):
in_byte = f.read(8)
print " ".join('{0:2x}'.format(ord(c)) for c in in_byte),
print " ",
in_byte = f.read(8)
print " ".join('{0:2x}'.format(ord(c)) for c in in_byte)
print "page break"
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-i", "--input", dest="in_file",
help="file to test")
parser.add_option("-o", "--output", dest="output",
help="output file")
(options, args) = parser.parse_args()
header = UBootHeader(options.in_file, options.output)
header.add_header()
#header.print_output()