Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2020 Ledger SAS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <stdint.h> // uint*_t
18#include <string.h> // memset, explicit_bzero
19
21#include "os.h"
22#include "io.h"
23#include "ledger_assert.h"
24
25#ifdef HAVE_SWAP
26#include "swap.h"
28
29#ifdef HAVE_NBGL
30#include "nbgl_use_case.h"
31#endif // HAVE_NBGL
32#endif // HAVE_SWAP
33
35bolos_ux_params_t G_ux_params;
36
40WEAK void __attribute__((noreturn)) app_exit(void)
41{
42 os_sched_exit(-1);
43}
44
46{
47 UX_INIT();
48
49 io_seproxyhal_init();
50
51#ifdef HAVE_IO_USB
52 USB_power(0);
53 USB_power(1);
54#endif
55
56#ifdef HAVE_BLE
57 BLE_power(0, NULL);
58 BLE_power(1, NULL);
59#endif // HAVE_BLE
60
61#ifdef HAVE_APP_STORAGE
62 /* Implicit app storage initialization */
63 app_storage_init();
64#endif // #ifdef HAVE_APP_STORAGE
65}
66
68{
69#ifdef HAVE_SWAP
70 G_called_from_swap = false;
73#endif // HAVE_SWAP
74
75 BEGIN_TRY
76 {
77 TRY
78 {
80
81 app_main();
82 }
83 CATCH_OTHER(e)
84 {
85 (void) e;
86#ifdef HAVE_DEBUG_THROWS
87 // Disable USB and BLE, the app have crashed and is going to be exited
88 // This is necessary to avoid device freeze while displaying throw error
89 // in a specific case:
90 // - the app receives an APDU
91 // - the app throws before replying
92 // - the app displays the error on screen
93 // - the user unplug the NanoX instead of confirming the screen
94 // - the NanoX goes on battery power and display the lock screen
95 // - the user plug the NanoX instead of entering its pin
96 // - the device is frozen, battery should be removed
97#ifdef HAVE_IO_USB
98 USB_power(0);
99#endif
100#ifdef HAVE_BLE
101 BLE_power(0, NULL);
102#endif
103 // Display crash info on screen for debug purpose
104 assert_display_exit();
105#else
106 PRINTF("Exiting following exception: 0x%04X\n", e);
107#endif
108 }
109 FINALLY {}
110 }
111 END_TRY;
112
113 // Exit the application and go back to the dashboard.
114 app_exit();
115}
116
117#ifdef HAVE_SWAP
118// --8<-- [start:library_app_main]
119/* This function is called by the main() function if this application was started by Exchange
120 * through an os_lib_call() as opposed to being started from the Dashboard.
121 *
122 * We dispatch the Exchange request to the correct handler.
123 * Handlers content are not defined in the `lib_standard_app`
124 */
125WEAK void library_app_main(libargs_t *args)
126{
127 BEGIN_TRY
128 {
129 TRY
130 {
131 PRINTF("Inside library\n");
132 switch (args->command) {
133 case SIGN_TRANSACTION: {
134 // Backup up transaction parameters and wipe BSS to avoid collusion with
135 // app-exchange BSS data.
137 if (success) {
138 // BSS was wiped, we can now init these globals
139 G_called_from_swap = true;
140 G_swap_response_ready = false;
141 // Keep the address at which we'll reply the signing status
143
145
146#ifdef HAVE_NBGL
147 nbgl_useCaseSpinner("Signing");
148#endif // HAVE_NBGL
149
150 app_main();
151 }
152 break;
153 }
154 case CHECK_ADDRESS:
156 break;
159 break;
160 default:
161 break;
162 }
163 }
164 CATCH_OTHER(e)
165 {
166 (void) e;
167 PRINTF("Exiting following exception: 0x%04X\n", e);
168 }
169 FINALLY
170 {
171 os_lib_end();
172 }
173 }
174 END_TRY;
175}
176// --8<-- [end:library_app_main]
177#endif // HAVE_SWAP
178
179WEAK __attribute__((section(".boot"))) int main(int arg0)
180{
181 // exit critical section
182 __asm volatile("cpsie i");
183
184 // Ensure exception will work as planned
185 os_boot();
186
187 if (arg0 == 0) {
188 // Called from dashboard as standalone App
190 }
191#ifdef HAVE_SWAP
192 else {
193 // Called as library from another app
194 libargs_t *args = (libargs_t *) arg0;
195 if (args->id == 0x100) {
196 library_app_main(args);
197 }
198 else {
199 app_exit();
200 }
201 }
202#endif // HAVE_SWAP
203
204 return 0;
205}
void USB_power(unsigned char enabled)
Definition usbd_impl.c:1560
void BLE_power(unsigned char powered, const char *discovered_name)
#define WEAK
Definition macros.h:8
bolos_ux_params_t G_ux_params
Definition main.c:35
WEAK void standalone_app_main(void)
Definition main.c:67
WEAK void common_app_init(void)
Definition main.c:45
WEAK void __attribute__((noreturn))
Definition main.c:40
ux_state_t G_ux
Definition main.c:34
API of the Advanced BOLOS Graphical Library, for typical application use-cases.
void nbgl_useCaseSpinner(const char *text)
unsigned int command
get_printable_amount_parameters_t * get_printable_amount
create_transaction_parameters_t * create_transaction
check_address_parameters_t * check_address
unsigned int id
void swap_handle_get_printable_amount(get_printable_amount_parameters_t *params)
void swap_handle_check_address(check_address_parameters_t *params)
bool swap_copy_transaction_parameters(create_transaction_parameters_t *sign_transaction_params)
#define SIGN_TRANSACTION
#define CHECK_ADDRESS
#define GET_PRINTABLE_AMOUNT
volatile bool G_called_from_swap
volatile bool G_swap_response_ready
volatile uint8_t * G_swap_signing_return_value_address
#define UX_INIT()
Definition ux_bagl.h:348