Skip to content

Commit 1723386

Browse files
Split clone main and ethereum main, add comments and remove unnecessary lines
1 parent f64addc commit 1723386

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

src/main.c

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -762,69 +762,77 @@ void library_main(libargs_t *args) {
762762
os_lib_end();
763763
}
764764

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

773-
PRINTF("Hello from Eth-clone\n");
774-
check_api_level(CX_COMPAT_APILEVEL);
775-
// delegate to Ethereum app/lib
776776
libcall_params[0] = (unsigned int) "Ethereum";
777777
libcall_params[1] = 0x100;
778-
libcall_params[2] = RUN_APPLICATION;
779778
libcall_params[3] = (unsigned int) &local_chainConfig;
780-
#ifdef HAVE_NBGL
781-
const char app_name[] = APPNAME;
782-
caller_app_t capp;
783-
nbgl_icon_details_t icon_details;
784-
uint8_t bitmap[sizeof(ICONBITMAP)];
785-
786-
memcpy(&icon_details, &ICONGLYPH, sizeof(ICONGLYPH));
787-
memcpy(&bitmap, &ICONBITMAP, sizeof(bitmap));
788-
icon_details.bitmap = (const uint8_t *) bitmap;
789-
capp.name = app_name;
790-
capp.icon = &icon_details;
791-
libcall_params[4] = (unsigned int) &capp;
792-
#else
793-
libcall_params[4] = NULL;
794-
#endif // HAVE_NBGL
795779

796-
if (arg0) {
797-
// call as a library
798-
libcall_params[2] = ((unsigned int *) arg0)[1];
799-
libcall_params[4] = ((unsigned int *) arg0)[3]; // library arguments
780+
// Clone called by Exchange, forward the request to Ethereum
781+
if (args != NULL) {
782+
if (args->id != 0x100) {
783+
os_sched_exit(0);
784+
}
785+
libcall_params[2] = args->command;
786+
libcall_params[4] = (unsigned int) args->get_printable_amount;
800787
os_lib_call((unsigned int *) &libcall_params);
801-
((unsigned int *) arg0)[0] = libcall_params[1];
788+
// Ethereum fulfilled the request and returned to us. We return to Exchange.
802789
os_lib_end();
803790
} else {
804-
// launch coin application
805-
libcall_params[1] = 0x100; // use the Init call, as we won't exit
791+
// Clone called from Dashboard, start Ethereum
792+
libcall_params[2] = RUN_APPLICATION;
793+
// On Stax, forward our icon to Ethereum
794+
#ifdef HAVE_NBGL
795+
const char app_name[] = APPNAME;
796+
caller_app_t capp;
797+
nbgl_icon_details_t icon_details;
798+
uint8_t bitmap[sizeof(ICONBITMAP)];
799+
800+
memcpy(&icon_details, &ICONGLYPH, sizeof(ICONGLYPH));
801+
memcpy(&bitmap, &ICONBITMAP, sizeof(bitmap));
802+
icon_details.bitmap = (const uint8_t *) bitmap;
803+
capp.name = app_name;
804+
capp.icon = &icon_details;
805+
libcall_params[4] = (unsigned int) &capp;
806+
#else
807+
libcall_params[4] = 0;
808+
#endif // HAVE_NBGL
806809
os_lib_call((unsigned int *) &libcall_params);
810+
// Ethereum should not return to us
811+
os_sched_exit(-1);
807812
}
808813
}
809814
FINALLY {
810815
}
811816
}
812817
END_TRY;
813-
// no return
814-
#else
818+
819+
// os_lib_call will raise if Ethereum application is not installed. Do not try to recover.
820+
os_sched_exit(-1);
821+
}
822+
823+
int ethereum_main(libargs_t *args) {
815824
// exit critical section
816825
__asm volatile("cpsie i");
817826

818827
// ensure exception will work as planned
819828
os_boot();
820829

821-
if (!arg0) {
830+
if (args == NULL) {
822831
// called from dashboard as standalone eth app
823832
coin_main(NULL);
824833
return 0;
825834
}
826835

827-
libargs_t *args = (libargs_t *) arg0;
828836
if (args->id != 0x100) {
829837
app_exit();
830838
return 0;
@@ -838,6 +846,13 @@ __attribute__((section(".boot"))) int main(int arg0) {
838846
// called as ethereum or altcoin library
839847
library_main(args);
840848
}
841-
#endif
842849
return 0;
843850
}
851+
852+
__attribute__((section(".boot"))) int main(int arg0) {
853+
#ifdef USE_LIB_ETHEREUM
854+
clone_main((libargs_t *) arg0);
855+
#else
856+
return ethereum_main((libargs_t *) arg0);
857+
#endif
858+
}

0 commit comments

Comments
 (0)