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
20#include "os.h"
21#include "io.h"
22#include "ledger_assert.h"
23
24#ifdef HAVE_SWAP
25#include "swap.h"
27
28#ifdef HAVE_NBGL
29#include "nbgl_use_case.h"
30#endif // HAVE_NBGL
31#endif // HAVE_SWAP
32
34bolos_ux_params_t G_ux_params;
35
39WEAK void __attribute__((noreturn)) app_exit(void)
40{
41 os_sched_exit(-1);
42}
43
45{
46 UX_INIT();
47
48 io_seproxyhal_init();
49
50#ifdef HAVE_IO_USB
51 USB_power(0);
52 USB_power(1);
53#endif
54
55#ifdef HAVE_BLE
56 BLE_power(0, NULL);
57 BLE_power(1, NULL);
58#endif // HAVE_BLE
59}
60
62{
63#ifdef HAVE_SWAP
64 G_called_from_swap = false;
67#endif // HAVE_SWAP
68
69 BEGIN_TRY
70 {
71 TRY
72 {
74
75 app_main();
76 }
77 CATCH_OTHER(e)
78 {
79 (void) e;
80#ifdef HAVE_DEBUG_THROWS
81 // Disable USB and BLE, the app have crashed and is going to be exited
82 // This is necessary to avoid device freeze while displaying throw error
83 // in a specific case:
84 // - the app receives an APDU
85 // - the app throws before replying
86 // - the app displays the error on screen
87 // - the user unplug the NanoX instead of confirming the screen
88 // - the NanoX goes on battery power and display the lock screen
89 // - the user plug the NanoX instead of entering its pin
90 // - the device is frozen, battery should be removed
91#ifdef HAVE_IO_USB
92 USB_power(0);
93#endif
94#ifdef HAVE_BLE
95 BLE_power(0, NULL);
96#endif
97 // Display crash info on screen for debug purpose
98 assert_display_exit();
99#else // HAVE_DEBUG_THROWS
100 PRINTF("Exiting following exception: 0x%04X\n", e);
101#endif // HAVE_DEBUG_THROWS
102 }
103 FINALLY {}
104 }
105 END_TRY;
106
107 // Exit the application and go back to the dashboard.
108 app_exit();
109}
110
111#ifdef HAVE_SWAP
113{
114 BEGIN_TRY
115 {
116 TRY
117 {
118 PRINTF("Inside library\n");
119 switch (args->command) {
120 case SIGN_TRANSACTION: {
121 // Backup up transaction parameters and wipe BSS to avoid collusion with
122 // app-exchange BSS data.
124 if (success) {
125 // BSS was wiped, we can now init these globals
126 G_called_from_swap = true;
127 G_swap_response_ready = false;
128 // Keep the address at which we'll reply the signing status
130
132
133#ifdef HAVE_NBGL
134 nbgl_useCaseSpinner("Signing");
135#endif // HAVE_NBGL
136
137 app_main();
138 }
139 break;
140 }
141 case CHECK_ADDRESS:
143 break;
146 break;
147 default:
148 break;
149 }
150 }
151 CATCH_OTHER(e)
152 {
153 (void) e;
154 PRINTF("Exiting following exception: 0x%04X\n", e);
155 }
156 FINALLY
157 {
158 os_lib_end();
159 }
160 }
161 END_TRY;
162}
163#endif // HAVE_SWAP
164
165WEAK __attribute__((section(".boot"))) int main(int arg0)
166{
167 // exit critical section
168 __asm volatile("cpsie i");
169
170 // Ensure exception will work as planned
171 os_boot();
172
173 if (arg0 == 0) {
174 // Called from dashboard as standalone App
176 }
177#ifdef HAVE_SWAP
178 else {
179 // Called as library from another app
180 libargs_t *args = (libargs_t *) arg0;
181 if (args->id == 0x100) {
182 library_app_main(args);
183 }
184 else {
185 app_exit();
186 }
187 }
188#endif // HAVE_SWAP
189
190 return 0;
191}
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:34
WEAK void standalone_app_main(void)
Definition main.c:61
WEAK void common_app_init(void)
Definition main.c:44
WEAK void __attribute__((noreturn))
Definition main.c:39
ux_state_t G_ux
Definition main.c:33
WEAK void library_app_main(void)
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