2023-03-23 14:42:21 +00:00
|
|
|
# pylint: disable=E0602
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tools
|
|
|
|
import boards
|
|
|
|
|
|
|
|
TREZOR_MODEL = ARGUMENTS.get('TREZOR_MODEL', 'T')
|
|
|
|
CMAKELISTS = int(ARGUMENTS.get('CMAKELISTS', 0))
|
|
|
|
DMA2D = False
|
|
|
|
|
2023-02-09 22:56:03 +00:00
|
|
|
if TREZOR_MODEL in ('1', 'DISC1'):
|
2023-03-23 14:42:21 +00:00
|
|
|
# skip bootloader build
|
|
|
|
env = Environment()
|
|
|
|
def build_bootloader(target,source,env):
|
|
|
|
print(f'Bootloader: nothing to build for Model {TREZOR_MODEL}')
|
|
|
|
program_bin = env.Command(
|
2023-02-09 22:56:03 +00:00
|
|
|
target='bootloader.elf',
|
2023-03-23 14:42:21 +00:00
|
|
|
source=None,
|
|
|
|
action=build_bootloader
|
|
|
|
)
|
|
|
|
Return()
|
|
|
|
|
|
|
|
FEATURES_WANTED = ["input", "rgb_led"]
|
|
|
|
|
|
|
|
CCFLAGS_MOD = ''
|
|
|
|
CPPPATH_MOD = []
|
|
|
|
CPPDEFINES_MOD = []
|
|
|
|
SOURCE_MOD = []
|
|
|
|
|
|
|
|
if TREZOR_MODEL in ('1', 'R'):
|
|
|
|
FONT_NORMAL='Font_PixelOperator_Regular_8'
|
2023-04-13 14:38:00 +00:00
|
|
|
FONT_DEMIBOLD='Font_PixelOperator_Regular_8'
|
|
|
|
FONT_BOLD='Font_PixelOperator_Bold_8'
|
|
|
|
FONT_MONO='Font_PixelOperator_Regular_8'
|
2023-06-05 15:03:22 +00:00
|
|
|
FONT_BIG=None
|
2023-08-24 09:33:50 +00:00
|
|
|
elif TREZOR_MODEL in ('T', 'T3T1', 'DISC2'):
|
2023-03-23 14:42:21 +00:00
|
|
|
FONT_NORMAL='Font_TTHoves_Regular_21'
|
|
|
|
FONT_DEMIBOLD=None
|
|
|
|
FONT_BOLD='Font_TTHoves_Bold_17'
|
|
|
|
FONT_MONO=None
|
2023-06-05 15:03:22 +00:00
|
|
|
FONT_BIG=None
|
2023-03-23 14:42:21 +00:00
|
|
|
|
|
|
|
# modtrezorcrypto
|
|
|
|
CCFLAGS_MOD += '-Wno-sequence-point '
|
|
|
|
CPPPATH_MOD += [
|
|
|
|
'vendor/trezor-crypto',
|
2023-06-30 10:12:55 +00:00
|
|
|
'vendor/trezor-storage',
|
2023-03-23 14:42:21 +00:00
|
|
|
]
|
|
|
|
CPPDEFINES_MOD += [
|
|
|
|
'AES_128',
|
|
|
|
'AES_192',
|
|
|
|
'USE_KECCAK',
|
|
|
|
'ED25519_NO_PRECOMP',
|
|
|
|
'TREZOR_UI2',
|
|
|
|
'FANCY_FATAL_ERROR'
|
|
|
|
]
|
|
|
|
SOURCE_MOD += [
|
|
|
|
'vendor/trezor-crypto/blake2s.c',
|
|
|
|
'vendor/trezor-crypto/chacha_drbg.c',
|
|
|
|
'vendor/trezor-crypto/chacha20poly1305/chacha_merged.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/curve25519-donna-32bit.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/curve25519-donna-helpers.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/curve25519-donna-scalarmult-base.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/ed25519.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/ed25519-donna-32bit-tables.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/ed25519-donna-basepoint-table.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/ed25519-donna-impl-base.c',
|
|
|
|
'vendor/trezor-crypto/ed25519-donna/modm-donna-32bit.c',
|
|
|
|
'vendor/trezor-crypto/memzero.c',
|
|
|
|
'vendor/trezor-crypto/rand.c',
|
|
|
|
'vendor/trezor-crypto/sha2.c',
|
|
|
|
]
|
|
|
|
|
|
|
|
# modtrezorui
|
|
|
|
CPPPATH_MOD += [
|
|
|
|
'vendor/micropython/lib/uzlib',
|
|
|
|
]
|
|
|
|
|
|
|
|
SOURCE_MOD += [
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/extmod/modtrezorcrypto/rand.c',
|
2023-05-09 09:40:23 +00:00
|
|
|
'embed/lib/buffers.c',
|
|
|
|
'embed/lib/colors.c',
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/lib/display_utils.c',
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/lib/display.c',
|
2023-05-09 09:40:23 +00:00
|
|
|
'embed/lib/fonts/font_bitmap.c',
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/lib/fonts/fonts.c',
|
2023-06-30 10:12:55 +00:00
|
|
|
'embed/lib/image.c',
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/lib/terminal.c',
|
2023-06-23 14:50:13 +00:00
|
|
|
'embed/lib/touch.c',
|
2024-01-17 14:29:49 +00:00
|
|
|
'embed/lib/unit_variant.c',
|
2023-03-23 14:42:21 +00:00
|
|
|
'vendor/micropython/lib/uzlib/adler32.c',
|
|
|
|
'vendor/micropython/lib/uzlib/crc32.c',
|
|
|
|
'vendor/micropython/lib/uzlib/tinflate.c',
|
2023-11-01 12:40:50 +00:00
|
|
|
'vendor/trezor-storage/flash_area.c',
|
2023-03-23 14:42:21 +00:00
|
|
|
]
|
|
|
|
|
2023-07-20 11:20:50 +00:00
|
|
|
if TREZOR_MODEL in ('1', ):
|
|
|
|
SOURCE_MOD += [
|
|
|
|
'embed/models/model_T1B1_layout.c',
|
|
|
|
]
|
|
|
|
elif TREZOR_MODEL in ('T', ):
|
|
|
|
SOURCE_MOD += [
|
|
|
|
'embed/models/model_T2T1_layout.c',
|
|
|
|
]
|
|
|
|
elif TREZOR_MODEL in ('R', ):
|
|
|
|
SOURCE_MOD += [
|
|
|
|
'embed/models/model_T2B1_layout.c',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
SOURCE_NANOPB = [
|
|
|
|
'vendor/nanopb/pb_common.c',
|
|
|
|
'vendor/nanopb/pb_decode.c',
|
|
|
|
'vendor/nanopb/pb_encode.c',
|
|
|
|
]
|
|
|
|
|
|
|
|
SOURCE_BOOTLOADER = [
|
|
|
|
'embed/bootloader/bootui.c',
|
|
|
|
'embed/bootloader/main.c',
|
|
|
|
'embed/bootloader/messages.c',
|
|
|
|
'embed/bootloader/emulator.c',
|
|
|
|
'embed/bootloader/protob/messages.pb.c',
|
|
|
|
]
|
|
|
|
|
|
|
|
SOURCE_TREZORHAL = [
|
2023-10-31 11:38:11 +00:00
|
|
|
'embed/trezorhal/unix/boot_args.c',
|
2023-06-23 14:50:13 +00:00
|
|
|
'embed/trezorhal/unix/display-unix.c',
|
2023-11-14 10:53:24 +00:00
|
|
|
'embed/trezorhal/unix/fault_handlers.c',
|
2023-06-23 14:50:13 +00:00
|
|
|
'embed/trezorhal/unix/flash.c',
|
2023-11-01 12:40:50 +00:00
|
|
|
'embed/trezorhal/unix/flash_otp.c',
|
2023-06-23 14:50:13 +00:00
|
|
|
'embed/trezorhal/unix/common.c',
|
|
|
|
'embed/trezorhal/unix/touch/touch.c',
|
|
|
|
'embed/trezorhal/unix/rng.c',
|
|
|
|
'embed/trezorhal/unix/usb.c',
|
|
|
|
'embed/trezorhal/unix/random_delays.c',
|
2023-03-23 14:42:21 +00:00
|
|
|
]
|
|
|
|
|
2023-08-31 20:51:35 +00:00
|
|
|
if TREZOR_MODEL in ('R', ):
|
|
|
|
SOURCE_TREZORHAL += [
|
|
|
|
'embed/trezorhal/unix/secret.c',
|
2023-09-26 09:44:43 +00:00
|
|
|
'embed/trezorhal/unix/optiga_hal.c',
|
2023-08-31 20:51:35 +00:00
|
|
|
]
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
SOURCE_UNIX = [
|
|
|
|
'embed/unix/profile.c',
|
|
|
|
]
|
|
|
|
|
2023-06-28 08:51:30 +00:00
|
|
|
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
# fonts
|
|
|
|
tools.add_font('NORMAL', FONT_NORMAL, CPPDEFINES_MOD, SOURCE_MOD)
|
|
|
|
tools.add_font('BOLD', FONT_BOLD, CPPDEFINES_MOD, SOURCE_MOD)
|
|
|
|
tools.add_font('DEMIBOLD', FONT_DEMIBOLD, CPPDEFINES_MOD, SOURCE_MOD)
|
|
|
|
tools.add_font('MONO', FONT_MONO, CPPDEFINES_MOD, SOURCE_MOD)
|
2023-06-05 15:03:22 +00:00
|
|
|
tools.add_font('BIG', FONT_BIG, CPPDEFINES_MOD, SOURCE_MOD)
|
2023-03-23 14:42:21 +00:00
|
|
|
|
|
|
|
env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0')))
|
|
|
|
|
|
|
|
env.Replace(
|
|
|
|
CP='cp',
|
|
|
|
AS='as',
|
|
|
|
AR='ar',
|
|
|
|
CC='gcc',
|
|
|
|
LINK='ld',
|
|
|
|
SIZE='size',
|
|
|
|
STRIP='strip',
|
|
|
|
OBJCOPY='objcopy',
|
|
|
|
PYTHON='python',
|
|
|
|
MAKECMAKELISTS='$PYTHON tools/make_cmakelists.py', )
|
|
|
|
|
|
|
|
env.Replace(
|
|
|
|
TREZOR_MODEL=TREZOR_MODEL, )
|
|
|
|
|
2023-02-09 22:56:03 +00:00
|
|
|
if TREZOR_MODEL in ('T', 'R'):
|
|
|
|
CPU_MODEL = 'STM32F427xx'
|
2023-08-24 09:33:50 +00:00
|
|
|
elif TREZOR_MODEL in ('T3T1', ):
|
|
|
|
CPU_MODEL = 'STM32U585xx'
|
2023-02-09 22:56:03 +00:00
|
|
|
elif TREZOR_MODEL in ('DISC1', ):
|
|
|
|
CPU_MODEL = 'STM32F429xx'
|
2023-06-28 08:51:37 +00:00
|
|
|
elif TREZOR_MODEL in ('DISC2', ):
|
|
|
|
CPU_MODEL = 'STM32U5A9xx'
|
2023-02-09 22:56:03 +00:00
|
|
|
elif TREZOR_MODEL in ('1',):
|
|
|
|
CPU_MODEL = 'STM32F405xx'
|
|
|
|
else:
|
|
|
|
raise ValueError('Unknown Trezor model')
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
MODEL_IDENTIFIER = tools.get_model_identifier(TREZOR_MODEL)
|
|
|
|
MODEL_AS_NUMBER = str(boards.get_hw_model_as_number(MODEL_IDENTIFIER))
|
|
|
|
|
|
|
|
env.Replace(
|
|
|
|
COPT=env.get('ENV').get('OPTIMIZE', '-Os'),
|
|
|
|
CCFLAGS='$COPT '
|
|
|
|
'-g3 '
|
|
|
|
'-nostdlib '
|
2023-08-07 13:50:53 +00:00
|
|
|
'-std=gnu11 -Wall -Werror -Wpointer-arith -Wno-missing-braces -fno-common '
|
2023-03-23 14:42:21 +00:00
|
|
|
'-fsingle-precision-constant -fdata-sections -ffunction-sections '
|
|
|
|
'-ffreestanding '
|
|
|
|
'-fstack-protector-all '
|
|
|
|
+ CCFLAGS_MOD,
|
|
|
|
CPPPATH=[
|
|
|
|
'embed/rust',
|
|
|
|
'embed/bootloader',
|
|
|
|
'embed/bootloader/nanopb',
|
|
|
|
'embed/bootloader/protob',
|
2023-05-09 09:40:23 +00:00
|
|
|
'embed/lib',
|
2023-06-01 12:21:51 +00:00
|
|
|
'embed/models',
|
2023-06-23 14:50:13 +00:00
|
|
|
'embed/trezorhal',
|
|
|
|
'embed/trezorhal/unix',
|
2023-03-23 14:42:21 +00:00
|
|
|
'embed/unix',
|
|
|
|
'embed/extmod/modtrezorui',
|
|
|
|
'vendor/nanopb',
|
|
|
|
] + CPPPATH_MOD,
|
|
|
|
CPPDEFINES=[
|
|
|
|
'BOOTLOADER',
|
|
|
|
'TREZOR_EMULATOR',
|
2023-02-09 22:56:03 +00:00
|
|
|
CPU_MODEL,
|
2023-03-23 14:42:21 +00:00
|
|
|
'HW_MODEL=' + MODEL_AS_NUMBER,
|
2023-06-30 20:57:04 +00:00
|
|
|
'HW_REVISION=' + ('10' if TREZOR_MODEL in ('R',) else '0'),
|
2023-03-23 14:42:21 +00:00
|
|
|
'TREZOR_MODEL_'+TREZOR_MODEL,
|
2023-06-23 14:50:13 +00:00
|
|
|
'TREZOR_BOARD=\\"boards/board-unix.h\\"',
|
2023-12-27 22:29:22 +00:00
|
|
|
('FLASH_BIT_ACCESS', '1'),
|
|
|
|
('FLASH_BLOCK_WORDS', '1'),
|
2023-06-27 22:10:46 +00:00
|
|
|
'MCU_TYPE='+CPU_MODEL,
|
2023-03-23 14:42:21 +00:00
|
|
|
'PB_FIELD_16BIT',
|
|
|
|
'PB_ENCODE_ARRAYS_UNPACKED',
|
|
|
|
'PB_VALIDATE_UTF8',
|
|
|
|
] + CPPDEFINES_MOD,
|
|
|
|
ASPPFLAGS='$CFLAGS $CCFLAGS', )
|
|
|
|
|
|
|
|
try:
|
|
|
|
env.ParseConfig('pkg-config --cflags --libs sdl2 SDL2_image')
|
|
|
|
except OSError:
|
|
|
|
print("SDL2 not installed, Emulator build is not possible")
|
|
|
|
|
|
|
|
|
|
|
|
env.Replace(
|
|
|
|
ALLSOURCES=SOURCE_MOD + SOURCE_BOOTLOADER + SOURCE_NANOPB + SOURCE_TREZORHAL + SOURCE_UNIX,
|
|
|
|
ALLDEFS=tools.get_defs_for_cmake(env['CPPDEFINES']))
|
|
|
|
|
|
|
|
cmake_gen = env.Command(
|
|
|
|
target='CMakeLists.txt',
|
|
|
|
source='',
|
|
|
|
action='$MAKECMAKELISTS --sources $ALLSOURCES --dirs $CPPPATH --defs $ALLDEFS',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Rust library
|
|
|
|
#
|
|
|
|
|
|
|
|
RUST_TARGET = 'x86_64-unknown-linux-gnu'
|
|
|
|
RUST_PROFILE = 'release'
|
|
|
|
RUST_LIB = 'trezor_lib'
|
|
|
|
RUST_LIBDIR = f'build/bootloader_emu/rust/{RUST_TARGET}/{RUST_PROFILE}'
|
|
|
|
RUST_LIBPATH = f'{RUST_LIBDIR}/lib{RUST_LIB}.a'
|
|
|
|
|
|
|
|
def cargo_build():
|
|
|
|
# Determine the profile build flags.
|
|
|
|
if RUST_PROFILE == 'release':
|
|
|
|
profile = '--release'
|
|
|
|
else:
|
|
|
|
profile = ''
|
|
|
|
if TREZOR_MODEL in ("1",):
|
|
|
|
features = ["model_t1"]
|
|
|
|
elif TREZOR_MODEL in ("R",):
|
|
|
|
features = ["model_tr"]
|
|
|
|
else:
|
|
|
|
features = ["model_tt"]
|
2023-05-09 09:40:23 +00:00
|
|
|
|
2023-08-24 09:33:50 +00:00
|
|
|
if TREZOR_MODEL in ('T', 'T3T1'):
|
2023-05-09 09:40:23 +00:00
|
|
|
features.append('touch')
|
2023-06-30 20:13:54 +00:00
|
|
|
features.append('backlight')
|
2023-05-09 09:40:23 +00:00
|
|
|
if TREZOR_MODEL in ('R', '1'):
|
|
|
|
features.append('button')
|
|
|
|
|
2023-03-23 14:42:21 +00:00
|
|
|
features.append("ui")
|
|
|
|
features.append("bootloader")
|
|
|
|
|
|
|
|
cargo_opts = [
|
|
|
|
f'--target={RUST_TARGET}',
|
2023-04-13 14:38:00 +00:00
|
|
|
'--target-dir=../../build/bootloader_emu/rust',
|
2023-03-23 14:42:21 +00:00
|
|
|
'--no-default-features',
|
|
|
|
'--features ' + ','.join(features),
|
|
|
|
'-Z build-std=core',
|
|
|
|
'-Z build-std-features=panic_immediate_abort',
|
|
|
|
]
|
|
|
|
|
|
|
|
return f'cd embed/rust; cargo build {profile} ' + ' '.join(cargo_opts)
|
|
|
|
|
|
|
|
rust = env.Command(
|
|
|
|
target=RUST_LIBPATH,
|
|
|
|
source='',
|
|
|
|
action=cargo_build(), )
|
|
|
|
|
2023-04-13 14:38:00 +00:00
|
|
|
env.Append(LINKFLAGS=f'-L{RUST_LIBDIR}')
|
|
|
|
env.Append(LINKFLAGS=f'-l{RUST_LIB}')
|
|
|
|
env.Append(LINKFLAGS='-lm')
|
2023-03-23 14:42:21 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Program objects
|
|
|
|
#
|
|
|
|
|
|
|
|
obj_program = []
|
|
|
|
obj_program += env.Object(source=SOURCE_MOD)
|
|
|
|
obj_program += env.Object(source=SOURCE_BOOTLOADER)
|
|
|
|
obj_program += env.Object(source=SOURCE_NANOPB)
|
|
|
|
obj_program += env.Object(source=SOURCE_TREZORHAL)
|
|
|
|
obj_program += env.Object(source=SOURCE_UNIX)
|
|
|
|
|
|
|
|
program_elf = env.Command(
|
|
|
|
target='bootloader.elf',
|
|
|
|
source=obj_program,
|
|
|
|
action=
|
|
|
|
'$CC -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS', )
|
|
|
|
|
|
|
|
env.Depends(program_elf, rust)
|
|
|
|
|
|
|
|
if CMAKELISTS != 0:
|
|
|
|
env.Depends(program_elf, cmake_gen)
|