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