blob: 209c79f8320fff4ce8d1488b7ff155e7c48fe0a9 [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>
Kévin Redonad0958e2018-09-06 22:49:56 +02004 * (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Kévin Redon9a12d682018-07-08 13:21:16 +02005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Christina Quast32906bb2015-02-24 11:35:19 +010020/*------------------------------------------------------------------------------
21 * Headers
22 *------------------------------------------------------------------------------*/
23
24#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +010025#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +010026#include "utils.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);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200105 if (cfgnum < ARRAY_SIZE(config_func_ptrs)) {
106 simtrace_config = cfgnum;
107 } else {
108 TRACE_ERROR("trying to set out of bounds config %u\r\n", cfgnum);
109 }
Harald Welte8d6a5d82015-11-07 18:27:05 +0100110}
Christina Quast32906bb2015-02-24 11:35:19 +0100111
Harald Welte3bafe432016-03-20 16:43:12 +0100112void USART1_IrqHandler(void)
113{
Harald Welte2afd57f2017-11-28 22:52:56 +0100114 if (config_func_ptrs[simtrace_config].usart1_irq)
115 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100116}
117
118void USART0_IrqHandler(void)
119{
Harald Welte2afd57f2017-11-28 22:52:56 +0100120 if (config_func_ptrs[simtrace_config].usart0_irq)
121 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100122}
123
Harald Weltef9a182d2017-01-11 22:22:16 +0100124/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100125static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100126{
Harald Welteaa3b8672017-02-10 19:21:10 +0100127 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100128
129 if (!UART_IsRxReady())
130 return;
131
Harald Welteaa3b8672017-02-10 19:21:10 +0100132 ch = UART_GetChar();
Kévin Redon4f3a0352018-09-06 22:48:11 +0200133 /* We must echo the character to make python fdexpect happy, which we use in factory testing */
Harald Welte46893452018-07-03 22:30:30 +0200134 fputc(ch, stdout);
Harald Welteaa3b8672017-02-10 19:21:10 +0100135 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100136}
Harald Weltee974fbb2016-10-19 19:36:07 +0200137
Christina Quast32906bb2015-02-24 11:35:19 +0100138/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200139 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100140 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100141#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
142extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100143{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100144 uint8_t isUsbConnected = 0;
145 enum confNum last_simtrace_config = simtrace_config;
146 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100147
Harald Welteabba8a82017-03-06 16:58:00 +0100148 led_init();
149 led_blink(LED_RED, BLINK_3O_5F);
Christina Quast32906bb2015-02-24 11:35:19 +0100150
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200151 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100152 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200153 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
Christina Quast32906bb2015-02-24 11:35:19 +0100154
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100155 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100156
Harald Welte8ee15db2016-03-20 16:00:39 +0100157 EEFC_ReadUniqueID(g_unique_id);
158
Kévin Redon33d1eb72018-07-08 13:58:12 +0200159 printf("\n\r\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100160 "=============================================================================\n\r"
161 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
162 "=============================================================================\n\r");
Harald Welte2315e6b2016-03-19 21:37:55 +0100163
Kévin Redon738a04a2018-07-28 19:02:54 +0200164#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
Harald Weltec3941092018-08-26 09:53:13 +0200165 TRACE_INFO("Chip ID: 0x%08lx (Ext 0x%08lx)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
Harald Welteec0837c2017-03-03 01:33:24 +0100166 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welte8ee15db2016-03-20 16:00:39 +0100167 g_unique_id[0], g_unique_id[1],
168 g_unique_id[2], g_unique_id[3]);
Kévin Redon738a04a2018-07-28 19:02:54 +0200169 uint8_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
170 static const char* reset_causes[] = {
171 "general reset (first power-up reset)",
172 "backup reset (return from backup mode)",
173 "watchdog reset (watchdog fault occurred)",
174 "software reset (processor reset required by the software)",
175 "user reset (NRST pin detected low)",
176 };
177 if (reset_cause < ARRAY_SIZE(reset_causes)) {
178 TRACE_INFO("Reset Cause: %s\n\r", reset_causes[reset_cause]);
179 } else {
Harald Weltec3941092018-08-26 09:53:13 +0200180 TRACE_INFO("Reset Cause: 0x%lx\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
Kévin Redon738a04a2018-07-28 19:02:54 +0200181 }
182#endif
Harald Welte8ee15db2016-03-20 16:00:39 +0100183
Harald Welteaa3b8672017-02-10 19:21:10 +0100184 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100185
Harald Welteec0837c2017-03-03 01:33:24 +0100186 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100187 SIMtrace_USB_Initialize();
188
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100189 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100190 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100191 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200192#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100193 if (i >= MAX_USB_ITER * 3) {
194 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100195 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100196 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100197 NVIC_SystemReset();
198 }
Harald Welte67415e32016-09-01 20:57:56 +0200199#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100200 i++;
201 }
Christina Quastde688672015-04-12 15:20:57 +0200202
Harald Welteec0837c2017-03-03 01:33:24 +0100203 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100204 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
205 ++i) {
206 if (config_func_ptrs[i].configure)
207 config_func_ptrs[i].configure();
208 }
Christina Quast95d66162015-04-09 22:38:47 +0200209
Harald Welteec0837c2017-03-03 01:33:24 +0100210 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200211 if (config_func_ptrs[simtrace_config].init) {
212 config_func_ptrs[simtrace_config].init();
213 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100214 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200215
Harald Welteec0837c2017-03-03 01:33:24 +0100216 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100217 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100218 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100219#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100220 const char rotor[] = { '-', '\\', '|', '/' };
221 putchar('\b');
222 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100223#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100224 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100225 osmo_timers_prepare();
226 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100227
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100228 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100229
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100230 if (isUsbConnected) {
231 isUsbConnected = 0;
232 }
233 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100234 TRACE_INFO("USB is now configured\n\r");
Christina Quast1161b272015-02-25 14:15:57 +0100235
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100236 isUsbConnected = 1;
237 }
238 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100239 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100240 last_simtrace_config, simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200241 if (config_func_ptrs[last_simtrace_config].exit) {
242 config_func_ptrs[last_simtrace_config].exit();
243 }
244 if (config_func_ptrs[simtrace_config].init) {
245 config_func_ptrs[simtrace_config].init();
246 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100247 last_simtrace_config = simtrace_config;
248 } else {
Kévin Redon7d5d0112018-09-06 22:46:54 +0200249 if (config_func_ptrs[simtrace_config].run) {
250 config_func_ptrs[simtrace_config].run();
251 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100252 }
253 }
Christina Quast32906bb2015-02-24 11:35:19 +0100254}