diff --git a/firmware/messages.c b/firmware/messages.c index 9e761a326a..cf80ac7921 100644 --- a/firmware/messages.c +++ b/firmware/messages.c @@ -38,79 +38,7 @@ struct MessagesMap_t { }; static const struct MessagesMap_t MessagesMap[] = { - // in messages - {'n', 'i', MessageType_MessageType_Initialize, Initialize_fields, (void (*)(void *))fsm_msgInitialize}, - {'n', 'i', MessageType_MessageType_GetFeatures, GetFeatures_fields, (void (*)(void *))fsm_msgGetFeatures}, - {'n', 'i', MessageType_MessageType_Ping, Ping_fields, (void (*)(void *))fsm_msgPing}, - {'n', 'i', MessageType_MessageType_ChangePin, ChangePin_fields, (void (*)(void *))fsm_msgChangePin}, - {'n', 'i', MessageType_MessageType_WipeDevice, WipeDevice_fields, (void (*)(void *))fsm_msgWipeDevice}, - {'n', 'i', MessageType_MessageType_GetEntropy, GetEntropy_fields, (void (*)(void *))fsm_msgGetEntropy}, - {'n', 'i', MessageType_MessageType_GetPublicKey, GetPublicKey_fields, (void (*)(void *))fsm_msgGetPublicKey}, - {'n', 'i', MessageType_MessageType_LoadDevice, LoadDevice_fields, (void (*)(void *))fsm_msgLoadDevice}, - {'n', 'i', MessageType_MessageType_ResetDevice, ResetDevice_fields, (void (*)(void *))fsm_msgResetDevice}, - {'n', 'i', MessageType_MessageType_SignTx, SignTx_fields, (void (*)(void *))fsm_msgSignTx}, -// {'n', 'i', MessageType_MessageType_PinMatrixAck, PinMatrixAck_fields, (void (*)(void *))fsm_msgPinMatrixAck}, - {'n', 'i', MessageType_MessageType_Cancel, Cancel_fields, (void (*)(void *))fsm_msgCancel}, - {'n', 'i', MessageType_MessageType_TxAck, TxAck_fields, (void (*)(void *))fsm_msgTxAck}, - {'n', 'i', MessageType_MessageType_CipherKeyValue, CipherKeyValue_fields, (void (*)(void *))fsm_msgCipherKeyValue}, - {'n', 'i', MessageType_MessageType_ClearSession, ClearSession_fields, (void (*)(void *))fsm_msgClearSession}, - {'n', 'i', MessageType_MessageType_ApplySettings, ApplySettings_fields, (void (*)(void *))fsm_msgApplySettings}, -// {'n', 'i', MessageType_MessageType_ButtonAck, ButtonAck_fields, (void (*)(void *))fsm_msgButtonAck}, - {'n', 'i', MessageType_MessageType_GetAddress, GetAddress_fields, (void (*)(void *))fsm_msgGetAddress}, - {'n', 'i', MessageType_MessageType_EntropyAck, EntropyAck_fields, (void (*)(void *))fsm_msgEntropyAck}, - {'n', 'i', MessageType_MessageType_SignMessage, SignMessage_fields, (void (*)(void *))fsm_msgSignMessage}, - {'n', 'i', MessageType_MessageType_SignIdentity, SignIdentity_fields, (void (*)(void *))fsm_msgSignIdentity}, - {'n', 'i', MessageType_MessageType_VerifyMessage, VerifyMessage_fields, (void (*)(void *))fsm_msgVerifyMessage}, - {'n', 'i', MessageType_MessageType_GetECDHSessionKey, GetECDHSessionKey_fields, (void (*)(void *))fsm_msgGetECDHSessionKey}, -/* ECIES disabled - {'n', 'i', MessageType_MessageType_EncryptMessage, EncryptMessage_fields, (void (*)(void *))fsm_msgEncryptMessage}, - {'n', 'i', MessageType_MessageType_DecryptMessage, DecryptMessage_fields, (void (*)(void *))fsm_msgDecryptMessage}, -*/ -// {'n', 'i', MessageType_MessageType_PassphraseAck, PassphraseAck_fields, (void (*)(void *))fsm_msgPassphraseAck}, - {'n', 'i', MessageType_MessageType_EstimateTxSize, EstimateTxSize_fields, (void (*)(void *))fsm_msgEstimateTxSize}, - {'n', 'i', MessageType_MessageType_RecoveryDevice, RecoveryDevice_fields, (void (*)(void *))fsm_msgRecoveryDevice}, - {'n', 'i', MessageType_MessageType_WordAck, WordAck_fields, (void (*)(void *))fsm_msgWordAck}, - {'n', 'i', MessageType_MessageType_SetU2FCounter, SetU2FCounter_fields, (void (*)(void *))fsm_msgSetU2FCounter}, - {'n', 'i', MessageType_MessageType_EthereumGetAddress, EthereumGetAddress_fields, (void (*)(void *))fsm_msgEthereumGetAddress}, - {'n', 'i', MessageType_MessageType_EthereumSignTx, EthereumSignTx_fields, (void (*)(void *))fsm_msgEthereumSignTx}, - {'n', 'i', MessageType_MessageType_EthereumTxAck, EthereumTxAck_fields, (void (*)(void *))fsm_msgEthereumTxAck}, - // out messages - {'n', 'o', MessageType_MessageType_Success, Success_fields, 0}, - {'n', 'o', MessageType_MessageType_Failure, Failure_fields, 0}, - {'n', 'o', MessageType_MessageType_Entropy, Entropy_fields, 0}, - {'n', 'o', MessageType_MessageType_PublicKey, PublicKey_fields, 0}, - {'n', 'o', MessageType_MessageType_Features, Features_fields, 0}, - {'n', 'o', MessageType_MessageType_PinMatrixRequest, PinMatrixRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_TxRequest, TxRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_CipheredKeyValue, CipheredKeyValue_fields, 0}, - {'n', 'o', MessageType_MessageType_ButtonRequest, ButtonRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_Address, Address_fields, 0}, - {'n', 'o', MessageType_MessageType_EntropyRequest, EntropyRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_MessageSignature, MessageSignature_fields, 0}, - {'n', 'o', MessageType_MessageType_SignedIdentity, SignedIdentity_fields, 0}, - {'n', 'o', MessageType_MessageType_ECDHSessionKey, ECDHSessionKey_fields, 0}, -/* ECIES disabled - {'n', 'o', MessageType_MessageType_EncryptedMessage, EncryptedMessage_fields, 0}, - {'n', 'o', MessageType_MessageType_DecryptedMessage, DecryptedMessage_fields, 0}, -*/ - {'n', 'o', MessageType_MessageType_PassphraseRequest, PassphraseRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_TxSize, TxSize_fields, 0}, - {'n', 'o', MessageType_MessageType_WordRequest, WordRequest_fields, 0}, - {'n', 'o', MessageType_MessageType_EthereumAddress, EthereumAddress_fields, 0}, - {'n', 'o', MessageType_MessageType_EthereumTxRequest, EthereumTxRequest_fields, 0}, -#if DEBUG_LINK - // debug in messages -// {'d', 'i', MessageType_MessageType_DebugLinkDecision, DebugLinkDecision_fields, (void (*)(void *))fsm_msgDebugLinkDecision}, - {'d', 'i', MessageType_MessageType_DebugLinkGetState, DebugLinkGetState_fields, (void (*)(void *))fsm_msgDebugLinkGetState}, - {'d', 'i', MessageType_MessageType_DebugLinkStop, DebugLinkStop_fields, (void (*)(void *))fsm_msgDebugLinkStop}, - {'d', 'i', MessageType_MessageType_DebugLinkMemoryRead, DebugLinkMemoryRead_fields, (void (*)(void *))fsm_msgDebugLinkMemoryRead}, - {'d', 'i', MessageType_MessageType_DebugLinkMemoryWrite, DebugLinkMemoryWrite_fields, (void (*)(void *))fsm_msgDebugLinkMemoryWrite}, - {'d', 'i', MessageType_MessageType_DebugLinkFlashErase, DebugLinkFlashErase_fields, (void (*)(void *))fsm_msgDebugLinkFlashErase}, - // debug out messages - {'d', 'o', MessageType_MessageType_DebugLinkState, DebugLinkState_fields, 0}, - {'d', 'o', MessageType_MessageType_DebugLinkLog, DebugLinkLog_fields, 0}, - {'d', 'o', MessageType_MessageType_DebugLinkMemory, DebugLinkMemory_fields, 0}, -#endif +#include "messages_map.h" // end {0, 0, 0, 0, 0} }; diff --git a/firmware/protob/.gitignore b/firmware/protob/.gitignore index 0a5bea8f2b..8d944df5a6 100644 --- a/firmware/protob/.gitignore +++ b/firmware/protob/.gitignore @@ -1 +1,3 @@ *.pb +*_pb2.py +__pycache__/ diff --git a/firmware/protob/Makefile b/firmware/protob/Makefile index 5b8d69e038..38486526cd 100644 --- a/firmware/protob/Makefile +++ b/firmware/protob/Makefile @@ -1,4 +1,4 @@ -all: messages.pb.c storage.pb.c types.pb.c +all: messages.pb.c storage.pb.c types.pb.c messages_map.h %.pb.c: %.pb %.options nanopb_generator.py $< -L '#include "%s"' -T @@ -6,5 +6,11 @@ all: messages.pb.c storage.pb.c types.pb.c %.pb: %.proto protoc -I/usr/include -I. $< -o $@ +%_pb2.py: %.proto + protoc -I/usr/include -I. $< --python_out=. + +messages_map.h: messages_map.py messages_pb2.py types_pb2.py + ./$< > $@ + clean: - rm -f *.pb *.o *.pb.c *.pb.h + rm -f *.pb *.o *.pb.c *.pb.h *_pb2.py messages_map.h diff --git a/firmware/protob/messages_map.h b/firmware/protob/messages_map.h new file mode 100644 index 0000000000..8bc6ad4b58 --- /dev/null +++ b/firmware/protob/messages_map.h @@ -0,0 +1,73 @@ + // in messages + { 'n', 'i', MessageType_MessageType_Initialize, Initialize_fields, (void (*)(void *)) fsm_msgInitialize }, + { 'n', 'i', MessageType_MessageType_Ping, Ping_fields, (void (*)(void *)) fsm_msgPing }, + { 'n', 'i', MessageType_MessageType_ChangePin, ChangePin_fields, (void (*)(void *)) fsm_msgChangePin }, + { 'n', 'i', MessageType_MessageType_WipeDevice, WipeDevice_fields, (void (*)(void *)) fsm_msgWipeDevice }, +// { 'n', 'i', MessageType_MessageType_FirmwareErase, FirmwareErase_fields, (void (*)(void *)) fsm_msgFirmwareErase }, // BOOTLOADER +// { 'n', 'i', MessageType_MessageType_FirmwareUpload, FirmwareUpload_fields, (void (*)(void *)) fsm_msgFirmwareUpload }, // BOOTLOADER + { 'n', 'i', MessageType_MessageType_GetEntropy, GetEntropy_fields, (void (*)(void *)) fsm_msgGetEntropy }, + { 'n', 'i', MessageType_MessageType_GetPublicKey, GetPublicKey_fields, (void (*)(void *)) fsm_msgGetPublicKey }, + { 'n', 'i', MessageType_MessageType_LoadDevice, LoadDevice_fields, (void (*)(void *)) fsm_msgLoadDevice }, + { 'n', 'i', MessageType_MessageType_ResetDevice, ResetDevice_fields, (void (*)(void *)) fsm_msgResetDevice }, + { 'n', 'i', MessageType_MessageType_SignTx, SignTx_fields, (void (*)(void *)) fsm_msgSignTx }, +// { 'n', 'i', MessageType_MessageType_SimpleSignTx, SimpleSignTx_fields, (void (*)(void *)) fsm_msgSimpleSignTx }, // DEPRECATED +// { 'n', 'i', MessageType_MessageType_PinMatrixAck, PinMatrixAck_fields, (void (*)(void *)) fsm_msgPinMatrixAck }, + { 'n', 'i', MessageType_MessageType_Cancel, Cancel_fields, (void (*)(void *)) fsm_msgCancel }, + { 'n', 'i', MessageType_MessageType_TxAck, TxAck_fields, (void (*)(void *)) fsm_msgTxAck }, + { 'n', 'i', MessageType_MessageType_CipherKeyValue, CipherKeyValue_fields, (void (*)(void *)) fsm_msgCipherKeyValue }, + { 'n', 'i', MessageType_MessageType_ClearSession, ClearSession_fields, (void (*)(void *)) fsm_msgClearSession }, + { 'n', 'i', MessageType_MessageType_ApplySettings, ApplySettings_fields, (void (*)(void *)) fsm_msgApplySettings }, +// { 'n', 'i', MessageType_MessageType_ButtonAck, ButtonAck_fields, (void (*)(void *)) fsm_msgButtonAck }, + { 'n', 'i', MessageType_MessageType_GetAddress, GetAddress_fields, (void (*)(void *)) fsm_msgGetAddress }, + { 'n', 'i', MessageType_MessageType_EntropyAck, EntropyAck_fields, (void (*)(void *)) fsm_msgEntropyAck }, + { 'n', 'i', MessageType_MessageType_SignMessage, SignMessage_fields, (void (*)(void *)) fsm_msgSignMessage }, + { 'n', 'i', MessageType_MessageType_VerifyMessage, VerifyMessage_fields, (void (*)(void *)) fsm_msgVerifyMessage }, +// { 'n', 'i', MessageType_MessageType_PassphraseAck, PassphraseAck_fields, (void (*)(void *)) fsm_msgPassphraseAck }, + { 'n', 'i', MessageType_MessageType_EstimateTxSize, EstimateTxSize_fields, (void (*)(void *)) fsm_msgEstimateTxSize }, + { 'n', 'i', MessageType_MessageType_RecoveryDevice, RecoveryDevice_fields, (void (*)(void *)) fsm_msgRecoveryDevice }, + { 'n', 'i', MessageType_MessageType_WordAck, WordAck_fields, (void (*)(void *)) fsm_msgWordAck }, +// { 'n', 'i', MessageType_MessageType_EncryptMessage, EncryptMessage_fields, (void (*)(void *)) fsm_msgEncryptMessage }, // DEPRECATED +// { 'n', 'i', MessageType_MessageType_DecryptMessage, DecryptMessage_fields, (void (*)(void *)) fsm_msgDecryptMessage }, // DEPRECATED + { 'n', 'i', MessageType_MessageType_SignIdentity, SignIdentity_fields, (void (*)(void *)) fsm_msgSignIdentity }, + { 'n', 'i', MessageType_MessageType_GetFeatures, GetFeatures_fields, (void (*)(void *)) fsm_msgGetFeatures }, + { 'n', 'i', MessageType_MessageType_EthereumGetAddress, EthereumGetAddress_fields, (void (*)(void *)) fsm_msgEthereumGetAddress }, + { 'n', 'i', MessageType_MessageType_EthereumSignTx, EthereumSignTx_fields, (void (*)(void *)) fsm_msgEthereumSignTx }, + { 'n', 'i', MessageType_MessageType_EthereumTxAck, EthereumTxAck_fields, (void (*)(void *)) fsm_msgEthereumTxAck }, + { 'n', 'i', MessageType_MessageType_GetECDHSessionKey, GetECDHSessionKey_fields, (void (*)(void *)) fsm_msgGetECDHSessionKey }, + { 'n', 'i', MessageType_MessageType_SetU2FCounter, SetU2FCounter_fields, (void (*)(void *)) fsm_msgSetU2FCounter }, + // out messages + { 'n', 'o', MessageType_MessageType_Success, Success_fields, 0 }, + { 'n', 'o', MessageType_MessageType_Failure, Failure_fields, 0 }, +// { 'n', 'o', MessageType_MessageType_FirmwareRequest, FirmwareRequest_fields, 0 }, // BOOTLOADER + { 'n', 'o', MessageType_MessageType_Entropy, Entropy_fields, 0 }, + { 'n', 'o', MessageType_MessageType_PublicKey, PublicKey_fields, 0 }, + { 'n', 'o', MessageType_MessageType_Features, Features_fields, 0 }, + { 'n', 'o', MessageType_MessageType_PinMatrixRequest, PinMatrixRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_TxRequest, TxRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_ButtonRequest, ButtonRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_Address, Address_fields, 0 }, + { 'n', 'o', MessageType_MessageType_EntropyRequest, EntropyRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_MessageSignature, MessageSignature_fields, 0 }, + { 'n', 'o', MessageType_MessageType_PassphraseRequest, PassphraseRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_TxSize, TxSize_fields, 0 }, + { 'n', 'o', MessageType_MessageType_WordRequest, WordRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_CipheredKeyValue, CipheredKeyValue_fields, 0 }, +// { 'n', 'o', MessageType_MessageType_EncryptedMessage, EncryptedMessage_fields, 0 }, // DEPRECATED +// { 'n', 'o', MessageType_MessageType_DecryptedMessage, DecryptedMessage_fields, 0 }, // DEPRECATED + { 'n', 'o', MessageType_MessageType_SignedIdentity, SignedIdentity_fields, 0 }, + { 'n', 'o', MessageType_MessageType_EthereumAddress, EthereumAddress_fields, 0 }, + { 'n', 'o', MessageType_MessageType_EthereumTxRequest, EthereumTxRequest_fields, 0 }, + { 'n', 'o', MessageType_MessageType_ECDHSessionKey, ECDHSessionKey_fields, 0 }, +#if DEBUG_LINK + // debug in messages +// { 'd', 'i', MessageType_MessageType_DebugLinkDecision, DebugLinkDecision_fields, (void (*)(void *)) fsm_msgDebugLinkDecision }, + { 'd', 'i', MessageType_MessageType_DebugLinkGetState, DebugLinkGetState_fields, (void (*)(void *)) fsm_msgDebugLinkGetState }, + { 'd', 'i', MessageType_MessageType_DebugLinkStop, DebugLinkStop_fields, (void (*)(void *)) fsm_msgDebugLinkStop }, + { 'd', 'i', MessageType_MessageType_DebugLinkMemoryRead, DebugLinkMemoryRead_fields, (void (*)(void *)) fsm_msgDebugLinkMemoryRead }, + { 'd', 'i', MessageType_MessageType_DebugLinkMemoryWrite, DebugLinkMemoryWrite_fields, (void (*)(void *)) fsm_msgDebugLinkMemoryWrite }, + { 'd', 'i', MessageType_MessageType_DebugLinkFlashErase, DebugLinkFlashErase_fields, (void (*)(void *)) fsm_msgDebugLinkFlashErase }, + // debug out messages + { 'd', 'o', MessageType_MessageType_DebugLinkState, DebugLinkState_fields, 0 }, + { 'd', 'o', MessageType_MessageType_DebugLinkLog, DebugLinkLog_fields, 0 }, + { 'd', 'o', MessageType_MessageType_DebugLinkMemory, DebugLinkMemory_fields, 0 }, +#endif diff --git a/firmware/protob/messages_map.py b/firmware/protob/messages_map.py new file mode 100755 index 0000000000..6cf6c04fae --- /dev/null +++ b/firmware/protob/messages_map.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +from collections import defaultdict +from messages_pb2 import MessageType +from types_pb2 import wire_in, wire_out, wire_debug_in, wire_debug_out, wire_tiny, wire_bootloader + +# len("MessageType_MessageType_") - len("_fields") == 17 +TEMPLATE = "\t{{ {type} {dir} {msg_id:46} {fields:29} {process_func} }}," + +LABELS = { + wire_in: "in messages", + wire_out: "out messages", + wire_debug_in: "debug in messages", + wire_debug_out: "debug out messages", +} + +def handle_message(message, extension): + name = message.name + short_name = name.split("MessageType_", 1).pop() + assert(short_name != name) + + interface = "d" if extension in (wire_debug_in, wire_debug_out) else "n" + direction = "i" if extension in (wire_in, wire_debug_in) else "o" + + options = message.GetOptions() + bootloader = options.Extensions[wire_bootloader] + tiny = options.Extensions[wire_tiny] and direction == "i" + + if options.deprecated or bootloader or tiny: + line = "// " + else: + line = "" + + line += TEMPLATE.format( + type="'%c'," % interface, + dir="'%c'," % direction, + msg_id="MessageType_%s," % name, + fields="%s_fields," % short_name, + process_func = "(void (*)(void *)) fsm_msg%s" % short_name if direction == "i" else "0" + ) + + if options.deprecated: + line += " // DEPRECATED" + elif bootloader: + line += " // BOOTLOADER" + + return line + +messages = defaultdict(list) + +for message in MessageType.DESCRIPTOR.values: + extensions = message.GetOptions().Extensions + + for extension in (wire_in, wire_out, wire_debug_in, wire_debug_out): + if extensions[extension]: + messages[extension].append(message) + +for extension in (wire_in, wire_out, wire_debug_in, wire_debug_out): + if extension == wire_debug_in: + print("#if DEBUG_LINK") + + print("\t// {label}".format(label=LABELS[extension])) + + for message in messages[extension]: + print(handle_message(message, extension)) + + if extension == wire_debug_out: + print("#endif") + diff --git a/firmware/protob/types.pb.c b/firmware/protob/types.pb.c index dfc9a49084..c35f3d10d8 100644 --- a/firmware/protob/types.pb.c +++ b/firmware/protob/types.pb.c @@ -166,6 +166,32 @@ const pb_extension_type_t wire_debug_out = { &wire_debug_out_field }; +typedef struct { + bool wire_tiny; +} wire_tiny_struct; + +static const pb_field_t wire_tiny_field = + PB_FIELD2(50006, BOOL , OPTEXT, STATIC , FIRST, wire_tiny_struct, wire_tiny, wire_tiny, 0); + +const pb_extension_type_t wire_tiny = { + NULL, + NULL, + &wire_tiny_field +}; + +typedef struct { + bool wire_bootloader; +} wire_bootloader_struct; + +static const pb_field_t wire_bootloader_field = + PB_FIELD2(50007, BOOL , OPTEXT, STATIC , FIRST, wire_bootloader_struct, wire_bootloader, wire_bootloader, 0); + +const pb_extension_type_t wire_bootloader = { + NULL, + NULL, + &wire_bootloader_field +}; + /* Check that field information fits in pb_field_t */ #if !defined(PB_FIELD_32BIT) diff --git a/firmware/protob/types.pb.h b/firmware/protob/types.pb.h index 892b0df88d..4e25d7de28 100644 --- a/firmware/protob/types.pb.h +++ b/firmware/protob/types.pb.h @@ -284,6 +284,8 @@ extern const pb_extension_type_t wire_in; extern const pb_extension_type_t wire_out; extern const pb_extension_type_t wire_debug_in; extern const pb_extension_type_t wire_debug_out; +extern const pb_extension_type_t wire_tiny; +extern const pb_extension_type_t wire_bootloader; /* Default values for struct fields */ extern const uint32_t CoinType_address_type_default; @@ -381,6 +383,8 @@ extern const uint32_t IdentityType_index_default; #define wire_out_tag 50003 #define wire_debug_in_tag 50004 #define wire_debug_out_tag 50005 +#define wire_tiny_tag 50006 +#define wire_bootloader_tag 50007 /* Struct field encoding specification for nanopb */ extern const pb_field_t HDNodeType_fields[7]; diff --git a/vendor/trezor-common b/vendor/trezor-common index 95834d75ff..4fd03dcc95 160000 --- a/vendor/trezor-common +++ b/vendor/trezor-common @@ -1 +1 @@ -Subproject commit 95834d75ffe28b912f5289e501cb05d333880e16 +Subproject commit 4fd03dcc955cbcd28a70a81dbcc0d597bad9dac4