Skip to content

Commit d384a37

Browse files
Merge pull request #421 from LedgerHQ/standard_make
Migration to standard makefile, main, io and swap [step 2]
2 parents d30a667 + 7504866 commit d384a37

20 files changed

+195
-471
lines changed

Makefile

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ ifeq ($(BOLOS_SDK),)
1919
$(error Environment variable BOLOS_SDK is not set)
2020
endif
2121

22-
# Application allowed derivation curves.
23-
CURVE_APP_LOAD_PARAMS = secp256k1
22+
include $(BOLOS_SDK)/Makefile.target
2423

25-
# Allowed SLIP21 paths
26-
PATH_SLIP21_APP_LOAD_PARAMS = "LEDGER-Wallet policy"
24+
########################################
25+
# Mandatory configuration #
26+
########################################
2727

2828
# Application version
2929
APPVERSION_M = 2
@@ -37,12 +37,22 @@ else
3737
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)-$(strip $(APPVERSION_SUFFIX))"
3838
endif
3939

40-
# If set, the app will automatically approve all requests without user interaction. Useful for performance tests.
41-
# It is critical that no such app is ever deployed in production.
42-
AUTOAPPROVE_FOR_PERF_TESTS ?= 0
43-
ifneq ($(AUTOAPPROVE_FOR_PERF_TESTS),0)
44-
DEFINES += HAVE_AUTOAPPROVE_FOR_PERF_TESTS
45-
endif
40+
# Application source files
41+
APP_SOURCE_PATH += src
42+
43+
# Application icons following guidelines:
44+
# https://developers.ledger.com/docs/embedded-app/design-requirements/#device-icon
45+
ICON_NANOX = icons/nanox_app_bitcoin.gif
46+
ICON_NANOSP = icons/nanox_app_bitcoin.gif
47+
ICON_STAX = icons/stax_app_bitcoin.gif
48+
ICON_FLEX = icons/flex_app_bitcoin.gif
49+
ICON_APEX_P = icons/apex_p_app_bitcoin.png
50+
51+
# Application allowed derivation curves.
52+
CURVE_APP_LOAD_PARAMS = secp256k1
53+
54+
# Allowed SLIP21 paths
55+
PATH_SLIP21_APP_LOAD_PARAMS = "LEDGER-Wallet policy"
4656

4757
# Setting to allow building variant applications
4858
VARIANT_PARAM = COIN
@@ -53,58 +63,32 @@ ifndef COIN
5363
COIN=bitcoin_testnet
5464
endif
5565

56-
########################################
57-
# Application custom permissions #
58-
########################################
59-
HAVE_APPLICATION_FLAG_GLOBAL_PIN = 1
60-
HAVE_APPLICATION_FLAG_BOLOS_SETTINGS = 1
61-
HAVE_APPLICATION_FLAG_LIBRARY = 1
62-
66+
# Coin-specific configuration
6367
ifeq ($(COIN),bitcoin_testnet)
64-
# Application allowed derivation paths (testnet) + exception for Electrum + BIP-45 whole tree
65-
PATH_APP_LOAD_PARAMS = "*/1'" "4541509'" "45'"
66-
6768
# Bitcoin testnet, no legacy support
6869
DEFINES += BIP32_PUBKEY_VERSION=0x043587CF
6970
DEFINES += BIP44_COIN_TYPE=1
7071
DEFINES += COIN_P2PKH_VERSION=111
7172
DEFINES += COIN_P2SH_VERSION=196
7273
DEFINES += COIN_NATIVE_SEGWIT_PREFIX=\"tb\"
7374
DEFINES += COIN_COINID_SHORT=\"TEST\"
74-
7575
APPNAME = "Bitcoin Test"
76+
# Application allowed derivation paths (testnet) + exception for Electrum + BIP-45 whole tree
77+
PATH_APP_LOAD_PARAMS = "*/1'" "4541509'" "45'"
7678

7779
else ifeq ($(COIN),bitcoin)
78-
# Application allowed derivation paths (mainnet) + exception for Electrum + BIP-45 whole tree
79-
PATH_APP_LOAD_PARAMS = "*/0'" "4541509'" "45'"
80-
81-
# the version for performance tests automatically approves all requests
82-
# there is no reason to ever compile the mainnet app with this flag
83-
ifneq ($(AUTOAPPROVE_FOR_PERF_TESTS),0)
84-
$(error Use testnet app for performance tests)
85-
endif
86-
8780
# Bitcoin mainnet, no legacy support
8881
DEFINES += BIP32_PUBKEY_VERSION=0x0488B21E
8982
DEFINES += BIP44_COIN_TYPE=0
9083
DEFINES += COIN_P2PKH_VERSION=0
9184
DEFINES += COIN_P2SH_VERSION=5
9285
DEFINES += COIN_NATIVE_SEGWIT_PREFIX=\"bc\"
9386
DEFINES += COIN_COINID_SHORT=\"BTC\"
94-
9587
APPNAME = "Bitcoin"
88+
# Application allowed derivation paths (mainnet) + exception for Electrum + BIP-45 whole tree
89+
PATH_APP_LOAD_PARAMS = "*/0'" "4541509'" "45'"
9690

9791
else ifeq ($(COIN),bitcoin_recovery)
98-
# Application allowed derivation paths (all paths are permitted).
99-
PATH_APP_LOAD_PARAMS = ""
100-
HAVE_APPLICATION_FLAG_DERIVE_MASTER = 1
101-
102-
# the version for performance tests automatically approves all requests
103-
# there is no reason to ever compile the mainnet app with this flag
104-
ifneq ($(AUTOAPPROVE_FOR_PERF_TESTS),0)
105-
$(error Use testnet app for performance tests)
106-
endif
107-
10892
# Bitcoin mainnet, no legacy support
10993
DEFINES += BIP32_PUBKEY_VERSION=0x0488B21E
11094
DEFINES += BIP44_COIN_TYPE=0
@@ -113,8 +97,10 @@ else ifeq ($(COIN),bitcoin_recovery)
11397
DEFINES += COIN_NATIVE_SEGWIT_PREFIX=\"bc\"
11498
DEFINES += COIN_COINID_SHORT=\"BTC\"
11599
DEFINES += BITCOIN_RECOVERY
116-
117100
APPNAME = "Bitcoin Recovery"
101+
# Application allowed derivation paths (all paths are permitted).
102+
PATH_APP_LOAD_PARAMS = ""
103+
HAVE_APPLICATION_FLAG_DERIVE_MASTER = 1
118104

119105
else
120106
ifeq ($(filter clean,$(MAKECMDGOALS)),)
@@ -128,40 +114,64 @@ ifneq (,$(filter-out clean,$(MAKECMDGOALS)))
128114
endif
129115
endif
130116

131-
ENABLE_NBGL_FOR_NANO_DEVICES = 1
132-
133-
# Application icons following guidelines:
134-
# https://developers.ledger.com/docs/embedded-app/design-requirements/#device-icon
135-
ICON_NANOX = icons/nanox_app_bitcoin.gif
136-
ICON_NANOSP = icons/nanox_app_bitcoin.gif
137-
ICON_STAX = icons/stax_app_bitcoin.gif
138-
ICON_FLEX = icons/flex_app_bitcoin.gif
139-
ICON_APEX_P = icons/apex_p_app_bitcoin.png
117+
########################################
118+
# Application custom permissions #
119+
########################################
120+
# See SDK `include/appflags.h` for the purpose of each permission
121+
HAVE_APPLICATION_FLAG_GLOBAL_PIN = 1
122+
HAVE_APPLICATION_FLAG_BOLOS_SETTINGS = 1
123+
HAVE_APPLICATION_FLAG_LIBRARY = 1
140124

141125
########################################
142126
# Application communication interfaces #
143127
########################################
144128
ENABLE_BLUETOOTH = 1
129+
ENABLE_NBGL_FOR_NANO_DEVICES = 1
145130

146131
########################################
147132
# NBGL custom features #
148133
########################################
149134
ENABLE_NBGL_QRCODE = 1
150135

136+
########################################
137+
# SWAP FEATURE FLAG #
138+
# This flag enables the swap feature #
139+
# in the Boilerplate application. #
140+
########################################
141+
# Testing only SWAP flag
142+
# ENABLE_TESTING_SWAP = 1
143+
# Production enabled SWAP flag
144+
ENABLE_SWAP = 1
145+
151146
########################################
152147
# Features disablers #
153148
########################################
154149
# Don't use standard app file to avoid conflicts for now
155-
DISABLE_STANDARD_APP_FILES = 1
150+
#DISABLE_STANDARD_APP_FILES = 1
156151

157152
# Don't use default IO_SEPROXY_BUFFER_SIZE to use another
158153
# value for NANOS for an unknown reason.
159-
DISABLE_DEFAULT_IO_SEPROXY_BUFFER_SIZE = 1
154+
#DISABLE_DEFAULT_IO_SEPROXY_BUFFER_SIZE = 1
160155

156+
########################################
157+
# Application defines #
158+
########################################
161159
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
162160

163-
164-
DEFINES += IO_SEPROXYHAL_BUFFER_SIZE_B=300
161+
# If set, the app will automatically approve all requests without user interaction. Useful for performance tests.
162+
# It is critical that no such app is ever deployed in production.
163+
AUTOAPPROVE_FOR_PERF_TESTS ?= 0
164+
ifneq ($(AUTOAPPROVE_FOR_PERF_TESTS),0)
165+
DEFINES += HAVE_AUTOAPPROVE_FOR_PERF_TESTS
166+
# the version for performance tests automatically approves all requests
167+
# there is no reason to ever compile the mainnet app with this flag
168+
ifeq ($(COIN),bitcoin)
169+
$(error Use testnet app for performance tests)
170+
endif
171+
ifeq ($(COIN),bitcoin_recovery)
172+
$(error Use testnet app for performance tests)
173+
endif
174+
endif
165175

166176
# debugging helper functions and macros
167177
CFLAGS += -include debug-helpers/debug.h
@@ -175,21 +185,6 @@ endif
175185

176186
# Needed to be able to include the definition of G_cx
177187
INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src
178-
INCLUDES_PATH += $(BOLOS_SDK)/lib_standard_app
179-
180-
# Application source files
181-
APP_SOURCE_PATH += src
182-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/base58.c
183-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/bip32.c
184-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/buffer.c
185-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/format.c
186-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/parser.c
187-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/read.c
188-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/varint.c
189-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/write.c
190-
191-
# Allow usage of function from lib_standard_app/crypto_helpers.c
192-
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c
193188

194189
########################################
195190
# Features enablers #

src/boilerplate/dispatcher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
/* SDK headers */
2424
#include "buffer.h"
25+
#include "io.h"
2526

2627
/* Local headers */
2728
#include "constants.h"
28-
#include "globals.h"
2929
#include "io_ext.h"
3030
#include "sw.h"
3131

@@ -52,7 +52,7 @@ static void finalize_response(uint16_t sw) {
5252
}
5353

5454
static void send_response() {
55-
io_confirm_response();
55+
io_send_response();
5656
}
5757

5858
static void set_ui_dirty() {

src/boilerplate/io_ext.c

Lines changed: 21 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
#include "nbgl_touch.h"
2626
#include "nbgl_use_case.h"
2727
#include "os.h"
28+
#include "swap.h"
2829
#include "ux.h"
2930
#include "write.h"
3031

3132
/* Local headers */
3233
#include "dispatcher.h"
3334
#include "display.h"
34-
#include "globals.h"
3535
#include "sw.h"
3636
#include "swap_globals.h"
3737

@@ -80,89 +80,32 @@ void io_reset_timeouts() {
8080
void io_show_processing_screen() {
8181
if (!G_was_processing_screen_shown) {
8282
G_was_processing_screen_shown = true;
83-
if (!G_swap_state.called_from_swap) {
83+
if (!G_called_from_swap) {
8484
nbgl_useCaseSpinner(ui_get_processing_screen_text());
8585
}
8686
}
8787
}
8888

89-
uint8_t io_event(uint8_t channel) {
90-
UNUSED(channel);
91-
92-
switch (G_io_seproxyhal_spi_buffer[0]) {
93-
case SEPROXYHAL_TAG_BUTTON_PUSH_EVENT:
94-
UX_BUTTON_PUSH_EVENT(G_io_seproxyhal_spi_buffer);
95-
break;
96-
case SEPROXYHAL_TAG_STATUS_EVENT:
97-
if (G_io_apdu_media == IO_APDU_MEDIA_USB_HID && //
98-
!(U4BE(G_io_seproxyhal_spi_buffer, 3) & //
99-
SEPROXYHAL_TAG_STATUS_EVENT_FLAG_USB_POWERED)) {
100-
THROW(EXCEPTION_IO_RESET);
101-
}
102-
__attribute__((fallthrough));
103-
case SEPROXYHAL_TAG_DISPLAY_PROCESSED_EVENT:
104-
UX_DEFAULT_EVENT();
105-
break;
106-
#ifdef SCREEN_SIZE_WALLET
107-
case SEPROXYHAL_TAG_FINGER_EVENT:
108-
UX_FINGER_EVENT(G_io_seproxyhal_spi_buffer);
109-
break;
110-
#endif // SCREEN_SIZE_WALLET
111-
case SEPROXYHAL_TAG_TICKER_EVENT:
112-
++G_ticks;
113-
114-
if (G_is_timeout_active.processing &&
115-
G_ticks - G_processing_timeout_start_tick >= PROCESSING_TIMEOUT_TICKS) {
116-
io_clear_processing_timeout();
117-
118-
io_show_processing_screen();
119-
}
120-
121-
if (G_is_timeout_active.interruption &&
122-
G_ticks - G_interruption_timeout_start_tick >= INTERRUPTION_TIMEOUT_TICKS) {
123-
io_clear_interruption_timeout();
124-
125-
// TODO: It would be better to have the dispatcher be notified somehow.
126-
// This would require some tampering with the io_exchange in
127-
// process_interruption.
128-
THROW(EXCEPTION_IO_RESET);
129-
}
130-
131-
UX_TICKER_EVENT(G_io_seproxyhal_spi_buffer, {});
132-
break;
133-
default:
134-
UX_DEFAULT_EVENT();
135-
break;
136-
}
89+
// This function can be used to declare a callback to SEPROXYHAL_TAG_TICKER_EVENT in the application
90+
void app_ticker_event_callback(void) {
91+
++G_ticks;
92+
93+
if (G_is_timeout_active.processing &&
94+
(uint16_t) (G_ticks - G_processing_timeout_start_tick) >= PROCESSING_TIMEOUT_TICKS) {
95+
io_clear_processing_timeout();
13796

138-
if (!io_seproxyhal_spi_is_status_sent()) {
139-
io_seproxyhal_general_status();
97+
io_show_processing_screen();
14098
}
14199

142-
return 1;
143-
}
100+
if (G_is_timeout_active.interruption &&
101+
(uint16_t) (G_ticks - G_interruption_timeout_start_tick) >= INTERRUPTION_TIMEOUT_TICKS) {
102+
io_clear_interruption_timeout();
144103

145-
uint16_t io_exchange_al(uint8_t channel, uint16_t tx_len) {
146-
switch (channel & ~(IO_FLAGS)) {
147-
case CHANNEL_KEYBOARD:
148-
break;
149-
case CHANNEL_SPI:
150-
if (tx_len) {
151-
io_seproxyhal_spi_send(G_io_apdu_buffer, tx_len);
152-
153-
if (channel & IO_RESET_AFTER_REPLIED) {
154-
halt();
155-
}
156-
157-
return 0;
158-
} else {
159-
return io_seproxyhal_spi_recv(G_io_apdu_buffer, sizeof(G_io_apdu_buffer), 0);
160-
}
161-
default:
162-
THROW(INVALID_PARAMETER);
104+
// TODO: It would be better to have the dispatcher be notified somehow.
105+
// This would require some tampering with the io_exchange in
106+
// process_interruption.
107+
THROW(EXCEPTION_IO_RESET);
163108
}
164-
165-
return 0;
166109
}
167110

168111
void io_add_to_response(const void *rdata, size_t rdata_len) {
@@ -188,19 +131,7 @@ void io_finalize_response(uint16_t sw) {
188131
}
189132
}
190133

191-
void io_reset_response() {
192-
G_output_len = 0;
193-
}
194-
195-
void io_set_response(const void *rdata, size_t rdata_len, uint16_t sw) {
196-
io_reset_response();
197-
if (rdata != NULL) {
198-
io_add_to_response(rdata, rdata_len);
199-
}
200-
io_finalize_response(sw);
201-
}
202-
203-
int io_confirm_response() {
134+
int io_send_response() {
204135
int ret;
205136

206137
ret = io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, G_output_len);
@@ -209,11 +140,8 @@ int io_confirm_response() {
209140
return ret;
210141
}
211142

212-
int io_send_response(void *rdata, size_t rdata_len, uint16_t sw) {
213-
io_set_response(rdata, rdata_len, sw);
214-
return io_confirm_response();
215-
}
216-
217143
int io_send_sw(uint16_t sw) {
218-
return io_send_response(NULL, 0, sw);
144+
G_output_len = 0;
145+
io_finalize_response(sw);
146+
return io_send_response();
219147
}

0 commit comments

Comments
 (0)