2017-04-01 13:45:50 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
import struct
|
2017-04-01 16:28:10 +00:00
|
|
|
import re
|
2017-04-01 13:45:50 +00:00
|
|
|
import binascii
|
2017-04-01 16:28:10 +00:00
|
|
|
import os
|
|
|
|
import imp
|
2017-04-01 13:45:50 +00:00
|
|
|
|
2017-04-01 16:28:10 +00:00
|
|
|
png2toi = imp.load_source('png2toi', os.path.dirname(__file__) + '/png2toi')
|
2017-04-01 13:45:50 +00:00
|
|
|
|
|
|
|
# encode vendor name, add length byte and padding to multiple of 4
|
2017-04-01 16:28:10 +00:00
|
|
|
def encodevendor(vname):
|
2017-04-01 13:45:50 +00:00
|
|
|
vbin = vname.encode('utf-8')
|
|
|
|
vbin = struct.pack('<B',len(vbin)) + vbin
|
|
|
|
vbin += b'\0' * (-len(vbin) & 3)
|
|
|
|
return vbin
|
|
|
|
|
|
|
|
def encodekey(key):
|
|
|
|
if len(key) != 64:
|
|
|
|
raise Exception("Wrong key length")
|
|
|
|
return binascii.unhexlify(key)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 7:
|
|
|
|
print('Usage build_vendorheader key1hex,... m version vendorname vendorimage.png vendorimage.bin')
|
|
|
|
return 1
|
|
|
|
|
2017-04-01 16:28:10 +00:00
|
|
|
keys = [encodekey(x) for x in sys.argv[1].split(',')]
|
2017-04-01 13:45:50 +00:00
|
|
|
m = int(sys.argv[2])
|
2017-04-01 16:28:10 +00:00
|
|
|
(vmajor, vminor) = [int(x) for x in sys.argv[3].split('.')]
|
2017-04-01 13:45:50 +00:00
|
|
|
vname = sys.argv[4]
|
|
|
|
ifn = sys.argv[5]
|
|
|
|
ofn = sys.argv[6]
|
|
|
|
if not ifn.endswith('.png'):
|
|
|
|
print('Must provide PNG file')
|
|
|
|
return 2
|
|
|
|
|
2017-04-01 16:28:10 +00:00
|
|
|
expiry = 0
|
|
|
|
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
|
2017-04-01 13:45:50 +00:00
|
|
|
for k in keys:
|
|
|
|
vheader += k
|
2017-04-01 16:28:10 +00:00
|
|
|
vheader += encodevendor(vname) + png2toi.process_image(ifn)
|
2017-04-01 13:45:50 +00:00
|
|
|
padding = 65 + (-len(vheader) - 65) & 511
|
|
|
|
vheader += b'\0' * padding
|
|
|
|
|
|
|
|
# put in length
|
|
|
|
vheader = vheader[0:4] + struct.pack('<I', len(vheader)) + vheader[8:]
|
|
|
|
|
|
|
|
with open(ofn, 'wb') as f:
|
|
|
|
f.write(vheader)
|
|
|
|
|
|
|
|
main()
|