From 38d164360ccfcdfba2236b3f40cc030bd5c72786 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Tue, 31 May 2016 18:16:51 +0900 Subject: [PATCH] Update to Chopstx 0.12 --- ChangeLog | 20 +++++++ chopstx | 2 +- polarssl/library/aes.c | 6 +- src/Makefile.in | 12 ++-- src/adc.h | 8 --- src/configure | 15 ++--- src/gnuk.ld.in | 8 +-- src/main.c | 2 +- src/neug.c | 15 ++--- src/sys.h | 130 ----------------------------------------- src/usb-ccid.c | 10 +++- src/usb_ctrl.c | 2 +- src/usb_desc.c | 2 + src/usb_lld.h | 118 ------------------------------------- 14 files changed, 61 insertions(+), 289 deletions(-) delete mode 100644 src/adc.h delete mode 100644 src/sys.h delete mode 100644 src/usb_lld.h diff --git a/ChangeLog b/ChangeLog index fb2a136..fbe500c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2016-05-31 NIIBE Yutaka + + * src/usb-ccid.c (ccid_thread): Handle RESET->CONFIGURE process + correctly. + + * polarssl/library/aes.c (FT0, FT1, FT2): Add "weak" flag. + + * src/neug.c: Update from NeuG. + + * src/usb_desc.c (usb_cb_get_descriptor): Only valid if USE_SYS3. + + * src/Makefile.in (USE_SYS, USE_USB, USE_ADC): Enabled. + (CHIP): Add. + + * src/sys.c, src/sys.h: Remove. + * src/usb_stm32f103.c, src/usb_lld.h: Remove. + * src/adc_stm32f103.c, src/adc.h: Remove. + + * chopstx: Update to 0.12. + 2016-05-21 Niibe Yutaka * src/main.c (led_blink, main): Fix LED blink protocol. diff --git a/chopstx b/chopstx index 5458b77..c79a418 160000 --- a/chopstx +++ b/chopstx @@ -1 +1 @@ -Subproject commit 5458b77d360be2a7c5393f57c3d30399a8b87f29 +Subproject commit c79a41870a9d473947adb1d2fbc6687b59b9c31b diff --git a/polarssl/library/aes.c b/polarssl/library/aes.c index b7cb308..ed83434 100644 --- a/polarssl/library/aes.c +++ b/polarssl/library/aes.c @@ -179,15 +179,15 @@ static const unsigned char FSb[256] = V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C) #define V(a,b,c,d) 0x##a##b##c##d -const uint32_t FT0[256] __attribute__((section(".sys.0"))) = { FT }; +const uint32_t FT0[256] __attribute__((weak,section(".sys.0"))) = { FT }; #undef V #define V(a,b,c,d) 0x##b##c##d##a -const uint32_t FT1[256] __attribute__((section(".sys.1"))) = { FT }; +const uint32_t FT1[256] __attribute__((weak,section(".sys.1"))) = { FT }; #undef V #define V(a,b,c,d) 0x##c##d##a##b -const uint32_t FT2[256] __attribute__((section(".sys.2"))) = { FT }; +const uint32_t FT2[256] __attribute__((weak,section(".sys.2"))) = { FT }; #undef V #define V(a,b,c,d) 0x##d##a##b##c diff --git a/src/Makefile.in b/src/Makefile.in index 96d8f62..ca32b0a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -8,15 +8,14 @@ CHOPSTX = ../chopstx # Define linker script file here LDSCRIPT= gnuk.ld -CSRC = main.c usb_stm32f103.c adc_stm32f103.c \ - usb_desc.c usb_ctrl.c \ +CSRC = main.c usb_desc.c usb_ctrl.c \ call-rsa.c \ usb-ccid.c openpgp.c ac.c openpgp-do.c flash.c \ bn.c mod.c \ modp256r1.c jpc_p256r1.c ec_p256r1.c call-ec_p256r1.c \ modp256k1.c jpc_p256k1.c ec_p256k1.c call-ec_p256k1.c \ mod25638.c ecc-edwards.c ecc-mont.c sha512.c \ - random.c neug.c sha256.c sys.c + random.c neug.c sha256.c INCDIR = @@ -46,6 +45,11 @@ ifeq ($(ENABLE_PINPAD),dnd) CSRC += usb-msc.c endif +CHIP=stm32f103 +USE_SYS = yes +USE_USB = yes +USE_ADC = yes + ################################### CROSS = arm-none-eabi- CC = $(CROSS)gcc @@ -55,7 +59,7 @@ OBJCOPY = $(CROSS)objcopy MCU = cortex-m3 CWARN = -Wall -Wextra -Wstrict-prototypes # DEFS: Add -DEFS = @HAVE_SYS_H@ +DEFS = @USE_SYS3@ OPT = -O3 -Os -g LIBS = diff --git a/src/adc.h b/src/adc.h deleted file mode 100644 index 54e1f4e..0000000 --- a/src/adc.h +++ /dev/null @@ -1,8 +0,0 @@ -void adc_init (void); -void adc_start (void); -void adc_stop (void); - -extern uint32_t adc_buf[64]; - -void adc_start_conversion (int offset, int count); -int adc_wait_completion (chopstx_intr_t *intr); diff --git a/src/configure b/src/configure index 673d096..249695f 100755 --- a/src/configure +++ b/src/configure @@ -332,17 +332,18 @@ then fi if test "$sys1_compat" = "no"; then - # Disable when you are sure that it's sys version 2.1. - # Note that Gnuk 1.0 and NeuG (until 0.06) uses sys version 1.0. - # Disabling the compatibility, executable will be target independent, - # assuming the clock initialization will be done by SYS (before entry). - have_sys_h="-DHAVE_SYS_H" + # Disable when you are sure that it's sys version 3.0 or later. + # Note that Gnuk 1.0 and NeuG (until 0.06) uses sys version 1.0. + # Disabling the compatibility, executable will be target independent, + # assuming the clock initialization will be done by clock_init in + # SYS. + use_sys3="-DUSE_SYS3" else - have_sys_h="" + use_sys3="" fi -sed -e "s%@HAVE_SYS_H@%$have_sys_h%" \ +sed -e "s%@USE_SYS3@%$use_sys3%" \ -e "s%@DEBUG_MAKE_OPTION@%$DEBUG_MAKE_OPTION%" \ -e "s%@PINPAD_MAKE_OPTION@%$PINPAD_MAKE_OPTION%" \ -e "s%@HEXOUTPUT_MAKE_OPTION@%$HEXOUTPUT_MAKE_OPTION%" \ diff --git a/src/gnuk.ld.in b/src/gnuk.ld.in index 6283d7d..63a5b55 100644 --- a/src/gnuk.ld.in +++ b/src/gnuk.ld.in @@ -34,10 +34,10 @@ SECTIONS KEEP(*(.sys.version)) KEEP(*(.sys.board_id)) KEEP(*(.sys.board_name)) - build/sys.o(.text) - build/sys.o(.text.*) - build/sys.o(.rodata) - build/sys.o(.rodata.*) + build/sys-*.o(.text) + build/sys-*.o(.text.*) + build/sys-*.o(.rodata) + build/sys-*.o(.rodata.*) . = ALIGN(1024); *(.sys.0) *(.sys.1) diff --git a/src/main.c b/src/main.c index 6604797..4098af0 100644 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,7 @@ #include "usb_lld.h" #include "usb-cdc.h" #include "random.h" -#include "stm32f103.h" +#include "mcu/stm32f103.h" /* diff --git a/src/neug.c b/src/neug.c index d3f3ee4..056e018 100644 --- a/src/neug.c +++ b/src/neug.c @@ -1,7 +1,8 @@ /* * neug.c - true random number generation * - * Copyright (C) 2011, 2012, 2013 Free Software Initiative of Japan + * Copyright (C) 2011, 2012, 2013, 2016 + * Free Software Initiative of Japan * Author: NIIBE Yutaka * * This file is a part of NeuG, a True Random Number Generator @@ -28,19 +29,13 @@ #include "sys.h" #include "neug.h" -#include "stm32f103.h" +#include "mcu/stm32f103.h" #include "adc.h" #include "sha256.h" static chopstx_mutex_t mode_mtx; static chopstx_cond_t mode_cond; -/* - * ADC finish interrupt - */ -#define INTR_REQ_DMA1_Channel1 11 - - static sha256_context sha256_ctx_data; static uint32_t sha256_output[SHA256_DIGEST_SIZE/sizeof (uint32_t)]; @@ -574,7 +569,6 @@ static void * rng (void *arg) { struct rng_rb *rb = (struct rng_rb *)arg; - chopstx_intr_t adc_intr; int mode = neug_mode; rng_should_terminate = 0; @@ -582,7 +576,6 @@ rng (void *arg) chopstx_cond_init (&mode_cond); /* Enable ADCs */ - chopstx_claim_irq (&adc_intr, INTR_REQ_DMA1_Channel1); adc_start (); ep_init (mode); @@ -591,7 +584,7 @@ rng (void *arg) int err; int n; - err = adc_wait_completion (&adc_intr); + err = adc_wait_completion (); chopstx_mutex_lock (&mode_mtx); if (err || mode != neug_mode) diff --git a/src/sys.h b/src/sys.h deleted file mode 100644 index aa7825d..0000000 --- a/src/sys.h +++ /dev/null @@ -1,130 +0,0 @@ -#define BOARD_ID_CQ_STARM 0xc5480875 -#define BOARD_ID_FST_01_00 0x613870a9 -#define BOARD_ID_FST_01 0x696886af -#define BOARD_ID_MAPLE_MINI 0x7a445272 -#define BOARD_ID_OLIMEX_STM32_H103 0xf92bb594 -#define BOARD_ID_STBEE_MINI 0x1f341961 -#define BOARD_ID_STBEE 0x945c37e8 -#define BOARD_ID_STM32_PRIMER2 0x21e5798d -#define BOARD_ID_STM8S_DISCOVERY 0x2f0976bb -#define BOARD_ID_ST_DONGLE 0x2cd4e471 -#define BOARD_ID_ST_NUCLEO_F103 0x9b87c16d -#define BOARD_ID_NITROKEY_START 0xad1e7ebd - -extern const uint8_t sys_version[8]; -extern const uint32_t sys_board_id; -extern const uint8_t sys_board_name[]; - -typedef void (*handler)(void); -extern handler vector[16]; - -static inline const uint8_t * -unique_device_id (void) -{ - /* STM32F103 has 96-bit unique device identifier */ - const uint8_t *addr = (const uint8_t *)0x1ffff7e8; - - return addr; -} - -static inline void -set_led (int on) -{ - void (*func) (int) = (void (*)(int))vector[2]; - - return (*func) (on); -} - -static inline void -flash_unlock (void) -{ - (*vector[3]) (); -} - -static inline int -flash_program_halfword (uint32_t addr, uint16_t data) -{ - int (*func) (uint32_t, uint16_t) = (int (*)(uint32_t, uint16_t))vector[4]; - - return (*func) (addr, data); -} - -static inline int -flash_erase_page (uint32_t addr) -{ - int (*func) (uint32_t) = (int (*)(uint32_t))vector[5]; - - return (*func) (addr); -} - -static inline int -flash_check_blank (const uint8_t *p_start, size_t size) -{ - int (*func) (const uint8_t *, int) = (int (*)(const uint8_t *, int))vector[6]; - - return (*func) (p_start, size); -} - -static inline int -flash_write (uint32_t dst_addr, const uint8_t *src, size_t len) -{ - int (*func) (uint32_t, const uint8_t *, size_t) - = (int (*)(uint32_t, const uint8_t *, size_t))vector[7]; - - return (*func) (dst_addr, src, len); -} - -static inline int -flash_protect (void) -{ - int (*func) (void) = (int (*)(void))vector[8]; - - return (*func) (); -} - -static inline void __attribute__((noreturn)) -flash_erase_all_and_exec (void (*entry)(void)) -{ - void (*func) (void (*)(void)) = (void (*)(void (*)(void)))vector[9]; - - (*func) (entry); - for (;;); -} - -static inline void -usb_lld_sys_init (void) -{ - (*vector[10]) (); -} - -static inline void -usb_lld_sys_shutdown (void) -{ - (*vector[11]) (); -} - -static inline void -nvic_system_reset (void) -{ - (*vector[12]) (); -} - -/* - * Users can override INLINE by 'attribute((used))' to have an - * implementation defined. - */ -#if !defined(INLINE) -#define INLINE __inline__ -#endif - -static INLINE void -clock_init (void) -{ - (*vector[13]) (); -} - -static INLINE void -gpio_init (void) -{ - (*vector[14]) (); -} diff --git a/src/usb-ccid.c b/src/usb-ccid.c index 1dc2562..2b16938 100644 --- a/src/usb-ccid.c +++ b/src/usb-ccid.c @@ -693,8 +693,9 @@ usb_cb_rx_ready (uint8_t ep_num) } void -usb_cb_tx_done (uint8_t ep_num) +usb_cb_tx_done (uint8_t ep_num, uint32_t len) { + (void)len; if (ep_num == ENDP1) EP1_IN_Callback (); else if (ep_num == ENDP2) @@ -1395,6 +1396,13 @@ ccid_thread (void *arg) apdu_init (a); ccid_init (c, epi, epo, a); + while (bDeviceState != CONFIGURED) + { + chopstx_poll (NULL, 1, &interrupt); + usb_interrupt_handler (); + } + + ccid.ccid_comm.flags = 0; timeout = USB_ICC_TIMEOUT; icc_prepare_receive (c); while (1) diff --git a/src/usb_ctrl.c b/src/usb_ctrl.c index 42f6529..fc723a4 100644 --- a/src/usb_ctrl.c +++ b/src/usb_ctrl.c @@ -38,7 +38,7 @@ #include "usb_lld.h" #include "usb_conf.h" #include "gnuk.h" -#include "stm32f103.h" +#include "mcu/stm32f103.h" #ifdef ENABLE_VIRTUAL_COM_PORT #include "usb-cdc.h" diff --git a/src/usb_desc.c b/src/usb_desc.c index 361a605..1a26796 100644 --- a/src/usb_desc.c +++ b/src/usb_desc.c @@ -360,6 +360,7 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, return usb_lld_reply_request (string_descriptors[desc_index].desc, string_descriptors[desc_index].size, arg); +#ifdef USE_SYS3 else if (desc_index == NUM_STRING_DESC) { uint8_t usbbuf[64]; @@ -378,6 +379,7 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, usbbuf[1] = STRING_DESCRIPTOR; return usb_lld_reply_request (usbbuf, len, arg); } +#endif } } #ifdef HID_CARD_CHANGE_SUPPORT diff --git a/src/usb_lld.h b/src/usb_lld.h deleted file mode 100644 index 3710f20..0000000 --- a/src/usb_lld.h +++ /dev/null @@ -1,118 +0,0 @@ -#define STANDARD_ENDPOINT_DESC_SIZE 0x09 - -/* endpoints enumeration */ -#define ENDP0 ((uint8_t)0) -#define ENDP1 ((uint8_t)1) -#define ENDP2 ((uint8_t)2) -#define ENDP3 ((uint8_t)3) -#define ENDP4 ((uint8_t)4) -#define ENDP5 ((uint8_t)5) -#define ENDP6 ((uint8_t)6) -#define ENDP7 ((uint8_t)7) - -/* EP_TYPE[1:0] EndPoint TYPE */ -#define EP_BULK (0x0000) /* EndPoint BULK */ -#define EP_CONTROL (0x0200) /* EndPoint CONTROL */ -#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */ -#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */ - -enum RECIPIENT_TYPE -{ - DEVICE_RECIPIENT = 0, /* Recipient device */ - INTERFACE_RECIPIENT, /* Recipient interface */ - ENDPOINT_RECIPIENT, /* Recipient endpoint */ - OTHER_RECIPIENT -}; - -enum DESCRIPTOR_TYPE -{ - DEVICE_DESCRIPTOR = 1, - CONFIG_DESCRIPTOR, - STRING_DESCRIPTOR, - INTERFACE_DESCRIPTOR, - ENDPOINT_DESCRIPTOR -}; - -#define REQUEST_DIR 0x80 /* Mask to get request dir */ -#define REQUEST_TYPE 0x60 /* Mask to get request type */ -#define STANDARD_REQUEST 0x00 /* Standard request */ -#define CLASS_REQUEST 0x20 /* Class request */ -#define VENDOR_REQUEST 0x40 /* Vendor request */ -#define RECIPIENT 0x1F /* Mask to get recipient */ - -#define USB_SETUP_SET(req) ((req & REQUEST_DIR) == 0) -#define USB_SETUP_GET(req) ((req & REQUEST_DIR) != 0) - -enum -{ - USB_UNSUPPORT = 0, - USB_SUCCESS = 1, -}; - -struct req_args { - uint16_t value; - uint16_t index; - uint16_t len; -}; - -void usb_cb_device_reset (void); -int usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg); -int usb_cb_interface (uint8_t cmd, struct req_args *arg); -int usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, - struct req_args *arg); -int usb_cb_handle_event (uint8_t event_type, uint16_t value); -void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, - struct req_args *arg); -void usb_cb_tx_done (uint8_t ep_num); -void usb_cb_rx_ready (uint8_t ep_num); - -enum { - USB_EVENT_ADDRESS, - USB_EVENT_CONFIG, - USB_EVENT_SUSPEND, - USB_EVENT_WAKEUP, - USB_EVENT_STALL, -}; - -enum { - USB_SET_INTERFACE, - USB_GET_INTERFACE, - USB_QUERY_INTERFACE, -}; - -enum DEVICE_STATE -{ - UNCONNECTED, - ATTACHED, - POWERED, - SUSPENDED, - ADDRESSED, - CONFIGURED -}; - -void usb_lld_init (uint8_t feature); -void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n); -void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n); -void usb_lld_stall_tx (int ep_num); -void usb_lld_stall_rx (int ep_num); -int usb_lld_tx_data_len (int ep_num); -void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len); -void usb_lld_tx_enable (int ep_num, size_t len); -void usb_lld_write (uint8_t ep_num, const void *buf, size_t len); -int usb_lld_reply_request (const void *buf, size_t buflen, - struct req_args *arg); -void usb_lld_rx_enable (int ep_num); -int usb_lld_rx_data_len (int ep_num); -void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len); -void usb_lld_reset (uint8_t feature); -void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind, - int ep_rx_addr, int ep_tx_addr, - int ep_rx_memory_size); -void usb_lld_set_configuration (uint8_t config); -uint8_t usb_lld_current_configuration (void); -void usb_lld_set_data_to_recv (void *p, size_t len); - -void usb_lld_prepare_shutdown (void); -void usb_lld_shutdown (void); - -void usb_interrupt_handler (void);