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 os_sched_exit(-1);
43}
44
46{
47 UX_INIT();
48 io_seproxyhal_init();
49}
50
52{
53 PRINTF("standalone_app_main");
54#ifdef HAVE_SWAP
55 G_called_from_swap = false;
58#endif // HAVE_SWAP
59
60 BEGIN_TRY
61 {
62 TRY
63 {
65
66 app_main();
67 }
68 CATCH_OTHER(e)
69 {
70 (void) e;
71#ifdef HAVE_DEBUG_THROWS
72 // Disable USB and BLE, the app have crashed and is going to be exited
73 // This is necessary to avoid device freeze while displaying throw error
74 // in a specific case:
75 // - the app receives an APDU
76 // - the app throws before replying
77 // - the app displays the error on screen
78 // - the user unplug the NanoX instead of confirming the screen
79 // - the NanoX goes on battery power and display the lock screen
80 // - the user plug the NanoX instead of entering its pin
81 // - the device is frozen, battery should be removed
82 os_io_stop();
83 // Display crash info on screen for debug purpose
84 assert_display_exit();
85#else // HAVE_DEBUG_THROWS
86 PRINTF("Exiting following exception: 0x%04X\n", e);
87#endif // HAVE_DEBUG_THROWS
88 }
89 FINALLY {}
90 }
91 END_TRY;
92
93 // Exit the application and go back to the dashboard.
94 app_exit();
95}
96
97#ifdef HAVE_SWAP
98// --8<-- [start:library_app_main]
99/* This function is called by the main() function if this application was started by Exchange
100 * through an os_lib_call() as opposed to being started from the Dashboard.
101 *
102 * We dispatch the Exchange request to the correct handler.
103 * Handlers content are not defined in the `lib_standard_app`
104 */
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 // Keep the address at which we'll reply the signing status
123
125
126#ifdef HAVE_NBGL
127 nbgl_useCaseSpinner("Signing");
128#endif // HAVE_NBGL
129
130 app_main();
131 }
132 break;
133 }
134 case CHECK_ADDRESS:
136 break;
139 break;
140 default:
141 break;
142 }
143 }
144 CATCH_OTHER(e)
145 {
146 (void) e;
147 PRINTF("Exiting following exception: 0x%04X\n", e);
148 }
149 FINALLY
150 {
151 os_lib_end();
152 }
153 }
154 END_TRY;
155}
156// --8<-- [end:library_app_main]
157#endif // HAVE_SWAP
158
159WEAK __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}
#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:51
WEAK void common_app_init(void)
Definition main.c:45
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