From f9ba64ea94bba28bb041a9e1aec5c27dd3ec64e5 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 4 Feb 2019 13:53:05 +0100 Subject: [PATCH] messages: introduce messages_map_limits --- firmware/messages.c | 2 + firmware/messages.h | 6 +-- firmware/protob/.gitignore | 1 + firmware/protob/Makefile | 8 ++-- firmware/protob/messages-bitcoin.options | 2 + firmware/protob/messages_map.py | 55 ++++++++++++++++++------ vendor/trezor-common | 2 +- 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/firmware/messages.c b/firmware/messages.c index 28ac098776..8e315413f7 100644 --- a/firmware/messages.c +++ b/firmware/messages.c @@ -45,6 +45,8 @@ static const struct MessagesMap_t MessagesMap[] = { {0, 0, 0, 0, 0} }; +#include "messages_map_limits.h" + const pb_field_t *MessageFields(char type, char dir, uint16_t msg_id) { const struct MessagesMap_t *m = MessagesMap; diff --git a/firmware/messages.h b/firmware/messages.h index 26c843ac3e..1806a7daee 100644 --- a/firmware/messages.h +++ b/firmware/messages.h @@ -24,9 +24,9 @@ #include #include "trezor.h" -#define MSG_IN_SIZE (11 * 1024) +#define MSG_IN_SIZE (15 * 1024) -#define MSG_OUT_SIZE (11 * 1024) +#define MSG_OUT_SIZE (3 * 1024) #define msg_read(buf, len) msg_read_common('n', (buf), (len)) #define msg_write(id, ptr) msg_write_common('n', (id), (ptr)) @@ -34,7 +34,7 @@ const uint8_t *msg_out_data(void); #if DEBUG_LINK -#define MSG_DEBUG_OUT_SIZE (4 * 1024) +#define MSG_DEBUG_OUT_SIZE (2 * 1024) #define msg_debug_read(buf, len) msg_read_common('d', (buf), (len)) #define msg_debug_write(id, ptr) msg_write_common('d', (id), (ptr)) diff --git a/firmware/protob/.gitignore b/firmware/protob/.gitignore index 8ea827fb73..9d1fced364 100644 --- a/firmware/protob/.gitignore +++ b/firmware/protob/.gitignore @@ -4,4 +4,5 @@ *.pb.h *.pyc messages_map.h +messages_map_limits.h __pycache__/ diff --git a/firmware/protob/Makefile b/firmware/protob/Makefile index dbdf0485db..1838454bc4 100644 --- a/firmware/protob/Makefile +++ b/firmware/protob/Makefile @@ -2,7 +2,7 @@ ifneq ($(V),1) Q := @ endif -all: messages_map.h messages-bitcoin.pb.c messages-common.pb.c messages-crypto.pb.c messages-debug.pb.c messages-ethereum.pb.c messages-management.pb.c messages-nem.pb.c messages.pb.c messages-stellar.pb.c messages-lisk.pb.c messages_nem_pb2.py +all: messages_map.h messages_map_limits.h messages-bitcoin.pb.c messages-common.pb.c messages-crypto.pb.c messages-debug.pb.c messages-ethereum.pb.c messages-management.pb.c messages-nem.pb.c messages.pb.c messages-stellar.pb.c messages-lisk.pb.c messages_nem_pb2.py PYTHON ?= python @@ -25,8 +25,8 @@ messages_%_pb2.py: messages-%.proto @printf " PROTOC $@\n" $(Q)protoc -I/usr/include -I. $< --python_out=. -messages_map.h: messages_map.py messages_pb2.py - $(Q)$(PYTHON) $< | grep -v -e MessageType_Cardano -e MessageType_Tezos -e MessageType_Ripple -e MessageType_Monero -e MessageType_DebugMonero -e MessageType_Ontology -e MessageType_Tron -e MessageType_Eos > $@ +messages_map.h messages_map_limits.h: messages_map.py messages_pb2.py + $(Q)$(PYTHON) $< Cardano Tezos Ripple Monero DebugMonero Ontology Tron Eos clean: - rm -f *.pb *.o *.d *.pb.c *.pb.h *_pb2.py messages_map.h + rm -f *.pb *.o *.d *.pb.c *.pb.h *_pb2.py messages_map.h messages_map_limits.h diff --git a/firmware/protob/messages-bitcoin.options b/firmware/protob/messages-bitcoin.options index 68a1817616..f04d01f93e 100644 --- a/firmware/protob/messages-bitcoin.options +++ b/firmware/protob/messages-bitcoin.options @@ -47,5 +47,7 @@ TxRequestSerializedType.serialized_tx max_size:2048 MultisigRedeemScriptType.pubkeys max_count:15 MultisigRedeemScriptType.signatures max_count:15 max_size:73 +MultisigRedeemScriptType.nodes max_count:15 +MultisigRedeemScriptType.address_n max_count:8 HDNodePathType.address_n max_count:8 diff --git a/firmware/protob/messages_map.py b/firmware/protob/messages_map.py index f5ab67d274..ece7b3446e 100755 --- a/firmware/protob/messages_map.py +++ b/firmware/protob/messages_map.py @@ -1,12 +1,17 @@ #!/usr/bin/env python +import sys + from collections import defaultdict from messages_pb2 import MessageType from messages_pb2 import wire_in, wire_out from messages_pb2 import wire_debug_in, wire_debug_out from messages_pb2 import wire_bootloader, wire_no_fsm +fh = open("messages_map.h", "wt") +fl = open("messages_map_limits.h", "wt") + # len("MessageType_MessageType_") - len("_fields") == 17 -TEMPLATE = "\t{{ {type} {dir} {msg_id:46} {fields:29} {process_func} }}," +TEMPLATE = "\t{{ {type} {dir} {msg_id:46} {fields:29} {process_func} }},\n" LABELS = { wire_in: "in messages", @@ -16,11 +21,15 @@ LABELS = { } -def handle_message(message, extension): +def handle_message(fh, fl, skipped, message, extension): name = message.name short_name = name.split("MessageType_", 1).pop() assert(short_name != name) + for s in skipped: + if short_name.startswith(s): + return + 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" @@ -29,27 +38,44 @@ def handle_message(message, extension): no_fsm = options.Extensions[wire_no_fsm] if getattr(options, 'deprecated', None): - return '\t// Message %s is deprecated' % short_name + fh.write('\t// Message %s is deprecated\n' % short_name) + return if bootloader: - return '\t// Message %s is used in bootloader mode only' % short_name + fh.write('\t// Message %s is used in bootloader mode only\n' % short_name) + return if no_fsm: - return '\t// Message %s is not used in FSM' % short_name + fh.write('\t// Message %s is not used in FSM\n' % short_name) + return if direction == "i": process_func = "(void (*)(void *)) fsm_msg%s" % short_name else: process_func = "0" - return TEMPLATE.format( + fh.write(TEMPLATE.format( type="'%c'," % interface, dir="'%c'," % direction, msg_id="MessageType_%s," % name, fields="%s_fields," % short_name, process_func=process_func, - ) + )) + + bufsize = None + t = interface + direction + if t == "ni": + bufsize = "MSG_IN_SIZE" + elif t == "no": + bufsize = "MSG_OUT_SIZE" + elif t == "do": + bufsize = "MSG_DEBUG_OUT_SIZE" + if bufsize: + fl.write("_Static_assert(%s >= sizeof(%s), \"msg buffer too small\");\n" % (bufsize, short_name)) -print("\t// This file is automatically generated by messages_map.py -- DO NOT EDIT!") +skipped = sys.argv[1:] + +fh.write("\t// This file is automatically generated by messages_map.py -- DO NOT EDIT!\n") +fl.write("// This file is automatically generated by messages_map.py -- DO NOT EDIT!\n\n") messages = defaultdict(list) @@ -62,12 +88,17 @@ for message in MessageType.DESCRIPTOR.values: for extension in (wire_in, wire_out, wire_debug_in, wire_debug_out): if extension == wire_debug_in: - print("\n#if DEBUG_LINK") + fh.write("\n#if DEBUG_LINK\n") + fl.write("\n#if DEBUG_LINK\n") - print("\n\t// {label}\n".format(label=LABELS[extension])) + fh.write("\n\t// {label}\n\n".format(label=LABELS[extension])) for message in messages[extension]: - print(handle_message(message, extension)) + handle_message(fh, fl, skipped, message, extension) if extension == wire_debug_out: - print("\n#endif") + fh.write("\n#endif\n") + fl.write("#endif\n") + +fh.close() +fl.close() diff --git a/vendor/trezor-common b/vendor/trezor-common index 4b41d2e638..0735c7d6f5 160000 --- a/vendor/trezor-common +++ b/vendor/trezor-common @@ -1 +1 @@ -Subproject commit 4b41d2e63841517bf701618434c018acf4f1bca2 +Subproject commit 0735c7d6f524b4c5108d201c789612aad7ce7920