1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-23 07:58:09 +00:00
trezor-firmware/tools/build_vendorheader

54 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
import sys
import struct
import binascii
# encode vendor name, add length byte and padding to multiple of 4
2017-06-13 14:50:03 +00:00
def encode_vendor(vname):
vbin = vname.encode('utf-8')
vbin = struct.pack('<B', len(vbin)) + vbin
vbin += b'\0' * (-len(vbin) & 3)
return vbin
2017-06-13 14:50:03 +00:00
def encode_pubkey(pubkey):
if len(pubkey) != 64:
raise Exception('Wrong public key length')
return binascii.unhexlify(pubkey)
2017-06-13 14:50:03 +00:00
def main():
if len(sys.argv) < 7:
print('Usage build_vendorheader "pubkey1hex:pubkey2hex:..." m version vendorname vendorimage.toif vendorheader.bin')
return 1
keys = [encode_pubkey(x) for x in sys.argv[1].split(':')]
m = int(sys.argv[2])
2017-04-01 16:28:10 +00:00
(vmajor, vminor) = [int(x) for x in sys.argv[3].split('.')]
vname = sys.argv[4]
ifn = sys.argv[5]
ofn = sys.argv[6]
if not ifn.endswith('.toif'):
print('Must provide TOIF file')
return 2
2017-04-01 16:28:10 +00:00
expiry = 0
2017-06-13 14:50:03 +00:00
vheader = b'TRZV' + \
struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
for k in keys:
vheader += k
vheader += encode_vendor(vname) + open(ifn, 'rb').read()
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)
2017-09-05 21:15:47 +00:00
main()