Skip to content

Commit

Permalink
Rationalize main function and clarify split between Clone and Ethereu…
Browse files Browse the repository at this point in the history
…m mains
  • Loading branch information
fbeutin-ledger committed Dec 1, 2023
1 parent 0b71208 commit cd84bfa
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,69 +747,73 @@ void library_main(libargs_t *args) {
os_lib_end();
}

__attribute__((section(".boot"))) int main(int arg0) {
#ifdef USE_LIB_ETHEREUM
/* Eth clones do not actually contain any logic, they delegate everything to the ETH application.
* Start Eth in lib mode with the correct chain config
*/
__attribute__((noreturn)) void clone_main(libargs_t *args) {
PRINTF("Starting in clone_main\n");
BEGIN_TRY {
TRY {
unsigned int libcall_params[5];
chain_config_t local_chainConfig;
init_coin_config(&local_chainConfig);

PRINTF("Hello from Eth-clone\n");
check_api_level(CX_COMPAT_APILEVEL);
// delegate to Ethereum app/lib
libcall_params[0] = (unsigned int) "Ethereum";
libcall_params[1] = 0x100;
libcall_params[2] = RUN_APPLICATION;
libcall_params[3] = (unsigned int) &local_chainConfig;
#ifdef HAVE_NBGL
const char app_name[] = APPNAME;
caller_app_t capp;
nbgl_icon_details_t icon_details;
uint8_t bitmap[sizeof(ICONBITMAP)];

memcpy(&icon_details, &ICONGLYPH, sizeof(ICONGLYPH));
memcpy(&bitmap, &ICONBITMAP, sizeof(bitmap));
icon_details.bitmap = (const uint8_t *) bitmap;
capp.name = app_name;
capp.icon = &icon_details;
libcall_params[4] = (unsigned int) &capp;
#else
libcall_params[4] = NULL;
#endif // HAVE_NBGL

if (arg0) {
// call as a library
libcall_params[2] = ((unsigned int *) arg0)[1];
libcall_params[4] = ((unsigned int *) arg0)[3]; // library arguments
os_lib_call((unsigned int *) &libcall_params);
((unsigned int *) arg0)[0] = libcall_params[1];
// Clone called by Exchange, forward the request to Ethereum
if (args != NULL) {
if (args->id != 0x100) {
os_sched_exit(0);
}
libcall_params[2] = args->command;
libcall_params[4] = (unsigned int) args->get_printable_amount;
os_lib_call((unsigned int *)&libcall_params);
// ((unsigned int *) arg0)[0] = libcall_params[1];
os_lib_end();
} else {
// launch coin application
libcall_params[1] = 0x100; // use the Init call, as we won't exit
// If the clone is started from the Dashboard on Stax, forward our icon to Ethereum
#ifdef HAVE_NBGL
const char app_name[] = APPNAME;
caller_app_t capp;
nbgl_icon_details_t icon_details;
uint8_t bitmap[sizeof(ICONBITMAP)];

memcpy(&icon_details, &ICONGLYPH, sizeof(ICONGLYPH));
memcpy(&bitmap, &ICONBITMAP, sizeof(bitmap));
icon_details.bitmap = (const uint8_t *) bitmap;
capp.name = app_name;
capp.icon = &icon_details;
libcall_params[4] = (unsigned int) &capp;
#else
libcall_params[4] = 0;
#endif // HAVE_NBGL
os_lib_call((unsigned int *) &libcall_params);
}
}
FINALLY {
}
}
END_TRY;
// no return
#else

// os_lib_call will raise if Ethereum application is not installed. Do not try to recover.
os_sched_exit(-1);
}

int ethereum_main(libargs_t *args) {
// exit critical section
__asm volatile("cpsie i");

// ensure exception will work as planned
os_boot();

if (!arg0) {
if (args == NULL) {
// called from dashboard as standalone eth app
coin_main(NULL);
return 0;
}

libargs_t *args = (libargs_t *) arg0;
if (args->id != 0x100) {
app_exit();
return 0;
Expand All @@ -823,6 +827,13 @@ __attribute__((section(".boot"))) int main(int arg0) {
// called as ethereum or altcoin library
library_main(args);
}
#endif
return 0;
}

__attribute__((section(".boot"))) int main(int arg0) {
#ifdef USE_LIB_ETHEREUM
clone_main((libargs_t *)arg0);
#else
return ethereum_main((libargs_t *) arg0);
#endif
}

0 comments on commit cd84bfa

Please sign in to comment.