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