diff --git a/.travis.yml b/.travis.yml index 0442efef6..3e22fdbd2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,8 @@ script: - make build_bootloader + - make build_loader + - make build_trezorhal - make build_unix TREZOR_NOUI=1 diff --git a/Makefile b/Makefile index 8be830b44..dcaf29ec5 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ JOBS=4 MAKE=make -j $(JOBS) BOOTLOADER_BUILD_DIR=micropython/bootloader/build -TREZORHAL_BUILD_DIR=micropython/firmware/build +LOADER_BUILD_DIR=micropython/loader/build +FIRMWARE_BUILD_DIR=micropython/firmware/build TREZORHAL_PORT_OPTS=FROZEN_MPY_DIR=src UNIX_PORT_OPTS=MICROPY_FORCE_32BIT=1 MICROPY_PY_BTREE=0 MICROPY_PY_TERMIOS=0 MICROPY_PY_FFI=0 MICROPY_PY_USSL=0 MICROPY_SSL_AXTLS=0 @@ -19,17 +20,20 @@ vendor: ## update git submodules res: ## update resources ./tools/res_collect -build: build_trezorhal build_unix build_cross ## build trezorhal, unix and mpy-cross micropython ports +build: build_firmware build_unix build_cross ## build firmware, unix and mpy-cross micropython ports -build_trezorhal: vendor res build_cross ## build trezorhal port with frozen modules +build_firmware: vendor res build_cross ## build firmware with frozen modules $(MAKE) -f Makefile.firmware $(TREZORHAL_PORT_OPTS) -build_trezorhal_debug: vendor res build_cross ## build trezorhal port with frozen modules and debug symbols +build_firmware_debug: vendor res build_cross ## build firmware with frozen modules and debug symbols $(MAKE) -f Makefile.firmware $(TREZORHAL_PORT_OPTS) DEBUG=1 build_bootloader: vendor ## build bootloader $(MAKE) -f Makefile.bootloader $(TREZORHAL_PORT_OPTS) +build_loader: vendor ## build loader + $(MAKE) -f Makefile.loader $(TREZORHAL_PORT_OPTS) + build_unix: vendor ## build unix port $(MAKE) -f ../../../micropython/unix/Makefile -C vendor/micropython/unix $(UNIX_PORT_OPTS) @@ -45,9 +49,9 @@ run: ## run unix port emu: ## run emulator ./emu.sh -clean: clean_trezorhal clean_unix clean_cross ## clean all builds +clean: clean_firmware clean_unix clean_cross ## clean all builds -clean_trezorhal: ## clean trezorhal build +clean_firmware: ## clean firmware build $(MAKE) -f Makefile.firmware clean $(TREZORHAL_PORT_OPTS) clean_unix: ## clean unix build @@ -59,17 +63,20 @@ clean_cross: ## clean mpy-cross build $(MAKE) -C vendor/micropython/mpy-cross clean $(CROSS_PORT_OPTS) flash: ## flash firmware using st-flash - st-flash write $(TREZORHAL_BUILD_DIR)/firmware.bin 0x8000000 + st-flash write $(FIRMWARE_BUILD_DIR)/firmware.bin 0x8000000 flash_bootloader: ## flash bootloader using st-flash st-flash write $(BOOTLOADER_BUILD_DIR)/bootloader.bin 0x8000000 -flash_openocd: $(TREZORHAL_BUILD_DIR)/firmware.hex ## flash firmware using openocd +flash_loader: ## flash loader using st-flash + st-flash write $(LOADER_BUILD_DIR)/loader.bin 0x8000000 + +flash_openocd: $(FIRMWARE_BUILD_DIR)/firmware.hex ## flash firmware using openocd openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg \ -c "init" \ -c "reset init" \ -c "stm32f4x mass_erase 0" \ - -c "flash write_image $(TREZORHAL_BUILD_DIR)/firmware.hex" \ + -c "flash write_image $(FIRMWARE_BUILD_DIR)/firmware.hex" \ -c "reset" \ -c "shutdown" @@ -77,7 +84,7 @@ openocd: ## start openocd which connects to the device openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg gdb: ## start remote gdb session which connects to the openocd - arm-none-eabi-gdb $(TREZORHAL_BUILD_DIR)/firmware.elf -ex 'target remote localhost:3333' + arm-none-eabi-gdb $(FIRMWARE_BUILD_DIR)/firmware.elf -ex 'target remote localhost:3333' test: ## run unit tests cd tests ; ./run_tests.sh diff --git a/Makefile.loader b/Makefile.loader new file mode 100644 index 000000000..0e5b32ebf --- /dev/null +++ b/Makefile.loader @@ -0,0 +1,166 @@ +# target directory +BUILD ?= micropython/loader/build + +# include py core make definitions +include vendor/micropython/py/mkenv.mk + +# sources +# ===================================== + +SRCDIR_MP = vendor/micropython +SRCDIR_FW = micropython + +BUILD_MP = $(BUILD)/$(SRCDIR_MP) +BUILD_FW = $(BUILD)/$(SRCDIR_FW) + +# OBJ vendor/micropython +OBJ_MP += $(addprefix $(BUILD_MP)/,\ + \ + lib/libc/string0.o \ + \ + stmhal/hal/f4/src/stm32f4xx_hal_adc_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_adc.o \ + stmhal/hal/f4/src/stm32f4xx_hal_can.o \ + stmhal/hal/f4/src/stm32f4xx_hal_cortex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_dac_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_dac.o \ + stmhal/hal/f4/src/stm32f4xx_hal_dma.o \ + stmhal/hal/f4/src/stm32f4xx_hal_flash_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_flash.o \ + stmhal/hal/f4/src/stm32f4xx_hal_gpio.o \ + stmhal/hal/f4/src/stm32f4xx_hal_i2c.o \ + stmhal/hal/f4/src/stm32f4xx_hal_pcd_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_pcd.o \ + stmhal/hal/f4/src/stm32f4xx_hal_pwr_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_pwr.o \ + stmhal/hal/f4/src/stm32f4xx_hal_rcc_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_rcc.o \ + stmhal/hal/f4/src/stm32f4xx_hal_rng.o \ + stmhal/hal/f4/src/stm32f4xx_hal_rtc_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_rtc.o \ + stmhal/hal/f4/src/stm32f4xx_hal_sd.o \ + stmhal/hal/f4/src/stm32f4xx_hal_spi.o \ + stmhal/hal/f4/src/stm32f4xx_hal_tim_ex.o \ + stmhal/hal/f4/src/stm32f4xx_hal_tim.o \ + stmhal/hal/f4/src/stm32f4xx_hal_uart.o \ + stmhal/hal/f4/src/stm32f4xx_hal.o \ + stmhal/hal/f4/src/stm32f4xx_ll_sdmmc.o \ + stmhal/hal/f4/src/stm32f4xx_ll_usb.o \ + \ + py/mpstate.o \ + py/nlrthumb.o \ + \ + stmhal/pendsv.o \ + stmhal/startup_stm32.o \ + stmhal/systick.o \ + ) + +# OBJ micropython/ +OBJ_FW += $(addprefix $(BUILD_FW)/, \ + loader/main.o \ + extmod/modtrezorui/display.o \ + extmod/modtrezorui/font_bitmap.o \ + trezorhal/stm32_it.o \ + trezorhal/stm32_system.o \ + trezorhal/hal/stm32f4xx_hal_sram.o \ + trezorhal/hal/stm32f4xx_ll_fsmc.o \ + ) + +# OBJ micropython/extmod/modtrezorcrypto +CFLAGS_MOD += \ + -I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto \ + -I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto/curve25519-donna \ + -I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto/ed25519-donna \ + -DED25519_CUSTOMRANDOM=1 \ + -DED25519_CUSTOMHASH=1 \ + -DED25519_NO_INLINE_ASM \ + -DED25519_FORCE_32BIT=1 \ + -DAES_128 \ + -DAES_192 \ + -DUSE_KECCAK=1 \ + -Wno-sequence-point + +OBJ_MOD += \ + $(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/ed25519-donna/ed25519.o \ + $(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/sha2.o \ + +OBJ = $(OBJ_MOD) $(OBJ_MP) $(OBJ_FW) +SRC_MP = $(patsubst $(BUILD_MP)%.o, $(SRCDIR_MP)%.c, $(OBJ_MP)) +SRC_FW = $(patsubst $(BUILD_FW)%.o, $(SRCDIR_FW)%.c, $(OBJ_FW)) +SRC_MOD = $(patsubst $(BUILD_FW)%.o, $(SRCDIR_FW)%.c, $(OBJ_MOD)) + +# comp flags +# ===================================== + +CROSS_COMPILE = arm-none-eabi- + +INC += -I. +INC += -Imicropython/extmod/modtrezorui +INC += -Imicropython/firmware +INC += -Imicropython/trezorhal +INC += -Imicropython/trezorhal/hal +INC += -Ivendor/trezor-crypto +INC += -Ivendor/micropython +INC += -Ivendor/micropython/stmhal +INC += -Ivendor/micropython/stmhal/cmsis +INC += -Ivendor/micropython/stmhal/hal/f4/inc +INC += -Ivendor/micropython/stmhal/usbdev/core/inc +INC += -Ivendor/micropython/stmhal/usbdev/class/inc +INC += -Ivendor/micropython/lib/cmsis/inc +INC += -I$(BUILD) + +ifeq ($(DEBUG), 1) +CFLAGS += -O0 -ggdb +else +CFLAGS += -Os -DNDEBUG +endif + +CFLAGS += $(INC) $(CFLAGS_MOD) $(CFLAGS_EXTRA) +CFLAGS += -std=gnu99 -nostdlib -Wall -Werror -Wdouble-promotion -Wpointer-arith +CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant + +CFLAGS += -DSTM32F405xx -DMCU_SERIES_F4 +CFLAGS += -DSTM32_HAL_H='' + +LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) + +LDFLAGS = -nostdlib -T micropython/trezorhal/memory.ld -Map=$@.map --cref + +# remove uncalled code from the final image +CFLAGS += -fdata-sections -ffunction-sections +LDFLAGS += --gc-sections + +# comp rules +# ===================================== + +all: $(BUILD)/loader.bin + +$(BUILD)/loader.elf: $(OBJ) + $(ECHO) "LINK $@" + $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) + $(Q)$(SIZE) $@ + +$(BUILD)/loader.bin: $(BUILD)/loader.elf + $(Q)$(OBJCOPY) -O binary -j .flash -j .data $^ $(BUILD)/loader.bin + +$(BUILD)/%.o: %.S + $(ECHO) "CC $<" + $(Q)$(CC) $(CFLAGS) -c -o $@ $< + +$(BUILD)/%.o: %.s + $(ECHO) "AS $<" + $(Q)$(AS) -o $@ $< + +$(BUILD)/%.o: %.c + $(ECHO) "CC $<" + $(Q)$(CC) $(CFLAGS) -c -MD -o $@ $< + +OBJ_DIRS = $(sort $(dir $(OBJ))) +$(OBJ): | $(OBJ_DIRS) +$(OBJ_DIRS): + $(MKDIR) -p $@ + +clean: + $(RM) -rf $(BUILD) + +.PHONY: all clean diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index a3f2d0515..ce0632dbb 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -156,7 +156,6 @@ bool copy_sdcard(void) int main(void) { - periph_init(); BOOTLOADER_PRINTLN("TREZOR Bootloader"); diff --git a/micropython/loader/.gitignore b/micropython/loader/.gitignore new file mode 100644 index 000000000..567609b12 --- /dev/null +++ b/micropython/loader/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/micropython/loader/build/genhdr/qstrdefs.generated.h b/micropython/loader/build/genhdr/qstrdefs.generated.h new file mode 100644 index 000000000..e69de29bb diff --git a/micropython/loader/main.c b/micropython/loader/main.c new file mode 100644 index 000000000..ff8b111d4 --- /dev/null +++ b/micropython/loader/main.c @@ -0,0 +1,48 @@ +#include STM32_HAL_H + +#include "display.h" + +void SystemClock_Config(void); + +void __attribute__((noreturn)) nlr_jump_fail(void *val) { + for (;;) {} +} + +void __attribute__((noreturn)) __fatal_error(const char *msg) { + for (volatile uint32_t delay = 0; delay < 10000000; delay++) { + } + display_print("FATAL ERROR:\n", -1); + display_print(msg, -1); + display_print("\n", -1); + for (;;) { + __WFI(); + } +} + +void mp_hal_stdout_tx_str(const char *str) { +} + +void periph_init(void) +{ + HAL_Init(); + + SystemClock_Config(); + + __GPIOA_CLK_ENABLE(); + __GPIOB_CLK_ENABLE(); + __GPIOC_CLK_ENABLE(); + __GPIOD_CLK_ENABLE(); + + display_init(); + display_clear(); + display_backlight(255); +} + +int main(void) +{ + periph_init(); + + __fatal_error("end reached"); + + return 0; +}