From 1395998d7b076d5a81f62bbe80e656b789fee4ba Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Fri, 15 Jul 2022 16:16:06 +0800 Subject: [PATCH] refactor: refactored app settings to reduce storage usage (#12) --- src/app_mode.c | 35 ----------------------------- src/app_mode.h | 18 --------------- src/globals.c | 3 +++ src/globals.h | 6 ++++- src/handler/get_app_configuration.c | 4 ++-- src/handler/sign_transaction_hash.c | 4 ++-- src/main.c | 7 ++++++ src/settings.h | 25 +++++++++++++++++++++ src/types.h | 2 +- src/ui/ui_menu.c | 6 ++--- 10 files changed, 48 insertions(+), 62 deletions(-) delete mode 100644 src/app_mode.c delete mode 100644 src/app_mode.h create mode 100644 src/settings.h diff --git a/src/app_mode.c b/src/app_mode.c deleted file mode 100644 index c0b53ea..0000000 --- a/src/app_mode.c +++ /dev/null @@ -1,35 +0,0 @@ -/***************************************************************************** - * Ledger Stellar App. - * (c) 2022 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 "app_mode.h" - -typedef struct { - uint8_t hash_signing_enabled; -} app_mode_persistent_t; - -app_mode_persistent_t const N_appmode_impl __attribute__((aligned(64))); -#define N_appmode (*(volatile app_mode_persistent_t *) PIC(&N_appmode_impl)) - -bool app_mode_hash_signing_enabled() { - return N_appmode.hash_signing_enabled; -} - -void app_mode_change_hash_signing_setting() { - app_mode_persistent_t mode; - mode.hash_signing_enabled = !app_mode_hash_signing_enabled(); - nvm_write((void *) PIC(&N_appmode_impl), (void *) &mode, sizeof(app_mode_persistent_t)); -} diff --git a/src/app_mode.h b/src/app_mode.h deleted file mode 100644 index 21d73cd..0000000 --- a/src/app_mode.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include -#include -#include "os.h" -#include "ux.h" - -/** - * Query whether hash signing model is enabled. - * - * @return true if enables, false otherwise. - */ -bool app_mode_hash_signing_enabled(); - -/** - * If the hash signing model is enabled, disable it; otherwise, enable it. - */ -void app_mode_change_hash_signing_setting(); \ No newline at end of file diff --git a/src/globals.c b/src/globals.c index 5e99f88..818dc0b 100644 --- a/src/globals.c +++ b/src/globals.c @@ -1,5 +1,8 @@ #include "globals.h" +// The settings, stored in NVRAM. +const internal_storage_t N_storage_real; + uint8_t G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B]; ux_state_t G_ux; bolos_ux_params_t G_ux_params; diff --git a/src/globals.h b/src/globals.h index d5a4966..f70f6df 100644 --- a/src/globals.h +++ b/src/globals.h @@ -4,8 +4,12 @@ #include "ux.h" #include "types.h" - #include "io.h" +#include "settings.h" +/** + * The settings, stored in NVRAM. Initializer is ignored by ledger. + */ +extern const internal_storage_t N_storage_real; /** * Global buffer for interactions between SE and MCU. diff --git a/src/handler/get_app_configuration.c b/src/handler/get_app_configuration.c index 40a89b5..efb06c3 100644 --- a/src/handler/get_app_configuration.c +++ b/src/handler/get_app_configuration.c @@ -28,7 +28,7 @@ #include "../sw.h" #include "../types.h" #include "common/buffer.h" -#include "../app_mode.h" +#include "../settings.h" int handler_get_app_configuration() { PRINTF("handler_get_app_configuration invoked\n"); @@ -44,7 +44,7 @@ int handler_get_app_configuration() { return io_send_response( &(const buffer_t){.ptr = (uint8_t[APP_CONFIGURATION_SIZE + APP_VERSION_SIZE]){ - (uint8_t) app_mode_hash_signing_enabled(), + (uint8_t) HAS_SETTING(S_HASH_SIGNING_ENABLED), (uint8_t) MAJOR_VERSION, (uint8_t) MINOR_VERSION, (uint8_t) PATCH_VERSION}, diff --git a/src/handler/sign_transaction_hash.c b/src/handler/sign_transaction_hash.c index 1123c50..3d51efe 100644 --- a/src/handler/sign_transaction_hash.c +++ b/src/handler/sign_transaction_hash.c @@ -17,14 +17,14 @@ #include "handler.h" #include "../globals.h" -#include "../app_mode.h" +#include "../settings.h" #include "../sw.h" #include "../crypto.h" #include "../ui/ui.h" int handler_sign_tx_hash(buffer_t *cdata) { PRINTF("handler_sign_tx_hash invoked\n"); - if (!app_mode_hash_signing_enabled()) { + if (!HAS_SETTING(S_HASH_SIGNING_ENABLED)) { return io_send_sw(SW_TX_HASH_SIGNING_MODE_NOT_ENABLED); } explicit_bzero(&G_context, sizeof(G_context)); diff --git a/src/main.c b/src/main.c index e45ef53..730a556 100644 --- a/src/main.c +++ b/src/main.c @@ -110,7 +110,14 @@ void standalone_app_main() { TRY { io_seproxyhal_init(); + if (!HAS_SETTING(S_INITIALIZED)) { + internal_storage_t storage = 0x00; + storage |= 0x80; + nvm_write((void *) &N_settings, (void *) &storage, sizeof(internal_storage_t)); + } + #ifdef TARGET_NANOX + // grab the current plane mode setting G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0); #endif // TARGET_NANOX diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..cc2d02e --- /dev/null +++ b/src/settings.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +typedef uint8_t internal_storage_t; + +#define N_settings (*(volatile internal_storage_t *) PIC(&N_storage_real)) + +// flip a bit k = 0 to 7 for u8 +#define _FLIP_BIT(n, k) (((n) ^ (1 << (k)))) + +// toggle a setting item +#define SETTING_TOGGLE(_set) \ + do { \ + internal_storage_t _temp_settings = _FLIP_BIT(N_settings, _set); \ + nvm_write((void *) &N_settings, (void *) &_temp_settings, sizeof(internal_storage_t)); \ + } while (0) + +// check a setting item +#define HAS_SETTING(k) ((N_settings & (1 << (k))) >> (k)) + +#define S_HASH_SIGNING_ENABLED 0 +#define S_SEQUENCE_DISPLAY_ENABLED 1 + +#define S_INITIALIZED 7 \ No newline at end of file diff --git a/src/types.h b/src/types.h index 048d492..c8fbf27 100644 --- a/src/types.h +++ b/src/types.h @@ -17,7 +17,7 @@ #define APP_VERSION_SIZE 3 /** - * Length of app_mode_persistent_t.hash_signing_enabled + * Length of hash_signing_enabled */ #define APP_CONFIGURATION_SIZE 1 diff --git a/src/ui/ui_menu.c b/src/ui/ui_menu.c index eb84797..a1bdd17 100644 --- a/src/ui/ui_menu.c +++ b/src/ui/ui_menu.c @@ -17,7 +17,7 @@ #include "ui.h" #include "globals.h" -#include "app_mode.h" +#include "settings.h" void ui_idle(void); void display_settings(const ux_flow_step_t* const start_step); @@ -88,12 +88,12 @@ void ui_idle(void) { void display_settings(const ux_flow_step_t* const start_step) { strlcpy(G_ui_detail_value, - (app_mode_hash_signing_enabled() ? "Enabled" : "NOT Enabled"), + (HAS_SETTING(S_HASH_SIGNING_ENABLED) ? "Enabled" : "NOT Enabled"), DETAIL_VALUE_MAX_LENGTH); ux_flow_init(0, ux_settings_flow, start_step); } void switch_settings_hash_signing() { - app_mode_change_hash_signing_setting(); + SETTING_TOGGLE(S_HASH_SIGNING_ENABLED); display_settings(&ux_settings_hash_signing_step); } \ No newline at end of file