|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
from micropython import const
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
from trezorcrypto import crc
|
|
|
|
|
from typing import Generator
|
|
|
|
|
|
|
|
|
|
import storage.device
|
|
|
|
|
from trezor import io, utils
|
|
|
|
|
from trezor.sdcard import with_filesystem, with_sdcard
|
|
|
|
|
from trezorcrypto import crc
|
|
|
|
|
from trezor.wire import ProcessError
|
|
|
|
|
|
|
|
|
|
if utils.USE_SD_CARD:
|
|
|
|
|
fatfs = io.fatfs # global_import_cache
|
|
|
|
@ -55,6 +55,7 @@ def _write_seed_unalloc(mnemonic_secret: bytes) -> None:
|
|
|
|
|
|
|
|
|
|
def _read_seed_unalloc() -> str | None:
|
|
|
|
|
block_buffer = bytearray(SDCARD_BLOCK_SIZE_B)
|
|
|
|
|
mnemonic_read = None
|
|
|
|
|
for block_idx in _storage_blocks_gen():
|
|
|
|
|
try:
|
|
|
|
|
sdcard.read(block_idx, block_buffer)
|
|
|
|
@ -73,18 +74,19 @@ def _storage_blocks_gen() -> Generator:
|
|
|
|
|
return _storage_blocks_gen_by_n()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _storage_blocks_gen_by_offset() -> Generator:
|
|
|
|
|
def _storage_blocks_gen_by_offset() -> Generator[int, None, None]:
|
|
|
|
|
cap = sdcard.capacity()
|
|
|
|
|
if cap == 0:
|
|
|
|
|
raise ProcessError
|
|
|
|
|
raise ProcessError("SD card operation failed")
|
|
|
|
|
BLOCK_END = cap // SDCARD_BLOCK_SIZE_B - 1
|
|
|
|
|
return range(SDBACKUP_BLOCK_START, BLOCK_END, SDBACKUP_BLOCK_OFFSET)
|
|
|
|
|
for i in range(SDBACKUP_BLOCK_START, BLOCK_END, SDBACKUP_BLOCK_OFFSET):
|
|
|
|
|
yield i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _storage_blocks_gen_by_n() -> Generator:
|
|
|
|
|
def _storage_blocks_gen_by_n() -> Generator[int, None, None]:
|
|
|
|
|
cap = sdcard.capacity()
|
|
|
|
|
if cap == 0:
|
|
|
|
|
raise ProcessError
|
|
|
|
|
raise ProcessError("SD card operation failed")
|
|
|
|
|
BLOCK_END = cap // SDCARD_BLOCK_SIZE_B - 1
|
|
|
|
|
return (
|
|
|
|
|
SDBACKUP_BLOCK_START
|
|
|
|
@ -111,14 +113,14 @@ Backup Memory Block Layout:
|
|
|
|
|
The total size of the block is defined by SDCARD_BLOCK_SIZE_B.
|
|
|
|
|
"""
|
|
|
|
|
# Constants for offsets and lengths
|
|
|
|
|
MAGIC_OFFSET = 0
|
|
|
|
|
MAGIC_LENGTH = 4
|
|
|
|
|
VERSION_OFFSET = MAGIC_OFFSET + MAGIC_LENGTH
|
|
|
|
|
VERSION_LENGTH = 2
|
|
|
|
|
SEED_LEN_OFFSET = VERSION_OFFSET + VERSION_LENGTH
|
|
|
|
|
SEED_LEN_LENGTH = 4
|
|
|
|
|
MNEMONIC_OFFSET = SEED_LEN_OFFSET + SEED_LEN_LENGTH
|
|
|
|
|
CHECKSUM_LENGTH = 4
|
|
|
|
|
MAGIC_OFFSET = const(0)
|
|
|
|
|
MAGIC_LENGTH = const(4)
|
|
|
|
|
VERSION_OFFSET = const(MAGIC_OFFSET + MAGIC_LENGTH)
|
|
|
|
|
VERSION_LENGTH = const(2)
|
|
|
|
|
SEED_LEN_OFFSET = const(VERSION_OFFSET + VERSION_LENGTH)
|
|
|
|
|
SEED_LEN_LENGTH = const(4)
|
|
|
|
|
MNEMONIC_OFFSET = const(SEED_LEN_OFFSET + SEED_LEN_LENGTH)
|
|
|
|
|
CHECKSUM_LENGTH = const(4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _encode_mnemonic_to_backup_block(mnemonic: bytes) -> bytes:
|
|
|
|
@ -161,7 +163,7 @@ def _decode_mnemonic_from_backup_block(block: bytes) -> bytes | None:
|
|
|
|
|
|
|
|
|
|
def _write_readme() -> None:
|
|
|
|
|
with fatfs.open("README.txt", "w") as f:
|
|
|
|
|
f.write("This is a Trezor backup SD card.")
|
|
|
|
|
f.write(b"This is a Trezor backup SD card.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _write_seed_plain_text(mnemonic_secret: bytes) -> None:
|
|
|
|
|