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