2020-08-21 12:09:21 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import click
|
|
|
|
|
|
|
|
from trezorlib import firmware
|
2020-09-15 09:28:18 +00:00
|
|
|
from trezorlib._internal import firmware_headers
|
2020-08-21 12:09:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.argument("filename", type=click.File("rb"))
|
|
|
|
@click.option("-o", "--output", type=click.File("w"), default="-")
|
|
|
|
def firmware_fingerprint(filename, output):
|
|
|
|
"""Display fingerprint of a firmware file."""
|
|
|
|
data = filename.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
version, fw = firmware.parse(data)
|
2020-09-15 09:28:18 +00:00
|
|
|
|
|
|
|
# Unsigned production builds for Trezor T do not have valid code hashes.
|
|
|
|
# Use the internal module which recomputes them first.
|
|
|
|
if version == firmware.FirmwareFormat.TREZOR_T:
|
|
|
|
fingerprint = firmware_headers.FirmwareImage(fw).digest()
|
|
|
|
else:
|
|
|
|
fingerprint = firmware.digest(version, fw)
|
2020-08-21 12:09:21 +00:00
|
|
|
except Exception as e:
|
|
|
|
click.echo(e, err=True)
|
|
|
|
sys.exit(2)
|
|
|
|
|
2020-09-15 09:28:18 +00:00
|
|
|
click.echo(fingerprint.hex(), file=output)
|
2020-08-21 12:09:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
firmware_fingerprint()
|