1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 06:18:07 +00:00

chore(core): decrease debug size by 240 bytes

This commit is contained in:
grdddj 2022-09-18 16:43:49 +02:00 committed by matejcik
parent a29ea11ea2
commit 5e7cc8b692
2 changed files with 91 additions and 95 deletions

View File

@ -68,7 +68,7 @@ if __debug__:
if storage.watch_layout_changes or layout_change_chan.takers:
layout_change_chan.publish(storage.current_content)
async def dispatch_debuglink_decision(msg: DebugLinkDecision) -> None:
async def _dispatch_debuglink_decision(msg: DebugLinkDecision) -> None:
from trezor.enums import DebugButton, DebugSwipeDirection
from trezor.ui import Result
from trezor.ui.components.common import (
@ -82,22 +82,24 @@ if __debug__:
confirm = trezorui2
else:
from trezor.ui.components.tt import confirm
button = msg.button # local_cache_attribute
swipe = msg.swipe # local_cache_attribute
if msg.button is not None:
if msg.button == DebugButton.NO:
if button is not None:
if button == DebugButton.NO:
await confirm_chan.put(Result(confirm.CANCELLED))
elif msg.button == DebugButton.YES:
elif button == DebugButton.YES:
await confirm_chan.put(Result(confirm.CONFIRMED))
elif msg.button == DebugButton.INFO:
elif button == DebugButton.INFO:
await confirm_chan.put(Result(confirm.INFO))
if msg.swipe is not None:
if msg.swipe == DebugSwipeDirection.UP:
if swipe is not None:
if swipe == DebugSwipeDirection.UP:
await swipe_chan.put(SWIPE_UP)
elif msg.swipe == DebugSwipeDirection.DOWN:
elif swipe == DebugSwipeDirection.DOWN:
await swipe_chan.put(SWIPE_DOWN)
elif msg.swipe == DebugSwipeDirection.LEFT:
elif swipe == DebugSwipeDirection.LEFT:
await swipe_chan.put(SWIPE_LEFT)
elif msg.swipe == DebugSwipeDirection.RIGHT:
elif swipe == DebugSwipeDirection.RIGHT:
await swipe_chan.put(SWIPE_RIGHT)
if msg.input is not None:
await input_chan.put(Result(msg.input))
@ -105,7 +107,7 @@ if __debug__:
async def debuglink_decision_dispatcher() -> None:
while True:
msg = await debuglink_decision_chan.take()
await dispatch_debuglink_decision(msg)
await _dispatch_debuglink_decision(msg)
async def return_layout_change() -> None:
content = await layout_change_chan.take()
@ -143,13 +145,15 @@ if __debug__:
if debuglink_decision_chan.putters:
log.warning(__name__, "DebugLinkDecision queue is not empty")
x = msg.x # local_cache_attribute
y = msg.y # local_cache_attribute
if msg.x is not None and msg.y is not None:
evt_down = io.TOUCH_START, msg.x, msg.y
evt_up = io.TOUCH_END, msg.x, msg.y
if x is not None and y is not None:
evt_down = io.TOUCH_START, x, y
evt_up = io.TOUCH_END, x, y
loop.synthetic_events.append((io.TOUCH, evt_down))
if msg.hold_ms is not None:
loop.schedule(touch_hold(msg.x, msg.y, msg.hold_ms))
loop.schedule(touch_hold(x, y, msg.hold_ms))
else:
loop.synthetic_events.append((io.TOUCH, evt_up))
else:
@ -212,38 +216,34 @@ if __debug__:
) -> Success:
from trezor import io
sdcard = io.sdcard # local_cache_attribute
try:
io.sdcard.power_on()
sdcard.power_on()
if msg.format:
io.fatfs.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)
assert sdcard.capacity() >= 1024 * 1024
empty_block = bytes([0xFF] * sdcard.BLOCK_SIZE)
for i in range(1024 * 1024 // sdcard.BLOCK_SIZE):
sdcard.write(i, empty_block)
except OSError:
raise wire.ProcessError("SD card operation failed")
finally:
io.sdcard.power_off()
sdcard.power_off()
return Success()
def boot() -> None:
workflow_handlers.register(MessageType.DebugLinkDecision, dispatch_DebugLinkDecision) # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkDecision) -> Coroutine[Any, Any, None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
workflow_handlers.register(MessageType.DebugLinkGetState, dispatch_DebugLinkGetState) # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkGetState) -> Coroutine[Any, Any, DebugLinkState | None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
workflow_handlers.register(
MessageType.DebugLinkReseedRandom, dispatch_DebugLinkReseedRandom
)
workflow_handlers.register(
MessageType.DebugLinkRecordScreen, dispatch_DebugLinkRecordScreen
)
workflow_handlers.register(
MessageType.DebugLinkEraseSdCard, dispatch_DebugLinkEraseSdCard
)
workflow_handlers.register(
MessageType.DebugLinkWatchLayout, dispatch_DebugLinkWatchLayout
)
register = workflow_handlers.register # local_cache_attribute
register(MessageType.DebugLinkDecision, dispatch_DebugLinkDecision) # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkDecision) -> Coroutine[Any, Any, None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
register(MessageType.DebugLinkGetState, dispatch_DebugLinkGetState) # type: ignore [Argument of type "(ctx: Context, msg: DebugLinkGetState) -> Coroutine[Any, Any, DebugLinkState | None]" cannot be assigned to parameter "handler" of type "Handler[Msg@register]" in function "register"]
register(MessageType.DebugLinkReseedRandom, dispatch_DebugLinkReseedRandom)
register(MessageType.DebugLinkRecordScreen, dispatch_DebugLinkRecordScreen)
register(MessageType.DebugLinkEraseSdCard, dispatch_DebugLinkEraseSdCard)
register(MessageType.DebugLinkWatchLayout, dispatch_DebugLinkWatchLayout)
loop.schedule(debuglink_decision_dispatcher())
if storage.layout_watcher is not LAYOUT_WATCHER_NONE:

View File

@ -1,78 +1,42 @@
from typing import TYPE_CHECKING
import storage
import storage.device
from trezor import config, wire
from trezor.crypto import bip39, slip39
from trezor.enums import BackupType
from trezor.messages import Success
from trezor.ui.layouts import confirm_action
from apps.management import backup_types
if TYPE_CHECKING:
from trezor.messages import LoadDevice
from trezor.messages import LoadDevice, Success
from trezor.wire import Context
async def load_device(ctx: wire.Context, msg: LoadDevice) -> Success:
word_count = _validate(msg)
is_slip39 = backup_types.is_slip39_word_count(word_count)
async def load_device(ctx: Context, msg: LoadDevice) -> Success:
import storage.device as storage_device
from trezor import config
from trezor.crypto import bip39, slip39
from trezor.enums import BackupType
from trezor.messages import Success
from apps.management import backup_types
from trezor.wire import UnexpectedMessage, ProcessError
from trezor.ui.layouts import confirm_action
if not is_slip39 and not msg.skip_checksum and not bip39.check(msg.mnemonics[0]):
raise wire.ProcessError("Mnemonic is not valid")
mnemonics = msg.mnemonics # local_cache_attribute
await _warn(ctx)
if not is_slip39: # BIP-39
secret = msg.mnemonics[0].encode()
backup_type = BackupType.Bip39
else:
identifier, iteration_exponent, secret = slip39.recover_ems(msg.mnemonics)
# this must succeed if the recover_ems call succeeded
share = slip39.decode_mnemonic(msg.mnemonics[0])
if share.group_count == 1:
backup_type = BackupType.Slip39_Basic
elif share.group_count > 1:
backup_type = BackupType.Slip39_Advanced
else:
raise wire.ProcessError("Invalid group count")
storage.device.set_slip39_identifier(identifier)
storage.device.set_slip39_iteration_exponent(iteration_exponent)
storage.device.store_mnemonic_secret(
secret,
backup_type,
needs_backup=msg.needs_backup is True,
no_backup=msg.no_backup is True,
)
storage.device.set_passphrase_enabled(bool(msg.passphrase_protection))
storage.device.set_label(msg.label or "")
if msg.pin:
config.change_pin("", msg.pin, None, None)
return Success(message="Device loaded")
def _validate(msg: LoadDevice) -> int:
if storage.device.is_initialized():
raise wire.UnexpectedMessage("Already initialized")
if not msg.mnemonics:
raise wire.ProcessError("No mnemonic provided")
# _validate
if storage_device.is_initialized():
raise UnexpectedMessage("Already initialized")
if not mnemonics:
raise ProcessError("No mnemonic provided")
word_count = len(msg.mnemonics[0].split(" "))
for m in msg.mnemonics[1:]:
for m in mnemonics[1:]:
if word_count != len(m.split(" ")):
raise wire.ProcessError(
raise ProcessError(
"All shares are required to have the same number of words"
)
# END _validate
return word_count
is_slip39 = backup_types.is_slip39_word_count(word_count)
if not is_slip39 and not msg.skip_checksum and not bip39.check(mnemonics[0]):
raise ProcessError("Mnemonic is not valid")
async def _warn(ctx: wire.Context) -> None:
# _warn
await confirm_action(
ctx,
"warn_loading_seed",
@ -80,3 +44,35 @@ async def _warn(ctx: wire.Context) -> None:
"Loading private seed\nis not recommended.",
"Continue only if you\nknow what you are doing!",
)
# END _warn
if not is_slip39: # BIP-39
secret = msg.mnemonics[0].encode()
backup_type = BackupType.Bip39
else:
identifier, iteration_exponent, secret = slip39.recover_ems(mnemonics)
# this must succeed if the recover_ems call succeeded
share = slip39.decode_mnemonic(mnemonics[0])
if share.group_count == 1:
backup_type = BackupType.Slip39_Basic
elif share.group_count > 1:
backup_type = BackupType.Slip39_Advanced
else:
raise ProcessError("Invalid group count")
storage_device.set_slip39_identifier(identifier)
storage_device.set_slip39_iteration_exponent(iteration_exponent)
storage_device.store_mnemonic_secret(
secret,
backup_type,
needs_backup=msg.needs_backup is True,
no_backup=msg.no_backup is True,
)
storage_device.set_passphrase_enabled(bool(msg.passphrase_protection))
storage_device.set_label(msg.label or "")
if msg.pin:
config.change_pin("", msg.pin, None, None)
return Success(message="Device loaded")