blob: b81abdd0cd72b8eb7915374358262df469c04068 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 firmware card emulation, CCID, and sniffer application
2 *
3 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Christina Quast32906bb2015-02-24 11:35:19 +010019/*------------------------------------------------------------------------------
20 * Headers
21 *------------------------------------------------------------------------------*/
22
23#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010024#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +010025#include "utils.h"
26#include "req_ctx.h"
Harald Welte9d90d282018-06-29 22:25:42 +020027#include <osmocom/core/timer.h>
Christina Quast32906bb2015-02-24 11:35:19 +010028
Harald Welted5252312017-02-27 20:34:20 +010029unsigned int g_unique_id[4];
Harald Welte8ee15db2016-03-20 16:00:39 +010030
Christina Quast32906bb2015-02-24 11:35:19 +010031/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010032 * Internal variables
33 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010034typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010035 /* static initialization, called whether or not the usb config is active */
36 void (*configure) (void);
37 /* initialization function after the config was selected */
38 void (*init) (void);
39 /* de-initialization before selecting new config */
40 void (*exit) (void);
41 /* main loop content for given configuration */
42 void (*run) (void);
Harald Welte3bafe432016-03-20 16:43:12 +010043 /* Interrupt handler for USART1 */
44 void (*usart0_irq) (void);
45 /* Interrupt handler for USART1 */
46 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010047} conf_func;
48
Harald Welted4c14212015-11-07 18:25:46 +010049static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010050 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010051#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010052 [CFG_NUM_SNIFF] = {
53 .configure = Sniffer_configure,
54 .init = Sniffer_init,
55 .exit = Sniffer_exit,
56 .run = Sniffer_run,
57 },
Harald Welte2fb59962016-02-28 12:34:26 +010058#endif
59#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010060 [CFG_NUM_CCID] = {
61 .configure = CCID_configure,
62 .init = CCID_init,
63 .exit = CCID_exit,
64 .run = CCID_run,
65 },
Harald Welte2fb59962016-02-28 12:34:26 +010066#endif
67#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010068 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010069 .configure = mode_cardemu_configure,
70 .init = mode_cardemu_init,
71 .exit = mode_cardemu_exit,
72 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010073 .usart0_irq = mode_cardemu_usart0_irq,
74 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010075 },
Harald Welte2fb59962016-02-28 12:34:26 +010076#endif
77#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010078 [CFG_NUM_MITM] = {
79 .configure = MITM_configure,
80 .init = MITM_init,
81 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010082 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010083 },
Harald Welte2fb59962016-02-28 12:34:26 +010084#endif
Christina Quastfb524b92015-02-27 13:39:45 +010085};
86
Christina Quastfb524b92015-02-27 13:39:45 +010087/*------------------------------------------------------------------------------
88 * Internal variables
89 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010090#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010091static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010092#elif defined(HAVE_CARDEM)
93static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
94#elif defined(HAVE_CCID)
95static volatile enum confNum simtrace_config = CFG_NUM_CCID;
96#endif
97
Harald Welte8d6a5d82015-11-07 18:27:05 +010098/*----------------------------------------------------------------------------
99 * Callbacks
100 *----------------------------------------------------------------------------*/
101
102void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
103{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100104 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
105 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +0100106}
Christina Quast32906bb2015-02-24 11:35:19 +0100107
Harald Welte3bafe432016-03-20 16:43:12 +0100108void USART1_IrqHandler(void)
109{
110 config_func_ptrs[simtrace_config].usart1_irq();
111}
112
113void USART0_IrqHandler(void)
114{
115 config_func_ptrs[simtrace_config].usart0_irq();
116}
117
Harald Weltef9a182d2017-01-11 22:22:16 +0100118/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100119static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100120{
Harald Welteaa3b8672017-02-10 19:21:10 +0100121 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100122
123 if (!UART_IsRxReady())
124 return;
125
Harald Welteaa3b8672017-02-10 19:21:10 +0100126 ch = UART_GetChar();
127
128 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100129}
Harald Weltee974fbb2016-10-19 19:36:07 +0200130
Christina Quast32906bb2015-02-24 11:35:19 +0100131/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200132 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100133 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100134#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
135extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100136{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100137 uint8_t isUsbConnected = 0;
138 enum confNum last_simtrace_config = simtrace_config;
139 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100140
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100141 LED_Configure(LED_NUM_RED);
142 LED_Configure(LED_NUM_GREEN);
143 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +0100144
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100145 /* Disable watchdog */
146 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +0100147
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100148 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100149
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100150 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100151
Harald Welte8ee15db2016-03-20 16:00:39 +0100152 EEFC_ReadUniqueID(g_unique_id);
153
Kévin Redon33d1eb72018-07-08 13:58:12 +0200154 printf("\r\n\r\n"
Harald Welte2315e6b2016-03-19 21:37:55 +0100155 "=============================================================================\r\n"
Harald Welted8a003d2017-02-27 20:31:09 +0100156 "SIMtrace2 firmware " GIT_REVISION " (C) 2010-2017 by Harald Welte\r\n"
Harald Welte2315e6b2016-03-19 21:37:55 +0100157 "=============================================================================\r\n");
158
Harald Welte8ee15db2016-03-20 16:00:39 +0100159 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
160 g_unique_id[0], g_unique_id[1],
161 g_unique_id[2], g_unique_id[3]);
162
Harald Welteaa3b8672017-02-10 19:21:10 +0100163 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100164
Harald Weltecf1c19a2016-03-20 15:18:18 +0100165 TRACE_INFO("USB init...\r\n");
Harald Weltee07aed62016-12-22 22:32:15 +0100166 SIMtrace_USB_Initialize();
167
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100168 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte006b16d2016-12-22 21:29:10 +0100169 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200170#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100171 if (i >= MAX_USB_ITER * 3) {
172 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100173 "not be configured)\r\n");
Harald Weltef415d712017-03-03 01:51:43 +0100174 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100175 NVIC_SystemReset();
176 }
Harald Welte67415e32016-09-01 20:57:56 +0200177#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100178 i++;
179 }
Christina Quastde688672015-04-12 15:20:57 +0200180
Harald Weltecf1c19a2016-03-20 15:18:18 +0100181 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100182 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
183 ++i) {
184 if (config_func_ptrs[i].configure)
185 config_func_ptrs[i].configure();
186 }
Christina Quast95d66162015-04-09 22:38:47 +0200187
Harald Weltecf1c19a2016-03-20 15:18:18 +0100188 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100189 config_func_ptrs[simtrace_config].init();
190 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200191
Harald Weltecf1c19a2016-03-20 15:18:18 +0100192 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100193 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100194#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100195 const char rotor[] = { '-', '\\', '|', '/' };
196 putchar('\b');
197 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100198#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100199 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100200 osmo_timers_prepare();
201 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100202
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100204
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100205 if (isUsbConnected) {
206 isUsbConnected = 0;
207 }
208 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100209 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100210 LED_Set(LED_NUM_GREEN);
211 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100212
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100213 isUsbConnected = 1;
214 }
215 if (last_simtrace_config != simtrace_config) {
216 TRACE_INFO("USB config chg %u -> %u\r\n",
217 last_simtrace_config, simtrace_config);
218 config_func_ptrs[last_simtrace_config].exit();
219 config_func_ptrs[simtrace_config].init();
220 last_simtrace_config = simtrace_config;
221 } else {
222 config_func_ptrs[simtrace_config].run();
223 }
224 }
Christina Quast32906bb2015-02-24 11:35:19 +0100225}