diff --git a/src/main.c b/src/main.c index 383bebe86f..788e37474c 100644 --- a/src/main.c +++ b/src/main.c @@ -747,47 +747,48 @@ 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); } } @@ -795,21 +796,24 @@ __attribute__((section(".boot"))) int main(int arg0) { } } 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; @@ -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 +}