Skip to content

Commit

Permalink
Merge branch 'multi-hardware'
Browse files Browse the repository at this point in the history
Detect hardware at runtime and choose the proper pin and peripheral
mapping. Supports hardware revisions 1-4.

Tested with:
- libnitrokey v3.6-15-g871a1c9
- Gnuk tests/ suite RTM.10-20-g64f8671
  • Loading branch information
szszszsz committed Jun 21, 2021
2 parents 5ac8382 + bd4e5e6 commit d3751fc
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 191 deletions.
16 changes: 6 additions & 10 deletions Makefile
Expand Up @@ -7,8 +7,6 @@ DEPS=gcc-arm-none-eabi

.PHONY: firmware flash-versaloon clean release

HW_REV?=4

firmware:
cd $(BUILD_DIR) && \
$(MAKE) && \
Expand Down Expand Up @@ -41,7 +39,7 @@ flash-versaloon:
clean:
cd $(BUILD_DIR) && \
make clean
-rm nitrokey-*-firmware-hw*.tar.gz
-rm nitrokey-*-firmware*.tar.gz

deps:
sudo apt-get install ${DEPS}
Expand All @@ -50,20 +48,18 @@ deps:
release: | clean
mkdir -p release
-rm -r release/*.*
$(MAKE) firmware -j12 HW_REV=$(HW_REV)
$(MAKE) firmware -j12
cd build/gcc && $(MAKE) -f dfu.mk firmware.hex
cd build/gcc && $(MAKE) -f dfu.mk all.hex
cp `readlink -f $(BUILD_DIR)/last.hex` `readlink -f $(BUILD_DIR)/last_update.hex` `readlink -f $(BUILD_DIR)/last.buildinfo` release/
cd release && find . -name "*.hex" -type f -printf "%f\0" | xargs -0 -n1 -I{} sh -c 'sha512sum -b {} > {}.sha512'
ls -lh release
tar -czvf nitrokey-pro-firmware-hw$(HW_REV).tar.gz -C release .
tar -czvf nitrokey-pro-firmware.tar.gz -C release .

release-all:
mkdir -p release-all
-rm -r release-all/*.*
$(MAKE) release HW_REV=3
mv *tar.gz release-all
$(MAKE) release HW_REV=4
$(MAKE) release
mv *tar.gz release-all
ls -lh release-all/

Expand All @@ -77,7 +73,7 @@ ocdtelnet:

.PHONY: devloop
devloop: | clean
$(MAKE) firmware -j12 BUILD_DEBUG=1 HW_REV=$(HW_REV)
$(MAKE) firmware -j12 BUILD_DEBUG=1
-# killall telnet
- killall openocd
cd build/gcc && $(MAKE) -f dfu.mk flash-full-single
Expand All @@ -87,6 +83,6 @@ devloop: | clean

.PHONY: devloop-release
devloop-release: | clean
$(MAKE) firmware -j12 HW_REV=$(HW_REV)
$(MAKE) firmware -j12
- killall openocd
cd build/gcc && $(MAKE) -f dfu.mk flash-full-single
5 changes: 2 additions & 3 deletions build/gcc/Makefile
Expand Up @@ -35,7 +35,6 @@
# Cmd interface paramenters
VID?=0x20a0
PID?=0x4108
HW_REV?=4
BUILD_DEBUG?=0

###########################################
Expand Down Expand Up @@ -67,7 +66,7 @@ RUN_MODE=ROM_RUN


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

# List C source files here. (C dependencies are automatically generated.)
# use file-extension c for "c-only"-files
Expand Down Expand Up @@ -213,7 +212,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) -DHW_REV=$(HW_REV) -DBUILD_DEBUG=$(BUILD_DEBUG)
CDEFS = -D$(RUN_MODE) -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DUSE_STM3210E_EVAL -DGLOBAL_VID=$(VID) -DGLOBAL_PID=$(PID) -DBUILD_DEBUG=$(BUILD_DEBUG)
#CDEFS = -D$(RUN_MODE) -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DUSE_BOARD_STICK_V12
# Place -I options here
CINCS =
Expand Down
8 changes: 6 additions & 2 deletions src/ccid/smartcard/smartcard.c
Expand Up @@ -28,6 +28,7 @@
#include "CCID_Global.h"
#include "CCID_usb.h"
#include "hw_config.h"
#include "hw_config_rev4.h"

/* Private typedef ----------------------------------------------------------- */
/* Private define ------------------------------------------------------------ */
Expand Down Expand Up @@ -86,7 +87,10 @@ vu8 IMSI_Content[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// SC_State SCState = SC_POWER_OFF;


static struct HardwareDefinition const * l_sc_current_hardware = NULL;
void set_hardware_for_smartcard(void){
l_sc_current_hardware = detect_hardware();
}

/*******************************************************************************
* Function Name : RCC_Configuration
Expand Down Expand Up @@ -503,7 +507,7 @@ int SC_PTSConfig (void)
/* Reconfigure the USART Baud Rate ------------------------------------------- */
RCC_GetClocksFreq (&RCC_ClocksStatus);

apbclock = SMARTCARD_PCLK_STATUS_FREQ;
apbclock = SMARTCARD_PCLK_STATUS_FREQ(&RCC_ClocksStatus);
apbclock /= ((SMARTCARD_USART->GTPR & (u16) 0x00FF) * 2);
/* Enable the DMA Receive (Set DMAR bit only) to enable interrupt generation in case of a framing error FE */
USART_DMACmd (SMARTCARD_USART, USART_DMAReq_Rx, ENABLE);
Expand Down
3 changes: 3 additions & 0 deletions src/crypt/sha4/sha4.c
Expand Up @@ -325,6 +325,7 @@ void sha4 (unsigned char* input, int ilen, unsigned char output[64], int is384)
memset (&ctx, 0, sizeof (sha4_context));
}

#if 0
/*
* output = SHA-512( file contents )
*/
Expand Down Expand Up @@ -357,6 +358,8 @@ int sha4_file (char* path, unsigned char output[64], int is384)
return (0);
}

#endif

/*
* SHA-512 HMAC context setup
*/
Expand Down
51 changes: 8 additions & 43 deletions src/hw_config.c
Expand Up @@ -40,6 +40,7 @@
#include "RAMDISK_usb_desc.h"

#include "CcidLocalAccess.h"
#include "hw_config_rev4.h"

/* Private typedef ----------------------------------------------------------- */
/* Private define ------------------------------------------------------------ */
Expand Down Expand Up @@ -72,12 +73,6 @@ void DisableFirmwareDownloadPort (void)
}
#endif

enum Hardware {
HW_UNKNOWN = 0,
HW_NON_BGA_REV3 = NK_HW_REV3_ID,
HW_BGA_REV4 = NK_HW_REV4_ID
};
enum Hardware detect_hardware(void);
/*******************************************************************************
DisableSmartcardLED
Expand Down Expand Up @@ -292,22 +287,20 @@ void reset_to_bootloader(void) {
NVIC_SystemReset();
}

void exec_bootloader_if_wrong_hardware(void){
const bool execute_bootloader = detect_hardware() == NULL;
if (execute_bootloader) {
reset_to_bootloader();
}
}

/*******************************************************************************
* Function Name : Set_System
* Description : Configures Main system clocks & power
* Input : None.
* Return : None.
*******************************************************************************/

void exec_bootloader_if_wrong_hardware(void){
const enum Hardware hardware = detect_hardware();
const bool execute_bootloader = NK_HW_REV_ID != hardware;

if (execute_bootloader) {
reset_to_bootloader();
}
}

void Set_System (void)
{
/* RCC configuration */
Expand Down Expand Up @@ -563,34 +556,6 @@ void RCC_Config (void)

}

enum Hardware g_current_hardware = HW_UNKNOWN;

enum Hardware detect_hardware(void) {
/*
* Check the hardware revision with the following:
* 1. set B7 to input-pull up
* 2. check if its high - low -> new hardware, high -> old hardware
*/

if (g_current_hardware != HW_UNKNOWN) {
return g_current_hardware;
}

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE);
// GPIO_InitTypeDef GPIO_InitStructure;
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init (GPIOB, &GPIO_InitStructure);

const uint8_t state = GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_7);
if (state == 0) {
g_current_hardware = HW_BGA_REV4;
} else{
g_current_hardware = HW_NON_BGA_REV3;
}
return g_current_hardware;
}

/*******************************************************************************
* Function Name : MAL_Config
Expand Down

0 comments on commit d3751fc

Please sign in to comment.