blob: ef24c3603dcfad18d957d70351571a9981a86ff3 [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);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200104 if (cfgnum < ARRAY_SIZE(config_func_ptrs)) {
105 simtrace_config = cfgnum;
106 } else {
107 TRACE_ERROR("trying to set out of bounds config %u\r\n", cfgnum);
108 }
Harald Welte8d6a5d82015-11-07 18:27:05 +0100109}
Christina Quast32906bb2015-02-24 11:35:19 +0100110
Harald Welte3bafe432016-03-20 16:43:12 +0100111void USART1_IrqHandler(void)
112{
Harald Welte2afd57f2017-11-28 22:52:56 +0100113 if (config_func_ptrs[simtrace_config].usart1_irq)
114 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100115}
116
117void USART0_IrqHandler(void)
118{
Harald Welte2afd57f2017-11-28 22:52:56 +0100119 if (config_func_ptrs[simtrace_config].usart0_irq)
120 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100121}
122
Harald Weltef9a182d2017-01-11 22:22:16 +0100123/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100124static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100125{
Harald Welteaa3b8672017-02-10 19:21:10 +0100126 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100127
128 if (!UART_IsRxReady())
129 return;
130
Harald Welteaa3b8672017-02-10 19:21:10 +0100131 ch = UART_GetChar();
Kévin Redon4f3a0352018-09-06 22:48:11 +0200132 /* We must echo the character to make python fdexpect happy, which we use in factory testing */
Harald Welte46893452018-07-03 22:30:30 +0200133 fputc(ch, stdout);
Harald Welteaa3b8672017-02-10 19:21:10 +0100134 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100135}
Harald Weltee974fbb2016-10-19 19:36:07 +0200136
Christina Quast32906bb2015-02-24 11:35:19 +0100137/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200138 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100139 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100140#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
141extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100142{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100143 uint8_t isUsbConnected = 0;
144 enum confNum last_simtrace_config = simtrace_config;
145 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100146
Harald Welteabba8a82017-03-06 16:58:00 +0100147 led_init();
148 led_blink(LED_RED, BLINK_3O_5F);
Christina Quast32906bb2015-02-24 11:35:19 +0100149
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200150 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100151 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redond8ebd6a2018-07-28 19:04:23 +0200152 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
Christina Quast32906bb2015-02-24 11:35:19 +0100153
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100154 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100155
Harald Welte8ee15db2016-03-20 16:00:39 +0100156 EEFC_ReadUniqueID(g_unique_id);
157
Kévin Redon33d1eb72018-07-08 13:58:12 +0200158 printf("\n\r\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100159 "=============================================================================\n\r"
160 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
161 "=============================================================================\n\r");
Harald Welte2315e6b2016-03-19 21:37:55 +0100162
Kévin Redon738a04a2018-07-28 19:02:54 +0200163#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
Harald Weltec3941092018-08-26 09:53:13 +0200164 TRACE_INFO("Chip ID: 0x%08lx (Ext 0x%08lx)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
Harald Welteec0837c2017-03-03 01:33:24 +0100165 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welte8ee15db2016-03-20 16:00:39 +0100166 g_unique_id[0], g_unique_id[1],
167 g_unique_id[2], g_unique_id[3]);
Kévin Redon738a04a2018-07-28 19:02:54 +0200168 uint8_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
169 static const char* reset_causes[] = {
170 "general reset (first power-up reset)",
171 "backup reset (return from backup mode)",
172 "watchdog reset (watchdog fault occurred)",
173 "software reset (processor reset required by the software)",
174 "user reset (NRST pin detected low)",
175 };
176 if (reset_cause < ARRAY_SIZE(reset_causes)) {
177 TRACE_INFO("Reset Cause: %s\n\r", reset_causes[reset_cause]);
178 } else {
Harald Weltec3941092018-08-26 09:53:13 +0200179 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 +0200180 }
181#endif
Harald Welte8ee15db2016-03-20 16:00:39 +0100182
Harald Welteaa3b8672017-02-10 19:21:10 +0100183 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100184
Harald Welteec0837c2017-03-03 01:33:24 +0100185 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100186 SIMtrace_USB_Initialize();
187
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100188 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100189 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100190 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200191#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100192 if (i >= MAX_USB_ITER * 3) {
193 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100194 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100195 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100196 NVIC_SystemReset();
197 }
Harald Welte67415e32016-09-01 20:57:56 +0200198#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100199 i++;
200 }
Christina Quastde688672015-04-12 15:20:57 +0200201
Harald Welteec0837c2017-03-03 01:33:24 +0100202 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
204 ++i) {
205 if (config_func_ptrs[i].configure)
206 config_func_ptrs[i].configure();
207 }
Christina Quast95d66162015-04-09 22:38:47 +0200208
Harald Welteec0837c2017-03-03 01:33:24 +0100209 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200210 if (config_func_ptrs[simtrace_config].init) {
211 config_func_ptrs[simtrace_config].init();
212 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100213 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200214
Harald Welteec0837c2017-03-03 01:33:24 +0100215 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100216 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100217 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100218#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100219 const char rotor[] = { '-', '\\', '|', '/' };
220 putchar('\b');
221 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100222#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100223 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100224 osmo_timers_prepare();
225 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100226
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100227 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100228
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100229 if (isUsbConnected) {
230 isUsbConnected = 0;
231 }
232 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100233 TRACE_INFO("USB is now configured\n\r");
Christina Quast1161b272015-02-25 14:15:57 +0100234
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100235 isUsbConnected = 1;
236 }
237 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100238 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100239 last_simtrace_config, simtrace_config);
Kévin Redon7d5d0112018-09-06 22:46:54 +0200240 if (config_func_ptrs[last_simtrace_config].exit) {
241 config_func_ptrs[last_simtrace_config].exit();
242 }
243 if (config_func_ptrs[simtrace_config].init) {
244 config_func_ptrs[simtrace_config].init();
245 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100246 last_simtrace_config = simtrace_config;
247 } else {
Kévin Redon7d5d0112018-09-06 22:46:54 +0200248 if (config_func_ptrs[simtrace_config].run) {
249 config_func_ptrs[simtrace_config].run();
250 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100251 }
252 }
Christina Quast32906bb2015-02-24 11:35:19 +0100253}