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#endif // HAVE_SWAP
29
30#ifdef HAVE_NBGL
31#include "nbgl_use_case.h"
32#endif // HAVE_NBGL
33
35bolos_ux_params_t G_ux_params;
36
40WEAK void __attribute__((noreturn)) app_exit(void)
41{
42#ifndef USE_OS_IO_STACK
43 os_io_stop();
44#endif // USE_OS_IO_STACK
45 os_sched_exit(-1);
46}
47
49{
50 UX_INIT();
51 io_seproxyhal_init();
52
53#ifdef HAVE_APP_STORAGE
54 /* Implicit app storage initialization */
55 app_storage_init();
56#endif // #ifdef HAVE_APP_STORAGE
57}
58
60{
61 PRINTF("standalone_app_main\n");
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 (void) e;
79#ifdef HAVE_DEBUG_THROWS
80 // Disable USB and BLE, the app have crashed and is going to be exited
81 // This is necessary to avoid device freeze while displaying throw error
82 // in a specific case:
83 // - the app receives an APDU
84 // - the app throws before replying
85 // - the app displays the error on screen
86 // - the user unplug the NanoX instead of confirming the screen
87 // - the NanoX goes on battery power and display the lock screen
88 // - the user plug the NanoX instead of entering its pin
89 // - the device is frozen, battery should be removed
90 os_io_stop();
91 // Display crash info on screen for debug purpose
92 assert_display_exit();
93#else // HAVE_DEBUG_THROWS
94 PRINTF("Exiting following exception: 0x%04X\n", e);
95#endif // HAVE_DEBUG_THROWS
96 }
97 FINALLY {}
98 }
99 END_TRY;
100
101 // Exit the application and go back to the dashboard.
102 app_exit();
103}
104
105#ifdef HAVE_SWAP
106// --8<-- [start:library_app_main]
107/* This function is called by the main() function if this application was started by Exchange
108 * through an os_lib_call() as opposed to being started from the Dashboard.
109 *
110 * We dispatch the Exchange request to the correct handler.
111 * Handlers content are not defined in the `lib_standard_app`
112 */
114{
115 BEGIN_TRY
116 {
117 TRY
118 {
119 PRINTF("Inside library\n");
120 switch (args->command) {
121 case SIGN_TRANSACTION: {
122 // Backup up transaction parameters and wipe BSS to avoid collusion with
123 // app-exchange BSS data.
125 if (success) {
126 // BSS was wiped, we can now init these globals
127 G_called_from_swap = true;
128 G_swap_response_ready = false;
129 // Keep the address at which we'll reply the signing status
131
133
134#ifdef HAVE_NBGL
135 nbgl_useCaseSpinner("Signing");
136#endif // HAVE_NBGL
137
138 app_main();
139 }
140 break;
141 }
142 case CHECK_ADDRESS:
144 break;
147 break;
148 default:
149 break;
150 }
151 }
152 CATCH_OTHER(e)
153 {
154 (void) e;
155 PRINTF("Exiting following exception: 0x%04X\n", e);
156 }
157 FINALLY
158 {
159 os_lib_end();
160 }
161 }
162 END_TRY;
163}
164// --8<-- [end:library_app_main]
165#endif // HAVE_SWAP
166
167WEAK __attribute__((section(".boot"))) int main(int arg0)
168{
169 // exit critical section
170 __asm volatile("cpsie i");
171
172 // Ensure exception will work as planned
173 os_boot();
174
175 if (arg0 == 0) {
176 // Called from dashboard as standalone App
178 }
179#ifdef HAVE_SWAP
180 else {
181 // Called as library from another app
182 libargs_t *args = (libargs_t *) arg0;
183 if (args->id == 0x100) {
184 library_app_main(args);
185 }
186 else {
187 app_exit();
188 }
189 }
190#endif // HAVE_SWAP
191
192 return 0;
193}
#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:59
WEAK void common_app_init(void)
Definition main.c:48
WEAK void __attribute__((noreturn))
Definition main.c:40
ux_state_t G_ux
Definition main.c:34
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:341