-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatchBitxfile.py
executable file
·203 lines (160 loc) · 6.32 KB
/
patchBitxfile.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
#!/usr/bin/python3.6
def getUpdateMem():
import os
updateMem = 'C:\\NIFPGA\\programs\\Vivado2017_2\\bin\\updatemem.bat' if os.name == 'nt' else '/usr/local/natinst/NIFPGA/programs/vivado2017_2/bin/updatemem'
print(f'Using the following location for updatemem: {updateMem}')
if not os.path.isfile(updateMem):
print("updatemem does not exist")
print('Exiting...')
import sys
sys.exit(0)
return updateMem
def getFile(mypath, expr):
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f.endswith(expr)]
return onlyfiles[0]
def getOnlyElfFile():
return getFile('.', '.elf')
def getOnlyBitFile():
mypath= "."
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f.endswith(".bit")]
return onlyfiles[0]
# binary file starts with 32 0xFFs
# We can also parse the bitstream header and remove it
def stripBitFileHeader(inBitStream):
binStartIndex = 0
for binStartIndex in range(0, len(inBitStream) - 32):
allEqual = True
for i in range(binStartIndex, binStartIndex + 31):
if inBitStream[i] != 0xFF:
allEqual = False
break
if allEqual:
break
return inBitStream[binStartIndex:]
def getBinFileFromBitFile(inBitFile):
print(f'Stripping Bitfile header from {inBitFile}')
bitStream = readFile(inBitFile)
outBinFile = inBitFile[:-4] + '.bin'
fout = open(outBinFile, 'wb')
bitStream = stripBitFileHeader(bitStream)
fout.write(bitStream)
fout.close()
return outBinFile
def getOnlyBitxFile():
mypath= "."
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f.endswith(".lvbitx")]
return onlyfiles[0]
def patch(inElfFile, inBitFile):
updateMem = getUpdateMem()
inMmiFile = inBitFile[:-4] + '.mmi'
outBitFile = inBitFile[:-3] + inElfFile + '.bit'
print("Updatemem command found")
print(f'Out bit file: {outBitFile}')
commandArr = [updateMem,
"-data", inElfFile,
"-bit", inBitFile,
"-proc", "PXIe6592RWindow/theCLIPs/UserRTL_microblaze_CLIP1/d_microblaze_i/microblaze_0",
'-meminfo', inMmiFile,
"-out", outBitFile
]
print("Running:")
print("{}".format( " ".join(commandArr)))
import subprocess
proc = subprocess.run(commandArr, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
if proc:
print(f'proc.returncode = {proc.returncode}')
print('Results')
print('-------')
print("proc.stdout = {}".format(proc.stdout.decode('utf-8')))
return outBitFile
def replaceBinstream(lvbitx, binFile, outLvbitxFile):
newBitStream = readFile(binFile)
import hashlib
m = hashlib.md5()
m.update(newBitStream)
newMd5_str = m.hexdigest()
print(f' NEW MD5: {newMd5_str}')
import base64
dataHexStr = base64.b64encode(newBitStream).decode('ascii')
import re
dataHexStrSplit = re.sub("(.{76})", "\\1\n", dataHexStr, 0, re.DOTALL)
import xml.etree.ElementTree as ET
tree = ET.parse(lvbitx)
root = tree.getroot()
root.find('BitstreamMD5').text = newMd5_str
root.find('Bitstream').text = dataHexStrSplit
tree.write(outLvbitxFile)
def saveToFile(fileName, binData):
binDataArray = bytearray(binData)
with open(fileName, "wb") as fout:
fout.write(binDataArray)
def readFile(fileName):
byteArray = open(fileName, "rb").read()
return byteArray
def main():
print('+-----------------------------------------+')
print('| patchBitxfile.py |')
print('| |')
print('| Patch lvbitx file with new .elf file |')
print('+-----------------------------------------+')
print('')
import argparse
parser = argparse.ArgumentParser(
description='Patch .lvbitx file with new .elf file')
parser.add_argument('--lvbitx', default=None, nargs='?',
help="Full or relative path to LabVIEW lvbitx file")
parser.add_argument('--bit', default=None, nargs='?',
help="Full or relative path to Vivado bit file")
parser.add_argument('--elf', default=None, nargs='?',
help="Full or relative path to Xilinx SDK elf file")
parser.add_argument('--no-confirmation', dest='noConfirmation',
action='store_true',
help="Do not ask for a confirmation before running patch")
args = parser.parse_args()
if args.lvbitx:
lvbitxFile = args.lvbitx
else:
print('- No .lvbitx file specified, checking current directory for one')
lvbitxFile = getOnlyBitxFile()
if lvbitxFile:
print(f' + Found a lvbitx file in the current directory: {lvbitxFile}')
if args.bit:
bitFile = args.bit
else:
print('- No .bin file speciied, checking current directory for one')
bitFile = getOnlyBitFile()
if bitFile:
print(f' + Found a bit file in the current directory: {bitFile}')
if args.elf:
elfFile = args.elf
else:
print('- No .elf file speciied, checking current directory for one')
elfFile = getOnlyElfFile()
if elfFile:
print(f' + Found an elf file in the current directory: {elfFile}')
print('')
if args.noConfirmation:
response = 'Y'
else:
print(f'Patch {lvbitxFile} file with {elfFile} via {bitFile}?')
response = input('Proceed? (Y/n)')
if response.upper().strip() != "Y":
print('Exiting...')
import sys
sys.exit(0)
outLvbitxFile = lvbitxFile[:-6] + '.' + elfFile + '.lvbitx'
print('')
print('------------------------------------------')
print(f'Patch {lvbitxFile} with {elfFile} embedded in to {bitFile}')
print(f'Temporary lvbitxFile {outLvbitxFile}')
newBitFile = patch(elfFile, bitFile)
newBinFile = getBinFileFromBitFile(newBitFile)
replaceBinstream(lvbitxFile, newBinFile, outLvbitxFile)
if __name__ == '__main__':
main()