Line data Source code
1 : /** 2 : * The MIT License (MIT) 3 : * 4 : * Copyright (c) 2021 RSK Labs Ltd 5 : * 6 : * Permission is hereby granted, free of charge, to any person obtaining a copy 7 : * of this software and associated documentation files (the "Software"), to 8 : * deal in the Software without restriction, including without limitation the 9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 : * sell copies of the Software, and to permit persons to whom the Software is 11 : * furnished to do so, subject to the following conditions: 12 : * 13 : * The above copyright notice and this permission notice shall be included in 14 : * all copies or substantial portions of the Software. 15 : * 16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 : * IN THE SOFTWARE. 23 : */ 24 : #include "bolos_ux_common.h" 25 : #include "ux_handlers.h" 26 : #include "bootloader.h" 27 : #include "ui_heartbeat.h" 28 : 29 : dashboard_action_t dashboard_action; 30 : 31 6 : void set_dashboard_action(dashboard_action_t action) { 32 6 : dashboard_action = action; 33 6 : } 34 : 35 : /** 36 : * Run the signer application 37 : * 38 : * @returns bool whether the application was scheduled to run 39 : */ 40 3 : static bool run_signer_app(void) { 41 3 : unsigned int i = 0; 42 4 : while (i < os_registry_count()) { 43 : application_t app; 44 3 : os_registry_get(i, &app); 45 3 : if (!(app.flags & APPLICATION_FLAG_BOLOS_UX)) { 46 3 : if (is_authorized_signer(app.hash)) { 47 2 : G_bolos_ux_context.app_auto_started = 1; 48 2 : set_dashboard_action(DASHBOARD_ACTION_UI_HEARTBEAT); 49 2 : screen_stack_pop(); 50 2 : io_seproxyhal_disable_io(); 51 2 : os_sched_exec(i); // no return 52 2 : return true; 53 : } 54 : } 55 1 : i++; 56 : } 57 1 : return false; 58 : } 59 : 60 : /** 61 : * BOLOS_UX_BOOT_ONBOARDING handler 62 : * 63 : * Shows onboarding screen and waits for commands if device is not onboarded, 64 : * does nothing otherwise. 65 : * 66 : * @ret BOLOS_UX_OK if device is already onboarded, never returns if an actual 67 : * onboarding was performed 68 : */ 69 2 : unsigned int handle_bolos_ux_boot_onboarding() { 70 : // re apply settings in the L4 (ble, brightness, etc) after exiting 71 : // application in case of wipe 72 2 : screen_settings_apply(); 73 : 74 : // request animation when dashboard has finished displaying all the 75 : // elements (after onboarding OR the first time displayed) 76 2 : G_bolos_ux_context.dashboard_redisplayed = 1; 77 : 78 : // avoid reperso is already onboarded to avoid leaking data through 79 : // parameters due to user land call 80 2 : if (os_perso_isonboarded()) { 81 1 : return BOLOS_UX_OK; 82 : } 83 : 84 1 : io_seproxyhal_init(); 85 1 : USB_power(1); 86 1 : screen_settings_apply(); 87 1 : screen_not_personalized_init(); 88 1 : bootloader_main(BOOTLOADER_MODE_ONBOARD); 89 : // bootloader_main() actually never returns when onboarding mode is active, 90 : // so this value is never actually returned to the caller 91 1 : return BOLOS_UX_CANCEL; 92 : } 93 : 94 : /** 95 : * BOLOS_UX_DASHBOARD handler 96 : * 97 : * Different ways of handling this depending 98 : * on the value of dashboard_action 99 : * Can run the heartbeat frontend, the app or 100 : * the dashboard itself. 101 : */ 102 4 : void handle_bolos_ux_boot_dashboard() { 103 4 : if (dashboard_action == DASHBOARD_ACTION_UI_HEARTBEAT) { 104 1 : USB_power(0); 105 1 : io_seproxyhal_disable_io(); 106 1 : USB_power(1); 107 1 : io_seproxyhal_init(); 108 : 109 : // Run the heartbeat frontend and then 110 : // run the app (i.e., signer) upon exit 111 1 : ui_heartbeat_main(&G_bolos_ux_context.ui_heartbeat); 112 1 : if (run_signer_app()) 113 1 : return; 114 : } 115 : 116 : // apply settings when redisplaying dashboard 117 3 : screen_settings_apply(); 118 : 119 : // when returning from application, the ticker could have been 120 : // disabled 121 3 : io_seproxyhal_setup_ticker(100); 122 : 123 : // Run application (i.e., signer) 124 3 : if (dashboard_action == DASHBOARD_ACTION_APP) { 125 2 : if (run_signer_app()) 126 1 : return; 127 : } 128 : 129 : // If we're here, then the dashboard action 130 : // is dashboard itself. Init the dashboard screen. 131 2 : screen_dashboard_init(); 132 : } 133 : 134 : /** 135 : * BOLOS_UX_VALIDATE_PIN handler 136 : * 137 : * Runs the bootloader_main function 138 : * 139 : * @ret BOLOS_UX_OK if bootloader_main runs successfully 140 : */ 141 1 : unsigned int handle_bolos_ux_boot_validate_pin() { 142 1 : io_seproxyhal_init(); 143 1 : USB_power(1); 144 1 : bootloader_main(BOOTLOADER_MODE_DEFAULT); 145 1 : return BOLOS_UX_OK; 146 : } 147 : 148 : /** 149 : * BOLOS_UX_CONSENT_APP_ADD handler 150 : * 151 : * Unlocks the device only if the signer app is authorized 152 : * 153 : * @ret BOLOS_UX_OK if the signer app is authorized and the device was unlocked, 154 : * BOLOS_UX_CANCEL otherwise 155 : */ 156 2 : unsigned int handle_bolos_ux_boot_consent_app_add(unsigned char *app_hash) { 157 2 : if (is_authorized_signer(app_hash)) { 158 : // PIN is invalidated so we must check it again. The pin value 159 : // used here is the same as in RSK_UNLOCK_CMD, so we also 160 : // don't have a prepended length byte 161 1 : unlock_with_pin(false); 162 1 : clear_pin(); 163 1 : return BOLOS_UX_OK; 164 : } else { 165 1 : return BOLOS_UX_CANCEL; 166 : } 167 : } 168 : 169 : /** 170 : * BOLOS_UX_CONSENT_FOREIGN_KEY handler 171 : * 172 : * Returns BOLOS_UX_OK to the caller 173 : * 174 : * @ret BOLOS_UX_OK 175 : */ 176 1 : unsigned int handle_bolos_ux_boot_consent_foreing_key() { 177 1 : return BOLOS_UX_OK; 178 : } 179 : 180 : /** 181 : * BOLOS_UX_CONSENT_APP_DEL handler 182 : * 183 : * Returns BOLOS_UX_OK to the caller 184 : * 185 : * @ret BOLOS_UX_OK 186 : */ 187 1 : unsigned int handle_bolos_ux_boot_consent_app_del() { 188 1 : return BOLOS_UX_OK; 189 : } 190 : 191 : /** 192 : * BOLOS_UX_PROCESSING handler 193 : * 194 : * Shows processing screen 195 : */ 196 1 : void handle_bolos_ux_boot_processing() { 197 1 : screen_processing_init(); 198 1 : }