gnuk/src/main.c

470 lines
9.7 KiB
C
Raw Normal View History

2010-08-10 03:11:02 +00:00
/*
2010-08-30 02:39:10 +00:00
* main.c - main routine of Gnuk
*
2012-05-11 23:06:33 +00:00
* Copyright (C) 2010, 2011, 2012 Free Software Initiative of Japan
2010-08-30 02:39:10 +00:00
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
2010-08-10 03:11:02 +00:00
2010-09-04 04:48:26 +00:00
#include "config.h"
2010-09-09 00:51:09 +00:00
#include "ch.h"
2012-05-18 17:05:31 +00:00
#include "hal.h"
2012-05-25 06:20:08 +00:00
#include "sys.h"
2010-08-26 10:50:06 +00:00
#include "gnuk.h"
2010-09-09 00:51:09 +00:00
#include "usb_lld.h"
2012-05-11 00:28:04 +00:00
#include "usb-cdc.h"
2010-08-10 03:11:02 +00:00
2010-09-03 15:42:36 +00:00
#ifdef DEBUG
2010-08-10 07:30:05 +00:00
struct stdout {
Mutex m;
CondVar start_cnd;
CondVar finish_cnd;
const char *str;
int size;
2010-09-04 04:48:26 +00:00
};
static struct stdout stdout;
2010-08-10 07:30:05 +00:00
static void
stdout_init (void)
{
chMtxInit (&stdout.m);
chCondInit (&stdout.start_cnd);
chCondInit (&stdout.finish_cnd);
stdout.size = 0;
stdout.str = NULL;
}
2010-08-26 10:50:06 +00:00
void
2010-08-10 07:30:05 +00:00
_write (const char *s, int size)
{
2010-08-10 08:47:55 +00:00
if (size == 0)
2010-08-26 10:50:06 +00:00
return;
2010-08-10 08:47:55 +00:00
2010-08-10 07:30:05 +00:00
chMtxLock (&stdout.m);
2010-08-18 08:02:04 +00:00
while (stdout.str)
2010-08-10 07:30:05 +00:00
chCondWait (&stdout.finish_cnd);
stdout.str = s;
stdout.size = size;
chCondSignal (&stdout.start_cnd);
chCondWait (&stdout.finish_cnd);
chMtxUnlock ();
}
2010-09-08 05:24:12 +00:00
Thread *stdout_thread;
uint32_t count_in;
uint8_t buffer_in[VIRTUAL_COM_PORT_DATA_SIZE];
2010-08-10 06:35:34 +00:00
2010-09-03 15:42:36 +00:00
static WORKING_AREA(waSTDOUTthread, 128);
2010-09-08 05:24:12 +00:00
2010-09-04 04:48:26 +00:00
static msg_t
STDOUTthread (void *arg)
2010-08-10 07:30:05 +00:00
{
(void)arg;
2010-09-08 05:24:12 +00:00
stdout_thread = chThdSelf ();
2010-08-10 07:30:05 +00:00
again:
while (1)
{
if (bDeviceState == CONFIGURED)
break;
chThdSleepMilliseconds (100);
}
while (1)
{
2010-08-10 08:47:55 +00:00
const char *p;
int len;
2010-08-10 07:30:05 +00:00
if (bDeviceState != CONFIGURED)
break;
chMtxLock (&stdout.m);
if (stdout.str == NULL)
chCondWait (&stdout.start_cnd);
2010-08-10 08:47:55 +00:00
p = stdout.str;
len = stdout.size;
while (1)
2010-08-10 08:47:55 +00:00
{
int i;
if (len == 0)
if (count_in != VIRTUAL_COM_PORT_DATA_SIZE)
break;
2010-08-10 08:47:55 +00:00
if (len < VIRTUAL_COM_PORT_DATA_SIZE)
{
for (i = 0; i < len; i++)
buffer_in[i] = p[i];
count_in = len;
len = 0;
}
else
{
for (i = 0; i < VIRTUAL_COM_PORT_DATA_SIZE; i++)
buffer_in[i] = p[i];
len -= VIRTUAL_COM_PORT_DATA_SIZE;
count_in = VIRTUAL_COM_PORT_DATA_SIZE;
p += count_in;
}
2010-09-08 05:24:12 +00:00
chEvtClear (EV_TX_READY);
2012-01-16 04:24:31 +00:00
usb_lld_write (ENDP3, buffer_in, count_in);
2010-08-10 08:47:55 +00:00
2010-09-08 05:24:12 +00:00
chEvtWaitOne (EV_TX_READY);
2010-08-10 08:47:55 +00:00
}
2010-08-10 07:30:05 +00:00
stdout.str = NULL;
stdout.size = 0;
2010-08-18 08:02:04 +00:00
chCondBroadcast (&stdout.finish_cnd);
2010-08-10 07:30:05 +00:00
chMtxUnlock ();
}
goto again;
return 0;
}
2012-05-15 01:16:25 +00:00
void
EP3_IN_Callback (void)
{
if (stdout_thread)
chEvtSignalI (stdout_thread, EV_TX_READY);
}
void
EP5_OUT_Callback (void)
{
usb_lld_rx_enable (ENDP5);
}
2010-09-03 15:42:36 +00:00
#else
void
_write (const char *s, int size)
{
(void)s;
(void)size;
}
#endif
2010-08-10 07:30:05 +00:00
2010-09-04 04:48:26 +00:00
static WORKING_AREA(waUSBthread, 128);
2010-08-19 08:09:59 +00:00
extern msg_t USBthread (void *arg);
2010-09-03 15:42:36 +00:00
/*
2010-12-09 01:12:54 +00:00
* main thread does 1-bit LED display output
2010-09-03 15:42:36 +00:00
*/
2012-06-18 03:24:54 +00:00
#define MAIN_TIMEOUT_INTERVAL MS2ST(5000)
#define LED_TIMEOUT_INTERVAL MS2ST(75)
#define LED_TIMEOUT_ZERO MS2ST(25)
#define LED_TIMEOUT_ONE MS2ST(100)
#define LED_TIMEOUT_STOP MS2ST(200)
2010-09-08 05:24:12 +00:00
2010-09-03 15:42:36 +00:00
2013-02-15 02:45:52 +00:00
/* It has two-byte prefix and content is "FSIJ-1.0.1-" (2 + 11*2). */
2012-08-03 02:20:13 +00:00
#define ID_OFFSET 24
2011-05-11 01:56:36 +00:00
static void
device_initialize_once (void)
{
const uint8_t *p = &gnukStringSerial[ID_OFFSET];
if (p[0] == 0xff && p[1] == 0xff && p[2] == 0xff && p[3] == 0xff)
{
/*
* This is the first time invocation.
* Setup serial number by unique device ID.
*/
const uint8_t *u = unique_device_id ();
int i;
for (i = 0; i < 4; i++)
{
uint8_t b = u[i];
2012-06-18 03:24:54 +00:00
uint8_t nibble;
2011-05-11 01:56:36 +00:00
nibble = (b >> 4);
nibble += (nibble >= 10 ? ('A' - 10) : '0');
2011-05-11 07:47:26 +00:00
flash_put_data_internal (&p[i*4], nibble);
2011-05-11 01:56:36 +00:00
nibble = (b & 0x0f);
nibble += (nibble >= 10 ? ('A' - 10) : '0');
2011-05-11 07:47:26 +00:00
flash_put_data_internal (&p[i*4+2], nibble);
2011-05-11 01:56:36 +00:00
}
}
}
2010-12-09 01:12:54 +00:00
static volatile uint8_t fatal_code;
2012-06-18 03:24:54 +00:00
static Thread *main_thread;
2011-11-01 05:57:11 +00:00
2012-06-18 03:24:54 +00:00
static void display_fatal_code (void)
2011-11-01 05:57:11 +00:00
{
while (1)
{
2012-06-18 03:24:54 +00:00
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
2012-06-08 00:48:40 +00:00
set_led (0);
2012-06-18 03:24:54 +00:00
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_STOP);
2012-06-08 00:48:40 +00:00
set_led (1);
2012-06-18 03:24:54 +00:00
if (fatal_code & 1)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
if (fatal_code & 2)
chThdSleep (LED_TIMEOUT_ONE);
else
chThdSleep (LED_TIMEOUT_ZERO);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL);
set_led (1);
chThdSleep (LED_TIMEOUT_STOP);
set_led (0);
chThdSleep (LED_TIMEOUT_INTERVAL*10);
2011-11-01 05:57:11 +00:00
}
}
2012-06-18 03:24:54 +00:00
static uint8_t led_inverted;
static eventmask_t emit_led (int on_time, int off_time)
2011-11-01 05:57:11 +00:00
{
2012-06-18 03:24:54 +00:00
eventmask_t m;
set_led (!led_inverted);
m = chEvtWaitOneTimeout (ALL_EVENTS, on_time);
set_led (led_inverted);
if (m) return m;
if ((m = chEvtWaitOneTimeout (ALL_EVENTS, off_time)))
return m;
return 0;
2011-11-01 05:57:11 +00:00
}
2012-06-18 03:24:54 +00:00
static eventmask_t display_status_code (void)
2011-11-01 05:57:11 +00:00
{
2012-01-20 09:18:23 +00:00
enum icc_state icc_state;
2012-06-18 03:24:54 +00:00
eventmask_t m;
2012-01-20 09:18:23 +00:00
if (icc_state_p == NULL)
icc_state = ICC_STATE_START;
else
icc_state = *icc_state_p;
2011-11-01 05:57:11 +00:00
if (icc_state == ICC_STATE_START)
2012-06-18 03:24:54 +00:00
return emit_led (LED_TIMEOUT_ONE, LED_TIMEOUT_STOP);
2011-11-01 05:57:11 +00:00
else
/* GPGthread running */
{
2012-06-18 03:24:54 +00:00
if ((m = emit_led ((auth_status & AC_ADMIN_AUTHORIZED)?
LED_TIMEOUT_ONE : LED_TIMEOUT_ZERO,
LED_TIMEOUT_INTERVAL)))
return m;
if ((m = emit_led ((auth_status & AC_OTHER_AUTHORIZED)?
LED_TIMEOUT_ONE : LED_TIMEOUT_ZERO,
LED_TIMEOUT_INTERVAL)))
return m;
if ((m = emit_led ((auth_status & AC_PSO_CDS_AUTHORIZED)?
LED_TIMEOUT_ONE : LED_TIMEOUT_ZERO,
LED_TIMEOUT_INTERVAL)))
return m;
2011-11-01 05:57:11 +00:00
if (icc_state == ICC_STATE_WAIT)
{
2012-06-18 03:24:54 +00:00
if ((m = chEvtWaitOneTimeout (ALL_EVENTS, LED_TIMEOUT_STOP * 2)))
return m;
2011-11-01 05:57:11 +00:00
}
else
{
2012-06-18 03:24:54 +00:00
if ((m = chEvtWaitOneTimeout (ALL_EVENTS, LED_TIMEOUT_INTERVAL)))
return m;
if ((m = emit_led (icc_state == ICC_STATE_RECEIVE?
LED_TIMEOUT_ONE : LED_TIMEOUT_ZERO,
LED_TIMEOUT_STOP)))
return m;
2011-11-01 05:57:11 +00:00
}
2012-06-18 03:24:54 +00:00
return 0;
2011-11-01 05:57:11 +00:00
}
}
void
led_blink (int spec)
{
2012-06-18 03:24:54 +00:00
chEvtSignal (main_thread, spec);
2011-11-01 05:57:11 +00:00
}
2012-05-23 05:55:04 +00:00
2010-08-10 03:11:02 +00:00
/*
2010-12-09 01:12:54 +00:00
* Entry point.
*
* NOTE: the main function is already a thread in the system on entry.
* See the hwinit1_common function.
2010-08-10 03:11:02 +00:00
*/
2010-08-30 02:39:10 +00:00
int
2012-05-18 07:54:17 +00:00
main (int argc, char *argv[])
2010-08-10 07:30:05 +00:00
{
2012-06-18 05:04:34 +00:00
unsigned int count = 0;
2010-08-10 03:11:02 +00:00
(void)argc;
(void)argv;
2011-11-01 05:57:11 +00:00
main_thread = chThdSelf ();
2011-05-16 01:14:09 +00:00
flash_unlock ();
2011-05-11 01:56:36 +00:00
device_initialize_once ();
2012-06-01 00:34:28 +00:00
usb_lld_init (Config_Descriptor.Descriptor[7]);
2011-10-06 07:56:08 +00:00
random_init ();
2010-08-10 03:11:02 +00:00
2012-05-11 23:06:33 +00:00
while (1)
{
if (bDeviceState != UNCONNECTED)
break;
chThdSleepMilliseconds (250);
}
2010-09-04 09:44:01 +00:00
#ifdef DEBUG
2010-08-10 07:30:05 +00:00
stdout_init ();
2010-08-10 03:11:02 +00:00
2010-08-10 07:30:05 +00:00
/*
* Creates 'stdout' thread.
*/
2010-12-08 06:10:41 +00:00
chThdCreateStatic (waSTDOUTthread, sizeof(waSTDOUTthread),
NORMALPRIO, STDOUTthread, NULL);
2010-09-03 15:42:36 +00:00
#endif
2010-08-10 07:30:05 +00:00
2010-12-08 06:10:41 +00:00
chThdCreateStatic (waUSBthread, sizeof(waUSBthread),
NORMALPRIO, USBthread, NULL);
2010-08-18 05:21:58 +00:00
2011-12-12 09:12:43 +00:00
#ifdef PINPAD_DND_SUPPORT
msc_init ();
#endif
2012-06-18 03:24:54 +00:00
2010-08-10 07:30:05 +00:00
while (1)
{
2011-11-01 05:57:11 +00:00
eventmask_t m;
2010-09-08 05:24:12 +00:00
2012-05-18 07:54:17 +00:00
if (icc_state_p != NULL && *icc_state_p == ICC_STATE_EXEC_REQUESTED)
break;
2012-06-18 03:24:54 +00:00
m = chEvtWaitOneTimeout (ALL_EVENTS, MAIN_TIMEOUT_INTERVAL);
got_it:
2012-06-18 05:04:34 +00:00
count++;
2011-11-01 05:57:11 +00:00
switch (m)
2010-12-09 01:12:54 +00:00
{
2012-06-18 03:24:54 +00:00
case LED_ONESHOT:
if ((m = emit_led (MS2ST (100), MAIN_TIMEOUT_INTERVAL))) goto got_it;
2011-11-01 05:57:11 +00:00
break;
2012-06-18 03:24:54 +00:00
case LED_TWOSHOTS:
if ((m = emit_led (MS2ST (50), MS2ST (50)))) goto got_it;
if ((m = emit_led (MS2ST (50), MAIN_TIMEOUT_INTERVAL))) goto got_it;
2011-11-01 05:57:11 +00:00
break;
2012-06-18 03:24:54 +00:00
case LED_SHOW_STATUS:
2012-06-18 05:12:00 +00:00
if ((count & 0x07) != 0) continue; /* Display once for eight times */
2012-06-18 03:24:54 +00:00
if ((m = display_status_code ())) goto got_it;
2011-11-01 05:57:11 +00:00
break;
2012-06-18 03:24:54 +00:00
case LED_START_COMMAND:
2010-12-09 01:12:54 +00:00
set_led (1);
2012-06-18 03:24:54 +00:00
led_inverted = 1;
break;
case LED_FINISH_COMMAND:
2012-06-18 03:44:37 +00:00
m = chEvtWaitOneTimeout (ALL_EVENTS, LED_TIMEOUT_STOP);
2012-06-18 03:24:54 +00:00
led_inverted = 0;
2010-12-09 01:12:54 +00:00
set_led (0);
2012-06-18 03:44:37 +00:00
if (m)
2012-06-18 03:24:54 +00:00
goto got_it;
2011-11-01 05:57:11 +00:00
break;
2012-06-18 03:24:54 +00:00
case LED_FATAL:
display_fatal_code ();
2011-11-01 05:57:11 +00:00
break;
default:
2012-06-18 03:24:54 +00:00
if ((m = emit_led (LED_TIMEOUT_ZERO, LED_TIMEOUT_STOP)))
goto got_it;
2011-11-01 05:57:11 +00:00
break;
2012-06-18 03:24:54 +00:00
}
2010-08-10 07:30:05 +00:00
2010-09-05 16:55:29 +00:00
#ifdef DEBUG_MORE
2010-12-09 01:12:54 +00:00
if (bDeviceState == CONFIGURED && (count % 10) == 0)
2010-08-10 08:47:55 +00:00
{
2010-12-09 01:12:54 +00:00
DEBUG_SHORT (count / 10);
2010-12-08 05:10:30 +00:00
_write ("\r\nThis is ChibiOS 2.0.8 on STM32.\r\n"
2010-08-10 08:47:55 +00:00
"Testing USB driver.\n\n"
2010-12-08 05:10:30 +00:00
"Hello world\r\n\r\n", 35+21+15);
2010-08-10 08:47:55 +00:00
}
2010-09-05 16:55:29 +00:00
#endif
2010-08-10 06:35:34 +00:00
}
2012-05-23 03:17:11 +00:00
set_led (1);
2012-05-18 07:54:17 +00:00
usb_lld_shutdown ();
2012-05-26 11:15:07 +00:00
/* Disable SysTick */
SysTick->CTRL = 0;
/* Disable all interrupts */
2012-05-18 17:05:31 +00:00
port_disable ();
2012-05-26 11:15:07 +00:00
/* Set vector */
2012-05-18 17:05:31 +00:00
SCB->VTOR = (uint32_t)&_regnual_start;
2012-05-29 01:07:23 +00:00
#ifdef DFU_SUPPORT
#define FLASH_SYS_START_ADDR 0x08000000
#define FLASH_SYS_END_ADDR (0x08000000+0x1000)
{
extern uint8_t _sys;
uint32_t addr;
handler *new_vector = (handler *)FLASH_SYS_START_ADDR;
void (*func) (void (*)(void)) = (void (*)(void (*)(void)))new_vector[10];
/* Kill DFU */
for (addr = FLASH_SYS_START_ADDR; addr < FLASH_SYS_END_ADDR;
addr += FLASH_PAGE_SIZE)
flash_erase_page (addr);
/* copy system service routines */
flash_write (FLASH_SYS_START_ADDR, &_sys, 0x1000);
2012-06-18 03:24:54 +00:00
/* Leave Gnuk to exec reGNUal */
2012-05-29 01:07:23 +00:00
(*func) (*((void (**)(void))(&_regnual_start+4)));
for (;;);
}
#else
2012-06-18 03:24:54 +00:00
/* Leave Gnuk to exec reGNUal */
flash_erase_all_and_exec (*((void (**)(void))(&_regnual_start+4)));
2012-05-29 01:07:23 +00:00
#endif
2012-05-18 17:05:31 +00:00
2012-05-23 05:55:04 +00:00
/* Never reached */
2010-08-10 03:11:02 +00:00
return 0;
}
2010-09-03 15:42:36 +00:00
void
2010-12-09 01:12:54 +00:00
fatal (uint8_t code)
2010-09-03 15:42:36 +00:00
{
2010-12-09 01:12:54 +00:00
fatal_code = code;
2012-06-18 03:24:54 +00:00
chEvtSignal (main_thread, LED_FATAL);
2010-09-03 15:42:36 +00:00
_write ("fatal\r\n", 7);
for (;;);
}