1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

refactor(core/debug): offload debug configuration from apps.debug

This commit is contained in:
matejcik 2021-03-04 17:00:14 +01:00 committed by matejcik
parent e859c13d70
commit b1ca6ca848
5 changed files with 72 additions and 38 deletions

View File

@ -4,7 +4,9 @@ if not __debug__:
halt("debug mode inactive")
if __debug__:
from trezor import config, log, loop, utils, wire
from storage import debug as storage
from trezor import log, loop, wire
from trezor.ui import display
from trezor.messages import MessageType
from trezor.messages.DebugLinkLayout import DebugLinkLayout
@ -16,17 +18,12 @@ if __debug__:
from trezor.ui import Layout
from trezor.messages.DebugLinkDecision import DebugLinkDecision
from trezor.messages.DebugLinkGetState import DebugLinkGetState
from trezor.messages.DebugLinkLayout import DebugLinkLayout
from trezor.messages.DebugLinkRecordScreen import DebugLinkRecordScreen
from trezor.messages.DebugLinkReseedRandom import DebugLinkReseedRandom
from trezor.messages.DebugLinkState import DebugLinkState
from trezor.messages.DebugLinkEraseSdCard import DebugLinkEraseSdCard
from trezor.messages.DebugLinkWatchLayout import DebugLinkWatchLayout
save_screen = False
save_screen_directory = "."
reset_internal_entropy: bytes | None = None
reset_current_words = loop.chan()
reset_word_index = loop.chan()
@ -40,20 +37,23 @@ if __debug__:
debuglink_decision_chan = loop.chan()
layout_change_chan = loop.chan()
current_content: list[str] = []
watch_layout_changes = False
DEBUG_CONTEXT: wire.Context | None = None
LAYOUT_WATCHER_NONE = 0
LAYOUT_WATCHER_STATE = 1
LAYOUT_WATCHER_LAYOUT = 2
def screenshot() -> bool:
if save_screen:
display.save(save_screen_directory + "/refresh-")
if storage.save_screen:
display.save(storage.save_screen_directory + "/refresh-")
return True
return False
def notify_layout_change(layout: Layout) -> None:
global current_content
current_content = layout.read_content()
if watch_layout_changes:
layout_change_chan.publish(current_content)
storage.current_content[:] = layout.read_content()
if storage.watch_layout_changes:
layout_change_chan.publish(storage.current_content)
async def dispatch_debuglink_decision(msg: DebugLinkDecision) -> None:
from trezor.messages import DebugSwipeDirection
@ -81,11 +81,16 @@ if __debug__:
msg = await debuglink_decision_chan.take()
await dispatch_debuglink_decision(msg)
loop.schedule(debuglink_decision_dispatcher())
async def return_layout_change(ctx: wire.Context) -> None:
async def return_layout_change() -> None:
content = await layout_change_chan.take()
await ctx.write(DebugLinkLayout(lines=content))
assert DEBUG_CONTEXT is not None
if storage.layout_watcher is LAYOUT_WATCHER_LAYOUT:
await DEBUG_CONTEXT.write(DebugLinkLayout(lines=content))
else:
from trezor.messages.DebugLinkState import DebugLinkState
await DEBUG_CONTEXT.write(DebugLinkState(layout_lines=content))
storage.layout_watcher = LAYOUT_WATCHER_NONE
async def touch_hold(x: int, y: int, duration_ms: int) -> None:
from trezor import io
@ -96,10 +101,14 @@ if __debug__:
async def dispatch_DebugLinkWatchLayout(
ctx: wire.Context, msg: DebugLinkWatchLayout
) -> Success:
global watch_layout_changes
from trezor import ui
layout_change_chan.putters.clear()
watch_layout_changes = bool(msg.watch)
log.debug(__name__, "Watch layout changes: {}".format(watch_layout_changes))
await ui.wait_until_layout_is_running()
storage.watch_layout_changes = bool(msg.watch)
log.debug(
__name__, "Watch layout changes: {}".format(storage.watch_layout_changes)
)
return Success()
async def dispatch_DebugLinkDecision(
@ -122,11 +131,12 @@ if __debug__:
debuglink_decision_chan.publish(msg)
if msg.wait:
loop.schedule(return_layout_change(ctx))
storage.layout_watcher = LAYOUT_WATCHER_LAYOUT
loop.schedule(return_layout_change())
async def dispatch_DebugLinkGetState(
ctx: wire.Context, msg: DebugLinkGetState
) -> DebugLinkState:
) -> DebugLinkState | None:
from trezor.messages.DebugLinkState import DebugLinkState
from apps.common import mnemonic, passphrase
@ -134,14 +144,16 @@ if __debug__:
m.mnemonic_secret = mnemonic.get_secret()
m.mnemonic_type = mnemonic.get_type()
m.passphrase_protection = passphrase.is_enabled()
m.reset_entropy = reset_internal_entropy
m.reset_entropy = storage.reset_internal_entropy
if msg.wait_layout:
if not watch_layout_changes:
if not storage.watch_layout_changes:
raise wire.ProcessError("Layout is not watched")
m.layout_lines = await layout_change_chan.take()
storage.layout_watcher = LAYOUT_WATCHER_STATE
loop.schedule(return_layout_change())
return None
else:
m.layout_lines = current_content
m.layout_lines = storage.current_content
if msg.wait_word_pos:
m.reset_word_pos = await reset_word_index.take()
@ -152,14 +164,11 @@ if __debug__:
async def dispatch_DebugLinkRecordScreen(
ctx: wire.Context, msg: DebugLinkRecordScreen
) -> Success:
global save_screen_directory
global save_screen
if msg.target_directory:
save_screen_directory = msg.target_directory
save_screen = True
storage.save_screen_directory = msg.target_directory
storage.save_screen = True
else:
save_screen = False
storage.save_screen = False
display.clear_save() # clear C buffers
return Success()
@ -201,9 +210,7 @@ if __debug__:
config.wipe()
workflow_handlers.register(MessageType.DebugLinkDecision, dispatch_DebugLinkDecision) # type: ignore
workflow_handlers.register(
MessageType.DebugLinkGetState, dispatch_DebugLinkGetState
)
workflow_handlers.register(MessageType.DebugLinkGetState, dispatch_DebugLinkGetState) # type: ignore
workflow_handlers.register(
MessageType.DebugLinkReseedRandom, dispatch_DebugLinkReseedRandom
)
@ -216,3 +223,7 @@ if __debug__:
workflow_handlers.register(
MessageType.DebugLinkWatchLayout, dispatch_DebugLinkWatchLayout
)
loop.schedule(debuglink_decision_dispatcher())
if storage.layout_watcher is not LAYOUT_WATCHER_NONE:
loop.schedule(return_layout_change())

View File

@ -14,7 +14,7 @@ from ..change_pin import request_pin_confirm
from . import layout
if __debug__:
from apps import debug
import storage.debug
if False:
from trezor.messages.ResetDevice import ResetDevice
@ -48,7 +48,7 @@ async def reset_device(ctx: wire.Context, msg: ResetDevice) -> Success:
# generate and display internal entropy
int_entropy = random.bytes(32)
if __debug__:
debug.reset_internal_entropy = int_entropy
storage.debug.reset_internal_entropy = int_entropy
if msg.display_random:
await layout.show_internal_entropy(ctx, int_entropy)

View File

@ -4,6 +4,8 @@
# Import always-active modules
import storage
import storage.device
if __debug__:
import storage.debug
from trezor import config, pin, utils # noqa: F401
# Prepare the USB interfaces first. Do not connect to the host yet.

16
core/src/storage/debug.py Normal file
View File

@ -0,0 +1,16 @@
from trezorutils import halt
if not __debug__:
halt("Debugging is disabled")
if __debug__:
save_screen = False
save_screen_directory = "."
current_content: list[str] = [""] * 20
current_content.clear()
watch_layout_changes = False
layout_watcher = 0
reset_internal_entropy: bytes = b""

View File

@ -296,6 +296,11 @@ async def handle_session(
req_type = None
req_msg = None
if __debug__ and is_debug_session:
import apps.debug
apps.debug.DEBUG_CONTEXT = ctx
modules = utils.unimport_begin()
while True:
try: