From 8983a346d9ea0f5db769cf31e7961102043b18d9 Mon Sep 17 00:00:00 2001 From: Jason Zavaglia Date: Sat, 30 Sep 2017 19:09:11 +1000 Subject: [PATCH] Have coins.h and coins.c generated at build time (#220) --- Makefile.include | 2 +- firmware/.gitignore | 2 ++ firmware/Makefile | 15 +++++++++++++++ firmware/coins-gen.py | 38 +++++++++++++++++++++++--------------- firmware/coins.c | 13 +------------ firmware/coins.h | 7 +------ firmware/coins.json | 1 + firmware/protob/Makefile | 2 +- 8 files changed, 45 insertions(+), 35 deletions(-) create mode 100644 firmware/.gitignore create mode 120000 firmware/coins.json diff --git a/Makefile.include b/Makefile.include index 7bbcb3d82..cd4813fe3 100644 --- a/Makefile.include +++ b/Makefile.include @@ -139,7 +139,7 @@ $(NAME).elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f2.a $(TOP %.small.o: %.c Makefile $(CC) $(CFLAGS) -MMD -o $@ -c $< -clean: +clean:: rm -f $(OBJS) rm -f *.a rm -f *.bin diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 000000000..248dabf29 --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1,2 @@ +coins_array.h +coins_count.h diff --git a/firmware/Makefile b/firmware/Makefile index 4599627d2..750838826 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -95,3 +95,18 @@ bootloader.o: ../fastflash/bootloader.bin --redefine-sym _binary_$(shell echo -n "$<" | tr -c "[:alnum:]" "_")_size=__bootloader_size__ \ --rename-section .data=.rodata \ $< $@ + +# ensure header files are generated prior to compiling sources +coins.c crypto.c fsm.c transaction.c: coins_count.h +coins.c: coins_array.h + +################# +# Code Generation +coins_count.h: coins-gen.py coins.json + ./$< count > $@ + +coins_array.h: coins-gen.py coins.json + ./$< array > $@ + +clean:: + rm -f coins_count.h coins_array.h diff --git a/firmware/coins-gen.py b/firmware/coins-gen.py index 6d78185d1..9535466e5 100755 --- a/firmware/coins-gen.py +++ b/firmware/coins-gen.py @@ -1,10 +1,15 @@ -#!/usr/bin/env python3 -import json +#!/usr/bin/env python2 +from __future__ import print_function +import json, sys -coins_json = json.load(open('../vendor/trezor-common/coins.json', 'r')) +coins_json = json.load(open('coins.json', 'r')) coins_stable, coins_debug = [], [] +if len(sys.argv) != 2 or sys.argv[1] not in ("count", "array"): + print("usage: coins-gen.py [count|array]\n", file=sys.stderr) + sys.exit(1) + def get_fields(coin): return [ @@ -59,20 +64,23 @@ for coin in coins_json: justify_width(coins_stable) justify_width(coins_debug) -for row in coins_stable: - print('\t{' + ' '.join(row) + ' },') +print("// THIS IS A GENERATED FILE - DO NOT HAND EDIT\n\n") + +if sys.argv[1] == "array": + for row in coins_stable: + print('\t{' + ' '.join(row) + ' },') -print('#if DEBUG_LINK') + print('#if DEBUG_LINK') -for row in coins_debug: - print('\t{' + ' '.join(row) + ' },') + for row in coins_debug: + print('\t{' + ' '.join(row) + ' },') -print('#endif') + print('#endif') -print('-' * 32) -print('#if DEBUG_LINK') -print('#define COINS_COUNT %d' % (len(coins_stable) + len(coins_debug))) -print('#else') -print('#define COINS_COUNT %d' % (len(coins_stable))) -print('#endif') +if sys.argv[1] == "count": + print('#if DEBUG_LINK') + print('#define COINS_COUNT %d' % (len(coins_stable) + len(coins_debug))) + print('#else') + print('#define COINS_COUNT %d' % (len(coins_stable))) + print('#endif') diff --git a/firmware/coins.c b/firmware/coins.c index 6fe150906..f2f69a0d1 100644 --- a/firmware/coins.c +++ b/firmware/coins.c @@ -26,18 +26,7 @@ // filled CoinType Protobuf structure defined in https://github.com/trezor/trezor-common/blob/master/protob/types.proto#L133 // address types > 0xFF represent a two-byte prefix in big-endian order const CoinType coins[COINS_COUNT] = { - {true, "Bitcoin", true, " BTC", true, 0, true, 500000, true, 5, true, "\x18" "Bitcoin Signed Message:\n", true, 0x0488b21e, true, 0x0488ade4, true, true, false, 0, }, - {true, "Testnet", true, " TEST", true, 111, true, 10000000, true, 196, true, "\x18" "Bitcoin Signed Message:\n", true, 0x043587cf, true, 0x04358394, true, true, false, 0, }, - {true, "Bcash", true, " BCH", true, 0, true, 500000, true, 5, true, "\x18" "Bitcoin Signed Message:\n", true, 0x0488b21e, true, 0x0488ade4, true, false, true, 0, }, - {true, "Namecoin", true, " NMC", true, 52, true, 10000000, true, 5, true, "\x19" "Namecoin Signed Message:\n", true, 0x019da462, true, 0x019d9cfe, true, false, false, 0, }, - {true, "Litecoin", true, " LTC", true, 48, true, 40000000, true, 50, true, "\x19" "Litecoin Signed Message:\n", true, 0x019da462, true, 0x019d9cfe, true, true, false, 0, }, - {true, "Dogecoin", true, " DOGE", true, 30, true, 1000000000, true, 22, true, "\x19" "Dogecoin Signed Message:\n", true, 0x02facafd, true, 0x02fac398, true, false, false, 0, }, - {true, "Dash", true, " DASH", true, 76, true, 100000, true, 16, true, "\x19" "DarkCoin Signed Message:\n", true, 0x02fe52cc, true, 0x02fe52f8, true, false, false, 0, }, - {true, "Zcash", true, " ZEC", true, 7352, true, 1000000, true, 7357, true, "\x16" "Zcash Signed Message:\n", true, 0x0488b21e, true, 0x0488ade4, true, false, false, 0, }, -#if DEBUG_LINK - {true, "Bcash Testnet", true, " TBCH", true, 111, true, 10000000, true, 196, true, "\x18" "Bitcoin Signed Message:\n", true, 0x043587cf, true, 0x04358394, true, false, true, 0, }, - {true, "Zcash Testnet", true, " TAZ", true, 7461, true, 10000000, true, 7354, true, "\x16" "Zcash Signed Message:\n", true, 0x043587cf, true, 0x04358394, true, false, false, 0, }, -#endif +#include "coins_array.h" }; const CoinType *coinByName(const char *name) diff --git a/firmware/coins.h b/firmware/coins.h index a00709b63..a00fa2704 100644 --- a/firmware/coins.h +++ b/firmware/coins.h @@ -21,13 +21,8 @@ #define __COINS_H__ #include "messages.pb.h" -#include "types.pb.h" -#if DEBUG_LINK -#define COINS_COUNT 10 -#else -#define COINS_COUNT 8 -#endif +#include "coins_count.h" _Static_assert(pb_arraysize(Features, coins) >= COINS_COUNT, "Features.coins max_count not large enough"); diff --git a/firmware/coins.json b/firmware/coins.json new file mode 120000 index 000000000..a13f2d9c0 --- /dev/null +++ b/firmware/coins.json @@ -0,0 +1 @@ +../vendor/trezor-common/coins.json \ No newline at end of file diff --git a/firmware/protob/Makefile b/firmware/protob/Makefile index 6897debd1..34a4739bb 100644 --- a/firmware/protob/Makefile +++ b/firmware/protob/Makefile @@ -13,4 +13,4 @@ messages_map.h: messages_map.py messages_pb2.py types_pb2.py ./$< > $@ clean: - rm -f *.pb *.o *.pb.c *.pb.h *_pb2.py messages_map.h + rm -f *.pb *.o *.d *.pb.c *.pb.h *_pb2.py messages_map.h