Skip to content
This repository has been archived by the owner on Sep 25, 2022. It is now read-only.

Commit

Permalink
refactor: refactored app settings to reduce storage usage (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Jul 15, 2022
1 parent b4ffbad commit 1395998
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 62 deletions.
35 changes: 0 additions & 35 deletions src/app_mode.c

This file was deleted.

18 changes: 0 additions & 18 deletions src/app_mode.h

This file was deleted.

3 changes: 3 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/handler/get_app_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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},
Expand Down
4 changes: 2 additions & 2 deletions src/handler/sign_transaction_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 7 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdint.h>

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
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/ui/ui_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

0 comments on commit 1395998

Please sign in to comment.