mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-24 15:28:10 +00:00
tools: rename firmwarectl to binctl, minor reformat
This commit is contained in:
parent
722e71eb31
commit
dd69929ce1
@ -24,7 +24,7 @@ it will start in a firmware update mode, allowing a firmware update via USB.
|
|||||||
* Hash function used for computing data digest for signatures is BLAKE2s.
|
* Hash function used for computing data digest for signatures is BLAKE2s.
|
||||||
* Signature system is Ed25519 (allows combining signatures by multiple keys into one).
|
* Signature system is Ed25519 (allows combining signatures by multiple keys into one).
|
||||||
* All multibyte integer values are little endian.
|
* All multibyte integer values are little endian.
|
||||||
* There is a tool called [firmwarectl](../tools/firmwarectl) which checks validity of the loader/firmware images including their headers.
|
* There is a tool called [binctl](../tools/binctl) which checks validity of the loader/firmware images including their headers.
|
||||||
|
|
||||||
## Loader Format
|
## Loader Format
|
||||||
|
|
||||||
|
@ -23,9 +23,17 @@ class LoaderImage:
|
|||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
header = struct.unpack('<4sIIIBBBB171sB64s', data[:256])
|
header = struct.unpack('<4sIIIBBBB171sB64s', data[:256])
|
||||||
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
self.magic, \
|
||||||
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
self.hdrlen, \
|
||||||
self.reserved, self.sigidx, self.sig = header
|
self.expiry, \
|
||||||
|
self.codelen, \
|
||||||
|
self.vmajor, \
|
||||||
|
self.vminor, \
|
||||||
|
self.vpatch, \
|
||||||
|
self.vbuild, \
|
||||||
|
self.reserved, \
|
||||||
|
self.sigidx, \
|
||||||
|
self.sig = header
|
||||||
assert self.magic == b'TRZL'
|
assert self.magic == b'TRZL'
|
||||||
assert self.hdrlen == 256
|
assert self.hdrlen == 256
|
||||||
assert self.codelen + self.hdrlen >= 4 * 1024
|
assert self.codelen + self.hdrlen >= 4 * 1024
|
||||||
@ -45,7 +53,7 @@ class LoaderImage:
|
|||||||
print(' * sigidx :', self.sigidx)
|
print(' * sigidx :', self.sigidx)
|
||||||
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
||||||
|
|
||||||
def header(self, sig=True):
|
def serialize_header(self, sig=True):
|
||||||
header = struct.pack('<4sIIIBBBB171s', \
|
header = struct.pack('<4sIIIBBBB171s', \
|
||||||
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
||||||
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
||||||
@ -58,14 +66,14 @@ class LoaderImage:
|
|||||||
return header
|
return header
|
||||||
|
|
||||||
def sign(self):
|
def sign(self):
|
||||||
header = self.header(sig=False)
|
header = self.serialize_header(sig=False)
|
||||||
data = header + self.code
|
data = header + self.code
|
||||||
assert len(data) == self.hdrlen + self.codelen
|
assert len(data) == self.hdrlen + self.codelen
|
||||||
self.sigidx, self.sig = get_sig(data)
|
self.sigidx, self.sig = get_sig(data)
|
||||||
|
|
||||||
def write(self, filename):
|
def write(self, filename):
|
||||||
with open(filename, 'wb') as f:
|
with open(filename, 'wb') as f:
|
||||||
f.write(self.header())
|
f.write(self.serialize_header())
|
||||||
f.write(self.code)
|
f.write(self.code)
|
||||||
|
|
||||||
|
|
||||||
@ -73,9 +81,13 @@ class VendorHeader:
|
|||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
header = struct.unpack('<4sIIBBBB', data[:16])
|
header = struct.unpack('<4sIIBBBB', data[:16])
|
||||||
self.magic, self.hdrlen, self.expiry, \
|
self.magic, \
|
||||||
self.vmajor, self.vminor, \
|
self.hdrlen, \
|
||||||
self.vsig_m, self.vsig_n = header
|
self.expiry, \
|
||||||
|
self.vmajor, \
|
||||||
|
self.vminor, \
|
||||||
|
self.vsig_m, \
|
||||||
|
self.vsig_n = header
|
||||||
assert self.magic == b'TRZF'
|
assert self.magic == b'TRZF'
|
||||||
assert self.vsig_m > 0 and self.vsig_m <= self.vsig_n
|
assert self.vsig_m > 0 and self.vsig_m <= self.vsig_n
|
||||||
assert self.vsig_n > 0 and self.vsig_n <= 8
|
assert self.vsig_n > 0 and self.vsig_n <= 8
|
||||||
@ -115,7 +127,7 @@ class VendorHeader:
|
|||||||
print(' * sigidx :', self.sigidx)
|
print(' * sigidx :', self.sigidx)
|
||||||
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
||||||
|
|
||||||
def header(self, sig=True):
|
def serialize_header(self, sig=True):
|
||||||
header = struct.pack('<4sIIBBBB', \
|
header = struct.pack('<4sIIBBBB', \
|
||||||
self.magic, self.hdrlen, self.expiry, \
|
self.magic, self.hdrlen, self.expiry, \
|
||||||
self.vmajor, self.vminor, \
|
self.vmajor, self.vminor, \
|
||||||
@ -132,21 +144,29 @@ class VendorHeader:
|
|||||||
return header
|
return header
|
||||||
|
|
||||||
def sign(self):
|
def sign(self):
|
||||||
header = self.header(sig=False)
|
header = self.serialize_header(sig=False)
|
||||||
self.sigidx, self.sig = get_sig(header)
|
self.sigidx, self.sig = get_sig(header)
|
||||||
|
|
||||||
def write(self, filename):
|
def write(self, filename):
|
||||||
with open(filename, 'wb') as f:
|
with open(filename, 'wb') as f:
|
||||||
f.write(self.header())
|
f.write(self.serialize_header())
|
||||||
|
|
||||||
|
|
||||||
class FirmwareImage:
|
class FirmwareImage:
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
header = struct.unpack('<4sIIIBBBB171sB64s', data[:256])
|
header = struct.unpack('<4sIIIBBBB171sB64s', data[:256])
|
||||||
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
self.magic, \
|
||||||
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
self.hdrlen, \
|
||||||
self.reserved, self.sigidx, self.sig = header
|
self.expiry, \
|
||||||
|
self.codelen, \
|
||||||
|
self.vmajor, \
|
||||||
|
self.vminor, \
|
||||||
|
self.vpatch, \
|
||||||
|
self.vbuild, \
|
||||||
|
self.reserved, \
|
||||||
|
self.sigidx, \
|
||||||
|
self.sig = header
|
||||||
assert self.magic == b'TRZF'
|
assert self.magic == b'TRZF'
|
||||||
assert self.hdrlen == 256
|
assert self.hdrlen == 256
|
||||||
assert self.codelen % 4 == 0
|
assert self.codelen % 4 == 0
|
||||||
@ -164,7 +184,7 @@ class FirmwareImage:
|
|||||||
print(' * sigidx :', self.sigidx)
|
print(' * sigidx :', self.sigidx)
|
||||||
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
print(' * sig :', binascii.hexlify(self.sig).decode('ascii'))
|
||||||
|
|
||||||
def header(self, sig=True):
|
def serialize_header(self, sig=True):
|
||||||
header = struct.pack('<4sIIIBBBB171s', \
|
header = struct.pack('<4sIIIBBBB171s', \
|
||||||
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
self.magic, self.hdrlen, self.expiry, self.codelen, \
|
||||||
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
self.vmajor, self.vminor, self.vpatch, self.vbuild, \
|
||||||
@ -177,14 +197,14 @@ class FirmwareImage:
|
|||||||
return header
|
return header
|
||||||
|
|
||||||
def sign(self):
|
def sign(self):
|
||||||
header = self.header(sig=False)
|
header = self.serialize_header(sig=False)
|
||||||
data = header + self.code
|
data = header + self.code
|
||||||
assert len(data) == self.hdrlen + self.codelen
|
assert len(data) == self.hdrlen + self.codelen
|
||||||
self.sigidx, self.sig = get_sig(data)
|
self.sigidx, self.sig = get_sig(data)
|
||||||
|
|
||||||
def write(self, filename):
|
def write(self, filename):
|
||||||
with open(filename, 'wb') as f:
|
with open(filename, 'wb') as f:
|
||||||
f.write(self.header())
|
f.write(self.serialize_header())
|
||||||
f.write(self.code)
|
f.write(self.code)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user