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
55{
56 PRINTF("standalone_app_main");
57#ifdef HAVE_SWAP
58 G_called_from_swap = false;
61#endif // HAVE_SWAP
62
63 BEGIN_TRY
64 {
65 TRY
66 {
68
69 app_main();
70 }
71 CATCH_OTHER(e)
72 {
73 (void) e;
74#ifdef HAVE_DEBUG_THROWS
75 // Disable USB and BLE, the app have crashed and is going to be exited
76 // This is necessary to avoid device freeze while displaying throw error
77 // in a specific case:
78 // - the app receives an APDU
79 // - the app throws before replying
80 // - the app displays the error on screen
81 // - the user unplug the NanoX instead of confirming the screen
82 // - the NanoX goes on battery power and display the lock screen
83 // - the user plug the NanoX instead of entering its pin
84 // - the device is frozen, battery should be removed
85 os_io_stop();
86 // Display crash info on screen for debug purpose
87 assert_display_exit();
88#else // HAVE_DEBUG_THROWS
89 PRINTF("Exiting following exception: 0x%04X\n", e);
90#endif // HAVE_DEBUG_THROWS
91 }
92 FINALLY {}
93 }
94 END_TRY;
95
96 // Exit the application and go back to the dashboard.
97 app_exit();
98}
99
100#ifdef HAVE_SWAP
101// --8<-- [start:library_app_main]
102/* This function is called by the main() function if this application was started by Exchange
103 * through an os_lib_call() as opposed to being started from the Dashboard.
104 *
105 * We dispatch the Exchange request to the correct handler.
106 * Handlers content are not defined in the `lib_standard_app`
107 */
109{
110 BEGIN_TRY
111 {
112 TRY
113 {
114 PRINTF("Inside library\n");
115 switch (args->command) {
116 case SIGN_TRANSACTION: {
117 // Backup up transaction parameters and wipe BSS to avoid collusion with
118 // app-exchange BSS data.
120 if (success) {
121 // BSS was wiped, we can now init these globals
122 G_called_from_swap = true;
123 G_swap_response_ready = false;
124 // Keep the address at which we'll reply the signing status
126
128
129#ifdef HAVE_NBGL
130 nbgl_useCaseSpinner("Signing");
131#endif // HAVE_NBGL
132
133 app_main();
134 }
135 break;
136 }
137 case CHECK_ADDRESS:
139 break;
142 break;
143 default:
144 break;
145 }
146 }
147 CATCH_OTHER(e)
148 {
149 (void) e;
150 PRINTF("Exiting following exception: 0x%04X\n", e);
151 }
152 FINALLY
153 {
154 os_lib_end();
155 }
156 }
157 END_TRY;
158}
159// --8<-- [end:library_app_main]
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}
#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:54
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