Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
upgrade to v2012 (#25)
Browse files Browse the repository at this point in the history
* upgrade to v2012

* restructure + upgrade zxlib

* update testcases

* disable allowlist code

* fix zemu tests
  • Loading branch information
jleni authored Jul 1, 2020
1 parent 23ef2c0 commit 4783355
Show file tree
Hide file tree
Showing 23 changed files with 2,160 additions and 2,129 deletions.
4 changes: 2 additions & 2 deletions app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
APPVERSION_M=1
APPVERSION_N=2011
APPVERSION_P=3
APPVERSION_N=2012
APPVERSION_P=0
28 changes: 28 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@
#include "coin.h"
#include "zxmacros.h"

void extractHDPath(uint32_t rx, uint32_t offset) {
if ((rx - offset) < sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
THROW(APDU_CODE_WRONG_LENGTH);
}

MEMCPY(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_DEFAULT;

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}

#if defined(APP_RESTRICTED)
if (hdPath[2] != HDPATH_2_STASH && hdPath[2] != HDPATH_2_VALIDATOR ) {
THROW(APDU_CODE_DATA_INVALID);
}
if (hdPath[3] != HDPATH_3_DEFAULT ) {
THROW(APDU_CODE_DATA_INVALID);
}
if (hdPath[4] < 0x80000000 ) {
THROW(APDU_CODE_DATA_INVALID);
}
#endif

}

__Z_INLINE void handle_getversion(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
#ifdef TESTING_ENABLED
G_io_apdu_buffer[0] = 0xFF;
Expand Down
28 changes: 0 additions & 28 deletions app/src/common/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,6 @@ unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) {
return 0;
}

void extractHDPath(uint32_t rx, uint32_t offset) {
if ((rx - offset) < sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
THROW(APDU_CODE_WRONG_LENGTH);
}

MEMCPY(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_DEFAULT;

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}

#if defined(APP_RESTRICTED)
if (hdPath[2] != HDPATH_2_STASH && hdPath[2] != HDPATH_2_VALIDATOR ) {
THROW(APDU_CODE_DATA_INVALID);
}
if (hdPath[3] != HDPATH_3_DEFAULT ) {
THROW(APDU_CODE_DATA_INVALID);
}
if (hdPath[4] < 0x80000000 ) {
THROW(APDU_CODE_DATA_INVALID);
}
#endif

}

bool process_chunk(volatile uint32_t *tx, uint32_t rx) {
const uint8_t payloadType = G_io_apdu_buffer[OFFSET_PAYLOAD_TYPE];

Expand Down
4 changes: 4 additions & 0 deletions app/src/common/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ void app_init();

void app_main();

void extractHDPath(uint32_t rx, uint32_t offset);

bool process_chunk(volatile uint32_t *tx, uint32_t rx);

void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx);
1 change: 1 addition & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef enum {
parser_unexepected_error,
// Coin specific
parser_spec_not_supported,
parser_not_allowed,
parser_not_supported,
parser_unexpected_buffer_end,
parser_unexpected_value,
Expand Down
25 changes: 24 additions & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ parser_error_t parser_parse(parser_context_t *ctx,
return _readTx(ctx, &parser_tx_obj);
}

#if defined(APP_RESTRICTED)
// FIXME: Re-enable
//parser_error_t parser_validate_vecLookupSource(pd_VecLookupSource_t *targets) {
// if (!allowlist_is_active()) {
// return parser_not_allowed;
// }
//
// parser_context_t ctx;
// // each look up source is 32 bytes
// pd_LookupSource_t lookupSource;
//
// parser_init(&ctx, targets->_ptr, targets->_lenBuffer);
// for (uint16_t i = 0; i < targets->_len; i++) {
// CHECK_ERROR(_readLookupSource(&ctx, &lookupSource));
// allowlist_validate(lookupSource->_ptr);
// }
//
// return parser_ok;
//}
#endif

parser_error_t parser_validate(const parser_context_t *ctx) {
#if defined(APP_RESTRICTED)
if (hdPath[2] == HDPATH_2_STASH) {
Expand All @@ -52,7 +73,9 @@ parser_error_t parser_validate(const parser_context_t *ctx) {
return parser_ok;
}
if (parser_tx_obj.callIndex.idx==PD_CALL_STAKING_NOMINATE) {
// FIXME: Check allowlist
// FIXME: Reenable
// pd_VecLookupSource_t *targets = parser_tx_obj.method.basic.staking_nominate.targets;
// CHECK_PARSER_ERR(parser_validate_vecLookupSource(targets))
return parser_ok;
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ const char *parser_getErrorDescription(parser_error_t err) {
case parser_display_page_out_of_range:
return "display_page_out_of_range";

// Coin specific
// Coin specific
case parser_spec_not_supported:
return "Spec version not supported";
case parser_not_allowed:
return "Not allowed";
case parser_not_supported:
return "Not supported";
case parser_unexpected_buffer_end:
Expand Down
4 changes: 2 additions & 2 deletions app/src/substrate_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ __Z_INLINE parser_error_t _readMethod_finalitytracker_final_hint(

__Z_INLINE parser_error_t _readMethod_grandpa_report_equivocation(
parser_context_t *c, pd_grandpa_report_equivocation_t *m) {
CHECK_ERROR(_readEquivocationProof(c, &m->equivocation_proof))
CHECK_ERROR(_readGrandpaEquivocationProof(c, &m->equivocation_proof))
CHECK_ERROR(_readKeyOwnerProof(c, &m->key_owner_proof))
return parser_ok;
}
Expand Down Expand Up @@ -4387,7 +4387,7 @@ parser_error_t _getMethod_ItemValue(
case 2560: /* module 10 call 0 */
switch(itemIdx) {
case 0: /* grandpa_report_equivocation - equivocation_proof */;
return _toStringEquivocationProof(
return _toStringGrandpaEquivocationProof(
&m->basic.grandpa_report_equivocation.equivocation_proof,
outValue, outValueLen,
pageIdx, pageCount);
Expand Down
2 changes: 1 addition & 1 deletion app/src/substrate_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ typedef struct {

#define PD_CALL_GRANDPA_REPORT_EQUIVOCATION 0
typedef struct {
pd_EquivocationProof_t equivocation_proof;
pd_GrandpaEquivocationProof_t equivocation_proof;
pd_KeyOwnerProof_t key_owner_proof;
} pd_grandpa_report_equivocation_t;

Expand Down
30 changes: 15 additions & 15 deletions app/src/substrate_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ parser_error_t _readElectionSize(parser_context_t *c, pd_ElectionSize_t *v) {
return parser_not_supported;
}

parser_error_t _readEquivocationProof(parser_context_t *c, pd_EquivocationProof_t *v) {
return parser_not_supported;
}

parser_error_t _readEraIndex(parser_context_t *c, pd_EraIndex_t *v) {
return _readUInt32(c, &v->value);
}
Expand All @@ -226,6 +222,10 @@ parser_error_t _readEthereumAddress(parser_context_t *c, pd_EthereumAddress_t *v
GEN_DEF_READARRAY(20)
}

parser_error_t _readGrandpaEquivocationProof(parser_context_t *c, pd_GrandpaEquivocationProof_t *v) {
return parser_not_supported;
}

parser_error_t _readHeadData(parser_context_t *c, pd_HeadData_t *v) {
return parser_not_supported;
}
Expand Down Expand Up @@ -988,17 +988,6 @@ parser_error_t _toStringElectionSize(
return parser_print_not_supported;
}

parser_error_t _toStringEquivocationProof(
const pd_EquivocationProof_t *v,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount) {
CLEAN_AND_CHECK()

return parser_print_not_supported;
}

parser_error_t _toStringEraIndex(
const pd_EraIndex_t *v,
char *outValue,
Expand All @@ -1017,6 +1006,17 @@ parser_error_t _toStringEthereumAddress(
GEN_DEF_TOSTRING_ARRAY(20)
}

parser_error_t _toStringGrandpaEquivocationProof(
const pd_GrandpaEquivocationProof_t *v,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount) {
CLEAN_AND_CHECK()

return parser_print_not_supported;
}

parser_error_t _toStringHeadData(
const pd_HeadData_t *v,
char *outValue,
Expand Down
24 changes: 12 additions & 12 deletions app/src/substrate_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,6 @@ typedef struct {
uint8_t _NOT_IMPLEMENTED__DO_NOT_USE;
} pd_ElectionSize_t;

typedef struct {
// TODO: Not implemented
uint8_t _NOT_IMPLEMENTED__DO_NOT_USE;
} pd_EquivocationProof_t;

typedef struct {
uint32_t value;
} pd_EraIndex_t;
Expand All @@ -277,6 +272,11 @@ typedef struct {
const uint8_t *_ptr;
} pd_EthereumAddress_t;

typedef struct {
// TODO: Not implemented
uint8_t _NOT_IMPLEMENTED__DO_NOT_USE;
} pd_GrandpaEquivocationProof_t;

typedef struct {
// TODO: Not implemented
uint8_t _NOT_IMPLEMENTED__DO_NOT_USE;
Expand Down Expand Up @@ -576,9 +576,9 @@ parser_error_t _readDoubleVoteReport(parser_context_t *c, pd_DoubleVoteReport_t
parser_error_t _readEcdsaSignature(parser_context_t *c, pd_EcdsaSignature_t *v);
parser_error_t _readElectionScore(parser_context_t *c, pd_ElectionScore_t *v);
parser_error_t _readElectionSize(parser_context_t *c, pd_ElectionSize_t *v);
parser_error_t _readEquivocationProof(parser_context_t *c, pd_EquivocationProof_t *v);
parser_error_t _readEraIndex(parser_context_t *c, pd_EraIndex_t *v);
parser_error_t _readEthereumAddress(parser_context_t *c, pd_EthereumAddress_t *v);
parser_error_t _readGrandpaEquivocationProof(parser_context_t *c, pd_GrandpaEquivocationProof_t *v);
parser_error_t _readHeadData(parser_context_t *c, pd_HeadData_t *v);
parser_error_t _readHeader(parser_context_t *c, pd_Header_t *v);
parser_error_t _readHeartbeat(parser_context_t *c, pd_Heartbeat_t *v);
Expand Down Expand Up @@ -895,22 +895,22 @@ parser_error_t _toStringElectionSize(
uint8_t pageIdx,
uint8_t *pageCount);

parser_error_t _toStringEquivocationProof(
const pd_EquivocationProof_t *v,
parser_error_t _toStringEraIndex(
const pd_EraIndex_t *v,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount);

parser_error_t _toStringEraIndex(
const pd_EraIndex_t *v,
parser_error_t _toStringEthereumAddress(
const pd_EthereumAddress_t *v,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount);

parser_error_t _toStringEthereumAddress(
const pd_EthereumAddress_t *v,
parser_error_t _toStringGrandpaEquivocationProof(
const pd_GrandpaEquivocationProof_t *v,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
Expand Down
30 changes: 15 additions & 15 deletions deps/ledger-zxlib/app/common/app_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@
#include "app_mode.h"

typedef struct {
uint32_t expert;
uint8_t expert;
} app_mode_t;

#if defined(TARGET_NANOS)
app_mode_t N_appmode_impl __attribute__ ((aligned(64)));
#define N_appmode (*(app_mode_t *)PIC(&N_appmode_impl))

#elif defined(TARGET_NANOX)
app_mode_t const N_appmode_impl __attribute__ ((aligned(64)));
#define N_appmode (*(volatile app_mode_t *)PIC(&N_appmode_impl))
#endif


#if defined(TARGET_NANOS) || defined(TARGET_NANOX)
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

//NV_CONST app_mode_t N_appmode NV_ALIGN;
//#define N_APPMODE_PTR ((NV_VOL app_mode_t *)PIC(&N_appmode))
app_mode_t app_mode;

void app_mode_reset(){
app_mode.expert = 0;
}

bool app_mode_expert() {
// TODO: read from NVRAM
// app_mode_t *p = N_APPMODE_PTR;
// uint8_t expert = p->expert;
// return expert;
// app_mode_t* p =(NV_VOL app_mode_t *)PIC(&N_appmode_impl);
// return p->expert;
return app_mode.expert;
return N_appmode.expert;
}

void app_mode_set_expert(uint8_t val) {
// TODO: write to NVRAM
app_mode.expert = val;
app_mode_t mode;
mode.expert = val;
MEMCPY_NV( (void*) PIC(&N_appmode_impl), (void*) &mode, sizeof(app_mode_t));
}

#else
Expand Down
8 changes: 4 additions & 4 deletions deps/ledger-zxlib/app/common/zbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ zbuffer_error_e zb_init() {

zbuffer_error_e zb_allocate(uint16_t size) {
if (size % 4 != 0) {
return zb_misaligned_buffer;
size += size % 4;
}
_internal.size = size;
_internal.ptr = (uint8_t *) (&app_stack_canary + 4);
_internal.ptr = (uint8_t * )(&app_stack_canary + 4);

uint32_t *zb_canary = (uint32_t *) (_internal.ptr + _internal.size + 4);
uint32_t *zb_canary = (uint32_t * )(_internal.ptr + _internal.size + 4);
*zb_canary = CANARY_EXPECTED;

return zb_no_error;
Expand All @@ -70,7 +70,7 @@ zbuffer_error_e zb_check_canary() {
CHECK_APP_CANARY();
if (_internal.size != 0) {
// allocated
uint32_t *zb_canary = (uint32_t *) (_internal.ptr + _internal.size + 4);
uint32_t *zb_canary = (uint32_t * )(_internal.ptr + _internal.size + 4);
if (*zb_canary != CANARY_EXPECTED) {
handle_stack_overflow();
}
Expand Down
5 changes: 5 additions & 0 deletions deps/ledger-zxlib/dockerized_build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ pull:
build_rust:
$(call run_docker,$(DOCKER_BOLOS_SDK),make -C $(DOCKER_APP_SRC) rust)

.PHONY: convert_icon
convert_icon:
@convert $(LEDGER_SRC)/tmp.gif -monochrome -size 16x16 -depth 1 $(LEDGER_SRC)/nanos_icon.gif
@convert $(LEDGER_SRC)/nanos_icon.gif -crop 14x14+1+1 +repage -negate $(LEDGER_SRC)/nanox_icon.gif

.PHONY: build
build:
$(info Replacing app icon)
Expand Down
Loading

0 comments on commit 4783355

Please sign in to comment.