forked from Foundation-Devices/passport-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
227 lines (172 loc) · 6.58 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# SPDX-FileCopyrightText: 2020 Foundation Devices, Inc. <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2018 Coinkite, Inc. <coldcardwallet.com>
# SPDX-License-Identifier: GPL-3.0-only
#
# (c) Copyright 2018 by Coinkite Inc. This file is part of Coldcard <coldcardwallet.com>
# and is covered by GPLv3 license found in COPYING.
#
# main.py - Main initialization code
#
import utime
import uasyncio.core as asyncio
from uasyncio import sleep_ms
from periodic import update_ambient_screen_brightness, update_battery_level, check_auto_shutdown, demo_loop
from schema_evolution import handle_schema_evolutions
#
# Show REPL welcome message
print("Entered main.py")
import gc
print('Available RAM = {}'.format(gc.mem_free()))
SETTINGS_FLASH_START = 0x81E0000
SETTINGS_FLASH_SIZE = 0x20000
# We run main in a separate task so that the startup loop's variables can be released
async def main():
from ux import the_ux
while 1:
await sleep_ms(10)
await the_ux.interact()
async def startup():
import common
from common import pa, loop
from actions import goto_top_menu, validate_passport_hw, initial_pin_setup, start_login_sequence
print("startup()")
common.system.hide_busy_bar()
import uctypes
buf = uctypes.bytearray_at(0x38000000, 1)
# Check for self-test
if buf[0] == 1:
from self_test_ux import SelfTestUX
self_test = SelfTestUX()
await self_test.show()
from accept_terms_ux import AcceptTermsUX
accept_terms = AcceptTermsUX()
await accept_terms.show()
await validate_passport_hw()
# Setup initial PIN if it's blank
if pa.is_blank():
await initial_pin_setup()
# Prompt for PIN and then pick appropriate top-level menu
await start_login_sequence()
# Set the key for the flash cache (cache is inaccessible prior to user logging in)
common.system.show_busy_bar()
common.flash_cache.set_key()
common.flash_cache.load()
# Trigger a get here so that the XFP & XPUB are captured
common.settings.get('xfp')
# See if an update was just performed -- we may need to run a schema evolution script
update_from_to = common.settings.get('update')
# print('update_from_to={}'.format(update_from_to))
if update_from_to:
await handle_schema_evolutions(update_from_to)
common.system.hide_busy_bar()
await goto_top_menu()
loop.create_task(main())
def go():
import common
from sram4 import viewfinder_buf
# Initialize the common objects
# Avalanche noise source
from foundation import Noise
common.noise = Noise()
# Initialize the seed of the PRNG in MicroPython with a real random number
# We only use the PRNG for non-critical randomness that just needs to be fast
import random
from utils import randint
random.seed(randint(0, 2147483647))
# Power monitor
from foundation import Powermon
common.powermon = Powermon()
# Get the async event loop to pass in where needed
common.loop = asyncio.get_event_loop()
# System
from foundation import System
common.system = System()
common.system.show_busy_bar()
# Initialize the keypad
from keypad import Keypad
common.keypad = Keypad()
# Initialize SD card
from files import CardSlot
CardSlot.setup()
# External SPI Flash
from sflash import SPIFlash
common.sf = SPIFlash()
# Initialize internal flash settings
from settings import Settings
common.settings = Settings(common.loop)
# Initialize the external flash cache
from flash_cache import FlashCache
common.flash_cache = FlashCache(common.loop)
# Initialize the display and show the splash screen
from display import Display
common.dis = Display()
common.dis.set_brightness(common.settings.get('screen_brightness', 100))
common.dis.splash()
# Allocate buffers for camera
from constants import VIEWFINDER_WIDTH, VIEWFINDER_HEIGHT, CAMERA_WIDTH, CAMERA_HEIGHT
# QR buf is 1 byte per pixel grayscale
import uctypes
common.qr_buf = uctypes.bytearray_at(0x20000000, CAMERA_WIDTH * CAMERA_HEIGHT)
# common.qr_buf = bytearray(CAMERA_WIDTH * CAMERA_HEIGHT)
# Viewfinder buf 1s 1 bit per pixel and we round the screen width up to 240
# so it's a multiple of 8 bits. The screen height of 303 minus 31 for the
# header and 31 for the footer gives 241 pixels, which we round down to 240
# to give one blank (white) line before the footer.
common.viewfinder_buf = bytearray((VIEWFINDER_WIDTH * VIEWFINDER_HEIGHT) // 8)
# Show REPL welcome message
print("Passport by Foundation Devices Inc. (C) 2020.\n")
from foundation import SettingsFlash
f = SettingsFlash()
try:
from pincodes import PinAttempt
common.pa = PinAttempt()
common.pa.setup(b'')
except RuntimeError as e:
print("Secure Element Problem: %r" % e)
# Setup the startup task
common.loop.create_task(startup())
# Setup check for automatic screen brightness control
# Not used at this time
# common.loop.create_task(update_ambient_screen_brightness())
# Setup check to read battery level and put it in common.battery_level
common.loop.create_task(update_battery_level())
# Setup check for auto shutdown
common.loop.create_task(check_auto_shutdown())
# Setup check to read battery level and put it in common.battery_level
common.loop.create_task(demo_loop())
gc.collect()
print('Available RAM after init = {}'.format(gc.mem_free()))
run_loop()
def run_loop():
# Wrapper for better error handling/recovery at top level.
try:
# This keeps all async tasks alive, including the main task created above
from common import loop
loop.run_forever()
except BaseException as exc:
import sys
sys.print_exception(exc)
# if isinstance(exc, KeyboardInterrupt):
# # preserve GUI state, but want to see where we are
# print("KeyboardInterrupt")
# raise
if isinstance(exc, SystemExit):
# Ctrl-D and warm reboot cause this, not bugs
raise
else:
print("Exception:")
# show stacktrace for debug photos
try:
import uio
import ux
tmp = uio.StringIO()
sys.print_exception(exc, tmp)
msg = tmp.getvalue()
del tmp
print(msg)
ux.show_fatal_error(msg)
except Exception as exc2:
sys.print_exception(exc2)
go()