Skip to content

Commit

Permalink
Support new BGA-based MCU (HW rev 4)
Browse files Browse the repository at this point in the history
Adds better hardware configuration, contained in platform_config.h.
If the firmware detects different hardware revision that it was built
for, it enters the bootloader mode.
Contains other improvements and discoveries, most important listed below.
This firmware was tested on both HW rev 3 and 4.

Tested on Fedora Linux against:
- libnitrokey test suite (libnitrokey v3.6-15-g871a1c9)
- GNUK test suite (Nitrokey Start: RTM.10-20-g64f8671)
- GnuPG 2.2.20 manual tests
- OpenSC 0.20 manual tests

Connected: Nitrokey/nitrokey-pro-hardware#20
Fixes #82

Squashed commit of the following:

    Blink debug LEDs in the development build

    Boot to the bootloader if flashed firmware does not suit to the hardware

    Correct compiler flags
    The -fdata-sections flag influences .text sections from the core drivers,
    which renders the CCID interface not working, and possibly introduced some other issues,
    despite having --gc-sections enabled only for the debug build.
    The -ffunction-sections flag seems to work fine otherwise. Leaving only for the debug builds for now.

    Debug: prepare test firmware with boot stages LED signalization
    Stage 1: boot start, before smart card configuration, no LED
    Stage 2: starting smart card configuration, awaiting for ATR, red LED blinking
    Stage 3: after smart card configuration, awaiting for USB configuration and connected state, blue/green LED flashes constantly
    Stage 4: after USB configration, boot process complete, blue/green LED blinking

    Debug: add ocd helpers for the development

    Introduce debug build with BUILD_DEBUG=1 switch
    Removing unused code sections results in a linking error, hence part of the code has to be discarded.
    To be confirmed, whether this changes the runtime behavior for the release version, and enable there as well.
    GC of the unused code is required for the development build, which does not fit the flash size otherwise.

    Mentioned error message:
    /gcc-arm-none-eabi-8-2018-q4-major/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../ar
    m-none-eabi/bin/ld: section .ARM.exidx LMA [000000000800b9cc,000000000800b9d3] overlaps section .data LMA [00000000080
    0b9cc,000000000800c02b]
  • Loading branch information
szszszsz committed Jun 18, 2021
1 parent 3c0f712 commit c8e20d4
Show file tree
Hide file tree
Showing 18 changed files with 800 additions and 288 deletions.
19 changes: 12 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ build-*-Debug
*.orig
*tags

build/gcc/nitrokey-pro-firmware.elf
build/gcc/nitrokey-pro-firmware.sym
build/gcc/nitrokey-pro-firmware.hex
build/gcc/nitrokey-pro-firmware.lss
build/gcc/nitrokey-pro-firmware.map
build/gcc/nitrokey-pro-firmware.hex
build/gcc/nitrokey-pro-firmware.sym
*.elf
*.lss
*.map
*.hex
*.sym
*.bin
*.sig
*.sha256
*.zip
cmake-build-debug
CMakeLists.txt
tmp
38 changes: 32 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DEPS=gcc-arm-none-eabi

firmware:
cd $(BUILD_DIR) && \
make && \
$(MAKE) && \
cd -
# mv $(BUILD_DIR)/crypto.elf .

Expand Down Expand Up @@ -44,8 +44,34 @@ deps:
sudo apt-get install ${DEPS}

release: firmware
mkdir -p release && \
cd release && \
cp $(BUILD_DIR)/nitrokey-pro-firmware.elf $(BUILD_DIR)/nitrokey-pro-firmware.hex . && \
find . -name *.elf -type f -printf "%f" | xargs -0 -n1 -I{} sh -c 'sha512sum -b {} > {}.sha512; md5sum -b {} > {}.md5' && \
find . -name *.hex -type f -printf "%f" | xargs -0 -n1 -I{} sh -c 'sha512sum -b {} > {}.sha512; md5sum -b {} > {}.md5'
mkdir -p release
-rm -r release/*.*
cp $(shell readlink -f $(BUILD_DIR)/last.hex) $(shell readlink -f $(BUILD_DIR)/last.buildinfo) release/
cd build/gcc && $(MAKE) -f dfu.mk flash-full-single
cd release && find . -name *.hex -type f -printf "%f" | xargs -0 -n1 -I{} sh -c 'sha512sum -b {} > {}.sha512'
ls release

.PHONY: gdbserver
gdbserver:
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg

.PHONY: ocdtelnet
ocdtelnet:
telnet localhost 4444

HW_REV?=4
.PHONY: devloop
devloop: | clean
$(MAKE) firmware -j12 BUILD_DEBUG=1 HW_REV=$(HW_REV)
-# killall telnet
- killall openocd
cd build/gcc && $(MAKE) -f dfu.mk flash-full-single
$(MAKE) gdbserver > /dev/null &
sleep 1
$(MAKE) ocdtelnet

.PHONY: devloop-release
devloop-release: | clean
$(MAKE) firmware -j12 HW_REV=$(HW_REV)
- killall openocd
cd build/gcc && $(MAKE) -f dfu.mk flash-full-single
64 changes: 58 additions & 6 deletions build/gcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
# Cmd interface paramenters
VID?=0x20a0
PID?=0x4108

HW_REV?=4
BUILD_DEBUG?=0

###########################################
# THUMB, RAM_RUN and Remapping currently
Expand Down Expand Up @@ -66,7 +67,7 @@ RUN_MODE=ROM_RUN


# Target file name (without extension).
TARGET = nitrokey-pro-firmware
TARGET = nitrokey-pro-firmware-hw$(HW_REV)

# List C source files here. (C dependencies are automatically generated.)
# use file-extension c for "c-only"-files
Expand Down Expand Up @@ -132,6 +133,7 @@ SRC = ../../src/main.c \
../../src/pwd-safe/FlashStorage.c \
../../src/pwd-safe/HandleAesStorageKey.c \
../../src/pwd-safe/password_safe.c \
../../src/hw_config_rev4.c \


# List C source files here which must be compiled in ARM-Mode.
Expand Down Expand Up @@ -174,8 +176,13 @@ FORMAT = ihex
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)

ifeq ($(BUILD_DEBUG), 1)
$(warning "Running development build")
OPT = 0
else
OPT = 2
#OPT = 0
endif

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
Expand Down Expand Up @@ -206,7 +213,7 @@ EXTRA_LIBDIRS =
CSTANDARD = -std=gnu99

# Place -D or -U options for C here
CDEFS = -D$(RUN_MODE) -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DUSE_STM3210E_EVAL -DGLOBAL_VID=$(VID) -DGLOBAL_PID=$(PID)
CDEFS = -D$(RUN_MODE) -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DUSE_STM3210E_EVAL -DGLOBAL_VID=$(VID) -DGLOBAL_PID=$(PID) -DHW_REV=$(HW_REV) -DBUILD_DEBUG=$(BUILD_DEBUG)
#CDEFS = -D$(RUN_MODE) -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DUSE_BOARD_STICK_V12
# Place -I options here
CINCS =
Expand Down Expand Up @@ -253,6 +260,16 @@ CONLYFLAGS += -Wmissing-declarations
endif

CFLAGS += -Wno-cast-qual
# extra diagnostic
#CFLAGS += -Wconversion -fno-common -Wdouble-promotion -Wshadow -Wpadded
#CFLAGS += -fstack-usage & -Wstack-usage=
ifeq ($(BUILD_DEBUG), 1)
#CFLAGS += -fdata-sections # makes CCID not working
CFLAGS += -ffunction-sections
#CFLAGS += -fstack-protector-all
endif
#CFLAGS += -funwind-tables
#CFLAGS += -fno-unwind-tables -fno-exceptions


# flags only for C++ (arm-elf-g++)
Expand Down Expand Up @@ -295,6 +312,12 @@ LDFLAGS += -lrdimon
LDFLAGS += $(CPLUSPLUS_LIB)
LDFLAGS += $(patsubst %,-L%,$(EXTRA_LIBDIRS))
LDFLAGS += $(patsubst %,-l%,$(EXTRA_LIBS))
LDFLAGS += -Wl,--print-memory-usage
LDFLAGS += -Wl,--print-gc-sections

ifeq ($(BUILD_DEBUG), 1)
LDFLAGS += -Wl,--gc-sections
endif

# Set Linker-Script Depending On Selected Memory and Controller
LDFLAGS +=-Tstm32.ld
Expand All @@ -304,6 +327,11 @@ LDFLAGS +=-Tstm32.ld
#LDFLAGS +=-T$(SUBMDL)-ROM.ld
#endif

ifeq ($(BUILD_DEBUG), 1)
LDFLAGS +=-TSTM32_SEC_FLASH-debug.ld
else
LDFLAGS +=-TSTM32_SEC_FLASH.ld
endif

# ---------------------------------------------------------------------------
# Flash-Programming support using lpc21isp by Martin Maurer
Expand Down Expand Up @@ -408,7 +436,7 @@ $(error "$(MSG_FORMATERROR) $(FORMAT)")
endif
endif

elf: $(TARGET).elf
elf: $(TARGET).elf $(TARGET).buildinfo
lss: $(TARGET).lss
sym: $(TARGET).sym

Expand Down Expand Up @@ -462,12 +490,14 @@ endif
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) $< $@
ln -sf $@ last.hex

# Create final output file (.bin) from ELF output file.
%.bin: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) $< $@
ln -sf $@ last.bin


# Create extended listing file from ELF output file.
Expand All @@ -476,13 +506,15 @@ endif
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S -C $< > $@
ln -sf $@ last.lss


# Create a symbol table from ELF output file.
%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
ln -sf $@ last.sym


# Link: create ELF output file from object files.
Expand All @@ -492,8 +524,28 @@ endif
@echo
@echo $(MSG_LINKING) $@
$(CC) $(THUMB) $(ALL_CFLAGS) $(AOBJARM) $(AOBJ) $(COBJARM) $(COBJ) $(CPPOBJ) $(CPPOBJARM) --output $@ $(LDFLAGS)
ln -sf $@ last.elf

# $(CPP) $(THUMB) $(ALL_CFLAGS) $(AOBJARM) $(AOBJ) $(COBJARM) $(COBJ) $(CPPOBJ) $(CPPOBJARM) --output $@ $(LDFLAGS)

$(TARGET).buildinfo:
date > $@
$(CC) --version >> $@
-git describe --long >> $@
-git describe --long --all >> $@
@echo CC=$(CC) >> $@
@echo THUMB=$(THUMB) >> $@
@echo ALL_CFLAGS=$(ALL_CFLAGS) >> $@
@echo AOBJARM=$(AOBJARM) >> $@
@echo AOBJ=$(AOBJ) >> $@
@echo COBJARM=$(COBJARM) >> $@
@echo COBJ=$(COBJ) >> $@
@echo CPPOBJ=$(CPPOBJ) >> $@
@echo CPPOBJARM=$(CPPOBJARM) >> $@
@echo LDFLAGS=$(LDFLAGS) >> $@
@echo "Writing build information to $@"
ln -sf $@ last.buildinfo

# Compile: create object files from C source files. ARM/Thumb
# Use CFLAGS_OUR_SRC flags only for our source code (all except located in Libraries dir)
$(COBJ) : %.o : %.c
Expand Down Expand Up @@ -550,7 +602,7 @@ clean_list :
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).bin
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).elf $(TARGET).buildinfo
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).obj
$(REMOVE) $(TARGET).a90
Expand Down
Loading

0 comments on commit c8e20d4

Please sign in to comment.