def main() -> int: """Main function""" parser = argparse.ArgumentParser(description="Tool for communicating with bitbox device") parser.add_argument("--debug", action="store_true", help="Print messages sent and received") parser.add_argument("--u2f", action="store_true", help="Use u2f menu instead") parser.add_argument( "--no-cache", action="store_true", help="Don't use cached or store noise keys" ) parser.add_argument( "--usart", action="store", help="Use USART (BitBoxBase) on the specified serial port." ) args = parser.parse_args() if args.u2f: try: u2fbitbox = u2f.bitbox02.get_bitbox02_u2f_device() except devices.TooManyFoundException: print("Multiple bitboxes detected. Only one supported") except devices.NoneFoundException: print("No bitboxes detected") else: hid_device = hid.device() hid_device.open_path(u2fbitbox["path"]) u2fdevice = u2f.bitbox02.BitBox02U2F(hid_device) u2fapp = U2FApp(u2fdevice, args.debug) return u2fapp.run() return 1 if args.usart is not None: with usart.SerialPort(args.usart) as serial_port: return connect_to_usart_bitboxbase(args.debug, serial_port, not args.no_cache) else: return connect_to_usb_bitbox(args.debug, not args.no_cache) return 1
def main() -> int: """Main function""" parser = argparse.ArgumentParser( description="Tool for flashing a new firmware on BitBox devices.") parser.add_argument("--no-cache", action="store_true", help="Don't use cached or store noise keys") parser.add_argument("--debug", action="store_true", help="Flash a debug (unsigned) firmware.") parser.add_argument( "--usart", action="store", help= "Flash firmware using U2F-over-UART (BitBoxBase), with the specified serial port.", ) parser.add_argument("firmware", nargs=1, help="Firmware to flash.") args = parser.parse_args() if not args.debug and ".signed.bin" not in args.firmware[0]: eprint("Expecting firmware to end with '.signed.bin'") return 1 if args.usart is not None: serial_port = usart.SerialPort(args.usart) bootloader_device = _find_and_open_usart_bitbox( serial_port, not args.no_cache) transport: TransportLayer = usart.U2FUsart(serial_port) bootloader = Bootloader(transport, bootloader_device) else: bootloader_device, transport = _find_and_open_usb_bitbox02( not args.no_cache) bootloader = Bootloader(transport, bootloader_device) with open(args.firmware[0], "rb") as file: firmware = file.read() def progress(perc: float) -> None: sys.stdout.write(f"{perc*100:.02f}%\r") if bootloader.erased(): print("device contains NO firmware") else: print("firmware version: %d\nsigning pubkeys version: %d" % bootloader.versions()) firmware_hash, signing_keydata_hash = bootloader.get_hashes() print("firmware hash:", firmware_hash.hex()) print("signing keydata hash:", signing_keydata_hash.hex()) if args.debug: bootloader.flash_unsigned_firmware(firmware, progress) else: bootloader.flash_signed_firmware(firmware, progress) print() # print a newline sleep(1) # Pause to show the upgrade finished at 100% bootloader.reboot() return 0