From ce190aa9c1679aeb0c4eb552bc3060e9de281e48 Mon Sep 17 00:00:00 2001 From: Charles-Edouard de la Vergne Date: Thu, 19 Oct 2023 17:52:58 +0200 Subject: [PATCH] Activate Standard App Files; Add App manifest --- Makefile | 10 +-- ledger_app.toml | 8 ++ src/gpg_main.c | 149 +++++--------------------------- src/gpg_ram.c | 32 ------- src/{gpg_nvram.c => gpg_vars.c} | 7 +- src/gpg_vars.h | 11 --- 6 files changed, 35 insertions(+), 182 deletions(-) create mode 100644 ledger_app.toml delete mode 100644 src/gpg_ram.c rename src/{gpg_nvram.c => gpg_vars.c} (91%) diff --git a/Makefile b/Makefile index cd58b05..b9ac244 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ DEFINES += SPEC_VERSION=$(SPECVERSION) APP_SOURCE_PATH += src APP_SOURCE_FILES += $(BOLOS_SDK)/lib_cxng/src/cx_rsa.c APP_SOURCE_FILES += $(BOLOS_SDK)/lib_cxng/src/cx_pkcs1.c -APP_SOURCE_FILES += $(BOLOS_SDK)/lib_cxng/src/cx_utils.c +APP_SOURCE_FILES += ${BOLOS_SDK}/lib_cxng/src/cx_ram.c INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src @@ -120,15 +120,13 @@ endif ######################################## # These advanced settings allow to disable some feature that are by # default enabled in the SDK `Makefile.standard_app`. -DISABLE_STANDARD_APP_FILES = 1 +#DISABLE_STANDARD_APP_FILES = 1 #DISABLE_DEFAULT_IO_SEPROXY_BUFFER_SIZE = 1 # To allow custom size declaration #DISABLE_STANDARD_APP_DEFINES = 1 # Will set all the following disablers #DISABLE_STANDARD_SNPRINTF = 1 #DISABLE_STANDARD_USB = 1 DISABLE_STANDARD_WEBUSB = 1 -ifeq ($(TARGET_NAME),TARGET_NANOS) -DISABLE_STANDARD_BAGL_UX_FLOW = 1 -endif +#DISABLE_STANDARD_BAGL_UX_FLOW = 1 #DISABLE_DEBUG_LEDGER_ASSERT = 1 #DISABLE_DEBUG_THROW = 1 @@ -138,12 +136,12 @@ endif DEFINES += GPG_MULTISLOT=$(GPG_MULTISLOT) DEFINES += CUSTOM_IO_APDU_BUFFER_SIZE=\(255+5+64\) -DEFINES += HAVE_LEGACY_PID DEFINES += HAVE_USB_CLASS_CCID DEFINES += HAVE_RSA ifeq ($(TARGET_NAME),TARGET_NANOS) DEFINES += UI_NANO_S +DEFINES += HAVE_UX_LEGACY else DEFINES += UI_NANO_X DEFINES += GPG_SHAKE256 diff --git a/ledger_app.toml b/ledger_app.toml new file mode 100644 index 0000000..b223878 --- /dev/null +++ b/ledger_app.toml @@ -0,0 +1,8 @@ +[app] +build_directory = "./" +sdk = "C" +devices = ["nanos", "nanox", "nanos+"] + +[tests] +unit_directory = "./unit-tests/" +pytest_directory = "./tests/" diff --git a/src/gpg_main.c b/src/gpg_main.c index 8609b78..0399b16 100644 --- a/src/gpg_main.c +++ b/src/gpg_main.c @@ -14,16 +14,33 @@ */ #include "gpg_vars.h" -#include "gpg_ux_nanos.h" +#include "io.h" #include "usbd_ccid_if.h" /* ----------------------------------------------------------------------- */ /* --- Application Entry --- */ /* ----------------------------------------------------------------------- */ -void gpg_main(void) { - unsigned int io_flags; +void app_main(void) { + unsigned int io_flags = 0; io_flags = 0; + + // start communication with MCU + ui_CCID_reset(); + + // set up + io_init(); + + gpg_init(); + + // set up initial screen + ui_init(); + + // start the application + // the first exchange will: + // - display the initial screen + // - send the ATR + // - receive the first command for (;;) { volatile unsigned short sw = 0; BEGIN_TRY { @@ -52,129 +69,3 @@ void gpg_main(void) { END_TRY; } } - -unsigned char io_event(unsigned char channel) { - UNUSED(channel); - // nothing done with the event, throw an error on the transport layer if - // needed - // can't have more than one tag in the reply, not supported yet. - switch (G_io_seproxyhal_spi_buffer[0]) { - case SEPROXYHAL_TAG_FINGER_EVENT: - UX_FINGER_EVENT(G_io_seproxyhal_spi_buffer); - break; - // power off if long push, else pass to the application callback if any - case SEPROXYHAL_TAG_BUTTON_PUSH_EVENT: // for Nano S - UX_BUTTON_PUSH_EVENT(G_io_seproxyhal_spi_buffer); - break; - - // other events are propagated to the UX just in case - default: - UX_DEFAULT_EVENT(); - break; - - case SEPROXYHAL_TAG_DISPLAY_PROCESSED_EVENT: - UX_DISPLAYED_EVENT({}); - break; - case SEPROXYHAL_TAG_TICKER_EVENT: - UX_TICKER_EVENT(G_io_seproxyhal_spi_buffer, { - // only allow display when not locked of overlaid by an OS UX. - if (UX_ALLOWED) { - UX_REDISPLAY(); - } - }); - break; - } - - // close the event if not done previously (by a display or whatever) - if (!io_seproxyhal_spi_is_status_sent()) { - io_seproxyhal_general_status(); - } - // command has been processed, DO NOT reset the current APDU transport - return 1; -} - -unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) { - switch (channel & ~(IO_FLAGS)) { - case CHANNEL_KEYBOARD: - break; - - // multiplexed io exchange over a SPI channel and TLV encapsulated protocol - case CHANNEL_SPI: - if (tx_len) { - io_seproxyhal_spi_send(G_io_apdu_buffer, tx_len); - - if (channel & IO_RESET_AFTER_REPLIED) { - reset(); - } - return 0; // nothing received from the master so far (it's a tx - // transaction) - } else { - return io_seproxyhal_spi_recv(G_io_apdu_buffer, sizeof(G_io_apdu_buffer), 0); - } - - default: - THROW(INVALID_PARAMETER); - return 0; - } - return 0; -} - -void app_exit(void) { - BEGIN_TRY_L(exit) { - TRY_L(exit) { - os_sched_exit(-1); - } - FINALLY_L(exit) { - } - } - END_TRY_L(exit); -} - -/* -------------------------------------------------------------- */ - -__attribute__((section(".boot"))) int main(void) { - // exit critical section - __asm volatile("cpsie i"); - - // ensure exception will work as planned - os_boot(); - for (;;) { - UX_INIT(); - - BEGIN_TRY { - TRY { - // start communication with MCU - io_seproxyhal_init(); - - USB_power(1); -#if HAVE_USB_CLASS_CCID - io_usb_ccid_set_card_inserted(1); -#endif - - // set up - gpg_init(); - - // set up initial screen - ui_init(); - - // start the application - // the first exchange will: - // - display the initial screen - // - send the ATR - // - receive the first command - gpg_main(); - } - CATCH(EXCEPTION_IO_RESET) { - // reset IO and UX - continue; - } - CATCH_ALL { - break; - } - FINALLY { - } - } - END_TRY; - } - app_exit(); -} diff --git a/src/gpg_ram.c b/src/gpg_ram.c deleted file mode 100644 index e2d9484..0000000 --- a/src/gpg_ram.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright 2017 Cedric Mesnil , 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. - */ - -#include "gpg_vars.h" - -#ifndef TARGET_NANOS -#include "ux.h" -ux_state_t G_ux; -bolos_ux_params_t G_ux_params; -#else -ux_state_t ux; -#endif - -unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B]; - -gpg_v_state_t G_gpg_vstate; - -#ifdef HAVE_RSA -union cx_u G_cx; -#endif // HAVE_RSA diff --git a/src/gpg_nvram.c b/src/gpg_vars.c similarity index 91% rename from src/gpg_nvram.c rename to src/gpg_vars.c index b10dc62..1f976c4 100644 --- a/src/gpg_nvram.c +++ b/src/gpg_vars.c @@ -15,8 +15,7 @@ #include "gpg_vars.h" -#ifndef TARGET_NANOS +gpg_v_state_t G_gpg_vstate; + const gpg_nv_state_t N_state_pic; -#else -gpg_nv_state_t N_state_pic; -#endif + diff --git a/src/gpg_vars.h b/src/gpg_vars.h index 82c7ccb..0257ff1 100644 --- a/src/gpg_vars.h +++ b/src/gpg_vars.h @@ -33,18 +33,7 @@ extern const unsigned char C_OID_cv25519[10]; extern gpg_v_state_t G_gpg_vstate; -#ifndef TARGET_NANOS extern const gpg_nv_state_t N_state_pic; #define N_gpg_pstate ((volatile gpg_nv_state_t *) PIC(&N_state_pic)) -#else -extern gpg_nv_state_t N_state_pic; -#define N_gpg_pstate ((WIDE gpg_nv_state_t *) PIC(&N_state_pic)) -#endif - -extern ux_state_t ux; -#ifdef HAVE_RSA -#include "cx_ram.h" -extern union cx_u G_cx; -#endif // HAVE_RSA #endif