2017-04-01 13:45:50 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
import struct
|
|
|
|
import binascii
|
|
|
|
|
2018-07-31 09:35:09 +00:00
|
|
|
|
2017-04-01 13:45:50 +00:00
|
|
|
# encode vendor name, add length byte and padding to multiple of 4
|
2017-04-01 21:14:55 +00:00
|
|
|
def encode_vendor(vname):
|
2018-01-10 19:47:07 +00:00
|
|
|
vbin = vname.encode()
|
2018-07-31 09:35:09 +00:00
|
|
|
vbin = struct.pack("<B", len(vbin)) + vbin
|
|
|
|
vbin += b"\0" * (-len(vbin) & 3)
|
2017-04-01 13:45:50 +00:00
|
|
|
return vbin
|
|
|
|
|
2017-06-13 14:50:03 +00:00
|
|
|
|
2017-04-01 21:14:55 +00:00
|
|
|
def encode_pubkey(pubkey):
|
|
|
|
if len(pubkey) != 64:
|
2018-07-31 09:35:09 +00:00
|
|
|
raise Exception("Wrong public key length")
|
2017-04-01 21:14:55 +00:00
|
|
|
return binascii.unhexlify(pubkey)
|
2017-04-01 13:45:50 +00:00
|
|
|
|
2017-06-13 14:50:03 +00:00
|
|
|
|
2018-01-10 19:47:07 +00:00
|
|
|
def decode_vtrust(vtrust):
|
|
|
|
t = 0xFFFF
|
|
|
|
for i, b in enumerate(reversed(vtrust)):
|
2018-07-31 09:35:09 +00:00
|
|
|
if b != ".":
|
2018-01-10 19:47:07 +00:00
|
|
|
t &= ~(1 << i)
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2017-04-01 13:45:50 +00:00
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 7:
|
2018-07-31 09:35:09 +00:00
|
|
|
print(
|
|
|
|
'Usage build_vendorheader "pubkey1hex:pubkey2hex:..." m version vendortrust vendorname vendorimage.toif vendorheader.bin'
|
|
|
|
)
|
2017-04-01 13:45:50 +00:00
|
|
|
return 1
|
|
|
|
|
2018-07-31 09:35:09 +00:00
|
|
|
keys = [encode_pubkey(x) for x in sys.argv[1].split(":")]
|
2017-04-01 13:45:50 +00:00
|
|
|
m = int(sys.argv[2])
|
2018-07-31 09:35:09 +00:00
|
|
|
(vmajor, vminor) = [int(x) for x in sys.argv[3].split(".")]
|
2018-01-10 19:47:07 +00:00
|
|
|
vtrust = decode_vtrust(sys.argv[4])
|
2017-10-05 15:31:05 +00:00
|
|
|
vname = sys.argv[5]
|
|
|
|
ifn = sys.argv[6]
|
|
|
|
ofn = sys.argv[7]
|
2018-07-31 09:35:09 +00:00
|
|
|
if not ifn.endswith(".toif"):
|
|
|
|
print("Must provide TOIF file")
|
2017-04-01 13:45:50 +00:00
|
|
|
return 2
|
|
|
|
|
2017-04-01 16:28:10 +00:00
|
|
|
expiry = 0
|
2018-07-31 09:35:09 +00:00
|
|
|
vheader = b"TRZV" + struct.pack(
|
|
|
|
"<IIBBBBH", 0, expiry, vmajor, vminor, m, len(keys), vtrust
|
|
|
|
)
|
|
|
|
vheader += 14 * b"\0"
|
2017-04-01 13:45:50 +00:00
|
|
|
for k in keys:
|
|
|
|
vheader += k
|
2018-07-31 09:35:09 +00:00
|
|
|
vheader += encode_vendor(vname) + open(ifn, "rb").read()
|
2017-04-01 13:45:50 +00:00
|
|
|
padding = 65 + (-len(vheader) - 65) & 511
|
2018-07-31 09:35:09 +00:00
|
|
|
vheader += b"\0" * padding
|
2017-04-01 13:45:50 +00:00
|
|
|
|
|
|
|
# put in length
|
2018-07-31 09:35:09 +00:00
|
|
|
vheader = vheader[0:4] + struct.pack("<I", len(vheader)) + vheader[8:]
|
2017-04-01 13:45:50 +00:00
|
|
|
|
2018-07-31 09:35:09 +00:00
|
|
|
with open(ofn, "wb") as f:
|
2017-04-01 13:45:50 +00:00
|
|
|
f.write(vheader)
|
|
|
|
|
2017-09-05 21:15:47 +00:00
|
|
|
|
2017-04-01 13:45:50 +00:00
|
|
|
main()
|