blob: ae467941677a95dac47258f00f5ae0bce9776ae3 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 firmware card emulation 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"
Harald Welte9d90d282018-06-29 22:25:42 +020026#include <osmocom/core/timer.h>
Christina Quast32906bb2015-02-24 11:35:19 +010027
Harald Welted5252312017-02-27 20:34:20 +010028unsigned int g_unique_id[4];
Harald Welte8ee15db2016-03-20 16:00:39 +010029
Christina Quast32906bb2015-02-24 11:35:19 +010030/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010031 * Internal variables
32 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010033typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010034 /* static initialization, called whether or not the usb config is active */
35 void (*configure) (void);
36 /* initialization function after the config was selected */
37 void (*init) (void);
38 /* de-initialization before selecting new config */
39 void (*exit) (void);
40 /* main loop content for given configuration */
41 void (*run) (void);
Harald Welte3bafe432016-03-20 16:43:12 +010042 /* Interrupt handler for USART1 */
43 void (*usart0_irq) (void);
44 /* Interrupt handler for USART1 */
45 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010046} conf_func;
47
Harald Welted4c14212015-11-07 18:25:46 +010048static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010049 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010050#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010051 [CFG_NUM_SNIFF] = {
52 .configure = Sniffer_configure,
53 .init = Sniffer_init,
54 .exit = Sniffer_exit,
55 .run = Sniffer_run,
56 },
Harald Welte2fb59962016-02-28 12:34:26 +010057#endif
58#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010059 [CFG_NUM_CCID] = {
60 .configure = CCID_configure,
61 .init = CCID_init,
62 .exit = CCID_exit,
63 .run = CCID_run,
64 },
Harald Welte2fb59962016-02-28 12:34:26 +010065#endif
66#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010067 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010068 .configure = mode_cardemu_configure,
69 .init = mode_cardemu_init,
70 .exit = mode_cardemu_exit,
71 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010072 .usart0_irq = mode_cardemu_usart0_irq,
73 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010074 },
Harald Welte2fb59962016-02-28 12:34:26 +010075#endif
76#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010077 [CFG_NUM_MITM] = {
78 .configure = MITM_configure,
79 .init = MITM_init,
80 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010081 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010082 },
Harald Welte2fb59962016-02-28 12:34:26 +010083#endif
Christina Quastfb524b92015-02-27 13:39:45 +010084};
85
Christina Quastfb524b92015-02-27 13:39:45 +010086/*------------------------------------------------------------------------------
87 * Internal variables
88 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010089#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010090static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010091#elif defined(HAVE_CARDEM)
92static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
93#elif defined(HAVE_CCID)
94static volatile enum confNum simtrace_config = CFG_NUM_CCID;
95#endif
96
Harald Welte8d6a5d82015-11-07 18:27:05 +010097/*----------------------------------------------------------------------------
98 * Callbacks
99 *----------------------------------------------------------------------------*/
100
101void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
102{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100103 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
104 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +0100105}
Christina Quast32906bb2015-02-24 11:35:19 +0100106
Harald Welte3bafe432016-03-20 16:43:12 +0100107void USART1_IrqHandler(void)
108{
Harald Welte2afd57f2017-11-28 22:52:56 +0100109 if (config_func_ptrs[simtrace_config].usart1_irq)
110 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100111}
112
113void USART0_IrqHandler(void)
114{
Harald Welte2afd57f2017-11-28 22:52:56 +0100115 if (config_func_ptrs[simtrace_config].usart0_irq)
116 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100117}
118
Harald Weltef9a182d2017-01-11 22:22:16 +0100119/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100120static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100121{
Harald Welteaa3b8672017-02-10 19:21:10 +0100122 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100123
124 if (!UART_IsRxReady())
125 return;
126
Harald Welteaa3b8672017-02-10 19:21:10 +0100127 ch = UART_GetChar();
Harald Welte46893452018-07-03 22:30:30 +0200128 /* We must echo the character to make python fdexpect happy, whcih we use in factory testing */
129 fputc(ch, stdout);
Harald Welteaa3b8672017-02-10 19:21:10 +0100130
131 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100132}
Harald Weltee974fbb2016-10-19 19:36:07 +0200133
Christina Quast32906bb2015-02-24 11:35:19 +0100134/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200135 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100136 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100137#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
138extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100139{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100140 uint8_t isUsbConnected = 0;
141 enum confNum last_simtrace_config = simtrace_config;
142 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100143
Harald Welteabba8a82017-03-06 16:58:00 +0100144 led_init();
145 led_blink(LED_RED, BLINK_3O_5F);
Christina Quast32906bb2015-02-24 11:35:19 +0100146
Harald Welte45ebe452017-03-03 19:01:53 +0100147 /* Enable watchdog for 500ms, with no window */
148 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
149 (WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500));
Christina Quast32906bb2015-02-24 11:35:19 +0100150
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100151 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100152
Harald Welte8ee15db2016-03-20 16:00:39 +0100153 EEFC_ReadUniqueID(g_unique_id);
154
Kévin Redon33d1eb72018-07-08 13:58:12 +0200155 printf("\n\r\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100156 "=============================================================================\n\r"
157 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
158 "=============================================================================\n\r");
Harald Welte2315e6b2016-03-19 21:37:55 +0100159
Harald Welte7214b472017-03-03 19:02:09 +0100160 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
Harald Welteec0837c2017-03-03 01:33:24 +0100161 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welte8ee15db2016-03-20 16:00:39 +0100162 g_unique_id[0], g_unique_id[1],
163 g_unique_id[2], g_unique_id[3]);
Harald Welte7214b472017-03-03 19:02:09 +0100164 TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
Harald Welte8ee15db2016-03-20 16:00:39 +0100165
Harald Welteaa3b8672017-02-10 19:21:10 +0100166 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100167
Harald Welteec0837c2017-03-03 01:33:24 +0100168 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100169 SIMtrace_USB_Initialize();
170
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100171 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100172 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100173 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200174#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100175 if (i >= MAX_USB_ITER * 3) {
176 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100177 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100178 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100179 NVIC_SystemReset();
180 }
Harald Welte67415e32016-09-01 20:57:56 +0200181#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100182 i++;
183 }
Christina Quastde688672015-04-12 15:20:57 +0200184
Harald Welteec0837c2017-03-03 01:33:24 +0100185 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100186 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
187 ++i) {
188 if (config_func_ptrs[i].configure)
189 config_func_ptrs[i].configure();
190 }
Christina Quast95d66162015-04-09 22:38:47 +0200191
Harald Welteec0837c2017-03-03 01:33:24 +0100192 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100193 config_func_ptrs[simtrace_config].init();
194 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200195
Harald Welteec0837c2017-03-03 01:33:24 +0100196 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100197 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100198 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100199#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100200 const char rotor[] = { '-', '\\', '|', '/' };
201 putchar('\b');
202 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100203#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100204 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100205 osmo_timers_prepare();
206 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100207
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100208 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100209
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100210 if (isUsbConnected) {
211 isUsbConnected = 0;
212 }
213 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100214 TRACE_INFO("USB is now configured\n\r");
Christina Quast1161b272015-02-25 14:15:57 +0100215
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100216 isUsbConnected = 1;
217 }
218 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100219 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100220 last_simtrace_config, simtrace_config);
221 config_func_ptrs[last_simtrace_config].exit();
222 config_func_ptrs[simtrace_config].init();
223 last_simtrace_config = simtrace_config;
224 } else {
225 config_func_ptrs[simtrace_config].run();
226 }
227 }
Christina Quast32906bb2015-02-24 11:35:19 +0100228}