Skip to content

Commit

Permalink
Merge pull request #18 from canokeys/wip-core-update
Browse files Browse the repository at this point in the history
Wip core update
  • Loading branch information
z4yx authored Apr 24, 2024
2 parents 7963ece + d481ff9 commit fef9ab8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 41 deletions.
30 changes: 30 additions & 0 deletions Src/admin_vendor.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ int admin_vendor_version(const CAPDU *capdu, RAPDU *rapdu) {
return 0;
}

int admin_vendor_nfc_enable(const CAPDU *capdu, RAPDU *rapdu) {
if (P1 != 0x00 && P1 != 0x01) EXCEPT(SW_WRONG_P1P2);
if (P2 != 0x00) EXCEPT(SW_WRONG_P1P2);
if (LC != 0x00) EXCEPT(SW_WRONG_LENGTH);

uint32_t magic = P1 * 0x50 + 0x100;
FLASH_OBProgramInitTypeDef cfg = {
.OptionType = OPTIONBYTE_PCROP,
.PCROPConfig = FLASH_BANK_1,
.PCROPStartAddr = FLASH_BASE + magic, // Reuse this option word as NFC switch
.PCROPEndAddr = FLASH_BASE + 0xF, // Fixed value
};
DBG_MSG("Unlock OB\n");
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
int ret = HAL_FLASHEx_OBProgram(&cfg);
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
HAL_FLASHEx_OBGetConfig(&cfg);
uint32_t *flash_loc = (uint32_t*) 0x1FFF7808U;
DBG_MSG("HAL_FLASHEx_OBGetConfig: %d %x %x %x\n",
ret, cfg.PCROPStartAddr, cfg.PCROPEndAddr, *flash_loc);
// DBG_MSG("value= %x %x\n", *(uint32_t*)FLASH_BASE, *(uint32_t*)cfg.PCROPStartAddr);

if (ret != HAL_OK) return -1;

return 0;
}


extern uint32_t _stack_boundary;

static int stack_test(const CAPDU *capdu, RAPDU *rapdu) {
Expand Down
31 changes: 16 additions & 15 deletions Src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

const uint32_t UNTOUCHED_MAX_VAL = 40; /* Suitable for 56K pull-down resistor */
const uint32_t CALI_TIMES = 4;
const uint32_t TOUCH_GAP_TIME = 1500; /* Gap period (in ms) between two consecutive touch events */
const uint32_t TOUCH_GAP_TIME = 800; /* Gap period (in ms) between two consecutive touch events */
const uint32_t MIN_LONG_TOUCH_TIME = 500;
const uint32_t MIN_TOUCH_TIME = 20;

extern TIM_HandleTypeDef htim6;
extern SPI_HandleTypeDef FM_SPI;
Expand Down Expand Up @@ -127,25 +129,27 @@ void device_periodic_task(void) {
if (LL_USART_IsActiveFlag_RXNE(DBG_UART.Instance)) {
int data = LL_USART_ReceiveData8(DBG_UART.Instance);
DBG_MSG("UART: %x\n", data);
if ('T' == data) {
set_touch_result(TOUCH_SHORT);
if ('T' == data || 'L' == data) {
set_touch_result('T' == data ? TOUCH_SHORT : TOUCH_LONG);
fsm = TOUCH_STATE_ASSERT;
event_tick = tick;
}
}
#endif
if(GPIO_Touched()) {
measure_touch = 0;
fsm = TOUCH_STATE_DOWN;
event_tick = tick;
}
break;
case TOUCH_STATE_DOWN:
if(!GPIO_Touched()) {
fsm = TOUCH_STATE_IDLE;
} else if (tick - event_tick > 50) {
set_touch_result(TOUCH_SHORT);
fsm = TOUCH_STATE_ASSERT;
event_tick = tick;
if(!GPIO_Touched() || tick - event_tick > MIN_LONG_TOUCH_TIME) {
if (tick - event_tick > MIN_TOUCH_TIME) {
set_touch_result(tick - event_tick > MIN_LONG_TOUCH_TIME ? TOUCH_LONG : TOUCH_SHORT);
fsm = TOUCH_STATE_ASSERT;
event_tick = tick;
} else
fsm = TOUCH_STATE_IDLE;
}
break;
case TOUCH_STATE_ASSERT:
Expand Down Expand Up @@ -232,12 +236,9 @@ void usb_resources_alloc(void) {
IFACE_TABLE.ccid = iface++;
EP_SIZE_TABLE.ccid = 64;

if (cfg_is_kbd_interface_enable() && ep <= EP_ADDR_MSK) {
DBG_MSG("Keyboard interface enabled, Iface %u\n", iface);
EP_TABLE.kbd_hid = ep;
IFACE_TABLE.kbd_hid = iface;
EP_SIZE_TABLE.kbd_hid = 8;
}
EP_TABLE.kbd_hid = ep;
IFACE_TABLE.kbd_hid = iface;
EP_SIZE_TABLE.kbd_hid = 8;
}

int device_atomic_compare_and_swap(volatile uint32_t *var, uint32_t expect, uint32_t update) {
Expand Down
67 changes: 45 additions & 22 deletions Src/lfs_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#define CHIP_FLASH_SIZE 0x40000
#define LOOKAHEAD_SIZE 16
#define CACHE_SIZE 128
#define WRITE_SIZE 8
#define READ_SIZE 1
#define FS_BASE (&_lfs_begin)
Expand All @@ -16,8 +15,9 @@
#define FLASH_ADDR2BLOCK(a) (((a) & ~0x8000000u) / FLASH_PAGE_SIZE)

static struct lfs_config config;
static uint8_t read_buffer[CACHE_SIZE];
static uint8_t prog_buffer[CACHE_SIZE];
static uint8_t read_buffer[LFS_CACHE_SIZE];
static alignas(4) uint8_t prog_buffer[LFS_CACHE_SIZE];
// uint8_t file_buffer[LFS_LFS_CACHE_SIZE];
static alignas(4) uint8_t lookahead_buffer[LOOKAHEAD_SIZE];
extern uint8_t _lfs_begin;

Expand All @@ -30,13 +30,29 @@ int block_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, voi

static int program_space(uint32_t paddr, const void *buffer, lfs_size_t size) {
int ret = 0;
for (lfs_size_t i = 0; i < size; i += WRITE_SIZE) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, paddr + i, *(const uint64_t *)((uintptr_t)buffer + i)) !=
uint32_t typ;
for (lfs_size_t i = 0; size;) {
// DBG_MSG("%d\n", i);
// if (size >= 512) {
// typ = FLASH_TYPEPROGRAM_FAST;
// } else if (size >= 256) {
// typ = FLASH_TYPEPROGRAM_FAST_AND_LAST;
// } else {
typ = FLASH_TYPEPROGRAM_DOUBLEWORD;
// }
if (HAL_FLASH_Program(typ, paddr + i, *(const uint64_t *)((uintptr_t)buffer + i)) !=
HAL_OK) {
ERR_MSG("Flash prog fail @%#lx", paddr + i);
ERR_MSG("Flash prog failed @%#lx\n", paddr + i);
ret = LFS_ERR_CORRUPT;
break;
}
if (typ == FLASH_TYPEPROGRAM_DOUBLEWORD) {
i += 8;
size -= 8;
} else {
i += 256;
size -= 256;
}
}

return ret;
Expand All @@ -51,18 +67,24 @@ int block_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, con
int ret;

// DBG_MSG("blk %d @ %p len %u buf %p\r\n", block, (void*)paddr, size, buffer);

// for (size_t i = 0; i < size; i++)
// {
// if(*(uint8_t*)(paddr+i) != 0xFF) {
// DBG_MSG("blank check: %p = %x\n", paddr+i, *(uint8_t*)(paddr+i));
// }
// }

HAL_FLASH_Unlock();
ret = program_space(paddr, buffer, size);
HAL_FLASH_Lock();

// Invalidate cache
__HAL_FLASH_DATA_CACHE_DISABLE();
// __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
__HAL_FLASH_DATA_CACHE_RESET();
// __HAL_FLASH_INSTRUCTION_CACHE_RESET();
// __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
__HAL_FLASH_DATA_CACHE_ENABLE();
// __HAL_FLASH_DATA_CACHE_DISABLE();
// // __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
// __HAL_FLASH_DATA_CACHE_RESET();
// // __HAL_FLASH_INSTRUCTION_CACHE_RESET();
// // __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
// __HAL_FLASH_DATA_CACHE_ENABLE();

// DBG_MSG("verify %d\n", memcmp(buffer, (const void *)FLASH_ADDR(block, off), size));

Expand All @@ -78,26 +100,27 @@ int block_erase(const struct lfs_config *c, lfs_block_t block) {
EraseInitStruct.Banks = FS_BANK;
EraseInitStruct.Page = block + FLASH_ADDR2BLOCK((uintptr_t)FS_BASE);
EraseInitStruct.NbPages = 1;
DBG_MSG("block 0x%x\r\n", EraseInitStruct.Page);
// DBG_MSG("block 0x%x\r\n", EraseInitStruct.Page);

HAL_FLASH_Unlock();

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) {
ret = LFS_ERR_IO;
ERR_MSG("HAL_FLASHEx_Erase %#x failed", (unsigned int)PageError);
ERR_MSG("HAL_FLASHEx_Erase %#x failed\n", (unsigned int)PageError);
goto erase_fail;
}

// Invalidate cache
__HAL_FLASH_DATA_CACHE_DISABLE();
// __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
__HAL_FLASH_DATA_CACHE_RESET();
// __HAL_FLASH_INSTRUCTION_CACHE_RESET();
// __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
__HAL_FLASH_DATA_CACHE_ENABLE();
// __HAL_FLASH_DATA_CACHE_DISABLE();
// // __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
// __HAL_FLASH_DATA_CACHE_RESET();
// // __HAL_FLASH_INSTRUCTION_CACHE_RESET();
// // __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
// __HAL_FLASH_DATA_CACHE_ENABLE();

erase_fail:
HAL_FLASH_Lock();
// DBG_MSG("done\n");

return ret;
}
Expand All @@ -121,7 +144,7 @@ void littlefs_init() {
config.block_size = FLASH_PAGE_SIZE;
config.block_count = CHIP_FLASH_SIZE / FLASH_PAGE_SIZE - FLASH_ADDR2BLOCK((uintptr_t)FS_BASE);
config.block_cycles = 100000;
config.cache_size = CACHE_SIZE;
config.cache_size = LFS_CACHE_SIZE;
config.lookahead_size = LOOKAHEAD_SIZE;
config.read_buffer = read_buffer;
config.prog_buffer = prog_buffer;
Expand Down
25 changes: 22 additions & 3 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
extern uint32_t _stack_boundary;
uint32_t device_loop_enable;
uint8_t device_loop_enable, usb_init_done;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand Down Expand Up @@ -274,6 +274,20 @@ static void config_usb_mode(void) {
// enable the device_periodic_task, which controls LED and Touch sensing
device_loop_enable = 1;
}

static int check_is_nfc_en(void) {
uint32_t *flash_loc = (uint32_t*) 0x1FFF7808U;
uint32_t val = *flash_loc; //FLASH->PCROP1SR;
DBG_MSG("%x\n", val);
return val == 0xFFFFFFFFU || // ST production default value
val == 0xFFFF802a; // magic written by admin_vendor_nfc_enable()
}

// Called by core library
void USBD_LL_Init_Done(void)
{
usb_init_done = 1;
}
/* USER CODE END 0 */

/**
Expand Down Expand Up @@ -311,7 +325,7 @@ int main(void) {
MX_USART2_UART_Init();
SetupMPU(); // comment out this line during on-chip debugging
/* USER CODE BEGIN 2 */
in_nfc_mode = 1; // boot in NFC mode by default
in_nfc_mode = check_is_nfc_en(); // boot in NFC mode by default
nfc_init();
set_nfc_state(in_nfc_mode);

Expand All @@ -322,6 +336,10 @@ int main(void) {
applets_install();
init_apdu_buffer();

if (!in_nfc_mode) {
while (!detect_usb());
config_usb_mode();
}
DBG_MSG("Main Loop\n");
/* USER CODE END 2 */

Expand All @@ -343,7 +361,8 @@ int main(void) {
DBG_MSG("Touch calibrating...\n");
GPIO_Touch_Calibrate();
}
device_loop(1);
if (usb_init_done)
device_loop(1);
++i;
}
}
Expand Down
1 change: 1 addition & 0 deletions Src/usbd_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ static void SystemClockConfig_Resume(void)
{
SystemClock_CustomConfig(false, true);
}

/* USER CODE END 5 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2 changes: 1 addition & 1 deletion canokey-core
Submodule canokey-core updated 57 files
+3 −2 CMakeLists.txt
+18 −5 README.md
+64 −30 applets/admin/admin.c
+7 −12 applets/ctap/cose-key.h
+9 −2 applets/ctap/ctap-internal.h
+7 −2 applets/ctap/ctap-parser.c
+230 −181 applets/ctap/ctap.c
+39 −17 applets/ctap/secret.c
+2 −2 applets/ctap/u2f.c
+3 −6 applets/ndef/ndef.c
+96 −114 applets/oath/oath.c
+192 −0 applets/pass/pass.c
+128 −78 applets/piv/piv.c
+1 −1 canokey-crypto
+1 −1 go.mod
+2 −6 go.sum
+8 −11 include/admin.h
+2 −0 include/apdu.h
+2 −0 include/ctap.h
+38 −7 include/device.h
+2 −0 include/fs.h
+1 −1 include/key.h
+60 −14 include/nfc.h
+1 −2 include/oath.h
+22 −0 include/pass.h
+12 −0 include/piv.h
+187 −31 interfaces/NFC/fm.c
+25 −6 interfaces/NFC/nfc.c
+135 −70 interfaces/USB/class/ccid/ccid.c
+17 −1 interfaces/USB/class/ccid/ccid.h
+5 −1 interfaces/USB/class/ccid/usbd_ccid.c
+2 −0 interfaces/USB/class/ccid/usbd_ccid.h
+73 −44 interfaces/USB/class/kbdhid/kbdhid.c
+28 −3 interfaces/USB/class/webusb/webusb.c
+1 −2 interfaces/USB/core/inc/usbd_core.h
+3 −19 interfaces/USB/core/src/usbd_core.c
+1 −1 interfaces/USB/core/src/usbd_ctlreq.c
+1 −0 interfaces/USB/device/usbd_canokey.c
+10 −10 interfaces/USB/device/usbd_desc.c
+1 −1 littlefs
+2 −0 src/applets.c
+1 −1 src/device.c
+13 −6 src/fs.c
+27 −29 src/key.c
+70 −0 test-real/test-pass.sh
+63 −4 test-via-pcsc/admin_test.go
+1 −0 test-via-pcsc/build_fido_tests.sh
+47 −0 test-via-pcsc/fido2_SM2_COSE_key.patch
+5 −22 test-via-pcsc/piv_test.go
+8 −6 test/test_key.c
+119 −21 test/test_oath.c
+8 −6 test/test_openpgp.c
+8 −6 test/test_piv.c
+1 −1 virt-card/device-sim.c
+20 −2 virt-card/fabrication.c
+5 −2 virt-card/ifdhandler.c
+2 −1 virt-card/usb-dummy.c

0 comments on commit fef9ab8

Please sign in to comment.