use functions in sys

This commit is contained in:
NIIBE Yutaka
2012-05-25 15:20:08 +09:00
parent 8c6ffaa167
commit 6b47ee56b8
22 changed files with 431 additions and 667 deletions

View File

@@ -76,15 +76,14 @@ CSRC = $(PORTSRC) \
$(HALSRC) \
$(PLATFORMSRC) \
$(BOARDSRC) \
../boards/common/hw_config.c \
$(BOARD_DIR)/board.c \
$(CHIBIOS)/os/various/evtimer.c \
$(CHIBIOS)/os/various/syscalls.c \
$(CRYPTSRC) \
main.c usb_lld.c usb_lld_sys.c \
main.c usb_lld.c \
usb_desc.c usb_ctrl.c \
usb-icc.c openpgp.c ac.c openpgp-do.c flash.c \
random.c neug.c
random.c neug.c sys.c
ifneq ($(ENABLE_DEBUG),)
CSRC += debug.c

3
src/configure vendored
View File

@@ -135,7 +135,6 @@ fi
BOARD_DIR=../boards/$target
if test -d $BOARD_DIR; then
echo "Configured for target: $target"
TARGET_LOWER=`echo $target | tr '[:upper:]_' '[:lower:]-'`
else
echo "Unsupported target \`$target'" >&2
exit 1
@@ -219,8 +218,6 @@ else
echo "CERT.3 Data Object is not supported"
fi
ln -sf sys-$TARGET_LOWER.h ../regnual/sys.h
sed -e "s%@BOARD_DIR@%$BOARD_DIR%" \
-e "s%@DEBUG_MAKE_OPTION@%$DEBUG_MAKE_OPTION%" \
-e "s%@PINPAD_MAKE_OPTION@%$PINPAD_MAKE_OPTION%" \

View File

@@ -32,108 +32,9 @@
#include "config.h"
#include "ch.h"
#include "hal.h"
#include "sys.h"
#include "gnuk.h"
#define FLASH_KEY1 0x45670123UL
#define FLASH_KEY2 0xCDEF89ABUL
enum flash_status
{
FLASH_BUSY = 1,
FLASH_ERROR_PG,
FLASH_ERROR_WRP,
FLASH_COMPLETE,
FLASH_TIMEOUT
};
void
flash_unlock (void)
{
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
}
static int
flash_get_status (void)
{
int status;
if ((FLASH->SR & FLASH_SR_BSY) != 0)
status = FLASH_BUSY;
else if ((FLASH->SR & FLASH_SR_PGERR) != 0)
status = FLASH_ERROR_PG;
else if((FLASH->SR & FLASH_SR_WRPRTERR) != 0)
status = FLASH_ERROR_WRP;
else
status = FLASH_COMPLETE;
return status;
}
static int
flash_wait_for_last_operation (uint32_t timeout)
{
int status;
do
if (--timeout == 0)
return FLASH_TIMEOUT;
else
status = flash_get_status ();
while (status == FLASH_BUSY);
return status;
}
#define FLASH_PROGRAM_TIMEOUT 0x00010000
#define FLASH_ERASE_TIMEOUT 0x01000000
static int
flash_program_halfword (uint32_t addr, uint16_t data)
{
int status;
status = flash_wait_for_last_operation (FLASH_PROGRAM_TIMEOUT);
chSysLock ();
if (status == FLASH_COMPLETE)
{
FLASH->CR |= FLASH_CR_PG;
*(volatile uint16_t *)addr = data;
status = flash_wait_for_last_operation (FLASH_PROGRAM_TIMEOUT);
if (status != FLASH_TIMEOUT)
FLASH->CR &= ~FLASH_CR_PG;
}
chSysUnlock ();
return status;
}
static int
flash_erase_page (uint32_t addr)
{
int status;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
chSysLock ();
if (status == FLASH_COMPLETE)
{
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = addr;
FLASH->CR |= FLASH_CR_STRT;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
if (status != FLASH_TIMEOUT)
FLASH->CR &= ~FLASH_CR_PER;
}
chSysUnlock ()
return status;
}
/*
* Flash memory map
*
@@ -294,14 +195,14 @@ flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len)
addr = (uint32_t)p;
hw = nr | (len << 8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
flash_warning ("DO WRITE ERROR");
addr += 2;
for (i = 0; i < len/2; i++)
{
hw = data[i*2] | (data[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
flash_warning ("DO WRITE ERROR");
addr += 2;
}
@@ -309,7 +210,7 @@ flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len)
if ((len & 1))
{
hw = data[i*2] | 0xff00;
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
flash_warning ("DO WRITE ERROR");
}
}
@@ -359,19 +260,19 @@ flash_do_release (const uint8_t *do_data)
/* Fill zero for content and pad */
for (i = 0; i < len/2; i ++)
{
if (flash_program_halfword (addr, 0) != FLASH_COMPLETE)
if (flash_program_halfword (addr, 0) != 0)
flash_warning ("fill-zero failure");
addr += 2;
}
if ((len & 1))
{
if (flash_program_halfword (addr, 0) != FLASH_COMPLETE)
if (flash_program_halfword (addr, 0) != 0)
flash_warning ("fill-zero pad failure");
}
/* Fill 0x0000 for "tag_number and length" word */
if (flash_program_halfword (addr_tag, 0) != FLASH_COMPLETE)
if (flash_program_halfword (addr_tag, 0) != 0)
flash_warning ("fill-zero tag_nr failure");
}
@@ -399,7 +300,7 @@ flash_key_write (uint8_t *key_addr, const uint8_t *key_data,
for (i = 0; i < KEY_CONTENT_LEN/2; i ++)
{
hw = key_data[i*2] | (key_data[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
return -1;
addr += 2;
}
@@ -407,7 +308,7 @@ flash_key_write (uint8_t *key_addr, const uint8_t *key_data,
for (i = 0; i < KEY_CONTENT_LEN/2; i ++)
{
hw = modulus[i*2] | (modulus[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
return -1;
addr += 2;
}
@@ -579,18 +480,6 @@ flash_cnt123_clear (const uint8_t **addr_p)
}
static int
flash_check_blank (const uint8_t *p_start, int size)
{
const uint8_t *p;
for (p = p_start; p < p_start + size; p++)
if (*p != 0xff)
return 0;
return 1;
}
#if defined(CERTDO_SUPPORT)
#define FLASH_CH_CERTIFICATE_SIZE 2048
int
@@ -652,7 +541,7 @@ flash_write_binary (uint8_t file_id, const uint8_t *data,
for (i = 0; i < len/2; i++)
{
hw = data[i*2] | (data[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
if (flash_program_halfword (addr, hw) != 0)
flash_warning ("DO WRITE ERROR");
addr += 2;
}

View File

@@ -129,7 +129,6 @@ enum kind_of_key {
GPG_KEY_FOR_AUTHENTICATION,
};
extern void flash_unlock (void);
extern const uint8_t *flash_init (void);
extern void flash_do_release (const uint8_t *);
extern const uint8_t *flash_do_write (uint8_t nr, const uint8_t *data, int len);
@@ -322,8 +321,6 @@ extern uint32_t get_salt (void);
extern uint32_t hardclock (void);
extern void set_led (int);
#define NUM_ALL_PRV_KEYS 3 /* SIG, DEC and AUT */
extern uint8_t pw1_keystring[KEYSTRING_SIZE_PW1];
@@ -351,7 +348,6 @@ extern void flash_bool_write_internal (const uint8_t *p, int nr);
extern void flash_cnt123_write_internal (const uint8_t *p, int which, int v);
extern void flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len);
extern const unsigned char *unique_device_id (void);
extern const uint8_t gnukStringSerial[];
#define LED_ONESHOT_SHORT ((eventmask_t)1)

View File

@@ -24,6 +24,7 @@
#include "config.h"
#include "ch.h"
#include "hal.h"
#include "sys.h"
#include "gnuk.h"
#include "usb_lld.h"
#include "usb-cdc.h"
@@ -370,55 +371,6 @@ led_blink (int spec)
}
#define FMEAE_SIZE 128
#define FLASH_MASS_ERASE_TIMEOUT 0xF0000000
static void __attribute__((naked))
flash_mass_erase_and_exec (void)
{
void (**func )(void) = (void (**)(void))(&_regnual_start + 4);
if ((FLASH->SR & FLASH_SR_BSY) == 0)
{
FLASH->CR |= FLASH_CR_MER;
FLASH->CR |= FLASH_CR_STRT;
while ((FLASH->SR & FLASH_SR_BSY) != 0)
;
FLASH->CR &= ~FLASH_CR_MER;
if ((FLASH->SR & (FLASH_SR_BSY|FLASH_SR_PGERR|FLASH_SR_WRPRTERR)) == 0)
(**func) ();
}
for (;;);
}
static void __attribute__((noreturn))
good_bye (void)
{
register uint32_t dst __asm__ ("r0") = 0x20000000; /* SRAM top */
register uint32_t src __asm__ ("r1") = (uint32_t)flash_mass_erase_and_exec & ~1;
register uint32_t len __asm__ ("r2") = FMEAE_SIZE;
register uint32_t entry __asm__ ("r3") = dst | 1;
register uint32_t use __asm__ ("r4");
/* copy function flash_mass_erase_and_exec to SRAM and jump to it */
asm volatile("add r2, r2, r1\n"
"0: ldr r4, [r1], #4\n\t"
"cmp r2, r1\n\t"
"str r4, [r0], #4\n\t"
"bhi 0b\n\t"
"isb\n\t"
"bx r3"
: "=r" (dst), "=r" (src), "=r" (len), "=r" (use)
: "0" (dst), "1" (src), "2" (len), "r" (entry)
: "memory");
for (;;);
}
/*
* Entry point.
*
@@ -524,15 +476,12 @@ main (int argc, char *argv[])
}
set_led (1);
/* USB Dissconnect (when supported) */
usb_lld_shutdown ();
USB_Cable_Config (0);
chThdSleep (MS2ST (1)); /* > 2.5us required */
port_disable ();
/* set vector */
SCB->VTOR = (uint32_t)&_regnual_start;
/* leave Gnuk */
good_bye ();
flash_mass_erase_and_exec ();
/* Never reached */
return 0;

View File

@@ -25,6 +25,7 @@
#include "config.h"
#include "ch.h"
#include "sys.h"
#include "gnuk.h"
#include "openpgp.h"

276
src/sys.c Normal file
View File

@@ -0,0 +1,276 @@
#include "ch.h"
#include "hal.h"
#include "board.h"
#include "usb_lld.h"
static 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 void
usb_cable_config (int enable)
{
#if defined(SET_USB_CONDITION)
if (SET_USB_CONDITION (enable))
palSetPad (IOPORT_USB, GPIO_USB);
else
palClearPad (IOPORT_USB, GPIO_USB);
#else
(void)enable;
#endif
}
static void
set_led (int on)
{
if (SET_LED_CONDITION (on))
palSetPad (IOPORT_LED, GPIO_LED);
else
palClearPad (IOPORT_LED, GPIO_LED);
}
#define FLASH_KEY1 0x45670123UL
#define FLASH_KEY2 0xCDEF89ABUL
static void
flash_unlock (void)
{
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
}
static int
flash_wait_for_last_operation (uint32_t timeout)
{
int status;
do
{
status = FLASH->SR;
if (--timeout == 0)
break;
}
while ((status & FLASH_SR_BSY) != 0);
return status & (FLASH_SR_BSY|FLASH_SR_PGERR|FLASH_SR_WRPRTERR);
}
#define FLASH_PROGRAM_TIMEOUT 0x00010000
#define FLASH_ERASE_TIMEOUT 0x01000000
static int
flash_program_halfword (uint32_t addr, uint16_t data)
{
int status;
status = flash_wait_for_last_operation (FLASH_PROGRAM_TIMEOUT);
port_disable ();
if (status == 0)
{
FLASH->CR |= FLASH_CR_PG;
*(volatile uint16_t *)addr = data;
status = flash_wait_for_last_operation (FLASH_PROGRAM_TIMEOUT);
FLASH->CR &= ~FLASH_CR_PG;
}
port_enable ();
return status;
}
static int
flash_erase_page (uint32_t addr)
{
int status;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
port_disable ();
if (status == 0)
{
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = addr;
FLASH->CR |= FLASH_CR_STRT;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
FLASH->CR &= ~FLASH_CR_PER;
}
port_enable ();
return status;
}
static int
flash_check_blank (const uint8_t *p_start, size_t size)
{
const uint8_t *p;
for (p = p_start; p < p_start + size; p++)
if (*p != 0xff)
return 0;
return 1;
}
static int
flash_write (uint32_t dst_addr, const uint8_t *src, size_t len)
{
int status;
while (len)
{
uint16_t hw = *src++;
hw |= (*src++ << 8);
status = flash_program_halfword (dst_addr, hw);
if (status != 0)
return 0; /* error return */
dst_addr += 2;
len -= 2;
}
return 1;
}
#define OPTION_BYTES_ADDR 0x1ffff800
static int
flash_protect (void)
{
int status;
uint32_t option_bytes_value;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
port_disable ();
if (status == 0)
{
FLASH->OPTKEYR = FLASH_KEY1;
FLASH->OPTKEYR = FLASH_KEY2;
FLASH->CR |= FLASH_CR_OPTER;
FLASH->CR |= FLASH_CR_STRT;
status = flash_wait_for_last_operation (FLASH_ERASE_TIMEOUT);
FLASH->CR &= ~FLASH_CR_OPTER;
}
port_enable ();
if (status != 0)
return 0;
option_bytes_value = *(uint32_t *)OPTION_BYTES_ADDR;
return (option_bytes_value & 0xff) == 0xff ? 1 : 0;
}
#define FLASH_MASS_ERASE_TIMEOUT 0xF0000000
extern uint8_t __flash_start__, __flash_end__;
extern uint8_t _regnual_start;
static void __attribute__((naked))
flash_mass_erase_and_exec (void)
{
void (**func )(void) = (void (**)(void))(&_regnual_start + 4);
uint32_t addr = (uint32_t)&__flash_start__;
uint32_t end = (uint32_t)&__flash_end__;
int r;
while (addr < end)
{
r = flash_erase_page (addr);
if (r != 0)
break;
addr += FLASH_PAGE_SIZE;
}
if (addr >= end)
(**func) ();
for (;;);
}
static void
nvic_enable_vector (uint32_t n, uint32_t prio)
{
unsigned int sh = (n & 3) << 3;
NVIC_IPR (n >> 2) = (NVIC_IPR(n >> 2) & ~(0xFF << sh)) | (prio << sh);
NVIC_ICPR (n >> 5) = 1 << (n & 0x1F);
NVIC_ISER (n >> 5) = 1 << (n & 0x1F);
}
static void
usb_lld_sys_init (void)
{
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
nvic_enable_vector (USB_LP_CAN1_RX0_IRQn,
CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY));
/*
* Note that we also have other IRQ(s):
* USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous)
* USBWakeUp_IRQn (suspend/resume)
*/
RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
RCC->APB1RSTR = 0;
usb_cable_config (1);
}
static void
usb_lld_sys_shutdown (void)
{
RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
usb_cable_config (0);
}
#define SYSRESETREQ 0x04
static void
nvic_system_reset (void)
{
SCB->AIRCR = (0x05FA0000 | (SCB->AIRCR & 0x70) | SYSRESETREQ);
asm volatile ("dsb");
}
static void __attribute__ ((naked))
reset (void)
{
asm volatile ("cpsid i\n\t" /* Mask all interrupts */
"ldr r0, =_text\n\t"
"ldr r1, [r0], #4\n\t"
"msr MSP, r1\n\t" /* Main (exception handler) stack */
"ldr r1, [r0]\n\t" /* Reset handler */
"bx r1\n"
: /* no output */ : /* no input */ : "memory");
}
typedef void (*handler)(void);
extern uint8_t __ram_end__;
handler vector[] __attribute__ ((section(".vectors"))) = {
(handler)&__ram_end__,
reset,
(handler)unique_device_id,
(handler)set_led,
flash_unlock,
(handler)flash_program_halfword,
(handler)flash_erase_page,
(handler)flash_check_blank,
(handler)flash_write,
(handler)flash_protect,
(handler)flash_mass_erase_and_exec,
usb_lld_sys_init,
usb_lld_sys_shutdown,
nvic_system_reset,
};

92
src/sys.h Normal file
View File

@@ -0,0 +1,92 @@
typedef void (*handler)(void);
extern handler vector[12];
static inline const uint8_t *
unique_device_id (void)
{
const uint8_t * (*func) (void) = (const uint8_t * (*)(void))vector[0];
return (*func) ();
}
static inline void
set_led (int on)
{
void (*func) (int) = (void (*)(int))vector[1];
return (*func) (on);
}
static inline void
flash_unlock (void)
{
(*vector[2]) ();
}
static inline int
flash_program_halfword (uint32_t addr, uint16_t data)
{
int (*func) (uint32_t, uint16_t) = (int (*)(uint32_t, uint16_t))vector[3];
return (*func) (addr, data);
}
static inline int
flash_erase_page (uint32_t addr)
{
int (*func) (uint32_t) = (int (*)(uint32_t))vector[4];
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[5];
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[6];
return (*func) (dst_addr, src, len);
}
static inline int
flash_protect (void)
{
int (*func) (void) = (int (*)(void))vector[7];
return (*func) ();
}
static inline void __attribute__((noreturn))
flash_mass_erase_and_exec (void)
{
void (*func) (void) = (void (*)(void))vector[8];
(*func) ();
for (;;);
}
static inline void
usb_lld_sys_init (void)
{
(*vector[9]) ();
}
static inline void
usb_lld_sys_shutdown (void)
{
(*vector[10]) ();
}
static inline void
nvic_system_reset (void)
{
(*vector[11]) ();
}

View File

@@ -423,3 +423,14 @@ const struct usb_device_method Device_Method = {
gnuk_usb_event,
gnuk_interface,
};
CH_IRQ_HANDLER (Vector90)
{
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
usb_interrupt_handler ();
chSysUnlockFromIsr();
CH_IRQ_EPILOGUE();
}

View File

@@ -4,7 +4,7 @@
#include "ch.h"
#include "hal.h"
#endif
#include "sys.h"
#include "usb_lld.h"
#define USB_MAX_PACKET_SIZE 64 /* For FS device */

View File

@@ -156,6 +156,3 @@ extern void usb_lld_prepare_shutdown (void);
extern void usb_lld_shutdown (void);
extern void usb_interrupt_handler (void);
extern void usb_lld_sys_init (void);
extern void usb_lld_sys_shutdown (void);

View File

@@ -1,34 +0,0 @@
#include "ch.h"
#include "hal.h"
#include "usb_lld.h"
CH_IRQ_HANDLER (Vector90) {
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
usb_interrupt_handler ();
chSysUnlockFromIsr();
CH_IRQ_EPILOGUE();
}
void usb_lld_sys_init (void)
{
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
NVICEnableVector (USB_LP_CAN1_RX0_IRQn,
CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY));
/*
* Note that we also have other IRQ(s):
* USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous)
* USBWakeUp_IRQn (suspend/resume)
*/
RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
RCC->APB1RSTR = 0;
USB_Cable_Config (1);
}
void usb_lld_sys_shutdown (void)
{
RCC->APB1ENR &= ~RCC_APB1ENR_USBEN;
}