diff --git a/common/protob/messages-debug.proto b/common/protob/messages-debug.proto index 6a1876c18..eeeb7563c 100644 --- a/common/protob/messages-debug.proto +++ b/common/protob/messages-debug.proto @@ -173,3 +173,15 @@ message DebugLinkMemoryWrite { message DebugLinkFlashErase { optional uint32 sector = 1; } + + +/** + * Request: Erase the SD card + * @start + * @next Success + * @next Failure + */ +message DebugLinkEraseSdCard { + optional bool format = 1; // if true, the card will be formatted to FAT32. + // if false, it will be all 0xFF bytes. +} diff --git a/common/protob/messages.proto b/common/protob/messages.proto index 0f4af5e79..879aa1fcb 100644 --- a/common/protob/messages.proto +++ b/common/protob/messages.proto @@ -113,6 +113,7 @@ enum MessageType { MessageType_DebugLinkReseedRandom = 9002 [(wire_debug_in) = true]; MessageType_DebugLinkRecordScreen = 9003 [(wire_debug_in) = true]; MessageType_DebugLinkShowText = 9004 [(wire_debug_in) = true]; + MessageType_DebugLinkEraseSdCard = 9005 [(wire_debug_in) = true]; // Ethereum MessageType_EthereumGetPublicKey = 450 [(wire_in) = true]; diff --git a/core/src/apps/debug/__init__.py b/core/src/apps/debug/__init__.py index c0293772f..af2122e47 100644 --- a/core/src/apps/debug/__init__.py +++ b/core/src/apps/debug/__init__.py @@ -17,6 +17,7 @@ if __debug__: from trezor.messages.DebugLinkRecordScreen import DebugLinkRecordScreen from trezor.messages.DebugLinkReseedRandom import DebugLinkReseedRandom from trezor.messages.DebugLinkState import DebugLinkState + from trezor.messages.DebugLinkEraseSdCard import DebugLinkEraseSdCard save_screen = False save_screen_directory = "." @@ -138,6 +139,27 @@ if __debug__: crypto.random.reseed(msg.value) return Success() + async def dispatch_DebugLinkEraseSdCard( + ctx: wire.Context, msg: DebugLinkEraseSdCard + ) -> Success: + try: + io.sdcard.power_on() + if msg.format: + fs = io.FatFS() + fs.mkfs() + else: + # trash first 1 MB of data to destroy the FAT filesystem + assert io.sdcard.capacity() >= 1024 * 1024 + empty_block = bytes([0xFF] * io.sdcard.BLOCK_SIZE) + for i in range(1024 * 1024 // io.sdcard.BLOCK_SIZE): + io.sdcard.write(i, empty_block) + + except OSError: + raise wire.ProcessError("SD card operation failed") + finally: + io.sdcard.power_off() + return Success() + def boot() -> None: # wipe storage when debug build is used on real hardware if not utils.EMULATOR: @@ -149,3 +171,4 @@ if __debug__: wire.register(MessageType.DebugLinkGetState, dispatch_DebugLinkGetState) wire.register(MessageType.DebugLinkReseedRandom, dispatch_DebugLinkReseedRandom) wire.register(MessageType.DebugLinkRecordScreen, dispatch_DebugLinkRecordScreen) + wire.register(MessageType.DebugLinkEraseSdCard, dispatch_DebugLinkEraseSdCard) diff --git a/core/src/trezor/messages/DebugLinkEraseSdCard.py b/core/src/trezor/messages/DebugLinkEraseSdCard.py new file mode 100644 index 000000000..58a090d26 --- /dev/null +++ b/core/src/trezor/messages/DebugLinkEraseSdCard.py @@ -0,0 +1,26 @@ +# Automatically generated by pb2py +# fmt: off +import protobuf as p + +if __debug__: + try: + from typing import Dict, List # noqa: F401 + from typing_extensions import Literal # noqa: F401 + except ImportError: + pass + + +class DebugLinkEraseSdCard(p.MessageType): + MESSAGE_WIRE_TYPE = 9005 + + def __init__( + self, + format: bool = None, + ) -> None: + self.format = format + + @classmethod + def get_fields(cls) -> Dict: + return { + 1: ('format', p.BoolType, 0), + } diff --git a/core/src/trezor/messages/MessageType.py b/core/src/trezor/messages/MessageType.py index 4f6497015..94e5be334 100644 --- a/core/src/trezor/messages/MessageType.py +++ b/core/src/trezor/messages/MessageType.py @@ -77,6 +77,7 @@ DebugLinkLayout = 9001 # type: Literal[9001] DebugLinkReseedRandom = 9002 # type: Literal[9002] DebugLinkRecordScreen = 9003 # type: Literal[9003] DebugLinkShowText = 9004 # type: Literal[9004] +DebugLinkEraseSdCard = 9005 # type: Literal[9005] if not utils.BITCOIN_ONLY: EthereumGetPublicKey = 450 # type: Literal[450] EthereumPublicKey = 451 # type: Literal[451] diff --git a/legacy/firmware/protob/Makefile b/legacy/firmware/protob/Makefile index 34433bafa..dfd4fbe45 100644 --- a/legacy/firmware/protob/Makefile +++ b/legacy/firmware/protob/Makefile @@ -3,7 +3,7 @@ Q := @ endif SKIPPED_MESSAGES := Binance Cardano DebugMonero Eos Monero Ontology Ripple SdProtect Tezos WebAuthn \ - DebugLinkRecordScreen DebugLinkReseedRandom DebugLinkShowText + DebugLinkRecordScreen DebugLinkReseedRandom DebugLinkShowText DebugLinkEraseSdCard ifeq ($(BITCOIN_ONLY), 1) SKIPPED_MESSAGES += Ethereum Lisk NEM Stellar diff --git a/python/src/trezorlib/messages/DebugLinkEraseSdCard.py b/python/src/trezorlib/messages/DebugLinkEraseSdCard.py new file mode 100644 index 000000000..e640527af --- /dev/null +++ b/python/src/trezorlib/messages/DebugLinkEraseSdCard.py @@ -0,0 +1,26 @@ +# Automatically generated by pb2py +# fmt: off +from .. import protobuf as p + +if __debug__: + try: + from typing import Dict, List # noqa: F401 + from typing_extensions import Literal # noqa: F401 + except ImportError: + pass + + +class DebugLinkEraseSdCard(p.MessageType): + MESSAGE_WIRE_TYPE = 9005 + + def __init__( + self, + format: bool = None, + ) -> None: + self.format = format + + @classmethod + def get_fields(cls) -> Dict: + return { + 1: ('format', p.BoolType, 0), + } diff --git a/python/src/trezorlib/messages/MessageType.py b/python/src/trezorlib/messages/MessageType.py index f24095c89..5bd0e8fc5 100644 --- a/python/src/trezorlib/messages/MessageType.py +++ b/python/src/trezorlib/messages/MessageType.py @@ -75,6 +75,7 @@ DebugLinkLayout = 9001 # type: Literal[9001] DebugLinkReseedRandom = 9002 # type: Literal[9002] DebugLinkRecordScreen = 9003 # type: Literal[9003] DebugLinkShowText = 9004 # type: Literal[9004] +DebugLinkEraseSdCard = 9005 # type: Literal[9005] EthereumGetPublicKey = 450 # type: Literal[450] EthereumPublicKey = 451 # type: Literal[451] EthereumGetAddress = 56 # type: Literal[56] diff --git a/python/src/trezorlib/messages/__init__.py b/python/src/trezorlib/messages/__init__.py index 28e4bfd33..a218abe54 100644 --- a/python/src/trezorlib/messages/__init__.py +++ b/python/src/trezorlib/messages/__init__.py @@ -40,6 +40,7 @@ from .CosiCommitment import CosiCommitment from .CosiSign import CosiSign from .CosiSignature import CosiSignature from .DebugLinkDecision import DebugLinkDecision +from .DebugLinkEraseSdCard import DebugLinkEraseSdCard from .DebugLinkFlashErase import DebugLinkFlashErase from .DebugLinkGetState import DebugLinkGetState from .DebugLinkLayout import DebugLinkLayout