diff --git a/src_plugins_sdk/plugin_main.c b/src_plugin_sdk/main.c similarity index 57% rename from src_plugins_sdk/plugin_main.c rename to src_plugin_sdk/main.c index c793cf8b4..76c3d4dac 100644 --- a/src_plugins_sdk/plugin_main.c +++ b/src_plugin_sdk/main.c @@ -1,6 +1,6 @@ /***************************************************************************** - * Ledger Plugins SDK. - * (c) 2023 Ledger SAS. + * Ledger Plugin SDK + * (c) 2023 Ledger SAS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,17 @@ * limitations under the License. *****************************************************************************/ +#include "eth_internals.h" +#include "eth_plugin_interface.h" + +// Functions implemented by the plugin +void handle_init_contract(ethPluginInitContract_t *parameters); +void handle_provide_parameter(ethPluginProvideParameter_t *parameters); +void handle_finalize(ethPluginFinalize_t *parameters); +void handle_provide_token(ethPluginProvideInfo_t *parameters); +void handle_query_contract_id(ethQueryContractID_t *parameters); +void handle_query_contract_ui(ethQueryContractUI_t *parameters); + // Calls the ethereum app. void call_app_ethereum() { unsigned int libcall_params[5]; @@ -40,6 +51,33 @@ void call_app_ethereum() { os_lib_call((unsigned int *) &libcall_params); } +// Function to dispatch calls from the ethereum app. +static void dispatch_call(int message, void *parameters) { + switch (message) { + case ETH_PLUGIN_INIT_CONTRACT: + handle_init_contract(parameters); + break; + case ETH_PLUGIN_PROVIDE_PARAMETER: + handle_provide_parameter(parameters); + break; + case ETH_PLUGIN_FINALIZE: + handle_finalize(parameters); + break; + case ETH_PLUGIN_PROVIDE_INFO: + handle_provide_token(parameters); + break; + case ETH_PLUGIN_QUERY_CONTRACT_ID: + handle_query_contract_id(parameters); + break; + case ETH_PLUGIN_QUERY_CONTRACT_UI: + handle_query_contract_ui(parameters); + break; + default: + PRINTF("Unhandled message %d\n", message); + break; + } +} + // Low-level main for plugins. __attribute__((section(".boot"))) int main(int arg0) { // Exit critical section @@ -61,8 +99,13 @@ __attribute__((section(".boot"))) int main(int arg0) { } else { // Not called from dashboard: called from the ethereum app! - // launch plugin main - plugin_main(arg0); + const unsigned int *args = (unsigned int *) arg0; + + // If `ETH_PLUGIN_CHECK_PRESENCE` is set, this means the caller is just trying to + // know whether this app exists or not. We can skip `paraswap_plugin_call`. + if (args[0] != ETH_PLUGIN_CHECK_PRESENCE) { + dispatch_call(args[0], (void *) args[1]); + } } // Call `os_lib_end`, go back to the ethereum app. diff --git a/src_plugins_sdk/plugin_main.h b/src_plugins_sdk/plugin_main.h deleted file mode 100644 index a120c5a7b..000000000 --- a/src_plugins_sdk/plugin_main.h +++ /dev/null @@ -1,19 +0,0 @@ -/***************************************************************************** - * Ledger Plugins SDK. - * (c) 2023 Ledger SAS. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *****************************************************************************/ - -// applicative main for plugins -void plugin_main(int arg0); diff --git a/tools/build_sdk.py b/tools/build_sdk.py index 6cc1e83b4..bc3af58cc 100755 --- a/tools/build_sdk.py +++ b/tools/build_sdk.py @@ -10,6 +10,7 @@ ''' import os +import shutil def extract_from_headers(sources, nodes_to_extract): @@ -28,12 +29,12 @@ def extract_from_headers(sources, nodes_to_extract): if key in line and value in line: node += [line] unclosed_curvy_brackets = line.count('{') - line.count('}') - if unclosed_curvy_brackets == False: + if not unclosed_curvy_brackets: break elif (key == "fn" and value in line) or unclosed_parantheses: node += [line] unclosed_parantheses = line.find(")") == -1 - if unclosed_parantheses == False: + if not unclosed_parantheses: break elif unclosed_curvy_brackets: node += [line] @@ -150,7 +151,8 @@ def merge_c_files(sources, nodes_to_extract): if __name__ == "__main__": - # some nodes will be extracted from these headers and merged into a new one, copied to sdk + # some nodes will be extracted from these headers and merged into a new + # one, copied to sdk headers_to_merge = [ "src/tokens.h", "src/chainConfig.h", @@ -160,13 +162,23 @@ def merge_c_files(sources, nodes_to_extract): "src/shared_context.h", "src/eth_plugin_internal.h", "src/nft.h", - "src/swap_lib_calls.h", - "src_plugins_sdk/plugin_main.h" + "src/swap_lib_calls.h", ] nodes_to_extract = { - "#define": ["MAX_TICKER_LEN", "ADDRESS_LENGTH", "INT256_LENGTH", "WEI_TO_ETHER", "SELECTOR_SIZE", "PARAMETER_LENGTH", "RUN_APPLICATION", "COLLECTION_NAME_MAX_LEN"], + "#define": ["MAX_TICKER_LEN", + "ADDRESS_LENGTH", + "INT256_LENGTH", + "WEI_TO_ETHER", + "SELECTOR_SIZE", + "PARAMETER_LENGTH", + "RUN_APPLICATION", + "COLLECTION_NAME_MAX_LEN"], "typedef enum": [], - "typedef struct": ["tokenDefinition_t", "txInt256_t", "txContent_t", "nftInfo_t", "caller_app_t"], + "typedef struct": ["tokenDefinition_t", + "txInt256_t", + "txContent_t", + "nftInfo_t", + "caller_app_t"], "typedef union": ["extraInfo_t"], "__attribute__((no_instrument_function)) inline": ["int allzeroes"], "const": ["HEXDIGITS"], @@ -180,21 +192,26 @@ def merge_c_files(sources, nodes_to_extract): "void copy_address", "void copy_parameter", "bool U2BE_from_parameter", - "bool U4BE_from_parameter", - "void plugin_main", - "void call_app_ethereum", - "int main"] + "bool U4BE_from_parameter"] } merge_headers(headers_to_merge, nodes_to_extract) - # this header will be stripped from all #include related to previously merged headers, then copied to sdk + # this header will be stripped from all #include related to previously + # merged headers, then copied to sdk copy_header("src/eth_plugin_interface.h", headers_to_merge) # extract and merge function bodies c_files_to_merge = [ "src/utils.c", "src_common/ethUtils.c", - "src/eth_plugin_internal.c", - "src_plugins_sdk/plugin_main.c" + "src/eth_plugin_internal.c", ] merge_c_files(c_files_to_merge, nodes_to_extract["fn"]) + + files_to_copy = [ + "main.c", + ] + + for file in files_to_copy: + shutil.copyfile("src_plugin_sdk/" + file, + "ethereum-plugin-sdk/include/" + file)